- 论坛徽章:
- 0
|
socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
没有设置IP_HDRINCL;
文档说不设置IP_HDRINCL.就可以让系统帮忙处理ip头了,所以我只制造了tcp头,
发的时候也发了,但是没发到对方机器,
本机抓到了发的包,但是tcpdump显示:
"tcp 120 [bad hdr length 0 - too short, < 20]"
可是tcp头里没有设置长度的地方,只有ip头里才有设置包长的地方,
那么不还是要设置ip头?
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <netinet/in.h>
- #include <netinet/ip.h>
- #define __FAVOR_BSD
- #include <netinet/tcp.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <arpa/inet.h>
- #include <errno.h>
- #include <string.h>
- int main(void) {
- int i, count = 10;
- char *ip = "192.168.0.2";
- unsigned short port = 21;
- int sock;
- struct sockaddr_in daddr;
- char data[2048];
- struct tcphdr *tcph = (struct tcphdr *)data;
- // build daddr;
- memset(&daddr, 0, sizeof(struct sockaddr_in));
- daddr.sin_family = AF_INET;
- daddr.sin_port = htons(port);
- daddr.sin_addr.s_addr = inet_addr(ip);
- sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
- if (sock < 0) {
- perror("sock");
- exit(1);
- }
- memset(data, 0, 2048);
- // build tcp header
- tcph->th_sport = htons(1024);
- tcph->th_dport = htons(port);
- tcph->th_seq = 123456789;
- tcph->th_ack = 0;
- tcph->th_x2 = 0;
- tcph->th_off = 0;
- tcph->th_flags = TH_SYN;
- tcph->th_win = htonl(65535);
- tcph->th_sum = 0;
- tcph->th_urp = 0;
- for(i = 0; i < count; i++) {
- if(sendto(sock, data, /*sizeof(data)*/ sizeof(struct tcphdr)+100, 0, (struct sockaddr *) &daddr, sizeof(daddr)) < 0)
- perror("sendto");
- else
- printf(".");
- fflush(stdout);
- }
- return(0);
- }
复制代码 |
|