blob: 4ab370c5ea52cae33b919d8fe3509153bf3efaf7 [file] [log] [blame]
Harald Weltee08da972017-11-13 01:00:26 +09001/*! \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 Hofmeyr17518fe2017-06-20 04:35:06 +020022
Harald Welte908085c2014-10-01 16:18:11 +080023#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
33static char apn_strbuf[APN_MAXLEN+1];
34
Harald Welte4a62eda2019-03-18 18:27:00 +010035char *osmo_apn_qualify_buf(char *buf, size_t buf_len, unsigned int mcc, unsigned int mnc, const char *ni)
Harald Welte908085c2014-10-01 16:18:11 +080036{
Harald Welte4a62eda2019-03-18 18:27:00 +010037 snprintf(buf, buf_len-1, APN_GPRS_FMT, ni, mnc, mcc);
38 buf[buf_len-1] = '\0';
Harald Welte908085c2014-10-01 16:18:11 +080039
Harald Welte4a62eda2019-03-18 18:27:00 +010040 return buf;
Harald Welte908085c2014-10-01 16:18:11 +080041}
42
Harald Welte4a62eda2019-03-18 18:27:00 +010043char *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
49char *osmo_apn_qualify_from_imsi_buf(char *buf, size_t buf_len, const char *imsi,
50 const char *ni, int have_3dig_mnc)
Harald Welte908085c2014-10-01 16:18:11 +080051{
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 Welte4a62eda2019-03-18 18:27:00 +010064 return osmo_apn_qualify_buf(buf, buf_len, atoi(cbuf), atoi(nbuf), ni);
65}
66
67char *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 Welte908085c2014-10-01 16:18:11 +080071}
Jacob Erlbeck81142942015-11-17 08:42:05 +010072
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 Maier8a942d22018-12-07 11:00:59 +010082char *osmo_apn_to_str(char *out_str, const uint8_t *apn_enc, size_t apn_enc_len)
Jacob Erlbeck81142942015-11-17 08:42:05 +010083{
84 char *str = out_str;
85 size_t rest_chars = apn_enc_len;
86
Maxfcf81b52017-10-19 15:50:44 +020087 if (!apn_enc)
88 return NULL;
89
Jacob Erlbeck81142942015-11-17 08:42:05 +010090 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 */
117int 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}