Review at  https://gerrit.osmocom.org/3662

Migrate from gprs_apn_to_str() to libosmocore osmo_apn_to_str()

In 2015, Jacob moved/copied related functions to libosmocore, but
for some reason didn't remove the copies here.  Let's follow-up on
that and remove duplicated code.

The libosmocore commit introducing osmo_apn_to_str() was
8114294bf29ac6e44822c0ae43d4b0819f11b022

Change-Id: I7315ffcbed8a54cca2056f313bb7783ad82d0ee9
---
M include/openbsc/gprs_utils.h
M src/gprs/gb_proxy_patch.c
M src/gprs/gb_proxy_vty.c
M src/gprs/gprs_sgsn.c
M src/gprs/gprs_subscriber.c
M src/gprs/gprs_utils.c
M src/gprs/gtphub.c
M src/gprs/sgsn_cdr.c
M src/gprs/sgsn_vty.c
M tests/gprs/gprs_test.c
M tests/gtphub/Makefile.am
11 files changed, 19 insertions(+), 148 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/osmo-msc refs/changes/62/3662/1

diff --git a/include/openbsc/gprs_utils.h b/include/openbsc/gprs_utils.h
index 603605c..574f5c5 100644
--- a/include/openbsc/gprs_utils.h
+++ b/include/openbsc/gprs_utils.h
@@ -30,7 +30,6 @@
 struct msgb *gprs_msgb_copy(const struct msgb *msg, const char *name);
 int gprs_msgb_resize_area(struct msgb *msg, uint8_t *area,
                            size_t old_size, size_t new_size);
-char *gprs_apn_to_str(char *out_str, const uint8_t *apn_enc, size_t 
rest_chars);
 int gprs_str_to_apn(uint8_t *apn_enc, size_t max_len, const char *str);
 
 /* GSM 04.08, 10.5.7.3 GPRS Timer */
diff --git a/src/gprs/gb_proxy_patch.c b/src/gprs/gb_proxy_patch.c
index 7bddc44..210fb2b 100644
--- a/src/gprs/gb_proxy_patch.c
+++ b/src/gprs/gb_proxy_patch.c
@@ -28,6 +28,7 @@
 
 #include <osmocom/gprs/protocol/gsm_08_18.h>
 #include <osmocom/core/rate_ctr.h>
+#include <osmocom/gsm/apn.h>
 
 /* patch RA identifier in place */
 static void gbproxy_patch_raid(uint8_t *raid_enc, struct gbproxy_peer *peer,
@@ -101,7 +102,7 @@
                LOGP(DGPRS, LOGL_DEBUG,
                     "Patching %s to SGSN: Removing APN '%s'\n",
                     log_text,
-                    gprs_apn_to_str(str1, apn, apn_len));
+                    osmo_apn_to_str(str1, apn, apn_len));
 
                *new_apn_ie_len = 0;
                gprs_msgb_resize_area(msg, apn_ie, apn_ie_len, 0);
@@ -116,8 +117,8 @@
                     "Patching %s to SGSN: "
                     "Replacing APN '%s' -> '%s'\n",
                     log_text,
-                    gprs_apn_to_str(str1, apn, apn_len),
-                    gprs_apn_to_str(str2, peer->cfg->core_apn,
+                    osmo_apn_to_str(str1, apn, apn_len),
+                    osmo_apn_to_str(str2, peer->cfg->core_apn,
                                       peer->cfg->core_apn_size));
 
                *new_apn_ie_len = peer->cfg->core_apn_size + 2;
diff --git a/src/gprs/gb_proxy_vty.c b/src/gprs/gb_proxy_vty.c
index 933b6b0..86d65a8 100644
--- a/src/gprs/gb_proxy_vty.c
+++ b/src/gprs/gb_proxy_vty.c
@@ -29,6 +29,7 @@
 
 #include <openbsc/gsm_04_08.h>
 #include <osmocom/gprs/gprs_ns.h>
+#include <osmocom/gsm/apn.h>
 
 #include <openbsc/debug.h>
 #include <openbsc/gb_proxy.h>
@@ -107,7 +108,7 @@
               if (g_cfg->core_apn_size > 0) {
                       char str[500] = {0};
                       vty_out(vty, " core-access-point-name %s%s",
-                              gprs_apn_to_str(str, g_cfg->core_apn,
+                              osmo_apn_to_str(str, g_cfg->core_apn,
                                                 g_cfg->core_apn_size),
                               VTY_NEWLINE);
               } else {
diff --git a/src/gprs/gprs_sgsn.c b/src/gprs/gprs_sgsn.c
index 93b133f..43eeaaa 100644
--- a/src/gprs/gprs_sgsn.c
+++ b/src/gprs/gprs_sgsn.c
@@ -30,6 +30,7 @@
 #include <osmocom/gprs/gprs_ns.h>
 #include <osmocom/gprs/gprs_bssgp.h>
 #include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
+#include <osmocom/gsm/apn.h>
 
 #include <openbsc/gprs_subscriber.h>
 #include <openbsc/debug.h>
@@ -758,7 +759,7 @@
                        return NULL;
                }
 
-               gprs_apn_to_str(req_apn_str,
+               osmo_apn_to_str(req_apn_str,
                                TLVP_VAL(tp, GSM48_IE_GSM_APN),
                                TLVP_LEN(tp, GSM48_IE_GSM_APN));
 
diff --git a/src/gprs/gprs_subscriber.c b/src/gprs/gprs_subscriber.c
index aa03509..94297d0 100644
--- a/src/gprs/gprs_subscriber.c
+++ b/src/gprs/gprs_subscriber.c
@@ -22,6 +22,7 @@
 
 #include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
 #include <osmocom/gsm/gsup.h>
+#include <osmocom/gsm/apn.h>
 #include <osmocom/core/utils.h>
 #include <osmocom/core/logging.h>
 #include <openbsc/gprs_subscriber.h>
@@ -371,7 +372,7 @@
 
                OSMO_ASSERT(pdp_data != NULL);
                pdp_data->pdp_type = pdp_info->pdp_type;
-               gprs_apn_to_str(pdp_data->apn_str,
+               osmo_apn_to_str(pdp_data->apn_str,
                                pdp_info->apn_enc, pdp_info->apn_enc_len);
                memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, 
pdp_info->qos_enc_len);
                pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
diff --git a/src/gprs/gprs_utils.c b/src/gprs/gprs_utils.c
index 64ed978..91a09d2 100644
--- a/src/gprs/gprs_utils.c
+++ b/src/gprs/gprs_utils.c
@@ -114,34 +114,6 @@
        return 0;
 }
 
-/* TODO: Move these conversion functions to a utils file. */
-/* TODO: consolidate with gprs_apn2str(). */
-/** memmove apn_enc to out_str, replacing the length octets in apn_enc with '.'
- * (omitting the first one) and terminating with a '\0'.
- * out_str needs to have rest_chars amount of bytes or 1 whatever is bigger.
- */
-char * gprs_apn_to_str(char *out_str, const uint8_t *apn_enc, size_t 
rest_chars)
-{
-       char *str = out_str;
-
-       while (rest_chars > 0 && apn_enc[0]) {
-               size_t label_size = apn_enc[0];
-               if (label_size + 1 > rest_chars)
-                       return NULL;
-
-               memmove(str, apn_enc + 1, label_size);
-               str += label_size;
-               rest_chars -= label_size + 1;
-               apn_enc += label_size + 1;
-
-               if (rest_chars)
-                       *(str++) = '.';
-       }
-       str[0] = '\0';
-
-       return out_str;
-}
-
 int gprs_str_to_apn(uint8_t *apn_enc, size_t max_len, const char *str)
 {
        uint8_t *last_len_field;
diff --git a/src/gprs/gtphub.c b/src/gprs/gtphub.c
index 5e7520e..0a8e375 100644
--- a/src/gprs/gtphub.c
+++ b/src/gprs/gtphub.c
@@ -42,6 +42,8 @@
 #include <osmocom/core/rate_ctr.h>
 #include <osmocom/core/stats.h>
 
+#include <osmocom/gsm/apn.h>
+
 
 static const int GTPH_GC_TICK_SECONDS = 1;
 
@@ -498,7 +500,7 @@
                len = sizeof(apn_buf) - 1;
        apn_buf[len] = '\0';
 
-       *apn_str = gprs_apn_to_str(apn_buf, (uint8_t*)apn_buf, len);
+       *apn_str = osmo_apn_to_str(apn_buf, (uint8_t*)apn_buf, len);
        if (!(*apn_str)) {
                LOG(LOGL_ERROR, "APN IE: present but cannot be decoded: %s\n",
                    osmo_hexdump((uint8_t*)apn_buf, len));
diff --git a/src/gprs/sgsn_cdr.c b/src/gprs/sgsn_cdr.c
index 0910896..16ea9d4 100644
--- a/src/gprs/sgsn_cdr.c
+++ b/src/gprs/sgsn_cdr.c
@@ -22,6 +22,7 @@
 #include <openbsc/signal.h>
 #include <openbsc/gprs_utils.h>
 #include <openbsc/debug.h>
+#include <osmocom/gsm/apn.h>
 
 #include <openbsc/vty.h>
 
@@ -145,7 +146,7 @@
 
 
        if (pdp->lib) {
-               gprs_apn_to_str(apni, pdp->lib->apn_use.v, pdp->lib->apn_use.l);
+               osmo_apn_to_str(apni, pdp->lib->apn_use.v, pdp->lib->apn_use.l);
                inet_ntop(AF_INET, &pdp->lib->hisaddr0.s_addr, ggsn_addr, 
sizeof(ggsn_addr));
                extract_eua(&pdp->lib->eua, eua_addr);
        }
diff --git a/src/gprs/sgsn_vty.c b/src/gprs/sgsn_vty.c
index cc8d6c3..cf44cc4 100644
--- a/src/gprs/sgsn_vty.c
+++ b/src/gprs/sgsn_vty.c
@@ -28,6 +28,7 @@
 #include <osmocom/core/utils.h>
 #include <osmocom/core/rate_ctr.h>
 #include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
+#include <osmocom/gsm/apn.h>
 
 #include <openbsc/debug.h>
 #include <openbsc/sgsn.h>
@@ -116,7 +117,6 @@
 
 
 #define GSM48_MAX_APN_LEN      102     /* 10.5.6.1 */
-/* TODO: consolidate with gprs_apn_to_str(). */
 /** Copy apn to a static buffer, replacing the length octets in apn_enc with 
'.'
  * and terminating with a '\0'. Return the static buffer.
  * len: the length of the encoded APN (which has no terminating zero).
@@ -124,23 +124,10 @@
 static char *gprs_apn2str(uint8_t *apn, unsigned int len)
 {
        static char apnbuf[GSM48_MAX_APN_LEN+1];
-       unsigned int i = 0;
 
        if (!apn)
                return "";
-
-       if (len > sizeof(apnbuf)-1)
-               len = sizeof(apnbuf)-1;
-
-       memcpy(apnbuf, apn, len);
-       apnbuf[len] = '\0';
-
-       /* replace the domain name step sizes with dots */
-       while (i < len) {
-               unsigned int step = apnbuf[i];
-               apnbuf[i] = '.';
-               i += step+1;
-       }
+       osmo_apn_to_str(apnbuf, apn, len);
 
        return apnbuf+1;
 }
diff --git a/tests/gprs/gprs_test.c b/tests/gprs/gprs_test.c
index ff77404..aac9bb8 100644
--- a/tests/gprs/gprs_test.c
+++ b/tests/gprs/gprs_test.c
@@ -48,101 +48,6 @@
        ASSERT_FALSE(nu_is_retransmission(479, 511)); // wrapped
 }
 
-static void apn_round_trip(const uint8_t *input, size_t len, const char 
*wanted_output)
-{
-       char output[len ? len : 1];
-       uint8_t encoded[len + 50];
-       char *out_str;
-       int enc_len;
-
-       /* decode and verify we have what we want */
-       out_str = gprs_apn_to_str(output, input, len);
-       OSMO_ASSERT(out_str);
-       OSMO_ASSERT(out_str == &output[0]);
-       OSMO_ASSERT(strlen(out_str) == strlen(wanted_output));
-       OSMO_ASSERT(strcmp(out_str, wanted_output) == 0);
-
-       /* encode and verify it */
-       if (len != 0) {
-               enc_len = gprs_str_to_apn(encoded, ARRAY_SIZE(encoded), 
wanted_output);
-               OSMO_ASSERT(enc_len == len);
-               OSMO_ASSERT(memcmp(encoded, input, enc_len) == 0);
-       } else {
-               enc_len = gprs_str_to_apn(encoded, 0, wanted_output);
-               OSMO_ASSERT(enc_len == -1);
-       }
-}
-
-static void test_gsm_03_03_apn(void)
-{
-
-       {
-               /* test invalid writes */
-               const uint8_t ref[10] = { 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xAB, 
0xAC, 0xAD, 0xAE, 0xAF };
-               uint8_t output[10];
-               int enc_len;
-
-               memcpy(output, ref, ARRAY_SIZE(output));
-               enc_len = gprs_str_to_apn(output, 0, "");
-               OSMO_ASSERT(enc_len == -1);
-               OSMO_ASSERT(memcmp(ref, output, ARRAY_SIZE(ref)) == 0);
-
-               memcpy(output, ref, ARRAY_SIZE(output));
-               enc_len = gprs_str_to_apn(output, 0, "foo");
-               OSMO_ASSERT(enc_len == -1);
-               OSMO_ASSERT(memcmp(ref, output, ARRAY_SIZE(ref)) == 0);
-
-               memcpy(output, ref, ARRAY_SIZE(output));
-               enc_len = gprs_str_to_apn(output, 1, "foo");
-               OSMO_ASSERT(enc_len == -1);
-               OSMO_ASSERT(memcmp(ref + 1, output + 1, ARRAY_SIZE(ref) - 1) == 
0);
-
-               memcpy(output, ref, ARRAY_SIZE(output));
-               enc_len = gprs_str_to_apn(output, 2, "foo");
-               OSMO_ASSERT(enc_len == -1);
-               OSMO_ASSERT(memcmp(ref + 2, output + 2, ARRAY_SIZE(ref) - 2) == 
0);
-
-               memcpy(output, ref, ARRAY_SIZE(output));
-               enc_len = gprs_str_to_apn(output, 3, "foo");
-               OSMO_ASSERT(enc_len == -1);
-               OSMO_ASSERT(memcmp(ref + 3, output + 3, ARRAY_SIZE(ref) - 3) == 
0);
-       }
-
-       {
-               /* single empty label */
-               uint8_t input[] = { 0x0 };
-               const char *output = "";
-               apn_round_trip(input, ARRAY_SIZE(input), output);
-       }
-
-       {
-               /* no label */
-               uint8_t input[] = { };
-               const char *output = "";
-               apn_round_trip(input, ARRAY_SIZE(input), output);
-       }
-
-       {
-               /* single label with A */
-               uint8_t input[] = { 0x1, 65 };
-               const char *output = "A";
-               apn_round_trip(input, ARRAY_SIZE(input), output);
-               OSMO_ASSERT(gprs_apn_to_str(NULL, input, ARRAY_SIZE(input) - 1) 
== NULL);
-       }
-
-       {
-               uint8_t input[] = { 0x3, 65, 66, 67, 0x2, 90, 122 };
-               const char *output = "ABC.Zz";
-               char tmp[strlen(output) + 1];
-               apn_round_trip(input, ARRAY_SIZE(input), output);
-               OSMO_ASSERT(gprs_apn_to_str(tmp, input, ARRAY_SIZE(input) - 1) 
== NULL);
-               OSMO_ASSERT(gprs_apn_to_str(tmp, input, ARRAY_SIZE(input) - 2) 
== NULL);
-               OSMO_ASSERT(gprs_apn_to_str(tmp, input, ARRAY_SIZE(input) - 4) 
== NULL);
-               OSMO_ASSERT(gprs_apn_to_str(tmp, input, ARRAY_SIZE(input) - 5) 
== NULL);
-               OSMO_ASSERT(gprs_apn_to_str(tmp, input, ARRAY_SIZE(input) - 6) 
== NULL);
-       }
-}
-
 static void test_gprs_timer_enc_dec(void)
 {
        int i, u, secs, tmr;
@@ -228,7 +133,6 @@
        osmo_init_logging(&info);
 
        test_8_4_2();
-       test_gsm_03_03_apn();
        test_gprs_timer_enc_dec();
 
        printf("Done.\n");
diff --git a/tests/gtphub/Makefile.am b/tests/gtphub/Makefile.am
index 5c834b7..f2a6b88 100644
--- a/tests/gtphub/Makefile.am
+++ b/tests/gtphub/Makefile.am
@@ -8,6 +8,7 @@
        -ggdb3 \
        $(LIBOSMOCORE_CFLAGS) \
        $(LIBOSMOABIS_CFLAGS) \
+       $(LIBOSMOGSM_CFLAGS) \
        $(LIBGTP_CFLAGS) \
        $(NULL)
 
@@ -37,6 +38,7 @@
        $(top_builddir)/src/gprs/gtphub.o \
        $(top_builddir)/src/gprs/gprs_utils.o \
        $(LIBOSMOCORE_LIBS) \
+       $(LIBOSMOGSM_LIBS) \
        $(LIBGTP_LIBS) \
        -lrt \
        $(NULL)

-- 
To view, visit https://gerrit.osmocom.org/3662
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7315ffcbed8a54cca2056f313bb7783ad82d0ee9
Gerrit-PatchSet: 1
Gerrit-Project: osmo-msc
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofm...@sysmocom.de>

Reply via email to