diff options
author | Neels Hofmeyr <neels@hofmeyr.de> | 2019-12-04 01:04:32 +0100 |
---|---|---|
committer | Neels Hofmeyr <neels@hofmeyr.de> | 2020-04-30 19:19:17 +0200 |
commit | c79bcdedc92a5ae7dad67b5f10452d549ccfb39e (patch) | |
tree | e6d952b6fc4b2dc5d14399da0e88e551e89b6493 /src/gsupclient | |
parent | ad868e29ba1b838f151cdf2ad9cb4f068a817493 (diff) |
2/2: wrap ipa_name in osmo_cni_peer_id with type enum and union
To be prepared for the future in public API, wrap the new osmo_ipa_name struct
in an enum-type and union called osmo_cni_peer.
During code review it was requested to insert an ability to handle different
kinds of peer id, in order to be able to add a Global Title in the future.
Use the generic osmo_cni_peer only in the publicly visible API. For osmo-hlr
internal code, I intend to postpone implementing this into the future, when a
different peer identification actually gets introduced.
This way we don't need to implement it now in all osmo-hlr code paths (save
time now), but still make all external API users aware that this type may be
extended in the future.
Change-Id: Ide9dcdca283ab989240cfc6e53e9211862a199c5
Diffstat (limited to 'src/gsupclient')
-rw-r--r-- | src/gsupclient/Makefile.am | 2 | ||||
-rw-r--r-- | src/gsupclient/cni_peer_id.c (renamed from src/gsupclient/ipa_name.c) | 102 | ||||
-rw-r--r-- | src/gsupclient/gsup_req.c | 13 |
3 files changed, 104 insertions, 13 deletions
diff --git a/src/gsupclient/Makefile.am b/src/gsupclient/Makefile.am index 38b1582..3412c40 100644 --- a/src/gsupclient/Makefile.am +++ b/src/gsupclient/Makefile.am @@ -9,7 +9,7 @@ AM_CFLAGS = -Wall $(all_includes) -I$(top_srcdir)/include -I$(top_builddir)/incl lib_LTLIBRARIES = libosmo-gsup-client.la libosmo_gsup_client_la_SOURCES = \ - ipa_name.c \ + cni_peer_id.c \ gsup_client.c \ gsup_req.c \ $(NULL) diff --git a/src/gsupclient/ipa_name.c b/src/gsupclient/cni_peer_id.c index 2db069f..de7b4bc 100644 --- a/src/gsupclient/ipa_name.c +++ b/src/gsupclient/cni_peer_id.c @@ -19,7 +19,12 @@ #include <errno.h> #include <string.h> #include <osmocom/core/utils.h> -#include <osmocom/gsupclient/ipa_name.h> +#include <osmocom/gsupclient/cni_peer_id.h> + +bool osmo_ipa_name_is_empty(const struct osmo_ipa_name *ipa_name) +{ + return (!ipa_name) || (!ipa_name->len); +} int osmo_ipa_name_set(struct osmo_ipa_name *ipa_name, const uint8_t *val, size_t len) { @@ -34,19 +39,25 @@ int osmo_ipa_name_set(struct osmo_ipa_name *ipa_name, const uint8_t *val, size_t return 0; } -int osmo_ipa_name_set_str(struct osmo_ipa_name *ipa_name, const char *str_fmt, ...) +static int osmo_ipa_name_set_str_va(struct osmo_ipa_name *ipa_name, const char *str_fmt, va_list ap) { - va_list ap; if (!str_fmt) return osmo_ipa_name_set(ipa_name, NULL, 0); - - va_start(ap, str_fmt); vsnprintf((char*)(ipa_name->val), sizeof(ipa_name->val), str_fmt, ap); - va_end(ap); ipa_name->len = strlen((char*)(ipa_name->val))+1; return 0; } +int osmo_ipa_name_set_str(struct osmo_ipa_name *ipa_name, const char *str_fmt, ...) +{ + va_list ap; + int rc; + va_start(ap, str_fmt); + rc = osmo_ipa_name_set_str_va(ipa_name, str_fmt, ap); + va_end(ap); + return rc; +} + int osmo_ipa_name_cmp(const struct osmo_ipa_name *a, const struct osmo_ipa_name *b) { int cmp; @@ -95,3 +106,82 @@ const char *osmo_ipa_name_to_str_c(void *ctx, const struct osmo_ipa_name *ipa_na len--; return osmo_escape_str_c(ctx, (char*)ipa_name->val, len); } + +bool osmo_cni_peer_id_is_empty(const struct osmo_cni_peer_id *cni_peer_id) +{ + if (!cni_peer_id) + return true; + switch (cni_peer_id->type) { + case OSMO_CNI_PEER_ID_EMPTY: + return true; + case OSMO_CNI_PEER_ID_IPA_NAME: + return osmo_ipa_name_is_empty(&cni_peer_id->ipa_name); + default: + return false; + } +} +int osmo_cni_peer_id_set(struct osmo_cni_peer_id *cni_peer_id, enum osmo_cni_peer_id_type type, + const uint8_t *val, size_t len) +{ + cni_peer_id->type = type; + switch (type) { + case OSMO_CNI_PEER_ID_IPA_NAME: + return osmo_ipa_name_set(&cni_peer_id->ipa_name, val, len); + default: + return -EINVAL; + } +} + +int osmo_cni_peer_id_set_str(struct osmo_cni_peer_id *cni_peer_id, enum osmo_cni_peer_id_type type, + const char *str_fmt, ...) +{ + va_list ap; + int rc; + + *cni_peer_id = (struct osmo_cni_peer_id){}; + + switch (type) { + case OSMO_CNI_PEER_ID_IPA_NAME: + cni_peer_id->type = OSMO_CNI_PEER_ID_IPA_NAME; + va_start(ap, str_fmt); + rc = osmo_ipa_name_set_str_va(&cni_peer_id->ipa_name, str_fmt, ap); + va_end(ap); + return rc; + default: + return -EINVAL; + } +} + +int osmo_cni_peer_id_cmp(const struct osmo_cni_peer_id *a, const struct osmo_cni_peer_id *b) +{ + if (a->type != b->type) + return OSMO_CMP(a->type, b->type); + switch (a->type) { + case OSMO_CNI_PEER_ID_IPA_NAME: + return osmo_ipa_name_cmp(&a->ipa_name, &b->ipa_name); + default: + return -EINVAL; + } +} + +const struct value_string osmo_cni_peer_id_type_names[] = { + { OSMO_CNI_PEER_ID_IPA_NAME, "IPA-name" }, + {} +}; + +/* Call osmo_cni_peer_id_to_str_c with OTC_SELECT */ +const char *osmo_cni_peer_id_to_str(const struct osmo_cni_peer_id *cpi) +{ + return osmo_cni_peer_id_to_str_c(OTC_SELECT, cpi); +} + +/* Return an unquoted string, not including the terminating zero. Used for writing VTY config. */ +const char *osmo_cni_peer_id_to_str_c(void *ctx, const struct osmo_cni_peer_id *cpi) +{ + switch (cpi->type) { + case OSMO_CNI_PEER_ID_IPA_NAME: + return osmo_ipa_name_to_str_c(ctx, &cpi->ipa_name); + default: + return talloc_strdup(ctx, osmo_cni_peer_id_type_name(cpi->type)); + } +} diff --git a/src/gsupclient/gsup_req.c b/src/gsupclient/gsup_req.c index 4a2ff23..a8a66d5 100644 --- a/src/gsupclient/gsup_req.c +++ b/src/gsupclient/gsup_req.c @@ -99,7 +99,7 @@ * \return a newly allocated osmo_gsup_req, or NULL on error. If NULL is returned, an error response has already been * dispatched to the send_response_cb. */ -struct osmo_gsup_req *osmo_gsup_req_new(void *ctx, const struct osmo_ipa_name *from_peer, struct msgb *msg, +struct osmo_gsup_req *osmo_gsup_req_new(void *ctx, const struct osmo_cni_peer_id *from_peer, struct msgb *msg, osmo_gsup_req_send_response_t send_response_cb, void *cb_data, struct llist_head *add_to_list) { @@ -109,7 +109,7 @@ struct osmo_gsup_req *osmo_gsup_req_new(void *ctx, const struct osmo_ipa_name *f if (!msgb_l2(msg) || !msgb_l2len(msg)) { LOGP(DLGSUP, LOGL_ERROR, "Rx GSUP from %s: missing or empty L2 data\n", - osmo_ipa_name_to_str(from_peer)); + osmo_cni_peer_id_to_str(from_peer)); msgb_free(msg); return NULL; } @@ -125,7 +125,7 @@ struct osmo_gsup_req *osmo_gsup_req_new(void *ctx, const struct osmo_ipa_name *f req->source_name = *from_peer; rc = osmo_gsup_decode(msgb_l2(req->msg), msgb_l2len(req->msg), (struct osmo_gsup_message*)&req->gsup); if (rc < 0) { - LOGP(DLGSUP, LOGL_ERROR, "Rx GSUP from %s: cannot decode (rc=%d)\n", osmo_ipa_name_to_str(from_peer), rc); + LOGP(DLGSUP, LOGL_ERROR, "Rx GSUP from %s: cannot decode (rc=%d)\n", osmo_cni_peer_id_to_str(from_peer), rc); osmo_gsup_req_free(req); return NULL; } @@ -133,17 +133,18 @@ struct osmo_gsup_req *osmo_gsup_req_new(void *ctx, const struct osmo_ipa_name *f LOG_GSUP_REQ(req, LOGL_DEBUG, "new request: {%s}\n", osmo_gsup_message_to_str_c(OTC_SELECT, &req->gsup)); if (req->gsup.source_name_len) { - if (osmo_ipa_name_set(&req->source_name, req->gsup.source_name, req->gsup.source_name_len)) { + if (osmo_cni_peer_id_set(&req->source_name, OSMO_CNI_PEER_ID_IPA_NAME, + req->gsup.source_name, req->gsup.source_name_len)) { LOGP(DLGSUP, LOGL_ERROR, "Rx GSUP from %s: failed to decode source_name, message is not routable\n", - osmo_ipa_name_to_str(from_peer)); + osmo_cni_peer_id_to_str(from_peer)); osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true); return NULL; } /* The source of the GSUP message is not the immediate GSUP peer; the peer is our proxy for that source. */ - if (osmo_ipa_name_cmp(&req->source_name, from_peer)) + if (osmo_cni_peer_id_cmp(&req->source_name, from_peer)) req->via_proxy = *from_peer; } |