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

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