+void sockaddr2str(const sockaddr_t *sa, char **addrstr, char **portstr) {
+ char address[NI_MAXHOST];
+ char port[NI_MAXSERV];
+ char *scopeid;
+ int err;
+
+ if(sa->sa.sa_family == AF_UNKNOWN) {
+ if(addrstr)
+ *addrstr = xstrdup(sa->unknown.address);
+ if(portstr)
+ *portstr = xstrdup(sa->unknown.port);
+ return;
+ }
+
+ err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof address, port, sizeof port, NI_NUMERICHOST | NI_NUMERICSERV);
+
+ if(err) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Error while translating addresses: %s", err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
+ abort();
+ }
+
+ scopeid = strchr(address, '%');
+
+ if(scopeid)
+ *scopeid = '\0'; /* Descope. */
+
+ if(addrstr)
+ *addrstr = xstrdup(address);
+ if(portstr)
+ *portstr = xstrdup(port);
+}
+
+char *sockaddr2hostname(const sockaddr_t *sa) {
+ char *str;
+ char address[NI_MAXHOST] = "unknown";
+ char port[NI_MAXSERV] = "unknown";
+ int err;
+
+ if(sa->sa.sa_family == AF_UNKNOWN) {
+ xasprintf(&str, "%s port %s", sa->unknown.address, sa->unknown.port);
+ return str;
+ }
+
+ err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof address, port, sizeof port,
+ hostnames ? 0 : (NI_NUMERICHOST | NI_NUMERICSERV));
+ if(err) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Error while looking up hostname: %s", err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
+ }
+
+ xasprintf(&str, "%s port %s", address, port);
+
+ return str;
+}
+
+int sockaddrcmp_noport(const sockaddr_t *a, const sockaddr_t *b) {
+ int result;
+
+ result = a->sa.sa_family - b->sa.sa_family;
+
+ if(result)
+ return result;
+
+ switch (a->sa.sa_family) {
+ case AF_UNSPEC:
+ return 0;
+
+ case AF_UNKNOWN:
+ return strcmp(a->unknown.address, b->unknown.address);
+
+ case AF_INET:
+ return memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a->in.sin_addr));
+
+ case AF_INET6:
+ return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr));
+
+ default:
+ logger(DEBUG_ALWAYS, LOG_ERR, "sockaddrcmp() was called with unknown address family %d, exitting!",
+ a->sa.sa_family);
+ abort();
+ }
+}
+
+int sockaddrcmp(const sockaddr_t *a, const sockaddr_t *b) {
+ int result;
+
+ result = a->sa.sa_family - b->sa.sa_family;
+
+ if(result)
+ return result;
+
+ switch (a->sa.sa_family) {
+ case AF_UNSPEC:
+ return 0;
+
+ case AF_UNKNOWN:
+ result = strcmp(a->unknown.address, b->unknown.address);
+
+ if(result)
+ return result;
+
+ return strcmp(a->unknown.port, b->unknown.port);
+
+ case AF_INET:
+ result = memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof a->in.sin_addr);
+
+ if(result)
+ return result;
+
+ return memcmp(&a->in.sin_port, &b->in.sin_port, sizeof a->in.sin_port);
+
+ case AF_INET6:
+ result = memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof a->in6.sin6_addr);
+
+ if(result)
+ return result;
+
+ return memcmp(&a->in6.sin6_port, &b->in6.sin6_port, sizeof a->in6.sin6_port);
+
+ default:
+ logger(DEBUG_ALWAYS, LOG_ERR, "sockaddrcmp() was called with unknown address family %d, exitting!",
+ a->sa.sa_family);
+ abort();
+ }
+}
+
+void sockaddrcpy(sockaddr_t *a, const sockaddr_t *b) {
+ if(b->sa.sa_family != AF_UNKNOWN) {
+ *a = *b;
+ } else {
+ a->unknown.family = AF_UNKNOWN;
+ a->unknown.address = xstrdup(b->unknown.address);
+ a->unknown.port = xstrdup(b->unknown.port);
+ }
+}
+
+void sockaddrfree(sockaddr_t *a) {
+ if(a->sa.sa_family == AF_UNKNOWN) {
+ free(a->unknown.address);
+ free(a->unknown.port);
+ }