blob: 6e78e636367296ea3a430fe9b1cfef6ed439b3ce [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 */
9
Dan Fandrichfdd7b562010-06-18 22:37:42 -070010#include <sys/types.h>
Denys Vlasenko043b1e52009-09-06 12:47:55 +020011#include <sys/socket.h> /* netinet/in.h needs it */
Denis Vlasenko9de420c2007-01-10 09:28:01 +000012#include <netinet/in.h>
Denis Vlasenkoe5373852008-12-10 11:12:16 +000013#include <net/if.h>
Denis Vlasenkof6b46852009-04-25 13:16:53 +000014#include <sys/un.h>
Eric Andersen0b315862002-07-03 11:51:44 +000015#include "libbb.h"
16
Denys Vlasenkoc52cbea2015-08-24 19:48:03 +020017int FAST_FUNC setsockopt_int(int fd, int level, int optname, int optval)
18{
19 return setsockopt(fd, level, optname, &optval, sizeof(int));
20}
21int FAST_FUNC setsockopt_1(int fd, int level, int optname)
22{
23 return setsockopt_int(fd, level, optname, 1);
24}
25int FAST_FUNC setsockopt_SOL_SOCKET_int(int fd, int optname, int optval)
26{
27 return setsockopt_int(fd, SOL_SOCKET, optname, optval);
28}
29int FAST_FUNC setsockopt_SOL_SOCKET_1(int fd, int optname)
30{
31 return setsockopt_SOL_SOCKET_int(fd, optname, 1);
32}
33
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000034void FAST_FUNC setsockopt_reuseaddr(int fd)
Denis Vlasenko48237b02006-11-22 23:22:06 +000035{
Denys Vlasenkoc52cbea2015-08-24 19:48:03 +020036 setsockopt_SOL_SOCKET_1(fd, SO_REUSEADDR);
Denis Vlasenko48237b02006-11-22 23:22:06 +000037}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000038int FAST_FUNC setsockopt_broadcast(int fd)
Denis Vlasenko48237b02006-11-22 23:22:06 +000039{
Denys Vlasenkoc52cbea2015-08-24 19:48:03 +020040 return setsockopt_SOL_SOCKET_1(fd, SO_BROADCAST);
41}
42int FAST_FUNC setsockopt_keepalive(int fd)
43{
44 return setsockopt_SOL_SOCKET_1(fd, SO_KEEPALIVE);
Denis Vlasenko48237b02006-11-22 23:22:06 +000045}
Jérémie Koenigfbedacf2010-03-26 19:08:53 +010046
47#ifdef SO_BINDTODEVICE
Denis Vlasenkoe5373852008-12-10 11:12:16 +000048int FAST_FUNC setsockopt_bindtodevice(int fd, const char *iface)
49{
50 int r;
51 struct ifreq ifr;
52 strncpy_IFNAMSIZ(ifr.ifr_name, iface);
Denis Vlasenkoa3ca7cf2008-12-10 11:24:33 +000053 /* NB: passing (iface, strlen(iface) + 1) does not work!
54 * (maybe it works on _some_ kernels, but not on 2.6.26)
55 * Actually, ifr_name is at offset 0, and in practice
Denis Vlasenkoe5373852008-12-10 11:12:16 +000056 * just giving char[IFNAMSIZ] instead of struct ifreq works too.
57 * But just in case it's not true on some obscure arch... */
58 r = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
59 if (r)
60 bb_perror_msg("can't bind to interface %s", iface);
61 return r;
62}
Jérémie Koenigfbedacf2010-03-26 19:08:53 +010063#else
64int FAST_FUNC setsockopt_bindtodevice(int fd UNUSED_PARAM,
65 const char *iface UNUSED_PARAM)
66{
67 bb_error_msg("SO_BINDTODEVICE is not supported on this system");
68 return -1;
69}
70#endif
Denis Vlasenkoe5373852008-12-10 11:12:16 +000071
Denys Vlasenkof6916db2010-04-06 17:43:29 +020072static 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 +000073{
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000074 len_and_sockaddr lsa;
75 len_and_sockaddr *lsa_ptr;
Denis Vlasenko73c571a2009-03-09 00:12:37 +000076
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000077 lsa.len = LSA_SIZEOF_SA;
Denys Vlasenkof6916db2010-04-06 17:43:29 +020078 if (get_name(fd, &lsa.u.sa, &lsa.len) != 0)
Denis Vlasenko73c571a2009-03-09 00:12:37 +000079 return NULL;
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000080
81 lsa_ptr = xzalloc(LSA_LEN_SIZE + lsa.len);
82 if (lsa.len > LSA_SIZEOF_SA) { /* rarely (if ever) happens */
83 lsa_ptr->len = lsa.len;
Denys Vlasenkof6916db2010-04-06 17:43:29 +020084 get_name(fd, &lsa_ptr->u.sa, &lsa_ptr->len);
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000085 } else {
86 memcpy(lsa_ptr, &lsa, LSA_LEN_SIZE + lsa.len);
87 }
88 return lsa_ptr;
Denis Vlasenko73c571a2009-03-09 00:12:37 +000089}
Denis Vlasenko48237b02006-11-22 23:22:06 +000090
Denys Vlasenkof6916db2010-04-06 17:43:29 +020091len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd)
92{
93 return get_lsa(fd, getsockname);
94}
95
96len_and_sockaddr* FAST_FUNC get_peer_lsa(int fd)
97{
98 return get_lsa(fd, getpeername);
99}
100
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000101void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000102{
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000103 if (connect(s, s_addr, addrlen) < 0) {
104 if (ENABLE_FEATURE_CLEAN_UP)
105 close(s);
106 if (s_addr->sa_family == AF_INET)
107 bb_perror_msg_and_die("%s (%s)",
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100108 "can't connect to remote host",
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000109 inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100110 bb_perror_msg_and_die("can't connect to remote host");
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000111 }
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000112}
113
Denis Vlasenko5d687242007-01-12 20:59:31 +0000114/* Return port number for a service.
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000115 * If "port" is a number use it as the port.
Denis Vlasenko73c571a2009-03-09 00:12:37 +0000116 * If "port" is a name it is looked up in /etc/services,
117 * if it isnt found return default_port
118 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000119unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000120{
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000121 unsigned port_nr = default_port;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000122 if (port) {
123 int old_errno;
124
125 /* Since this is a lib function, we're not allowed to reset errno to 0.
126 * Doing so could break an app that is deferring checking of errno. */
127 old_errno = errno;
128 port_nr = bb_strtou(port, NULL, 10);
129 if (errno || port_nr > 65535) {
130 struct servent *tserv = getservbyname(port, protocol);
Denis Vlasenko24182882007-01-13 21:05:38 +0000131 port_nr = default_port;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000132 if (tserv)
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000133 port_nr = ntohs(tserv->s_port);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000134 }
135 errno = old_errno;
136 }
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000137 return (uint16_t)port_nr;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000138}
139
140
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000141/* "New" networking API */
142
143
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000144int FAST_FUNC get_nport(const struct sockaddr *sa)
Denis Vlasenko5d687242007-01-12 20:59:31 +0000145{
146#if ENABLE_FEATURE_IPV6
Denis Vlasenko2856dab2007-04-01 01:18:20 +0000147 if (sa->sa_family == AF_INET6) {
148 return ((struct sockaddr_in6*)sa)->sin6_port;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000149 }
150#endif
Denis Vlasenko2856dab2007-04-01 01:18:20 +0000151 if (sa->sa_family == AF_INET) {
152 return ((struct sockaddr_in*)sa)->sin_port;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000153 }
Denis Vlasenko5d687242007-01-12 20:59:31 +0000154 /* What? UNIX socket? IPX?? :) */
Denis Vlasenko85629f02007-01-22 09:36:41 +0000155 return -1;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000156}
157
Denys Vlasenkoca183112011-04-07 17:52:20 +0200158void FAST_FUNC set_nport(struct sockaddr *sa, unsigned port)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000159{
160#if ENABLE_FEATURE_IPV6
Denys Vlasenkoca183112011-04-07 17:52:20 +0200161 if (sa->sa_family == AF_INET6) {
162 struct sockaddr_in6 *sin6 = (void*) sa;
163 sin6->sin6_port = port;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000164 return;
165 }
166#endif
Denys Vlasenkoca183112011-04-07 17:52:20 +0200167 if (sa->sa_family == AF_INET) {
168 struct sockaddr_in *sin = (void*) sa;
169 sin->sin_port = port;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000170 return;
171 }
172 /* What? UNIX socket? IPX?? :) */
173}
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000174
Denis Vlasenko42823d52007-02-04 02:39:08 +0000175/* We hijack this constant to mean something else */
176/* It doesn't hurt because we will remove this bit anyway */
177#define DIE_ON_ERROR AI_CANONNAME
178
Denis Vlasenko85629f02007-01-22 09:36:41 +0000179/* host: "1.2.3.4[:port]", "www.google.com[:port]"
Denis Vlasenko42823d52007-02-04 02:39:08 +0000180 * port: if neither of above specifies port # */
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000181static len_and_sockaddr* str2sockaddr(
182 const char *host, int port,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000183IF_FEATURE_IPV6(sa_family_t af,)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000184 int ai_flags)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000185{
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100186IF_NOT_FEATURE_IPV6(sa_family_t af = AF_INET;)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000187 int rc;
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000188 len_and_sockaddr *r;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000189 struct addrinfo *result = NULL;
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000190 struct addrinfo *used_res;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000191 const char *org_host = host; /* only for error msg */
192 const char *cp;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000193 struct addrinfo hint;
194
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100195 if (ENABLE_FEATURE_UNIX_LOCAL && is_prefixed_with(host, "local:")) {
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000196 struct sockaddr_un *sun;
197
198 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un));
199 r->len = sizeof(struct sockaddr_un);
200 r->u.sa.sa_family = AF_UNIX;
201 sun = (struct sockaddr_un *)&r->u.sa;
202 safe_strncpy(sun->sun_path, host + 6, sizeof(sun->sun_path));
203 return r;
204 }
205
206 r = NULL;
207
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000208 /* Ugly parsing of host:addr */
209 if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000210 /* Even uglier parsing of [xx]:nn */
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000211 host++;
212 cp = strchr(host, ']');
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000213 if (!cp || (cp[1] != ':' && cp[1] != '\0')) {
214 /* Malformed: must be [xx]:nn or [xx] */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000215 bb_error_msg("bad address '%s'", org_host);
216 if (ai_flags & DIE_ON_ERROR)
217 xfunc_die();
218 return NULL;
219 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000220 } else {
221 cp = strrchr(host, ':');
222 if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
223 /* There is more than one ':' (e.g. "::1") */
224 cp = NULL; /* it's not a port spec */
225 }
226 }
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000227 if (cp) { /* points to ":" or "]:" */
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000228 int sz = cp - host + 1;
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000229
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000230 host = safe_strncpy(alloca(sz), host, sz);
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000231 if (ENABLE_FEATURE_IPV6 && *cp != ':') {
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000232 cp++; /* skip ']' */
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000233 if (*cp == '\0') /* [xx] without port */
234 goto skip;
235 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000236 cp++; /* skip ':' */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000237 port = bb_strtou(cp, NULL, 10);
238 if (errno || (unsigned)port > 0xffff) {
239 bb_error_msg("bad port spec '%s'", org_host);
240 if (ai_flags & DIE_ON_ERROR)
241 xfunc_die();
242 return NULL;
243 }
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000244 skip: ;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000245 }
246
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100247 /* Next two if blocks allow to skip getaddrinfo()
Denys Vlasenko75b73972009-11-24 14:06:04 +0100248 * in case host name is a numeric IP(v6) address.
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100249 * getaddrinfo() initializes DNS resolution machinery,
250 * scans network config and such - tens of syscalls.
251 */
252 /* If we were not asked specifically for IPv6,
253 * check whether this is a numeric IPv4 */
254 IF_FEATURE_IPV6(if(af != AF_INET6)) {
255 struct in_addr in4;
256 if (inet_aton(host, &in4) != 0) {
257 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in));
258 r->len = sizeof(struct sockaddr_in);
259 r->u.sa.sa_family = AF_INET;
260 r->u.sin.sin_addr = in4;
261 goto set_port;
262 }
263 }
264#if ENABLE_FEATURE_IPV6
265 /* If we were not asked specifically for IPv4,
266 * check whether this is a numeric IPv6 */
267 if (af != AF_INET) {
268 struct in6_addr in6;
269 if (inet_pton(AF_INET6, host, &in6) > 0) {
270 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in6));
271 r->len = sizeof(struct sockaddr_in6);
272 r->u.sa.sa_family = AF_INET6;
273 r->u.sin6.sin6_addr = in6;
274 goto set_port;
275 }
276 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000277#endif
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100278
279 memset(&hint, 0 , sizeof(hint));
280 hint.ai_family = af;
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100281 /* Need SOCK_STREAM, or else we get each address thrice (or more)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000282 * for each possible socket type (tcp,udp,raw...): */
283 hint.ai_socktype = SOCK_STREAM;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000284 hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000285 rc = getaddrinfo(host, NULL, &hint, &result);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000286 if (rc || !result) {
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000287 bb_error_msg("bad address '%s'", org_host);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000288 if (ai_flags & DIE_ON_ERROR)
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000289 xfunc_die();
Denis Vlasenko42823d52007-02-04 02:39:08 +0000290 goto ret;
291 }
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000292 used_res = result;
293#if ENABLE_FEATURE_PREFER_IPV4_ADDRESS
294 while (1) {
295 if (used_res->ai_family == AF_INET)
296 break;
297 used_res = used_res->ai_next;
298 if (!used_res) {
299 used_res = result;
300 break;
301 }
302 }
303#endif
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100304 r = xmalloc(LSA_LEN_SIZE + used_res->ai_addrlen);
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000305 r->len = used_res->ai_addrlen;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000306 memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen);
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100307
Denys Vlasenko75b73972009-11-24 14:06:04 +0100308 set_port:
Denys Vlasenkoca183112011-04-07 17:52:20 +0200309 set_nport(&r->u.sa, htons(port));
Denis Vlasenko42823d52007-02-04 02:39:08 +0000310 ret:
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100311 if (result)
312 freeaddrinfo(result);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000313 return r;
314}
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000315#if !ENABLE_FEATURE_IPV6
316#define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
317#endif
318
319#if ENABLE_FEATURE_IPV6
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000320len_and_sockaddr* FAST_FUNC host_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenkoe9913032007-02-04 02:41:57 +0000321{
322 return str2sockaddr(host, port, af, 0);
323}
324
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000325len_and_sockaddr* FAST_FUNC xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000326{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000327 return str2sockaddr(host, port, af, DIE_ON_ERROR);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000328}
329#endif
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000330
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000331len_and_sockaddr* FAST_FUNC host2sockaddr(const char *host, int port)
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000332{
333 return str2sockaddr(host, port, AF_UNSPEC, 0);
334}
335
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000336len_and_sockaddr* FAST_FUNC xhost2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000337{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000338 return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000339}
340
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000341len_and_sockaddr* FAST_FUNC xdotted2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000342{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000343 return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000344}
345
Jonh Wendell91061072012-02-09 15:14:33 +0100346int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, int family, int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000347{
Denis Vlasenko5d687242007-01-12 20:59:31 +0000348 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000349 int fd;
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000350 int len;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000351
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000352 if (family == AF_UNSPEC) {
Jonh Wendell91061072012-02-09 15:14:33 +0100353#if ENABLE_FEATURE_IPV6
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000354 fd = socket(AF_INET6, sock_type, 0);
355 if (fd >= 0) {
356 family = AF_INET6;
357 goto done;
358 }
Jonh Wendell91061072012-02-09 15:14:33 +0100359#endif
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000360 family = AF_INET;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000361 }
Jonh Wendell91061072012-02-09 15:14:33 +0100362
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000363 fd = xsocket(family, sock_type, 0);
Jonh Wendell91061072012-02-09 15:14:33 +0100364
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000365 len = sizeof(struct sockaddr_in);
Jonh Wendell91061072012-02-09 15:14:33 +0100366 if (family == AF_UNIX)
367 len = sizeof(struct sockaddr_un);
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000368#if ENABLE_FEATURE_IPV6
369 if (family == AF_INET6) {
370 done:
371 len = sizeof(struct sockaddr_in6);
372 }
373#endif
Denys Vlasenkob1278a32009-11-24 16:03:47 +0100374 lsa = xzalloc(LSA_LEN_SIZE + len);
Denis Vlasenko5d687242007-01-12 20:59:31 +0000375 lsa->len = len;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000376 lsa->u.sa.sa_family = family;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000377 *lsap = lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000378 return fd;
379}
380
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000381int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000382{
Jonh Wendell91061072012-02-09 15:14:33 +0100383 return xsocket_type(lsap, AF_UNSPEC, SOCK_STREAM);
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000384}
385
386static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000387{
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000388 int fd;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000389 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000390
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000391 if (bindaddr && bindaddr[0]) {
Denis Vlasenko42823d52007-02-04 02:39:08 +0000392 lsa = xdotted2sockaddr(bindaddr, port);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000393 /* user specified bind addr dictates family */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000394 fd = xsocket(lsa->u.sa.sa_family, sock_type, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000395 } else {
Jonh Wendell91061072012-02-09 15:14:33 +0100396 fd = xsocket_type(&lsa, AF_UNSPEC, sock_type);
Denys Vlasenkoca183112011-04-07 17:52:20 +0200397 set_nport(&lsa->u.sa, htons(port));
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000398 }
Denis Vlasenko48237b02006-11-22 23:22:06 +0000399 setsockopt_reuseaddr(fd);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000400 xbind(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000401 free(lsa);
402 return fd;
403}
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000404
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000405int FAST_FUNC create_and_bind_stream_or_die(const char *bindaddr, int port)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000406{
407 return create_and_bind_or_die(bindaddr, port, SOCK_STREAM);
408}
409
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000410int FAST_FUNC create_and_bind_dgram_or_die(const char *bindaddr, int port)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000411{
412 return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM);
413}
414
415
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000416int FAST_FUNC create_and_connect_stream_or_die(const char *peer, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000417{
418 int fd;
419 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000420
Denis Vlasenko42823d52007-02-04 02:39:08 +0000421 lsa = xhost2sockaddr(peer, port);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000422 fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000423 setsockopt_reuseaddr(fd);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000424 xconnect(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000425 free(lsa);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000426 return fd;
427}
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000428
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000429int FAST_FUNC xconnect_stream(const len_and_sockaddr *lsa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000430{
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000431 int fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
432 xconnect(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000433 return fd;
434}
435
Denis Vlasenko448f0242007-01-22 22:43:05 +0000436/* We hijack this constant to mean something else */
437/* It doesn't hurt because we will add this bit anyway */
438#define IGNORE_PORT NI_NUMERICSERV
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000439static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000440{
441 char host[128];
442 char serv[16];
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000443 int rc;
444 socklen_t salen;
445
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000446 if (ENABLE_FEATURE_UNIX_LOCAL && sa->sa_family == AF_UNIX) {
447 struct sockaddr_un *sun = (struct sockaddr_un *)sa;
448 return xasprintf("local:%.*s",
449 (int) sizeof(sun->sun_path),
450 sun->sun_path);
451 }
452
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000453 salen = LSA_SIZEOF_SA;
454#if ENABLE_FEATURE_IPV6
455 if (sa->sa_family == AF_INET)
456 salen = sizeof(struct sockaddr_in);
457 if (sa->sa_family == AF_INET6)
458 salen = sizeof(struct sockaddr_in6);
459#endif
460 rc = getnameinfo(sa, salen,
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000461 host, sizeof(host),
Denis Vlasenko448f0242007-01-22 22:43:05 +0000462 /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000463 serv, sizeof(serv),
Denis Vlasenko85629f02007-01-22 09:36:41 +0000464 /* do not resolve port# into service _name_ */
465 flags | NI_NUMERICSERV
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000466 );
Denis Vlasenko85629f02007-01-22 09:36:41 +0000467 if (rc)
468 return NULL;
Denis Vlasenko448f0242007-01-22 22:43:05 +0000469 if (flags & IGNORE_PORT)
470 return xstrdup(host);
Denis Vlasenko85629f02007-01-22 09:36:41 +0000471#if ENABLE_FEATURE_IPV6
Denis Vlasenko448f0242007-01-22 22:43:05 +0000472 if (sa->sa_family == AF_INET6) {
473 if (strchr(host, ':')) /* heh, it's not a resolved hostname */
474 return xasprintf("[%s]:%s", host, serv);
475 /*return xasprintf("%s:%s", host, serv);*/
476 /* - fall through instead */
477 }
Denis Vlasenko85629f02007-01-22 09:36:41 +0000478#endif
479 /* For now we don't support anything else, so it has to be INET */
480 /*if (sa->sa_family == AF_INET)*/
481 return xasprintf("%s:%s", host, serv);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000482 /*return xstrdup(host);*/
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000483}
484
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000485char* FAST_FUNC xmalloc_sockaddr2host(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000486{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000487 return sockaddr2str(sa, 0);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000488}
489
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000490char* FAST_FUNC xmalloc_sockaddr2host_noport(const struct sockaddr *sa)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000491{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000492 return sockaddr2str(sa, IGNORE_PORT);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000493}
494
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000495char* FAST_FUNC xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000496{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000497 return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000498}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000499char* FAST_FUNC xmalloc_sockaddr2dotted(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000500{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000501 return sockaddr2str(sa, NI_NUMERICHOST);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000502}
Denis Vlasenko448f0242007-01-22 22:43:05 +0000503
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000504char* FAST_FUNC xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000505{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000506 return sockaddr2str(sa, NI_NUMERICHOST | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000507}