2020-06-02 13:08:23 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <exception>
|
|
|
|
#include <string>
|
2020-06-05 16:15:20 +02:00
|
|
|
#include <netinet/in.h>
|
2020-06-02 13:08:23 +02:00
|
|
|
|
2020-06-04 11:46:35 +02:00
|
|
|
/* Debugging -- taken from babeld */
|
|
|
|
|
|
|
|
extern int debug;
|
|
|
|
|
|
|
|
#if defined(__GNUC__) && (__GNUC__ >= 3)
|
|
|
|
#define ATTRIBUTE(x) __attribute__ (x)
|
|
|
|
#define LIKELY(_x) __builtin_expect(!!(_x), 1)
|
|
|
|
#define UNLIKELY(_x) __builtin_expect(!!(_x), 0)
|
|
|
|
#else
|
|
|
|
#define ATTRIBUTE(x) /**/
|
|
|
|
#define LIKELY(_x) !!(_x)
|
|
|
|
#define UNLIKELY(_x) !!(_x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
|
|
|
|
#define debugf(...) \
|
|
|
|
do { \
|
|
|
|
if(UNLIKELY(debug >= 2)) do_debugf(2, __VA_ARGS__); \
|
|
|
|
} while(0)
|
|
|
|
#define kdebugf(...) \
|
|
|
|
do { \
|
|
|
|
if(UNLIKELY(debug >= 3)) do_debugf(3, __VA_ARGS__); \
|
|
|
|
} while(0)
|
|
|
|
#elif defined __GNUC__
|
|
|
|
#define debugf(_args...) \
|
|
|
|
do { \
|
|
|
|
if(UNLIKELY(debug >= 2)) do_debugf(2, _args); \
|
|
|
|
} while(0)
|
|
|
|
#define kdebugf(_args...) \
|
|
|
|
do { \
|
|
|
|
if(UNLIKELY(debug >= 3)) do_debugf(3, _args); \
|
|
|
|
} while(0)
|
|
|
|
#else
|
|
|
|
static inline void debugf(const char *format, ...) { return; }
|
|
|
|
static inline void kdebugf(const char *format, ...) { return; }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void do_debugf(int level, const char *format, ...);
|
|
|
|
|
|
|
|
|
2020-06-05 16:15:20 +02:00
|
|
|
/** format_address -- taken from babeld */
|
|
|
|
const char* format_address(const unsigned char* address);
|
|
|
|
|
2020-07-03 16:13:47 +02:00
|
|
|
/** turns a value into a human-readable one, eg. "21.2 kB" */
|
2020-07-03 18:03:26 +02:00
|
|
|
const char* human_readable_unit(double value, const char* unit);
|
2020-07-03 16:13:47 +02:00
|
|
|
|
2020-06-05 16:15:20 +02:00
|
|
|
|
2020-06-28 23:29:39 +02:00
|
|
|
/** remove the upper bit from a microsecond timestamp, to conform with the
|
|
|
|
* packet header timestamp format. */
|
|
|
|
inline uint32_t to_us_timestamp(uint32_t clock_output) {
|
|
|
|
return clock_output & 0x7fffffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-22 15:45:16 +02:00
|
|
|
/** Max seconds that can be be represented in a microseconds timestamp, on 31
|
|
|
|
* bits. */
|
|
|
|
const uint32_t MAX_USEC_DIFF = ((1U<<31) - 1) / (1000*1000) - 1;
|
|
|
|
|
|
|
|
|
|
|
|
/** Get a `struct timespec` difference, ie `t1 - t2`, as a new `struct
|
|
|
|
* timespec`. Assumes `t1 >= t2`. */
|
|
|
|
void timespec_diff(
|
|
|
|
const struct timespec t1, const struct timespec t2,
|
|
|
|
struct timespec* result);
|
|
|
|
|
|
|
|
/** Get a `struct timespec` difference, ie `t1 - t2`, expressed in
|
|
|
|
* microseconds. Assumes `t1 >= t2`. Capped to MAX_USEC_DIFF seconds. */
|
|
|
|
uint32_t timespec_us_diff(const struct timespec t1, const struct timespec t2);
|
|
|
|
|
|
|
|
/** Get the number of microseconds ellapsed since a `struct timespec`, based on
|
|
|
|
* the monotonic clock */
|
|
|
|
uint32_t timespec_us_ellapsed(const struct timespec ref);
|
|
|
|
|
|
|
|
/** Get the current timestamp, in microseconds */
|
|
|
|
uint32_t current_us_timestamp();
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-06-05 16:15:20 +02:00
|
|
|
/** in6_addr hash & equality */
|
|
|
|
namespace std {
|
|
|
|
template<>
|
|
|
|
class hash<in6_addr> {
|
|
|
|
public:
|
|
|
|
size_t operator()(const in6_addr& addr) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
class equal_to<in6_addr> {
|
|
|
|
public:
|
|
|
|
bool operator()(const in6_addr& lhs, const in6_addr& rhs) const;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-04 11:46:35 +02:00
|
|
|
|
2020-06-03 14:54:01 +02:00
|
|
|
/** MsgException -- an exception bearing a passed explanation message
|
|
|
|
*
|
|
|
|
* If `is_perror` is true, then the `strerror` corresponding message is appened
|
|
|
|
* to the message in `what()`.
|
|
|
|
*/
|
2020-06-02 13:08:23 +02:00
|
|
|
class MsgException : public std::exception {
|
|
|
|
public:
|
2020-06-03 14:54:01 +02:00
|
|
|
MsgException(const std::string& msg, int code=0, bool is_perror=false);
|
2020-06-02 13:08:23 +02:00
|
|
|
int errcode() const noexcept { return _code; }
|
|
|
|
const char* what() const noexcept { return _what.c_str(); };
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string _msg;
|
|
|
|
int _code;
|
|
|
|
std::string _what;
|
|
|
|
};
|