Commit Graph

79 Commits

Author SHA1 Message Date
Vadim Yanitskiy c797e25f3e core/utils.h: make use of OSMO_LIKELY in OSMO_ASSERT
Theoretically, this should improve performance of the code where
we frequently invoke OSMO_ASSERT(), like osmo-msc and osmo-bsc.

Change-Id: I29b32a2477ec92762f8f0ce5e5c5a30810f6abbe
2022-02-09 10:05:25 +00:00
Vadim Yanitskiy 9ac355ad51 core/utils.h: wrap OSMO_ASSERT() with do { ... } while (0)
Using do-while is a common practice for complex macros.  This allows
invoking OSMO_ASSERT in simple if-else statements without braces.

Change-Id: I42d9c315c9c30bce828564a63c496ee62e5c1431
2022-02-08 12:50:48 +06:00
Vadim Yanitskiy e59e839dd9 core/utils.h: add OSMO_LIKELY / OSMO_UNLIKELY macros
These macros are built on top of the __builtin_expect() function [1],
which provides the compiler with branch prediction information.

Similar macros exist in the Linux kernel: likely() / unlikely().

[1] https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html

Change-Id: I0b029654ba050f079eed4a0574a3fa8019677067
2022-02-08 12:48:28 +06:00
Neels Hofmeyr 1633735e9b add osmo_quote_str_buf3, osmo_escape_str_buf3
There already are osmo_quote_str_buf() and osmo_quote_str_buf2(), same
for _escape_, but none of them return the snprintf() like string length.
A private function does, publish this in the API.

The returned chars_needed is required to accurately allocate sufficient
size in string functions that call osmo_quote_str/osmo_escape_str. I am
adding such in osmo-upf.git.

Related: SYS#5599
Change-Id: I05d75a40599e3133da099a11e8babaaad0e9493a
2022-02-07 22:10:29 +01:00
Harald Welte 7d6166a0e3 utils: Fix -Wsign-compare warnings
Change-Id: I8b1118ca519b0a419a42eab8b7d4ba9e26a0bab5
2022-01-09 12:04:19 +01:00
Vadim Yanitskiy c549719088 utils: introduce osmo_talloc_replace_string_fmt()
Change-Id: I6b84fa0525555a98c531fc558e5dc1298fec00c1
2021-10-13 14:03:26 +03:00
Neels Hofmeyr 47773344fb utils: add osmo_str_to_int() and osmo_str_to_int64()
Properly converting a string to an integer while validating against all
possible errors is not trivial. It is a recurring theme in code review,
and there are places in osmo code that do it wrong.
End this by providing a simple API, if for nothing else then as an
example of how to use strol() / strtoul() / strtoll() / strtoull()
in an airtight way.

A subsequent patch, adding stat items to the CTRL interface, uses this
to properly validate indexes in CTRL variables and convert them to int.

Related: SYS#5542
Change-Id: I4dac826aab00bc1780a5258b6b55d34ce7d50c60
2021-09-12 21:24:50 +02:00
Pau Espin 7af860fb78 utils: Fix c++ warn in OSMO_STRBUF_APPEND
It's really a false positive since _sb_l is compared and granted to be
psotivie by the time we compare, so we don't really care, but c++ is not
happy about it.

"""
/osmocom/core/utils.h:227:40: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
  227 |                 if (_sb_l < 0 || _sb_l > _sb_remain) \
      |                                  ~~~~~~^~~~~~~~~~~~
"""

Change-Id: I90e7374aa959468670f1c0ea65a427398d423ddb
2021-07-28 20:14:14 +00:00
Neels Hofmeyr 87c3afb5a9 add osmo_float_str_to_int() and osmo_int_to_float_str_*()
This will be useful to handle latitude and longitude numbers for GAD, which is
the location estimate representation used for LCS (Location Services).

The OsmoSMLC VTY user interface will provide floating-point strings like
"23.456" while GAD stores them as micro-degress 23456000. The osmo_gad_to_str*
will also convert latitude and longitude to floating-point string.

There was code review concerns against adding this API, upon which I tried to
use floating point string formats. But I encountered various problems with
accuracy and trailing zeros. For global positioning data (latitude and
longitude), even inaccuracy on the sixth significant decimal digit causes
noticeable positional shift. To achieve sufficient accuracy on the least
significant end, I need to use double instead of float. To remove trailing
zeros, the idea was to use '%.6g' format, but that can cause rounding. '%.6f'
on a double looks ok, but always includes trailing zeros. A test program shows:

 %.6g of ((double)(int32_t)23230100)/1e6 = "23.2301"     <-- good
 %.6g of ((double)(int32_t)42419993)/1e6 = "42.42"       <-- bad rounding
 %.6g of ((double)(int32_t)23230199)/1e6 = "23.2302"     <-- bad rounding

 %.6f of ((double)(int32_t)23230100)/1e6 = "23.230100"   <-- trailing zeros
 %.6f of ((double)(int32_t)42419993)/1e6 = "42.419993"   <-- good
 %.6f of ((double)(int32_t)23230199)/1e6 = "23.230199"   <-- good

