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) { int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p) {
_cleanup_free_ char *addr_p = NULL; _cleanup_free_ char *addr_p = NULL;
sd_radv_prefix *cur; sd_radv_prefix *cur;
bool update = false;
int r; int r;
assert_return(ra, -EINVAL); assert_return(ra, -EINVAL);
@ -603,22 +604,8 @@ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p) {
continue; continue;
if (cur->opt.prefixlen == p->opt.prefixlen) { if (cur->opt.prefixlen == p->opt.prefixlen) {
/* p and cur may be equivalent. First increment the counter. */ update = true;
sd_radv_prefix_ref(p); break;
/* Then, remove the old entry. */
LIST_REMOVE(prefix, ra->prefixes, cur);
sd_radv_prefix_unref(cur);
/* Finally, add the new entry. */
LIST_APPEND(prefix, ra->prefixes, p);
log_radv(ra, "Updated/replaced IPv6 prefix %s (preferred: %s, valid: %s)",
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; _cleanup_free_ char *addr_cur = NULL;
@ -628,25 +615,46 @@ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p) {
strna(addr_p), strna(addr_cur)); strna(addr_p), strna(addr_cur));
} }
sd_radv_prefix_ref(p); if (update) {
LIST_APPEND(prefix, ra->prefixes, p); assert(cur);
ra->n_prefixes++;
log_radv(ra, "Added prefix %s", strna(addr_p)); /* p and cur may be equivalent. First increment the reference counter. */
sd_radv_prefix_ref(p);
/* Then, remove the old entry. */
LIST_REMOVE(prefix, ra->prefixes, cur);
sd_radv_prefix_unref(cur);
/* Finally, add the new entry. */
LIST_APPEND(prefix, ra->prefixes, p);
log_radv(ra, "Updated/replaced IPv6 prefix %s (preferred: %s, valid: %s)",
strna(addr_p),
FORMAT_TIMESPAN(p->lifetime_preferred_usec, USEC_PER_SEC),
FORMAT_TIMESPAN(p->lifetime_valid_usec, USEC_PER_SEC));
} 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) if (ra->state == RADV_STATE_IDLE)
return 0; return 0;
announce:
if (ra->ra_sent == 0) if (ra->ra_sent == 0)
return 0; return 0;
/* If RAs have already been sent, send an RA immediately to announce the newly-added prefix */ /* If RAs have already been sent, send an RA immediately to announce the newly-added prefix */
r = radv_send(ra, NULL, ra->lifetime_usec); r = radv_send(ra, NULL, ra->lifetime_usec);
if (r < 0) 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 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; 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) { int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p) {
_cleanup_free_ char *addr_p = NULL; _cleanup_free_ char *addr_p = NULL;
sd_radv_route_prefix *cur; sd_radv_route_prefix *cur;
bool update = false;
int r; int r;
assert_return(ra, -EINVAL); assert_return(ra, -EINVAL);
@ -699,21 +708,8 @@ int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p) {
continue; continue;
if (cur->opt.prefixlen == p->opt.prefixlen) { if (cur->opt.prefixlen == p->opt.prefixlen) {
/* p and cur may be equivalent. First increment the counter. */ update = true;
sd_radv_route_prefix_ref(p); break;
/* Then, remove the old entry. */
LIST_REMOVE(prefix, ra->route_prefixes, cur);
sd_radv_route_prefix_unref(cur);
/* Finally, add the new entry. */
LIST_APPEND(prefix, ra->route_prefixes, 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; _cleanup_free_ char *addr_cur = NULL;
@ -723,25 +719,45 @@ int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p) {
strna(addr_p), strna(addr_cur)); strna(addr_p), strna(addr_cur));
} }
sd_radv_route_prefix_ref(p); if (update) {
LIST_APPEND(prefix, ra->route_prefixes, p); assert(cur);
ra->n_route_prefixes++;
log_radv(ra, "Added route prefix %s", strna(addr_p)); /* p and cur may be equivalent. First increment the reference counter. */
sd_radv_route_prefix_ref(p);
/* Then, remove the old entry. */
LIST_REMOVE(prefix, ra->route_prefixes, cur);
sd_radv_route_prefix_unref(cur);
/* Finally, add the new entry. */
LIST_APPEND(prefix, ra->route_prefixes, p);
log_radv(ra, "Updated/replaced IPv6 route prefix %s (lifetime: %s)",
strna(addr_p),
FORMAT_TIMESPAN(p->lifetime_usec, USEC_PER_SEC));
} 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) if (ra->state == RADV_STATE_IDLE)
return 0; return 0;
announce:
if (ra->ra_sent == 0) if (ra->ra_sent == 0)
return 0; return 0;
/* If RAs have already been sent, send an RA immediately to announce the newly-added route prefix */ /* 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); r = radv_send(ra, NULL, ra->lifetime_usec);
if (r < 0) 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 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; return 0;
} }