blob: e6632dd330a815f30539ab35fdd53516237fa62d [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
35char *osmo_apn_qualify(unsigned int mcc, unsigned int mnc, const char *ni)
36{
37 snprintf(apn_strbuf, sizeof(apn_strbuf)-1, APN_GPRS_FMT,
38 ni, mnc, mcc);
39 apn_strbuf[sizeof(apn_strbuf)-1] = '\0';
40
41 return apn_strbuf;
42}
43
44char *osmo_apn_qualify_from_imsi(const char *imsi,
45 const char *ni, int have_3dig_mnc)
46{
47 char cbuf[3+1], nbuf[3+1];
48
49 strncpy(cbuf, imsi, 3);
50 cbuf[3] = '\0';
51
52 if (have_3dig_mnc) {
53 strncpy(nbuf, imsi+3, 3);
54 nbuf[3] = '\0';
55 } else {
56 strncpy(nbuf, imsi+3, 2);
57 nbuf[2] = '\0';
58 }
59 return osmo_apn_qualify(atoi(cbuf), atoi(nbuf), ni);
60}
Jacob Erlbeck81142942015-11-17 08:42:05 +010061
62/**
63 * Convert an encoded APN into a dot-separated string.
64 *
65 * \param out_str the destination buffer (size must be >= max(app_enc_len,1))
66 * \param apn_enc the encoded APN
67 * \param apn_enc_len the length of the encoded APN
68 *
69 * \returns out_str on success and NULL otherwise
70 */
71char * osmo_apn_to_str(char *out_str, const uint8_t *apn_enc, size_t apn_enc_len)
72{
73 char *str = out_str;
74 size_t rest_chars = apn_enc_len;
75
Maxfcf81b52017-10-19 15:50:44 +020076 if (!apn_enc)
77 return NULL;
78
Jacob Erlbeck81142942015-11-17 08:42:05 +010079 while (rest_chars > 0 && apn_enc[0]) {
80 size_t label_size = apn_enc[0];
81 if (label_size + 1 > rest_chars)
82 return NULL;
83
84 memmove(str, apn_enc + 1, label_size);
85 str += label_size;
86 rest_chars -= label_size + 1;
87 apn_enc += label_size + 1;
88
89 if (rest_chars)
90 *(str++) = '.';
91 }
92 str[0] = '\0';
93
94 return out_str;
95}
96
97/**
98 * Convert a dot-separated string into an encoded APN.
99 *
100 * \param apn_enc the encoded APN
101 * \param max_apn_enc_len the size of the apn_enc buffer
102 * \param str the source string
103 *
104 * \returns out_str on success and NULL otherwise
105 */
106int osmo_apn_from_str(uint8_t *apn_enc, size_t max_apn_enc_len, const char *str)
107{
108 uint8_t *last_len_field;
109 int len;
110
111 /* Can we even write the length field to the output? */
112 if (max_apn_enc_len == 0)
113 return -1;
114
115 /* Remember where we need to put the length once we know it */
116 last_len_field = apn_enc;
117 len = 1;
118 apn_enc += 1;
119
120 while (str[0]) {
121 if (len >= max_apn_enc_len)
122 return -1;
123
124 if (str[0] == '.') {
125 *last_len_field = (apn_enc - last_len_field) - 1;
126 last_len_field = apn_enc;
127 } else {
128 *apn_enc = str[0];
129 }
130 apn_enc += 1;
131 str += 1;
132 len += 1;
133 }
134
135 *last_len_field = (apn_enc - last_len_field) - 1;
136
137 return len;
138}