blob: 8a1e1c1b28bda42c098849a3e893d3ec0c9ab859 [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
99/* "Old" networking API - only IPv4 */
100
Denis Vlasenko85629f02007-01-22 09:36:41 +0000101/*
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000102void FAST_FUNC bb_lookup_host(struct sockaddr_in *s_in, const char *host)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000103{
104 struct hostent *he;
105
106 memset(s_in, 0, sizeof(struct sockaddr_in));
107 s_in->sin_family = AF_INET;
108 he = xgethostbyname(host);
109 memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
110}
111
Denis Vlasenko85629f02007-01-22 09:36:41 +0000112
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000113int FAST_FUNC xconnect_tcp_v4(struct sockaddr_in *s_addr)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000114{
115 int s = xsocket(AF_INET, SOCK_STREAM, 0);
116 xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr));
117 return s;
118}
Denis Vlasenko85629f02007-01-22 09:36:41 +0000119*/
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000120
121/* "New" networking API */
122
123
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000124int FAST_FUNC get_nport(const struct sockaddr *sa)
Denis Vlasenko5d687242007-01-12 20:59:31 +0000125{
126#if ENABLE_FEATURE_IPV6
Denis Vlasenko2856dab2007-04-01 01:18:20 +0000127 if (sa->sa_family == AF_INET6) {
128 return ((struct sockaddr_in6*)sa)->sin6_port;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000129 }
130#endif
Denis Vlasenko2856dab2007-04-01 01:18:20 +0000131 if (sa->sa_family == AF_INET) {
132 return ((struct sockaddr_in*)sa)->sin_port;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000133 }
Denis Vlasenko5d687242007-01-12 20:59:31 +0000134 /* What? UNIX socket? IPX?? :) */
Denis Vlasenko85629f02007-01-22 09:36:41 +0000135 return -1;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000136}
137
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000138void FAST_FUNC set_nport(len_and_sockaddr *lsa, unsigned port)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000139{
140#if ENABLE_FEATURE_IPV6
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000141 if (lsa->u.sa.sa_family == AF_INET6) {
142 lsa->u.sin6.sin6_port = port;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000143 return;
144 }
145#endif
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000146 if (lsa->u.sa.sa_family == AF_INET) {
147 lsa->u.sin.sin_port = port;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000148 return;
149 }
150 /* What? UNIX socket? IPX?? :) */
151}
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000152
Denis Vlasenko42823d52007-02-04 02:39:08 +0000153/* We hijack this constant to mean something else */
154/* It doesn't hurt because we will remove this bit anyway */
155#define DIE_ON_ERROR AI_CANONNAME
156
Denis Vlasenko85629f02007-01-22 09:36:41 +0000157/* host: "1.2.3.4[:port]", "www.google.com[:port]"
Denis Vlasenko42823d52007-02-04 02:39:08 +0000158 * port: if neither of above specifies port # */
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000159static len_and_sockaddr* str2sockaddr(
160 const char *host, int port,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000161IF_FEATURE_IPV6(sa_family_t af,)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000162 int ai_flags)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000163{
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100164IF_NOT_FEATURE_IPV6(sa_family_t af = AF_INET;)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000165 int rc;
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000166 len_and_sockaddr *r;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000167 struct addrinfo *result = NULL;
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000168 struct addrinfo *used_res;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000169 const char *org_host = host; /* only for error msg */
170 const char *cp;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000171 struct addrinfo hint;
172
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000173 if (ENABLE_FEATURE_UNIX_LOCAL && strncmp(host, "local:", 6) == 0) {
174 struct sockaddr_un *sun;
175
176 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un));
177 r->len = sizeof(struct sockaddr_un);
178 r->u.sa.sa_family = AF_UNIX;
179 sun = (struct sockaddr_un *)&r->u.sa;
180 safe_strncpy(sun->sun_path, host + 6, sizeof(sun->sun_path));
181 return r;
182 }
183
184 r = NULL;
185
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000186 /* Ugly parsing of host:addr */
187 if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000188 /* Even uglier parsing of [xx]:nn */
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000189 host++;
190 cp = strchr(host, ']');
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000191 if (!cp || (cp[1] != ':' && cp[1] != '\0')) {
192 /* Malformed: must be [xx]:nn or [xx] */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000193 bb_error_msg("bad address '%s'", org_host);
194 if (ai_flags & DIE_ON_ERROR)
195 xfunc_die();
196 return NULL;
197 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000198 } else {
199 cp = strrchr(host, ':');
200 if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
201 /* There is more than one ':' (e.g. "::1") */
202 cp = NULL; /* it's not a port spec */
203 }
204 }
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000205 if (cp) { /* points to ":" or "]:" */
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000206 int sz = cp - host + 1;
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000207
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000208 host = safe_strncpy(alloca(sz), host, sz);
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000209 if (ENABLE_FEATURE_IPV6 && *cp != ':') {
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000210 cp++; /* skip ']' */
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000211 if (*cp == '\0') /* [xx] without port */
212 goto skip;
213 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000214 cp++; /* skip ':' */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000215 port = bb_strtou(cp, NULL, 10);
216 if (errno || (unsigned)port > 0xffff) {
217 bb_error_msg("bad port spec '%s'", org_host);
218 if (ai_flags & DIE_ON_ERROR)
219 xfunc_die();
220 return NULL;
221 }
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000222 skip: ;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000223 }
224
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100225 /* Next two if blocks allow to skip getaddrinfo()
Denys Vlasenko75b73972009-11-24 14:06:04 +0100226 * in case host name is a numeric IP(v6) address.
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100227 * getaddrinfo() initializes DNS resolution machinery,
228 * scans network config and such - tens of syscalls.
229 */
230 /* If we were not asked specifically for IPv6,
231 * check whether this is a numeric IPv4 */
232 IF_FEATURE_IPV6(if(af != AF_INET6)) {
233 struct in_addr in4;
234 if (inet_aton(host, &in4) != 0) {
235 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in));
236 r->len = sizeof(struct sockaddr_in);
237 r->u.sa.sa_family = AF_INET;
238 r->u.sin.sin_addr = in4;
239 goto set_port;
240 }
241 }
242#if ENABLE_FEATURE_IPV6
243 /* If we were not asked specifically for IPv4,
244 * check whether this is a numeric IPv6 */
245 if (af != AF_INET) {
246 struct in6_addr in6;
247 if (inet_pton(AF_INET6, host, &in6) > 0) {
248 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in6));
249 r->len = sizeof(struct sockaddr_in6);
250 r->u.sa.sa_family = AF_INET6;
251 r->u.sin6.sin6_addr = in6;
252 goto set_port;
253 }
254 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000255#endif
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100256
257 memset(&hint, 0 , sizeof(hint));
258 hint.ai_family = af;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000259 /* Needed. Or else we will get each address thrice (or more)
260 * for each possible socket type (tcp,udp,raw...): */
261 hint.ai_socktype = SOCK_STREAM;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000262 hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000263 rc = getaddrinfo(host, NULL, &hint, &result);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000264 if (rc || !result) {
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000265 bb_error_msg("bad address '%s'", org_host);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000266 if (ai_flags & DIE_ON_ERROR)
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000267 xfunc_die();
Denis Vlasenko42823d52007-02-04 02:39:08 +0000268 goto ret;
269 }
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000270 used_res = result;
271#if ENABLE_FEATURE_PREFER_IPV4_ADDRESS
272 while (1) {
273 if (used_res->ai_family == AF_INET)
274 break;
275 used_res = used_res->ai_next;
276 if (!used_res) {
277 used_res = result;
278 break;
279 }
280 }
281#endif
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100282 r = xmalloc(LSA_LEN_SIZE + used_res->ai_addrlen);
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000283 r->len = used_res->ai_addrlen;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000284 memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen);
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100285
Denys Vlasenko75b73972009-11-24 14:06:04 +0100286 set_port:
Denis Vlasenko5d687242007-01-12 20:59:31 +0000287 set_nport(r, htons(port));
Denis Vlasenko42823d52007-02-04 02:39:08 +0000288 ret:
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000289 freeaddrinfo(result);
290 return r;
291}
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000292#if !ENABLE_FEATURE_IPV6
293#define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
294#endif
295
296#if ENABLE_FEATURE_IPV6
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000297len_and_sockaddr* FAST_FUNC host_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenkoe9913032007-02-04 02:41:57 +0000298{
299 return str2sockaddr(host, port, af, 0);
300}
301
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000302len_and_sockaddr* FAST_FUNC xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000303{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000304 return str2sockaddr(host, port, af, DIE_ON_ERROR);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000305}
306#endif
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000307
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000308len_and_sockaddr* FAST_FUNC host2sockaddr(const char *host, int port)
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000309{
310 return str2sockaddr(host, port, AF_UNSPEC, 0);
311}
312
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000313len_and_sockaddr* FAST_FUNC xhost2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000314{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000315 return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000316}
317
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000318len_and_sockaddr* FAST_FUNC xdotted2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000319{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000320 return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000321}
322
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000323#undef xsocket_type
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000324int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, IF_FEATURE_IPV6(int family,) int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000325{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000326 IF_NOT_FEATURE_IPV6(enum { family = AF_INET };)
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 Vlasenkoc8717cd2006-11-22 16:10:39 +0000331#if ENABLE_FEATURE_IPV6
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000332 if (family == AF_UNSPEC) {
333 fd = socket(AF_INET6, sock_type, 0);
334 if (fd >= 0) {
335 family = AF_INET6;
336 goto done;
337 }
338 family = AF_INET;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000339 }
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000340#endif
341 fd = xsocket(family, sock_type, 0);
342 len = sizeof(struct sockaddr_in);
343#if ENABLE_FEATURE_IPV6
344 if (family == AF_INET6) {
345 done:
346 len = sizeof(struct sockaddr_in6);
347 }
348#endif
Denys Vlasenkob1278a32009-11-24 16:03:47 +0100349 lsa = xzalloc(LSA_LEN_SIZE + len);
Denis Vlasenko5d687242007-01-12 20:59:31 +0000350 lsa->len = len;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000351 lsa->u.sa.sa_family = family;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000352 *lsap = lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000353 return fd;
354}
355
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000356int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000357{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000358 return xsocket_type(lsap, IF_FEATURE_IPV6(AF_UNSPEC,) SOCK_STREAM);
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000359}
360
361static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000362{
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000363 int fd;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000364 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000365
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000366 if (bindaddr && bindaddr[0]) {
Denis Vlasenko42823d52007-02-04 02:39:08 +0000367 lsa = xdotted2sockaddr(bindaddr, port);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000368 /* user specified bind addr dictates family */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000369 fd = xsocket(lsa->u.sa.sa_family, sock_type, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000370 } else {
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000371 fd = xsocket_type(&lsa, IF_FEATURE_IPV6(AF_UNSPEC,) sock_type);
Denis Vlasenko24182882007-01-13 21:05:38 +0000372 set_nport(lsa, htons(port));
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000373 }
Denis Vlasenko48237b02006-11-22 23:22:06 +0000374 setsockopt_reuseaddr(fd);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000375 xbind(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000376 free(lsa);
377 return fd;
378}
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000379
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000380int FAST_FUNC create_and_bind_stream_or_die(const char *bindaddr, int port)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000381{
382 return create_and_bind_or_die(bindaddr, port, SOCK_STREAM);
383}
384
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000385int FAST_FUNC create_and_bind_dgram_or_die(const char *bindaddr, int port)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000386{
387 return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM);
388}
389
390
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000391int FAST_FUNC create_and_connect_stream_or_die(const char *peer, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000392{
393 int fd;
394 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000395
Denis Vlasenko42823d52007-02-04 02:39:08 +0000396 lsa = xhost2sockaddr(peer, port);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000397 fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000398 setsockopt_reuseaddr(fd);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000399 xconnect(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000400 free(lsa);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000401 return fd;
402}
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000403
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000404int FAST_FUNC xconnect_stream(const len_and_sockaddr *lsa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000405{
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000406 int fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
407 xconnect(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000408 return fd;
409}
410
Denis Vlasenko448f0242007-01-22 22:43:05 +0000411/* We hijack this constant to mean something else */
412/* It doesn't hurt because we will add this bit anyway */
413#define IGNORE_PORT NI_NUMERICSERV
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000414static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000415{
416 char host[128];
417 char serv[16];
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000418 int rc;
419 socklen_t salen;
420
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000421 if (ENABLE_FEATURE_UNIX_LOCAL && sa->sa_family == AF_UNIX) {
422 struct sockaddr_un *sun = (struct sockaddr_un *)sa;
423 return xasprintf("local:%.*s",
424 (int) sizeof(sun->sun_path),
425 sun->sun_path);
426 }
427
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000428 salen = LSA_SIZEOF_SA;
429#if ENABLE_FEATURE_IPV6
430 if (sa->sa_family == AF_INET)
431 salen = sizeof(struct sockaddr_in);
432 if (sa->sa_family == AF_INET6)
433 salen = sizeof(struct sockaddr_in6);
434#endif
435 rc = getnameinfo(sa, salen,
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000436 host, sizeof(host),
Denis Vlasenko448f0242007-01-22 22:43:05 +0000437 /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000438 serv, sizeof(serv),
Denis Vlasenko85629f02007-01-22 09:36:41 +0000439 /* do not resolve port# into service _name_ */
440 flags | NI_NUMERICSERV
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000441 );
Denis Vlasenko85629f02007-01-22 09:36:41 +0000442 if (rc)
443 return NULL;
Denis Vlasenko448f0242007-01-22 22:43:05 +0000444 if (flags & IGNORE_PORT)
445 return xstrdup(host);
Denis Vlasenko85629f02007-01-22 09:36:41 +0000446#if ENABLE_FEATURE_IPV6
Denis Vlasenko448f0242007-01-22 22:43:05 +0000447 if (sa->sa_family == AF_INET6) {
448 if (strchr(host, ':')) /* heh, it's not a resolved hostname */
449 return xasprintf("[%s]:%s", host, serv);
450 /*return xasprintf("%s:%s", host, serv);*/
451 /* - fall through instead */
452 }
Denis Vlasenko85629f02007-01-22 09:36:41 +0000453#endif
454 /* For now we don't support anything else, so it has to be INET */
455 /*if (sa->sa_family == AF_INET)*/
456 return xasprintf("%s:%s", host, serv);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000457 /*return xstrdup(host);*/
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000458}
459
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000460char* FAST_FUNC xmalloc_sockaddr2host(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000461{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000462 return sockaddr2str(sa, 0);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000463}
464
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000465char* FAST_FUNC xmalloc_sockaddr2host_noport(const struct sockaddr *sa)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000466{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000467 return sockaddr2str(sa, IGNORE_PORT);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000468}
469
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000470char* FAST_FUNC xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000471{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000472 return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000473}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000474char* FAST_FUNC xmalloc_sockaddr2dotted(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000475{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000476 return sockaddr2str(sa, NI_NUMERICHOST);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000477}
Denis Vlasenko448f0242007-01-22 22:43:05 +0000478
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000479char* FAST_FUNC xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000480{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000481 return sockaddr2str(sa, NI_NUMERICHOST | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000482}