1 /* vi: set sw=4 ts=4: */
5 * Connect to host at port using address resolution from getaddrinfo
9 #include <netinet/in.h>
12 int setsockopt_reuseaddr(int fd)
14 return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &const_int_1, sizeof(const_int_1));
16 int setsockopt_broadcast(int fd)
18 return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1));
21 void 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 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 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 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 get_nport(len_and_sockaddr *lsa)
87 #if ENABLE_FEATURE_IPV6
88 if (lsa->sa.sa_family == AF_INET6) {
89 return lsa->sin6.sin6_port;
92 if (lsa->sa.sa_family == AF_INET) {
93 return lsa->sin.sin_port;
95 /* What? UNIX socket? IPX?? :) */
99 void set_nport(len_and_sockaddr *lsa, unsigned port)
101 #if ENABLE_FEATURE_IPV6
102 if (lsa->sa.sa_family == AF_INET6) {
103 lsa->sin6.sin6_port = port;
107 if (lsa->sa.sa_family == AF_INET) {
108 lsa->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 const char *org_host = host; /* only for error msg */
130 struct addrinfo hint;
132 /* Ugly parsing of host:addr */
133 if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
135 cp = strchr(host, ']');
136 if (!cp || cp[1] != ':') /* Malformed: must have [xx]:nn */
137 bb_error_msg_and_die("bad address '%s'", org_host);
138 //return r; /* return NULL */
140 cp = strrchr(host, ':');
141 if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
142 /* There is more than one ':' (e.g. "::1") */
143 cp = NULL; /* it's not a port spec */
147 int sz = cp - host + 1;
148 host = safe_strncpy(alloca(sz), host, sz);
149 if (ENABLE_FEATURE_IPV6 && *cp != ':')
155 memset(&hint, 0 , sizeof(hint));
156 #if !ENABLE_FEATURE_IPV6
157 hint.ai_family = AF_INET; /* do not try to find IPv6 */
161 /* Needed. Or else we will get each address thrice (or more)
162 * for each possible socket type (tcp,udp,raw...): */
163 hint.ai_socktype = SOCK_STREAM;
164 hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
165 rc = getaddrinfo(host, NULL, &hint, &result);
167 if (ai_flags & DIE_ON_ERROR)
168 bb_error_msg_and_die("bad address '%s'", org_host);
171 r = xmalloc(offsetof(len_and_sockaddr, sa) + result->ai_addrlen);
172 r->len = result->ai_addrlen;
173 memcpy(&r->sa, result->ai_addr, result->ai_addrlen);
174 set_nport(r, htons(port));
176 freeaddrinfo(result);
179 #if !ENABLE_FEATURE_IPV6
180 #define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
183 #if ENABLE_FEATURE_IPV6
184 len_and_sockaddr* xhost_and_af2sockaddr(const char *host, int port, sa_family_t af)
186 return str2sockaddr(host, port, af, DIE_ON_ERROR);
190 len_and_sockaddr* xhost2sockaddr(const char *host, int port)
192 return str2sockaddr(host, port, AF_UNSPEC, DIE_ON_ERROR);
195 static len_and_sockaddr* xdotted2sockaddr(const char *host, int port)
197 return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
200 int xsocket_stream(len_and_sockaddr **lsap)
202 len_and_sockaddr *lsa;
204 int len = sizeof(struct sockaddr_in);
205 int family = AF_INET;
207 #if ENABLE_FEATURE_IPV6
208 fd = socket(AF_INET6, SOCK_STREAM, 0);
210 len = sizeof(struct sockaddr_in6);
215 fd = xsocket(AF_INET, SOCK_STREAM, 0);
217 lsa = xzalloc(offsetof(len_and_sockaddr, sa) + len);
219 lsa->sa.sa_family = family;
224 int create_and_bind_stream_or_die(const char *bindaddr, int port)
227 len_and_sockaddr *lsa;
229 if (bindaddr && bindaddr[0]) {
230 lsa = xdotted2sockaddr(bindaddr, port);
231 /* user specified bind addr dictates family */
232 fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
234 fd = xsocket_stream(&lsa);
235 set_nport(lsa, htons(port));
237 setsockopt_reuseaddr(fd);
238 xbind(fd, &lsa->sa, lsa->len);
243 int create_and_connect_stream_or_die(const char *peer, int port)
246 len_and_sockaddr *lsa;
248 lsa = xhost2sockaddr(peer, port);
249 fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
250 setsockopt_reuseaddr(fd);
251 xconnect(fd, &lsa->sa, lsa->len);
256 int xconnect_stream(const len_and_sockaddr *lsa)
258 int fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
259 xconnect(fd, &lsa->sa, lsa->len);
263 /* We hijack this constant to mean something else */
264 /* It doesn't hurt because we will add this bit anyway */
265 #define IGNORE_PORT NI_NUMERICSERV
266 static char* sockaddr2str(const struct sockaddr *sa, socklen_t salen, int flags)
270 int rc = getnameinfo(sa, salen,
272 /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
274 /* do not resolve port# into service _name_ */
275 flags | NI_NUMERICSERV
279 if (flags & IGNORE_PORT)
280 return xstrdup(host);
281 #if ENABLE_FEATURE_IPV6
282 if (sa->sa_family == AF_INET6) {
283 if (strchr(host, ':')) /* heh, it's not a resolved hostname */
284 return xasprintf("[%s]:%s", host, serv);
285 /*return xasprintf("%s:%s", host, serv);*/
286 /* - fall through instead */
289 /* For now we don't support anything else, so it has to be INET */
290 /*if (sa->sa_family == AF_INET)*/
291 return xasprintf("%s:%s", host, serv);
292 /*return xstrdup(host);*/
295 char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen)
297 return sockaddr2str(sa, salen, 0);
301 char* xmalloc_sockaddr2host_noport(const struct sockaddr *sa, socklen_t salen)
303 return sockaddr2str(sa, salen, IGNORE_PORT);
307 char* xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa, socklen_t salen)
309 return sockaddr2str(sa, salen, NI_NAMEREQD | IGNORE_PORT);
311 char* xmalloc_sockaddr2dotted(const struct sockaddr *sa, socklen_t salen)
313 return sockaddr2str(sa, salen, NI_NUMERICHOST);
316 char* xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa, socklen_t salen)
318 return sockaddr2str(sa, salen, NI_NUMERICHOST | IGNORE_PORT);