netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / lib / socket-util.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
3  *
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:
7  *
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.
15  */
16
17 #include <config.h>
18 #include "socket-util.h"
19 #include <arpa/inet.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <net/if.h>
23 #include <netdb.h>
24 #include <netinet/tcp.h>
25 #include <poll.h>
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/socket.h>
31 #include <sys/stat.h>
32 #include <sys/uio.h>
33 #include <sys/un.h>
34 #include <unistd.h>
35 #include "dynamic-string.h"
36 #include "ovs-thread.h"
37 #include "packets.h"
38 #include "poll-loop.h"
39 #include "util.h"
40 #include "openvswitch/vlog.h"
41 #ifdef __linux__
42 #include <linux/if_packet.h>
43 #endif
44 #ifdef HAVE_NETLINK
45 #include "netlink-protocol.h"
46 #include "netlink-socket.h"
47 #endif
48
49 VLOG_DEFINE_THIS_MODULE(socket_util);
50
51 static int getsockopt_int(int fd, int level, int option, const char *optname,
52                           int *valuep);
53
54 /* Sets 'fd' to non-blocking mode.  Returns 0 if successful, otherwise a
55  * positive errno value. */
56 int
57 set_nonblocking(int fd)
58 {
59 #ifndef _WIN32
60     int flags = fcntl(fd, F_GETFL, 0);
61     if (flags != -1) {
62         if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) != -1) {
63             return 0;
64         } else {
65             VLOG_ERR("fcntl(F_SETFL) failed: %s", ovs_strerror(errno));
66             return errno;
67         }
68     } else {
69         VLOG_ERR("fcntl(F_GETFL) failed: %s", ovs_strerror(errno));
70         return errno;
71     }
72 #else
73     unsigned long arg = 1;
74     if (ioctlsocket(fd, FIONBIO, &arg)) {
75         int error = sock_errno();
76         VLOG_ERR("set_nonblocking failed: %s", sock_strerror(error));
77         return error;
78     }
79     return 0;
80 #endif
81 }
82
83 void
84 xset_nonblocking(int fd)
85 {
86     if (set_nonblocking(fd)) {
87         exit(EXIT_FAILURE);
88     }
89 }
90
91 void
92 setsockopt_tcp_nodelay(int fd)
93 {
94     int on = 1;
95     int retval;
96
97     retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
98     if (retval) {
99         retval = sock_errno();
100         VLOG_ERR("setsockopt(TCP_NODELAY): %s", sock_strerror(retval));
101     }
102 }
103
104 /* Sets the DSCP value of socket 'fd' to 'dscp', which must be 63 or less.
105  * 'family' must indicate the socket's address family (AF_INET or AF_INET6, to
106  * do anything useful). */
107 int
108 set_dscp(int fd, int family, uint8_t dscp)
109 {
110     int retval;
111     int val;
112
113 #ifdef _WIN32
114     /* XXX: Consider using QoS2 APIs for Windows to set dscp. */
115     return 0;
116 #endif
117
118     if (dscp > 63) {
119         return EINVAL;
120     }
121     val = dscp << 2;
122
123     switch (family) {
124     case AF_INET:
125         retval = setsockopt(fd, IPPROTO_IP, IP_TOS, &val, sizeof val);
126         break;
127
128     case AF_INET6:
129         retval = setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &val, sizeof val);
130         break;
131
132     default:
133         return ENOPROTOOPT;
134     }
135
136     return retval ? sock_errno() : 0;
137 }
138
139 /* Checks whether 'host_name' is an IPv4 or IPv6 address.  It is assumed
140  * that 'host_name' is valid.  Returns false if it is IPv4 address, true if
141  * it is IPv6 address. */
142 bool
143 addr_is_ipv6(const char *host_name)
144 {
145     return strchr(host_name, ':') != NULL;
146 }
147
148 /* Translates 'host_name', which must be a string representation of an IP
149  * address, into a numeric IP address in '*addr'.  Returns 0 if successful,
150  * otherwise a positive errno value. */
151 int
152 lookup_ip(const char *host_name, struct in_addr *addr)
153 {
154     if (!ip_parse(host_name, &addr->s_addr)) {
155         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
156         VLOG_ERR_RL(&rl, "\"%s\" is not a valid IP address", host_name);
157         return ENOENT;
158     }
159     return 0;
160 }
161
162 /* Translates 'host_name', which must be a string representation of an IPv6
163  * address, into a numeric IPv6 address in '*addr'.  Returns 0 if successful,
164  * otherwise a positive errno value. */
165 int
166 lookup_ipv6(const char *host_name, struct in6_addr *addr)
167 {
168     if (!ipv6_parse(host_name, addr)) {
169         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
170         VLOG_ERR_RL(&rl, "\"%s\" is not a valid IPv6 address", host_name);
171         return ENOENT;
172     }
173     return 0;
174 }
175
176 /* Translates 'host_name', which must be a host name or a string representation
177  * of an IP address, into a numeric IP address in '*addr'.  Returns 0 if
178  * successful, otherwise a positive errno value.
179  *
180  * Most Open vSwitch code should not use this because it causes deadlocks:
181  * getaddrinfo() sends out a DNS request but that starts a new flow for which
182  * OVS must set up a flow, but it can't because it's waiting for a DNS reply.
183  * The synchronous lookup also delays other activity.  (Of course we can solve
184  * this but it doesn't seem worthwhile quite yet.)  */
185 int
186 lookup_hostname(const char *host_name, struct in_addr *addr)
187 {
188     struct addrinfo *result;
189     struct addrinfo hints;
190
191     if (ip_parse(host_name, &addr->s_addr)) {
192         return 0;
193     }
194
195     memset(&hints, 0, sizeof hints);
196     hints.ai_family = AF_INET;
197
198     switch (getaddrinfo(host_name, NULL, &hints, &result)) {
199     case 0:
200         *addr = ALIGNED_CAST(struct sockaddr_in *,
201                              result->ai_addr)->sin_addr;
202         freeaddrinfo(result);
203         return 0;
204
205 #ifdef EAI_ADDRFAMILY
206     case EAI_ADDRFAMILY:
207 #endif
208     case EAI_NONAME:
209     case EAI_SERVICE:
210         return ENOENT;
211
212     case EAI_AGAIN:
213         return EAGAIN;
214
215     case EAI_BADFLAGS:
216     case EAI_FAMILY:
217     case EAI_SOCKTYPE:
218         return EINVAL;
219
220     case EAI_FAIL:
221         return EIO;
222
223     case EAI_MEMORY:
224         return ENOMEM;
225
226 #if defined (EAI_NODATA) && EAI_NODATA != EAI_NONAME
227     case EAI_NODATA:
228         return ENXIO;
229 #endif
230
231 #ifdef EAI_SYSTEM
232     case EAI_SYSTEM:
233         return sock_errno();
234 #endif
235
236     default:
237         return EPROTO;
238     }
239 }
240
241 int
242 check_connection_completion(int fd)
243 {
244     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 10);
245     struct pollfd pfd;
246     int retval;
247
248     pfd.fd = fd;
249     pfd.events = POLLOUT;
250
251 #ifndef _WIN32
252     do {
253         retval = poll(&pfd, 1, 0);
254     } while (retval < 0 && errno == EINTR);
255 #else
256     retval = WSAPoll(&pfd, 1, 0);
257 #endif
258     if (retval == 1) {
259         if (pfd.revents & POLLERR) {
260             ssize_t n = send(fd, "", 1, 0);
261             if (n < 0) {
262                 return sock_errno();
263             } else {
264                 VLOG_ERR_RL(&rl, "poll return POLLERR but send succeeded");
265                 return EPROTO;
266             }
267         }
268         return 0;
269     } else if (retval < 0) {
270         VLOG_ERR_RL(&rl, "poll: %s", sock_strerror(sock_errno()));
271         return errno;
272     } else {
273         return EAGAIN;
274     }
275 }
276
277 /* Returns the size of socket 'sock''s receive buffer (SO_RCVBUF), or a
278  * negative errno value if an error occurs. */
279 int
280 get_socket_rcvbuf(int sock)
281 {
282     int rcvbuf;
283     int error;
284
285     error = getsockopt_int(sock, SOL_SOCKET, SO_RCVBUF, "SO_RCVBUF", &rcvbuf);
286     return error ? -error : rcvbuf;
287 }
288
289 /* Reads and discards up to 'n' datagrams from 'fd', stopping as soon as no
290  * more data can be immediately read.  ('fd' should therefore be in
291  * non-blocking mode.)*/
292 void
293 drain_fd(int fd, size_t n_packets)
294 {
295     for (; n_packets > 0; n_packets--) {
296         /* 'buffer' only needs to be 1 byte long in most circumstances.  This
297          * size is defensive against the possibility that we someday want to
298          * use a Linux tap device without TUN_NO_PI, in which case a buffer
299          * smaller than sizeof(struct tun_pi) will give EINVAL on read. */
300         char buffer[128];
301         if (read(fd, buffer, sizeof buffer) <= 0) {
302             break;
303         }
304     }
305 }
306
307 ovs_be32
308 guess_netmask(ovs_be32 ip_)
309 {
310     uint32_t ip = ntohl(ip_);
311     return ((ip >> 31) == 0 ? htonl(0xff000000)   /* Class A */
312             : (ip >> 30) == 2 ? htonl(0xffff0000) /* Class B */
313             : (ip >> 29) == 6 ? htonl(0xffffff00) /* Class C */
314             : htonl(0));                          /* ??? */
315 }
316
317 /* This is like strsep() except:
318  *
319  *    - The separator string is ":".
320  *
321  *    - Square brackets [] quote ":" separators and are removed from the
322  *      tokens. */
323 static char *
324 parse_bracketed_token(char **pp)
325 {
326     char *p = *pp;
327
328     if (p == NULL) {
329         return NULL;
330     } else if (*p == '\0') {
331         *pp = NULL;
332         return p;
333     } else if (*p == '[') {
334         char *start = p + 1;
335         char *end = start + strcspn(start, "]");
336         *pp = (*end == '\0' ? NULL
337                : end[1] == ':' ? end + 2
338                : end + 1);
339         *end = '\0';
340         return start;
341     } else {
342         char *start = p;
343         char *end = start + strcspn(start, ":");
344         *pp = *end == '\0' ? NULL : end + 1;
345         *end = '\0';
346         return start;
347     }
348 }
349
350 static bool
351 parse_sockaddr_components(struct sockaddr_storage *ss,
352                           const char *host_s,
353                           const char *port_s, uint16_t default_port,
354                           const char *s)
355 {
356     struct sockaddr_in *sin = ALIGNED_CAST(struct sockaddr_in *, ss);
357     int port;
358
359     if (port_s && port_s[0]) {
360         if (!str_to_int(port_s, 10, &port) || port < 0 || port > 65535) {
361             VLOG_ERR("%s: bad port number \"%s\"", s, port_s);
362         }
363     } else {
364         port = default_port;
365     }
366
367     memset(ss, 0, sizeof *ss);
368     if (strchr(host_s, ':')) {
369         struct sockaddr_in6 *sin6
370             = ALIGNED_CAST(struct sockaddr_in6 *, ss);
371
372         sin6->sin6_family = AF_INET6;
373         sin6->sin6_port = htons(port);
374         if (!ipv6_parse(host_s, &sin6->sin6_addr)) {
375             VLOG_ERR("%s: bad IPv6 address \"%s\"", s, host_s);
376             goto exit;
377         }
378     } else {
379         sin->sin_family = AF_INET;
380         sin->sin_port = htons(port);
381         if (!ip_parse(host_s, &sin->sin_addr.s_addr)) {
382             VLOG_ERR("%s: bad IPv4 address \"%s\"", s, host_s);
383             goto exit;
384         }
385     }
386
387     return true;
388
389 exit:
390     memset(ss, 0, sizeof *ss);
391     return false;
392 }
393
394 /* Parses 'target', which should be a string in the format "<host>[:<port>]".
395  * <host>, which is required, may be an IPv4 address or an IPv6 address
396  * enclosed in square brackets.  If 'default_port' is nonzero then <port> is
397  * optional and defaults to 'default_port'.
398  *
399  * On success, returns true and stores the parsed remote address into '*ss'.
400  * On failure, logs an error, stores zeros into '*ss', and returns false. */
401 bool
402 inet_parse_active(const char *target_, uint16_t default_port,
403                   struct sockaddr_storage *ss)
404 {
405     char *target = xstrdup(target_);
406     const char *port;
407     const char *host;
408     char *p;
409     bool ok;
410
411     p = target;
412     host = parse_bracketed_token(&p);
413     port = parse_bracketed_token(&p);
414     if (!host) {
415         VLOG_ERR("%s: host must be specified", target_);
416         ok = false;
417     } else if (!port && !default_port) {
418         VLOG_ERR("%s: port must be specified", target_);
419         ok = false;
420     } else {
421         ok = parse_sockaddr_components(ss, host, port, default_port, target_);
422     }
423     if (!ok) {
424         memset(ss, 0, sizeof *ss);
425     }
426     free(target);
427     return ok;
428 }
429
430
431 /* Opens a non-blocking IPv4 or IPv6 socket of the specified 'style' and
432  * connects to 'target', which should be a string in the format
433  * "<host>[:<port>]".  <host>, which is required, may be an IPv4 address or an
434  * IPv6 address enclosed in square brackets.  If 'default_port' is nonzero then
435  * <port> is optional and defaults to 'default_port'.
436  *
437  * 'style' should be SOCK_STREAM (for TCP) or SOCK_DGRAM (for UDP).
438  *
439  * On success, returns 0 (indicating connection complete) or EAGAIN (indicating
440  * connection in progress), in which case the new file descriptor is stored
441  * into '*fdp'.  On failure, returns a positive errno value other than EAGAIN
442  * and stores -1 into '*fdp'.
443  *
444  * If 'ss' is non-null, then on success stores the target address into '*ss'.
445  *
446  * 'dscp' becomes the DSCP bits in the IP headers for the new connection.  It
447  * should be in the range [0, 63] and will automatically be shifted to the
448  * appropriately place in the IP tos field. */
449 int
450 inet_open_active(int style, const char *target, uint16_t default_port,
451                  struct sockaddr_storage *ssp, int *fdp, uint8_t dscp)
452 {
453     struct sockaddr_storage ss;
454     int fd = -1;
455     int error;
456
457     /* Parse. */
458     if (!inet_parse_active(target, default_port, &ss)) {
459         error = EAFNOSUPPORT;
460         goto exit;
461     }
462
463     /* Create non-blocking socket. */
464     fd = socket(ss.ss_family, style, 0);
465     if (fd < 0) {
466         error = sock_errno();
467         VLOG_ERR("%s: socket: %s", target, sock_strerror(error));
468         goto exit;
469     }
470     error = set_nonblocking(fd);
471     if (error) {
472         goto exit;
473     }
474
475     /* The dscp bits must be configured before connect() to ensure that the
476      * TOS field is set during the connection establishment.  If set after
477      * connect(), the handshake SYN frames will be sent with a TOS of 0. */
478     error = set_dscp(fd, ss.ss_family, dscp);
479     if (error) {
480         VLOG_ERR("%s: set_dscp: %s", target, sock_strerror(error));
481         goto exit;
482     }
483
484     /* Connect. */
485     error = connect(fd, (struct sockaddr *) &ss, ss_length(&ss)) == 0
486                     ? 0
487                     : sock_errno();
488     if (error == EINPROGRESS
489 #ifdef _WIN32
490         || error == WSAEALREADY || error == WSAEWOULDBLOCK
491 #endif
492         ) {
493         error = EAGAIN;
494     }
495
496 exit:
497     if (error && error != EAGAIN) {
498         if (ssp) {
499             memset(ssp, 0, sizeof *ssp);
500         }
501         if (fd >= 0) {
502             closesocket(fd);
503             fd = -1;
504         }
505     } else {
506         if (ssp) {
507             *ssp = ss;
508         }
509     }
510     *fdp = fd;
511     return error;
512 }
513
514 /* Parses 'target', which should be a string in the format "[<port>][:<host>]":
515  *
516  *      - If 'default_port' is -1, then <port> is required.  Otherwise, if
517  *        <port> is omitted, then 'default_port' is used instead.
518  *
519  *      - If <port> (or 'default_port', if used) is 0, then no port is bound
520  *        and the TCP/IP stack will select a port.
521  *
522  *      - <host> is optional.  If supplied, it may be an IPv4 address or an
523  *        IPv6 address enclosed in square brackets.  If omitted, the IP address
524  *        is wildcarded.
525  *
526  * If successful, stores the address into '*ss' and returns true; otherwise
527  * zeros '*ss' and returns false. */
528 bool
529 inet_parse_passive(const char *target_, int default_port,
530                    struct sockaddr_storage *ss)
531 {
532     char *target = xstrdup(target_);
533     const char *port;
534     const char *host;
535     char *p;
536     bool ok;
537
538     p = target;
539     port = parse_bracketed_token(&p);
540     host = parse_bracketed_token(&p);
541     if (!port && default_port < 0) {
542         VLOG_ERR("%s: port must be specified", target_);
543         ok = false;
544     } else {
545         ok = parse_sockaddr_components(ss, host ? host : "0.0.0.0",
546                                        port, default_port, target_);
547     }
548     if (!ok) {
549         memset(ss, 0, sizeof *ss);
550     }
551     free(target);
552     return ok;
553 }
554
555
556 /* Opens a non-blocking IPv4 or IPv6 socket of the specified 'style', binds to
557  * 'target', and listens for incoming connections.  Parses 'target' in the same
558  * way was inet_parse_passive().
559  *
560  * 'style' should be SOCK_STREAM (for TCP) or SOCK_DGRAM (for UDP).
561  *
562  * For TCP, the socket will have SO_REUSEADDR turned on.
563  *
564  * On success, returns a non-negative file descriptor.  On failure, returns a
565  * negative errno value.
566  *
567  * If 'ss' is non-null, then on success stores the bound address into '*ss'.
568  *
569  * 'dscp' becomes the DSCP bits in the IP headers for the new connection.  It
570  * should be in the range [0, 63] and will automatically be shifted to the
571  * appropriately place in the IP tos field.
572  *
573  * If 'kernel_print_port' is true and the port is dynamically assigned by
574  * the kernel, print the chosen port. */
575 int
576 inet_open_passive(int style, const char *target, int default_port,
577                   struct sockaddr_storage *ssp, uint8_t dscp,
578                   bool kernel_print_port)
579 {
580     bool kernel_chooses_port;
581     struct sockaddr_storage ss;
582     int fd = 0, error;
583     unsigned int yes = 1;
584
585     if (!inet_parse_passive(target, default_port, &ss)) {
586         return -EAFNOSUPPORT;
587     }
588     kernel_chooses_port = ss_get_port(&ss) == 0;
589
590     /* Create non-blocking socket, set SO_REUSEADDR. */
591     fd = socket(ss.ss_family, style, 0);
592     if (fd < 0) {
593         error = sock_errno();
594         VLOG_ERR("%s: socket: %s", target, sock_strerror(error));
595         return -error;
596     }
597     error = set_nonblocking(fd);
598     if (error) {
599         goto error;
600     }
601     if (style == SOCK_STREAM
602         && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) < 0) {
603         error = sock_errno();
604         VLOG_ERR("%s: setsockopt(SO_REUSEADDR): %s",
605                  target, sock_strerror(error));
606         goto error;
607     }
608
609     /* Bind. */
610     if (bind(fd, (struct sockaddr *) &ss, ss_length(&ss)) < 0) {
611         error = sock_errno();
612         VLOG_ERR("%s: bind: %s", target, sock_strerror(error));
613         goto error;
614     }
615
616     /* The dscp bits must be configured before connect() to ensure that the TOS
617      * field is set during the connection establishment.  If set after
618      * connect(), the handshake SYN frames will be sent with a TOS of 0. */
619     error = set_dscp(fd, ss.ss_family, dscp);
620     if (error) {
621         VLOG_ERR("%s: set_dscp: %s", target, sock_strerror(error));
622         goto error;
623     }
624
625     /* Listen. */
626     if (style == SOCK_STREAM && listen(fd, 10) < 0) {
627         error = sock_errno();
628         VLOG_ERR("%s: listen: %s", target, sock_strerror(error));
629         goto error;
630     }
631
632     if (ssp || kernel_chooses_port) {
633         socklen_t ss_len = sizeof ss;
634         if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) < 0) {
635             error = sock_errno();
636             VLOG_ERR("%s: getsockname: %s", target, sock_strerror(error));
637             goto error;
638         }
639         if (kernel_chooses_port && kernel_print_port) {
640             VLOG_INFO("%s: listening on port %"PRIu16,
641                       target, ss_get_port(&ss));
642         }
643         if (ssp) {
644             *ssp = ss;
645         }
646     }
647
648     return fd;
649
650 error:
651     if (ssp) {
652         memset(ssp, 0, sizeof *ssp);
653     }
654     closesocket(fd);
655     return -error;
656 }
657
658 int
659 read_fully(int fd, void *p_, size_t size, size_t *bytes_read)
660 {
661     uint8_t *p = p_;
662
663     *bytes_read = 0;
664     while (size > 0) {
665         ssize_t retval = read(fd, p, size);
666         if (retval > 0) {
667             *bytes_read += retval;
668             size -= retval;
669             p += retval;
670         } else if (retval == 0) {
671             return EOF;
672         } else if (errno != EINTR) {
673             return errno;
674         }
675     }
676     return 0;
677 }
678
679 int
680 write_fully(int fd, const void *p_, size_t size, size_t *bytes_written)
681 {
682     const uint8_t *p = p_;
683
684     *bytes_written = 0;
685     while (size > 0) {
686         ssize_t retval = write(fd, p, size);
687         if (retval > 0) {
688             *bytes_written += retval;
689             size -= retval;
690             p += retval;
691         } else if (retval == 0) {
692             VLOG_WARN("write returned 0");
693             return EPROTO;
694         } else if (errno != EINTR) {
695             return errno;
696         }
697     }
698     return 0;
699 }
700
701 /* Given file name 'file_name', fsyncs the directory in which it is contained.
702  * Returns 0 if successful, otherwise a positive errno value. */
703 int
704 fsync_parent_dir(const char *file_name)
705 {
706     int error = 0;
707 #ifndef _WIN32
708     char *dir;
709     int fd;
710
711     dir = dir_name(file_name);
712     fd = open(dir, O_RDONLY);
713     if (fd >= 0) {
714         if (fsync(fd)) {
715             if (errno == EINVAL || errno == EROFS) {
716                 /* This directory does not support synchronization.  Not
717                  * really an error. */
718             } else {
719                 error = errno;
720                 VLOG_ERR("%s: fsync failed (%s)", dir, ovs_strerror(error));
721             }
722         }
723         close(fd);
724     } else {
725         error = errno;
726         VLOG_ERR("%s: open failed (%s)", dir, ovs_strerror(error));
727     }
728     free(dir);
729 #endif
730
731     return error;
732 }
733
734 /* Obtains the modification time of the file named 'file_name' to the greatest
735  * supported precision.  If successful, stores the mtime in '*mtime' and
736  * returns 0.  On error, returns a positive errno value and stores zeros in
737  * '*mtime'. */
738 int
739 get_mtime(const char *file_name, struct timespec *mtime)
740 {
741     struct stat s;
742
743     if (!stat(file_name, &s)) {
744         mtime->tv_sec = s.st_mtime;
745
746 #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
747         mtime->tv_nsec = s.st_mtim.tv_nsec;
748 #elif HAVE_STRUCT_STAT_ST_MTIMENSEC
749         mtime->tv_nsec = s.st_mtimensec;
750 #else
751         mtime->tv_nsec = 0;
752 #endif
753
754         return 0;
755     } else {
756         mtime->tv_sec = mtime->tv_nsec = 0;
757         return errno;
758     }
759 }
760
761 static int
762 getsockopt_int(int fd, int level, int option, const char *optname, int *valuep)
763 {
764     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 10);
765     socklen_t len;
766     int value;
767     int error;
768
769     len = sizeof value;
770     if (getsockopt(fd, level, option, &value, &len)) {
771         error = sock_errno();
772         VLOG_ERR_RL(&rl, "getsockopt(%s): %s", optname, sock_strerror(error));
773     } else if (len != sizeof value) {
774         error = EINVAL;
775         VLOG_ERR_RL(&rl, "getsockopt(%s): value is %u bytes (expected %"PRIuSIZE")",
776                     optname, (unsigned int) len, sizeof value);
777     } else {
778         error = 0;
779     }
780
781     *valuep = error ? 0 : value;
782     return error;
783 }
784
785 static void
786 describe_sockaddr(struct ds *string, int fd,
787                   int (*getaddr)(int, struct sockaddr *, socklen_t *))
788 {
789     struct sockaddr_storage ss;
790     socklen_t len = sizeof ss;
791
792     if (!getaddr(fd, (struct sockaddr *) &ss, &len)) {
793         if (ss.ss_family == AF_INET || ss.ss_family == AF_INET6) {
794             char addrbuf[SS_NTOP_BUFSIZE];
795
796             ds_put_format(string, "%s:%"PRIu16,
797                           ss_format_address(&ss, addrbuf, sizeof addrbuf),
798                           ss_get_port(&ss));
799 #ifndef _WIN32
800         } else if (ss.ss_family == AF_UNIX) {
801             struct sockaddr_un sun;
802             const char *null;
803             size_t maxlen;
804
805             memcpy(&sun, &ss, sizeof sun);
806             maxlen = len - offsetof(struct sockaddr_un, sun_path);
807             null = memchr(sun.sun_path, '\0', maxlen);
808             ds_put_buffer(string, sun.sun_path,
809                           null ? null - sun.sun_path : maxlen);
810 #endif
811         }
812 #ifdef HAVE_NETLINK
813         else if (ss.ss_family == AF_NETLINK) {
814             int protocol;
815
816 /* SO_PROTOCOL was introduced in 2.6.32.  Support it regardless of the version
817  * of the Linux kernel headers in use at build time. */
818 #ifndef SO_PROTOCOL
819 #define SO_PROTOCOL 38
820 #endif
821
822             if (!getsockopt_int(fd, SOL_SOCKET, SO_PROTOCOL, "SO_PROTOCOL",
823                                 &protocol)) {
824                 switch (protocol) {
825                 case NETLINK_ROUTE:
826                     ds_put_cstr(string, "NETLINK_ROUTE");
827                     break;
828
829                 case NETLINK_GENERIC:
830                     ds_put_cstr(string, "NETLINK_GENERIC");
831                     break;
832
833                 default:
834                     ds_put_format(string, "AF_NETLINK family %d", protocol);
835                     break;
836                 }
837             } else {
838                 ds_put_cstr(string, "AF_NETLINK");
839             }
840         }
841 #endif
842 #if __linux__
843         else if (ss.ss_family == AF_PACKET) {
844             struct sockaddr_ll sll;
845
846             memcpy(&sll, &ss, sizeof sll);
847             ds_put_cstr(string, "AF_PACKET");
848             if (sll.sll_ifindex) {
849                 char name[IFNAMSIZ];
850
851                 if (if_indextoname(sll.sll_ifindex, name)) {
852                     ds_put_format(string, "(%s)", name);
853                 } else {
854                     ds_put_format(string, "(ifindex=%d)", sll.sll_ifindex);
855                 }
856             }
857             if (sll.sll_protocol) {
858                 ds_put_format(string, "(protocol=0x%"PRIu16")",
859                               ntohs(sll.sll_protocol));
860             }
861         }
862 #endif
863         else if (ss.ss_family == AF_UNSPEC) {
864             ds_put_cstr(string, "AF_UNSPEC");
865         } else {
866             ds_put_format(string, "AF_%d", (int) ss.ss_family);
867         }
868     }
869 }
870
871
872 #ifdef __linux__
873 static void
874 put_fd_filename(struct ds *string, int fd)
875 {
876     char buf[1024];
877     char *linkname;
878     int n;
879
880     linkname = xasprintf("/proc/self/fd/%d", fd);
881     n = readlink(linkname, buf, sizeof buf);
882     if (n > 0) {
883         ds_put_char(string, ' ');
884         ds_put_buffer(string, buf, n);
885         if (n > sizeof buf) {
886             ds_put_cstr(string, "...");
887         }
888     }
889     free(linkname);
890 }
891 #endif
892
893 /* Returns a malloc()'d string describing 'fd', for use in logging. */
894 char *
895 describe_fd(int fd)
896 {
897     struct ds string;
898     struct stat s;
899
900     ds_init(&string);
901 #ifndef _WIN32
902     if (fstat(fd, &s)) {
903         ds_put_format(&string, "fstat failed (%s)", ovs_strerror(errno));
904     } else if (S_ISSOCK(s.st_mode)) {
905         describe_sockaddr(&string, fd, getsockname);
906         ds_put_cstr(&string, "<->");
907         describe_sockaddr(&string, fd, getpeername);
908     } else {
909         ds_put_cstr(&string, (isatty(fd) ? "tty"
910                               : S_ISDIR(s.st_mode) ? "directory"
911                               : S_ISCHR(s.st_mode) ? "character device"
912                               : S_ISBLK(s.st_mode) ? "block device"
913                               : S_ISREG(s.st_mode) ? "file"
914                               : S_ISFIFO(s.st_mode) ? "FIFO"
915                               : S_ISLNK(s.st_mode) ? "symbolic link"
916                               : "unknown"));
917 #ifdef __linux__
918         put_fd_filename(&string, fd);
919 #endif
920     }
921 #else
922     ds_put_format(&string,"file descriptor");
923 #endif /* _WIN32 */
924     return ds_steal_cstr(&string);
925 }
926
927 \f
928 /* sockaddr_storage helpers. */
929
930 /* Returns the IPv4 or IPv6 port in 'ss'. */
931 uint16_t
932 ss_get_port(const struct sockaddr_storage *ss)
933 {
934     if (ss->ss_family == AF_INET) {
935         const struct sockaddr_in *sin
936             = ALIGNED_CAST(const struct sockaddr_in *, ss);
937         return ntohs(sin->sin_port);
938     } else if (ss->ss_family == AF_INET6) {
939         const struct sockaddr_in6 *sin6
940             = ALIGNED_CAST(const struct sockaddr_in6 *, ss);
941         return ntohs(sin6->sin6_port);
942     } else {
943         OVS_NOT_REACHED();
944     }
945 }
946
947 /* Formats the IPv4 or IPv6 address in 'ss' into the 'bufsize' bytes in 'buf'.
948  * If 'ss' is an IPv6 address, puts square brackets around the address.
949  * 'bufsize' should be at least SS_NTOP_BUFSIZE.
950  *
951  * Returns 'buf'. */
952 char *
953 ss_format_address(const struct sockaddr_storage *ss,
954                   char *buf, size_t bufsize)
955 {
956     ovs_assert(bufsize >= SS_NTOP_BUFSIZE);
957     if (ss->ss_family == AF_INET) {
958         const struct sockaddr_in *sin
959             = ALIGNED_CAST(const struct sockaddr_in *, ss);
960
961         snprintf(buf, bufsize, IP_FMT, IP_ARGS(sin->sin_addr.s_addr));
962     } else if (ss->ss_family == AF_INET6) {
963         const struct sockaddr_in6 *sin6
964             = ALIGNED_CAST(const struct sockaddr_in6 *, ss);
965
966         buf[0] = '[';
967         inet_ntop(AF_INET6, sin6->sin6_addr.s6_addr, buf + 1, bufsize - 1);
968         strcpy(strchr(buf, '\0'), "]");
969     } else {
970         OVS_NOT_REACHED();
971     }
972
973     return buf;
974 }
975
976 size_t
977 ss_length(const struct sockaddr_storage *ss)
978 {
979     switch (ss->ss_family) {
980     case AF_INET:
981         return sizeof(struct sockaddr_in);
982
983     case AF_INET6:
984         return sizeof(struct sockaddr_in6);
985
986     default:
987         OVS_NOT_REACHED();
988     }
989 }
990
991 /* For Windows socket calls, 'errno' is not set.  One has to call
992  * WSAGetLastError() to get the error number and then pass it to
993  * this function to get the correct error string.
994  *
995  * ovs_strerror() calls strerror_r() and would not get the correct error
996  * string for Windows sockets, but is good for POSIX. */
997 const char *
998 sock_strerror(int error)
999 {
1000 #ifdef _WIN32
1001     return ovs_format_message(error);
1002 #else
1003     return ovs_strerror(error);
1004 #endif
1005 }