]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: fix & improve `%pSU` format
authorDavid Lamparter <equinox@diac24.net>
Fri, 26 Mar 2021 13:16:01 +0000 (14:16 +0100)
committerDavid Lamparter <equinox@diac24.net>
Tue, 30 Mar 2021 20:34:56 +0000 (22:34 +0200)
This wasn't quite formatting IPv6+port in a useful way (no brackets),
and printing the scope ID (interface index) and unix addrs is useful
too.

Signed-off-by: David Lamparter <equinox@diac24.net>
lib/sockunion.c
lib/sockunion.h

index 2175ac33607315e613bc739435188e2b64b85a47..37bd3b841e2376cefb0df24cf989fd9731b17fd1 100644 (file)
@@ -668,7 +668,7 @@ static ssize_t printfrr_psu(struct fbuf *buf, struct printfrr_eargs *ea,
                            const void *ptr)
 {
        const union sockunion *su = ptr;
-       bool include_port = false;
+       bool include_port = false, include_scope = false;
        bool endflags = false;
        ssize_t ret = 0;
        char cbuf[INET6_ADDRSTRLEN];
@@ -682,6 +682,10 @@ static ssize_t printfrr_psu(struct fbuf *buf, struct printfrr_eargs *ea,
                        ea->fmt++;
                        include_port = true;
                        break;
+               case 's':
+                       ea->fmt++;
+                       include_scope = true;
+                       break;
                default:
                        endflags = true;
                        break;
@@ -696,14 +700,35 @@ static ssize_t printfrr_psu(struct fbuf *buf, struct printfrr_eargs *ea,
                inet_ntop(AF_INET, &su->sin.sin_addr, cbuf, sizeof(cbuf));
                ret += bputs(buf, cbuf);
                if (include_port)
-                       ret += bprintfrr(buf, ":%d", su->sin.sin_port);
+                       ret += bprintfrr(buf, ":%d", ntohs(su->sin.sin_port));
                break;
        case AF_INET6:
+               if (include_port)
+                       ret += bputch(buf, '[');
                inet_ntop(AF_INET6, &su->sin6.sin6_addr, cbuf, sizeof(cbuf));
                ret += bputs(buf, cbuf);
+               if (include_scope && su->sin6.sin6_scope_id)
+                       ret += bprintfrr(buf, "%%%u",
+                                        (unsigned int)su->sin6.sin6_scope_id);
                if (include_port)
-                       ret += bprintfrr(buf, ":%d", su->sin6.sin6_port);
+                       ret += bprintfrr(buf, "]:%d",
+                                        ntohs(su->sin6.sin6_port));
+               break;
+       case AF_UNIX: {
+               int len;
+#ifdef __linux__
+               if (su->sun.sun_path[0] == '\0' && su->sun.sun_path[1]) {
+                       len = strnlen(su->sun.sun_path + 1,
+                                     sizeof(su->sun.sun_path) - 1);
+                       ret += bprintfrr(buf, "@%*pSE", len,
+                                        su->sun.sun_path + 1);
+                       break;
+               }
+#endif
+               len = strnlen(su->sun.sun_path, sizeof(su->sun.sun_path));
+               ret += bprintfrr(buf, "%*pSE", len, su->sun.sun_path);
                break;
+       }
        default:
                ret += bprintfrr(buf, "(af %d)", sockunion_family(su));
        }
index 5e80ba1090b2a71b1244a564be32e257fe8d90f4..46ab7ff20e715d5d510034cc5bfec086b34dc4bd 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "privs.h"
 #include "if.h"
+#include <sys/un.h>
 #ifdef __OpenBSD__
 #include <netmpls/mpls.h>
 #endif
@@ -36,6 +37,7 @@ union sockunion {
        struct sockaddr sa;
        struct sockaddr_in sin;
        struct sockaddr_in6 sin6;
+       struct sockaddr_un sun;
 #ifdef __OpenBSD__
        struct sockaddr_mpls smpls;
        struct sockaddr_rtlabel rtlabel;
@@ -106,6 +108,11 @@ extern int sockunion_is_null(const union sockunion *su);
 
 #ifdef _FRR_ATTRIBUTE_PRINTFRR
 #pragma FRR printfrr_ext "%pSU"  (union sockunion *)
+#pragma FRR printfrr_ext "%pSU"  (struct sockaddr *)
+#pragma FRR printfrr_ext "%pSU"  (struct sockaddr_storage *)
+#pragma FRR printfrr_ext "%pSU"  (struct sockaddr_in *)
+#pragma FRR printfrr_ext "%pSU"  (struct sockaddr_in6 *)
+#pragma FRR printfrr_ext "%pSU"  (struct sockaddr_un *)
 #endif
 
 #ifdef __cplusplus