blob: 4800702cfb69ce9d33c1dc660c6f09850954036d [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
Vadim Yanitskiybe5e8382022-04-23 02:16:07 +030028#include <osmocom/core/talloc.h>
Harald Welte908085c2014-10-01 16:18:11 +080029#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 Welte171ef822019-03-28 10:49:05 +010034static __thread char apn_strbuf[APN_MAXLEN+1];
Harald Welte908085c2014-10-01 16:18:11 +080035
Harald Welte4a62eda2019-03-18 18:27:00 +010036char *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 +080037{
Harald Welte4a62eda2019-03-18 18:27:00 +010038 snprintf(buf, buf_len-1, APN_GPRS_FMT, ni, mnc, mcc);
39 buf[buf_len-1] = '\0';
Harald Welte908085c2014-10-01 16:18:11 +080040
Harald Welte4a62eda2019-03-18 18:27:00 +010041 return buf;
Harald Welte908085c2014-10-01 16:18:11 +080042}
43
Harald Welte4a62eda2019-03-18 18:27:00 +010044char *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 Welte179f3572019-03-18 18:38:47 +010049char *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 Welte4a62eda2019-03-18 18:27:00 +010056
57char *osmo_apn_qualify_from_imsi_buf(char *buf, size_t buf_len, const char *imsi,
58 const char *ni, int have_3dig_mnc)
Harald Welte908085c2014-10-01 16:18:11 +080059{
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 Welte4a62eda2019-03-18 18:27:00 +010072 return osmo_apn_qualify_buf(buf, buf_len, atoi(cbuf), atoi(nbuf), ni);
73}
74
75char *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 Welte908085c2014-10-01 16:18:11 +080079}
Jacob Erlbeck81142942015-11-17 08:42:05 +010080
Harald Welte179f3572019-03-18 18:38:47 +010081char *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 Erlbeck81142942015-11-17 08:42:05 +010089/**
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 Maier8a942d22018-12-07 11:00:59 +010098char *osmo_apn_to_str(char *out_str, const uint8_t *apn_enc, size_t apn_enc_len)
Jacob Erlbeck81142942015-11-17 08:42:05 +010099{
100 char *str = out_str;
101 size_t rest_chars = apn_enc_len;
102
Maxfcf81b52017-10-19 15:50:44 +0200103 if (!apn_enc)
104 return NULL;
105
Jacob Erlbeck81142942015-11-17 08:42:05 +0100106 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 */
133int 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}