Blame SOURCES/openssl-0.9.8b-ipv6-apps.patch

c4366c
diff --exclude-from=exclude-diff-openssl-0.9.8b -bru openssl-0.9.8b.orig/apps/s_apps.h openssl-0.9.8b/apps/s_apps.h
c4366c
--- openssl-0.9.8b.orig/apps/s_apps.h	2006-07-11 16:14:29.000000000 +0200
c4366c
+++ openssl-0.9.8b/apps/s_apps.h	2006-07-13 08:44:29.000000000 +0200
c4366c
@@ -148,7 +148,7 @@
c4366c
 #define PORT_STR        "4433"
c4366c
 #define PROTOCOL        "tcp"
c4366c
 
c4366c
-int do_server(int port, int type, int *ret, int (*cb) (char *hostname, int s, unsigned char *context), unsigned char *context);
c4366c
+int do_server(char *port, int type, int *ret, int (*cb) (char *hostname, int s, unsigned char *context), unsigned char *context);
c4366c
 #ifdef HEADER_X509_H
c4366c
 int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx);
c4366c
 #endif
c4366c
@@ -156,10 +156,9 @@
c4366c
 int set_cert_stuff(SSL_CTX *ctx, char *cert_file, char *key_file);
c4366c
 int set_cert_key_stuff(SSL_CTX *ctx, X509 *cert, EVP_PKEY *key);
c4366c
 #endif
c4366c
-int init_client(int *sock, char *server, int port, int type);
c4366c
+int init_client(int *sock, char *server, char *port, int type);
c4366c
 int should_retry(int i);
c4366c
-int extract_port(char *str, short *port_ptr);
c4366c
-int extract_host_port(char *str,char **host_ptr,unsigned char *ip,short *p);
c4366c
+int extract_host_port(char *str,char **host_ptr,char **port_ptr);
c4366c
 
c4366c
 long MS_CALLBACK bio_dump_callback(BIO *bio, int cmd, const char *argp,
c4366c
 	int argi, long argl, long ret);
c4366c
diff --exclude-from=exclude-diff-openssl-0.9.8b -bru openssl-0.9.8b.orig/apps/s_client.c openssl-0.9.8b/apps/s_client.c
c4366c
--- openssl-0.9.8b.orig/apps/s_client.c	2005-11-25 14:46:41.000000000 +0100
c4366c
+++ openssl-0.9.8b/apps/s_client.c	2006-07-13 08:44:29.000000000 +0200
c4366c
@@ -246,7 +246,7 @@
c4366c
 	int cbuf_len,cbuf_off;
c4366c
 	int sbuf_len,sbuf_off;
c4366c
 	fd_set readfds,writefds;
c4366c
-	short port=PORT;
c4366c
+	char *port_str = PORT_STR;
c4366c
 	int full_log=1;
c4366c
 	char *host=SSL_HOST_NAME;
c4366c
 	char *cert_file=NULL,*key_file=NULL;
c4366c
@@ -330,13 +330,12 @@
c4366c
 		else if	(strcmp(*argv,"-port") == 0)
c4366c
 			{
c4366c
 			if (--argc < 1) goto bad;
c4366c
-			port=atoi(*(++argv));
c4366c
-			if (port == 0) goto bad;
c4366c
+			port_str= *(++argv);
c4366c
 			}
c4366c
 		else if (strcmp(*argv,"-connect") == 0)
c4366c
 			{
c4366c
 			if (--argc < 1) goto bad;
c4366c
-			if (!extract_host_port(*(++argv),&host,NULL,&port))
c4366c
+			if (!extract_host_port(*(++argv),&host,&port_str))
c4366c
 				goto bad;
c4366c
 			}
c4366c
 		else if	(strcmp(*argv,"-verify") == 0)
c4366c
@@ -619,7 +618,7 @@
c4366c
 
c4366c
 re_start:
c4366c
 
c4366c
-	if (init_client(&s,host,port,sock_type) == 0)
c4366c
+	if (init_client(&s,host,port_str,sock_type) == 0)
c4366c
 		{
c4366c
 		BIO_printf(bio_err,"connect:errno=%d\n",get_last_socket_error());
c4366c
 		SHUTDOWN(s);
c4366c
diff --exclude-from=exclude-diff-openssl-0.9.8b -bru openssl-0.9.8b.orig/apps/s_server.c openssl-0.9.8b/apps/s_server.c
c4366c
--- openssl-0.9.8b.orig/apps/s_server.c	2005-09-02 14:27:02.000000000 +0200
c4366c
+++ openssl-0.9.8b/apps/s_server.c	2006-07-13 08:44:29.000000000 +0200
c4366c
@@ -532,7 +532,7 @@
c4366c
 	{
c4366c
 	X509_STORE *store = NULL;
c4366c
 	int vflags = 0;
c4366c
-	short port=PORT;
c4366c
+	char *port_str = PORT_STR;
c4366c
 	char *CApath=NULL,*CAfile=NULL;
c4366c
 	unsigned char *context = NULL;
c4366c
 	char *dhfile = NULL;
c4366c
@@ -597,8 +597,7 @@
c4366c
 			 (strcmp(*argv,"-accept") == 0))
c4366c
 			{
c4366c
 			if (--argc < 1) goto bad;
c4366c
-			if (!extract_port(*(++argv),&port))
c4366c
-				goto bad;
c4366c
+			port_str= *(++argv);
c4366c
 			}
c4366c
 		else if	(strcmp(*argv,"-verify") == 0)
c4366c
 			{
c4366c
@@ -1086,9 +1085,9 @@
c4366c
 
c4366c
 	BIO_printf(bio_s_out,"ACCEPT\n");
c4366c
 	if (www)
c4366c
-		do_server(port,sock_type,&accept_socket,www_body, context);
c4366c
+		do_server(port_str,sock_type,&accept_socket,www_body, context);
c4366c
 	else
c4366c
-		do_server(port,sock_type,&accept_socket,sv_body, context);
c4366c
+		do_server(port_str,sock_type,&accept_socket,sv_body, context);
c4366c
 	print_stats(bio_s_out,ctx);
c4366c
 	ret=0;
c4366c
 end:
c4366c
diff --exclude-from=exclude-diff-openssl-0.9.8b -bru openssl-0.9.8b.orig/apps/s_socket.c openssl-0.9.8b/apps/s_socket.c
c4366c
--- openssl-0.9.8b.orig/apps/s_socket.c	2005-06-13 05:21:00.000000000 +0200
c4366c
+++ openssl-0.9.8b/apps/s_socket.c	2006-07-13 08:44:29.000000000 +0200
c4366c
@@ -96,9 +96,7 @@
c4366c
 static void ssl_sock_cleanup(void);
c4366c
 #endif
c4366c
 static int ssl_sock_init(void);
c4366c
-static int init_client_ip(int *sock,unsigned char ip[4], int port, int type);
c4366c
-static int init_server(int *sock, int port, int type);
c4366c
-static int init_server_long(int *sock, int port,char *ip, int type);
c4366c
+static int init_server(int *sock, char *port, int type);
c4366c
 static int do_accept(int acc_sock, int *sock, char **host);
c4366c
 static int host_ip(char *str, unsigned char ip[4]);
c4366c
 
c4366c
@@ -228,60 +226,69 @@
c4366c
 	return(1);
c4366c
 	}
c4366c
 
c4366c
-int init_client(int *sock, char *host, int port, int type)
c4366c
+int init_client(int *sock, char *host, char *port, int type)
c4366c
 	{
c4366c
-	unsigned char ip[4];
c4366c
-	short p=0;
c4366c
-
c4366c
-	if (!host_ip(host,&(ip[0])))
c4366c
-		{
c4366c
-		return(0);
c4366c
-		}
c4366c
-	if (p != 0) port=p;
c4366c
-	return(init_client_ip(sock,ip,port,type));
c4366c
-	}
c4366c
-
c4366c
-static int init_client_ip(int *sock, unsigned char ip[4], int port, int type)
c4366c
-	{
c4366c
-	unsigned long addr;
c4366c
-	struct sockaddr_in them;
c4366c
-	int s,i;
c4366c
+	struct addrinfo *res, *res0, hints;
c4366c
+	char * failed_call = NULL;
c4366c
+	int s;
c4366c
+	int e;
c4366c
 
c4366c
 	if (!ssl_sock_init()) return(0);
c4366c
 
c4366c
-	memset((char *)&them,0,sizeof(them));
c4366c
-	them.sin_family=AF_INET;
c4366c
-	them.sin_port=htons((unsigned short)port);
c4366c
-	addr=(unsigned long)
c4366c
-		((unsigned long)ip[0]<<24L)|
c4366c
-		((unsigned long)ip[1]<<16L)|
c4366c
-		((unsigned long)ip[2]<< 8L)|
c4366c
-		((unsigned long)ip[3]);
c4366c
-	them.sin_addr.s_addr=htonl(addr);
c4366c
-
c4366c
-	if (type == SOCK_STREAM)
c4366c
-		s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
c4366c
-	else /* ( type == SOCK_DGRAM) */
c4366c
-		s=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
c4366c
-			
c4366c
-	if (s == INVALID_SOCKET) { perror("socket"); return(0); }
c4366c
+	memset(&hints, '\0', sizeof(hints));
c4366c
+	hints.ai_socktype = type;
c4366c
+	hints.ai_flags = AI_ADDRCONFIG;
c4366c
+
c4366c
+	e = getaddrinfo(host, port, &hints, &res;;
c4366c
+	if (e)
c4366c
+	{
c4366c
+		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(e));
c4366c
+		if (e == EAI_SYSTEM)
c4366c
+			perror("getaddrinfo");
c4366c
+		return (0);
c4366c
+		}
c4366c
 
c4366c
+	res0 = res;
c4366c
+	while (res)
c4366c
+		{
c4366c
+		s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
c4366c
+		if (s == INVALID_SOCKET)
c4366c
+			{
c4366c
+			failed_call = "socket";
c4366c
+			goto nextres;
c4366c
+			}
c4366c
 #ifndef OPENSSL_SYS_MPE
c4366c
 	if (type == SOCK_STREAM)
c4366c
 		{
c4366c
-		i=0;
c4366c
-		i=setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
c4366c
-		if (i < 0) { perror("keepalive"); return(0); }
c4366c
+			int i=0;
c4366c
+			i=setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,
c4366c
+				(char *)&i,sizeof(i));
c4366c
+			if (i < 0) {
c4366c
+				failed_call = "keepalive";
c4366c
+				goto nextres;
c4366c
+				}
c4366c
 		}
c4366c
 #endif
c4366c
-
c4366c
-	if (connect(s,(struct sockaddr *)&them,sizeof(them)) == -1)
c4366c
-		{ close(s); perror("connect"); return(0); }
c4366c
+		if (connect(s,(struct sockaddr *)res->ai_addr,
c4366c
+			res->ai_addrlen) == 0)
c4366c
+			{
c4366c
+			freeaddrinfo(res0);
c4366c
 	*sock=s;
c4366c
 	return(1);
c4366c
 	}
c4366c
+		failed_call = "socket";
c4366c
+nextres:
c4366c
+		if (s != INVALID_SOCKET)
c4366c
+			close(s);
c4366c
+		res = res->ai_next;
c4366c
+		}
c4366c
+	freeaddrinfo(res0);
c4366c
+
c4366c
+	perror(failed_call);
c4366c
+	return(0);
c4366c
+	}
c4366c
 
c4366c
-int do_server(int port, int type, int *ret, int (*cb)(char *hostname, int s, unsigned char *context), unsigned char *context)
c4366c
+int do_server(char *port, int type, int *ret, int (*cb)(char *hostname, int s, unsigned char *context), unsigned char *context)
c4366c
 	{
c4366c
 	int sock;
c4366c
 	char *name = NULL;
c4366c
@@ -319,33 +326,38 @@
c4366c
 		}
c4366c
 	}
c4366c
 
c4366c
-static int init_server_long(int *sock, int port, char *ip, int type)
c4366c
+static int init_server(int *sock, char *port, int type)
c4366c
 	{
c4366c
-	int ret=0;
c4366c
-	struct sockaddr_in server;
c4366c
-	int s= -1,i;
c4366c
+	struct addrinfo *res, *res0, hints;
c4366c
+	char * failed_call = NULL;
c4366c
+	char port_name[8];
c4366c
+	int s;
c4366c
+	int e;
c4366c
 
c4366c
 	if (!ssl_sock_init()) return(0);
c4366c
 
c4366c
-	memset((char *)&server,0,sizeof(server));
c4366c
-	server.sin_family=AF_INET;
c4366c
-	server.sin_port=htons((unsigned short)port);
c4366c
-	if (ip == NULL)
c4366c
-		server.sin_addr.s_addr=INADDR_ANY;
c4366c
-	else
c4366c
-/* Added for T3E, address-of fails on bit field (beckman@acl.lanl.gov) */
c4366c
-#ifndef BIT_FIELD_LIMITS
c4366c
-		memcpy(&server.sin_addr.s_addr,ip,4);
c4366c
-#else
c4366c
-		memcpy(&server.sin_addr,ip,4);
c4366c
-#endif
c4366c
-	
c4366c
-		if (type == SOCK_STREAM)
c4366c
-			s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
c4366c
-		else /* type == SOCK_DGRAM */
c4366c
-			s=socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP);
c4366c
+	memset(&hints, '\0', sizeof(hints));
c4366c
+	hints.ai_socktype = type;
c4366c
+	hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
c4366c
+	
c4366c
+	e = getaddrinfo(NULL, port, &hints, &res;;
c4366c
+	if (e)
c4366c
+		{
c4366c
+		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(e));
c4366c
+		if (e == EAI_SYSTEM)
c4366c
+			perror("getaddrinfo");
c4366c
+		return (0);
c4366c
+		}
c4366c
 
