diff options
| author | David Lamparter <equinox@opensourcerouting.org> | 2022-11-30 11:19:14 +0100 |
|---|---|---|
| committer | David Lamparter <equinox@opensourcerouting.org> | 2023-11-05 18:19:52 +0100 |
| commit | bf098b4a11a42e123fbfef664027b2ba183a31c4 (patch) | |
| tree | d66a03ee9f2ae14ade6779c1d16dfe441de573b6 /lib/sockunion.h | |
| parent | 19cbc3157983ec0a1a15ec1be90a4a1a3de9969b (diff) | |
lib: add a transparent union for sockaddrs
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>
Diffstat (limited to 'lib/sockunion.h')
| -rw-r--r-- | lib/sockunion.h | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/sockunion.h b/lib/sockunion.h index e507255999..675855e2b6 100644 --- a/lib/sockunion.h +++ b/lib/sockunion.h @@ -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. */ |
