本文整理汇总了C++中pcap_setnonblock函数的典型用法代码示例。如果您正苦于以下问题:C++ pcap_setnonblock函数的具体用法?C++ pcap_setnonblock怎么用?C++ pcap_setnonblock使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pcap_setnonblock函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: pcap_ex_setnonblock
void
pcap_ex_setnonblock(pcap_t *pcap, int nonblock, char *ebuf)
{
#ifdef HAVE_PCAP_SETNONBLOCK
pcap_setnonblock(pcap, nonblock, ebuf);
#endif
}
示例2: pcap_open
struct pcap_port *
pcap_open(struct pcap_port *pcap_port) {
char errbuf[PCAP_ERRBUF_SIZE];
struct pcap_drv *pcap_drv = pcap_port->drv;
pcap_t *pcap = NULL;
pcap = pcap_open_live(pcap_port->name, PCAP_SNAPLEN, true/*promisc*/, -1/*to_ms*/, errbuf);
if (pcap == NULL) {
logger_log(pcap_drv->logger, LOG_ERR, "Unable to open device %s: %s.", pcap_port->name, errbuf);
return NULL;
}
if(pcap_setnonblock(pcap, true, errbuf) != 0) {
logger_log(pcap_drv->logger, LOG_ERR, "Unable to set device to promisc %s: %s.", pcap_port->name, errbuf);
return NULL;
}
if(pcap_setdirection(pcap, PCAP_D_IN) != 0) {
logger_log(pcap_drv->logger, LOG_ERR, "Unable to set device direction %s: %s.", pcap_port->name, pcap_geterr(pcap));
return NULL;
}
pcap_port->pcap = pcap;
pcap_port->fd = pcap_fileno(pcap);
pcap_port->watcher = malloc(sizeof(ev_io));
pcap_port->watcher->data = pcap_port;
ev_io_init(pcap_port->watcher, event_loop_packet_in_cb, pcap_port->fd, EV_READ);
pcap_port_fill(pcap_port);
return pcap_port;
}
示例3: add_device
static void
add_device(orchids_t *ctx, mod_entry_t *mod, config_directive_t *dir)
{
/* dev: the network device to open
capd: a packet capture descriptor
*/
int fd;
char* dev;
char errbuf[PCAP_ERRBUF_SIZE]="";
pcap_t* capd = NULL;
dev = dir->args;
DebugLog(DF_MOD, DS_INFO, "Add 802.11 listen device %s\n", dev);
capd = pcap_open_live(dev, 65535, 1, 1, errbuf);
if ( strlen(errbuf) != 0 ) {
DebugLog(DF_MOD, DS_ERROR, "pcap_open_live error: %s\n", errbuf);
return;
}
pcap_setnonblock(capd, 1, NULL);
fd = pcap_fileno(capd);
datalink_type = pcap_datalink(capd);
add_input_descriptor(ctx, mod, wifi_callback, fd, capd);
}
示例4: prvConfigureCaptureBehaviour
static void prvConfigureCaptureBehaviour( void )
{
struct bpf_program xFilterCode;
const long lMinBytesToCopy = 10L, lBlocking = 1L;
unsigned long ulNetMask;
/* Unblock a read as soon as anything is received. */
pcap_setmintocopy( pxOpenedInterfaceHandle, lMinBytesToCopy );
/* Allow blocking. */
pcap_setnonblock( pxOpenedInterfaceHandle, lBlocking, cErrorBuffer );
/* Set up a filter so only the packets of interest are passed to the lwIP
stack. cErrorBuffer is used for convenience to create the string. Don't
confuse this with an error message. */
sprintf( cErrorBuffer, "broadcast or multicast or host %d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
ulNetMask = ( configNET_MASK3 << 24UL ) | ( configNET_MASK2 << 16UL ) | ( configNET_MASK1 << 8L ) | configNET_MASK0;
if( pcap_compile(pxOpenedInterfaceHandle, &xFilterCode, cErrorBuffer, 1, ulNetMask ) < 0 ) {
printf( "\r\nThe packet filter string is invalid\r\n" );
} else {
if( pcap_setfilter( pxOpenedInterfaceHandle, &xFilterCode ) < 0 ) {
printf( "\r\nAn error occurred setting the packet filter.\r\n" );
}
}
/* Create a task that simulates an interrupt in a real system. This will
block waiting for packets, then send a message to the uIP task when data
is available. */
xTaskCreate( prvInterruptSimulator, "MAC_ISR", configMINIMAL_STACK_SIZE, NULL, configMAC_ISR_SIMULATOR_PRIORITY, NULL );
}
示例5: 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;
}
示例6: 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;
}
示例7: 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;
}
示例8: p_setnonblock
static PyObject * p_setnonblock (PyObject *self, PyObject *args)
{
pcap_t * ppcap;
int nonblock;
char errbuf[PCAP_ERRBUF_SIZE];
if (!PyArg_ParseTuple(args, "li", (long*)&ppcap, (int*)&nonblock)) return NULL;
if (pcap_setnonblock(ppcap, nonblock ? 1 : 0, errbuf) == -1)
{
PyErr_SetString(PyExc_RuntimeError, errbuf);
return NULL;
}
Py_RETURN_NONE;
}
示例9: mi_alloc
struct mif *mi_open(char *iface)
{
struct mif *mi;
struct priv_if *pi;
pcap_t *pkt_in, *pkt_out;
char errbuf[PCAP_ERRBUF_SIZE];
/* setup mi struct */
mi = mi_alloc(sizeof(*pi));
if (!mi)
return NULL;
mi->read = mi_read;
mi->write = mi_write;
mi->close = mi_close;
// for pkt in
pkt_in = pcap_open_live(iface, 4096, 1, 10, errbuf);
if (pkt_in == NULL) {
printf("Unable to open interface %s in pcap: %s\n", iface, errbuf);
return NULL;
}
if (pcap_datalink(pkt_in) != DLT_IEEE802_11_RADIO) {
printf("Device %s doesn't provide 80211 radiotap header\n", iface);
return NULL;
}
if (pcap_setnonblock(pkt_in, 1, errbuf) == -1) {
printf("Device %s doesn't set non-blocking mode\n", iface);
return NULL;
}
// for pkt out
pkt_out = pcap_open_live(iface, 4096, 1, 10, errbuf);
if (pkt_out == NULL) {
printf("Unable to open interface %s in pcap: %s\n", iface, errbuf);
return NULL;
}
pi = mi_priv(mi);
pi->fd_in = pkt_in;
pi->fd_out = pkt_out;
return mi;
}
示例10: pcap_init
void pcap_init(void)
{
//char *dev;
char err_buff[PCAP_ERRBUF_SIZE];
if (dev == NULL) {
// 1. find the active interface for capturing data
if ((dev = pcap_lookupdev(err_buff)) == NULL) {
fprintf(stderr, "Couldn't find default device: %s\n", err_buff);
exit(EXIT_FAILURE);
}
}
// 2. open the interface
bpf_u_int32 net, mask;
if ((pcap_dest = pcap_open_live(dev, SNAP_LEN, 0, 1000, err_buff)) == NULL){
fprintf(stderr, "Failed to open the device %s\n", dev);
exit(EXIT_FAILURE);
}
if (pcap_lookupnet(dev, &net, &mask, err_buff) == -1) {
fprintf(stderr, "Failed to get net.\n");
exit(EXIT_FAILURE);
}
// 3. complie the filter
char filter[] = "ip";
struct bpf_program fp;
if (pcap_compile(pcap_dest, &fp, filter, 0, net) == -1) {
fprintf(stderr, "failed to compile the filter %s: %s\n",
filter, pcap_geterr(pcap_dest));
exit(EXIT_FAILURE);
}
if (pcap_setfilter(pcap_dest, &fp) == -1) {
fprintf(stderr, "failed to set filter %s: %s\n", filter,
pcap_geterr(pcap_dest));
exit(EXIT_FAILURE);
}
// 4. set the mode of pcap as non-block
if ((pcap_setnonblock(pcap_dest, 1, err_buff)) != 0) {
fprintf(stderr, "\e\[31m[Error]\e\[0m: %s\n", err_buff);
exit(EXIT_FAILURE);
}
示例11: PyErr_SetString
static PyObject *ppcap_setnonblock(ppcap *self, PyObject *args)
{
int nonblock, retval;
char errbuf[PCAP_ERRBUF_SIZE];
if (!PyArg_ParseTuple(args, "i", &nonblock))
return NULL;
if (!ppcap_isset_handle(self->handle)) {
PyErr_SetString(PyExc_Ppcap, "pcap handle is not created.");
return NULL;
}
retval = pcap_setnonblock(self->handle, nonblock, errbuf);
if (retval == -1) {
PyErr_Format(PyExc_Ppcap, "%s", errbuf);
return NULL;
}
Py_RETURN_NONE;
}
示例12: pcap_findalldevs
ebbrt::RawSocket::RawSocket()
{
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t* alldevs;
int ret = pcap_findalldevs(&alldevs, errbuf);
assert(ret != -1);
while (alldevs != NULL) {
if (strcmp(alldevs->name, dev) == 0) {
break;
}
alldevs = alldevs->next;
}
assert(alldevs != NULL);
auto addr = alldevs->addresses;
while (addr != NULL) {
if (addr->addr->sa_family == AF_PACKET) {
break;
}
addr = addr->next;
}
assert(addr != NULL);
auto packet_addr = reinterpret_cast<struct sockaddr_ll*>(addr->addr);
std::copy(&packet_addr->sll_addr[0], &packet_addr->sll_addr[5], mac_addr_);
pdev_ = pcap_open_live(dev, 65535, 0, 0, errbuf);
assert(dev != NULL);
ret = pcap_setnonblock(pdev_, 1, errbuf);
assert(ret != -1);
int fd = pcap_get_selectable_fd(pdev_);
assert(fd != -1);
uint8_t interrupt = event_manager->AllocateInterrupt([]() {
ethernet->Receive();
});
event_manager->RegisterFD(fd, EPOLLIN, interrupt);
}
示例13: pcap_event_init
void
pcap_event_init(client_conn_t * conn)
{
struct bpf_program filterp;
bpf_u_int32 maskp,
netp;
char errbuf[PCAP_ERRBUF_SIZE];
if (pcap_lookupnet(conn->start_header.dev, &netp, &maskp, errbuf) < 0)
goto pcap_err;
if ((conn->descr =
pcap_open_live(conn->start_header.dev,
conn->start_header.snaplen, 1, 100,
errbuf)) == NULL)
goto pcap_err;
if (conn->start_header.bpf != NULL) {
if (pcap_compile(conn->descr, &filterp,
conn->start_header.bpf, 0, netp) < 0)
goto pcap_err;
pcap_setfilter(conn->descr, &filterp);
}
if (pcap_setnonblock(conn->descr, 1, errbuf) < 0)
goto pcap_err;
if ((conn->pcap_fd = pcap_get_selectable_fd(conn->descr)) <= 0)
goto pcap_err;
event_set(&conn->pcap_event, conn->pcap_fd,
EV_READ | EV_PERSIST, (void *) pcap_driver, (void *) conn);
event_add(&conn->pcap_event, 0);
return;
pcap_err:
LOG("pcap err %s\n", errbuf);
free_client_conn(conn);
return;
}
示例14: hijack_open
/**
* Open pcap device
*
*/
static int hijack_open ( const char *interface, struct hijack *hijack ) {
char errbuf[PCAP_ERRBUF_SIZE];
/* Open interface via pcap */
errbuf[0] = '\0';
hijack->pcap = pcap_open_live ( interface, SNAPLEN, 1, 0, errbuf );
if ( ! hijack->pcap ) {
logmsg ( LOG_ERR, "Failed to open %s: %s\n",
interface, errbuf );
goto err;
}
if ( errbuf[0] )
logmsg ( LOG_WARNING, "Warning: %s\n", errbuf );
/* Set capture interface to non-blocking mode */
if ( pcap_setnonblock ( hijack->pcap, 1, errbuf ) < 0 ) {
logmsg ( LOG_ERR, "Could not make %s non-blocking: %s\n",
interface, errbuf );
goto err;
}
/* Get file descriptor for select() */
hijack->fd = pcap_get_selectable_fd ( hijack->pcap );
if ( hijack->fd < 0 ) {
logmsg ( LOG_ERR, "Cannot get selectable file descriptor "
"for %s\n", interface );
goto err;
}
/* Get link layer type */
hijack->datalink = pcap_datalink ( hijack->pcap );
return 0;
err:
if ( hijack->pcap )
pcap_close ( hijack->pcap );
return -1;
}
示例15: create_ether_device
ether_device *
create_ether_device( const char *name, const size_t max_send_queue, const size_t max_recv_queue ) {
assert( name != NULL );
assert( strlen( name ) > 0 );
assert( max_send_queue > 0 );
assert( max_recv_queue > 0 );
int nfd = socket( PF_INET, SOCK_DGRAM, 0 );
if ( nfd < 0 ) {
char error_string[ ERROR_STRING_SIZE ];
error( "Failed to open a socket ( ret = %d, errno = %s [%d] ).",
nfd, safe_strerror_r( errno, error_string, sizeof( error_string ) ), errno );
return NULL;
}
struct ifreq ifr;
memset( &ifr, 0, sizeof( ifr ) );
strncpy( ifr.ifr_name, name, IFNAMSIZ );
ifr.ifr_name[ IFNAMSIZ - 1 ] = '\0';
int ret = ioctl( nfd, SIOCGIFINDEX, &ifr );
if ( ret == -1 ) {
char error_string[ ERROR_STRING_SIZE ];
error( "Failed to retrieve an interface index of %s ( ret = %d, errno = %s [%d] ).",
name, ret, safe_strerror_r( errno, error_string, sizeof( error_string ) ), errno );
close( nfd );
return NULL;
}
int ifindex = ifr.ifr_ifindex;
ret = ioctl( nfd, SIOCGIFMTU, &ifr );
if ( ret == -1 ) {
char error_string[ ERROR_STRING_SIZE ];
error( "Failed to retrieve MTU of %s ( ret = %d, errno = %s [%d] ).",
name, ret, safe_strerror_r( errno, error_string, sizeof( error_string ) ), errno );
close( nfd );
return NULL;
}
int mtu = ifr.ifr_mtu;
ret = ioctl( nfd, SIOCGIFHWADDR, &ifr );
if ( ret == -1 ) {
char error_string[ ERROR_STRING_SIZE ];
error( "Failed to retrieve hardware address of %s ( ret = %d, error = %s [%d] ).",
name, ret, safe_strerror_r( errno, error_string, sizeof( error_string ) ), errno );
close( nfd );
return NULL;
}
close( nfd );
size_t device_mtu = ( size_t ) mtu + MAX_L2_HEADER_LENGTH;
#ifdef WITH_PCAP
char errbuf[ PCAP_ERRBUF_SIZE ];
pcap_t *handle = pcap_open_live( name, ( int ) device_mtu, 1, 100, errbuf );
if( handle == NULL ) {
error( "Failed to open %s ( %s ).", name, errbuf );
return NULL;
}
if ( pcap_setnonblock( handle, 1, errbuf ) == -1 ) {
warn( "Failed to setnonblock %s ( %s ).", name, errbuf );
}
int fd = pcap_get_selectable_fd( handle );
#else // WITH_PCAP
int fd = socket( PF_PACKET, SOCK_RAW, htons( ETH_P_ALL ) );
if ( fd < 0 ) {
char error_string[ ERROR_STRING_SIZE ];
error( "Failed to open a socket ( ret = %d, errno = %s [%d] ).",
fd, safe_strerror_r( errno, error_string, sizeof( error_string ) ), errno );
return NULL;
}
struct sockaddr_ll sll;
memset( &sll, 0, sizeof( sll ) );
sll.sll_family = AF_PACKET;
sll.sll_protocol = htons( ETH_P_ALL );
sll.sll_ifindex = ifindex;
ret = bind( fd, ( struct sockaddr * ) &sll, sizeof( sll ) );
if ( ret < 0 ) {
char error_string[ ERROR_STRING_SIZE ];
error( "Failed to bind ( fd = %d, ret = %d, errno = %s [%d] ).",
fd, ret, safe_strerror_r( errno, error_string, sizeof( error_string ) ), errno );
close( fd );
return NULL;
}
int val = TPACKET_V2;
ret = setsockopt( fd, SOL_PACKET, PACKET_VERSION, &val, sizeof( val ) );
if ( ret < 0 ) {
char error_string[ ERROR_STRING_SIZE ];
error( "Failed to set PACKET_VERSION to %d ( fd = %d, ret = %d, errno = %s [%d] ).",
val, fd, ret, safe_strerror_r( errno, error_string, sizeof( error_string ) ), errno );
close( fd );
return NULL;
}
val = 1;
ret = setsockopt( fd, SOL_PACKET, PACKET_AUXDATA, &val, sizeof( val ) );
if ( ret < 0 ) {
char error_string[ ERROR_STRING_SIZE ];
error( "Failed to set PACKET_AUXDATA to %d ( fd = %d, ret = %d, errno = %s [%d] ).",
//.........这里部分代码省略.........