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 | |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 10 | #include <netinet/in.h> |
Denis Vlasenko | e537385 | 2008-12-10 11:12:16 +0000 | [diff] [blame^] | 11 | #include <net/if.h> |
Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
| 13 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 14 | void FAST_FUNC setsockopt_reuseaddr(int fd) |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 15 | { |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 16 | 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] | 17 | } |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 18 | int FAST_FUNC setsockopt_broadcast(int fd) |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 19 | { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 20 | 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] | 21 | } |
Denis Vlasenko | e537385 | 2008-12-10 11:12:16 +0000 | [diff] [blame^] | 22 | int FAST_FUNC setsockopt_bindtodevice(int fd, const char *iface) |
| 23 | { |
| 24 | int r; |
| 25 | struct ifreq ifr; |
| 26 | strncpy_IFNAMSIZ(ifr.ifr_name, iface); |
| 27 | /* Actually, ifr_name is at offset 0, and in practice |
| 28 | * just giving char[IFNAMSIZ] instead of struct ifreq works too. |
| 29 | * But just in case it's not true on some obscure arch... */ |
| 30 | r = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)); |
| 31 | if (r) |
| 32 | bb_perror_msg("can't bind to interface %s", iface); |
| 33 | return r; |
| 34 | } |
| 35 | |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 37 | 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] | 38 | { |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 39 | if (connect(s, s_addr, addrlen) < 0) { |
| 40 | if (ENABLE_FEATURE_CLEAN_UP) |
| 41 | close(s); |
| 42 | if (s_addr->sa_family == AF_INET) |
| 43 | bb_perror_msg_and_die("%s (%s)", |
| 44 | "cannot connect to remote host", |
| 45 | inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr)); |
| 46 | bb_perror_msg_and_die("cannot connect to remote host"); |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 47 | } |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 50 | /* Return port number for a service. |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 51 | * If "port" is a number use it as the port. |
| 52 | * If "port" is a name it is looked up in /etc/services, if it isnt found return |
| 53 | * default_port */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 54 | 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] | 55 | { |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 56 | unsigned port_nr = default_port; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 57 | if (port) { |
| 58 | int old_errno; |
| 59 | |
| 60 | /* Since this is a lib function, we're not allowed to reset errno to 0. |
| 61 | * Doing so could break an app that is deferring checking of errno. */ |
| 62 | old_errno = errno; |
| 63 | port_nr = bb_strtou(port, NULL, 10); |
| 64 | if (errno || port_nr > 65535) { |
| 65 | struct servent *tserv = getservbyname(port, protocol); |
Denis Vlasenko | 2418288 | 2007-01-13 21:05:38 +0000 | [diff] [blame] | 66 | port_nr = default_port; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 67 | if (tserv) |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 68 | port_nr = ntohs(tserv->s_port); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 69 | } |
| 70 | errno = old_errno; |
| 71 | } |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 72 | return (uint16_t)port_nr; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | |
| 76 | /* "Old" networking API - only IPv4 */ |
| 77 | |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 78 | /* |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 79 | void FAST_FUNC bb_lookup_host(struct sockaddr_in *s_in, const char *host) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 80 | { |
| 81 | struct hostent *he; |
| 82 | |
| 83 | memset(s_in, 0, sizeof(struct sockaddr_in)); |
| 84 | s_in->sin_family = AF_INET; |
| 85 | he = xgethostbyname(host); |
| 86 | memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length); |
| 87 | } |
| 88 | |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 89 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 90 | int FAST_FUNC xconnect_tcp_v4(struct sockaddr_in *s_addr) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 91 | { |
| 92 | int s = xsocket(AF_INET, SOCK_STREAM, 0); |
| 93 | xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr)); |
| 94 | return s; |
| 95 | } |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 96 | */ |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 97 | |
| 98 | /* "New" networking API */ |
| 99 | |
| 100 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 101 | int FAST_FUNC get_nport(const struct sockaddr *sa) |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 102 | { |
| 103 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 104 | if (sa->sa_family == AF_INET6) { |
| 105 | return ((struct sockaddr_in6*)sa)->sin6_port; |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 106 | } |
| 107 | #endif |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 108 | if (sa->sa_family == AF_INET) { |
| 109 | return ((struct sockaddr_in*)sa)->sin_port; |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 110 | } |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 111 | /* What? UNIX socket? IPX?? :) */ |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 112 | return -1; |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 115 | void FAST_FUNC set_nport(len_and_sockaddr *lsa, unsigned port) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 116 | { |
| 117 | #if ENABLE_FEATURE_IPV6 |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 118 | if (lsa->u.sa.sa_family == AF_INET6) { |
| 119 | lsa->u.sin6.sin6_port = port; |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 120 | return; |
| 121 | } |
| 122 | #endif |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 123 | if (lsa->u.sa.sa_family == AF_INET) { |
| 124 | lsa->u.sin.sin_port = port; |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 125 | return; |
| 126 | } |
| 127 | /* What? UNIX socket? IPX?? :) */ |
| 128 | } |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 129 | |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 130 | /* We hijack this constant to mean something else */ |
| 131 | /* It doesn't hurt because we will remove this bit anyway */ |
| 132 | #define DIE_ON_ERROR AI_CANONNAME |
| 133 | |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 134 | /* host: "1.2.3.4[:port]", "www.google.com[:port]" |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 135 | * port: if neither of above specifies port # */ |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 136 | static len_and_sockaddr* str2sockaddr( |
| 137 | const char *host, int port, |
| 138 | USE_FEATURE_IPV6(sa_family_t af,) |
| 139 | int ai_flags) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 140 | { |
| 141 | int rc; |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 142 | len_and_sockaddr *r = NULL; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 143 | struct addrinfo *result = NULL; |
Denis Vlasenko | 9d6c469 | 2007-11-14 10:18:33 +0000 | [diff] [blame] | 144 | struct addrinfo *used_res; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 145 | const char *org_host = host; /* only for error msg */ |
| 146 | const char *cp; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 147 | struct addrinfo hint; |
| 148 | |
| 149 | /* Ugly parsing of host:addr */ |
| 150 | if (ENABLE_FEATURE_IPV6 && host[0] == '[') { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 151 | /* Even uglier parsing of [xx]:nn */ |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 152 | host++; |
| 153 | cp = strchr(host, ']'); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 154 | if (!cp || cp[1] != ':') { /* Malformed: must have [xx]:nn */ |
| 155 | bb_error_msg("bad address '%s'", org_host); |
| 156 | if (ai_flags & DIE_ON_ERROR) |
| 157 | xfunc_die(); |
| 158 | return NULL; |
| 159 | } |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 160 | } else { |
| 161 | cp = strrchr(host, ':'); |
| 162 | if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) { |
| 163 | /* There is more than one ':' (e.g. "::1") */ |
| 164 | cp = NULL; /* it's not a port spec */ |
| 165 | } |
| 166 | } |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 167 | if (cp) { /* points to ":" or "]:" */ |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame] | 168 | int sz = cp - host + 1; |
| 169 | host = safe_strncpy(alloca(sz), host, sz); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 170 | if (ENABLE_FEATURE_IPV6 && *cp != ':') |
| 171 | cp++; /* skip ']' */ |
| 172 | cp++; /* skip ':' */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 173 | port = bb_strtou(cp, NULL, 10); |
| 174 | if (errno || (unsigned)port > 0xffff) { |
| 175 | bb_error_msg("bad port spec '%s'", org_host); |
| 176 | if (ai_flags & DIE_ON_ERROR) |
| 177 | xfunc_die(); |
| 178 | return NULL; |
| 179 | } |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | memset(&hint, 0 , sizeof(hint)); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 183 | #if !ENABLE_FEATURE_IPV6 |
| 184 | hint.ai_family = AF_INET; /* do not try to find IPv6 */ |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 185 | #else |
| 186 | hint.ai_family = af; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 187 | #endif |
| 188 | /* Needed. Or else we will get each address thrice (or more) |
| 189 | * for each possible socket type (tcp,udp,raw...): */ |
| 190 | hint.ai_socktype = SOCK_STREAM; |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 191 | hint.ai_flags = ai_flags & ~DIE_ON_ERROR; |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 192 | rc = getaddrinfo(host, NULL, &hint, &result); |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 193 | if (rc || !result) { |
Denis Vlasenko | 5870ad9 | 2007-02-04 02:39:55 +0000 | [diff] [blame] | 194 | bb_error_msg("bad address '%s'", org_host); |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 195 | if (ai_flags & DIE_ON_ERROR) |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 196 | xfunc_die(); |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 197 | goto ret; |
| 198 | } |
Denis Vlasenko | 9d6c469 | 2007-11-14 10:18:33 +0000 | [diff] [blame] | 199 | used_res = result; |
| 200 | #if ENABLE_FEATURE_PREFER_IPV4_ADDRESS |
| 201 | while (1) { |
| 202 | if (used_res->ai_family == AF_INET) |
| 203 | break; |
| 204 | used_res = used_res->ai_next; |
| 205 | if (!used_res) { |
| 206 | used_res = result; |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | #endif |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 211 | r = xmalloc(offsetof(len_and_sockaddr, u.sa) + used_res->ai_addrlen); |
Denis Vlasenko | 9d6c469 | 2007-11-14 10:18:33 +0000 | [diff] [blame] | 212 | r->len = used_res->ai_addrlen; |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 213 | memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen); |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 214 | set_nport(r, htons(port)); |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 215 | ret: |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 216 | freeaddrinfo(result); |
| 217 | return r; |
| 218 | } |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 219 | #if !ENABLE_FEATURE_IPV6 |
| 220 | #define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags) |
| 221 | #endif |
| 222 | |
| 223 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 224 | 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] | 225 | { |
| 226 | return str2sockaddr(host, port, af, 0); |
| 227 | } |
| 228 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 229 | 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] | 230 | { |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 231 | return str2sockaddr(host, port, af, DIE_ON_ERROR); |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 232 | } |
| 233 | #endif |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 234 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 235 | len_and_sockaddr* FAST_FUNC host2sockaddr(const char *host, int port) |
Denis Vlasenko | 5870ad9 | 2007-02-04 02:39:55 +0000 | [diff] [blame] | 236 | { |
| 237 | return str2sockaddr(host, port, AF_UNSPEC, 0); |
| 238 | } |
| 239 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 240 | len_and_sockaddr* FAST_FUNC xhost2sockaddr(const char *host, int port) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 241 | { |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 242 | return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 245 | len_and_sockaddr* FAST_FUNC xdotted2sockaddr(const char *host, int port) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 246 | { |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 247 | return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 250 | #undef xsocket_type |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 251 | int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, USE_FEATURE_IPV6(int family,) int sock_type) |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 252 | { |
Denis Vlasenko | 5c51a7c | 2007-06-05 20:08:11 +0000 | [diff] [blame] | 253 | SKIP_FEATURE_IPV6(enum { family = AF_INET };) |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 254 | len_and_sockaddr *lsa; |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 255 | int fd; |
Denis Vlasenko | 5c51a7c | 2007-06-05 20:08:11 +0000 | [diff] [blame] | 256 | int len; |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 257 | |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 258 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 5c51a7c | 2007-06-05 20:08:11 +0000 | [diff] [blame] | 259 | if (family == AF_UNSPEC) { |
| 260 | fd = socket(AF_INET6, sock_type, 0); |
| 261 | if (fd >= 0) { |
| 262 | family = AF_INET6; |
| 263 | goto done; |
| 264 | } |
| 265 | family = AF_INET; |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 266 | } |
Denis Vlasenko | 5c51a7c | 2007-06-05 20:08:11 +0000 | [diff] [blame] | 267 | #endif |
| 268 | fd = xsocket(family, sock_type, 0); |
| 269 | len = sizeof(struct sockaddr_in); |
| 270 | #if ENABLE_FEATURE_IPV6 |
| 271 | if (family == AF_INET6) { |
| 272 | done: |
| 273 | len = sizeof(struct sockaddr_in6); |
| 274 | } |
| 275 | #endif |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 276 | lsa = xzalloc(offsetof(len_and_sockaddr, u.sa) + len); |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 277 | lsa->len = len; |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 278 | lsa->u.sa.sa_family = family; |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 279 | *lsap = lsa; |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 280 | return fd; |
| 281 | } |
| 282 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 283 | int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap) |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 284 | { |
Denis Vlasenko | 5c51a7c | 2007-06-05 20:08:11 +0000 | [diff] [blame] | 285 | return xsocket_type(lsap, USE_FEATURE_IPV6(AF_UNSPEC,) SOCK_STREAM); |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | 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] | 289 | { |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 290 | int fd; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 291 | len_and_sockaddr *lsa; |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 292 | |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame] | 293 | if (bindaddr && bindaddr[0]) { |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 294 | lsa = xdotted2sockaddr(bindaddr, port); |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 295 | /* user specified bind addr dictates family */ |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 296 | fd = xsocket(lsa->u.sa.sa_family, sock_type, 0); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 297 | } else { |
Denis Vlasenko | 5c51a7c | 2007-06-05 20:08:11 +0000 | [diff] [blame] | 298 | fd = xsocket_type(&lsa, USE_FEATURE_IPV6(AF_UNSPEC,) sock_type); |
Denis Vlasenko | 2418288 | 2007-01-13 21:05:38 +0000 | [diff] [blame] | 299 | set_nport(lsa, htons(port)); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 300 | } |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 301 | setsockopt_reuseaddr(fd); |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 302 | xbind(fd, &lsa->u.sa, lsa->len); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 303 | free(lsa); |
| 304 | return fd; |
| 305 | } |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 306 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 307 | 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] | 308 | { |
| 309 | return create_and_bind_or_die(bindaddr, port, SOCK_STREAM); |
| 310 | } |
| 311 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 312 | 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] | 313 | { |
| 314 | return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM); |
| 315 | } |
| 316 | |
| 317 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 318 | 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] | 319 | { |
| 320 | int fd; |
| 321 | len_and_sockaddr *lsa; |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 322 | |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 323 | lsa = xhost2sockaddr(peer, port); |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 324 | fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 325 | setsockopt_reuseaddr(fd); |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 326 | xconnect(fd, &lsa->u.sa, lsa->len); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 327 | free(lsa); |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 328 | return fd; |
| 329 | } |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 330 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 331 | int FAST_FUNC xconnect_stream(const len_and_sockaddr *lsa) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 332 | { |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 333 | int fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0); |
| 334 | xconnect(fd, &lsa->u.sa, lsa->len); |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 335 | return fd; |
| 336 | } |
| 337 | |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 338 | /* We hijack this constant to mean something else */ |
| 339 | /* It doesn't hurt because we will add this bit anyway */ |
| 340 | #define IGNORE_PORT NI_NUMERICSERV |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 341 | static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 342 | { |
| 343 | char host[128]; |
| 344 | char serv[16]; |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 345 | int rc; |
| 346 | socklen_t salen; |
| 347 | |
| 348 | salen = LSA_SIZEOF_SA; |
| 349 | #if ENABLE_FEATURE_IPV6 |
| 350 | if (sa->sa_family == AF_INET) |
| 351 | salen = sizeof(struct sockaddr_in); |
| 352 | if (sa->sa_family == AF_INET6) |
| 353 | salen = sizeof(struct sockaddr_in6); |
| 354 | #endif |
| 355 | rc = getnameinfo(sa, salen, |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 356 | host, sizeof(host), |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 357 | /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */ |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 358 | serv, sizeof(serv), |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 359 | /* do not resolve port# into service _name_ */ |
| 360 | flags | NI_NUMERICSERV |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 361 | ); |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 362 | if (rc) |
| 363 | return NULL; |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 364 | if (flags & IGNORE_PORT) |
| 365 | return xstrdup(host); |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 366 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 367 | if (sa->sa_family == AF_INET6) { |
| 368 | if (strchr(host, ':')) /* heh, it's not a resolved hostname */ |
| 369 | return xasprintf("[%s]:%s", host, serv); |
| 370 | /*return xasprintf("%s:%s", host, serv);*/ |
| 371 | /* - fall through instead */ |
| 372 | } |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 373 | #endif |
| 374 | /* For now we don't support anything else, so it has to be INET */ |
| 375 | /*if (sa->sa_family == AF_INET)*/ |
| 376 | return xasprintf("%s:%s", host, serv); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 377 | /*return xstrdup(host);*/ |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 380 | char* FAST_FUNC xmalloc_sockaddr2host(const struct sockaddr *sa) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 381 | { |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 382 | return sockaddr2str(sa, 0); |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 385 | char* FAST_FUNC xmalloc_sockaddr2host_noport(const struct sockaddr *sa) |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 386 | { |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 387 | return sockaddr2str(sa, IGNORE_PORT); |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 390 | char* FAST_FUNC xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa) |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 391 | { |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 392 | return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 393 | } |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 394 | char* FAST_FUNC xmalloc_sockaddr2dotted(const struct sockaddr *sa) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 395 | { |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 396 | return sockaddr2str(sa, NI_NUMERICHOST); |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 397 | } |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 398 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 399 | char* FAST_FUNC xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa) |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 400 | { |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 401 | return sockaddr2str(sa, NI_NUMERICHOST | IGNORE_PORT); |
Denis Vlasenko | 448f024 | 2007-01-22 22:43:05 +0000 | [diff] [blame] | 402 | } |