ping6: stop using xgethostbyname2, remove it from libbb.
[platform/upstream/busybox.git] / libbb / xconnect.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Connect to host at port using address resolution from getaddrinfo
6  *
7  */
8
9 #include <netinet/in.h>
10 #include "libbb.h"
11
12 int setsockopt_reuseaddr(int fd)
13 {
14         return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &const_int_1, sizeof(const_int_1));
15 }
16 int setsockopt_broadcast(int fd)
17 {
18         return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1));
19 }
20
21 void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
22 {
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");
31         }
32 }
33
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
37  * default_port */
38 unsigned bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
39 {
40         unsigned port_nr = default_port;
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);
50                         port_nr = default_port;
51                         if (tserv)
52                                 port_nr = ntohs(tserv->s_port);
53                 }
54                 errno = old_errno;
55         }
56         return (uint16_t)port_nr;
57 }
58
59
60 /* "Old" networking API - only IPv4 */
61
62 /*
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
73
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 }
80 */
81
82 /* "New" networking API */
83
84
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         }
95         /* What? UNIX socket? IPX?? :) */
96         return -1;
97 }
98
99 void set_nport(len_and_sockaddr *lsa, unsigned port)
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 }
113
114 /* host: "1.2.3.4[:port]", "www.google.com[:port]"
115  * port: if neither of above specifies port #
116  */
117 static len_and_sockaddr* str2sockaddr(
118                 const char *host, int port,
119 USE_FEATURE_IPV6(sa_family_t af,)
120                 int ai_flags)
121 {
122         int rc;
123         len_and_sockaddr *r; // = NULL;
124         struct addrinfo *result = NULL;
125         const char *org_host = host; /* only for error msg */
126         const char *cp;
127         struct addrinfo hint;
128
129         /* Ugly parsing of host:addr */
130         if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
131                 host++;
132                 cp = strchr(host, ']');
133                 if (!cp || cp[1] != ':') /* Malformed: must have [xx]:nn */
134                         bb_error_msg_and_die("bad address '%s'", org_host);
135                         //return r; /* return NULL */
136         } else {
137                 cp = strrchr(host, ':');
138                 if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
139                         /* There is more than one ':' (e.g. "::1") */
140                         cp = NULL; /* it's not a port spec */
141                 }
142         }
143         if (cp) {
144                 int sz = cp - host + 1;
145                 host = safe_strncpy(alloca(sz), host, sz);
146                 if (ENABLE_FEATURE_IPV6 && *cp != ':')
147                         cp++; /* skip ']' */
148                 cp++; /* skip ':' */
149                 port = xatou16(cp);
150         }
151
152         memset(&hint, 0 , sizeof(hint));
153 #if !ENABLE_FEATURE_IPV6
154         hint.ai_family = AF_INET; /* do not try to find IPv6 */
155 #else
156         hint.ai_family = af;
157 #endif
158         /* Needed. Or else we will get each address thrice (or more)
159          * for each possible socket type (tcp,udp,raw...): */
160         hint.ai_socktype = SOCK_STREAM;
161         hint.ai_flags = ai_flags;
162         rc = getaddrinfo(host, NULL, &hint, &result);
163         if (rc || !result)
164                 bb_error_msg_and_die("bad address '%s'", org_host);
165         r = xmalloc(offsetof(len_and_sockaddr, sa) + result->ai_addrlen);
166         r->len = result->ai_addrlen;
167         memcpy(&r->sa, result->ai_addr, result->ai_addrlen);
168         set_nport(r, htons(port));
169         freeaddrinfo(result);
170         return r;
171 }
172 #if !ENABLE_FEATURE_IPV6
173 #define str2sockaddr(host, port, af, ai_flags) str2sockaddr(host, port, ai_flags)
174 #endif
175
176 #if ENABLE_FEATURE_IPV6
177 len_and_sockaddr* host_and_af2sockaddr(const char *host, int port, sa_family_t af)
178 {
179         return str2sockaddr(host, port, af, 0);
180 }
181 #endif
182
183 len_and_sockaddr* host2sockaddr(const char *host, int port)
184 {
185         return str2sockaddr(host, port, AF_UNSPEC, 0);
186 }
187
188 static len_and_sockaddr* dotted2sockaddr(const char *host, int port)
189 {
190         return str2sockaddr(host, port, AF_UNSPEC, NI_NUMERICHOST);
191 }
192
193 int xsocket_stream(len_and_sockaddr **lsap)
194 {
195         len_and_sockaddr *lsa;
196         int fd;
197         int len = sizeof(struct sockaddr_in);
198         int family = AF_INET;
199
200 #if ENABLE_FEATURE_IPV6
201         fd = socket(AF_INET6, SOCK_STREAM, 0);
202         if (fd >= 0) {
203                 len = sizeof(struct sockaddr_in6);
204                 family = AF_INET6;
205         } else
206 #endif
207         {
208                 fd = xsocket(AF_INET, SOCK_STREAM, 0);
209         }
210         lsa = xzalloc(offsetof(len_and_sockaddr, sa) + len);
211         lsa->len = len;
212         lsa->sa.sa_family = family;
213         *lsap = lsa;
214         return fd;
215 }
216
217 int create_and_bind_stream_or_die(const char *bindaddr, int port)
218 {
219         int fd;
220         len_and_sockaddr *lsa;
221
222         if (bindaddr && bindaddr[0]) {
223                 lsa = dotted2sockaddr(bindaddr, port);
224                 /* currently NULL check is in str2sockaddr */
225                 //if (!lsa)
226                 //      bb_error_msg_and_die("bad address '%s'", bindaddr);
227                 /* user specified bind addr dictates family */
228                 fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
229         } else {
230                 fd = xsocket_stream(&lsa);
231                 set_nport(lsa, htons(port));
232         }
233         setsockopt_reuseaddr(fd);
234         xbind(fd, &lsa->sa, lsa->len);
235         free(lsa);
236         return fd;
237 }
238
239 int create_and_connect_stream_or_die(const char *peer, int port)
240 {
241         int fd;
242         len_and_sockaddr *lsa;
243
244         lsa = host2sockaddr(peer, port);
245         /* currently NULL check is in str2sockaddr */
246         //if (!lsa)
247         //      bb_error_msg_and_die("bad address '%s'", peer);
248         fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
249         setsockopt_reuseaddr(fd);
250         xconnect(fd, &lsa->sa, lsa->len);
251         free(lsa);
252         return fd;
253 }
254
255 int xconnect_stream(const len_and_sockaddr *lsa)
256 {
257         int fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
258         xconnect(fd, &lsa->sa, lsa->len);
259         return fd;
260 }
261
262 /* We hijack this constant to mean something else */
263 /* It doesn't hurt because we will add this bit anyway */
264 #define IGNORE_PORT NI_NUMERICSERV
265 static char* sockaddr2str(const struct sockaddr *sa, socklen_t salen, int flags)
266 {
267         char host[128];
268         char serv[16];
269         int rc = getnameinfo(sa, salen,
270                         host, sizeof(host),
271         /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
272                         serv, sizeof(serv),
273                         /* do not resolve port# into service _name_ */
274                         flags | NI_NUMERICSERV
275         );
276         if (rc)
277                 return NULL;
278         if (flags & IGNORE_PORT)
279                 return xstrdup(host);
280 #if ENABLE_FEATURE_IPV6
281         if (sa->sa_family == AF_INET6) {
282                 if (strchr(host, ':')) /* heh, it's not a resolved hostname */
283                         return xasprintf("[%s]:%s", host, serv);
284                 /*return xasprintf("%s:%s", host, serv);*/
285                 /* - fall through instead */
286         }
287 #endif
288         /* For now we don't support anything else, so it has to be INET */
289         /*if (sa->sa_family == AF_INET)*/
290                 return xasprintf("%s:%s", host, serv);
291         /*return xstrdup(host);*/
292 }
293
294 char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen)
295 {
296         return sockaddr2str(sa, salen, 0);
297 }
298
299 char* xmalloc_sockaddr2host_noport(const struct sockaddr *sa, socklen_t salen)
300 {
301         return sockaddr2str(sa, salen, IGNORE_PORT);
302 }
303
304 char* xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa, socklen_t salen)
305 {
306         return sockaddr2str(sa, salen, NI_NAMEREQD | IGNORE_PORT);
307 }
308 char* xmalloc_sockaddr2dotted(const struct sockaddr *sa, socklen_t salen)
309 {
310         return sockaddr2str(sa, salen, NI_NUMERICHOST);
311 }
312
313 char* xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa, socklen_t salen)
314 {
315         return sockaddr2str(sa, salen, NI_NUMERICHOST | IGNORE_PORT);
316 }