socket: Introduce API osmo_sockaddr_is_any

Change-Id: I2810a889fc14052d0e0be6a2b500ad4e5088ffa9
changes/86/29586/3
Pau Espin 2 months ago
parent 3c12f557bd
commit 3b3955b203
  1. 1
      TODO-RELEASE
  2. 1
      include/osmocom/core/socket.h
  3. 20
      src/socket.c

@ -7,3 +7,4 @@
# If any interfaces have been added since the last public release: c:r:a + 1.
# If any interfaces have been removed or changed since the last public release: c:r:0.
#library what description / commit summary line
libosmocore new API osmo_sockaddr_is_any()

@ -31,6 +31,7 @@ struct osmo_sockaddr {
};
int osmo_sockaddr_is_local(struct sockaddr *addr, unsigned int addrlen);
int osmo_sockaddr_is_any(const struct osmo_sockaddr *addr);
unsigned int osmo_sockaddr_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port,
const struct sockaddr *sa);

@ -1175,6 +1175,26 @@ int osmo_sockaddr_is_local(struct sockaddr *addr, unsigned int addrlen)
return 0;
}
/*! Determine if the given address is an ANY address ("0.0.0.0", "::"). Port is not checked.
* \param[in] addr Socket Address
* \param[in] addrlen Length of socket address in bytes
* \returns 1 if address is ANY, 0 otherwise. -1 is address family not supported/detected.
*/
int osmo_sockaddr_is_any(const struct osmo_sockaddr *addr)
{
switch (addr->u.sa.sa_family) {
case AF_INET6: {
struct in6_addr ip6_any = IN6ADDR_ANY_INIT;
return memcmp(&addr->u.sin6.sin6_addr,
&ip6_any, sizeof(ip6_any)) == 0;
}
case AF_INET:
return addr->u.sin.sin_addr.s_addr == INADDR_ANY;
default:
return -1;
}
}
/*! Convert sockaddr_in to IP address as char string and port as uint16_t.
* \param[out] addr String buffer to write IP address to, or NULL.
* \param[out] addr_len Size of \a addr.

Loading…
Cancel
Save