1 /* $OpenBSD: b_sock.c,v 1.66 2017/04/30 05:43:05 beck 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 <limits.h> 69 #include <netdb.h> 70 #include <stdio.h> 71 #include <stdlib.h> 72 #include <unistd.h> 73 74 #include <openssl/bio.h> 75 #include <openssl/buffer.h> 76 #include <openssl/err.h> 77 78 int 79 BIO_get_host_ip(const char *str, unsigned char *ip) 80 { 81 struct addrinfo *res = NULL; 82 struct addrinfo hints = { 83 .ai_family = AF_INET, 84 .ai_socktype = SOCK_STREAM, 85 .ai_flags = AI_PASSIVE, 86 }; 87 uint32_t *iap = (in_addr_t *)ip; 88 int error; 89 90 if (str == NULL) { 91 ERR_asprintf_error_data("NULL host provided"); 92 return (0); 93 } 94 95 if ((error = getaddrinfo(str, NULL, &hints, &res)) != 0) { 96 BIOerror(BIO_R_BAD_HOSTNAME_LOOKUP); 97 ERR_asprintf_error_data("getaddrinfo: host='%s' : %s'", str, 98 gai_strerror(error)); 99 return (0); 100 } 101 *iap = (uint32_t)(((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr); 102 freeaddrinfo(res); 103 return (1); 104 } 105 106 int 107 BIO_get_port(const char *str, unsigned short *port_ptr) 108 { 109 struct addrinfo *res = NULL; 110 struct addrinfo hints = { 111 .ai_family = AF_UNSPEC, 112 .ai_socktype = SOCK_STREAM, 113 .ai_flags = AI_PASSIVE, 114 }; 115 int error; 116 117 if (str == NULL) { 118 BIOerror(BIO_R_NO_PORT_SPECIFIED); 119 return (0); 120 } 121 122 if ((error = getaddrinfo(NULL, str, &hints, &res)) != 0) { 123 ERR_asprintf_error_data("getaddrinfo: service='%s' : %s'", str, 124 gai_strerror(error)); 125 return (0); 126 } 127 *port_ptr = ntohs(((struct sockaddr_in *)(res->ai_addr))->sin_port); 128 freeaddrinfo(res); 129 return (1); 130 } 131 132 int 133 BIO_sock_error(int sock) 134 { 135 socklen_t len; 136 int err; 137 138 len = sizeof(err); 139 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &len) != 0) 140 return (1); 141 return (err); 142 } 143 144 struct hostent * 145 BIO_gethostbyname(const char *name) 146 { 147 return gethostbyname(name); 148 } 149 150 int 151 BIO_socket_ioctl(int fd, long type, void *arg) 152 { 153 int ret; 154 155 ret = ioctl(fd, type, arg); 156 if (ret < 0) 157 SYSerror(errno); 158 return (ret); 159 } 160 161 int 162 BIO_get_accept_socket(char *host, int bind_mode) 163 { 164 struct addrinfo hints = { 165 .ai_family = AF_INET, 166 .ai_socktype = SOCK_STREAM, 167 .ai_flags = AI_PASSIVE, 168 }; 169 struct addrinfo *res = NULL; 170 char *h, *p, *str = NULL; 171 int error, ret = 0, s = -1; 172 173 if (host == NULL || (str = strdup(host)) == NULL) 174 return (-1); 175 p = NULL; 176 h = str; 177 if ((p = strrchr(str, ':')) == NULL) { 178 BIOerror(BIO_R_NO_PORT_SPECIFIED); 179 goto err; 180 } 181 *p++ = '\0'; 182 if (*p == '\0') { 183 BIOerror(BIO_R_NO_PORT_SPECIFIED); 184 goto err; 185 } 186 if (*h == '\0' || strcmp(h, "*") == 0) 187 h = NULL; 188 189 if ((error = getaddrinfo(h, p, &hints, &res)) != 0) { 190 ERR_asprintf_error_data("getaddrinfo: '%s:%s': %s'", h, p, 191 gai_strerror(error)); 192 goto err; 193 } 194 if (h == NULL) { 195 struct sockaddr_in *sin = (struct sockaddr_in *)res->ai_addr; 196 sin->sin_addr.s_addr = INADDR_ANY; 197 } 198 199 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 200 if (s == -1) { 201 SYSerror(errno); 202 ERR_asprintf_error_data("host='%s'", host); 203 BIOerror(BIO_R_UNABLE_TO_CREATE_SOCKET); 204 goto err; 205 } 206 if (bind_mode == BIO_BIND_REUSEADDR) { 207 int i = 1; 208 209 ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)); 210 bind_mode = BIO_BIND_NORMAL; 211 } 212 if (bind(s, res->ai_addr, res->ai_addrlen) == -1) { 213 SYSerror(errno); 214 ERR_asprintf_error_data("host='%s'", host); 215 BIOerror(BIO_R_UNABLE_TO_BIND_SOCKET); 216 goto err; 217 } 218 if (listen(s, SOMAXCONN) == -1) { 219 SYSerror(errno); 220 ERR_asprintf_error_data("host='%s'", host); 221 BIOerror(BIO_R_UNABLE_TO_LISTEN_SOCKET); 222 goto err; 223 } 224 ret = 1; 225 226 err: 227 free(str); 228 freeaddrinfo(res); 229 if ((ret == 0) && (s != -1)) { 230 close(s); 231 s = -1; 232 } 233 return (s); 234 } 235 236 int 237 BIO_accept(int sock, char **addr) 238 { 239 char h[NI_MAXHOST], s[NI_MAXSERV]; 240 struct sockaddr_in sin; 241 socklen_t sin_len = sizeof(sin); 242 int ret = -1; 243 244 if (addr == NULL) 245 goto end; 246 247 ret = accept(sock, (struct sockaddr *)&sin, &sin_len); 248 if (ret == -1) { 249 if (BIO_sock_should_retry(ret)) 250 return -2; 251 SYSerror(errno); 252 BIOerror(BIO_R_ACCEPT_ERROR); 253 goto end; 254 } 255 /* XXX Crazy API. Can't be helped */ 256 if (*addr != NULL) { 257 free(*addr); 258 *addr = NULL; 259 } 260 261 if (sin.sin_family != AF_INET) 262 goto end; 263 264 if (getnameinfo((struct sockaddr *)&sin, sin_len, h, sizeof(h), 265 s, sizeof(s), NI_NUMERICHOST|NI_NUMERICSERV) != 0) 266 goto end; 267 268 if ((asprintf(addr, "%s:%s", h, s)) == -1) { 269 BIOerror(ERR_R_MALLOC_FAILURE); 270 *addr = NULL; 271 goto end; 272 } 273 end: 274 return (ret); 275 } 276 277 int 278 BIO_set_tcp_ndelay(int s, int on) 279 { 280 return (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) == 0); 281 } 282