This is a small collection of general purpose utilities.
|
| int | utl_ip_port_from_ss (char *ip, size_t ip_len, uint16_t *port, struct sockaddr_storage *ss) |
| |
| double | utl_diff_timespec_as_double (const struct timespec *t1, const struct timespec *t2) |
| |
| void | utl_diff_timespec (struct timespec *dst, const struct timespec *t1, const struct timespec *t2) |
| |
| void | char_to_lc (char *c) |
| |
| void | str_to_lc (uint8_t *str, size_t str_len) |
| |
| bool | str_is_numeric (uint8_t *str, size_t str_len) |
| |
| int | str_to_unsigned_long (unsigned long *dst, char *str) |
| |
| int | str_to_bool (bool *dst, char *str) |
| |
| int | parse_csv_to_ul_array (size_t *ul_array, size_t ul_array_len, char *str) |
| |
| int | sockaddr_storage_to_string (char *buf, size_t buf_len, struct sockaddr_storage *ss) |
| |
| int | utl_readall (int fd, size_t size, void **buf, char *err, size_t err_len) |
| |
| int | utl_writeall (int fd, void *buf, size_t buf_len, char *err, size_t err_len) |
| |
| int | utl_timespec_to_rfc3339nano (struct timespec *ts, char *buf) |
| |
| void | utl_clock_gettime_rt_fatal (struct timespec *tp) |
| |
◆ ARRAY_COUNT
| #define ARRAY_COUNT |
( |
|
a | ) |
sizeof(a) / sizeof(a[0]) |
Macro to calculate array size.
◆ CHECK_MALLOC
| #define CHECK_MALLOC |
( |
|
a | ) |
|
Value: do { \
if((a) == NULL) { \
assert(0); \
} } while (0)
Macro to check result of memory allocation, asserts on memory allocation failure.
◆ debug
Value: do {
if (
DEBUG_TEST) fprintf(stderr,
"%s:%d:%s(): \n", __FILE__, \
__LINE__, __func__); } while (0)
#define DEBUG_TEST
Definition utils.h:55
Macro to print out debug message if DEBUG is set at compile time. This prints out filename, line in file, and function it was called in.
◆ debug_print
| #define debug_print |
( |
|
str | ) |
|
Value: do {
if (
DEBUG_TEST) fprintf(stderr,
"%s:%d:%s(): " str
"\n", __FILE__, \
__LINE__, __func__); } while (0)
Macro to print out debug message if DEBUG is set at compile time. This prints out everything macro debug does plus a string passed in as argument.
- Parameters
-
◆ debug_printf
| #define debug_printf |
( |
|
fmt, |
|
|
|
... |
|
) |
| |
Value: do {
if (
DEBUG_TEST) fprintf(stderr,
"%s:%d:%s(): " fmt
"\n", __FILE__, \
__LINE__, __func__, __VA_ARGS__); } while (0)
Macro to print out debug message if DEBUG is set at compile time. This prints out everything macro debug does plus a string passed in as argument. String can be formatted.
- Note
- Only use this function to output a formatted string as "fmt" + variable list is REQUIRED. To output string with no format use debug_print macro.
- Parameters
-
| fmt | String format followed by variable list. |
◆ DEBUG_TEST
Constant enables printing of debug messages to stderr. Otherwise if not set the compiler optimizes out (removes) all of the debug message code.
◆ DECREMENT
| #define DECREMENT |
( |
|
a | ) |
a -= 1 |
Macro to decrement a variable by one.
◆ INCREMENT
| #define INCREMENT |
( |
|
a | ) |
a += 1 |
Macro to increment a variable by one.
◆ TIME_RFC3339_STRLEN
| #define TIME_RFC3339_STRLEN 31 |
Length of buffer needed to store time as rfc399nano formatted string
◆ char_to_lc()
| void char_to_lc |
( |
char * |
c | ) |
|
|
inline |
Convert ascii character to lower case. If character is not alphabet [A-Z] or is already in lower case, this is a no-op.
- Parameters
-
| c | Character to convert to lower case. |
◆ parse_csv_to_ul_array()
| int parse_csv_to_ul_array |
( |
size_t * |
ul_array, |
|
|
size_t |
ul_array_len, |
|
|
char * |
str |
|
) |
| |
Parse comma separated values into array of unsigend long integers.
strtok() is not used as it skips empty entries (i.e. ',,').
- Parameters
-
| ul_array | Array to parse entries into. |
| ul_array_len | Number of slots in array. |
| str | String to parse CSV from. |
- Returns
- 0 on success, otherwise an error occurred.
◆ sockaddr_storage_to_string()
| int sockaddr_storage_to_string |
( |
char * |
buf, |
|
|
size_t |
buf_len, |
|
|
struct sockaddr_storage * |
ss |
|
) |
| |
Convert IPv4 or IPv6 addresses and port from binary to text form as "ip:port".
- Parameters
-
| buf | Buffer to store string into. |
| buf_len | Length of buffer, must be large enough to account for IPv6, ":", port (5 characters), plus a '\0' string terminator. |
| ss | struct_sockaddr to extract IP and port from. |
- Returns
- 0 on success, otherwise error occurred. -1 indicates that buf_len was too small, any other value is errno set by inet_ntop().
◆ str_is_numeric()
| bool str_is_numeric |
( |
uint8_t * |
str, |
|
|
size_t |
str_len |
|
) |
| |
Check if ascii string is solely composed of numeric [0-9] characters.
- Parameters
-
| str | String to check. |
| str_len | Length of string. |
- Returns
- Returns tue if ascii string is numeric, otherwise returns false.
◆ str_to_bool()
| int str_to_bool |
( |
bool * |
dst, |
|
|
char * |
str |
|
) |
| |
Convert strings "true" and "false" to boolean and store result in dst. Input strings can be a mix of upper and lower case characters.
- Parameters
-
| dst | Destination to store value to |
| str | String to convert to bool. |
- Returns
- 0 on success, otherwise an error occurred, meaning string does not represent "true" or "false".
◆ str_to_lc()
| void str_to_lc |
( |
uint8_t * |
str, |
|
|
size_t |
str_len |
|
) |
| |
Convert string ascii characters to lower case.
- Parameters
-
| str | String to convert to all lower case ASCII. |
| str_len | Length of string. |
◆ str_to_unsigned_long()
| int str_to_unsigned_long |
( |
unsigned long * |
dst, |
|
|
char * |
str |
|
) |
| |
Convert string to unsigned long and store result in dst. This function only handles base 10 numbers.
- Parameters
-
| dst | Destination to store value to |
| str | String to convert to unsigned long. |
- Returns
- 0 on success, otherwise an error occurred.
◆ utl_clock_gettime_rt_fatal()
| void utl_clock_gettime_rt_fatal |
( |
struct timespec * |
tp | ) |
|
Get current time from clock CLOCK_REALTIME (see man -3 time) and store it in tp.
- Note
- This function is fatal on error. Error can only occur if there is something majorly wrong with underlying OS. Regardless, not being able to get current time impacts critical application function.
- Parameters
-
| tp | Where to store current time. |
◆ utl_diff_timespec()
| void utl_diff_timespec |
( |
struct timespec * |
dst, |
|
|
const struct timespec * |
t1, |
|
|
const struct timespec * |
t2 |
|
) |
| |
Get a difference between two timespec stuctures t1 - t2.
- Note
- t1 MUST be larger than t2.
- Parameters
-
| dst | Where to store result. |
| t1 | Time to subtract from. |
| t2 | Time to subtract. |
◆ utl_diff_timespec_as_double()
| double utl_diff_timespec_as_double |
( |
const struct timespec * |
t1, |
|
|
const struct timespec * |
t2 |
|
) |
| |
Get a difference between two timespec stuctures t1 - t2 and return the result as type double.
- Parameters
-
| t1 | Time to subtract from. |
| t2 | Time to subtract. |
- Returns
- Time difference as double.
◆ utl_ip_port_from_ss()
| int utl_ip_port_from_ss |
( |
char * |
ip, |
|
|
size_t |
ip_len, |
|
|
uint16_t * |
port, |
|
|
struct sockaddr_storage * |
ss |
|
) |
| |
From sockaddr_storage extract IP and port into strings.
- Parameters
-
| ip | Buffer where to store IP address. |
| ip_len | Length of space available in ip buffer. |
| port | Buffer where to store TCP/UDP port. |
| ss | sockaddr_storage to extract IP and port from. |
- Returns
- 0 on success, otherwise error occurred.
◆ utl_readall()
| int utl_readall |
( |
int |
fd, |
|
|
size_t |
size, |
|
|
void ** |
buf, |
|
|
char * |
err, |
|
|
size_t |
err_len |
|
) |
| |
Read size specified from file descriptor into memory.
- Note
- Per Linux manual read() will return at most 0x7ffff000 (2,147,479,552) bytes, hence a need for this function to keep reading until the full size specified is read into memory buffer.
- Parameters
-
| fd | Open file descriptor to read from, seek position MUST be at the begining of the file. |
| size | Number of bytes to read in. |
| buf | Pointer where to store buffer data is read into. |
| err | Buffer where to store error string if error occurred. Error string is guaranteed to end with '\0' so i.e. a followup call to strlen() is a valid action. |
| err_len | Size of err buffer. |
- Returns
- On success returns 0, otherwise error occurred in which case *buf is set to NULL and error message is populated.
◆ utl_timespec_to_rfc3339nano()
| int utl_timespec_to_rfc3339nano |
( |
struct timespec * |
ts, |
|
|
char * |
buf |
|
) |
| |
Store time in ts as string in GMT rfc3339nano format.
- Parameters
-
| ts | Time to store as string. |
| buf | Buffer to store times string at. This buffer MUST have minimum length of TIME_RFC3339_STRLEN. |
◆ utl_writeall()
| int utl_writeall |
( |
int |
fd, |
|
|
void * |
buf, |
|
|
size_t |
buf_len, |
|
|
char * |
err, |
|
|
size_t |
err_len |
|
) |
| |
Write data from buffer to open file descriptor fd until all data was written.
- Parameters
-
| fd | Open file descriptor to write to. |
| buf | Buffer to write data from. |
| buf_len | Length of data in buff to write. |
| err | Buffer where to store error message if one is encountered. |
| err_len | Size of err buffer available to store error message. |
- Returns
- On success returns 0. Otherwise error occurred and error message was populated. If error occurred it is possible that only part of the data was written.