本文整理汇总了C++中pcap_get_selectable_fd函数的典型用法代码示例。如果您正苦于以下问题:C++ pcap_get_selectable_fd函数的具体用法?C++ pcap_get_selectable_fd怎么用?C++ pcap_get_selectable_fd使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pcap_get_selectable_fd函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: sniffer_thread
DWORD sniffer_thread(THREAD *thread)
{
int fd;
fd_set rfds;
struct timeval tv;
int count;
CaptureJob *j = (CaptureJob *)(thread->parameter1);
fd = pcap_get_selectable_fd(j->pcap);
dprintf("pcap @ %p, selectable fd is %d", j->pcap, fd);
while(event_poll(thread->sigterm, 0) == FALSE && j->active) {
tv.tv_sec = 0;
tv.tv_usec = 5000;
FD_ZERO(&rfds);
FD_SET(pcap_get_selectable_fd(j->pcap), &rfds);
select(fd+1, &rfds, NULL, NULL, &tv);
count = pcap_dispatch(j->pcap, 100, packet_handler, (u_char *)(j));
if (-1 == count)
dprintf("pcap error: %s", pcap_geterr(j->pcap));
if(count <= 0) continue;
if(count) dprintf("dispatched %d packets", count);
}
dprintf("and we're done");
return 0;
}
示例2: tc_pcap_socket_in_init
int
tc_pcap_socket_in_init(pcap_t **pd, char *device,
int snap_len, int buf_size, char *pcap_filter)
{
int fd;
char ebuf[PCAP_ERRBUF_SIZE];
struct bpf_program fp;
bpf_u_int32 net, netmask;
if (device == NULL) {
return TC_INVALID_SOCK;
}
tc_log_info(LOG_NOTICE, 0, "pcap open,device:%s", device);
*ebuf = '\0';
if (tc_pcap_open(pd, device, snap_len, buf_size) == TC_ERR) {
return TC_INVALID_SOCK;
}
if (pcap_lookupnet(device, &net, &netmask, ebuf) < 0) {
net = 0;
netmask = 0;
tc_log_info(LOG_WARN, 0, "lookupnet:%s", ebuf);
return TC_INVALID_SOCK;
}
if (pcap_compile(*pd, &fp, pcap_filter, 0, netmask) == -1) {
tc_log_info(LOG_ERR, 0, "couldn't parse filter %s: %s",
pcap_filter, pcap_geterr(*pd));
return TC_INVALID_SOCK;
}
if (pcap_setfilter(*pd, &fp) == -1) {
tc_log_info(LOG_ERR, 0, "couldn't install filter %s: %s",
pcap_filter, pcap_geterr(*pd));
pcap_freecode(&fp);
return TC_INVALID_SOCK;
}
pcap_freecode(&fp);
if (pcap_get_selectable_fd(*pd) == -1) {
tc_log_info(LOG_ERR, 0, "pcap_get_selectable_fd fails");
return TC_INVALID_SOCK;
}
if (pcap_setnonblock(*pd, 1, ebuf) == -1) {
tc_log_info(LOG_ERR, 0, "pcap_setnonblock failed: %s", ebuf);
return TC_INVALID_SOCK;
}
fd = pcap_get_selectable_fd(*pd);
return fd;
}
示例3: tc_pcap_socket_in_init
int
tc_pcap_socket_in_init(pcap_t **pd, char *device, char *pcap_filter)
{
int fd;
char ebuf[PCAP_ERRBUF_SIZE];
struct bpf_program fp;
bpf_u_int32 net, netmask;
if (device == NULL) {
return TC_INVALID_SOCKET;
}
tc_log_info(LOG_NOTICE, 0, "pcap open,device:%s", device);
*ebuf = '\0';
*pd = pcap_open_live(device, PCAP_RECV_BUF_SIZE, 0, 1000, ebuf);
if (*pd == NULL) {
tc_log_info(LOG_ERR, 0, "pcap error:%s", ebuf);
return TC_INVALID_SOCKET;
} else if (*ebuf) {
tc_log_info(LOG_WARN, 0, "pcap warn:%s", ebuf);
}
if (pcap_lookupnet(device, &net, &netmask, ebuf) < 0) {
net = 0;
netmask = 0;
tc_log_info(LOG_WARN, 0, "lookupnet:%s", ebuf);
}
if (pcap_compile(*pd, &fp, pcap_filter, 0, netmask) == -1) {
tc_log_info(LOG_ERR, 0, "couldn't parse filter %s: %s",
pcap_filter, pcap_geterr(*pd));
return TC_INVALID_SOCKET;
}
if (pcap_setfilter(*pd, &fp) == -1) {
tc_log_info(LOG_ERR, 0, "couldn't install filter %s: %s",
pcap_filter, pcap_geterr(*pd));
return TC_INVALID_SOCKET;
}
if (pcap_get_selectable_fd(*pd) == -1) {
tc_log_info(LOG_ERR, 0, "pcap_get_selectable_fd fails");
return TC_INVALID_SOCKET;
}
if (pcap_setnonblock(*pd, 1, ebuf) == -1) {
tc_log_info(LOG_ERR, 0, "pcap_setnonblock failed: %s", ebuf);
return TC_INVALID_SOCKET;
}
fd = pcap_get_selectable_fd(*pd);
return fd;
}
示例4: pcap_get_selectable_fd
int BaseSniffer::get_fd() {
#ifndef _WIN32
return pcap_get_selectable_fd(handle);
#else
throw std::runtime_error("Method not supported in Windows platform");
#endif // _WIN32
}
示例5: pcap_get_selectable_fd
int BaseSniffer::get_fd() {
#ifndef _WIN32
return pcap_get_selectable_fd(handle_);
#else
throw unsupported_function();
#endif // _WIN32
}
示例6: l2_packet_init_libpcap
static int l2_packet_init_libpcap(struct l2_packet_data *l2,
unsigned short protocol)
{
bpf_u_int32 pcap_maskp, pcap_netp;
char pcap_filter[200], pcap_err[PCAP_ERRBUF_SIZE];
struct bpf_program pcap_fp;
pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err);
l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 10, pcap_err);
if (l2->pcap == NULL) {
fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
fprintf(stderr, "ifname='%s'\n", l2->ifname);
return -1;
}
if (pcap_datalink(l2->pcap) != DLT_EN10MB &&
pcap_set_datalink(l2->pcap, DLT_EN10MB) < 0) {
fprintf(stderr, "pcap_set_datalink(DLT_EN10MB): %s\n",
pcap_geterr(l2->pcap));
return -1;
}
os_snprintf(pcap_filter, sizeof(pcap_filter),
"not ether src " MACSTR " and "
"( ether dst " MACSTR " or ether dst " MACSTR " ) and "
"ether proto 0x%x",
MAC2STR(l2->own_addr), /* do not receive own packets */
MAC2STR(l2->own_addr), MAC2STR(pae_group_addr),
protocol);
if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) {
fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap));
return -1;
}
if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
return -1;
}
pcap_freecode(&pcap_fp);
#ifndef __sun__
/*
* When libpcap uses BPF we must enable "immediate mode" to
* receive frames right away; otherwise the system may
* buffer them for us.
*/
{
unsigned int on = 1;
if (ioctl(pcap_fileno(l2->pcap), BIOCIMMEDIATE, &on) < 0) {
fprintf(stderr, "%s: cannot enable immediate mode on "
"interface %s: %s\n",
__func__, l2->ifname, strerror(errno));
/* XXX should we fail? */
}
}
#endif /* __sun__ */
eloop_register_read_sock(pcap_get_selectable_fd(l2->pcap),
l2_packet_receive, l2, l2->pcap);
return 0;
}
示例7: set_raw_filter
int set_raw_filter(unsigned int loc_idx, char *filter) {
struct bpf_program raw_filter;
//uint16_t snaplen = 65535;
int linktype;
//struct pcap_t *aa;
int fd = -1;
LERR("APPLY FILTER [%d]\n", loc_idx);
if(loc_idx >= MAX_SOCKETS || sniffer_proto[loc_idx] == NULL) return 0;
fd = pcap_get_selectable_fd(sniffer_proto[loc_idx]);
linktype = profile_socket[loc_idx].link_type ? profile_socket[loc_idx].link_type : DLT_EN10MB;
if (pcap_compile_nopcap(profile_socket[loc_idx].snap_len ? profile_socket[loc_idx].snap_len : 0xffff, linktype, &raw_filter, filter, 1, 0) == -1) {
LERR("Failed to compile filter '%s'", filter);
return -1;
}
#if ( defined (OS_LINUX) || defined (OS_SOLARIS) )
if(setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &raw_filter, sizeof(raw_filter)) < 0 ) {
LERR(" setsockopt filter: [%s] [%d]", strerror(errno), errno);
return -1;
}
#endif
//free(BPF_code);
pcap_freecode( (struct bpf_program *) &raw_filter);
return 1;
}
示例8: tuntap_openmon_cb
/* Monitor, inject, and injmon are all the same method, open a new vap */
int tuntap_openmon_cb(lorcon_t *context) {
char *parent;
char pcaperr[PCAP_ERRBUF_SIZE];
struct mac80211_lorcon *extras = (struct mac80211_lorcon *) context->auxptr;
short flags;
struct ifreq if_req;
struct sockaddr_ll sa_ll;
if (ifconfig_delta_flags(context->ifname, context->errstr,
(IFF_UP | IFF_RUNNING | IFF_PROMISC)) < 0) {
return -1;
}
pcaperr[0] = '\0';
if ((context->pcap = pcap_open_live(context->ifname, LORCON_MAX_PACKET_LEN,
1, 1000, pcaperr)) == NULL) {
snprintf(context->errstr, LORCON_STATUS_MAX, "%s", pcaperr);
return -1;
}
context->capture_fd = pcap_get_selectable_fd(context->pcap);
context->dlt = pcap_datalink(context->pcap);
context->inject_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (context->inject_fd < 0) {
snprintf(context->errstr, LORCON_STATUS_MAX, "failed to create injection "
"socket: %s", strerror(errno));
pcap_close(context->pcap);
return -1;
}
memset(&if_req, 0, sizeof(if_req));
memcpy(if_req.ifr_name, context->ifname, IFNAMSIZ);
if_req.ifr_name[IFNAMSIZ - 1] = 0;
if (ioctl(context->inject_fd, SIOCGIFINDEX, &if_req) < 0) {
snprintf(context->errstr, LORCON_STATUS_MAX, "failed to get interface idex: %s",
strerror(errno));
close(context->inject_fd);
pcap_close(context->pcap);
return -1;
}
memset(&sa_ll, 0, sizeof(sa_ll));
sa_ll.sll_family = AF_PACKET;
sa_ll.sll_protocol = htons(ETH_P_80211_RAW);
sa_ll.sll_ifindex = if_req.ifr_ifindex;
if (bind(context->inject_fd, (struct sockaddr *) &sa_ll, sizeof(sa_ll)) != 0) {
snprintf(context->errstr, LORCON_STATUS_MAX, "failed to bind injection "
"socket: %s", strerror(errno));
close(context->inject_fd);
pcap_close(context->pcap);
return -1;
}
return 1;
}
示例9: netfpga_init_port
static rofl_result_t netfpga_init_port(switch_port_t* port){
struct ifreq interface;
netfpga_port_t* nport = (netfpga_port_t*)malloc(sizeof(*nport));
char *useless;
//fprintf(stderr, "device= %s \n",dev);
useless = pcap_lookupdev(port->name); //test if device exist// gives char pointer, why not pcap_if_t?
if (useless == NULL) {
ROFL_ERR( "Couldn't find device: error= %s; no permission to listen on interface or other failure \n", port->name);
return ROFL_FAILURE;
}
ROFL_DEBUG("Device :%s found\n", port->name);
char errbuf[PCAP_ERRBUF_SIZE];
nport->pcap_fd = pcap_open_live(port->name, BUFSIZ, 1, 0, errbuf);//wait until the packet arrive, NO TIMEOUT
if (nport->pcap_fd == NULL) {
ROFL_ERR( "Couldn't open device %s : %s\n",port->name, errbuf);
return ROFL_FAILURE;
}
nport->fd = pcap_get_selectable_fd(nport->pcap_fd);
nport->test=25;
ROFL_DEBUG("pcap_open_live: socket opened \n ");
ROFL_DEBUG("Ports.c creating socket over %s inerface\n", port->name);
strncpy(interface.ifr_ifrn.ifrn_name, port->name, IFNAMSIZ/*&SWITCH_PORT_MAX_LEN_NAME*/);
int flags;
/* Set non-blocking mode. */
flags = fcntl(nport->fd, F_GETFL, 0);
if(fcntl(nport->fd, F_SETFL, flags | O_NONBLOCK) < 0) {
return ROFL_FAILURE;
}
//Store in platform state and return
port->platform_port_state = (platform_port_state_t*) nport;
return ROFL_SUCCESS;
}
示例10: l2_packet_deinit
void l2_packet_deinit(struct l2_packet_data *l2)
{
if (l2 != NULL) {
if (l2->pcap) {
eloop_unregister_read_sock(
pcap_get_selectable_fd(l2->pcap));
pcap_close(l2->pcap);
}
os_free(l2);
}
}
示例11: lpcap_getfd
static int lpcap_getfd(lua_State* L)
{
pcap_t* cap = checkpcap(L);
int fd = pcap_get_selectable_fd(cap);
if(fd < 0) {
lua_pushnil(L);
lua_pushstring(L, "not selectable");
return 2;
}
lua_pushnumber(L, fd);
return 1;
}
示例12: main
int main(int argc, char *argv[])
{
char *dev, errbuf[PCAP_ERRBUF_SIZE];
pcap_t *handle;
int selectable_fd;
if (argc == 2) {
dev = argv[1];
} else {
dev = pcap_lookupdev(errbuf);
}
if (dev == NULL) {
fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
return (2);
}
handle = pcap_open_live(dev, BUFSIZ, 1, 0, errbuf);
if (handle == NULL) {
fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
return (2);
}
if (pcap_datalink(handle) != DLT_EN10MB) {
fprintf(stderr, "Device %s doesn't provide Ethernet headers - "
"not supported\n",
dev);
return (2);
}
if (pcap_setnonblock(handle, 1, errbuf) != 0) {
fprintf(stderr, "Non-blocking mode failed: %s\n", errbuf);
return (2);
}
selectable_fd = pcap_get_selectable_fd(handle);
if (-1 == selectable_fd) {
fprintf(stderr, "pcap handle not selectable.\n");
return (2);
}
init_curses();
mvprintw(0, 0, "Device: %s\n", dev);
grab_packets(selectable_fd, handle);
/* And close the session */
pcap_close(handle);
return 0;
}
示例13: pcap_open_live
// アダプタを開く (Pcap)
ETH *OpenEthPcap(char *name, bool local, bool tapmode, char *tapaddr)
{
char errbuf[PCAP_ERRBUF_SIZE];
ETH *e;
pcap_t *p;
CANCEL *c;
// 引数チェック
if (name == NULL || tapmode != false)
{
return NULL;
}
// エラーメッセージバッファの初期化
errbuf[0] = 0;
// キャプチャデバイスを開く
p = pcap_open_live(name, 65535, (local == false), 1, errbuf);
if(p==NULL)
{
return NULL;
}
// ノンブロックモードに設定
// BSD系OSでは、BPFのselectが正常に動作しないのでブロックさせないとビジーループになる
/*
if(pcap_setnonblock(p, true, errbuf) == -1)
{
Debug("pcap_setnonblock:%s\n",errbuf);
pcap_close(p);
return NULL;
}
*/
e = ZeroMalloc(sizeof(ETH));
e->Name = CopyStr(name);
e->Title = CopyStr(name);
e->Queue = NewQueue();
e->QueueSize = 0;
e->Cancel = NewCancel();
e->IfIndex = -1;
e->Socket = pcap_get_selectable_fd(p);
e->Pcap = p;
e->CaptureThread = NewThread(PcapThread, e);
WaitThreadInit(e->CaptureThread);
return e;
}
示例14: pcap_open_live
// Open Ethernet adapter (Pcap)
ETH *OpenEthPcap(char *name, bool local, bool tapmode, char *tapaddr)
{
char errbuf[PCAP_ERRBUF_SIZE];
ETH *e;
pcap_t *p;
CANCEL *c;
// Validate arguments
if (name == NULL || tapmode != false)
{
return NULL;
}
// Initialize error message buffer
errbuf[0] = 0;
// Open capturing device
p = pcap_open_live(name, 65535, (local == false), 1, errbuf);
if(p==NULL)
{
return NULL;
}
// Set to non-block mode
// (In old BSD OSs, 'select(2)' don't block normally for BPF device. To prevent busy loop)
/*
if(pcap_setnonblock(p, true, errbuf) == -1)
{
Debug("pcap_setnonblock:%s\n",errbuf);
pcap_close(p);
return NULL;
}
*/
e = ZeroMalloc(sizeof(ETH));
e->Name = CopyStr(name);
e->Title = CopyStr(name);
e->Queue = NewQueue();
e->QueueSize = 0;
e->Cancel = NewCancel();
e->IfIndex = -1;
e->Socket = pcap_get_selectable_fd(p);
e->Pcap = p;
e->CaptureThread = NewThread(PcapThread, e);
WaitThreadInit(e->CaptureThread);
return e;
}
示例15: l2_packet_deinit
void l2_packet_deinit(struct l2_packet_data *l2)
{
if (l2 == NULL)
return;
#ifdef CONFIG_WINPCAP
eloop_cancel_timeout(l2_packet_receive_timeout, l2, l2->pcap);
#else /* CONFIG_WINPCAP */
if (l2->eth)
eth_close(l2->eth);
eloop_unregister_read_sock(pcap_get_selectable_fd(l2->pcap));
#endif /* CONFIG_WINPCAP */
if (l2->pcap)
pcap_close(l2->pcap);
os_free(l2);
}