]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-radv: do not use goto for non-error-handling cases
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 19 Dec 2021 00:03:16 +0000 (09:03 +0900)
committerLuca Boccassi <luca.boccassi@gmail.com>
Sun, 19 Dec 2021 12:23:14 +0000 (12:23 +0000)
Follow-up for 059d7b6eae827e73c73633bcc40fe74f3696f3f6.

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.

src/libsystemd-network/sd-radv.c

index a305171902cd3f360ca5575df83e4df091bf33b2..e332f6a2ab93796ab42d753d9f166e9cacd63c5c 100644 (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,22 +604,8 @@ 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. */
-                        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));
-
-                        goto announce;
+                        update = true;
+                        break;
                 }
 
                 _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));
         }
 
-        sd_radv_prefix_ref(p);
-        LIST_APPEND(prefix, ra->prefixes, p);
-        ra->n_prefixes++;
+        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. */
+                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));
+                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,21 +708,8 @@ 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. */
-                        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));
-
-                        goto announce;
+                        update = true;
+                        break;
                 }
 
                 _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));
         }
 
-        sd_radv_route_prefix_ref(p);
-        LIST_APPEND(prefix, ra->route_prefixes, p);
-        ra->n_route_prefixes++;
+        if (update) {
+                assert(cur);
 
-        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)
                 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;
 }