changeset 4617:cc89abc93e7c

clean up host:port usage, particularly with ipv6
author corvid
date So, 03 Jul 2016 17:36:47 +0000
parents 53c2580b50c5
children 8dedbcb54a36
files src/IO/http.c src/IO/tls.c src/cache.c src/url.c src/url.h
diffstat 5 files changed, 20 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/src/IO/http.c	So Jul 03 16:58:54 2016 +0000
+++ b/src/IO/http.c	So Jul 03 17:36:47 2016 +0000
@@ -570,7 +570,7 @@
          memcpy(&sin6->sin6_addr, dh->data, dh->alen);
          inet_ntop(dh->af, dh->data, buf, sizeof(buf));
          if (a_Web_valid(S->web) && (S->web->flags & WEB_RootUrl))
-            MSG("Connecting to %s:%u\n", buf, S->connect_port);
+            MSG("Connecting to [%s]:%u\n", buf, S->connect_port);
          break;
       }
 #endif
--- a/src/IO/tls.c	So Jul 03 16:58:54 2016 +0000
+++ b/src/IO/tls.c	So Jul 03 17:36:47 2016 +0000
@@ -946,8 +946,8 @@
             const char *version = mbedtls_ssl_get_version(ssl),
                        *cipher = mbedtls_ssl_get_ciphersuite(ssl);
 
-            MSG("%s: %s, cipher %s\n", URL_AUTHORITY(conn->url), version,
-                cipher);
+            MSG("%s:%d %s, cipher %s\n", URL_AUTHORITY(conn->url),
+                URL_PORT(conn->url), version, cipher);
          }
          if (srv->cert_status == CERT_STATUS_USER_ACCEPTED ||
              (Tls_examine_certificate(conn->ssl, srv) != -1)) {
@@ -1121,8 +1121,9 @@
 
       for (j = 0; j < servers_len; j++) {
          Server_t *s = dList_nth_data(ca->servers, j);
+         bool_t ipv6 = a_Url_host_type(s->hostname) == URL_HOST_IPV6;
 
-         MSG("%s:%d ", s->hostname, s->port);
+         MSG("%s%s%s:%d ", ipv6?"[":"", s->hostname, ipv6?"]":"", s->port);
       }
       MSG("\n");
    }
--- a/src/cache.c	So Jul 03 16:58:54 2016 +0000
+++ b/src/cache.c	So Jul 03 17:36:47 2016 +0000
@@ -724,7 +724,7 @@
 
    if (prefs.http_strict_transport_security &&
        !dStrAsciiCasecmp(URL_SCHEME(entry->Url), "https") &&
-       !a_Url_host_is_ip(URL_HOST(entry->Url)) &&
+       a_Url_host_type(URL_HOST(entry->Url)) == URL_HOST_NAME &&
        (hsts = Cache_parse_field(header, "Strict-Transport-Security"))) {
       a_Hsts_set(hsts, entry->Url);
       dFree(hsts);
--- a/src/url.c	So Jul 03 16:58:54 2016 +0000
+++ b/src/url.c	So Jul 03 17:36:47 2016 +0000
@@ -658,28 +658,26 @@
 }
 
 /*
- * Is the provided hostname an IP address?
+ * What type of host is this?
  */
-bool_t a_Url_host_is_ip(const char *host)
+int a_Url_host_type(const char *host)
 {
    uint_t len;
 
    if (!host || !*host)
-      return FALSE;
+      return URL_HOST_ERROR;
 
    len = strlen(host);
 
    if (len == strspn(host, "0123456789.")) {
-      _MSG("an IPv4 address\n");
-      return TRUE;
+      return URL_HOST_IPV4;
    }
    if (strchr(host, ':') &&
        (len == strspn(host, "0123456789abcdefABCDEF:."))) {
       /* The precise format is shown in section 3.2.2 of rfc 3986 */
-      MSG("an IPv6 address\n");
-      return TRUE;
+      return URL_HOST_IPV6;
    }
-   return FALSE;
+   return URL_HOST_NAME;
 }
 
 /*
@@ -746,7 +744,7 @@
    const char *s;
    uint_t dots;
 
-   if (!host || !*host || a_Url_host_is_ip(host))
+   if (a_Url_host_type(host) != URL_HOST_NAME)
       return host;
 
    s = host;
--- a/src/url.h	So Jul 03 16:58:54 2016 +0000
+++ b/src/url.h	So Jul 03 17:36:47 2016 +0000
@@ -16,6 +16,12 @@
 #define URL_HTTP_PORT        80
 #define URL_HTTPS_PORT       443
 
+/* for a_Url_host_type() */
+#define URL_HOST_ERROR      -1
+#define URL_HOST_NAME        0
+#define URL_HOST_IPV4        1
+#define URL_HOST_IPV6        2
+
 /*
  * Values for DilloUrl->flags.
  * Specifies which which action to perform with an URL.
@@ -106,7 +112,7 @@
 char *a_Url_decode_hex_str(const char *str);
 char *a_Url_encode_hex_str(const char *str);
 char *a_Url_string_strip_delimiters(const char *str);
-bool_t a_Url_host_is_ip(const char *host);
+int a_Url_host_type(const char *host);
 bool_t a_Url_same_organization(const DilloUrl *u1, const DilloUrl *u2);
 #ifdef __cplusplus
 }