sd-ndisc: several trivial cleanups

- update several log messages,
- use event_reset_time_relative(),
- split out ndisc_setup_recv_event() and ndisc_setup_timer().

No functional change, just refactoring and preparation for later commits.
This commit is contained in:
Yu Watanabe
2024-02-24 12:37:50 +09:00
parent 37c011e74c
commit 6a27ca08c6
+59 -35
View File
@@ -234,19 +234,19 @@ static int ndisc_recv(sd_event_source *s, int fd, uint32_t revents, void *userda
if (r < 0)
switch (r) {
case -EADDRNOTAVAIL:
log_ndisc(nd, "Received RA from neither link-local nor null address. Ignoring.");
log_ndisc(nd, "Received an ICMPv6 packet from neither link-local nor null address, ignoring.");
return 0;
case -EMULTIHOP:
log_ndisc(nd, "Received RA with invalid hop limit. Ignoring.");
log_ndisc(nd, "Received an ICMPv6 packet with an invalid hop limit, ignoring.");
return 0;
case -EPFNOSUPPORT:
log_ndisc(nd, "Received invalid source address from ICMPv6 socket. Ignoring.");
log_ndisc(nd, "Received an ICMPv6 packet with an invalid source address, ignoring.");
return 0;
default:
log_ndisc_errno(nd, r, "Unexpected error while reading from ICMPv6, ignoring: %m");
log_ndisc_errno(nd, r, "Unexpected error while receiving an ICMPv6 packet, ignoring: %m");
return 0;
}
@@ -335,9 +335,61 @@ int sd_ndisc_stop(sd_ndisc *nd) {
return 1;
}
static int ndisc_setup_recv_event(sd_ndisc *nd) {
int r;
assert(nd);
assert(nd->event);
assert(nd->ifindex > 0);
_cleanup_close_ int fd = -EBADF;
fd = icmp6_bind(nd->ifindex, /* is_router = */ false);
if (fd < 0)
return fd;
_cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
r = sd_event_add_io(nd->event, &s, fd, EPOLLIN, ndisc_recv, nd);
if (r < 0)
return r;
r = sd_event_source_set_priority(s, nd->event_priority);
if (r < 0)
return r;
(void) sd_event_source_set_description(s, "ndisc-receive-router-message");
nd->fd = TAKE_FD(fd);
nd->recv_event_source = TAKE_PTR(s);
return 1;
}
static int ndisc_setup_timer(sd_ndisc *nd) {
int r;
assert(nd);
assert(nd->event);
r = event_reset_time_relative(nd->event, &nd->timeout_event_source,
CLOCK_BOOTTIME,
USEC_PER_SEC / 2, 1 * USEC_PER_SEC, /* See RFC 8415 sec. 18.2.1 */
ndisc_timeout, nd,
nd->event_priority, "ndisc-timeout", true);
if (r < 0)
return r;
r = event_reset_time_relative(nd->event, &nd->timeout_no_ra,
CLOCK_BOOTTIME,
NDISC_TIMEOUT_NO_RA_USEC, 10 * USEC_PER_MSEC,
ndisc_timeout_no_ra, nd,
nd->event_priority, "ndisc-timeout-no-ra", true);
if (r < 0)
return r;
return 0;
}
int sd_ndisc_start(sd_ndisc *nd) {
int r;
usec_t time_now;
assert_return(nd, -EINVAL);
assert_return(nd->event, -EINVAL);
@@ -346,39 +398,11 @@ int sd_ndisc_start(sd_ndisc *nd) {
if (sd_ndisc_is_running(nd))
return 0;
assert(!nd->recv_event_source);
r = sd_event_now(nd->event, CLOCK_BOOTTIME, &time_now);
r = ndisc_setup_recv_event(nd);
if (r < 0)
goto fail;
nd->fd = icmp6_bind(nd->ifindex, /* is_router = */ false);
if (nd->fd < 0)
return nd->fd;
r = sd_event_add_io(nd->event, &nd->recv_event_source, nd->fd, EPOLLIN, ndisc_recv, nd);
if (r < 0)
goto fail;
r = sd_event_source_set_priority(nd->recv_event_source, nd->event_priority);
if (r < 0)
goto fail;
(void) sd_event_source_set_description(nd->recv_event_source, "ndisc-receive-message");
r = event_reset_time(nd->event, &nd->timeout_event_source,
CLOCK_BOOTTIME,
time_now + USEC_PER_SEC / 2, 1 * USEC_PER_SEC, /* See RFC 8415 sec. 18.2.1 */
ndisc_timeout, nd,
nd->event_priority, "ndisc-timeout", true);
if (r < 0)
goto fail;
r = event_reset_time(nd->event, &nd->timeout_no_ra,
CLOCK_BOOTTIME,
time_now + NDISC_TIMEOUT_NO_RA_USEC, 10 * USEC_PER_MSEC,
ndisc_timeout_no_ra, nd,
nd->event_priority, "ndisc-timeout-no-ra", true);
r = ndisc_setup_timer(nd);
if (r < 0)
goto fail;