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
This commit is contained in:
Neels Hofmeyr 2022-11-26 02:53:56 +01:00
parent 25c4c9b355
commit c41bfcbbf0
2 changed files with 33 additions and 9 deletions

View File

@ -37,6 +37,8 @@ struct osmo_pfcp_ip_addrs {
};
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 {

View File

@ -514,26 +514,41 @@ int osmo_pfcp_enc_f_seid(struct osmo_gtlv_put *tlv, const void *decoded_struct,
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 @@ int osmo_pfcp_ie_f_teid_to_str_buf(char *buf, size_t buflen, const struct osmo_p
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 @@ int osmo_pfcp_enc_to_str_outer_header_creation(char *buf, size_t buflen, const v
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 @@ int osmo_pfcp_enc_to_str_ue_ip_address(char *buf, size_t buflen, const void *enc
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)