From 945b252a483f5368af242408022c055e1ef53d3f Mon Sep 17 00:00:00 2001 From: Pau Espin Pedrol Date: Wed, 20 Sep 2023 19:13:33 +0200 Subject: sccp2sua: Produce smaller output if msg contains no optional section Change-Id: I5c8fcb30b76ac7bafd4a99bc3004049644e7e3f9 --- src/sccp2sua.c | 18 +++++++++++++----- tests/xua/xua_test.c | 4 ++++ tests/xua/xua_test.ok | 3 ++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/sccp2sua.c b/src/sccp2sua.c index 9bad528..cdab771 100644 --- a/src/sccp2sua.c +++ b/src/sccp2sua.c @@ -987,18 +987,26 @@ static int xua_ies_to_sccp_opts(struct msgb *msg, uint8_t *ptr_opt, enum sccp_message_types type, const struct xua_msg *xua) { const struct xua_msg_part *part; - - /* store relative pointer to start of optional part */ - *ptr_opt = msg->tail - ptr_opt; + bool any_added = false; + uint8_t *old_tail = msg->tail; llist_for_each_entry(part, &xua->headers, entry) { /* make sure we don't add a SCCP option for information * that is already present in mandatory fixed or * mandatory variable parts of the header */ - if (!sccp_is_mandatory(type, part) && sccp_option_permitted(type, part)) + if (!sccp_is_mandatory(type, part) && sccp_option_permitted(type, part)) { sccp_msg_add_sua_opt(type, msg, part); + any_added = true; + } + } + if (any_added) { + msgb_put_u8(msg, SCCP_PNC_END_OF_OPTIONAL); + /* store relative pointer to start of optional part */ + *ptr_opt = old_tail - ptr_opt; + } else { + /* If nothing was added, simply update the pointer to 0 to signal the optional part is omitted. */ + ptr_opt[0] = 0; } - msgb_put_u8(msg, SCCP_PNC_END_OF_OPTIONAL); return 0; } diff --git a/tests/xua/xua_test.c b/tests/xua/xua_test.c index 6b4eef5..628d860 100644 --- a/tests/xua/xua_test.c +++ b/tests/xua/xua_test.c @@ -439,6 +439,10 @@ static const struct sccp2sua_testcase sccp2sua_testcases[] = { }, }, }, { + /* This case expectedly prints "Input != re-encoded output!" because input has + * (struct sccp_connection_confirm).optional_start=1 pointing to SCCP_PNC_END_OF_OPTIONAL, + * while our (re-)encoder uses optional_start=0 which means entire msg is 1 byte + * lower (hence better). Both are valid. */ .name = "BSSMAP-CC", .sccp = PANDSIZ(bssmap_cc), .sua = { diff --git a/tests/xua/xua_test.ok b/tests/xua/xua_test.ok index 6738cc0..cb6aab1 100644 --- a/tests/xua/xua_test.ok +++ b/tests/xua/xua_test.ok @@ -55,7 +55,8 @@ SCCP Input: [L2]> 02 01 02 03 00 00 03 02 01 00 Transcoding message SCCP -> XUA Decoded SUA: HDR=(CO:COAK,V=0,LEN=0), PART(T=Protocol Class,L=4,D=00000002), PART(T=Destination Reference,L=4,D=00010203), PART(T=Source Reference,L=4,D=00000003) Re-Encoding decoded SUA to SCCP -SCCP Output: [L2]> 02 01 02 03 00 00 03 02 01 00 +SCCP Output: [L2]> 02 01 02 03 00 00 03 02 00 +Input != re-encoded output! => BSSMAP-DTAP SCCP Input: [L2]> 06 00 00 03 00 01 0f 01 00 0c 03 05 5c 08 11 81 33 66 02 13 45 f4 -- cgit v1.2.3