net: Simplify makesock().
[paraslash.git] / net.c
diff --git a/net.c b/net.c
index 2ec3f03e546b3b1f3c038b7eea2e9cf8cf8a3438..70d3ec0f2d1ad297a8249acb3bd0f1a1a0815eb9 100644 (file)
--- a/net.c
+++ b/net.c
@@ -142,9 +142,9 @@ static bool host_string_ok(const char *host)
  * \param hostlen The maximum length of \a host.
  * \param port   To return the port number (if any) of \a url.
  *
- * \return Pointer to \a host, or NULL if failed.
- * If NULL is returned, \a host and \a portnum are undefined. If no
- * port number was present in \a url, \a portnum is set to -1.
+ * \return Pointer to \a host, or \p NULL if failed.  If \p NULL is returned,
+ * \a host and \a port are undefined. If no port number was present in \a url,
+ * \a port is set to -1.
  *
  * \sa RFC 3986, 3.2.2/3.2.3
  */
@@ -168,16 +168,16 @@ char *parse_url(const char *url,
                if (*o++ != ']' || (*o != '\0' && *o != ':'))
                        goto failed;
        } else {
-               for (; (*c = *o == ':'? '\0' : *o); c++, o++)
-                       if (c == end)
+               for (; (*c = *o == ':'? '\0' : *o); c++, o++) {
+                       if (c == end && o[1])
                                goto failed;
+               }
        }
 
        if (*o == ':')
                if (para_atoi32(++o, port) < 0 ||
                    *port < 0 || *port > 0xffff)
                        goto failed;
-
        if (host_string_ok(host))
                return host;
 failed:
@@ -377,7 +377,6 @@ int makesock(unsigned l4type, bool passive,
 {
        struct addrinfo *local = NULL, *src = NULL, *remote = NULL,
                *dst = NULL, hints;
-       unsigned int    l3type = AF_UNSPEC;
        int             rc, on = 1, sockfd = -1,
                        socktype = sock_type(l4type);
        char port[6]; /* port number has at most 5 digits */
@@ -385,7 +384,7 @@ int makesock(unsigned l4type, bool passive,
        sprintf(port, "%u", port_number);
        /* Set up address hint structure */
        memset(&hints, 0, sizeof(hints));
-       hints.ai_family = l3type;
+       hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = socktype;
        /* 
         * getaddrinfo does not support SOCK_DCCP, so for the sake of lookup
@@ -396,9 +395,6 @@ int makesock(unsigned l4type, bool passive,
 
        /* only use addresses available on the host */
        hints.ai_flags = AI_ADDRCONFIG;
-       if (l3type == AF_INET6)
-               /* use v4-mapped-v6 if no v6 addresses found */
-               hints.ai_flags |= AI_V4MAPPED | AI_ALL;
 
        if (passive && host == NULL)
                hints.ai_flags |= AI_PASSIVE;
@@ -672,19 +668,19 @@ char *remote_name(int sockfd)
 
 /**
  * Extract IPv4 or IPv6-mapped-IPv4 address from sockaddr_storage.
- * \param ss Container of IPv4/6 address
- * \return Extracted IPv4 address (different from 0) or 0 if unsuccessful.
  *
- * \sa RFC 3493
+ * \param ss Container of IPv4/6 address.
+ * \param ia Extracted IPv4 address (different from 0) or 0 if unsuccessful.
+ *
+ * \sa RFC 3493.
  */
-struct in_addr extract_v4_addr(const struct sockaddr_storage *ss)
+void extract_v4_addr(const struct sockaddr_storage *ss, struct in_addr *ia)
 {
-       struct in_addr ia = {.s_addr = 0};
        const struct sockaddr *sa = normalize_ip_address(ss);
 
+       memset(ia, 0, sizeof(*ia));
        if (sa->sa_family == AF_INET)
-               ia = ((struct sockaddr_in *)sa)->sin_addr;
-       return ia;
+               *ia = ((struct sockaddr_in *)sa)->sin_addr;
 }
 
 /**