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 | |
| 28 | #include <osmocom/gsm/apn.h> |
| 29 | |
| 30 | #define APN_OI_GPRS_FMT "mnc%03u.mcc%03u.gprs" |
| 31 | #define APN_GPRS_FMT "%s.mnc%03u.mcc%03u.gprs" |
| 32 | |
| 33 | static char apn_strbuf[APN_MAXLEN+1]; |
| 34 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame^] | 35 | 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] | 36 | { |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame^] | 37 | snprintf(buf, buf_len-1, APN_GPRS_FMT, ni, mnc, mcc); |
| 38 | buf[buf_len-1] = '\0'; |
Harald Welte | 908085c | 2014-10-01 16:18:11 +0800 | [diff] [blame] | 39 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame^] | 40 | return buf; |
Harald Welte | 908085c | 2014-10-01 16:18:11 +0800 | [diff] [blame] | 41 | } |
| 42 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame^] | 43 | char *osmo_apn_qualify(unsigned int mcc, unsigned int mnc, const char *ni) |
| 44 | { |
| 45 | return osmo_apn_qualify_buf(apn_strbuf, sizeof(apn_strbuf), mcc, mnc, ni); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | char *osmo_apn_qualify_from_imsi_buf(char *buf, size_t buf_len, const char *imsi, |
| 50 | const char *ni, int have_3dig_mnc) |
Harald Welte | 908085c | 2014-10-01 16:18:11 +0800 | [diff] [blame] | 51 | { |
| 52 | char cbuf[3+1], nbuf[3+1]; |
| 53 | |
| 54 | strncpy(cbuf, imsi, 3); |
| 55 | cbuf[3] = '\0'; |
| 56 | |
| 57 | if (have_3dig_mnc) { |
| 58 | strncpy(nbuf, imsi+3, 3); |
| 59 | nbuf[3] = '\0'; |
| 60 | } else { |
| 61 | strncpy(nbuf, imsi+3, 2); |
| 62 | nbuf[2] = '\0'; |
| 63 | } |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame^] | 64 | return osmo_apn_qualify_buf(buf, buf_len, atoi(cbuf), atoi(nbuf), ni); |
| 65 | } |
| 66 | |
| 67 | char *osmo_apn_qualify_from_imsi(const char *imsi, |
| 68 | const char *ni, int have_3dig_mnc) |
| 69 | { |
| 70 | 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] | 71 | } |
Jacob Erlbeck | 8114294 | 2015-11-17 08:42:05 +0100 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Convert an encoded APN into a dot-separated string. |
| 75 | * |
| 76 | * \param out_str the destination buffer (size must be >= max(app_enc_len,1)) |
| 77 | * \param apn_enc the encoded APN |
| 78 | * \param apn_enc_len the length of the encoded APN |
| 79 | * |
| 80 | * \returns out_str on success and NULL otherwise |
| 81 | */ |
Philipp Maier | 8a942d2 | 2018-12-07 11:00:59 +0100 | [diff] [blame] | 82 | 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] | 83 | { |
| 84 | char *str = out_str; |
| 85 | size_t rest_chars = apn_enc_len; |
| 86 | |
Max | fcf81b5 | 2017-10-19 15:50:44 +0200 | [diff] [blame] | 87 | if (!apn_enc) |
| 88 | return NULL; |
| 89 | |
Jacob Erlbeck | 8114294 | 2015-11-17 08:42:05 +0100 | [diff] [blame] | 90 | while (rest_chars > 0 && apn_enc[0]) { |
| 91 | size_t label_size = apn_enc[0]; |
| 92 | if (label_size + 1 > rest_chars) |
| 93 | return NULL; |
| 94 | |
| 95 | memmove(str, apn_enc + 1, label_size); |
| 96 | str += label_size; |
| 97 | rest_chars -= label_size + 1; |
| 98 | apn_enc += label_size + 1; |
| 99 | |
| 100 | if (rest_chars) |
| 101 | *(str++) = '.'; |
| 102 | } |
| 103 | str[0] = '\0'; |
| 104 | |
| 105 | return out_str; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Convert a dot-separated string into an encoded APN. |
| 110 | * |
| 111 | * \param apn_enc the encoded APN |
| 112 | * \param max_apn_enc_len the size of the apn_enc buffer |
| 113 | * \param str the source string |
| 114 | * |
| 115 | * \returns out_str on success and NULL otherwise |
| 116 | */ |
| 117 | int osmo_apn_from_str(uint8_t *apn_enc, size_t max_apn_enc_len, const char *str) |
| 118 | { |
| 119 | uint8_t *last_len_field; |
| 120 | int len; |
| 121 | |
| 122 | /* Can we even write the length field to the output? */ |
| 123 | if (max_apn_enc_len == 0) |
| 124 | return -1; |
| 125 | |
| 126 | /* Remember where we need to put the length once we know it */ |
| 127 | last_len_field = apn_enc; |
| 128 | len = 1; |
| 129 | apn_enc += 1; |
| 130 | |
| 131 | while (str[0]) { |
| 132 | if (len >= max_apn_enc_len) |
| 133 | return -1; |
| 134 | |
| 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 | } |
| 145 | |
| 146 | *last_len_field = (apn_enc - last_len_field) - 1; |
| 147 | |
| 148 | return len; |
| 149 | } |