1 diff -up openssl-1.0.1h/apps/s_apps.h.ipv6-apps openssl-1.0.1h/apps/s_apps.h
2 --- openssl-1.0.1h/apps/s_apps.h.ipv6-apps 2014-06-05 14:33:38.515668750 +0200
3 +++ openssl-1.0.1h/apps/s_apps.h 2014-06-05 14:33:38.540669335 +0200
4 @@ -148,7 +148,7 @@ typedef fd_mask fd_set;
5 #define PORT_STR "4433"
8 -int do_server(int port, int type, int *ret, int (*cb) (char *hostname, int s, unsigned char *context), unsigned char *context);
9 +int do_server(char *port, int type, int *ret, int (*cb) (char *hostname, int s, unsigned char *context), unsigned char *context);
11 int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx);
13 @@ -156,10 +156,9 @@ int MS_CALLBACK verify_callback(int ok,
14 int set_cert_stuff(SSL_CTX *ctx, char *cert_file, char *key_file);
15 int set_cert_key_stuff(SSL_CTX *ctx, X509 *cert, EVP_PKEY *key);
17 -int init_client(int *sock, char *server, int port, int type);
18 +int init_client(int *sock, char *server, char *port, int type);
19 int should_retry(int i);
20 -int extract_port(char *str, short *port_ptr);
21 -int extract_host_port(char *str,char **host_ptr,unsigned char *ip,short *p);
22 +int extract_host_port(char *str,char **host_ptr,char **port_ptr);
24 long MS_CALLBACK bio_dump_callback(BIO *bio, int cmd, const char *argp,
25 int argi, long argl, long ret);
26 diff -up openssl-1.0.1h/apps/s_client.c.ipv6-apps openssl-1.0.1h/apps/s_client.c
27 --- openssl-1.0.1h/apps/s_client.c.ipv6-apps 2014-06-05 14:33:38.533669171 +0200
28 +++ openssl-1.0.1h/apps/s_client.c 2014-06-05 14:33:38.540669335 +0200
29 @@ -567,7 +567,7 @@ int MAIN(int argc, char **argv)
30 int cbuf_len,cbuf_off;
31 int sbuf_len,sbuf_off;
32 fd_set readfds,writefds;
34 + char *port_str = PORT_STR;
36 char *host=SSL_HOST_NAME;
37 char *cert_file=NULL,*key_file=NULL;
38 @@ -668,13 +668,12 @@ int MAIN(int argc, char **argv)
39 else if (strcmp(*argv,"-port") == 0)
41 if (--argc < 1) goto bad;
42 - port=atoi(*(++argv));
43 - if (port == 0) goto bad;
44 + port_str= *(++argv);
46 else if (strcmp(*argv,"-connect") == 0)
48 if (--argc < 1) goto bad;
49 - if (!extract_host_port(*(++argv),&host,NULL,&port))
50 + if (!extract_host_port(*(++argv),&host,&port_str))
53 else if (strcmp(*argv,"-verify") == 0)
54 @@ -1267,7 +1266,7 @@ bad:
58 - if (init_client(&s,host,port,socket_type) == 0)
59 + if (init_client(&s,host,port_str,socket_type) == 0)
61 BIO_printf(bio_err,"connect:errno=%d\n",get_last_socket_error());
63 diff -up openssl-1.0.1h/apps/s_server.c.ipv6-apps openssl-1.0.1h/apps/s_server.c
64 --- openssl-1.0.1h/apps/s_server.c.ipv6-apps 2014-06-05 14:33:38.533669171 +0200
65 +++ openssl-1.0.1h/apps/s_server.c 2014-06-05 14:33:38.541669358 +0200
66 @@ -933,7 +933,7 @@ int MAIN(int argc, char *argv[])
68 X509_VERIFY_PARAM *vpm = NULL;
71 + char *port_str = PORT_STR;
72 char *CApath=NULL,*CAfile=NULL;
73 unsigned char *context = NULL;
75 @@ -1004,8 +1004,7 @@ int MAIN(int argc, char *argv[])
76 (strcmp(*argv,"-accept") == 0))
78 if (--argc < 1) goto bad;
79 - if (!extract_port(*(++argv),&port))
81 + port_str= *(++argv);
83 else if (strcmp(*argv,"-verify") == 0)
85 @@ -1892,9 +1891,9 @@ bad:
86 BIO_printf(bio_s_out,"ACCEPT\n");
87 (void)BIO_flush(bio_s_out);
89 - do_server(port,socket_type,&accept_socket,www_body, context);
90 + do_server(port_str,socket_type,&accept_socket,www_body, context);
92 - do_server(port,socket_type,&accept_socket,sv_body, context);
93 + do_server(port_str,socket_type,&accept_socket,sv_body, context);
94 print_stats(bio_s_out,ctx);
97 diff -up openssl-1.0.1h/apps/s_socket.c.ipv6-apps openssl-1.0.1h/apps/s_socket.c
98 --- openssl-1.0.1h/apps/s_socket.c.ipv6-apps 2014-06-05 11:44:33.000000000 +0200
99 +++ openssl-1.0.1h/apps/s_socket.c 2014-06-05 14:39:53.226442195 +0200
100 @@ -102,9 +102,7 @@ static struct hostent *GetHostByName(cha
101 static void ssl_sock_cleanup(void);
103 static int ssl_sock_init(void);
104 -static int init_client_ip(int *sock,unsigned char ip[4], int port, int type);
105 -static int init_server(int *sock, int port, int type);
106 -static int init_server_long(int *sock, int port,char *ip, int type);
107 +static int init_server(int *sock, char *port, int type);
108 static int do_accept(int acc_sock, int *sock, char **host);
109 static int host_ip(char *str, unsigned char ip[4]);
111 @@ -234,57 +232,71 @@ static int ssl_sock_init(void)
115 -int init_client(int *sock, char *host, int port, int type)
116 +int init_client(int *sock, char *host, char *port, int type)
118 - unsigned char ip[4];
120 - memset(ip, '\0', sizeof ip);
121 - if (!host_ip(host,&(ip[0])))
123 - return init_client_ip(sock,ip,port,type);
126 -static int init_client_ip(int *sock, unsigned char ip[4], int port, int type)
128 - unsigned long addr;
129 - struct sockaddr_in them;
131 + struct addrinfo *res, *res0, hints;
132 + char * failed_call = NULL;
136 if (!ssl_sock_init()) return(0);
138 - memset((char *)&them,0,sizeof(them));
139 - them.sin_family=AF_INET;
140 - them.sin_port=htons((unsigned short)port);
141 - addr=(unsigned long)
142 - ((unsigned long)ip[0]<<24L)|
143 - ((unsigned long)ip[1]<<16L)|
144 - ((unsigned long)ip[2]<< 8L)|
145 - ((unsigned long)ip[3]);
146 - them.sin_addr.s_addr=htonl(addr);
148 - if (type == SOCK_STREAM)
149 - s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
150 - else /* ( type == SOCK_DGRAM) */
151 - s=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
153 - if (s == INVALID_SOCKET) { perror("socket"); return(0); }
154 + memset(&hints, '\0', sizeof(hints));
155 + hints.ai_socktype = type;
156 + hints.ai_flags = AI_ADDRCONFIG;
158 + e = getaddrinfo(host, port, &hints, &res);
161 + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(e));
162 + if (e == EAI_SYSTEM)
163 + perror("getaddrinfo");
170 + s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
171 + if (s == INVALID_SOCKET)
173 + failed_call = "socket";
176 #if defined(SO_KEEPALIVE) && !defined(OPENSSL_SYS_MPE)
177 if (type == SOCK_STREAM)
180 - i=setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
181 - if (i < 0) { closesocket(s); perror("keepalive"); return(0); }
183 + i=setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,
184 + (char *)&i,sizeof(i));
186 + failed_call = "keepalive";
192 - if (connect(s,(struct sockaddr *)&them,sizeof(them)) == -1)
193 - { closesocket(s); perror("connect"); return(0); }
194 + if (connect(s,(struct sockaddr *)res->ai_addr,
195 + res->ai_addrlen) == 0)
197 + freeaddrinfo(res0);
202 -int do_server(int port, int type, int *ret, int (*cb)(char *hostname, int s, unsigned char *context), unsigned char *context)
203 + failed_call = "socket";
205 + if (s != INVALID_SOCKET)
207 + res = res->ai_next;
209 + freeaddrinfo(res0);
212 + perror(failed_call);
216 +int do_server(char *port, int type, int *ret, int (*cb)(char *hostname, int s, unsigned char *context), unsigned char *context)
220 @@ -322,33 +334,50 @@ int do_server(int port, int type, int *r
224 -static int init_server_long(int *sock, int port, char *ip, int type)
225 +static int init_server(int *sock, char *port, int type)
228 - struct sockaddr_in server;
230 + struct addrinfo *res, *res0 = NULL, hints;
231 + char * failed_call = NULL;
232 + int s = INVALID_SOCKET;
235 if (!ssl_sock_init()) return(0);
237 - memset((char *)&server,0,sizeof(server));
238 - server.sin_family=AF_INET;
239 - server.sin_port=htons((unsigned short)port);
241 - server.sin_addr.s_addr=INADDR_ANY;
243 -/* Added for T3E, address-of fails on bit field (beckman@acl.lanl.gov) */
244 -#ifndef BIT_FIELD_LIMITS
245 - memcpy(&server.sin_addr.s_addr,ip,4);
247 - memcpy(&server.sin_addr,ip,4);
249 + memset(&hints, '\0', sizeof(hints));
250 + hints.ai_family = AF_INET6;
252 + hints.ai_socktype = type;
253 + hints.ai_flags = AI_PASSIVE;
255 - if (type == SOCK_STREAM)
256 - s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
257 - else /* type == SOCK_DGRAM */
258 - s=socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP);
259 + e = getaddrinfo(NULL, port, &hints, &res);
262 + if (hints.ai_family == AF_INET)
264 + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(e));
265 + if (e == EAI_SYSTEM)
266 + perror("getaddrinfo");
273 - if (s == INVALID_SOCKET) goto err;
277 + s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
278 + if (s == INVALID_SOCKET)
280 + failed_call = "socket";
283 + if (hints.ai_family == AF_INET6)
286 + setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
287 + (void *) &j, sizeof j);
289 #if defined SOL_SOCKET && defined SO_REUSEADDR
292 @@ -356,35 +385,49 @@ static int init_server_long(int *sock, i
293 (void *) &j, sizeof j);
296 - if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1)
298 + if (bind(s,(struct sockaddr *)res->ai_addr, res->ai_addrlen) == -1)
300 -#ifndef OPENSSL_SYS_WINDOWS
304 + failed_call = "bind";
307 - /* Make it 128 for linux */
308 - if (type==SOCK_STREAM && listen(s,128) == -1) goto err;
312 - if ((ret == 0) && (s != -1))
313 + if (type==SOCK_STREAM && listen(s,128) == -1)
316 + failed_call = "listen";
325 + if (s != INVALID_SOCKET)
327 + res = res->ai_next;
330 + freeaddrinfo(res0);
332 -static int init_server(int *sock, int port, int type)
333 + if (s == INVALID_SOCKET)
335 - return(init_server_long(sock, port, NULL, type));
336 + if (hints.ai_family == AF_INET6)
338 + hints.ai_family = AF_INET;
345 + perror(failed_call);
349 static int do_accept(int acc_sock, int *sock, char **host)
351 + static struct sockaddr_storage from;
352 + char buffer[NI_MAXHOST];
354 - struct hostent *h1,*h2;
355 - static struct sockaddr_in from;
357 /* struct linger ling; */
359 @@ -424,145 +467,66 @@ redoit:
362 i=setsockopt(ret,SOL_SOCKET,SO_LINGER,(char *)&ling,sizeof(ling));
363 - if (i < 0) { perror("linger"); return(0); }
364 + if (i < 0) { closesocket(ret); perror("linger"); return(0); }
366 i=setsockopt(ret,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
367 - if (i < 0) { perror("keepalive"); return(0); }
368 + if (i < 0) { closesocket(ret); perror("keepalive"); return(0); }
371 if (host == NULL) goto end;
372 -#ifndef BIT_FIELD_LIMITS
373 - /* I should use WSAAsyncGetHostByName() under windows */
374 - h1=gethostbyaddr((char *)&from.sin_addr.s_addr,
375 - sizeof(from.sin_addr.s_addr),AF_INET);
377 - h1=gethostbyaddr((char *)&from.sin_addr,
378 - sizeof(struct in_addr),AF_INET);
382 + if (getnameinfo((struct sockaddr *)&from, sizeof(from),
383 + buffer, sizeof(buffer),
386 - BIO_printf(bio_err,"bad gethostbyaddr\n");
387 + BIO_printf(bio_err,"getnameinfo failed\n");
393 - if ((*host=(char *)OPENSSL_malloc(strlen(h1->h_name)+1)) == NULL)
394 + if ((*host=(char *)OPENSSL_malloc(strlen(buffer)+1)) == NULL)
396 perror("OPENSSL_malloc");
400 - BUF_strlcpy(*host,h1->h_name,strlen(h1->h_name)+1);
402 - h2=GetHostByName(*host);
405 - BIO_printf(bio_err,"gethostbyname failure\n");
409 - if (h2->h_addrtype != AF_INET)
411 - BIO_printf(bio_err,"gethostbyname addr is not AF_INET\n");
415 + strcpy(*host, buffer);
422 -int extract_host_port(char *str, char **host_ptr, unsigned char *ip,
424 +int extract_host_port(char *str, char **host_ptr,
439 - BIO_printf(bio_err,"no port defined\n");
440 + BIO_printf(bio_err,"no ending bracket for IPv6 address\n");
445 - if ((ip != NULL) && !host_ip(str,ip))
447 - if (host_ptr != NULL) *host_ptr=h;
449 - if (!extract_port(p,port_ptr))
457 -static int host_ip(char *str, unsigned char ip[4])
459 - unsigned int in[4];
462 - if (sscanf(str,"%u.%u.%u.%u",&(in[0]),&(in[1]),&(in[2]),&(in[3])) == 4)
464 - for (i=0; i<4; i++)
467 - BIO_printf(bio_err,"invalid IP address\n");
476 - { /* do a gethostbyname */
477 - struct hostent *he;
479 - if (!ssl_sock_init()) return(0);
481 - he=GetHostByName(str);
484 - BIO_printf(bio_err,"gethostbyname failure\n");
487 - /* cast to short because of win16 winsock definition */
488 - if ((short)he->h_addrtype != AF_INET)
492 - BIO_printf(bio_err,"gethostbyname addr is not AF_INET\n");
495 - ip[0]=he->h_addr_list[0][0];
496 - ip[1]=he->h_addr_list[0][1];
497 - ip[2]=he->h_addr_list[0][2];
498 - ip[3]=he->h_addr_list[0][3];
502 + BIO_printf(bio_err,"no port defined\n");
507 -int extract_port(char *str, short *port_ptr)
511 + if (host_ptr != NULL) *host_ptr=h;
512 + if (port_ptr != NULL) *port_ptr=p;
516 - *port_ptr=(unsigned short)i;
519 - s=getservbyname(str,"tcp");
522 - BIO_printf(bio_err,"getservbyname failure for %s\n",str);
525 - *port_ptr=ntohs((unsigned short)s->s_port);