免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2272 | 回复: 1

raw socket里能不能只自己制造tcp头而不制造ip头? [复制链接]

论坛徽章:
0
发表于 2008-03-05 14:15 |显示全部楼层
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头?


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <netinet/in.h>
  5. #include <netinet/ip.h>
  6. #define __FAVOR_BSD
  7. #include <netinet/tcp.h>
  8. #include <sys/socket.h>
  9. #include <sys/types.h>
  10. #include <arpa/inet.h>
  11. #include <errno.h>
  12. #include <string.h>


  13. int main(void) {
  14.   int i, count = 10;
  15.   char *ip = "192.168.0.2";
  16.   unsigned short port = 21;
  17.   int sock;
  18.   struct sockaddr_in daddr;

  19.   char data[2048];
  20.   struct tcphdr *tcph = (struct tcphdr *)data;

  21.   // build daddr;
  22.   memset(&daddr, 0, sizeof(struct sockaddr_in));
  23.   daddr.sin_family = AF_INET;
  24.   daddr.sin_port = htons(port);
  25.   daddr.sin_addr.s_addr = inet_addr(ip);

  26.   sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
  27.   if (sock < 0) {
  28.     perror("sock");
  29.     exit(1);
  30.   }
  31.   memset(data, 0, 2048);

  32.   // build tcp header
  33.   tcph->th_sport = htons(1024);
  34.   tcph->th_dport = htons(port);
  35.   tcph->th_seq = 123456789;
  36.   tcph->th_ack = 0;
  37.   tcph->th_x2 = 0;
  38.   tcph->th_off = 0;
  39.   tcph->th_flags = TH_SYN;
  40.   tcph->th_win = htonl(65535);
  41.   tcph->th_sum = 0;
  42.   tcph->th_urp = 0;

  43.   for(i = 0; i < count; i++) {
  44.     if(sendto(sock, data, /*sizeof(data)*/ sizeof(struct tcphdr)+100, 0, (struct sockaddr *) &daddr, sizeof(daddr)) < 0)
  45.       perror("sendto");
  46.     else
  47.       printf(".");
  48.     fflush(stdout);
  49.   }
  50.   return(0);
  51. }

复制代码

论坛徽章:
0
发表于 2008-03-05 15:47 |显示全部楼层
错在哪呢? 自己制造ip头也是出这样的错,

我见有的人的代码里有这样一个伪头

  1.         struct pseudohdr  {
  2.            unsigned long saddr;
  3.            unsigned long daddr;
  4.            char useless;
  5.            unsigned char protocol;
  6.            unsigned int tcplength;
  7.         };

复制代码


有注释说是为了计算cksum时用到,为什么呢?

ip头结构, netinet/ip.h里早就定义好了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP