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
This commit is contained in:
Harald Welte 2017-08-12 13:43:54 +02:00 committed by Neels Hofmeyr
parent 0f7013266b
commit 586e6819eb
11 changed files with 19 additions and 148 deletions

View File

@ -30,7 +30,6 @@ struct gprs_ra_id;
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 */

View File

@ -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 @@ static void gbproxy_patch_apn_ie(struct msgb *msg,
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 @@ static void gbproxy_patch_apn_ie(struct msgb *msg,
"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;

View File

@ -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 @@ static int config_write_gbproxy(struct vty *vty)
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 {

View File

@ -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 @@ struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx,
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));

View File

@ -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 @@ static void gprs_subscr_gsup_insert_data(struct gprs_subscr *subscr,
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;

View File

@ -114,34 +114,6 @@ int gprs_msgb_resize_area(struct msgb *msg, uint8_t *area,
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;

View File

@ -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 @@ static int get_ie_apn_str(union gtpie_member *ie[], const char **apn_str)
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));

View File

@ -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 @@ static void cdr_log_pdp(struct sgsn_instance *inst, const char *ev,
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);
}

View File

@ -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 @@ DECLARE_TIMER(3397, "Wait for DEACT AA PDP CTX ACK timer (s)")
#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 @@ DECLARE_TIMER(3397, "Wait for DEACT AA PDP CTX ACK timer (s)")
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;
}

View File

@ -48,101 +48,6 @@ static void test_8_4_2()
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 @@ int main(int argc, char **argv)
osmo_init_logging(&info);
test_8_4_2();
test_gsm_03_03_apn();
test_gprs_timer_enc_dec();
printf("Done.\n");

View File

@ -8,6 +8,7 @@ AM_CFLAGS = \
-ggdb3 \
$(LIBOSMOCORE_CFLAGS) \
$(LIBOSMOABIS_CFLAGS) \
$(LIBOSMOGSM_CFLAGS) \
$(LIBGTP_CFLAGS) \
$(NULL)
@ -37,6 +38,7 @@ gtphub_test_LDADD = \
$(top_builddir)/src/gprs/gtphub.o \
$(top_builddir)/src/gprs/gprs_utils.o \
$(LIBOSMOCORE_LIBS) \
$(LIBOSMOGSM_LIBS) \
$(LIBGTP_LIBS) \
-lrt \
$(NULL)