Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 5 | * Connect to host at port using address resolution from getaddrinfo |
Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 6 | * |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Dan Fandrich | fdd7b56 | 2010-06-18 22:37:42 -0700 | [diff] [blame] | 10 | #include <sys/types.h> |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 11 | #include <sys/socket.h> /* netinet/in.h needs it */ |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 12 | #include <netinet/in.h> |
Denis Vlasenko | e537385 | 2008-12-10 11:12:16 +0000 | [diff] [blame] | 13 | #include <net/if.h> |
Denis Vlasenko | f6b4685 | 2009-04-25 13:16:53 +0000 | [diff] [blame] | 14 | #include <sys/un.h> |
Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 15 | #include "libbb.h" |
| 16 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 17 | void FAST_FUNC setsockopt_reuseaddr(int fd) |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 18 | { |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 19 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &const_int_1, sizeof(const_int_1)); |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 20 | } |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 21 | int FAST_FUNC setsockopt_broadcast(int fd) |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 22 | { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 23 | return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1)); |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 24 | } |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 25 | |
| 26 | #ifdef SO_BINDTODEVICE |
Denis Vlasenko | e537385 | 2008-12-10 11:12:16 +0000 | [diff] [blame] | 27 | int FAST_FUNC setsockopt_bindtodevice(int fd, const char *iface) |
| 28 | { |
| 29 | int r; |
| 30 | struct ifreq ifr; |
| 31 | strncpy_IFNAMSIZ(ifr.ifr_name, iface); |
Denis Vlasenko | a3ca7cf | 2008-12-10 11:24:33 +0000 | [diff] [blame] | 32 | /* NB: passing (iface, strlen(iface) + 1) does not work! |
| 33 | * (maybe it works on _some_ kernels, but not on 2.6.26) |
| 34 | * Actually, ifr_name is at offset 0, and in practice |
Denis Vlasenko | e537385 | 2008-12-10 11:12:16 +0000 | [diff] [blame] | 35 | * just giving char[IFNAMSIZ] instead of struct ifreq works too. |
| 36 | * But just in case it's not true on some obscure arch... */ |
| 37 | r = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)); |
| 38 | if (r) |
| 39 | bb_perror_msg("can't bind to interface %s", iface); |
| 40 | return r; |
| 41 | } |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 42 | #else |
| 43 | int FAST_FUNC setsockopt_bindtodevice(int fd UNUSED_PARAM, |
| 44 | const char *iface UNUSED_PARAM) |
| 45 | { |
| 46 | bb_error_msg("SO_BINDTODEVICE is not supported on this system"); |
| 47 | return -1; |
| 48 | } |
| 49 | #endif |
Denis Vlasenko | e537385 | 2008-12-10 11:12:16 +0000 | [diff] [blame] | 50 | |
Denys Vlasenko | f6916db | 2010-04-06 17:43:29 +0200 | [diff] [blame] | 51 | static len_and_sockaddr* get_lsa(int fd, int (*get_name)(int fd, struct sockaddr *addr, socklen_t *addrlen)) |
Denis Vlasenko | 73c571a | 2009-03-09 00:12:37 +0000 | [diff] [blame] | 52 | { |
Denis Vlasenko | a771e7c | 2009-04-21 23:48:38 +0000 | [diff] [blame] | 53 | len_and_sockaddr lsa; |
| 54 | len_and_sockaddr *lsa_ptr; |
Denis Vlasenko | 73c571a | 2009-03-09 00:12:37 +0000 | [diff] [blame] | 55 | |
Denis Vlasenko | a771e7c | 2009-04-21 23:48:38 +0000 | [diff] [blame] | 56 | lsa.len = LSA_SIZEOF_SA; |
Denys Vlasenko | f6916db | 2010-04-06 17:43:29 +0200 | [diff] [blame] | 57 | if (get_name(fd, &lsa.u.sa, &lsa.len) != 0) |
Denis Vlasenko | 73c571a | 2009-03-09 00:12:37 +0000 | [diff] [blame] | 58 | return NULL; |
Denis Vlasenko | a771e7c | 2009-04-21 23:48:38 +0000 | [diff] [blame] | 59 | |
| 60 | lsa_ptr = xzalloc(LSA_LEN_SIZE + lsa.len); |
| 61 | if (lsa.len > LSA_SIZEOF_SA) { /* rarely (if ever) happens */ |
| 62 | lsa_ptr->len = lsa.len; |
Denys Vlasenko | f6916db | 2010-04-06 17:43:29 +0200 | [diff] [blame] | 63 | get_name(fd, &lsa_ptr->u.sa, &lsa_ptr->len); |
Denis Vlasenko | a771e7c | 2009-04-21 23:48:38 +0000 | [diff] [blame] | 64 | } else { |
| 65 | memcpy(lsa_ptr, &lsa, LSA_LEN_SIZE + lsa.len); |
| 66 | } |
| 67 | return lsa_ptr; |
Denis Vlasenko | 73c571a | 2009-03-09 00:12:37 +0000 | [diff] [blame] | 68 | } |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 69 | |
Denys Vlasenko | f6916db | 2010-04-06 17:43:29 +0200 | [diff] [blame] | 70 | len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd) |
| 71 | { |
| 72 | return get_lsa(fd, getsockname); |
| 73 | } |
| 74 | |
| 75 | len_and_sockaddr* FAST_FUNC get_peer_lsa(int fd) |
| 76 | { |
| 77 | return get_lsa(fd, getpeername); |
| 78 | } |
| 79 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 80 | void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen) |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 81 | { |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 82 | if (connect(s, s_addr, addrlen) < 0) { |
| 83 | if (ENABLE_FEATURE_CLEAN_UP) |
| 84 | close(s); |
| 85 | if (s_addr->sa_family == AF_INET) |
| 86 | bb_perror_msg_and_die("%s (%s)", |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 87 | "can't connect to remote host", |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 88 | inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr)); |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 89 | bb_perror_msg_and_die("can't connect to remote host"); |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 90 | } |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 93 | /* Return port number for a service. |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 94 | * If "port" is a number use it as the port. |
Denis Vlasenko | 73c571a | 2009-03-09 00:12:37 +0000 | [diff] [blame] | 95 | * If "port" is a name it is looked up in /etc/services, |
| 96 | * if it isnt found return default_port |
| 97 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 98 | unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned default_port) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 99 | { |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 100 | unsigned port_nr = default_port; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 101 | if (port) { |
| 102 | int old_errno; |
| 103 | |
| 104 | /* Since this is a lib function, we're not allowed to reset errno to 0. |
| 105 | * Doing so could break an app that is deferring checking of errno. */ |
| 106 | old_errno = errno; |
| 107 | port_nr = bb_strtou(port, NULL, 10); |
| 108 | if (errno || port_nr > 65535) { |
| 109 | struct servent *tserv = getservbyname(port, protocol); |
Denis Vlasenko | 2418288 | 2007-01-13 21:05:38 +0000 | [diff] [blame] | 110 | port_nr = default_port; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 111 | if (tserv) |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 112 | port_nr = ntohs(tserv->s_port); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 113 | } |
| 114 | errno = old_errno; |
| 115 | } |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 116 | return (uint16_t)port_nr; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 120 | /* "New" networking API */ |
| 121 | |
| 122 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 123 | int FAST_FUNC get_nport(const struct sockaddr *sa) |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 124 | { |
| 125 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 126 | if (sa->sa_family == AF_INET6) { |
| 127 | return ((struct sockaddr_in6*)sa)->sin6_port; |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 128 | } |
| 129 | #endif |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 130 | if (sa->sa_family == AF_INET) { |
| 131 | return ((struct sockaddr_in*)sa)->sin_port; |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 132 | } |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 133 | /* What? UNIX socket? IPX?? :) */ |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 134 | return -1; |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 137 | void FAST_FUNC set_nport(len_and_sockaddr *lsa, unsigned port) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 138 | { |
| 139 | #if ENABLE_FEATURE_IPV6 |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 140 | if (lsa->u.sa.sa_family == AF_INET6) { |
| 141 | lsa->u.sin6.sin6_port = port; |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 142 | return; |
| 143 | } |
| 144 | #endif |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 145 | if (lsa->u.sa.sa_family == AF_INET) { |
| 146 | lsa->u.sin.sin_port = port; |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 147 | return; |
| 148 | } |
| 149 | /* What? UNIX socket? IPX?? :) */ |
| 150 | } |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 151 | |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 152 | /* We hijack this constant to mean something else */ |
| 153 | /* It doesn't hurt because we will remove this bit anyway */ |
| 154 | #define DIE_ON_ERROR AI_CANONNAME |
| 155 | |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 156 | /* host: "1.2.3.4[:port]", "www.google.com[:port]" |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 157 | * port: if neither of above specifies port # */ |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 158 | static len_and_sockaddr* str2sockaddr( |
| 159 | const char *host, int port, |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 160 | IF_FEATURE_IPV6(sa_family_t af,) |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 161 | int ai_flags) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 162 | { |
Denys Vlasenko | ca6c7e4 | 2009-11-24 07:07:42 +0100 | [diff] [blame] | 163 | IF_NOT_FEATURE_IPV6(sa_family_t af = AF_INET;) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 164 | int rc; |
Denis Vlasenko | f6b4685 | 2009-04-25 13:16:53 +0000 | [diff] [blame] | 165 | len_and_sockaddr *r; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 166 | struct addrinfo *result = NULL; |
Denis Vlasenko | 9d6c469 | 2007-11-14 10:18:33 +0000 | [diff] [blame] | 167 | struct addrinfo *used_res; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 168 | const char *org_host = host; /* only for error msg */ |
| 169 | const char *cp; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 170 | struct addrinfo hint; |
| 171 | |
Denis Vlasenko | f6b4685 | 2009-04-25 13:16:53 +0000 | [diff] [blame] | 172 | if (ENABLE_FEATURE_UNIX_LOCAL && strncmp(host, "local:", 6) == 0) { |
| 173 | struct sockaddr_un *sun; |
| 174 | |
| 175 | r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un)); |
| 176 | r->len = sizeof(struct sockaddr_un); |
| 177 | r->u.sa.sa_family = AF_UNIX; |
| 178 | sun = (struct sockaddr_un *)&r->u.sa; |
| 179 | safe_strncpy(sun->sun_path, host + 6, sizeof(sun->sun_path)); |
| 180 | return r; |
| 181 | } |
| 182 | |
| 183 | r = NULL; |
| 184 | |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 185 | /* Ugly parsing of host:addr */ |
| 186 | if (ENABLE_FEATURE_IPV6 && host[0] == '[') { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 187 | /* Even uglier parsing of [xx]:nn */ |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 188 | host++; |
| 189 | cp = strchr(host, ']'); |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 190 | if (!cp || (cp[1] != ':' && cp[1] != '\0')) { |
| 191 | /* Malformed: must be [xx]:nn or [xx] */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 192 | bb_error_msg("bad address '%s'", org_host); |
| 193 | if (ai_flags & DIE_ON_ERROR) |
| 194 | xfunc_die(); |
| 195 | return NULL; |
| 196 | } |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 197 | } else { |
| 198 | cp = strrchr(host, ':'); |
| 199 | if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) { |
| 200 | /* There is more than one ':' (e.g. "::1") */ |
| 201 | cp = NULL; /* it's not a port spec */ |
| 202 | } |
| 203 | } |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 204 | if (cp) { /* points to ":" or "]:" */ |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame] | 205 | int sz = cp - host + 1; |
Denis Vlasenko | f6b4685 | 2009-04-25 13:16:53 +0000 | [diff] [blame] | 206 | |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame] | 207 | host = safe_strncpy(alloca(sz), host, sz); |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 208 | if (ENABLE_FEATURE_IPV6 && *cp != ':') { |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 209 | cp++; /* skip ']' */ |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 210 | if (*cp == '\0') /* [xx] without port */ |
| 211 | goto skip; |
| 212 | } |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 213 | cp++; /* skip ':' */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 214 | port = bb_strtou(cp, NULL, 10); |
| 215 | if (errno || (unsigned)port > 0xffff) { |
| 216 | bb_error_msg("bad port spec '%s'", org_host); |
| 217 | if (ai_flags & DIE_ON_ERROR) |
| 218 | xfunc_die(); |
| 219 | return NULL; |
| 220 | } |
Denis Vlasenko | 9b2fbda | 2009-03-09 13:01:08 +0000 | [diff] [blame] | 221 | skip: ; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Denys Vlasenko | ca6c7e4 | 2009-11-24 07:07:42 +0100 | [diff] [blame] | 224 | /* Next two if blocks allow to skip getaddrinfo() |
Denys Vlasenko | 75b7397 | 2009-11-24 14:06:04 +0100 | [diff] [blame] | 225 | * in case host name is a numeric IP(v6) address. |
Denys Vlasenko | ca6c7e4 | 2009-11-24 07:07:42 +0100 | [diff] [blame] | 226 | * getaddrinfo() initializes DNS resolution machinery, |
| 227 | * scans network config and such - tens of syscalls. |
| 228 | */ |
| 229 | /* If we were not asked specifically for IPv6, |
| 230 | * check whether this is a numeric IPv4 */ |
| 231 | IF_FEATURE_IPV6(if(af != AF_INET6)) { |
| 232 | struct in_addr in4; |
| 233 | if (inet_aton(host, &in4) != 0) { |
| 234 | r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in)); |
| 235 | r->len = sizeof(struct sockaddr_in); |
| 236 | r->u.sa.sa_family = AF_INET; |
| 237 | r->u.sin.sin_addr = in4; |
| 238 | goto set_port; |
| 239 | } |
| 240 | } |
| 241 | #if ENABLE_FEATURE_IPV6 |
| 242 | /* If we were not asked specifically for IPv4, |
| 243 | * check whether this is a numeric IPv6 */ |
| 244 | if (af != AF_INET) { |
| 245 | struct in6_addr in6; |
| 246 | if (inet_pton(AF_INET6, host, &in6) > 0) { |
| 247 | r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in6)); |
| 248 | r->len = sizeof(struct sockaddr_in6); |
| 249 | r->u.sa.sa_family = AF_INET6; |
| 250 | r->u.sin6.sin6_addr = in6; |
| 251 | goto set_port; |
| 252 | } |
| 253 | } |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 254 | #endif |
Denys Vlasenko | ca6c7e4 | 2009-11-24 07:07:42 +0100 | [diff] [blame] | 255 | |
| 256 | memset(&hint, 0 , sizeof(hint)); |
| 257 | hint.ai_family = af; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 258 | /* Needed. Or else we will get each address thrice (or more) |
| 259 | * for each possible socket type (tcp,udp,raw...): */ |
| 260 | hint.ai_socktype = SOCK_STREAM; |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 261 | hint.ai_flags = ai_flags & ~DIE_ON_ERROR; |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 262 | rc = getaddrinfo(host, NULL, &hint, &result); |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 263 | if (rc || !result) { |
Denis Vlasenko | 5870ad9 | 2007-02-04 02:39:55 +0000 | [diff] [blame] | 264 | bb_error_msg("bad address '%s'", org_host); |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 265 | if (ai_flags & DIE_ON_ERROR) |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 266 | xfunc_die(); |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 267 | goto ret; |
| 268 | } |
Denis Vlasenko | 9d6c469 | 2007-11-14 10:18:33 +0000 | [diff] [blame] | 269 | used_res = result; |
| 270 | #if ENABLE_FEATURE_PREFER_IPV4_ADDRESS |
| 271 | while (1) { |
| 272 | if (used_res->ai_family == AF_INET) |
| 273 | break; |
| 274 | used_res = used_res->ai_next; |
| 275 | if (!used_res) { |
| 276 | used_res = result; |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | #endif |
Denys Vlasenko | ca6c7e4 | 2009-11-24 07:07:42 +0100 | [diff] [blame] | 281 | r = xmalloc(LSA_LEN_SIZE + used_res->ai_addrlen); |
Denis Vlasenko | 9d6c469 | 2007-11-14 10:18:33 +0000 | [diff] [blame] | 282 | r->len = used_res->ai_addrlen; |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 283 | memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen); |
Denys Vlasenko | ca6c7e4 | 2009-11-24 07:07:42 +0100 | [diff] [blame] | 284 | |
Denys Vlasenko | 75b7397 | 2009-11-24 14:06:04 +0100 | [diff] [blame] | 285 | set_port: |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 286 | set_nport(r, htons(port)); |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 287 | ret: |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 288 | freeaddrinfo(result); |
| 289 | return r; |
| 290 | } |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 291 | #if !ENABLE_FEATURE_IPV6 |
| 292 | #define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags) |
| 293 | #endif |
| 294 | |
| 295 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 296 | len_and_sockaddr* FAST_FUNC host_and_af2sockaddr(const char *host, int port, sa_family_t af) |
Denis Vlasenko | e991303 | 2007-02-04 02:41:57 +0000 | [diff] [blame] | 297 | { |
| 298 | return str2sockaddr(host, port, af, 0); |
| 299 | } |
| 300 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 301 | len_and_sockaddr* FAST_FUNC xhost_and_af2sockaddr(const char *host, int port, sa_family_t af) |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 302 | { |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 303 | return str2sockaddr(host, port, af, DIE_ON_ERROR); |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 304 | } |
| 305 | #endif |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 306 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 307 | len_and_sockaddr* FAST_FUNC host2sockaddr(const char *host, int port) |
Denis Vlasenko | 5870ad9 | 2007-02-04 02:39:55 +0000 | [diff] [blame] | 308 | { |
| 309 | return str2sockaddr(host, port, AF_UNSPEC, 0); |
| 310 | } |
| 311 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 312 | len_and_sockaddr* FAST_FUNC xhost2sockaddr(const char *host, int port) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 313 | { |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 314 | return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 317 | len_and_sockaddr* FAST_FUNC xdotted2sockaddr(const char *host, int port) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 318 | { |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 319 | return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 322 | #undef xsocket_type |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 323 | int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, IF_FEATURE_IPV6(int family,) int sock_type) |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 324 | { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 325 | IF_NOT_FEATURE_IPV6(enum { family = AF_INET };) |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 326 | len_and_sockaddr *lsa; |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 327 | int fd; |
Denis Vlasenko | 5c51a7c | 2007-06-05 20:08:11 +0000 | [diff] [blame] | 328 | int len; |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 329 | |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 330 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 5c51a7c | 2007-06-05 20:08:11 +0000 | [diff] [blame] | 331 | if (family == AF_UNSPEC) { |
| 332 | fd = socket(AF_INET6, sock_type, 0); |
| 333 | if (fd >= 0) { |
| 334 | family = AF_INET6; |
| 335 | goto done; |
| 336 | } |
| 337 | family = AF_INET; |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 338 | } |
Denis Vlasenko | 5c51a7c | 2007-06-05 20:08:11 +0000 | [diff] [blame] | 339 | #endif |
| 340 | fd = xsocket(family, sock_type, 0); |
| 341 | len = sizeof(struct sockaddr_in); |
| 342 | #if ENABLE_FEATURE_IPV6 |
| 343 | if (family == AF_INET6) { |
| 344 | done: |
| 345 | len = sizeof(struct sockaddr_in6); |
| 346 | } |
| 347 | #endif |
Denys Vlasenko | b1278a3 | 2009-11-24 16:03:47 +0100 | [diff] [blame] | 348 | lsa = xzalloc(LSA_LEN_SIZE + len); |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 349 | lsa->len = len; |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 350 | lsa->u.sa.sa_family = family; |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 351 | *lsap = lsa; |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 352 | return fd; |
| 353 | } |
| 354 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 355 | int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap) |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 356 | { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 357 | return xsocket_type(lsap, IF_FEATURE_IPV6(AF_UNSPEC,) SOCK_STREAM); |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type) |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 361 | { |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 362 | int fd; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 363 | len_and_sockaddr *lsa; |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 364 | |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame] | 365 | if (bindaddr && bindaddr[0]) { |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 366 | lsa = xdotted2sockaddr(bindaddr, port); |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 367 | /* user specified bind addr dictates family */ |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 368 | fd = xsocket(lsa->u.sa.sa_family, sock_type, 0); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 369 | } else { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 370 | fd = xsocket_type(&lsa, IF_FEATURE_IPV6(AF_UNSPEC,) sock_type); |
Denis Vlasenko | 2418288 | 2007-01-13 21:05:38 +0000 | [diff] [blame] | 371 | set_nport(lsa, htons(port)); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 372 | } |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 373 | setsockopt_reuseaddr(fd); |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 374 | xbind(fd, &lsa->u.sa, lsa->len); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 375 | free(lsa); |
| 376 | return fd; |
| 377 | } |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 378 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 379 | int FAST_FUNC create_and_bind_stream_or_die(const char *bindaddr, int port) |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 380 | { |
| 381 | return create_and_bind_or_die(bindaddr, port, SOCK_STREAM); |
| 382 | } |
| 383 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 384 | int FAST_FUNC create_and_bind_dgram_or_die(const char *bindaddr, int port) |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 385 | { |
| 386 | return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM); |
| 387 | } |
| 388 | |
| 389 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 390 | int FAST_FUNC create_and_connect_stream_or_die(const char *peer, int port) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 391 | { |
| 392 | int fd; |
| 393 | len_and_sockaddr *lsa; |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 394 | |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 395 | lsa = xhost2sockaddr(peer, port); |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 396 | fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 397 | setsockopt_reuseaddr(fd); |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 398 | xconnect(fd, &lsa->u.sa, lsa->len); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 399 | free(lsa); |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 400 | return fd; |
| 401 | } |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 402 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 403 | int FAST_FUNC xconnect_stream(const len_and_sockaddr *lsa) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 404 | { |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 405 | int fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0); |
| 406 | xconnect(fd, &lsa->u.sa, lsa->len); |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 407 | return fd; |
| 408 | } |
| 409 | |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 410 | /* We hijack this constant to mean something else */ |
| 411 | /* It doesn't hurt because we will add this bit anyway */ |
| 412 | #define IGNORE_PORT NI_NUMERICSERV |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 413 | static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 414 | { |
| 415 | char host[128]; |
| 416 | char serv[16]; |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 417 | int rc; |
| 418 | socklen_t salen; |
| 419 | |
Denis Vlasenko | f6b4685 | 2009-04-25 13:16:53 +0000 | [diff] [blame] | 420 | if (ENABLE_FEATURE_UNIX_LOCAL && sa->sa_family == AF_UNIX) { |
| 421 | struct sockaddr_un *sun = (struct sockaddr_un *)sa; |
| 422 | return xasprintf("local:%.*s", |
| 423 | (int) sizeof(sun->sun_path), |
| 424 | sun->sun_path); |
| 425 | } |
| 426 | |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 427 | salen = LSA_SIZEOF_SA; |
| 428 | #if ENABLE_FEATURE_IPV6 |
| 429 | if (sa->sa_family == AF_INET) |
| 430 | salen = sizeof(struct sockaddr_in); |
| 431 | if (sa->sa_family == AF_INET6) |
| 432 | salen = sizeof(struct sockaddr_in6); |
| 433 | #endif |
| 434 | rc = getnameinfo(sa, salen, |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 435 | host, sizeof(host), |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 436 | /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */ |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 437 | serv, sizeof(serv), |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 438 | /* do not resolve port# into service _name_ */ |
| 439 | flags | NI_NUMERICSERV |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 440 | ); |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 441 | if (rc) |
| 442 | return NULL; |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 443 | if (flags & IGNORE_PORT) |
| 444 | return xstrdup(host); |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 445 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 446 | if (sa->sa_family == AF_INET6) { |
| 447 | if (strchr(host, ':')) /* heh, it's not a resolved hostname */ |
| 448 | return xasprintf("[%s]:%s", host, serv); |
| 449 | /*return xasprintf("%s:%s", host, serv);*/ |
| 450 | /* - fall through instead */ |
| 451 | } |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 452 | #endif |
| 453 | /* For now we don't support anything else, so it has to be INET */ |
| 454 | /*if (sa->sa_family == AF_INET)*/ |
| 455 | return xasprintf("%s:%s", host, serv); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 456 | /*return xstrdup(host);*/ |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 459 | char* FAST_FUNC xmalloc_sockaddr2host(const struct sockaddr *sa) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 460 | { |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 461 | return sockaddr2str(sa, 0); |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 464 | char* FAST_FUNC xmalloc_sockaddr2host_noport(const struct sockaddr *sa) |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 465 | { |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 466 | return sockaddr2str(sa, IGNORE_PORT); |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 469 | char* FAST_FUNC xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa) |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 470 | { |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 471 | return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 472 | } |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 473 | char* FAST_FUNC xmalloc_sockaddr2dotted(const struct sockaddr *sa) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 474 | { |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 475 | return sockaddr2str(sa, NI_NUMERICHOST); |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 476 | } |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 477 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 478 | char* FAST_FUNC xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa) |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 479 | { |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 480 | return sockaddr2str(sa, NI_NUMERICHOST | IGNORE_PORT); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 481 | } |