]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/socket-util.c
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "socket-util.h"
19 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
27 #include <netinet/tcp.h>
33 #include <sys/socket.h>
38 #include "openvswitch/dynamic-string.h"
39 #include "ovs-thread.h"
41 #include "openvswitch/poll-loop.h"
43 #include "openvswitch/vlog.h"
45 #include <linux/if_packet.h>
48 #include "netlink-protocol.h"
49 #include "netlink-socket.h"
51 #include "dns-resolve.h"
53 VLOG_DEFINE_THIS_MODULE(socket_util
);
55 static int getsockopt_int(int fd
, int level
, int option
, const char *optname
,
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
*);
60 static bool parse_sockaddr_components(struct sockaddr_storage
*ss
,
63 uint16_t default_port
,
67 /* Sets 'fd' to non-blocking mode. Returns 0 if successful, otherwise a
68 * positive errno value. */
70 set_nonblocking(int fd
)
73 int flags
= fcntl(fd
, F_GETFL
, 0);
75 if (fcntl(fd
, F_SETFL
, flags
| O_NONBLOCK
) != -1) {
78 VLOG_ERR("fcntl(F_SETFL) failed: %s", ovs_strerror(errno
));
82 VLOG_ERR("fcntl(F_GETFL) failed: %s", ovs_strerror(errno
));
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
));
97 xset_nonblocking(int fd
)
99 if (set_nonblocking(fd
)) {
105 setsockopt_tcp_nodelay(int fd
)
110 retval
= setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, &on
, sizeof on
);
112 retval
= sock_errno();
113 VLOG_ERR("setsockopt(TCP_NODELAY): %s", sock_strerror(retval
));
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). */
121 set_dscp(int fd
, int family
, uint8_t dscp
)
127 /* XXX: Consider using QoS2 APIs for Windows to set dscp. */
138 retval
= setsockopt(fd
, IPPROTO_IP
, IP_TOS
, &val
, sizeof val
);
142 retval
= setsockopt(fd
, IPPROTO_IPV6
, IPV6_TCLASS
, &val
, sizeof val
);
149 return retval
? sock_errno() : 0;
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. */
156 addr_is_ipv6(const char *host_name
)
158 return strchr(host_name
, ':') != NULL
;
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. */
165 lookup_ip(const char *host_name
, struct in_addr
*addr
)
167 if (!ip_parse(host_name
, &addr
->s_addr
)) {
168 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
169 VLOG_ERR_RL(&rl
, "\"%s\" is not a valid IP address", host_name
);
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. */
179 lookup_ipv6(const char *host_name
, struct in6_addr
*addr
)
181 if (!ipv6_parse(host_name
, addr
)) {
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
);
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.
193 * Most Open vSwitch code should not use this because it causes deadlocks:
194 * getaddrinfo() sends out a DNS request but that starts a new flow for which
195 * OVS must set up a flow, but it can't because it's waiting for a DNS reply.
196 * The synchronous lookup also delays other activity. (Of course we can solve
197 * this but it doesn't seem worthwhile quite yet.) */
199 lookup_hostname(const char *host_name
, struct in_addr
*addr
)
201 struct addrinfo
*result
;
202 struct addrinfo hints
;
204 if (ip_parse(host_name
, &addr
->s_addr
)) {
208 memset(&hints
, 0, sizeof hints
);
209 hints
.ai_family
= AF_INET
;
211 switch (getaddrinfo(host_name
, NULL
, &hints
, &result
)) {
213 *addr
= ALIGNED_CAST(struct sockaddr_in
*,
214 result
->ai_addr
)->sin_addr
;
215 freeaddrinfo(result
);
218 #ifdef EAI_ADDRFAMILY
239 #if defined (EAI_NODATA) && EAI_NODATA != EAI_NONAME
255 check_connection_completion(int fd
)
257 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 10);
262 pfd
.events
= POLLOUT
;
266 retval
= poll(&pfd
, 1, 0);
267 } while (retval
< 0 && errno
== EINTR
);
275 struct timeval tv
= { 0, 0 };
276 /* WSAPoll is broken on Windows, instead do a select */
277 retval
= select(0, NULL
, &wrset
, &exset
, &tv
);
279 if (FD_ISSET(fd
, &wrset
)) {
280 pfd
.revents
|= pfd
.events
;
282 if (FD_ISSET(fd
, &exset
)) {
283 pfd
.revents
|= POLLERR
;
288 if (pfd
.revents
& POLLERR
) {
289 ssize_t n
= send(fd
, "", 1, 0);
293 VLOG_ERR_RL(&rl
, "poll return POLLERR but send succeeded");
298 } else if (retval
< 0) {
299 VLOG_ERR_RL(&rl
, "poll: %s", sock_strerror(sock_errno()));
306 /* Returns the size of socket 'sock''s receive buffer (SO_RCVBUF), or a
307 * negative errno value if an error occurs. */
309 get_socket_rcvbuf(int sock
)
314 error
= getsockopt_int(sock
, SOL_SOCKET
, SO_RCVBUF
, "SO_RCVBUF", &rcvbuf
);
315 return error
? -error
: rcvbuf
;
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.)*/
322 drain_fd(int fd
, size_t n_packets
)
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. */
330 if (read(fd
, buffer
, sizeof buffer
) <= 0) {
337 guess_netmask(ovs_be32 ip_
)
339 uint32_t ip
= ntohl(ip_
);
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)); /* ??? */
352 char *end
= strchr(s
, '\0');
353 if (end
[-1] == ']') {
360 /* 'host_index' is 0 if the host precedes the port within 's', 1 otherwise. */
362 inet_parse_tokens__(char *s
, int host_index
, char **hostp
, char **portp
)
365 bool in_brackets
= false;
367 for (char *p
= s
; *p
; p
++) {
370 } else if (*p
== ']') {
372 } else if (*p
== ':' && !in_brackets
) {
378 *hostp
= *portp
= NULL
;
383 tokens
[host_index
] = hostp
;
384 tokens
[!host_index
] = portp
;
388 *tokens
[1] = unbracket(colon
+ 1);
390 *tokens
[0] = unbracket(s
);
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
397 * an empty string. Can set '*portp' to the null string.
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.
404 * Modifies 's' and points '*hostp' and '*portp' (if nonnull) into it.
407 inet_parse_host_port_tokens(char *s
, char **hostp
, char **portp
)
409 inet_parse_tokens__(s
, 0, hostp
, portp
);
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'
414 * respectively. Either '*portp' and '*hostp' (but not both) can end up null.
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.
421 * Modifies 's' and points '*hostp' and '*portp' (if nonnull) into it.
424 inet_parse_port_host_tokens(char *s
, char **portp
, char **hostp
)
426 inet_parse_tokens__(s
, 1, hostp
, portp
);
430 parse_sockaddr_components_dns(struct sockaddr_storage
*ss OVS_UNUSED
,
432 const char *port_s OVS_UNUSED
,
433 uint16_t default_port OVS_UNUSED
,
434 const char *s OVS_UNUSED
)
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);
449 parse_sockaddr_components(struct sockaddr_storage
*ss
,
451 const char *port_s
, uint16_t default_port
,
455 struct sockaddr_in
*sin
= sin_cast(sa_cast(ss
));
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
);
467 memset(ss
, 0, sizeof *ss
);
468 if (host_s
&& strchr(host_s
, ':')) {
469 struct sockaddr_in6
*sin6
= sin6_cast(sa_cast(ss
));
470 char *addr
= strsep(&host_s
, "%");
472 sin6
->sin6_family
= AF_INET6
;
473 sin6
->sin6_port
= htons(port
);
474 if (!addr
|| !*addr
|| !ipv6_parse(addr
, &sin6
->sin6_addr
)) {
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
);
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
));
494 sin
->sin_family
= AF_INET
;
495 sin
->sin_port
= htons(port
);
496 if (host_s
&& !ip_parse(host_s
, &sin
->sin_addr
.s_addr
)) {
504 if (resolve_host
&& parse_sockaddr_components_dns(ss
, host_s
, port_s
,
507 } else if (!resolve_host
) {
508 VLOG_ERR("%s: bad IP address \"%s\"", s
, host_s
);
511 memset(ss
, 0, sizeof *ss
);
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
517 * enclosed in square brackets. If 'default_port' is nonnegative then <port>
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.
522 * On success, returns true and stores the parsed remote address into '*ss'.
523 * On failure, logs an error, stores zeros into '*ss', and returns false. */
525 inet_parse_active(const char *target_
, int default_port
,
526 struct sockaddr_storage
*ss
)
528 char *target
= xstrdup(target_
);
532 inet_parse_host_port_tokens(target
, &host
, &port
);
534 VLOG_ERR("%s: host must be specified", target_
);
536 } else if (!port
&& default_port
< 0) {
537 VLOG_ERR("%s: port must be specified", target_
);
540 ok
= parse_sockaddr_components(ss
, host
, port
, default_port
,
544 memset(ss
, 0, sizeof *ss
);
551 /* Opens a non-blocking IPv4 or IPv6 socket of the specified 'style' and
552 * connects to 'target', which should be a string in the format
553 * "<host>[:<port>]". <host>, which is required, may be an IPv4 address or an
554 * IPv6 address enclosed in square brackets. If 'default_port' is nonnegative
555 * then <port> is optional and defaults to 'default_port'.
557 * 'style' should be SOCK_STREAM (for TCP) or SOCK_DGRAM (for UDP).
559 * On success, returns 0 (indicating connection complete) or EAGAIN (indicating
560 * connection in progress), in which case the new file descriptor is stored
561 * into '*fdp'. On failure, returns a positive errno value other than EAGAIN
562 * and stores -1 into '*fdp'.
564 * If 'ss' is non-null, then on success stores the target address into '*ss'.
566 * 'dscp' becomes the DSCP bits in the IP headers for the new connection. It
567 * should be in the range [0, 63] and will automatically be shifted to the
568 * appropriately place in the IP tos field. */
570 inet_open_active(int style
, const char *target
, int default_port
,
571 struct sockaddr_storage
*ssp
, int *fdp
, uint8_t dscp
)
573 struct sockaddr_storage ss
;
578 if (!inet_parse_active(target
, default_port
, &ss
)) {
579 error
= EAFNOSUPPORT
;
583 /* Create non-blocking socket. */
584 fd
= socket(ss
.ss_family
, style
, 0);
586 error
= sock_errno();
587 VLOG_ERR("%s: socket: %s", target
, sock_strerror(error
));
590 error
= set_nonblocking(fd
);
595 /* The dscp bits must be configured before connect() to ensure that the
596 * TOS field is set during the connection establishment. If set after
597 * connect(), the handshake SYN frames will be sent with a TOS of 0. */
598 error
= set_dscp(fd
, ss
.ss_family
, dscp
);
600 VLOG_ERR("%s: set_dscp: %s", target
, sock_strerror(error
));
605 error
= connect(fd
, (struct sockaddr
*) &ss
, ss_length(&ss
)) == 0
608 if (error
== EINPROGRESS
610 || error
== WSAEALREADY
|| error
== WSAEWOULDBLOCK
617 if (error
&& error
!= EAGAIN
) {
619 memset(ssp
, 0, sizeof *ssp
);
634 /* Parses 'target', which should be a string in the format "[<port>][:<host>]":
636 * - If 'default_port' is -1, then <port> is required. Otherwise, if
637 * <port> is omitted, then 'default_port' is used instead.
639 * - If <port> (or 'default_port', if used) is 0, then no port is bound
640 * and the TCP/IP stack will select a port.
642 * - <host> is optional. If supplied, it may be an IPv4 address or an
643 * IPv6 address enclosed in square brackets. If omitted, the IP address
646 * If successful, stores the address into '*ss' and returns true; otherwise
647 * zeros '*ss' and returns false. */
649 inet_parse_passive(const char *target_
, int default_port
,
650 struct sockaddr_storage
*ss
)
652 char *target
= xstrdup(target_
);
656 inet_parse_port_host_tokens(target
, &port
, &host
);
657 if (!port
&& default_port
< 0) {
658 VLOG_ERR("%s: port must be specified", target_
);
661 ok
= parse_sockaddr_components(ss
, host
, port
, default_port
,
665 memset(ss
, 0, sizeof *ss
);
672 /* Opens a non-blocking IPv4 or IPv6 socket of the specified 'style', binds to
673 * 'target', and listens for incoming connections. Parses 'target' in the same
674 * way was inet_parse_passive().
676 * 'style' should be SOCK_STREAM (for TCP) or SOCK_DGRAM (for UDP).
678 * For TCP, the socket will have SO_REUSEADDR turned on.
680 * On success, returns a non-negative file descriptor. On failure, returns a
681 * negative errno value.
683 * If 'ss' is non-null, then on success stores the bound address into '*ss'.
685 * 'dscp' becomes the DSCP bits in the IP headers for the new connection. It
686 * should be in the range [0, 63] and will automatically be shifted to the
687 * appropriately place in the IP tos field.
689 * If 'kernel_print_port' is true and the port is dynamically assigned by
690 * the kernel, print the chosen port. */
692 inet_open_passive(int style
, const char *target
, int default_port
,
693 struct sockaddr_storage
*ssp
, uint8_t dscp
,
694 bool kernel_print_port
)
696 bool kernel_chooses_port
;
697 struct sockaddr_storage ss
;
699 unsigned int yes
= 1;
701 if (!inet_parse_passive(target
, default_port
, &ss
)) {
702 return -EAFNOSUPPORT
;
704 kernel_chooses_port
= ss_get_port(&ss
) == 0;
706 /* Create non-blocking socket, set SO_REUSEADDR. */
707 fd
= socket(ss
.ss_family
, style
, 0);
709 error
= sock_errno();
710 VLOG_ERR("%s: socket: %s", target
, sock_strerror(error
));
713 error
= set_nonblocking(fd
);
717 if (style
== SOCK_STREAM
718 && setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &yes
, sizeof yes
) < 0) {
719 error
= sock_errno();
720 VLOG_ERR("%s: setsockopt(SO_REUSEADDR): %s",
721 target
, sock_strerror(error
));
726 if (bind(fd
, (struct sockaddr
*) &ss
, ss_length(&ss
)) < 0) {
727 error
= sock_errno();
728 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
729 VLOG_ERR_RL(&rl
, "%s: bind: %s", target
, sock_strerror(error
));
733 /* The dscp bits must be configured before connect() to ensure that the TOS
734 * field is set during the connection establishment. If set after
735 * connect(), the handshake SYN frames will be sent with a TOS of 0. */
736 error
= set_dscp(fd
, ss
.ss_family
, dscp
);
738 VLOG_ERR("%s: set_dscp: %s", target
, sock_strerror(error
));
743 if (style
== SOCK_STREAM
&& listen(fd
, 10) < 0) {
744 error
= sock_errno();
745 VLOG_ERR("%s: listen: %s", target
, sock_strerror(error
));
749 if (ssp
|| kernel_chooses_port
) {
750 socklen_t ss_len
= sizeof ss
;
751 if (getsockname(fd
, (struct sockaddr
*) &ss
, &ss_len
) < 0) {
752 error
= sock_errno();
753 VLOG_ERR("%s: getsockname: %s", target
, sock_strerror(error
));
756 if (kernel_chooses_port
&& kernel_print_port
) {
757 VLOG_INFO("%s: listening on port %"PRIu16
,
758 target
, ss_get_port(&ss
));
769 memset(ssp
, 0, sizeof *ssp
);
775 /* Parses 'target', which may be an IPv4 address or an IPv6 address
776 * enclosed in square brackets.
778 * On success, returns true and stores the parsed remote address into '*ss'.
779 * On failure, logs an error, stores zeros into '*ss', and returns false. */
781 inet_parse_address(const char *target_
, struct sockaddr_storage
*ss
)
783 char *target
= xstrdup(target_
);
784 char *host
= unbracket(target
);
785 bool ok
= parse_sockaddr_components(ss
, host
, NULL
, 0, target_
, false);
787 memset(ss
, 0, sizeof *ss
);
794 read_fully(int fd
, void *p_
, size_t size
, size_t *bytes_read
)
800 ssize_t retval
= read(fd
, p
, size
);
802 *bytes_read
+= retval
;
805 } else if (retval
== 0) {
807 } else if (errno
!= EINTR
) {
815 write_fully(int fd
, const void *p_
, size_t size
, size_t *bytes_written
)
817 const uint8_t *p
= p_
;
821 ssize_t retval
= write(fd
, p
, size
);
823 *bytes_written
+= retval
;
826 } else if (retval
== 0) {
827 VLOG_WARN("write returned 0");
829 } else if (errno
!= EINTR
) {
836 /* Given file name 'file_name', fsyncs the directory in which it is contained.
837 * Returns 0 if successful, otherwise a positive errno value. */
839 fsync_parent_dir(const char *file_name
)
846 dir
= dir_name(file_name
);
847 fd
= open(dir
, O_RDONLY
);
850 if (errno
== EINVAL
|| errno
== EROFS
) {
851 /* This directory does not support synchronization. Not
852 * really an error. */
855 VLOG_ERR("%s: fsync failed (%s)", dir
, ovs_strerror(error
));
861 VLOG_ERR("%s: open failed (%s)", dir
, ovs_strerror(error
));
869 /* Obtains the modification time of the file named 'file_name' to the greatest
870 * supported precision. If successful, stores the mtime in '*mtime' and
871 * returns 0. On error, returns a positive errno value and stores zeros in
874 get_mtime(const char *file_name
, struct timespec
*mtime
)
878 if (!stat(file_name
, &s
)) {
879 mtime
->tv_sec
= s
.st_mtime
;
881 #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
882 mtime
->tv_nsec
= s
.st_mtim
.tv_nsec
;
883 #elif HAVE_STRUCT_STAT_ST_MTIMENSEC
884 mtime
->tv_nsec
= s
.st_mtimensec
;
891 mtime
->tv_sec
= mtime
->tv_nsec
= 0;
897 getsockopt_int(int fd
, int level
, int option
, const char *optname
, int *valuep
)
899 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 10);
905 if (getsockopt(fd
, level
, option
, &value
, &len
)) {
906 error
= sock_errno();
907 VLOG_ERR_RL(&rl
, "getsockopt(%s): %s", optname
, sock_strerror(error
));
908 } else if (len
!= sizeof value
) {
910 VLOG_ERR_RL(&rl
, "getsockopt(%s): value is %u bytes (expected %"PRIuSIZE
")",
911 optname
, (unsigned int) len
, sizeof value
);
916 *valuep
= error
? 0 : value
;
921 describe_sockaddr(struct ds
*string
, int fd
,
922 int (*getaddr
)(int, struct sockaddr
*, socklen_t
*))
924 struct sockaddr_storage ss
;
925 socklen_t len
= sizeof ss
;
927 if (!getaddr(fd
, (struct sockaddr
*) &ss
, &len
)) {
928 if (ss
.ss_family
== AF_INET
|| ss
.ss_family
== AF_INET6
) {
929 ss_format_address(&ss
, string
);
930 ds_put_format(string
, ":%"PRIu16
, ss_get_port(&ss
));
932 } else if (ss
.ss_family
== AF_UNIX
) {
933 struct sockaddr_un sun
;
937 memcpy(&sun
, &ss
, sizeof sun
);
938 maxlen
= len
- offsetof(struct sockaddr_un
, sun_path
);
939 null
= memchr(sun
.sun_path
, '\0', maxlen
);
940 ds_put_buffer(string
, sun
.sun_path
,
941 null
? null
- sun
.sun_path
: maxlen
);
945 else if (ss
.ss_family
== AF_NETLINK
) {
948 /* SO_PROTOCOL was introduced in 2.6.32. Support it regardless of the version
949 * of the Linux kernel headers in use at build time. */
951 #define SO_PROTOCOL 38
954 if (!getsockopt_int(fd
, SOL_SOCKET
, SO_PROTOCOL
, "SO_PROTOCOL",
958 ds_put_cstr(string
, "NETLINK_ROUTE");
961 case NETLINK_GENERIC
:
962 ds_put_cstr(string
, "NETLINK_GENERIC");
966 ds_put_format(string
, "AF_NETLINK family %d", protocol
);
970 ds_put_cstr(string
, "AF_NETLINK");
975 else if (ss
.ss_family
== AF_PACKET
) {
976 struct sockaddr_ll sll
;
978 memcpy(&sll
, &ss
, sizeof sll
);
979 ds_put_cstr(string
, "AF_PACKET");
980 if (sll
.sll_ifindex
) {
983 if (if_indextoname(sll
.sll_ifindex
, name
)) {
984 ds_put_format(string
, "(%s)", name
);
986 ds_put_format(string
, "(ifindex=%d)", sll
.sll_ifindex
);
989 if (sll
.sll_protocol
) {
990 ds_put_format(string
, "(protocol=0x%"PRIu16
")",
991 ntohs(sll
.sll_protocol
));
995 else if (ss
.ss_family
== AF_UNSPEC
) {
996 ds_put_cstr(string
, "AF_UNSPEC");
998 ds_put_format(string
, "AF_%d", (int) ss
.ss_family
);
1006 put_fd_filename(struct ds
*string
, int fd
)
1012 linkname
= xasprintf("/proc/self/fd/%d", fd
);
1013 n
= readlink(linkname
, buf
, sizeof buf
);
1015 ds_put_char(string
, ' ');
1016 ds_put_buffer(string
, buf
, n
);
1017 if (n
> sizeof buf
) {
1018 ds_put_cstr(string
, "...");
1025 /* Returns a malloc()'d string describing 'fd', for use in logging. */
1034 if (fstat(fd
, &s
)) {
1035 ds_put_format(&string
, "fstat failed (%s)", ovs_strerror(errno
));
1036 } else if (S_ISSOCK(s
.st_mode
)) {
1037 describe_sockaddr(&string
, fd
, getsockname
);
1038 ds_put_cstr(&string
, "<->");
1039 describe_sockaddr(&string
, fd
, getpeername
);
1041 ds_put_cstr(&string
, (isatty(fd
) ? "tty"
1042 : S_ISDIR(s
.st_mode
) ? "directory"
1043 : S_ISCHR(s
.st_mode
) ? "character device"
1044 : S_ISBLK(s
.st_mode
) ? "block device"
1045 : S_ISREG(s
.st_mode
) ? "file"
1046 : S_ISFIFO(s
.st_mode
) ? "FIFO"
1047 : S_ISLNK(s
.st_mode
) ? "symbolic link"
1050 put_fd_filename(&string
, fd
);
1054 ds_put_format(&string
,"file descriptor");
1056 return ds_steal_cstr(&string
);
1059 /* sockaddr helpers. */
1061 static struct sockaddr_in
*
1062 sin_cast(const struct sockaddr
*sa
)
1064 return ALIGNED_CAST(struct sockaddr_in
*, sa
);
1067 static struct sockaddr_in6
*
1068 sin6_cast(const struct sockaddr
*sa
)
1070 return ALIGNED_CAST(struct sockaddr_in6
*, sa
);
1073 /* Returns true if 'sa' represents an IPv4 or IPv6 address, false otherwise. */
1075 sa_is_ip(const struct sockaddr
*sa
)
1077 return sa
->sa_family
== AF_INET
|| sa
->sa_family
== AF_INET6
;
1080 /* Returns the IPv4 or IPv6 address in 'sa'. Returns IPv4 addresses as
1083 sa_get_address(const struct sockaddr
*sa
)
1085 ovs_assert(sa_is_ip(sa
));
1086 return (sa
->sa_family
== AF_INET
1087 ? in6_addr_mapped_ipv4(sin_cast(sa
)->sin_addr
.s_addr
)
1088 : sin6_cast(sa
)->sin6_addr
);
1091 /* Returns the IPv4 or IPv6 port in 'sa'. */
1093 sa_get_port(const struct sockaddr
*sa
)
1095 ovs_assert(sa_is_ip(sa
));
1096 return ntohs(sa
->sa_family
== AF_INET
1097 ? sin_cast(sa
)->sin_port
1098 : sin6_cast(sa
)->sin6_port
);
1101 /* Returns true if 'name' is safe to include inside a network address field.
1102 * We want to avoid names that include confusing punctuation, etc. */
1103 static bool OVS_UNUSED
1104 is_safe_name(const char *name
)
1106 if (!name
[0] || isdigit((unsigned char) name
[0])) {
1109 for (const char *p
= name
; *p
; p
++) {
1110 if (!isalnum((unsigned char) *p
) && *p
!= '-' && *p
!= '_') {
1118 sa_format_address__(const struct sockaddr
*sa
,
1119 const char *lbrack
, const char *rbrack
,
1122 ovs_assert(sa_is_ip(sa
));
1123 if (sa
->sa_family
== AF_INET
) {
1124 ds_put_format(s
, IP_FMT
, IP_ARGS(sin_cast(sa
)->sin_addr
.s_addr
));
1126 const struct sockaddr_in6
*sin6
= sin6_cast(sa
);
1128 ds_put_cstr(s
, lbrack
);
1129 ds_reserve(s
, s
->length
+ INET6_ADDRSTRLEN
);
1130 char *tail
= &s
->string
[s
->length
];
1131 inet_ntop(AF_INET6
, sin6
->sin6_addr
.s6_addr
, tail
, INET6_ADDRSTRLEN
);
1132 s
->length
+= strlen(tail
);
1134 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1135 uint32_t scope
= sin6
->sin6_scope_id
;
1137 char namebuf
[IF_NAMESIZE
];
1138 char *name
= if_indextoname(scope
, namebuf
);
1139 ds_put_char(s
, '%');
1140 if (name
&& is_safe_name(name
)) {
1141 ds_put_cstr(s
, name
);
1143 ds_put_format(s
, "%"PRIu32
, scope
);
1148 ds_put_cstr(s
, rbrack
);
1152 /* Formats the IPv4 or IPv6 address in 'sa' into 's'. If 'sa' is an IPv6
1153 * address, puts square brackets around the address. */
1155 sa_format_address(const struct sockaddr
*sa
, struct ds
*s
)
1157 sa_format_address__(sa
, "[", "]", s
);
1160 /* Formats the IPv4 or IPv6 address in 'sa' into 's'. Does not add square
1161 * brackets around IPv6 addresses. */
1163 sa_format_address_nobracks(const struct sockaddr
*sa
, struct ds
*s
)
1165 sa_format_address__(sa
, "", "", s
);
1169 sa_length(const struct sockaddr
*sa
)
1171 switch (sa
->sa_family
) {
1173 return sizeof(struct sockaddr_in
);
1176 return sizeof(struct sockaddr_in6
);
1183 /* sockaddr_storage helpers. */
1185 static const struct sockaddr
*
1186 sa_cast(const struct sockaddr_storage
*ss
)
1188 return ALIGNED_CAST(const struct sockaddr
*, ss
);
1192 ss_is_ip(const struct sockaddr_storage
*ss
)
1194 return sa_is_ip(sa_cast(ss
));
1198 ss_get_port(const struct sockaddr_storage
*ss
)
1200 return sa_get_port(sa_cast(ss
));
1204 ss_get_address(const struct sockaddr_storage
*ss
)
1206 return sa_get_address(sa_cast(ss
));
1210 ss_format_address(const struct sockaddr_storage
*ss
, struct ds
*s
)
1212 sa_format_address(sa_cast(ss
), s
);
1216 ss_format_address_nobracks(const struct sockaddr_storage
*ss
, struct ds
*s
)
1218 sa_format_address_nobracks(sa_cast(ss
), s
);
1222 ss_length(const struct sockaddr_storage
*ss
)
1224 return sa_length(sa_cast(ss
));
1227 /* For Windows socket calls, 'errno' is not set. One has to call
1228 * WSAGetLastError() to get the error number and then pass it to
1229 * this function to get the correct error string.
1231 * ovs_strerror() calls strerror_r() and would not get the correct error
1232 * string for Windows sockets, but is good for POSIX. */
1234 sock_strerror(int error
)
1237 return ovs_format_message(error
);
1239 return ovs_strerror(error
);
1243 #ifndef _WIN32 //Avoid using sendmsg on Windows entirely
1245 emulate_sendmmsg(int fd
, struct mmsghdr
*msgs
, unsigned int n
,
1248 for (unsigned int i
= 0; i
< n
; i
++) {
1249 ssize_t retval
= sendmsg(fd
, &msgs
[i
].msg_hdr
, flags
);
1251 return i
? i
: retval
;
1253 msgs
[i
].msg_len
= retval
;
1258 #ifndef HAVE_SENDMMSG
1260 sendmmsg(int fd
, struct mmsghdr
*msgs
, unsigned int n
, unsigned int flags
)
1262 return emulate_sendmmsg(fd
, msgs
, n
, flags
);
1265 /* sendmmsg was redefined in lib/socket-util.c, should undef sendmmsg here
1266 * to avoid recursion */
1269 wrap_sendmmsg(int fd
, struct mmsghdr
*msgs
, unsigned int n
, unsigned int flags
)
1271 static bool sendmmsg_broken
= false;
1272 if (!sendmmsg_broken
) {
1273 int save_errno
= errno
;
1274 int retval
= sendmmsg(fd
, msgs
, n
, flags
);
1275 if (retval
>= 0 || errno
!= ENOSYS
) {
1278 sendmmsg_broken
= true;
1281 return emulate_sendmmsg(fd
, msgs
, n
, flags
);