1 /* $OpenBSD: b_sock.c,v 1.41 2014/06/22 16:47:08 jsing Exp $ */ 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 #include <sys/socket.h> 61 62 #include <netinet/in.h> 63 64 #include <errno.h> 65 #include <netdb.h> 66 #include <stdio.h> 67 #include <stdlib.h> 68 #include <unistd.h> 69 70 #include <openssl/bio.h> 71 72 #include "cryptlib.h" 73 74 static int get_ip(const char *str, unsigned char *ip); 75 76 int 77 BIO_get_host_ip(const char *str, unsigned char *ip) 78 { 79 int i; 80 int err = 1; 81 int locked = 0; 82 struct hostent *he; 83 84 i = get_ip(str, ip); 85 if (i < 0) { 86 BIOerr(BIO_F_BIO_GET_HOST_IP, BIO_R_INVALID_IP_ADDRESS); 87 goto err; 88 } 89 90 /* If the string actually contained an IP address, we need not do 91 anything more */ 92 if (i > 0) 93 return (1); 94 95 /* do a gethostbyname */ 96 CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME); 97 locked = 1; 98 he = BIO_gethostbyname(str); 99 if (he == NULL) { 100 BIOerr(BIO_F_BIO_GET_HOST_IP, BIO_R_BAD_HOSTNAME_LOOKUP); 101 goto err; 102 } 103 104 /* cast to short because of win16 winsock definition */ 105 if ((short)he->h_addrtype != AF_INET) { 106 BIOerr(BIO_F_BIO_GET_HOST_IP, BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET); 107 goto err; 108 } 109 for (i = 0; i < 4; i++) 110 ip[i] = he->h_addr_list[0][i]; 111 err = 0; 112 113 err: 114 if (locked) 115 CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME); 116 if (err) { 117 ERR_asprintf_error_data("host=%s", str); 118 return 0; 119 } else 120 return 1; 121 } 122 123 int 124 BIO_get_port(const char *str, unsigned short *port_ptr) 125 { 126 struct servent *s; 127 long port; 128 char *ep; 129 130 if (str == NULL) { 131 BIOerr(BIO_F_BIO_GET_PORT, BIO_R_NO_PORT_SPECIFIED); 132 return (0); 133 } 134 135 errno = 0; 136 port = strtol(str, &ep, 10); 137 if (str[0] != '\0' && *ep == '\0') { 138 if (errno == ERANGE && (port == LONG_MAX || port == LONG_MIN)) { 139 BIOerr(BIO_F_BIO_GET_PORT, BIO_R_INVALID_PORT_NUMBER); 140 return (0); 141 } 142 if (port < 0 || port > 65535) { 143 BIOerr(BIO_F_BIO_GET_PORT, BIO_R_INVALID_PORT_NUMBER); 144 return (0); 145 } 146 147 *port_ptr = (unsigned short)port; 148 return (1); 149 } 150 151 CRYPTO_w_lock(CRYPTO_LOCK_GETSERVBYNAME); 152 s = getservbyname(str, "tcp"); 153 if (s != NULL) 154 *port_ptr = ntohs((unsigned short)s->s_port); 155 CRYPTO_w_unlock(CRYPTO_LOCK_GETSERVBYNAME); 156 157 if (s == NULL) { 158 if (strcmp(str, "http") == 0) 159 *port_ptr = 80; 160 else if (strcmp(str, "telnet") == 0) 161 *port_ptr = 23; 162 else if (strcmp(str, "socks") == 0) 163 *port_ptr = 1080; 164 else if (strcmp(str, "https") == 0) 165 *port_ptr = 443; 166 else if (strcmp(str, "ssl") == 0) 167 *port_ptr = 443; 168 else if (strcmp(str, "ftp") == 0) 169 *port_ptr = 21; 170 else if (strcmp(str, "gopher") == 0) 171 *port_ptr = 70; 172 else { 173 SYSerr(SYS_F_GETSERVBYNAME, errno); 174 ERR_asprintf_error_data("service='%s'", str); 175 return (0); 176 } 177 } 178 return (1); 179 } 180 181 int 182 BIO_sock_error(int sock) 183 { 184 int j, i; 185 int size; 186 187 size = sizeof(int); 188 /* Note: under Windows the third parameter is of type (char *) 189 * whereas under other systems it is (void *) if you don't have 190 * a cast it will choke the compiler: if you do have a cast then 191 * you can either go for (char *) or (void *). 192 */ 193 i = getsockopt(sock, SOL_SOCKET, SO_ERROR, (void *)&j, (void *)&size); 194 if (i < 0) 195 return (1); 196 else 197 return (j); 198 } 199 200 struct hostent * 201 BIO_gethostbyname(const char *name) 202 { 203 return gethostbyname(name); 204 } 205 206 int 207 BIO_sock_init(void) 208 { 209 return (1); 210 } 211 212 void 213 BIO_sock_cleanup(void) 214 { 215 } 216 217 int 218 BIO_socket_ioctl(int fd, long type, void *arg) 219 { 220 int i; 221 222 # define ARG arg 223 224 i = ioctl(fd, type, ARG); 225 if (i < 0) 226 SYSerr(SYS_F_IOCTLSOCKET, errno); 227 return (i); 228 } 229 230 /* The reason I have implemented this instead of using sscanf is because 231 * Visual C 1.52c gives an unresolved external when linking a DLL :-( */ 232 static int 233 get_ip(const char *str, unsigned char ip[4]) 234 { 235 unsigned int tmp[4]; 236 int num = 0, c, ok = 0; 237 238 tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0; 239 240 for (;;) { 241 c= *(str++); 242 if ((c >= '0') && (c <= '9')) { 243 ok = 1; 244 tmp[num] = tmp[num]*10 + c-'0'; 245 if (tmp[num] > 255) 246 return (0); 247 } else if (c == '.') { 248 if (!ok) 249 return (-1); 250 if (num == 3) 251 return (0); 252 num++; 253 ok = 0; 254 } else if (c == '\0' && (num == 3) && ok) 255 break; 256 else 257 return (0); 258 } 259 ip[0] = tmp[0]; 260 ip[1] = tmp[1]; 261 ip[2] = tmp[2]; 262 ip[3] = tmp[3]; 263 return (1); 264 } 265 266 int 267 BIO_get_accept_socket(char *host, int bind_mode) 268 { 269 int ret = 0; 270 union { 271 struct sockaddr sa; 272 struct sockaddr_in sa_in; 273 struct sockaddr_in6 sa_in6; 274 } server, client; 275 int s = -1, cs, addrlen; 276 unsigned char ip[4]; 277 unsigned short port; 278 char *str = NULL, *e; 279 char *h, *p; 280 unsigned long l; 281 int err_num; 282 283 if ((str = BUF_strdup(host)) == NULL) 284 return (-1); 285 286 h = p = NULL; 287 h = str; 288 for (e = str; *e; e++) { 289 if (*e == ':') { 290 p = e; 291 } else if (*e == '/') { 292 *e = '\0'; 293 break; 294 } 295 } 296 if (p) 297 *p++='\0'; /* points at last ':', '::port' is special [see below] */ 298 else 299 p = h, h = NULL; 300 301 #ifdef EAI_FAMILY 302 do { 303 struct addrinfo *res, hint; 304 305 /* '::port' enforces IPv6 wildcard listener. Some OSes, 306 * e.g. Solaris, default to IPv6 without any hint. Also 307 * note that commonly IPv6 wildchard socket can service 308 * IPv4 connections just as well... */ 309 memset(&hint, 0, sizeof(hint)); 310 hint.ai_flags = AI_PASSIVE; 311 if (h) { 312 if (strchr(h, ':')) { 313 if (h[1] == '\0') 314 h = NULL; 315 hint.ai_family = AF_INET6; 316 } else if (h[0] == '*' && h[1] == '\0') { 317 hint.ai_family = AF_INET; 318 h = NULL; 319 } 320 } 321 322 if (getaddrinfo(h, p, &hint, &res)) 323 break; 324 325 addrlen = res->ai_addrlen <= sizeof(server) ? 326 res->ai_addrlen : sizeof(server); 327 memcpy(&server, res->ai_addr, addrlen); 328 329 freeaddrinfo(res); 330 goto again; 331 } while (0); 332 #endif 333 334 if (!BIO_get_port(p, &port)) 335 goto err; 336 337 memset((char *)&server, 0, sizeof(server)); 338 server.sa_in.sin_family = AF_INET; 339 server.sa_in.sin_port = htons(port); 340 addrlen = sizeof(server.sa_in); 341 342 if (h == NULL || strcmp(h, "*") == 0) 343 server.sa_in.sin_addr.s_addr = INADDR_ANY; 344 else { 345 if (!BIO_get_host_ip(h, &(ip[0]))) 346 goto err; 347 l = (unsigned long)((unsigned long)ip[0]<<24L)| 348 ((unsigned long)ip[1]<<16L)| 349 ((unsigned long)ip[2]<< 8L)| 350 ((unsigned long)ip[3]); 351 server.sa_in.sin_addr.s_addr = htonl(l); 352 } 353 354 again: 355 s = socket(server.sa.sa_family, SOCK_STREAM, IPPROTO_TCP); 356 if (s == -1) { 357 SYSerr(SYS_F_SOCKET, errno); 358 ERR_asprintf_error_data("port='%s'", host); 359 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET, BIO_R_UNABLE_TO_CREATE_SOCKET); 360 goto err; 361 } 362 363 #ifdef SO_REUSEADDR 364 if (bind_mode == BIO_BIND_REUSEADDR) { 365 int i = 1; 366 367 ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&i, sizeof(i)); 368 bind_mode = BIO_BIND_NORMAL; 369 } 370 #endif 371 if (bind(s, &server.sa, addrlen) == -1) { 372 #ifdef SO_REUSEADDR 373 err_num = errno; 374 if ((bind_mode == BIO_BIND_REUSEADDR_IF_UNUSED) && 375 (err_num == EADDRINUSE)) { 376 client = server; 377 if (h == NULL || strcmp(h, "*") == 0) { 378 if (client.sa.sa_family == AF_INET6) { 379 memset(&client.sa_in6.sin6_addr, 0, sizeof(client.sa_in6.sin6_addr)); 380 client.sa_in6.sin6_addr.s6_addr[15] = 1; 381 } else if (client.sa.sa_family == AF_INET) { 382 client.sa_in.sin_addr.s_addr = htonl(0x7F000001); 383 } else 384 goto err; 385 } 386 cs = socket(client.sa.sa_family, SOCK_STREAM, IPPROTO_TCP); 387 if (cs != -1) { 388 int ii; 389 ii = connect(cs, &client.sa, addrlen); 390 close(cs); 391 if (ii == -1) { 392 bind_mode = BIO_BIND_REUSEADDR; 393 close(s); 394 goto again; 395 } 396 /* else error */ 397 } 398 /* else error */ 399 } 400 #endif 401 SYSerr(SYS_F_BIND, err_num); 402 ERR_asprintf_error_data("port='%s'", host); 403 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET, BIO_R_UNABLE_TO_BIND_SOCKET); 404 goto err; 405 } 406 if (listen(s, SOMAXCONN) == -1) { 407 SYSerr(SYS_F_BIND, errno); 408 ERR_asprintf_error_data("port='%s'", host); 409 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET, BIO_R_UNABLE_TO_LISTEN_SOCKET); 410 goto err; 411 } 412 ret = 1; 413 err: 414 free(str); 415 if ((ret == 0) && (s != -1)) { 416 close(s); 417 s = -1; 418 } 419 return (s); 420 } 421 422 int 423 BIO_accept(int sock, char **addr) 424 { 425 int ret = -1; 426 unsigned long l; 427 unsigned short port; 428 char *p, *tmp; 429 430 struct { 431 socklen_t len; 432 union { 433 struct sockaddr sa; 434 struct sockaddr_in sa_in; 435 struct sockaddr_in6 sa_in6; 436 } from; 437 } sa; 438 439 sa.len = sizeof(sa.from); 440 memset(&sa.from, 0, sizeof(sa.from)); 441 ret = accept(sock, &sa.from.sa, &sa.len); 442 if (ret == -1) { 443 if (BIO_sock_should_retry(ret)) 444 return -2; 445 SYSerr(SYS_F_ACCEPT, errno); 446 BIOerr(BIO_F_BIO_ACCEPT, BIO_R_ACCEPT_ERROR); 447 goto end; 448 } 449 450 if (addr == NULL) 451 goto end; 452 453 #ifdef EAI_FAMILY 454 do { 455 char h[NI_MAXHOST], s[NI_MAXSERV]; 456 size_t nl; 457 458 if (getnameinfo(&sa.from.sa, sa.len, h, sizeof(h), 459 s, sizeof(s), NI_NUMERICHOST|NI_NUMERICSERV)) 460 break; 461 nl = strlen(h) + strlen(s) + 2; 462 p = *addr; 463 if (p) { 464 *p = '\0'; 465 if (!(tmp = realloc(p, nl))) { 466 close(ret); 467 ret = -1; 468 free(p); 469 *addr = NULL; 470 BIOerr(BIO_F_BIO_ACCEPT, ERR_R_MALLOC_FAILURE); 471 goto end; 472 } 473 p = tmp; 474 } else { 475 p = malloc(nl); 476 } 477 if (p == NULL) { 478 close(ret); 479 ret = -1; 480 BIOerr(BIO_F_BIO_ACCEPT, ERR_R_MALLOC_FAILURE); 481 goto end; 482 } 483 *addr = p; 484 snprintf(*addr, nl, "%s:%s", h, s); 485 goto end; 486 } while (0); 487 #endif 488 if (sa.from.sa.sa_family != AF_INET) 489 goto end; 490 l = ntohl(sa.from.sa_in.sin_addr.s_addr); 491 port = ntohs(sa.from.sa_in.sin_port); 492 if (*addr == NULL) { 493 if ((p = malloc(24)) == NULL) { 494 close(ret); 495 ret = -1; 496 BIOerr(BIO_F_BIO_ACCEPT, ERR_R_MALLOC_FAILURE); 497 goto end; 498 } 499 *addr = p; 500 } 501 snprintf(*addr, 24, "%d.%d.%d.%d:%d", 502 (unsigned char)(l >> 24L) & 0xff, (unsigned char)(l >> 16L) & 0xff, 503 (unsigned char)(l >> 8L) & 0xff, (unsigned char)(l) & 0xff, port); 504 505 end: 506 return (ret); 507 } 508 509 int 510 BIO_set_tcp_ndelay(int s, int on) 511 { 512 int ret = 0; 513 #if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP)) 514 int opt; 515 516 #ifdef SOL_TCP 517 opt = SOL_TCP; 518 #else 519 #ifdef IPPROTO_TCP 520 opt = IPPROTO_TCP; 521 #endif 522 #endif 523 524 ret = setsockopt(s, opt, TCP_NODELAY, (char *)&on, sizeof(on)); 525 #endif 526 return (ret == 0); 527 } 528 529 int 530 BIO_socket_nbio(int s, int mode) 531 { 532 int ret = -1; 533 int l; 534 535 l = mode; 536 ret = BIO_socket_ioctl(s, FIONBIO, &l); 537 538 return (ret == 0); 539 } 540