Neels Hofmeyr | 7dde1f4 | 2020-05-11 19:43:20 +0200 | [diff] [blame] | 1 | /*! \file gsm23236.c |
| 2 | * Utility function implementations related to 3GPP TS 23.236 */ |
| 3 | /* |
| 4 | * (C) 2020 sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 5 | * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de> |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
| 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 Hofmeyr | 7dde1f4 | 2020-05-11 19:43:20 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <errno.h> |
| 23 | #include <stdlib.h> |
| 24 | |
| 25 | #include <osmocom/core/utils.h> |
| 26 | #include <osmocom/gsm/gsm23236.h> |
| 27 | |
| 28 | /*! Validate that the given NRI is valid for a given nri_bitlen range. |
| 29 | * \param[in] nri_v NRI value to validate. |
| 30 | * \param[in] nri_bitlen Valid NRI range in nr of bits used; if nri_bitlen > OSMO_NRI_BITLEN_MAX, nri_v is only |
| 31 | * checked to not be marked invalid. |
| 32 | * \returns 0 if valid, <0 if the NRI is <0, >0 if the NRI surpasses the range. |
| 33 | */ |
| 34 | int osmo_nri_v_validate(int16_t nri_v, uint8_t nri_bitlen) |
| 35 | { |
| 36 | if (nri_v < 0) |
| 37 | return -1; |
| 38 | if (nri_bitlen < OSMO_NRI_BITLEN_MIN) |
| 39 | return 1; |
| 40 | if (nri_bitlen < OSMO_NRI_BITLEN_MAX && (nri_v >> nri_bitlen)) |
| 41 | return 1; |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | /*! Match NRI value against a list NRI ranges. */ |
| 46 | static bool nri_v_matches_range(const struct osmo_nri_range *range, int16_t nri_v) |
| 47 | { |
| 48 | return range && nri_v >= range->first && nri_v <= range->last; |
| 49 | } |
| 50 | |
| 51 | /*! Return true if the ranges overlap, i.e. one or more NRI values appear in both ranges. */ |
| 52 | static bool nri_range_overlaps_range(const struct osmo_nri_range *a, const struct osmo_nri_range *b) |
| 53 | { |
| 54 | return nri_v_matches_range(b, a->first) || nri_v_matches_range(b, a->last) |
| 55 | || nri_v_matches_range(a, b->first) || nri_v_matches_range(a, b->last); |
| 56 | } |
| 57 | |
| 58 | /*! Return true if the ranges overlap or are directly adjacent to each other. */ |
| 59 | static bool nri_range_touches(const struct osmo_nri_range *a, const struct osmo_nri_range *b) |
| 60 | { |
| 61 | /* The first > last check may seem redundant, but ensures integer overflow safety. */ |
| 62 | return nri_range_overlaps_range(a, b) |
| 63 | || (a->first > b->last && a->first == b->last + 1) |
| 64 | || (b->first > a->last && b->first == a->last + 1); |
| 65 | } |
| 66 | |
| 67 | /*! Grow target range to also span range 'add'. Only useful for touching ranges, since all values between the two ranges |
| 68 | * are also included. */ |
| 69 | static void nri_range_extend(struct osmo_nri_range *target, const struct osmo_nri_range *add) |
| 70 | { |
| 71 | target->first = OSMO_MIN(target->first, add->first); |
| 72 | target->last = OSMO_MAX(target->last, add->last); |
| 73 | } |
| 74 | |
| 75 | /*! Return true when the given NRI value appears in the list of NRI ranges. |
| 76 | * \param[in] nri_v NRI value to look for. |
| 77 | * \param[in] nri_ranges List NRI ranges. |
| 78 | * \returns true iff nri_v appears anywhere in nri_ranges. |
| 79 | */ |
| 80 | bool osmo_nri_v_matches_ranges(int16_t nri_v, const struct osmo_nri_ranges *nri_ranges) |
| 81 | { |
| 82 | struct osmo_nri_range *range; |
| 83 | if (!nri_ranges) |
| 84 | return false; |
| 85 | llist_for_each_entry(range, &nri_ranges->entries, entry) { |
| 86 | if (nri_v_matches_range(range, nri_v)) |
| 87 | return true; |
| 88 | } |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | /*! Modulo and shift the given NRI value so that it becomes a value present in a list of NRI ranges. |
| 93 | * Only range values within nri_bitlen are used. |
| 94 | * \param[inout] nri_v The NRI value to limit, e.g. random bits or an increment counter value. |
| 95 | * \param[in] nri_ranges List of NRI ranges indicating valid NRI values, where no entries may overlap in range values, |
| 96 | * and all entries must be valid (first <= last). |
| 97 | * \returns 0 on success, negative on error. |
| 98 | */ |
| 99 | int osmo_nri_v_limit_by_ranges(int16_t *nri_v, const struct osmo_nri_ranges *nri_ranges, uint32_t nri_bitlen) |
| 100 | { |
| 101 | struct osmo_nri_range *range; |
| 102 | uint32_t total_values = 0; |
| 103 | int16_t v = *nri_v; |
| 104 | int16_t range_max = (((int16_t)1) << nri_bitlen) - 1; |
| 105 | |
| 106 | if (v < 0 || !nri_ranges) |
| 107 | return -1; |
| 108 | |
| 109 | /* Sum up total amount of range values */ |
| 110 | llist_for_each_entry(range, &nri_ranges->entries, entry) { |
| 111 | if (osmo_nri_range_validate(range, 255)) |
| 112 | return -1; |
| 113 | if (range->first > range_max) |
| 114 | continue; |
| 115 | total_values += OSMO_MIN(range_max, range->last) - range->first + 1; |
| 116 | } |
| 117 | |
| 118 | /* Modulo the given NRI value by that, and pick that nth value from the given ranges. |
| 119 | * (nri_ranges is pretty much guaranteed to be sorted and range_max checks thus would no longer be needed, but |
| 120 | * just check them anyway.) */ |
| 121 | v %= total_values; |
| 122 | llist_for_each_entry(range, &nri_ranges->entries, entry) { |
| 123 | uint32_t len; |
| 124 | if (range->first > range_max) |
| 125 | continue; |
| 126 | len = OSMO_MIN(range_max, range->last) - range->first + 1; |
| 127 | if (v < len) { |
| 128 | *nri_v = range->first + v; |
| 129 | return 0; |
| 130 | } |
| 131 | v -= len; |
| 132 | } |
| 133 | |
| 134 | /* Nothing found -- there are no entires or my math is off. */ |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | /*! Retrieve the Network Resource Indicator bits from a TMSI or p-TMSI. |
| 139 | * Useful for MSC pooling as described by 3GPP TS 23.236. |
| 140 | * \param[out] nri_v Write the extracted NRI value to this location (if non-NULL). If 0 is returned, it is guaranteed |
| 141 | * that nri_v >= 0. On non-zero return code, nri_v == -1. |
| 142 | * \param[in] tmsi TMSI value containing NRI bits. |
| 143 | * \param[in] nri_bitlen Length of the NRI value in number of bits, |
| 144 | * OSMO_NRI_BITLEN_MIN <= nri_bitlen <= * OSMO_NRI_BITLEN_MAX. |
| 145 | * \return 0 on success, negative on error (i.e. if nri_bitlen is not in the valid range). |
| 146 | */ |
| 147 | int osmo_tmsi_nri_v_get(int16_t *nri_v, uint32_t tmsi, uint8_t nri_bitlen) |
| 148 | { |
| 149 | uint8_t lowest_bit; |
| 150 | if (nri_v) |
| 151 | *nri_v = -1; |
| 152 | if (nri_bitlen < OSMO_NRI_BITLEN_MIN || nri_bitlen > OSMO_NRI_BITLEN_MAX) |
| 153 | return -1; |
| 154 | /* If not interested in the NRI value, exit here. */ |
| 155 | if (!nri_v) |
| 156 | return 0; |
| 157 | /* According to 3GPP TS 23.236, the most significant bit of the NRI is always bit 23. |
| 158 | * (So this is not a temporary placeholder 23 we sometimes like to use, it is an actually specified 23!) */ |
| 159 | lowest_bit = 23 - (nri_bitlen - 1); |
| 160 | /* ????xxxxxx??????? -> 0000000????xxxxxx tmsi >> lowest_bit |
| 161 | * -> xxxxxx & (bitmask that is nri_bitlen bits wide) |
| 162 | */ |
| 163 | *nri_v = (tmsi >> lowest_bit) & ((((uint32_t)1) << nri_bitlen) - 1); |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | /*! Write Network Resource Indicator bits into a TMSI or p-TMSI. |
| 168 | * Overwrite the NRI bits with a given NRI value in a TMSI or p-TMSI. |
| 169 | * Useful for MSC pooling as described by 3GPP TS 23.236. |
| 170 | * \param[inout] tmsi A base TMSI or p-TMSI to replace the NRI value in, result is written back to this location. |
| 171 | * \param[in] nri_v The NRI value to place in the tmsi. |
| 172 | * \param[in] nri_bitlen Length of the NRI value in number of bits, |
| 173 | * OSMO_NRI_BITLEN_MIN <= nri_bitlen <= * OSMO_NRI_BITLEN_MAX. |
| 174 | * \return 0 on success, negative on error (i.e. if nri_bitlen is not in the valid range or if tmsi is NULL). |
| 175 | */ |
| 176 | int osmo_tmsi_nri_v_set(uint32_t *tmsi, int16_t nri_v, uint8_t nri_bitlen) |
| 177 | { |
| 178 | uint8_t lowest_bit; |
| 179 | uint32_t v_mask; |
| 180 | if (nri_bitlen < OSMO_NRI_BITLEN_MIN || nri_bitlen > OSMO_NRI_BITLEN_MAX) |
| 181 | return -1; |
| 182 | if (nri_v < 0) |
| 183 | return -1; |
| 184 | if (!tmsi) |
| 185 | return -1; |
| 186 | lowest_bit = 23 - (nri_bitlen - 1); |
| 187 | v_mask = ((((uint32_t)1) << nri_bitlen) - 1) << lowest_bit; |
| 188 | *tmsi = ((*tmsi) & ~v_mask) | ((((uint32_t)nri_v) << lowest_bit) & v_mask); |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | /*! Apply osmo_nri_v_limit_by_ranges() in-place on the NRI value included in a TMSI. |
| 193 | * Extract the NRI value from the TMSI, limit that to be part of the ranges given in 'nri_ranges', and place the |
| 194 | * resulting NRI value back in the TMSI. |
| 195 | * \param[inout] tmsi TMSI value of which to modify the NRI bits, e.g. fresh randomized bits. |
| 196 | * \param[in] nri_ranges List of NRI ranges indicating valid NRI values, where no entries may overlap in range values, |
| 197 | * and all entries must be valid (first <= last). |
| 198 | * \param[in] nri_bitlen Valid NRI range in nr of bits used. |
| 199 | * \returns 0 on success, negative on error. |
| 200 | */ |
| 201 | int osmo_tmsi_nri_v_limit_by_ranges(uint32_t *tmsi, const struct osmo_nri_ranges *nri_ranges, uint8_t nri_bitlen) |
| 202 | { |
| 203 | int rc; |
| 204 | int16_t nri_v; |
| 205 | rc = osmo_tmsi_nri_v_get(&nri_v, *tmsi, nri_bitlen); |
| 206 | if (rc) |
| 207 | return rc; |
| 208 | rc = osmo_nri_v_limit_by_ranges(&nri_v, nri_ranges, nri_bitlen); |
| 209 | if (rc) |
| 210 | return rc; |
| 211 | return osmo_tmsi_nri_v_set(tmsi, nri_v, nri_bitlen); |
| 212 | } |
| 213 | |
| 214 | /*! Validate that the given NRI range is valid for a given nri_bitlen range. |
| 215 | * \param[in] nri_range NRI value range to validate. |
| 216 | * \param[in] nri_bitlen Valid NRI range in nr of bits used. If nri_bitlen > OSMO_NRI_BITLEN_MAX, the NRI range is only |
| 217 | * validated to be first <= last and non-negative, not checked to fit a bit length range, |
| 218 | * \returns 0 if valid, -1 or 1 if range->first is invalid, -2 or 2 if range->last is invalid, -3 if first > last. |
| 219 | */ |
| 220 | int osmo_nri_range_validate(const struct osmo_nri_range *range, uint8_t nri_bitlen) |
| 221 | { |
| 222 | int rc; |
| 223 | rc = osmo_nri_v_validate(range->first, nri_bitlen); |
| 224 | if (rc) |
| 225 | return rc; |
| 226 | rc = osmo_nri_v_validate(range->last, nri_bitlen); |
| 227 | if (rc) |
| 228 | return 2 * rc; |
| 229 | if (range->first > range->last) |
| 230 | return -3; |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | /*! Return true when the given NRI range has at least one NRI value that appears in a list of other NRI ranges. |
| 235 | * \param[in] range NRI range to look for. |
| 236 | * \param[in] nri_ranges List NRI ranges. |
| 237 | * \returns true iff any NRI value from 'range' appears anywhere in nri_ranges. |
| 238 | */ |
| 239 | bool osmo_nri_range_overlaps_ranges(const struct osmo_nri_range *range, const struct osmo_nri_ranges *nri_ranges) |
| 240 | { |
| 241 | struct osmo_nri_range *i; |
| 242 | if (!nri_ranges) |
| 243 | return false; |
| 244 | llist_for_each_entry(i, &nri_ranges->entries, entry) { |
| 245 | if (nri_range_overlaps_range(i, range)) |
| 246 | return true; |
| 247 | } |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | /*! Allocate an empty struct osmo_nri_ranges (list of struct osmo_nri_range). |
| 252 | * \param ctx Talloc context to allocate from. |
| 253 | * \return allocated empty list. |
| 254 | */ |
| 255 | struct osmo_nri_ranges *osmo_nri_ranges_alloc(void *ctx) |
| 256 | { |
| 257 | struct osmo_nri_ranges *nri_ranges; |
| 258 | nri_ranges = talloc_zero(ctx, struct osmo_nri_ranges); |
| 259 | OSMO_ASSERT(nri_ranges); |
| 260 | INIT_LLIST_HEAD(&nri_ranges->entries); |
| 261 | return nri_ranges; |
| 262 | } |
| 263 | |
| 264 | /*! Free a struct osmo_nri_ranges. |
| 265 | * \param nri_ranges The list to discard. |
| 266 | */ |
| 267 | void osmo_nri_ranges_free(struct osmo_nri_ranges *nri_ranges) |
| 268 | { |
| 269 | if (nri_ranges) |
| 270 | talloc_free(nri_ranges); |
| 271 | } |
| 272 | |
| 273 | /*! Insert a new struct osmo_nri_range in an osmo_nri_ranges list, so that it remains sorted by 'first' values. */ |
| 274 | static void nri_ranges_add_entry_sorted(struct osmo_nri_ranges *nri_ranges, struct osmo_nri_range *add) |
| 275 | { |
| 276 | struct osmo_nri_range *r; |
| 277 | struct llist_head *at_pos; |
| 278 | OSMO_ASSERT(nri_ranges); |
| 279 | at_pos = nri_ranges->entries.prev; |
| 280 | llist_for_each_entry(r, &nri_ranges->entries, entry) { |
| 281 | if (r->first <= add->first) |
| 282 | continue; |
| 283 | at_pos = r->entry.prev; |
| 284 | break; |
| 285 | } |
| 286 | llist_add(&add->entry, at_pos); |
| 287 | } |
| 288 | |
| 289 | /*! Add a range of NRI values to a list of nri_range structs. |
| 290 | * Intelligently add and/or combine the entries in a list of NRI ranges to also include the NRI range given in 'add'. |
| 291 | * The list remains sorted by 'first' values. |
| 292 | * \param[inout] nri_ranges List of talloc allocated struct osmo_nri_range entries to add the new range to. |
| 293 | * \param[in] add NRI range to add to 'nri_ranges'. |
| 294 | * \returns 0 on success, negative on error (if the range in 'add' is invalid). |
| 295 | */ |
| 296 | int osmo_nri_ranges_add(struct osmo_nri_ranges *nri_ranges, const struct osmo_nri_range *add) |
| 297 | { |
| 298 | struct osmo_nri_range *range; |
| 299 | struct osmo_nri_range *range_next; |
| 300 | struct osmo_nri_range *target = NULL; |
| 301 | |
| 302 | if (osmo_nri_range_validate(add, 255)) |
| 303 | return -1; |
| 304 | if (!nri_ranges) |
| 305 | return -1; |
| 306 | |
| 307 | /* Is there an entry overlapping this range? */ |
| 308 | llist_for_each_entry(range, &nri_ranges->entries, entry) { |
| 309 | if (!nri_range_touches(range, add)) |
| 310 | continue; |
| 311 | target = range; |
| 312 | } |
| 313 | |
| 314 | if (!target) { |
| 315 | /* No overlaps with existing ranges, create a new one. */ |
| 316 | target = talloc_zero(nri_ranges, struct osmo_nri_range); |
| 317 | OSMO_ASSERT(target); |
| 318 | *target = *add; |
| 319 | nri_ranges_add_entry_sorted(nri_ranges, target); |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | /* Overlap found, join into existing entry */ |
| 324 | nri_range_extend(target, add); |
| 325 | |
| 326 | /* Remove redundant entries */ |
| 327 | llist_for_each_entry_safe(range, range_next, &nri_ranges->entries, entry) { |
| 328 | if (range == target) |
| 329 | continue; |
| 330 | if (!nri_range_touches(target, range)) |
| 331 | continue; |
| 332 | nri_range_extend(target, range); |
| 333 | llist_del(&range->entry); |
| 334 | talloc_free(range); |
| 335 | } |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | /*! Remove a range of NRI values from a list of nri_range structs. |
| 340 | * Intelligently drop and/or cut or split the entries in a list of NRI ranges to no longer include the NRI range given |
| 341 | * in 'del'. Note that after this, the list may have more entries than before, if a range was split into two smaller |
| 342 | * ranges. |
| 343 | * \param[inout] nri_ranges List of talloc allocated struct osmo_nri_range entries to remove values from. |
| 344 | * \param[in] del NRI range to remove from 'nri_ranges'. |
| 345 | * \returns 0 on success, negative on error (if the range in 'del' is invalid). |
| 346 | */ |
| 347 | int osmo_nri_ranges_del(struct osmo_nri_ranges *nri_ranges, const struct osmo_nri_range *del) |
| 348 | { |
| 349 | struct osmo_nri_range *range; |
| 350 | struct osmo_nri_range *range_next; |
| 351 | |
| 352 | if (osmo_nri_range_validate(del, 255)) |
| 353 | return -1; |
| 354 | if (!nri_ranges) |
| 355 | return -1; |
| 356 | |
| 357 | llist_for_each_entry_safe(range, range_next, &nri_ranges->entries, entry) { |
| 358 | bool head; |
| 359 | bool tail; |
| 360 | if (!nri_range_overlaps_range(range, del)) |
| 361 | continue; |
| 362 | |
| 363 | head = nri_v_matches_range(range, del->first) && (del->first > range->first); |
| 364 | tail = nri_v_matches_range(range, del->last) && (del->last < range->last); |
| 365 | |
| 366 | if (head && tail) { |
| 367 | /* Range cut in two */ |
| 368 | struct osmo_nri_range *new_tail; |
| 369 | |
| 370 | /* Add a new entry for the tail section */ |
| 371 | new_tail = talloc_zero(nri_ranges, struct osmo_nri_range); |
| 372 | OSMO_ASSERT(new_tail); |
| 373 | *new_tail = (struct osmo_nri_range){ |
| 374 | .first = del->last + 1, |
| 375 | .last = range->last, |
| 376 | }; |
| 377 | llist_add(&new_tail->entry, &range->entry); |
| 378 | |
| 379 | /* Existing entry becomes the head section */ |
| 380 | range->last = del->first - 1; |
| 381 | } else if (head) { |
| 382 | /* Range reduced, a head remains */ |
| 383 | range->last = del->first - 1; |
| 384 | } else if (tail) { |
| 385 | /* Range reduced, a tail remains */ |
| 386 | range->first = del->last + 1; |
| 387 | } else { |
| 388 | /* nothing remains */ |
| 389 | llist_del(&range->entry); |
| 390 | talloc_free(range); |
| 391 | } |
| 392 | } |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | /*! Compose a human readable representation of a list of NRI ranges in a buffer, like "23..42,123..142". |
| 397 | * \param[out] buf Target buffer. |
| 398 | * \param[in] buflen sizeof(buf). |
| 399 | * \param[in] nri_ranges List NRI ranges. |
| 400 | * \returns strlen() of string that would be written if the buffer is large enough, like snprintf(). |
| 401 | */ |
| 402 | int osmo_nri_ranges_to_str_buf(char *buf, size_t buflen, const struct osmo_nri_ranges *nri_ranges) |
| 403 | { |
| 404 | struct osmo_nri_range *range; |
| 405 | struct osmo_strbuf sb = { .buf = buf, .len = buflen }; |
| 406 | bool first = true; |
| 407 | if (!nri_ranges || llist_empty(&nri_ranges->entries)) { |
| 408 | OSMO_STRBUF_PRINTF(sb, "empty"); |
| 409 | return sb.chars_needed; |
| 410 | } |
| 411 | llist_for_each_entry(range, &nri_ranges->entries, entry) { |
| 412 | OSMO_STRBUF_PRINTF(sb, "%s%d..%d", first ? "" : ",", range->first, range->last); |
| 413 | } |
| 414 | return sb.chars_needed; |
| 415 | } |
| 416 | |
| 417 | /*! Compose a human readable representation of a list of NRI ranges in a talloc buffer, like "23..42,123..142". |
| 418 | * \param[in] ctx Talloc context. |
| 419 | * \param[in] nri_ranges List of NRI ranges. |
| 420 | * \returns a talloc allocated string. |
| 421 | */ |
| 422 | char *osmo_nri_ranges_to_str_c(void *ctx, const struct osmo_nri_ranges *nri_ranges) |
| 423 | { |
| 424 | OSMO_NAME_C_IMPL(ctx, 16, "ERROR", osmo_nri_ranges_to_str_buf, nri_ranges); |
| 425 | } |
| 426 | |
| 427 | /*! Parse a string to an NRI value, allowing both decimal and hexadecimal formats; useful for VTY config |
| 428 | * implementations. |
| 429 | * \param[out] dst Write the resulting NRI value to this location. |
| 430 | * \param[in] str Decimal "511" or hex "0x1ff" string to parse. |
| 431 | * \returns 0 on success, negative on error. |
| 432 | */ |
| 433 | static int osmo_nri_parse(int16_t *dst, const char *str) |
| 434 | { |
Neels Hofmeyr | 34907fe | 2021-09-05 19:50:34 +0200 | [diff] [blame] | 435 | int val; |
Neels Hofmeyr | 7dde1f4 | 2020-05-11 19:43:20 +0200 | [diff] [blame] | 436 | int base = 10; |
Neels Hofmeyr | 34907fe | 2021-09-05 19:50:34 +0200 | [diff] [blame] | 437 | if (osmo_str_startswith(str, "0x")) |
Neels Hofmeyr | 7dde1f4 | 2020-05-11 19:43:20 +0200 | [diff] [blame] | 438 | base = 16; |
Neels Hofmeyr | 34907fe | 2021-09-05 19:50:34 +0200 | [diff] [blame] | 439 | if (osmo_str_to_int(&val, str, base, 0, INT16_MAX)) |
Neels Hofmeyr | 7dde1f4 | 2020-05-11 19:43:20 +0200 | [diff] [blame] | 440 | return -1; |
Neels Hofmeyr | 34907fe | 2021-09-05 19:50:34 +0200 | [diff] [blame] | 441 | *dst = (int16_t)val; |
Neels Hofmeyr | 7dde1f4 | 2020-05-11 19:43:20 +0200 | [diff] [blame] | 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | /*! Parse string arguments to a struct osmo_nri_range; useful for VTY config implementations. |
| 446 | * Validate and parse 'first' and optional 'last' string arguments into struct osmo_nri_range values. |
| 447 | * The strings may be in decimal format ("511") or hexadecimal with leading "0x" ("0x1ff"). |
| 448 | * If only one of 'first'/'last' is provided, the resulting range will have only that value (first == last). |
| 449 | * \param[out] nri_range Target for parsed values. |
| 450 | * \param[in] first_str Decimal or hex string, representing the first value in the range, or NULL if omitted. |
| 451 | * \param[in] last_str Decimal or hex string, representing the last value in the range, or NULL if omitted. |
| 452 | * \returns 0 on success, negative on error. |
| 453 | */ |
| 454 | static int osmo_nri_parse_range(struct osmo_nri_range *nri_range, const char *first_str, const char *last_str) |
| 455 | { |
| 456 | if (!nri_range) |
| 457 | return -1; |
| 458 | if (!first_str) { |
| 459 | first_str = last_str; |
| 460 | last_str = NULL; |
| 461 | if (!first_str) |
| 462 | return -1; |
| 463 | } |
| 464 | if (osmo_nri_parse(&nri_range->first, first_str)) |
| 465 | return -1; |
| 466 | nri_range->last = nri_range->first; |
| 467 | if (last_str) { |
| 468 | if (osmo_nri_parse(&nri_range->last, last_str)) |
| 469 | return -1; |
| 470 | } |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | /*! VTY implementation for adding an NRI range to a list of ranges. |
| 475 | * Parse one or, if present, two argv arguments, which must be numbers representing the first and last value to add to |
| 476 | * the list of NRI ranges, in decimal format ("511") or hexadecimal with leading "0x" ("0x1ff"). If the range values |
| 477 | * surpass the nri_bitlen, return a warning in 'message', but still add the values to the list. |
| 478 | * \param[out] message Returned string constant to alert the user with, or NULL if all is well. |
| 479 | * \param[out] added_range If not NULL, write the range parsing result to this location. |
| 480 | * \param[in] nri_ranges List NRI ranges to add to. |
| 481 | * \param[in] argc Argument count. |
| 482 | * \param[in] argv Argument list. |
| 483 | * \param[in] nri_bitlen Valid NRI range in nr of bits used. |
| 484 | * \returns 0 on success, -1 on error, 1 for a warning (if adding was successful but the added range surpasses |
| 485 | * nri_bitlen). |
| 486 | */ |
| 487 | int osmo_nri_ranges_vty_add(const char **message, struct osmo_nri_range *added_range, |
| 488 | struct osmo_nri_ranges *nri_ranges, int argc, const char **argv, uint8_t nri_bitlen) |
| 489 | { |
| 490 | struct osmo_nri_range add_range; |
| 491 | if (osmo_nri_parse_range(&add_range, argv[0], argc > 1 ? argv[1] : NULL)) { |
| 492 | *message = "Error: cannot parse NRI range"; |
| 493 | return -1; |
| 494 | } |
| 495 | |
| 496 | if (added_range) |
| 497 | *added_range = add_range; |
| 498 | |
| 499 | if (osmo_nri_ranges_add(nri_ranges, &add_range)) { |
| 500 | *message = "Error: failed to add NRI range"; |
| 501 | return -1; |
| 502 | } |
| 503 | |
| 504 | if (nri_bitlen <= OSMO_NRI_BITLEN_MAX && osmo_nri_range_validate(&add_range, nri_bitlen)) { |
| 505 | *message = "Warning: NRI range surpasses current NRI bitlen"; |
| 506 | return 1; |
| 507 | } |
| 508 | |
| 509 | *message = NULL; |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | /*! VTY implementation for removing an NRI range from a list of ranges. |
| 514 | * Parse one or, if present, two argv arguments, which must be numbers representing the first and last value to remove |
| 515 | * from the list of NRI ranges, in decimal format ("511") or hexadecimal with leading "0x" ("0x1ff"). |
| 516 | * \param[out] message Returned string constant to alert the user with, or NULL if all is well. |
| 517 | * \param[out] removed_range If not NULL, write the range parsing result to this location. |
| 518 | * \param[in] nri_ranges List of NRI ranges to remove from. |
| 519 | * \param[in] argc Argument count. |
| 520 | * \param[in] argv Argument list. |
| 521 | * \returns 0 on success, -1 on error, 1 for a warning. |
| 522 | */ |
| 523 | int osmo_nri_ranges_vty_del(const char **message, struct osmo_nri_range *removed_range, |
| 524 | struct osmo_nri_ranges *nri_ranges, int argc, const char **argv) |
| 525 | { |
| 526 | struct osmo_nri_range del_range; |
| 527 | if (osmo_nri_parse_range(&del_range, argv[0], argc > 1 ? argv[1] : NULL)) { |
| 528 | *message = "Error: cannot parse NRI range"; |
| 529 | return -1; |
| 530 | } |
| 531 | |
| 532 | if (removed_range) |
| 533 | *removed_range = del_range; |
| 534 | |
| 535 | if (osmo_nri_ranges_del(nri_ranges, &del_range)) { |
| 536 | *message = "Error: failed to remove NRI range"; |
| 537 | return -1; |
| 538 | } |
| 539 | |
| 540 | *message = NULL; |
| 541 | return 0; |
| 542 | } |