blob: c2b3de86865c47fd4e86b123f1ee302556dfaf3e [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{
Oliver Smithcdac6202019-05-10 11:28:27 +020072 return is_n_digits(msisdn, GSM23003_MSISDN_MIN_DIGITS, GSM23003_MSISDN_MAX_DIGITS);
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +020073}
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{
Harald Welte171ef822019-03-28 10:49:05 +0100110 static __thread char buf[8];
Harald Welte4a62eda2019-03-18 18:27:00 +0100111 return osmo_mcc_name_buf(buf, sizeof(buf), mcc);
112}
113
Harald Welte179f3572019-03-18 18:38:47 +0100114/*! Return MCC string as standardized 3-digit with leading zeros, into a talloc-allocated buffer.
115 * \param[in] ctx talloc context from which to allocate output buffer
116 * \param[in] mcc MCC value.
117 * \returns string in dynamically allocated buffer.
118 */
119const char *osmo_mcc_name_c(const void *ctx, uint16_t mcc)
120{
121 char *buf = talloc_size(ctx, 8);
122 if (!buf)
123 return NULL;
124 return osmo_mcc_name_buf(buf, 8, mcc);
125}
126
Harald Welte4a62eda2019-03-18 18:27:00 +0100127/*! Return MNC string as standardized 2- or 3-digit with leading zeros.
128 * \param[out] buf caller-allocated output buffer
129 * \param[in] buf_len size of buf in bytes
130 * \param[in] mnc MNC value.
131 * \param[in] mnc_3_digits True if an MNC should fill three digits, only has an effect if MNC < 100.
132 * \returns string in static buffer.
133 */
134char *osmo_mnc_name_buf(char *buf, size_t buf_len, uint16_t mnc, bool mnc_3_digits)
135{
136 snprintf(buf, buf_len, "%0*u", mnc_3_digits ? 3 : 2, mnc);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100137 return buf;
138}
139
Harald Welte179f3572019-03-18 18:38:47 +0100140/*! Return MNC string as standardized 2- or 3-digit with leading zeros, into a talloc-allocated buffer.
141 * \param[in] ctx talloc context from which to allocate output buffer
142 * \param[in] mnc MNC value.
143 * \param[in] mnc_3_digits True if an MNC should fill three digits, only has an effect if MNC < 100.
144 * \returns string in dynamically allocated buffer.
145 */
146char *osmo_mnc_name_c(const void *ctx, uint16_t mnc, bool mnc_3_digits)
147{
148 char *buf = talloc_size(ctx, 8);
149 if (!buf)
150 return buf;
151 return osmo_mnc_name_buf(buf, 8, mnc, mnc_3_digits);
152}
153
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100154/*! Return MNC string as standardized 2- or 3-digit with leading zeros.
155 * \param[in] mnc MNC value.
156 * \param[in] mnc_3_digits True if an MNC should fill three digits, only has an effect if MNC < 100.
157 * \returns string in static buffer.
158 */
159const char *osmo_mnc_name(uint16_t mnc, bool mnc_3_digits)
160{
Harald Welte171ef822019-03-28 10:49:05 +0100161 static __thread char buf[8];
Harald Welte4a62eda2019-03-18 18:27:00 +0100162 return osmo_mnc_name_buf(buf, sizeof(buf), mnc, mnc_3_digits);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100163}
164
Harald Welte4a62eda2019-03-18 18:27:00 +0100165/*! Return MCC-MNC string as standardized 3-digit-dash-2/3-digit with leading zeros.
166 * \param[out] buf caller-allocated output buffer
167 * \param[in] buf_len size of buf in bytes
168 * \param[in] plmn MCC-MNC value.
169 * \returns string in static buffer.
170 */
171char *osmo_plmn_name_buf(char *buf, size_t buf_len, const struct osmo_plmn_id *plmn)
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100172{
Harald Welte4a62eda2019-03-18 18:27:00 +0100173 char mcc[8], mnc[8];
174 snprintf(buf, buf_len, "%s-%s", osmo_mcc_name_buf(mcc, sizeof(mcc), plmn->mcc),
175 osmo_mnc_name_buf(mnc, sizeof(mnc), plmn->mnc, plmn->mnc_3_digits));
176 return buf;
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100177}
178
179/*! Return MCC-MNC string as standardized 3-digit-dash-2/3-digit with leading zeros.
180 * \param[in] plmn MCC-MNC value.
181 * \returns string in static buffer.
182 */
183const char *osmo_plmn_name(const struct osmo_plmn_id *plmn)
184{
Harald Welte171ef822019-03-28 10:49:05 +0100185 static __thread char buf[16];
Harald Welte4a62eda2019-03-18 18:27:00 +0100186 return osmo_plmn_name_buf(buf, sizeof(buf), plmn);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100187}
188
Harald Welte4a62eda2019-03-18 18:27:00 +0100189
Neels Hofmeyr6d57c572019-02-26 21:54:00 +0100190/*! Same as osmo_plmn_name(), but returning in a different static buffer.
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100191 * \param[in] plmn MCC-MNC value.
192 * \returns string in static buffer.
193 */
194const char *osmo_plmn_name2(const struct osmo_plmn_id *plmn)
195{
Harald Welte171ef822019-03-28 10:49:05 +0100196 static __thread char buf[16];
Harald Welte4a62eda2019-03-18 18:27:00 +0100197 return osmo_plmn_name_buf(buf, sizeof(buf), plmn);
198}
199
Harald Welte179f3572019-03-18 18:38:47 +0100200/*! Return MCC-MNC string as standardized 3-digit-dash-2/3-digit with leading zeros, into
201 * a dynamically-allocated output buffer.
202 * \param[in] ctx talloc context from which to allocate output buffer
203 * \param[in] plmn MCC-MNC value.
204 * \returns string in dynamically allocated buffer.
205 */
206char *osmo_plmn_name_c(const void *ctx, const struct osmo_plmn_id *plmn)
207{
208 char *buf = talloc_size(ctx, 16);
209 if (!buf)
210 return NULL;
211 return osmo_plmn_name_buf(buf, 16, plmn);
212}
213
Harald Welte4a62eda2019-03-18 18:27:00 +0100214/*! Return MCC-MNC-LAC as string, in caller-provided output buffer.
215 * \param[out] buf caller-allocated output buffer
216 * \param[in] buf_len size of buf in bytes
217 * \param[in] lai LAI to encode, the rac member is ignored.
218 * \returns buf
219 */
220char *osmo_lai_name_buf(char *buf, size_t buf_len, const struct osmo_location_area_id *lai)
221{
222 char plmn[16];
223 snprintf(buf, buf_len, "%s-%u", osmo_plmn_name_buf(plmn, sizeof(plmn), &lai->plmn), lai->lac);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100224 return buf;
225}
226
227/*! Return MCC-MNC-LAC as string, in a static buffer.
228 * \param[in] lai LAI to encode, the rac member is ignored.
229 * \returns Static string buffer.
230 */
231const char *osmo_lai_name(const struct osmo_location_area_id *lai)
232{
Harald Welte171ef822019-03-28 10:49:05 +0100233 static __thread char buf[32];
Harald Welte4a62eda2019-03-18 18:27:00 +0100234 return osmo_lai_name_buf(buf, sizeof(buf), lai);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100235}
236
Harald Welte179f3572019-03-18 18:38:47 +0100237/*! Return MCC-MNC-LAC as string, in a talloc-allocated output buffer.
238 * \param[in] ctx talloc context from which to allocate output buffer
239 * \param[in] lai LAI to encode, the rac member is ignored.
240 * \returns string representation of lai in dynamically allocated buffer.
241 */
242char *osmo_lai_name_c(const void *ctx, const struct osmo_location_area_id *lai)
243{
244 char *buf = talloc_size(ctx, 32);
245 if (!buf)
246 return NULL;
247 return osmo_lai_name_buf(buf, 32, lai);
248}
249
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100250/*! Return MCC-MNC-LAC-RAC as string, in caller-provided output buffer.
251 * \param[out] buf caller-allocated output buffer
252 * \param[in] buf_len size of buf in bytes
253 * \param[in] rai RAI to encode, the rac member is ignored.
254 * \returns buf
255 */
256char *osmo_rai_name2_buf(char *buf, size_t buf_len, const struct osmo_routing_area_id *rai)
257{
258 char plmn[16];
259 snprintf(buf, buf_len, "%s-%u-%u", osmo_plmn_name_buf(plmn, sizeof(plmn),
260 &rai->lac.plmn), rai->lac.lac, rai->rac);
261 return buf;
262}
263
264/*! Return MCC-MNC-LAC-RAC as string, in a static buffer.
265 * \param[in] rai RAI to encode, the rac member is ignored.
266 * \returns Static string buffer.
267 */
268const char *osmo_rai_name2(const struct osmo_routing_area_id *rai)
269{
270 static __thread char buf[32];
271 return osmo_rai_name2_buf(buf, sizeof(buf), rai);
272}
273
274/*! Return MCC-MNC-LAC-RAC as string, in a talloc-allocated output buffer.
275 * \param[in] ctx talloc context from which to allocate output buffer
276 * \param[in] rai RAI to encode, the rac member is ignored.
277 * \returns string representation of lai in dynamically allocated buffer.
278 */
279char *osmo_rai_name2_c(const void *ctx, const struct osmo_routing_area_id *rai)
280{
281 char *buf = talloc_size(ctx, 32);
282 if (!buf)
283 return NULL;
284 return osmo_rai_name2_buf(buf, 32, rai);
285}
286
Harald Welte4a62eda2019-03-18 18:27:00 +0100287/*! Return MCC-MNC-LAC-CI as string, in caller-provided output buffer.
288 * \param[out] buf caller-allocated output buffer
289 * \param[in] buf_len size of buf in bytes
290 * \param[in] cgi CGI to encode.
291 * \returns buf
292 */
293char *osmo_cgi_name_buf(char *buf, size_t buf_len, const struct osmo_cell_global_id *cgi)
Neels Hofmeyr43496202018-03-22 14:04:30 +0100294{
Harald Welte4a62eda2019-03-18 18:27:00 +0100295 snprintf(buf, buf_len, "%s-%u", osmo_lai_name(&cgi->lai), cgi->cell_identity);
Neels Hofmeyr43496202018-03-22 14:04:30 +0100296 return buf;
297}
298
299/*! Return MCC-MNC-LAC-CI as string, in a static buffer.
300 * \param[in] cgi CGI to encode.
301 * \returns Static string buffer.
302 */
303const char *osmo_cgi_name(const struct osmo_cell_global_id *cgi)
304{
Harald Welte171ef822019-03-28 10:49:05 +0100305 static __thread char buf[32];
Harald Welte4a62eda2019-03-18 18:27:00 +0100306 return osmo_cgi_name_buf(buf, sizeof(buf), cgi);
Neels Hofmeyr43496202018-03-22 14:04:30 +0100307}
308
309/*! Same as osmo_cgi_name(), but uses a different static buffer.
310 * Useful for printing two distinct CGIs in the same printf format.
311 * \param[in] cgi CGI to encode.
312 * \returns Static string buffer.
313 */
314const char *osmo_cgi_name2(const struct osmo_cell_global_id *cgi)
315{
Harald Welte171ef822019-03-28 10:49:05 +0100316 static __thread char buf[32];
Harald Welte4a62eda2019-03-18 18:27:00 +0100317 return osmo_cgi_name_buf(buf, sizeof(buf), cgi);
Neels Hofmeyr43496202018-03-22 14:04:30 +0100318}
319
Harald Welte179f3572019-03-18 18:38:47 +0100320/*! Return MCC-MNC-LAC-CI as string, in a talloc-allocated output buffer.
321 * \param[in] ctx talloc context from which to allocate output buffer
322 * \param[in] cgi CGI to encode.
323 * \returns string representation of CGI in dynamically-allocated buffer.
324 */
325char *osmo_cgi_name_c(const void *ctx, const struct osmo_cell_global_id *cgi)
326{
327 char *buf = talloc_size(ctx, 32);
328 return osmo_cgi_name_buf(buf, 32, cgi);
329}
330
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100331/*! Return MCC-MNC-LAC-RAC-CI as string, in caller-provided output buffer.
332 * \param[out] buf caller-allocated output buffer
333 * \param[in] buf_len size of buf in bytes
334 * \param[in] cgi_ps CGI-PS to encode.
335 * \returns buf
336 */
337char *osmo_cgi_ps_name_buf(char *buf, size_t buf_len, const struct osmo_cell_global_id_ps *cgi_ps)
338{
339 snprintf(buf, buf_len, "%s-%u", osmo_rai_name2(&cgi_ps->rai), cgi_ps->cell_identity);
340 return buf;
341}
342
343/*! Return MCC-MNC-LAC-RAC-CI as string, in a static buffer.
344 * \param[in] cgi_ps CGI-PS to encode.
345 * \returns Static string buffer.
346 */
347const char *osmo_cgi_ps_name(const struct osmo_cell_global_id_ps *cgi_ps)
348{
349 static __thread char buf[32];
350 return osmo_cgi_ps_name_buf(buf, sizeof(buf), cgi_ps);
351}
352
353/*! Same as osmo_cgi_ps_name(), but uses a different static buffer.
354 * Useful for printing two distinct CGI-PSs in the same printf format.
355 * \param[in] cgi CGI-PS to encode.
356 * \returns Static string buffer.
357 */
358const char *osmo_cgi_ps_name2(const struct osmo_cell_global_id_ps *cgi_ps)
359{
360 static __thread char buf[32];
361 return osmo_cgi_ps_name_buf(buf, sizeof(buf), cgi_ps);
362}
363
364/*! Return MCC-MNC-LAC-RAC-CI as string, in a talloc-allocated output buffer.
365 * \param[in] ctx talloc context from which to allocate output buffer
366 * \param[in] cgi_ps CGI-PS to encode.
367 * \returns string representation of CGI in dynamically-allocated buffer.
368 */
369char *osmo_cgi_ps_name_c(const void *ctx, const struct osmo_cell_global_id_ps *cgi_ps)
370{
371 char *buf = talloc_size(ctx, 32);
372 return osmo_cgi_ps_name_buf(buf, 32, cgi_ps);
373}
374
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100375static void to_bcd(uint8_t *bcd, uint16_t val)
376{
377 bcd[2] = val % 10;
378 val = val / 10;
379 bcd[1] = val % 10;
380 val = val / 10;
381 bcd[0] = val % 10;
382}
383
Harald Welte4a62eda2019-03-18 18:27:00 +0100384/*! Return string representation of GUMMEI in caller-provided output buffer.
385 * \param[out] buf pointer to caller-provided output buffer
386 * \param[in] buf_len size of buf in bytes
387 * \param[in] gummei GUMMEI to be stringified
388 * \returns buf
389 */
390char *osmo_gummei_name_buf(char *buf, size_t buf_len, const struct osmo_gummei *gummei)
Harald Weltede1da352018-10-08 22:27:04 +0200391{
Harald Welte4a62eda2019-03-18 18:27:00 +0100392 char plmn[16];
393 snprintf(buf, buf_len, "%s-%04x-%02x", osmo_plmn_name_buf(plmn, sizeof(plmn), &gummei->plmn),
Harald Weltede1da352018-10-08 22:27:04 +0200394 gummei->mme.group_id, gummei->mme.code);
395 return buf;
396}
397
Harald Welte4a62eda2019-03-18 18:27:00 +0100398/*! Return string representation of GUMMEI in static output buffer.
399 * \param[in] gummei GUMMEI to be stringified
400 * \returns pointer to static output buffer
401 */
402const char *osmo_gummei_name(const struct osmo_gummei *gummei)
403{
Harald Welte171ef822019-03-28 10:49:05 +0100404 static __thread char buf[32];
Harald Welte4a62eda2019-03-18 18:27:00 +0100405 return osmo_gummei_name_buf(buf, sizeof(buf), gummei);
406}
407
Harald Welte179f3572019-03-18 18:38:47 +0100408/*! Return string representation of GUMMEI in static output buffer.
409 * \param[out] buf pointer to caller-provided output buffer
410 * \param[in] buf_len size of buf in bytes
411 * \param[in] gummei GUMMEI to be stringified
412 * \returns pointer to static output buffer
413 */
414char *osmo_gummei_name_c(const void *ctx, const struct osmo_gummei *gummei)
415{
416 char *buf = talloc_size(ctx, 32);
417 if (!buf)
418 return NULL;
419 return osmo_gummei_name_buf(buf, 32, gummei);
420}
Harald Welte4a62eda2019-03-18 18:27:00 +0100421
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100422/* Convert MCC + MNC to BCD representation
423 * \param[out] bcd_dst caller-allocated memory for output
424 * \param[in] mcc Mobile Country Code
425 * \param[in] mnc Mobile Network Code
426 * \param[in] mnc_3_digits true if the MNC shall have three digits.
427 *
428 * Convert given mcc and mnc to BCD and write to *bcd_dst, which must be an
429 * allocated buffer of (at least) 3 bytes length. Encode the MNC in three
430 * digits if its integer value is > 99, or if mnc_3_digits is passed true.
431 * Encode an MNC < 100 with mnc_3_digits passed as true as a three-digit MNC
432 * with leading zeros in the BCD representation.
433 */
434void osmo_plmn_to_bcd(uint8_t *bcd_dst, const struct osmo_plmn_id *plmn)
435{
436 uint8_t bcd[3];
437
438 to_bcd(bcd, plmn->mcc);
439 bcd_dst[0] = bcd[0] | (bcd[1] << 4);
440 bcd_dst[1] = bcd[2];
441
442 to_bcd(bcd, plmn->mnc);
443 if (plmn->mnc > 99 || plmn->mnc_3_digits) {
444 bcd_dst[1] |= bcd[2] << 4;
445 bcd_dst[2] = bcd[0] | (bcd[1] << 4);
446 } else {
447 bcd_dst[1] |= 0xf << 4;
448 bcd_dst[2] = bcd[1] | (bcd[2] << 4);
449 }
450}
451
452/* Convert given 3-byte BCD buffer to integers and write results to *mcc and
453 * *mnc. The first three BCD digits result in the MCC and the remaining ones in
454 * the MNC. Return mnc_3_digits as false if the MNC's most significant digit is encoded as 0xF, true
455 * otherwise; i.e. true if MNC > 99 or if it is represented with leading zeros instead of 0xF.
456 * \param[in] bcd_src 3-byte BCD buffer containing MCC+MNC representations.
457 * \param[out] mcc MCC result buffer, or NULL.
458 * \param[out] mnc MNC result buffer, or NULL.
459 * \param[out] mnc_3_digits Result buffer for 3-digit flag, or NULL.
460 */
461void osmo_plmn_from_bcd(const uint8_t *bcd_src, struct osmo_plmn_id *plmn)
462{
463 plmn->mcc = (bcd_src[0] & 0x0f) * 100
464 + (bcd_src[0] >> 4) * 10
465 + (bcd_src[1] & 0x0f);
466
467 if ((bcd_src[1] & 0xf0) == 0xf0) {
468 plmn->mnc = (bcd_src[2] & 0x0f) * 10
469 + (bcd_src[2] >> 4);
470 plmn->mnc_3_digits = false;
471 } else {
472 plmn->mnc = (bcd_src[2] & 0x0f) * 100
473 + (bcd_src[2] >> 4) * 10
474 + (bcd_src[1] >> 4);
475 plmn->mnc_3_digits = true;
476 }
477}
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100478
479/* Convert string to MNC, detecting 3-digit MNC with leading zeros.
480 * Return mnc_3_digits as false if the MNC's most significant digit is encoded as 0xF, true
481 * otherwise; i.e. true if MNC > 99 or if it is represented with leading zeros instead of 0xF.
482 * \param mnc_str[in] String representation of an MNC, with or without leading zeros.
483 * \param mnc[out] MNC result buffer, or NULL.
484 * \param[out] mnc_3_digits Result buffer for 3-digit flag, or NULL.
485 * \returns zero on success, -EINVAL in case of surplus characters, negative errno in case of conversion
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100486 * errors. In case of error, do not modify the out-arguments.
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100487 */
488int osmo_mnc_from_str(const char *mnc_str, uint16_t *mnc, bool *mnc_3_digits)
489{
490 long int _mnc = 0;
491 bool _mnc_3_digits = false;
492 char *endptr;
493 int rc = 0;
494
Kévin Redon1af2cd52019-05-23 19:00:19 +0200495 if (!mnc_str || !isdigit((unsigned char)mnc_str[0]) || strlen(mnc_str) > 3)
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100496 return -EINVAL;
497
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100498 errno = 0;
499 _mnc = strtol(mnc_str, &endptr, 10);
500 if (errno)
501 rc = -errno;
502 else if (*endptr)
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100503 return -EINVAL;
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100504 if (_mnc < 0 || _mnc > 999)
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100505 return -ERANGE;
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100506 _mnc_3_digits = strlen(mnc_str) > 2;
507
508 if (mnc)
509 *mnc = (uint16_t)_mnc;
510 if (mnc_3_digits)
511 *mnc_3_digits = _mnc_3_digits;
512 return rc;
513}
514
515/* Compare two MNC with three-digit flag.
516 * The mnc_3_digits flags passed in only have an effect if the MNC are < 100, i.e. if they would amount
517 * to a change in leading zeros in a BCD representation. An MNC >= 100 implies three digits, and the flag
518 * is actually ignored.
519 * \param a_mnc[in] "Left" side MNC.
520 * \param a_mnc_3_digits[in] "Left" side three-digits flag.
521 * \param b_mnc[in] "Right" side MNC.
522 * \param b_mnc_3_digits[in] "Right" side three-digits flag.
523 * \returns 0 if the MNC are equal, -1 if a < b or a shorter, 1 if a > b or a longer. */
524int osmo_mnc_cmp(uint16_t a_mnc, bool a_mnc_3_digits, uint16_t b_mnc, bool b_mnc_3_digits)
525{
526 if (a_mnc < b_mnc)
527 return -1;
528 if (a_mnc > b_mnc)
529 return 1;
530 /* a_mnc == b_mnc, but same amount of leading zeros? */
531 if (a_mnc < 100 && a_mnc_3_digits != b_mnc_3_digits)
532 return a_mnc_3_digits ? 1 : -1;
533 return 0;
534}
535
536/* Compare two PLMN.
537 * \param a[in] "Left" side PLMN.
538 * \param b[in] "Right" side PLMN.
539 * \returns 0 if the PLMN are equal, -1 if a < b or a shorter, 1 if a > b or a longer. */
540int osmo_plmn_cmp(const struct osmo_plmn_id *a, const struct osmo_plmn_id *b)
541{
542 if (a == b)
543 return 0;
544 if (a->mcc < b->mcc)
545 return -1;
546 if (a->mcc > b->mcc)
547 return 1;
548 return osmo_mnc_cmp(a->mnc, a->mnc_3_digits, b->mnc, b->mnc_3_digits);
549}
Harald Weltede1da352018-10-08 22:27:04 +0200550
Neels Hofmeyrd01ef752018-09-21 15:57:26 +0200551/* Compare two LAI.
552 * The order of comparison is MCC, MNC, LAC. See also osmo_plmn_cmp().
553 * \param a[in] "Left" side LAI.
554 * \param b[in] "Right" side LAI.
555 * \returns 0 if the LAI are equal, -1 if a < b, 1 if a > b. */
556int osmo_lai_cmp(const struct osmo_location_area_id *a, const struct osmo_location_area_id *b)
557{
558 int rc = osmo_plmn_cmp(&a->plmn, &b->plmn);
559 if (rc)
560 return rc;
561 if (a->lac < b->lac)
562 return -1;
563 if (a->lac > b->lac)
564 return 1;
565 return 0;
566}
567
Pau Espin Pedrold426ba62021-01-22 17:44:34 +0100568/* Compare two RAI.
569 * The order of comparison is MCC, MNC, LAC, RAC. See also osmo_lai_cmp().
570 * \param a[in] "Left" side RAI.
571 * \param b[in] "Right" side RAI.
572 * \returns 0 if the RAI are equal, -1 if a < b, 1 if a > b. */
573int osmo_rai_cmp(const struct osmo_routing_area_id *a, const struct osmo_routing_area_id *b)
574{
575 int rc = osmo_lai_cmp(&a->lac, &b->lac);
576 if (rc)
577 return rc;
578 if (a->rac < b->rac)
579 return -1;
580 if (a->rac > b->rac)
581 return 1;
582 return 0;
583}
584
Neels Hofmeyrd01ef752018-09-21 15:57:26 +0200585/* Compare two CGI.
586 * The order of comparison is MCC, MNC, LAC, CI. See also osmo_lai_cmp().
587 * \param a[in] "Left" side CGI.
588 * \param b[in] "Right" side CGI.
589 * \returns 0 if the CGI are equal, -1 if a < b, 1 if a > b. */
590int osmo_cgi_cmp(const struct osmo_cell_global_id *a, const struct osmo_cell_global_id *b)
591{
592 int rc = osmo_lai_cmp(&a->lai, &b->lai);
593 if (rc)
594 return rc;
595 if (a->cell_identity < b->cell_identity)
596 return -1;
597 if (a->cell_identity > b->cell_identity)
598 return 1;
599 return 0;
600}
601
Pau Espin Pedrold426ba62021-01-22 17:44:34 +0100602/* Compare two CGI-PS.
603 * The order of comparison is MCC, MNC, LAC, RAC, CI. See also osmo_rai_cmp().
604 * \param a[in] "Left" side CGI-PS.
605 * \param b[in] "Right" side CGI-PS.
606 * \returns 0 if the CGI are equal, -1 if a < b, 1 if a > b. */
607int osmo_cgi_ps_cmp(const struct osmo_cell_global_id_ps *a, const struct osmo_cell_global_id_ps *b)
608{
609 int rc = osmo_rai_cmp(&a->rai, &b->rai);
610 if (rc)
611 return rc;
612 if (a->cell_identity < b->cell_identity)
613 return -1;
614 if (a->cell_identity > b->cell_identity)
615 return 1;
616 return 0;
617}
618
Harald Weltede1da352018-10-08 22:27:04 +0200619/*! Generate TS 23.003 Section 19.2 Home Network Realm/Domain (text form)
620 * \param out[out] caller-provided output buffer, at least 33 bytes long
621 * \param plmn[in] Osmocom representation of PLMN ID (MCC + MNC)
622 * \returns number of characters printed (excluding NUL); negative on error */
623int osmo_gen_home_network_domain(char *out, const struct osmo_plmn_id *plmn)
624{
625 if (plmn->mcc > 999)
626 return -EINVAL;
627 if (plmn->mnc > 999)
628 return -EINVAL;
629 return sprintf(out, "epc.mnc%03u.mcc%03u.3gppnetwork.org", plmn->mnc, plmn->mcc);
630}
631
632/*! Parse a TS 23.003 Section 19.2 Home Network Realm/Domain (text form) into a \ref osmo_plmn_id
633 * \param out[out] caller-allocated output structure
634 * \param in[in] character string representation to be parsed
635 * \returns 0 on success; negative on error */
636int osmo_parse_home_network_domain(struct osmo_plmn_id *out, const char *in)
637{
638 int rc;
639
640 memset(out, 0, sizeof(*out));
641 rc = sscanf(in, "epc.mnc%03hu.mcc%03hu.3gppnetwork.org", &out->mnc, &out->mcc);
642 if (rc < 0)
643 return rc;
644 if (rc != 2)
645 return -EINVAL;
646 return 0;
647}
648
649/*! Generate TS 23.003 Section 19.4.2.4 MME Domain (text form)
650 * \param out[out] caller-provided output buffer, at least 56 bytes long
651 * \param gummei[in] Structure representing the Globally Unique MME Identifier
652 * \returns number of characters printed (excluding NUL); negative on error */
653int osmo_gen_mme_domain(char *out, const struct osmo_gummei *gummei)
654{
655 char domain[GSM23003_HOME_NETWORK_DOMAIN_LEN+1];
656 int rc;
657 rc = osmo_gen_home_network_domain(domain, &gummei->plmn);
658 if (rc < 0)
659 return rc;
660 return sprintf(out, "mmec%02x.mmegi%04x.mme.%s", gummei->mme.code, gummei->mme.group_id, domain);
661}
662
663/*! Parse a TS 23.003 Section 19.4.2.4 MME Domain (text form) into a \ref osmo_gummei
664 * \param out[out] caller-allocated output GUMMEI structure
665 * \param in[in] character string representation to be parsed
666 * \returns 0 on success; negative on error */
667int osmo_parse_mme_domain(struct osmo_gummei *out, const char *in)
668{
669 int rc;
670
671 memset(out, 0, sizeof(*out));
672 rc = sscanf(in, "mmec%02hhx.mmegi%04hx.mme.epc.mnc%03hu.mcc%03hu.3gppnetwork.org",
673 &out->mme.code, &out->mme.group_id,
674 &out->plmn.mnc, &out->plmn.mcc);
675 if (rc < 0)
676 return rc;
677 if (rc != 4)
678 return -EINVAL;
679 return 0;
680}
681
682/*! Generate TS 23.003 Section 19.4.2.4 MME Group Domain (text form)
683 * \param out[out] caller-provided output buffer, at least 56 bytes long
684 * \param mmegi[in] MME Group Identifier
685 * \param plmn[in] Osmocom representation of PLMN ID (MCC + MNC)
686 * \returns number of characters printed (excluding NUL); negative on error */
687int osmo_gen_mme_group_domain(char *out, uint16_t mmegi, const struct osmo_plmn_id *plmn)
688{
689 char domain[GSM23003_HOME_NETWORK_DOMAIN_LEN+1];
690 int rc;
691 rc = osmo_gen_home_network_domain(domain, plmn);
692 if (rc < 0)
693 return rc;
694 return sprintf(out, "mmegi%04x.mme.%s", mmegi, domain);
695}