免费注册 查看新帖 |

Chinaunix

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

[C++] pcap_open_live函数报错 求高人指点 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-06-11 11:45 |只看该作者 |倒序浏览
以太网帧的监视小程序,基本书上COPY的 就修改了下
#include <pcap.h>
#include <stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<stdlib.h>
#include<math.h>
#include<errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <cf.h>
#include <sys/cfgodm.h>
#include <sys/cfgdb.h>
#define DEFAULT_SNAPLEN 68

void packet_print
(u_char* user, const struct pcap_pkthdr* h, const u_char* p)
{
          static int icount=1;
    printf("find %d packet\n", icount++);       
}


void   main()   
{   
  char   error_content[PCAP_ERRBUF_SIZE];   
  struct   in_addr   net_ip_address;   
  struct   in_addr   net_mask_address;   
  char   *net_interface;   
  char   *net_ip_string;   
  char   *net_mask_string;   
  bpf_u_int32   net_ip;   
  u_int32_t   net_mask;   
  /*net_interface   =   pcap_lookupdev(error_content);
  if ( net_interface == NULL)
  {
      fprintf(stderr, "pcap_lookupdev() error: %s\n", error_content);
              exit(-1);        
  }  */
  printf("网络接口:%s\n",   net_interface);   
  pcap_lookupnet("en0",   &net_ip,   &net_mask,   error_content);   
  net_ip_address.s_addr   =   net_ip;   
  net_ip_string   =   inet_ntoa(net_ip_address);   
  printf("网络地址:%s\n",   inet_ntoa(net_ip_address));   
  net_mask_address.s_addr   =   net_mask;   
  net_mask_string   =   inet_ntoa(net_mask_address);   
  printf("网络掩码:%s\n",   net_mask_string);   
  //
  pcap_t *pd;
  if ( genmajor("bpf") == -1 )
  {
      perror("genmajor");
      exit(-1);       
  }

  memset(error_content,0x00, sizeof(error_content));
  char tmpstr[]="en0";
  if ( (pd=pcap_open_live(tmpstr,DEFAULT_SNAPLEN, 0, 1000, error_content)) == NULL )
  {
            perror("pcap_open_live");
            fprintf(stderr, "%s\n", error_content);
              exit(-1);
  }
  memset(error_content,0x00, sizeof(error_content));
  if ( pcap_loop(pd, 10, packet_print, NULL) < 0 )
  {
      fprintf(stderr, "pcap_loop() error: %s\n", error_content);
              exit(-1);        
  }
  pcap_close(pd);
  exit(0);
}


//报错信息:
&Iacute;&oslash;&Acirc;&ccedil;&frac12;&Oacute;&iquest;&Uacute;:
&Iacute;&oslash;&Acirc;&ccedil;&micro;&Oslash;&Ouml;·:10.40.96.0
&Iacute;&oslash;&Acirc;&ccedil;&Ntilde;&Uacute;&Acirc;&euml;:255.255.255.0
genmajor: No such file or directory

红色 部分是我测试genmajor这个函数用的
如果删除去的话 在pcap_open_live报错
信息如下:
&Iacute;&oslash;&Acirc;&ccedil;&frac12;&Oacute;&iquest;&Uacute;:
&Iacute;&oslash;&Acirc;&ccedil;&micro;&Oslash;&Ouml;·:10.40.96.0
&Iacute;&oslash;&Acirc;&ccedil;&Ntilde;&Uacute;&Acirc;&euml;:255.255.255.0
pcap_open_live: Bad file number
bpf_load: genmajor failed: Error 0

我的系统是AIX

论坛徽章:
0
2 [报告]
发表于 2009-06-11 11:56 |只看该作者
顶一个
寻人启示
寻高人启示

论坛徽章:
0
3 [报告]
发表于 2009-06-11 13:43 |只看该作者
再顶

论坛徽章:
0
4 [报告]
发表于 2009-06-11 14:18 |只看该作者
虽然我不知道pcap_open_live是干什么用的,不过google一下一大把(仍然看不懂)

楼主何不自己解决?

论坛徽章:
0
5 [报告]
发表于 2009-06-11 14:55 |只看该作者
总算有点眉目了, 可能是权限不够 不过我现在没有管理员权限,不能试

论坛徽章:
0
6 [报告]
发表于 2009-06-11 16:19 |只看该作者
没管理员权限那没办法了。

论坛徽章:
0
7 [报告]
发表于 2009-07-06 21:04 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP