1 /* $OpenBSD: s_socket.c,v 1.14 2025/01/02 13:10:03 tb 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/socket.h> 60 61 #include <netinet/in.h> 62 63 #include <errno.h> 64 #include <netdb.h> 65 #include <stdio.h> 66 #include <stdlib.h> 67 #include <string.h> 68 #include <unistd.h> 69 70 #include "apps.h" 71 72 #include <openssl/ssl.h> 73 74 static int init_server(int *sock, int port, int type); 75 static int init_server_long(int *sock, int port, char *ip, int type); 76 static int do_accept(int acc_sock, int *sock); 77 78 int 79 init_client(int *sock, char *host, char *port, int type, int af) 80 { 81 struct addrinfo hints, *ai_top, *ai; 82 int i, s = -1; 83 84 memset(&hints, '\0', sizeof(hints)); 85 hints.ai_family = af; 86 hints.ai_socktype = type; 87 88 if ((i = getaddrinfo(host, port, &hints, &ai_top)) != 0) { 89 BIO_printf(bio_err, "getaddrinfo: %s\n", gai_strerror(i)); 90 return (0); 91 } 92 if (ai_top == NULL || ai_top->ai_addr == NULL) { 93 BIO_printf(bio_err, "getaddrinfo returned no addresses\n"); 94 if (ai_top != NULL) { 95 freeaddrinfo(ai_top); 96 } 97 return (0); 98 } 99 for (ai = ai_top; ai != NULL; ai = ai->ai_next) { 100 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 101 if (s == -1) { 102 continue; 103 } 104 if (type == SOCK_STREAM) { 105 i = 0; 106 i = setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, 107 (char *) &i, sizeof(i)); 108 if (i == -1) { 109 perror("keepalive"); 110 goto out; 111 } 112 } 113 if ((i = connect(s, ai->ai_addr, ai->ai_addrlen)) == 0) { 114 *sock = s; 115 freeaddrinfo(ai_top); 116 return (1); 117 } 118 close(s); 119 s = -1; 120 } 121 122 perror("connect"); 123 out: 124 if (s != -1) 125 close(s); 126 freeaddrinfo(ai_top); 127 return (0); 128 } 129 130 int 131 do_server(int port, int type, int *ret, 132 int (*cb)(int s, unsigned char *context), 133 unsigned char *context, int naccept) 134 { 135 int sock; 136 int accept_socket = 0; 137 int i; 138 139 if (!init_server(&accept_socket, port, type)) 140 return (0); 141 142 if (ret != NULL) { 143 *ret = accept_socket; 144 /* return(1); */ 145 } 146 for (;;) { 147 if (type == SOCK_STREAM) { 148 if (do_accept(accept_socket, &sock) == 0) { 149 shutdown(accept_socket, SHUT_RD); 150 close(accept_socket); 151 return (0); 152 } 153 } else 154 sock = accept_socket; 155 i = cb(sock, context); 156 if (type == SOCK_STREAM) { 157 shutdown(sock, SHUT_RDWR); 158 close(sock); 159 } 160 if (naccept != -1) 161 naccept--; 162 if (i < 0 || naccept == 0) { 163 shutdown(accept_socket, SHUT_RDWR); 164 close(accept_socket); 165 return (i); 166 } 167 } 168 } 169 170 static int 171 init_server_long(int *sock, int port, char *ip, int type) 172 { 173 int ret = 0; 174 struct sockaddr_in server; 175 int s = -1; 176 177 memset((char *) &server, 0, sizeof(server)); 178 server.sin_family = AF_INET; 179 server.sin_port = htons((unsigned short) port); 180 if (ip == NULL) 181 server.sin_addr.s_addr = INADDR_ANY; 182 else 183 memcpy(&server.sin_addr.s_addr, ip, 4); 184 185 if (type == SOCK_STREAM) 186 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 187 else /* type == SOCK_DGRAM */ 188 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 189 190 if (s == -1) 191 goto err; 192 #if defined SOL_SOCKET && defined SO_REUSEADDR 193 { 194 int j = 1; 195 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 196 (void *) &j, sizeof j) == -1) { 197 perror("setsockopt"); 198 goto err; 199 } 200 } 201 #endif 202 if (bind(s, (struct sockaddr *) & server, sizeof(server)) == -1) { 203 perror("bind"); 204 goto err; 205 } 206 /* Make it 128 for linux */ 207 if (type == SOCK_STREAM && listen(s, 128) == -1) 208 goto err; 209 *sock = s; 210 ret = 1; 211 err: 212 if ((ret == 0) && (s != -1)) { 213 shutdown(s, SHUT_RD); 214 close(s); 215 } 216 return (ret); 217 } 218 219 static int 220 init_server(int *sock, int port, int type) 221 { 222 return (init_server_long(sock, port, NULL, type)); 223 } 224 225 static int 226 do_accept(int acc_sock, int *sock) 227 { 228 struct hostent *h1, *h2; 229 static struct sockaddr_in from; 230 socklen_t len; 231 char *host = NULL; 232 int ret; 233 234 redoit: 235 236 memset((char *) &from, 0, sizeof(from)); 237 len = sizeof(from); 238 ret = accept(acc_sock, (struct sockaddr *) & from, &len); 239 if (ret == -1) { 240 if (errno == EINTR) { 241 /* check_timeout(); */ 242 goto redoit; 243 } 244 fprintf(stderr, "errno=%d ", errno); 245 perror("accept"); 246 return (0); 247 } 248 249 h1 = gethostbyaddr((char *) &from.sin_addr.s_addr, 250 sizeof(from.sin_addr.s_addr), AF_INET); 251 if (h1 == NULL) { 252 BIO_printf(bio_err, "bad gethostbyaddr\n"); 253 } else { 254 if ((host = strdup(h1->h_name)) == NULL) { 255 perror("strdup"); 256 close(ret); 257 return (0); 258 } 259 260 h2 = gethostbyname(host); 261 if (h2 == NULL) { 262 BIO_printf(bio_err, "gethostbyname failure\n"); 263 close(ret); 264 free(host); 265 return (0); 266 } 267 if (h2->h_addrtype != AF_INET) { 268 BIO_printf(bio_err, "gethostbyname addr is not AF_INET\n"); 269 close(ret); 270 free(host); 271 return (0); 272 } 273 } 274 275 free(host); 276 *sock = ret; 277 return (1); 278 } 279 280 int 281 extract_host_port(char *str, char **host_ptr, unsigned char *ip, 282 char **port_ptr) 283 { 284 char *h, *p; 285 286 h = str; 287 p = strrchr(str, '/'); /* IPv6 host/port */ 288 if (p == NULL) { 289 p = strrchr(str, ':'); 290 } 291 if (p == NULL) { 292 BIO_printf(bio_err, "no port defined\n"); 293 return (0); 294 } 295 *(p++) = '\0'; 296 297 if (host_ptr != NULL) 298 *host_ptr = h; 299 300 if (port_ptr != NULL && p != NULL && *p != '\0') 301 *port_ptr = p; 302 303 return (1); 304 } 305 306 int 307 extract_port(char *str, short *port_ptr) 308 { 309 int i; 310 const char *errstr; 311 struct servent *s; 312 313 i = strtonum(str, 1, 65535, &errstr); 314 if (!errstr) { 315 *port_ptr = (unsigned short) i; 316 } else { 317 s = getservbyname(str, "tcp"); 318 if (s == NULL) { 319 BIO_printf(bio_err, "getservbyname failure for %s\n", str); 320 return (0); 321 } 322 *port_ptr = ntohs((unsigned short) s->s_port); 323 } 324 return (1); 325 } 326