2020-06-02 13:08:23 +02:00
|
|
|
#include <cstdio>
|
2020-06-03 14:54:01 +02:00
|
|
|
#include <string.h>
|
2020-06-04 11:46:35 +02:00
|
|
|
#include <stdarg.h>
|
2020-06-05 16:15:20 +02:00
|
|
|
#include <arpa/inet.h>
|
2020-06-02 13:08:23 +02:00
|
|
|
|
|
|
|
#include "util.hpp"
|
|
|
|
|
2020-06-04 11:46:35 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-06-05 16:15:20 +02:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 14:54:01 +02:00
|
|
|
MsgException::MsgException(const std::string& msg, int code, bool is_perror)
|
2020-06-02 13:08:23 +02:00
|
|
|
: _msg(msg), _code(code)
|
|
|
|
{
|
|
|
|
_what = _msg;
|
|
|
|
|
|
|
|
if(_code != 0) {
|
2020-06-03 14:54:01 +02:00
|
|
|
if(is_perror) {
|
|
|
|
_what += ": ";
|
|
|
|
_what += strerror(errno);
|
|
|
|
}
|
|
|
|
|
2020-06-02 13:08:23 +02:00
|
|
|
char remainder[20];
|
|
|
|
sprintf(remainder, " (code %d)", _code);
|
|
|
|
_what += remainder;
|
|
|
|
}
|
|
|
|
}
|