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