socket-util: add helper for generically initializing sockaddr_union from in_addr_union

This commit is contained in:
Lennart Poettering
2021-11-22 11:29:42 +01:00
parent f96b500ece
commit c1b91f06b9
2 changed files with 37 additions and 0 deletions

View 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,