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