source: trunk/server/common/patches/openssl-1.0.0n-ipv6-apps.patch @ 2558

Last change on this file since 2558 was 2558, checked in by andersk, 10 years ago
Update OpenSSL to 1.0.0n
File size: 13.0 KB
RevLine 
[2558]1diff -up openssl-1.0.0b/apps/s_apps.h.ipv6-apps openssl-1.0.0b/apps/s_apps.h
2--- openssl-1.0.0b/apps/s_apps.h.ipv6-apps      2010-11-16 17:19:29.000000000 +0100
3+++ openssl-1.0.0b/apps/s_apps.h        2010-11-16 17:19:29.000000000 +0100
4@@ -148,7 +148,7 @@ typedef fd_mask fd_set;
5 #define PORT_STR        "4433"
6 #define PROTOCOL        "tcp"
7 
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);
10 #ifdef HEADER_X509_H
11 int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx);
12 #endif
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);
16 #endif
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);
23 
24 long MS_CALLBACK bio_dump_callback(BIO *bio, int cmd, const char *argp,
25                                   int argi, long argl, long ret);
26diff -up openssl-1.0.0b/apps/s_client.c.ipv6-apps openssl-1.0.0b/apps/s_client.c
27--- openssl-1.0.0b/apps/s_client.c.ipv6-apps    2010-11-16 17:19:29.000000000 +0100
28+++ openssl-1.0.0b/apps/s_client.c      2010-11-16 17:19:29.000000000 +0100
29@@ -389,7 +389,7 @@ int MAIN(int argc, char **argv)
30        int cbuf_len,cbuf_off;
31        int sbuf_len,sbuf_off;
32        fd_set readfds,writefds;
33-       short port=PORT;
34+       char *port_str = PORT_STR;
35        int full_log=1;
36        char *host=SSL_HOST_NAME;
37        char *cert_file=NULL,*key_file=NULL;
38@@ -488,13 +488,12 @@ int MAIN(int argc, char **argv)
39                else if (strcmp(*argv,"-port") == 0)
40                        {
41                        if (--argc < 1) goto bad;
42-                       port=atoi(*(++argv));
43-                       if (port == 0) goto bad;
44+                       port_str= *(++argv);
45                        }
46                else if (strcmp(*argv,"-connect") == 0)
47                        {
48                        if (--argc < 1) goto bad;
49-                       if (!extract_host_port(*(++argv),&host,NULL,&port))
50+                       if (!extract_host_port(*(++argv),&host,&port_str))
51                                goto bad;
52                        }
53                else if (strcmp(*argv,"-verify") == 0)
54@@ -967,7 +966,7 @@ bad:
55 
56 re_start:
57 
58-       if (init_client(&s,host,port,socket_type) == 0)
59+       if (init_client(&s,host,port_str,socket_type) == 0)
60                {
61                BIO_printf(bio_err,"connect:errno=%d\n",get_last_socket_error());
62                SHUTDOWN(s);
63diff -up openssl-1.0.0b/apps/s_server.c.ipv6-apps openssl-1.0.0b/apps/s_server.c
64--- openssl-1.0.0b/apps/s_server.c.ipv6-apps    2010-11-16 17:19:29.000000000 +0100
65+++ openssl-1.0.0b/apps/s_server.c      2010-11-16 17:19:29.000000000 +0100
66@@ -838,7 +838,7 @@ int MAIN(int argc, char *argv[])
67        {
68        X509_VERIFY_PARAM *vpm = NULL;
69        int badarg = 0;
70-       short port=PORT;
71+       char *port_str = PORT_STR;
72        char *CApath=NULL,*CAfile=NULL;
73        unsigned char *context = NULL;
74        char *dhfile = NULL;
75@@ -909,8 +909,7 @@ int MAIN(int argc, char *argv[])
76                         (strcmp(*argv,"-accept") == 0))
77                        {
78                        if (--argc < 1) goto bad;
79-                       if (!extract_port(*(++argv),&port))
80-                               goto bad;
81+                       port_str= *(++argv);
82                        }
83                else if (strcmp(*argv,"-verify") == 0)
84                        {
85@@ -1700,9 +1699,9 @@ bad:
86        BIO_printf(bio_s_out,"ACCEPT\n");
87        (void)BIO_flush(bio_s_out);
88        if (www)
89-               do_server(port,socket_type,&accept_socket,www_body, context);
90+               do_server(port_str,socket_type,&accept_socket,www_body, context);
91        else
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);
95        ret=0;
96 end:
97diff -up openssl-1.0.0b/apps/s_socket.c.ipv6-apps openssl-1.0.0b/apps/s_socket.c
98--- openssl-1.0.0b/apps/s_socket.c.ipv6-apps    2010-07-05 13:03:22.000000000 +0200
99+++ openssl-1.0.0b/apps/s_socket.c      2010-11-16 17:27:18.000000000 +0100
100@@ -102,9 +102,7 @@ static struct hostent *GetHostByName(cha
101 static void ssl_sock_cleanup(void);
102 #endif
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]);
110 
111@@ -234,58 +232,70 @@ static int ssl_sock_init(void)
112        return(1);
113        }
114 
115-int init_client(int *sock, char *host, int port, int type)
116+int init_client(int *sock, char *host, char *port, int type)
117        {
118-       unsigned char ip[4];
119-
120-       if (!host_ip(host,&(ip[0])))
121-               {
122-               return(0);
123-               }
124-       return(init_client_ip(sock,ip,port,type));
125-       }
126-
127-static int init_client_ip(int *sock, unsigned char ip[4], int port, int type)
128-       {
129-       unsigned long addr;
130-       struct sockaddr_in them;
131-       int s,i;
132+       struct addrinfo *res, *res0, hints;
133+       char * failed_call = NULL;
134+       int s;
135+       int e;
136 
137        if (!ssl_sock_init()) return(0);
138 
139-       memset((char *)&them,0,sizeof(them));
140-       them.sin_family=AF_INET;
141-       them.sin_port=htons((unsigned short)port);
142-       addr=(unsigned long)
143-               ((unsigned long)ip[0]<<24L)|
144-               ((unsigned long)ip[1]<<16L)|
145-               ((unsigned long)ip[2]<< 8L)|
146-               ((unsigned long)ip[3]);
147-       them.sin_addr.s_addr=htonl(addr);
148-
149-       if (type == SOCK_STREAM)
150-               s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
151-       else /* ( type == SOCK_DGRAM) */
152-               s=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
153-                       
154-       if (s == INVALID_SOCKET) { perror("socket"); return(0); }
155+       memset(&hints, '\0', sizeof(hints));
156+       hints.ai_socktype = type;
157+       hints.ai_flags = AI_ADDRCONFIG;
158+
159+       e = getaddrinfo(host, port, &hints, &res);
160+       if (e)
161+       {
162+               fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(e));
163+               if (e == EAI_SYSTEM)
164+                       perror("getaddrinfo");
165+               return (0);
166+               }
167 
168+       res0 = res;
169+       while (res)
170+               {
171+               s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
172+               if (s == INVALID_SOCKET)
173+                       {
174+                       failed_call = "socket";
175+                       goto nextres;
176+                       }
177 #if defined(SO_KEEPALIVE) && !defined(OPENSSL_SYS_MPE)
178        if (type == SOCK_STREAM)
179                {
180-               i=0;
181-               i=setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
182-               if (i < 0) { closesocket(s); perror("keepalive"); return(0); }
183+                       int i=0;
184+                       i=setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,
185+                               (char *)&i,sizeof(i));
186+                       if (i < 0) {
187+                               failed_call = "keepalive";
188+                               goto nextres;
189+                               }
190                }
191 #endif
192-
193-       if (connect(s,(struct sockaddr *)&them,sizeof(them)) == -1)
194-               { closesocket(s); perror("connect"); return(0); }
195+               if (connect(s,(struct sockaddr *)res->ai_addr,
196+                       res->ai_addrlen) == 0)
197+                       {
198+                       freeaddrinfo(res0);
199        *sock=s;
200        return(1);
201        }
202 
203-int do_server(int port, int type, int *ret, int (*cb)(char *hostname, int s, unsigned char *context), unsigned char *context)
204+               failed_call = "socket";
205+nextres:
206+               if (s != INVALID_SOCKET)
207+                       close(s);
208+               res = res->ai_next;
209+               }
210+       freeaddrinfo(res0);
211+
212+       perror(failed_call);
213+       return(0);
214+       }
215+
216+int do_server(char *port, int type, int *ret, int (*cb)(char *hostname, int s, unsigned char *context), unsigned char *context)
217        {
218        int sock;
219        char *name = NULL;
220@@ -323,33 +333,38 @@ int do_server(int port, int type, int *r
221                }
222        }
223 
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)
226        {
227-       int ret=0;
228-       struct sockaddr_in server;
229-       int s= -1;
230+       struct addrinfo *res, *res0, hints;
231+       char * failed_call = NULL;
232+       char port_name[8];
233+       int s;
234+       int e;
235 
236        if (!ssl_sock_init()) return(0);
237 
238-       memset((char *)&server,0,sizeof(server));
239-       server.sin_family=AF_INET;
240-       server.sin_port=htons((unsigned short)port);
241-       if (ip == NULL)
242-               server.sin_addr.s_addr=INADDR_ANY;
243-       else
244-/* Added for T3E, address-of fails on bit field (beckman@acl.lanl.gov) */
245-#ifndef BIT_FIELD_LIMITS
246-               memcpy(&server.sin_addr.s_addr,ip,4);
247-#else
248-               memcpy(&server.sin_addr,ip,4);
249-#endif
250+       memset(&hints, '\0', sizeof(hints));
251+       hints.ai_socktype = type;
252+       hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
253       
254-               if (type == SOCK_STREAM)
255-                       s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
256-               else /* type == SOCK_DGRAM */
257-                       s=socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP);
258+       e = getaddrinfo(NULL, port, &hints, &res);
259+       if (e)
260+               {
261+               fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(e));
262+               if (e == EAI_SYSTEM)
263+                       perror("getaddrinfo");
264+               return (0);
265+               }
266 
267-       if (s == INVALID_SOCKET) goto err;
268+       res0 = res;
269+       while (res)
270+               {
271+               s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
272+               if (s == INVALID_SOCKET)
273+                       {
274+                       failed_call = "socket";
275+                       goto nextres;
276+                       }
277 #if defined SOL_SOCKET && defined SO_REUSEADDR
278                {
279                int j = 1;
280@@ -357,35 +372,39 @@ static int init_server_long(int *sock, i
281                           (void *) &j, sizeof j);
282                }
283 #endif
284-       if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1)
285+
286+               if (bind(s,(struct sockaddr *)res->ai_addr, res->ai_addrlen) == -1)
287                {
288-#ifndef OPENSSL_SYS_WINDOWS
289-               perror("bind");
290-#endif
291-               goto err;
292+                       failed_call = "bind";
293+                       goto nextres;
294                }
295-       /* Make it 128 for linux */
296-       if (type==SOCK_STREAM && listen(s,128) == -1) goto err;
297-       *sock=s;
298-       ret=1;
299-err:
300-       if ((ret == 0) && (s != -1))
301+               if (type==SOCK_STREAM && listen(s,128) == -1)
302                {
303-               SHUTDOWN(s);
304+                       failed_call = "listen";
305+                       goto nextres;
306                }
307-       return(ret);
308+
309+               *sock=s;
310+               return(1);
311+
312+nextres:
313+               if (s != INVALID_SOCKET)
314+                       close(s);
315+               res = res->ai_next;
316        }
317+       freeaddrinfo(res0);
318 
319-static int init_server(int *sock, int port, int type)
320-       {
321-       return(init_server_long(sock, port, NULL, type));
322+       if (s == INVALID_SOCKET) { perror("socket"); return(0); }
323+
324+       perror(failed_call);
325+       return(0);
326        }
327 
328 static int do_accept(int acc_sock, int *sock, char **host)
329        {
330+       static struct sockaddr_storage from;
331+       char buffer[NI_MAXHOST];
332        int ret;
333-       struct hostent *h1,*h2;
334-       static struct sockaddr_in from;
335        int len;
336 /*     struct linger ling; */
337 
338@@ -432,138 +451,59 @@ redoit:
339 */
340 
341        if (host == NULL) goto end;
342-#ifndef BIT_FIELD_LIMITS
343-       /* I should use WSAAsyncGetHostByName() under windows */
344-       h1=gethostbyaddr((char *)&from.sin_addr.s_addr,
345-               sizeof(from.sin_addr.s_addr),AF_INET);
346-#else
347-       h1=gethostbyaddr((char *)&from.sin_addr,
348-               sizeof(struct in_addr),AF_INET);
349-#endif
350-       if (h1 == NULL)
351+
352+       if (getnameinfo((struct sockaddr *)&from, sizeof(from),
353+               buffer, sizeof(buffer),
354+               NULL, 0, 0))
355                {
356-               BIO_printf(bio_err,"bad gethostbyaddr\n");
357+               BIO_printf(bio_err,"getnameinfo failed\n");
358                *host=NULL;
359                /* return(0); */
360                }
361        else
362                {
363-               if ((*host=(char *)OPENSSL_malloc(strlen(h1->h_name)+1)) == NULL)
364+               if ((*host=(char *)OPENSSL_malloc(strlen(buffer)+1)) == NULL)
365                        {
366                        perror("OPENSSL_malloc");
367                        closesocket(ret);
368                        return(0);
369                        }
370-               BUF_strlcpy(*host,h1->h_name,strlen(h1->h_name)+1);
371-
372-               h2=GetHostByName(*host);
373-               if (h2 == NULL)
374-                       {
375-                       BIO_printf(bio_err,"gethostbyname failure\n");
376-                       closesocket(ret);
377-                       return(0);
378-                       }
379-               if (h2->h_addrtype != AF_INET)
380-                       {
381-                       BIO_printf(bio_err,"gethostbyname addr is not AF_INET\n");
382-                       closesocket(ret);
383-                       return(0);
384-                       }
385+               strcpy(*host, buffer);
386                }
387 end:
388        *sock=ret;
389        return(1);
390        }
391 
392-int extract_host_port(char *str, char **host_ptr, unsigned char *ip,
393-            short *port_ptr)
394+int extract_host_port(char *str, char **host_ptr,
395+            char **port_ptr)
396        {
397-       char *h,*p;
398+       char *h,*p,*x;
399 
400-       h=str;
401-       p=strchr(str,':');
402+       x=h=str;
403+       if (*h == '[')
404+               {
405+               h++;
406+               p=strchr(h,']');
407        if (p == NULL)
408                {
409-               BIO_printf(bio_err,"no port defined\n");
410+                       BIO_printf(bio_err,"no ending bracket for IPv6 address\n");
411                return(0);
412                }
413        *(p++)='\0';
414-
415-       if ((ip != NULL) && !host_ip(str,ip))
416-               goto err;
417-       if (host_ptr != NULL) *host_ptr=h;
418-
419-       if (!extract_port(p,port_ptr))
420-               goto err;
421-       return(1);
422-err:
423-       return(0);
424+               x = p;
425        }
426-
427-static int host_ip(char *str, unsigned char ip[4])
428-       {
429-       unsigned int in[4];
430-       int i;
431-
432-       if (sscanf(str,"%u.%u.%u.%u",&(in[0]),&(in[1]),&(in[2]),&(in[3])) == 4)
433-               {
434-               for (i=0; i<4; i++)
435-                       if (in[i] > 255)
436-                               {
437-                               BIO_printf(bio_err,"invalid IP address\n");
438-                               goto err;
439-                               }
440-               ip[0]=in[0];
441-               ip[1]=in[1];
442-               ip[2]=in[2];
443-               ip[3]=in[3];
444-               }
445-       else
446-               { /* do a gethostbyname */
447-               struct hostent *he;
448-
449-               if (!ssl_sock_init()) return(0);
450-
451-               he=GetHostByName(str);
452-               if (he == NULL)
453-                       {
454-                       BIO_printf(bio_err,"gethostbyname failure\n");
455-                       goto err;
456-                       }
457-               /* cast to short because of win16 winsock definition */
458-               if ((short)he->h_addrtype != AF_INET)
459+       p=strchr(x,':');
460+       if (p == NULL)
461                        {
462-                       BIO_printf(bio_err,"gethostbyname addr is not AF_INET\n");
463-                       return(0);
464-                       }
465-               ip[0]=he->h_addr_list[0][0];
466-               ip[1]=he->h_addr_list[0][1];
467-               ip[2]=he->h_addr_list[0][2];
468-               ip[3]=he->h_addr_list[0][3];
469-               }
470-       return(1);
471-err:
472+               BIO_printf(bio_err,"no port defined\n");
473        return(0);
474        }
475+       *(p++)='\0';
476 
477-int extract_port(char *str, short *port_ptr)
478-       {
479-       int i;
480-       struct servent *s;
481+       if (host_ptr != NULL) *host_ptr=h;
482+       if (port_ptr != NULL) *port_ptr=p;
483 
484-       i=atoi(str);
485-       if (i != 0)
486-               *port_ptr=(unsigned short)i;
487-       else
488-               {
489-               s=getservbyname(str,"tcp");
490-               if (s == NULL)
491-                       {
492-                       BIO_printf(bio_err,"getservbyname failure for %s\n",str);
493-                       return(0);
494-                       }
495-               *port_ptr=ntohs((unsigned short)s->s_port);
496-               }
497        return(1);
498        }
499 
Note: See TracBrowser for help on using the repository browser.