blob: 975844500df80e6c90fa12aa6dbf8dbd309069a2 [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>
Eric Andersen0b315862002-07-03 11:51:44 +000012#include "libbb.h"
13
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000014void FAST_FUNC setsockopt_reuseaddr(int fd)
Denis Vlasenko48237b02006-11-22 23:22:06 +000015{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +000016 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &const_int_1, sizeof(const_int_1));
Denis Vlasenko48237b02006-11-22 23:22:06 +000017}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000018int FAST_FUNC setsockopt_broadcast(int fd)
Denis Vlasenko48237b02006-11-22 23:22:06 +000019{
Denis Vlasenko703e2022007-01-22 14:12:08 +000020 return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1));
Denis Vlasenko48237b02006-11-22 23:22:06 +000021}
Denis Vlasenkoe5373852008-12-10 11:12:16 +000022int FAST_FUNC setsockopt_bindtodevice(int fd, const char *iface)
23{
24 int r;
25 struct ifreq ifr;
26 strncpy_IFNAMSIZ(ifr.ifr_name, iface);
Denis Vlasenkoa3ca7cf2008-12-10 11:24:33 +000027 /* NB: passing (iface, strlen(iface) + 1) does not work!
28 * (maybe it works on _some_ kernels, but not on 2.6.26)
29 * Actually, ifr_name is at offset 0, and in practice
Denis Vlasenkoe5373852008-12-10 11:12:16 +000030 * just giving char[IFNAMSIZ] instead of struct ifreq works too.
31 * But just in case it's not true on some obscure arch... */
32 r = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
33 if (r)
34 bb_perror_msg("can't bind to interface %s", iface);
35 return r;
36}
37
Denis Vlasenko73c571a2009-03-09 00:12:37 +000038len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd)
39{
40 len_and_sockaddr *lsa;
41 socklen_t len = 0;
42
43 /* Can be optimized to do only one getsockname() */
44 if (getsockname(fd, NULL, &len) != 0)
45 return NULL;
46 lsa = xzalloc(LSA_LEN_SIZE + len);
47 lsa->len = len;
48 getsockname(fd, &lsa->u.sa, &lsa->len);
49 return lsa;
50}
Denis Vlasenko48237b02006-11-22 23:22:06 +000051
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000052void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000053{
Denis Vlasenko9de420c2007-01-10 09:28:01 +000054 if (connect(s, s_addr, addrlen) < 0) {
55 if (ENABLE_FEATURE_CLEAN_UP)
56 close(s);
57 if (s_addr->sa_family == AF_INET)
58 bb_perror_msg_and_die("%s (%s)",
59 "cannot connect to remote host",
60 inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
61 bb_perror_msg_and_die("cannot connect to remote host");
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000062 }
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000063}
64
Denis Vlasenko5d687242007-01-12 20:59:31 +000065/* Return port number for a service.
Denis Vlasenko9de420c2007-01-10 09:28:01 +000066 * If "port" is a number use it as the port.
Denis Vlasenko73c571a2009-03-09 00:12:37 +000067 * If "port" is a name it is looked up in /etc/services,
68 * if it isnt found return default_port
69 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000070unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +000071{
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000072 unsigned port_nr = default_port;
Denis Vlasenko9de420c2007-01-10 09:28:01 +000073 if (port) {
74 int old_errno;
75
76 /* Since this is a lib function, we're not allowed to reset errno to 0.
77 * Doing so could break an app that is deferring checking of errno. */
78 old_errno = errno;
79 port_nr = bb_strtou(port, NULL, 10);
80 if (errno || port_nr > 65535) {
81 struct servent *tserv = getservbyname(port, protocol);
Denis Vlasenko24182882007-01-13 21:05:38 +000082 port_nr = default_port;
Denis Vlasenko9de420c2007-01-10 09:28:01 +000083 if (tserv)
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000084 port_nr = ntohs(tserv->s_port);
Denis Vlasenko9de420c2007-01-10 09:28:01 +000085 }
86 errno = old_errno;
87 }
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000088 return (uint16_t)port_nr;
Denis Vlasenko9de420c2007-01-10 09:28:01 +000089}
90
91
92/* "Old" networking API - only IPv4 */
93
Denis Vlasenko85629f02007-01-22 09:36:41 +000094/*
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000095void FAST_FUNC bb_lookup_host(struct sockaddr_in *s_in, const char *host)
Denis Vlasenko9de420c2007-01-10 09:28:01 +000096{
97 struct hostent *he;
98
99 memset(s_in, 0, sizeof(struct sockaddr_in));
100 s_in->sin_family = AF_INET;
101 he = xgethostbyname(host);
102 memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
103}
104
Denis Vlasenko85629f02007-01-22 09:36:41 +0000105
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000106int FAST_FUNC xconnect_tcp_v4(struct sockaddr_in *s_addr)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000107{
108 int s = xsocket(AF_INET, SOCK_STREAM, 0);
109 xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr));
110 return s;
111}
Denis Vlasenko85629f02007-01-22 09:36:41 +0000112*/
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000113
114/* "New" networking API */
115
116
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000117int FAST_FUNC get_nport(const struct sockaddr *sa)
Denis Vlasenko5d687242007-01-12 20:59:31 +0000118{
119#if ENABLE_FEATURE_IPV6
Denis Vlasenko2856dab2007-04-01 01:18:20 +0000120 if (sa->sa_family == AF_INET6) {
121 return ((struct sockaddr_in6*)sa)->sin6_port;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000122 }
123#endif
Denis Vlasenko2856dab2007-04-01 01:18:20 +0000124 if (sa->sa_family == AF_INET) {
125 return ((struct sockaddr_in*)sa)->sin_port;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000126 }
Denis Vlasenko5d687242007-01-12 20:59:31 +0000127 /* What? UNIX socket? IPX?? :) */
Denis Vlasenko85629f02007-01-22 09:36:41 +0000128 return -1;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000129}
130
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000131void FAST_FUNC set_nport(len_and_sockaddr *lsa, unsigned port)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000132{
133#if ENABLE_FEATURE_IPV6
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000134 if (lsa->u.sa.sa_family == AF_INET6) {
135 lsa->u.sin6.sin6_port = port;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000136 return;
137 }
138#endif
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000139 if (lsa->u.sa.sa_family == AF_INET) {
140 lsa->u.sin.sin_port = port;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000141 return;
142 }
143 /* What? UNIX socket? IPX?? :) */
144}
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000145
Denis Vlasenko42823d52007-02-04 02:39:08 +0000146/* We hijack this constant to mean something else */
147/* It doesn't hurt because we will remove this bit anyway */
148#define DIE_ON_ERROR AI_CANONNAME
149
Denis Vlasenko85629f02007-01-22 09:36:41 +0000150/* host: "1.2.3.4[:port]", "www.google.com[:port]"
Denis Vlasenko42823d52007-02-04 02:39:08 +0000151 * port: if neither of above specifies port # */
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000152static len_and_sockaddr* str2sockaddr(
153 const char *host, int port,
154USE_FEATURE_IPV6(sa_family_t af,)
155 int ai_flags)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000156{
157 int rc;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000158 len_and_sockaddr *r = NULL;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000159 struct addrinfo *result = NULL;
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000160 struct addrinfo *used_res;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000161 const char *org_host = host; /* only for error msg */
162 const char *cp;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000163 struct addrinfo hint;
164
165 /* Ugly parsing of host:addr */
166 if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000167 /* Even uglier parsing of [xx]:nn */
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000168 host++;
169 cp = strchr(host, ']');
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000170 if (!cp || cp[1] != ':') { /* Malformed: must have [xx]:nn */
171 bb_error_msg("bad address '%s'", org_host);
172 if (ai_flags & DIE_ON_ERROR)
173 xfunc_die();
174 return NULL;
175 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000176 } else {
177 cp = strrchr(host, ':');
178 if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
179 /* There is more than one ':' (e.g. "::1") */
180 cp = NULL; /* it's not a port spec */
181 }
182 }
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000183 if (cp) { /* points to ":" or "]:" */
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000184 int sz = cp - host + 1;
185 host = safe_strncpy(alloca(sz), host, sz);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000186 if (ENABLE_FEATURE_IPV6 && *cp != ':')
187 cp++; /* skip ']' */
188 cp++; /* skip ':' */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000189 port = bb_strtou(cp, NULL, 10);
190 if (errno || (unsigned)port > 0xffff) {
191 bb_error_msg("bad port spec '%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 }
197
198 memset(&hint, 0 , sizeof(hint));
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000199#if !ENABLE_FEATURE_IPV6
200 hint.ai_family = AF_INET; /* do not try to find IPv6 */
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000201#else
202 hint.ai_family = af;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000203#endif
204 /* Needed. Or else we will get each address thrice (or more)
205 * for each possible socket type (tcp,udp,raw...): */
206 hint.ai_socktype = SOCK_STREAM;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000207 hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000208 rc = getaddrinfo(host, NULL, &hint, &result);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000209 if (rc || !result) {
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000210 bb_error_msg("bad address '%s'", org_host);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000211 if (ai_flags & DIE_ON_ERROR)
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000212 xfunc_die();
Denis Vlasenko42823d52007-02-04 02:39:08 +0000213 goto ret;
214 }
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000215 used_res = result;
216#if ENABLE_FEATURE_PREFER_IPV4_ADDRESS
217 while (1) {
218 if (used_res->ai_family == AF_INET)
219 break;
220 used_res = used_res->ai_next;
221 if (!used_res) {
222 used_res = result;
223 break;
224 }
225 }
226#endif
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000227 r = xmalloc(offsetof(len_and_sockaddr, u.sa) + used_res->ai_addrlen);
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000228 r->len = used_res->ai_addrlen;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000229 memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen);
Denis Vlasenko5d687242007-01-12 20:59:31 +0000230 set_nport(r, htons(port));
Denis Vlasenko42823d52007-02-04 02:39:08 +0000231 ret:
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000232 freeaddrinfo(result);
233 return r;
234}
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000235#if !ENABLE_FEATURE_IPV6
236#define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
237#endif
238
239#if ENABLE_FEATURE_IPV6
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000240len_and_sockaddr* FAST_FUNC host_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenkoe9913032007-02-04 02:41:57 +0000241{
242 return str2sockaddr(host, port, af, 0);
243}
244
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000245len_and_sockaddr* FAST_FUNC xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000246{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000247 return str2sockaddr(host, port, af, DIE_ON_ERROR);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000248}
249#endif
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000250
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000251len_and_sockaddr* FAST_FUNC host2sockaddr(const char *host, int port)
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000252{
253 return str2sockaddr(host, port, AF_UNSPEC, 0);
254}
255
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000256len_and_sockaddr* FAST_FUNC xhost2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000257{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000258 return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000259}
260
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000261len_and_sockaddr* FAST_FUNC xdotted2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000262{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000263 return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000264}
265
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000266#undef xsocket_type
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000267int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, USE_FEATURE_IPV6(int family,) int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000268{
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000269 SKIP_FEATURE_IPV6(enum { family = AF_INET };)
Denis Vlasenko5d687242007-01-12 20:59:31 +0000270 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000271 int fd;
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000272 int len;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000273
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000274#if ENABLE_FEATURE_IPV6
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000275 if (family == AF_UNSPEC) {
276 fd = socket(AF_INET6, sock_type, 0);
277 if (fd >= 0) {
278 family = AF_INET6;
279 goto done;
280 }
281 family = AF_INET;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000282 }
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000283#endif
284 fd = xsocket(family, sock_type, 0);
285 len = sizeof(struct sockaddr_in);
286#if ENABLE_FEATURE_IPV6
287 if (family == AF_INET6) {
288 done:
289 len = sizeof(struct sockaddr_in6);
290 }
291#endif
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000292 lsa = xzalloc(offsetof(len_and_sockaddr, u.sa) + len);
Denis Vlasenko5d687242007-01-12 20:59:31 +0000293 lsa->len = len;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000294 lsa->u.sa.sa_family = family;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000295 *lsap = lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000296 return fd;
297}
298
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000299int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000300{
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000301 return xsocket_type(lsap, USE_FEATURE_IPV6(AF_UNSPEC,) SOCK_STREAM);
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000302}
303
304static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000305{
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000306 int fd;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000307 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000308
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000309 if (bindaddr && bindaddr[0]) {
Denis Vlasenko42823d52007-02-04 02:39:08 +0000310 lsa = xdotted2sockaddr(bindaddr, port);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000311 /* user specified bind addr dictates family */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000312 fd = xsocket(lsa->u.sa.sa_family, sock_type, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000313 } else {
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000314 fd = xsocket_type(&lsa, USE_FEATURE_IPV6(AF_UNSPEC,) sock_type);
Denis Vlasenko24182882007-01-13 21:05:38 +0000315 set_nport(lsa, htons(port));
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000316 }
Denis Vlasenko48237b02006-11-22 23:22:06 +0000317 setsockopt_reuseaddr(fd);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000318 xbind(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000319 free(lsa);
320 return fd;
321}
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000322
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000323int FAST_FUNC create_and_bind_stream_or_die(const char *bindaddr, int port)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000324{
325 return create_and_bind_or_die(bindaddr, port, SOCK_STREAM);
326}
327
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000328int FAST_FUNC create_and_bind_dgram_or_die(const char *bindaddr, int port)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000329{
330 return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM);
331}
332
333
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000334int FAST_FUNC create_and_connect_stream_or_die(const char *peer, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000335{
336 int fd;
337 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000338
Denis Vlasenko42823d52007-02-04 02:39:08 +0000339 lsa = xhost2sockaddr(peer, port);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000340 fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000341 setsockopt_reuseaddr(fd);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000342 xconnect(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000343 free(lsa);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000344 return fd;
345}
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000346
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000347int FAST_FUNC xconnect_stream(const len_and_sockaddr *lsa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000348{
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000349 int fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
350 xconnect(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000351 return fd;
352}
353
Denis Vlasenko448f0242007-01-22 22:43:05 +0000354/* We hijack this constant to mean something else */
355/* It doesn't hurt because we will add this bit anyway */
356#define IGNORE_PORT NI_NUMERICSERV
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000357static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000358{
359 char host[128];
360 char serv[16];
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000361 int rc;
362 socklen_t salen;
363
364 salen = LSA_SIZEOF_SA;
365#if ENABLE_FEATURE_IPV6
366 if (sa->sa_family == AF_INET)
367 salen = sizeof(struct sockaddr_in);
368 if (sa->sa_family == AF_INET6)
369 salen = sizeof(struct sockaddr_in6);
370#endif
371 rc = getnameinfo(sa, salen,
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000372 host, sizeof(host),
Denis Vlasenko448f0242007-01-22 22:43:05 +0000373 /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000374 serv, sizeof(serv),
Denis Vlasenko85629f02007-01-22 09:36:41 +0000375 /* do not resolve port# into service _name_ */
376 flags | NI_NUMERICSERV
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000377 );
Denis Vlasenko85629f02007-01-22 09:36:41 +0000378 if (rc)
379 return NULL;
Denis Vlasenko448f0242007-01-22 22:43:05 +0000380 if (flags & IGNORE_PORT)
381 return xstrdup(host);
Denis Vlasenko85629f02007-01-22 09:36:41 +0000382#if ENABLE_FEATURE_IPV6
Denis Vlasenko448f0242007-01-22 22:43:05 +0000383 if (sa->sa_family == AF_INET6) {
384 if (strchr(host, ':')) /* heh, it's not a resolved hostname */
385 return xasprintf("[%s]:%s", host, serv);
386 /*return xasprintf("%s:%s", host, serv);*/
387 /* - fall through instead */
388 }
Denis Vlasenko85629f02007-01-22 09:36:41 +0000389#endif
390 /* For now we don't support anything else, so it has to be INET */
391 /*if (sa->sa_family == AF_INET)*/
392 return xasprintf("%s:%s", host, serv);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000393 /*return xstrdup(host);*/
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000394}
395
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000396char* FAST_FUNC xmalloc_sockaddr2host(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000397{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000398 return sockaddr2str(sa, 0);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000399}
400
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000401char* FAST_FUNC xmalloc_sockaddr2host_noport(const struct sockaddr *sa)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000402{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000403 return sockaddr2str(sa, IGNORE_PORT);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000404}
405
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000406char* FAST_FUNC xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000407{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000408 return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000409}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000410char* FAST_FUNC xmalloc_sockaddr2dotted(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000411{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000412 return sockaddr2str(sa, NI_NUMERICHOST);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000413}
Denis Vlasenko448f0242007-01-22 22:43:05 +0000414
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000415char* FAST_FUNC xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000416{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000417 return sockaddr2str(sa, NI_NUMERICHOST | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000418}