blob: f853e95934375b9b232bb909874c1f2d489cfd81 [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{
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000040 len_and_sockaddr lsa;
41 len_and_sockaddr *lsa_ptr;
Denis Vlasenko73c571a2009-03-09 00:12:37 +000042
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000043 lsa.len = LSA_SIZEOF_SA;
44 if (getsockname(fd, &lsa.u.sa, &lsa.len) != 0)
Denis Vlasenko73c571a2009-03-09 00:12:37 +000045 return NULL;
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000046
47 lsa_ptr = xzalloc(LSA_LEN_SIZE + lsa.len);
48 if (lsa.len > LSA_SIZEOF_SA) { /* rarely (if ever) happens */
49 lsa_ptr->len = lsa.len;
50 getsockname(fd, &lsa_ptr->u.sa, &lsa_ptr->len);
51 } else {
52 memcpy(lsa_ptr, &lsa, LSA_LEN_SIZE + lsa.len);
53 }
54 return lsa_ptr;
Denis Vlasenko73c571a2009-03-09 00:12:37 +000055}
Denis Vlasenko48237b02006-11-22 23:22:06 +000056
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000057void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000058{
Denis Vlasenko9de420c2007-01-10 09:28:01 +000059 if (connect(s, s_addr, addrlen) < 0) {
60 if (ENABLE_FEATURE_CLEAN_UP)
61 close(s);
62 if (s_addr->sa_family == AF_INET)
63 bb_perror_msg_and_die("%s (%s)",
64 "cannot connect to remote host",
65 inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
66 bb_perror_msg_and_die("cannot connect to remote host");
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000067 }
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000068}
69
Denis Vlasenko5d687242007-01-12 20:59:31 +000070/* Return port number for a service.
Denis Vlasenko9de420c2007-01-10 09:28:01 +000071 * If "port" is a number use it as the port.
Denis Vlasenko73c571a2009-03-09 00:12:37 +000072 * If "port" is a name it is looked up in /etc/services,
73 * if it isnt found return default_port
74 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000075unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +000076{
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000077 unsigned port_nr = default_port;
Denis Vlasenko9de420c2007-01-10 09:28:01 +000078 if (port) {
79 int old_errno;
80
81 /* Since this is a lib function, we're not allowed to reset errno to 0.
82 * Doing so could break an app that is deferring checking of errno. */
83 old_errno = errno;
84 port_nr = bb_strtou(port, NULL, 10);
85 if (errno || port_nr > 65535) {
86 struct servent *tserv = getservbyname(port, protocol);
Denis Vlasenko24182882007-01-13 21:05:38 +000087 port_nr = default_port;
Denis Vlasenko9de420c2007-01-10 09:28:01 +000088 if (tserv)
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000089 port_nr = ntohs(tserv->s_port);
Denis Vlasenko9de420c2007-01-10 09:28:01 +000090 }
91 errno = old_errno;
92 }
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000093 return (uint16_t)port_nr;
Denis Vlasenko9de420c2007-01-10 09:28:01 +000094}
95
96
97/* "Old" networking API - only IPv4 */
98
Denis Vlasenko85629f02007-01-22 09:36:41 +000099/*
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000100void FAST_FUNC bb_lookup_host(struct sockaddr_in *s_in, const char *host)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000101{
102 struct hostent *he;
103
104 memset(s_in, 0, sizeof(struct sockaddr_in));
105 s_in->sin_family = AF_INET;
106 he = xgethostbyname(host);
107 memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
108}
109
Denis Vlasenko85629f02007-01-22 09:36:41 +0000110
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000111int FAST_FUNC xconnect_tcp_v4(struct sockaddr_in *s_addr)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000112{
113 int s = xsocket(AF_INET, SOCK_STREAM, 0);
114 xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr));
115 return s;
116}
Denis Vlasenko85629f02007-01-22 09:36:41 +0000117*/
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000118
119/* "New" networking API */
120
121
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000122int FAST_FUNC get_nport(const struct sockaddr *sa)
Denis Vlasenko5d687242007-01-12 20:59:31 +0000123{
124#if ENABLE_FEATURE_IPV6
Denis Vlasenko2856dab2007-04-01 01:18:20 +0000125 if (sa->sa_family == AF_INET6) {
126 return ((struct sockaddr_in6*)sa)->sin6_port;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000127 }
128#endif
Denis Vlasenko2856dab2007-04-01 01:18:20 +0000129 if (sa->sa_family == AF_INET) {
130 return ((struct sockaddr_in*)sa)->sin_port;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000131 }
Denis Vlasenko5d687242007-01-12 20:59:31 +0000132 /* What? UNIX socket? IPX?? :) */
Denis Vlasenko85629f02007-01-22 09:36:41 +0000133 return -1;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000134}
135
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000136void FAST_FUNC set_nport(len_and_sockaddr *lsa, unsigned port)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000137{
138#if ENABLE_FEATURE_IPV6
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000139 if (lsa->u.sa.sa_family == AF_INET6) {
140 lsa->u.sin6.sin6_port = port;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000141 return;
142 }
143#endif
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000144 if (lsa->u.sa.sa_family == AF_INET) {
145 lsa->u.sin.sin_port = port;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000146 return;
147 }
148 /* What? UNIX socket? IPX?? :) */
149}
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000150
Denis Vlasenko42823d52007-02-04 02:39:08 +0000151/* We hijack this constant to mean something else */
152/* It doesn't hurt because we will remove this bit anyway */
153#define DIE_ON_ERROR AI_CANONNAME
154
Denis Vlasenko85629f02007-01-22 09:36:41 +0000155/* host: "1.2.3.4[:port]", "www.google.com[:port]"
Denis Vlasenko42823d52007-02-04 02:39:08 +0000156 * port: if neither of above specifies port # */
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000157static len_and_sockaddr* str2sockaddr(
158 const char *host, int port,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000159IF_FEATURE_IPV6(sa_family_t af,)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000160 int ai_flags)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000161{
162 int rc;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000163 len_and_sockaddr *r = NULL;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000164 struct addrinfo *result = NULL;
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000165 struct addrinfo *used_res;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000166 const char *org_host = host; /* only for error msg */
167 const char *cp;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000168 struct addrinfo hint;
169
170 /* Ugly parsing of host:addr */
171 if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000172 /* Even uglier parsing of [xx]:nn */
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000173 host++;
174 cp = strchr(host, ']');
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000175 if (!cp || (cp[1] != ':' && cp[1] != '\0')) {
176 /* Malformed: must be [xx]:nn or [xx] */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000177 bb_error_msg("bad address '%s'", org_host);
178 if (ai_flags & DIE_ON_ERROR)
179 xfunc_die();
180 return NULL;
181 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000182 } else {
183 cp = strrchr(host, ':');
184 if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
185 /* There is more than one ':' (e.g. "::1") */
186 cp = NULL; /* it's not a port spec */
187 }
188 }
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000189 if (cp) { /* points to ":" or "]:" */
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000190 int sz = cp - host + 1;
191 host = safe_strncpy(alloca(sz), host, sz);
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000192 if (ENABLE_FEATURE_IPV6 && *cp != ':') {
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000193 cp++; /* skip ']' */
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000194 if (*cp == '\0') /* [xx] without port */
195 goto skip;
196 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000197 cp++; /* skip ':' */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000198 port = bb_strtou(cp, NULL, 10);
199 if (errno || (unsigned)port > 0xffff) {
200 bb_error_msg("bad port spec '%s'", org_host);
201 if (ai_flags & DIE_ON_ERROR)
202 xfunc_die();
203 return NULL;
204 }
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000205 skip: ;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000206 }
207
208 memset(&hint, 0 , sizeof(hint));
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000209#if !ENABLE_FEATURE_IPV6
210 hint.ai_family = AF_INET; /* do not try to find IPv6 */
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000211#else
212 hint.ai_family = af;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000213#endif
214 /* Needed. Or else we will get each address thrice (or more)
215 * for each possible socket type (tcp,udp,raw...): */
216 hint.ai_socktype = SOCK_STREAM;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000217 hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000218 rc = getaddrinfo(host, NULL, &hint, &result);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000219 if (rc || !result) {
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000220 bb_error_msg("bad address '%s'", org_host);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000221 if (ai_flags & DIE_ON_ERROR)
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000222 xfunc_die();
Denis Vlasenko42823d52007-02-04 02:39:08 +0000223 goto ret;
224 }
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000225 used_res = result;
226#if ENABLE_FEATURE_PREFER_IPV4_ADDRESS
227 while (1) {
228 if (used_res->ai_family == AF_INET)
229 break;
230 used_res = used_res->ai_next;
231 if (!used_res) {
232 used_res = result;
233 break;
234 }
235 }
236#endif
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000237 r = xmalloc(offsetof(len_and_sockaddr, u.sa) + used_res->ai_addrlen);
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000238 r->len = used_res->ai_addrlen;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000239 memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen);
Denis Vlasenko5d687242007-01-12 20:59:31 +0000240 set_nport(r, htons(port));
Denis Vlasenko42823d52007-02-04 02:39:08 +0000241 ret:
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000242 freeaddrinfo(result);
243 return r;
244}
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000245#if !ENABLE_FEATURE_IPV6
246#define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
247#endif
248
249#if ENABLE_FEATURE_IPV6
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000250len_and_sockaddr* FAST_FUNC host_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenkoe9913032007-02-04 02:41:57 +0000251{
252 return str2sockaddr(host, port, af, 0);
253}
254
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000255len_and_sockaddr* FAST_FUNC xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000256{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000257 return str2sockaddr(host, port, af, DIE_ON_ERROR);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000258}
259#endif
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000260
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000261len_and_sockaddr* FAST_FUNC host2sockaddr(const char *host, int port)
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000262{
263 return str2sockaddr(host, port, AF_UNSPEC, 0);
264}
265
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000266len_and_sockaddr* FAST_FUNC xhost2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000267{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000268 return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000269}
270
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000271len_and_sockaddr* FAST_FUNC xdotted2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000272{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000273 return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000274}
275
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000276#undef xsocket_type
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000277int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, IF_FEATURE_IPV6(int family,) int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000278{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000279 IF_NOT_FEATURE_IPV6(enum { family = AF_INET };)
Denis Vlasenko5d687242007-01-12 20:59:31 +0000280 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000281 int fd;
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000282 int len;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000283
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000284#if ENABLE_FEATURE_IPV6
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000285 if (family == AF_UNSPEC) {
286 fd = socket(AF_INET6, sock_type, 0);
287 if (fd >= 0) {
288 family = AF_INET6;
289 goto done;
290 }
291 family = AF_INET;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000292 }
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000293#endif
294 fd = xsocket(family, sock_type, 0);
295 len = sizeof(struct sockaddr_in);
296#if ENABLE_FEATURE_IPV6
297 if (family == AF_INET6) {
298 done:
299 len = sizeof(struct sockaddr_in6);
300 }
301#endif
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000302 lsa = xzalloc(offsetof(len_and_sockaddr, u.sa) + len);
Denis Vlasenko5d687242007-01-12 20:59:31 +0000303 lsa->len = len;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000304 lsa->u.sa.sa_family = family;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000305 *lsap = lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000306 return fd;
307}
308
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000309int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000310{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000311 return xsocket_type(lsap, IF_FEATURE_IPV6(AF_UNSPEC,) SOCK_STREAM);
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000312}
313
314static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000315{
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000316 int fd;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000317 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000318
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000319 if (bindaddr && bindaddr[0]) {
Denis Vlasenko42823d52007-02-04 02:39:08 +0000320 lsa = xdotted2sockaddr(bindaddr, port);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000321 /* user specified bind addr dictates family */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000322 fd = xsocket(lsa->u.sa.sa_family, sock_type, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000323 } else {
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000324 fd = xsocket_type(&lsa, IF_FEATURE_IPV6(AF_UNSPEC,) sock_type);
Denis Vlasenko24182882007-01-13 21:05:38 +0000325 set_nport(lsa, htons(port));
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000326 }
Denis Vlasenko48237b02006-11-22 23:22:06 +0000327 setsockopt_reuseaddr(fd);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000328 xbind(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000329 free(lsa);
330 return fd;
331}
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000332
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000333int FAST_FUNC create_and_bind_stream_or_die(const char *bindaddr, int port)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000334{
335 return create_and_bind_or_die(bindaddr, port, SOCK_STREAM);
336}
337
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000338int FAST_FUNC create_and_bind_dgram_or_die(const char *bindaddr, int port)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000339{
340 return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM);
341}
342
343
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000344int FAST_FUNC create_and_connect_stream_or_die(const char *peer, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000345{
346 int fd;
347 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000348
Denis Vlasenko42823d52007-02-04 02:39:08 +0000349 lsa = xhost2sockaddr(peer, port);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000350 fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000351 setsockopt_reuseaddr(fd);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000352 xconnect(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000353 free(lsa);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000354 return fd;
355}
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000356
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000357int FAST_FUNC xconnect_stream(const len_and_sockaddr *lsa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000358{
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000359 int fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
360 xconnect(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000361 return fd;
362}
363
Denis Vlasenko448f0242007-01-22 22:43:05 +0000364/* We hijack this constant to mean something else */
365/* It doesn't hurt because we will add this bit anyway */
366#define IGNORE_PORT NI_NUMERICSERV
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000367static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000368{
369 char host[128];
370 char serv[16];
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000371 int rc;
372 socklen_t salen;
373
374 salen = LSA_SIZEOF_SA;
375#if ENABLE_FEATURE_IPV6
376 if (sa->sa_family == AF_INET)
377 salen = sizeof(struct sockaddr_in);
378 if (sa->sa_family == AF_INET6)
379 salen = sizeof(struct sockaddr_in6);
380#endif
381 rc = getnameinfo(sa, salen,
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000382 host, sizeof(host),
Denis Vlasenko448f0242007-01-22 22:43:05 +0000383 /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000384 serv, sizeof(serv),
Denis Vlasenko85629f02007-01-22 09:36:41 +0000385 /* do not resolve port# into service _name_ */
386 flags | NI_NUMERICSERV
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000387 );
Denis Vlasenko85629f02007-01-22 09:36:41 +0000388 if (rc)
389 return NULL;
Denis Vlasenko448f0242007-01-22 22:43:05 +0000390 if (flags & IGNORE_PORT)
391 return xstrdup(host);
Denis Vlasenko85629f02007-01-22 09:36:41 +0000392#if ENABLE_FEATURE_IPV6
Denis Vlasenko448f0242007-01-22 22:43:05 +0000393 if (sa->sa_family == AF_INET6) {
394 if (strchr(host, ':')) /* heh, it's not a resolved hostname */
395 return xasprintf("[%s]:%s", host, serv);
396 /*return xasprintf("%s:%s", host, serv);*/
397 /* - fall through instead */
398 }
Denis Vlasenko85629f02007-01-22 09:36:41 +0000399#endif
400 /* For now we don't support anything else, so it has to be INET */
401 /*if (sa->sa_family == AF_INET)*/
402 return xasprintf("%s:%s", host, serv);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000403 /*return xstrdup(host);*/
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000404}
405
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000406char* FAST_FUNC xmalloc_sockaddr2host(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000407{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000408 return sockaddr2str(sa, 0);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000409}
410
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000411char* FAST_FUNC xmalloc_sockaddr2host_noport(const struct sockaddr *sa)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000412{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000413 return sockaddr2str(sa, IGNORE_PORT);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000414}
415
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000416char* FAST_FUNC xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000417{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000418 return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000419}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000420char* FAST_FUNC xmalloc_sockaddr2dotted(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000421{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000422 return sockaddr2str(sa, NI_NUMERICHOST);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000423}
Denis Vlasenko448f0242007-01-22 22:43:05 +0000424
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000425char* FAST_FUNC xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000426{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000427 return sockaddr2str(sa, NI_NUMERICHOST | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000428}