Check hub addresses for sources hubs.
[doldaconnect.git] / daemon / net.c
index a5ff113..576ea6f 100644 (file)
@@ -90,7 +90,7 @@ int getpublicaddr(int af, struct sockaddr **addr, socklen_t *lenbuf)
            *lenbuf = sizeof(*ipv4);
            return(0);
        }
-       if((pif = icwcstombs(confgetstr("net", "publicif"), NULL)) == NULL)
+       if((pif = icswcstombs(confgetstr("net", "publicif"), NULL, NULL)) == NULL)
        {
            flog(LOG_ERR, "could not convert net.publicif into local charset: %s", strerror(errno));
            return(-1);
@@ -128,12 +128,14 @@ int getpublicaddr(int af, struct sockaddr **addr, socklen_t *lenbuf)
                    memcpy(ipv4, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
                } else {
                    free(ipv4);
+                   free(conf.ifc_buf);
                    flog(LOG_WARNING, "could not locate an unambiguous interface for determining your public IP address - set net.publicif");
                    errno = ENFILE; /* XXX: There's no appropriate one for this... */
                    return(-1);
                }
            }
        }
+       free(conf.ifc_buf);
        close(sock);
        if(ipv4 != NULL)
        {
@@ -1090,6 +1092,40 @@ int sockgetremotename(struct socket *sk, struct sockaddr **namebuf, socklen_t *l
     }
 }
 
+int addreq(struct sockaddr *x, struct sockaddr *y)
+{
+    struct sockaddr_un *u1, *u2;
+    struct sockaddr_in *n1, *n2;
+#ifdef HAVE_IPV6
+    struct sockaddr_in6 *s1, *s2;
+#endif
+    
+    if(x->sa_family != y->sa_family)
+       return(0);
+    switch(x->sa_family) {
+    case AF_UNIX:
+       u1 = (struct sockaddr_un *)x; u2 = (struct sockaddr_un *)y;
+       if(strncmp(u1->sun_path, u2->sun_path, sizeof(u1->sun_path)))
+           return(0);
+       break;
+    case AF_INET:
+       n1 = (struct sockaddr_in *)x; n2 = (struct sockaddr_in *)y;
+       if(n1->sin_port != n2->sin_port)
+           return(0);
+       if(n1->sin_addr.s_addr != n2->sin_addr.s_addr)
+           return(0);
+       break;
+    case AF_INET6:
+       s1 = (struct sockaddr_in6 *)x; s2 = (struct sockaddr_in6 *)y;
+       if(s1->sin6_port != s2->sin6_port)
+           return(0);
+       if(memcmp(s1->sin6_addr.s6_addr, s2->sin6_addr.s6_addr, sizeof(s1->sin6_addr.s6_addr)))
+           return(0);
+       break;
+    }
+    return(1);
+}
+
 char *formataddress(struct sockaddr *arg, socklen_t arglen)
 {
     struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with
@@ -1122,7 +1158,7 @@ char *formataddress(struct sockaddr *arg, socklen_t arglen)
        ipv6 = (struct sockaddr_in6 *)arg;
        if(inet_ntop(AF_INET6, &ipv6->sin6_addr, buf, sizeof(buf)) == NULL)
            return(NULL);
-       ret = sprintf2("%s:%i", buf, (int)ntohs(ipv6->sin6_port));
+       ret = sprintf2("[%s]:%i", buf, (int)ntohs(ipv6->sin6_port));
        break;
 #endif
     default: