blob: bbfe236acc69a42c0c53603ba9c20955832800e6 [file] [log] [blame]
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +02001/*! \file gsm23003.c
2 * Utility function implementations related to 3GPP TS 23.003 */
3/*
Harald Weltee08da972017-11-13 01:00:26 +09004 * (C) 2017 sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
5 * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de>
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +02006 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +02009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 */
25
26#include <ctype.h>
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010027#include <stdio.h>
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +010028#include <stdlib.h>
29#include <errno.h>
30#include <string.h>
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +020031
32#include <osmocom/gsm/gsm23003.h>
33#include <osmocom/gsm/protocol/gsm_23_003.h>
Oliver Smith894be2d2019-01-11 13:13:37 +010034#include <osmocom/core/utils.h>
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +020035
36static bool is_n_digits(const char *str, int min_digits, int max_digits)
37{
38 int len;
39 /* Use unsigned char * to avoid a compiler warning of
40 * "error: array subscript has type 'char' [-Werror=char-subscripts]" */
41 const unsigned char *pos = (const unsigned char *)str;
Neels Hofmeyr4b7c7912017-10-07 04:45:01 +020042 if (!pos)
43 return min_digits < 1;
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +020044 for (len = 0; *pos && len < max_digits; len++, pos++)
45 if (!isdigit(*pos))
46 return false;
47 if (len < min_digits)
48 return false;
49 /* With not too many digits, we should have reached *str == nul */
50 if (*pos)
51 return false;
52 return true;
53}
54
55/*! Determine whether the given IMSI is valid according to 3GPP TS 23.003.
56 * \param imsi IMSI digits in ASCII string representation.
57 * \returns true when the IMSI is valid, false for invalid characters or number
58 * of digits.
59 */
60bool osmo_imsi_str_valid(const char *imsi)
61{
62 return is_n_digits(imsi, GSM23003_IMSI_MIN_DIGITS, GSM23003_IMSI_MAX_DIGITS);
63}
64
65/*! Determine whether the given MSISDN is valid according to 3GPP TS 23.003.
66 * \param msisdn MSISDN digits in ASCII string representation.
67 * \returns true when the MSISDN is valid, false for invalid characters or number
68 * of digits.
69 */
70bool osmo_msisdn_str_valid(const char *msisdn)
71{
72 return is_n_digits(msisdn, 1, 15);
73}
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010074
Oliver Smith894be2d2019-01-11 13:13:37 +010075/*! Determine whether the given IMEI is valid according to 3GPP TS 23.003,
76 * Section 6.2.1. It consists of 14 digits, the 15th check digit is not
77 * intended for digital transmission.
78 * \param imei IMEI digits in ASCII string representation.
79 * \param with_15th_digit when true, expect the 15th digit to be present and
80 * verify it.
81 * \returns true when the IMEI is valid, false for invalid characters or number
82 * of digits.
83 */
84bool osmo_imei_str_valid(const char *imei, bool with_15th_digit)
85{
86 if (with_15th_digit)
87 return is_n_digits(imei, 15, 15) && osmo_luhn(imei, 14) == imei[14];
88 else
89 return is_n_digits(imei, 14, 14);
90}
91
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010092/*! Return MCC string as standardized 3-digit with leading zeros.
Harald Welte4a62eda2019-03-18 18:27:00 +010093 * \param[out] buf caller-allocated output buffer
94 * \param[in] buf_len size of buf in bytes
95 * \param[in] mcc MCC value.
96 * \returns string in user-supplied output buffer
97 */
98char *osmo_mcc_name_buf(char *buf, size_t buf_len, uint16_t mcc)
99{
100 snprintf(buf, buf_len, "%03u", mcc);
101 return buf;
102}
103
104/*! Return MCC string as standardized 3-digit with leading zeros.
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100105 * \param[in] mcc MCC value.
106 * \returns string in static buffer.
107 */
108const char *osmo_mcc_name(uint16_t mcc)
109{
110 static char buf[8];
Harald Welte4a62eda2019-03-18 18:27:00 +0100111 return osmo_mcc_name_buf(buf, sizeof(buf), mcc);
112}
113
114/*! Return MNC string as standardized 2- or 3-digit with leading zeros.
115 * \param[out] buf caller-allocated output buffer
116 * \param[in] buf_len size of buf in bytes
117 * \param[in] mnc MNC value.
118 * \param[in] mnc_3_digits True if an MNC should fill three digits, only has an effect if MNC < 100.
119 * \returns string in static buffer.
120 */
121char *osmo_mnc_name_buf(char *buf, size_t buf_len, uint16_t mnc, bool mnc_3_digits)
122{
123 snprintf(buf, buf_len, "%0*u", mnc_3_digits ? 3 : 2, mnc);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100124 return buf;
125}
126
127/*! Return MNC string as standardized 2- or 3-digit with leading zeros.
128 * \param[in] mnc MNC value.
129 * \param[in] mnc_3_digits True if an MNC should fill three digits, only has an effect if MNC < 100.
130 * \returns string in static buffer.
131 */
132const char *osmo_mnc_name(uint16_t mnc, bool mnc_3_digits)
133{
134 static char buf[8];
Harald Welte4a62eda2019-03-18 18:27:00 +0100135 return osmo_mnc_name_buf(buf, sizeof(buf), mnc, mnc_3_digits);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100136}
137
Harald Welte4a62eda2019-03-18 18:27:00 +0100138/*! Return MCC-MNC string as standardized 3-digit-dash-2/3-digit with leading zeros.
139 * \param[out] buf caller-allocated output buffer
140 * \param[in] buf_len size of buf in bytes
141 * \param[in] plmn MCC-MNC value.
142 * \returns string in static buffer.
143 */
144char *osmo_plmn_name_buf(char *buf, size_t buf_len, const struct osmo_plmn_id *plmn)
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100145{
Harald Welte4a62eda2019-03-18 18:27:00 +0100146 char mcc[8], mnc[8];
147 snprintf(buf, buf_len, "%s-%s", osmo_mcc_name_buf(mcc, sizeof(mcc), plmn->mcc),
148 osmo_mnc_name_buf(mnc, sizeof(mnc), plmn->mnc, plmn->mnc_3_digits));
149 return buf;
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100150}
151
152/*! Return MCC-MNC string as standardized 3-digit-dash-2/3-digit with leading zeros.
153 * \param[in] plmn MCC-MNC value.
154 * \returns string in static buffer.
155 */
156const char *osmo_plmn_name(const struct osmo_plmn_id *plmn)
157{
158 static char buf[16];
Harald Welte4a62eda2019-03-18 18:27:00 +0100159 return osmo_plmn_name_buf(buf, sizeof(buf), plmn);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100160}
161
Harald Welte4a62eda2019-03-18 18:27:00 +0100162
Neels Hofmeyr6d57c572019-02-26 21:54:00 +0100163/*! Same as osmo_plmn_name(), but returning in a different static buffer.
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100164 * \param[in] plmn MCC-MNC value.
165 * \returns string in static buffer.
166 */
167const char *osmo_plmn_name2(const struct osmo_plmn_id *plmn)
168{
169 static char buf[16];
Harald Welte4a62eda2019-03-18 18:27:00 +0100170 return osmo_plmn_name_buf(buf, sizeof(buf), plmn);
171}
172
173/*! Return MCC-MNC-LAC as string, in caller-provided output buffer.
174 * \param[out] buf caller-allocated output buffer
175 * \param[in] buf_len size of buf in bytes
176 * \param[in] lai LAI to encode, the rac member is ignored.
177 * \returns buf
178 */
179char *osmo_lai_name_buf(char *buf, size_t buf_len, const struct osmo_location_area_id *lai)
180{
181 char plmn[16];
182 snprintf(buf, buf_len, "%s-%u", osmo_plmn_name_buf(plmn, sizeof(plmn), &lai->plmn), lai->lac);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100183 return buf;
184}
185
186/*! Return MCC-MNC-LAC as string, in a static buffer.
187 * \param[in] lai LAI to encode, the rac member is ignored.
188 * \returns Static string buffer.
189 */
190const char *osmo_lai_name(const struct osmo_location_area_id *lai)
191{
192 static char buf[32];
Harald Welte4a62eda2019-03-18 18:27:00 +0100193 return osmo_lai_name_buf(buf, sizeof(buf), lai);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100194}
195
Harald Welte4a62eda2019-03-18 18:27:00 +0100196/*! Return MCC-MNC-LAC-CI as string, in caller-provided output buffer.
197 * \param[out] buf caller-allocated output buffer
198 * \param[in] buf_len size of buf in bytes
199 * \param[in] cgi CGI to encode.
200 * \returns buf
201 */
202char *osmo_cgi_name_buf(char *buf, size_t buf_len, const struct osmo_cell_global_id *cgi)
Neels Hofmeyr43496202018-03-22 14:04:30 +0100203{
Harald Welte4a62eda2019-03-18 18:27:00 +0100204 snprintf(buf, buf_len, "%s-%u", osmo_lai_name(&cgi->lai), cgi->cell_identity);
Neels Hofmeyr43496202018-03-22 14:04:30 +0100205 return buf;
206}
207
208/*! Return MCC-MNC-LAC-CI as string, in a static buffer.
209 * \param[in] cgi CGI to encode.
210 * \returns Static string buffer.
211 */
212const char *osmo_cgi_name(const struct osmo_cell_global_id *cgi)
213{
214 static char buf[32];
Harald Welte4a62eda2019-03-18 18:27:00 +0100215 return osmo_cgi_name_buf(buf, sizeof(buf), cgi);
Neels Hofmeyr43496202018-03-22 14:04:30 +0100216}
217
218/*! Same as osmo_cgi_name(), but uses a different static buffer.
219 * Useful for printing two distinct CGIs in the same printf format.
220 * \param[in] cgi CGI to encode.
221 * \returns Static string buffer.
222 */
223const char *osmo_cgi_name2(const struct osmo_cell_global_id *cgi)
224{
225 static char buf[32];
Harald Welte4a62eda2019-03-18 18:27:00 +0100226 return osmo_cgi_name_buf(buf, sizeof(buf), cgi);
Neels Hofmeyr43496202018-03-22 14:04:30 +0100227}
228
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100229static void to_bcd(uint8_t *bcd, uint16_t val)
230{
231 bcd[2] = val % 10;
232 val = val / 10;
233 bcd[1] = val % 10;
234 val = val / 10;
235 bcd[0] = val % 10;
236}
237
Harald Welte4a62eda2019-03-18 18:27:00 +0100238/*! Return string representation of GUMMEI in caller-provided output buffer.
239 * \param[out] buf pointer to caller-provided output buffer
240 * \param[in] buf_len size of buf in bytes
241 * \param[in] gummei GUMMEI to be stringified
242 * \returns buf
243 */
244char *osmo_gummei_name_buf(char *buf, size_t buf_len, const struct osmo_gummei *gummei)
Harald Weltede1da352018-10-08 22:27:04 +0200245{
Harald Welte4a62eda2019-03-18 18:27:00 +0100246 char plmn[16];
247 snprintf(buf, buf_len, "%s-%04x-%02x", osmo_plmn_name_buf(plmn, sizeof(plmn), &gummei->plmn),
Harald Weltede1da352018-10-08 22:27:04 +0200248 gummei->mme.group_id, gummei->mme.code);
249 return buf;
250}
251
Harald Welte4a62eda2019-03-18 18:27:00 +0100252/*! Return string representation of GUMMEI in static output buffer.
253 * \param[in] gummei GUMMEI to be stringified
254 * \returns pointer to static output buffer
255 */
256const char *osmo_gummei_name(const struct osmo_gummei *gummei)
257{
258 static char buf[32];
259 return osmo_gummei_name_buf(buf, sizeof(buf), gummei);
260}
261
262
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100263/* Convert MCC + MNC to BCD representation
264 * \param[out] bcd_dst caller-allocated memory for output
265 * \param[in] mcc Mobile Country Code
266 * \param[in] mnc Mobile Network Code
267 * \param[in] mnc_3_digits true if the MNC shall have three digits.
268 *
269 * Convert given mcc and mnc to BCD and write to *bcd_dst, which must be an
270 * allocated buffer of (at least) 3 bytes length. Encode the MNC in three
271 * digits if its integer value is > 99, or if mnc_3_digits is passed true.
272 * Encode an MNC < 100 with mnc_3_digits passed as true as a three-digit MNC
273 * with leading zeros in the BCD representation.
274 */
275void osmo_plmn_to_bcd(uint8_t *bcd_dst, const struct osmo_plmn_id *plmn)
276{
277 uint8_t bcd[3];
278
279 to_bcd(bcd, plmn->mcc);
280 bcd_dst[0] = bcd[0] | (bcd[1] << 4);
281 bcd_dst[1] = bcd[2];
282
283 to_bcd(bcd, plmn->mnc);
284 if (plmn->mnc > 99 || plmn->mnc_3_digits) {
285 bcd_dst[1] |= bcd[2] << 4;
286 bcd_dst[2] = bcd[0] | (bcd[1] << 4);
287 } else {
288 bcd_dst[1] |= 0xf << 4;
289 bcd_dst[2] = bcd[1] | (bcd[2] << 4);
290 }
291}
292
293/* Convert given 3-byte BCD buffer to integers and write results to *mcc and
294 * *mnc. The first three BCD digits result in the MCC and the remaining ones in
295 * the MNC. Return mnc_3_digits as false if the MNC's most significant digit is encoded as 0xF, true
296 * otherwise; i.e. true if MNC > 99 or if it is represented with leading zeros instead of 0xF.
297 * \param[in] bcd_src 3-byte BCD buffer containing MCC+MNC representations.
298 * \param[out] mcc MCC result buffer, or NULL.
299 * \param[out] mnc MNC result buffer, or NULL.
300 * \param[out] mnc_3_digits Result buffer for 3-digit flag, or NULL.
301 */
302void osmo_plmn_from_bcd(const uint8_t *bcd_src, struct osmo_plmn_id *plmn)
303{
304 plmn->mcc = (bcd_src[0] & 0x0f) * 100
305 + (bcd_src[0] >> 4) * 10
306 + (bcd_src[1] & 0x0f);
307
308 if ((bcd_src[1] & 0xf0) == 0xf0) {
309 plmn->mnc = (bcd_src[2] & 0x0f) * 10
310 + (bcd_src[2] >> 4);
311 plmn->mnc_3_digits = false;
312 } else {
313 plmn->mnc = (bcd_src[2] & 0x0f) * 100
314 + (bcd_src[2] >> 4) * 10
315 + (bcd_src[1] >> 4);
316 plmn->mnc_3_digits = true;
317 }
318}
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100319
320/* Convert string to MNC, detecting 3-digit MNC with leading zeros.
321 * Return mnc_3_digits as false if the MNC's most significant digit is encoded as 0xF, true
322 * otherwise; i.e. true if MNC > 99 or if it is represented with leading zeros instead of 0xF.
323 * \param mnc_str[in] String representation of an MNC, with or without leading zeros.
324 * \param mnc[out] MNC result buffer, or NULL.
325 * \param[out] mnc_3_digits Result buffer for 3-digit flag, or NULL.
326 * \returns zero on success, -EINVAL in case of surplus characters, negative errno in case of conversion
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100327 * errors. In case of error, do not modify the out-arguments.
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100328 */
329int osmo_mnc_from_str(const char *mnc_str, uint16_t *mnc, bool *mnc_3_digits)
330{
331 long int _mnc = 0;
332 bool _mnc_3_digits = false;
333 char *endptr;
334 int rc = 0;
335
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100336 if (!mnc_str || !isdigit(mnc_str[0]) || strlen(mnc_str) > 3)
337 return -EINVAL;
338
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100339 errno = 0;
340 _mnc = strtol(mnc_str, &endptr, 10);
341 if (errno)
342 rc = -errno;
343 else if (*endptr)
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100344 return -EINVAL;
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100345 if (_mnc < 0 || _mnc > 999)
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100346 return -ERANGE;
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100347 _mnc_3_digits = strlen(mnc_str) > 2;
348
349 if (mnc)
350 *mnc = (uint16_t)_mnc;
351 if (mnc_3_digits)
352 *mnc_3_digits = _mnc_3_digits;
353 return rc;
354}
355
356/* Compare two MNC with three-digit flag.
357 * The mnc_3_digits flags passed in only have an effect if the MNC are < 100, i.e. if they would amount
358 * to a change in leading zeros in a BCD representation. An MNC >= 100 implies three digits, and the flag
359 * is actually ignored.
360 * \param a_mnc[in] "Left" side MNC.
361 * \param a_mnc_3_digits[in] "Left" side three-digits flag.
362 * \param b_mnc[in] "Right" side MNC.
363 * \param b_mnc_3_digits[in] "Right" side three-digits flag.
364 * \returns 0 if the MNC are equal, -1 if a < b or a shorter, 1 if a > b or a longer. */
365int osmo_mnc_cmp(uint16_t a_mnc, bool a_mnc_3_digits, uint16_t b_mnc, bool b_mnc_3_digits)
366{
367 if (a_mnc < b_mnc)
368 return -1;
369 if (a_mnc > b_mnc)
370 return 1;
371 /* a_mnc == b_mnc, but same amount of leading zeros? */
372 if (a_mnc < 100 && a_mnc_3_digits != b_mnc_3_digits)
373 return a_mnc_3_digits ? 1 : -1;
374 return 0;
375}
376
377/* Compare two PLMN.
378 * \param a[in] "Left" side PLMN.
379 * \param b[in] "Right" side PLMN.
380 * \returns 0 if the PLMN are equal, -1 if a < b or a shorter, 1 if a > b or a longer. */
381int osmo_plmn_cmp(const struct osmo_plmn_id *a, const struct osmo_plmn_id *b)
382{
383 if (a == b)
384 return 0;
385 if (a->mcc < b->mcc)
386 return -1;
387 if (a->mcc > b->mcc)
388 return 1;
389 return osmo_mnc_cmp(a->mnc, a->mnc_3_digits, b->mnc, b->mnc_3_digits);
390}
Harald Weltede1da352018-10-08 22:27:04 +0200391
Neels Hofmeyrd01ef752018-09-21 15:57:26 +0200392/* Compare two LAI.
393 * The order of comparison is MCC, MNC, LAC. See also osmo_plmn_cmp().
394 * \param a[in] "Left" side LAI.
395 * \param b[in] "Right" side LAI.
396 * \returns 0 if the LAI are equal, -1 if a < b, 1 if a > b. */
397int osmo_lai_cmp(const struct osmo_location_area_id *a, const struct osmo_location_area_id *b)
398{
399 int rc = osmo_plmn_cmp(&a->plmn, &b->plmn);
400 if (rc)
401 return rc;
402 if (a->lac < b->lac)
403 return -1;
404 if (a->lac > b->lac)
405 return 1;
406 return 0;
407}
408
409/* Compare two CGI.
410 * The order of comparison is MCC, MNC, LAC, CI. See also osmo_lai_cmp().
411 * \param a[in] "Left" side CGI.
412 * \param b[in] "Right" side CGI.
413 * \returns 0 if the CGI are equal, -1 if a < b, 1 if a > b. */
414int osmo_cgi_cmp(const struct osmo_cell_global_id *a, const struct osmo_cell_global_id *b)
415{
416 int rc = osmo_lai_cmp(&a->lai, &b->lai);
417 if (rc)
418 return rc;
419 if (a->cell_identity < b->cell_identity)
420 return -1;
421 if (a->cell_identity > b->cell_identity)
422 return 1;
423 return 0;
424}
425
Harald Weltede1da352018-10-08 22:27:04 +0200426/*! Generate TS 23.003 Section 19.2 Home Network Realm/Domain (text form)
427 * \param out[out] caller-provided output buffer, at least 33 bytes long
428 * \param plmn[in] Osmocom representation of PLMN ID (MCC + MNC)
429 * \returns number of characters printed (excluding NUL); negative on error */
430int osmo_gen_home_network_domain(char *out, const struct osmo_plmn_id *plmn)
431{
432 if (plmn->mcc > 999)
433 return -EINVAL;
434 if (plmn->mnc > 999)
435 return -EINVAL;
436 return sprintf(out, "epc.mnc%03u.mcc%03u.3gppnetwork.org", plmn->mnc, plmn->mcc);
437}
438
439/*! Parse a TS 23.003 Section 19.2 Home Network Realm/Domain (text form) into a \ref osmo_plmn_id
440 * \param out[out] caller-allocated output structure
441 * \param in[in] character string representation to be parsed
442 * \returns 0 on success; negative on error */
443int osmo_parse_home_network_domain(struct osmo_plmn_id *out, const char *in)
444{
445 int rc;
446
447 memset(out, 0, sizeof(*out));
448 rc = sscanf(in, "epc.mnc%03hu.mcc%03hu.3gppnetwork.org", &out->mnc, &out->mcc);
449 if (rc < 0)
450 return rc;
451 if (rc != 2)
452 return -EINVAL;
453 return 0;
454}
455
456/*! Generate TS 23.003 Section 19.4.2.4 MME Domain (text form)
457 * \param out[out] caller-provided output buffer, at least 56 bytes long
458 * \param gummei[in] Structure representing the Globally Unique MME Identifier
459 * \returns number of characters printed (excluding NUL); negative on error */
460int osmo_gen_mme_domain(char *out, const struct osmo_gummei *gummei)
461{
462 char domain[GSM23003_HOME_NETWORK_DOMAIN_LEN+1];
463 int rc;
464 rc = osmo_gen_home_network_domain(domain, &gummei->plmn);
465 if (rc < 0)
466 return rc;
467 return sprintf(out, "mmec%02x.mmegi%04x.mme.%s", gummei->mme.code, gummei->mme.group_id, domain);
468}
469
470/*! Parse a TS 23.003 Section 19.4.2.4 MME Domain (text form) into a \ref osmo_gummei
471 * \param out[out] caller-allocated output GUMMEI structure
472 * \param in[in] character string representation to be parsed
473 * \returns 0 on success; negative on error */
474int osmo_parse_mme_domain(struct osmo_gummei *out, const char *in)
475{
476 int rc;
477
478 memset(out, 0, sizeof(*out));
479 rc = sscanf(in, "mmec%02hhx.mmegi%04hx.mme.epc.mnc%03hu.mcc%03hu.3gppnetwork.org",
480 &out->mme.code, &out->mme.group_id,
481 &out->plmn.mnc, &out->plmn.mcc);
482 if (rc < 0)
483 return rc;
484 if (rc != 4)
485 return -EINVAL;
486 return 0;
487}
488
489/*! Generate TS 23.003 Section 19.4.2.4 MME Group Domain (text form)
490 * \param out[out] caller-provided output buffer, at least 56 bytes long
491 * \param mmegi[in] MME Group Identifier
492 * \param plmn[in] Osmocom representation of PLMN ID (MCC + MNC)
493 * \returns number of characters printed (excluding NUL); negative on error */
494int osmo_gen_mme_group_domain(char *out, uint16_t mmegi, const struct osmo_plmn_id *plmn)
495{
496 char domain[GSM23003_HOME_NETWORK_DOMAIN_LEN+1];
497 int rc;
498 rc = osmo_gen_home_network_domain(domain, plmn);
499 if (rc < 0)
500 return rc;
501 return sprintf(out, "mmegi%04x.mme.%s", mmegi, domain);
502}