Holger Hans Peter Freyther | 8ac3a72 | 2014-08-04 11:52:52 +0200 | [diff] [blame] | 1 | /* 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 */ |
| 30 | struct 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 */ |
| 71 | int 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. */ |
Holger Hans Peter Freyther | 2d10ad1 | 2014-08-04 14:22:13 +0200 | [diff] [blame] | 106 | /** |
| 107 | * out_str needs to have rest_chars amount of bytes or 1 whatever is bigger. |
| 108 | */ |
Holger Hans Peter Freyther | 8ac3a72 | 2014-08-04 11:52:52 +0200 | [diff] [blame] | 109 | char * gprs_apn_to_str(char *out_str, const uint8_t *apn_enc, size_t rest_chars) |
| 110 | { |
| 111 | char *str = out_str; |
| 112 | |
| 113 | while (rest_chars > 0 && apn_enc[0]) { |
| 114 | size_t label_size = apn_enc[0]; |
| 115 | if (label_size + 1 > rest_chars) |
| 116 | return NULL; |
| 117 | |
| 118 | memmove(str, apn_enc + 1, label_size); |
| 119 | str += label_size; |
| 120 | rest_chars -= label_size + 1; |
| 121 | apn_enc += label_size + 1; |
| 122 | |
| 123 | if (rest_chars) |
| 124 | *(str++) = '.'; |
| 125 | } |
| 126 | str[0] = '\0'; |
| 127 | |
| 128 | return out_str; |
| 129 | } |
| 130 | |
Holger Hans Peter Freyther | 2d10ad1 | 2014-08-04 14:22:13 +0200 | [diff] [blame] | 131 | int gprs_str_to_apn(uint8_t *apn_enc, size_t max_len, const char *str) |
Holger Hans Peter Freyther | 8ac3a72 | 2014-08-04 11:52:52 +0200 | [diff] [blame] | 132 | { |
Holger Hans Peter Freyther | 2d10ad1 | 2014-08-04 14:22:13 +0200 | [diff] [blame] | 133 | uint8_t *last_len_field; |
| 134 | int len; |
| 135 | |
| 136 | /* Can we even write the length field to the output? */ |
| 137 | if (max_len == 0) |
| 138 | return -1; |
| 139 | |
| 140 | /* Remember where we need to put the length once we know it */ |
| 141 | last_len_field = apn_enc; |
| 142 | len = 1; |
Holger Hans Peter Freyther | 8ac3a72 | 2014-08-04 11:52:52 +0200 | [diff] [blame] | 143 | apn_enc += 1; |
| 144 | |
| 145 | while (str[0]) { |
Holger Hans Peter Freyther | 2d10ad1 | 2014-08-04 14:22:13 +0200 | [diff] [blame] | 146 | if (len >= max_len) |
| 147 | return -1; |
| 148 | |
Holger Hans Peter Freyther | 8ac3a72 | 2014-08-04 11:52:52 +0200 | [diff] [blame] | 149 | if (str[0] == '.') { |
| 150 | *last_len_field = (apn_enc - last_len_field) - 1; |
| 151 | last_len_field = apn_enc; |
| 152 | } else { |
| 153 | *apn_enc = str[0]; |
| 154 | } |
| 155 | apn_enc += 1; |
| 156 | str += 1; |
| 157 | len += 1; |
Holger Hans Peter Freyther | 8ac3a72 | 2014-08-04 11:52:52 +0200 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | *last_len_field = (apn_enc - last_len_field) - 1; |
| 161 | |
| 162 | return len; |
| 163 | } |
| 164 | |