免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 9130 | 回复: 14

[函数] 关于ioctl函数,麻烦高手看看,急!!! [复制链接]

论坛徽章:
0
发表于 2006-08-04 14:21 |显示全部楼层
我是新手,最近看网络编程,今天从网上载了一个程序,编译运行以后出现这样的册抱错信息,麻烦各位高手帮忙看看,错在那里,谢谢!!
以下是代码:

#include <stdio.h>
#include <string.h>
#include <errno.h>  
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>  
#include <linux/in.h>
#include <linux/if_ether.h>
#include <net/if.h>
#include <sys/ioctl.h>

int main(int argc, char **argv) {
  int sock, n;
  char buffer[2048];
  unsigned char *iphead, *ethhead;
  struct ifreq ethreq;
  
  if ( (sock=socket(PF_PACKET, SOCK_RAW,
                    htons(ETH_P_IP)))<0) {
    perror("socket";
    exit(1);
  }

  /* Set the network card in promiscuos mode */
  strncpy(ethreq.ifr_name,"eth0",IFNAMSIZ);
  if (ioctl(sock,SIOCGIFFLAGS,ethreq)==-1) {
    perror("ioctl";
    close(sock);
    exit(1);
  }
  ethreq.ifr_flags|=IFF_PROMISC;
  if (ioctl(sock,SIOCSIFFLAGS,ethreq)==-1) {
    perror("ioctl";
    close(sock);
    exit(1);
  }
  
  while (1) {
    printf("----------\n";
    n = recvfrom(sock,buffer,2048,0,NULL,NULL);
    printf("%d bytes read\n",n);

    /* Check to see if the packet contains at least
     * complete Ethernet (14), IP (20) and TCP/UDP
     * ( headers.
     */
    if (n<42) {
      perror("recvfrom():";
      printf("Incomplete packet (errno is %d)\n",
             errno);
      close(sock);
      exit(0);
    }

    ethhead = buffer;
    printf("Source MAC address: "
           "%02x:%02x:%02x:%02x:%02x:%02x\n",
           ethhead[0],ethhead[1],ethhead[2],
           ethhead[3],ethhead[4],ethhead[5]);
    printf("Destination MAC address: "
           "%02x:%02x:%02x:%02x:%02x:%02x\n",
           ethhead[6],ethhead[7],ethhead[8],
           ethhead[9],ethhead[10],ethhead[11]);

    iphead = buffer+14; /* Skip Ethernet header */
    if (*iphead==0x45) { /* Double check for IPv4
                          * and no options present */
      printf("Source host %d.%d.%d.%d\n",
             iphead[12],iphead[13],
             iphead[14],iphead[15]);
      printf("Dest host %d.%d.%d.%d\n",
             iphead[16],iphead[17],
             iphead[18],iphead[19]);
      printf("Source,Dest ports %d,%d\n",
             (iphead[20]<<+iphead[21],
             (iphead[22]<<+iphead[23]);
      printf("Layer-4 protocol %d\n",iphead[9]);
    }
  }
  
}
gcc -o sniffer sniffer.c
./sniffer   后出现以下错误信息:
ioctl: Bad address

论坛徽章:
0
发表于 2006-08-04 14:31 |显示全部楼层
不知道怎么搞的,总是出现好多笑脸,不过还好,不影响程序的阅读,不好意思:)

论坛徽章:
0
发表于 2006-08-04 14:34 |显示全部楼层
好象是在设置网卡的混杂模式的时候出现的问题,但是我实在是找不出问题.
还有,那位好心人告诉我用什么方法能让贴出的代码在"白色的代码框"中?
谢了!!!

论坛徽章:
0
发表于 2006-08-04 15:20 |显示全部楼层
将代码全选后按"代码"按钮

论坛徽章:
0
发表于 2006-08-04 15:23 |显示全部楼层
是用超级用户执行的不

论坛徽章:
0
发表于 2006-08-04 15:44 |显示全部楼层
是啊.
我确定是用超级用户执行的!
我刚才在网了搜了个帖子,其中socket 是原始套接字的即:
((sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP))
这样,就可以设计,没什么问题.不知道为什么.

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
发表于 2006-08-04 15:53 |显示全部楼层
用netstat看一下你的网卡接口名

论坛徽章:
0
发表于 2006-08-04 16:17 |显示全部楼层
ioctl 调用是一个基于实现的调用,你看看你的头文件是否有哪个命令字的定义

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
发表于 2006-08-04 16:24 |显示全部楼层
可能系统不支持

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
发表于 2006-08-04 16:44 |显示全部楼层
刚才看的太急了.
linux一定支持ioctl的SIOCGIFFLAGS啊,呵呵
再仔细一读,ioctl最后一个参数应该是指针.
所以.修改一下吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP