Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 1 | /*! \file apn.c |
| 2 | * |
| 3 | * (C) 2014 by Harald Welte <laforge@gnumonks.org> |
| 4 | * (C) 2015,2017 by sysmocom - s.f.m.c. GmbH |
| 5 | * All Rights Reserved. |
| 6 | * |
| 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | */ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 22 | |
Harald Welte | 908085c | 2014-10-01 16:18:11 +0800 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdint.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | |
Vadim Yanitskiy | be5e838 | 2022-04-23 02:16:07 +0300 | [diff] [blame] | 28 | #include <osmocom/core/talloc.h> |
Harald Welte | 908085c | 2014-10-01 16:18:11 +0800 | [diff] [blame] | 29 | #include <osmocom/gsm/apn.h> |
| 30 | |
| 31 | #define APN_OI_GPRS_FMT "mnc%03u.mcc%03u.gprs" |
| 32 | #define APN_GPRS_FMT "%s.mnc%03u.mcc%03u.gprs" |
| 33 | |
Harald Welte | 171ef82 | 2019-03-28 10:49:05 +0100 | [diff] [blame] | 34 | static __thread char apn_strbuf[APN_MAXLEN+1]; |
Harald Welte | 908085c | 2014-10-01 16:18:11 +0800 | [diff] [blame] | 35 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 36 | char *osmo_apn_qualify_buf(char *buf, size_t buf_len, unsigned int mcc, unsigned int mnc, const char *ni) |
Harald Welte | 908085c | 2014-10-01 16:18:11 +0800 | [diff] [blame] | 37 | { |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 38 | snprintf(buf, buf_len-1, APN_GPRS_FMT, ni, mnc, mcc); |
| 39 | buf[buf_len-1] = '\0'; |
Harald Welte | 908085c | 2014-10-01 16:18:11 +0800 | [diff] [blame] | 40 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 41 | return buf; |
Harald Welte | 908085c | 2014-10-01 16:18:11 +0800 | [diff] [blame] | 42 | } |
| 43 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 44 | char *osmo_apn_qualify(unsigned int mcc, unsigned int mnc, const char *ni) |
| 45 | { |
| 46 | return osmo_apn_qualify_buf(apn_strbuf, sizeof(apn_strbuf), mcc, mnc, ni); |
| 47 | } |
| 48 | |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 49 | char *osmo_apn_qualify_c(const void *ctx, unsigned int mcc, unsigned int mnc, const char *ni) |
| 50 | { |
| 51 | char *buf = talloc_size(ctx, APN_MAXLEN); |
| 52 | if (!buf) |
| 53 | return NULL; |
| 54 | return osmo_apn_qualify_buf(buf, APN_MAXLEN, mcc, mnc, ni); |
| 55 | } |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 56 | |
| 57 | char *osmo_apn_qualify_from_imsi_buf(char *buf, size_t buf_len, const char *imsi, |
| 58 | const char *ni, int have_3dig_mnc) |
Harald Welte | 908085c | 2014-10-01 16:18:11 +0800 | [diff] [blame] | 59 | { |
| 60 | char cbuf[3+1], nbuf[3+1]; |
| 61 | |
| 62 | strncpy(cbuf, imsi, 3); |
| 63 | cbuf[3] = '\0'; |
| 64 | |
| 65 | if (have_3dig_mnc) { |
| 66 | strncpy(nbuf, imsi+3, 3); |
| 67 | nbuf[3] = '\0'; |
| 68 | } else { |
| 69 | strncpy(nbuf, imsi+3, 2); |
| 70 | nbuf[2] = '\0'; |
| 71 | } |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 72 | return osmo_apn_qualify_buf(buf, buf_len, atoi(cbuf), atoi(nbuf), ni); |
| 73 | } |
| 74 | |
| 75 | char *osmo_apn_qualify_from_imsi(const char *imsi, |
| 76 | const char *ni, int have_3dig_mnc) |
| 77 | { |
| 78 | return osmo_apn_qualify_from_imsi_buf(apn_strbuf, sizeof(apn_strbuf), imsi, ni, have_3dig_mnc); |
Harald Welte | 908085c | 2014-10-01 16:18:11 +0800 | [diff] [blame] | 79 | } |
Jacob Erlbeck | 8114294 | 2015-11-17 08:42:05 +0100 | [diff] [blame] | 80 | |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 81 | char *osmo_apn_qualify_from_imsi_c(const void *ctx, const char *imsi, const char *ni, int have_3dig_mnc) |
| 82 | { |
| 83 | char *buf = talloc_size(ctx, APN_MAXLEN); |
| 84 | if (!buf) |
| 85 | return NULL; |
| 86 | return osmo_apn_qualify_from_imsi_buf(buf, APN_MAXLEN, imsi, ni, have_3dig_mnc); |
| 87 | } |
| 88 | |
Jacob Erlbeck | 8114294 | 2015-11-17 08:42:05 +0100 | [diff] [blame] | 89 | /** |
| 90 | * Convert an encoded APN into a dot-separated string. |
| 91 | * |
| 92 | * \param out_str the destination buffer (size must be >= max(app_enc_len,1)) |
| 93 | * \param apn_enc the encoded APN |
| 94 | * \param apn_enc_len the length of the encoded APN |
| 95 | * |
| 96 | * \returns out_str on success and NULL otherwise |
| 97 | */ |
Philipp Maier | 8a942d2 | 2018-12-07 11:00:59 +0100 | [diff] [blame] | 98 | char *osmo_apn_to_str(char *out_str, const uint8_t *apn_enc, size_t apn_enc_len) |
Jacob Erlbeck | 8114294 | 2015-11-17 08:42:05 +0100 | [diff] [blame] | 99 | { |
| 100 | char *str = out_str; |
| 101 | size_t rest_chars = apn_enc_len; |
| 102 | |
Max | fcf81b5 | 2017-10-19 15:50:44 +0200 | [diff] [blame] | 103 | if (!apn_enc) |
| 104 | return NULL; |
| 105 | |
Jacob Erlbeck | 8114294 | 2015-11-17 08:42:05 +0100 | [diff] [blame] | 106 | while (rest_chars > 0 && apn_enc[0]) { |
| 107 | size_t label_size = apn_enc[0]; |
| 108 | if (label_size + 1 > rest_chars) |
| 109 | return NULL; |
| 110 | |
| 111 | memmove(str, apn_enc + 1, label_size); |
| 112 | str += label_size; |
| 113 | rest_chars -= label_size + 1; |
| 114 | apn_enc += label_size + 1; |
| 115 | |
| 116 | if (rest_chars) |
| 117 | *(str++) = '.'; |
| 118 | } |
| 119 | str[0] = '\0'; |
| 120 | |
| 121 | return out_str; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Convert a dot-separated string into an encoded APN. |
| 126 | * |
| 127 | * \param apn_enc the encoded APN |
| 128 | * \param max_apn_enc_len the size of the apn_enc buffer |
| 129 | * \param str the source string |
| 130 | * |
| 131 | * \returns out_str on success and NULL otherwise |
| 132 | */ |
| 133 | int osmo_apn_from_str(uint8_t *apn_enc, size_t max_apn_enc_len, const char *str) |
| 134 | { |
| 135 | uint8_t *last_len_field; |
| 136 | int len; |
| 137 | |
| 138 | /* Can we even write the length field to the output? */ |
| 139 | if (max_apn_enc_len == 0) |
| 140 | return -1; |
| 141 | |
| 142 | /* Remember where we need to put the length once we know it */ |
| 143 | last_len_field = apn_enc; |
| 144 | len = 1; |
| 145 | apn_enc += 1; |
| 146 | |
| 147 | while (str[0]) { |
| 148 | if (len >= max_apn_enc_len) |
| 149 | return -1; |
| 150 | |
| 151 | if (str[0] == '.') { |
| 152 | *last_len_field = (apn_enc - last_len_field) - 1; |
| 153 | last_len_field = apn_enc; |
| 154 | } else { |
| 155 | *apn_enc = str[0]; |
| 156 | } |
| 157 | apn_enc += 1; |
| 158 | str += 1; |
| 159 | len += 1; |
| 160 | } |
| 161 | |
| 162 | *last_len_field = (apn_enc - last_len_field) - 1; |
| 163 | |
| 164 | return len; |
| 165 | } |