blob: b893873ba8597e4961fc9bd83a8aa1c28cc3b44e [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 *
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +020020 */
21
22#include <ctype.h>
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010023#include <stdio.h>
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +010024#include <stdlib.h>
25#include <errno.h>
26#include <string.h>
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +020027
28#include <osmocom/gsm/gsm23003.h>
29#include <osmocom/gsm/protocol/gsm_23_003.h>
Oliver Smith894be2d2019-01-11 13:13:37 +010030#include <osmocom/core/utils.h>
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +020031
32static bool is_n_digits(const char *str, int min_digits, int max_digits)
33{
34 int len;
35 /* Use unsigned char * to avoid a compiler warning of
36 * "error: array subscript has type 'char' [-Werror=char-subscripts]" */
37 const unsigned char *pos = (const unsigned char *)str;
Neels Hofmeyr4b7c7912017-10-07 04:45:01 +020038 if (!pos)
39 return min_digits < 1;
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +020040 for (len = 0; *pos && len < max_digits; len++, pos++)
41 if (!isdigit(*pos))
42 return false;
43 if (len < min_digits)
44 return false;
45 /* With not too many digits, we should have reached *str == nul */
46 if (*pos)
47 return false;
48 return true;
49}
50
51/*! Determine whether the given IMSI is valid according to 3GPP TS 23.003.
52 * \param imsi IMSI digits in ASCII string representation.
53 * \returns true when the IMSI is valid, false for invalid characters or number
54 * of digits.
55 */
56bool osmo_imsi_str_valid(const char *imsi)
57{
58 return is_n_digits(imsi, GSM23003_IMSI_MIN_DIGITS, GSM23003_IMSI_MAX_DIGITS);
59}
60
61/*! Determine whether the given MSISDN is valid according to 3GPP TS 23.003.
62 * \param msisdn MSISDN digits in ASCII string representation.
63 * \returns true when the MSISDN is valid, false for invalid characters or number
64 * of digits.
65 */
66bool osmo_msisdn_str_valid(const char *msisdn)
67{
Oliver Smithcdac6202019-05-10 11:28:27 +020068 return is_n_digits(msisdn, GSM23003_MSISDN_MIN_DIGITS, GSM23003_MSISDN_MAX_DIGITS);
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +020069}
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010070
Oliver Smith894be2d2019-01-11 13:13:37 +010071/*! Determine whether the given IMEI is valid according to 3GPP TS 23.003,
72 * Section 6.2.1. It consists of 14 digits, the 15th check digit is not
73 * intended for digital transmission.
74 * \param imei IMEI digits in ASCII string representation.
75 * \param with_15th_digit when true, expect the 15th digit to be present and
76 * verify it.
77 * \returns true when the IMEI is valid, false for invalid characters or number
78 * of digits.
79 */
80bool osmo_imei_str_valid(const char *imei, bool with_15th_digit)
81{
82 if (with_15th_digit)
83 return is_n_digits(imei, 15, 15) && osmo_luhn(imei, 14) == imei[14];
84 else
85 return is_n_digits(imei, 14, 14);
86}
87
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010088/*! Return MCC string as standardized 3-digit with leading zeros.
Harald Welte4a62eda2019-03-18 18:27:00 +010089 * \param[out] buf caller-allocated output buffer
90 * \param[in] buf_len size of buf in bytes
91 * \param[in] mcc MCC value.
92 * \returns string in user-supplied output buffer
93 */
94char *osmo_mcc_name_buf(char *buf, size_t buf_len, uint16_t mcc)
95{
96 snprintf(buf, buf_len, "%03u", mcc);
97 return buf;
98}
99
100/*! Return MCC string as standardized 3-digit with leading zeros.
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100101 * \param[in] mcc MCC value.
102 * \returns string in static buffer.
103 */
104const char *osmo_mcc_name(uint16_t mcc)
105{
Harald Welte171ef822019-03-28 10:49:05 +0100106 static __thread char buf[8];
Harald Welte4a62eda2019-03-18 18:27:00 +0100107 return osmo_mcc_name_buf(buf, sizeof(buf), mcc);
108}
109
Harald Welte179f3572019-03-18 18:38:47 +0100110/*! Return MCC string as standardized 3-digit with leading zeros, into a talloc-allocated buffer.
111 * \param[in] ctx talloc context from which to allocate output buffer
112 * \param[in] mcc MCC value.
113 * \returns string in dynamically allocated buffer.
114 */
115const char *osmo_mcc_name_c(const void *ctx, uint16_t mcc)
116{
117 char *buf = talloc_size(ctx, 8);
118 if (!buf)
119 return NULL;
120 return osmo_mcc_name_buf(buf, 8, mcc);
121}
122
Harald Welte4a62eda2019-03-18 18:27:00 +0100123/*! Return MNC string as standardized 2- or 3-digit with leading zeros.
124 * \param[out] buf caller-allocated output buffer
125 * \param[in] buf_len size of buf in bytes
126 * \param[in] mnc MNC value.
127 * \param[in] mnc_3_digits True if an MNC should fill three digits, only has an effect if MNC < 100.
128 * \returns string in static buffer.
129 */
130char *osmo_mnc_name_buf(char *buf, size_t buf_len, uint16_t mnc, bool mnc_3_digits)
131{
132 snprintf(buf, buf_len, "%0*u", mnc_3_digits ? 3 : 2, mnc);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100133 return buf;
134}
135
Harald Welte179f3572019-03-18 18:38:47 +0100136/*! Return MNC string as standardized 2- or 3-digit with leading zeros, into a talloc-allocated buffer.
137 * \param[in] ctx talloc context from which to allocate output buffer
138 * \param[in] mnc MNC value.
139 * \param[in] mnc_3_digits True if an MNC should fill three digits, only has an effect if MNC < 100.
140 * \returns string in dynamically allocated buffer.
141 */
142char *osmo_mnc_name_c(const void *ctx, uint16_t mnc, bool mnc_3_digits)
143{
144 char *buf = talloc_size(ctx, 8);
145 if (!buf)
146 return buf;
147 return osmo_mnc_name_buf(buf, 8, mnc, mnc_3_digits);
148}
149
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100150/*! Return MNC string as standardized 2- or 3-digit with leading zeros.
151 * \param[in] mnc MNC value.
152 * \param[in] mnc_3_digits True if an MNC should fill three digits, only has an effect if MNC < 100.
153 * \returns string in static buffer.
154 */
155const char *osmo_mnc_name(uint16_t mnc, bool mnc_3_digits)
156{
Harald Welte171ef822019-03-28 10:49:05 +0100157 static __thread char buf[8];
Harald Welte4a62eda2019-03-18 18:27:00 +0100158 return osmo_mnc_name_buf(buf, sizeof(buf), mnc, mnc_3_digits);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100159}
160
Harald Welte4a62eda2019-03-18 18:27:00 +0100161/*! Return MCC-MNC string as standardized 3-digit-dash-2/3-digit with leading zeros.
162 * \param[out] buf caller-allocated output buffer
163 * \param[in] buf_len size of buf in bytes
164 * \param[in] plmn MCC-MNC value.
165 * \returns string in static buffer.
166 */
167char *osmo_plmn_name_buf(char *buf, size_t buf_len, const struct osmo_plmn_id *plmn)
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100168{
Harald Welte4a62eda2019-03-18 18:27:00 +0100169 char mcc[8], mnc[8];
170 snprintf(buf, buf_len, "%s-%s", osmo_mcc_name_buf(mcc, sizeof(mcc), plmn->mcc),
171 osmo_mnc_name_buf(mnc, sizeof(mnc), plmn->mnc, plmn->mnc_3_digits));
172 return buf;
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100173}
174
175/*! Return MCC-MNC string as standardized 3-digit-dash-2/3-digit with leading zeros.
176 * \param[in] plmn MCC-MNC value.
177 * \returns string in static buffer.
178 */
179const char *osmo_plmn_name(const struct osmo_plmn_id *plmn)
180{
Harald Welte171ef822019-03-28 10:49:05 +0100181 static __thread char buf[16];
Harald Welte4a62eda2019-03-18 18:27:00 +0100182 return osmo_plmn_name_buf(buf, sizeof(buf), plmn);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100183}
184
Harald Welte4a62eda2019-03-18 18:27:00 +0100185
Neels Hofmeyr6d57c572019-02-26 21:54:00 +0100186/*! Same as osmo_plmn_name(), but returning in a different static buffer.
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100187 * \param[in] plmn MCC-MNC value.
188 * \returns string in static buffer.
189 */
190const char *osmo_plmn_name2(const struct osmo_plmn_id *plmn)
191{
Harald Welte171ef822019-03-28 10:49:05 +0100192 static __thread char buf[16];
Harald Welte4a62eda2019-03-18 18:27:00 +0100193 return osmo_plmn_name_buf(buf, sizeof(buf), plmn);
194}
195
Harald Welte179f3572019-03-18 18:38:47 +0100196/*! Return MCC-MNC string as standardized 3-digit-dash-2/3-digit with leading zeros, into
197 * a dynamically-allocated output buffer.
198 * \param[in] ctx talloc context from which to allocate output buffer
199 * \param[in] plmn MCC-MNC value.
200 * \returns string in dynamically allocated buffer.
201 */
202char *osmo_plmn_name_c(const void *ctx, const struct osmo_plmn_id *plmn)
203{
204 char *buf = talloc_size(ctx, 16);
205 if (!buf)
206 return NULL;
207 return osmo_plmn_name_buf(buf, 16, plmn);
208}
209
Harald Welte4a62eda2019-03-18 18:27:00 +0100210/*! Return MCC-MNC-LAC as string, in caller-provided output buffer.
211 * \param[out] buf caller-allocated output buffer
212 * \param[in] buf_len size of buf in bytes
213 * \param[in] lai LAI to encode, the rac member is ignored.
214 * \returns buf
215 */
216char *osmo_lai_name_buf(char *buf, size_t buf_len, const struct osmo_location_area_id *lai)
217{
218 char plmn[16];
219 snprintf(buf, buf_len, "%s-%u", osmo_plmn_name_buf(plmn, sizeof(plmn), &lai->plmn), lai->lac);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100220 return buf;
221}
222
223/*! Return MCC-MNC-LAC as string, in a static buffer.
224 * \param[in] lai LAI to encode, the rac member is ignored.
225 * \returns Static string buffer.
226 */
227const char *osmo_lai_name(const struct osmo_location_area_id *lai)
228{
Harald Welte171ef822019-03-28 10:49:05 +0100229 static __thread char buf[32];
Harald Welte4a62eda2019-03-18 18:27:00 +0100230 return osmo_lai_name_buf(buf, sizeof(buf), lai);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100231}
232
Harald Welte179f3572019-03-18 18:38:47 +0100233/*! Return MCC-MNC-LAC as string, in a talloc-allocated output buffer.
234 * \param[in] ctx talloc context from which to allocate output buffer
235 * \param[in] lai LAI to encode, the rac member is ignored.
236 * \returns string representation of lai in dynamically allocated buffer.
237 */
238char *osmo_lai_name_c(const void *ctx, const struct osmo_location_area_id *lai)
239{
240 char *buf = talloc_size(ctx, 32);
241 if (!buf)
242 return NULL;
243 return osmo_lai_name_buf(buf, 32, lai);
244}
245
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100246/*! Return MCC-MNC-LAC-RAC as string, in caller-provided output buffer.
247 * \param[out] buf caller-allocated output buffer
248 * \param[in] buf_len size of buf in bytes
249 * \param[in] rai RAI to encode, the rac member is ignored.
250 * \returns buf
251 */
252char *osmo_rai_name2_buf(char *buf, size_t buf_len, const struct osmo_routing_area_id *rai)
253{
254 char plmn[16];
255 snprintf(buf, buf_len, "%s-%u-%u", osmo_plmn_name_buf(plmn, sizeof(plmn),
256 &rai->lac.plmn), rai->lac.lac, rai->rac);
257 return buf;
258}
259
260/*! Return MCC-MNC-LAC-RAC as string, in a static buffer.
261 * \param[in] rai RAI to encode, the rac member is ignored.
262 * \returns Static string buffer.
263 */
264const char *osmo_rai_name2(const struct osmo_routing_area_id *rai)
265{
266 static __thread char buf[32];
267 return osmo_rai_name2_buf(buf, sizeof(buf), rai);
268}
269
270/*! Return MCC-MNC-LAC-RAC as string, in a talloc-allocated output buffer.
271 * \param[in] ctx talloc context from which to allocate output buffer
272 * \param[in] rai RAI to encode, the rac member is ignored.
273 * \returns string representation of lai in dynamically allocated buffer.
274 */
275char *osmo_rai_name2_c(const void *ctx, const struct osmo_routing_area_id *rai)
276{
277 char *buf = talloc_size(ctx, 32);
278 if (!buf)
279 return NULL;
280 return osmo_rai_name2_buf(buf, 32, rai);
281}
282
Harald Welte4a62eda2019-03-18 18:27:00 +0100283/*! Return MCC-MNC-LAC-CI as string, in caller-provided output buffer.
284 * \param[out] buf caller-allocated output buffer
285 * \param[in] buf_len size of buf in bytes
286 * \param[in] cgi CGI to encode.
287 * \returns buf
288 */
289char *osmo_cgi_name_buf(char *buf, size_t buf_len, const struct osmo_cell_global_id *cgi)
Neels Hofmeyr43496202018-03-22 14:04:30 +0100290{
Harald Welte4a62eda2019-03-18 18:27:00 +0100291 snprintf(buf, buf_len, "%s-%u", osmo_lai_name(&cgi->lai), cgi->cell_identity);
Neels Hofmeyr43496202018-03-22 14:04:30 +0100292 return buf;
293}
294
295/*! Return MCC-MNC-LAC-CI as string, in a static buffer.
296 * \param[in] cgi CGI to encode.
297 * \returns Static string buffer.
298 */
299const char *osmo_cgi_name(const struct osmo_cell_global_id *cgi)
300{
Harald Welte171ef822019-03-28 10:49:05 +0100301 static __thread char buf[32];
Harald Welte4a62eda2019-03-18 18:27:00 +0100302 return osmo_cgi_name_buf(buf, sizeof(buf), cgi);
Neels Hofmeyr43496202018-03-22 14:04:30 +0100303}
304
305/*! Same as osmo_cgi_name(), but uses a different static buffer.
306 * Useful for printing two distinct CGIs in the same printf format.
307 * \param[in] cgi CGI to encode.
308 * \returns Static string buffer.
309 */
310const char *osmo_cgi_name2(const struct osmo_cell_global_id *cgi)
311{
Harald Welte171ef822019-03-28 10:49:05 +0100312 static __thread char buf[32];
Harald Welte4a62eda2019-03-18 18:27:00 +0100313 return osmo_cgi_name_buf(buf, sizeof(buf), cgi);
Neels Hofmeyr43496202018-03-22 14:04:30 +0100314}
315
Harald Welte179f3572019-03-18 18:38:47 +0100316/*! Return MCC-MNC-LAC-CI as string, in a talloc-allocated output buffer.
317 * \param[in] ctx talloc context from which to allocate output buffer
318 * \param[in] cgi CGI to encode.
319 * \returns string representation of CGI in dynamically-allocated buffer.
320 */
321char *osmo_cgi_name_c(const void *ctx, const struct osmo_cell_global_id *cgi)
322{
323 char *buf = talloc_size(ctx, 32);
324 return osmo_cgi_name_buf(buf, 32, cgi);
325}
326
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100327/*! Return MCC-MNC-LAC-RAC-CI as string, in caller-provided output buffer.
328 * \param[out] buf caller-allocated output buffer
329 * \param[in] buf_len size of buf in bytes
330 * \param[in] cgi_ps CGI-PS to encode.
331 * \returns buf
332 */
333char *osmo_cgi_ps_name_buf(char *buf, size_t buf_len, const struct osmo_cell_global_id_ps *cgi_ps)
334{
335 snprintf(buf, buf_len, "%s-%u", osmo_rai_name2(&cgi_ps->rai), cgi_ps->cell_identity);
336 return buf;
337}
338
339/*! Return MCC-MNC-LAC-RAC-CI as string, in a static buffer.
340 * \param[in] cgi_ps CGI-PS to encode.
341 * \returns Static string buffer.
342 */
343const char *osmo_cgi_ps_name(const struct osmo_cell_global_id_ps *cgi_ps)
344{
345 static __thread char buf[32];
346 return osmo_cgi_ps_name_buf(buf, sizeof(buf), cgi_ps);
347}
348
349/*! Same as osmo_cgi_ps_name(), but uses a different static buffer.
350 * Useful for printing two distinct CGI-PSs in the same printf format.
351 * \param[in] cgi CGI-PS to encode.
352 * \returns Static string buffer.
353 */
354const char *osmo_cgi_ps_name2(const struct osmo_cell_global_id_ps *cgi_ps)
355{
356 static __thread char buf[32];
357 return osmo_cgi_ps_name_buf(buf, sizeof(buf), cgi_ps);
358}
359
360/*! Return MCC-MNC-LAC-RAC-CI as string, in a talloc-allocated output buffer.
361 * \param[in] ctx talloc context from which to allocate output buffer
362 * \param[in] cgi_ps CGI-PS to encode.
363 * \returns string representation of CGI in dynamically-allocated buffer.
364 */
365char *osmo_cgi_ps_name_c(const void *ctx, const struct osmo_cell_global_id_ps *cgi_ps)
366{
367 char *buf = talloc_size(ctx, 32);
368 return osmo_cgi_ps_name_buf(buf, 32, cgi_ps);
369}
370
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100371static void to_bcd(uint8_t *bcd, uint16_t val)
372{
373 bcd[2] = val % 10;
374 val = val / 10;
375 bcd[1] = val % 10;
376 val = val / 10;
377 bcd[0] = val % 10;
378}
379
Harald Welte4a62eda2019-03-18 18:27:00 +0100380/*! Return string representation of GUMMEI in caller-provided output buffer.
381 * \param[out] buf pointer to caller-provided output buffer
382 * \param[in] buf_len size of buf in bytes
383 * \param[in] gummei GUMMEI to be stringified
384 * \returns buf
385 */
386char *osmo_gummei_name_buf(char *buf, size_t buf_len, const struct osmo_gummei *gummei)
Harald Weltede1da352018-10-08 22:27:04 +0200387{
Harald Welte4a62eda2019-03-18 18:27:00 +0100388 char plmn[16];
389 snprintf(buf, buf_len, "%s-%04x-%02x", osmo_plmn_name_buf(plmn, sizeof(plmn), &gummei->plmn),
Harald Weltede1da352018-10-08 22:27:04 +0200390 gummei->mme.group_id, gummei->mme.code);
391 return buf;
392}
393
Harald Welte4a62eda2019-03-18 18:27:00 +0100394/*! Return string representation of GUMMEI in static output buffer.
395 * \param[in] gummei GUMMEI to be stringified
396 * \returns pointer to static output buffer
397 */
398const char *osmo_gummei_name(const struct osmo_gummei *gummei)
399{
Harald Welte171ef822019-03-28 10:49:05 +0100400 static __thread char buf[32];
Harald Welte4a62eda2019-03-18 18:27:00 +0100401 return osmo_gummei_name_buf(buf, sizeof(buf), gummei);
402}
403
Harald Welte179f3572019-03-18 18:38:47 +0100404/*! Return string representation of GUMMEI in static output buffer.
405 * \param[out] buf pointer to caller-provided output buffer
406 * \param[in] buf_len size of buf in bytes
407 * \param[in] gummei GUMMEI to be stringified
408 * \returns pointer to static output buffer
409 */
410char *osmo_gummei_name_c(const void *ctx, const struct osmo_gummei *gummei)
411{
412 char *buf = talloc_size(ctx, 32);
413 if (!buf)
414 return NULL;
415 return osmo_gummei_name_buf(buf, 32, gummei);
416}
Harald Welte4a62eda2019-03-18 18:27:00 +0100417
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100418/* Convert MCC + MNC to BCD representation
419 * \param[out] bcd_dst caller-allocated memory for output
420 * \param[in] mcc Mobile Country Code
421 * \param[in] mnc Mobile Network Code
422 * \param[in] mnc_3_digits true if the MNC shall have three digits.
423 *
424 * Convert given mcc and mnc to BCD and write to *bcd_dst, which must be an
425 * allocated buffer of (at least) 3 bytes length. Encode the MNC in three
426 * digits if its integer value is > 99, or if mnc_3_digits is passed true.
427 * Encode an MNC < 100 with mnc_3_digits passed as true as a three-digit MNC
428 * with leading zeros in the BCD representation.
429 */
430void osmo_plmn_to_bcd(uint8_t *bcd_dst, const struct osmo_plmn_id *plmn)
431{
432 uint8_t bcd[3];
433
434 to_bcd(bcd, plmn->mcc);
435 bcd_dst[0] = bcd[0] | (bcd[1] << 4);
436 bcd_dst[1] = bcd[2];
437
438 to_bcd(bcd, plmn->mnc);
439 if (plmn->mnc > 99 || plmn->mnc_3_digits) {
440 bcd_dst[1] |= bcd[2] << 4;
441 bcd_dst[2] = bcd[0] | (bcd[1] << 4);
442 } else {
443 bcd_dst[1] |= 0xf << 4;
444 bcd_dst[2] = bcd[1] | (bcd[2] << 4);
445 }
446}
447
Philipp Maierc7c1ff52022-01-14 17:13:29 +0100448/* Convert given 3-byte BCD buffer to integers and write results to plmn->mcc and plmn->mnc. The first three BCD digits
449 * result in the MCC and the remaining ones in the MNC. Set plmn->mnc_3_digits as false if the MNC's most significant
450 * digit is encoded as 0xF, true otherwise; i.e. true if MNC > 99 or if it is represented with leading zeros instead of
451 * 0xF.
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100452 * \param[in] bcd_src 3-byte BCD buffer containing MCC+MNC representations.
Philipp Maierc7c1ff52022-01-14 17:13:29 +0100453 * \param[out] plmn user provided memory to store the result.
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100454 */
455void osmo_plmn_from_bcd(const uint8_t *bcd_src, struct osmo_plmn_id *plmn)
456{
457 plmn->mcc = (bcd_src[0] & 0x0f) * 100
458 + (bcd_src[0] >> 4) * 10
459 + (bcd_src[1] & 0x0f);
460
461 if ((bcd_src[1] & 0xf0) == 0xf0) {
462 plmn->mnc = (bcd_src[2] & 0x0f) * 10
463 + (bcd_src[2] >> 4);
464 plmn->mnc_3_digits = false;
465 } else {
466 plmn->mnc = (bcd_src[2] & 0x0f) * 100
467 + (bcd_src[2] >> 4) * 10
468 + (bcd_src[1] >> 4);
469 plmn->mnc_3_digits = true;
470 }
471}
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100472
473/* Convert string to MNC, detecting 3-digit MNC with leading zeros.
474 * Return mnc_3_digits as false if the MNC's most significant digit is encoded as 0xF, true
475 * otherwise; i.e. true if MNC > 99 or if it is represented with leading zeros instead of 0xF.
476 * \param mnc_str[in] String representation of an MNC, with or without leading zeros.
477 * \param mnc[out] MNC result buffer, or NULL.
478 * \param[out] mnc_3_digits Result buffer for 3-digit flag, or NULL.
479 * \returns zero on success, -EINVAL in case of surplus characters, negative errno in case of conversion
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100480 * errors. In case of error, do not modify the out-arguments.
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100481 */
482int osmo_mnc_from_str(const char *mnc_str, uint16_t *mnc, bool *mnc_3_digits)
483{
Neels Hofmeyr34907fe2021-09-05 19:50:34 +0200484 int _mnc = 0;
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100485 bool _mnc_3_digits = false;
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100486 int rc = 0;
487
Kévin Redon1af2cd52019-05-23 19:00:19 +0200488 if (!mnc_str || !isdigit((unsigned char)mnc_str[0]) || strlen(mnc_str) > 3)
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100489 return -EINVAL;
490
Neels Hofmeyr34907fe2021-09-05 19:50:34 +0200491 rc = osmo_str_to_int(&_mnc, mnc_str, 10, 0, 999);
492 /* Heed the API definition to return -EINVAL in case of surplus chars */
493 if (rc == -E2BIG)
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100494 return -EINVAL;
Neels Hofmeyr34907fe2021-09-05 19:50:34 +0200495 /* Heed the API definition to always return negative errno */
496 if (rc > 0)
497 return -rc;
498 if (rc < 0)
499 return rc;
500
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100501 _mnc_3_digits = strlen(mnc_str) > 2;
502
503 if (mnc)
504 *mnc = (uint16_t)_mnc;
505 if (mnc_3_digits)
506 *mnc_3_digits = _mnc_3_digits;
507 return rc;
508}
509
510/* Compare two MNC with three-digit flag.
511 * The mnc_3_digits flags passed in only have an effect if the MNC are < 100, i.e. if they would amount
512 * to a change in leading zeros in a BCD representation. An MNC >= 100 implies three digits, and the flag
513 * is actually ignored.
514 * \param a_mnc[in] "Left" side MNC.
515 * \param a_mnc_3_digits[in] "Left" side three-digits flag.
516 * \param b_mnc[in] "Right" side MNC.
517 * \param b_mnc_3_digits[in] "Right" side three-digits flag.
518 * \returns 0 if the MNC are equal, -1 if a < b or a shorter, 1 if a > b or a longer. */
519int osmo_mnc_cmp(uint16_t a_mnc, bool a_mnc_3_digits, uint16_t b_mnc, bool b_mnc_3_digits)
520{
521 if (a_mnc < b_mnc)
522 return -1;
523 if (a_mnc > b_mnc)
524 return 1;
525 /* a_mnc == b_mnc, but same amount of leading zeros? */
526 if (a_mnc < 100 && a_mnc_3_digits != b_mnc_3_digits)
527 return a_mnc_3_digits ? 1 : -1;
528 return 0;
529}
530
531/* Compare two PLMN.
532 * \param a[in] "Left" side PLMN.
533 * \param b[in] "Right" side PLMN.
534 * \returns 0 if the PLMN are equal, -1 if a < b or a shorter, 1 if a > b or a longer. */
535int osmo_plmn_cmp(const struct osmo_plmn_id *a, const struct osmo_plmn_id *b)
536{
537 if (a == b)
538 return 0;
539 if (a->mcc < b->mcc)
540 return -1;
541 if (a->mcc > b->mcc)
542 return 1;
543 return osmo_mnc_cmp(a->mnc, a->mnc_3_digits, b->mnc, b->mnc_3_digits);
544}
Harald Weltede1da352018-10-08 22:27:04 +0200545
Neels Hofmeyrd01ef752018-09-21 15:57:26 +0200546/* Compare two LAI.
547 * The order of comparison is MCC, MNC, LAC. See also osmo_plmn_cmp().
548 * \param a[in] "Left" side LAI.
549 * \param b[in] "Right" side LAI.
550 * \returns 0 if the LAI are equal, -1 if a < b, 1 if a > b. */
551int osmo_lai_cmp(const struct osmo_location_area_id *a, const struct osmo_location_area_id *b)
552{
553 int rc = osmo_plmn_cmp(&a->plmn, &b->plmn);
554 if (rc)
555 return rc;
556 if (a->lac < b->lac)
557 return -1;
558 if (a->lac > b->lac)
559 return 1;
560 return 0;
561}
562
Pau Espin Pedrold426ba62021-01-22 17:44:34 +0100563/* Compare two RAI.
564 * The order of comparison is MCC, MNC, LAC, RAC. See also osmo_lai_cmp().
565 * \param a[in] "Left" side RAI.
566 * \param b[in] "Right" side RAI.
567 * \returns 0 if the RAI are equal, -1 if a < b, 1 if a > b. */
568int osmo_rai_cmp(const struct osmo_routing_area_id *a, const struct osmo_routing_area_id *b)
569{
570 int rc = osmo_lai_cmp(&a->lac, &b->lac);
571 if (rc)
572 return rc;
573 if (a->rac < b->rac)
574 return -1;
575 if (a->rac > b->rac)
576 return 1;
577 return 0;
578}
579
Neels Hofmeyrd01ef752018-09-21 15:57:26 +0200580/* Compare two CGI.
581 * The order of comparison is MCC, MNC, LAC, CI. See also osmo_lai_cmp().
582 * \param a[in] "Left" side CGI.
583 * \param b[in] "Right" side CGI.
584 * \returns 0 if the CGI are equal, -1 if a < b, 1 if a > b. */
585int osmo_cgi_cmp(const struct osmo_cell_global_id *a, const struct osmo_cell_global_id *b)
586{
587 int rc = osmo_lai_cmp(&a->lai, &b->lai);
588 if (rc)
589 return rc;
590 if (a->cell_identity < b->cell_identity)
591 return -1;
592 if (a->cell_identity > b->cell_identity)
593 return 1;
594 return 0;
595}
596
Pau Espin Pedrold426ba62021-01-22 17:44:34 +0100597/* Compare two CGI-PS.
598 * The order of comparison is MCC, MNC, LAC, RAC, CI. See also osmo_rai_cmp().
599 * \param a[in] "Left" side CGI-PS.
600 * \param b[in] "Right" side CGI-PS.
601 * \returns 0 if the CGI are equal, -1 if a < b, 1 if a > b. */
602int osmo_cgi_ps_cmp(const struct osmo_cell_global_id_ps *a, const struct osmo_cell_global_id_ps *b)
603{
604 int rc = osmo_rai_cmp(&a->rai, &b->rai);
605 if (rc)
606 return rc;
607 if (a->cell_identity < b->cell_identity)
608 return -1;
609 if (a->cell_identity > b->cell_identity)
610 return 1;
611 return 0;
612}
613
Harald Weltede1da352018-10-08 22:27:04 +0200614/*! Generate TS 23.003 Section 19.2 Home Network Realm/Domain (text form)
615 * \param out[out] caller-provided output buffer, at least 33 bytes long
616 * \param plmn[in] Osmocom representation of PLMN ID (MCC + MNC)
617 * \returns number of characters printed (excluding NUL); negative on error */
618int osmo_gen_home_network_domain(char *out, const struct osmo_plmn_id *plmn)
619{
620 if (plmn->mcc > 999)
621 return -EINVAL;
622 if (plmn->mnc > 999)
623 return -EINVAL;
624 return sprintf(out, "epc.mnc%03u.mcc%03u.3gppnetwork.org", plmn->mnc, plmn->mcc);
625}
626
627/*! Parse a TS 23.003 Section 19.2 Home Network Realm/Domain (text form) into a \ref osmo_plmn_id
628 * \param out[out] caller-allocated output structure
629 * \param in[in] character string representation to be parsed
630 * \returns 0 on success; negative on error */
631int osmo_parse_home_network_domain(struct osmo_plmn_id *out, const char *in)
632{
633 int rc;
634
635 memset(out, 0, sizeof(*out));
636 rc = sscanf(in, "epc.mnc%03hu.mcc%03hu.3gppnetwork.org", &out->mnc, &out->mcc);
637 if (rc < 0)
638 return rc;
639 if (rc != 2)
640 return -EINVAL;
641 return 0;
642}
643
644/*! Generate TS 23.003 Section 19.4.2.4 MME Domain (text form)
645 * \param out[out] caller-provided output buffer, at least 56 bytes long
646 * \param gummei[in] Structure representing the Globally Unique MME Identifier
647 * \returns number of characters printed (excluding NUL); negative on error */
648int osmo_gen_mme_domain(char *out, const struct osmo_gummei *gummei)
649{
650 char domain[GSM23003_HOME_NETWORK_DOMAIN_LEN+1];
651 int rc;
652 rc = osmo_gen_home_network_domain(domain, &gummei->plmn);
653 if (rc < 0)
654 return rc;
655 return sprintf(out, "mmec%02x.mmegi%04x.mme.%s", gummei->mme.code, gummei->mme.group_id, domain);
656}
657
658/*! Parse a TS 23.003 Section 19.4.2.4 MME Domain (text form) into a \ref osmo_gummei
659 * \param out[out] caller-allocated output GUMMEI structure
660 * \param in[in] character string representation to be parsed
661 * \returns 0 on success; negative on error */
662int osmo_parse_mme_domain(struct osmo_gummei *out, const char *in)
663{
664 int rc;
665
666 memset(out, 0, sizeof(*out));
667 rc = sscanf(in, "mmec%02hhx.mmegi%04hx.mme.epc.mnc%03hu.mcc%03hu.3gppnetwork.org",
668 &out->mme.code, &out->mme.group_id,
669 &out->plmn.mnc, &out->plmn.mcc);
670 if (rc < 0)
671 return rc;
672 if (rc != 4)
673 return -EINVAL;
674 return 0;
675}
676
677/*! Generate TS 23.003 Section 19.4.2.4 MME Group Domain (text form)
678 * \param out[out] caller-provided output buffer, at least 56 bytes long
679 * \param mmegi[in] MME Group Identifier
680 * \param plmn[in] Osmocom representation of PLMN ID (MCC + MNC)
681 * \returns number of characters printed (excluding NUL); negative on error */
682int osmo_gen_mme_group_domain(char *out, uint16_t mmegi, const struct osmo_plmn_id *plmn)
683{
684 char domain[GSM23003_HOME_NETWORK_DOMAIN_LEN+1];
685 int rc;
686 rc = osmo_gen_home_network_domain(domain, plmn);
687 if (rc < 0)
688 return rc;
689 return sprintf(out, "mmegi%04x.mme.%s", mmegi, domain);
690}