uloop: Do not override signal handlers not installed by us
[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 <stdlib.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <stdbool.h>
29 #include <stdio.h>
30
31 #include "usock.h"
32
33 static void usock_set_flags(int sock, unsigned int type)
34 {
35 if (!(type & USOCK_NOCLOEXEC))
36 fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC);
37
38 if (type & USOCK_NONBLOCK)
39 fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK);
40 }
41
42 static int usock_connect(struct sockaddr *sa, int sa_len, int family, int socktype, bool server)
43 {
44 int sock;
45
46 sock = socket(family, socktype, 0);
47 if (sock < 0)
48 return -1;
49
50 if (server) {
51 const int one = 1;
52 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
53
54 if (!bind(sock, sa, sa_len) &&
55 (socktype != SOCK_STREAM || !listen(sock, SOMAXCONN)))
56 return sock;
57 } else {
58 if (!connect(sock, sa, sa_len) || errno == EINPROGRESS)
59 return sock;
60 }
61
62 close(sock);
63 return -1;
64 }
65
66 static int usock_unix(const char *host, int socktype, bool server)
67 {
68 struct sockaddr_un sun = {.sun_family = AF_UNIX};
69
70 if (strlen(host) >= sizeof(sun.sun_path)) {
71 errno = EINVAL;
72 return -1;
73 }
74 strcpy(sun.sun_path, host);
75
76 return usock_connect((struct sockaddr*)&sun, sizeof(sun), AF_UNIX, socktype, server);
77 }
78
79 static int usock_inet(int type, const char *host, const char *service, int socktype, bool server)
80 {
81 struct addrinfo *result, *rp;
82 struct addrinfo hints = {
83 .ai_family = (type & USOCK_IPV6ONLY) ? AF_INET6 :
84 (type & USOCK_IPV4ONLY) ? AF_INET : AF_UNSPEC,
85 .ai_socktype = socktype,
86 .ai_flags = AI_ADDRCONFIG
87 | ((type & USOCK_SERVER) ? AI_PASSIVE : 0)
88 | ((type & USOCK_NUMERIC) ? AI_NUMERICHOST : 0),
89 };
90 int sock = -1;
91
92 if (getaddrinfo(host, service, &hints, &result))
93 return -1;
94
95 for (rp = result; rp != NULL; rp = rp->ai_next) {
96 sock = usock_connect(rp->ai_addr, rp->ai_addrlen, rp->ai_family, socktype, server);
97 if (sock >= 0)
98 break;
99 }
100
101 freeaddrinfo(result);
102 return sock;
103 }
104
105 const char *usock_port(int port)
106 {
107 static char buffer[sizeof("65535\0")];
108
109 if (port < 0 || port > 65535)
110 return NULL;
111
112 snprintf(buffer, sizeof(buffer), "%u", port);
113
114 return buffer;
115 }
116
117 int usock(int type, const char *host, const char *service) {
118 int socktype = ((type & 0xff) == USOCK_TCP) ? SOCK_STREAM : SOCK_DGRAM;
119 bool server = !!(type & USOCK_SERVER);
120 int sock;
121
122 if (type & USOCK_UNIX)
123 sock = usock_unix(host, socktype, server);
124 else
125 sock = usock_inet(type, host, service, socktype, server);
126
127 if (sock < 0)
128 return -1;
129
130 usock_set_flags(sock, type);
131 return sock;
132 }