gpt4 book ai didi

c++ - 制作 TCP/IP 数据包

转载 作者:太空宇宙 更新时间:2023-11-04 08:07:09 25 4
gpt4 key购买 nike

我正在研究使用 C/C++ 进行套接字编程,我认为最好的方法是深入研究它。我可以使用 socket.h send() 将数据发送到套接字,因此想通过制作网络数据包来更深入。

我试过了,但仍然无法弄清楚我的数据的哪一部分无效,因为我收到无效参数 errno 22。这是我的十六进制 IP header :

45 00 28 00 
d4 31 00 00
ff 06 3c 6e
c0 a8 01 06
c0 a8 01 01

这是我的 TCP header :

00 50 00 50 
00 00 00 00
00 00 00 00
50 02 16 d0
15 1b 00 00

我很感激任何提示。

注意:我正在阅读 beej.ushere为了我的学习。

编辑:这是我的代码:

struct pseudo_header {
u_int32_t source_address;
u_int32_t dest_address;
u_int8_t placeholder;
u_int8_t protocol;
u_int16_t tcp_length;
};

int main(int argc, char* argv[]) {
int sockfd = socket (PF_INET, SOCK_RAW, IPPROTO_TCP);
if (sockfd == -1) {
perror("Failed to create socket");
exit(1);
}

// Datagram to represent the packet
char datagram[4096];
memset(datagram, 0, 4096); // zero out the packet buffer

//Data part
char *data = datagram + sizeof(struct ip) + sizeof(struct tcphdr);
strcpy(data, "");

// some address resolution
char source_ip[32];
strcpy(source_ip, "192.168.1.6");
struct sockaddr_in sai;
sai.sin_family = AF_INET;
sai.sin_port = htons(80);

sai.sin_addr.s_addr = inet_addr("192.168.1.1");
cout << "sai.sin_addr.s_addr=" << sai.sin_addr.s_addr << endl;

//Fill in the IP Header
struct ip *iph = (struct ip *) datagram;
iph->ip_hl = 5;
iph->ip_v = 4;
iph->ip_tos = 0;
iph->ip_len = sizeof(struct ip) + sizeof(struct tcphdr) + strlen(data);
iph->ip_id = htons(54321);
iph->ip_off = 0;
iph->ip_ttl = 255;
iph->ip_p = IPPROTO_TCP;
iph->ip_sum = 0;
iph->ip_src.s_addr = inet_addr(source_ip);
iph->ip_dst.s_addr = sai.sin_addr.s_addr;

//Ip checksum
unsigned short checksum = csum((unsigned short *) datagram, iph->ip_len);
iph->ip_sum = checksum;
cout << "iph->ip_sum=" << checksum << endl;

unsigned char *pIph = (unsigned char *) datagram;
for (int i = 0; i < 20; i++) {
cout << setfill('0') << setw(2) << hex << (int) pIph[i] << " ";
if (i + 1 >= 4 && (i + 1) % 4 == 0) {
cout << endl;
}
}

//TCP Header
struct tcphdr *tcph = (struct tcphdr *) (datagram + sizeof(struct ip));
struct pseudo_header psh;
tcph->th_sport = htons(80);
tcph->th_dport = htons(80);
tcph->th_seq = 0;
tcph->th_ack = 0;
tcph->th_off = 5;
tcph->th_flags = TH_SYN;
tcph->th_win = htons(5840); /* maximum allowed window size */
tcph->th_sum = 0;
tcph->th_urp = 0;

//Now the TCP checksum
psh.source_address = inet_addr(source_ip);
psh.dest_address = sai.sin_addr.s_addr;
psh.placeholder = 0;
psh.protocol = IPPROTO_TCP;
psh.tcp_length = htons(sizeof(struct tcphdr) + strlen(data));

int psize = sizeof(struct pseudo_header) +
sizeof(struct tcphdr) +
strlen(data);

char *pseudogram = malloc(psize);

memcpy(pseudogram, (char*) &psh, sizeof(struct pseudo_header));
memcpy(pseudogram + sizeof(struct pseudo_header), tcph, sizeof(struct tcphdr) + strlen(data));

checksum = csum((unsigned short*) pseudogram, psize);
tcph->th_sum = checksum;
cout << "tcph->th_sum=" << checksum << endl;

unsigned char *pTcph = (unsigned char *) tcph;
for (int i = 0; i < 20; i++) {
cout << setfill('0') << setw(2) << hex << (int) pTcph[i] << " ";
if (i + 1 >= 4 && (i + 1) % 4 == 0) {
cout << endl;
}
}

//IP_HDRINCL to tell the kernel that headers are included in the packet
int one = 1;
const int *val = &one;
if (setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0) {
perror("Error setting IP_HDRINCL");
exit(0);
}

struct sockaddr *pSa = (struct sockaddr *) &sai;

// Send the packet
if (sendto(sockfd, datagram, iph->ip_len, 0, pSa, sizeof(sai)) < 0) { // failed here
perror("sendto failed");

} else { //Data send successfully
printf("Packet Send. Length : %d \n", iph->ip_len);
}

return 1;
}

最佳答案

在您的 IPv4 header 中:

45 00[28 00] 
d4 31 00 00
ff 06 3c 6e
c0 a8 01 06
c0 a8 01 01

你的包长度是10240(0x2800)吗?

你确定它不是 40 (0x0028) 吗?

45 00[00 28] 
d4 31 00 00
ff 06[64 46] // checksum updated
c0 a8 01 06
c0 a8 01 01

编辑:既然您发布了代码……

你应该替换:

iph->ip_len = sizeof(struct ip) + sizeof(struct tcphdr) + strlen(data);

通过:

iph->ip_len = htons(sizeof(struct ip) + sizeof(struct tcphdr) + strlen(data));

关于c++ - 制作 TCP/IP 数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42004374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com