offline pcap reading working
[netsniff-ng.git] / src / poll.h
blobd151f6e37a79ef5e0b3b9224c368c332a1d2d1cb
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Subject to the GPL.
6 */
8 #ifndef POLL_H
9 #define POLL_H
11 #define _GNU_SOURCE
12 #include <poll.h>
13 #include <errno.h>
14 #include <sys/poll.h>
15 #include <sys/socket.h>
17 #include "die.h"
19 #ifndef POLLRDNORM
20 # define POLLRDNORM 0x0040
21 #endif
22 #ifndef POLLWRNORM
23 # define POLLWRNORM 0x0100
24 #endif
25 #ifndef POLLRDHUP
26 # define POLLRDHUP 0x2000
27 #endif
29 #define POLL_NEXT_PKT 0
30 #define POLL_MOVE_OUT 1
32 static inline void prepare_polling(int sock, struct pollfd *pfd)
34 memset(pfd, 0, sizeof(*pfd));
36 pfd->fd = sock;
37 pfd->revents = 0;
38 pfd->events = POLLIN | POLLRDNORM | POLLERR;
41 static inline int poll_error_maybe_die(int sock, struct pollfd *pfd)
43 if ((pfd->revents & (POLLHUP | POLLRDHUP | POLLERR | POLLNVAL)) == 0)
44 return POLL_NEXT_PKT;
46 if (pfd->revents & (POLLHUP | POLLRDHUP))
47 error_and_die(EXIT_FAILURE, "Hangup on socket occured!\n");
49 if (pfd->revents & POLLERR) {
50 int tmp;
52 errno = 0;
53 /* recv is more specififc on the error */
54 if (recv(sock, &tmp, sizeof(tmp), MSG_PEEK) >= 0)
55 return POLL_NEXT_PKT;
57 if (errno == ENETDOWN)
58 error_and_die(EXIT_FAILURE, "Interface went down!\n");
59 return POLL_MOVE_OUT;
62 if (pfd->revents & POLLNVAL) {
63 whine("Invalid polling request on socket!\n");
64 return POLL_MOVE_OUT;
67 return POLL_NEXT_PKT;
70 #endif /* POLL_H */