Blame SOURCES/openssl-1.0.2a-ipv6-apps.patch

e62613
diff -up openssl-1.0.2a/apps/s_apps.h.ipv6-apps openssl-1.0.2a/apps/s_apps.h
e62613
--- openssl-1.0.2a/apps/s_apps.h.ipv6-apps	2015-04-20 15:01:24.029120104 +0200
e62613
+++ openssl-1.0.2a/apps/s_apps.h	2015-04-20 15:05:00.353137701 +0200
e62613
@@ -151,7 +151,7 @@ typedef fd_mask fd_set;
e62613
 #define PORT_STR        "4433"
e62613
 #define PROTOCOL        "tcp"
e62613
 
e62613
-int do_server(int port, int type, int *ret,
e62613
+int do_server(char *port, int type, int *ret,
e62613
               int (*cb) (char *hostname, int s, int stype,
e62613
                          unsigned char *context), unsigned char *context,
e62613
               int naccept);
e62613
@@ -167,11 +167,10 @@ int ssl_print_point_formats(BIO *out, SS
e62613
 int ssl_print_curves(BIO *out, SSL *s, int noshared);
e62613
 #endif
e62613
 int ssl_print_tmp_key(BIO *out, SSL *s);
e62613
-int init_client(int *sock, char *server, int port, int type);
e62613
+int init_client(int *sock, char *server, char *port, int type);
e62613
 int should_retry(int i);
e62613
 int extract_port(char *str, short *port_ptr);
e62613
-int extract_host_port(char *str, char **host_ptr, unsigned char *ip,
e62613
-                      short *p);
e62613
+int extract_host_port(char *str, char **host_ptr, char **port_ptr);
e62613
 
e62613
 long MS_CALLBACK bio_dump_callback(BIO *bio, int cmd, const char *argp,
e62613
                                    int argi, long argl, long ret);
e62613
diff -up openssl-1.0.2a/apps/s_client.c.ipv6-apps openssl-1.0.2a/apps/s_client.c
e62613
--- openssl-1.0.2a/apps/s_client.c.ipv6-apps	2015-04-20 15:01:24.022119942 +0200
e62613
+++ openssl-1.0.2a/apps/s_client.c	2015-04-20 15:06:42.338503234 +0200
e62613
@@ -662,7 +662,7 @@ int MAIN(int argc, char **argv)
e62613
     int cbuf_len, cbuf_off;
e62613
     int sbuf_len, sbuf_off;
e62613
     fd_set readfds, writefds;
e62613
-    short port = PORT;
e62613
+    char *port_str = PORT_STR;
e62613
     int full_log = 1;
e62613
     char *host = SSL_HOST_NAME;
e62613
     char *cert_file = NULL, *key_file = NULL, *chain_file = NULL;
e62613
@@ -785,13 +785,11 @@ int MAIN(int argc, char **argv)
e62613
         } else if (strcmp(*argv, "-port") == 0) {
e62613
             if (--argc < 1)
e62613
                 goto bad;
e62613
-            port = atoi(*(++argv));
e62613
-            if (port == 0)
e62613
-                goto bad;
e62613
+            port_str = *(++argv);
e62613
         } else if (strcmp(*argv, "-connect") == 0) {
e62613
             if (--argc < 1)
e62613
                 goto bad;
e62613
-            if (!extract_host_port(*(++argv), &host, NULL, &port))
e62613
+            if (!extract_host_port(*(++argv), &host, &port_str))
e62613
                 goto bad;
e62613
         } else if (strcmp(*argv, "-verify") == 0) {
e62613
             verify = SSL_VERIFY_PEER;
e62613
@@ -1417,7 +1415,7 @@ int MAIN(int argc, char **argv)
e62613
 
e62613
  re_start:
e62613
 
e62613
-    if (init_client(&s, host, port, socket_type) == 0) {
e62613
+    if (init_client(&s, host, port_str, socket_type) == 0) {
e62613
         BIO_printf(bio_err, "connect:errno=%d\n", get_last_socket_error());
e62613
         SHUTDOWN(s);
e62613
         goto end;
e62613
diff -up openssl-1.0.2a/apps/s_server.c.ipv6-apps openssl-1.0.2a/apps/s_server.c
e62613
--- openssl-1.0.2a/apps/s_server.c.ipv6-apps	2015-04-20 15:01:24.030120127 +0200
e62613
+++ openssl-1.0.2a/apps/s_server.c	2015-04-20 15:10:47.245187746 +0200
e62613
@@ -1061,7 +1061,7 @@ int MAIN(int argc, char *argv[])
e62613
 {
e62613
     X509_VERIFY_PARAM *vpm = NULL;
e62613
     int badarg = 0;
e62613
-    short port = PORT;
e62613
+    char *port_str = PORT_STR;
e62613
     char *CApath = NULL, *CAfile = NULL;
e62613
     char *chCApath = NULL, *chCAfile = NULL;
e62613
     char *vfyCApath = NULL, *vfyCAfile = NULL;
e62613
@@ -1148,7 +1148,8 @@ int MAIN(int argc, char *argv[])
e62613
         if ((strcmp(*argv, "-port") == 0) || (strcmp(*argv, "-accept") == 0)) {
e62613
             if (--argc < 1)
e62613
                 goto bad;
e62613
-            if (!extract_port(*(++argv), &port))
e62613
+            port_str = *(++argv);
e62613
+            if (port_str == NULL || *port_str == '\0')
e62613
                 goto bad;
e62613
         } else if (strcmp(*argv, "-naccept") == 0) {
e62613
             if (--argc < 1)
e62613
@@ -2020,13 +2021,13 @@ int MAIN(int argc, char *argv[])
e62613
     BIO_printf(bio_s_out, "ACCEPT\n");
e62613
     (void)BIO_flush(bio_s_out);
e62613
     if (rev)
e62613
-        do_server(port, socket_type, &accept_socket, rev_body, context,
e62613
+        do_server(port_str, socket_type, &accept_socket, rev_body, context,
e62613
                   naccept);
e62613
     else if (www)
e62613
-        do_server(port, socket_type, &accept_socket, www_body, context,
e62613
+        do_server(port_str, socket_type, &accept_socket, www_body, context,
e62613
                   naccept);
e62613
     else
e62613
-        do_server(port, socket_type, &accept_socket, sv_body, context,
e62613
+        do_server(port_str, socket_type, &accept_socket, sv_body, context,
e62613
                   naccept);
e62613
     print_stats(bio_s_out, ctx);
e62613
     ret = 0;
e62613
diff -up openssl-1.0.2a/apps/s_socket.c.ipv6-apps openssl-1.0.2a/apps/s_socket.c
e62613
--- openssl-1.0.2a/apps/s_socket.c.ipv6-apps	2015-03-19 14:30:36.000000000 +0100
e62613
+++ openssl-1.0.2a/apps/s_socket.c	2015-04-20 15:32:53.960079507 +0200
e62613
@@ -106,9 +106,7 @@ static struct hostent *GetHostByName(cha
e62613
 static void ssl_sock_cleanup(void);
e62613
 # endif
e62613
 static int ssl_sock_init(void);
e62613
-static int init_client_ip(int *sock, unsigned char ip[4], int port, int type);
e62613
-static int init_server(int *sock, int port, int type);
e62613
-static int init_server_long(int *sock, int port, char *ip, int type);
e62613
+static int init_server(int *sock, char *port, int type);
e62613
 static int do_accept(int acc_sock, int *sock, char **host);
e62613
 static int host_ip(char *str, unsigned char ip[4]);
e62613
 
e62613
@@ -231,65 +229,66 @@ static int ssl_sock_init(void)
e62613
     return (1);
e62613
 }
e62613
 
e62613
-int init_client(int *sock, char *host, int port, int type)
e62613
+int init_client(int *sock, char *host, char *port, int type)
e62613
 {
e62613
-    unsigned char ip[4];
e62613
-
e62613
-    memset(ip, '\0', sizeof ip);
e62613
-    if (!host_ip(host, &(ip[0])))
e62613
-        return 0;
e62613
-    return init_client_ip(sock, ip, port, type);
e62613
-}
e62613
-
e62613
-static int init_client_ip(int *sock, unsigned char ip[4], int port, int type)
e62613
-{
e62613
-    unsigned long addr;
e62613
-    struct sockaddr_in them;
e62613
-    int s, i;
e62613
+    struct addrinfo *res, *res0, hints;
e62613
+    char *failed_call = NULL;
e62613
+    int s;
e62613
+    int e;
e62613
 
e62613
     if (!ssl_sock_init())
e62613
         return (0);
e62613
 
e62613
-    memset((char *)&them, 0, sizeof(them));
e62613
-    them.sin_family = AF_INET;
e62613
-    them.sin_port = htons((unsigned short)port);
e62613
-    addr = (unsigned long)
e62613
-        ((unsigned long)ip[0] << 24L) |
e62613
-        ((unsigned long)ip[1] << 16L) |
e62613
-        ((unsigned long)ip[2] << 8L) | ((unsigned long)ip[3]);
e62613
-    them.sin_addr.s_addr = htonl(addr);
e62613
-
e62613
-    if (type == SOCK_STREAM)
e62613
-        s = socket(AF_INET, SOCK_STREAM, SOCKET_PROTOCOL);
e62613
-    else                        /* ( type == SOCK_DGRAM) */
e62613
-        s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
e62613
-
e62613
-    if (s == INVALID_SOCKET) {
e62613
-        perror("socket");
e62613
+    memset(&hints, '\0', sizeof(hints));
e62613
+    hints.ai_socktype = type;
e62613
+    hints.ai_flags = AI_ADDRCONFIG;
e62613
+
e62613
+    e = getaddrinfo(host, port, &hints, &res;;
e62613
+    if (e) {
e62613
+        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(e));
e62613
+        if (e == EAI_SYSTEM)
e62613
+            perror("getaddrinfo");
e62613
         return (0);
e62613
     }
e62613
+
e62613
+    res0 = res;
e62613
+    while (res) {
e62613
+        s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
e62613
+        if (s == INVALID_SOCKET) {
e62613
+            failed_call = "socket";
e62613
+            goto nextres;
e62613
+        }
e62613
 # if defined(SO_KEEPALIVE) && !defined(OPENSSL_SYS_MPE)
e62613
-    if (type == SOCK_STREAM) {
e62613
-        i = 0;
e62613
-        i = setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, sizeof(i));
e62613
-        if (i < 0) {
e62613
-            closesocket(s);
e62613
-            perror("keepalive");
e62613
-            return (0);
e62613
+        if (type == SOCK_STREAM) {
e62613
+            int i = 0;
e62613
+            i = setsockopt(s, SOL_SOCKET, SO_KEEPALIVE,
e62613
+                           (char *)&i, sizeof(i));
e62613
+            if (i < 0) {
e62613
+                failed_call = "keepalive";
e62613
+                goto nextres;
e62613
+            }
e62613
         }
e62613
-    }
e62613
 # endif
e62613
-
e62613
-    if (connect(s, (struct sockaddr *)&them, sizeof(them)) == -1) {
e62613
-        closesocket(s);
e62613
-        perror("connect");
e62613
-        return (0);
e62613
+        if (connect(s, (struct sockaddr *)res->ai_addr, res->ai_addrlen) == 0) {
e62613
+            freeaddrinfo(res0);
e62613
+            *sock = s;
e62613
+            return (1);
e62613
+        }
e62613
+
e62613
+        failed_call = "socket";
e62613
+ nextres:
e62613
+        if (s != INVALID_SOCKET)
e62613
+            close(s);
e62613
+        res = res->ai_next;
e62613
     }
e62613
-    *sock = s;
e62613
-    return (1);
e62613
+    freeaddrinfo(res0);
e62613
+    closesocket(s);
e62613
+
e62613
+    perror(failed_call);
e62613
+    return (0);
e62613
 }
e62613
 
e62613
-int do_server(int port, int type, int *ret,
e62613
+int do_server(char *port, int type, int *ret,
e62613
               int (*cb) (char *hostname, int s, int stype,
e62613
                          unsigned char *context), unsigned char *context,
e62613
               int naccept)
e62613
@@ -328,69 +327,89 @@ int do_server(int port, int type, int *r
e62613
     }
e62613
 }
e62613
 
e62613
-static int init_server_long(int *sock, int port, char *ip, int type)
e62613
+static int init_server(int *sock, char *port, int type)
e62613
 {
e62613
-    int ret = 0;
e62613
-    struct sockaddr_in server;
e62613
-    int s = -1;
e62613
+    struct addrinfo *res, *res0 = NULL, hints;
e62613
+    char *failed_call = NULL;
e62613
+    int s = INVALID_SOCKET;
e62613
+    int e;
e62613
 
e62613
     if (!ssl_sock_init())
e62613
         return (0);
e62613
 
e62613
-    memset((char *)&server, 0, sizeof(server));
e62613
-    server.sin_family = AF_INET;
e62613
-    server.sin_port = htons((unsigned short)port);
e62613
-    if (ip == NULL)
e62613
-        server.sin_addr.s_addr = INADDR_ANY;
e62613
-    else
e62613
-/* Added for T3E, address-of fails on bit field (beckman@acl.lanl.gov) */
e62613
-# ifndef BIT_FIELD_LIMITS
e62613
-        memcpy(&server.sin_addr.s_addr, ip, 4);
e62613
-# else
e62613
-        memcpy(&server.sin_addr, ip, 4);
e62613
-# endif
e62613
-
e62613
-    if (type == SOCK_STREAM)
e62613
-        s = socket(AF_INET, SOCK_STREAM, SOCKET_PROTOCOL);
e62613
-    else                        /* type == SOCK_DGRAM */
e62613
-        s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
e62613
+    memset(&hints, '\0', sizeof(hints));
e62613
+    hints.ai_family = AF_INET6;
e62613
+ tryipv4:
e62613
+    hints.ai_socktype = type;
e62613
+    hints.ai_flags = AI_PASSIVE;
e62613
+
e62613
+    e = getaddrinfo(NULL, port, &hints, &res;;
e62613
+    if (e) {
e62613
+        if (hints.ai_family == AF_INET) {
e62613
+            fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(e));
e62613
+            if (e == EAI_SYSTEM)
e62613
+                perror("getaddrinfo");
e62613
+            return (0);
e62613
+        } else
e62613
+            res = NULL;
e62613
+    }
e62613
 
e62613
-    if (s == INVALID_SOCKET)
e62613
-        goto err;
e62613
+    res0 = res;
e62613
+    while (res) {
e62613
+        s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
e62613
+        if (s == INVALID_SOCKET) {
e62613
+            failed_call = "socket";
e62613
+            goto nextres;
e62613
+        }
e62613
+        if (hints.ai_family == AF_INET6) {
e62613
+            int j = 0;
e62613
+            setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&j, sizeof j);
e62613
+        }
e62613
 # if defined SOL_SOCKET && defined SO_REUSEADDR
e62613
-    {
e62613
-        int j = 1;
e62613
-        setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&j, sizeof j);
e62613
-    }
e62613
-# endif
e62613
-    if (bind(s, (struct sockaddr *)&server, sizeof(server)) == -1) {
e62613
-# ifndef OPENSSL_SYS_WINDOWS
e62613
-        perror("bind");
e62613
+        {
e62613
+            int j = 1;
e62613
+            setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&j, sizeof j);
e62613
+        }
e62613
 # endif
e62613
-        goto err;
e62613
+
e62613
+        if (bind(s, (struct sockaddr *)res->ai_addr, res->ai_addrlen) == -1) {
e62613
+            failed_call = "bind";
e62613
+            goto nextres;
e62613
+        }
e62613
+        if (type == SOCK_STREAM && listen(s, 128) == -1) {
e62613
+            failed_call = "listen";
e62613
+            goto nextres;
e62613
+        }
e62613
+
e62613
+        *sock = s;
e62613
+        return (1);
e62613
+
e62613
+ nextres:
e62613
+        if (s != INVALID_SOCKET)
e62613
+            close(s);
e62613
+        res = res->ai_next;
e62613
     }
e62613
-    /* Make it 128 for linux */
e62613
-    if (type == SOCK_STREAM && listen(s, 128) == -1)
e62613
-        goto err;
e62613
-    *sock = s;
e62613
-    ret = 1;
e62613
- err:
e62613
-    if ((ret == 0) && (s != -1)) {
e62613
-        SHUTDOWN(s);
e62613
+    if (res0)
e62613
+        freeaddrinfo(res0);
e62613
+
e62613
+    if (s == INVALID_SOCKET) {
e62613
+        if (hints.ai_family == AF_INET6) {
e62613
+            hints.ai_family = AF_INET;
e62613
+            goto tryipv4;
e62613
+        }
e62613
+        perror("socket");
e62613
+        return (0);
e62613
     }
e62613
-    return (ret);
e62613
-}
e62613
 
e62613
-static int init_server(int *sock, int port, int type)
e62613
-{
e62613
-    return (init_server_long(sock, port, NULL, type));
e62613
+    perror(failed_call);
e62613
+    return (0);
e62613
 }
e62613
 
e62613
 static int do_accept(int acc_sock, int *sock, char **host)
e62613
 {
e62613
+    static struct sockaddr_storage from;
e62613
+    char buffer[NI_MAXHOST];
e62613
     int ret;
e62613
-    struct hostent *h1, *h2;
e62613
-    static struct sockaddr_in from;
e62613
     int len;
e62613
 /*      struct linger ling; */
e62613
 
e62613
@@ -432,134 +451,60 @@ static int do_accept(int acc_sock, int *
e62613
     ling.l_onoff=1;
e62613
     ling.l_linger=0;
e62613
     i=setsockopt(ret,SOL_SOCKET,SO_LINGER,(char *)&ling,sizeof(ling));
e62613
-    if (i < 0) { perror("linger"); return(0); }
e62613
+    if (i < 0) { closesocket(ret); perror("linger"); return(0); }
e62613
     i=0;
e62613
     i=setsockopt(ret,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
e62613
-    if (i < 0) { perror("keepalive"); return(0); }
e62613
+    if (i < 0) { closesocket(ret); perror("keepalive"); return(0); }
e62613
 */
e62613
 
e62613
     if (host == NULL)
e62613
         goto end;
e62613
-# ifndef BIT_FIELD_LIMITS
e62613
-    /* I should use WSAAsyncGetHostByName() under windows */
e62613
-    h1 = gethostbyaddr((char *)&from.sin_addr.s_addr,
e62613
-                       sizeof(from.sin_addr.s_addr), AF_INET);
e62613
-# else
e62613
-    h1 = gethostbyaddr((char *)&from.sin_addr,
e62613
-                       sizeof(struct in_addr), AF_INET);
e62613
-# endif
e62613
-    if (h1 == NULL) {
e62613
-        BIO_printf(bio_err, "bad gethostbyaddr\n");
e62613
+
e62613
+    if (getnameinfo((struct sockaddr *)&from, sizeof(from),
e62613
+                    buffer, sizeof(buffer), NULL, 0, 0)) {
e62613
+        BIO_printf(bio_err, "getnameinfo failed\n");
e62613
         *host = NULL;
e62613
         /* return(0); */
e62613
     } else {
e62613
-        if ((*host = (char *)OPENSSL_malloc(strlen(h1->h_name) + 1)) == NULL) {
e62613
+        if ((*host = (char *)OPENSSL_malloc(strlen(buffer) + 1)) == NULL) {
e62613
             perror("OPENSSL_malloc");
e62613
             closesocket(ret);
e62613
             return (0);
e62613
         }
e62613
-        BUF_strlcpy(*host, h1->h_name, strlen(h1->h_name) + 1);
e62613
-
e62613
-        h2 = GetHostByName(*host);
e62613
-        if (h2 == NULL) {
e62613
-            BIO_printf(bio_err, "gethostbyname failure\n");
e62613
-            closesocket(ret);
e62613
-            return (0);
e62613
-        }
e62613
-        if (h2->h_addrtype != AF_INET) {
e62613
-            BIO_printf(bio_err, "gethostbyname addr is not AF_INET\n");
e62613
-            closesocket(ret);
e62613
-            return (0);
e62613
-        }
e62613
+        strcpy(*host, buffer);
e62613
     }
e62613
  end:
e62613
     *sock = ret;
e62613
     return (1);
e62613
 }
e62613
 
e62613
-int extract_host_port(char *str, char **host_ptr, unsigned char *ip,
e62613
-                      short *port_ptr)
e62613
+int extract_host_port(char *str, char **host_ptr, char **port_ptr)
e62613
 {
e62613
-    char *h, *p;
e62613
+    char *h, *p, *x;
e62613
 
e62613
-    h = str;
e62613
-    p = strchr(str, ':');
e62613
+    x = h = str;
e62613
+    if (*h == '[') {
e62613
+        h++;
e62613
+        p = strchr(h, ']');
e62613
+        if (p == NULL) {
e62613
+            BIO_printf(bio_err, "no ending bracket for IPv6 address\n");
e62613
+            return (0);
e62613
+        }
e62613
+        *(p++) = '\0';
e62613
+        x = p;
e62613
+    }
e62613
+    p = strchr(x, ':');
e62613
     if (p == NULL) {
e62613
         BIO_printf(bio_err, "no port defined\n");
e62613
         return (0);
e62613
     }
e62613
     *(p++) = '\0';
e62613
 
e62613
-    if ((ip != NULL) && !host_ip(str, ip))
e62613
-        goto err;
e62613
     if (host_ptr != NULL)
e62613
         *host_ptr = h;
e62613
+    if (port_ptr != NULL)
e62613
+        *port_ptr = p;
e62613
 
e62613
-    if (!extract_port(p, port_ptr))
e62613
-        goto err;
e62613
-    return (1);
e62613
- err:
e62613
-    return (0);
e62613
-}
e62613
-
e62613
-static int host_ip(char *str, unsigned char ip[4])
e62613
-{
e62613
-    unsigned int in[4];
e62613
-    int i;
e62613
-
e62613
-    if (sscanf(str, "%u.%u.%u.%u", &(in[0]), &(in[1]), &(in[2]), &(in[3])) ==
e62613
-        4) {
e62613
-        for (i = 0; i < 4; i++)
e62613
-            if (in[i] > 255) {
e62613
-                BIO_printf(bio_err, "invalid IP address\n");
e62613
-                goto err;
e62613
-            }
e62613
-        ip[0] = in[0];
e62613
-        ip[1] = in[1];
e62613
-        ip[2] = in[2];
e62613
-        ip[3] = in[3];
e62613
-    } else {                    /* do a gethostbyname */
e62613
-        struct hostent *he;
e62613
-
e62613
-        if (!ssl_sock_init())
e62613
-            return (0);
e62613
-
e62613
-        he = GetHostByName(str);
e62613
-        if (he == NULL) {
e62613
-            BIO_printf(bio_err, "gethostbyname failure\n");
e62613
-            goto err;
e62613
-        }
e62613
-        /* cast to short because of win16 winsock definition */
e62613
-        if ((short)he->h_addrtype != AF_INET) {
e62613
-            BIO_printf(bio_err, "gethostbyname addr is not AF_INET\n");
e62613
-            return (0);
e62613
-        }
e62613
-        ip[0] = he->h_addr_list[0][0];
e62613
-        ip[1] = he->h_addr_list[0][1];
e62613
-        ip[2] = he->h_addr_list[0][2];
e62613
-        ip[3] = he->h_addr_list[0][3];
e62613
-    }
e62613
-    return (1);
e62613
- err:
e62613
-    return (0);
e62613
-}
e62613
-
e62613
-int extract_port(char *str, short *port_ptr)
e62613
-{
e62613
-    int i;
e62613
-    struct servent *s;
e62613
-
e62613
-    i = atoi(str);
e62613
-    if (i != 0)
e62613
-        *port_ptr = (unsigned short)i;
e62613
-    else {
e62613
-        s = getservbyname(str, "tcp");
e62613
-        if (s == NULL) {
e62613
-            BIO_printf(bio_err, "getservbyname failure for %s\n", str);
e62613
-            return (0);
e62613
-        }
e62613
-        *port_ptr = ntohs((unsigned short)s->s_port);
e62613
-    }
e62613
     return (1);
e62613
 }
e62613