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/.
neels gerrit-no-reply at lists.osmocom.orgneels has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/26608 ) Change subject: add osmo_sockaddr_to_str_c(), osmo_sockaddr_to_str_buf2() ...................................................................... add osmo_sockaddr_to_str_c(), osmo_sockaddr_to_str_buf2() To easily log and print a sockaddr using OTC_SELECT, add osmo_sockaddr_to_str_c(). Implement osmo_sockaddr_to_str_buf2() using osmo_strbuf, so that we can return the chars_needed which osmo_sockaddr_to_str_c() uses. From previous osmo_sockaddr_to_str_buf(), call osmo_sockaddr_to_str_buf2() and return NULL if the buf_len was insufficient, to mimick previous behavior. This makes it more consistently returning NULL for insufficient buf_len, as shown in the tweak that is needed in socket_test.c. Before osmo_sockaddr_to_str_buf() would return a truncated port number, now it's all or NULL. I will use osmo_sockaddr_to_str_c() in the new osmo-upf implementation. Related: SYS#5599 Change-Id: I12771bf8a021e6785217b1faad03c09ec1cfef0e --- M include/osmocom/core/socket.h M src/socket.c M tests/socket/socket_test.c 3 files changed, 59 insertions(+), 39 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/08/26608/1 diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h index a053391..6d11e6f 100644 --- a/include/osmocom/core/socket.h +++ b/include/osmocom/core/socket.h @@ -130,6 +130,8 @@ const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *sockaddr); char *osmo_sockaddr_to_str_buf(char *buf, size_t buf_len, const struct osmo_sockaddr *sockaddr); +int osmo_sockaddr_to_str_buf2(char *buf, size_t buf_len, const struct osmo_sockaddr *sockaddr); +char *osmo_sockaddr_to_str_c(void *ctx, const struct osmo_sockaddr *sockaddr); int osmo_sock_set_dscp(int fd, uint8_t dscp); int osmo_sock_set_priority(int fd, int prio); diff --git a/src/socket.c b/src/socket.c index 1251061..3757539 100644 --- a/src/socket.c +++ b/src/socket.c @@ -1775,7 +1775,57 @@ return osmo_sockaddr_to_str_buf(buf, sizeof(buf), sockaddr); } -/*! string-format a given osmo_sockaddr address into a user-supplied buffer +/*! string-format a given osmo_sockaddr address into a user-supplied buffer. + * Same as osmo_sockaddr_to_str_buf() but returns a would-be length in snprintf() style. + * \param[in] buf user-supplied output buffer + * \param[in] buf_len size of the user-supplied output buffer in bytes + * \param[in] sockaddr the osmo_sockaddr to print + * \return number of characters that would be written if the buffer is large enough, like snprintf(). + */ +int osmo_sockaddr_to_str_buf2(char *buf, size_t buf_len, const struct osmo_sockaddr *sockaddr) +{ + struct osmo_strbuf sb = { .buf = buf, .len = buf_len }; + uint16_t port = 0; + + if (!sockaddr) { + OSMO_STRBUF_PRINTF(sb, "NULL"); + return sb.chars_needed; + } + + switch (sockaddr->u.sa.sa_family) { + case AF_INET: + OSMO_STRBUF_APPEND(sb, osmo_sockaddr_to_str_and_uint, &port, &sockaddr->u.sa); + if (port) + OSMO_STRBUF_PRINTF(sb, ":%u", port); + break; + case AF_INET6: + OSMO_STRBUF_PRINTF(sb, "["); + OSMO_STRBUF_APPEND(sb, osmo_sockaddr_to_str_and_uint, &port, &sockaddr->u.sa); + OSMO_STRBUF_PRINTF(sb, "]"); + if (port) + OSMO_STRBUF_PRINTF(sb, ":%u", port); + break; + default: + OSMO_STRBUF_PRINTF(sb, "unsupported family %d", sockaddr->u.sa.sa_family); + break; + } + + return sb.chars_needed; +} + +/*! string-format a given osmo_sockaddr address into a talloc allocated buffer. + * Like osmo_sockaddr_to_str_buf2() but returns a talloc allocated string. + * \param[in] ctx talloc context to allocate from, e.g. OTC_SELECT. + * \param[in] sockaddr the osmo_sockaddr to print. + * \return human readable string. + */ +char *osmo_sockaddr_to_str_c(void *ctx, const struct osmo_sockaddr *sockaddr) +{ + OSMO_NAME_C_IMPL(ctx, 64, "ERROR", osmo_sockaddr_to_str_buf2, sockaddr) +} + +/*! string-format a given osmo_sockaddr address into a user-supplied buffer. + * Like osmo_sockaddr_to_str_buf2() but returns buf, or NULL if too short. * \param[in] buf user-supplied output buffer * \param[in] buf_len size of the user-supplied output buffer in bytes * \param[in] sockaddr the osmo_sockaddr to print @@ -1784,43 +1834,9 @@ char *osmo_sockaddr_to_str_buf(char *buf, size_t buf_len, const struct osmo_sockaddr *sockaddr) { - uint16_t port = 0; - size_t written; - if (buf_len < 5) + int chars_needed = osmo_sockaddr_to_str_buf2(buf, buf_len, sockaddr); + if (chars_needed >= buf_len) return NULL; - - if (!sockaddr) - return NULL; - - switch (sockaddr->u.sa.sa_family) { - case AF_INET: - written = osmo_sockaddr_to_str_and_uint(buf, buf_len, &port, &sockaddr->u.sa); - if (written + 1 >= buf_len && port) - return NULL; - if (port) - snprintf(buf + written, buf_len - written, ":%u", port); - break; - case AF_INET6: - buf[0] = '['; - written = osmo_sockaddr_to_str_and_uint(buf + 1, buf_len - 1, &port, &sockaddr->u.sa); - if (written + 2 >= buf_len) - return NULL; - - if (written + 3 >= buf_len && port) - return NULL; - - if (port) - snprintf(buf + 1 + written, buf_len - written - 1, "]:%u", port); - else { - buf[written + 1] = ']'; - buf[written + 2] = 0; - } - break; - default: - snprintf(buf, buf_len, "unsupported family %d", sockaddr->u.sa.sa_family); - return buf; - } - return buf; } diff --git a/tests/socket/socket_test.c b/tests/socket/socket_test.c index eda716a..fe77b92 100644 --- a/tests/socket/socket_test.c +++ b/tests/socket/socket_test.c @@ -345,9 +345,11 @@ OSMO_ASSERT(!strncmp("[::1]:42", result, sizeof(buf))); memset(&buf[0], 0, sizeof(buf)); - result = osmo_sockaddr_to_str_buf(buf, 8, &localhost6); printf("Checking osmo_sockaddr_to_str_buf too short IPv6\n"); - OSMO_ASSERT(!strncmp("[::1]:4", result, sizeof(buf))); + result = osmo_sockaddr_to_str_buf(buf, 8, &localhost6); + OSMO_ASSERT(result == NULL); + osmo_sockaddr_to_str_buf2(buf, 8, &localhost6); + OSMO_ASSERT(!strncmp("[::1]:4", buf, sizeof(buf))); memset(&buf[0], 0, sizeof(buf)); result = osmo_sockaddr_to_str_buf(buf, 5, &localhost6); -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/26608 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: I12771bf8a021e6785217b1faad03c09ec1cfef0e Gerrit-Change-Number: 26608 Gerrit-PatchSet: 1 Gerrit-Owner: neels <nhofmeyr at sysmocom.de> Gerrit-Reviewer: lynxis lazus <lynxis at fe80.eu> Gerrit-MessageType: newchange -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20211217/192abc00/attachment.htm>