blob: 85f93b6395a25c8a007b64f2ebd323ad4940d961 [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 *
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00007 * Licensed under GPLv2, see file LICENSE in this tarball for details.
Eric Andersen0b315862002-07-03 11:51:44 +00008 */
9
Denis Vlasenko9de420c2007-01-10 09:28:01 +000010#include <netinet/in.h>
Denis Vlasenkoe5373852008-12-10 11:12:16 +000011#include <net/if.h>
Denis Vlasenkof6b46852009-04-25 13:16:53 +000012#include <sys/un.h>
Eric Andersen0b315862002-07-03 11:51:44 +000013#include "libbb.h"
14
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000015void FAST_FUNC setsockopt_reuseaddr(int fd)
Denis Vlasenko48237b02006-11-22 23:22:06 +000016{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +000017 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &const_int_1, sizeof(const_int_1));
Denis Vlasenko48237b02006-11-22 23:22:06 +000018}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000019int FAST_FUNC setsockopt_broadcast(int fd)
Denis Vlasenko48237b02006-11-22 23:22:06 +000020{
Denis Vlasenko703e2022007-01-22 14:12:08 +000021 return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1));
Denis Vlasenko48237b02006-11-22 23:22:06 +000022}
Denis Vlasenkoe5373852008-12-10 11:12:16 +000023int FAST_FUNC setsockopt_bindtodevice(int fd, const char *iface)
24{
25 int r;
26 struct ifreq ifr;
27 strncpy_IFNAMSIZ(ifr.ifr_name, iface);
Denis Vlasenkoa3ca7cf2008-12-10 11:24:33 +000028 /* NB: passing (iface, strlen(iface) + 1) does not work!
29 * (maybe it works on _some_ kernels, but not on 2.6.26)
30 * Actually, ifr_name is at offset 0, and in practice
Denis Vlasenkoe5373852008-12-10 11:12:16 +000031 * just giving char[IFNAMSIZ] instead of struct ifreq works too.
32 * But just in case it's not true on some obscure arch... */
33 r = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
34 if (r)
35 bb_perror_msg("can't bind to interface %s", iface);
36 return r;
37}
38
Denis Vlasenko73c571a2009-03-09 00:12:37 +000039len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd)
40{
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000041 len_and_sockaddr lsa;
42 len_and_sockaddr *lsa_ptr;
Denis Vlasenko73c571a2009-03-09 00:12:37 +000043
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000044 lsa.len = LSA_SIZEOF_SA;
45 if (getsockname(fd, &lsa.u.sa, &lsa.len) != 0)
Denis Vlasenko73c571a2009-03-09 00:12:37 +000046 return NULL;
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000047
48 lsa_ptr = xzalloc(LSA_LEN_SIZE + lsa.len);
49 if (lsa.len > LSA_SIZEOF_SA) { /* rarely (if ever) happens */
50 lsa_ptr->len = lsa.len;
51 getsockname(fd, &lsa_ptr->u.sa, &lsa_ptr->len);
52 } else {
53 memcpy(lsa_ptr, &lsa, LSA_LEN_SIZE + lsa.len);
54 }
55 return lsa_ptr;
Denis Vlasenko73c571a2009-03-09 00:12:37 +000056}
Denis Vlasenko48237b02006-11-22 23:22:06 +000057
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000058void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000059{
Denis Vlasenko9de420c2007-01-10 09:28:01 +000060 if (connect(s, s_addr, addrlen) < 0) {
61 if (ENABLE_FEATURE_CLEAN_UP)
62 close(s);
63 if (s_addr->sa_family == AF_INET)
64 bb_perror_msg_and_die("%s (%s)",
65 "cannot connect to remote host",
66 inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
67 bb_perror_msg_and_die("cannot connect to remote host");
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000068 }
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000069}
70
Denis Vlasenko5d687242007-01-12 20:59:31 +000071/* Return port number for a service.
Denis Vlasenko9de420c2007-01-10 09:28:01 +000072 * If "port" is a number use it as the port.
Denis Vlasenko73c571a2009-03-09 00:12:37 +000073 * If "port" is a name it is looked up in /etc/services,
74 * if it isnt found return default_port
75 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000076unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +000077{
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000078 unsigned port_nr = default_port;
Denis Vlasenko9de420c2007-01-10 09:28:01 +000079 if (port) {
80 int old_errno;
81
82 /* Since this is a lib function, we're not allowed to reset errno to 0.
83 * Doing so could break an app that is deferring checking of errno. */
84 old_errno = errno;
85 port_nr = bb_strtou(port, NULL, 10);
86 if (errno || port_nr > 65535) {
87 struct servent *tserv = getservbyname(port, protocol);
Denis Vlasenko24182882007-01-13 21:05:38 +000088 port_nr = default_port;
Denis Vlasenko9de420c2007-01-10 09:28:01 +000089 if (tserv)
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000090 port_nr = ntohs(tserv->s_port);
Denis Vlasenko9de420c2007-01-10 09:28:01 +000091 }
92 errno = old_errno;
93 }
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000094 return (uint16_t)port_nr;
Denis Vlasenko9de420c2007-01-10 09:28:01 +000095}
96
97
98/* "Old" networking API - only IPv4 */
99
Denis Vlasenko85629f02007-01-22 09:36:41 +0000100/*
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000101void FAST_FUNC bb_lookup_host(struct sockaddr_in *s_in, const char *host)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000102{
103 struct hostent *he;
104
105 memset(s_in, 0, sizeof(struct sockaddr_in));
106 s_in->sin_family = AF_INET;
107 he = xgethostbyname(host);
108 memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
109}
110
Denis Vlasenko85629f02007-01-22 09:36:41 +0000111
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000112int FAST_FUNC xconnect_tcp_v4(struct sockaddr_in *s_addr)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000113{
114 int s = xsocket(AF_INET, SOCK_STREAM, 0);
115 xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr));
116 return s;
117}
Denis Vlasenko85629f02007-01-22 09:36:41 +0000118*/
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000119
120/* "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
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000137void FAST_FUNC set_nport(len_and_sockaddr *lsa, unsigned port)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000138{
139#if ENABLE_FEATURE_IPV6
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000140 if (lsa->u.sa.sa_family == AF_INET6) {
141 lsa->u.sin6.sin6_port = port;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000142 return;
143 }
144#endif
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000145 if (lsa->u.sa.sa_family == AF_INET) {
146 lsa->u.sin.sin_port = port;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000147 return;
148 }
149 /* What? UNIX socket? IPX?? :) */
150}
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000151
Denis Vlasenko42823d52007-02-04 02:39:08 +0000152/* We hijack this constant to mean something else */
153/* It doesn't hurt because we will remove this bit anyway */
154#define DIE_ON_ERROR AI_CANONNAME
155
Denis Vlasenko85629f02007-01-22 09:36:41 +0000156/* host: "1.2.3.4[:port]", "www.google.com[:port]"
Denis Vlasenko42823d52007-02-04 02:39:08 +0000157 * port: if neither of above specifies port # */
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000158static len_and_sockaddr* str2sockaddr(
159 const char *host, int port,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000160IF_FEATURE_IPV6(sa_family_t af,)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000161 int ai_flags)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000162{
163 int rc;
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000164 len_and_sockaddr *r;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000165 struct addrinfo *result = NULL;
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000166 struct addrinfo *used_res;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000167 const char *org_host = host; /* only for error msg */
168 const char *cp;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000169 struct addrinfo hint;
170
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000171 if (ENABLE_FEATURE_UNIX_LOCAL && strncmp(host, "local:", 6) == 0) {
172 struct sockaddr_un *sun;
173
174 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un));
175 r->len = sizeof(struct sockaddr_un);
176 r->u.sa.sa_family = AF_UNIX;
177 sun = (struct sockaddr_un *)&r->u.sa;
178 safe_strncpy(sun->sun_path, host + 6, sizeof(sun->sun_path));
179 return r;
180 }
181
182 r = NULL;
183
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000184 /* Ugly parsing of host:addr */
185 if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000186 /* Even uglier parsing of [xx]:nn */
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000187 host++;
188 cp = strchr(host, ']');
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000189 if (!cp || (cp[1] != ':' && cp[1] != '\0')) {
190 /* Malformed: must be [xx]:nn or [xx] */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000191 bb_error_msg("bad address '%s'", org_host);
192 if (ai_flags & DIE_ON_ERROR)
193 xfunc_die();
194 return NULL;
195 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000196 } else {
197 cp = strrchr(host, ':');
198 if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
199 /* There is more than one ':' (e.g. "::1") */
200 cp = NULL; /* it's not a port spec */
201 }
202 }
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000203 if (cp) { /* points to ":" or "]:" */
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000204 int sz = cp - host + 1;
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000205
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000206 host = safe_strncpy(alloca(sz), host, sz);
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000207 if (ENABLE_FEATURE_IPV6 && *cp != ':') {
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000208 cp++; /* skip ']' */
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000209 if (*cp == '\0') /* [xx] without port */
210 goto skip;
211 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000212 cp++; /* skip ':' */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000213 port = bb_strtou(cp, NULL, 10);
214 if (errno || (unsigned)port > 0xffff) {
215 bb_error_msg("bad port spec '%s'", org_host);
216 if (ai_flags & DIE_ON_ERROR)
217 xfunc_die();
218 return NULL;
219 }
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000220 skip: ;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000221 }
222
223 memset(&hint, 0 , sizeof(hint));
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000224#if !ENABLE_FEATURE_IPV6
225 hint.ai_family = AF_INET; /* do not try to find IPv6 */
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000226#else
227 hint.ai_family = af;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000228#endif
229 /* Needed. Or else we will get each address thrice (or more)
230 * for each possible socket type (tcp,udp,raw...): */
231 hint.ai_socktype = SOCK_STREAM;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000232 hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000233 rc = getaddrinfo(host, NULL, &hint, &result);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000234 if (rc || !result) {
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000235 bb_error_msg("bad address '%s'", org_host);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000236 if (ai_flags & DIE_ON_ERROR)
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000237 xfunc_die();
Denis Vlasenko42823d52007-02-04 02:39:08 +0000238 goto ret;
239 }
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000240 used_res = result;
241#if ENABLE_FEATURE_PREFER_IPV4_ADDRESS
242 while (1) {
243 if (used_res->ai_family == AF_INET)
244 break;
245 used_res = used_res->ai_next;
246 if (!used_res) {
247 used_res = result;
248 break;
249 }
250 }
251#endif
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000252 r = xmalloc(offsetof(len_and_sockaddr, u.sa) + used_res->ai_addrlen);
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000253 r->len = used_res->ai_addrlen;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000254 memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen);
Denis Vlasenko5d687242007-01-12 20:59:31 +0000255 set_nport(r, htons(port));
Denis Vlasenko42823d52007-02-04 02:39:08 +0000256 ret:
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000257 freeaddrinfo(result);
258 return r;
259}
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000260#if !ENABLE_FEATURE_IPV6
261#define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
262#endif
263
264#if ENABLE_FEATURE_IPV6
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000265len_and_sockaddr* FAST_FUNC host_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenkoe9913032007-02-04 02:41:57 +0000266{
267 return str2sockaddr(host, port, af, 0);
268}
269
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000270len_and_sockaddr* FAST_FUNC xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000271{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000272 return str2sockaddr(host, port, af, DIE_ON_ERROR);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000273}
274#endif
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000275
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000276len_and_sockaddr* FAST_FUNC host2sockaddr(const char *host, int port)
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000277{
278 return str2sockaddr(host, port, AF_UNSPEC, 0);
279}
280
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000281len_and_sockaddr* FAST_FUNC xhost2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000282{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000283 return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000284}
285
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000286len_and_sockaddr* FAST_FUNC xdotted2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000287{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000288 return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000289}
290
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000291#undef xsocket_type
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000292int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, IF_FEATURE_IPV6(int family,) int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000293{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000294 IF_NOT_FEATURE_IPV6(enum { family = AF_INET };)
Denis Vlasenko5d687242007-01-12 20:59:31 +0000295 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000296 int fd;
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000297 int len;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000298
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000299#if ENABLE_FEATURE_IPV6
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000300 if (family == AF_UNSPEC) {
301 fd = socket(AF_INET6, sock_type, 0);
302 if (fd >= 0) {
303 family = AF_INET6;
304 goto done;
305 }
306 family = AF_INET;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000307 }
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000308#endif
309 fd = xsocket(family, sock_type, 0);
310 len = sizeof(struct sockaddr_in);
311#if ENABLE_FEATURE_IPV6
312 if (family == AF_INET6) {
313 done:
314 len = sizeof(struct sockaddr_in6);
315 }
316#endif
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000317 lsa = xzalloc(offsetof(len_and_sockaddr, u.sa) + len);
Denis Vlasenko5d687242007-01-12 20:59:31 +0000318 lsa->len = len;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000319 lsa->u.sa.sa_family = family;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000320 *lsap = lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000321 return fd;
322}
323
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000324int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000325{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000326 return xsocket_type(lsap, IF_FEATURE_IPV6(AF_UNSPEC,) SOCK_STREAM);
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000327}
328
329static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000330{
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000331 int fd;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000332 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000333
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000334 if (bindaddr && bindaddr[0]) {
Denis Vlasenko42823d52007-02-04 02:39:08 +0000335 lsa = xdotted2sockaddr(bindaddr, port);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000336 /* user specified bind addr dictates family */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000337 fd = xsocket(lsa->u.sa.sa_family, sock_type, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000338 } else {
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000339 fd = xsocket_type(&lsa, IF_FEATURE_IPV6(AF_UNSPEC,) sock_type);
Denis Vlasenko24182882007-01-13 21:05:38 +0000340 set_nport(lsa, htons(port));
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000341 }
Denis Vlasenko48237b02006-11-22 23:22:06 +0000342 setsockopt_reuseaddr(fd);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000343 xbind(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000344 free(lsa);
345 return fd;
346}
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000347
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000348int FAST_FUNC create_and_bind_stream_or_die(const char *bindaddr, int port)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000349{
350 return create_and_bind_or_die(bindaddr, port, SOCK_STREAM);
351}
352
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000353int FAST_FUNC create_and_bind_dgram_or_die(const char *bindaddr, int port)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000354{
355 return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM);
356}
357
358
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000359int FAST_FUNC create_and_connect_stream_or_die(const char *peer, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000360{
361 int fd;
362 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000363
Denis Vlasenko42823d52007-02-04 02:39:08 +0000364 lsa = xhost2sockaddr(peer, port);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000365 fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000366 setsockopt_reuseaddr(fd);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000367 xconnect(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000368 free(lsa);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000369 return fd;
370}
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000371
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000372int FAST_FUNC xconnect_stream(const len_and_sockaddr *lsa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000373{
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000374 int fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
375 xconnect(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000376 return fd;
377}
378
Denis Vlasenko448f0242007-01-22 22:43:05 +0000379/* We hijack this constant to mean something else */
380/* It doesn't hurt because we will add this bit anyway */
381#define IGNORE_PORT NI_NUMERICSERV
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000382static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000383{
384 char host[128];
385 char serv[16];
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000386 int rc;
387 socklen_t salen;
388
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000389 if (ENABLE_FEATURE_UNIX_LOCAL && sa->sa_family == AF_UNIX) {
390 struct sockaddr_un *sun = (struct sockaddr_un *)sa;
391 return xasprintf("local:%.*s",
392 (int) sizeof(sun->sun_path),
393 sun->sun_path);
394 }
395
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000396 salen = LSA_SIZEOF_SA;
397#if ENABLE_FEATURE_IPV6
398 if (sa->sa_family == AF_INET)
399 salen = sizeof(struct sockaddr_in);
400 if (sa->sa_family == AF_INET6)
401 salen = sizeof(struct sockaddr_in6);
402#endif
403 rc = getnameinfo(sa, salen,
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000404 host, sizeof(host),
Denis Vlasenko448f0242007-01-22 22:43:05 +0000405 /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000406 serv, sizeof(serv),
Denis Vlasenko85629f02007-01-22 09:36:41 +0000407 /* do not resolve port# into service _name_ */
408 flags | NI_NUMERICSERV
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000409 );
Denis Vlasenko85629f02007-01-22 09:36:41 +0000410 if (rc)
411 return NULL;
Denis Vlasenko448f0242007-01-22 22:43:05 +0000412 if (flags & IGNORE_PORT)
413 return xstrdup(host);
Denis Vlasenko85629f02007-01-22 09:36:41 +0000414#if ENABLE_FEATURE_IPV6
Denis Vlasenko448f0242007-01-22 22:43:05 +0000415 if (sa->sa_family == AF_INET6) {
416 if (strchr(host, ':')) /* heh, it's not a resolved hostname */
417 return xasprintf("[%s]:%s", host, serv);
418 /*return xasprintf("%s:%s", host, serv);*/
419 /* - fall through instead */
420 }
Denis Vlasenko85629f02007-01-22 09:36:41 +0000421#endif
422 /* For now we don't support anything else, so it has to be INET */
423 /*if (sa->sa_family == AF_INET)*/
424 return xasprintf("%s:%s", host, serv);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000425 /*return xstrdup(host);*/
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000426}
427
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000428char* FAST_FUNC xmalloc_sockaddr2host(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000429{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000430 return sockaddr2str(sa, 0);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000431}
432
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000433char* FAST_FUNC xmalloc_sockaddr2host_noport(const struct sockaddr *sa)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000434{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000435 return sockaddr2str(sa, IGNORE_PORT);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000436}
437
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000438char* FAST_FUNC xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000439{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000440 return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000441}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000442char* FAST_FUNC xmalloc_sockaddr2dotted(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000443{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000444 return sockaddr2str(sa, NI_NUMERICHOST);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000445}
Denis Vlasenko448f0242007-01-22 22:43:05 +0000446
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000447char* FAST_FUNC xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000448{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000449 return sockaddr2str(sa, NI_NUMERICHOST | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000450}