+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_UNSPEC) {
+ xasprintf(&str, "unspec port unspec");
+ return str;
+ } else 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);
+ }
+}
+
+void sockaddrunmap(sockaddr_t *sa) {
+ if(sa->sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->in6.sin6_addr)) {
+ sa->in.sin_addr.s_addr = ((uint32_t *) & sa->in6.sin6_addr)[3];
+ sa->in.sin_family = AF_INET;
+ }
+}
+
+void sockaddr_setport(sockaddr_t *sa, const char *port) {
+ uint16_t portnum = htons(atoi(port));
+ if(!portnum)
+ return;
+ switch(sa->sa.sa_family) {
+ case AF_INET:
+ sa->in.sin_port = portnum;
+ break;
+ case AF_INET6:
+ sa->in6.sin6_port = portnum;
+ break;
+ case AF_UNKNOWN:
+ free(sa->unknown.port);
+ sa->unknown.port = xstrdup(port);
+ default:
+ return;
+ }
+}