1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
/*
mtr -- a network diagnostic tool
Copyright (C) 2016 Matt Kimball
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "wait.h"
#include <assert.h>
#include <errno.h>
#ifdef HAVE_ERROR_H
#include <error.h>
#else
#include "portability/error.h"
#endif
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
/*
Gather all the file descriptors which should wake our select call when
they become readable.
*/
static
int gather_read_fds(
const struct command_buffer_t *command_buffer,
const struct net_state_t *net_state,
fd_set * read_set,
fd_set * write_set)
{
int nfds;
int probe_nfds;
int ip4_socket;
int ip6_socket;
int command_stream = command_buffer->command_stream;
FD_ZERO(read_set);
FD_ZERO(write_set);
FD_SET(command_stream, read_set);
nfds = command_stream + 1;
if (net_state->platform.ip4_socket_raw) {
ip4_socket = net_state->platform.ip4_recv_socket;
FD_SET(ip4_socket, read_set);
if (ip4_socket >= nfds) {
nfds = ip4_socket + 1;
}
} else {
ip4_socket = net_state->platform.ip4_txrx_icmp_socket;
FD_SET(ip4_socket, read_set);
if (ip4_socket >= nfds) {
nfds = ip4_socket + 1;
}
ip4_socket = net_state->platform.ip4_txrx_udp_socket;
FD_SET(ip4_socket, read_set);
if (ip4_socket >= nfds) {
nfds = ip4_socket + 1;
}
}
if (net_state->platform.ip6_socket_raw) {
ip6_socket = net_state->platform.ip6_recv_socket;
FD_SET(ip6_socket, read_set);
if (ip6_socket >= nfds) {
nfds = ip6_socket + 1;
}
} else {
ip6_socket = net_state->platform.ip6_txrx_icmp_socket;
FD_SET(ip6_socket, read_set);
if (ip6_socket >= nfds) {
nfds = ip6_socket + 1;
}
ip6_socket = net_state->platform.ip6_txrx_udp_socket;
FD_SET(ip6_socket, read_set);
if (ip6_socket >= nfds) {
nfds = ip6_socket + 1;
}
}
probe_nfds = gather_probe_sockets(net_state, write_set);
if (probe_nfds > nfds) {
nfds = probe_nfds;
}
return nfds;
}
/*
Sleep until we receive a new probe response, a new command on the
command stream, or a probe timeout. On Unix systems, this means
we use select to wait on file descriptors for the command stream
and the raw receive socket.
*/
void wait_for_activity(
struct command_buffer_t *command_buffer,
struct net_state_t *net_state)
{
int nfds;
fd_set read_set;
fd_set write_set;
struct timeval probe_timeout;
struct timeval *select_timeout;
int ready_count;
nfds =
gather_read_fds(command_buffer, net_state, &read_set, &write_set);
while (true) {
select_timeout = NULL;
/* Use the soonest probe timeout time as our maximum wait time */
if (get_next_probe_timeout(net_state, &probe_timeout)) {
assert(probe_timeout.tv_sec >= 0);
select_timeout = &probe_timeout;
}
ready_count =
select(nfds, &read_set, &write_set, NULL, select_timeout);
/*
If we didn't have an error, either one of our descriptors is
readable, or we timed out. So we can now return.
*/
if (ready_count != -1) {
break;
}
/*
We will get EINTR if we received a signal during the select, so
retry in that case. We may get EAGAIN if "the kernel was
(perhaps temporarily) unable to allocate the requested number of
file descriptors." I haven't seen this in practice, but selecting
again seems like the right thing to do.
*/
if (errno != EINTR && errno != EAGAIN) {
/* We don't expect other errors, so report them */
error(EXIT_FAILURE, errno, "unexpected select error");
}
}
}
|