- 論壇徽章:
- 0
|
socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
沒有設(shè)置IP_HDRINCL;
文檔說不設(shè)置IP_HDRINCL.就可以讓系統(tǒng)幫忙處理ip頭了,所以我只制造了tcp頭,
發(fā)的時候也發(fā)了,但是沒發(fā)到對方機器,
本機抓到了發(fā)的包,但是tcpdump顯示:
"tcp 120 [bad hdr length 0 - too short, < 20]"
可是tcp頭里沒有設(shè)置長度的地方,只有ip頭里才有設(shè)置包長的地方,
那么不還是要設(shè)置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);
- }
復(fù)制代碼 |
|