Skip to content

Commit

Permalink
arping: use additional timerfd to control when timeout happens
Browse files Browse the repository at this point in the history
Trying to determine timeout by adding up interval values is pointlessly
complicating.  With separate timer everything just works.

Addresses: iputils#259
Fixes: 1df5350
Signed-off-by: Sami Kerola <[email protected]>
  • Loading branch information
kerolasa committed Mar 8, 2020
1 parent 7b91790 commit e594ca5
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions arping.c
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ static int event_loop(struct run_state *ctl)
enum {
POLLFD_SIGNAL = 0,
POLLFD_TIMER,
POLLFD_TIMEOUT,
POLLFD_SOCKET,
POLLFD_COUNT
};
Expand All @@ -686,6 +687,13 @@ static int event_loop(struct run_state *ctl)
.it_value.tv_sec = ctl->interval,
.it_value.tv_nsec = 0
};
int timeoutfd;
struct itimerspec timeoutfd_vals = {
.it_interval.tv_sec = ctl->timeout,
.it_interval.tv_nsec = 0,
.it_value.tv_sec = ctl->timeout,
.it_value.tv_nsec = 0
};
uint64_t exp, total_expires = 1;

unsigned char packet[4096];
Expand All @@ -710,7 +718,7 @@ static int event_loop(struct run_state *ctl)
pfds[POLLFD_SIGNAL].fd = sfd;
pfds[POLLFD_SIGNAL].events = POLLIN | POLLERR | POLLHUP;

/* timerfd */
/* interval timerfd */
tfd = timerfd_create(CLOCK_MONOTONIC, 0);
if (tfd == -1) {
error(0, errno, "timerfd_create failed");
Expand All @@ -723,6 +731,19 @@ static int event_loop(struct run_state *ctl)
pfds[POLLFD_TIMER].fd = tfd;
pfds[POLLFD_TIMER].events = POLLIN | POLLERR | POLLHUP;

/* timeout timerfd */
timeoutfd = timerfd_create(CLOCK_MONOTONIC, 0);
if (tfd == -1) {
error(0, errno, "timerfd_create failed");
return 1;
}
if (timerfd_settime(timeoutfd, 0, &timeoutfd_vals, NULL)) {
error(0, errno, "timerfd_settime failed");
return 1;
}
pfds[POLLFD_TIMEOUT].fd = timeoutfd;
pfds[POLLFD_TIMEOUT].events = POLLIN | POLLERR | POLLHUP;

/* socket */
pfds[POLLFD_SOCKET].fd = ctl->socketfd;
pfds[POLLFD_SOCKET].events = POLLIN | POLLERR | POLLHUP;
Expand Down Expand Up @@ -765,13 +786,15 @@ static int event_loop(struct run_state *ctl)
continue;
}
total_expires += exp;
if ((0 < ctl->count && (uint64_t)ctl->count < total_expires) ||
(ctl->quit_on_reply && ctl->timeout < (long)total_expires)) {
if (0 < ctl->count && (uint64_t)ctl->count < total_expires) {
exit_loop = 1;
continue;
}
send_pack(ctl);
break;
case POLLFD_TIMEOUT:
exit_loop = 1;
break;
case POLLFD_SOCKET:
if ((s =
recvfrom(ctl->socketfd, packet, sizeof(packet), 0,
Expand Down

0 comments on commit e594ca5

Please sign in to comment.