blob: c3ee633e4c0d9e00187aa83534d5e54359404bf9 [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}
Jérémie Koenigfbedacf2010-03-26 19:08:53 +010024
25#ifdef SO_BINDTODEVICE
Denis Vlasenkoe5373852008-12-10 11:12:16 +000026int FAST_FUNC setsockopt_bindtodevice(int fd, const char *iface)
27{
28 int r;
29 struct ifreq ifr;
30 strncpy_IFNAMSIZ(ifr.ifr_name, iface);
Denis Vlasenkoa3ca7cf2008-12-10 11:24:33 +000031 /* NB: passing (iface, strlen(iface) + 1) does not work!
32 * (maybe it works on _some_ kernels, but not on 2.6.26)
33 * Actually, ifr_name is at offset 0, and in practice
Denis Vlasenkoe5373852008-12-10 11:12:16 +000034 * just giving char[IFNAMSIZ] instead of struct ifreq works too.
35 * But just in case it's not true on some obscure arch... */
36 r = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
37 if (r)
38 bb_perror_msg("can't bind to interface %s", iface);
39 return r;
40}
Jérémie Koenigfbedacf2010-03-26 19:08:53 +010041#else
42int FAST_FUNC setsockopt_bindtodevice(int fd UNUSED_PARAM,
43 const char *iface UNUSED_PARAM)
44{
45 bb_error_msg("SO_BINDTODEVICE is not supported on this system");
46 return -1;
47}
48#endif
Denis Vlasenkoe5373852008-12-10 11:12:16 +000049
Denys Vlasenkof6916db2010-04-06 17:43:29 +020050static len_and_sockaddr* get_lsa(int fd, int (*get_name)(int fd, struct sockaddr *addr, socklen_t *addrlen))
Denis Vlasenko73c571a2009-03-09 00:12:37 +000051{
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000052 len_and_sockaddr lsa;
53 len_and_sockaddr *lsa_ptr;
Denis Vlasenko73c571a2009-03-09 00:12:37 +000054
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000055 lsa.len = LSA_SIZEOF_SA;
Denys Vlasenkof6916db2010-04-06 17:43:29 +020056 if (get_name(fd, &lsa.u.sa, &lsa.len) != 0)
Denis Vlasenko73c571a2009-03-09 00:12:37 +000057 return NULL;
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000058
59 lsa_ptr = xzalloc(LSA_LEN_SIZE + lsa.len);
60 if (lsa.len > LSA_SIZEOF_SA) { /* rarely (if ever) happens */
61 lsa_ptr->len = lsa.len;
Denys Vlasenkof6916db2010-04-06 17:43:29 +020062 get_name(fd, &lsa_ptr->u.sa, &lsa_ptr->len);
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000063 } else {
64 memcpy(lsa_ptr, &lsa, LSA_LEN_SIZE + lsa.len);
65 }
66 return lsa_ptr;
Denis Vlasenko73c571a2009-03-09 00:12:37 +000067}
Denis Vlasenko48237b02006-11-22 23:22:06 +000068
Denys Vlasenkof6916db2010-04-06 17:43:29 +020069len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd)
70{
71 return get_lsa(fd, getsockname);
72}
73
74len_and_sockaddr* FAST_FUNC get_peer_lsa(int fd)
75{
76 return get_lsa(fd, getpeername);
77}
78
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000079void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000080{
Denis Vlasenko9de420c2007-01-10 09:28:01 +000081 if (connect(s, s_addr, addrlen) < 0) {
82 if (ENABLE_FEATURE_CLEAN_UP)
83 close(s);
84 if (s_addr->sa_family == AF_INET)
85 bb_perror_msg_and_die("%s (%s)",
Denys Vlasenko6331cf02009-11-13 09:08:27 +010086 "can't connect to remote host",
Denis Vlasenko9de420c2007-01-10 09:28:01 +000087 inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
Denys Vlasenko6331cf02009-11-13 09:08:27 +010088 bb_perror_msg_and_die("can't connect to remote host");
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000089 }
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +000090}
91
Denis Vlasenko5d687242007-01-12 20:59:31 +000092/* Return port number for a service.
Denis Vlasenko9de420c2007-01-10 09:28:01 +000093 * If "port" is a number use it as the port.
Denis Vlasenko73c571a2009-03-09 00:12:37 +000094 * If "port" is a name it is looked up in /etc/services,
95 * if it isnt found return default_port
96 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000097unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +000098{
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000099 unsigned port_nr = default_port;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000100 if (port) {
101 int old_errno;
102
103 /* Since this is a lib function, we're not allowed to reset errno to 0.
104 * Doing so could break an app that is deferring checking of errno. */
105 old_errno = errno;
106 port_nr = bb_strtou(port, NULL, 10);
107 if (errno || port_nr > 65535) {
108 struct servent *tserv = getservbyname(port, protocol);
Denis Vlasenko24182882007-01-13 21:05:38 +0000109 port_nr = default_port;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000110 if (tserv)
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000111 port_nr = ntohs(tserv->s_port);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000112 }
113 errno = old_errno;
114 }
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000115 return (uint16_t)port_nr;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000116}
117
118
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000119/* "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{
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100162IF_NOT_FEATURE_IPV6(sa_family_t af = AF_INET;)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000163 int rc;
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000164 len_and_sockaddr *r;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000165 struct addrinfo *result = NULL;
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000166 struct addrinfo *used_res;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000167 const char *org_host = host; /* only for error msg */
168 const char *cp;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000169 struct addrinfo hint;
170
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000171 if (ENABLE_FEATURE_UNIX_LOCAL && strncmp(host, "local:", 6) == 0) {
172 struct sockaddr_un *sun;
173
174 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un));
175 r->len = sizeof(struct sockaddr_un);
176 r->u.sa.sa_family = AF_UNIX;
177 sun = (struct sockaddr_un *)&r->u.sa;
178 safe_strncpy(sun->sun_path, host + 6, sizeof(sun->sun_path));
179 return r;
180 }
181
182 r = NULL;
183
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000184 /* Ugly parsing of host:addr */
185 if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000186 /* Even uglier parsing of [xx]:nn */
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000187 host++;
188 cp = strchr(host, ']');
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000189 if (!cp || (cp[1] != ':' && cp[1] != '\0')) {
190 /* Malformed: must be [xx]:nn or [xx] */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000191 bb_error_msg("bad address '%s'", org_host);
192 if (ai_flags & DIE_ON_ERROR)
193 xfunc_die();
194 return NULL;
195 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000196 } else {
197 cp = strrchr(host, ':');
198 if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
199 /* There is more than one ':' (e.g. "::1") */
200 cp = NULL; /* it's not a port spec */
201 }
202 }
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000203 if (cp) { /* points to ":" or "]:" */
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000204 int sz = cp - host + 1;
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000205
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000206 host = safe_strncpy(alloca(sz), host, sz);
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000207 if (ENABLE_FEATURE_IPV6 && *cp != ':') {
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000208 cp++; /* skip ']' */
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000209 if (*cp == '\0') /* [xx] without port */
210 goto skip;
211 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000212 cp++; /* skip ':' */
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000213 port = bb_strtou(cp, NULL, 10);
214 if (errno || (unsigned)port > 0xffff) {
215 bb_error_msg("bad port spec '%s'", org_host);
216 if (ai_flags & DIE_ON_ERROR)
217 xfunc_die();
218 return NULL;
219 }
Denis Vlasenko9b2fbda2009-03-09 13:01:08 +0000220 skip: ;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000221 }
222
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100223 /* Next two if blocks allow to skip getaddrinfo()
Denys Vlasenko75b73972009-11-24 14:06:04 +0100224 * in case host name is a numeric IP(v6) address.
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100225 * getaddrinfo() initializes DNS resolution machinery,
226 * scans network config and such - tens of syscalls.
227 */
228 /* If we were not asked specifically for IPv6,
229 * check whether this is a numeric IPv4 */
230 IF_FEATURE_IPV6(if(af != AF_INET6)) {
231 struct in_addr in4;
232 if (inet_aton(host, &in4) != 0) {
233 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in));
234 r->len = sizeof(struct sockaddr_in);
235 r->u.sa.sa_family = AF_INET;
236 r->u.sin.sin_addr = in4;
237 goto set_port;
238 }
239 }
240#if ENABLE_FEATURE_IPV6
241 /* If we were not asked specifically for IPv4,
242 * check whether this is a numeric IPv6 */
243 if (af != AF_INET) {
244 struct in6_addr in6;
245 if (inet_pton(AF_INET6, host, &in6) > 0) {
246 r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in6));
247 r->len = sizeof(struct sockaddr_in6);
248 r->u.sa.sa_family = AF_INET6;
249 r->u.sin6.sin6_addr = in6;
250 goto set_port;
251 }
252 }
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000253#endif
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100254
255 memset(&hint, 0 , sizeof(hint));
256 hint.ai_family = af;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000257 /* Needed. Or else we will get each address thrice (or more)
258 * for each possible socket type (tcp,udp,raw...): */
259 hint.ai_socktype = SOCK_STREAM;
Denis Vlasenko42823d52007-02-04 02:39:08 +0000260 hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000261 rc = getaddrinfo(host, NULL, &hint, &result);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000262 if (rc || !result) {
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000263 bb_error_msg("bad address '%s'", org_host);
Denis Vlasenko42823d52007-02-04 02:39:08 +0000264 if (ai_flags & DIE_ON_ERROR)
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000265 xfunc_die();
Denis Vlasenko42823d52007-02-04 02:39:08 +0000266 goto ret;
267 }
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000268 used_res = result;
269#if ENABLE_FEATURE_PREFER_IPV4_ADDRESS
270 while (1) {
271 if (used_res->ai_family == AF_INET)
272 break;
273 used_res = used_res->ai_next;
274 if (!used_res) {
275 used_res = result;
276 break;
277 }
278 }
279#endif
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100280 r = xmalloc(LSA_LEN_SIZE + used_res->ai_addrlen);
Denis Vlasenko9d6c4692007-11-14 10:18:33 +0000281 r->len = used_res->ai_addrlen;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000282 memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen);
Denys Vlasenkoca6c7e42009-11-24 07:07:42 +0100283
Denys Vlasenko75b73972009-11-24 14:06:04 +0100284 set_port:
Denis Vlasenko5d687242007-01-12 20:59:31 +0000285 set_nport(r, htons(port));
Denis Vlasenko42823d52007-02-04 02:39:08 +0000286 ret:
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000287 freeaddrinfo(result);
288 return r;
289}
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000290#if !ENABLE_FEATURE_IPV6
291#define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
292#endif
293
294#if ENABLE_FEATURE_IPV6
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000295len_and_sockaddr* FAST_FUNC host_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenkoe9913032007-02-04 02:41:57 +0000296{
297 return str2sockaddr(host, port, af, 0);
298}
299
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000300len_and_sockaddr* FAST_FUNC xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000301{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000302 return str2sockaddr(host, port, af, DIE_ON_ERROR);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000303}
304#endif
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000305
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000306len_and_sockaddr* FAST_FUNC host2sockaddr(const char *host, int port)
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000307{
308 return str2sockaddr(host, port, AF_UNSPEC, 0);
309}
310
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000311len_and_sockaddr* FAST_FUNC xhost2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000312{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000313 return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000314}
315
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000316len_and_sockaddr* FAST_FUNC xdotted2sockaddr(const char *host, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000317{
Denis Vlasenko42823d52007-02-04 02:39:08 +0000318 return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000319}
320
Denis Vlasenko4e6d5112008-03-12 22:14:34 +0000321#undef xsocket_type
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000322int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, IF_FEATURE_IPV6(int family,) int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000323{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000324 IF_NOT_FEATURE_IPV6(enum { family = AF_INET };)
Denis Vlasenko5d687242007-01-12 20:59:31 +0000325 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000326 int fd;
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000327 int len;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000328
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000329#if ENABLE_FEATURE_IPV6
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000330 if (family == AF_UNSPEC) {
331 fd = socket(AF_INET6, sock_type, 0);
332 if (fd >= 0) {
333 family = AF_INET6;
334 goto done;
335 }
336 family = AF_INET;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000337 }
Denis Vlasenko5c51a7c2007-06-05 20:08:11 +0000338#endif
339 fd = xsocket(family, sock_type, 0);
340 len = sizeof(struct sockaddr_in);
341#if ENABLE_FEATURE_IPV6
342 if (family == AF_INET6) {
343 done:
344 len = sizeof(struct sockaddr_in6);
345 }
346#endif
Denys Vlasenkob1278a32009-11-24 16:03:47 +0100347 lsa = xzalloc(LSA_LEN_SIZE + len);
Denis Vlasenko5d687242007-01-12 20:59:31 +0000348 lsa->len = len;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000349 lsa->u.sa.sa_family = family;
Denis Vlasenko5d687242007-01-12 20:59:31 +0000350 *lsap = lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000351 return fd;
352}
353
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000354int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000355{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000356 return xsocket_type(lsap, IF_FEATURE_IPV6(AF_UNSPEC,) SOCK_STREAM);
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000357}
358
359static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000360{
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000361 int fd;
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000362 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000363
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000364 if (bindaddr && bindaddr[0]) {
Denis Vlasenko42823d52007-02-04 02:39:08 +0000365 lsa = xdotted2sockaddr(bindaddr, port);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000366 /* user specified bind addr dictates family */
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000367 fd = xsocket(lsa->u.sa.sa_family, sock_type, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000368 } else {
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000369 fd = xsocket_type(&lsa, IF_FEATURE_IPV6(AF_UNSPEC,) sock_type);
Denis Vlasenko24182882007-01-13 21:05:38 +0000370 set_nport(lsa, htons(port));
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000371 }
Denis Vlasenko48237b02006-11-22 23:22:06 +0000372 setsockopt_reuseaddr(fd);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000373 xbind(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000374 free(lsa);
375 return fd;
376}
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000377
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000378int FAST_FUNC create_and_bind_stream_or_die(const char *bindaddr, int port)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000379{
380 return create_and_bind_or_die(bindaddr, port, SOCK_STREAM);
381}
382
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000383int FAST_FUNC create_and_bind_dgram_or_die(const char *bindaddr, int port)
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000384{
385 return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM);
386}
387
388
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000389int FAST_FUNC create_and_connect_stream_or_die(const char *peer, int port)
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000390{
391 int fd;
392 len_and_sockaddr *lsa;
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000393
Denis Vlasenko42823d52007-02-04 02:39:08 +0000394 lsa = xhost2sockaddr(peer, port);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000395 fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000396 setsockopt_reuseaddr(fd);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000397 xconnect(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000398 free(lsa);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000399 return fd;
400}
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000401
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000402int FAST_FUNC xconnect_stream(const len_and_sockaddr *lsa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000403{
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000404 int fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
405 xconnect(fd, &lsa->u.sa, lsa->len);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000406 return fd;
407}
408
Denis Vlasenko448f0242007-01-22 22:43:05 +0000409/* We hijack this constant to mean something else */
410/* It doesn't hurt because we will add this bit anyway */
411#define IGNORE_PORT NI_NUMERICSERV
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000412static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000413{
414 char host[128];
415 char serv[16];
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000416 int rc;
417 socklen_t salen;
418
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000419 if (ENABLE_FEATURE_UNIX_LOCAL && sa->sa_family == AF_UNIX) {
420 struct sockaddr_un *sun = (struct sockaddr_un *)sa;
421 return xasprintf("local:%.*s",
422 (int) sizeof(sun->sun_path),
423 sun->sun_path);
424 }
425
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000426 salen = LSA_SIZEOF_SA;
427#if ENABLE_FEATURE_IPV6
428 if (sa->sa_family == AF_INET)
429 salen = sizeof(struct sockaddr_in);
430 if (sa->sa_family == AF_INET6)
431 salen = sizeof(struct sockaddr_in6);
432#endif
433 rc = getnameinfo(sa, salen,
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000434 host, sizeof(host),
Denis Vlasenko448f0242007-01-22 22:43:05 +0000435 /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000436 serv, sizeof(serv),
Denis Vlasenko85629f02007-01-22 09:36:41 +0000437 /* do not resolve port# into service _name_ */
438 flags | NI_NUMERICSERV
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000439 );
Denis Vlasenko85629f02007-01-22 09:36:41 +0000440 if (rc)
441 return NULL;
Denis Vlasenko448f0242007-01-22 22:43:05 +0000442 if (flags & IGNORE_PORT)
443 return xstrdup(host);
Denis Vlasenko85629f02007-01-22 09:36:41 +0000444#if ENABLE_FEATURE_IPV6
Denis Vlasenko448f0242007-01-22 22:43:05 +0000445 if (sa->sa_family == AF_INET6) {
446 if (strchr(host, ':')) /* heh, it's not a resolved hostname */
447 return xasprintf("[%s]:%s", host, serv);
448 /*return xasprintf("%s:%s", host, serv);*/
449 /* - fall through instead */
450 }
Denis Vlasenko85629f02007-01-22 09:36:41 +0000451#endif
452 /* For now we don't support anything else, so it has to be INET */
453 /*if (sa->sa_family == AF_INET)*/
454 return xasprintf("%s:%s", host, serv);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000455 /*return xstrdup(host);*/
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000456}
457
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000458char* FAST_FUNC xmalloc_sockaddr2host(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000459{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000460 return sockaddr2str(sa, 0);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000461}
462
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000463char* FAST_FUNC xmalloc_sockaddr2host_noport(const struct sockaddr *sa)
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000464{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000465 return sockaddr2str(sa, IGNORE_PORT);
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000466}
467
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000468char* FAST_FUNC xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000469{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000470 return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000471}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000472char* FAST_FUNC xmalloc_sockaddr2dotted(const struct sockaddr *sa)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000473{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000474 return sockaddr2str(sa, NI_NUMERICHOST);
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000475}
Denis Vlasenko448f0242007-01-22 22:43:05 +0000476
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000477char* FAST_FUNC xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa)
Denis Vlasenko448f0242007-01-22 22:43:05 +0000478{
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000479 return sockaddr2str(sa, NI_NUMERICHOST | IGNORE_PORT);
Denis Vlasenko448f0242007-01-22 22:43:05 +0000480}