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

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