jshn: properly support JSON "null" type
[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 #include <poll.h>
32
33 #include "usock.h"
34 #include "utils.h"
35
36 static void usock_set_flags(int sock, unsigned int type)
37 {
38 if (!(type & USOCK_NOCLOEXEC))
39 fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC);
40
41 if (type & USOCK_NONBLOCK)
42 fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK);
43 }
44
45 static int usock_connect(int type, struct sockaddr *sa, int sa_len, int family, int socktype, bool server)
46 {
47 int sock;
48
49 sock = socket(family, socktype, 0);
50 if (sock < 0)
51 return -1;
52
53 usock_set_flags(sock, type);
54
55 if (server) {
56 const int one = 1;
57 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
58
59 if (!bind(sock, sa, sa_len) &&
60 (socktype != SOCK_STREAM || !listen(sock, SOMAXCONN)))
61 return sock;
62 } else {
63 if (!connect(sock, sa, sa_len) || errno == EINPROGRESS)
64 return sock;
65 }
66
67 close(sock);
68 return -1;
69 }
70
71 static int usock_unix(int type, const char *host)
72 {
73 struct sockaddr_un sun = {.sun_family = AF_UNIX};
74 bool server = !!(type & USOCK_SERVER);
75 int socktype = ((type & 0xff) == USOCK_TCP) ? SOCK_STREAM : SOCK_DGRAM;
76
77 if (strlen(host) >= sizeof(sun.sun_path)) {
78 errno = EINVAL;
79 return -1;
80 }
81 strcpy(sun.sun_path, host);
82
83 return usock_connect(type, (struct sockaddr*)&sun, sizeof(sun), AF_UNIX, socktype, server);
84 }
85
86 static int
87 usock_inet_notimeout(int type, struct addrinfo *result, void *addr)
88 {
89 struct addrinfo *rp;
90 int socktype = ((type & 0xff) == USOCK_TCP) ? SOCK_STREAM : SOCK_DGRAM;
91 bool server = !!(type & USOCK_SERVER);
92 int sock;
93
94 for (rp = result; rp != NULL; rp = rp->ai_next) {
95 sock = usock_connect(type, rp->ai_addr, rp->ai_addrlen, rp->ai_family, socktype, server);
96 if (sock >= 0) {
97 if (addr)
98 memcpy(addr, rp->ai_addr, rp->ai_addrlen);
99 return sock;
100 }
101 }
102
103 return -1;
104 }
105
106 static int poll_restart(struct pollfd *fds, int nfds, int timeout)
107 {
108 struct timespec ts, cur;
109 int msec = timeout % 1000;
110 int ret;
111
112 clock_gettime(CLOCK_MONOTONIC, &ts);
113
114 ts.tv_nsec += msec * 1000000;
115 if (ts.tv_nsec > 1000000000) {
116 ts.tv_sec++;
117 ts.tv_nsec -= 1000000000;
118 }
119 ts.tv_sec += timeout / 1000;
120
121 while (1) {
122 ret = poll(fds, nfds, timeout);
123 if (ret == EAGAIN)
124 continue;
125
126 if (ret != EINTR)
127 return ret;
128
129 clock_gettime(CLOCK_MONOTONIC, &cur);
130 timeout = (ts.tv_sec - cur.tv_sec) * 1000;
131 timeout += (ts.tv_nsec - cur.tv_nsec) / 1000000;
132 if (timeout <= 0)
133 return 0;
134 }
135 }
136
137 int usock_inet_timeout(int type, const char *host, const char *service,
138 void *addr, int timeout)
139 {
140 int socktype = ((type & 0xff) == USOCK_TCP) ? SOCK_STREAM : SOCK_DGRAM;
141 bool server = !!(type & USOCK_SERVER);
142 struct addrinfo *result, *rp;
143 struct addrinfo hints = {
144 .ai_family = (type & USOCK_IPV6ONLY) ? AF_INET6 :
145 (type & USOCK_IPV4ONLY) ? AF_INET : AF_UNSPEC,
146 .ai_socktype = socktype,
147 .ai_flags = AI_ADDRCONFIG
148 | ((type & USOCK_SERVER) ? AI_PASSIVE : 0)
149 | ((type & USOCK_NUMERIC) ? AI_NUMERICHOST : 0),
150 };
151 struct addrinfo *rp_v6 = NULL;
152 struct addrinfo *rp_v4 = NULL;
153 struct pollfd pfds[2] = {
154 { .fd = -1, .events = POLLOUT },
155 { .fd = -1, .events = POLLOUT },
156 };
157 int sock = -1;
158 int i;
159
160 if (getaddrinfo(host, service, &hints, &result))
161 return -1;
162
163 if (timeout <= 0 || server) {
164 sock = usock_inet_notimeout(type, result, addr);
165 goto free_addrinfo;
166 }
167
168 for (rp = result; rp != NULL; rp = rp->ai_next) {
169 if (rp->ai_family == AF_INET6 && !rp_v6)
170 rp_v6 = rp;
171 if (rp->ai_family == AF_INET && !rp_v4)
172 rp_v4 = rp;
173 }
174
175 if (!rp_v6 && !rp_v4)
176 goto out;
177
178 if (rp_v6) {
179 rp = rp_v6;
180 pfds[0].fd = usock_connect(type | USOCK_NONBLOCK, rp->ai_addr,
181 rp->ai_addrlen, rp->ai_family,
182 socktype, server);
183 if (pfds[0].fd < 0) {
184 rp_v6 = NULL;
185 goto try_v4;
186 }
187
188 if (timeout > 300) {
189 if (poll_restart(pfds, 1, 300) == 1) {
190 rp = rp_v6;
191 sock = pfds[0].fd;
192 goto out;
193 }
194 }
195 timeout -= 300;
196 }
197
198 try_v4:
199 if (rp_v4) {
200 rp = rp_v4;
201 pfds[1].fd = usock_connect(type | USOCK_NONBLOCK, rp->ai_addr,
202 rp->ai_addrlen, rp->ai_family,
203 socktype, server);
204 if (pfds[1].fd < 0) {
205 rp_v4 = NULL;
206 if (!rp_v6)
207 goto out;
208 goto wait;
209 }
210 }
211
212 wait:
213 poll_restart(pfds + !rp_v6, !!rp_v6 + !!rp_v4, timeout);
214 if (pfds[0].revents & POLLOUT) {
215 rp = rp_v6;
216 sock = pfds[0].fd;
217 goto out;
218 }
219
220 if (pfds[1].revents & POLLOUT) {
221 rp = rp_v4;
222 sock = pfds[1].fd;
223 goto out;
224 }
225
226 out:
227 for (i = 0; i < 2; i++) {
228 int fd = pfds[i].fd;
229 if (fd >= 0 && fd != sock)
230 close(fd);
231 }
232
233 if (!(type & USOCK_NONBLOCK))
234 fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) & ~O_NONBLOCK);
235
236 if (addr && sock >= 0)
237 memcpy(addr, rp->ai_addr, rp->ai_addrlen);
238 free_addrinfo:
239 freeaddrinfo(result);
240 return sock;
241 }
242
243 const char *usock_port(int port)
244 {
245 static char buffer[sizeof("65535\0")];
246
247 if (port < 0 || port > 65535)
248 return NULL;
249
250 snprintf(buffer, sizeof(buffer), "%u", port);
251
252 return buffer;
253 }
254
255 int usock(int type, const char *host, const char *service) {
256 int sock;
257
258 if (type & USOCK_UNIX)
259 sock = usock_unix(type, host);
260 else
261 sock = usock_inet(type, host, service, NULL);
262
263 if (sock < 0)
264 return -1;
265
266 return sock;
267 }
268
269 int usock_wait_ready(int fd, int msecs) {
270 struct pollfd fds[1];
271 int res;
272
273 fds[0].fd = fd;
274 fds[0].events = POLLOUT;
275
276 res = poll(fds, 1, msecs);
277 if (res < 0) {
278 return errno;
279 } else if (res == 0) {
280 return -ETIMEDOUT;
281 } else {
282 int err = 0;
283 socklen_t optlen = sizeof(err);
284
285 res = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &optlen);
286 if (res)
287 return errno;
288 if (err)
289 return err;
290 }
291
292 return 0;
293 }