128 lines
3.1 KiB
C++
128 lines
3.1 KiB
C++
#include <cstdio>
|
|
#include <string.h>
|
|
#include <stdarg.h>
|
|
#include <arpa/inet.h>
|
|
|
|
#include "util.hpp"
|
|
|
|
int debug;
|
|
|
|
void do_debugf(int level, const char *format, ...)
|
|
{
|
|
va_list args;
|
|
va_start(args, format);
|
|
if(debug >= level) {
|
|
vfprintf(stderr, format, args);
|
|
fflush(stderr);
|
|
}
|
|
va_end(args);
|
|
}
|
|
|
|
const char *
|
|
format_address(const unsigned char *address)
|
|
{
|
|
static char buf[4][INET6_ADDRSTRLEN];
|
|
static int i = 0;
|
|
i = (i + 1) % 4;
|
|
/*
|
|
if(v4mapped(address))
|
|
inet_ntop(AF_INET, address + 12, buf[i], INET6_ADDRSTRLEN);
|
|
else
|
|
*/
|
|
inet_ntop(AF_INET6, address, buf[i], INET6_ADDRSTRLEN);
|
|
return buf[i];
|
|
}
|
|
|
|
const char*
|
|
human_readable_unit(double value, const char* unit) {
|
|
static char buf[4][24];
|
|
static int cur_buf = 0;
|
|
static const char MULTIPLIERS[] = {' ', 'k', 'M', 'G', 'T'};
|
|
static const int NB_MULTIPLIERS = 5;
|
|
|
|
int multiplier = 0;
|
|
double div_value = value;
|
|
|
|
cur_buf = (cur_buf + 1) % 4;
|
|
while(div_value >= 1000 && multiplier < NB_MULTIPLIERS) {
|
|
div_value /= 1000;
|
|
multiplier++;
|
|
}
|
|
snprintf(buf[cur_buf], 24, "%.1lf %c%s",
|
|
div_value, MULTIPLIERS[multiplier], unit);
|
|
return buf[cur_buf];
|
|
}
|
|
|
|
void
|
|
timespec_diff(const struct timespec t1, const struct timespec t2,
|
|
struct timespec* result)
|
|
{
|
|
result->tv_nsec = t1.tv_nsec - t2.tv_nsec;
|
|
result->tv_sec = t1.tv_sec - t2.tv_sec;
|
|
if(result->tv_nsec < 0) {
|
|
result->tv_nsec += 1000*1000*1000;
|
|
result->tv_sec--;
|
|
}
|
|
}
|
|
|
|
uint32_t timespec_us_diff(const struct timespec t1, const struct timespec t2) {
|
|
long nsec_diff = t1.tv_nsec - t2.tv_nsec;
|
|
time_t sec_diff = t1.tv_sec - t2.tv_sec;
|
|
if(nsec_diff < 0) {
|
|
nsec_diff += 1000*1000*1000;
|
|
sec_diff--;
|
|
}
|
|
|
|
if(sec_diff > MAX_USEC_DIFF)
|
|
sec_diff = MAX_USEC_DIFF;
|
|
return (nsec_diff / 1000) + (sec_diff * 1000*1000);
|
|
}
|
|
|
|
uint32_t timespec_us_ellapsed(const struct timespec ref) {
|
|
struct timespec now;
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
return timespec_us_diff(now, ref);
|
|
}
|
|
|
|
uint32_t current_us_timestamp() {
|
|
struct timespec now;
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
return (now.tv_sec * 1000*1000) + (now.tv_nsec / 1000);
|
|
}
|
|
|
|
namespace std {
|
|
size_t hash<in6_addr>::operator() (const in6_addr& addr) const {
|
|
size_t out_hash = 0;
|
|
for(int i=0; i < 4; ++i) {
|
|
uint32_t value;
|
|
memcpy((unsigned char*)(&value),
|
|
addr.s6_addr + 4*i,
|
|
4);
|
|
out_hash ^= (std::hash<uint32_t>{}(value) << 1);
|
|
}
|
|
return out_hash;
|
|
}
|
|
|
|
bool equal_to<in6_addr>::operator()(
|
|
const in6_addr& lhs, const in6_addr& rhs) const
|
|
{
|
|
return memcmp(lhs.s6_addr, rhs.s6_addr, sizeof(lhs.s6_addr)) == 0;
|
|
}
|
|
}
|
|
|
|
MsgException::MsgException(const std::string& msg, int code, bool is_perror)
|
|
: _msg(msg), _code(code)
|
|
{
|
|
_what = _msg;
|
|
|
|
if(_code != 0) {
|
|
if(is_perror) {
|
|
_what += ": ";
|
|
_what += strerror(errno);
|
|
}
|
|
|
|
char remainder[20];
|
|
sprintf(remainder, " (code %d)", _code);
|
|
_what += remainder;
|
|
}
|
|
}
|