1 /* crypto/bio/b_sock.c */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 59 #include <sys/ioctl.h> 60 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <unistd.h> 64 #include <errno.h> 65 #include "cryptlib.h" 66 #include <openssl/bio.h> 67 #include <sys/ioctl.h> 68 69 #include <netdb.h> 70 #include <sys/socket.h> 71 #include <netinet/in.h> 72 73 74 #include <openssl/dso.h> 75 76 #define SOCKET_PROTOCOL IPPROTO_TCP 77 78 #ifdef SO_MAXCONN 79 #define MAX_LISTEN SO_MAXCONN 80 #elif defined(SOMAXCONN) 81 #define MAX_LISTEN SOMAXCONN 82 #else 83 #define MAX_LISTEN 32 84 #endif 85 86 static int get_ip(const char *str, unsigned char *ip); 87 88 int 89 BIO_get_host_ip(const char *str, unsigned char *ip) 90 { 91 int i; 92 int err = 1; 93 int locked = 0; 94 struct hostent *he; 95 96 i = get_ip(str, ip); 97 if (i < 0) { 98 BIOerr(BIO_F_BIO_GET_HOST_IP, BIO_R_INVALID_IP_ADDRESS); 99 goto err; 100 } 101 102 /* At this point, we have something that is most probably correct 103 in some way, so let's init the socket. */ 104 if (BIO_sock_init() != 1) 105 return 0; /* don't generate another error code here */ 106 107 /* If the string actually contained an IP address, we need not do 108 anything more */ 109 if (i > 0) 110 return (1); 111 112 /* do a gethostbyname */ 113 CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME); 114 locked = 1; 115 he = BIO_gethostbyname(str); 116 if (he == NULL) { 117 BIOerr(BIO_F_BIO_GET_HOST_IP, BIO_R_BAD_HOSTNAME_LOOKUP); 118 goto err; 119 } 120 121 /* cast to short because of win16 winsock definition */ 122 if ((short)he->h_addrtype != AF_INET) { 123 BIOerr(BIO_F_BIO_GET_HOST_IP, BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET); 124 goto err; 125 } 126 for (i = 0; i < 4; i++) 127 ip[i] = he->h_addr_list[0][i]; 128 err = 0; 129 130 err: 131 if (locked) 132 CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME); 133 if (err) { 134 ERR_asprintf_error_data("host=%s", str); 135 return 0; 136 } else 137 return 1; 138 } 139 140 int 141 BIO_get_port(const char *str, unsigned short *port_ptr) 142 { 143 int i; 144 struct servent *s; 145 146 if (str == NULL) { 147 BIOerr(BIO_F_BIO_GET_PORT, BIO_R_NO_PORT_DEFINED); 148 return (0); 149 } 150 i = atoi(str); 151 if (i != 0) 152 *port_ptr = (unsigned short)i; 153 else { 154 CRYPTO_w_lock(CRYPTO_LOCK_GETSERVBYNAME); 155 s = getservbyname(str, "tcp"); 156 if (s != NULL) 157 *port_ptr = ntohs((unsigned short)s->s_port); 158 CRYPTO_w_unlock(CRYPTO_LOCK_GETSERVBYNAME); 159 if (s == NULL) { 160 if (strcmp(str, "http") == 0) 161 *port_ptr = 80; 162 else if (strcmp(str, "telnet") == 0) 163 *port_ptr = 23; 164 else if (strcmp(str, "socks") == 0) 165 *port_ptr = 1080; 166 else if (strcmp(str, "https") == 0) 167 *port_ptr = 443; 168 else if (strcmp(str, "ssl") == 0) 169 *port_ptr = 443; 170 else if (strcmp(str, "ftp") == 0) 171 *port_ptr = 21; 172 else if (strcmp(str, "gopher") == 0) 173 *port_ptr = 70; 174 else { 175 SYSerr(SYS_F_GETSERVBYNAME, errno); 176 ERR_asprintf_error_data("service='%s'", str); 177 return (0); 178 } 179 } 180 } 181 return (1); 182 } 183 184 int 185 BIO_sock_error(int sock) 186 { 187 int j, i; 188 int size; 189 190 size = sizeof(int); 191 /* Note: under Windows the third parameter is of type (char *) 192 * whereas under other systems it is (void *) if you don't have 193 * a cast it will choke the compiler: if you do have a cast then 194 * you can either go for (char *) or (void *). 195 */ 196 i = getsockopt(sock, SOL_SOCKET, SO_ERROR, (void *)&j, (void *)&size); 197 if (i < 0) 198 return (1); 199 else 200 return (j); 201 } 202 203 struct hostent * 204 BIO_gethostbyname(const char *name) 205 { 206 return gethostbyname(name); 207 } 208 209 210 int 211 BIO_sock_init(void) 212 { 213 return (1); 214 } 215 216 void 217 BIO_sock_cleanup(void) 218 { 219 } 220 221 int 222 BIO_socket_ioctl(int fd, long type, void *arg) 223 { 224 int i; 225 226 # define ARG arg 227 228 i = ioctl(fd, type, ARG); 229 if (i < 0) 230 SYSerr(SYS_F_IOCTLSOCKET, errno); 231 return (i); 232 } 233 234 /* The reason I have implemented this instead of using sscanf is because 235 * Visual C 1.52c gives an unresolved external when linking a DLL :-( */ 236 static int 237 get_ip(const char *str, unsigned char ip[4]) 238 { 239 unsigned int tmp[4]; 240 int num = 0, c, ok = 0; 241 242 tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0; 243 244 for (;;) { 245 c= *(str++); 246 if ((c >= '0') && (c <= '9')) { 247 ok = 1; 248 tmp[num] = tmp[num]*10 + c-'0'; 249 if (tmp[num] > 255) 250 return (0); 251 } else if (c == '.') { 252 if (!ok) 253 return (-1); 254 if (num == 3) 255 return (0); 256 num++; 257 ok = 0; 258 } else if (c == '\0' && (num == 3) && ok) 259 break; 260 else 261 return (0); 262 } 263 ip[0] = tmp[0]; 264 ip[1] = tmp[1]; 265 ip[2] = tmp[2]; 266 ip[3] = tmp[3]; 267 return (1); 268 } 269 270 int 271 BIO_get_accept_socket(char *host, int bind_mode) 272 { 273 int ret = 0; 274 union { 275 struct sockaddr sa; 276 struct sockaddr_in sa_in; 277 struct sockaddr_in6 sa_in6; 278 } server, client; 279 int s = -1, cs, addrlen; 280 unsigned char ip[4]; 281 unsigned short port; 282 char *str = NULL, *e; 283 char *h, *p; 284 unsigned long l; 285 int err_num; 286 287 if (BIO_sock_init() != 1) 288 return (-1); 289 290 if ((str = BUF_strdup(host)) == NULL) 291 return (-1); 292 293 h = p = NULL; 294 h = str; 295 for (e = str; *e; e++) { 296 if (*e == ':') { 297 p = e; 298 } else if (*e == '/') { 299 *e = '\0'; 300 break; 301 } 302 } 303 if (p) 304 *p++='\0'; /* points at last ':', '::port' is special [see below] */ 305 else 306 p = h, h = NULL; 307 308 #ifdef EAI_FAMILY 309 do { 310 static union { 311 void *p; 312 int (*f)(const char *, const char *, 313 const struct addrinfo *, 314 struct addrinfo **); 315 } p_getaddrinfo = {NULL}; 316 static union { 317 void *p; 318 void (*f)(struct addrinfo *); 319 } p_freeaddrinfo = {NULL}; 320 struct addrinfo *res, hint; 321 322 if (p_getaddrinfo.p == NULL) { 323 if ((p_getaddrinfo.p = DSO_global_lookup("getaddrinfo"))==NULL || 324 (p_freeaddrinfo.p = DSO_global_lookup("freeaddrinfo"))==NULL) 325 p_getaddrinfo.p = (void*) - 1; 326 } 327 if (p_getaddrinfo.p == (void *) - 1) 328 break; 329 330 /* '::port' enforces IPv6 wildcard listener. Some OSes, 331 * e.g. Solaris, default to IPv6 without any hint. Also 332 * note that commonly IPv6 wildchard socket can service 333 * IPv4 connections just as well... */ 334 memset(&hint, 0, sizeof(hint)); 335 hint.ai_flags = AI_PASSIVE; 336 if (h) { 337 if (strchr(h, ':')) { 338 if (h[1] == '\0') 339 h = NULL; 340 hint.ai_family = AF_INET6; 341 } else if (h[0] == '*' && h[1] == '\0') { 342 hint.ai_family = AF_INET; 343 h = NULL; 344 } 345 } 346 347 if ((*p_getaddrinfo.f)(h, p, &hint, &res)) 348 break; 349 350 addrlen = res->ai_addrlen <= sizeof(server) ? 351 res->ai_addrlen : sizeof(server); 352 memcpy(&server, res->ai_addr, addrlen); 353 354 (*p_freeaddrinfo.f)(res); 355 goto again; 356 } while (0); 357 #endif 358 359 if (!BIO_get_port(p, &port)) 360 goto err; 361 362 memset((char *)&server, 0, sizeof(server)); 363 server.sa_in.sin_family = AF_INET; 364 server.sa_in.sin_port = htons(port); 365 addrlen = sizeof(server.sa_in); 366 367 if (h == NULL || strcmp(h, "*") == 0) 368 server.sa_in.sin_addr.s_addr = INADDR_ANY; 369 else { 370 if (!BIO_get_host_ip(h, &(ip[0]))) 371 goto err; 372 l = (unsigned long)((unsigned long)ip[0]<<24L)| 373 ((unsigned long)ip[1]<<16L)| 374 ((unsigned long)ip[2]<< 8L)| 375 ((unsigned long)ip[3]); 376 server.sa_in.sin_addr.s_addr = htonl(l); 377 } 378 379 again: 380 s = socket(server.sa.sa_family, SOCK_STREAM, SOCKET_PROTOCOL); 381 if (s == -1) { 382 SYSerr(SYS_F_SOCKET, errno); 383 ERR_asprintf_error_data("port='%s'", host); 384 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET, BIO_R_UNABLE_TO_CREATE_SOCKET); 385 goto err; 386 } 387 388 #ifdef SO_REUSEADDR 389 if (bind_mode == BIO_BIND_REUSEADDR) { 390 int i = 1; 391 392 ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&i, sizeof(i)); 393 bind_mode = BIO_BIND_NORMAL; 394 } 395 #endif 396 if (bind(s, &server.sa, addrlen) == -1) { 397 #ifdef SO_REUSEADDR 398 err_num = errno; 399 if ((bind_mode == BIO_BIND_REUSEADDR_IF_UNUSED) && 400 (err_num == EADDRINUSE)) { 401 client = server; 402 if (h == NULL || strcmp(h, "*") == 0) { 403 if (client.sa.sa_family == AF_INET6) { 404 memset(&client.sa_in6.sin6_addr, 0, sizeof(client.sa_in6.sin6_addr)); 405 client.sa_in6.sin6_addr.s6_addr[15] = 1; 406 } else if (client.sa.sa_family == AF_INET) { 407 client.sa_in.sin_addr.s_addr = htonl(0x7F000001); 408 } else 409 goto err; 410 } 411 cs = socket(client.sa.sa_family, SOCK_STREAM, SOCKET_PROTOCOL); 412 if (cs != -1) { 413 int ii; 414 ii = connect(cs, &client.sa, addrlen); 415 close(cs); 416 if (ii == -1) { 417 bind_mode = BIO_BIND_REUSEADDR; 418 close(s); 419 goto again; 420 } 421 /* else error */ 422 } 423 /* else error */ 424 } 425 #endif 426 SYSerr(SYS_F_BIND, err_num); 427 ERR_asprintf_error_data("port='%s'", host); 428 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET, BIO_R_UNABLE_TO_BIND_SOCKET); 429 goto err; 430 } 431 if (listen(s, MAX_LISTEN) == -1) { 432 SYSerr(SYS_F_BIND, errno); 433 ERR_asprintf_error_data("port='%s'", host); 434 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET, BIO_R_UNABLE_TO_LISTEN_SOCKET); 435 goto err; 436 } 437 ret = 1; 438 err: 439 if (str != NULL) 440 free(str); 441 if ((ret == 0) && (s != -1)) { 442 close(s); 443 s = -1; 444 } 445 return (s); 446 } 447 448 int 449 BIO_accept(int sock, char **addr) 450 { 451 int ret = -1; 452 unsigned long l; 453 unsigned short port; 454 char *p, *tmp; 455 456 struct { 457 socklen_t len; 458 union { 459 struct sockaddr sa; 460 struct sockaddr_in sa_in; 461 struct sockaddr_in6 sa_in6; 462 } from; 463 } sa; 464 465 sa.len = sizeof(sa.from); 466 memset(&sa.from, 0, sizeof(sa.from)); 467 ret = accept(sock, &sa.from.sa, &sa.len); 468 if (ret == -1) { 469 if (BIO_sock_should_retry(ret)) 470 return -2; 471 SYSerr(SYS_F_ACCEPT, errno); 472 BIOerr(BIO_F_BIO_ACCEPT, BIO_R_ACCEPT_ERROR); 473 goto end; 474 } 475 476 if (addr == NULL) 477 goto end; 478 479 #ifdef EAI_FAMILY 480 do { 481 char h[NI_MAXHOST], s[NI_MAXSERV]; 482 size_t nl; 483 static union { 484 void *p; 485 int (*f)(const struct sockaddr *, 486 socklen_t, char *, size_t, 487 char *, size_t, int); 488 } p_getnameinfo = {NULL}; 489 /* 2nd argument to getnameinfo is specified to 490 * be socklen_t. Unfortunately there is a number 491 * of environments where socklen_t is not defined. 492 * As it's passed by value, it's safe to pass it 493 * as size_t... <appro> */ 494 495 if (p_getnameinfo.p == NULL) { 496 if ((p_getnameinfo.p = DSO_global_lookup("getnameinfo")) == NULL) 497 p_getnameinfo.p = (void*) - 1; 498 } 499 if (p_getnameinfo.p == (void *) - 1) 500 break; 501 502 if ((*p_getnameinfo.f)(&sa.from.sa, sa.len, h, sizeof(h), 503 s, sizeof(s), NI_NUMERICHOST|NI_NUMERICSERV)) 504 break; 505 nl = strlen(h) + strlen(s) + 2; 506 p = *addr; 507 if (p) { 508 *p = '\0'; 509 if (!(tmp = realloc(p, nl))) { 510 close(ret); 511 ret = -1; 512 free(p); 513 *addr = NULL; 514 BIOerr(BIO_F_BIO_ACCEPT, ERR_R_MALLOC_FAILURE); 515 goto end; 516 } 517 p = tmp; 518 } else { 519 p = malloc(nl); 520 } 521 if (p == NULL) { 522 close(ret); 523 ret = -1; 524 BIOerr(BIO_F_BIO_ACCEPT, ERR_R_MALLOC_FAILURE); 525 goto end; 526 } 527 *addr = p; 528 snprintf(*addr, nl, "%s:%s", h, s); 529 goto end; 530 } while (0); 531 #endif 532 if (sa.from.sa.sa_family != AF_INET) 533 goto end; 534 l = ntohl(sa.from.sa_in.sin_addr.s_addr); 535 port = ntohs(sa.from.sa_in.sin_port); 536 if (*addr == NULL) { 537 if ((p = malloc(24)) == NULL) { 538 close(ret); 539 ret = -1; 540 BIOerr(BIO_F_BIO_ACCEPT, ERR_R_MALLOC_FAILURE); 541 goto end; 542 } 543 *addr = p; 544 } 545 snprintf(*addr, 24, "%d.%d.%d.%d:%d", 546 (unsigned char)(l >> 24L) & 0xff, (unsigned char)(l >> 16L) & 0xff, 547 (unsigned char)(l >> 8L) & 0xff, (unsigned char)(l) & 0xff, port); 548 549 end: 550 return (ret); 551 } 552 553 int 554 BIO_set_tcp_ndelay(int s, int on) 555 { 556 int ret = 0; 557 #if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP)) 558 int opt; 559 560 #ifdef SOL_TCP 561 opt = SOL_TCP; 562 #else 563 #ifdef IPPROTO_TCP 564 opt = IPPROTO_TCP; 565 #endif 566 #endif 567 568 ret = setsockopt(s, opt, TCP_NODELAY, (char *)&on, sizeof(on)); 569 #endif 570 return (ret == 0); 571 } 572 573 int 574 BIO_socket_nbio(int s, int mode) 575 { 576 int ret = -1; 577 int l; 578 579 l = mode; 580 ret = BIO_socket_ioctl(s, FIONBIO, &l); 581 582 return (ret == 0); 583 } 584