It looks like when accepting that there will be trailing zeros, using double
with '%.6f' would work out, but in the end I am not certain enough that there
aren't more hidden rounding / precision glitches. Hence I decided to reinforce
the need to add this API: it is glitch free in sufficient precision for
latitude and longitude data, because it is based on integer arithmetic.

The need for this precision is particular to the (new) OsmoSMLC vty
configuration, where reading and writing back user config must not modify the
values the user entered. Considering to add these functions to osmo-smlc.git,
we might as well add them here to libosmocore utils, and also use them in
osmo_gad_to_str_*() functions.

Change-Id: Ib9aee749cd331712a4dcdadfb6a2dfa4c26da957
2020-10-07 11:39:46 +00:00
Neels Hofmeyr 83025bf1a6 add osmo_mobile_identity API
Implement better API around 3GPP TS 24.008 Mobile Identity coding.

struct osmo_mobile_identity is a decoded representation of the raw Mobile
Identity, with a string representation as well as dedicated raw uint32_t TMSI.
The aim is to remove all uncertainty about decoded buffer sizes / data types.

I have patches ready for current osmo CNI programs, replacing the Mobile
Identity coding with this new API. Deprecate the old MI API.
osmo-bsc: I71c3b4c65dbfdfa51409e09d4868aea83225338a
osmo-msc: Ic3f969e739654c1e8c387aedeeba5cce07fe2307
osmo-sgsn: I4cacb10bac419633ca0c14f244f9903f7f517b49
Note that some GPRS and SGs related coding is done here in libosmocore and
hence currently remains using the old implementation (see previous version of
this patch: Ic3f969e739654c1e8c387aedeeba5cce07fe2307).

New API functions provide properly size-checking implementations of:
- decoding a raw MI from a bunch of MI octets;
- locating and decoding MI from a full 3GPP TS 24.008 Complete Layer 3 msgb;
- encoding to a buffer;
- encoding to the end of a msgb.

Other than the old gsm48_generate_mid(), omit a TLV tag and length from
encoding. Many callers manually stripped the tag and value after calling
gsm48_generate_mid(). The aim is to leave writing a TL to the caller entirely,
especially since some callers need to use a TvL, i.e. support a variable-size
length of 8 or 16 bit.

