usock: add usock_inet, which returns the remote address
[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(int type, 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 usock_set_flags(sock, type);
52
53 if (server) {
54 const int one = 1;
55 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
56
57 if (!bind(sock, sa, sa_len) &&
58 (socktype != SOCK_STREAM || !listen(sock, SOMAXCONN)))
59 return sock;
60 } else {
61 if (!connect(sock, sa, sa_len) || errno == EINPROGRESS)
62 return sock;
63 }
64
65 close(sock);
66 return -1;
67 }
68
69 static int usock_unix(int type, const char *host)
70 {
71 struct sockaddr_un sun = {.sun_family = AF_UNIX};
72 bool server = !!(type & USOCK_SERVER);
73 int socktype = ((type & 0xff) == USOCK_TCP) ? SOCK_STREAM : SOCK_DGRAM;
74
75 if (strlen(host) >= sizeof(sun.sun_path)) {
76 errno = EINVAL;
77 return -1;
78 }
79 strcpy(sun.sun_path, host);
80
81 return usock_connect(type, (struct sockaddr*)&sun, sizeof(sun), AF_UNIX, socktype, server);
82 }
83
84 int usock_inet(int type, const char *host, const char *service, void *addr)
85 {
86 int socktype = ((type & 0xff) == USOCK_TCP) ? SOCK_STREAM : SOCK_DGRAM;
87 bool server = !!(type & USOCK_SERVER);
88 struct addrinfo *result, *rp;
89 struct addrinfo hints = {
90 .ai_family = (type & USOCK_IPV6ONLY) ? AF_INET6 :
91 (type & USOCK_IPV4ONLY) ? AF_INET : AF_UNSPEC,
92 .ai_socktype = socktype,
93 .ai_flags = AI_ADDRCONFIG
94 | ((type & USOCK_SERVER) ? AI_PASSIVE : 0)
95 | ((type & USOCK_NUMERIC) ? AI_NUMERICHOST : 0),
96 };
97 int sock = -1;
98
99 if (getaddrinfo(host, service, &hints, &result))
100 return -1;
101
102 for (rp = result; rp != NULL; rp = rp->ai_next) {
103 sock = usock_connect(type, rp->ai_addr, rp->ai_addrlen, rp->ai_family, socktype, server);
104 if (sock >= 0) {
105 if (addr)
106 memcpy(addr, rp->ai_addr, rp->ai_addrlen);
107 break;
108 }
109 }
110
111 freeaddrinfo(result);
112 return sock;
113 }
114
115 const char *usock_port(int port)
116 {
117 static char buffer[sizeof("65535\0")];
118
119 if (port < 0 || port > 65535)
120 return NULL;
121
122 snprintf(buffer, sizeof(buffer), "%u", port);
123
124 return buffer;
125 }
126
127 int usock(int type, const char *host, const char *service) {
128 int sock;
129
130 if (type & USOCK_UNIX)
131 sock = usock_unix(type, host);
132 else
133 sock = usock_inet(type, host, service, NULL);
134
135 if (sock < 0)
136 return -1;
137
138 return sock;
139 }
140
141 int usock_wait_ready(int fd, int msecs) {
142 struct pollfd fds[1];
143 int res;
144
145 fds[0].fd = fd;
146 fds[0].events = POLLOUT;
147
148 res = poll(fds, 1, msecs);
149 if (res < 0) {
150 return errno;
151 } else if (res == 0) {
152 return -ETIMEDOUT;
153 } else {
154 int err = 0;
155 socklen_t optlen = sizeof(err);
156
157 res = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &optlen);
158 if (res)
159 return errno;
160 if (err)
161 return err;
162 }
163
164 return 0;
165 }