xref: /openbsd-src/usr.bin/openssl/s_socket.c (revision 23cc9d400791825fb9615b5c61d55ff2adb3056b)
1*23cc9d40Stb /* $OpenBSD: s_socket.c,v 1.14 2025/01/02 13:10:03 tb Exp $ */
2dab3f910Sjsing /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3dab3f910Sjsing  * All rights reserved.
4dab3f910Sjsing  *
5dab3f910Sjsing  * This package is an SSL implementation written
6dab3f910Sjsing  * by Eric Young (eay@cryptsoft.com).
7dab3f910Sjsing  * The implementation was written so as to conform with Netscapes SSL.
8dab3f910Sjsing  *
9dab3f910Sjsing  * This library is free for commercial and non-commercial use as long as
10dab3f910Sjsing  * the following conditions are aheared to.  The following conditions
11dab3f910Sjsing  * apply to all code found in this distribution, be it the RC4, RSA,
12dab3f910Sjsing  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13dab3f910Sjsing  * included with this distribution is covered by the same copyright terms
14dab3f910Sjsing  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15dab3f910Sjsing  *
16dab3f910Sjsing  * Copyright remains Eric Young's, and as such any Copyright notices in
17dab3f910Sjsing  * the code are not to be removed.
18dab3f910Sjsing  * If this package is used in a product, Eric Young should be given attribution
19dab3f910Sjsing  * as the author of the parts of the library used.
20dab3f910Sjsing  * This can be in the form of a textual message at program startup or
21dab3f910Sjsing  * in documentation (online or textual) provided with the package.
22dab3f910Sjsing  *
23dab3f910Sjsing  * Redistribution and use in source and binary forms, with or without
24dab3f910Sjsing  * modification, are permitted provided that the following conditions
25dab3f910Sjsing  * are met:
26dab3f910Sjsing  * 1. Redistributions of source code must retain the copyright
27dab3f910Sjsing  *    notice, this list of conditions and the following disclaimer.
28dab3f910Sjsing  * 2. Redistributions in binary form must reproduce the above copyright
29dab3f910Sjsing  *    notice, this list of conditions and the following disclaimer in the
30dab3f910Sjsing  *    documentation and/or other materials provided with the distribution.
31dab3f910Sjsing  * 3. All advertising materials mentioning features or use of this software
32dab3f910Sjsing  *    must display the following acknowledgement:
33dab3f910Sjsing  *    "This product includes cryptographic software written by
34dab3f910Sjsing  *     Eric Young (eay@cryptsoft.com)"
35dab3f910Sjsing  *    The word 'cryptographic' can be left out if the rouines from the library
36dab3f910Sjsing  *    being used are not cryptographic related :-).
37dab3f910Sjsing  * 4. If you include any Windows specific code (or a derivative thereof) from
38dab3f910Sjsing  *    the apps directory (application code) you must include an acknowledgement:
39dab3f910Sjsing  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40dab3f910Sjsing  *
41dab3f910Sjsing  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42dab3f910Sjsing  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43dab3f910Sjsing  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44dab3f910Sjsing  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45dab3f910Sjsing  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46dab3f910Sjsing  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47dab3f910Sjsing  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48dab3f910Sjsing  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49dab3f910Sjsing  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50dab3f910Sjsing  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51dab3f910Sjsing  * SUCH DAMAGE.
52dab3f910Sjsing  *
53dab3f910Sjsing  * The licence and distribution terms for any publically available version or
54dab3f910Sjsing  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55dab3f910Sjsing  * copied and put under another distribution licence
56dab3f910Sjsing  * [including the GNU Public Licence.]
57dab3f910Sjsing  */
58dab3f910Sjsing 
59dab3f910Sjsing #include <sys/socket.h>
60dab3f910Sjsing 
61dab3f910Sjsing #include <netinet/in.h>
62dab3f910Sjsing 
63dab3f910Sjsing #include <errno.h>
64dab3f910Sjsing #include <netdb.h>
65dab3f910Sjsing #include <stdio.h>
66dab3f910Sjsing #include <stdlib.h>
67dab3f910Sjsing #include <string.h>
68dab3f910Sjsing #include <unistd.h>
69dab3f910Sjsing 
70dab3f910Sjsing #include "apps.h"
71dab3f910Sjsing 
72dab3f910Sjsing #include <openssl/ssl.h>
73dab3f910Sjsing 
74dab3f910Sjsing static int init_server(int *sock, int port, int type);
75dab3f910Sjsing static int init_server_long(int *sock, int port, char *ip, int type);
767537a7c2Stb static int do_accept(int acc_sock, int *sock);
77dab3f910Sjsing 
78dab3f910Sjsing int
79dab3f910Sjsing init_client(int *sock, char *host, char *port, int type, int af)
80dab3f910Sjsing {
81dab3f910Sjsing 	struct addrinfo hints, *ai_top, *ai;
820a17e261Sdoug 	int i, s = -1;
83dab3f910Sjsing 
84dab3f910Sjsing 	memset(&hints, '\0', sizeof(hints));
85dab3f910Sjsing 	hints.ai_family = af;
86dab3f910Sjsing 	hints.ai_socktype = type;
87dab3f910Sjsing 
88dab3f910Sjsing 	if ((i = getaddrinfo(host, port, &hints, &ai_top)) != 0) {
89dab3f910Sjsing 		BIO_printf(bio_err, "getaddrinfo: %s\n", gai_strerror(i));
90dab3f910Sjsing 		return (0);
91dab3f910Sjsing 	}
92dab3f910Sjsing 	if (ai_top == NULL || ai_top->ai_addr == NULL) {
93dab3f910Sjsing 		BIO_printf(bio_err, "getaddrinfo returned no addresses\n");
94dab3f910Sjsing 		if (ai_top != NULL) {
95dab3f910Sjsing 			freeaddrinfo(ai_top);
96dab3f910Sjsing 		}
97dab3f910Sjsing 		return (0);
98dab3f910Sjsing 	}
99dab3f910Sjsing 	for (ai = ai_top; ai != NULL; ai = ai->ai_next) {
100dab3f910Sjsing 		s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
101dab3f910Sjsing 		if (s == -1) {
102dab3f910Sjsing 			continue;
103dab3f910Sjsing 		}
104dab3f910Sjsing 		if (type == SOCK_STREAM) {
105dab3f910Sjsing 			i = 0;
106dab3f910Sjsing 			i = setsockopt(s, SOL_SOCKET, SO_KEEPALIVE,
107dab3f910Sjsing 			    (char *) &i, sizeof(i));
1083aaa63ebSderaadt 			if (i == -1) {
109dab3f910Sjsing 				perror("keepalive");
1109e588cd4Sbeck 				goto out;
111dab3f910Sjsing 			}
112dab3f910Sjsing 		}
113dab3f910Sjsing 		if ((i = connect(s, ai->ai_addr, ai->ai_addrlen)) == 0) {
114dab3f910Sjsing 			*sock = s;
115dab3f910Sjsing 			freeaddrinfo(ai_top);
116dab3f910Sjsing 			return (1);
117dab3f910Sjsing 		}
118dab3f910Sjsing 		close(s);
1190a17e261Sdoug 		s = -1;
120dab3f910Sjsing 	}
121dab3f910Sjsing 
122dab3f910Sjsing 	perror("connect");
1239e588cd4Sbeck  out:
1240a17e261Sdoug 	if (s != -1)
125dab3f910Sjsing 		close(s);
126dab3f910Sjsing 	freeaddrinfo(ai_top);
127dab3f910Sjsing 	return (0);
128dab3f910Sjsing }
129dab3f910Sjsing 
130dab3f910Sjsing int
131dab3f910Sjsing do_server(int port, int type, int *ret,
1327537a7c2Stb     int (*cb)(int s, unsigned char *context),
13313017aedStb     unsigned char *context, int naccept)
134dab3f910Sjsing {
135dab3f910Sjsing 	int sock;
136dab3f910Sjsing 	int accept_socket = 0;
137dab3f910Sjsing 	int i;
138dab3f910Sjsing 
139dab3f910Sjsing 	if (!init_server(&accept_socket, port, type))
140dab3f910Sjsing 		return (0);
141dab3f910Sjsing 
142dab3f910Sjsing 	if (ret != NULL) {
143dab3f910Sjsing 		*ret = accept_socket;
144dab3f910Sjsing 		/* return(1); */
145dab3f910Sjsing 	}
146dab3f910Sjsing 	for (;;) {
147dab3f910Sjsing 		if (type == SOCK_STREAM) {
1487537a7c2Stb 			if (do_accept(accept_socket, &sock) == 0) {
149dab3f910Sjsing 				shutdown(accept_socket, SHUT_RD);
150dab3f910Sjsing 				close(accept_socket);
151dab3f910Sjsing 				return (0);
152dab3f910Sjsing 			}
153dab3f910Sjsing 		} else
154dab3f910Sjsing 			sock = accept_socket;
1557537a7c2Stb 		i = cb(sock, context);
156dab3f910Sjsing 		if (type == SOCK_STREAM) {
157dab3f910Sjsing 			shutdown(sock, SHUT_RDWR);
158dab3f910Sjsing 			close(sock);
159dab3f910Sjsing 		}
16013017aedStb 		if (naccept != -1)
16113017aedStb 			naccept--;
16213017aedStb 		if (i < 0 || naccept == 0) {
163dab3f910Sjsing 			shutdown(accept_socket, SHUT_RDWR);
164dab3f910Sjsing 			close(accept_socket);
165dab3f910Sjsing 			return (i);
166dab3f910Sjsing 		}
167dab3f910Sjsing 	}
168dab3f910Sjsing }
169dab3f910Sjsing 
170dab3f910Sjsing static int
171dab3f910Sjsing init_server_long(int *sock, int port, char *ip, int type)
172dab3f910Sjsing {
173dab3f910Sjsing 	int ret = 0;
174dab3f910Sjsing 	struct sockaddr_in server;
175dab3f910Sjsing 	int s = -1;
176dab3f910Sjsing 
177dab3f910Sjsing 	memset((char *) &server, 0, sizeof(server));
178dab3f910Sjsing 	server.sin_family = AF_INET;
179dab3f910Sjsing 	server.sin_port = htons((unsigned short) port);
180dab3f910Sjsing 	if (ip == NULL)
181dab3f910Sjsing 		server.sin_addr.s_addr = INADDR_ANY;
182dab3f910Sjsing 	else
183dab3f910Sjsing 		memcpy(&server.sin_addr.s_addr, ip, 4);
184dab3f910Sjsing 
185dab3f910Sjsing 	if (type == SOCK_STREAM)
1865a55ebe2Slteo 		s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
187dab3f910Sjsing 	else			/* type == SOCK_DGRAM */
188dab3f910Sjsing 		s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
189dab3f910Sjsing 
190dab3f910Sjsing 	if (s == -1)
191dab3f910Sjsing 		goto err;
192dab3f910Sjsing #if defined SOL_SOCKET && defined SO_REUSEADDR
193dab3f910Sjsing 	{
194dab3f910Sjsing 		int j = 1;
1957450d28aSdoug 		if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
1967450d28aSdoug 		    (void *) &j, sizeof j) == -1) {
1977450d28aSdoug 			perror("setsockopt");
1987450d28aSdoug 			goto err;
1997450d28aSdoug 		}
200dab3f910Sjsing 	}
201dab3f910Sjsing #endif
202dab3f910Sjsing 	if (bind(s, (struct sockaddr *) & server, sizeof(server)) == -1) {
203dab3f910Sjsing 		perror("bind");
204dab3f910Sjsing 		goto err;
205dab3f910Sjsing 	}
206dab3f910Sjsing 	/* Make it 128 for linux */
207dab3f910Sjsing 	if (type == SOCK_STREAM && listen(s, 128) == -1)
208dab3f910Sjsing 		goto err;
209dab3f910Sjsing 	*sock = s;
210dab3f910Sjsing 	ret = 1;
211dab3f910Sjsing  err:
212dab3f910Sjsing 	if ((ret == 0) && (s != -1)) {
213dab3f910Sjsing 		shutdown(s, SHUT_RD);
214dab3f910Sjsing 		close(s);
215dab3f910Sjsing 	}
216dab3f910Sjsing 	return (ret);
217dab3f910Sjsing }
218dab3f910Sjsing 
219dab3f910Sjsing static int
220dab3f910Sjsing init_server(int *sock, int port, int type)
221dab3f910Sjsing {
222dab3f910Sjsing 	return (init_server_long(sock, port, NULL, type));
223dab3f910Sjsing }
224dab3f910Sjsing 
225dab3f910Sjsing static int
2267537a7c2Stb do_accept(int acc_sock, int *sock)
227dab3f910Sjsing {
228dab3f910Sjsing 	struct hostent *h1, *h2;
229dab3f910Sjsing 	static struct sockaddr_in from;
230dab3f910Sjsing 	socklen_t len;
2317537a7c2Stb 	char *host = NULL;
2327537a7c2Stb 	int ret;
233dab3f910Sjsing 
234dab3f910Sjsing  redoit:
235dab3f910Sjsing 
236dab3f910Sjsing 	memset((char *) &from, 0, sizeof(from));
237dab3f910Sjsing 	len = sizeof(from);
238dab3f910Sjsing 	ret = accept(acc_sock, (struct sockaddr *) & from, &len);
239dab3f910Sjsing 	if (ret == -1) {
240dab3f910Sjsing 		if (errno == EINTR) {
241dab3f910Sjsing 			/* check_timeout(); */
242dab3f910Sjsing 			goto redoit;
243dab3f910Sjsing 		}
244dab3f910Sjsing 		fprintf(stderr, "errno=%d ", errno);
245dab3f910Sjsing 		perror("accept");
246dab3f910Sjsing 		return (0);
247dab3f910Sjsing 	}
248dab3f910Sjsing 
249dab3f910Sjsing 	h1 = gethostbyaddr((char *) &from.sin_addr.s_addr,
250dab3f910Sjsing 	    sizeof(from.sin_addr.s_addr), AF_INET);
251dab3f910Sjsing 	if (h1 == NULL) {
252dab3f910Sjsing 		BIO_printf(bio_err, "bad gethostbyaddr\n");
253dab3f910Sjsing 	} else {
2547537a7c2Stb 		if ((host = strdup(h1->h_name)) == NULL) {
255dab3f910Sjsing 			perror("strdup");
256dab3f910Sjsing 			close(ret);
257dab3f910Sjsing 			return (0);
258dab3f910Sjsing 		}
259dab3f910Sjsing 
2607537a7c2Stb 		h2 = gethostbyname(host);
261dab3f910Sjsing 		if (h2 == NULL) {
262dab3f910Sjsing 			BIO_printf(bio_err, "gethostbyname failure\n");
263dab3f910Sjsing 			close(ret);
2647537a7c2Stb 			free(host);
265dab3f910Sjsing 			return (0);
266dab3f910Sjsing 		}
267dab3f910Sjsing 		if (h2->h_addrtype != AF_INET) {
268dab3f910Sjsing 			BIO_printf(bio_err, "gethostbyname addr is not AF_INET\n");
269dab3f910Sjsing 			close(ret);
2707537a7c2Stb 			free(host);
271dab3f910Sjsing 			return (0);
272dab3f910Sjsing 		}
273dab3f910Sjsing 	}
274dab3f910Sjsing 
2757537a7c2Stb 	free(host);
276dab3f910Sjsing 	*sock = ret;
277dab3f910Sjsing 	return (1);
278dab3f910Sjsing }
279dab3f910Sjsing 
280dab3f910Sjsing int
281dab3f910Sjsing extract_host_port(char *str, char **host_ptr, unsigned char *ip,
282dab3f910Sjsing     char **port_ptr)
283dab3f910Sjsing {
284dab3f910Sjsing 	char *h, *p;
285dab3f910Sjsing 
286dab3f910Sjsing 	h = str;
287dab3f910Sjsing 	p = strrchr(str, '/');	/* IPv6 host/port */
288dab3f910Sjsing 	if (p == NULL) {
289dab3f910Sjsing 		p = strrchr(str, ':');
290dab3f910Sjsing 	}
291dab3f910Sjsing 	if (p == NULL) {
292dab3f910Sjsing 		BIO_printf(bio_err, "no port defined\n");
293dab3f910Sjsing 		return (0);
294dab3f910Sjsing 	}
295dab3f910Sjsing 	*(p++) = '\0';
296dab3f910Sjsing 
297dab3f910Sjsing 	if (host_ptr != NULL)
298dab3f910Sjsing 		*host_ptr = h;
299dab3f910Sjsing 
300dab3f910Sjsing 	if (port_ptr != NULL && p != NULL && *p != '\0')
301dab3f910Sjsing 		*port_ptr = p;
302dab3f910Sjsing 
303dab3f910Sjsing 	return (1);
304dab3f910Sjsing }
305dab3f910Sjsing 
306dab3f910Sjsing int
307dab3f910Sjsing extract_port(char *str, short *port_ptr)
308dab3f910Sjsing {
309dab3f910Sjsing 	int i;
310dab3f910Sjsing 	const char *errstr;
311dab3f910Sjsing 	struct servent *s;
312dab3f910Sjsing 
313dab3f910Sjsing 	i = strtonum(str, 1, 65535, &errstr);
314dab3f910Sjsing 	if (!errstr) {
315dab3f910Sjsing 		*port_ptr = (unsigned short) i;
316dab3f910Sjsing 	} else {
317dab3f910Sjsing 		s = getservbyname(str, "tcp");
318dab3f910Sjsing 		if (s == NULL) {
319dab3f910Sjsing 			BIO_printf(bio_err, "getservbyname failure for %s\n", str);
320dab3f910Sjsing 			return (0);
321dab3f910Sjsing 		}
322dab3f910Sjsing 		*port_ptr = ntohs((unsigned short) s->s_port);
323dab3f910Sjsing 	}
324dab3f910Sjsing 	return (1);
325dab3f910Sjsing }
326