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