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