Netexpert FAQ 网络分析专家学习建议入口 @netexpert成员申请指南
netexpert积分规则的说明 Netis招贤纳士(2008年11月22日更新)
发新话题
打印

学习libpcap时,编译PCAP.H出现的数据定义问题?

学习libpcap时,编译PCAP.H出现的数据定义问题?

来源:http://www.cet.nau.edu/~mc8/Socket/Tutorials/section3.html
程序代码:
#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h>

/* callback function that is passed to pcap_loop(..) and called each time
* a packet is recieved                                                    */
void my_callback(u_char *useless,const struct pcap_pkthdr* pkthdr,const u_char*
        packet)
{
    static int count = 1;
    fprintf(stdout,"%d, ",count);
    if(count == 4)
        fprintf(stdout,"Come on baby sayyy you love me!!! ");
    if(count == 7)
        fprintf(stdout,"Tiiimmmeesss!! ");
    fflush(stdout);
    count++;
}

int main(int argc,char **argv)
{
    int i;
    char *dev;
    char errbuf[PCAP_ERRBUF_SIZE];
    pcap_t* descr;
    const u_char *packet;
    struct pcap_pkthdr hdr;     /* pcap.h */
    struct ether_header *eptr;  /* net/ethernet.h */

    if(argc != 2){ fprintf(stdout,"Usage: %s numpackets\n",argv[0]);return 0;}

    /* grab a device to peak into... */
    dev = pcap_lookupdev(errbuf);
    if(dev == NULL)
    { printf("%s\n",errbuf); exit(1); }
    /* open device for reading */
    descr = pcap_open_live(dev,BUFSIZ,0,-1,errbuf);
    if(descr == NULL)
    { printf("pcap_open_live(): %s\n",errbuf); exit(1); }

    /* allright here we call pcap_loop(..) and pass in our callback function */
    /* int pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)*/
    /* If you are wondering what the user argument is all about, so am I!!   */
    pcap_loop(descr,atoi(argv[1]),my_callback,NULL);

    fprintf(stdout,"\nDone processing packets... wheew!\n");
    return 0;
}

在LINUX下用 :gcc testpcap2.c -lpcap  进行编译出现一些错误:
提示好象都是那种带u_ 的不能识别??如  u_int , u_short
请问仁兄:
      这个问题怎么解决?(另我怎么知道在编译时要带 -lpcap 参数呢?  )

TOP

好象没有遇到过这个问题,已经解决了,只要写成这样就可以了:

#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>           //移动一下位置
本帖最近评分记录
  • scz 威望 +2 自问自答 2007-5-23 13:30

TOP

发新话题
版块跳转