blob: 39e56b223b5f8577e9f95c87251d3178380daee9 [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>
Eric Andersen0b315862002-07-03 11:51:44 +000014#include "libbb.h"
15
Denys Vlasenkoc52cbea2015-08-24 19:48:03 +020016int FAST_FUNC setsockopt_int(int fd, int level, int optname, int optval)
17{
18 return setsockopt(fd, level, optname, &optval, sizeof(int));
19}
20int FAST_FUNC setsockopt_1(int fd, int level, int optname)
21{
22 return setsockopt_int(fd, level, optname, 1);
23}
24int FAST_FUNC setsockopt_SOL_SOCKET_int(int fd, int optname, int optval)
25{
26 return setsockopt_int(fd, SOL_SOCKET, optname, optval);
27}
28int FAST_FUNC setsockopt_SOL_SOCKET_1(int fd, int optname)
29{
30 return setsockopt_SOL_SOCKET_int(fd, optname, 1);
31}
32
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000033void FAST_FUNC setsockopt_reuseaddr(int fd)
Denis Vlasenko48237b02006-11-22 23:22:06 +000034{
Denys Vlasenkoc52cbea2015-08-24 19:48:03 +020035 setsockopt_SOL_SOCKET_1(fd, SO_REUSEADDR);
Denis Vlasenko48237b02006-11-22 23:22:06 +000036}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000037int FAST_FUNC setsockopt_broadcast(int fd)
Denis Vlasenko48237b02006-11-22 23:22:06 +000038{
Denys Vlasenkoc52cbea2015-08-24 19:48:03 +020039 return setsockopt_SOL_SOCKET_1(fd, SO_BROADCAST);
40}
41int FAST_FUNC setsockopt_keepalive(int fd)
42{
43 return setsockopt_SOL_SOCKET_1(fd, SO_KEEPALIVE);
Denis Vlasenko48237b02006-11-22 23:22:06 +000044}
Jérémie Koenigfbedacf2010-03-26 19:08:53 +010045
46#ifdef SO_BINDTODEVICE
Denis Vlasenkoe5373852008-12-10 11:12:16 +000047int FAST_FUNC setsockopt_bindtodevice(int fd, const char *iface)
48{
49 int r;
50 struct ifreq ifr;
51 strncpy_IFNAMSIZ(ifr.ifr_name, iface);
Denis Vlasenkoa3ca7cf2008-12-10 11:24:33 +000052 /* NB: passing (iface, strlen(iface) + 1) does not work!
53 * (maybe it works on _some_ kernels, but not on 2.6.26)
54 * Actually, ifr_name is at offset 0, and in practice
Denis Vlasenkoe5373852008-12-10 11:12:16 +000055 * just giving char[IFNAMSIZ] instead of struct ifreq works too.
56 * But just in case it's not true on some obscure arch... */
57 r = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
58 if (r)
59 bb_perror_msg("can't bind to interface %s", iface);
60 return r;
61}
Jérémie Koenigfbedacf2010-03-26 19:08:53 +010062#else
63int FAST_FUNC setsockopt_bindtodevice(int fd UNUSED_PARAM,
64 const char *iface UNUSED_PARAM)
65{
66 bb_error_msg("SO_BINDTODEVICE is not supported on this system");
67 return -1;
68}
69#endif
Denis Vlasenkoe5373852008-12-10 11:12:16 +000070
Denys Vlasenkof6916db2010-04-06 17:43:29 +020071static 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 +000072{
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000073 len_and_sockaddr lsa;
74 len_and_sockaddr *lsa_ptr;
Denis Vlasenko73c571a2009-03-09 00:12:37 +000075
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000076 lsa.len = LSA_SIZEOF_SA;
Denys Vlasenkof6916db2010-04-06 17:43:29 +020077 if (get_name(fd, &lsa.u.sa, &lsa.len) != 0)
Denis Vlasenko73c571a2009-03-09 00:12:37 +000078 return NULL;
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000079
80 lsa_ptr = xzalloc(LSA_LEN_SIZE + lsa.len);
81 if (lsa.len > LSA_SIZEOF_SA) { /* rarely (if ever) happens */
82 lsa_ptr->len = lsa.len;
Denys Vlasenkof6916db2010-04-06 17:43:29 +020083 get_name(fd, &lsa_ptr->u.sa, &lsa_ptr->len);
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000084 } else {
85 memcpy(lsa_ptr, &lsa, LSA_LEN_SIZE + lsa.len);
86 }
87 return lsa_ptr;
Denis Vlasenko73c571a2009-03-09 00:12:37 +000088}
Denis Vlasenko48237b02006-11-22 23:22:06 +000089
Denys Vlasenkof6916db2010-04-06 17:43:29 +020090len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd)
91{
92 return get_lsa(fd, getsockname);
93}
94
95len_and_sockaddr* FAST_FUNC get_peer_lsa(int fd)
96{
97 return get_lsa(fd, getpeername);
98}
99
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000100void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000101{
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000102 if (connect(s, s_addr, addrlen) < 0) {
103 if (ENABLE_FEATURE_CLEAN_UP)
104 close(s);
105 if (s_addr->sa_family == AF_INET)
106 bb_perror_msg_and_die("%s (%s)",
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100107 "can't connect to remote host",
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000108 inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100109 bb_perror_msg_and_die("can't connect to remote host");
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000110 }
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000111}
112
Denis Vlasenko5d687242007-01-12 20:59:31 +0000113/* Return port number for a service.
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000114 * If "port" is a number use it as the port.
Denis Vlasenko73c571a2009-03-09 00:12:37 +0000115 * If "port" is a name it is looked up in /etc/services,
116 * if it isnt found return default_port
117 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000118unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000119{
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000120 unsigned port_nr = default_port;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000121 if (port) {
122 int old_errno;
123
124 /* Since this is a lib function, we're not allowed to reset errno to 0.
125 * Doing so could break an app that is deferring checking of errno. */
126 old_errno = errno;
127 port_nr = bb_strtou(port, NULL, 10);
128 if (errno || port_nr > 65535) {
129 struct servent *tserv = getservbyname(port, protocol);
Denis Vlasenko24182882007-01-13 21:05:38 +0000130 port_nr = default_port;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000131 if (tserv)
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000132 port_nr = ntohs(tserv->s_port);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000133 }
134 errno = old_errno;
135 }
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000136 return (uint16_t)port_nr;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000137}
138
139
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000140/* "New" networking API */
141
142
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000143int FAST_FUNC get_nport(const struct sockaddr *sa)
Denis Vlasenko5d687242007-01-12 20:59:31 +0000144{
145#if ENABLE_FEATURE_IPV6
Denis Vlasenko2856dab2007-04-01 01:18:20 +0000146 if (sa->sa_family == AF_INET6) {
147 return ((struct sockaddr_in6*)sa)->sin6_port;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000148 }
149#endif
Denis Vlasenko2856dab2007-04-01 01:18:20 +0000150 if (sa->sa_family == AF_INET) {
151 return ((struct sockaddr_in*)sa)->sin_port;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000152 }
Denis Vlasenko5d687242007-01-12 20:59:31 +0000153 /* What? UNIX socket? IPX?? :) */
Denis Vlasenko85629f02007-01-22 09:36:41 +0000154 return -1;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000155}
156
Denys Vlasenkoca183112011-04-07 17:52:20 +0200157void FAST_FUNC set_nport(struct sockaddr *sa, unsigned port)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000158{
159#if ENABLE_FEATURE_IPV6
Denys Vlasenkoca183112011-04-07 17:52:20 +0200160 if (sa->sa_family == AF_INET6) {
161 struct sockaddr_in6 *sin6 = (void*) sa;
162 sin6->sin6_port = port;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000163 return;
164 }
165#endif
Denys Vlasenkoca183112011-04-07 17:52:20 +0200166 if (sa->sa_family == AF_INET) {
167 struct sockaddr_in *sin = (void*) sa;
168 sin->sin_port = port;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000169 return;
170 }
171 /* What? UNIX socket? IPX?? :) */
172}
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000173
Denis Vlasenko42823d52007-02-04 02:39:08 +0000174/* We hijack this constant to mean something else */
175/* It doesn't hurt because we will remove this bit anyway */
176#define DIE_ON_ERROR AI_CANONNAME
177
Denis Vlasenko85629f02007-01-22 09:36:41 +0000178/* host: "1.2.3.4[:port]", "www.google.com[:port]"
Denis Vlasenko42823d52007-02-04 02:39:08 +0000179 * port: if neither of above specifies port # */
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000180static len_and_sockaddr* str2sockaddr(
181 const char *host, int port,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000182IF_FEATURE_IPV6(sa_family_t af,)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000183 int ai_flags)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000184{
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100185IF_NOT_FEATURE_IPV6(sa_family_t af = AF_INET;)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000186 int rc;
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000187 len_and_sockaddr *r;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000188 struct addrinfo *result = NULL;
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000189 struct addrinfo *used_res;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000190 const char *org_host = host; /* only for error msg */
191 const char *cp;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000192 struct addrinfo hint;
193
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100194 if (ENABLE_FEATURE_UNIX_LOCAL && is_prefixed_with(host, "local:")) {
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000195 struct sockaddr_un *sun;
196
197 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un));
198 r->len = sizeof(struct sockaddr_un);
199 r->u.sa.sa_family = AF_UNIX;
200 sun = (struct sockaddr_un *)&r->u.sa;
201 safe_strncpy(sun->sun_path, host + 6, sizeof(sun->sun_path));
202 return r;
203 }
204
205 r = NULL;
206
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000207 /* Ugly parsing of host:addr */
208 if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000209 /* Even uglier parsing of [xx]:nn */
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000210 host++;
211 cp = strchr(host, ']');
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000212 if (!cp || (cp[1] != ':' && cp[1] != '\0')) {
213 /* Malformed: must be [xx]:nn or [xx] */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000214 bb_error_msg("bad address '%s'", org_host);
215 if (ai_flags & DIE_ON_ERROR)
216 xfunc_die();
217 return NULL;
218 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000219 } else {
220 cp = strrchr(host, ':');
221 if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
222 /* There is more than one ':' (e.g. "::1") */
223 cp = NULL; /* it's not a port spec */
224 }
225 }
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000226 if (cp) { /* points to ":" or "]:" */
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000227 int sz = cp - host + 1;
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000228
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000229 host = safe_strncpy(alloca(sz), host, sz);
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000230 if (ENABLE_FEATURE_IPV6 && *cp != ':') {
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000231 cp++; /* skip ']' */
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000232 if (*cp == '\0') /* [xx] without port */
233 goto skip;
234 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000235 cp++; /* skip ':' */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000236 port = bb_strtou(cp, NULL, 10);
237 if (errno || (unsigned)port > 0xffff) {
238 bb_error_msg("bad port spec '%s'", org_host);
239 if (ai_flags & DIE_ON_ERROR)
240 xfunc_die();
241 return NULL;
242 }
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000243 skip: ;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000244 }
245
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100246 /* Next two if blocks allow to skip getaddrinfo()
Denys Vlasenko75b73972009-11-24 14:06:04 +0100247 * in case host name is a numeric IP(v6) address.
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100248 * getaddrinfo() initializes DNS resolution machinery,
249 * scans network config and such - tens of syscalls.
250 */
251 /* If we were not asked specifically for IPv6,
252 * check whether this is a numeric IPv4 */
253 IF_FEATURE_IPV6(if(af != AF_INET6)) {
254 struct in_addr in4;
255 if (inet_aton(host, &in4) != 0) {
256 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in));
257 r->len = sizeof(struct sockaddr_in);
258 r->u.sa.sa_family = AF_INET;
259 r->u.sin.sin_addr = in4;
260 goto set_port;
261 }
262 }
263#if ENABLE_FEATURE_IPV6
264 /* If we were not asked specifically for IPv4,
265 * check whether this is a numeric IPv6 */
266 if (af != AF_INET) {
267 struct in6_addr in6;
268 if (inet_pton(AF_INET6, host, &in6) > 0) {
269 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in6));
270 r->len = sizeof(struct sockaddr_in6);
271 r->u.sa.sa_family = AF_INET6;
272 r->u.sin6.sin6_addr = in6;
273 goto set_port;
274 }
275 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000276#endif
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100277
278 memset(&hint, 0 , sizeof(hint));
279 hint.ai_family = af;
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100280 /* Need SOCK_STREAM, or else we get each address thrice (or more)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000281 * for each possible socket type (tcp,udp,raw...): */
282 hint.ai_socktype = SOCK_STREAM;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000283 hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000284 rc = getaddrinfo(host, NULL, &hint, &result);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000285 if (rc || !result) {
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000286 bb_error_msg("bad address '%s'", org_host);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000287 if (ai_flags & DIE_ON_ERROR)
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000288 xfunc_die();
Denis Vlasenko42823d52007-02-04 02:39:08 +0000289 goto ret;
290 }
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000291 used_res = result;
292#if ENABLE_FEATURE_PREFER_IPV4_ADDRESS
293 while (1) {
294 if (used_res->ai_family == AF_INET)
295 break;
296 used_res = used_res->ai_next;
297 if (!used_res) {
298 used_res = result;
299 break;
300 }
301 }
302#endif
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100303 r = xmalloc(LSA_LEN_SIZE + used_res->ai_addrlen);
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000304 r->len = used_res->ai_addrlen;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000305 memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen);
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100306
Denys Vlasenko75b73972009-11-24 14:06:04 +0100307 set_port:
Denys Vlasenkoca183112011-04-07 17:52:20 +0200308 set_nport(&r->u.sa, htons(port));
Denis Vlasenko42823d52007-02-04 02:39:08 +0000309 ret:
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100310 if (result)
311 freeaddrinfo(result);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000312 return r;
313}
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000314#if !ENABLE_FEATURE_IPV6
315#define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
316#endif
317
318#if ENABLE_FEATURE_IPV6
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000319len_and_sockaddr* FAST_FUNC host_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenkoe9913032007-02-04 02:41:57 +0000320{
321 return str2sockaddr(host, port, af, 0);
322}
323
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000324len_and_sockaddr* FAST_FUNC xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000325{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000326 return str2sockaddr(host, port, af, DIE_ON_ERROR);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000327}
328#endif
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000329
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000330len_and_sockaddr* FAST_FUNC host2sockaddr(const char *host, int port)
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000331{
332 return str2sockaddr(host, port, AF_UNSPEC, 0);
333}
334
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000335len_and_sockaddr* FAST_FUNC xhost2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000336{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000337 return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000338}
339
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000340len_and_sockaddr* FAST_FUNC xdotted2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000341{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000342 return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000343}
344
Jonh Wendell91061072012-02-09 15:14:33 +0100345int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, int family, int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000346{
Denis Vlasenko5d687242007-01-12 20:59:31 +0000347 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000348 int fd;
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000349 int len;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000350
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000351 if (family == AF_UNSPEC) {
Jonh Wendell91061072012-02-09 15:14:33 +0100352#if ENABLE_FEATURE_IPV6
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000353 fd = socket(AF_INET6, sock_type, 0);
354 if (fd >= 0) {
355 family = AF_INET6;
356 goto done;
357 }
Jonh Wendell91061072012-02-09 15:14:33 +0100358#endif
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000359 family = AF_INET;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000360 }
Jonh Wendell91061072012-02-09 15:14:33 +0100361
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000362 fd = xsocket(family, sock_type, 0);
Jonh Wendell91061072012-02-09 15:14:33 +0100363
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000364 len = sizeof(struct sockaddr_in);
Jonh Wendell91061072012-02-09 15:14:33 +0100365 if (family == AF_UNIX)
366 len = sizeof(struct sockaddr_un);
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000367#if ENABLE_FEATURE_IPV6
368 if (family == AF_INET6) {
369 done:
370 len = sizeof(struct sockaddr_in6);
371 }
372#endif
Denys Vlasenkob1278a32009-11-24 16:03:47 +0100373 lsa = xzalloc(LSA_LEN_SIZE + len);
Denis Vlasenko5d687242007-01-12 20:59:31 +0000374 lsa->len = len;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000375 lsa->u.sa.sa_family = family;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000376 *lsap = lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000377 return fd;
378}
379
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000380int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000381{
Jonh Wendell91061072012-02-09 15:14:33 +0100382 return xsocket_type(lsap, AF_UNSPEC, SOCK_STREAM);
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000383}
384
385static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000386{
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000387 int fd;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000388 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000389
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000390 if (bindaddr && bindaddr[0]) {
Denis Vlasenko42823d52007-02-04 02:39:08 +0000391 lsa = xdotted2sockaddr(bindaddr, port);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000392 /* user specified bind addr dictates family */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000393 fd = xsocket(lsa->u.sa.sa_family, sock_type, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000394 } else {
Jonh Wendell91061072012-02-09 15:14:33 +0100395 fd = xsocket_type(&lsa, AF_UNSPEC, sock_type);
Denys Vlasenkoca183112011-04-07 17:52:20 +0200396 set_nport(&lsa->u.sa, htons(port));
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000397 }
Denis Vlasenko48237b02006-11-22 23:22:06 +0000398 setsockopt_reuseaddr(fd);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000399 xbind(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000400 free(lsa);
401 return fd;
402}
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000403
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000404int FAST_FUNC create_and_bind_stream_or_die(const char *bindaddr, int port)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000405{
406 return create_and_bind_or_die(bindaddr, port, SOCK_STREAM);
407}
408
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000409int FAST_FUNC create_and_bind_dgram_or_die(const char *bindaddr, int port)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000410{
411 return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM);
412}
413
414
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000415int FAST_FUNC create_and_connect_stream_or_die(const char *peer, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000416{
417 int fd;
418 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000419
Denis Vlasenko42823d52007-02-04 02:39:08 +0000420 lsa = xhost2sockaddr(peer, port);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000421 fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000422 setsockopt_reuseaddr(fd);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000423 xconnect(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000424 free(lsa);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000425 return fd;
426}
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000427
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000428int FAST_FUNC xconnect_stream(const len_and_sockaddr *lsa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000429{
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000430 int fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
431 xconnect(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000432 return fd;
433}
434
Denis Vlasenko448f0242007-01-22 22:43:05 +0000435/* We hijack this constant to mean something else */
436/* It doesn't hurt because we will add this bit anyway */
437#define IGNORE_PORT NI_NUMERICSERV
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000438static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000439{
440 char host[128];
441 char serv[16];
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000442 int rc;
443 socklen_t salen;
444
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000445 if (ENABLE_FEATURE_UNIX_LOCAL && sa->sa_family == AF_UNIX) {
446 struct sockaddr_un *sun = (struct sockaddr_un *)sa;
447 return xasprintf("local:%.*s",
448 (int) sizeof(sun->sun_path),
449 sun->sun_path);
450 }
451
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000452 salen = LSA_SIZEOF_SA;
453#if ENABLE_FEATURE_IPV6
454 if (sa->sa_family == AF_INET)
455 salen = sizeof(struct sockaddr_in);
456 if (sa->sa_family == AF_INET6)
457 salen = sizeof(struct sockaddr_in6);
458#endif
459 rc = getnameinfo(sa, salen,
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000460 host, sizeof(host),
Denis Vlasenko448f0242007-01-22 22:43:05 +0000461 /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000462 serv, sizeof(serv),
Denis Vlasenko85629f02007-01-22 09:36:41 +0000463 /* do not resolve port# into service _name_ */
464 flags | NI_NUMERICSERV
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000465 );
Denis Vlasenko85629f02007-01-22 09:36:41 +0000466 if (rc)
467 return NULL;
Denis Vlasenko448f0242007-01-22 22:43:05 +0000468 if (flags & IGNORE_PORT)
469 return xstrdup(host);
Denis Vlasenko85629f02007-01-22 09:36:41 +0000470#if ENABLE_FEATURE_IPV6
Denis Vlasenko448f0242007-01-22 22:43:05 +0000471 if (sa->sa_family == AF_INET6) {
472 if (strchr(host, ':')) /* heh, it's not a resolved hostname */
473 return xasprintf("[%s]:%s", host, serv);
474 /*return xasprintf("%s:%s", host, serv);*/
475 /* - fall through instead */
476 }
Denis Vlasenko85629f02007-01-22 09:36:41 +0000477#endif
478 /* For now we don't support anything else, so it has to be INET */
479 /*if (sa->sa_family == AF_INET)*/
480 return xasprintf("%s:%s", host, serv);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000481 /*return xstrdup(host);*/
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000482}
483
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000484char* FAST_FUNC xmalloc_sockaddr2host(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000485{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000486 return sockaddr2str(sa, 0);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000487}
488
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000489char* FAST_FUNC xmalloc_sockaddr2host_noport(const struct sockaddr *sa)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000490{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000491 return sockaddr2str(sa, IGNORE_PORT);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000492}
493
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000494char* FAST_FUNC xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000495{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000496 return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000497}
Denys Vlasenko3c18e302016-09-26 19:53:04 +0200498#ifndef NI_NUMERICSCOPE
499# define NI_NUMERICSCOPE 0
500#endif
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000501char* FAST_FUNC xmalloc_sockaddr2dotted(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000502{
Denys Vlasenko3c18e302016-09-26 19:53:04 +0200503 return sockaddr2str(sa, NI_NUMERICHOST | NI_NUMERICSCOPE);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000504}
Denis Vlasenko448f0242007-01-22 22:43:05 +0000505
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000506char* FAST_FUNC xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000507{
Denys Vlasenko3c18e302016-09-26 19:53:04 +0200508 return sockaddr2str(sa, NI_NUMERICHOST | NI_NUMERICSCOPE | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000509}