libosmocore
0.10.2
Osmocom core library
|
various utility routines More...
Files | |
file | defs.h |
General definitions that are meant to be included from header files. | |
file | panic.h |
file | utils.h |
file | macaddr.c |
MAC address utility routines. | |
file | panic.c |
Routines for panic handling. | |
file | plugin.c |
Routines for loading and managing shared library plug-ins. | |
file | strrb.c |
Ringbuffer implementation, tailored for logging. | |
file | utils.c |
Data Structures | |
struct | value_string |
A mapping between human-readable string and numeric value. More... | |
Macros | |
#define | OSMO_GNUC_PREREQ(maj, min) 0 |
Check for gcc and version. More... | |
#define | OSMO_DEPRECATED(text) |
Set the deprecated attribute with a message. More... | |
#define | OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE OSMO_DEPRECATED("For internal use inside libosmocore only.") |
#define | ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
Determine number of elements in an array of static size. More... | |
#define | OSMO_MAX(a, b) ((a) >= (b) ? (a) : (b)) |
Return the maximum of two specified values. More... | |
#define | OSMO_MIN(a, b) ((a) >= (b) ? (b) : (a)) |
Return the minimum of two specified values. More... | |
#define | OSMO_STRINGIFY(x) #x |
Stringify the contents of a macro, e.g. More... | |
#define | OSMO_VALUE_STRING(x) { x, #x } |
Make a value_string entry from an enum value name. More... | |
#define | OSMO_BYTES_FOR_BITS(BITS) ((BITS + 8 - 1) / 8) |
Number of bytes necessary to store given BITS. More... | |
#define | osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1] __attribute__((__unused__)); |
#define | OSMO_SNPRINTF_RET(ret, rem, offset, len) |
#define | OSMO_ASSERT(exp) |
Helper macro to terminate when an assertion failes. More... | |
#define | osmo_talloc_asprintf(ctx, dest, fmt, args ...) |
Append to a string and re-/allocate if necessary. More... | |
Typedefs | |
typedef void(* | osmo_panic_handler_t) (const char *fmt, va_list args) |
panic handler callback function type More... | |
Functions | |
void | osmo_panic (const char *fmt,...) |
Terminate the current program with a panic. More... | |
void | osmo_set_panic_handler (osmo_panic_handler_t h) |
Set the panic handler. More... | |
const char * | get_value_string (const struct value_string *vs, uint32_t val) |
get human-readable string for given value More... | |
const char * | get_value_string_or_null (const struct value_string *vs, uint32_t val) |
get human-readable string or NULL for given value More... | |
int | get_string_value (const struct value_string *vs, const char *str) |
get numeric value for given human-readable string More... | |
char | osmo_bcd2char (uint8_t bcd) |
Convert BCD-encoded digit into printable character. More... | |
uint8_t | osmo_char2bcd (char c) |
Convert number in ASCII to BCD value. More... | |
int | osmo_hexparse (const char *str, uint8_t *b, int max_len) |
Parse a string containing hexadecimal digits. More... | |
char * | osmo_ubit_dump (const uint8_t *bits, unsigned int len) |
Convert a sequence of unpacked bits to ASCII string. More... | |
char * | osmo_hexdump (const unsigned char *buf, int len) |
Convert binary sequence to hexadecimal ASCII string. More... | |
char * | osmo_hexdump_nospc (const unsigned char *buf, int len) |
Convert binary sequence to hexadecimal ASCII string. More... | |
char * | osmo_osmo_hexdump_nospc (const unsigned char *buf, int len) __attribute__((__deprecated__)) |
void | osmo_str2lower (char *out, const char *in) |
Convert an entire string to lower case. More... | |
void | osmo_str2upper (char *out, const char *in) |
Convert an entire string to upper case. More... | |
static void | osmo_talloc_replace_string (void *ctx, char **dst, const char *newstr) |
duplicate a string using talloc and release its prior content (if any) More... | |
int | osmo_constant_time_cmp (const uint8_t *exp, const uint8_t *rel, const int count) |
Wishful thinking to generate a constant time compare. More... | |
uint64_t | osmo_decode_big_endian (const uint8_t *data, size_t data_len) |
Generic retrieval of 1..8 bytes as big-endian uint64_t. More... | |
uint8_t * | osmo_encode_big_endian (uint64_t value, size_t data_len) |
Generic big-endian encoding of big endian number up to 64bit. More... | |
size_t | osmo_strlcpy (char *dst, const char *src, size_t siz) |
Copy a C-string into a sized buffer. More... | |
bool | osmo_is_hexstr (const char *str, int min_digits, int max_digits, bool require_even) |
Validate that a given string is a hex string within given size limits. More... | |
bool | osmo_identifier_valid (const char *str) |
Determine if a given identifier is valid, i.e. More... | |
int | osmo_macaddr_parse (uint8_t *out, const char *in) |
Parse a MAC address from human-readable notation This function parses an ethernet MAC address in the commonly-used hex/colon notation (00:00:00:00:00:00) and generates the binary representation from it. More... | |
int | osmo_get_macaddr (uint8_t *mac_out, const char *dev_name) |
Obtain the MAC address of a given network device. More... | |
static void | osmo_panic_default (const char *fmt, va_list args) |
int | osmo_plugin_load_all (const char *directory) |
Load all plugins available in given directory. More... | |
struct osmo_strrb * | osmo_strrb_create (TALLOC_CTX *ctx, size_t rb_size) |
Create an empty, initialized osmo_strrb. More... | |
bool | osmo_strrb_is_empty (const struct osmo_strrb *rb) |
Check if an osmo_strrb is empty. More... | |
const char * | osmo_strrb_get_nth (const struct osmo_strrb *rb, unsigned int string_index) |
Return a pointer to the Nth string in the osmo_strrb. More... | |
bool | _osmo_strrb_is_bufindex_valid (const struct osmo_strrb *rb, unsigned int bufi) |
size_t | osmo_strrb_elements (const struct osmo_strrb *rb) |
Count the number of log messages in an osmo_strrb. More... | |
int | osmo_strrb_add (struct osmo_strrb *rb, const char *data) |
Add a string to the osmo_strrb. More... | |
static char * | _osmo_hexdump (const unsigned char *buf, int len, char *delim) |
char | alias ("osmo_hexdump_nospc"))) |
Variables | |
static osmo_panic_handler_t | osmo_panic_handler = (void*)0 |
static char | namebuf [255] |
static char | hexd_buff [4096] |
static const char | hex_chars [] = "0123456789abcdef" |
various utility routines
#define ARRAY_SIZE | ( | x | ) | (sizeof(x) / sizeof((x)[0])) |
Determine number of elements in an array of static size.
Referenced by _osmo_backtrace(), dispatch_rx_msg(), interval_expired(), log_init(), msgb_hexdump(), osmo_encode_big_endian(), osmo_sercomm_change_speed(), osmo_sercomm_drv_pull(), osmo_sercomm_init(), and osmo_sercomm_register_rx_cb().
#define OSMO_ASSERT | ( | exp | ) |
Helper macro to terminate when an assertion failes.
[in] | exp | Predicate to verify This function will generate a backtrace and terminate the program if the predicate evaluates to false (0). |
Referenced by _osmo_fsm_inst_dispatch(), assert_loginfo(), log_parse_category_mask(), mangle_identifier_ifneeded(), map_subsys(), msgb_printf(), osmo_encode_big_endian(), osmo_stats_reporter_alloc(), osmo_stats_reporter_find(), and rate_ctr_group_desc_mangle().
#define OSMO_BYTES_FOR_BITS | ( | BITS | ) | ((BITS + 8 - 1) / 8) |
Number of bytes necessary to store given BITS.
#define OSMO_DEPRECATED | ( | text | ) |
Set the deprecated attribute with a message.
Referenced by msgb_test_invariant().
#define OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE OSMO_DEPRECATED("For internal use inside libosmocore only.") |
#define OSMO_GNUC_PREREQ | ( | maj, | |
min | |||
) | 0 |
Check for gcc and version.
#define OSMO_MAX | ( | a, | |
b | |||
) | ((a) >= (b) ? (a) : (b)) |
Return the maximum of two specified values.
#define OSMO_MIN | ( | a, | |
b | |||
) | ((a) >= (b) ? (b) : (a)) |
Return the minimum of two specified values.
#define OSMO_SNPRINTF_RET | ( | ret, | |
rem, | |||
offset, | |||
len | |||
) |
Referenced by _output(), log_vty_command_description(), and log_vty_command_string().
#define osmo_static_assert | ( | exp, | |
name | |||
) | typedef int dummy##name [(exp) ? 1 : -1] __attribute__((__unused__)); |
Referenced by msgb_alloc_headroom().
#define OSMO_STRINGIFY | ( | x | ) | #x |
Stringify the contents of a macro, e.g.
a port number
#define osmo_talloc_asprintf | ( | ctx, | |
dest, | |||
fmt, | |||
args ... | |||
) |
Append to a string and re-/allocate if necessary.
[in] | ctx | Talloc context to use for initial allocation. |
[in,out] | dest | char* to re-/allocate and append to. |
[in] | fmt | printf-like string format. |
[in] | args | Arguments for fmt. |
dest may be passed in NULL, or a string previously allocated by talloc. If an existing string is passed in, it will remain associated with whichever ctx it was allocated before, regardless whether it matches ctx or not.
#define OSMO_VALUE_STRING | ( | x | ) | { x, #x } |
Make a value_string entry from an enum value name.
typedef void(* osmo_panic_handler_t) (const char *fmt, va_list args) |
panic handler callback function type
|
static |
References hex_chars, and hexd_buff.
Referenced by osmo_hexdump(), and osmo_hexdump_nospc().
bool _osmo_strrb_is_bufindex_valid | ( | const struct osmo_strrb * | rb, |
unsigned int | bufi | ||
) |
References osmo_strrb::end, osmo_strrb_is_empty(), osmo_strrb::size, and osmo_strrb::start.
Referenced by osmo_strrb_get_nth().
char alias | ( | "osmo_hexdump_nospc" | ) |
Referenced by osmo_hexdump_nospc().
int get_string_value | ( | const struct value_string * | vs, |
const char * | str | ||
) |
get numeric value for given human-readable string
[in] | vs | Array of value_string tuples |
[in] | str | human-readable string |
References value_string::value.
Referenced by log_parse_level().
const char * get_value_string | ( | const struct value_string * | vs, |
uint32_t | val | ||
) |
get human-readable string for given value
[in] | vs | Array of value_string tuples |
[in] | val | Value to be converted |
If val is found in vs, the array's string entry is returned. Otherwise, an "unknown" string containing the actual value is composed in a static buffer that is reused across invocations.
References get_value_string_or_null(), and namebuf.
Referenced by log_level_str(), osmo_fsm_event_name(), and osmo_fsm_term_cause_name().
const char * get_value_string_or_null | ( | const struct value_string * | vs, |
uint32_t | val | ||
) |
get human-readable string or NULL for given value
[in] | vs | Array of value_string tuples |
[in] | val | Value to be converted |
References value_string::str.
Referenced by get_value_string().
char osmo_bcd2char | ( | uint8_t | bcd | ) |
Convert BCD-encoded digit into printable character.
[in] | bcd | A single BCD-encoded digit |
uint8_t osmo_char2bcd | ( | char | c | ) |
Convert number in ASCII to BCD value.
[in] | c | ASCII character |
int osmo_constant_time_cmp | ( | const uint8_t * | exp, |
const uint8_t * | rel, | ||
const int | count | ||
) |
Wishful thinking to generate a constant time compare.
[in] | exp | Expected data |
[in] | rel | Comparison value |
[in] | count | Number of bytes to compare |
Compare count bytes of exp to rel. Return 0 if they are identical, 1 otherwise. Do not return a mismatch on the first mismatching byte, but always compare all bytes, regardless. The idea is that the amount of matching bytes cannot be inferred from the time the comparison took.
uint64_t osmo_decode_big_endian | ( | const uint8_t * | data, |
size_t | data_len | ||
) |
Generic retrieval of 1..8 bytes as big-endian uint64_t.
[in] | data | Input data as byte-array |
[in] | data_len | Length of data in octets |
This is like osmo_load64be_ext, except that if data_len is less than sizeof(uint64_t), the data is interpreted as the least significant bytes (osmo_load64be_ext loads them as the most significant bytes into the returned uint64_t). In this way, any integer size up to 64 bits can be decoded conveniently by using sizeof(), without the need to call specific numbered functions (osmo_load16, 32, ...).
uint8_t * osmo_encode_big_endian | ( | uint64_t | value, |
size_t | data_len | ||
) |
Generic big-endian encoding of big endian number up to 64bit.
[in] | value | unsigned integer value to be stored |
[in] | data_len | number of octets |
This is like osmo_store64be_ext, except that this returns a static buffer of the result (for convenience, but not threadsafe). If data_len is less than sizeof(uint64_t), only the least significant bytes of value are encoded.
References ARRAY_SIZE, OSMO_ASSERT, and osmo_store64be_ext().
int osmo_get_macaddr | ( | uint8_t * | mac_out, |
const char * | dev_name | ||
) |
Obtain the MAC address of a given network device.
[out] | mac_out | pointer to caller-allocated buffer of 6 bytes |
[in] | dev_name | string name of the network device |
Referenced by osmo_macaddr_parse().
char * osmo_hexdump | ( | const unsigned char * | buf, |
int | len | ||
) |
Convert binary sequence to hexadecimal ASCII string.
[in] | buf | pointer to sequence of bytes |
[in] | len | length of buf in number of bytes |
This function will print a sequence of bytes as hexadecimal numbers, adding one space character between each byte (e.g. "1a ef d9")
The maximum size of the output buffer is 4096 bytes, i.e. the maximum number of input bytes that can be printed in one call is 1365!
References _osmo_hexdump().
Referenced by msgb_hexdump().
char * osmo_hexdump_nospc | ( | const unsigned char * | buf, |
int | len | ||
) |
Convert binary sequence to hexadecimal ASCII string.
[in] | buf | pointer to sequence of bytes |
[in] | len | length of buf in number of bytes |
This function will print a sequence of bytes as hexadecimal numbers, without any space character between each byte (e.g. "1aefd9")
The maximum size of the output buffer is 4096 bytes, i.e. the maximum number of input bytes that can be printed in one call is 2048!
References __attribute__, _osmo_hexdump(), alias(), and osmo_osmo_hexdump_nospc().
int osmo_hexparse | ( | const char * | str, |
uint8_t * | b, | ||
int | max_len | ||
) |
Parse a string containing hexadecimal digits.
[in] | str | string containing ASCII encoded hexadecimal digits |
[out] | b | output buffer |
[in] | max_len | maximum space in output buffer |
bool osmo_identifier_valid | ( | const char * | str | ) |
Determine if a given identifier is valid, i.e.
doesn't contain illegal chars
[in] | str | String to validate |
Referenced by osmo_fsm_inst_alloc(), osmo_fsm_register(), and rate_ctrl_group_desc_validate().
bool osmo_is_hexstr | ( | const char * | str, |
int | min_digits, | ||
int | max_digits, | ||
bool | require_even | ||
) |
Validate that a given string is a hex string within given size limits.
Note that each hex digit amounts to a nibble, so if checking for a hex string to result in N bytes, pass amount of digits as 2*N.
str | A nul-terminated string to validate, or NULL. |
min_digits | least permitted amount of digits. |
max_digits | most permitted amount of digits. |
require_even | if true, require an even amount of digits. |
int osmo_macaddr_parse | ( | uint8_t * | out, |
const char * | in | ||
) |
Parse a MAC address from human-readable notation This function parses an ethernet MAC address in the commonly-used hex/colon notation (00:00:00:00:00:00) and generates the binary representation from it.
[out] | out | pointer to caller-allocated buffer of 6 bytes |
[in] | in | pointer to input data as string with hex/colon notation |
References osmo_get_macaddr().
char * osmo_osmo_hexdump_nospc | ( | const unsigned char * | buf, |
int | len | ||
) |
Referenced by osmo_hexdump_nospc().
void osmo_panic | ( | const char * | fmt, |
... | |||
) |
Terminate the current program with a panic.
You can call this function in case some severely unexpected situation is detected and the program is supposed to terminate in a way that reports the fact that it terminates.
The application can register a panic handler function using osmo_set_panic_handler. If it doesn't, a default panic handler function is called automatically.
The default function on most systems will generate a backtrace and then abort() the process.
References osmo_panic_default(), and osmo_panic_handler.
|
static |
References osmo_generate_backtrace().
Referenced by osmo_panic().
int osmo_plugin_load_all | ( | const char * | directory | ) |
Load all plugins available in given directory.
[in] | directory | full path name of directory containing plug-ins |
void osmo_set_panic_handler | ( | osmo_panic_handler_t | h | ) |
Set the panic handler.
[in] | h | New panic handler function |
This changes the panic handling function from the currently active function to a new call-back function supplied by the caller.
References osmo_panic_handler.
void osmo_str2lower | ( | char * | out, |
const char * | in | ||
) |
Convert an entire string to lower case.
[out] | out | output string, caller-allocated |
[in] | in | input string |
void osmo_str2upper | ( | char * | out, |
const char * | in | ||
) |
Convert an entire string to upper case.
[out] | out | output string, caller-allocated |
[in] | in | input string |
size_t osmo_strlcpy | ( | char * | dst, |
const char * | src, | ||
size_t | siz | ||
) |
Copy a C-string into a sized buffer.
[in] | src | source string |
[out] | dst | destination string |
[in] | siz | size of the dst buffer |
Copy at most siz bytes from src to dst, ensuring that the result is NUL terminated. The NUL character is included in siz, i.e. passing the actual sizeof(*dst) is correct.
Referenced by _gsmtap_raw_output().
int osmo_strrb_add | ( | struct osmo_strrb * | rb, |
const char * | data | ||
) |
Add a string to the osmo_strrb.
[in] | rb | The osmo_strrb to add to. |
[in] | data | The string to add. |
Add a message to the osmo_strrb. Older messages will be overwritten as necessary.
References osmo_strrb::buffer, osmo_strrb::end, RB_MAX_MESSAGE_SIZE, osmo_strrb::size, and osmo_strrb::start.
Referenced by _rb_output().
struct osmo_strrb* osmo_strrb_create | ( | TALLOC_CTX * | ctx, |
size_t | rb_size | ||
) |
Create an empty, initialized osmo_strrb.
[in] | ctx | The talloc memory context which should own this. |
[in] | rb_size | The number of message slots the osmo_strrb can hold. |
This function creates and initializes a ringbuffer. Note that the ringbuffer stores at most rb_size - 1 messages.
References osmo_strrb::buffer, RB_MAX_MESSAGE_SIZE, and osmo_strrb::size.
Referenced by log_target_create_rb().
size_t osmo_strrb_elements | ( | const struct osmo_strrb * | rb | ) |
Count the number of log messages in an osmo_strrb.
[in] | rb | The osmo_strrb to count the elements of. |
References osmo_strrb::end, osmo_strrb::size, and osmo_strrb::start.
Referenced by log_target_rb_used_size().
const char* osmo_strrb_get_nth | ( | const struct osmo_strrb * | rb, |
unsigned int | string_index | ||
) |
Return a pointer to the Nth string in the osmo_strrb.
[in] | rb | The osmo_strrb to search. |
[in] | string_index | The index sought (N), zero-indexed. |
Return a pointer to the Nth string in the osmo_strrb. Return NULL if there is no Nth string. Note that N is zero-indexed.
References _osmo_strrb_is_bufindex_valid(), osmo_strrb::buffer, osmo_strrb::end, osmo_strrb::size, and osmo_strrb::start.
Referenced by log_target_rb_get().
bool osmo_strrb_is_empty | ( | const struct osmo_strrb * | rb | ) |
Check if an osmo_strrb is empty.
[in] | rb | The osmo_strrb to check. |
References osmo_strrb::end, and osmo_strrb::start.
Referenced by _osmo_strrb_is_bufindex_valid().
|
inlinestatic |
duplicate a string using talloc and release its prior content (if any)
[in] | ctx | Talloc context to use for allocation |
[out] | dst | pointer to string, will be updated with ptr to new string |
[in] | newstr | String that will be copieed to newly allocated string |
char * osmo_ubit_dump | ( | const uint8_t * | bits, |
unsigned int | len | ||
) |
Convert a sequence of unpacked bits to ASCII string.
[in] | bits | A sequence of unpacked bits |
[in] | len | Length of bits |
References hexd_buff.
|
static |
Referenced by _osmo_hexdump().
|
static |
Referenced by _osmo_hexdump(), and osmo_ubit_dump().
|
static |
Referenced by get_value_string().
|
static |
Referenced by osmo_panic(), and osmo_set_panic_handler().