1 /* vi: set sw=4 ts=4: */
5 * Connect to host at port using address resolution from getaddrinfo
9 #include <netinet/in.h>
12 void FAST_FUNC setsockopt_reuseaddr(int fd)
14 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &const_int_1, sizeof(const_int_1));
16 int FAST_FUNC setsockopt_broadcast(int fd)
18 return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1));
21 void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
23 if (connect(s, s_addr, addrlen) < 0) {
24 if (ENABLE_FEATURE_CLEAN_UP)
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");
34 /* Return port number for a service.
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
38 unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
40 unsigned port_nr = default_port;
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. */
47 port_nr = bb_strtou(port, NULL, 10);
48 if (errno || port_nr > 65535) {
49 struct servent *tserv = getservbyname(port, protocol);
50 port_nr = default_port;
52 port_nr = ntohs(tserv->s_port);
56 return (uint16_t)port_nr;
60 /* "Old" networking API - only IPv4 */
63 void FAST_FUNC bb_lookup_host(struct sockaddr_in *s_in, const char *host)
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);
74 int FAST_FUNC xconnect_tcp_v4(struct sockaddr_in *s_addr)
76 int s = xsocket(AF_INET, SOCK_STREAM, 0);
77 xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr));
82 /* "New" networking API */
85 int FAST_FUNC get_nport(const struct sockaddr *sa)
87 #if ENABLE_FEATURE_IPV6
88 if (sa->sa_family == AF_INET6) {
89 return ((struct sockaddr_in6*)sa)->sin6_port;
92 if (sa->sa_family == AF_INET) {
93 return ((struct sockaddr_in*)sa)->sin_port;
95 /* What? UNIX socket? IPX?? :) */
99 void FAST_FUNC set_nport(len_and_sockaddr *lsa, unsigned port)
101 #if ENABLE_FEATURE_IPV6
102 if (lsa->u.sa.sa_family == AF_INET6) {
103 lsa->u.sin6.sin6_port = port;
107 if (lsa->u.sa.sa_family == AF_INET) {
108 lsa->u.sin.sin_port = port;
111 /* What? UNIX socket? IPX?? :) */
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
118 /* host: "1.2.3.4[:port]", "www.google.com[:port]"
119 * port: if neither of above specifies port # */
120 static len_and_sockaddr* str2sockaddr(
121 const char *host, int port,
122 USE_FEATURE_IPV6(sa_family_t af,)
126 len_and_sockaddr *r = NULL;
127 struct addrinfo *result = NULL;
128 struct addrinfo *used_res;
129 const char *org_host = host; /* only for error msg */
131 struct addrinfo hint;
133 /* Ugly parsing of host:addr */
134 if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
135 /* Even uglier parsing of [xx]:nn */
137 cp = strchr(host, ']');
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)
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 */
151 if (cp) { /* points to ":" or "]:" */
152 int sz = cp - host + 1;
153 host = safe_strncpy(alloca(sz), host, sz);
154 if (ENABLE_FEATURE_IPV6 && *cp != ':')
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)
166 memset(&hint, 0 , sizeof(hint));
167 #if !ENABLE_FEATURE_IPV6
168 hint.ai_family = AF_INET; /* do not try to find IPv6 */
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;
175 hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
176 rc = getaddrinfo(host, NULL, &hint, &result);
178 bb_error_msg("bad address '%s'", org_host);
179 if (ai_flags & DIE_ON_ERROR)
184 #if ENABLE_FEATURE_PREFER_IPV4_ADDRESS
186 if (used_res->ai_family == AF_INET)
188 used_res = used_res->ai_next;
195 r = xmalloc(offsetof(len_and_sockaddr, u.sa) + used_res->ai_addrlen);
196 r->len = used_res->ai_addrlen;
197 memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen);
198 set_nport(r, htons(port));
200 freeaddrinfo(result);
203 #if !ENABLE_FEATURE_IPV6
204 #define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
207 #if ENABLE_FEATURE_IPV6
208 len_and_sockaddr* FAST_FUNC host_and_af2sockaddr(const char *host, int port, sa_family_t af)
210 return str2sockaddr(host, port, af, 0);
213 len_and_sockaddr* FAST_FUNC xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
215 return str2sockaddr(host, port, af, DIE_ON_ERROR);
219 len_and_sockaddr* FAST_FUNC host2sockaddr(const char *host, int port)
221 return str2sockaddr(host, port, AF_UNSPEC, 0);
224 len_and_sockaddr* FAST_FUNC xhost2sockaddr(const char *host, int port)
226 return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR);
229 len_and_sockaddr* FAST_FUNC xdotted2sockaddr(const char *host, int port)
231 return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
235 int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, USE_FEATURE_IPV6(int family,) int sock_type)
237 SKIP_FEATURE_IPV6(enum { family = AF_INET };)
238 len_and_sockaddr *lsa;
242 #if ENABLE_FEATURE_IPV6
243 if (family == AF_UNSPEC) {
244 fd = socket(AF_INET6, sock_type, 0);
252 fd = xsocket(family, sock_type, 0);
253 len = sizeof(struct sockaddr_in);
254 #if ENABLE_FEATURE_IPV6
255 if (family == AF_INET6) {
257 len = sizeof(struct sockaddr_in6);
260 lsa = xzalloc(offsetof(len_and_sockaddr, u.sa) + len);
262 lsa->u.sa.sa_family = family;
267 int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap)
269 return xsocket_type(lsap, USE_FEATURE_IPV6(AF_UNSPEC,) SOCK_STREAM);
272 static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
275 len_and_sockaddr *lsa;
277 if (bindaddr && bindaddr[0]) {
278 lsa = xdotted2sockaddr(bindaddr, port);
279 /* user specified bind addr dictates family */
280 fd = xsocket(lsa->u.sa.sa_family, sock_type, 0);
282 fd = xsocket_type(&lsa, USE_FEATURE_IPV6(AF_UNSPEC,) sock_type);
283 set_nport(lsa, htons(port));
285 setsockopt_reuseaddr(fd);
286 xbind(fd, &lsa->u.sa, lsa->len);
291 int FAST_FUNC create_and_bind_stream_or_die(const char *bindaddr, int port)
293 return create_and_bind_or_die(bindaddr, port, SOCK_STREAM);
296 int FAST_FUNC create_and_bind_dgram_or_die(const char *bindaddr, int port)
298 return create_and_bind_or_die(bindaddr, port, SOCK_DGRAM);
302 int FAST_FUNC create_and_connect_stream_or_die(const char *peer, int port)
305 len_and_sockaddr *lsa;
307 lsa = xhost2sockaddr(peer, port);
308 fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
309 setsockopt_reuseaddr(fd);
310 xconnect(fd, &lsa->u.sa, lsa->len);
315 int FAST_FUNC xconnect_stream(const len_and_sockaddr *lsa)
317 int fd = xsocket(lsa->u.sa.sa_family, SOCK_STREAM, 0);
318 xconnect(fd, &lsa->u.sa, lsa->len);
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
325 static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags)
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);
339 rc = getnameinfo(sa, salen,
341 /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
343 /* do not resolve port# into service _name_ */
344 flags | NI_NUMERICSERV
348 if (flags & IGNORE_PORT)
349 return xstrdup(host);
350 #if ENABLE_FEATURE_IPV6
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 */
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);
361 /*return xstrdup(host);*/
364 char* FAST_FUNC xmalloc_sockaddr2host(const struct sockaddr *sa)
366 return sockaddr2str(sa, 0);
369 char* FAST_FUNC xmalloc_sockaddr2host_noport(const struct sockaddr *sa)
371 return sockaddr2str(sa, IGNORE_PORT);
374 char* FAST_FUNC xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa)
376 return sockaddr2str(sa, NI_NAMEREQD | IGNORE_PORT);
378 char* FAST_FUNC xmalloc_sockaddr2dotted(const struct sockaddr *sa)
380 return sockaddr2str(sa, NI_NUMERICHOST);
383 char* FAST_FUNC xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa)
385 return sockaddr2str(sa, NI_NUMERICHOST | IGNORE_PORT);