2009-04-30 14:25:52 +02:00
|
|
|
|
|
|
|
#ifndef __MISC_H__
|
|
|
|
#define __MISC_H__
|
|
|
|
|
2017-01-06 00:44:18 -08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2009-04-30 14:51:36 +02:00
|
|
|
#include <stdint.h>
|
2017-11-15 23:13:20 +01:00
|
|
|
#include <stdbool.h>
|
2014-09-27 22:59:19 +02:00
|
|
|
#include <time.h>
|
2017-01-13 17:32:59 -05:00
|
|
|
#include <pthread.h>
|
2010-09-18 17:15:41 +02:00
|
|
|
|
2016-12-26 19:29:47 +01:00
|
|
|
/* Samples to bytes, bytes to samples */
|
|
|
|
#define STOB(s) ((s) * 4)
|
|
|
|
#define BTOS(b) ((b) / 4)
|
|
|
|
|
2018-09-04 20:07:05 +02:00
|
|
|
#define ARRAY_SIZE(x) ((unsigned int)(sizeof(x) / sizeof((x)[0])))
|
2016-12-26 19:29:47 +01:00
|
|
|
|
2010-09-18 17:15:41 +02:00
|
|
|
struct onekeyval {
|
|
|
|
char *name;
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
struct onekeyval *next;
|
2014-08-15 22:56:39 +02:00
|
|
|
struct onekeyval *sort;
|
2010-09-18 17:15:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct keyval {
|
|
|
|
struct onekeyval *head;
|
|
|
|
struct onekeyval *tail;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-09-02 07:30:23 +02:00
|
|
|
char **
|
|
|
|
buildopts_get(void);
|
|
|
|
|
2009-04-30 14:25:52 +02:00
|
|
|
int
|
2010-02-02 21:02:24 +01:00
|
|
|
safe_atoi32(const char *str, int32_t *val);
|
2009-04-30 14:25:52 +02:00
|
|
|
|
2010-02-08 14:58:14 +01:00
|
|
|
int
|
|
|
|
safe_atou32(const char *str, uint32_t *val);
|
|
|
|
|
2010-05-05 19:03:53 +02:00
|
|
|
int
|
|
|
|
safe_hextou32(const char *str, uint32_t *val);
|
|
|
|
|
2009-04-30 14:25:52 +02:00
|
|
|
int
|
2010-02-02 21:02:24 +01:00
|
|
|
safe_atoi64(const char *str, int64_t *val);
|
2010-01-10 12:09:29 +01:00
|
|
|
|
2010-02-08 14:58:14 +01:00
|
|
|
int
|
|
|
|
safe_atou64(const char *str, uint64_t *val);
|
|
|
|
|
2010-04-04 18:07:32 +02:00
|
|
|
int
|
|
|
|
safe_hextou64(const char *str, uint64_t *val);
|
|
|
|
|
2017-01-01 10:23:34 +01:00
|
|
|
char *
|
|
|
|
safe_strdup(const char *str);
|
2010-09-18 17:15:41 +02:00
|
|
|
|
2017-05-05 20:00:51 +02:00
|
|
|
char *
|
|
|
|
safe_asprintf(const char *fmt, ...);
|
|
|
|
|
2010-09-18 17:15:41 +02:00
|
|
|
/* Key/value functions */
|
2014-08-15 22:56:39 +02:00
|
|
|
struct keyval *
|
|
|
|
keyval_alloc(void);
|
|
|
|
|
2010-09-18 17:15:41 +02:00
|
|
|
int
|
|
|
|
keyval_add(struct keyval *kv, const char *name, const char *value);
|
|
|
|
|
|
|
|
int
|
|
|
|
keyval_add_size(struct keyval *kv, const char *name, const char *value, size_t size);
|
|
|
|
|
|
|
|
void
|
|
|
|
keyval_remove(struct keyval *kv, const char *name);
|
|
|
|
|
|
|
|
const char *
|
|
|
|
keyval_get(struct keyval *kv, const char *name);
|
|
|
|
|
|
|
|
void
|
|
|
|
keyval_clear(struct keyval *kv);
|
|
|
|
|
2014-08-15 22:56:39 +02:00
|
|
|
void
|
|
|
|
keyval_sort(struct keyval *kv);
|
|
|
|
|
2010-09-18 17:15:41 +02:00
|
|
|
|
2017-06-14 23:49:18 +02:00
|
|
|
char **
|
|
|
|
m_readfile(const char *path, int num_lines);
|
|
|
|
|
2010-06-21 17:50:09 +02:00
|
|
|
char *
|
2015-05-31 14:05:31 +01:00
|
|
|
unicode_fixup_string(char *str, const char *fromcode);
|
2010-06-21 17:50:09 +02:00
|
|
|
|
2014-08-15 22:56:39 +02:00
|
|
|
char *
|
|
|
|
trimwhitespace(const char *str);
|
|
|
|
|
2017-01-22 23:06:13 +01:00
|
|
|
void
|
|
|
|
swap_pointers(char **a, char **b);
|
|
|
|
|
2009-04-30 14:51:36 +02:00
|
|
|
uint32_t
|
2016-01-17 20:59:16 +01:00
|
|
|
djb_hash(const void *data, size_t len);
|
2009-04-30 14:51:36 +02:00
|
|
|
|
2009-05-01 20:56:22 +02:00
|
|
|
char *
|
|
|
|
b64_decode(const char *b64);
|
|
|
|
|
2010-04-12 18:22:38 +02:00
|
|
|
char *
|
2016-07-29 19:53:56 +02:00
|
|
|
b64_encode(const uint8_t *in, size_t len);
|
2010-04-12 18:22:38 +02:00
|
|
|
|
2010-01-04 17:58:28 +01:00
|
|
|
uint64_t
|
|
|
|
murmur_hash64(const void *key, int len, uint32_t seed);
|
|
|
|
|
2017-11-15 23:13:20 +01:00
|
|
|
|
|
|
|
/* Checks if the address is in a network that is configured as trusted */
|
|
|
|
bool
|
|
|
|
peer_address_is_trusted(const char *addr);
|
|
|
|
|
|
|
|
|
2017-01-14 00:56:43 -05:00
|
|
|
#ifndef HAVE_CLOCK_GETTIME
|
|
|
|
|
|
|
|
#ifndef CLOCK_REALTIME
|
|
|
|
# define CLOCK_REALTIME 0
|
|
|
|
#endif
|
|
|
|
#ifndef CLOCK_MONOTONIC
|
|
|
|
# define CLOCK_MONOTONIC 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef int clockid_t;
|
|
|
|
|
|
|
|
int
|
|
|
|
clock_gettime(clockid_t clock_id, struct timespec *tp);
|
|
|
|
|
|
|
|
int
|
|
|
|
clock_getres(clockid_t clock_id, struct timespec *res);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_TIMER_SETTIME
|
|
|
|
|
|
|
|
struct itimerspec {
|
|
|
|
struct timespec it_interval;
|
|
|
|
struct timespec it_value;
|
|
|
|
};
|
|
|
|
typedef uint64_t timer_t;
|
|
|
|
|
|
|
|
int
|
|
|
|
timer_create(clockid_t clock_id, void *sevp, timer_t *timer_id);
|
|
|
|
|
|
|
|
int
|
|
|
|
timer_delete(timer_t timer_id);
|
|
|
|
|
|
|
|
int
|
|
|
|
timer_settime(timer_t timer_id, int flags, const struct itimerspec *tp,
|
|
|
|
struct itimerspec *old);
|
|
|
|
|
|
|
|
int
|
|
|
|
timer_getoverrun(timer_t timer_id);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2014-09-27 22:59:19 +02:00
|
|
|
/* Timer function for platforms without hi-res timers */
|
2011-02-17 07:21:35 -08:00
|
|
|
int
|
|
|
|
clock_gettime_with_res(clockid_t clock_id, struct timespec *tp, struct timespec *res);
|
|
|
|
|
|
|
|
struct timespec
|
|
|
|
timespec_add(struct timespec time1, struct timespec time2);
|
|
|
|
|
|
|
|
int
|
|
|
|
timespec_cmp(struct timespec time1, struct timespec time2);
|
|
|
|
|
2017-01-15 23:19:57 +01:00
|
|
|
struct timespec
|
|
|
|
timespec_reltoabs(struct timespec relative);
|
|
|
|
|
2017-01-21 07:11:20 -08:00
|
|
|
/* initialize mutex with error checking (not default on all platforms) */
|
2017-01-13 17:32:59 -05:00
|
|
|
int
|
2017-01-21 07:11:20 -08:00
|
|
|
mutex_init(pthread_mutex_t *mutex);
|
|
|
|
|
|
|
|
/* Check that the function returns 0, logging a fatal error referencing
|
|
|
|
returned error (type errno) if it fails, and aborts the process.
|
|
|
|
Example: CHECK_ERR(L_MAIN, my_function()); */
|
|
|
|
#define CHECK_ERR(d, f) \
|
|
|
|
do { int chk_err; \
|
|
|
|
if ( (chk_err = (f)) != 0) \
|
|
|
|
log_fatal_err(d, #f, __LINE__, chk_err); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
/* Check that the function returns 0 or okval, logging a fatal
|
|
|
|
error referencing returned erro (type errno) if not, and aborts the process.
|
|
|
|
Example: int err; CHECK_ERR_EXCEPT(L_MAIN, my_wait(), err, ETIMEDOUT); */
|
|
|
|
#define CHECK_ERR_EXCEPT(d, f, var, okval) \
|
|
|
|
do { (var) = (f); \
|
|
|
|
if (! (((var) == (okval)) || ((var) == 0))) \
|
|
|
|
log_fatal_err(d, #f, __LINE__, (var)); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
/* Check that the function returns value >= 0, logging a fatal error
|
|
|
|
referencing errno if it not, and aborts the process.
|
|
|
|
Example: int ret; CHECK_ERRNO(L_MAIN, ret = my_function()); */
|
|
|
|
#define CHECK_ERRNO(d, f) \
|
|
|
|
do { \
|
|
|
|
if ( (f) < 0 ) \
|
|
|
|
log_fatal_errno(d, #f, __LINE__); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
/* Check that the function returns non-NULL, logging a fatal error if not,
|
|
|
|
and aborts the process.
|
|
|
|
Example: void *ptr; CHECK_NULL(L_MAIN, ptr = my_create()); */
|
|
|
|
#define CHECK_NULL(d, f) \
|
|
|
|
do { \
|
|
|
|
if ( (f) == NULL ) \
|
|
|
|
log_fatal_null(d, #f, __LINE__); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
/* Used by CHECK_*() macros */
|
2017-03-04 18:30:38 +01:00
|
|
|
void
|
|
|
|
log_fatal_err(int domain, const char *func, int line, int err) __attribute__((__noreturn__));
|
|
|
|
|
|
|
|
void
|
|
|
|
log_fatal_errno(int domain, const char *func, int line) __attribute__((__noreturn__));
|
|
|
|
|
|
|
|
void
|
|
|
|
log_fatal_null(int domain, const char *func, int line) __attribute__((__noreturn__));
|
2017-01-13 17:32:59 -05:00
|
|
|
|
2009-04-30 14:25:52 +02:00
|
|
|
#endif /* !__MISC_H__ */
|