From ca6054ef8c30646bc36b0de60d28523804e717c3 Mon Sep 17 00:00:00 2001 From: Philipp Maier Date: Fri, 28 Jan 2022 11:42:52 +0100 Subject: [PATCH] ranap_common_cn: add functions for direct access to decoder The message encoder functions that decode a message to a struct ranap_message are only accessible via a callback function that is passed to ranap_cn_rx_cx The decoded ranap_message only lives inside the callback since it is freed by ranap_cn_rx_cx when the callback is done. In some situations this might make using the decoder incredibly difficult since it is not possible to keep the decoding results for an extended amount of time. Lets put the decoding into a separate function and use this function in ranap_cn_rx_cx. Then lets make the decoder and the free function public so that the decoder can be used in a more open way. Change-Id: I14d2ed8e597a5d12024a6a6c72ff011dbeb2549d Related: OS#5152 --- include/osmocom/ranap/ranap_common_cn.h | 12 +++++ src/ranap_common_cn.c | 72 ++++++++++++++++--------- 2 files changed, 58 insertions(+), 26 deletions(-) diff --git a/include/osmocom/ranap/ranap_common_cn.h b/include/osmocom/ranap/ranap_common_cn.h index dfce3584..90ff6daf 100644 --- a/include/osmocom/ranap/ranap_common_cn.h +++ b/include/osmocom/ranap/ranap_common_cn.h @@ -7,8 +7,20 @@ typedef void (*ranap_handle_cb)(void *ctx, ranap_message *ranap_msg); +/* free a decoded connection-less RANAP message */ +void ranap_cn_rx_cl_free(ranap_message *message); + +/* decode a connection-less RANAP message */ +int ranap_cn_rx_cl_decode(void *ctx, ranap_message *message, uint8_t *data, size_t len); + /* receive a connection-less RANAP message */ int ranap_cn_rx_cl(ranap_handle_cb cb, void *ctx, uint8_t *data, size_t len); +/* free a decoded connection-oriented RANAP message */ +void ranap_cn_rx_co_free(ranap_message *message); + +/* decode a connection-oriented RANAP message */ +int ranap_cn_rx_co_decode(void *ctx, ranap_message *message, uint8_t *data, size_t len); + /* receive a connection-oriented RANAP message */ int ranap_cn_rx_co(ranap_handle_cb cb, void *ctx, uint8_t *data, size_t len); diff --git a/src/ranap_common_cn.c b/src/ranap_common_cn.c index 03ce9ead..e7b83398 100644 --- a/src/ranap_common_cn.c +++ b/src/ranap_common_cn.c @@ -257,7 +257,8 @@ static int _cn_ranap_rx_co(void *ctx, RANAP_RANAP_PDU_t *pdu, ranap_message *mes return rc; } -static void _cn_ranap_free_co(ranap_message *message) +/* free a decoded connection-oriented RANAP message */ +void ranap_cn_rx_co_free(ranap_message *message) { switch (message->direction) { case RANAP_RANAP_PDU_PR_initiatingMessage: @@ -281,27 +282,38 @@ static void _cn_ranap_free_co(ranap_message *message) } } -/* receive a connection-oriented RANAP message and call - * cn_ranap_handle_co() with the resulting ranap_message struct */ -int ranap_cn_rx_co(ranap_handle_cb cb, void *ctx, uint8_t *data, size_t len) +/* decode a connection-oriented RANAP message */ +int ranap_cn_rx_co_decode(void *ctx, ranap_message *message, uint8_t *data, size_t len) { RANAP_RANAP_PDU_t *pdu = NULL; - ranap_message message; asn_dec_rval_t dec_ret; int rc; - memset(&message, 0, sizeof(message)); + memset(message, 0, sizeof(*message)); - dec_ret = aper_decode(NULL,&asn_DEF_RANAP_RANAP_PDU, (void **) &pdu, - data, len, 0, 0); + dec_ret = aper_decode(NULL, &asn_DEF_RANAP_RANAP_PDU, (void **)&pdu, data, len, 0, 0); if (dec_ret.code != RC_OK) { LOGP(DRANAP, LOGL_ERROR, "Error in RANAP ASN.1 decode\n"); return -1; } - message.direction = pdu->present; + message->direction = pdu->present; - rc = _cn_ranap_rx_co(ctx, pdu, &message); + rc = _cn_ranap_rx_co(ctx, pdu, message); + + ASN_STRUCT_FREE(asn_DEF_RANAP_RANAP_PDU, pdu); + + return rc; +} + +/* receive a connection-oriented RANAP message and call + * cn_ranap_handle_co() with the resulting ranap_message struct */ +int ranap_cn_rx_co(ranap_handle_cb cb, void *ctx, uint8_t *data, size_t len) +{ + ranap_message message; + int rc; + + rc = ranap_cn_rx_co_decode(ctx, &message, data, len); if (rc == 0) (*cb)(ctx, &message); @@ -309,9 +321,7 @@ int ranap_cn_rx_co(ranap_handle_cb cb, void *ctx, uint8_t *data, size_t len) LOGP(DRANAP, LOGL_ERROR, "Not calling cn_ranap_handle_co() due to rc=%d\n", rc); /* Free the asn1 structs in message */ - _cn_ranap_free_co(&message); - - ASN_STRUCT_FREE(asn_DEF_RANAP_RANAP_PDU, pdu); + ranap_cn_rx_co_free(&message); return rc; } @@ -496,7 +506,8 @@ static int _cn_ranap_rx_cl(void *ctx, RANAP_RANAP_PDU_t *pdu, ranap_message *mes return rc; } -static void _cn_ranap_free_cl(ranap_message *message) +/* free a decoded connection-less RANAP message */ +void ranap_cn_rx_cl_free(ranap_message *message) { switch (message->direction) { case RANAP_RANAP_PDU_PR_initiatingMessage: @@ -516,27 +527,38 @@ static void _cn_ranap_free_cl(ranap_message *message) } } -/* receive a connection-less RANAP message and call - * cn_ranap_handle_co() with the resulting ranap_message struct */ -int ranap_cn_rx_cl(ranap_handle_cb cb, void *ctx, uint8_t *data, size_t len) +/* decode a connection-less RANAP message */ +int ranap_cn_rx_cl_decode(void *ctx, ranap_message *message, uint8_t *data, size_t len) { RANAP_RANAP_PDU_t *pdu = NULL; - ranap_message message; asn_dec_rval_t dec_ret; int rc; - memset(&message, 0, sizeof(message)); + memset(message, 0, sizeof(*message)); - dec_ret = aper_decode(NULL,&asn_DEF_RANAP_RANAP_PDU, (void **) &pdu, - data, len, 0, 0); + dec_ret = aper_decode(NULL, &asn_DEF_RANAP_RANAP_PDU, (void **)&pdu, data, len, 0, 0); if (dec_ret.code != RC_OK) { LOGP(DRANAP, LOGL_ERROR, "Error in RANAP ASN.1 decode\n"); return -1; } - message.direction = pdu->present; + message->direction = pdu->present; - rc = _cn_ranap_rx_cl(ctx, pdu, &message); + rc = _cn_ranap_rx_cl(ctx, pdu, message); + + ASN_STRUCT_FREE(asn_DEF_RANAP_RANAP_PDU, pdu); + + return rc; +} + +/* receive a connection-less RANAP message and call + * cn_ranap_handle_co() with the resulting ranap_message struct */ +int ranap_cn_rx_cl(ranap_handle_cb cb, void *ctx, uint8_t *data, size_t len) +{ + ranap_message message; + int rc; + + rc = ranap_cn_rx_cl_decode(ctx, &message, data, len); if (rc == 0) (*cb)(ctx, &message); @@ -544,9 +566,7 @@ int ranap_cn_rx_cl(ranap_handle_cb cb, void *ctx, uint8_t *data, size_t len) LOGP(DRANAP, LOGL_ERROR, "Not calling cn_ranap_handle_cl() due to rc=%d\n", rc); /* Free the asn1 structs in message */ - _cn_ranap_free_cl(&message); - - ASN_STRUCT_FREE(asn_DEF_RANAP_RANAP_PDU, pdu); + ranap_cn_rx_cl_free(&message); return rc; }