blob: 8b82f250602af3cc2ae53b1726d9f1ba80e1cb62 [file] [log] [blame]
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +02001/* GPRS utility functions */
2
3/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2010-2014 by On-Waves
5 * (C) 2013 by Holger Hans Peter Freyther
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22#include <openbsc/gprs_utils.h>
23
24#include <osmocom/core/msgb.h>
25#include <osmocom/gprs/gprs_ns.h>
26
27#include <string.h>
28
29/* FIXME: this needs to go to libosmocore/msgb.c */
30struct msgb *gprs_msgb_copy(const struct msgb *msg, const char *name)
31{
32 struct libgb_msgb_cb *old_cb, *new_cb;
33 struct msgb *new_msg;
34
35 new_msg = msgb_alloc(msg->data_len, name);
36 if (!new_msg)
37 return NULL;
38
39 /* copy data */
40 memcpy(new_msg->_data, msg->_data, new_msg->data_len);
41
42 /* copy header */
43 new_msg->len = msg->len;
44 new_msg->data += msg->data - msg->_data;
45 new_msg->head += msg->head - msg->_data;
46 new_msg->tail += msg->tail - msg->_data;
47
48 new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
49 new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data);
50 new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data);
51 new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data);
52
53 /* copy GB specific data */
54 old_cb = LIBGB_MSGB_CB(msg);
55 new_cb = LIBGB_MSGB_CB(new_msg);
56
57 new_cb->bssgph = new_msg->_data + (old_cb->bssgph - msg->_data);
58 new_cb->llch = new_msg->_data + (old_cb->llch - msg->_data);
59
60 /* bssgp_cell_id is a pointer into the old msgb, so we need to make
61 * it a pointer into the new msgb */
62 new_cb->bssgp_cell_id = new_msg->_data + (old_cb->bssgp_cell_id - msg->_data);
63 new_cb->nsei = old_cb->nsei;
64 new_cb->bvci = old_cb->bvci;
65 new_cb->tlli = old_cb->tlli;
66
67 return new_msg;
68}
69
70/* TODO: Move this to libosmocore/msgb.c */
71int gprs_msgb_resize_area(struct msgb *msg, uint8_t *area,
72 size_t old_size, size_t new_size)
73{
74 int rc;
75 uint8_t *rest = area + old_size;
76 int rest_len = msg->len - old_size - (area - msg->data);
77 int delta_size = (int)new_size - (int)old_size;
78
79 if (delta_size == 0)
80 return 0;
81
82 if (delta_size > 0) {
83 rc = msgb_trim(msg, msg->len + delta_size);
84 if (rc < 0)
85 return rc;
86 }
87
88 memmove(area + new_size, area + old_size, rest_len);
89
90 if (msg->l1h >= rest)
91 msg->l1h += delta_size;
92 if (msg->l2h >= rest)
93 msg->l2h += delta_size;
94 if (msg->l3h >= rest)
95 msg->l3h += delta_size;
96 if (msg->l4h >= rest)
97 msg->l4h += delta_size;
98
99 if (delta_size < 0)
100 msgb_trim(msg, msg->len + delta_size);
101
102 return 0;
103}
104
105/* TODO: Move these conversion functions to a utils file. */
106char * gprs_apn_to_str(char *out_str, const uint8_t *apn_enc, size_t rest_chars)
107{
108 char *str = out_str;
109
110 while (rest_chars > 0 && apn_enc[0]) {
111 size_t label_size = apn_enc[0];
112 if (label_size + 1 > rest_chars)
113 return NULL;
114
115 memmove(str, apn_enc + 1, label_size);
116 str += label_size;
117 rest_chars -= label_size + 1;
118 apn_enc += label_size + 1;
119
120 if (rest_chars)
121 *(str++) = '.';
122 }
123 str[0] = '\0';
124
125 return out_str;
126}
127
128int gprs_str_to_apn(uint8_t *apn_enc, const char *str, size_t max_chars)
129{
130 uint8_t *last_len_field = apn_enc;
131 int len = 1;
132 apn_enc += 1;
133
134 while (str[0]) {
135 if (str[0] == '.') {
136 *last_len_field = (apn_enc - last_len_field) - 1;
137 last_len_field = apn_enc;
138 } else {
139 *apn_enc = str[0];
140 }
141 apn_enc += 1;
142 str += 1;
143 len += 1;
144 if (len > max_chars)
145 return -1;
146 }
147
148 *last_len_field = (apn_enc - last_len_field) - 1;
149
150 return len;
151}
152