免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6314 | 回复: 5
打印 上一主题 下一主题

[C] raw socket, sendto() Invalid argument [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-06-26 17:36 |只看该作者 |倒序浏览
写了一个测试交换机的性能的工具, 运行时提示: Sendto: Invalid argument
大家帮忙看看。我是菜鸟。


  1. extern int                      quit_id, sockfd, ifindex;
  2. extern char                     *head[POOL_MAX], destmac[ETH_ALEN];
  3. extern struct sockaddr_in       toaddr;

  4. int
  5. send_hdr(char *head[])
  6. {
  7.         size_t buflen   = sizeof(struct ethhdr) + sizeof(struct iphdr) + sizeof(struct tcphdr);
  8.         size_t addrlen  = sizeof(struct sockaddr_ll);
  9.         int     i, k;
  10.         struct sockaddr_ll toif;
  11.         

  12.         toif.sll_family         = PF_PACKET;
  13.         toif.sll_protocol       = htons(ETH_P_IP);
  14.         toif.sll_ifindex        = ifindex;
  15.         toif.sll_hatype         = ARPHRD_ETHER;
  16.         toif.sll_pkttype        = PACKET_OTHERHOST;
  17.         toif.sll_halen          = ETH_ALEN;

  18.         memset(toif.sll_addr, 0, 8);
  19.         memcpy(toif.sll_addr, destmac, ETH_ALEN);
  20.         while(1) {
  21.                 i = get_rand_n(POOL_MAX);
  22.                
  23.                 if (sendto(sockfd, head[i], buflen, 0, (struct sockaddr *)&toif, addrlen) < 0) {
  24.                         perror("Sendto");
  25.                 }
  26.                 if (quit_id == 1) {
  27.                         goto over;
  28.                 }
  29.         }
  30. over:
  31.         return(0);
  32. }

  33. int
  34. build_hdr(char *head[], struct sockaddr_in *to)
  35. {
  36.         int             i, k;
  37.         struct ethhdr   *ethh;
  38.         struct iphdr    *iph;
  39.         struct tcphdr   *tcph;
  40.         size_t          ethhlen, iphlen, tcphlen, buf_size;
  41.         ethhlen  = sizeof(struct ethhdr);
  42.         iphlen   = sizeof(struct iphdr);
  43.         tcphlen  = sizeof(struct tcphdr);
  44.         buf_size = ethhlen + iphlen + tcphlen;
  45.         unsigned char srcmac[ETH_ALEN];

  46.         rand_init();

  47.         for (i = 0; i < POOL_MAX; i ++) {
  48.                 head[i] = (char *)malloc(buf_size);
  49.                 if (head[i] == NULL) {
  50.                         return(1);
  51.                 }

  52.                 ethh = (struct ethhdr *)head[i];
  53.                 iph  = (struct iphdr *)(head[i] + ethhlen);
  54.                 tcph = (struct tcphdr *)(head[i] + ethhlen + iphlen);

  55.                 /* build ethernet header */

  56.                 for (k = 0; k < ETH_ALEN; k ++) {
  57.                         srcmac[k] = get_rand_uint8();
  58.                 }

  59.                 memcpy(ethh -> h_dest, destmac, ETH_ALEN);
  60.                 memcpy(ethh -> h_source, srcmac, ETH_ALEN);
  61.                 ethh -> h_proto = 0x0800;

  62.                 /* build ip header */
  63.                 iph -> version  = 4;
  64.                 iph -> ihl      = 5; /* min valid ip header length in 32bits word; 5*4 Byte = 20 Byte */
  65.                 iph -> tos      = 0;
  66.                 iph -> tot_len  = htons(iphlen + tcphlen); /* ip packet total length */
  67.                 iph -> id       = get_rand_uint16();
  68.                 iph -> frag_off = 0;
  69.                 iph -> ttl      = get_rand_uint8();
  70.                 iph -> protocol = IPPROTO_TCP;
  71.                 iph -> check    = 0;
  72.                 iph -> saddr    = get_rand_uint32(); /* spoof source address */
  73.                 iph -> daddr    = (to -> sin_addr).s_addr;

  74.                 /* build tcp header */
  75.                 tcph -> source  = get_rand_uint16(); /* spoof source port */
  76.                 tcph -> dest    = to -> sin_port;
  77.                 tcph -> seq     = htonl(get_rand_uint32() );
  78.                 tcph -> ack_seq = 0;
  79.                 tcph -> doff    = sizeof(struct tcphdr) / 4; /* measured in 32bits word */
  80.                 tcph -> res1    = 0;
  81.                 tcph -> res2    = 0;
  82.                 tcph -> urg     = 0;
  83.                 tcph -> ack     = 0;
  84.                 tcph -> psh     = 0;
  85.                 tcph -> syn     = 1;
  86.                 tcph -> fin     = 0;
  87.                 tcph -> rst     = 0;
  88.                 tcph -> window  = htonl(get_rand_uint16() );
  89.                 tcph -> urg_ptr = 0;
  90.                 tcph -> check   = tcp_checksum( iph -> saddr,
  91.                                                 iph -> daddr,
  92.                                                 (unsigned short *)tcph,
  93.                                                 tcphlen);
  94.         }
  95. }
复制代码





  1. int
  2. main(int argc, char **argv)
  3. {
  4.         ...
  5.         if ((sockfd = socket(PF_INET, SOCK_RAW, htons(ETH_P_ALL))) < 0 ) {
  6.                 perror("Socket");
  7.                 return(1);
  8.         }

  9.         if (ioctl(sockfd, SIOCGIFINDEX, &ifr ) < 0) {
  10.                 perror("Ioctl");
  11.                 return(1);
  12.         }

  13.         
  14.         build_hdr(head, &toaddr);

  15.       
  16.         send_hdr(head);
  17.          ...
  18. }

复制代码

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
2 [报告]
发表于 2008-06-26 20:44 |只看该作者
如果是sendto的参数不对,楼主可以确定一下sendto的具体参数的使用啊。

论坛徽章:
0
3 [报告]
发表于 2008-06-27 09:42 |只看该作者
#include <sys/socket.h>

       ssize_t sendto(int socket, const void *message, size_t length,
              int flags, const struct sockaddr *dest_addr,
              socklen_t dest_len);

这样用不知道哪错了:

  1. size_t buflen = sizeof(struct ethhdr) + sizeof(struct iphdr) + sizeof(struct tcphdr);
  2. size_t addrlen = sizeof(struct sockaddr_ll);
  3. struct sockaddr_ll toif;

  4. sendto(sockfd, head[i], buflen, 0, (struct sockaddr *)&toif, addrlen);

复制代码


请大侠们指点。

[ 本帖最后由 KLL 于 2008-6-27 09:44 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2008-06-27 09:47 |只看该作者
恕小弟愚昧
我找了三天了,google 了,在咱们的论坛里也搜了,还是毫无头绪。

论坛徽章:
0
5 [报告]
发表于 2008-06-27 10:20 |只看该作者
socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)

论坛徽章:
0
6 [报告]
发表于 2008-10-30 11:00 |只看该作者
可能的原因是,你的ip地址可能不对。之前遇到过一个这样的问题,ip地址是随机生成的,可能会随机到240以上的网段或者是0网段,这些地址都是无效的,就会报INVALID ARGUMENT错误。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP