免费注册 查看新帖 |

Chinaunix

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

pcap_next 为什么总是返回NULL?? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-06-18 19:55 |只看该作者 |倒序浏览
用的是网上找到的源码
  1. /***************************************************
  2. * file:     testpcap1.c
  3. * Date:     Thu Mar 08 17:14:36 MST 2001
  4. * Author:   Martin Casado
  5. * Location: LAX Airport (woof!)
  6. *
  7. * Simple single packet capture program
  8. *****************************************************/
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <pcap.h>
  12. #include <errno.h>
  13. #include <sys/socket.h>
  14. #include <netinet/in.h>
  15. #include <arpa/inet.h>
  16. #include <netinet/if_ether.h> /* includes net/ethernet.h */

  17. int main(int argc, char **argv)
  18. {
  19.     int i;
  20.     char *dev;
  21.     char errbuf[PCAP_ERRBUF_SIZE];
  22.     pcap_t* descr;
  23.     const u_char *packet;
  24.     struct pcap_pkthdr hdr;     /* pcap.h */
  25.     struct ether_header *eptr;  /* net/ethernet.h */

  26.     u_char *ptr; /* printing out hardware header info */

  27.     /* grab a device to peak into... */
  28.     dev = pcap_lookupdev(errbuf);

  29.     if(dev == NULL)
  30.     {
  31.         printf("%s\n",errbuf);
  32.         exit(1);
  33.     }

  34.     printf("DEV: %s\n",dev);

  35.     /* open the device for sniffing.

  36.        pcap_t *pcap_open_live(char *device,int snaplen, int prmisc,int to_ms,
  37.        char *ebuf)

  38.        snaplen - maximum size of packets to capture in bytes
  39.        promisc - set card in promiscuous mode?
  40.        to_ms   - time to wait for packets in miliseconds before read
  41.        times out
  42.        errbuf  - if something happens, place error string here
  43.      */

  44.     /* get packet capture descriptor from capture.  Note if you change
  45.        "prmisc" param to anything other than zero, you will get all
  46.        packets your device sees, whether they are intendeed for you or
  47.        not!! Be sure you know the rules of the network you are running
  48.        on before you set your card in promiscuous mode or you could get
  49.        yourself in serious doo doo!!! (also need to be root to run in
  50.        promisuous mode)                                               */
  51.     descr = pcap_open_live(dev,BUFSIZ,0,-1,errbuf);

  52.     if(descr == NULL)
  53.     {
  54.         printf("pcap_open_live(): %s\n",errbuf);
  55.         exit(1);
  56.     }


  57.     /* grab a packet from descr (yay!)                    */
  58.     /* const u_char *pcap_next(pcap_t *p,struct pcap_pkthdr *h) */
  59.     /* so just pass in the descriptor we got from         */
  60.     /* our call to pcap_open_live and an allocated        */
  61.     /* struct pcap_pkthdr                                 */
  62.     packet = pcap_next(descr,&hdr);

  63.     if(packet == NULL)
  64.     {/* dinna work *sob* */
  65.         printf("Didn't grab packet\n");
  66.         exit(1);
  67.     }

  68.     /*  
  69.         struct pcap_pkthdr {
  70.         struct timeval ts;    time stamp
  71.         bpf_u_int32 caplen;   length of portion present
  72.         bpf_u_int32;          lebgth this packet (off wire)
  73.         }
  74.      */

  75.     printf("Grabbed packet of length %d\n",hdr.len);
  76.     printf("Recieved at ..... %s\n",ctime((const time_t*)&hdr.ts.tv_sec));
  77.     printf("Ethernet address length is %d\n",ETHER_HDR_LEN);

  78.     /* lets start with the ether header... */
  79.     eptr = (struct ether_header *) packet;

  80.     /* check to see if we have an ip packet */
  81.     if (ntohs (eptr->ether_type) == ETHERTYPE_IP)
  82.     {
  83.         printf("Ethernet type hex:%x dec:%d is an IP packet\n",
  84.                 ntohs(eptr->ether_type),
  85.                 ntohs(eptr->ether_type));
  86.     }else  if (ntohs (eptr->ether_type) == ETHERTYPE_ARP)
  87.     {
  88.         printf("Ethernet type hex:%x dec:%d is an ARP packet\n",
  89.                 ntohs(eptr->ether_type),
  90.                 ntohs(eptr->ether_type));
  91.     }else {
  92.         printf("Ethernet type %x not IP", ntohs(eptr->ether_type));
  93.         exit(1);
  94.     }

  95.     /* THANK YOU RICHARD STEVENS!!! */
  96.     ptr = eptr->ether_dhost;
  97.     i = ETHER_ADDR_LEN;
  98.     printf(" Destination Address:  ");
  99.     do{
  100.         printf("%s%x",(i == ETHER_ADDR_LEN) ? " " : ":",*ptr++);
  101.     }while(--i>0);
  102.     printf("\n");

  103.     ptr = eptr->ether_shost;
  104.     i = ETHER_ADDR_LEN;
  105.     printf(" Source Address:  ");
  106.     do{
  107.         printf("%s%x",(i == ETHER_ADDR_LEN) ? " " : ":",*ptr++);
  108.     }while(--i>0);
  109.     printf("\n");

  110.     return 0;
  111. }
复制代码
运行[root@Fedora12 pcap]#./a.out
结果总是:
DEV: ppp0
Didn't grab packet

麻烦大家帮我看下什么原因。谢谢

论坛徽章:
0
2 [报告]
发表于 2010-06-18 22:37 |只看该作者
为什么都没人回答呢。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP