blob: 71d44fb999f4612db69913db2da89a201b0c6b65 [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
448/* Convert given 3-byte BCD buffer to integers and write results to *mcc and
449 * *mnc. The first three BCD digits result in the MCC and the remaining ones in
450 * the MNC. Return mnc_3_digits as false if the MNC's most significant digit is encoded as 0xF, true
451 * otherwise; i.e. true if MNC > 99 or if it is represented with leading zeros instead of 0xF.
452 * \param[in] bcd_src 3-byte BCD buffer containing MCC+MNC representations.
453 * \param[out] mcc MCC result buffer, or NULL.
454 * \param[out] mnc MNC result buffer, or NULL.
455 * \param[out] mnc_3_digits Result buffer for 3-digit flag, or NULL.
456 */
457void osmo_plmn_from_bcd(const uint8_t *bcd_src, struct osmo_plmn_id *plmn)
458{
459 plmn->mcc = (bcd_src[0] & 0x0f) * 100
460 + (bcd_src[0] >> 4) * 10
461 + (bcd_src[1] & 0x0f);
462
463 if ((bcd_src[1] & 0xf0) == 0xf0) {
464 plmn->mnc = (bcd_src[2] & 0x0f) * 10
465 + (bcd_src[2] >> 4);
466 plmn->mnc_3_digits = false;
467 } else {
468 plmn->mnc = (bcd_src[2] & 0x0f) * 100
469 + (bcd_src[2] >> 4) * 10
470 + (bcd_src[1] >> 4);
471 plmn->mnc_3_digits = true;
472 }
473}
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100474
475/* Convert string to MNC, detecting 3-digit MNC with leading zeros.
476 * Return mnc_3_digits as false if the MNC's most significant digit is encoded as 0xF, true
477 * otherwise; i.e. true if MNC > 99 or if it is represented with leading zeros instead of 0xF.
478 * \param mnc_str[in] String representation of an MNC, with or without leading zeros.
479 * \param mnc[out] MNC result buffer, or NULL.
480 * \param[out] mnc_3_digits Result buffer for 3-digit flag, or NULL.
481 * \returns zero on success, -EINVAL in case of surplus characters, negative errno in case of conversion
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100482 * errors. In case of error, do not modify the out-arguments.
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100483 */
484int osmo_mnc_from_str(const char *mnc_str, uint16_t *mnc, bool *mnc_3_digits)
485{
Neels Hofmeyr34907fe2021-09-05 19:50:34 +0200486 int _mnc = 0;
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100487 bool _mnc_3_digits = false;
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100488 int rc = 0;
489
Kévin Redon1af2cd52019-05-23 19:00:19 +0200490 if (!mnc_str || !isdigit((unsigned char)mnc_str[0]) || strlen(mnc_str) > 3)
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100491 return -EINVAL;
492
Neels Hofmeyr34907fe2021-09-05 19:50:34 +0200493 rc = osmo_str_to_int(&_mnc, mnc_str, 10, 0, 999);
494 /* Heed the API definition to return -EINVAL in case of surplus chars */
495 if (rc == -E2BIG)
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100496 return -EINVAL;
Neels Hofmeyr34907fe2021-09-05 19:50:34 +0200497 /* Heed the API definition to always return negative errno */
498 if (rc > 0)
499 return -rc;
500 if (rc < 0)
501 return rc;
502
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100503 _mnc_3_digits = strlen(mnc_str) > 2;
504
505 if (mnc)
506 *mnc = (uint16_t)_mnc;
507 if (mnc_3_digits)
508 *mnc_3_digits = _mnc_3_digits;
509 return rc;
510}
511
512/* Compare two MNC with three-digit flag.
513 * The mnc_3_digits flags passed in only have an effect if the MNC are < 100, i.e. if they would amount
514 * to a change in leading zeros in a BCD representation. An MNC >= 100 implies three digits, and the flag
515 * is actually ignored.
516 * \param a_mnc[in] "Left" side MNC.
517 * \param a_mnc_3_digits[in] "Left" side three-digits flag.
518 * \param b_mnc[in] "Right" side MNC.
519 * \param b_mnc_3_digits[in] "Right" side three-digits flag.
520 * \returns 0 if the MNC are equal, -1 if a < b or a shorter, 1 if a > b or a longer. */
521int osmo_mnc_cmp(uint16_t a_mnc, bool a_mnc_3_digits, uint16_t b_mnc, bool b_mnc_3_digits)
522{
523 if (a_mnc < b_mnc)
524 return -1;
525 if (a_mnc > b_mnc)
526 return 1;
527 /* a_mnc == b_mnc, but same amount of leading zeros? */
528 if (a_mnc < 100 && a_mnc_3_digits != b_mnc_3_digits)
529 return a_mnc_3_digits ? 1 : -1;
530 return 0;
531}
532
533/* Compare two PLMN.
534 * \param a[in] "Left" side PLMN.
535 * \param b[in] "Right" side PLMN.
536 * \returns 0 if the PLMN are equal, -1 if a < b or a shorter, 1 if a > b or a longer. */
537int osmo_plmn_cmp(const struct osmo_plmn_id *a, const struct osmo_plmn_id *b)
538{
539 if (a == b)
540 return 0;
541 if (a->mcc < b->mcc)
542 return -1;
543 if (a->mcc > b->mcc)
544 return 1;
545 return osmo_mnc_cmp(a->mnc, a->mnc_3_digits, b->mnc, b->mnc_3_digits);
546}
Harald Weltede1da352018-10-08 22:27:04 +0200547
Neels Hofmeyrd01ef752018-09-21 15:57:26 +0200548/* Compare two LAI.
549 * The order of comparison is MCC, MNC, LAC. See also osmo_plmn_cmp().
550 * \param a[in] "Left" side LAI.
551 * \param b[in] "Right" side LAI.
552 * \returns 0 if the LAI are equal, -1 if a < b, 1 if a > b. */
553int osmo_lai_cmp(const struct osmo_location_area_id *a, const struct osmo_location_area_id *b)
554{
555 int rc = osmo_plmn_cmp(&a->plmn, &b->plmn);
556 if (rc)
557 return rc;
558 if (a->lac < b->lac)
559 return -1;
560 if (a->lac > b->lac)
561 return 1;
562 return 0;
563}
564
Pau Espin Pedrold426ba62021-01-22 17:44:34 +0100565/* Compare two RAI.
566 * The order of comparison is MCC, MNC, LAC, RAC. See also osmo_lai_cmp().
567 * \param a[in] "Left" side RAI.
568 * \param b[in] "Right" side RAI.
569 * \returns 0 if the RAI are equal, -1 if a < b, 1 if a > b. */
570int osmo_rai_cmp(const struct osmo_routing_area_id *a, const struct osmo_routing_area_id *b)
571{
572 int rc = osmo_lai_cmp(&a->lac, &b->lac);
573 if (rc)
574 return rc;
575 if (a->rac < b->rac)
576 return -1;
577 if (a->rac > b->rac)
578 return 1;
579 return 0;
580}
581
Neels Hofmeyrd01ef752018-09-21 15:57:26 +0200582/* Compare two CGI.
583 * The order of comparison is MCC, MNC, LAC, CI. See also osmo_lai_cmp().
584 * \param a[in] "Left" side CGI.
585 * \param b[in] "Right" side CGI.
586 * \returns 0 if the CGI are equal, -1 if a < b, 1 if a > b. */
587int osmo_cgi_cmp(const struct osmo_cell_global_id *a, const struct osmo_cell_global_id *b)
588{
589 int rc = osmo_lai_cmp(&a->lai, &b->lai);
590 if (rc)
591 return rc;
592 if (a->cell_identity < b->cell_identity)
593 return -1;
594 if (a->cell_identity > b->cell_identity)
595 return 1;
596 return 0;
597}
598
Pau Espin Pedrold426ba62021-01-22 17:44:34 +0100599/* Compare two CGI-PS.
600 * The order of comparison is MCC, MNC, LAC, RAC, CI. See also osmo_rai_cmp().
601 * \param a[in] "Left" side CGI-PS.
602 * \param b[in] "Right" side CGI-PS.
603 * \returns 0 if the CGI are equal, -1 if a < b, 1 if a > b. */
604int osmo_cgi_ps_cmp(const struct osmo_cell_global_id_ps *a, const struct osmo_cell_global_id_ps *b)
605{
606 int rc = osmo_rai_cmp(&a->rai, &b->rai);
607 if (rc)
608 return rc;
609 if (a->cell_identity < b->cell_identity)
610 return -1;
611 if (a->cell_identity > b->cell_identity)
612 return 1;
613 return 0;
614}
615
Harald Weltede1da352018-10-08 22:27:04 +0200616/*! Generate TS 23.003 Section 19.2 Home Network Realm/Domain (text form)
617 * \param out[out] caller-provided output buffer, at least 33 bytes long
618 * \param plmn[in] Osmocom representation of PLMN ID (MCC + MNC)
619 * \returns number of characters printed (excluding NUL); negative on error */
620int osmo_gen_home_network_domain(char *out, const struct osmo_plmn_id *plmn)
621{
622 if (plmn->mcc > 999)
623 return -EINVAL;
624 if (plmn->mnc > 999)
625 return -EINVAL;
626 return sprintf(out, "epc.mnc%03u.mcc%03u.3gppnetwork.org", plmn->mnc, plmn->mcc);
627}
628
629/*! Parse a TS 23.003 Section 19.2 Home Network Realm/Domain (text form) into a \ref osmo_plmn_id
630 * \param out[out] caller-allocated output structure
631 * \param in[in] character string representation to be parsed
632 * \returns 0 on success; negative on error */
633int osmo_parse_home_network_domain(struct osmo_plmn_id *out, const char *in)
634{
635 int rc;
636
637 memset(out, 0, sizeof(*out));
638 rc = sscanf(in, "epc.mnc%03hu.mcc%03hu.3gppnetwork.org", &out->mnc, &out->mcc);
639 if (rc < 0)
640 return rc;
641 if (rc != 2)
642 return -EINVAL;
643 return 0;
644}
645
646/*! Generate TS 23.003 Section 19.4.2.4 MME Domain (text form)
647 * \param out[out] caller-provided output buffer, at least 56 bytes long
648 * \param gummei[in] Structure representing the Globally Unique MME Identifier
649 * \returns number of characters printed (excluding NUL); negative on error */
650int osmo_gen_mme_domain(char *out, const struct osmo_gummei *gummei)
651{
652 char domain[GSM23003_HOME_NETWORK_DOMAIN_LEN+1];
653 int rc;
654 rc = osmo_gen_home_network_domain(domain, &gummei->plmn);
655 if (rc < 0)
656 return rc;
657 return sprintf(out, "mmec%02x.mmegi%04x.mme.%s", gummei->mme.code, gummei->mme.group_id, domain);
658}
659
660/*! Parse a TS 23.003 Section 19.4.2.4 MME Domain (text form) into a \ref osmo_gummei
661 * \param out[out] caller-allocated output GUMMEI structure
662 * \param in[in] character string representation to be parsed
663 * \returns 0 on success; negative on error */
664int osmo_parse_mme_domain(struct osmo_gummei *out, const char *in)
665{
666 int rc;
667
668 memset(out, 0, sizeof(*out));
669 rc = sscanf(in, "mmec%02hhx.mmegi%04hx.mme.epc.mnc%03hu.mcc%03hu.3gppnetwork.org",
670 &out->mme.code, &out->mme.group_id,
671 &out->plmn.mnc, &out->plmn.mcc);
672 if (rc < 0)
673 return rc;
674 if (rc != 4)
675 return -EINVAL;
676 return 0;
677}
678
679/*! Generate TS 23.003 Section 19.4.2.4 MME Group Domain (text form)
680 * \param out[out] caller-provided output buffer, at least 56 bytes long
681 * \param mmegi[in] MME Group Identifier
682 * \param plmn[in] Osmocom representation of PLMN ID (MCC + MNC)
683 * \returns number of characters printed (excluding NUL); negative on error */
684int osmo_gen_mme_group_domain(char *out, uint16_t mmegi, const struct osmo_plmn_id *plmn)
685{
686 char domain[GSM23003_HOME_NETWORK_DOMAIN_LEN+1];
687 int rc;
688 rc = osmo_gen_home_network_domain(domain, plmn);
689 if (rc < 0)
690 return rc;
691 return sprintf(out, "mmegi%04x.mme.%s", mmegi, domain);
692}