blob: 1c8bb2b7309a7ad0c77a1107e54c78da5d633eaf [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
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000017void FAST_FUNC setsockopt_reuseaddr(int fd)
Denis Vlasenko48237b02006-11-22 23:22:06 +000018{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +000019 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &const_int_1, sizeof(const_int_1));
Denis Vlasenko48237b02006-11-22 23:22:06 +000020}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000021int FAST_FUNC setsockopt_broadcast(int fd)
Denis Vlasenko48237b02006-11-22 23:22:06 +000022{
Denis Vlasenko703e2022007-01-22 14:12:08 +000023 return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1));
Denis Vlasenko48237b02006-11-22 23:22:06 +000024}
Jérémie Koenigfbedacf2010-03-26 19:08:53 +010025
26#ifdef SO_BINDTODEVICE
Denis Vlasenkoe5373852008-12-10 11:12:16 +000027int FAST_FUNC setsockopt_bindtodevice(int fd, const char *iface)
28{
29 int r;
30 struct ifreq ifr;
31 strncpy_IFNAMSIZ(ifr.ifr_name, iface);
Denis Vlasenkoa3ca7cf2008-12-10 11:24:33 +000032 /* NB: passing (iface, strlen(iface) + 1) does not work!
33 * (maybe it works on _some_ kernels, but not on 2.6.26)
34 * Actually, ifr_name is at offset 0, and in practice
Denis Vlasenkoe5373852008-12-10 11:12:16 +000035 * just giving char[IFNAMSIZ] instead of struct ifreq works too.
36 * But just in case it's not true on some obscure arch... */
37 r = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
38 if (r)
39 bb_perror_msg("can't bind to interface %s", iface);
40 return r;
41}
Jérémie Koenigfbedacf2010-03-26 19:08:53 +010042#else
43int FAST_FUNC setsockopt_bindtodevice(int fd UNUSED_PARAM,
44 const char *iface UNUSED_PARAM)
45{
46 bb_error_msg("SO_BINDTODEVICE is not supported on this system");
47 return -1;
48}
49#endif
Denis Vlasenkoe5373852008-12-10 11:12:16 +000050
Denys Vlasenkof6916db2010-04-06 17:43:29 +020051static 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 +000052{
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000053 len_and_sockaddr lsa;
54 len_and_sockaddr *lsa_ptr;
Denis Vlasenko73c571a2009-03-09 00:12:37 +000055
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000056 lsa.len = LSA_SIZEOF_SA;
Denys Vlasenkof6916db2010-04-06 17:43:29 +020057 if (get_name(fd, &lsa.u.sa, &lsa.len) != 0)
Denis Vlasenko73c571a2009-03-09 00:12:37 +000058 return NULL;
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000059
60 lsa_ptr = xzalloc(LSA_LEN_SIZE + lsa.len);
61 if (lsa.len > LSA_SIZEOF_SA) { /* rarely (if ever) happens */
62 lsa_ptr->len = lsa.len;
Denys Vlasenkof6916db2010-04-06 17:43:29 +020063 get_name(fd, &lsa_ptr->u.sa, &lsa_ptr->len);
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000064 } else {
65 memcpy(lsa_ptr, &lsa, LSA_LEN_SIZE + lsa.len);
66 }
67 return lsa_ptr;
Denis Vlasenko73c571a2009-03-09 00:12:37 +000068}
Denis Vlasenko48237b02006-11-22 23:22:06 +000069
Denys Vlasenkof6916db2010-04-06 17:43:29 +020070len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd)
71{
72 return get_lsa(fd, getsockname);
73}
74
75len_and_sockaddr* FAST_FUNC get_peer_lsa(int fd)
76{
77 return get_lsa(fd, getpeername);
78}
79
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000080void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000081{
Denis Vlasenko9de420c2007-01-10 09:28:01 +000082 if (connect(s, s_addr, addrlen) < 0) {
83 if (ENABLE_FEATURE_CLEAN_UP)
84 close(s);
85 if (s_addr->sa_family == AF_INET)
86 bb_perror_msg_and_die("%s (%s)",
Denys Vlasenko6331cf02009-11-13 09:08:27 +010087 "can't connect to remote host",
Denis Vlasenko9de420c2007-01-10 09:28:01 +000088 inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
Denys Vlasenko6331cf02009-11-13 09:08:27 +010089 bb_perror_msg_and_die("can't connect to remote host");
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000090 }
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000091}
92
Denis Vlasenko5d687242007-01-12 20:59:31 +000093/* Return port number for a service.
Denis Vlasenko9de420c2007-01-10 09:28:01 +000094 * If "port" is a number use it as the port.
Denis Vlasenko73c571a2009-03-09 00:12:37 +000095 * If "port" is a name it is looked up in /etc/services,
96 * if it isnt found return default_port
97 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000098unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +000099{
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000100 unsigned port_nr = default_port;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000101 if (port) {
102 int old_errno;
103
104 /* Since this is a lib function, we're not allowed to reset errno to 0.
105 * Doing so could break an app that is deferring checking of errno. */
106 old_errno = errno;
107 port_nr = bb_strtou(port, NULL, 10);
108 if (errno || port_nr > 65535) {
109 struct servent *tserv = getservbyname(port, protocol);
Denis Vlasenko24182882007-01-13 21:05:38 +0000110 port_nr = default_port;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000111 if (tserv)
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000112 port_nr = ntohs(tserv->s_port);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000113 }
114 errno = old_errno;
115 }
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000116 return (uint16_t)port_nr;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000117}
118
119
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000120/* "New" networking API */
121
122
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000123int FAST_FUNC get_nport(const struct sockaddr *sa)
Denis Vlasenko5d687242007-01-12 20:59:31 +0000124{
125#if ENABLE_FEATURE_IPV6
Denis Vlasenko2856dab2007-04-01 01:18:20 +0000126 if (sa->sa_family == AF_INET6) {
127 return ((struct sockaddr_in6*)sa)->sin6_port;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000128 }
129#endif
Denis Vlasenko2856dab2007-04-01 01:18:20 +0000130 if (sa->sa_family == AF_INET) {
131 return ((struct sockaddr_in*)sa)->sin_port;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000132 }
Denis Vlasenko5d687242007-01-12 20:59:31 +0000133 /* What? UNIX socket? IPX?? :) */
Denis Vlasenko85629f02007-01-22 09:36:41 +0000134 return -1;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000135}
136
Denys Vlasenkoca183112011-04-07 17:52:20 +0200137void FAST_FUNC set_nport(struct sockaddr *sa, unsigned port)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000138{
139#if ENABLE_FEATURE_IPV6
Denys Vlasenkoca183112011-04-07 17:52:20 +0200140 if (sa->sa_family == AF_INET6) {
141 struct sockaddr_in6 *sin6 = (void*) sa;
142 sin6->sin6_port = port;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000143 return;
144 }
145#endif
Denys Vlasenkoca183112011-04-07 17:52:20 +0200146 if (sa->sa_family == AF_INET) {
147 struct sockaddr_in *sin = (void*) sa;
148 sin->sin_port = port;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000149 return;
150 }
151 /* What? UNIX socket? IPX?? :) */
152}
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000153
Denis Vlasenko42823d52007-02-04 02:39:08 +0000154/* We hijack this constant to mean something else */
155/* It doesn't hurt because we will remove this bit anyway */
156#define DIE_ON_ERROR AI_CANONNAME
157
Denis Vlasenko85629f02007-01-22 09:36:41 +0000158/* host: "1.2.3.4[:port]", "www.google.com[:port]"
Denis Vlasenko42823d52007-02-04 02:39:08 +0000159 * port: if neither of above specifies port # */
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000160static len_and_sockaddr* str2sockaddr(
161 const char *host, int port,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000162IF_FEATURE_IPV6(sa_family_t af,)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000163 int ai_flags)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000164{
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100165IF_NOT_FEATURE_IPV6(sa_family_t af = AF_INET;)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000166 int rc;
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000167 len_and_sockaddr *r;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000168 struct addrinfo *result = NULL;
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000169 struct addrinfo *used_res;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000170 const char *org_host = host; /* only for error msg */
171 const char *cp;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000172 struct addrinfo hint;
173
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000174 if (ENABLE_FEATURE_UNIX_LOCAL && strncmp(host, "local:", 6) == 0) {
175 struct sockaddr_un *sun;
176
177 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un));
178 r->len = sizeof(struct sockaddr_un);
179 r->u.sa.sa_family = AF_UNIX;
180 sun = (struct sockaddr_un *)&r->u.sa;
181 safe_strncpy(sun->sun_path, host + 6, sizeof(sun->sun_path));
182 return r;
183 }
184
185 r = NULL;
186
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000187 /* Ugly parsing of host:addr */
188 if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000189 /* Even uglier parsing of [xx]:nn */
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000190 host++;
191 cp = strchr(host, ']');
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000192 if (!cp || (cp[1] != ':' && cp[1] != '\0')) {
193 /* Malformed: must be [xx]:nn or [xx] */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000194 bb_error_msg("bad address '%s'", org_host);
195 if (ai_flags & DIE_ON_ERROR)
196 xfunc_die();
197 return NULL;
198 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000199 } else {
200 cp = strrchr(host, ':');
201 if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
202 /* There is more than one ':' (e.g. "::1") */
203 cp = NULL; /* it's not a port spec */
204 }
205 }
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000206 if (cp) { /* points to ":" or "]:" */
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000207 int sz = cp - host + 1;
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000208
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000209 host = safe_strncpy(alloca(sz), host, sz);
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000210 if (ENABLE_FEATURE_IPV6 && *cp != ':') {
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000211 cp++; /* skip ']' */
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000212 if (*cp == '\0') /* [xx] without port */
213 goto skip;
214 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000215 cp++; /* skip ':' */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000216 port = bb_strtou(cp, NULL, 10);
217 if (errno || (unsigned)port > 0xffff) {
218 bb_error_msg("bad port spec '%s'", org_host);
219 if (ai_flags & DIE_ON_ERROR)
220 xfunc_die();
221 return NULL;
222 }
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000223 skip: ;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000224 }
225
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100226 /* Next two if blocks allow to skip getaddrinfo()
Denys Vlasenko75b73972009-11-24 14:06:04 +0100227 * in case host name is a numeric IP(v6) address.
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100228 * getaddrinfo() initializes DNS resolution machinery,
229 * scans network config and such - tens of syscalls.
230 */
231 /* If we were not asked specifically for IPv6,
232 * check whether this is a numeric IPv4 */
233 IF_FEATURE_IPV6(if(af != AF_INET6)) {
234 struct in_addr in4;
235 if (inet_aton(host, &in4) != 0) {
236 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in));
237 r->len = sizeof(struct sockaddr_in);
238 r->u.sa.sa_family = AF_INET;
239 r->u.sin.sin_addr = in4;
240 goto set_port;
241 }
242 }
243#if ENABLE_FEATURE_IPV6
244 /* If we were not asked specifically for IPv4,
245 * check whether this is a numeric IPv6 */
246 if (af != AF_INET) {
247 struct in6_addr in6;
248 if (inet_pton(AF_INET6, host, &in6) > 0) {
249 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in6));
250 r->len = sizeof(struct sockaddr_in6);
251 r->u.sa.sa_family = AF_INET6;
252 r->u.sin6.sin6_addr = in6;
253 goto set_port;
254 }
255 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000256#endif
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100257
258 memset(&hint, 0 , sizeof(hint));
259 hint.ai_family = af;
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100260 /* Need SOCK_STREAM, or else we get each address thrice (or more)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000261 * for each possible socket type (tcp,udp,raw...): */
262 hint.ai_socktype = SOCK_STREAM;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000263 hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000264 rc = getaddrinfo(host, NULL, &hint, &result);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000265 if (rc || !result) {
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000266 bb_error_msg("bad address '%s'", org_host);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000267 if (ai_flags & DIE_ON_ERROR)
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000268 xfunc_die();
Denis Vlasenko42823d52007-02-04 02:39:08 +0000269 goto ret;
270 }
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000271 used_res = result;
272#if ENABLE_FEATURE_PREFER_IPV4_ADDRESS
273 while (1) {
274 if (used_res->ai_family == AF_INET)
275 break;
276 used_res = used_res->ai_next;
277 if (!used_res) {
278 used_res = result;
279 break;
280 }
281 }
282#endif
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100283 r = xmalloc(LSA_LEN_SIZE + used_res->ai_addrlen);
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000284 r->len = used_res->ai_addrlen;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000285 memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen);
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100286
Denys Vlasenko75b73972009-11-24 14:06:04 +0100287 set_port:
Denys Vlasenkoca183112011-04-07 17:52:20 +0200288 set_nport(&r->u.sa, htons(port));
Denis Vlasenko42823d52007-02-04 02:39:08 +0000289 ret:
Vitaly Magerya7f4b7692011-03-22 20:14:26 +0100290 if (result)
291 freeaddrinfo(result);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000292 return r;
293}
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000294#if !ENABLE_FEATURE_IPV6
295#define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
296#endif
297
298#if ENABLE_FEATURE_IPV6
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000299len_and_sockaddr* FAST_FUNC host_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenkoe9913032007-02-04 02:41:57 +0000300{
301 return str2sockaddr(host, port, af, 0);
302}
303
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000304len_and_sockaddr* FAST_FUNC xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000305{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000306 return str2sockaddr(host, port, af, DIE_ON_ERROR);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000307}
308#endif
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000309
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000310len_and_sockaddr* FAST_FUNC host2sockaddr(const char *host, int port)
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000311{
312 return str2sockaddr(host, port, AF_UNSPEC, 0);
313}
314
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000315len_and_sockaddr* FAST_FUNC xhost2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000316{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000317 return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000318}
319
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000320len_and_sockaddr* FAST_FUNC xdotted2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000321{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000322 return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000323}
324
Jonh Wendell91061072012-02-09 15:14:33 +0100325int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, int family, int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000326{
Denis Vlasenko5d687242007-01-12 20:59:31 +0000327 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000328 int fd;
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000329 int len;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000330
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000331 if (family == AF_UNSPEC) {
Jonh Wendell91061072012-02-09 15:14:33 +0100332#if ENABLE_FEATURE_IPV6
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000333 fd = socket(AF_INET6, sock_type, 0);
334 if (fd >= 0) {
335 family = AF_INET6;
336 goto done;
337 }
Jonh Wendell91061072012-02-09 15:14:33 +0100338#endif
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000339 family = AF_INET;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000340 }
Jonh Wendell91061072012-02-09 15:14:33 +0100341
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000342 fd = xsocket(family, sock_type, 0);
Jonh Wendell91061072012-02-09 15:14:33 +0100343
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000344 len = sizeof(struct sockaddr_in);
Jonh Wendell91061072012-02-09 15:14:33 +0100345 if (family == AF_UNIX)
346 len = sizeof(struct sockaddr_un);
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000347#if ENABLE_FEATURE_IPV6
348 if (family == AF_INET6) {
349 done:
350 len = sizeof(struct sockaddr_in6);
351 }
352#endif
Denys Vlasenkob1278a32009-11-24 16:03:47 +0100353 lsa = xzalloc(LSA_LEN_SIZE + len);
Denis Vlasenko5d687242007-01-12 20:59:31 +0000354 lsa->len = len;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000355 lsa->u.sa.sa_family = family;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000356 *lsap = lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000357 return fd;
358}
359
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000360int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000361{
Jonh Wendell91061072012-02-09 15:14:33 +0100362 return xsocket_type(lsap, AF_UNSPEC, SOCK_STREAM);
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000363}
364
365static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000366{
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000367 int fd;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000368 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000369
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000370 if (bindaddr && bindaddr[0]) {
Denis Vlasenko42823d52007-02-04 02:39:08 +0000371 lsa = xdotted2sockaddr(bindaddr, port);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000372 /* user specified bind addr dictates family */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000373 fd = xsocket(lsa->u.sa.sa_family, sock_type, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000374 } else {
Jonh Wendell91061072012-02-09 15:14:33 +0100375 fd = xsocket_type(&lsa, AF_UNSPEC, sock_type);
Denys Vlasenkoca183112011-04-07 17:52:20 +0200376 set_nport(&lsa->u.sa, htons(port));
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000377 }
Denis Vlasenko48237b02006-11-22 23:22:06 +0000378 setsockopt_reuseaddr(fd);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000379 xbind(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000380 free(lsa);
381 return fd;
382}
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000383
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000384int FAST_FUNC create_and_bind_stream_or_die(const char *bindaddr, int port)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000385{
386 return create_and_bind_or_die(bindaddr, port, SOCK_STREAM);
387}
388
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000389int FAST_FUNC create_and_bind_dgram_or_die(const char *bindaddr, int port)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000390{
391 return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM);
392}
393
394
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000395int FAST_FUNC create_and_connect_stream_or_die(const char *peer, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000396{
397 int fd;
398 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000399
Denis Vlasenko42823d52007-02-04 02:39:08 +0000400 lsa = xhost2sockaddr(peer, port);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000401 fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000402 setsockopt_reuseaddr(fd);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000403 xconnect(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000404 free(lsa);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000405 return fd;
406}
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000407
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000408int FAST_FUNC xconnect_stream(const len_and_sockaddr *lsa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000409{
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000410 int fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
411 xconnect(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000412 return fd;
413}
414
Denis Vlasenko448f0242007-01-22 22:43:05 +0000415/* We hijack this constant to mean something else */
416/* It doesn't hurt because we will add this bit anyway */
417#define IGNORE_PORT NI_NUMERICSERV
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000418static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000419{
420 char host[128];
421 char serv[16];
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000422 int rc;
423 socklen_t salen;
424
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000425 if (ENABLE_FEATURE_UNIX_LOCAL && sa->sa_family == AF_UNIX) {
426 struct sockaddr_un *sun = (struct sockaddr_un *)sa;
427 return xasprintf("local:%.*s",
428 (int) sizeof(sun->sun_path),
429 sun->sun_path);
430 }
431
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000432 salen = LSA_SIZEOF_SA;
433#if ENABLE_FEATURE_IPV6
434 if (sa->sa_family == AF_INET)
435 salen = sizeof(struct sockaddr_in);
436 if (sa->sa_family == AF_INET6)
437 salen = sizeof(struct sockaddr_in6);
438#endif
439 rc = getnameinfo(sa, salen,
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000440 host, sizeof(host),
Denis Vlasenko448f0242007-01-22 22:43:05 +0000441 /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000442 serv, sizeof(serv),
Denis Vlasenko85629f02007-01-22 09:36:41 +0000443 /* do not resolve port# into service _name_ */
444 flags | NI_NUMERICSERV
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000445 );
Denis Vlasenko85629f02007-01-22 09:36:41 +0000446 if (rc)
447 return NULL;
Denis Vlasenko448f0242007-01-22 22:43:05 +0000448 if (flags & IGNORE_PORT)
449 return xstrdup(host);
Denis Vlasenko85629f02007-01-22 09:36:41 +0000450#if ENABLE_FEATURE_IPV6
Denis Vlasenko448f0242007-01-22 22:43:05 +0000451 if (sa->sa_family == AF_INET6) {
452 if (strchr(host, ':')) /* heh, it's not a resolved hostname */
453 return xasprintf("[%s]:%s", host, serv);
454 /*return xasprintf("%s:%s", host, serv);*/
455 /* - fall through instead */
456 }
Denis Vlasenko85629f02007-01-22 09:36:41 +0000457#endif
458 /* For now we don't support anything else, so it has to be INET */
459 /*if (sa->sa_family == AF_INET)*/
460 return xasprintf("%s:%s", host, serv);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000461 /*return xstrdup(host);*/
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000462}
463
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000464char* FAST_FUNC xmalloc_sockaddr2host(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000465{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000466 return sockaddr2str(sa, 0);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000467}
468
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000469char* FAST_FUNC xmalloc_sockaddr2host_noport(const struct sockaddr *sa)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000470{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000471 return sockaddr2str(sa, IGNORE_PORT);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000472}
473
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000474char* FAST_FUNC xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000475{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000476 return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000477}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000478char* FAST_FUNC xmalloc_sockaddr2dotted(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000479{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000480 return sockaddr2str(sa, NI_NUMERICHOST);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000481}
Denis Vlasenko448f0242007-01-22 22:43:05 +0000482
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000483char* FAST_FUNC xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000484{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000485 return sockaddr2str(sa, NI_NUMERICHOST | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000486}