1 diff -up openssl-0.9.8g/apps/s_socket.c.ipv6-apps openssl-0.9.8g/apps/s_socket.c
2 --- openssl-0.9.8g/apps/s_socket.c.ipv6-apps 2005-06-13 05:21:00.000000000 +0200
3 +++ openssl-0.9.8g/apps/s_socket.c 2007-12-03 13:28:42.000000000 +0100
4 @@ -96,9 +96,7 @@ static struct hostent *GetHostByName(cha
5 static void ssl_sock_cleanup(void);
7 static int ssl_sock_init(void);
8 -static int init_client_ip(int *sock,unsigned char ip[4], int port, int type);
9 -static int init_server(int *sock, int port, int type);
10 -static int init_server_long(int *sock, int port,char *ip, int type);
11 +static int init_server(int *sock, char *port, int type);
12 static int do_accept(int acc_sock, int *sock, char **host);
13 static int host_ip(char *str, unsigned char ip[4]);
15 @@ -228,60 +226,69 @@ static int ssl_sock_init(void)
19 -int init_client(int *sock, char *host, int port, int type)
20 +int init_client(int *sock, char *host, char *port, int type)
22 - unsigned char ip[4];
25 - if (!host_ip(host,&(ip[0])))
30 - return(init_client_ip(sock,ip,port,type));
33 -static int init_client_ip(int *sock, unsigned char ip[4], int port, int type)
36 - struct sockaddr_in them;
38 + struct addrinfo *res, *res0, hints;
39 + char * failed_call = NULL;
43 if (!ssl_sock_init()) return(0);
45 - memset((char *)&them,0,sizeof(them));
46 - them.sin_family=AF_INET;
47 - them.sin_port=htons((unsigned short)port);
48 - addr=(unsigned long)
49 - ((unsigned long)ip[0]<<24L)|
50 - ((unsigned long)ip[1]<<16L)|
51 - ((unsigned long)ip[2]<< 8L)|
52 - ((unsigned long)ip[3]);
53 - them.sin_addr.s_addr=htonl(addr);
55 - if (type == SOCK_STREAM)
56 - s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
57 - else /* ( type == SOCK_DGRAM) */
58 - s=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
60 - if (s == INVALID_SOCKET) { perror("socket"); return(0); }
61 + memset(&hints, '\0', sizeof(hints));
62 + hints.ai_socktype = type;
63 + hints.ai_flags = AI_ADDRCONFIG;
65 + e = getaddrinfo(host, port, &hints, &res);
68 + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(e));
69 + if (e == EAI_SYSTEM)
70 + perror("getaddrinfo");
77 + s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
78 + if (s == INVALID_SOCKET)
80 + failed_call = "socket";
83 #ifndef OPENSSL_SYS_MPE
84 if (type == SOCK_STREAM)
87 - i=setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
88 - if (i < 0) { perror("keepalive"); return(0); }
90 + i=setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,
91 + (char *)&i,sizeof(i));
93 + failed_call = "keepalive";
99 - if (connect(s,(struct sockaddr *)&them,sizeof(them)) == -1)
100 - { close(s); perror("connect"); return(0); }
101 + if (connect(s,(struct sockaddr *)res->ai_addr,
102 + res->ai_addrlen) == 0)
104 + freeaddrinfo(res0);
108 + failed_call = "socket";
110 + if (s != INVALID_SOCKET)
112 + res = res->ai_next;
114 + freeaddrinfo(res0);
116 -int do_server(int port, int type, int *ret, int (*cb)(char *hostname, int s, unsigned char *context), unsigned char *context)
117 + perror(failed_call);
121 +int do_server(char *port, int type, int *ret, int (*cb)(char *hostname, int s, unsigned char *context), unsigned char *context)
125 @@ -319,33 +326,38 @@ int do_server(int port, int type, int *r
129 -static int init_server_long(int *sock, int port, char *ip, int type)
130 +static int init_server(int *sock, char *port, int type)
133 - struct sockaddr_in server;
135 + struct addrinfo *res, *res0, hints;
136 + char * failed_call = NULL;
141 if (!ssl_sock_init()) return(0);
143 - memset((char *)&server,0,sizeof(server));
144 - server.sin_family=AF_INET;
145 - server.sin_port=htons((unsigned short)port);
147 - server.sin_addr.s_addr=INADDR_ANY;
149 -/* Added for T3E, address-of fails on bit field (beckman@acl.lanl.gov) */
150 -#ifndef BIT_FIELD_LIMITS
151 - memcpy(&server.sin_addr.s_addr,ip,4);
153 - memcpy(&server.sin_addr,ip,4);
155 + memset(&hints, '\0', sizeof(hints));
156 + hints.ai_socktype = type;
157 + hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
159 - if (type == SOCK_STREAM)
160 - s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
161 - else /* type == SOCK_DGRAM */
162 - s=socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP);
163 + e = getaddrinfo(NULL, port, &hints, &res);
166 + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(e));
167 + if (e == EAI_SYSTEM)
168 + perror("getaddrinfo");
172 - if (s == INVALID_SOCKET) goto err;
176 + s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
177 + if (s == INVALID_SOCKET)
179 + failed_call = "socket";
182 #if defined SOL_SOCKET && defined SO_REUSEADDR
185 @@ -353,36 +365,39 @@ static int init_server_long(int *sock, i
186 (void *) &j, sizeof j);
189 - if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1)
191 + if (bind(s,(struct sockaddr *)res->ai_addr, res->ai_addrlen) == -1)
193 -#ifndef OPENSSL_SYS_WINDOWS
197 + failed_call = "bind";
200 - /* Make it 128 for linux */
201 - if (type==SOCK_STREAM && listen(s,128) == -1) goto err;
206 - if ((ret == 0) && (s != -1))
207 + if (type==SOCK_STREAM && listen(s,128) == -1)
210 + failed_call = "listen";
219 + if (s != INVALID_SOCKET)
221 + res = res->ai_next;
223 + freeaddrinfo(res0);
225 -static int init_server(int *sock, int port, int type)
227 - return(init_server_long(sock, port, NULL, type));
228 + if (s == INVALID_SOCKET) { perror("socket"); return(0); }
230 + perror(failed_call);
234 static int do_accept(int acc_sock, int *sock, char **host)
237 - struct hostent *h1,*h2;
238 - static struct sockaddr_in from;
239 + static struct sockaddr_storage from;
240 + char buffer[NI_MAXHOST];
243 /* struct linger ling; */
245 @@ -427,137 +442,62 @@ redoit:
246 if (i < 0) { perror("keepalive"); return(0); }
249 - if (host == NULL) goto end;
250 -#ifndef BIT_FIELD_LIMITS
251 - /* I should use WSAAsyncGetHostByName() under windows */
252 - h1=gethostbyaddr((char *)&from.sin_addr.s_addr,
253 - sizeof(from.sin_addr.s_addr),AF_INET);
255 - h1=gethostbyaddr((char *)&from.sin_addr,
256 - sizeof(struct in_addr),AF_INET);
260 - BIO_printf(bio_err,"bad gethostbyaddr\n");
267 - if ((*host=(char *)OPENSSL_malloc(strlen(h1->h_name)+1)) == NULL)
269 - perror("OPENSSL_malloc");
273 - BUF_strlcpy(*host,h1->h_name,strlen(h1->h_name)+1);
275 - h2=GetHostByName(*host);
277 + if (getnameinfo((struct sockaddr *)&from, sizeof(from),
278 + buffer, sizeof(buffer),
281 - BIO_printf(bio_err,"gethostbyname failure\n");
282 + BIO_printf(bio_err,"getnameinfo failed\n");
287 - if (h2->h_addrtype != AF_INET)
290 - BIO_printf(bio_err,"gethostbyname addr is not AF_INET\n");
291 + if ((*host=(char *)OPENSSL_malloc(strlen(buffer)+1)) == NULL)
293 + perror("OPENSSL_malloc");
298 + strcpy(*host, buffer);
304 -int extract_host_port(char *str, char **host_ptr, unsigned char *ip,
306 +int extract_host_port(char *str, char **host_ptr,
321 - BIO_printf(bio_err,"no port defined\n");
322 + BIO_printf(bio_err,"no ending bracket for IPv6 address\n");
327 - if ((ip != NULL) && !host_ip(str,ip))
329 - if (host_ptr != NULL) *host_ptr=h;
331 - if (!extract_port(p,port_ptr))
339 -static int host_ip(char *str, unsigned char ip[4])
341 - unsigned int in[4];
344 - if (sscanf(str,"%u.%u.%u.%u",&(in[0]),&(in[1]),&(in[2]),&(in[3])) == 4)
346 - for (i=0; i<4; i++)
349 - BIO_printf(bio_err,"invalid IP address\n");
358 - { /* do a gethostbyname */
359 - struct hostent *he;
361 - if (!ssl_sock_init()) return(0);
363 - he=GetHostByName(str);
366 - BIO_printf(bio_err,"gethostbyname failure\n");
369 - /* cast to short because of win16 winsock definition */
370 - if ((short)he->h_addrtype != AF_INET)
374 - BIO_printf(bio_err,"gethostbyname addr is not AF_INET\n");
377 - ip[0]=he->h_addr_list[0][0];
378 - ip[1]=he->h_addr_list[0][1];
379 - ip[2]=he->h_addr_list[0][2];
380 - ip[3]=he->h_addr_list[0][3];
384 + BIO_printf(bio_err,"no port defined\n");
389 -int extract_port(char *str, short *port_ptr)
393 + if (host_ptr != NULL) *host_ptr=h;
394 + if (port_ptr != NULL) *port_ptr=p;
398 - *port_ptr=(unsigned short)i;
401 - s=getservbyname(str,"tcp");
404 - BIO_printf(bio_err,"getservbyname failure for %s\n",str);
407 - *port_ptr=ntohs((unsigned short)s->s_port);
412 diff -up openssl-0.9.8g/apps/s_server.c.ipv6-apps openssl-0.9.8g/apps/s_server.c
413 --- openssl-0.9.8g/apps/s_server.c.ipv6-apps 2007-08-23 14:16:02.000000000 +0200
414 +++ openssl-0.9.8g/apps/s_server.c 2007-12-03 13:31:14.000000000 +0100
415 @@ -592,7 +592,7 @@ int MAIN(int argc, char *argv[])
417 X509_STORE *store = NULL;
420 + char *port_str = PORT_STR;
421 char *CApath=NULL,*CAfile=NULL;
422 unsigned char *context = NULL;
424 @@ -662,8 +662,7 @@ int MAIN(int argc, char *argv[])
425 (strcmp(*argv,"-accept") == 0))
427 if (--argc < 1) goto bad;
428 - if (!extract_port(*(++argv),&port))
430 + port_str= *(++argv);
432 else if (strcmp(*argv,"-verify") == 0)
434 @@ -1332,9 +1331,9 @@ bad:
436 BIO_printf(bio_s_out,"ACCEPT\n");
438 - do_server(port,socket_type,&accept_socket,www_body, context);
439 + do_server(port_str,socket_type,&accept_socket,www_body, context);
441 - do_server(port,socket_type,&accept_socket,sv_body, context);
442 + do_server(port_str,socket_type,&accept_socket,sv_body, context);
443 print_stats(bio_s_out,ctx);
446 diff -up openssl-0.9.8g/apps/s_client.c.ipv6-apps openssl-0.9.8g/apps/s_client.c
447 --- openssl-0.9.8g/apps/s_client.c.ipv6-apps 2007-08-23 14:20:56.000000000 +0200
448 +++ openssl-0.9.8g/apps/s_client.c 2007-12-03 13:28:42.000000000 +0100
449 @@ -285,7 +285,7 @@ int MAIN(int argc, char **argv)
450 int cbuf_len,cbuf_off;
451 int sbuf_len,sbuf_off;
452 fd_set readfds,writefds;
454 + char *port_str = PORT_STR;
456 char *host=SSL_HOST_NAME;
457 char *cert_file=NULL,*key_file=NULL;
458 @@ -377,13 +377,12 @@ int MAIN(int argc, char **argv)
459 else if (strcmp(*argv,"-port") == 0)
461 if (--argc < 1) goto bad;
462 - port=atoi(*(++argv));
463 - if (port == 0) goto bad;
464 + port_str= *(++argv);
466 else if (strcmp(*argv,"-connect") == 0)
468 if (--argc < 1) goto bad;
469 - if (!extract_host_port(*(++argv),&host,NULL,&port))
470 + if (!extract_host_port(*(++argv),&host,&port_str))
473 else if (strcmp(*argv,"-verify") == 0)
474 @@ -739,7 +738,7 @@ bad:
478 - if (init_client(&s,host,port,sock_type) == 0)
479 + if (init_client(&s,host,port_str,sock_type) == 0)
481 BIO_printf(bio_err,"connect:errno=%d\n",get_last_socket_error());
483 diff -up openssl-0.9.8g/apps/s_apps.h.ipv6-apps openssl-0.9.8g/apps/s_apps.h
484 --- openssl-0.9.8g/apps/s_apps.h.ipv6-apps 2007-12-03 13:28:42.000000000 +0100
485 +++ openssl-0.9.8g/apps/s_apps.h 2007-12-03 13:28:42.000000000 +0100
486 @@ -148,7 +148,7 @@ typedef fd_mask fd_set;
487 #define PORT_STR "4433"
488 #define PROTOCOL "tcp"
490 -int do_server(int port, int type, int *ret, int (*cb) (char *hostname, int s, unsigned char *context), unsigned char *context);
491 +int do_server(char *port, int type, int *ret, int (*cb) (char *hostname, int s, unsigned char *context), unsigned char *context);
493 int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx);
495 @@ -156,10 +156,9 @@ int MS_CALLBACK verify_callback(int ok,
496 int set_cert_stuff(SSL_CTX *ctx, char *cert_file, char *key_file);
497 int set_cert_key_stuff(SSL_CTX *ctx, X509 *cert, EVP_PKEY *key);
499 -int init_client(int *sock, char *server, int port, int type);
500 +int init_client(int *sock, char *server, char *port, int type);
501 int should_retry(int i);
502 -int extract_port(char *str, short *port_ptr);
503 -int extract_host_port(char *str,char **host_ptr,unsigned char *ip,short *p);
504 +int extract_host_port(char *str,char **host_ptr,char **port_ptr);
506 long MS_CALLBACK bio_dump_callback(BIO *bio, int cmd, const char *argp,
507 int argi, long argl, long ret);