sockaddr_str: add conversion to,from osmo_sockaddr

Add functions to pass struct osmo_sockaddr to the
osmo_sockaddr_str_{to,from}* API directly.

So far the interface to osmo_sockaddr_str_ is:

  osmo_sockaddr_str_from_sockaddr(&my_sa_str, &my_osa->;

I'm working a lot with osmo_sockaddr at the moment, and the cumulated
time of forgetting to add '' and having another compilation cycle
because of those is justifying this additional API.

Change-Id: I0df84b4bb8cb5d8434b735fa3a38e7f95be43e91
This commit is contained in:
Neels Hofmeyr 2024-03-13 23:32:43 +01:00
parent 50e38cf36a
commit 1c24962946
4 changed files with 28 additions and 0 deletions

View File

@ -11,6 +11,7 @@ core ADD osmo_sock_multiaddr_{add,del}_local_addr()
core ADD osmo_sock_multiaddr_get_ip_and_port(), osmo_multiaddr_ip_and_port_snprintf(), osmo_sock_multiaddr_get_name_buf()
core ADD osmo_sock_sctp_get_peer_addr_info()
core ADD gsmtap_inst_fd2() core, DEPRECATE gsmtap_inst_fd()
core ADD osmo_sockaddr_str_from_osa() osmo_sockaddr_str_to_osa()
core behavior change osmo_tdef_fsm_inst_state_chg(): allow millisecond precision
core ABI change osmo_io_ops now contains a struct of structs, not union of structs
core ABI change osmo_iofd_set_ioops() now returns a value (error code)

View File

@ -34,6 +34,7 @@ struct in6_addr;
struct sockaddr_storage;
struct sockaddr_in;
struct sockaddr_in6;
struct osmo_sockaddr;
/*! \defgroup sockaddr_str IP address/port utilities.
* @{
@ -84,6 +85,7 @@ int osmo_sockaddr_str_from_32h(struct osmo_sockaddr_str *sockaddr_str, uint32_t
int osmo_sockaddr_str_from_sockaddr_in(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_in *src);
int osmo_sockaddr_str_from_sockaddr_in6(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_in6 *src);
int osmo_sockaddr_str_from_sockaddr(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_storage *src);
int osmo_sockaddr_str_from_osa(struct osmo_sockaddr_str *sockaddr_str, const struct osmo_sockaddr *src);
int osmo_sockaddr_str_to_in_addr(const struct osmo_sockaddr_str *sockaddr_str, struct in_addr *dst);
int osmo_sockaddr_str_to_in6_addr(const struct osmo_sockaddr_str *sockaddr_str, struct in6_addr *dst);
@ -92,6 +94,7 @@ int osmo_sockaddr_str_to_32h(const struct osmo_sockaddr_str *sockaddr_str, uint3
int osmo_sockaddr_str_to_sockaddr_in(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_in *dst);
int osmo_sockaddr_str_to_sockaddr_in6(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_in6 *dst);
int osmo_sockaddr_str_to_sockaddr(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_storage *dst);
int osmo_sockaddr_str_to_osa(const struct osmo_sockaddr_str *sockaddr_str, struct osmo_sockaddr *dst);
int osmo_sockaddr_str_from_32n(struct osmo_sockaddr_str *sockaddr_str, uint32_t ip, uint16_t port)
OSMO_DEPRECATED("osmo_sockaddr_str_from_32n() actually uses *host* byte order. Use osmo_sockaddr_str_from_32h() instead");

View File

@ -400,6 +400,7 @@ osmo_sockaddr_str_from_in_addr;
@ -412,6 +413,7 @@ osmo_sockaddr_str_to_in_addr;

View File

@ -33,6 +33,7 @@
#include <osmocom/core/sockaddr_str.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/byteswap.h>
#include <osmocom/core/socket.h>
/*! \addtogroup sockaddr_str
@ -359,6 +360,17 @@ int osmo_sockaddr_str_from_sockaddr(struct osmo_sockaddr_str *sockaddr_str, cons
return -EINVAL;
/*! Convert IPv4 or IPv6 address and port to osmo_sockaddr_str.
* \param[out] sockaddr_str The instance to copy to.
* \param[in] src IPv4 or IPv6 address and port data.
* \return 0 on success, negative if src does not indicate AF_INET nor AF_INET6 (or if the conversion fails, which
* should not be possible in practice).
int osmo_sockaddr_str_from_osa(struct osmo_sockaddr_str *sockaddr_str, const struct osmo_sockaddr *src)
return osmo_sockaddr_str_from_sockaddr(sockaddr_str, &src->;
/*! Convert osmo_sockaddr_str address string to IPv4 address data.
* \param[in] sockaddr_str The instance to convert the IP of.
* \param[out] dst IPv4 address data to write to.
@ -509,5 +521,15 @@ int osmo_sockaddr_str_to_sockaddr(const struct osmo_sockaddr_str *sockaddr_str,
/*! Convert osmo_sockaddr_str address string and port to IPv4 or IPv6 address and port data.
* \param[in] sockaddr_str The instance to convert the IP and port of.
* \param[out] dst IPv4/IPv6 address and port data to write to.
* \return 0 on success, negative on error (e.g. invalid IP address string for the family indicated by sockaddr_str->af).
int osmo_sockaddr_str_to_osa(const struct osmo_sockaddr_str *sockaddr_str, struct osmo_sockaddr *dst)
return osmo_sockaddr_str_to_sockaddr(sockaddr_str, &dst->;
/*! @} */