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