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 | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 12 | int setsockopt_reuseaddr(int fd) |
| 13 | { |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame^] | 14 | return 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 | } |
| 16 | int setsockopt_broadcast(int fd) |
| 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 | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 21 | void 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 */ |
| 38 | unsigned bb_lookup_port(const char *port, const char *protocol, unsigned default_port) |
| 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 | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 63 | void bb_lookup_host(struct sockaddr_in *s_in, const char *host) |
| 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 | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 74 | int xconnect_tcp_v4(struct sockaddr_in *s_addr) |
| 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 | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 85 | int get_nport(len_and_sockaddr *lsa) |
| 86 | { |
| 87 | #if ENABLE_FEATURE_IPV6 |
| 88 | if (lsa->sa.sa_family == AF_INET6) { |
| 89 | return lsa->sin6.sin6_port; |
| 90 | } |
| 91 | #endif |
| 92 | if (lsa->sa.sa_family == AF_INET) { |
| 93 | return lsa->sin.sin_port; |
| 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 | |
| 99 | void 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 |
| 102 | if (lsa->sa.sa_family == AF_INET6) { |
| 103 | lsa->sin6.sin6_port = port; |
| 104 | return; |
| 105 | } |
| 106 | #endif |
| 107 | if (lsa->sa.sa_family == AF_INET) { |
| 108 | lsa->sin.sin_port = port; |
| 109 | return; |
| 110 | } |
| 111 | /* What? UNIX socket? IPX?? :) */ |
| 112 | } |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 113 | |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 114 | /* host: "1.2.3.4[:port]", "www.google.com[:port]" |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame] | 115 | * port: if neither of above specifies port # |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 116 | */ |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame] | 117 | static len_and_sockaddr* str2sockaddr(const char *host, int port, int ai_flags) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 118 | { |
| 119 | int rc; |
| 120 | len_and_sockaddr *r; // = NULL; |
| 121 | struct addrinfo *result = NULL; |
| 122 | const char *org_host = host; /* only for error msg */ |
| 123 | const char *cp; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 124 | struct addrinfo hint; |
| 125 | |
| 126 | /* Ugly parsing of host:addr */ |
| 127 | if (ENABLE_FEATURE_IPV6 && host[0] == '[') { |
| 128 | host++; |
| 129 | cp = strchr(host, ']'); |
| 130 | if (!cp || cp[1] != ':') /* Malformed: must have [xx]:nn */ |
| 131 | bb_error_msg_and_die("bad address '%s'", org_host); |
| 132 | //return r; /* return NULL */ |
| 133 | } else { |
| 134 | cp = strrchr(host, ':'); |
| 135 | if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) { |
| 136 | /* There is more than one ':' (e.g. "::1") */ |
| 137 | cp = NULL; /* it's not a port spec */ |
| 138 | } |
| 139 | } |
| 140 | if (cp) { |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame] | 141 | int sz = cp - host + 1; |
| 142 | host = safe_strncpy(alloca(sz), host, sz); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 143 | if (ENABLE_FEATURE_IPV6 && *cp != ':') |
| 144 | cp++; /* skip ']' */ |
| 145 | cp++; /* skip ':' */ |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 146 | port = xatou16(cp); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | memset(&hint, 0 , sizeof(hint)); |
| 150 | /* hint.ai_family = AF_UNSPEC; - zero anyway */ |
| 151 | #if !ENABLE_FEATURE_IPV6 |
| 152 | hint.ai_family = AF_INET; /* do not try to find IPv6 */ |
| 153 | #endif |
| 154 | /* Needed. Or else we will get each address thrice (or more) |
| 155 | * for each possible socket type (tcp,udp,raw...): */ |
| 156 | hint.ai_socktype = SOCK_STREAM; |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 157 | hint.ai_flags = ai_flags; |
| 158 | rc = getaddrinfo(host, NULL, &hint, &result); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 159 | if (rc || !result) |
| 160 | bb_error_msg_and_die("bad address '%s'", org_host); |
| 161 | r = xmalloc(offsetof(len_and_sockaddr, sa) + result->ai_addrlen); |
| 162 | r->len = result->ai_addrlen; |
| 163 | memcpy(&r->sa, result->ai_addr, result->ai_addrlen); |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 164 | set_nport(r, htons(port)); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 165 | freeaddrinfo(result); |
| 166 | return r; |
| 167 | } |
| 168 | |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame] | 169 | len_and_sockaddr* host2sockaddr(const char *host, int port) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 170 | { |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame] | 171 | return str2sockaddr(host, port, 0); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame] | 174 | static len_and_sockaddr* dotted2sockaddr(const char *host, int port) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 175 | { |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame] | 176 | return str2sockaddr(host, port, NI_NUMERICHOST); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 179 | int xsocket_stream(len_and_sockaddr **lsap) |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 180 | { |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 181 | len_and_sockaddr *lsa; |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 182 | int fd; |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 183 | int len = sizeof(struct sockaddr_in); |
| 184 | int family = AF_INET; |
| 185 | |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 186 | #if ENABLE_FEATURE_IPV6 |
| 187 | fd = socket(AF_INET6, SOCK_STREAM, 0); |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 188 | if (fd >= 0) { |
| 189 | len = sizeof(struct sockaddr_in6); |
| 190 | family = AF_INET6; |
| 191 | } else |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 192 | #endif |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 193 | { |
| 194 | fd = xsocket(AF_INET, SOCK_STREAM, 0); |
| 195 | } |
| 196 | lsa = xzalloc(offsetof(len_and_sockaddr, sa) + len); |
| 197 | lsa->len = len; |
| 198 | lsa->sa.sa_family = family; |
| 199 | *lsap = lsa; |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 200 | return fd; |
| 201 | } |
| 202 | |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 203 | int create_and_bind_stream_or_die(const char *bindaddr, int port) |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 204 | { |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 205 | int fd; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 206 | len_and_sockaddr *lsa; |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 207 | |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame] | 208 | if (bindaddr && bindaddr[0]) { |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 209 | lsa = dotted2sockaddr(bindaddr, port); |
| 210 | /* currently NULL check is in str2sockaddr */ |
| 211 | //if (!lsa) |
| 212 | // bb_error_msg_and_die("bad address '%s'", bindaddr); |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 213 | /* user specified bind addr dictates family */ |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 214 | fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0); |
| 215 | } else { |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 216 | fd = xsocket_stream(&lsa); |
Denis Vlasenko | 2418288 | 2007-01-13 21:05:38 +0000 | [diff] [blame] | 217 | set_nport(lsa, htons(port)); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 218 | } |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 219 | setsockopt_reuseaddr(fd); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 220 | xbind(fd, &lsa->sa, lsa->len); |
| 221 | free(lsa); |
| 222 | return fd; |
| 223 | } |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 224 | |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 225 | int create_and_connect_stream_or_die(const char *peer, int port) |
| 226 | { |
| 227 | int fd; |
| 228 | len_and_sockaddr *lsa; |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 229 | |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 230 | lsa = host2sockaddr(peer, port); |
| 231 | /* currently NULL check is in str2sockaddr */ |
| 232 | //if (!lsa) |
| 233 | // bb_error_msg_and_die("bad address '%s'", peer); |
| 234 | fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0); |
| 235 | setsockopt_reuseaddr(fd); |
| 236 | xconnect(fd, &lsa->sa, lsa->len); |
| 237 | free(lsa); |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 238 | return fd; |
| 239 | } |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 240 | |
| 241 | int xconnect_stream(const len_and_sockaddr *lsa) |
| 242 | { |
| 243 | int fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0); |
| 244 | xconnect(fd, &lsa->sa, lsa->len); |
| 245 | return fd; |
| 246 | } |
| 247 | |
| 248 | static char* sockaddr2str(const struct sockaddr *sa, socklen_t salen, int flags) |
| 249 | { |
| 250 | char host[128]; |
| 251 | char serv[16]; |
| 252 | int rc = getnameinfo(sa, salen, |
| 253 | host, sizeof(host), |
| 254 | serv, sizeof(serv), |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 255 | /* do not resolve port# into service _name_ */ |
| 256 | flags | NI_NUMERICSERV |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 257 | ); |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 258 | if (rc) |
| 259 | return NULL; |
| 260 | #if ENABLE_FEATURE_IPV6 |
| 261 | if (sa->sa_family == AF_INET6) |
| 262 | return xasprintf("[%s]:%s", host, serv); |
| 263 | #endif |
| 264 | /* For now we don't support anything else, so it has to be INET */ |
| 265 | /*if (sa->sa_family == AF_INET)*/ |
| 266 | return xasprintf("%s:%s", host, serv); |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen) |
| 270 | { |
| 271 | return sockaddr2str(sa, salen, 0); |
| 272 | } |
| 273 | |
| 274 | char* xmalloc_sockaddr2dotted(const struct sockaddr *sa, socklen_t salen) |
| 275 | { |
| 276 | return sockaddr2str(sa, salen, NI_NUMERICHOST); |
| 277 | } |