blob: 97751eb275edf841fab87f8ce0f8889d7f316f79 [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
Denys Vlasenko043b1e52009-09-06 12:47:55 +020010#include <sys/socket.h> /* netinet/in.h needs it */
Denis Vlasenko9de420c2007-01-10 09:28:01 +000011#include <netinet/in.h>
Denis Vlasenkoe5373852008-12-10 11:12:16 +000012#include <net/if.h>
Denis Vlasenkof6b46852009-04-25 13:16:53 +000013#include <sys/un.h>
Eric Andersen0b315862002-07-03 11:51:44 +000014#include "libbb.h"
15
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000016void FAST_FUNC setsockopt_reuseaddr(int fd)
Denis Vlasenko48237b02006-11-22 23:22:06 +000017{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +000018 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &const_int_1, sizeof(const_int_1));
Denis Vlasenko48237b02006-11-22 23:22:06 +000019}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000020int FAST_FUNC setsockopt_broadcast(int fd)
Denis Vlasenko48237b02006-11-22 23:22:06 +000021{
Denis Vlasenko703e2022007-01-22 14:12:08 +000022 return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1));
Denis Vlasenko48237b02006-11-22 23:22:06 +000023}
Denis Vlasenkoe5373852008-12-10 11:12:16 +000024int FAST_FUNC setsockopt_bindtodevice(int fd, const char *iface)
25{
26 int r;
27 struct ifreq ifr;
28 strncpy_IFNAMSIZ(ifr.ifr_name, iface);
Denis Vlasenkoa3ca7cf2008-12-10 11:24:33 +000029 /* NB: passing (iface, strlen(iface) + 1) does not work!
30 * (maybe it works on _some_ kernels, but not on 2.6.26)
31 * Actually, ifr_name is at offset 0, and in practice
Denis Vlasenkoe5373852008-12-10 11:12:16 +000032 * just giving char[IFNAMSIZ] instead of struct ifreq works too.
33 * But just in case it's not true on some obscure arch... */
34 r = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
35 if (r)
36 bb_perror_msg("can't bind to interface %s", iface);
37 return r;
38}
39
Denis Vlasenko73c571a2009-03-09 00:12:37 +000040len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd)
41{
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000042 len_and_sockaddr lsa;
43 len_and_sockaddr *lsa_ptr;
Denis Vlasenko73c571a2009-03-09 00:12:37 +000044
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000045 lsa.len = LSA_SIZEOF_SA;
46 if (getsockname(fd, &lsa.u.sa, &lsa.len) != 0)
Denis Vlasenko73c571a2009-03-09 00:12:37 +000047 return NULL;
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000048
49 lsa_ptr = xzalloc(LSA_LEN_SIZE + lsa.len);
50 if (lsa.len > LSA_SIZEOF_SA) { /* rarely (if ever) happens */
51 lsa_ptr->len = lsa.len;
52 getsockname(fd, &lsa_ptr->u.sa, &lsa_ptr->len);
53 } else {
54 memcpy(lsa_ptr, &lsa, LSA_LEN_SIZE + lsa.len);
55 }
56 return lsa_ptr;
Denis Vlasenko73c571a2009-03-09 00:12:37 +000057}
Denis Vlasenko48237b02006-11-22 23:22:06 +000058
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000059void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000060{
Denis Vlasenko9de420c2007-01-10 09:28:01 +000061 if (connect(s, s_addr, addrlen) < 0) {
62 if (ENABLE_FEATURE_CLEAN_UP)
63 close(s);
64 if (s_addr->sa_family == AF_INET)
65 bb_perror_msg_and_die("%s (%s)",
Denys Vlasenko6331cf02009-11-13 09:08:27 +010066 "can't connect to remote host",
Denis Vlasenko9de420c2007-01-10 09:28:01 +000067 inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
Denys Vlasenko6331cf02009-11-13 09:08:27 +010068 bb_perror_msg_and_die("can't connect to remote host");
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000069 }
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000070}
71
Denis Vlasenko5d687242007-01-12 20:59:31 +000072/* Return port number for a service.
Denis Vlasenko9de420c2007-01-10 09:28:01 +000073 * If "port" is a number use it as the port.
Denis Vlasenko73c571a2009-03-09 00:12:37 +000074 * If "port" is a name it is looked up in /etc/services,
75 * if it isnt found return default_port
76 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000077unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +000078{
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000079 unsigned port_nr = default_port;
Denis Vlasenko9de420c2007-01-10 09:28:01 +000080 if (port) {
81 int old_errno;
82
83 /* Since this is a lib function, we're not allowed to reset errno to 0.
84 * Doing so could break an app that is deferring checking of errno. */
85 old_errno = errno;
86 port_nr = bb_strtou(port, NULL, 10);
87 if (errno || port_nr > 65535) {
88 struct servent *tserv = getservbyname(port, protocol);
Denis Vlasenko24182882007-01-13 21:05:38 +000089 port_nr = default_port;
Denis Vlasenko9de420c2007-01-10 09:28:01 +000090 if (tserv)
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000091 port_nr = ntohs(tserv->s_port);
Denis Vlasenko9de420c2007-01-10 09:28:01 +000092 }
93 errno = old_errno;
94 }
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000095 return (uint16_t)port_nr;
Denis Vlasenko9de420c2007-01-10 09:28:01 +000096}
97
98
Denis Vlasenko9de420c2007-01-10 09:28:01 +000099/* "New" networking API */
100
101
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000102int FAST_FUNC get_nport(const struct sockaddr *sa)
Denis Vlasenko5d687242007-01-12 20:59:31 +0000103{
104#if ENABLE_FEATURE_IPV6
Denis Vlasenko2856dab2007-04-01 01:18:20 +0000105 if (sa->sa_family == AF_INET6) {
106 return ((struct sockaddr_in6*)sa)->sin6_port;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000107 }
108#endif
Denis Vlasenko2856dab2007-04-01 01:18:20 +0000109 if (sa->sa_family == AF_INET) {
110 return ((struct sockaddr_in*)sa)->sin_port;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000111 }
Denis Vlasenko5d687242007-01-12 20:59:31 +0000112 /* What? UNIX socket? IPX?? :) */
Denis Vlasenko85629f02007-01-22 09:36:41 +0000113 return -1;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000114}
115
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000116void FAST_FUNC set_nport(len_and_sockaddr *lsa, unsigned port)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000117{
118#if ENABLE_FEATURE_IPV6
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000119 if (lsa->u.sa.sa_family == AF_INET6) {
120 lsa->u.sin6.sin6_port = port;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000121 return;
122 }
123#endif
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000124 if (lsa->u.sa.sa_family == AF_INET) {
125 lsa->u.sin.sin_port = port;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000126 return;
127 }
128 /* What? UNIX socket? IPX?? :) */
129}
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000130
Denis Vlasenko42823d52007-02-04 02:39:08 +0000131/* We hijack this constant to mean something else */
132/* It doesn't hurt because we will remove this bit anyway */
133#define DIE_ON_ERROR AI_CANONNAME
134
Denis Vlasenko85629f02007-01-22 09:36:41 +0000135/* host: "1.2.3.4[:port]", "www.google.com[:port]"
Denis Vlasenko42823d52007-02-04 02:39:08 +0000136 * port: if neither of above specifies port # */
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000137static len_and_sockaddr* str2sockaddr(
138 const char *host, int port,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000139IF_FEATURE_IPV6(sa_family_t af,)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000140 int ai_flags)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000141{
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100142IF_NOT_FEATURE_IPV6(sa_family_t af = AF_INET;)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000143 int rc;
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000144 len_and_sockaddr *r;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000145 struct addrinfo *result = NULL;
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000146 struct addrinfo *used_res;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000147 const char *org_host = host; /* only for error msg */
148 const char *cp;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000149 struct addrinfo hint;
150
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000151 if (ENABLE_FEATURE_UNIX_LOCAL && strncmp(host, "local:", 6) == 0) {
152 struct sockaddr_un *sun;
153
154 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un));
155 r->len = sizeof(struct sockaddr_un);
156 r->u.sa.sa_family = AF_UNIX;
157 sun = (struct sockaddr_un *)&r->u.sa;
158 safe_strncpy(sun->sun_path, host + 6, sizeof(sun->sun_path));
159 return r;
160 }
161
162 r = NULL;
163
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000164 /* Ugly parsing of host:addr */
165 if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000166 /* Even uglier parsing of [xx]:nn */
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000167 host++;
168 cp = strchr(host, ']');
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000169 if (!cp || (cp[1] != ':' && cp[1] != '\0')) {
170 /* Malformed: must be [xx]:nn or [xx] */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000171 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;
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000185
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000186 host = safe_strncpy(alloca(sz), host, sz);
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000187 if (ENABLE_FEATURE_IPV6 && *cp != ':') {
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000188 cp++; /* skip ']' */
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000189 if (*cp == '\0') /* [xx] without port */
190 goto skip;
191 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000192 cp++; /* skip ':' */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000193 port = bb_strtou(cp, NULL, 10);
194 if (errno || (unsigned)port > 0xffff) {
195 bb_error_msg("bad port spec '%s'", org_host);
196 if (ai_flags & DIE_ON_ERROR)
197 xfunc_die();
198 return NULL;
199 }
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000200 skip: ;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000201 }
202
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100203 /* Next two if blocks allow to skip getaddrinfo()
Denys Vlasenko75b73972009-11-24 14:06:04 +0100204 * in case host name is a numeric IP(v6) address.
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100205 * getaddrinfo() initializes DNS resolution machinery,
206 * scans network config and such - tens of syscalls.
207 */
208 /* If we were not asked specifically for IPv6,
209 * check whether this is a numeric IPv4 */
210 IF_FEATURE_IPV6(if(af != AF_INET6)) {
211 struct in_addr in4;
212 if (inet_aton(host, &in4) != 0) {
213 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in));
214 r->len = sizeof(struct sockaddr_in);
215 r->u.sa.sa_family = AF_INET;
216 r->u.sin.sin_addr = in4;
217 goto set_port;
218 }
219 }
220#if ENABLE_FEATURE_IPV6
221 /* If we were not asked specifically for IPv4,
222 * check whether this is a numeric IPv6 */
223 if (af != AF_INET) {
224 struct in6_addr in6;
225 if (inet_pton(AF_INET6, host, &in6) > 0) {
226 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in6));
227 r->len = sizeof(struct sockaddr_in6);
228 r->u.sa.sa_family = AF_INET6;
229 r->u.sin6.sin6_addr = in6;
230 goto set_port;
231 }
232 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000233#endif
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100234
235 memset(&hint, 0 , sizeof(hint));
236 hint.ai_family = af;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000237 /* Needed. Or else we will get each address thrice (or more)
238 * for each possible socket type (tcp,udp,raw...): */
239 hint.ai_socktype = SOCK_STREAM;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000240 hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000241 rc = getaddrinfo(host, NULL, &hint, &result);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000242 if (rc || !result) {
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000243 bb_error_msg("bad address '%s'", org_host);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000244 if (ai_flags & DIE_ON_ERROR)
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000245 xfunc_die();
Denis Vlasenko42823d52007-02-04 02:39:08 +0000246 goto ret;
247 }
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000248 used_res = result;
249#if ENABLE_FEATURE_PREFER_IPV4_ADDRESS
250 while (1) {
251 if (used_res->ai_family == AF_INET)
252 break;
253 used_res = used_res->ai_next;
254 if (!used_res) {
255 used_res = result;
256 break;
257 }
258 }
259#endif
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100260 r = xmalloc(LSA_LEN_SIZE + used_res->ai_addrlen);
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000261 r->len = used_res->ai_addrlen;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000262 memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen);
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100263
Denys Vlasenko75b73972009-11-24 14:06:04 +0100264 set_port:
Denis Vlasenko5d687242007-01-12 20:59:31 +0000265 set_nport(r, htons(port));
Denis Vlasenko42823d52007-02-04 02:39:08 +0000266 ret:
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000267 freeaddrinfo(result);
268 return r;
269}
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000270#if !ENABLE_FEATURE_IPV6
271#define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
272#endif
273
274#if ENABLE_FEATURE_IPV6
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000275len_and_sockaddr* FAST_FUNC host_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenkoe9913032007-02-04 02:41:57 +0000276{
277 return str2sockaddr(host, port, af, 0);
278}
279
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000280len_and_sockaddr* FAST_FUNC xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000281{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000282 return str2sockaddr(host, port, af, DIE_ON_ERROR);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000283}
284#endif
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000285
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000286len_and_sockaddr* FAST_FUNC host2sockaddr(const char *host, int port)
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000287{
288 return str2sockaddr(host, port, AF_UNSPEC, 0);
289}
290
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000291len_and_sockaddr* FAST_FUNC xhost2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000292{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000293 return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000294}
295
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000296len_and_sockaddr* FAST_FUNC xdotted2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000297{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000298 return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000299}
300
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000301#undef xsocket_type
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000302int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, IF_FEATURE_IPV6(int family,) int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000303{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000304 IF_NOT_FEATURE_IPV6(enum { family = AF_INET };)
Denis Vlasenko5d687242007-01-12 20:59:31 +0000305 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000306 int fd;
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000307 int len;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000308
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000309#if ENABLE_FEATURE_IPV6
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000310 if (family == AF_UNSPEC) {
311 fd = socket(AF_INET6, sock_type, 0);
312 if (fd >= 0) {
313 family = AF_INET6;
314 goto done;
315 }
316 family = AF_INET;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000317 }
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000318#endif
319 fd = xsocket(family, sock_type, 0);
320 len = sizeof(struct sockaddr_in);
321#if ENABLE_FEATURE_IPV6
322 if (family == AF_INET6) {
323 done:
324 len = sizeof(struct sockaddr_in6);
325 }
326#endif
Denys Vlasenkob1278a32009-11-24 16:03:47 +0100327 lsa = xzalloc(LSA_LEN_SIZE + len);
Denis Vlasenko5d687242007-01-12 20:59:31 +0000328 lsa->len = len;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000329 lsa->u.sa.sa_family = family;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000330 *lsap = lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000331 return fd;
332}
333
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000334int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000335{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000336 return xsocket_type(lsap, IF_FEATURE_IPV6(AF_UNSPEC,) SOCK_STREAM);
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000337}
338
339static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000340{
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000341 int fd;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000342 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000343
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000344 if (bindaddr && bindaddr[0]) {
Denis Vlasenko42823d52007-02-04 02:39:08 +0000345 lsa = xdotted2sockaddr(bindaddr, port);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000346 /* user specified bind addr dictates family */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000347 fd = xsocket(lsa->u.sa.sa_family, sock_type, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000348 } else {
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000349 fd = xsocket_type(&lsa, IF_FEATURE_IPV6(AF_UNSPEC,) sock_type);
Denis Vlasenko24182882007-01-13 21:05:38 +0000350 set_nport(lsa, htons(port));
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000351 }
Denis Vlasenko48237b02006-11-22 23:22:06 +0000352 setsockopt_reuseaddr(fd);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000353 xbind(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000354 free(lsa);
355 return fd;
356}
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000357
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000358int FAST_FUNC create_and_bind_stream_or_die(const char *bindaddr, int port)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000359{
360 return create_and_bind_or_die(bindaddr, port, SOCK_STREAM);
361}
362
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000363int FAST_FUNC create_and_bind_dgram_or_die(const char *bindaddr, int port)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000364{
365 return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM);
366}
367
368
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000369int FAST_FUNC create_and_connect_stream_or_die(const char *peer, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000370{
371 int fd;
372 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000373
Denis Vlasenko42823d52007-02-04 02:39:08 +0000374 lsa = xhost2sockaddr(peer, port);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000375 fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000376 setsockopt_reuseaddr(fd);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000377 xconnect(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000378 free(lsa);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000379 return fd;
380}
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000381
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000382int FAST_FUNC xconnect_stream(const len_and_sockaddr *lsa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000383{
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000384 int fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
385 xconnect(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000386 return fd;
387}
388
Denis Vlasenko448f0242007-01-22 22:43:05 +0000389/* We hijack this constant to mean something else */
390/* It doesn't hurt because we will add this bit anyway */
391#define IGNORE_PORT NI_NUMERICSERV
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000392static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000393{
394 char host[128];
395 char serv[16];
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000396 int rc;
397 socklen_t salen;
398
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000399 if (ENABLE_FEATURE_UNIX_LOCAL && sa->sa_family == AF_UNIX) {
400 struct sockaddr_un *sun = (struct sockaddr_un *)sa;
401 return xasprintf("local:%.*s",
402 (int) sizeof(sun->sun_path),
403 sun->sun_path);
404 }
405
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000406 salen = LSA_SIZEOF_SA;
407#if ENABLE_FEATURE_IPV6
408 if (sa->sa_family == AF_INET)
409 salen = sizeof(struct sockaddr_in);
410 if (sa->sa_family == AF_INET6)
411 salen = sizeof(struct sockaddr_in6);
412#endif
413 rc = getnameinfo(sa, salen,
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000414 host, sizeof(host),
Denis Vlasenko448f0242007-01-22 22:43:05 +0000415 /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000416 serv, sizeof(serv),
Denis Vlasenko85629f02007-01-22 09:36:41 +0000417 /* do not resolve port# into service _name_ */
418 flags | NI_NUMERICSERV
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000419 );
Denis Vlasenko85629f02007-01-22 09:36:41 +0000420 if (rc)
421 return NULL;
Denis Vlasenko448f0242007-01-22 22:43:05 +0000422 if (flags & IGNORE_PORT)
423 return xstrdup(host);
Denis Vlasenko85629f02007-01-22 09:36:41 +0000424#if ENABLE_FEATURE_IPV6
Denis Vlasenko448f0242007-01-22 22:43:05 +0000425 if (sa->sa_family == AF_INET6) {
426 if (strchr(host, ':')) /* heh, it's not a resolved hostname */
427 return xasprintf("[%s]:%s", host, serv);
428 /*return xasprintf("%s:%s", host, serv);*/
429 /* - fall through instead */
430 }
Denis Vlasenko85629f02007-01-22 09:36:41 +0000431#endif
432 /* For now we don't support anything else, so it has to be INET */
433 /*if (sa->sa_family == AF_INET)*/
434 return xasprintf("%s:%s", host, serv);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000435 /*return xstrdup(host);*/
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000436}
437
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000438char* FAST_FUNC xmalloc_sockaddr2host(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000439{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000440 return sockaddr2str(sa, 0);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000441}
442
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000443char* FAST_FUNC xmalloc_sockaddr2host_noport(const struct sockaddr *sa)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000444{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000445 return sockaddr2str(sa, IGNORE_PORT);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000446}
447
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000448char* FAST_FUNC xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000449{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000450 return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000451}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000452char* FAST_FUNC xmalloc_sockaddr2dotted(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000453{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000454 return sockaddr2str(sa, NI_NUMERICHOST);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000455}
Denis Vlasenko448f0242007-01-22 22:43:05 +0000456
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000457char* FAST_FUNC xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000458{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000459 return sockaddr2str(sa, NI_NUMERICHOST | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000460}