New validity checks so far not implemented anywhere else:
- stricter validation of number of digits of IMSI, IMEI, IMEI-SV MI.
- stricter on filler nibbles to be 0xf.
As a result, applications using osmo_mobile_identity will be stricter in
rejecting coding mistakes (some of which we currently have in our test suites,
and which we'll need to fix).

Rationale:

While implementing osmo-bsc's MSC pooling feature in osmo-bsc, this API will be
used to reduce the number of times a Mobile Identity is extracted from a raw
RSL message.

Extracting the Mobile Identity from messages has numerous duplicate
implementations across our code with various levels of specialization.
https://xkcd.com/927/

To name a few:
- libosmocore: gsm48_mi_to_string(), osmo_mi_name_buf()
- osmo-bsc: extract_sub()
- osmo-msc: mm_rx_loc_upd_req(), cm_serv_reuse_conn(), gsm48_rx_mm_serv_req(),
  vlr_proc_acc_req()

We have existing functions to produce a human readable string from a Mobile
Identity, more or less awkward:
- gsm48_mi_to_string() decodes a TMSI as a decimal number. These days we use
  hexadecimal TMSI everywhere.
- osmo_mi_name_buf() decodes the BCD digits from a raw MI every time, so we'd
  need to pass around the raw message bytes. Also, osmo_mi_name_buf() has the
  wrong signature, it should return a length like snprintf().
- osmo-bsc's extract_sub() first uses gsm48_mi_to_string() which encodes the
  raw uint32_t TMSI to a string, and then calls strtoul() via
  tmsi_from_string() to code those back to a raw uint32_t.

Each of the above implementations employ their own size overflow checks, each
invoke osmo_bcd2str() and implement their own TMSI osmo_load32be() handling.
Too much code dup, let's hope that each and every one is correct.

In osmo-bsc, I am now implementing MSC pooling, and need to extract NRI bits
from a TMSI Mobile Identity. Since none of the above functions are general
enough to be re-used, I found myself again copy-pasting Mobile Identity code:
locating the MI in a 24.008 message with proper size checks, decoding MI
octets.

This time I would like it to become a generally re-usable API.

This patch was first merged as Ic3f969e739654c1e8c387aedeeba5cce07fe2307 and
caused test fallout, because it re-implemented old API with the new stricter
decoding. In this patch version, old API remains 1:1 unchanged to avoid such
fallout. Applications will soon switch to the new osmo_mobile_identity API and
become stricter on MI coding when that happens, not implicitly by a new
libosmocore version.

Change-Id: If4f7be606e54cfa1c59084cf169785b1cbda5cf5
2020-06-16 15:17:48 +02:00
Harald Welte a13fb75030 Revert "add osmo_mobile_identity API"
This reverts commit d1ceca9d48, as it
introduces regressions in both osmo-msc and osmo-nitb which have been
causing failing builds for several days now.

Change-Id: I4bd958d0cd2ab4b0c4725e6d114f4404d725fcf7
2020-06-16 09:21:08 +02:00
Neels Hofmeyr d1ceca9d48 add osmo_mobile_identity API
Implement better API around 3GPP TS 24.008 Mobile Identity coding.

struct osmo_mobile_identity is a decoded representation of the raw Mobile
Identity, with a string representation as well as dedicated raw uint32_t TMSI.
The aim is to remove all uncertainty about decoded buffer sizes / data types.

I have patches ready for all osmo programs, completely replacing the Mobile
Identity coding with this new API. Hence deprecate the old MI API.

New API functions provide properly size-checking implementations of:
- decoding a raw MI from a bunch of MI octets;
- locating and decoding MI from a full 3GPP TS 24.008 Complete Layer 3 msgb;
- encoding to a buffer;
- encoding to the end of a msgb.

Other than the old gsm48_generate_mid(), omit a TLV tag and length from
encoding. Many callers manually stripped the tag and value after calling
gsm48_generate_mid(). The aim is to leave writing a TL to the caller entirely,
especially since some callers need to use a TvL, i.e. support a variable-size
length of 8 or 16 bit.

New validity checks so far not implemented anywhere else:
- stricter validation of number of digits of IMSI, IMEI, IMEI-SV MI.
- stricter on filler nibbles to be 0xf.

Rationale:

While implementing osmo-bsc's MSC pooling feature in osmo-bsc, this API will be
used to reduce the number of times a Mobile Identity is extracted from a raw
RSL message.

Extracting the Mobile Identity from messages has numerous duplicate
implementations across our code with various levels of specialization.
https://xkcd.com/927/

To name a few:
- libosmocore: gsm48_mi_to_string(), osmo_mi_name_buf()
- osmo-bsc: extract_sub()
- osmo-msc: mm_rx_loc_upd_req(), cm_serv_reuse_conn(), gsm48_rx_mm_serv_req(),
  vlr_proc_acc_req()

We have existing functions to produce a human readable string from a Mobile
Identity, more or less awkward:
- gsm48_mi_to_string() decodes a TMSI as a decimal number. These days we use
  hexadecimal TMSI everywhere.
- osmo_mi_name_buf() decodes the BCD digits from a raw MI every time, so we'd
  need to pass around the raw message bytes. Also, osmo_mi_name_buf() has the
  wrong signature, it should return a length like snprintf().
- osmo-bsc's extract_sub() first uses gsm48_mi_to_string() which encodes the
  raw uint32_t TMSI to a string, and then calls strtoul() via
  tmsi_from_string() to code those back to a raw uint32_t.

Each of the above implementations employ their own size overflow checks, each
invoke osmo_bcd2str() and implement their own TMSI osmo_load32be() handling.
Too much code dup, let's hope that each and every one is correct.

In osmo-bsc, I am now implementing MSC pooling, and need to extract NRI bits
from a TMSI Mobile Identity. Since none of the above functions are general
enough to be re-used, I found myself again copy-pasting Mobile Identity code:
locating the MI in a 24.008 message with proper size checks, decoding MI
octets.

This time I would like it to become a generally re-usable API.

Change-Id: Ic3f969e739654c1e8c387aedeeba5cce07fe2307
2020-06-12 16:35:26 +02:00
Neels Hofmeyr dacac990f9 api doc: clarify OSMO_NAME_C_IMPL() required FUNC_BUF signature
Change-Id: Ibe722d38d28e5590a35e856dd15c2538e6ee0a3e
2020-05-26 22:30:49 +02:00
Pau Espin cc0b5d527d value_string: Switch val from unsigned to int
There's nothing really preventing a user from user negative values.

Otherwise if we keep it like this then g++ is not happy when passing eg.
{ -2, "foobar" } when initializing a value_string array.

Change-Id: I754fa7e054cb89801ef82edc82199dcfbe59c6ab
2020-02-04 15:22:19 +00:00
Neels Hofmeyr 8a7eed50db add osmo_escape_cstr and osmo_quote_cstr
Provide string escaping that
- returns the required buffer size, so it can be used with OSMO_STRBUF_APPEND().
- uses C compatible string constant escaping sequences.

This is intended as a replacement for all previous osmo_escape_str* and
osmo_quote_str* API. It pains me that I didn't get them right the first nor the
second time:
- The buffer functions do not return the chars needed, which is required for
  allocating sufficient memory in the *_c versions of the functions.
- Because of that, these functions are accurately usable for
  OSMO_STRBUF_APPEND(), producing truncated strings, for example when dumping a
  GSUP message.
- They do not use the C equivalent string constant escaping: for some reason I
  thought "\15" would be valid, but it should be "\x0f".
If I could, I would completely drop those mislead implementations ... but
backwards compat prohibits that.

A previous patch already provided internal static functions that accurately
return the required buffer size. Enhance these to also support C compatible
string escaping, and use them as implementation of the new functions:

osmo_escape_cstr_buf()
osmo_escape_cstr_c()
osmo_quote_cstr_buf()
osmo_quote_cstr_c()

In the tests for these, also test C string equivalence.

Naming: from API versions, it would be kind of logical to call them
osmo_escape_str_buf3() and osmo_escape_str_c2(). Since these anyway return a
different escaping, it makes sense to me to have distinct names instead.

Quasi missing are variants of the non-C-compatible weird legacy escaping that
return the required buffer size, but I refrain from adding those, because we
have enough API cruft as it is. Just always use these new cstr variants.

Change-Id: I3dfb892036e01000033dd8e7e4a6a0c32a3caa9b
2019-11-24 19:59:35 +01:00
Neels Hofmeyr 06356fd9c3 utils: add osmo_strnchr()
When finding a char in a string, I want to be able to limit the search area by
size, not only by nul terminator.

Change-Id: I48f8ace9f51f8a06796648883afcabe3b4e8b537
2019-11-24 19:59:35 +01:00
Neels Hofmeyr 823073aa91 utils.h: add OSMO_NAME_C_IMPL() macro
Provide a common implementation for foo_name_c() functions that base on
foo_name_buf() functions.

  char *foo_name_c(void *ctx, example_t arg)
  {
          OSMO_NAME_C_IMPL(ctx, 64, "ERROR", foo_name_buf, arg)
  }

Rationale: the most efficient way of composing strings that have optional parts
or require loops for composition is by writing to a ready char[], and this in
turn is easiest done by using OSMO_STRBUF_* API. Using such a basic name string
implementation which typically returns a length, I often want a more convenient
version that returns a char*, which can just be inlined in a "%s" string format
-- crucially: skipping string composition when inlined in a LOGP(). This common
implementation allows saving code dup, only the function signature is needed.

Why not include the function signature in the macro? The two sets of varargs
(1: signature args, 2: function call args) are hard to do. Also, having an
explicit signature is good for readability and code grepping / ctags.

Upcoming uses: in libosmocore in the mslookup (D-GSM) implementation
(osmo_mslookup_result_name_c()), and in osmo_msc's codec negotiation
implementation (sdp_audio_codecs_name_c(), sdp_msg_name_c(), ...).
I54b6c0810f181259da307078977d9ef3d90458c9 (libosmocore)
If3ce23cd5bab15e2ab4c52ef3e4c75979dffe931 (osmo-msc)

Change-Id: Ida5ba8d9640ea641aafef0236800f6d489d3d322
2019-11-23 07:58:47 +00:00
Neels Hofmeyr 002a51d731 add osmo_sockaddr_str_cmp()
Currently planned user: for Distributed GSM in osmo-hlr: setting per-MSC
service addresses in VTY: replace/remove existing entries.

osmo_sockaddr_str_cmp() is useful to catch identical resulting IP addresses,
regardless of differing strings (e.g. '0::' and '::' are equal but differ in
strings).

Change-Id: I0dbc1cf707098dcda75f8e07c1b936951f9f9501
2019-11-21 21:17:12 +01:00
Vadim Yanitskiy e2425485c4 Revert "utils.h: require a semi colon after OSMO_ASSERT"
This reverts commit 4e284b6379.

Unfortunately, some projects such as OsmoMSC, OsmoBTS and OpenBSC
do contain OSMO_ASSERT statements without a semi colon. Thus,
this change causes compilation errors when building them.

Please note that only the OSMO_ASSERT's definition is reverted,
while changes to other files (adding missing semicolons) are kept.

Change-Id: I6da4d7397d993f6c1af658cb5ae1e49c92a1b350
2019-07-09 14:05:19 +07:00
Alexander Couzens 4e284b6379 utils.h: require a semi colon after OSMO_ASSERT
When using `OSMO_ASSERT(exp);` clang will warn about
an empty expression because the semi colon was superflous.
Use do {} while (0) to enfore the need of a semi colon.
This might break other test.

Change-Id: I2272d29a81496164bebd1696a694383a28a86434
2019-07-08 13:23:45 +00:00
Kevin Redon 77021c7bec (minor) fix typo in comments
Change-Id: I697af428a2ea9a0ccd3f04ba8ec4664935ae29f8
2019-06-13 18:13:34 +02:00
Kevin Redon d1e220fb67 minor: don't redefine macros
the DEBUG macro name and ARRAY_SIZE macro function are frequently
used in other projects. If these projects also use libosmocore,
the macros will be redefined. This also generates a warning message
during compilation.
Not redefining the macros removes the warning message and possible
(but unlikely) mis-redefinition.

Change-Id: I0ba91eae8eacc5542d1647601b372e417ed1713c
2019-06-13 13:49:30 +00:00
Vadim Yanitskiy d9fc6041e7 core/utils: drop meaningless const from return value of osmo_luhn()
Change-Id: I085da06f31a0a6862ae2ba041fafc134cc240f7e
2019-06-12 11:29:06 +00:00
Neels Hofmeyr b480b74192 add identifier sanitation for setting FSM instance ids
We often compose FSM instance IDs from context information, for example placing
an MSISDN string or IP:port information in the FSM instance id, using
osmo_fsm_inst_update_id_f(). This fails if any characters are contained that
don't pass osmo_identifier_valid(). Hence it is the task of the caller to make
sure only characters allowed in an FSM id are applied.

Provide API to trivially allow this by replacing illegal chars:
- osmo_identifier_sanitize_buf(), with access to the same set of illegal
  characters defined in utils.c,
- osmo_fsm_inst_update_id_f_sanitize() implicitly replaces non-identifier
  chars.

This makes it easy to add strings like '192.168.0.1:2342' or '+4987654321' to
an FSM instance id, without adding string mangling to each place that sets an
id; e.g. replacing with '-' to yield '192-168-0-1:2342' or '-4987654321'.

Change-Id: Ia40a6f3b2243c95fe428a080b938e11d8ab771a7
2019-04-12 01:00:16 +02:00
Neels Hofmeyr ecef7ec3c3 add osmo_{escape,quote}_str_buf2() for standard args ordering
To be able to append an escaped or quoted string using
OSMO_STRBUF_APPEND_NOLEN(), the function signature must have the buf and len as
first args, like most other *_buf() functions.

Add osmo_escape_str_buf2() and osmo_quote_str_buf2() to match this signature.

A recent patch [1] has changed the return value of osmo_escape_str_buf() to
char*, removing the const. However, the functions may return const strings,
hence re-add the const. The new signatures always return the non-const buffer.

To avoid code duplication, implement osmo_quote_str_buf() and
osmo_escape_str_buf() by calling the new functions.

I decided to allow slight changes to the behavior for current osmo_escape_str()
and osmo_escape_str_buf(), because impact on callers is minimal:

(1) The new implementation uses OSMO_STRBUF_*, and in consequence
osmo_quote_str() no longer prints an ending double quote after truncated
strings; Before, a truncated output was, sic:
  "this string is trunca"
and now this becomes, sic:
  "this string is truncat
I decided to not keep the old behavior because it is questionable to begin
with. It looks like the string actually ended at the truncation boundary
instead of the reason being not enough space in the output buffer.

(2) The new osmo_escape_str_buf2() function obviously cannot pass-thru an
unchanged char* if no escaping was needed. Sacrifice this tiny optimization
feature to avoid code duplication:
- it is an unnoticeable optimization,
- the caller anyway always passes a string buffer,
- the feature caused handling strings and buffers differently depending on
  their content (i.e. code that usually writes out strings in full length
  "suddenly" truncates because a non-printable character is contained, etc.)
I considered adding a skip_if_unescaped flag to the osmo_quote_str_buf2()
function signature, but in the end decided that the API clutter is not worth
having for all the above reasons.

Adjust tests to accomodate above changes.

[1] 4a62eda225
    Ibf85f79e93244f53b2684ff6f1095c5b41203e05

Change-Id: Id748b906b0083b1f1887f2be7a53cae705a8a9ae
2019-04-12 01:00:16 +02:00
Neels Hofmeyr 8531d6695f tweak OSMO_STRBUF_APPEND(), add OSMO_STRBUF_APPEND_NOLEN()
In OSMO_STRBUF_APPEND, use local variable names that are less likely to shadow
other local variables: prefix with _sb_.

In OSMO_STRBUF_APPEND, add a check to add to .pos only if it is not NULL.

Add OSMO_STRBUF_APPEND_NOLEN(), which works for function signatures that don't
return a length. This is useful for any osmo_*_buf() string writing functions,
so that these write directly to the strbuf.

Change-Id: I108cadf72deb3a3bcab9a07e50572d9da1ab0359
2019-04-11 07:40:25 +02:00
Neels Hofmeyr d79ccc65f7 add osmo_str_startswith()
Move from a static implementation in tdef_vty.c to utils.c, I also want to use
this in osmo-msc.

The point is that the telnet VTY allows unambiguous partly matches of keyword
args. For example, if I have a command definition of:

    compare (apples|oranges)

then it is perfectly legal as for the vty parser to write only

    compare app

One could expect the VTY to then pass the unambiguous match of "apples" to the
parsing function, but that is not the case.

Hence a VTY function implementation is faced with parsing a keyword of "app"
instead of the expected "apples".

This is actually a very widespread bug in our VTY implementations, which assume
that exactly one full keyword will always be found. I am now writing new
commands in a way that are able to manage only the starts of keywords.

Arguably, strstr(a, b) == a does the same thing, but it searches the entire
string unnecessarily.

Change-Id: Ib2ffb0e9a870dd52e081c7e66d8818057d159513
2019-04-11 05:36:36 +00:00
Harald Welte 179f35702e Add _c versions of functions that otherwise return static buffers
We have a habit of returning static buffers from some functions,
particularly when generating some kind of string values.  This is
convenient in terms of memory management, but it comes at the expense
of not being thread-safe, and not allowing for two calls of the
related function within one printf() statement.

Let's introduce _c suffix versions of those functions where the
caller passes in a talloc context from which the output buffer shall
be allocated.

Change-Id: I8481c19b68ff67cfa22abb93c405ebcfcb0ab19b
2019-04-10 22:42:32 +00:00
Harald Welte 4a62eda225 Add _buf() functions to bypass static string buffers
We have a number of static buffers in use in libosmo*.  This means
the related functions are not usable in a thread-safe way.  While
we so far don't have many multi-threaded programs in the osmocom
universe, the static buffers also prevent us from calling the same
e.g. string-ify function twice within a single printf() call.

Let's make sure there's an alternative function in all those cases,
where the user can pass in a caller-allocated buffer + size, and make
the 'classic' function with the static buffer a wrapper around that
_buf() variant.

Change-Id: Ibf85f79e93244f53b2684ff6f1095c5b41203e05
2019-04-03 18:03:14 +02:00
Harald Welte 98ed3393cd osmo_escape_str_buf: Always copy, don't return input string pointer
osmo_escape_str_buf() used to have the somewhat odd semantics that
if no escaping was needed, it would return the original pointer without
making any copy to the output buffer.  While this seems like an elegant
optimization, it is a very strange behavior and it works differently
than all of our other *_buf() functions.  Let's unify the API and
turn osmo_escape_str_buf() into a strlcpy() if no escaping is needed.

Change-Id: I3a02bdb27008a73101c2db41ac04248960ed4064
2019-03-29 16:42:25 +00:00
Neels Hofmeyr 2cbe25f484 add OSMO_STRBUF_PRINTF()
We are using macros like this or different workarounds in libmsc. In the course
of implementing inter-MSC handover, I am encountering yet another such
situation of appending multiple strings to a limited char buffer. Standardize.

Add a unit test to utils_test.c.

Change-Id: I2497514e26c5e7a5d88985fc7e58343be1a027b2
2019-02-17 20:40:17 +00:00
Neels Hofmeyr 0423b61aa8 add osmo_hexdump_buf() and test
Add osmo_hexdump_buf() as an all-purpose hexdump function, which all other
osmo_hexdump_*() implementations now call. It absorbs the static
_osmo_hexdump(). Add tests for osmo_hexdump_buf().

Rationale: recently during patch review, a situation came up where two hexdumps
in a single printf would have been useful. Now I've faced a similar situation
again, in ongoing development. So I decided it is time to provide this API.

The traditional osmo_hexdump() API returns a non-const char*, which should
probably have been a const instead. Particularly this new function may return a
string constant "" if the buf is NULL or empty, so return const char*. That is
why the older implementations calling osmo_hexdump_buf() separately return the
buffer instead of the const return value directly.

Change-Id: I590595567b218b24e53c9eb1fd8736c0324d371d
2019-01-28 23:58:53 +00:00
Oliver Smith 894be2d9da gsm23003: add osmo_imei_str_valid()
Verify 14 digit and 15 digit IMEI strings. OsmoHLR will use the 14
digit version to check IMEIs before writing them to the DB.

Place the Luhn checksum code in a dedicated osmo_luhn() function, so
it can be used elsewhere.

Related: OS#2541
Change-Id: Id2d2a3a93b033bafc74c62e15297034bf4aafe61
2019-01-14 14:39:57 +00:00
Neels Hofmeyr 7079e69848 add osmo_bcd2str()
Add a standalone bcd-to-string conversion function with generic parameters.
Add a regression test in utils_test.c.

So far there is no single universal implementation that converts a BCD to a
string. I could only find gsm48_mi_to_string(), which also interprets
surrounding bytes, MI type and TMSI as non-BCD value.

The idea is to use this function from gsm48_mi_to_string() and similar
implementations in subsequent commits.

Root cause: in osmo-msc, I want to have an alternative MI-to-string function
for composing an FSM name, which needs the BCD part of gsm48_mi_to_string() but
not the TMSI part.

Change-Id: I86b09d37ceef33331c1a56046a5443127d6c6be0
2018-12-10 17:06:30 +00:00
Neels Hofmeyr 7c749893bf add osmo_str_tolower() and _toupper() with test
We already have osmo_str2lower() and osmo_str2upper(), but these lack:
* proper destination buffer bounds checking,
* ability to call directly as printf() argument.

Deprecate osmo_str2upper() and osmo_str2lower() because of missing bounds
checking.

Introduce osmo_str_tolower_buf(), osmo_str_toupper_buf() to provide
bounds-safe conversion, also able to safely convert a buffer in-place.

Introduce osmo_str_tolower(), osmo_str_toupper() that call the above _buf()
equivalents using a static buffer[128] and returning the resulting string
directly, convenient for direct printing. Possibly truncated but always safe.

Add unit tests to utils_test.c.

Replace all libosmocore uses of now deprecated osmo_str2lower().

Naming: the ctype.h API is called tolower() and toupper(), so just prepend
'osmo_str_' and don't separate 'to_lower'.

Change-Id: Ib0ee1206b9f31d7ba25c31f8008119ac55440797
2018-09-07 04:28:39 +02:00
Vadim Yanitskiy 5594430405 core/utils.h: move includes to the top
Having all inculdes listed in one place is a common good
practice, which prevents one from adding duplicates.

Change-Id: I3f52189d5e8f9afafc39525e95385a085f8f850a
2018-09-05 02:58:03 +07:00
Vadim Yanitskiy 80f4ff9368 core/utils.h: drop duplicate '<stdbool.h>' include
Change-Id: I0979ddda91c4c0aa080b714cf2a698d7634f5091
2018-09-05 02:57:25 +07:00
Neels Hofmeyr 983dcb9af3 use __FILE__, not __BASE_FILE__
The intention was to use the file's basename, but __BASE_FILE__ means "the root
file that is being parsed and contains #include statements".

If we had a function using __BASE_FILE__ and that was defined in an #included
file, __BASE_FILE__ would indicate the first file where the #include is, and
not the file where the function is defined. __BASE_FILE__ works for us because
we don't ever include function definitions that log something, so __BASE_FILE__
always coincides with __FILE__ for our logging; but still __BASE_FILE__ is
semantically the wrong constant.

Related: OS#2740
Change-Id: Ibc1d3746f1876ac42d6b1faf0e5f83bd2283cdcc
2018-08-20 20:23:18 +00:00
Harald Welte 459a180877 Don't call abort() directly, always use osmo_panic()
A loooong time ago, we introduced osmo_panic() as a wrapper around
abort().  The advantage is, that this wrapper can be overridden, and
that it will also work in embedded (bare iron) targets, where the
abort simply translates to an infinite loop.

Change-Id: I5a70eb65952cbc329bf96eacb428b07a9da32433
2018-06-28 10:57:42 +02:00
Harald Welte 15a5f8de00 Add osmo_isqrt32() to compute 32bit integer square root
Change-Id: I2b96db6e037e72e92317fec874877e473a1cf909
2018-06-06 16:58:53 +02:00
Neels Hofmeyr 04eb56f146 add osmo_quote_str(),osmo_quote_str_buf() and test
Rationale: with osmo_escape_str(), you get the escaped contents of the string,
but not so graceful handling of NULL strings. The caller needs to quote it, and
for NULL strings not quote it.

osmo_quote_str() is like osmo_escape_str() but always quotes a non-NULL string,
and for a NULL string returns a literal NULL, i.e. it should (tm) give the
exact C representation of a string.

That's useful in testing, to show exactly what char* situation we have, without
jumping through hoops like
  if (str)
  	printf("\"%s\"", osmo_escape_str(str, -1));
  else
  	printf("NULL");

Copy the unit test for osmo_escape_str() and adjust. To indicate that the
double quotes are returned by osmo_quote_str(), use single quotes in the test
printf()s.

I considered allowing to pick the quoting characters by further arguments, but
that complicates things: we'd need to escape the quoting characters. Just
hardcode double quotes like C.

Change-Id: I6f1b3709b32c23fc52f70ad9ecc9439c62b02a12
2018-04-09 15:56:14 +00:00
Max 18c014de67 utils: add helper wrapper for osmo_strlcpy()
Add wrapper for osmo_strlcpy() which uses sizeof() to automatically
determine buffer's size and use it for GSMTAP logging. This is pretty
common use case for osmo_strlcpy() so it's a good idea to save some
typing by using generic define.

Related: OS#2864
Change-Id: I03d0d3d32a8d572ad573d03c603e14cdc27a3f7b
2018-02-05 11:34:14 +00:00
Neels Hofmeyr 9910bbc62d utils: add osmo_escape_str()
To report invalid characters in identifiers, it is desirable to escape any
weird characters. Otherwise we might print stray newlines or control characters
in the log output.

ctrl_test.c already uses a print_escaped() function, which will be replaced by
osmo_escape_str() in a subsequent patch.

control_cmd.c will use osmo_escape_str() to log invalid identifiers.

Change-Id: Ic685eb63dead3967d01aaa4f1e9899e5461ca49a
2017-12-18 23:05:49 +00:00
Neels Hofmeyr 937ddea6cc utils: add osmo_separated_identifiers_valid()
For validating CTRL input, we want to verify that an input variable is a series
of valid osmo_identifier_valid() separated by dots. Allow validating any
additional chars with identifiers, for CTRL vars will be just ".".

Change-Id: I13dfd02c8c870620f937d789873ad84c6b1c45de
2017-12-17 21:57:55 +00:00
Neels Hofmeyr 84ea2e0374 logging vty: describe 'logging set-log-mask' command, add OSMO_STRINGIFY_VAL()
Change-Id: I73ac5873ede858da44e1486d8a5c81da1ed5b19f
2017-12-12 18:00:24 +00:00
Neels Hofmeyr dc75b114b4 comment: utils: more accurately describe OSMO_STRINGIFY macro
Change-Id: I7b057c026f9df90608b7cbd12481ab9e7a41d88c
2017-12-10 13:34:36 +00:00
Harald Welte febe83c424 Introduce osmo_identifier_valid() function to check validity of identifier
We define the notion of an 'osmocom identifier' which is basically a
7-bit US-ASCII without any special characters beyond "-_:@".  We
introduce a function to verify if an identifier consists only of the
permitted characters.

Change-Id: I96a8d345c5a69238a12d040f39b70c485a5c421c
2017-10-24 16:00:44 +00:00
Neels Hofmeyr abdd7a28ed add osmo_talloc_asprintf() and ctrl_cmd_reply_printf()
Add macro to append to a CTRL commands' reply string, ctrl_cmd_reply_printf().
The talloc_asprintf() part of it is generic enough to qualify for a separate
macro, osmo_talloc_asprintf().

The idea is to not have to decide for each bit added to a string whether the
string is already allocated or not, but simply be able to issue printf commands
and let the macro worry about initial allocation or reallocation.

This originally came from osmo-hlr change
I1bd62ae0d4eefde7e1517db15a2155640a1bab58, where it was requested to move this
bit to libosmocore.

Change-Id: Ic9dba0e4a1eb5a7dc3cee2f181b9024ed4fc7005
2017-10-24 10:54:07 +00:00
Neels Hofmeyr 4335badd0e utils: add osmo_is_hexstr(), add unit test
Will be used by OsmoHLR to validate VTY and CTRL input.

Change-Id: Idf75946eb0a84e145adad13fc7c78bb7a267aa0a
2017-10-09 16:30:45 +02:00
Neels Hofmeyr 17518fe393 doxygen: unify use of \file across the board
Considering the various styles and implications found in the sources, edit
scores of files to follow the same API doc guidelines around the doxygen
grouping and the \file tag.

Many files now show a short description in the generated API doc that was so
far only available as C comment.

The guidelines and reasoning behind it is documented at
https://osmocom.org/projects/cellular-infrastructure/wiki/Guidelines_for_API_documentation

In some instances, remove file comments and add to the corresponding group
instead, to be shared among several files (e.g. bitvec).

Change-Id: Ifa70e77e90462b5eb2b0457c70fd25275910c72b
2017-06-23 00:18:23 +00:00