usock: add helper waiting for socket to be ready
[project/libubox.git] / usock.c
1 /*
2 * usock - socket helper functions
3 *
4 * Copyright (C) 2010 Steven Barth <steven@midlink.org>
5 * Copyright (C) 2011-2012 Felix Fietkau <nbd@openwrt.org>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <netdb.h>
23 #include <poll.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <stdbool.h>
30 #include <stdio.h>
31
32 #include "usock.h"
33
34 static void usock_set_flags(int sock, unsigned int type)
35 {
36 if (!(type & USOCK_NOCLOEXEC))
37 fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC);
38
39 if (type & USOCK_NONBLOCK)
40 fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK);
41 }
42
43 static int usock_connect(struct sockaddr *sa, int sa_len, int family, int socktype, bool server)
44 {
45 int sock;
46
47 sock = socket(family, socktype, 0);
48 if (sock < 0)
49 return -1;
50
51 if (server) {
52 const int one = 1;
53 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
54
55 if (!bind(sock, sa, sa_len) &&
56 (socktype != SOCK_STREAM || !listen(sock, SOMAXCONN)))
57 return sock;
58 } else {
59 if (!connect(sock, sa, sa_len) || errno == EINPROGRESS)
60 return sock;
61 }
62
63 close(sock);
64 return -1;
65 }
66
67 static int usock_unix(const char *host, int socktype, bool server)
68 {
69 struct sockaddr_un sun = {.sun_family = AF_UNIX};
70
71 if (strlen(host) >= sizeof(sun.sun_path)) {
72 errno = EINVAL;
73 return -1;
74 }
75 strcpy(sun.sun_path, host);
76
77 return usock_connect((struct sockaddr*)&sun, sizeof(sun), AF_UNIX, socktype, server);
78 }
79
80 static int usock_inet(int type, const char *host, const char *service, int socktype, bool server)
81 {
82 struct addrinfo *result, *rp;
83 struct addrinfo hints = {
84 .ai_family = (type & USOCK_IPV6ONLY) ? AF_INET6 :
85 (type & USOCK_IPV4ONLY) ? AF_INET : AF_UNSPEC,
86 .ai_socktype = socktype,
87 .ai_flags = AI_ADDRCONFIG
88 | ((type & USOCK_SERVER) ? AI_PASSIVE : 0)
89 | ((type & USOCK_NUMERIC) ? AI_NUMERICHOST : 0),
90 };
91 int sock = -1;
92
93 if (getaddrinfo(host, service, &hints, &result))
94 return -1;
95
96 for (rp = result; rp != NULL; rp = rp->ai_next) {
97 sock = usock_connect(rp->ai_addr, rp->ai_addrlen, rp->ai_family, socktype, server);
98 if (sock >= 0)
99 break;
100 }
101
102 freeaddrinfo(result);
103 return sock;
104 }
105
106 const char *usock_port(int port)
107 {
108 static char buffer[sizeof("65535\0")];
109
110 if (port < 0 || port > 65535)
111 return NULL;
112
113 snprintf(buffer, sizeof(buffer), "%u", port);
114
115 return buffer;
116 }
117
118 int usock(int type, const char *host, const char *service) {
119 int socktype = ((type & 0xff) == USOCK_TCP) ? SOCK_STREAM : SOCK_DGRAM;
120 bool server = !!(type & USOCK_SERVER);
121 int sock;
122
123 if (type & USOCK_UNIX)
124 sock = usock_unix(host, socktype, server);
125 else
126 sock = usock_inet(type, host, service, socktype, server);
127
128 if (sock < 0)
129 return -1;
130
131 usock_set_flags(sock, type);
132 return sock;
133 }
134
135 int usock_wait_ready(int fd, int msecs) {
136 struct pollfd fds[1];
137 int res;
138
139 fds[0].fd = fd;
140 fds[0].events = POLLOUT;
141
142 res = poll(fds, 1, msecs);
143 if (res < 0) {
144 return errno;
145 } else if (res == 0) {
146 return -ETIMEDOUT;
147 } else {
148 int err = 0;
149 socklen_t optlen = sizeof(err);
150
151 res = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &optlen);
152 if (res)
153 return errno;
154 if (err)
155 return err;
156 }
157
158 return 0;
159 }