This is merely a historical archive of years 2008-2021, before the migration to mailman3.
A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.
pespin gerrit-no-reply at lists.osmocom.orgpespin has submitted this change. ( https://gerrit.osmocom.org/c/libosmocore/+/19917 ) Change subject: socket: Add some osmo_sockaddr print helpers ...................................................................... socket: Add some osmo_sockaddr print helpers These are APIs useful to inline in log calls. Change-Id: Ie07a38b05b7888885dba4ae795e9f3d9a561543d --- M include/osmocom/core/socket.h M src/socket.c 2 files changed, 35 insertions(+), 0 deletions(-) Approvals: Jenkins Builder: Verified fixeria: Looks good to me, but someone else must approve pespin: Looks good to me, approved diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h index 9cb8d6b..e6a6bd5 100644 --- a/include/osmocom/core/socket.h +++ b/include/osmocom/core/socket.h @@ -75,6 +75,9 @@ size_t osmo_sockaddr_in_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port, const struct sockaddr_in *sin); +const char *osmo_sockaddr_ntop(const struct sockaddr *sa, char *dst); +uint16_t osmo_sockaddr_port(const struct sockaddr *sa); + 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 f91ad10..e8a2ce2 100644 --- a/src/socket.c +++ b/src/socket.c @@ -1001,6 +1001,38 @@ return 0; } +/*! inet_ntop() wrapper for a struct sockaddr. + * \param[in] sa source sockaddr to get the address from. + * \param[out] dst string buffer of at least INET6_ADDRSTRLEN size. + * \returns returns a non-null pointer to dst. NULL is returned if there was an + * error, with errno set to indicate the error. + */ +const char *osmo_sockaddr_ntop(const struct sockaddr *sa, char *dst) +{ + const struct osmo_sockaddr *osa = (const struct osmo_sockaddr *)sa; + return inet_ntop(osa->u.sa.sa_family, + osa->u.sa.sa_family == AF_INET6 ? + (const void *)&osa->u.sin6.sin6_addr : + (const void *)&osa->u.sin.sin_addr, + dst, INET6_ADDRSTRLEN); +} + +/*! Get sockaddr port content (in host byte order) + * \param[in] sa source sockaddr to get the port from. + * \returns returns the sockaddr port in host byte order + */ +uint16_t osmo_sockaddr_port(const struct sockaddr *sa) +{ + const struct osmo_sockaddr *osa = (const struct osmo_sockaddr *)sa; + switch (osa->u.sa.sa_family) { + case AF_INET6: + return ntohs(osa->u.sin6.sin6_port); + case AF_INET: + return ntohs(osa->u.sin.sin_port); + } + return 0; +} + /*! 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 -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/19917 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: Ie07a38b05b7888885dba4ae795e9f3d9a561543d Gerrit-Change-Number: 19917 Gerrit-PatchSet: 1 Gerrit-Owner: pespin <pespin at sysmocom.de> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: daniel <dwillmann at sysmocom.de> Gerrit-Reviewer: fixeria <vyanitskiy at sysmocom.de> Gerrit-Reviewer: lynxis lazus <lynxis at fe80.eu> Gerrit-Reviewer: pespin <pespin at sysmocom.de> Gerrit-MessageType: merged -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20200901/6bf84b53/attachment.htm>