]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
socket-util: add helper for generically initializing sockaddr_union from in_addr_union
authorLennart Poettering <lennart@poettering.net>
Mon, 22 Nov 2021 10:29:42 +0000 (11:29 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 22 Nov 2021 21:18:34 +0000 (22:18 +0100)
src/basic/socket-util.c
src/basic/socket-util.h

index ca6085ef64daedc236c6f79992e6ba18068b82de..8d0494ece55564f8170570d62f485f9465e3795a 100644 (file)
@@ -400,6 +400,41 @@ const union in_addr_union *sockaddr_in_addr(const struct sockaddr *_sa) {
         }
 }
 
+int sockaddr_set_in_addr(
+                union sockaddr_union *u,
+                int family,
+                const union in_addr_union *a,
+                uint16_t port) {
+
+        assert(u);
+        assert(a);
+
+        switch (family) {
+
+        case AF_INET:
+                u->in = (struct sockaddr_in) {
+                        .sin_family = AF_INET,
+                        .sin_addr = a->in,
+                        .sin_port = htobe16(port),
+                };
+
+                return 0;
+
+        case AF_INET6:
+                u->in6 = (struct sockaddr_in6) {
+                        .sin6_family = AF_INET6,
+                        .sin6_addr = a->in6,
+                        .sin6_port = htobe16(port),
+                };
+
+                return 0;
+
+        default:
+                return -EAFNOSUPPORT;
+
+        }
+}
+
 int sockaddr_pretty(
                 const struct sockaddr *_sa,
                 socklen_t salen,
index c4fafa084b7f06809a3f1944bf1ba601ebd6c603..57e655154c5b050c848ecaf289750a6bb3e61231 100644 (file)
@@ -15,6 +15,7 @@
 #include <sys/un.h>
 
 #include "errno-util.h"
+#include "in-addr-util.h"
 #include "macro.h"
 #include "missing_network.h"
 #include "missing_socket.h"
@@ -106,6 +107,7 @@ bool socket_ipv6_is_enabled(void);
 
 int sockaddr_port(const struct sockaddr *_sa, unsigned *port);
 const union in_addr_union *sockaddr_in_addr(const struct sockaddr *sa);
+int sockaddr_set_in_addr(union sockaddr_union *u, int family, const union in_addr_union *a, uint16_t port);
 
 int sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool translate_ipv6, bool include_port, char **ret);
 int getpeername_pretty(int fd, bool include_port, char **ret);