1 #include "toys.h"
2
xsocket(int domain,int type,int protocol)3 int xsocket(int domain, int type, int protocol)
4 {
5 int fd = socket(domain, type, protocol);
6
7 if (fd < 0) perror_exit("socket %x %x", type, protocol);
8 fcntl(fd, F_SETFD, FD_CLOEXEC);
9
10 return fd;
11 }
12
xsetsockopt(int fd,int level,int opt,void * val,socklen_t len)13 void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len)
14 {
15 if (-1 == setsockopt(fd, level, opt, val, len)) perror_exit("setsockopt");
16 }
17
18 // if !host bind to all local interfaces
xgetaddrinfo(char * host,char * port,int family,int socktype,int protocol,int flags)19 struct addrinfo *xgetaddrinfo(char *host, char *port, int family, int socktype,
20 int protocol, int flags)
21 {
22 struct addrinfo info, *ai;
23 int rc;
24
25 memset(&info, 0, sizeof(struct addrinfo));
26 info.ai_family = family;
27 info.ai_socktype = socktype;
28 info.ai_protocol = protocol;
29 info.ai_flags = flags;
30 if (!host) info.ai_flags |= AI_PASSIVE;
31
32 rc = getaddrinfo(host, port, &info, &ai);
33 if (rc || !ai)
34 error_exit("%s%s%s: %s", host ? host : "*", port ? ":" : "",
35 port ? port : "", rc ? gai_strerror(rc) : "not found");
36
37 return ai;
38 }
39
xconnbind(struct addrinfo * ai_arg,int dobind)40 static int xconnbind(struct addrinfo *ai_arg, int dobind)
41 {
42 struct addrinfo *ai;
43 int fd = -1, one = 1;
44
45 // Try all the returned addresses. Report errors if last entry can't connect.
46 for (ai = ai_arg; ai; ai = ai->ai_next) {
47 fd = (ai->ai_next ? socket : xsocket)(ai->ai_family, ai->ai_socktype,
48 ai->ai_protocol);
49 xsetsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
50 if (!(dobind ? bind : connect)(fd, ai->ai_addr, ai->ai_addrlen)) break;
51 else if (!ai->ai_next) perror_exit_raw(dobind ? "bind" : "connect");
52 close(fd);
53 }
54 freeaddrinfo(ai_arg);
55
56 return fd;
57 }
58
xconnectany(struct addrinfo * ai)59 int xconnectany(struct addrinfo *ai)
60 {
61 return xconnbind(ai, 0);
62 }
63
64
xbindany(struct addrinfo * ai)65 int xbindany(struct addrinfo *ai)
66 {
67 return xconnbind(ai, 1);
68 }
69
xbind(int fd,const struct sockaddr * sa,socklen_t len)70 void xbind(int fd, const struct sockaddr *sa, socklen_t len)
71 {
72 if (bind(fd, sa, len)) perror_exit("bind");
73 }
74
xconnect(int fd,const struct sockaddr * sa,socklen_t len)75 void xconnect(int fd, const struct sockaddr *sa, socklen_t len)
76 {
77 if (connect(fd, sa, len)) perror_exit("connect");
78 }
79
xpoll(struct pollfd * fds,int nfds,int timeout)80 int xpoll(struct pollfd *fds, int nfds, int timeout)
81 {
82 int i;
83 long long now, then = timeout>0 ? millitime() : 0;
84
85 for (;;) {
86 if (0<=(i = poll(fds, nfds, timeout)) || toys.signal) return i;
87 if (errno != EINTR && errno != ENOMEM) perror_exit("xpoll");
88 else {
89 now = millitime();
90 timeout -= now-then;
91 then = now;
92 }
93 }
94 }
95
96 // Loop forwarding data from in1 to out1 and in2 to out2, handling
97 // half-connection shutdown. timeouts return if no data for X ms.
98 // Returns 0: both closed, 1 shutdown_timeout, 2 timeout
99 #ifdef TOYBOX_OH_ADAPT
100 /* fix "netcat -q" fail problem */
pollinate(int in1,int in2,int out1,int out2,int timeout,int shutdown_timeout)101 int pollinate(int in1, int in2, int out1, int out2, int timeout, int shutdown_timeout)
102 {
103 struct pollfd pollfds[2];
104 int i, pollcount = 2;
105 long long deadline = -1;
106 int socket_closed = 0;
107 int is_server = 0;
108 int is_udp = 0;
109
110 is_server = (isatty(in2) && !isatty(in1));
111
112 {
113 int type;
114 socklen_t len = sizeof(type);
115 if(getsockopt(in1, SOL_SOCKET, SO_TYPE, &type, &len) == 0) {
116 is_udp = (type == SOCK_DGRAM);
117 }
118 }
119
120 memset(pollfds, 0, 2*sizeof(struct pollfd));
121 pollfds[0].events = pollfds[1].events = POLLIN;
122 pollfds[0].fd = in1;
123 pollfds[1].fd = in2;
124
125 for(;;) {
126 int current_timeout = timeout;
127 long long now = millitime();
128
129 if(deadline >= 0) {
130 current_timeout = deadline - now;
131 if(current_timeout < 0) current_timeout = 0;
132 }
133
134 if(socket_closed && pollcount == 1) {
135 if(is_server) {
136 close(pollfds[0].fd);
137 return 0;
138 } else {
139 if(current_timeout <= 0) return 2;
140 usleep(current_timeout * 1000);
141 return 2;
142 }
143 }
144
145 int poll_result = xpoll(pollfds, pollcount, current_timeout);
146 if(poll_result == 0) {
147 if(deadline >= 0) return 2;
148 else return 1;
149 }
150
151 for(i = 0; i < pollcount; i++) {
152 if (i == 0 && socket_closed) continue;
153
154 if(pollfds[i].revents & POLLIN) {
155 int len = read(pollfds[i].fd, libbuf, sizeof(libbuf));
156 if(len < 1) {
157 pollfds[i].revents = POLLHUP;
158 } else {
159 xwrite(i ? out2 : out1, libbuf, len);
160 continue;
161 }
162 }
163
164 if(pollfds[i].revents & POLLHUP) {
165 if(i) {
166 if(!is_udp && !socket_closed)
167 shutdown(pollfds[0].fd, SHUT_WR);
168
169 pollcount--;
170 if(shutdown_timeout >= 0)
171 deadline = millitime() + shutdown_timeout;
172 } else {
173 socket_closed = 1;
174 pollcount--;
175 }
176 }
177 }
178 }
179 }
180 #else
pollinate(int in1,int in2,int out1,int out2,int timeout,int shutdown_timeout)181 int pollinate(int in1, int in2, int out1, int out2, int timeout, int shutdown_timeout)
182 {
183 struct pollfd pollfds[2];
184 int i, pollcount = 2;
185
186 memset(pollfds, 0, 2*sizeof(struct pollfd));
187 pollfds[0].events = pollfds[1].events = POLLIN;
188 pollfds[0].fd = in1;
189 pollfds[1].fd = in2;
190
191 // Poll loop copying data from each fd to the other one.
192 for (;;) {
193 if (!xpoll(pollfds, pollcount, timeout)) return pollcount;
194
195 for (i=0; i<pollcount; i++) {
196 if (pollfds[i].revents & POLLIN) {
197 int len = read(pollfds[i].fd, libbuf, sizeof(libbuf));
198 if (len<1) pollfds[i].revents = POLLHUP;
199 else xwrite(i ? out2 : out1, libbuf, len);
200 }
201 if (pollfds[i].revents & POLLHUP) {
202 // Close half-connection. This is needed for things like
203 // "echo GET / | netcat landley.net 80"
204 // Note that in1 closing triggers timeout, in2 returns now.
205 if (i) {
206 shutdown(pollfds[0].fd, SHUT_WR);
207 pollcount--;
208 timeout = shutdown_timeout;
209 } else return 0;
210 }
211 }
212 }
213 }
214 #endif
215
216 // Return converted ipv4/ipv6 numeric address in libbuf
ntop(struct sockaddr * sa)217 char *ntop(struct sockaddr *sa)
218 {
219 void *addr;
220
221 if (sa->sa_family == AF_INET) addr = &((struct sockaddr_in *)sa)->sin_addr;
222 else addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
223
224 inet_ntop(sa->sa_family, addr, libbuf, sizeof(libbuf));
225
226 return libbuf;
227 }
228
xsendto(int sockfd,void * buf,size_t len,struct sockaddr * dest)229 void xsendto(int sockfd, void *buf, size_t len, struct sockaddr *dest)
230 {
231 int rc = sendto(sockfd, buf, len, 0, dest,
232 dest->sa_family == AF_INET ? sizeof(struct sockaddr_in) :
233 sizeof(struct sockaddr_in6));
234
235 if (rc != len) perror_exit("sendto");
236 }
237
238 // xrecvfrom with timeout in milliseconds
xrecvwait(int fd,char * buf,int len,union socksaddr * sa,int timeout)239 int xrecvwait(int fd, char *buf, int len, union socksaddr *sa, int timeout)
240 {
241 socklen_t sl = sizeof(*sa);
242
243 if (timeout >= 0) {
244 struct pollfd pfd;
245
246 pfd.fd = fd;
247 pfd.events = POLLIN;
248 if (!xpoll(&pfd, 1, timeout)) return 0;
249 }
250
251 len = recvfrom(fd, buf, len, 0, (void *)sa, &sl);
252 if (len<0) perror_exit("recvfrom");
253
254 return len;
255 }
256
257 // Convert space/low ascii to %XX escapes, plus any chars in "and" string.
258 // Returns newly allocated copy of string (even if no changes)
escape_url(char * str,char * and)259 char *escape_url(char *str, char *and)
260 {
261 int i, j , count;
262 char *ret QUIET, *ss QUIET;
263
264 for (j = count = 0;;) {
265 for (i = 0;;) {
266 if (str[i] && (str[i]<=' ' || (and && strchr(and, str[i])))) {
267 if (j) ss += sprintf(ss, "%%%02x", str[i]);
268 else count++;
269 } else if (j) *ss++ = str[i];
270 if (!str[i++]) break;
271 }
272 if (j++) break;
273 ret = ss = xmalloc(i+count*2);
274 }
275
276 return ret;
277 }
278
279 // Convert %XX escapes to character (in place)
unescape_url(char * str,int do_cut)280 char *unescape_url(char *str, int do_cut)
281 {
282 char *to, *cut = do_cut ? strchr(str, '?') : 0;
283 int i;
284
285 for (to = str;;) {
286 if (*str!='%' || !isxdigit(str[1]) || !isxdigit(str[2])) {
287 if (str==cut) {
288 *to = 0;
289 cut++;
290
291 break;
292 } else if (!(*to++ = *str++)) break;
293 } else {
294 sscanf(++str, "%2x", &i);
295 *to++ = i;
296 str += 2;
297 }
298 }
299
300 return cut;
301 }