blob: e9a2470e4173c7df8fecfc8b077004e7733b2fab [file] [log] [blame]
Eric Andersen0b315862002-07-03 11:51:44 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenaff114c2004-04-14 17:51:38 +00005 * Connect to host at port using address resolution from getaddrinfo
Eric Andersen0b315862002-07-03 11:51:44 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersen0b315862002-07-03 11:51:44 +00008 */
Dan Fandrichfdd7b562010-06-18 22:37:42 -07009#include <sys/types.h>
Denys Vlasenko043b1e52009-09-06 12:47:55 +020010#include <sys/socket.h> /* netinet/in.h needs it */
Denis Vlasenko9de420c2007-01-10 09:28:01 +000011#include <netinet/in.h>
Denis Vlasenkoe5373852008-12-10 11:12:16 +000012#include <net/if.h>
Denis Vlasenkof6b46852009-04-25 13:16:53 +000013#include <sys/un.h>
Denys Vlasenko45e39672019-06-03 14:16:52 +020014#if ENABLE_IFPLUGD || ENABLE_FEATURE_MDEV_DAEMON || ENABLE_UEVENT
15# include <linux/netlink.h>
16#endif
Eric Andersen0b315862002-07-03 11:51:44 +000017#include "libbb.h"
18
Denys Vlasenkoc52cbea2015-08-24 19:48:03 +020019int FAST_FUNC setsockopt_int(int fd, int level, int optname, int optval)
20{
21 return setsockopt(fd, level, optname, &optval, sizeof(int));
22}
23int FAST_FUNC setsockopt_1(int fd, int level, int optname)
24{
25 return setsockopt_int(fd, level, optname, 1);
26}
27int FAST_FUNC setsockopt_SOL_SOCKET_int(int fd, int optname, int optval)
28{
29 return setsockopt_int(fd, SOL_SOCKET, optname, optval);
30}
31int FAST_FUNC setsockopt_SOL_SOCKET_1(int fd, int optname)
32{
33 return setsockopt_SOL_SOCKET_int(fd, optname, 1);
34}
35
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000036void FAST_FUNC setsockopt_reuseaddr(int fd)
Denis Vlasenko48237b02006-11-22 23:22:06 +000037{
Denys Vlasenkoc52cbea2015-08-24 19:48:03 +020038 setsockopt_SOL_SOCKET_1(fd, SO_REUSEADDR);
Denis Vlasenko48237b02006-11-22 23:22:06 +000039}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000040int FAST_FUNC setsockopt_broadcast(int fd)
Denis Vlasenko48237b02006-11-22 23:22:06 +000041{
Denys Vlasenkoc52cbea2015-08-24 19:48:03 +020042 return setsockopt_SOL_SOCKET_1(fd, SO_BROADCAST);
43}
44int FAST_FUNC setsockopt_keepalive(int fd)
45{
46 return setsockopt_SOL_SOCKET_1(fd, SO_KEEPALIVE);
Denis Vlasenko48237b02006-11-22 23:22:06 +000047}
Jérémie Koenigfbedacf2010-03-26 19:08:53 +010048
49#ifdef SO_BINDTODEVICE
Denis Vlasenkoe5373852008-12-10 11:12:16 +000050int FAST_FUNC setsockopt_bindtodevice(int fd, const char *iface)
51{
52 int r;
53 struct ifreq ifr;
54 strncpy_IFNAMSIZ(ifr.ifr_name, iface);
Denis Vlasenkoa3ca7cf2008-12-10 11:24:33 +000055 /* NB: passing (iface, strlen(iface) + 1) does not work!
56 * (maybe it works on _some_ kernels, but not on 2.6.26)
57 * Actually, ifr_name is at offset 0, and in practice
Denis Vlasenkoe5373852008-12-10 11:12:16 +000058 * just giving char[IFNAMSIZ] instead of struct ifreq works too.
59 * But just in case it's not true on some obscure arch... */
60 r = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
61 if (r)
62 bb_perror_msg("can't bind to interface %s", iface);
63 return r;
64}
Jérémie Koenigfbedacf2010-03-26 19:08:53 +010065#else
66int FAST_FUNC setsockopt_bindtodevice(int fd UNUSED_PARAM,
67 const char *iface UNUSED_PARAM)
68{
James Byrne69374872019-07-02 11:35:03 +020069 bb_simple_error_msg("SO_BINDTODEVICE is not supported on this system");
Jérémie Koenigfbedacf2010-03-26 19:08:53 +010070 return -1;
71}
72#endif
Denis Vlasenkoe5373852008-12-10 11:12:16 +000073
Denys Vlasenkof6916db2010-04-06 17:43:29 +020074static len_and_sockaddr* get_lsa(int fd, int (*get_name)(int fd, struct sockaddr *addr, socklen_t *addrlen))
Denis Vlasenko73c571a2009-03-09 00:12:37 +000075{
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000076 len_and_sockaddr lsa;
77 len_and_sockaddr *lsa_ptr;
Denis Vlasenko73c571a2009-03-09 00:12:37 +000078
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000079 lsa.len = LSA_SIZEOF_SA;
Denys Vlasenkof6916db2010-04-06 17:43:29 +020080 if (get_name(fd, &lsa.u.sa, &lsa.len) != 0)
Denis Vlasenko73c571a2009-03-09 00:12:37 +000081 return NULL;
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000082
83 lsa_ptr = xzalloc(LSA_LEN_SIZE + lsa.len);
84 if (lsa.len > LSA_SIZEOF_SA) { /* rarely (if ever) happens */
85 lsa_ptr->len = lsa.len;
Denys Vlasenkof6916db2010-04-06 17:43:29 +020086 get_name(fd, &lsa_ptr->u.sa, &lsa_ptr->len);
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000087 } else {
88 memcpy(lsa_ptr, &lsa, LSA_LEN_SIZE + lsa.len);
89 }
90 return lsa_ptr;
Denis Vlasenko73c571a2009-03-09 00:12:37 +000091}
Denis Vlasenko48237b02006-11-22 23:22:06 +000092
Denys Vlasenkof6916db2010-04-06 17:43:29 +020093len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd)
94{
95 return get_lsa(fd, getsockname);
96}
97
98len_and_sockaddr* FAST_FUNC get_peer_lsa(int fd)
99{
100 return get_lsa(fd, getpeername);
101}
102
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000103void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000104{
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000105 if (connect(s, s_addr, addrlen) < 0) {
106 if (ENABLE_FEATURE_CLEAN_UP)
107 close(s);
108 if (s_addr->sa_family == AF_INET)
109 bb_perror_msg_and_die("%s (%s)",
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100110 "can't connect to remote host",
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000111 inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
James Byrne69374872019-07-02 11:35:03 +0200112 bb_simple_perror_msg_and_die("can't connect to remote host");
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000113 }
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000114}
115
Denis Vlasenko5d687242007-01-12 20:59:31 +0000116/* Return port number for a service.
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000117 * If "port" is a number use it as the port.
Denis Vlasenko73c571a2009-03-09 00:12:37 +0000118 * If "port" is a name it is looked up in /etc/services,
119 * if it isnt found return default_port
120 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000121unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000122{
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000123 unsigned port_nr = default_port;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000124 if (port) {
125 int old_errno;
126
127 /* Since this is a lib function, we're not allowed to reset errno to 0.
128 * Doing so could break an app that is deferring checking of errno. */
129 old_errno = errno;
130 port_nr = bb_strtou(port, NULL, 10);
131 if (errno || port_nr > 65535) {
132 struct servent *tserv = getservbyname(port, protocol);
Denis Vlasenko24182882007-01-13 21:05:38 +0000133 port_nr = default_port;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000134 if (tserv)
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000135 port_nr = ntohs(tserv->s_port);
Denys Vlasenko3b8025f2019-06-09 11:12:02 +0200136//FIXME: else: port string was garbage, but we don't report that???
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000137 }
138 errno = old_errno;
139 }
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000140 return (uint16_t)port_nr;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000141}
142
143
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000144/* "New" networking API */
145
146
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000147int FAST_FUNC get_nport(const struct sockaddr *sa)
Denis Vlasenko5d687242007-01-12 20:59:31 +0000148{
149#if ENABLE_FEATURE_IPV6
Denis Vlasenko2856dab2007-04-01 01:18:20 +0000150 if (sa->sa_family == AF_INET6) {
151 return ((struct sockaddr_in6*)sa)->sin6_port;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000152 }
153#endif
Denis Vlasenko2856dab2007-04-01 01:18:20 +0000154 if (sa->sa_family == AF_INET) {
155 return ((struct sockaddr_in*)sa)->sin_port;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000156 }
Denis Vlasenko5d687242007-01-12 20:59:31 +0000157 /* What? UNIX socket? IPX?? :) */
Denis Vlasenko85629f02007-01-22 09:36:41 +0000158 return -1;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000159}
160
Denys Vlasenkoca183112011-04-07 17:52:20 +0200161void FAST_FUNC set_nport(struct sockaddr *sa, unsigned port)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000162{
163#if ENABLE_FEATURE_IPV6
Denys Vlasenkoca183112011-04-07 17:52:20 +0200164 if (sa->sa_family == AF_INET6) {
165 struct sockaddr_in6 *sin6 = (void*) sa;
166 sin6->sin6_port = port;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000167 return;
168 }
169#endif
Denys Vlasenkoca183112011-04-07 17:52:20 +0200170 if (sa->sa_family == AF_INET) {
171 struct sockaddr_in *sin = (void*) sa;
172 sin->sin_port = port;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000173 return;
174 }
175 /* What? UNIX socket? IPX?? :) */
176}
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000177
Denis Vlasenko42823d52007-02-04 02:39:08 +0000178/* We hijack this constant to mean something else */
179/* It doesn't hurt because we will remove this bit anyway */
180#define DIE_ON_ERROR AI_CANONNAME
181
Denis Vlasenko85629f02007-01-22 09:36:41 +0000182/* host: "1.2.3.4[:port]", "www.google.com[:port]"
Denis Vlasenko42823d52007-02-04 02:39:08 +0000183 * port: if neither of above specifies port # */
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000184static len_and_sockaddr* str2sockaddr(
185 const char *host, int port,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000186IF_FEATURE_IPV6(sa_family_t af,)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000187 int ai_flags)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000188{
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100189IF_NOT_FEATURE_IPV6(sa_family_t af = AF_INET;)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000190 int rc;
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000191 len_and_sockaddr *r;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000192 struct addrinfo *result = NULL;
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000193 struct addrinfo *used_res;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000194 const char *org_host = host; /* only for error msg */
195 const char *cp;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000196 struct addrinfo hint;
197
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100198 if (ENABLE_FEATURE_UNIX_LOCAL && is_prefixed_with(host, "local:")) {
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000199 struct sockaddr_un *sun;
200
201 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un));
202 r->len = sizeof(struct sockaddr_un);
203 r->u.sa.sa_family = AF_UNIX;
204 sun = (struct sockaddr_un *)&r->u.sa;
205 safe_strncpy(sun->sun_path, host + 6, sizeof(sun->sun_path));
206 return r;
207 }
208
209 r = NULL;
210
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000211 /* Ugly parsing of host:addr */
212 if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000213 /* Even uglier parsing of [xx]:nn */
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000214 host++;
215 cp = strchr(host, ']');
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000216 if (!cp || (cp[1] != ':' && cp[1] != '\0')) {
217 /* Malformed: must be [xx]:nn or [xx] */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000218 bb_error_msg("bad address '%s'", org_host);
219 if (ai_flags & DIE_ON_ERROR)
220 xfunc_die();
221 return NULL;
222 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000223 } else {
224 cp = strrchr(host, ':');
225 if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
226 /* There is more than one ':' (e.g. "::1") */
227 cp = NULL; /* it's not a port spec */
228 }
229 }
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000230 if (cp) { /* points to ":" or "]:" */
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000231 int sz = cp - host + 1;
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000232
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000233 host = safe_strncpy(alloca(sz), host, sz);
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000234 if (ENABLE_FEATURE_IPV6 && *cp != ':') {
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000235 cp++; /* skip ']' */
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000236 if (*cp == '\0') /* [xx] without port */
237 goto skip;
238 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000239 cp++; /* skip ':' */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000240 port = bb_strtou(cp, NULL, 10);
241 if (errno || (unsigned)port > 0xffff) {
242 bb_error_msg("bad port spec '%s'", org_host);
243 if (ai_flags & DIE_ON_ERROR)
244 xfunc_die();
245 return NULL;
246 }
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000247 skip: ;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000248 }
249
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100250 /* Next two if blocks allow to skip getaddrinfo()
Denys Vlasenko75b73972009-11-24 14:06:04 +0100251 * in case host name is a numeric IP(v6) address.
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100252 * getaddrinfo() initializes DNS resolution machinery,
253 * scans network config and such - tens of syscalls.
254 */
255 /* If we were not asked specifically for IPv6,
256 * check whether this is a numeric IPv4 */
257 IF_FEATURE_IPV6(if(af != AF_INET6)) {
258 struct in_addr in4;
259 if (inet_aton(host, &in4) != 0) {
260 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in));
261 r->len = sizeof(struct sockaddr_in);
262 r->u.sa.sa_family = AF_INET;
263 r->u.sin.sin_addr = in4;
264 goto set_port;
265 }
266 }
267#if ENABLE_FEATURE_IPV6
268 /* If we were not asked specifically for IPv4,
269 * check whether this is a numeric IPv6 */
270 if (af != AF_INET) {
271 struct in6_addr in6;
272 if (inet_pton(AF_INET6, host, &in6) > 0) {
273 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in6));
274 r->len = sizeof(struct sockaddr_in6);
275 r->u.sa.sa_family = AF_INET6;
276 r->u.sin6.sin6_addr = in6;
277 goto set_port;
278 }
279 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000280#endif
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100281
282 memset(&hint, 0 , sizeof(hint));
283 hint.ai_family = af;
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100284 /* Need SOCK_STREAM, or else we get each address thrice (or more)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000285 * for each possible socket type (tcp,udp,raw...): */
286 hint.ai_socktype = SOCK_STREAM;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000287 hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000288 rc = getaddrinfo(host, NULL, &hint, &result);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000289 if (rc || !result) {
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000290 bb_error_msg("bad address '%s'", org_host);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000291 if (ai_flags & DIE_ON_ERROR)
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000292 xfunc_die();
Denis Vlasenko42823d52007-02-04 02:39:08 +0000293 goto ret;
294 }
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000295 used_res = result;
296#if ENABLE_FEATURE_PREFER_IPV4_ADDRESS
297 while (1) {
298 if (used_res->ai_family == AF_INET)
299 break;
300 used_res = used_res->ai_next;
301 if (!used_res) {
302 used_res = result;
303 break;
304 }
305 }
306#endif
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100307 r = xmalloc(LSA_LEN_SIZE + used_res->ai_addrlen);
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000308 r->len = used_res->ai_addrlen;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000309 memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen);
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100310
Denys Vlasenko75b73972009-11-24 14:06:04 +0100311 set_port:
Denys Vlasenkoca183112011-04-07 17:52:20 +0200312 set_nport(&r->u.sa, htons(port));
Denis Vlasenko42823d52007-02-04 02:39:08 +0000313 ret:
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100314 if (result)
315 freeaddrinfo(result);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000316 return r;
317}
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000318#if !ENABLE_FEATURE_IPV6
319#define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
320#endif
321
322#if ENABLE_FEATURE_IPV6
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000323len_and_sockaddr* FAST_FUNC host_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenkoe9913032007-02-04 02:41:57 +0000324{
325 return str2sockaddr(host, port, af, 0);
326}
327
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000328len_and_sockaddr* FAST_FUNC xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000329{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000330 return str2sockaddr(host, port, af, DIE_ON_ERROR);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000331}
332#endif
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000333
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000334len_and_sockaddr* FAST_FUNC host2sockaddr(const char *host, int port)
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000335{
336 return str2sockaddr(host, port, AF_UNSPEC, 0);
337}
338
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000339len_and_sockaddr* FAST_FUNC xhost2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000340{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000341 return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000342}
343
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000344len_and_sockaddr* FAST_FUNC xdotted2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000345{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000346 return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000347}
348
Jonh Wendell91061072012-02-09 15:14:33 +0100349int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, int family, int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000350{
Denis Vlasenko5d687242007-01-12 20:59:31 +0000351 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000352 int fd;
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000353 int len;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000354
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000355 if (family == AF_UNSPEC) {
Jonh Wendell91061072012-02-09 15:14:33 +0100356#if ENABLE_FEATURE_IPV6
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000357 fd = socket(AF_INET6, sock_type, 0);
358 if (fd >= 0) {
359 family = AF_INET6;
360 goto done;
361 }
Jonh Wendell91061072012-02-09 15:14:33 +0100362#endif
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000363 family = AF_INET;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000364 }
Jonh Wendell91061072012-02-09 15:14:33 +0100365
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000366 fd = xsocket(family, sock_type, 0);
Jonh Wendell91061072012-02-09 15:14:33 +0100367
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000368 len = sizeof(struct sockaddr_in);
Jonh Wendell91061072012-02-09 15:14:33 +0100369 if (family == AF_UNIX)
370 len = sizeof(struct sockaddr_un);
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000371#if ENABLE_FEATURE_IPV6
372 if (family == AF_INET6) {
373 done:
374 len = sizeof(struct sockaddr_in6);
375 }
376#endif
Denys Vlasenkob1278a32009-11-24 16:03:47 +0100377 lsa = xzalloc(LSA_LEN_SIZE + len);
Denis Vlasenko5d687242007-01-12 20:59:31 +0000378 lsa->len = len;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000379 lsa->u.sa.sa_family = family;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000380 *lsap = lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000381 return fd;
382}
383
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000384int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000385{
Jonh Wendell91061072012-02-09 15:14:33 +0100386 return xsocket_type(lsap, AF_UNSPEC, SOCK_STREAM);
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000387}
388
389static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000390{
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000391 int fd;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000392 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000393
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000394 if (bindaddr && bindaddr[0]) {
Denis Vlasenko42823d52007-02-04 02:39:08 +0000395 lsa = xdotted2sockaddr(bindaddr, port);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000396 /* user specified bind addr dictates family */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000397 fd = xsocket(lsa->u.sa.sa_family, sock_type, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000398 } else {
Jonh Wendell91061072012-02-09 15:14:33 +0100399 fd = xsocket_type(&lsa, AF_UNSPEC, sock_type);
Denys Vlasenkoca183112011-04-07 17:52:20 +0200400 set_nport(&lsa->u.sa, htons(port));
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000401 }
Denis Vlasenko48237b02006-11-22 23:22:06 +0000402 setsockopt_reuseaddr(fd);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000403 xbind(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000404 free(lsa);
405 return fd;
406}
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000407
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000408int FAST_FUNC create_and_bind_stream_or_die(const char *bindaddr, int port)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000409{
410 return create_and_bind_or_die(bindaddr, port, SOCK_STREAM);
411}
412
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000413int FAST_FUNC create_and_bind_dgram_or_die(const char *bindaddr, int port)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000414{
415 return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM);
416}
417
418
Denys Vlasenko45e39672019-06-03 14:16:52 +0200419#if ENABLE_IFPLUGD || ENABLE_FEATURE_MDEV_DAEMON || ENABLE_UEVENT
420int FAST_FUNC create_and_bind_to_netlink(int proto, int grp, unsigned rcvbuf)
421{
422 struct sockaddr_nl sa;
423 int fd;
424
425 memset(&sa, 0, sizeof(sa));
426 sa.nl_family = AF_NETLINK;
427 sa.nl_pid = getpid();
428 sa.nl_groups = grp;
429 fd = xsocket(AF_NETLINK, SOCK_DGRAM, proto);
430 xbind(fd, (struct sockaddr *) &sa, sizeof(sa));
431 close_on_exec_on(fd);
432
433 if (rcvbuf != 0) {
434 // SO_RCVBUFFORCE (root only) can go above net.core.rmem_max sysctl
435 setsockopt_SOL_SOCKET_int(fd, SO_RCVBUF, rcvbuf);
436 setsockopt_SOL_SOCKET_int(fd, SO_RCVBUFFORCE, rcvbuf);
437# if 0
438 {
439 int z;
440 socklen_t zl = sizeof(z);
441 getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &z, &zl);
442 bb_error_msg("SO_RCVBUF:%d", z);
443 }
444# endif
445 }
446
447 return fd;
448}
449#endif
450
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000451int FAST_FUNC create_and_connect_stream_or_die(const char *peer, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000452{
453 int fd;
454 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000455
Denis Vlasenko42823d52007-02-04 02:39:08 +0000456 lsa = xhost2sockaddr(peer, port);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000457 fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000458 setsockopt_reuseaddr(fd);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000459 xconnect(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000460 free(lsa);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000461 return fd;
462}
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000463
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000464int FAST_FUNC xconnect_stream(const len_and_sockaddr *lsa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000465{
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000466 int fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
467 xconnect(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000468 return fd;
469}
470
Denis Vlasenko448f0242007-01-22 22:43:05 +0000471/* We hijack this constant to mean something else */
472/* It doesn't hurt because we will add this bit anyway */
473#define IGNORE_PORT NI_NUMERICSERV
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000474static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000475{
476 char host[128];
477 char serv[16];
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000478 int rc;
479 socklen_t salen;
480
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000481 if (ENABLE_FEATURE_UNIX_LOCAL && sa->sa_family == AF_UNIX) {
482 struct sockaddr_un *sun = (struct sockaddr_un *)sa;
483 return xasprintf("local:%.*s",
484 (int) sizeof(sun->sun_path),
485 sun->sun_path);
486 }
487
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000488 salen = LSA_SIZEOF_SA;
489#if ENABLE_FEATURE_IPV6
490 if (sa->sa_family == AF_INET)
491 salen = sizeof(struct sockaddr_in);
492 if (sa->sa_family == AF_INET6)
493 salen = sizeof(struct sockaddr_in6);
494#endif
495 rc = getnameinfo(sa, salen,
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000496 host, sizeof(host),
Denis Vlasenko448f0242007-01-22 22:43:05 +0000497 /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000498 serv, sizeof(serv),
Denis Vlasenko85629f02007-01-22 09:36:41 +0000499 /* do not resolve port# into service _name_ */
500 flags | NI_NUMERICSERV
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000501 );
Denis Vlasenko85629f02007-01-22 09:36:41 +0000502 if (rc)
503 return NULL;
Denis Vlasenko448f0242007-01-22 22:43:05 +0000504 if (flags & IGNORE_PORT)
505 return xstrdup(host);
Denis Vlasenko85629f02007-01-22 09:36:41 +0000506#if ENABLE_FEATURE_IPV6
Denis Vlasenko448f0242007-01-22 22:43:05 +0000507 if (sa->sa_family == AF_INET6) {
508 if (strchr(host, ':')) /* heh, it's not a resolved hostname */
509 return xasprintf("[%s]:%s", host, serv);
510 /*return xasprintf("%s:%s", host, serv);*/
511 /* - fall through instead */
512 }
Denis Vlasenko85629f02007-01-22 09:36:41 +0000513#endif
514 /* For now we don't support anything else, so it has to be INET */
515 /*if (sa->sa_family == AF_INET)*/
516 return xasprintf("%s:%s", host, serv);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000517 /*return xstrdup(host);*/
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000518}
519
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000520char* FAST_FUNC xmalloc_sockaddr2host(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000521{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000522 return sockaddr2str(sa, 0);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000523}
524
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000525char* FAST_FUNC xmalloc_sockaddr2host_noport(const struct sockaddr *sa)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000526{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000527 return sockaddr2str(sa, IGNORE_PORT);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000528}
529
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000530char* FAST_FUNC xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000531{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000532 return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000533}
Denys Vlasenko3c18e302016-09-26 19:53:04 +0200534#ifndef NI_NUMERICSCOPE
535# define NI_NUMERICSCOPE 0
536#endif
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000537char* FAST_FUNC xmalloc_sockaddr2dotted(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000538{
Denys Vlasenko3c18e302016-09-26 19:53:04 +0200539 return sockaddr2str(sa, NI_NUMERICHOST | NI_NUMERICSCOPE);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000540}
Denis Vlasenko448f0242007-01-22 22:43:05 +0000541
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000542char* FAST_FUNC xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000543{
Denys Vlasenko3c18e302016-09-26 19:53:04 +0200544 return sockaddr2str(sa, NI_NUMERICHOST | NI_NUMERICSCOPE | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000545}