gprs: Create a gprs_utils file and move to be shared code in there
We intend to move some of these routines to libosmocore but to avoid a feature symbol clash we are prefixing these routines with gprs_. Done with Jacob
This commit is contained in:
parent
1ddd9e518e
commit
7127b0295e
|
@ -14,7 +14,7 @@ noinst_HEADERS = abis_nm.h abis_rsl.h db.h gsm_04_08.h gsm_data.h \
|
|||
osmo_msc_data.h osmo_bsc_grace.h sms_queue.h abis_om2000.h \
|
||||
bss.h gsm_data_shared.h control_cmd.h ipaccess.h mncc_int.h \
|
||||
arfcn_range_encode.h nat_rewrite_trie.h bsc_nat_callstats.h \
|
||||
osmux.h mgcp_transcode.h rtp.h
|
||||
osmux.h mgcp_transcode.h rtp.h gprs_utils.h
|
||||
|
||||
openbsc_HEADERS = gsm_04_08.h meas_rep.h bsc_api.h
|
||||
openbscdir = $(includedir)/openbsc
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
/* GPRS utility functions */
|
||||
|
||||
/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
|
||||
* (C) 2010-2014 by On-Waves
|
||||
* (C) 2013 by Holger Hans Peter Freyther
|
||||
* All Rights Reserved
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
struct msgb;
|
||||
|
||||
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, const char *str, size_t max_chars);
|
|
@ -14,7 +14,7 @@ bin_PROGRAMS = osmo-gbproxy
|
|||
endif
|
||||
|
||||
osmo_gbproxy_SOURCES = gb_proxy.c gb_proxy_main.c gb_proxy_vty.c \
|
||||
gprs_llc_parse.c crc24.c
|
||||
gprs_llc_parse.c crc24.c gprs_utils.c
|
||||
osmo_gbproxy_LDADD = $(top_builddir)/src/libcommon/libcommon.a \
|
||||
$(OSMO_LIBS)
|
||||
|
||||
|
|
|
@ -47,6 +47,7 @@
|
|||
#include <openbsc/gprs_llc.h>
|
||||
#include <openbsc/gsm_04_08.h>
|
||||
#include <openbsc/gsm_04_08_gprs.h>
|
||||
#include <openbsc/gprs_utils.h>
|
||||
|
||||
enum gbprox_global_ctr {
|
||||
GBPROX_GLOB_CTR_INV_BVCI,
|
||||
|
@ -229,47 +230,6 @@ static void peer_free(struct gbproxy_peer *peer)
|
|||
talloc_free(peer);
|
||||
}
|
||||
|
||||
/* FIXME: this needs to go to libosmocore/msgb.c */
|
||||
static struct msgb *msgb_copy(const struct msgb *msg, const char *name)
|
||||
{
|
||||
struct libgb_msgb_cb *old_cb, *new_cb;
|
||||
struct msgb *new_msg;
|
||||
|
||||
new_msg = msgb_alloc(msg->data_len, name);
|
||||
if (!new_msg)
|
||||
return NULL;
|
||||
|
||||
/* copy data */
|
||||
memcpy(new_msg->_data, msg->_data, new_msg->data_len);
|
||||
|
||||
/* copy header */
|
||||
new_msg->len = msg->len;
|
||||
new_msg->data += msg->data - msg->_data;
|
||||
new_msg->head += msg->head - msg->_data;
|
||||
new_msg->tail += msg->tail - msg->_data;
|
||||
|
||||
new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
|
||||
new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data);
|
||||
new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data);
|
||||
new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data);
|
||||
|
||||
/* copy GB specific data */
|
||||
old_cb = LIBGB_MSGB_CB(msg);
|
||||
new_cb = LIBGB_MSGB_CB(new_msg);
|
||||
|
||||
new_cb->bssgph = new_msg->_data + (old_cb->bssgph - msg->_data);
|
||||
new_cb->llch = new_msg->_data + (old_cb->llch - msg->_data);
|
||||
|
||||
/* bssgp_cell_id is a pointer into the old msgb, so we need to make
|
||||
* it a pointer into the new msgb */
|
||||
new_cb->bssgp_cell_id = new_msg->_data + (old_cb->bssgp_cell_id - msg->_data);
|
||||
new_cb->nsei = old_cb->nsei;
|
||||
new_cb->bvci = old_cb->bvci;
|
||||
new_cb->tlli = old_cb->tlli;
|
||||
|
||||
return new_msg;
|
||||
}
|
||||
|
||||
/* strip off the NS header */
|
||||
static void strip_ns_hdr(struct msgb *msg)
|
||||
{
|
||||
|
@ -277,89 +237,6 @@ static void strip_ns_hdr(struct msgb *msg)
|
|||
msgb_pull(msg, strip_len);
|
||||
}
|
||||
|
||||
/* TODO: Move this to libosmocore/msgb.c */
|
||||
static int msgb_resize_area(struct msgb *msg, uint8_t *area,
|
||||
size_t old_size, size_t new_size)
|
||||
{
|
||||
int rc;
|
||||
uint8_t *rest = area + old_size;
|
||||
int rest_len = msg->len - old_size - (area - msg->data);
|
||||
int delta_size = (int)new_size - (int)old_size;
|
||||
|
||||
if (delta_size == 0)
|
||||
return 0;
|
||||
|
||||
if (delta_size > 0) {
|
||||
rc = msgb_trim(msg, msg->len + delta_size);
|
||||
if (rc < 0)
|
||||
return rc;
|
||||
}
|
||||
|
||||
memmove(area + new_size, area + old_size, rest_len);
|
||||
|
||||
if (msg->l1h >= rest)
|
||||
msg->l1h += delta_size;
|
||||
if (msg->l2h >= rest)
|
||||
msg->l2h += delta_size;
|
||||
if (msg->l3h >= rest)
|
||||
msg->l3h += delta_size;
|
||||
if (msg->l4h >= rest)
|
||||
msg->l4h += delta_size;
|
||||
|
||||
if (delta_size < 0)
|
||||
msgb_trim(msg, msg->len + delta_size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* TODO: Move these conversion functions to a utils file. */
|
||||
char * gbprox_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 gbprox_str_to_apn(uint8_t *apn_enc, const char *str, size_t max_chars)
|
||||
{
|
||||
uint8_t *last_len_field = apn_enc;
|
||||
int len = 1;
|
||||
apn_enc += 1;
|
||||
|
||||
while (str[0]) {
|
||||
if (str[0] == '.') {
|
||||
*last_len_field = (apn_enc - last_len_field) - 1;
|
||||
last_len_field = apn_enc;
|
||||
} else {
|
||||
*apn_enc = str[0];
|
||||
}
|
||||
apn_enc += 1;
|
||||
str += 1;
|
||||
len += 1;
|
||||
if (len > max_chars)
|
||||
return -1;
|
||||
}
|
||||
|
||||
*last_len_field = (apn_enc - last_len_field) - 1;
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
/* TODO: Move shift functions to libosmocore */
|
||||
|
||||
int v_fixed_shift(uint8_t **data, size_t *data_len,
|
||||
|
@ -888,10 +765,10 @@ static void gbprox_patch_apn_ie(struct msgb *msg,
|
|||
LOGP(DGPRS, LOGL_DEBUG,
|
||||
"Patching %s to SGSN: Removing APN '%s'\n",
|
||||
log_text,
|
||||
gbprox_apn_to_str(str1, apn, apn_len));
|
||||
gprs_apn_to_str(str1, apn, apn_len));
|
||||
|
||||
*new_apn_ie_len = 0;
|
||||
msgb_resize_area(msg, apn_ie, apn_ie_len, 0);
|
||||
gprs_msgb_resize_area(msg, apn_ie, apn_ie_len, 0);
|
||||
} else {
|
||||
/* Resize the IE */
|
||||
char str1[110];
|
||||
|
@ -903,12 +780,12 @@ static void gbprox_patch_apn_ie(struct msgb *msg,
|
|||
"Patching %s to SGSN: "
|
||||
"Replacing APN '%s' -> '%s'\n",
|
||||
log_text,
|
||||
gbprox_apn_to_str(str1, apn, apn_len),
|
||||
gbprox_apn_to_str(str2, gbcfg.core_apn,
|
||||
gprs_apn_to_str(str1, apn, apn_len),
|
||||
gprs_apn_to_str(str2, gbcfg.core_apn,
|
||||
gbcfg.core_apn_size));
|
||||
|
||||
*new_apn_ie_len = gbcfg.core_apn_size + 2;
|
||||
msgb_resize_area(msg, apn, apn_len, gbcfg.core_apn_size);
|
||||
gprs_msgb_resize_area(msg, apn, apn_len, gbcfg.core_apn_size);
|
||||
memcpy(apn, gbcfg.core_apn, gbcfg.core_apn_size);
|
||||
hdr->apn_len = gbcfg.core_apn_size;
|
||||
}
|
||||
|
@ -1411,7 +1288,7 @@ static int gbprox_relay2sgsn(struct msgb *old_msg,
|
|||
{
|
||||
/* create a copy of the message so the old one can
|
||||
* be free()d safely when we return from gbprox_rcvmsg() */
|
||||
struct msgb *msg = msgb_copy(old_msg, "msgb_relay2sgsn");
|
||||
struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2sgsn");
|
||||
int rc;
|
||||
|
||||
gbprox_patch_bssgp_message(msg, peer, 0);
|
||||
|
@ -1437,7 +1314,7 @@ static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
|
|||
{
|
||||
/* create a copy of the message so the old one can
|
||||
* be free()d safely when we return from gbprox_rcvmsg() */
|
||||
struct msgb *msg = msgb_copy(old_msg, "msgb_relay2peer");
|
||||
struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2peer");
|
||||
int rc;
|
||||
|
||||
DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n",
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
|
||||
#include <openbsc/debug.h>
|
||||
#include <openbsc/gb_proxy.h>
|
||||
#include <openbsc/gprs_utils.h>
|
||||
#include <openbsc/vty.h>
|
||||
|
||||
#include <osmocom/vty/command.h>
|
||||
|
@ -92,7 +93,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",
|
||||
gbprox_apn_to_str(str, g_cfg->core_apn,
|
||||
gprs_apn_to_str(str, g_cfg->core_apn,
|
||||
g_cfg->core_apn_size));
|
||||
} else {
|
||||
vty_out(vty, " core-access-point-name none");
|
||||
|
@ -229,7 +230,7 @@ static int set_core_apn(struct vty *vty, const char *apn, const char *filter)
|
|||
g_cfg->core_apn =
|
||||
talloc_realloc_size(NULL, g_cfg->core_apn, apn_len + 1);
|
||||
g_cfg->core_apn_size =
|
||||
gbprox_str_to_apn(g_cfg->core_apn, apn, apn_len + 1);
|
||||
gprs_str_to_apn(g_cfg->core_apn, apn, apn_len + 1);
|
||||
}
|
||||
|
||||
return CMD_SUCCESS;
|
||||
|
|
|
@ -0,0 +1,152 @@
|
|||
/* GPRS utility functions */
|
||||
|
||||
/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
|
||||
* (C) 2010-2014 by On-Waves
|
||||
* (C) 2013 by Holger Hans Peter Freyther
|
||||
* All Rights Reserved
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
#include <openbsc/gprs_utils.h>
|
||||
|
||||
#include <osmocom/core/msgb.h>
|
||||
#include <osmocom/gprs/gprs_ns.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/* FIXME: this needs to go to libosmocore/msgb.c */
|
||||
struct msgb *gprs_msgb_copy(const struct msgb *msg, const char *name)
|
||||
{
|
||||
struct libgb_msgb_cb *old_cb, *new_cb;
|
||||
struct msgb *new_msg;
|
||||
|
||||
new_msg = msgb_alloc(msg->data_len, name);
|
||||
if (!new_msg)
|
||||
return NULL;
|
||||
|
||||
/* copy data */
|
||||
memcpy(new_msg->_data, msg->_data, new_msg->data_len);
|
||||
|
||||
/* copy header */
|
||||
new_msg->len = msg->len;
|
||||
new_msg->data += msg->data - msg->_data;
|
||||
new_msg->head += msg->head - msg->_data;
|
||||
new_msg->tail += msg->tail - msg->_data;
|
||||
|
||||
new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
|
||||
new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data);
|
||||
new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data);
|
||||
new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data);
|
||||
|
||||
/* copy GB specific data */
|
||||
old_cb = LIBGB_MSGB_CB(msg);
|
||||
new_cb = LIBGB_MSGB_CB(new_msg);
|
||||
|
||||
new_cb->bssgph = new_msg->_data + (old_cb->bssgph - msg->_data);
|
||||
new_cb->llch = new_msg->_data + (old_cb->llch - msg->_data);
|
||||
|
||||
/* bssgp_cell_id is a pointer into the old msgb, so we need to make
|
||||
* it a pointer into the new msgb */
|
||||
new_cb->bssgp_cell_id = new_msg->_data + (old_cb->bssgp_cell_id - msg->_data);
|
||||
new_cb->nsei = old_cb->nsei;
|
||||
new_cb->bvci = old_cb->bvci;
|
||||
new_cb->tlli = old_cb->tlli;
|
||||
|
||||
return new_msg;
|
||||
}
|
||||
|
||||
/* TODO: Move this to libosmocore/msgb.c */
|
||||
int gprs_msgb_resize_area(struct msgb *msg, uint8_t *area,
|
||||
size_t old_size, size_t new_size)
|
||||
{
|
||||
int rc;
|
||||
uint8_t *rest = area + old_size;
|
||||
int rest_len = msg->len - old_size - (area - msg->data);
|
||||
int delta_size = (int)new_size - (int)old_size;
|
||||
|
||||
if (delta_size == 0)
|
||||
return 0;
|
||||
|
||||
if (delta_size > 0) {
|
||||
rc = msgb_trim(msg, msg->len + delta_size);
|
||||
if (rc < 0)
|
||||
return rc;
|
||||
}
|
||||
|
||||
memmove(area + new_size, area + old_size, rest_len);
|
||||
|
||||
if (msg->l1h >= rest)
|
||||
msg->l1h += delta_size;
|
||||
if (msg->l2h >= rest)
|
||||
msg->l2h += delta_size;
|
||||
if (msg->l3h >= rest)
|
||||
msg->l3h += delta_size;
|
||||
if (msg->l4h >= rest)
|
||||
msg->l4h += delta_size;
|
||||
|
||||
if (delta_size < 0)
|
||||
msgb_trim(msg, msg->len + delta_size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* TODO: Move these conversion functions to a utils file. */
|
||||
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, const char *str, size_t max_chars)
|
||||
{
|
||||
uint8_t *last_len_field = apn_enc;
|
||||
int len = 1;
|
||||
apn_enc += 1;
|
||||
|
||||
while (str[0]) {
|
||||
if (str[0] == '.') {
|
||||
*last_len_field = (apn_enc - last_len_field) - 1;
|
||||
last_len_field = apn_enc;
|
||||
} else {
|
||||
*apn_enc = str[0];
|
||||
}
|
||||
apn_enc += 1;
|
||||
str += 1;
|
||||
len += 1;
|
||||
if (len > max_chars)
|
||||
return -1;
|
||||
}
|
||||
|
||||
*last_len_field = (apn_enc - last_len_field) - 1;
|
||||
|
||||
return len;
|
||||
}
|
||||
|
|
@ -11,6 +11,7 @@ gbproxy_test_LDADD = \
|
|||
$(top_builddir)/src/gprs/gb_proxy.o \
|
||||
$(top_builddir)/src/gprs/gprs_llc_parse.o \
|
||||
$(top_builddir)/src/gprs/crc24.o \
|
||||
$(top_builddir)/src/gprs/gprs_utils.o \
|
||||
$(top_builddir)/src/libcommon/libcommon.a \
|
||||
$(top_builddir)/src/libbsc/libbsc.a \
|
||||
$(top_builddir)/src/libtrau/libtrau.a \
|
||||
|
|
|
@ -30,6 +30,7 @@
|
|||
#include <osmocom/gprs/gprs_bssgp.h>
|
||||
|
||||
#include <openbsc/gb_proxy.h>
|
||||
#include <openbsc/gprs_utils.h>
|
||||
#include <openbsc/debug.h>
|
||||
|
||||
#define REMOTE_BSS_ADDR 0x01020304
|
||||
|
@ -944,7 +945,7 @@ static void test_gbproxy_ra_patching()
|
|||
gbcfg.core_mcc = 123;
|
||||
gbcfg.core_mnc = 456;
|
||||
gbcfg.core_apn = talloc_zero_size(NULL, 100);
|
||||
gbcfg.core_apn_size = gbprox_str_to_apn(gbcfg.core_apn, "foo.bar", 100);
|
||||
gbcfg.core_apn_size = gprs_str_to_apn(gbcfg.core_apn, "foo.bar", 100);
|
||||
|
||||
configure_sgsn_peer(&sgsn_peer);
|
||||
configure_bss_peers(bss_peer, ARRAY_SIZE(bss_peer));
|
||||
|
|
Loading…
Reference in New Issue