comment out unused old networking API parts
[oweals/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 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
22 void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
23 {
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");
32         }
33 }
34
35 /* Return 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 = 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                         port_nr = default_port;
52                         if (tserv)
53                                 port_nr = ntohs(tserv->s_port);
54                 }
55                 errno = old_errno;
56         }
57         return (uint16_t)port_nr;
58 }
59
60
61 /* "Old" networking API - only IPv4 */
62
63 /*
64 void bb_lookup_host(struct sockaddr_in *s_in, const char *host)
65 {
66         struct hostent *he;
67
68         memset(s_in, 0, sizeof(struct sockaddr_in));
69         s_in->sin_family = AF_INET;
70         he = xgethostbyname(host);
71         memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
72 }
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
86 int get_nport(len_and_sockaddr *lsa)
87 {
88 #if ENABLE_FEATURE_IPV6
89         if (lsa->sa.sa_family == AF_INET6) {
90                 return lsa->sin6.sin6_port;
91         }
92 #endif
93         if (lsa->sa.sa_family == AF_INET) {
94                 return lsa->sin.sin_port;
95         }
96         /* What? UNIX socket? IPX?? :) */
97         return -1;
98 }
99
100 void set_nport(len_and_sockaddr *lsa, unsigned port)
101 {
102 #if ENABLE_FEATURE_IPV6
103         if (lsa->sa.sa_family == AF_INET6) {
104                 lsa->sin6.sin6_port = port;
105                 return;
106         }
107 #endif
108         if (lsa->sa.sa_family == AF_INET) {
109                 lsa->sin.sin_port = port;
110                 return;
111         }
112         /* What? UNIX socket? IPX?? :) */
113 }
114
115 /* host: "1.2.3.4[:port]", "www.google.com[:port]"
116  * port: if neither of above specifies port #
117  */
118 static len_and_sockaddr* str2sockaddr(const char *host, int port, int ai_flags)
119 {
120         int rc;
121         len_and_sockaddr *r; // = NULL;
122         struct addrinfo *result = NULL;
123         const char *org_host = host; /* only for error msg */
124         const char *cp;
125         struct addrinfo hint;
126
127         /* Ugly parsing of host:addr */
128         if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
129                 host++;
130                 cp = strchr(host, ']');
131                 if (!cp || cp[1] != ':') /* Malformed: must have [xx]:nn */
132                         bb_error_msg_and_die("bad address '%s'", org_host);
133                         //return r; /* return NULL */
134         } else {
135                 cp = strrchr(host, ':');
136                 if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
137                         /* There is more than one ':' (e.g. "::1") */
138                         cp = NULL; /* it's not a port spec */
139                 }
140         }
141         if (cp) {
142                 int sz = cp - host + 1;
143                 host = safe_strncpy(alloca(sz), host, sz);
144                 if (ENABLE_FEATURE_IPV6 && *cp != ':')
145                         cp++; /* skip ']' */
146                 cp++; /* skip ':' */
147                 port = xatou16(cp);
148         }
149
150         memset(&hint, 0 , sizeof(hint));
151         /* hint.ai_family = AF_UNSPEC; - zero anyway */
152 #if !ENABLE_FEATURE_IPV6
153         hint.ai_family = AF_INET; /* do not try to find IPv6 */
154 #endif
155         /* Needed. Or else we will get each address thrice (or more)
156          * for each possible socket type (tcp,udp,raw...): */
157         hint.ai_socktype = SOCK_STREAM;
158         hint.ai_flags = ai_flags;
159         rc = getaddrinfo(host, NULL, &hint, &result);
160         if (rc || !result)
161                 bb_error_msg_and_die("bad address '%s'", org_host);
162         r = xmalloc(offsetof(len_and_sockaddr, sa) + result->ai_addrlen);
163         r->len = result->ai_addrlen;
164         memcpy(&r->sa, result->ai_addr, result->ai_addrlen);
165         set_nport(r, htons(port));
166         freeaddrinfo(result);
167         return r;
168 }
169
170 len_and_sockaddr* host2sockaddr(const char *host, int port)
171 {
172         return str2sockaddr(host, port, 0);
173 }
174
175 static len_and_sockaddr* dotted2sockaddr(const char *host, int port)
176 {
177         return str2sockaddr(host, port, NI_NUMERICHOST);
178 }
179
180 int xsocket_stream(len_and_sockaddr **lsap)
181 {
182         len_and_sockaddr *lsa;
183         int fd;
184         int len = sizeof(struct sockaddr_in);
185         int family = AF_INET;
186
187 #if ENABLE_FEATURE_IPV6
188         fd = socket(AF_INET6, SOCK_STREAM, 0);
189         if (fd >= 0) {
190                 len = sizeof(struct sockaddr_in6);
191                 family = AF_INET6;
192         } else
193 #endif
194         {
195                 fd = xsocket(AF_INET, SOCK_STREAM, 0);
196         }
197         lsa = xzalloc(offsetof(len_and_sockaddr, sa) + len);
198         lsa->len = len;
199         lsa->sa.sa_family = family;
200         *lsap = lsa;
201         return fd;
202 }
203
204 int create_and_bind_stream_or_die(const char *bindaddr, int port)
205 {
206         int fd;
207         len_and_sockaddr *lsa;
208
209         if (bindaddr && bindaddr[0]) {
210                 lsa = dotted2sockaddr(bindaddr, port);
211                 /* currently NULL check is in str2sockaddr */
212                 //if (!lsa)
213                 //      bb_error_msg_and_die("bad address '%s'", bindaddr);
214                 /* user specified bind addr dictates family */
215                 fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
216         } else {
217                 fd = xsocket_stream(&lsa);
218                 set_nport(lsa, htons(port));
219         }
220         setsockopt_reuseaddr(fd);
221         xbind(fd, &lsa->sa, lsa->len);
222         free(lsa);
223         return fd;
224 }
225
226 int create_and_connect_stream_or_die(const char *peer, int port)
227 {
228         int fd;
229         len_and_sockaddr *lsa;
230
231         lsa = host2sockaddr(peer, port);
232         /* currently NULL check is in str2sockaddr */
233         //if (!lsa)
234         //      bb_error_msg_and_die("bad address '%s'", peer);
235         fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
236         setsockopt_reuseaddr(fd);
237         xconnect(fd, &lsa->sa, lsa->len);
238         free(lsa);
239         return fd;
240 }
241
242 int xconnect_stream(const len_and_sockaddr *lsa)
243 {
244         int fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
245         xconnect(fd, &lsa->sa, lsa->len);
246         return fd;
247 }
248
249 static char* sockaddr2str(const struct sockaddr *sa, socklen_t salen, int flags)
250 {
251         char host[128];
252         char serv[16];
253         int rc = getnameinfo(sa, salen,
254                         host, sizeof(host),
255                         serv, sizeof(serv),
256                         /* do not resolve port# into service _name_ */
257                         flags | NI_NUMERICSERV
258         );
259         if (rc)
260                 return NULL;
261 #if ENABLE_FEATURE_IPV6
262         if (sa->sa_family == AF_INET6)
263                 return xasprintf("[%s]:%s", host, serv);
264 #endif
265         /* For now we don't support anything else, so it has to be INET */
266         /*if (sa->sa_family == AF_INET)*/
267                 return xasprintf("%s:%s", host, serv);
268 }
269
270 char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen)
271 {
272         return sockaddr2str(sa, salen, 0);
273 }
274
275 char* xmalloc_sockaddr2dotted(const struct sockaddr *sa, socklen_t salen)
276 {
277         return sockaddr2str(sa, salen, NI_NUMERICHOST);
278 }