]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
ce04c33d | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
064af421 | 7 | * |
a14bc59f BP |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
064af421 BP |
15 | */ |
16 | ||
17 | #include <config.h> | |
18 | #include "socket-util.h" | |
b2befd5b BP |
19 | #include <sys/types.h> |
20 | #include <netinet/in.h> | |
064af421 | 21 | #include <arpa/inet.h> |
5d77b36b | 22 | #include <ctype.h> |
064af421 BP |
23 | #include <errno.h> |
24 | #include <fcntl.h> | |
f89ffb0e | 25 | #include <net/if.h> |
064af421 | 26 | #include <netdb.h> |
b7cefbf7 | 27 | #include <netinet/tcp.h> |
064af421 BP |
28 | #include <poll.h> |
29 | #include <stddef.h> | |
30 | #include <stdio.h> | |
78ff0270 | 31 | #include <stdlib.h> |
064af421 | 32 | #include <string.h> |
3762274e BP |
33 | #include <sys/socket.h> |
34 | #include <sys/stat.h> | |
a7a27d07 | 35 | #include <sys/uio.h> |
064af421 BP |
36 | #include <sys/un.h> |
37 | #include <unistd.h> | |
3e8a2ad1 | 38 | #include "openvswitch/dynamic-string.h" |
b26f46a4 | 39 | #include "ovs-thread.h" |
f89ffb0e | 40 | #include "packets.h" |
fd016ae3 | 41 | #include "openvswitch/poll-loop.h" |
064af421 | 42 | #include "util.h" |
e6211adc | 43 | #include "openvswitch/vlog.h" |
2f51a7eb | 44 | #ifdef __linux__ |
f89ffb0e BP |
45 | #include <linux/if_packet.h> |
46 | #endif | |
47 | #ifdef HAVE_NETLINK | |
48 | #include "netlink-protocol.h" | |
49 | #include "netlink-socket.h" | |
50 | #endif | |
771680d9 | 51 | #include "dns-resolve.h" |
5136ce49 | 52 | |
d98e6007 | 53 | VLOG_DEFINE_THIS_MODULE(socket_util); |
064af421 | 54 | |
c1c19657 BP |
55 | static int getsockopt_int(int fd, int level, int option, const char *optname, |
56 | int *valuep); | |
93b7faf1 BP |
57 | static struct sockaddr_in *sin_cast(const struct sockaddr *); |
58 | static struct sockaddr_in6 *sin6_cast(const struct sockaddr *); | |
59 | static const struct sockaddr *sa_cast(const struct sockaddr_storage *); | |
771680d9 YS |
60 | static bool parse_sockaddr_components(struct sockaddr_storage *ss, |
61 | char *host_s, | |
62 | const char *port_s, | |
63 | uint16_t default_port, | |
64 | const char *s, | |
65 | bool resolve_host); | |
c1c19657 | 66 | |
064af421 BP |
67 | /* Sets 'fd' to non-blocking mode. Returns 0 if successful, otherwise a |
68 | * positive errno value. */ | |
69 | int | |
70 | set_nonblocking(int fd) | |
71 | { | |
de8bd597 | 72 | #ifndef _WIN32 |
064af421 BP |
73 | int flags = fcntl(fd, F_GETFL, 0); |
74 | if (flags != -1) { | |
75 | if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) != -1) { | |
76 | return 0; | |
77 | } else { | |
10a89ef0 | 78 | VLOG_ERR("fcntl(F_SETFL) failed: %s", ovs_strerror(errno)); |
064af421 BP |
79 | return errno; |
80 | } | |
81 | } else { | |
10a89ef0 | 82 | VLOG_ERR("fcntl(F_GETFL) failed: %s", ovs_strerror(errno)); |
064af421 BP |
83 | return errno; |
84 | } | |
de8bd597 GS |
85 | #else |
86 | unsigned long arg = 1; | |
87 | if (ioctlsocket(fd, FIONBIO, &arg)) { | |
88 | int error = sock_errno(); | |
89 | VLOG_ERR("set_nonblocking failed: %s", sock_strerror(error)); | |
90 | return error; | |
91 | } | |
92 | return 0; | |
93 | #endif | |
064af421 BP |
94 | } |
95 | ||
a0505c49 BP |
96 | void |
97 | xset_nonblocking(int fd) | |
98 | { | |
99 | if (set_nonblocking(fd)) { | |
100 | exit(EXIT_FAILURE); | |
101 | } | |
102 | } | |
103 | ||
b7cefbf7 GS |
104 | void |
105 | setsockopt_tcp_nodelay(int fd) | |
106 | { | |
107 | int on = 1; | |
108 | int retval; | |
109 | ||
110 | retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on); | |
111 | if (retval) { | |
112 | retval = sock_errno(); | |
113 | VLOG_ERR("setsockopt(TCP_NODELAY): %s", sock_strerror(retval)); | |
114 | } | |
115 | } | |
116 | ||
afc1d536 BP |
117 | /* Sets the DSCP value of socket 'fd' to 'dscp', which must be 63 or less. |
118 | * 'family' must indicate the socket's address family (AF_INET or AF_INET6, to | |
119 | * do anything useful). */ | |
6b9c1eab | 120 | int |
afc1d536 | 121 | set_dscp(int fd, int family, uint8_t dscp) |
cea15768 | 122 | { |
afc1d536 | 123 | int retval; |
eb0cb316 EM |
124 | int val; |
125 | ||
5349904d GS |
126 | #ifdef _WIN32 |
127 | /* XXX: Consider using QoS2 APIs for Windows to set dscp. */ | |
128 | return 0; | |
129 | #endif | |
130 | ||
cea15768 EJ |
131 | if (dscp > 63) { |
132 | return EINVAL; | |
133 | } | |
eb0cb316 | 134 | val = dscp << 2; |
afc1d536 BP |
135 | |
136 | switch (family) { | |
137 | case AF_INET: | |
138 | retval = setsockopt(fd, IPPROTO_IP, IP_TOS, &val, sizeof val); | |
139 | break; | |
140 | ||
141 | case AF_INET6: | |
142 | retval = setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &val, sizeof val); | |
143 | break; | |
144 | ||
145 | default: | |
92ae5930 | 146 | return ENOPROTOOPT; |
cea15768 EJ |
147 | } |
148 | ||
afc1d536 | 149 | return retval ? sock_errno() : 0; |
cea15768 EJ |
150 | } |
151 | ||
9835576b JB |
152 | /* Checks whether 'host_name' is an IPv4 or IPv6 address. It is assumed |
153 | * that 'host_name' is valid. Returns false if it is IPv4 address, true if | |
154 | * it is IPv6 address. */ | |
155 | bool | |
156 | addr_is_ipv6(const char *host_name) | |
157 | { | |
158 | return strchr(host_name, ':') != NULL; | |
159 | } | |
160 | ||
2b35e147 BP |
161 | /* Translates 'host_name', which must be a string representation of an IP |
162 | * address, into a numeric IP address in '*addr'. Returns 0 if successful, | |
163 | * otherwise a positive errno value. */ | |
064af421 | 164 | int |
d295e8e9 | 165 | lookup_ip(const char *host_name, struct in_addr *addr) |
064af421 | 166 | { |
e7695092 | 167 | if (!ip_parse(host_name, &addr->s_addr)) { |
db5ce514 | 168 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
2b35e147 BP |
169 | VLOG_ERR_RL(&rl, "\"%s\" is not a valid IP address", host_name); |
170 | return ENOENT; | |
064af421 BP |
171 | } |
172 | return 0; | |
173 | } | |
174 | ||
d31f1109 JP |
175 | /* Translates 'host_name', which must be a string representation of an IPv6 |
176 | * address, into a numeric IPv6 address in '*addr'. Returns 0 if successful, | |
177 | * otherwise a positive errno value. */ | |
178 | int | |
179 | lookup_ipv6(const char *host_name, struct in6_addr *addr) | |
180 | { | |
e7695092 | 181 | if (!ipv6_parse(host_name, addr)) { |
d31f1109 JP |
182 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
183 | VLOG_ERR_RL(&rl, "\"%s\" is not a valid IPv6 address", host_name); | |
184 | return ENOENT; | |
185 | } | |
186 | return 0; | |
187 | } | |
188 | ||
13f2ef97 BP |
189 | /* Translates 'host_name', which must be a host name or a string representation |
190 | * of an IP address, into a numeric IP address in '*addr'. Returns 0 if | |
191 | * successful, otherwise a positive errno value. | |
192 | * | |
193 | * Most Open vSwitch code should not use this because it causes deadlocks: | |
3cbb5dc7 | 194 | * getaddrinfo() sends out a DNS request but that starts a new flow for which |
13f2ef97 | 195 | * OVS must set up a flow, but it can't because it's waiting for a DNS reply. |
293d49bd | 196 | * The synchronous lookup also delays other activity. (Of course we can solve |
13f2ef97 BP |
197 | * this but it doesn't seem worthwhile quite yet.) */ |
198 | int | |
199 | lookup_hostname(const char *host_name, struct in_addr *addr) | |
200 | { | |
3cbb5dc7 BP |
201 | struct addrinfo *result; |
202 | struct addrinfo hints; | |
13f2ef97 | 203 | |
e7695092 | 204 | if (ip_parse(host_name, &addr->s_addr)) { |
13f2ef97 BP |
205 | return 0; |
206 | } | |
207 | ||
3cbb5dc7 BP |
208 | memset(&hints, 0, sizeof hints); |
209 | hints.ai_family = AF_INET; | |
210 | ||
211 | switch (getaddrinfo(host_name, NULL, &hints, &result)) { | |
212 | case 0: | |
db5a1019 AW |
213 | *addr = ALIGNED_CAST(struct sockaddr_in *, |
214 | result->ai_addr)->sin_addr; | |
3cbb5dc7 | 215 | freeaddrinfo(result); |
13f2ef97 | 216 | return 0; |
13f2ef97 | 217 | |
44f645a5 | 218 | #ifdef EAI_ADDRFAMILY |
3cbb5dc7 | 219 | case EAI_ADDRFAMILY: |
44f645a5 | 220 | #endif |
3cbb5dc7 BP |
221 | case EAI_NONAME: |
222 | case EAI_SERVICE: | |
223 | return ENOENT; | |
224 | ||
225 | case EAI_AGAIN: | |
226 | return EAGAIN; | |
227 | ||
228 | case EAI_BADFLAGS: | |
229 | case EAI_FAMILY: | |
230 | case EAI_SOCKTYPE: | |
231 | return EINVAL; | |
232 | ||
233 | case EAI_FAIL: | |
234 | return EIO; | |
235 | ||
236 | case EAI_MEMORY: | |
237 | return ENOMEM; | |
238 | ||
99ad8ba8 | 239 | #if defined (EAI_NODATA) && EAI_NODATA != EAI_NONAME |
3cbb5dc7 BP |
240 | case EAI_NODATA: |
241 | return ENXIO; | |
44f645a5 | 242 | #endif |
3cbb5dc7 | 243 | |
99ad8ba8 | 244 | #ifdef EAI_SYSTEM |
3cbb5dc7 | 245 | case EAI_SYSTEM: |
0f0b5401 | 246 | return sock_errno(); |
99ad8ba8 | 247 | #endif |
3cbb5dc7 BP |
248 | |
249 | default: | |
250 | return EPROTO; | |
251 | } | |
13f2ef97 BP |
252 | } |
253 | ||
064af421 | 254 | int |
d295e8e9 | 255 | check_connection_completion(int fd) |
064af421 | 256 | { |
d6cedfd9 | 257 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 10); |
064af421 BP |
258 | struct pollfd pfd; |
259 | int retval; | |
260 | ||
261 | pfd.fd = fd; | |
262 | pfd.events = POLLOUT; | |
1bada7ab GS |
263 | |
264 | #ifndef _WIN32 | |
064af421 BP |
265 | do { |
266 | retval = poll(&pfd, 1, 0); | |
267 | } while (retval < 0 && errno == EINTR); | |
1bada7ab | 268 | #else |
ff792c6a AS |
269 | fd_set wrset, exset; |
270 | FD_ZERO(&wrset); | |
271 | FD_ZERO(&exset); | |
272 | FD_SET(fd, &exset); | |
273 | FD_SET(fd, &wrset); | |
274 | pfd.revents = 0; | |
275 | struct timeval tv = { 0, 0 }; | |
276 | /* WSAPoll is broken on Windows, instead do a select */ | |
277 | retval = select(0, NULL, &wrset, &exset, &tv); | |
278 | if (retval == 1) { | |
279 | if (FD_ISSET(fd, &wrset)) { | |
280 | pfd.revents |= pfd.events; | |
281 | } | |
282 | if (FD_ISSET(fd, &exset)) { | |
283 | pfd.revents |= POLLERR; | |
284 | } | |
285 | } | |
1bada7ab | 286 | #endif |
064af421 | 287 | if (retval == 1) { |
cfef5ae8 | 288 | if (pfd.revents & (POLLERR | POLLHUP)) { |
1bada7ab | 289 | ssize_t n = send(fd, "", 1, 0); |
d6cedfd9 | 290 | if (n < 0) { |
1bada7ab | 291 | return sock_errno(); |
d6cedfd9 BP |
292 | } else { |
293 | VLOG_ERR_RL(&rl, "poll return POLLERR but send succeeded"); | |
294 | return EPROTO; | |
295 | } | |
296 | } | |
297 | return 0; | |
064af421 | 298 | } else if (retval < 0) { |
1bada7ab | 299 | VLOG_ERR_RL(&rl, "poll: %s", sock_strerror(sock_errno())); |
064af421 BP |
300 | return errno; |
301 | } else { | |
302 | return EAGAIN; | |
303 | } | |
304 | } | |
305 | ||
c1c19657 BP |
306 | /* Returns the size of socket 'sock''s receive buffer (SO_RCVBUF), or a |
307 | * negative errno value if an error occurs. */ | |
308 | int | |
309 | get_socket_rcvbuf(int sock) | |
310 | { | |
311 | int rcvbuf; | |
312 | int error; | |
313 | ||
314 | error = getsockopt_int(sock, SOL_SOCKET, SO_RCVBUF, "SO_RCVBUF", &rcvbuf); | |
315 | return error ? -error : rcvbuf; | |
316 | } | |
317 | ||
064af421 BP |
318 | /* Reads and discards up to 'n' datagrams from 'fd', stopping as soon as no |
319 | * more data can be immediately read. ('fd' should therefore be in | |
320 | * non-blocking mode.)*/ | |
321 | void | |
322 | drain_fd(int fd, size_t n_packets) | |
323 | { | |
324 | for (; n_packets > 0; n_packets--) { | |
325 | /* 'buffer' only needs to be 1 byte long in most circumstances. This | |
326 | * size is defensive against the possibility that we someday want to | |
327 | * use a Linux tap device without TUN_NO_PI, in which case a buffer | |
328 | * smaller than sizeof(struct tun_pi) will give EINVAL on read. */ | |
329 | char buffer[128]; | |
330 | if (read(fd, buffer, sizeof buffer) <= 0) { | |
331 | break; | |
332 | } | |
333 | } | |
334 | } | |
335 | ||
dbba996b BP |
336 | ovs_be32 |
337 | guess_netmask(ovs_be32 ip_) | |
064af421 | 338 | { |
dbba996b | 339 | uint32_t ip = ntohl(ip_); |
064af421 BP |
340 | return ((ip >> 31) == 0 ? htonl(0xff000000) /* Class A */ |
341 | : (ip >> 30) == 2 ? htonl(0xffff0000) /* Class B */ | |
342 | : (ip >> 29) == 6 ? htonl(0xffffff00) /* Class C */ | |
343 | : htonl(0)); /* ??? */ | |
344 | } | |
345 | ||
0b043300 BP |
346 | static char * |
347 | unbracket(char *s) | |
78ff0270 | 348 | { |
0b043300 BP |
349 | if (*s == '[') { |
350 | s++; | |
351 | ||
352 | char *end = strchr(s, '\0'); | |
353 | if (end[-1] == ']') { | |
354 | end[-1] = '\0'; | |
355 | } | |
356 | } | |
357 | return s; | |
358 | } | |
359 | ||
360 | /* 'host_index' is 0 if the host precedes the port within 's', 1 otherwise. */ | |
361 | static void | |
362 | inet_parse_tokens__(char *s, int host_index, char **hostp, char **portp) | |
363 | { | |
364 | char *colon = NULL; | |
365 | bool in_brackets = false; | |
366 | int n_colons = 0; | |
367 | for (char *p = s; *p; p++) { | |
368 | if (*p == '[') { | |
369 | in_brackets = true; | |
370 | } else if (*p == ']') { | |
371 | in_brackets = false; | |
372 | } else if (*p == ':' && !in_brackets) { | |
373 | n_colons++; | |
374 | colon = p; | |
375 | } | |
376 | } | |
377 | ||
378 | *hostp = *portp = NULL; | |
379 | if (n_colons > 1) { | |
380 | *hostp = s; | |
e731d71b | 381 | } else { |
0b043300 BP |
382 | char **tokens[2]; |
383 | tokens[host_index] = hostp; | |
384 | tokens[!host_index] = portp; | |
385 | ||
386 | if (colon) { | |
387 | *colon = '\0'; | |
388 | *tokens[1] = unbracket(colon + 1); | |
389 | } | |
390 | *tokens[0] = unbracket(s); | |
78ff0270 | 391 | } |
e731d71b | 392 | } |
78ff0270 | 393 | |
0b043300 BP |
394 | /* Parses 's', a string in the form "<host>[:<port>]", into its (required) host |
395 | * and (optional) port components, and stores pointers to them in '*hostp' and | |
396 | * '*portp' respectively. Always sets '*hostp' nonnull, although possibly to | |
f415dad9 | 397 | * an empty string. Can set '*portp' to the null string. |
0b043300 BP |
398 | * |
399 | * Supports both IPv4 and IPv6. IPv6 addresses may be quoted with square | |
400 | * brackets. Resolves ambiguous cases that might represent an IPv6 address or | |
401 | * an IPv6 address and a port as representing just a host, e.g. "::1:2:3:4:80" | |
402 | * is a host but "[::1:2:3:4]:80" is a host and a port. | |
403 | * | |
404 | * Modifies 's' and points '*hostp' and '*portp' (if nonnull) into it. | |
405 | */ | |
406 | void | |
407 | inet_parse_host_port_tokens(char *s, char **hostp, char **portp) | |
408 | { | |
409 | inet_parse_tokens__(s, 0, hostp, portp); | |
410 | } | |
411 | ||
412 | /* Parses 's', a string in the form "<port>[:<host>]", into its port and host | |
413 | * components, and stores pointers to them in '*portp' and '*hostp' | |
f415dad9 | 414 | * respectively. Either '*portp' and '*hostp' (but not both) can end up null. |
0b043300 BP |
415 | * |
416 | * Supports both IPv4 and IPv6. IPv6 addresses may be quoted with square | |
417 | * brackets. Resolves ambiguous cases that might represent an IPv6 address or | |
418 | * an IPv6 address and a port as representing just a host, e.g. "::1:2:3:4:80" | |
419 | * is a host but "[::1:2:3:4]:80" is a host and a port. | |
420 | * | |
421 | * Modifies 's' and points '*hostp' and '*portp' (if nonnull) into it. | |
422 | */ | |
423 | void | |
424 | inet_parse_port_host_tokens(char *s, char **portp, char **hostp) | |
425 | { | |
426 | inet_parse_tokens__(s, 1, hostp, portp); | |
427 | } | |
428 | ||
771680d9 YS |
429 | static bool |
430 | parse_sockaddr_components_dns(struct sockaddr_storage *ss OVS_UNUSED, | |
431 | char *host_s, | |
432 | const char *port_s OVS_UNUSED, | |
433 | uint16_t default_port OVS_UNUSED, | |
434 | const char *s OVS_UNUSED) | |
435 | { | |
436 | char *tmp_host_s; | |
437 | ||
438 | dns_resolve(host_s, &tmp_host_s); | |
439 | if (tmp_host_s != NULL) { | |
440 | parse_sockaddr_components(ss, tmp_host_s, port_s, | |
441 | default_port, s, false); | |
442 | free(tmp_host_s); | |
443 | return true; | |
444 | } | |
445 | return false; | |
446 | } | |
447 | ||
e731d71b AS |
448 | static bool |
449 | parse_sockaddr_components(struct sockaddr_storage *ss, | |
5d77b36b | 450 | char *host_s, |
e731d71b | 451 | const char *port_s, uint16_t default_port, |
771680d9 YS |
452 | const char *s, |
453 | bool resolve_host) | |
e731d71b | 454 | { |
93b7faf1 | 455 | struct sockaddr_in *sin = sin_cast(sa_cast(ss)); |
e731d71b AS |
456 | int port; |
457 | ||
458 | if (port_s && port_s[0]) { | |
459 | if (!str_to_int(port_s, 10, &port) || port < 0 || port > 65535) { | |
460 | VLOG_ERR("%s: bad port number \"%s\"", s, port_s); | |
12c5f227 | 461 | goto exit; |
e731d71b AS |
462 | } |
463 | } else { | |
464 | port = default_port; | |
78ff0270 | 465 | } |
e731d71b AS |
466 | |
467 | memset(ss, 0, sizeof *ss); | |
5d77b36b | 468 | if (host_s && strchr(host_s, ':')) { |
93b7faf1 | 469 | struct sockaddr_in6 *sin6 = sin6_cast(sa_cast(ss)); |
5d77b36b BP |
470 | char *addr = strsep(&host_s, "%"); |
471 | ||
e731d71b AS |
472 | sin6->sin6_family = AF_INET6; |
473 | sin6->sin6_port = htons(port); | |
5d77b36b | 474 | if (!addr || !*addr || !ipv6_parse(addr, &sin6->sin6_addr)) { |
e731d71b AS |
475 | goto exit; |
476 | } | |
5d77b36b BP |
477 | |
478 | #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID | |
479 | char *scope = strsep(&host_s, "%"); | |
480 | if (scope && *scope) { | |
481 | if (!scope[strspn(scope, "0123456789")]) { | |
482 | sin6->sin6_scope_id = atoi(scope); | |
483 | } else { | |
484 | sin6->sin6_scope_id = if_nametoindex(scope); | |
485 | if (!sin6->sin6_scope_id) { | |
486 | VLOG_ERR("%s: bad IPv6 scope \"%s\" (%s)", | |
487 | s, scope, ovs_strerror(errno)); | |
488 | goto exit; | |
489 | } | |
490 | } | |
491 | } | |
492 | #endif | |
e731d71b AS |
493 | } else { |
494 | sin->sin_family = AF_INET; | |
495 | sin->sin_port = htons(port); | |
5d77b36b | 496 | if (host_s && !ip_parse(host_s, &sin->sin_addr.s_addr)) { |
771680d9 | 497 | goto resolve; |
e731d71b | 498 | } |
52f8a75e BP |
499 | } |
500 | ||
e731d71b | 501 | return true; |
52f8a75e | 502 | |
771680d9 YS |
503 | resolve: |
504 | if (resolve_host && parse_sockaddr_components_dns(ss, host_s, port_s, | |
505 | default_port, s)) { | |
506 | return true; | |
507 | } else if (!resolve_host) { | |
508 | VLOG_ERR("%s: bad IP address \"%s\"", s, host_s); | |
509 | } | |
52f8a75e | 510 | exit: |
e731d71b AS |
511 | memset(ss, 0, sizeof *ss); |
512 | return false; | |
513 | } | |
514 | ||
515 | /* Parses 'target', which should be a string in the format "<host>[:<port>]". | |
516 | * <host>, which is required, may be an IPv4 address or an IPv6 address | |
1bb01121 | 517 | * enclosed in square brackets. If 'default_port' is nonnegative then <port> |
28533680 DB |
518 | * is optional and defaults to 'default_port' (use 0 to make the kernel choose |
519 | * an available port, although this isn't usually appropriate for active | |
520 | * connections). If 'default_port' is negative, then <port> is required. | |
f31b8ae7 | 521 | * It resolves the host if 'resolve_host' is true. |
e731d71b AS |
522 | * |
523 | * On success, returns true and stores the parsed remote address into '*ss'. | |
524 | * On failure, logs an error, stores zeros into '*ss', and returns false. */ | |
525 | bool | |
1bb01121 | 526 | inet_parse_active(const char *target_, int default_port, |
f31b8ae7 | 527 | struct sockaddr_storage *ss, bool resolve_host) |
e731d71b AS |
528 | { |
529 | char *target = xstrdup(target_); | |
0b043300 | 530 | char *port, *host; |
e731d71b AS |
531 | bool ok; |
532 | ||
0b043300 | 533 | inet_parse_host_port_tokens(target, &host, &port); |
e731d71b AS |
534 | if (!host) { |
535 | VLOG_ERR("%s: host must be specified", target_); | |
536 | ok = false; | |
1bb01121 | 537 | } else if (!port && default_port < 0) { |
e731d71b AS |
538 | VLOG_ERR("%s: port must be specified", target_); |
539 | ok = false; | |
540 | } else { | |
771680d9 | 541 | ok = parse_sockaddr_components(ss, host, port, default_port, |
f31b8ae7 | 542 | target_, resolve_host); |
e731d71b | 543 | } |
52f8a75e | 544 | if (!ok) { |
e731d71b | 545 | memset(ss, 0, sizeof *ss); |
52f8a75e BP |
546 | } |
547 | free(target); | |
548 | return ok; | |
549 | } | |
550 | ||
e731d71b AS |
551 | |
552 | /* Opens a non-blocking IPv4 or IPv6 socket of the specified 'style' and | |
553 | * connects to 'target', which should be a string in the format | |
554 | * "<host>[:<port>]". <host>, which is required, may be an IPv4 address or an | |
1bb01121 BP |
555 | * IPv6 address enclosed in square brackets. If 'default_port' is nonnegative |
556 | * then <port> is optional and defaults to 'default_port'. | |
52f8a75e BP |
557 | * |
558 | * 'style' should be SOCK_STREAM (for TCP) or SOCK_DGRAM (for UDP). | |
559 | * | |
560 | * On success, returns 0 (indicating connection complete) or EAGAIN (indicating | |
561 | * connection in progress), in which case the new file descriptor is stored | |
562 | * into '*fdp'. On failure, returns a positive errno value other than EAGAIN | |
563 | * and stores -1 into '*fdp'. | |
564 | * | |
e731d71b | 565 | * If 'ss' is non-null, then on success stores the target address into '*ss'. |
f125905c | 566 | * |
cea15768 EJ |
567 | * 'dscp' becomes the DSCP bits in the IP headers for the new connection. It |
568 | * should be in the range [0, 63] and will automatically be shifted to the | |
569 | * appropriately place in the IP tos field. */ | |
52f8a75e | 570 | int |
1bb01121 | 571 | inet_open_active(int style, const char *target, int default_port, |
e731d71b | 572 | struct sockaddr_storage *ssp, int *fdp, uint8_t dscp) |
52f8a75e | 573 | { |
e731d71b | 574 | struct sockaddr_storage ss; |
52f8a75e BP |
575 | int fd = -1; |
576 | int error; | |
577 | ||
578 | /* Parse. */ | |
f31b8ae7 | 579 | if (!inet_parse_active(target, default_port, &ss, true)) { |
1901968e BP |
580 | error = EAFNOSUPPORT; |
581 | goto exit; | |
78ff0270 BP |
582 | } |
583 | ||
584 | /* Create non-blocking socket. */ | |
e731d71b | 585 | fd = socket(ss.ss_family, style, 0); |
78ff0270 | 586 | if (fd < 0) { |
0f0b5401 GS |
587 | error = sock_errno(); |
588 | VLOG_ERR("%s: socket: %s", target, sock_strerror(error)); | |
78ff0270 BP |
589 | goto exit; |
590 | } | |
591 | error = set_nonblocking(fd); | |
592 | if (error) { | |
a4efa3fc | 593 | goto exit; |
78ff0270 BP |
594 | } |
595 | ||
e731d71b AS |
596 | /* The dscp bits must be configured before connect() to ensure that the |
597 | * TOS field is set during the connection establishment. If set after | |
cea15768 | 598 | * connect(), the handshake SYN frames will be sent with a TOS of 0. */ |
afc1d536 | 599 | error = set_dscp(fd, ss.ss_family, dscp); |
cea15768 | 600 | if (error) { |
909dad2b | 601 | VLOG_ERR("%s: set_dscp: %s", target, sock_strerror(error)); |
ef8a3d14 | 602 | goto exit; |
f125905c MM |
603 | } |
604 | ||
78ff0270 | 605 | /* Connect. */ |
e731d71b AS |
606 | error = connect(fd, (struct sockaddr *) &ss, ss_length(&ss)) == 0 |
607 | ? 0 | |
0f0b5401 GS |
608 | : sock_errno(); |
609 | if (error == EINPROGRESS | |
610 | #ifdef _WIN32 | |
611 | || error == WSAEALREADY || error == WSAEWOULDBLOCK | |
612 | #endif | |
613 | ) { | |
78ff0270 | 614 | error = EAGAIN; |
78ff0270 BP |
615 | } |
616 | ||
78ff0270 | 617 | exit: |
e731d71b AS |
618 | if (error && error != EAGAIN) { |
619 | if (ssp) { | |
620 | memset(ssp, 0, sizeof *ssp); | |
621 | } | |
622 | if (fd >= 0) { | |
7009a594 | 623 | closesocket(fd); |
e731d71b AS |
624 | fd = -1; |
625 | } | |
626 | } else { | |
627 | if (ssp) { | |
628 | *ssp = ss; | |
78ff0270 | 629 | } |
78ff0270 | 630 | } |
a4efa3fc | 631 | *fdp = fd; |
78ff0270 BP |
632 | return error; |
633 | } | |
634 | ||
e731d71b | 635 | /* Parses 'target', which should be a string in the format "[<port>][:<host>]": |
e1bd3bee BP |
636 | * |
637 | * - If 'default_port' is -1, then <port> is required. Otherwise, if | |
638 | * <port> is omitted, then 'default_port' is used instead. | |
639 | * | |
640 | * - If <port> (or 'default_port', if used) is 0, then no port is bound | |
641 | * and the TCP/IP stack will select a port. | |
642 | * | |
e731d71b AS |
643 | * - <host> is optional. If supplied, it may be an IPv4 address or an |
644 | * IPv6 address enclosed in square brackets. If omitted, the IP address | |
645 | * is wildcarded. | |
4f2eb9a7 | 646 | * |
e731d71b AS |
647 | * If successful, stores the address into '*ss' and returns true; otherwise |
648 | * zeros '*ss' and returns false. */ | |
d98fa503 | 649 | bool |
995337c4 | 650 | inet_parse_passive(const char *target_, int default_port, |
e731d71b | 651 | struct sockaddr_storage *ss) |
78ff0270 BP |
652 | { |
653 | char *target = xstrdup(target_); | |
0b043300 | 654 | char *port, *host; |
e731d71b AS |
655 | bool ok; |
656 | ||
0b043300 | 657 | inet_parse_port_host_tokens(target, &port, &host); |
e731d71b AS |
658 | if (!port && default_port < 0) { |
659 | VLOG_ERR("%s: port must be specified", target_); | |
660 | ok = false; | |
661 | } else { | |
771680d9 YS |
662 | ok = parse_sockaddr_components(ss, host, port, default_port, |
663 | target_, true); | |
d98fa503 | 664 | } |
d98fa503 | 665 | if (!ok) { |
e731d71b | 666 | memset(ss, 0, sizeof *ss); |
d98fa503 BP |
667 | } |
668 | free(target); | |
669 | return ok; | |
670 | } | |
671 | ||
672 | ||
e731d71b | 673 | /* Opens a non-blocking IPv4 or IPv6 socket of the specified 'style', binds to |
d98fa503 BP |
674 | * 'target', and listens for incoming connections. Parses 'target' in the same |
675 | * way was inet_parse_passive(). | |
676 | * | |
677 | * 'style' should be SOCK_STREAM (for TCP) or SOCK_DGRAM (for UDP). | |
678 | * | |
679 | * For TCP, the socket will have SO_REUSEADDR turned on. | |
680 | * | |
681 | * On success, returns a non-negative file descriptor. On failure, returns a | |
682 | * negative errno value. | |
683 | * | |
e731d71b | 684 | * If 'ss' is non-null, then on success stores the bound address into '*ss'. |
f125905c | 685 | * |
cea15768 EJ |
686 | * 'dscp' becomes the DSCP bits in the IP headers for the new connection. It |
687 | * should be in the range [0, 63] and will automatically be shifted to the | |
b52ecd96 GS |
688 | * appropriately place in the IP tos field. |
689 | * | |
690 | * If 'kernel_print_port' is true and the port is dynamically assigned by | |
691 | * the kernel, print the chosen port. */ | |
d98fa503 BP |
692 | int |
693 | inet_open_passive(int style, const char *target, int default_port, | |
b52ecd96 GS |
694 | struct sockaddr_storage *ssp, uint8_t dscp, |
695 | bool kernel_print_port) | |
d98fa503 | 696 | { |
df451457 | 697 | bool kernel_chooses_port; |
e731d71b | 698 | struct sockaddr_storage ss; |
d98fa503 BP |
699 | int fd = 0, error; |
700 | unsigned int yes = 1; | |
701 | ||
e731d71b | 702 | if (!inet_parse_passive(target, default_port, &ss)) { |
ca286ba9 | 703 | return -EAFNOSUPPORT; |
78ff0270 | 704 | } |
e731d71b | 705 | kernel_chooses_port = ss_get_port(&ss) == 0; |
78ff0270 BP |
706 | |
707 | /* Create non-blocking socket, set SO_REUSEADDR. */ | |
e731d71b | 708 | fd = socket(ss.ss_family, style, 0); |
78ff0270 | 709 | if (fd < 0) { |
0f0b5401 GS |
710 | error = sock_errno(); |
711 | VLOG_ERR("%s: socket: %s", target, sock_strerror(error)); | |
ca286ba9 | 712 | return -error; |
78ff0270 BP |
713 | } |
714 | error = set_nonblocking(fd); | |
715 | if (error) { | |
d98fa503 | 716 | goto error; |
78ff0270 | 717 | } |
4f2eb9a7 BP |
718 | if (style == SOCK_STREAM |
719 | && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) < 0) { | |
0f0b5401 | 720 | error = sock_errno(); |
10a89ef0 | 721 | VLOG_ERR("%s: setsockopt(SO_REUSEADDR): %s", |
0f0b5401 | 722 | target, sock_strerror(error)); |
d98fa503 | 723 | goto error; |
78ff0270 BP |
724 | } |
725 | ||
726 | /* Bind. */ | |
e731d71b | 727 | if (bind(fd, (struct sockaddr *) &ss, ss_length(&ss)) < 0) { |
0f0b5401 | 728 | error = sock_errno(); |
aeada0ea BP |
729 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
730 | VLOG_ERR_RL(&rl, "%s: bind: %s", target, sock_strerror(error)); | |
d98fa503 | 731 | goto error; |
78ff0270 BP |
732 | } |
733 | ||
cea15768 EJ |
734 | /* The dscp bits must be configured before connect() to ensure that the TOS |
735 | * field is set during the connection establishment. If set after | |
736 | * connect(), the handshake SYN frames will be sent with a TOS of 0. */ | |
afc1d536 | 737 | error = set_dscp(fd, ss.ss_family, dscp); |
cea15768 | 738 | if (error) { |
909dad2b | 739 | VLOG_ERR("%s: set_dscp: %s", target, sock_strerror(error)); |
ef8a3d14 | 740 | goto error; |
f125905c MM |
741 | } |
742 | ||
78ff0270 | 743 | /* Listen. */ |
041dc07f | 744 | if (style == SOCK_STREAM && listen(fd, 10) < 0) { |
0f0b5401 GS |
745 | error = sock_errno(); |
746 | VLOG_ERR("%s: listen: %s", target, sock_strerror(error)); | |
d98fa503 | 747 | goto error; |
78ff0270 | 748 | } |
36775dad | 749 | |
e731d71b AS |
750 | if (ssp || kernel_chooses_port) { |
751 | socklen_t ss_len = sizeof ss; | |
752 | if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) < 0) { | |
0f0b5401 GS |
753 | error = sock_errno(); |
754 | VLOG_ERR("%s: getsockname: %s", target, sock_strerror(error)); | |
d98fa503 | 755 | goto error; |
36775dad | 756 | } |
b52ecd96 | 757 | if (kernel_chooses_port && kernel_print_port) { |
df451457 | 758 | VLOG_INFO("%s: listening on port %"PRIu16, |
e731d71b AS |
759 | target, ss_get_port(&ss)); |
760 | } | |
761 | if (ssp) { | |
762 | *ssp = ss; | |
df451457 | 763 | } |
36775dad BP |
764 | } |
765 | ||
d98fa503 | 766 | return fd; |
78ff0270 | 767 | |
d98fa503 | 768 | error: |
e731d71b AS |
769 | if (ssp) { |
770 | memset(ssp, 0, sizeof *ssp); | |
771 | } | |
7009a594 | 772 | closesocket(fd); |
ca286ba9 | 773 | return -error; |
78ff0270 BP |
774 | } |
775 | ||
3a974b92 BP |
776 | /* Parses 'target', which may be an IPv4 address or an IPv6 address |
777 | * enclosed in square brackets. | |
778 | * | |
779 | * On success, returns true and stores the parsed remote address into '*ss'. | |
780 | * On failure, logs an error, stores zeros into '*ss', and returns false. */ | |
781 | bool | |
782 | inet_parse_address(const char *target_, struct sockaddr_storage *ss) | |
783 | { | |
784 | char *target = xstrdup(target_); | |
0b043300 | 785 | char *host = unbracket(target); |
771680d9 | 786 | bool ok = parse_sockaddr_components(ss, host, NULL, 0, target_, false); |
3a974b92 BP |
787 | if (!ok) { |
788 | memset(ss, 0, sizeof *ss); | |
789 | } | |
790 | free(target); | |
791 | return ok; | |
792 | } | |
793 | ||
064af421 BP |
794 | int |
795 | read_fully(int fd, void *p_, size_t size, size_t *bytes_read) | |
796 | { | |
797 | uint8_t *p = p_; | |
798 | ||
799 | *bytes_read = 0; | |
800 | while (size > 0) { | |
801 | ssize_t retval = read(fd, p, size); | |
802 | if (retval > 0) { | |
803 | *bytes_read += retval; | |
804 | size -= retval; | |
805 | p += retval; | |
806 | } else if (retval == 0) { | |
807 | return EOF; | |
808 | } else if (errno != EINTR) { | |
809 | return errno; | |
810 | } | |
811 | } | |
812 | return 0; | |
813 | } | |
814 | ||
815 | int | |
816 | write_fully(int fd, const void *p_, size_t size, size_t *bytes_written) | |
817 | { | |
818 | const uint8_t *p = p_; | |
819 | ||
820 | *bytes_written = 0; | |
821 | while (size > 0) { | |
822 | ssize_t retval = write(fd, p, size); | |
823 | if (retval > 0) { | |
824 | *bytes_written += retval; | |
825 | size -= retval; | |
826 | p += retval; | |
827 | } else if (retval == 0) { | |
828 | VLOG_WARN("write returned 0"); | |
829 | return EPROTO; | |
830 | } else if (errno != EINTR) { | |
831 | return errno; | |
832 | } | |
833 | } | |
834 | return 0; | |
835 | } | |
8e71cf88 BP |
836 | |
837 | /* Given file name 'file_name', fsyncs the directory in which it is contained. | |
838 | * Returns 0 if successful, otherwise a positive errno value. */ | |
839 | int | |
840 | fsync_parent_dir(const char *file_name) | |
841 | { | |
842 | int error = 0; | |
cbf414e5 | 843 | #ifndef _WIN32 |
8e71cf88 BP |
844 | char *dir; |
845 | int fd; | |
846 | ||
847 | dir = dir_name(file_name); | |
848 | fd = open(dir, O_RDONLY); | |
849 | if (fd >= 0) { | |
850 | if (fsync(fd)) { | |
851 | if (errno == EINVAL || errno == EROFS) { | |
852 | /* This directory does not support synchronization. Not | |
853 | * really an error. */ | |
854 | } else { | |
855 | error = errno; | |
10a89ef0 | 856 | VLOG_ERR("%s: fsync failed (%s)", dir, ovs_strerror(error)); |
8e71cf88 BP |
857 | } |
858 | } | |
859 | close(fd); | |
860 | } else { | |
861 | error = errno; | |
10a89ef0 | 862 | VLOG_ERR("%s: open failed (%s)", dir, ovs_strerror(error)); |
8e71cf88 BP |
863 | } |
864 | free(dir); | |
cbf414e5 | 865 | #endif |
8e71cf88 BP |
866 | |
867 | return error; | |
868 | } | |
26efd256 BP |
869 | |
870 | /* Obtains the modification time of the file named 'file_name' to the greatest | |
871 | * supported precision. If successful, stores the mtime in '*mtime' and | |
872 | * returns 0. On error, returns a positive errno value and stores zeros in | |
873 | * '*mtime'. */ | |
874 | int | |
875 | get_mtime(const char *file_name, struct timespec *mtime) | |
876 | { | |
877 | struct stat s; | |
878 | ||
879 | if (!stat(file_name, &s)) { | |
880 | mtime->tv_sec = s.st_mtime; | |
881 | ||
882 | #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC | |
883 | mtime->tv_nsec = s.st_mtim.tv_nsec; | |
884 | #elif HAVE_STRUCT_STAT_ST_MTIMENSEC | |
885 | mtime->tv_nsec = s.st_mtimensec; | |
886 | #else | |
887 | mtime->tv_nsec = 0; | |
888 | #endif | |
889 | ||
890 | return 0; | |
891 | } else { | |
892 | mtime->tv_sec = mtime->tv_nsec = 0; | |
893 | return errno; | |
894 | } | |
895 | } | |
896 | ||
f89ffb0e | 897 | static int |
c1c19657 | 898 | getsockopt_int(int fd, int level, int option, const char *optname, int *valuep) |
f89ffb0e | 899 | { |
c1c19657 BP |
900 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 10); |
901 | socklen_t len; | |
902 | int value; | |
903 | int error; | |
f89ffb0e | 904 | |
c1c19657 BP |
905 | len = sizeof value; |
906 | if (getsockopt(fd, level, option, &value, &len)) { | |
0f0b5401 GS |
907 | error = sock_errno(); |
908 | VLOG_ERR_RL(&rl, "getsockopt(%s): %s", optname, sock_strerror(error)); | |
c1c19657 BP |
909 | } else if (len != sizeof value) { |
910 | error = EINVAL; | |
34582733 | 911 | VLOG_ERR_RL(&rl, "getsockopt(%s): value is %u bytes (expected %"PRIuSIZE")", |
c1c19657 BP |
912 | optname, (unsigned int) len, sizeof value); |
913 | } else { | |
914 | error = 0; | |
915 | } | |
916 | ||
917 | *valuep = error ? 0 : value; | |
918 | return error; | |
f89ffb0e BP |
919 | } |
920 | ||
921 | static void | |
922 | describe_sockaddr(struct ds *string, int fd, | |
923 | int (*getaddr)(int, struct sockaddr *, socklen_t *)) | |
924 | { | |
925 | struct sockaddr_storage ss; | |
926 | socklen_t len = sizeof ss; | |
927 | ||
928 | if (!getaddr(fd, (struct sockaddr *) &ss, &len)) { | |
e731d71b | 929 | if (ss.ss_family == AF_INET || ss.ss_family == AF_INET6) { |
fd245f1d BP |
930 | ss_format_address(&ss, string); |
931 | ds_put_format(string, ":%"PRIu16, ss_get_port(&ss)); | |
7ff04d92 | 932 | #ifndef _WIN32 |
f89ffb0e BP |
933 | } else if (ss.ss_family == AF_UNIX) { |
934 | struct sockaddr_un sun; | |
935 | const char *null; | |
936 | size_t maxlen; | |
937 | ||
938 | memcpy(&sun, &ss, sizeof sun); | |
939 | maxlen = len - offsetof(struct sockaddr_un, sun_path); | |
940 | null = memchr(sun.sun_path, '\0', maxlen); | |
941 | ds_put_buffer(string, sun.sun_path, | |
942 | null ? null - sun.sun_path : maxlen); | |
7ff04d92 | 943 | #endif |
f89ffb0e BP |
944 | } |
945 | #ifdef HAVE_NETLINK | |
946 | else if (ss.ss_family == AF_NETLINK) { | |
947 | int protocol; | |
948 | ||
949 | /* SO_PROTOCOL was introduced in 2.6.32. Support it regardless of the version | |
950 | * of the Linux kernel headers in use at build time. */ | |
951 | #ifndef SO_PROTOCOL | |
952 | #define SO_PROTOCOL 38 | |
953 | #endif | |
954 | ||
c1c19657 BP |
955 | if (!getsockopt_int(fd, SOL_SOCKET, SO_PROTOCOL, "SO_PROTOCOL", |
956 | &protocol)) { | |
f89ffb0e BP |
957 | switch (protocol) { |
958 | case NETLINK_ROUTE: | |
959 | ds_put_cstr(string, "NETLINK_ROUTE"); | |
960 | break; | |
961 | ||
962 | case NETLINK_GENERIC: | |
963 | ds_put_cstr(string, "NETLINK_GENERIC"); | |
964 | break; | |
965 | ||
966 | default: | |
967 | ds_put_format(string, "AF_NETLINK family %d", protocol); | |
968 | break; | |
969 | } | |
970 | } else { | |
971 | ds_put_cstr(string, "AF_NETLINK"); | |
972 | } | |
973 | } | |
974 | #endif | |
2f51a7eb | 975 | #if __linux__ |
f89ffb0e BP |
976 | else if (ss.ss_family == AF_PACKET) { |
977 | struct sockaddr_ll sll; | |
978 | ||
979 | memcpy(&sll, &ss, sizeof sll); | |
980 | ds_put_cstr(string, "AF_PACKET"); | |
981 | if (sll.sll_ifindex) { | |
982 | char name[IFNAMSIZ]; | |
983 | ||
984 | if (if_indextoname(sll.sll_ifindex, name)) { | |
985 | ds_put_format(string, "(%s)", name); | |
986 | } else { | |
987 | ds_put_format(string, "(ifindex=%d)", sll.sll_ifindex); | |
988 | } | |
989 | } | |
990 | if (sll.sll_protocol) { | |
991 | ds_put_format(string, "(protocol=0x%"PRIu16")", | |
992 | ntohs(sll.sll_protocol)); | |
993 | } | |
994 | } | |
995 | #endif | |
996 | else if (ss.ss_family == AF_UNSPEC) { | |
997 | ds_put_cstr(string, "AF_UNSPEC"); | |
998 | } else { | |
999 | ds_put_format(string, "AF_%d", (int) ss.ss_family); | |
1000 | } | |
1001 | } | |
1002 | } | |
1003 | ||
1004 | ||
2f51a7eb | 1005 | #ifdef __linux__ |
f89ffb0e BP |
1006 | static void |
1007 | put_fd_filename(struct ds *string, int fd) | |
1008 | { | |
1009 | char buf[1024]; | |
1010 | char *linkname; | |
1011 | int n; | |
1012 | ||
1013 | linkname = xasprintf("/proc/self/fd/%d", fd); | |
1014 | n = readlink(linkname, buf, sizeof buf); | |
1015 | if (n > 0) { | |
1016 | ds_put_char(string, ' '); | |
1017 | ds_put_buffer(string, buf, n); | |
1018 | if (n > sizeof buf) { | |
1019 | ds_put_cstr(string, "..."); | |
1020 | } | |
1021 | } | |
1022 | free(linkname); | |
1023 | } | |
1024 | #endif | |
1025 | ||
1026 | /* Returns a malloc()'d string describing 'fd', for use in logging. */ | |
1027 | char * | |
1028 | describe_fd(int fd) | |
1029 | { | |
1030 | struct ds string; | |
1031 | struct stat s; | |
1032 | ||
1033 | ds_init(&string); | |
54a1cfb5 | 1034 | #ifndef _WIN32 |
f89ffb0e | 1035 | if (fstat(fd, &s)) { |
10a89ef0 | 1036 | ds_put_format(&string, "fstat failed (%s)", ovs_strerror(errno)); |
f89ffb0e BP |
1037 | } else if (S_ISSOCK(s.st_mode)) { |
1038 | describe_sockaddr(&string, fd, getsockname); | |
1039 | ds_put_cstr(&string, "<->"); | |
1040 | describe_sockaddr(&string, fd, getpeername); | |
1041 | } else { | |
1042 | ds_put_cstr(&string, (isatty(fd) ? "tty" | |
1043 | : S_ISDIR(s.st_mode) ? "directory" | |
1044 | : S_ISCHR(s.st_mode) ? "character device" | |
1045 | : S_ISBLK(s.st_mode) ? "block device" | |
1046 | : S_ISREG(s.st_mode) ? "file" | |
1047 | : S_ISFIFO(s.st_mode) ? "FIFO" | |
1048 | : S_ISLNK(s.st_mode) ? "symbolic link" | |
1049 | : "unknown")); | |
2f51a7eb | 1050 | #ifdef __linux__ |
f89ffb0e BP |
1051 | put_fd_filename(&string, fd); |
1052 | #endif | |
1053 | } | |
54a1cfb5 GS |
1054 | #else |
1055 | ds_put_format(&string,"file descriptor"); | |
1056 | #endif /* _WIN32 */ | |
f89ffb0e BP |
1057 | return ds_steal_cstr(&string); |
1058 | } | |
e731d71b | 1059 | \f |
93b7faf1 BP |
1060 | /* sockaddr helpers. */ |
1061 | ||
1062 | static struct sockaddr_in * | |
1063 | sin_cast(const struct sockaddr *sa) | |
1064 | { | |
1065 | return ALIGNED_CAST(struct sockaddr_in *, sa); | |
1066 | } | |
1067 | ||
1068 | static struct sockaddr_in6 * | |
1069 | sin6_cast(const struct sockaddr *sa) | |
1070 | { | |
1071 | return ALIGNED_CAST(struct sockaddr_in6 *, sa); | |
1072 | } | |
1073 | ||
1074 | /* Returns true if 'sa' represents an IPv4 or IPv6 address, false otherwise. */ | |
1075 | bool | |
1076 | sa_is_ip(const struct sockaddr *sa) | |
1077 | { | |
1078 | return sa->sa_family == AF_INET || sa->sa_family == AF_INET6; | |
1079 | } | |
1080 | ||
1081 | /* Returns the IPv4 or IPv6 address in 'sa'. Returns IPv4 addresses as | |
1082 | * v6-mapped. */ | |
1083 | struct in6_addr | |
1084 | sa_get_address(const struct sockaddr *sa) | |
1085 | { | |
1086 | ovs_assert(sa_is_ip(sa)); | |
1087 | return (sa->sa_family == AF_INET | |
1088 | ? in6_addr_mapped_ipv4(sin_cast(sa)->sin_addr.s_addr) | |
1089 | : sin6_cast(sa)->sin6_addr); | |
1090 | } | |
259e0b1a | 1091 | |
93b7faf1 | 1092 | /* Returns the IPv4 or IPv6 port in 'sa'. */ |
e731d71b | 1093 | uint16_t |
93b7faf1 | 1094 | sa_get_port(const struct sockaddr *sa) |
e731d71b | 1095 | { |
93b7faf1 BP |
1096 | ovs_assert(sa_is_ip(sa)); |
1097 | return ntohs(sa->sa_family == AF_INET | |
1098 | ? sin_cast(sa)->sin_port | |
1099 | : sin6_cast(sa)->sin6_port); | |
e731d71b AS |
1100 | } |
1101 | ||
5d77b36b BP |
1102 | /* Returns true if 'name' is safe to include inside a network address field. |
1103 | * We want to avoid names that include confusing punctuation, etc. */ | |
1104 | static bool OVS_UNUSED | |
1105 | is_safe_name(const char *name) | |
1106 | { | |
1107 | if (!name[0] || isdigit((unsigned char) name[0])) { | |
1108 | return false; | |
1109 | } | |
1110 | for (const char *p = name; *p; p++) { | |
1111 | if (!isalnum((unsigned char) *p) && *p != '-' && *p != '_') { | |
1112 | return false; | |
1113 | } | |
1114 | } | |
1115 | return true; | |
1116 | } | |
fd245f1d | 1117 | |
51b8505b | 1118 | static void |
93b7faf1 | 1119 | sa_format_address__(const struct sockaddr *sa, |
51b8505b BP |
1120 | const char *lbrack, const char *rbrack, |
1121 | struct ds *s) | |
e731d71b | 1122 | { |
93b7faf1 BP |
1123 | ovs_assert(sa_is_ip(sa)); |
1124 | if (sa->sa_family == AF_INET) { | |
1125 | ds_put_format(s, IP_FMT, IP_ARGS(sin_cast(sa)->sin_addr.s_addr)); | |
1126 | } else { | |
1127 | const struct sockaddr_in6 *sin6 = sin6_cast(sa); | |
e731d71b | 1128 | |
51b8505b | 1129 | ds_put_cstr(s, lbrack); |
fd245f1d BP |
1130 | ds_reserve(s, s->length + INET6_ADDRSTRLEN); |
1131 | char *tail = &s->string[s->length]; | |
1132 | inet_ntop(AF_INET6, sin6->sin6_addr.s6_addr, tail, INET6_ADDRSTRLEN); | |
1133 | s->length += strlen(tail); | |
1134 | ||
5d77b36b BP |
1135 | #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID |
1136 | uint32_t scope = sin6->sin6_scope_id; | |
1137 | if (scope) { | |
1138 | char namebuf[IF_NAMESIZE]; | |
1139 | char *name = if_indextoname(scope, namebuf); | |
1140 | ds_put_char(s, '%'); | |
1141 | if (name && is_safe_name(name)) { | |
1142 | ds_put_cstr(s, name); | |
1143 | } else { | |
1144 | ds_put_format(s, "%"PRIu32, scope); | |
1145 | } | |
1146 | } | |
1147 | #endif | |
1148 | ||
51b8505b | 1149 | ds_put_cstr(s, rbrack); |
e731d71b | 1150 | } |
e731d71b AS |
1151 | } |
1152 | ||
93b7faf1 | 1153 | /* Formats the IPv4 or IPv6 address in 'sa' into 's'. If 'sa' is an IPv6 |
51b8505b BP |
1154 | * address, puts square brackets around the address. */ |
1155 | void | |
93b7faf1 | 1156 | sa_format_address(const struct sockaddr *sa, struct ds *s) |
51b8505b | 1157 | { |
93b7faf1 | 1158 | sa_format_address__(sa, "[", "]", s); |
51b8505b BP |
1159 | } |
1160 | ||
93b7faf1 | 1161 | /* Formats the IPv4 or IPv6 address in 'sa' into 's'. Does not add square |
51b8505b BP |
1162 | * brackets around IPv6 addresses. */ |
1163 | void | |
93b7faf1 | 1164 | sa_format_address_nobracks(const struct sockaddr *sa, struct ds *s) |
51b8505b | 1165 | { |
93b7faf1 | 1166 | sa_format_address__(sa, "", "", s); |
51b8505b BP |
1167 | } |
1168 | ||
e731d71b | 1169 | size_t |
93b7faf1 | 1170 | sa_length(const struct sockaddr *sa) |
e731d71b | 1171 | { |
93b7faf1 | 1172 | switch (sa->sa_family) { |
e731d71b AS |
1173 | case AF_INET: |
1174 | return sizeof(struct sockaddr_in); | |
1175 | ||
1176 | case AF_INET6: | |
1177 | return sizeof(struct sockaddr_in6); | |
1178 | ||
1179 | default: | |
1180 | OVS_NOT_REACHED(); | |
1181 | } | |
1182 | } | |
93b7faf1 BP |
1183 | \f |
1184 | /* sockaddr_storage helpers. */ | |
b26f46a4 | 1185 | |
93b7faf1 BP |
1186 | static const struct sockaddr * |
1187 | sa_cast(const struct sockaddr_storage *ss) | |
1188 | { | |
1189 | return ALIGNED_CAST(const struct sockaddr *, ss); | |
1190 | } | |
1191 | ||
1192 | bool | |
1193 | ss_is_ip(const struct sockaddr_storage *ss) | |
1194 | { | |
1195 | return sa_is_ip(sa_cast(ss)); | |
1196 | } | |
1197 | ||
1198 | uint16_t | |
1199 | ss_get_port(const struct sockaddr_storage *ss) | |
1200 | { | |
1201 | return sa_get_port(sa_cast(ss)); | |
1202 | } | |
1203 | ||
1204 | struct in6_addr | |
1205 | ss_get_address(const struct sockaddr_storage *ss) | |
1206 | { | |
1207 | return sa_get_address(sa_cast(ss)); | |
1208 | } | |
1209 | ||
1210 | void | |
1211 | ss_format_address(const struct sockaddr_storage *ss, struct ds *s) | |
1212 | { | |
1213 | sa_format_address(sa_cast(ss), s); | |
1214 | } | |
1215 | ||
1216 | void | |
1217 | ss_format_address_nobracks(const struct sockaddr_storage *ss, struct ds *s) | |
1218 | { | |
1219 | sa_format_address_nobracks(sa_cast(ss), s); | |
1220 | } | |
1221 | ||
1222 | size_t | |
1223 | ss_length(const struct sockaddr_storage *ss) | |
1224 | { | |
1225 | return sa_length(sa_cast(ss)); | |
1226 | } | |
1227 | \f | |
b26f46a4 GS |
1228 | /* For Windows socket calls, 'errno' is not set. One has to call |
1229 | * WSAGetLastError() to get the error number and then pass it to | |
315ea327 GS |
1230 | * this function to get the correct error string. |
1231 | * | |
b26f46a4 GS |
1232 | * ovs_strerror() calls strerror_r() and would not get the correct error |
1233 | * string for Windows sockets, but is good for POSIX. */ | |
1234 | const char * | |
1235 | sock_strerror(int error) | |
1236 | { | |
1237 | #ifdef _WIN32 | |
315ea327 | 1238 | return ovs_format_message(error); |
b26f46a4 GS |
1239 | #else |
1240 | return ovs_strerror(error); | |
1241 | #endif | |
1242 | } | |
8a8c1b93 | 1243 | \f |
b9b7b989 | 1244 | #ifdef __linux__ |
8a8c1b93 BP |
1245 | static int |
1246 | emulate_sendmmsg(int fd, struct mmsghdr *msgs, unsigned int n, | |
1247 | unsigned int flags) | |
1248 | { | |
1249 | for (unsigned int i = 0; i < n; i++) { | |
1250 | ssize_t retval = sendmsg(fd, &msgs[i].msg_hdr, flags); | |
1251 | if (retval < 0) { | |
1252 | return i ? i : retval; | |
1253 | } | |
1254 | msgs[i].msg_len = retval; | |
1255 | } | |
1256 | return n; | |
1257 | } | |
1258 | ||
1259 | #ifndef HAVE_SENDMMSG | |
1260 | int | |
1261 | sendmmsg(int fd, struct mmsghdr *msgs, unsigned int n, unsigned int flags) | |
1262 | { | |
1263 | return emulate_sendmmsg(fd, msgs, n, flags); | |
1264 | } | |
1265 | #else | |
00f5565c ZG |
1266 | /* sendmmsg was redefined in lib/socket-util.c, should undef sendmmsg here |
1267 | * to avoid recursion */ | |
1268 | #undef sendmmsg | |
8a8c1b93 BP |
1269 | int |
1270 | wrap_sendmmsg(int fd, struct mmsghdr *msgs, unsigned int n, unsigned int flags) | |
1271 | { | |
1272 | static bool sendmmsg_broken = false; | |
1273 | if (!sendmmsg_broken) { | |
1274 | int save_errno = errno; | |
1275 | int retval = sendmmsg(fd, msgs, n, flags); | |
1276 | if (retval >= 0 || errno != ENOSYS) { | |
1277 | return retval; | |
1278 | } | |
1279 | sendmmsg_broken = true; | |
1280 | errno = save_errno; | |
1281 | } | |
1282 | return emulate_sendmmsg(fd, msgs, n, flags); | |
1283 | } | |
1284 | #endif | |
b9b7b989 | 1285 | |
b9018984 BP |
1286 | static int |
1287 | emulate_recvmmsg(int fd, struct mmsghdr *msgs, unsigned int n, | |
1288 | int flags, struct timespec *timeout OVS_UNUSED) | |
1289 | { | |
1290 | ovs_assert(!timeout); /* XXX not emulated */ | |
1291 | ||
1292 | bool waitforone = flags & MSG_WAITFORONE; | |
1293 | flags &= ~MSG_WAITFORONE; | |
1294 | ||
1295 | for (unsigned int i = 0; i < n; i++) { | |
1296 | ssize_t retval = recvmsg(fd, &msgs[i].msg_hdr, flags); | |
1297 | if (retval < 0) { | |
1298 | return i ? i : retval; | |
1299 | } | |
1300 | msgs[i].msg_len = retval; | |
1301 | ||
1302 | if (waitforone) { | |
1303 | flags |= MSG_DONTWAIT; | |
1304 | } | |
1305 | } | |
1306 | return n; | |
1307 | } | |
1308 | ||
1309 | #ifndef HAVE_SENDMMSG | |
1310 | int | |
1311 | recvmmsg(int fd, struct mmsghdr *msgs, unsigned int n, | |
1312 | int flags, struct timespec *timeout) | |
1313 | { | |
1314 | return emulate_recvmmsg(fd, msgs, n, flags, timeout); | |
1315 | } | |
1316 | #else | |
1317 | /* recvmmsg was redefined in lib/socket-util.c, should undef recvmmsg here | |
1318 | * to avoid recursion */ | |
1319 | #undef recvmmsg | |
1320 | int | |
1321 | wrap_recvmmsg(int fd, struct mmsghdr *msgs, unsigned int n, | |
1322 | int flags, struct timespec *timeout) | |
1323 | { | |
1324 | ovs_assert(!timeout); /* XXX not emulated */ | |
1325 | ||
1326 | static bool recvmmsg_broken = false; | |
1327 | if (!recvmmsg_broken) { | |
1328 | int save_errno = errno; | |
1329 | int retval = recvmmsg(fd, msgs, n, flags, timeout); | |
1330 | if (retval >= 0 || errno != ENOSYS) { | |
1331 | return retval; | |
1332 | } | |
1333 | recvmmsg_broken = true; | |
1334 | errno = save_errno; | |
1335 | } | |
1336 | return emulate_recvmmsg(fd, msgs, n, flags, timeout); | |
1337 | } | |
1338 | #endif | |
b9b7b989 | 1339 | #endif /* __linux__ */ |