Releasing 1.0.29.
[tinc] / src / netutl.c
index 56fb65f..e473f17 100644 (file)
@@ -1,7 +1,7 @@
 /*
     netutl.c -- some supporting network utility code
     Copyright (C) 1998-2005 Ivo Timmermans
-                  2000-2009 Guus Sliepen <guus@tinc-vpn.org>
+                  2000-2016 Guus Sliepen <guus@tinc-vpn.org>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -33,12 +33,16 @@ bool hostnames = false;
   Return NULL on failure.
 */
 struct addrinfo *str2addrinfo(const char *address, const char *service, int socktype) {
-       struct addrinfo *ai, hint = {0};
+       struct addrinfo *ai = NULL, hint = {0};
        int err;
 
        hint.ai_family = addressfamily;
        hint.ai_socktype = socktype;
 
+#if HAVE_DECL_RES_INIT
+       // ensure glibc reloads /etc/resolv.conf.
+       res_init();
+#endif
        err = getaddrinfo(address, service, &hint, &ai);
 
        if(err) {
@@ -51,7 +55,7 @@ struct addrinfo *str2addrinfo(const char *address, const char *service, int sock
 }
 
 sockaddr_t str2sockaddr(const char *address, const char *port) {
-       struct addrinfo *ai, hint = {0};
+       struct addrinfo *ai = NULL, hint = {0};
        sockaddr_t result;
        int err;
 
@@ -83,8 +87,10 @@ void sockaddr2str(const sockaddr_t *sa, char **addrstr, char **portstr) {
        int err;
 
        if(sa->sa.sa_family == AF_UNKNOWN) {
-               *addrstr = xstrdup(sa->unknown.address);
-               *portstr = xstrdup(sa->unknown.port);
+               if(addrstr)
+                       *addrstr = xstrdup(sa->unknown.address);
+               if(portstr)
+                       *portstr = xstrdup(sa->unknown.port);
                return;
        }
 
@@ -225,6 +231,25 @@ void sockaddrunmap(sockaddr_t *sa) {
        }
 }
 
+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;
+       }
+}
+
 /* Subnet mask handling */
 
 int maskcmp(const void *va, const void *vb, int masklen) {