The first two patches are fixes/enhancements.
The third one is only for review (works for me without problems, but I don't see any improvement); it's on you, if you apply this.
Vitezslav Samel (3): packet_get(): don't try to receive packet in case of error packet_get(): propagate errors to the callers and check for them packet_get(): use poll() instead of select()
src/detstats.c | 6 +++++- src/hostmon.c | 6 +++++- src/ifstats.c | 6 +++++- src/iptraf-ng-compat.h | 1 + src/itrafmon.c | 6 +++++- src/packet.c | 43 ++++++++++++++++++++++--------------------- src/packet.h | 2 +- src/pktsize.c | 6 +++++- src/serv.c | 6 +++++- 9 files changed, 54 insertions(+), 28 deletions(-)
Don't try to receive packet or get keyboard press in case of select() returns error or zero fd's prepared.
Signed-off-by: Vitezslav Samel vitezslav@samel.cz --- src/packet.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/packet.c b/src/packet.c index 3e7181a..b48e98f 100644 --- a/src/packet.c +++ b/src/packet.c @@ -154,7 +154,7 @@ void packet_get(int fd, struct pkt_hdr *pkt, int *ch, WINDOW *win) } while ((ss < 0) && (errno == EINTR));
pkt->pkt_len = 0; /* signalize we have no packet prepared */ - if (FD_ISSET(fd, &set)) { + if ((ss > 0) && FD_ISSET(fd, &set)) { struct sockaddr_ll from; socklen_t fromlen = sizeof(struct sockaddr_ll); ssize_t len; @@ -176,7 +176,7 @@ void packet_get(int fd, struct pkt_hdr *pkt, int *ch, WINDOW *win) }
*ch = ERR; /* signalize we have no key ready */ - if (!daemonized && FD_ISSET(0, &set)) + if (!daemonized && (ss > 0) && FD_ISSET(0, &set)) *ch = wgetch(win); }
Signed-off-by: Vitezslav Samel vitezslav@samel.cz --- src/detstats.c | 6 +++++- src/hostmon.c | 6 +++++- src/ifstats.c | 6 +++++- src/itrafmon.c | 6 +++++- src/packet.c | 7 +++++-- src/packet.h | 2 +- src/pktsize.c | 6 +++++- src/serv.c | 6 +++++- 8 files changed, 36 insertions(+), 9 deletions(-)
diff --git a/src/detstats.c b/src/detstats.c index e3de9a0..8a07dc0 100644 --- a/src/detstats.c +++ b/src/detstats.c @@ -508,7 +508,11 @@ void detstats(char *iface, const struct OPTIONS *options, time_t facilitytime, && (((now - statbegin) / 60) >= facilitytime)) exitloop = 1;
- packet_get(fd, &pkt, &ch, statwin); + if (packet_get(fd, &pkt, &ch, statwin) == -1) { + write_error("Packet receive failed"); + exitloop = 1; + break; + }
switch (ch) { case ERR: diff --git a/src/hostmon.c b/src/hostmon.c index e9f3dc0..76ac805 100644 --- a/src/hostmon.c +++ b/src/hostmon.c @@ -908,7 +908,11 @@ void hostmon(const struct OPTIONS *options, time_t facilitytime, char *ifptr, && (((now - statbegin) / 60) >= facilitytime)) exitloop = 1;
- packet_get(fd, &pkt, &ch, table.tabwin); + if (packet_get(fd, &pkt, &ch, table.tabwin) == -1) { + write_error("Packet receive failed"); + exitloop = 1; + break; + }
if (ch != ERR) { if (keymode == 0) { diff --git a/src/ifstats.c b/src/ifstats.c index 8b336bc..0e0bf49 100644 --- a/src/ifstats.c +++ b/src/ifstats.c @@ -563,7 +563,11 @@ void ifstats(const struct OPTIONS *options, struct filterstate *ofilter, && (((now - statbegin) / 60) >= facilitytime)) exitloop = 1;
- packet_get(fd, &pkt, &ch, table.statwin); + if (packet_get(fd, &pkt, &ch, table.statwin) == -1) { + write_error("Packet receive failed"); + exitloop = 1; + break; + }
switch (ch) { case ERR: diff --git a/src/itrafmon.c b/src/itrafmon.c index ebe053b..02a4626 100644 --- a/src/itrafmon.c +++ b/src/itrafmon.c @@ -820,7 +820,11 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter, rotate_flag = 0; }
- packet_get(fd, &pkt, &ch, table.tcpscreen); + if (packet_get(fd, &pkt, &ch, table.tcpscreen) == -1) { + write_error("Packet receive failed"); + exitloop = 1; + break; + }
if (ch == ERR) goto no_key_ready; diff --git a/src/packet.c b/src/packet.c index b48e98f..e7f8371 100644 --- a/src/packet.c +++ b/src/packet.c @@ -130,7 +130,7 @@ static int packet_adjust(struct pkt_hdr *pkt) }
/* IPTraf input function; reads both keystrokes and network packets. */ -void packet_get(int fd, struct pkt_hdr *pkt, int *ch, WINDOW *win) +int packet_get(int fd, struct pkt_hdr *pkt, int *ch, WINDOW *win) { fd_set set; int ss; @@ -172,12 +172,15 @@ void packet_get(int fd, struct pkt_hdr *pkt, int *ch, WINDOW *win) pkt->pkt_ifindex = from.sll_ifindex; pkt->pkt_hatype = from.sll_hatype; pkt->pkt_pkttype = from.sll_pkttype; - } + } else + ss = len; }
*ch = ERR; /* signalize we have no key ready */ if (!daemonized && (ss > 0) && FD_ISSET(0, &set)) *ch = wgetch(win); + + return ss; }
int packet_process(struct pkt_hdr *pkt, unsigned int *total_br, diff --git a/src/packet.h b/src/packet.h index 185a229..dd24533 100644 --- a/src/packet.h +++ b/src/packet.h @@ -46,7 +46,7 @@ struct pkt_hdr { };
void open_socket(int *fd); -void packet_get(int fd, struct pkt_hdr *pkt, int *ch, WINDOW *win); +int packet_get(int fd, struct pkt_hdr *pkt, int *ch, WINDOW *win); int packet_process(struct pkt_hdr *pkt, unsigned int *total_br, unsigned int *sport, unsigned int *dport, struct filterstate *filter, int match_opposite, diff --git a/src/pktsize.c b/src/pktsize.c index 7dfcb92..8ec0a2c 100644 --- a/src/pktsize.c +++ b/src/pktsize.c @@ -299,7 +299,11 @@ void packet_size_breakdown(struct OPTIONS *options, char *ifname, && (((now - starttime) / 60) >= facilitytime)) exitloop = 1;
- packet_get(fd, &pkt, &ch, win); + if (packet_get(fd, &pkt, &ch, win) == -1) { + write_error("Packet receive failed"); + exitloop = 1; + break; + }
if (ch != ERR) { switch (ch) { diff --git a/src/serv.c b/src/serv.c index 7bbcdf7..b9f1f59 100644 --- a/src/serv.c +++ b/src/serv.c @@ -961,7 +961,11 @@ void servmon(char *ifname, struct porttab *ports, const struct OPTIONS *options, && (((now - starttime) / 60) >= facilitytime)) exitloop = 1;
- packet_get(fd, &pkt, &ch, list.win); + if (packet_get(fd, &pkt, &ch, list.win) == -1) { + write_error("Packet receive failed"); + exitloop = 1; + break; + }
if (ch == ERR) goto no_key_ready;
Signed-off-by: Vitezslav Samel vitezslav@samel.cz --- src/iptraf-ng-compat.h | 1 + src/packet.c | 36 +++++++++++++++++------------------- 2 files changed, 18 insertions(+), 19 deletions(-)
diff --git a/src/iptraf-ng-compat.h b/src/iptraf-ng-compat.h index ac8b612..90d85bd 100644 --- a/src/iptraf-ng-compat.h +++ b/src/iptraf-ng-compat.h @@ -16,6 +16,7 @@ #include <panel.h> #include <assert.h> #include <stddef.h> +#include <poll.h>
#include <sys/types.h> #include <sys/stat.h> diff --git a/src/packet.c b/src/packet.c index e7f8371..4f02a34 100644 --- a/src/packet.c +++ b/src/packet.c @@ -132,29 +132,27 @@ static int packet_adjust(struct pkt_hdr *pkt) /* IPTraf input function; reads both keystrokes and network packets. */ int packet_get(int fd, struct pkt_hdr *pkt, int *ch, WINDOW *win) { - fd_set set; + struct pollfd pfds[2]; + nfds_t nfds = 0; int ss;
- do { - FD_ZERO(&set); - - /* Monitor stdin only if in interactive, not daemon mode. */ - if (!daemonized) - FD_SET(0, &set); - - /* Monitor raw socket */ - FD_SET(fd, &set); + /* Monitor raw socket */ + pfds[0].fd = fd; + pfds[0].events = POLLIN; + nfds++;
- struct timeval tv = { - .tv_sec = 0, - .tv_usec = DEFAULT_UPDATE_DELAY - }; - - ss = select(fd + 1, &set, 0, 0, &tv); - } while ((ss < 0) && (errno == EINTR)); + /* Monitor stdin only if in interactive, not daemon mode. */ + if (!daemonized) { + pfds[1].fd = 0; + pfds[1].events = POLLIN; + nfds++; + } + do { + ss = poll(pfds, nfds, DEFAULT_UPDATE_DELAY / 1000); + } while ((ss == -1) && (errno == EINTR));
pkt->pkt_len = 0; /* signalize we have no packet prepared */ - if ((ss > 0) && FD_ISSET(fd, &set)) { + if ((ss > 0) && (pfds[0].revents & POLLIN) != 0) { struct sockaddr_ll from; socklen_t fromlen = sizeof(struct sockaddr_ll); ssize_t len; @@ -177,7 +175,7 @@ int packet_get(int fd, struct pkt_hdr *pkt, int *ch, WINDOW *win) }
*ch = ERR; /* signalize we have no key ready */ - if (!daemonized && (ss > 0) && FD_ISSET(0, &set)) + if (!daemonized && (ss > 0) && ((pfds[1].revents & POLLIN) != 0)) *ch = wgetch(win);
return ss;
Vitezslav Samel vitezslav@samel.cz writes:
The first two patches are fixes/enhancements.
The third one is only for review (works for me without problems, but I don't see any improvement); it's on you, if you apply this.
Vitezslav Samel (3): packet_get(): don't try to receive packet in case of error packet_get(): propagate errors to the callers and check for them packet_get(): use poll() instead of select()
src/detstats.c | 6 +++++- src/hostmon.c | 6 +++++- src/ifstats.c | 6 +++++- src/iptraf-ng-compat.h | 1 + src/itrafmon.c | 6 +++++- src/packet.c | 43 ++++++++++++++++++++++--------------------- src/packet.h | 2 +- src/pktsize.c | 6 +++++- src/serv.c | 6 +++++- 9 files changed, 54 insertions(+), 28 deletions(-)
applied.
iptraf-ng@lists.fedorahosted.org