winpcap 获取数据链路层类型

#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
/*
 * 获取数据链路层类型
 */
 
/*
 * pcap_lookupdev介绍
 *
NAME
       pcap_lookupdev - find the default device on which to capture
SYNOPSIS
       #include <pcap/pcap.h>
       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 <pcap/pcap.h>
       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 <pcap/pcap.h>
       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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值