1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-22 13:33:56 +03:00

sd-radv: do not use goto for non-error-handling cases

Follow-up for 059d7b6eae.

The comment https://github.com/systemd/systemd/pull/21814#discussion_r771842132
suggests to introduce new helper, but it is used only one place.
Let's not add such, but simply replace the goto with a flag.
This commit is contained in:
Yu Watanabe 2021-12-19 09:03:16 +09:00 committed by Luca Boccassi
parent 73b3314bfd
commit b9f27a0567

View File

@ -579,6 +579,7 @@ int sd_radv_set_preference(sd_radv *ra, unsigned preference) {
int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p) {
_cleanup_free_ char *addr_p = NULL;
sd_radv_prefix *cur;
bool update = false;
int r;
assert_return(ra, -EINVAL);
@ -603,7 +604,21 @@ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p) {
continue;
if (cur->opt.prefixlen == p->opt.prefixlen) {
/* p and cur may be equivalent. First increment the counter. */
update = true;
break;
}
_cleanup_free_ char *addr_cur = NULL;
(void) in6_addr_prefix_to_string(&cur->opt.in6_addr, cur->opt.prefixlen, &addr_cur);
return log_radv_errno(ra, SYNTHETIC_ERRNO(EEXIST),
"IPv6 prefix %s conflicts with %s, ignoring.",
strna(addr_p), strna(addr_cur));
}
if (update) {
assert(cur);
/* p and cur may be equivalent. First increment the reference counter. */
sd_radv_prefix_ref(p);
/* Then, remove the old entry. */
@ -617,36 +632,29 @@ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p) {
strna(addr_p),
FORMAT_TIMESPAN(p->lifetime_preferred_usec, USEC_PER_SEC),
FORMAT_TIMESPAN(p->lifetime_valid_usec, USEC_PER_SEC));
goto announce;
}
_cleanup_free_ char *addr_cur = NULL;
(void) in6_addr_prefix_to_string(&cur->opt.in6_addr, cur->opt.prefixlen, &addr_cur);
return log_radv_errno(ra, SYNTHETIC_ERRNO(EEXIST),
"IPv6 prefix %s conflicts with %s, ignoring.",
strna(addr_p), strna(addr_cur));
}
} else {
/* The prefix is new. Let's simply add it. */
sd_radv_prefix_ref(p);
LIST_APPEND(prefix, ra->prefixes, p);
ra->n_prefixes++;
log_radv(ra, "Added prefix %s", strna(addr_p));
}
if (ra->state == RADV_STATE_IDLE)
return 0;
announce:
if (ra->ra_sent == 0)
return 0;
/* If RAs have already been sent, send an RA immediately to announce the newly-added prefix */
r = radv_send(ra, NULL, ra->lifetime_usec);
if (r < 0)
log_radv_errno(ra, r, "Unable to send Router Advertisement for added prefix: %m");
log_radv_errno(ra, r, "Unable to send Router Advertisement for added prefix %s: %m",
strna(addr_p));
else
log_radv(ra, "Sent Router Advertisement for added/updated prefix");
log_radv(ra, "Sent Router Advertisement for added/updated prefix %s.", strna(addr_p));
return 0;
}
@ -679,6 +687,7 @@ sd_radv_prefix *sd_radv_remove_prefix(sd_radv *ra,
int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p) {
_cleanup_free_ char *addr_p = NULL;
sd_radv_route_prefix *cur;
bool update = false;
int r;
assert_return(ra, -EINVAL);
@ -699,7 +708,21 @@ int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p) {
continue;
if (cur->opt.prefixlen == p->opt.prefixlen) {
/* p and cur may be equivalent. First increment the counter. */
update = true;
break;
}
_cleanup_free_ char *addr_cur = NULL;
(void) in6_addr_prefix_to_string(&cur->opt.in6_addr, cur->opt.prefixlen, &addr_cur);
return log_radv_errno(ra, SYNTHETIC_ERRNO(EEXIST),
"IPv6 route prefix %s conflicts with %s, ignoring.",
strna(addr_p), strna(addr_cur));
}
if (update) {
assert(cur);
/* p and cur may be equivalent. First increment the reference counter. */
sd_radv_route_prefix_ref(p);
/* Then, remove the old entry. */
@ -712,36 +735,29 @@ int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p) {
log_radv(ra, "Updated/replaced IPv6 route prefix %s (lifetime: %s)",
strna(addr_p),
FORMAT_TIMESPAN(p->lifetime_usec, USEC_PER_SEC));
goto announce;
}
_cleanup_free_ char *addr_cur = NULL;
(void) in6_addr_prefix_to_string(&cur->opt.in6_addr, cur->opt.prefixlen, &addr_cur);
return log_radv_errno(ra, SYNTHETIC_ERRNO(EEXIST),
"IPv6 route prefix %s conflicts with %s, ignoring.",
strna(addr_p), strna(addr_cur));
}
} else {
/* The route prefix is new. Let's simply add it. */
sd_radv_route_prefix_ref(p);
LIST_APPEND(prefix, ra->route_prefixes, p);
ra->n_route_prefixes++;
log_radv(ra, "Added route prefix %s", strna(addr_p));
}
if (ra->state == RADV_STATE_IDLE)
return 0;
announce:
if (ra->ra_sent == 0)
return 0;
/* If RAs have already been sent, send an RA immediately to announce the newly-added route prefix */
r = radv_send(ra, NULL, ra->lifetime_usec);
if (r < 0)
log_radv_errno(ra, r, "Unable to send Router Advertisement for added route prefix: %m");
log_radv_errno(ra, r, "Unable to send Router Advertisement for added route prefix %s: %m",
strna(addr_p));
else
log_radv(ra, "Sent Router Advertisement for added route prefix");
log_radv(ra, "Sent Router Advertisement for added route prefix %s.", strna(addr_p));
return 0;
}