diff options
author | Anas Nashif <anas.nashif@intel.com> | 2012-11-04 06:56:27 -0800 |
---|---|---|
committer | Anas Nashif <anas.nashif@intel.com> | 2012-11-04 06:56:27 -0800 |
commit | 71040993a907ce0e6bb2c9fb2f824a0bf963afe6 (patch) | |
tree | 927da213803e6fee353280bfcf12b7d8613f8124 | |
parent | 70ea5de2f910c744e52ce1dd6d4d6edb0e1e3339 (diff) | |
download | libpcap-71040993a907ce0e6bb2c9fb2f824a0bf963afe6.tar.gz libpcap-71040993a907ce0e6bb2c9fb2f824a0bf963afe6.tar.bz2 libpcap-71040993a907ce0e6bb2c9fb2f824a0bf963afe6.zip |
filter fix
-rw-r--r-- | pcap-bpf.c | 2 | ||||
-rw-r--r-- | pcap-linux.c | 22 |
2 files changed, 23 insertions, 1 deletions
@@ -487,7 +487,7 @@ bpf_open(pcap_t *p) fd = open(device, O_RDWR); if (fd == -1 && errno == EACCES) fd = open(device, O_RDONLY); - } while (fd < 0 && errno == EBUSY); + } while (fd < 0 && errno == EBUSY && n < 1000); /* * XXX better message for all minors used diff --git a/pcap-linux.c b/pcap-linux.c index 82a9025..f93335f 100644 --- a/pcap-linux.c +++ b/pcap-linux.c @@ -2437,8 +2437,30 @@ pcap_setfilter_linux_common(pcap_t *handle, struct bpf_program *filter, if (can_filter_in_kernel) { if ((err = set_kernel_filter(handle, &fcode)) == 0) { + char buf[1024]; + int oldflags; + int ret; + unsigned int received = 0, rec_len = 0; + socklen_t optlen = sizeof(rec_len); /* Installation succeded - using kernel filter. */ handle->md.use_bpf = 1; + + oldflags = fcntl(handle->fd, F_GETFL, 0); + oldflags |= O_NONBLOCK; + fcntl(handle->fd, F_SETFL, oldflags); + getsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF, + (char *)&rec_len, &optlen); + + /* now read all packets received until now */ + while((ret = read(handle->fd, buf, 1024)) > 0 + && received < rec_len) { + received += ret; + } + + if(oldflags > 0) { + oldflags &= ~O_NONBLOCK; + fcntl(handle->fd, F_SETFL, oldflags); + } } else if (err == -1) /* Non-fatal error */ { |