xsys: functions that are not really inline-able went into the xsys.c
[netsniff-ng.git] / src / poll.h
blobaac37c3483d8993b6884701d5cce8afda8e86415
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, version 2.
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
28 #define POLL_NEXT_PKT 0
29 #define POLL_MOVE_OUT 1
31 static inline void prepare_polling(int sock, struct pollfd *pfd)
33 memset(pfd, 0, sizeof(*pfd));
34 pfd->fd = sock;
35 pfd->revents = 0;
36 pfd->events = POLLIN | POLLRDNORM | POLLERR;
39 static inline int poll_error_maybe_die(int sock, struct pollfd *pfd)
41 if ((pfd->revents & (POLLHUP | POLLRDHUP | POLLERR | POLLNVAL)) == 0)
42 return POLL_NEXT_PKT;
43 if (pfd->revents & (POLLHUP | POLLRDHUP))
44 panic("Hangup on socket occured!\n");
45 if (pfd->revents & POLLERR) {
46 int tmp;
47 errno = 0;
48 /* recv is more specififc on the error */
49 if (recv(sock, &tmp, sizeof(tmp), MSG_PEEK) >= 0)
50 return POLL_NEXT_PKT;
51 if (errno == ENETDOWN)
52 panic("Interface went down!\n");
53 return POLL_MOVE_OUT;
55 if (pfd->revents & POLLNVAL) {
56 whine("Invalid polling request on socket!\n");
57 return POLL_MOVE_OUT;
59 return POLL_NEXT_PKT;
62 #endif /* POLL_H */