winpcap 获取数据链路层类型
2013-12-13 15:45
489 查看
#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;
}
#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;
}
相关文章推荐
- WinPcap教程(1):获取网卡列表
- WinPcap教程(2):获取设备高级信息
- WinPcap教程(1):获取网卡列表
- 使用WinPcap获取网卡MAC地址
- WinPcap获取设备列表
- WinPcap教程(2):获取设备高级信息
- 如何修改winpcap源码,使之能从内核直接获取数据包,而不经过userbuff
- winPcap学习笔记1_获取设备列表(转)
- winpcap获取适配器列表
- 使用WinPcap获取网卡MAC地址
- Winpcap教程(获取数据包)
- winPcap学习笔记2_获取已安装设备的高级信息(转)
- WinPcap 之 获取设备列表
- 使用Winpcap 获取设备列表 2
- 使用Winpcap获取本地IP和子网掩码的简单程序
- Microsoft Visual Studio 10.0执行WinPcap获取网络设配器的代码
- WinPcap教程(一) 获取本地驱动设备列表
- winpcap编程设置过滤器之指定获取某个网站的数据
- 使用Winpcap 获取设备列表 2
- WinPcap编程入门(4)——不使用回调方法获取数据包