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