免费注册 查看新帖 |

Chinaunix

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

[SCO UNIX] 设置混杂模式 ioctl [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-06-13 17:11 |只看该作者 |倒序浏览
想设置网卡为混杂模式,代码如下:

struct strioctl siotcl;
siotcl.ic_cmd = MACIOC(11);
  siotcl.ic_timout = 0;
  siotcl.ic_len = 0;
  siotcl.ic_dp = NULL;
//  siotcl.ic_cmd = mibufed;
  
  fd = open("/dev/mdi/e3A0",O_RDWR);
  res = ioctl(fd,I_STR,&siotcl);
  if(res == -1)
  {
          perror("ioctl failed");
  }
  else
  {
          printf("ioctl success\n");
  }

但是运行后总出现错误,错误如下:
ioctl failed: Invalid argument.

请高手帮帮忙,看怎么写代码才正确。。我现在对ioctl也不是太了解。

论坛徽章:
0
2 [报告]
发表于 2008-06-16 14:40 |只看该作者
没人回复???

论坛徽章:
0
3 [报告]
发表于 2008-06-17 14:41 |只看该作者
开发的问题,需要sun兄出来解答一下

论坛徽章:
0
4 [报告]
发表于 2008-06-18 10:48 |只看该作者
谁是SUN兄,快快出来解决一下。。。。。

论坛徽章:
0
5 [报告]
发表于 2008-06-18 11:10 |只看该作者
原帖由 sufei7099 于 2008-6-18 10:48 发表
谁是SUN兄,快快出来解决一下。。。。。

版主,估计这两天没上来

论坛徽章:
0
6 [报告]
发表于 2008-06-18 11:38 |只看该作者
急等。。。。。。
最近又发现在了一个设置SCO 网卡混杂模式的方法,并且可以抓到数据。但是实现起来也有问题,如下:
int Set_Promisc(char *interface, int sock )
{
                struct ifreq ifr;
                printf("strnlen(interface)+1 : %d\n",strlen(interface)+1);
                strncpy(ifr.ifr_name, interface,strlen(interface)+1);
                printf("ifr.ifr_name : %s\n",ifr.ifr_name);
                if((ioctl(sock, SIOCGIFFLAGS, &ifr) == -1))
                {
                        /*Could not retrieve flags for the interface*/
                        perror("Could not retrive flags for the interface");
                        exit(0);
                }
                printf("The interface is ::: %s\n", interface);
                printf("Retrieved flags from interface successfully\n");
                ifr.ifr_flags |= IFF_LINK0;
                if (ioctl (sock, SIOCSIFFLAGS, &ifr) == -1 )
                {
                        /*Could not set the flags on the interface */
                        perror("Could not set the PROMISC flag:");
                        exit(0);
                }
                printf("Setting interface ::: %s ::: to promisc\n", interface);
                return(0);
}

在main函数中:
int main()
{
         int sock, bytes_recieved, fromlen;
   char buffer[65535];
   struct sockaddr_in from;
   struct ip *ip;
   struct tcp *tcp;
   sock = Open_Raw_Socket();
//   set_all_promisc();
         Set_Promisc("net1", sock);
         
         while(1)
   {
                        fromlen = sizeof(from);
                        printf("before recvfrom\n");
                        bytes_recieved = recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&from, &fromlen);
                        printf("\nBytes received ::: %5d\n",bytes_recieved);
                        printf("Source address ::: %s\n",inet_ntoa(from.sin_addr));
                        ip = (struct ip *)buffer;
                        /*See if this is a TCP packet*/
                        if(ip->ip_protocol == 6)
                        {
                                        printf("IP header length ::: %d\n",ip->ip_length);
                                        printf("Protocol ::: %d\n",ip->ip_protocol);
                                        tcp = (struct tcp *)(buffer + (4*ip->ip_length));
                                        printf("Source port ::: %d\n",ntohs(tcp->tcp_source_port));
                                        printf("Dest port ::: %d\n",ntohs(tcp->tcp_dest_port));
                  }

                }//end for while
}
当程序执行到recvfrom时,就停止不动了。不知道问题在哪里,也请大家帮忙看一看。

另外,我把程序移植到linux中,把AF_INET改成PF_PACKET后,就成功了,在linux上可以跑起来。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP