add osmo_sockaddr_set_port()

Do it like osmo_sockaddr_port() in reverse.

Related: SYS#5599
Change-Id: I9512e44c3203daebb3fe3435fceef167613c1a73
This commit is contained in:
Neels Hofmeyr 2022-01-11 19:25:40 +01:00 committed by laforge
parent a25a6dca25
commit 9c7f7f85c8
2 changed files with 18 additions and 0 deletions

View File

@ -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); const char *osmo_sockaddr_ntop(const struct sockaddr *sa, char *dst);
uint16_t osmo_sockaddr_port(const struct sockaddr *sa); 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, int osmo_sock_unix_init(uint16_t type, uint8_t proto,
const char *socket_path, unsigned int flags); const char *socket_path, unsigned int flags);

View File

@ -1254,6 +1254,23 @@ uint16_t osmo_sockaddr_port(const struct sockaddr *sa)
return 0; 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) /*! Initialize a unix domain socket (including bind/connect)
* \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM
* \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP