C++編寫自定義TCP包併傳送
轉載修改自:
https://blog.csdn.net/e_wsq/article/details/12766443
#include "pch.h"
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <time.h>
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#pragma comment(lib,"ws2_32.lib")
#pragma warning (disable:4996) //去除安全方法提示
#define IPVER 4
#define MAX_BUFF_LEN 65500
typedef struct ip_hdr
{
UCHAR h_verlen;
UCHAR tos;
USHORT total_len;
USHORT ident;
USHORT frag_and_flags;
UCHAR ttl;
UCHAR proto;
USHORT checksum;
ULONG sourceIP;
ULONG destIP;
}IP_HEADER;
typedef struct tsd_hdr
{
ULONG saddr;
ULONG daddr;
UCHAR mbz;
UCHAR ptcl;
USHORT tcpl;
}PSD_HEADER;
typedef struct tcp_hdr
{
USHORT th_sport;
USHORT th_dport;
ULONG th_seq;
ULONG th_ack;
UCHAR th_lenres;
UCHAR th_flag;
USHORT th_win;
USHORT th_sum;
USHORT th_urp;
}TCP_HEADER;
USHORT checksum(USHORT *buffer, int size)
{
unsigned long cksum = 0;
while (size > 1)
{
cksum += *buffer++;
size -= sizeof(USHORT);
}
if (size)
{
cksum += *(UCHAR*)buffer;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >> 16);
return (USHORT)(~cksum);
}
int main(int argc, char* argv[])
{
WSADATA WSAData;
SOCKET sock;
IP_HEADER ipHeader;
TCP_HEADER tcpHeader;
PSD_HEADER psdHeader;
char Sendto_Buff[MAX_BUFF_LEN];
unsigned short check_Buff[MAX_BUFF_LEN];
const char tcp_send_data[] = { "This is TCP data!" };
BOOL flag;
int rect, nTimeOver;
if (argc != 5)
{
printf("Useage: SendTcp soruce_ip source_port dest_ip dest_port \n");
return false;
}
if (WSAStartup(MAKEWORD(2, 2), &WSAData) != 0)
{
printf("WSAStartup Error!\n");
return false;
}
if ((sock = WSASocket
(AF_INET, SOCK_RAW, IPPROTO_IP, NULL, 0, WSA_FLAG_OVERLAPPED))
== INVALID_SOCKET)
{
printf("Socket Setup Error!\n");
return false;
}
flag = true;
if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char *)&flag, sizeof
(flag)) == SOCKET_ERROR)
{
printf("setsockopt IP_HDRINCL error!\n");
return false;
}
nTimeOver = 1000;
if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char*)&nTimeOver, sizeof(nTimeOver)) == SOCKET_ERROR)
{
printf("setsockopt SO_SNDTIMEO error!\n");
return false;
}
ipHeader.h_verlen = (IPVER << 4 | sizeof(ipHeader) / sizeof(unsigned long));
ipHeader.tos = (UCHAR)0;
ipHeader.total_len = htons((unsigned short)sizeof(ipHeader) + sizeof
(tcpHeader)+sizeof(tcp_send_data));
ipHeader.ident = 0;
ipHeader.frag_and_flags = 0;
ipHeader.ttl = 128;
ipHeader.proto = IPPROTO_TCP;
ipHeader.checksum = 0;
ipHeader.sourceIP = inet_addr(argv[1]);
ipHeader.destIP = inet_addr(argv[3]);
memset(check_Buff, 0, MAX_BUFF_LEN);
memcpy(check_Buff, &ipHeader, sizeof(IP_HEADER));
ipHeader.checksum = checksum(check_Buff, sizeof(IP_HEADER));
psdHeader.saddr = ipHeader.sourceIP;
psdHeader.daddr = ipHeader.destIP;
psdHeader.mbz = 0;
psdHeader.ptcl = ipHeader.proto;
psdHeader.tcpl = htons(sizeof(TCP_HEADER) + sizeof(tcp_send_data));
tcpHeader.th_dport = htons(atoi(argv[4]));
tcpHeader.th_sport = htons(atoi(argv[2]));
tcpHeader.th_seq = 0;
tcpHeader.th_ack = 0;
tcpHeader.th_lenres = (sizeof(tcpHeader) / sizeof(unsigned long) << 4 | 0);
tcpHeader.th_flag = 2;
tcpHeader.th_win = htons((unsigned short)16384);
tcpHeader.th_urp = 0;
tcpHeader.th_sum = 0;
memset(check_Buff, 0, MAX_BUFF_LEN);
memcpy(check_Buff, &psdHeader, sizeof(psdHeader));
memcpy(check_Buff + sizeof(psdHeader), &tcpHeader, sizeof(tcpHeader));
memcpy(check_Buff + sizeof(PSD_HEADER) + sizeof
(TCP_HEADER), tcp_send_data, sizeof(tcp_send_data));
tcpHeader.th_sum = checksum(check_Buff, sizeof(PSD_HEADER) + sizeof
(TCP_HEADER)+sizeof(tcp_send_data));
memset(Sendto_Buff, 0, MAX_BUFF_LEN);
memcpy(Sendto_Buff, &ipHeader, sizeof(IP_HEADER));
memcpy(Sendto_Buff + sizeof(IP_HEADER), &tcpHeader, sizeof
(TCP_HEADER));
memcpy(Sendto_Buff + sizeof(IP_HEADER) + sizeof
(TCP_HEADER), tcp_send_data, sizeof(tcp_send_data));
int datasize = sizeof(IP_HEADER) + sizeof(TCP_HEADER) + sizeof
(tcp_send_data);
SOCKADDR_IN dest;
memset(&dest, 0, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_addr.s_addr = inet_addr(argv[3]);
rect = sendto(sock, Sendto_Buff, datasize, 0, (struct sockaddr*)&dest,
sizeof(dest));
if (rect == SOCKET_ERROR)
{
printf("send error!:%d\n", WSAGetLastError());
return false;
}
else
printf("send ok!\n");
closesocket(sock);
WSACleanup();
return 0;
}
相關文章
- 【測試】echo傳送和接收TCP/UDP資料包|shell 傳送TCP/UDP資料包TCPUDP
- 基於 WebRTC 實現自定義編碼解析度傳送Web
- PostgreSQL編寫自定義extensionSQL
- WIN32傳送自定義訊息Win32
- C++ - tcp網路傳輸如何傳送結構體型別C++TCP結構體型別
- JAVA 資料寫入excel併傳送郵件JavaExcel
- C/C++ 運用Npcap傳送UDP資料包C++PCAUDP
- 從零開始系列-Laravel編寫api服務介面:11.自定義包LaravelAPI
- 0230-TCP 傳送和接收TCP
- qt傳送自定義signal,直接呼叫也可以,不使用emitQTMIT
- liunx通過TCP傳送資訊TCP
- 如何為 Cloud TPU 編寫自定義估算器模型Cloud模型
- 編寫自定義 Laravel 擴充套件包測試用例,phpunit 錯誤提示 “class not found”Laravel套件PHP
- Excel 開始支援使用 JavaScript 編寫自定義函式ExcelJavaScript函式
- Laravel SMS 簡訊傳送包Laravel
- Qt編寫自定義控制元件屬性設計器QT控制元件
- 如何編寫一個前端框架之六-自定義元素(譯)前端框架
- Golang 編寫 Tcp 伺服器GolangTCP伺服器
- 【Grafana】告警配置併傳送郵件Grafana
- tableau自定義地理編碼
- 彙編--串傳送指令 MOVS
- 使用自定義 HTTP Interceptor 記錄 SAP Spartacus 傳送的 OCC API 以及響應HTTPAPI
- nGrinder中快速編寫groovy指令碼04-傳送POST請求指令碼
- Qt - TCP網路傳輸如何傳送結構體型別QTTCP結構體型別
- 併發程式設計:自定義併發類:自定義非同步流(釋出與訂閱)程式設計非同步
- 如何確保TCP包的有序傳輸?TCP
- Rust引用自定義c/c++庫RustC++
- Python3實現自動傳送MySql查詢併傳送郵件PythonMySql
- 使用gitlab ci構建IOS包併傳送通知訊息到企業微信GitlabiOS
- iOS 環信 EaseUI 的使用一:可傳送類似名片自定義的訊息iOSUI
- [C++] 自定義C++比較器比較大小C++
- 十分鐘瞭解eslint配置 && 編寫自定義eslint規則EsLint
- EFCore3.1+編寫自定義的EF.Functions擴充套件方法Function套件
- 手寫一個自定義PromisePromise
- Android之Mina頻繁傳送心跳包Android
- 測試 iris 時自定義 context 包Context
- 自定義 Composer 包 dome 小測試
- C++:使自定義類支援迭代器C++