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 | static const int one = 1; |
| 13 | int setsockopt_reuseaddr(int fd) |
| 14 | { |
| 15 | return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); |
| 16 | } |
| 17 | int setsockopt_broadcast(int fd) |
| 18 | { |
| 19 | return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one)); |
| 20 | } |
| 21 | |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 22 | void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen) |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 23 | { |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 24 | if (connect(s, s_addr, addrlen) < 0) { |
| 25 | if (ENABLE_FEATURE_CLEAN_UP) |
| 26 | close(s); |
| 27 | if (s_addr->sa_family == AF_INET) |
| 28 | bb_perror_msg_and_die("%s (%s)", |
| 29 | "cannot connect to remote host", |
| 30 | inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr)); |
| 31 | bb_perror_msg_and_die("cannot connect to remote host"); |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 32 | } |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 35 | /* Return network byte ordered port number for a service. |
| 36 | * If "port" is a number use it as the port. |
| 37 | * If "port" is a name it is looked up in /etc/services, if it isnt found return |
| 38 | * default_port */ |
| 39 | unsigned bb_lookup_port(const char *port, const char *protocol, unsigned default_port) |
| 40 | { |
| 41 | unsigned port_nr = htons(default_port); |
| 42 | if (port) { |
| 43 | int old_errno; |
| 44 | |
| 45 | /* Since this is a lib function, we're not allowed to reset errno to 0. |
| 46 | * Doing so could break an app that is deferring checking of errno. */ |
| 47 | old_errno = errno; |
| 48 | port_nr = bb_strtou(port, NULL, 10); |
| 49 | if (errno || port_nr > 65535) { |
| 50 | struct servent *tserv = getservbyname(port, protocol); |
| 51 | if (tserv) |
| 52 | port_nr = tserv->s_port; |
| 53 | } else { |
| 54 | port_nr = htons(port_nr); |
| 55 | } |
| 56 | errno = old_errno; |
| 57 | } |
| 58 | return port_nr; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /* "Old" networking API - only IPv4 */ |
| 63 | |
| 64 | |
| 65 | void bb_lookup_host(struct sockaddr_in *s_in, const char *host) |
| 66 | { |
| 67 | struct hostent *he; |
| 68 | |
| 69 | memset(s_in, 0, sizeof(struct sockaddr_in)); |
| 70 | s_in->sin_family = AF_INET; |
| 71 | he = xgethostbyname(host); |
| 72 | memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length); |
| 73 | } |
| 74 | |
| 75 | int xconnect_tcp_v4(struct sockaddr_in *s_addr) |
| 76 | { |
| 77 | int s = xsocket(AF_INET, SOCK_STREAM, 0); |
| 78 | xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr)); |
| 79 | return s; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /* "New" networking API */ |
| 84 | |
| 85 | |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 86 | //extern int xsocket_stream_ip4or6(sa_family_t *fp); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 87 | //extern len_and_sockaddr* dotted2sockaddr(const char *dotted, int def_port); |
| 88 | |
| 89 | /* peer: "1.2.3.4[:port]", "www.google.com[:port]" |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame^] | 90 | * port: if neither of above specifies port # |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 91 | */ |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame^] | 92 | 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] | 93 | { |
| 94 | int rc; |
| 95 | len_and_sockaddr *r; // = NULL; |
| 96 | struct addrinfo *result = NULL; |
| 97 | const char *org_host = host; /* only for error msg */ |
| 98 | const char *cp; |
| 99 | char service[sizeof(int)*3 + 1]; |
| 100 | struct addrinfo hint; |
| 101 | |
| 102 | /* Ugly parsing of host:addr */ |
| 103 | if (ENABLE_FEATURE_IPV6 && host[0] == '[') { |
| 104 | host++; |
| 105 | cp = strchr(host, ']'); |
| 106 | if (!cp || cp[1] != ':') /* Malformed: must have [xx]:nn */ |
| 107 | bb_error_msg_and_die("bad address '%s'", org_host); |
| 108 | //return r; /* return NULL */ |
| 109 | } else { |
| 110 | cp = strrchr(host, ':'); |
| 111 | if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) { |
| 112 | /* There is more than one ':' (e.g. "::1") */ |
| 113 | cp = NULL; /* it's not a port spec */ |
| 114 | } |
| 115 | } |
| 116 | if (cp) { |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame^] | 117 | int sz = cp - host + 1; |
| 118 | host = safe_strncpy(alloca(sz), host, sz); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 119 | if (ENABLE_FEATURE_IPV6 && *cp != ':') |
| 120 | cp++; /* skip ']' */ |
| 121 | cp++; /* skip ':' */ |
| 122 | } else { |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame^] | 123 | utoa_to_buf(port, service, sizeof(service)); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 124 | cp = service; |
| 125 | } |
| 126 | |
| 127 | memset(&hint, 0 , sizeof(hint)); |
| 128 | /* hint.ai_family = AF_UNSPEC; - zero anyway */ |
| 129 | #if !ENABLE_FEATURE_IPV6 |
| 130 | hint.ai_family = AF_INET; /* do not try to find IPv6 */ |
| 131 | #endif |
| 132 | /* Needed. Or else we will get each address thrice (or more) |
| 133 | * for each possible socket type (tcp,udp,raw...): */ |
| 134 | hint.ai_socktype = SOCK_STREAM; |
| 135 | hint.ai_flags = ai_flags | AI_NUMERICSERV; |
| 136 | rc = getaddrinfo(host, cp, &hint, &result); |
| 137 | if (rc || !result) |
| 138 | bb_error_msg_and_die("bad address '%s'", org_host); |
| 139 | r = xmalloc(offsetof(len_and_sockaddr, sa) + result->ai_addrlen); |
| 140 | r->len = result->ai_addrlen; |
| 141 | memcpy(&r->sa, result->ai_addr, result->ai_addrlen); |
| 142 | freeaddrinfo(result); |
| 143 | return r; |
| 144 | } |
| 145 | |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame^] | 146 | len_and_sockaddr* host2sockaddr(const char *host, int port) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 147 | { |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame^] | 148 | return str2sockaddr(host, port, 0); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame^] | 151 | static len_and_sockaddr* dotted2sockaddr(const char *host, int port) |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 152 | { |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame^] | 153 | return str2sockaddr(host, port, NI_NUMERICHOST); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static int xsocket_stream_ip4or6(len_and_sockaddr *lsa) |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 157 | { |
| 158 | int fd; |
| 159 | #if ENABLE_FEATURE_IPV6 |
| 160 | fd = socket(AF_INET6, SOCK_STREAM, 0); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 161 | lsa->sa.sa_family = AF_INET6; |
| 162 | lsa->len = sizeof(struct sockaddr_in6); |
| 163 | if (fd >= 0) |
| 164 | return fd; |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 165 | #endif |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 166 | fd = xsocket(AF_INET, SOCK_STREAM, 0); |
| 167 | lsa->sa.sa_family = AF_INET; |
| 168 | lsa->len = sizeof(struct sockaddr_in); |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 169 | return fd; |
| 170 | } |
| 171 | |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 172 | int create_and_bind_stream_or_die(const char *bindaddr, int port) |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 173 | { |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 174 | int fd; |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 175 | len_and_sockaddr *lsa; |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 176 | |
Denis Vlasenko | bbe5146 | 2007-01-10 20:19:56 +0000 | [diff] [blame^] | 177 | if (bindaddr && bindaddr[0]) { |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 178 | lsa = dotted2sockaddr(bindaddr, port); |
| 179 | /* currently NULL check is in str2sockaddr */ |
| 180 | //if (!lsa) |
| 181 | // bb_error_msg_and_die("bad address '%s'", bindaddr); |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 182 | /* user specified bind addr dictates family */ |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 183 | fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0); |
| 184 | } else { |
| 185 | lsa = xzalloc(offsetof(len_and_sockaddr, sa) + |
| 186 | USE_FEATURE_IPV6(sizeof(struct sockaddr_in6)) |
| 187 | SKIP_FEATURE_IPV6(sizeof(struct sockaddr_in)) |
| 188 | ); |
| 189 | fd = xsocket_stream_ip4or6(lsa); |
| 190 | } |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 191 | setsockopt_reuseaddr(fd); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 192 | xbind(fd, &lsa->sa, lsa->len); |
| 193 | free(lsa); |
| 194 | return fd; |
| 195 | } |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 196 | |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 197 | int create_and_connect_stream_or_die(const char *peer, int port) |
| 198 | { |
| 199 | int fd; |
| 200 | len_and_sockaddr *lsa; |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 201 | |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 202 | lsa = host2sockaddr(peer, port); |
| 203 | /* currently NULL check is in str2sockaddr */ |
| 204 | //if (!lsa) |
| 205 | // bb_error_msg_and_die("bad address '%s'", peer); |
| 206 | fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0); |
| 207 | setsockopt_reuseaddr(fd); |
| 208 | xconnect(fd, &lsa->sa, lsa->len); |
| 209 | free(lsa); |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 210 | return fd; |
| 211 | } |