libpcap/libpcap-0.9.5-socket-timeout.diff

48 lines
1.1 KiB
Diff

--- pcap-linux.c
+++ pcap-linux.c
@@ -1021,8 +1021,9 @@
handle->md.use_bpf = 1;
oldflags = fcntl(handle->fd, F_GETFL, 0);
- oldflags |= O_NONBLOCK;
- fcntl(handle->fd, F_SETFL, oldflags);
+ if (!(oldflags & O_NONBLOCK))
+ fcntl(handle->fd, F_SETFL, oldflags | O_NONBLOCK);
+
getsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF,
(char *)&rec_len, &optlen);
@@ -1031,11 +1032,9 @@
&& received < rec_len) {
received += ret;
}
-
- if(oldflags > 0) {
- oldflags &= ~O_NONBLOCK;
+
+ if(!(oldflags & O_NONBLOCK))
fcntl(handle->fd, F_SETFL, oldflags);
- }
}
else if (err == -1) /* Non-fatal error */
{
@@ -1594,6 +1593,18 @@
}
}
+ if (to_ms > 0) {
+ struct timeval timeout;
+ timeout.tv_sec = to_ms / 1000;
+ timeout.tv_usec = (to_ms * 1000) % 1000000;
+ if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO,
+ &timeout, sizeof(timeout)) == -1) {
+ snprintf(ebuf, PCAP_ERRBUF_SIZE,
+ "setsockopt: %s", pcap_strerror(errno));
+ break;
+ };
+ }
+
/* Save the socket FD in the pcap structure */
handle->fd = sock_fd;