aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeels Hofmeyr <nhofmeyr@sysmocom.de>2022-01-11 19:25:40 +0100
committerNeels Hofmeyr <nhofmeyr@sysmocom.de>2022-02-28 00:24:59 +0100
commit39d31ae9ec655dc392a5924a5a9c49b1355e41cc (patch)
tree024b3a485b77c7bad323973350816df70a35c824
parenta6d1ef0d6048cf59cdd154a3452681ad65141859 (diff)
add osmo_sockaddr_set_port()neels/pfcp
Do it like osmo_sockaddr_port() in reverse. Related: SYS#5599 Change-Id: I9512e44c3203daebb3fe3435fceef167613c1a73
-rw-r--r--include/osmocom/core/socket.h1
-rw-r--r--src/socket.c17
2 files changed, 18 insertions, 0 deletions
diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h
index fffc25c7..183220d5 100644
--- a/include/osmocom/core/socket.h
+++ b/include/osmocom/core/socket.h
@@ -96,6 +96,7 @@ size_t osmo_sockaddr_in_to_str_and_uint(char *addr, unsigned int addr_len, uint1
const char *osmo_sockaddr_ntop(const struct sockaddr *sa, char *dst);
uint16_t osmo_sockaddr_port(const struct sockaddr *sa);
+void osmo_sockaddr_set_port(struct sockaddr *sa, uint16_t port);
int osmo_sock_unix_init(uint16_t type, uint8_t proto,
const char *socket_path, unsigned int flags);
diff --git a/src/socket.c b/src/socket.c
index 80cba178..4605db4c 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -1254,6 +1254,23 @@ uint16_t osmo_sockaddr_port(const struct sockaddr *sa)
return 0;
}
+/*! Set sockaddr port content (to network byte order).
+ * \param[out] sa sockaddr to set the port of.
+ * \param[in] port port nr to set.
+ */
+void osmo_sockaddr_set_port(struct sockaddr *sa, uint16_t port)
+{
+ struct osmo_sockaddr *osa = (struct osmo_sockaddr *)sa;
+ switch (osa->u.sa.sa_family) {
+ case AF_INET6:
+ osa->u.sin6.sin6_port = htons(port);
+ return;
+ case AF_INET:
+ osa->u.sin.sin_port = htons(port);
+ return;
+ }
+}
+
/*! Initialize a unix domain socket (including bind/connect)
* \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM
* \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP