aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeels Hofmeyr <nhofmeyr@sysmocom.de>2022-01-11 19:25:40 +0100
committerNeels Hofmeyr <nhofmeyr@sysmocom.de>2022-01-19 22:15:04 +0100
commitdbc65558ba3a8a70f36493481d6b1362ca06c575 (patch)
tree05c71f0410b3676197feed54bccb7c803b8199af
parentdd8dd6d13047627cea27ef01a0b75650230e2497 (diff)
add osmo_sockaddr_set_port()neels/pfcp
-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 6d11e6f0..9305a20f 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 882ff022..eee25242 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