c4366c
-	if (s == INVALID_SOCKET) goto err;
c4366c
+	res0 = res;
c4366c
+	while (res)
c4366c
+		{
c4366c
+		s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
c4366c
+		if (s == INVALID_SOCKET)
c4366c
+			{
c4366c
+			failed_call = "socket";
c4366c
+			goto nextres;
c4366c
+			}
c4366c
 #if defined SOL_SOCKET && defined SO_REUSEADDR
c4366c
 		{
c4366c
 		int j = 1;
c4366c
@@ -353,36 +365,39 @@
c4366c
 			   (void *) &j, sizeof j);
c4366c
 		}
c4366c
 #endif
c4366c
-	if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1)
c4366c
+
c4366c
+		if (bind(s,(struct sockaddr *)res->ai_addr, res->ai_addrlen) == -1)
c4366c
 		{
c4366c
-#ifndef OPENSSL_SYS_WINDOWS
c4366c
-		perror("bind");
c4366c
-#endif
c4366c
-		goto err;
c4366c
+			failed_call = "bind";
c4366c
+			goto nextres;
c4366c
 		}
c4366c
-	/* Make it 128 for linux */
c4366c
-	if (type==SOCK_STREAM && listen(s,128) == -1) goto err;
c4366c
-	i=0;
c4366c
-	*sock=s;
c4366c
-	ret=1;
c4366c
-err:
c4366c
-	if ((ret == 0) && (s != -1))
c4366c
+		if (type==SOCK_STREAM && listen(s,128) == -1)
c4366c
 		{
c4366c
-		SHUTDOWN(s);
c4366c
+			failed_call = "listen";
c4366c
+			goto nextres;
c4366c
 		}
c4366c
-	return(ret);
c4366c
+
c4366c
+		*sock=s;
c4366c
+		return(1);
c4366c
+
c4366c
+nextres:
c4366c
+		if (s != INVALID_SOCKET)
c4366c
+			close(s);
c4366c
+		res = res->ai_next;
c4366c
 	}
c4366c
+	freeaddrinfo(res0);
c4366c
 
c4366c
-static int init_server(int *sock, int port, int type)
c4366c
-	{
c4366c
-	return(init_server_long(sock, port, NULL, type));
c4366c
+	if (s == INVALID_SOCKET) { perror("socket"); return(0); }
c4366c
+
c4366c
+	perror(failed_call);
c4366c
+	return(0);
c4366c
 	}
c4366c
 
c4366c
 static int do_accept(int acc_sock, int *sock, char **host)
c4366c
 	{
c4366c
-	int ret,i;
c4366c
-	struct hostent *h1,*h2;
c4366c
-	static struct sockaddr_in from;
c4366c
+	static struct sockaddr_storage from;
c4366c
+	char buffer[NI_MAXHOST];
c4366c
+	int ret;
c4366c
 	int len;
c4366c
 /*	struct linger ling; */
c4366c
 
c4366c
@@ -427,137 +442,62 @@
c4366c
 	if (i < 0) { perror("keepalive"); return(0); }
c4366c
 */
c4366c
 
c4366c
-	if (host == NULL) goto end;
c4366c
-#ifndef BIT_FIELD_LIMITS
c4366c
-	/* I should use WSAAsyncGetHostByName() under windows */
c4366c
-	h1=gethostbyaddr((char *)&from.sin_addr.s_addr,
c4366c
-		sizeof(from.sin_addr.s_addr),AF_INET);
c4366c
-#else
c4366c
-	h1=gethostbyaddr((char *)&from.sin_addr,
c4366c
-		sizeof(struct in_addr),AF_INET);
c4366c
-#endif
c4366c
-	if (h1 == NULL)
c4366c
+	if (host == NULL)
c4366c
 		{
c4366c
-		BIO_printf(bio_err,"bad gethostbyaddr\n");
c4366c
-		*host=NULL;
c4366c
-		/* return(0); */
c4366c
-		}
c4366c
-	else
c4366c
-		{
c4366c
-		if ((*host=(char *)OPENSSL_malloc(strlen(h1->h_name)+1)) == NULL)
c4366c
-			{
c4366c
-			perror("OPENSSL_malloc");
c4366c
-			return(0);
c4366c
+		*sock=ret;
c4366c
+		return(1);
c4366c
 			}
c4366c
-		BUF_strlcpy(*host,h1->h_name,strlen(h1->h_name)+1);
c4366c
 
c4366c
-		h2=GetHostByName(*host);
c4366c
-		if (h2 == NULL)
c4366c
+	if (getnameinfo((struct sockaddr *)&from, sizeof(from),
c4366c
+		buffer, sizeof(buffer),
c4366c
+		NULL, 0, 0))
c4366c
 			{
c4366c
-			BIO_printf(bio_err,"gethostbyname failure\n");
c4366c
+		BIO_printf(bio_err,"getnameinfo failed\n");
c4366c
+		*host=NULL; *sock=ret;
c4366c
+		return(1);
c4366c
-			return(0);
c4366c
 			}
c4366c
-		i=0;
c4366c
-		if (h2->h_addrtype != AF_INET)
c4366c
+	else
c4366c
 			{
c4366c
-			BIO_printf(bio_err,"gethostbyname addr is not AF_INET\n");
c4366c
+		if ((*host=(char *)OPENSSL_malloc(strlen(buffer)+1)) == NULL)
c4366c
+			{
c4366c
+			perror("OPENSSL_malloc");
c4366c
 			return(0);
c4366c
 			}
c4366c
-		}
c4366c
-end:
c4366c
+		strcpy(*host, buffer);
c4366c
 	*sock=ret;
c4366c
 	return(1);
c4366c
 	}
c4366c
+	}
c4366c
 
