File: | src/lib/libcrypto/bio/b_sock.c |
Warning: | line 185, column 3 Value stored to 'bind_mode' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* $OpenBSD: b_sock.c,v 1.71 2023/07/05 21:23:37 beck Exp $ */ |
2 | /* |
3 | * Copyright (c) 2017 Bob Beck <beck@openbsd.org> |
4 | * |
5 | * Permission to use, copy, modify, and distribute this software for any |
6 | * purpose with or without fee is hereby granted, provided that the above |
7 | * copyright notice and this permission notice appear in all copies. |
8 | * |
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
16 | */ |
17 | |
18 | #include <sys/ioctl.h> |
19 | #include <sys/socket.h> |
20 | #include <string.h> |
21 | |
22 | #include <arpa/inet.h> |
23 | #include <netinet/in.h> |
24 | #include <netinet/tcp.h> |
25 | |
26 | #include <errno(*__errno()).h> |
27 | #include <limits.h> |
28 | #include <netdb.h> |
29 | #include <stdio.h> |
30 | #include <stdlib.h> |
31 | #include <unistd.h> |
32 | |
33 | #include <openssl/bio.h> |
34 | #include <openssl/buffer.h> |
35 | #include <openssl/err.h> |
36 | |
37 | int |
38 | BIO_get_host_ip(const char *str, unsigned char *ip) |
39 | { |
40 | struct addrinfo *res = NULL((void *)0); |
41 | struct addrinfo hints = { |
42 | .ai_family = AF_INET2, |
43 | .ai_socktype = SOCK_STREAM1, |
44 | .ai_flags = AI_PASSIVE1, |
45 | }; |
46 | uint32_t *iap = (in_addr_t *)ip; |
47 | int error; |
48 | |
49 | if (str == NULL((void *)0)) { |
50 | BIOerror(BIO_R_BAD_HOSTNAME_LOOKUP)ERR_put_error(32,(0xfff),(102),"/usr/src/lib/libcrypto/bio/b_sock.c" ,50); |
51 | ERR_asprintf_error_data("NULL host provided"); |
52 | return (0); |
53 | } |
54 | |
55 | if ((error = getaddrinfo(str, NULL((void *)0), &hints, &res)) != 0) { |
56 | BIOerror(BIO_R_BAD_HOSTNAME_LOOKUP)ERR_put_error(32,(0xfff),(102),"/usr/src/lib/libcrypto/bio/b_sock.c" ,56); |
57 | ERR_asprintf_error_data("getaddrinfo: host='%s' : %s'", str, |
58 | gai_strerror(error)); |
59 | return (0); |
60 | } |
61 | *iap = (uint32_t)(((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr); |
62 | freeaddrinfo(res); |
63 | return (1); |
64 | } |
65 | LCRYPTO_ALIAS(BIO_get_host_ip)asm(""); |
66 | |
67 | int |
68 | BIO_get_port(const char *str, unsigned short *port_ptr) |
69 | { |
70 | struct addrinfo *res = NULL((void *)0); |
71 | struct addrinfo hints = { |
72 | .ai_family = AF_UNSPEC0, |
73 | .ai_socktype = SOCK_STREAM1, |
74 | .ai_flags = AI_PASSIVE1, |
75 | }; |
76 | int error; |
77 | |
78 | if (str == NULL((void *)0)) { |
79 | BIOerror(BIO_R_NO_PORT_SPECIFIED)ERR_put_error(32,(0xfff),(114),"/usr/src/lib/libcrypto/bio/b_sock.c" ,79); |
80 | return (0); |
81 | } |
82 | |
83 | if ((error = getaddrinfo(NULL((void *)0), str, &hints, &res)) != 0) { |
84 | BIOerror(BIO_R_INVALID_ARGUMENT)ERR_put_error(32,(0xfff),(125),"/usr/src/lib/libcrypto/bio/b_sock.c" ,84); |
85 | ERR_asprintf_error_data("getaddrinfo: service='%s' : %s'", str, |
86 | gai_strerror(error)); |
87 | return (0); |
88 | } |
89 | *port_ptr = ntohs(((struct sockaddr_in *)(res->ai_addr))->sin_port)(__uint16_t)(__builtin_constant_p(((struct sockaddr_in *)(res ->ai_addr))->sin_port) ? (__uint16_t)(((__uint16_t)(((struct sockaddr_in *)(res->ai_addr))->sin_port) & 0xffU) << 8 | ((__uint16_t)(((struct sockaddr_in *)(res->ai_addr))-> sin_port) & 0xff00U) >> 8) : __swap16md(((struct sockaddr_in *)(res->ai_addr))->sin_port)); |
90 | freeaddrinfo(res); |
91 | return (1); |
92 | } |
93 | LCRYPTO_ALIAS(BIO_get_port)asm(""); |
94 | |
95 | int |
96 | BIO_sock_error(int sock) |
97 | { |
98 | socklen_t len; |
99 | int err; |
100 | |
101 | len = sizeof(err); |
102 | if (getsockopt(sock, SOL_SOCKET0xffff, SO_ERROR0x1007, &err, &len) != 0) |
103 | return (1); |
104 | return (err); |
105 | } |
106 | LCRYPTO_ALIAS(BIO_sock_error)asm(""); |
107 | |
108 | struct hostent * |
109 | BIO_gethostbyname(const char *name) |
110 | { |
111 | return gethostbyname(name); |
112 | } |
113 | LCRYPTO_ALIAS(BIO_gethostbyname)asm(""); |
114 | |
115 | int |
116 | BIO_socket_ioctl(int fd, long type, void *arg) |
117 | { |
118 | int ret; |
119 | |
120 | ret = ioctl(fd, type, arg); |
121 | if (ret < 0) |
122 | SYSerror(errno)ERR_put_error(2,(0xfff),((*__errno())),"/usr/src/lib/libcrypto/bio/b_sock.c" ,122); |
123 | return (ret); |
124 | } |
125 | LCRYPTO_ALIAS(BIO_socket_ioctl)asm(""); |
126 | |
127 | int |
128 | BIO_get_accept_socket(char *host, int bind_mode) |
129 | { |
130 | struct addrinfo hints = { |
131 | .ai_family = AF_INET2, |
132 | .ai_socktype = SOCK_STREAM1, |
133 | .ai_flags = AI_PASSIVE1, |
134 | }; |
135 | struct addrinfo *res = NULL((void *)0); |
136 | char *h, *p, *str = NULL((void *)0); |
137 | int error, ret = 0, s = -1; |
138 | |
139 | if (host == NULL((void *)0)) { |
140 | BIOerror(BIO_R_NO_PORT_SPECIFIED)ERR_put_error(32,(0xfff),(114),"/usr/src/lib/libcrypto/bio/b_sock.c" ,140); |
141 | return (-1); |
142 | } |
143 | if ((str = strdup(host)) == NULL((void *)0)) { |
144 | BIOerror(ERR_R_MALLOC_FAILURE)ERR_put_error(32,(0xfff),((1|64)),"/usr/src/lib/libcrypto/bio/b_sock.c" ,144); |
145 | return (-1); |
146 | } |
147 | p = NULL((void *)0); |
148 | h = str; |
149 | if ((p = strrchr(str, ':')) == NULL((void *)0)) { |
150 | /* A string without a colon is treated as a port. */ |
151 | p = str; |
152 | h = NULL((void *)0); |
153 | } else { |
154 | *p++ = '\0'; |
155 | if (*p == '\0') { |
156 | BIOerror(BIO_R_NO_PORT_SPECIFIED)ERR_put_error(32,(0xfff),(114),"/usr/src/lib/libcrypto/bio/b_sock.c" ,156); |
157 | goto err; |
158 | } |
159 | if (*h == '\0' || strcmp(h, "*") == 0) |
160 | h = NULL((void *)0); |
161 | } |
162 | |
163 | if ((error = getaddrinfo(h, p, &hints, &res)) != 0) { |
164 | BIOerror(BIO_R_BAD_HOSTNAME_LOOKUP)ERR_put_error(32,(0xfff),(102),"/usr/src/lib/libcrypto/bio/b_sock.c" ,164); |
165 | ERR_asprintf_error_data("getaddrinfo: '%s:%s': %s'", h, p, |
166 | gai_strerror(error)); |
167 | goto err; |
168 | } |
169 | if (h == NULL((void *)0)) { |
170 | struct sockaddr_in *sin = (struct sockaddr_in *)res->ai_addr; |
171 | sin->sin_addr.s_addr = INADDR_ANY((u_int32_t)(0x00000000)); |
172 | } |
173 | |
174 | s = socket(AF_INET2, SOCK_STREAM1, IPPROTO_TCP6); |
175 | if (s == -1) { |
176 | SYSerror(errno)ERR_put_error(2,(0xfff),((*__errno())),"/usr/src/lib/libcrypto/bio/b_sock.c" ,176); |
177 | ERR_asprintf_error_data("host='%s'", host); |
178 | BIOerror(BIO_R_UNABLE_TO_CREATE_SOCKET)ERR_put_error(32,(0xfff),(118),"/usr/src/lib/libcrypto/bio/b_sock.c" ,178); |
179 | goto err; |
180 | } |
181 | if (bind_mode == BIO_BIND_REUSEADDR2) { |
182 | int i = 1; |
183 | |
184 | ret = setsockopt(s, SOL_SOCKET0xffff, SO_REUSEADDR0x0004, &i, sizeof(i)); |
185 | bind_mode = BIO_BIND_NORMAL0; |
Value stored to 'bind_mode' is never read | |
186 | } |
187 | if (bind(s, res->ai_addr, res->ai_addrlen) == -1) { |
188 | SYSerror(errno)ERR_put_error(2,(0xfff),((*__errno())),"/usr/src/lib/libcrypto/bio/b_sock.c" ,188); |
189 | ERR_asprintf_error_data("host='%s'", host); |
190 | BIOerror(BIO_R_UNABLE_TO_BIND_SOCKET)ERR_put_error(32,(0xfff),(117),"/usr/src/lib/libcrypto/bio/b_sock.c" ,190); |
191 | goto err; |
192 | } |
193 | if (listen(s, SOMAXCONN128) == -1) { |
194 | SYSerror(errno)ERR_put_error(2,(0xfff),((*__errno())),"/usr/src/lib/libcrypto/bio/b_sock.c" ,194); |
195 | ERR_asprintf_error_data("host='%s'", host); |
196 | BIOerror(BIO_R_UNABLE_TO_LISTEN_SOCKET)ERR_put_error(32,(0xfff),(119),"/usr/src/lib/libcrypto/bio/b_sock.c" ,196); |
197 | goto err; |
198 | } |
199 | ret = 1; |
200 | |
201 | err: |
202 | free(str); |
203 | if (res != NULL((void *)0)) |
204 | freeaddrinfo(res); |
205 | if ((ret == 0) && (s != -1)) { |
206 | close(s); |
207 | s = -1; |
208 | } |
209 | return (s); |
210 | } |
211 | LCRYPTO_ALIAS(BIO_get_accept_socket)asm(""); |
212 | |
213 | int |
214 | BIO_accept(int sock, char **addr) |
215 | { |
216 | char h[NI_MAXHOST256], s[NI_MAXSERV32]; |
217 | struct sockaddr_in sin; |
218 | socklen_t sin_len = sizeof(sin); |
219 | int ret = -1; |
220 | |
221 | if (addr == NULL((void *)0)) { |
222 | BIOerror(BIO_R_NULL_PARAMETER)ERR_put_error(32,(0xfff),(115),"/usr/src/lib/libcrypto/bio/b_sock.c" ,222); |
223 | goto end; |
224 | } |
225 | ret = accept(sock, (struct sockaddr *)&sin, &sin_len); |
226 | if (ret == -1) { |
227 | if (BIO_sock_should_retry(ret)) |
228 | return -2; |
229 | SYSerror(errno)ERR_put_error(2,(0xfff),((*__errno())),"/usr/src/lib/libcrypto/bio/b_sock.c" ,229); |
230 | BIOerror(BIO_R_ACCEPT_ERROR)ERR_put_error(32,(0xfff),(100),"/usr/src/lib/libcrypto/bio/b_sock.c" ,230); |
231 | goto end; |
232 | } |
233 | /* XXX Crazy API. Can't be helped */ |
234 | if (*addr != NULL((void *)0)) { |
235 | free(*addr); |
236 | *addr = NULL((void *)0); |
237 | } |
238 | |
239 | if (sin.sin_family != AF_INET2) |
240 | goto end; |
241 | |
242 | if (getnameinfo((struct sockaddr *)&sin, sin_len, h, sizeof(h), |
243 | s, sizeof(s), NI_NUMERICHOST1|NI_NUMERICSERV2) != 0) |
244 | goto end; |
245 | |
246 | if ((asprintf(addr, "%s:%s", h, s)) == -1) { |
247 | BIOerror(ERR_R_MALLOC_FAILURE)ERR_put_error(32,(0xfff),((1|64)),"/usr/src/lib/libcrypto/bio/b_sock.c" ,247); |
248 | *addr = NULL((void *)0); |
249 | goto end; |
250 | } |
251 | end: |
252 | return (ret); |
253 | } |
254 | LCRYPTO_ALIAS(BIO_accept)asm(""); |
255 | |
256 | int |
257 | BIO_set_tcp_ndelay(int s, int on) |
258 | { |
259 | return (setsockopt(s, IPPROTO_TCP6, TCP_NODELAY0x01, &on, sizeof(on)) == 0); |
260 | } |
261 | LCRYPTO_ALIAS(BIO_set_tcp_ndelay)asm(""); |