add osmo_sockaddr_set_port()

Do it like osmo_sockaddr_port() in reverse.

Related: SYS#5599
Change-Id: I9512e44c3203daebb3fe3435fceef167613c1a73
changes/63/27263/2
Neels Hofmeyr 11 months ago committed by laforge
parent a25a6dca25
commit 9c7f7f85c8
  1. 1
      include/osmocom/core/socket.h
  2. 17
      src/socket.c

@ -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);

@ -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

Loading…
Cancel
Save