neels has submitted this change. ( https://gerrit.osmocom.org/c/libosmo-pfcp/+/30320 )
Change subject: add osmo_pfcp_ip_addrs_to_str_*() ......................................................................
add osmo_pfcp_ip_addrs_to_str_*()
Move static function ip_addrs_to_str_buf() to public API as osmo_pfcp_ip_addrs_to_str_buf() and osmo_pfcp_ip_addrs_to_str_c().
So far the static function was only used in places where it follows other strings, so that it made sense to always start with a comma. Move this comma out of the function to the callers.
Sensibly handle a NULL pointer and an empty address set.
Rationale: osmo-upf would like to print an osmo_pfcp_ip_addrs struct in logging.
Change-Id: I5f67db8d347690cbb1ce273a2d072636859f1bf6 --- M include/osmocom/pfcp/pfcp_ies_custom.h M src/libosmo-pfcp/pfcp_ies_custom.c 2 files changed, 33 insertions(+), 9 deletions(-)
Approvals: Jenkins Builder: Verified msuraev: Looks good to me, but someone else must approve pespin: Looks good to me, but someone else must approve neels: Looks good to me, approved
diff --git a/include/osmocom/pfcp/pfcp_ies_custom.h b/include/osmocom/pfcp/pfcp_ies_custom.h index f273a87..565fee2 100644 --- a/include/osmocom/pfcp/pfcp_ies_custom.h +++ b/include/osmocom/pfcp/pfcp_ies_custom.h @@ -37,6 +37,8 @@ };
int osmo_pfcp_ip_addrs_set(struct osmo_pfcp_ip_addrs *dst, const struct osmo_sockaddr *addr); +int osmo_pfcp_ip_addrs_to_str_buf(char *buf, size_t buflen, const struct osmo_pfcp_ip_addrs *addrs); +char *osmo_pfcp_ip_addrs_to_str_c(void *ctx, const struct osmo_pfcp_ip_addrs *addrs);
/* 3GPP TS 29.244 8.2.38, IETF RFC 1035 3.1 */ struct osmo_pfcp_ie_node_id { diff --git a/src/libosmo-pfcp/pfcp_ies_custom.c b/src/libosmo-pfcp/pfcp_ies_custom.c index 0175c6f..422b6a9 100644 --- a/src/libosmo-pfcp/pfcp_ies_custom.c +++ b/src/libosmo-pfcp/pfcp_ies_custom.c @@ -514,26 +514,41 @@ return 0; }
-static int ip_addrs_to_str_buf(char *buf, size_t buflen, const struct osmo_pfcp_ip_addrs *addrs) +int osmo_pfcp_ip_addrs_to_str_buf(char *buf, size_t buflen, const struct osmo_pfcp_ip_addrs *addrs) { struct osmo_strbuf sb = { .buf = buf, .len = buflen }; + if (!addrs) { + OSMO_STRBUF_PRINTF(sb, "NULL-addr"); + return sb.chars_needed; + } + if (!(addrs->v4_present || addrs->v6_present)) { + OSMO_STRBUF_PRINTF(sb, "empty:-addr"); + return sb.chars_needed; + } if (addrs->v4_present) { - OSMO_STRBUF_PRINTF(sb, ",v4:"); + OSMO_STRBUF_PRINTF(sb, "v4:"); OSMO_STRBUF_APPEND(sb, osmo_sockaddr_to_str_buf2, &addrs->v4); } + if (addrs->v4_present && addrs->v6_present) + OSMO_STRBUF_PRINTF(sb, ","); if (addrs->v6_present) { - OSMO_STRBUF_PRINTF(sb, ",v6:"); + OSMO_STRBUF_PRINTF(sb, "v6:"); OSMO_STRBUF_APPEND(sb, osmo_sockaddr_to_str_buf2, &addrs->v6); } return sb.chars_needed; }
+char *osmo_pfcp_ip_addrs_to_str_c(void *ctx, const struct osmo_pfcp_ip_addrs *addrs) +{ + OSMO_NAME_C_IMPL(ctx, 32, "ERROR", osmo_pfcp_ip_addrs_to_str_buf, addrs) +} + int osmo_pfcp_enc_to_str_f_seid(char *buf, size_t buflen, const void *encode_from) { const struct osmo_pfcp_ie_f_seid *f_seid = encode_from; struct osmo_strbuf sb = { .buf = buf, .len = buflen }; - OSMO_STRBUF_PRINTF(sb, "0x%"PRIx64, f_seid->seid); - OSMO_STRBUF_APPEND(sb, ip_addrs_to_str_buf, &f_seid->ip_addr); + OSMO_STRBUF_PRINTF(sb, "0x%"PRIx64",", f_seid->seid); + OSMO_STRBUF_APPEND(sb, osmo_pfcp_ip_addrs_to_str_buf, &f_seid->ip_addr); return sb.chars_needed; }
@@ -640,8 +655,8 @@ if (ft->choose.choose_id_present) OSMO_STRBUF_PRINTF(sb, "-id%u", ft->choose.choose_id); } else { - OSMO_STRBUF_PRINTF(sb, "TEID-0x%x", ft->fixed.teid); - OSMO_STRBUF_APPEND(sb, ip_addrs_to_str_buf, &ft->fixed.ip_addr); + OSMO_STRBUF_PRINTF(sb, "TEID-0x%x,", ft->fixed.teid); + OSMO_STRBUF_APPEND(sb, osmo_pfcp_ip_addrs_to_str_buf, &ft->fixed.ip_addr); } return sb.chars_needed; } @@ -849,7 +864,10 @@ OSMO_STRBUF_APPEND(sb, osmo_pfcp_bits_to_str_buf, ohc->desc_bits, osmo_pfcp_outer_header_creation_strs); if (ohc->teid_present) OSMO_STRBUF_PRINTF(sb, ",TEID:0x%x", ohc->teid); - OSMO_STRBUF_APPEND(sb, ip_addrs_to_str_buf, &ohc->ip_addr); + + OSMO_STRBUF_PRINTF(sb, ","); + OSMO_STRBUF_APPEND(sb, osmo_pfcp_ip_addrs_to_str_buf, &ohc->ip_addr); + if (ohc->port_number_present) OSMO_STRBUF_PRINTF(sb, ",port:%u", ohc->port_number); if (ohc->c_tag_present) @@ -1015,7 +1033,11 @@ OSMO_STRBUF_PRINTF(sb, "%schv4", sb.pos ? "," : ""); if (uia->ip_is_destination) OSMO_STRBUF_PRINTF(sb, "%sdst", sb.pos ? "," : ""); - OSMO_STRBUF_APPEND(sb, ip_addrs_to_str_buf, &uia->ip_addr); + + if (sb.pos) + OSMO_STRBUF_PRINTF(sb, ","); + OSMO_STRBUF_APPEND(sb, osmo_pfcp_ip_addrs_to_str_buf, &uia->ip_addr); + if (uia->ipv6_prefix_delegation_bits_present) OSMO_STRBUF_PRINTF(sb, ",ipv6-prefix-deleg:%x", uia->ipv6_prefix_delegation_bits); if (uia->ipv6_prefix_length_present)