From a46a126fb1f3b4f1dfb6040f9a90886615972198 Mon Sep 17 00:00:00 2001 From: Neels Janosch Hofmeyr Date: Sat, 16 Mar 2024 05:50:14 +0100 Subject: add osmo_pfcp_ip_addrs_get This pattern shows up a lot when working with PFCP. Let's make it easier to handle IPv4 and v6 at the same time. Change-Id: I4338a83f26ef2443f90cf835621e73aed5eac521 --- include/osmocom/pfcp/pfcp_ies_custom.h | 6 ++++++ src/libosmo-pfcp/pfcp_msg.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/include/osmocom/pfcp/pfcp_ies_custom.h b/include/osmocom/pfcp/pfcp_ies_custom.h index 0540bfe..84f862a 100644 --- a/include/osmocom/pfcp/pfcp_ies_custom.h +++ b/include/osmocom/pfcp/pfcp_ies_custom.h @@ -25,6 +25,7 @@ #pragma once #include +#include #include @@ -40,6 +41,11 @@ int osmo_pfcp_ip_addrs_set(struct osmo_pfcp_ip_addrs *dst, const struct osmo_soc 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); +int osmo_pfcp_ip_addrs_get(struct osmo_sockaddr *v4, struct osmo_sockaddr *v6, + struct osmo_pfcp_ip_addrs *src); +int osmo_pfcp_ip_addrs_get_str(struct osmo_sockaddr_str *v4, struct osmo_sockaddr_str *v6, + struct osmo_pfcp_ip_addrs *src); + /* 3GPP TS 29.244 8.2.38, IETF RFC 1035 3.1 */ struct osmo_pfcp_ie_node_id { enum osmo_pfcp_node_id_type type; diff --git a/src/libosmo-pfcp/pfcp_msg.c b/src/libosmo-pfcp/pfcp_msg.c index abcee18..66f6e59 100644 --- a/src/libosmo-pfcp/pfcp_msg.c +++ b/src/libosmo-pfcp/pfcp_msg.c @@ -514,6 +514,38 @@ int osmo_pfcp_ip_addrs_set(struct osmo_pfcp_ip_addrs *dst, const struct osmo_soc } } +int osmo_pfcp_ip_addrs_get(struct osmo_sockaddr *v4, struct osmo_sockaddr *v6, struct osmo_pfcp_ip_addrs *src) +{ + if (v4) { + if (src->v4_present) + *v4 = src->v4; + else + *v4 = (struct osmo_sockaddr){ .u.sa.sa_family = AF_UNSPEC }; + } + if (v6) { + if (src->v6_present) + *v6 = src->v6; + else + *v6 = (struct osmo_sockaddr){ .u.sa.sa_family = AF_UNSPEC }; + } + return 0; +} + +int osmo_pfcp_ip_addrs_get_str(struct osmo_sockaddr_str *v4, struct osmo_sockaddr_str *v6, struct osmo_pfcp_ip_addrs *src) +{ + if (v4) { + if (src->v4_present) + return osmo_sockaddr_str_from_sockaddr(v4, &src->v4.u.sas); + *v4 = (struct osmo_sockaddr_str){}; + } + if (v6) { + if (src->v6_present) + return osmo_sockaddr_str_from_sockaddr(v6, &src->v6.u.sas); + *v6 = (struct osmo_sockaddr_str){}; + } + return 0; +} + /* If a osmo_fsm_inst placed in m->ctx deallocates before the osmo_pfcp_msg, call this function, to make sure to avoid * use after free. Alternatively use m->ctx.*_use_count to make sure the FSM inst does not deallocate before the * osmo_pfcp_msg is discarded from the resend queue. */ -- cgit v1.2.3