summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2020-11-18 10:25:27 +0100
committerLennart Poettering <lennart@poettering.net>2020-12-02 15:14:21 +0100
commit3132597182c806e5193aebb0b67cdc0f73154a51 (patch)
treed647d7028a42e23f9f62ba499d5f9c32c84e2e53 /src
parentMerge pull request #17707 from yuwata/network-fix-reconfigure (diff)
downloadsystemd-3132597182c806e5193aebb0b67cdc0f73154a51.tar.xz
systemd-3132597182c806e5193aebb0b67cdc0f73154a51.zip
socket-util: add sockaddr_in_addr() helper
This extracts the IP address (as union in_addr_union) from a socket address (i.e. a struct sockaddr).
Diffstat (limited to 'src')
-rw-r--r--src/basic/socket-util.c21
-rw-r--r--src/basic/socket-util.h1
2 files changed, 21 insertions, 1 deletions
diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c
index 48d0718d5d..63ab53b8fc 100644
--- a/src/basic/socket-util.c
+++ b/src/basic/socket-util.c
@@ -320,7 +320,7 @@ bool socket_address_matches_fd(const SocketAddress *a, int fd) {
}
int sockaddr_port(const struct sockaddr *_sa, unsigned *ret_port) {
- union sockaddr_union *sa = (union sockaddr_union*) _sa;
+ const union sockaddr_union *sa = (const union sockaddr_union*) _sa;
/* Note, this returns the port as 'unsigned' rather than 'uint16_t', as AF_VSOCK knows larger ports */
@@ -345,6 +345,25 @@ int sockaddr_port(const struct sockaddr *_sa, unsigned *ret_port) {
}
}
+const union in_addr_union *sockaddr_in_addr(const struct sockaddr *_sa) {
+ const union sockaddr_union *sa = (const union sockaddr_union*) _sa;
+
+ if (!sa)
+ return NULL;
+
+ switch (sa->sa.sa_family) {
+
+ case AF_INET:
+ return (const union in_addr_union*) &sa->in.sin_addr;
+
+ case AF_INET6:
+ return (const union in_addr_union*) &sa->in6.sin6_addr;
+
+ default:
+ return NULL;
+ }
+}
+
int sockaddr_pretty(
const struct sockaddr *_sa,
socklen_t salen,
diff --git a/src/basic/socket-util.h b/src/basic/socket-util.h
index 9f7928040d..923898a3fc 100644
--- a/src/basic/socket-util.h
+++ b/src/basic/socket-util.h
@@ -102,6 +102,7 @@ const char* socket_address_get_path(const SocketAddress *a);
bool socket_ipv6_is_supported(void);
int sockaddr_port(const struct sockaddr *_sa, unsigned *port);
+const union in_addr_union *sockaddr_in_addr(const struct sockaddr *sa);
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);