]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: add a transparent union for sockaddrs 14736/head
authorDavid Lamparter <equinox@opensourcerouting.org>
Wed, 30 Nov 2022 10:19:14 +0000 (11:19 +0100)
committerDavid Lamparter <equinox@opensourcerouting.org>
Sun, 5 Nov 2023 17:19:52 +0000 (18:19 +0100)
As with "prefixptr"/"prefixconstptr", this allows function prototypes to
say "just give me any sockaddr" typing-wise, without having the compiler
complain that a "struct sockaddr_in" is not a "struct sockaddr".

(Split off from other changes that start using this.)

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
lib/sockunion.h

index e5072559999df5dbd2f017c434eb849925a95066..675855e2b68bf00793ae999159cf122278af2a07 100644 (file)
@@ -7,6 +7,8 @@
 #ifndef _ZEBRA_SOCKUNION_H
 #define _ZEBRA_SOCKUNION_H
 
+#include "compiler.h"
+
 #include "privs.h"
 #include "if.h"
 #include <sys/un.h>
@@ -27,8 +29,40 @@ union sockunion {
        struct sockaddr_mpls smpls;
        struct sockaddr_rtlabel rtlabel;
 #endif
+
+       /* sockaddr_storage is guaranteed to be larger than the others */
+       struct sockaddr_storage sa_storage;
 };
 
+/* clang-format off */
+/* for functions that want to accept any sockaddr pointer without casts */
+union sockaddrptr {
+       uniontype(sockaddrptr, union sockunion, su)
+       uniontype(sockaddrptr, struct sockaddr, sa)
+       uniontype(sockaddrptr, struct sockaddr_in, sin)
+       uniontype(sockaddrptr, struct sockaddr_in6, sin6)
+       uniontype(sockaddrptr, struct sockaddr_un, sun)
+#ifdef __OpenBSD__
+       uniontype(sockaddrptr, struct sockaddr_mpls, smpls)
+       uniontype(sockaddrptr, struct sockaddr_rtlabel, rtlabel)
+#endif
+       uniontype(sockaddrptr, struct sockaddr_storage, sa_storage)
+} TRANSPARENT_UNION;
+
+union sockaddrconstptr {
+       uniontype(sockaddrconstptr, const union sockunion, su)
+       uniontype(sockaddrconstptr, const struct sockaddr, sa)
+       uniontype(sockaddrconstptr, const struct sockaddr_in, sin)
+       uniontype(sockaddrconstptr, const struct sockaddr_in6, sin6)
+       uniontype(sockaddrconstptr, const struct sockaddr_un, sun)
+#ifdef __OpenBSD__
+       uniontype(sockaddrconstptr, const struct sockaddr_mpls, smpls)
+       uniontype(sockaddrconstptr, const struct sockaddr_rtlabel, rtlabel)
+#endif
+       uniontype(sockaddrconstptr, const struct sockaddr_storage, sa_storage)
+} TRANSPARENT_UNION;
+/* clang-format on */
+
 enum connect_result { connect_error, connect_success, connect_in_progress };
 
 /* Default address family. */