2 * apps/s_socket.c - socket-related functions used by s_client and s_server
4 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
7 * This package is an SSL implementation written
8 * by Eric Young (eay@cryptsoft.com).
9 * The implementation was written so as to conform with Netscapes SSL.
11 * This library is free for commercial and non-commercial use as long as
12 * the following conditions are aheared to. The following conditions
13 * apply to all code found in this distribution, be it the RC4, RSA,
14 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
15 * included with this distribution is covered by the same copyright terms
16 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
18 * Copyright remains Eric Young's, and as such any Copyright notices in
19 * the code are not to be removed.
20 * If this package is used in a product, Eric Young should be given attribution
21 * as the author of the parts of the library used.
22 * This can be in the form of a textual message at program startup or
23 * in documentation (online or textual) provided with the package.
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
28 * 1. Redistributions of source code must retain the copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
33 * 3. All advertising materials mentioning features or use of this software
34 * must display the following acknowledgement:
35 * "This product includes cryptographic software written by
36 * Eric Young (eay@cryptsoft.com)"
37 * The word 'cryptographic' can be left out if the rouines from the library
38 * being used are not cryptographic related :-).
39 * 4. If you include any Windows specific code (or a derivative thereof) from
40 * the apps directory (application code) you must include an acknowledgement:
41 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
43 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * The licence and distribution terms for any publically available version or
56 * derivative of this code cannot be changed. i.e. this code cannot simply be
57 * copied and put under another distribution licence
58 * [including the GNU Public Licence.]
70 # include "../e_os2.h"
74 * With IPv6, it looks like Digital has mixed up the proper order of
75 * recursive header file inclusion, resulting in the compiler complaining
76 * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
77 * needed to have fileno() declared correctly... So let's define u_int
79 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
81 typedef unsigned int u_int
;
90 #include <openssl/ssl.h>
98 #ifndef OPENSSL_NO_SOCK
100 # if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_BSDSOCK)
104 static struct hostent
*GetHostByName(char *name
);
105 # if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK))
106 static void ssl_sock_cleanup(void);
108 static int ssl_sock_init(void);
109 static int init_client_ip(int *sock
, unsigned char ip
[4], int port
, int type
);
110 static int init_server(int *sock
, int port
, int type
);
111 static int init_server_long(int *sock
, int port
, char *ip
, int type
);
112 static int do_accept(int acc_sock
, int *sock
, char **host
);
113 static int host_ip(char *str
, unsigned char ip
[4]);
115 # ifdef OPENSSL_SYS_WIN16
116 # define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
118 # define SOCKET_PROTOCOL IPPROTO_TCP
121 # if defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
122 static int wsa_init_done
= 0;
125 # ifdef OPENSSL_SYS_WINDOWS
126 static struct WSAData wsa_state
;
127 static int wsa_init_done
= 0;
129 # ifdef OPENSSL_SYS_WIN16
130 static HWND topWnd
= 0;
131 static FARPROC lpTopWndProc
= NULL
;
132 static FARPROC lpTopHookProc
= NULL
;
133 extern HINSTANCE _hInstance
; /* nice global CRT provides */
135 static LONG FAR PASCAL
topHookProc(HWND hwnd
, UINT message
, WPARAM wParam
,
138 if (hwnd
== topWnd
) {
142 SetWindowLong(topWnd
, GWL_WNDPROC
, (LONG
) lpTopWndProc
);
147 return CallWindowProc(lpTopWndProc
, hwnd
, message
, wParam
, lParam
);
150 static BOOL CALLBACK
enumproc(HWND hwnd
, LPARAM lParam
)
156 # endif /* OPENSSL_SYS_WIN32 */
157 # endif /* OPENSSL_SYS_WINDOWS */
159 # ifdef OPENSSL_SYS_WINDOWS
160 static void ssl_sock_cleanup(void)
164 # ifndef OPENSSL_SYS_WINCE
165 WSACancelBlockingCall();
170 # elif defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
171 static void sock_cleanup(void)
180 static int ssl_sock_init(void)
183 extern int _watt_do_exit
;
187 # elif defined(OPENSSL_SYS_WINDOWS)
188 if (!wsa_init_done
) {
192 signal(SIGINT
, (void (*)(int))ssl_sock_cleanup
);
195 memset(&wsa_state
, 0, sizeof(wsa_state
));
196 if (WSAStartup(0x0101, &wsa_state
) != 0) {
197 err
= WSAGetLastError();
198 BIO_printf(bio_err
, "unable to start WINSOCK, error code=%d\n",
202 # ifdef OPENSSL_SYS_WIN16
203 EnumTaskWindows(GetCurrentTask(), enumproc
, 0L);
204 lpTopWndProc
= (FARPROC
) GetWindowLong(topWnd
, GWL_WNDPROC
);
205 lpTopHookProc
= MakeProcInstance((FARPROC
) topHookProc
, _hInstance
);
207 SetWindowLong(topWnd
, GWL_WNDPROC
, (LONG
) lpTopHookProc
);
208 # endif /* OPENSSL_SYS_WIN16 */
210 # elif defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
215 if (!wsa_init_done
) {
218 signal(SIGINT
, (void (*)(int))sock_cleanup
);
222 wVerReq
= MAKEWORD(2, 0);
223 err
= WSAStartup(wVerReq
, &wsaData
);
225 BIO_printf(bio_err
, "unable to start WINSOCK2, error code=%d\n",
230 # endif /* OPENSSL_SYS_WINDOWS */
234 int init_client(int *sock
, char *host
, int port
, int type
)
238 memset(ip
, '\0', sizeof ip
);
239 if (!host_ip(host
, &(ip
[0])))
241 return init_client_ip(sock
, ip
, port
, type
);
244 static int init_client_ip(int *sock
, unsigned char ip
[4], int port
, int type
)
247 struct sockaddr_in them
;
250 if (!ssl_sock_init())
253 memset((char *)&them
, 0, sizeof(them
));
254 them
.sin_family
= AF_INET
;
255 them
.sin_port
= htons((unsigned short)port
);
256 addr
= (unsigned long)
257 ((unsigned long)ip
[0] << 24L) |
258 ((unsigned long)ip
[1] << 16L) |
259 ((unsigned long)ip
[2] << 8L) | ((unsigned long)ip
[3]);
260 them
.sin_addr
.s_addr
= htonl(addr
);
262 if (type
== SOCK_STREAM
)
263 s
= socket(AF_INET
, SOCK_STREAM
, SOCKET_PROTOCOL
);
264 else /* ( type == SOCK_DGRAM) */
265 s
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
267 if (s
== INVALID_SOCKET
) {
271 # if defined(SO_KEEPALIVE) && !defined(OPENSSL_SYS_MPE)
272 if (type
== SOCK_STREAM
) {
274 i
= setsockopt(s
, SOL_SOCKET
, SO_KEEPALIVE
, (char *)&i
, sizeof(i
));
283 if (connect(s
, (struct sockaddr
*)&them
, sizeof(them
)) == -1) {
292 int do_server(int port
, int type
, int *ret
,
293 int (*cb
) (char *hostname
, int s
, unsigned char *context
),
294 unsigned char *context
)
298 int accept_socket
= 0;
301 if (!init_server(&accept_socket
, port
, type
))
305 *ret
= accept_socket
;
309 if (type
== SOCK_STREAM
) {
310 if (do_accept(accept_socket
, &sock
, &name
) == 0) {
311 SHUTDOWN(accept_socket
);
315 sock
= accept_socket
;
316 i
= (*cb
) (name
, sock
, context
);
319 if (type
== SOCK_STREAM
)
322 SHUTDOWN2(accept_socket
);
328 static int init_server_long(int *sock
, int port
, char *ip
, int type
)
331 struct sockaddr_in server
;
334 if (!ssl_sock_init())
337 memset((char *)&server
, 0, sizeof(server
));
338 server
.sin_family
= AF_INET
;
339 server
.sin_port
= htons((unsigned short)port
);
341 server
.sin_addr
.s_addr
= INADDR_ANY
;
343 /* Added for T3E, address-of fails on bit field (beckman@acl.lanl.gov) */
344 # ifndef BIT_FIELD_LIMITS
345 memcpy(&server
.sin_addr
.s_addr
, ip
, 4);
347 memcpy(&server
.sin_addr
, ip
, 4);
350 if (type
== SOCK_STREAM
)
351 s
= socket(AF_INET
, SOCK_STREAM
, SOCKET_PROTOCOL
);
352 else /* type == SOCK_DGRAM */
353 s
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
355 if (s
== INVALID_SOCKET
)
357 # if defined SOL_SOCKET && defined SO_REUSEADDR
360 setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, (void *)&j
, sizeof j
);
363 if (bind(s
, (struct sockaddr
*)&server
, sizeof(server
)) == -1) {
364 # ifndef OPENSSL_SYS_WINDOWS
369 /* Make it 128 for linux */
370 if (type
== SOCK_STREAM
&& listen(s
, 128) == -1)
375 if ((ret
== 0) && (s
!= -1)) {
381 static int init_server(int *sock
, int port
, int type
)
383 return (init_server_long(sock
, port
, NULL
, type
));
386 static int do_accept(int acc_sock
, int *sock
, char **host
)
389 struct hostent
*h1
, *h2
;
390 static struct sockaddr_in from
;
392 /* struct linger ling; */
394 if (!ssl_sock_init())
397 # ifndef OPENSSL_SYS_WINDOWS
401 memset((char *)&from
, 0, sizeof(from
));
404 * Note: under VMS with SOCKETSHR the fourth parameter is currently of
405 * type (int *) whereas under other systems it is (void *) if you don't
406 * have a cast it will choke the compiler: if you do have a cast then you
407 * can either go for (int *) or (void *).
409 ret
= accept(acc_sock
, (struct sockaddr
*)&from
, (void *)&len
);
410 if (ret
== INVALID_SOCKET
) {
411 # if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK))
413 i
= WSAGetLastError();
414 BIO_printf(bio_err
, "accept error %d\n", i
);
416 if (errno
== EINTR
) {
422 fprintf(stderr
, "errno=%d ", errno
);
431 i=setsockopt(ret,SOL_SOCKET,SO_LINGER,(char *)&ling,sizeof(ling));
432 if (i < 0) { perror("linger"); return(0); }
434 i=setsockopt(ret,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
435 if (i < 0) { perror("keepalive"); return(0); }
440 # ifndef BIT_FIELD_LIMITS
441 /* I should use WSAAsyncGetHostByName() under windows */
442 h1
= gethostbyaddr((char *)&from
.sin_addr
.s_addr
,
443 sizeof(from
.sin_addr
.s_addr
), AF_INET
);
445 h1
= gethostbyaddr((char *)&from
.sin_addr
,
446 sizeof(struct in_addr
), AF_INET
);
449 BIO_printf(bio_err
, "bad gethostbyaddr\n");
453 if ((*host
= (char *)OPENSSL_malloc(strlen(h1
->h_name
) + 1)) == NULL
) {
454 perror("OPENSSL_malloc");
458 BUF_strlcpy(*host
, h1
->h_name
, strlen(h1
->h_name
) + 1);
460 h2
= GetHostByName(*host
);
462 BIO_printf(bio_err
, "gethostbyname failure\n");
466 if (h2
->h_addrtype
!= AF_INET
) {
467 BIO_printf(bio_err
, "gethostbyname addr is not AF_INET\n");
477 int extract_host_port(char *str
, char **host_ptr
, unsigned char *ip
,
483 p
= strchr(str
, ':');
485 BIO_printf(bio_err
, "no port defined\n");
490 if ((ip
!= NULL
) && !host_ip(str
, ip
))
492 if (host_ptr
!= NULL
)
495 if (!extract_port(p
, port_ptr
))
502 static int host_ip(char *str
, unsigned char ip
[4])
507 if (sscanf(str
, "%u.%u.%u.%u", &(in
[0]), &(in
[1]), &(in
[2]), &(in
[3])) ==
509 for (i
= 0; i
< 4; i
++)
511 BIO_printf(bio_err
, "invalid IP address\n");
518 } else { /* do a gethostbyname */
521 if (!ssl_sock_init())
524 he
= GetHostByName(str
);
526 BIO_printf(bio_err
, "gethostbyname failure\n");
529 /* cast to short because of win16 winsock definition */
530 if ((short)he
->h_addrtype
!= AF_INET
) {
531 BIO_printf(bio_err
, "gethostbyname addr is not AF_INET\n");
534 ip
[0] = he
->h_addr_list
[0][0];
535 ip
[1] = he
->h_addr_list
[0][1];
536 ip
[2] = he
->h_addr_list
[0][2];
537 ip
[3] = he
->h_addr_list
[0][3];
544 int extract_port(char *str
, short *port_ptr
)
551 *port_ptr
= (unsigned short)i
;
553 s
= getservbyname(str
, "tcp");
555 BIO_printf(bio_err
, "getservbyname failure for %s\n", str
);
558 *port_ptr
= ntohs((unsigned short)s
->s_port
);
564 static struct ghbn_cache_st
{
568 } ghbn_cache
[GHBN_NUM
];
570 static unsigned long ghbn_hits
= 0L;
571 static unsigned long ghbn_miss
= 0L;
573 static struct hostent
*GetHostByName(char *name
)
577 unsigned long low
= (unsigned long)-1;
579 for (i
= 0; i
< GHBN_NUM
; i
++) {
580 if (low
> ghbn_cache
[i
].order
) {
581 low
= ghbn_cache
[i
].order
;
584 if (ghbn_cache
[i
].order
> 0) {
585 if (strncmp(name
, ghbn_cache
[i
].name
, 128) == 0)
589 if (i
== GHBN_NUM
) { /* no hit */
591 ret
= gethostbyname(name
);
594 /* else add to cache */
595 if (strlen(name
) < sizeof ghbn_cache
[0].name
) {
596 strcpy(ghbn_cache
[lowi
].name
, name
);
597 memcpy((char *)&(ghbn_cache
[lowi
].ent
), ret
,
598 sizeof(struct hostent
));
599 ghbn_cache
[lowi
].order
= ghbn_miss
+ ghbn_hits
;
604 ret
= &(ghbn_cache
[i
].ent
);
605 ghbn_cache
[i
].order
= ghbn_miss
+ ghbn_hits
;