c4366c
-int extract_host_port(char *str, char **host_ptr, unsigned char *ip,
c4366c
-	     short *port_ptr)
c4366c
+int extract_host_port(char *str, char **host_ptr, 
c4366c
+	     char **port_ptr)
c4366c
 	{
c4366c
-	char *h,*p;
c4366c
+	char *h,*p,*x;
c4366c
 
c4366c
-	h=str;
c4366c
-	p=strchr(str,':');
c4366c
+	x=h=str;
c4366c
+	if (*h == '[')
c4366c
+		{
c4366c
+		h++;
c4366c
+		p=strchr(h,']');
c4366c
 	if (p == NULL)
c4366c
 		{
c4366c
-		BIO_printf(bio_err,"no port defined\n");
c4366c
+			BIO_printf(bio_err,"no ending bracket for IPv6 address\n");
c4366c
 		return(0);
c4366c
 		}
c4366c
 	*(p++)='\0';
c4366c
-
c4366c
-	if ((ip != NULL) && !host_ip(str,ip))
c4366c
-		goto err;
c4366c
-	if (host_ptr != NULL) *host_ptr=h;
c4366c
-
c4366c
-	if (!extract_port(p,port_ptr))
c4366c
-		goto err;
c4366c
-	return(1);
c4366c
-err:
c4366c
-	return(0);
c4366c
+		x = p;
c4366c
 	}
c4366c
-
c4366c
-static int host_ip(char *str, unsigned char ip[4])
c4366c
-	{
c4366c
-	unsigned int in[4]; 
c4366c
-	int i;
c4366c
-
c4366c
-	if (sscanf(str,"%u.%u.%u.%u",&(in[0]),&(in[1]),&(in[2]),&(in[3])) == 4)
c4366c
-		{
c4366c
-		for (i=0; i<4; i++)
c4366c
-			if (in[i] > 255)
c4366c
-				{
c4366c
-				BIO_printf(bio_err,"invalid IP address\n");
c4366c
-				goto err;
c4366c
-				}
c4366c
-		ip[0]=in[0];
c4366c
-		ip[1]=in[1];
c4366c
-		ip[2]=in[2];
c4366c
-		ip[3]=in[3];
c4366c
-		}
c4366c
-	else
c4366c
-		{ /* do a gethostbyname */
c4366c
-		struct hostent *he;
c4366c
-
c4366c
-		if (!ssl_sock_init()) return(0);
c4366c
-
c4366c
-		he=GetHostByName(str);
c4366c
-		if (he == NULL)
c4366c
-			{
c4366c
-			BIO_printf(bio_err,"gethostbyname failure\n");
c4366c
-			goto err;
c4366c
-			}
c4366c
-		/* cast to short because of win16 winsock definition */
c4366c
-		if ((short)he->h_addrtype != AF_INET)
c4366c
+	p=strchr(x,':');
c4366c
+	if (p == NULL)
c4366c
 			{
c4366c
-			BIO_printf(bio_err,"gethostbyname addr is not AF_INET\n");
c4366c
-			return(0);
c4366c
-			}
c4366c
-		ip[0]=he->h_addr_list[0][0];
c4366c
-		ip[1]=he->h_addr_list[0][1];
c4366c
-		ip[2]=he->h_addr_list[0][2];
c4366c
-		ip[3]=he->h_addr_list[0][3];
c4366c
-		}
c4366c
-	return(1);
c4366c
-err:
c4366c
+		BIO_printf(bio_err,"no port defined\n");
c4366c
 	return(0);
c4366c
 	}
c4366c
+	*(p++)='\0';
c4366c
 
c4366c
-int extract_port(char *str, short *port_ptr)
c4366c
-	{
c4366c
-	int i;
c4366c
-	struct servent *s;
c4366c
+	if (host_ptr != NULL) *host_ptr=h;
c4366c
+	if (port_ptr != NULL) *port_ptr=p;
c4366c
 
c4366c
-	i=atoi(str);
c4366c
-	if (i != 0)
c4366c
-		*port_ptr=(unsigned short)i;
c4366c
-	else
c4366c
-		{
c4366c
-		s=getservbyname(str,"tcp");
c4366c
-		if (s == NULL)
c4366c
-			{
c4366c
-			BIO_printf(bio_err,"getservbyname failure for %s\n",str);
c4366c
-			return(0);
c4366c
-			}
c4366c
-		*port_ptr=ntohs((unsigned short)s->s_port);
c4366c
-		}
c4366c
 	return(1);
c4366c
 	}
c4366c