winpcap 获取数据链路层类型

#include
#include
#include
/*
 * 获取数据链路层类型
 */
 
/*
 * pcap_lookupdev介绍
 *
NAME
       pcap_lookupdev - find the default device on which to capture
SYNOPSIS
       #include
       char errbuf[PCAP_ERRBUF_SIZE];
       char *pcap_lookupdev(char *errbuf);
DESCRIPTION
       pcap_lookupdev()  returns  a  pointer  to a string giving the name of a
       network device suitable for use with pcap_create() and pcap_activate(),
       or  with  pcap_open_live(),  and with pcap_lookupnet(). If there is an
       error, NULL is returned and errbuf is filled  in  with  an  appropriate
       error  message. errbuf  is  assumed  to  be  able  to hold  at least
       PCAP_ERRBUF_SIZE chars.
*/
/*
 * pcap_open_live介绍
 *
NAME
       pcap_open_live - open a device for capturing
SYNOPSIS
       #include
       char errbuf[PCAP_ERRBUF_SIZE];
       pcap_t *pcap_open_live(const char *device, int snaplen,
      int promisc, int to_ms, char *errbuf);
DESCRIPTION
       pcap_open_live()  is  used to obtain a packet capture handle to look at
       packets on the network. device is a string that specifies the  network
       device  to  open;  on Linux systems with 2.2 or later kernels, a device
       argument of "any" or NULL can be  used  to  capture  packets  from  all
       interfaces.
       snaplen specifies the snapshot length to be set on the handle.
       promisc specifies if the interface is to be put into promiscuous mode.
       to_ms specifies the read timeout in milliseconds.
RETURN VALUE
       pcap_open_live() returns a pcap_t * on success and NULL on failure.  If
       NULL is returned, errbuf is filled in with an  appropriate  error  mes-
       sage.   errbuf  may  also  be set to warning text when pcap_open_live()
       succeds; to detect this case the  caller  should  store a  zero-length
       string  in errbuf before calling pcap_open_live() and display the warn-
       ing to the user if errbuf is no longer a zero-length string.  errbuf is
       assumed to be able to hold at least PCAP_ERRBUF_SIZE chars.
*/
/*
 * pcap_datalink介绍
 *
NAME
       pcap_datalink - get the link-layer header type
SYNOPSIS
       #include
       int pcap_datalink(pcap_t *p);
DESCRIPTION
       pcap_datalink() returns  the  link  layer type for the live capture or
       ``savefile'' specified by p.
*/
#define DEFAULT_SNAPLEN 68 
int
main (int argc, char **argv)
{
char errbuf[PCAP_ERRBUF_SIZE];
char *netdev = pcap_lookupdev(errbuf);
if(netdev == NULL)
{
printf("error:%s/n",errbuf);
exit(1);
}

pcap_t *pd = pcap_open_live(netdev, DEFAULT_SNAPLEN, 0, 1000, errbuf);
if(pd == NULL)
{
printf("error:%s/n",errbuf);
exit(1);
}

int type = pcap_datalink(pd);
printf("type:%d/n",type); // 返回值的意思请查阅:http://www.unix.com/man-page/All/7/pcap-linktype/

pcap_close(pd);
return 0;
}

你可能感兴趣的:(C)