Add the ability to connect to a vconn asynchronously.
[openvswitch] / lib / socket-util.c
1 /* Copyright (C) 2007 Board of Trustees, Leland Stanford Jr. University.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21
22 #include "socket-util.h"
23 #include <arpa/inet.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <netdb.h>
27 #include <poll.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include "vlog.h"
32 #define THIS_MODULE VLM_socket_util
33
34 /* Sets 'fd' to non-blocking mode.  Returns 0 if successful, otherwise a
35  * positive errno value. */
36 int
37 set_nonblocking(int fd)
38 {
39     int flags = fcntl(fd, F_GETFL, 0);
40     if (flags != -1) {
41         if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) != -1) {
42             return 0;
43         } else {
44             VLOG_ERR("fcntl(F_SETFL) failed: %s", strerror(errno));
45             return errno;
46         }
47     } else {
48         VLOG_ERR("fcntl(F_GETFL) failed: %s", strerror(errno));
49         return errno;
50     }
51 }
52
53 /* Translates 'host_name', which may be a DNS name or an IP address, into a
54  * numeric IP address in '*addr'.  Returns 0 if successful, otherwise a
55  * positive errno value. */
56 int
57 lookup_ip(const char *host_name, struct in_addr *addr) 
58 {
59     if (!inet_aton(host_name, addr)) {
60         struct hostent *he = gethostbyname(host_name);
61         if (he == NULL) {
62             VLOG_ERR("gethostbyname(%s): %s", host_name,
63                      (h_errno == HOST_NOT_FOUND ? "host not found"
64                       : h_errno == TRY_AGAIN ? "try again"
65                       : h_errno == NO_RECOVERY ? "non-recoverable error"
66                       : h_errno == NO_ADDRESS ? "no address"
67                       : "unknown error"));
68             return ENOENT;
69         }
70         addr->s_addr = *(uint32_t *) he->h_addr;
71     }
72     return 0;
73 }
74
75 /* Returns the error condition associated with socket 'fd' and resets the
76  * socket's error status. */
77 int
78 get_socket_error(int fd) 
79 {
80     int error;
81     socklen_t len = sizeof(error);
82     if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
83         error = errno;
84         VLOG_ERR("getsockopt(SO_ERROR): %s", strerror(error));
85     }
86     return error;
87 }
88
89 int
90 check_connection_completion(int fd) 
91 {
92     struct pollfd pfd;
93     int retval;
94
95     pfd.fd = fd;
96     pfd.events = POLLOUT;
97     do {
98         retval = poll(&pfd, 1, 0);
99     } while (retval < 0 && errno == EINTR);
100     if (retval == 1) {
101         return get_socket_error(fd);
102     } else if (retval < 0) {
103         VLOG_ERR("poll: %s", strerror(errno));
104         return errno;
105     } else {
106         return EAGAIN;
107     }
108 }