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
pollinate(int in1,int in2,int out1,int out2,void (* callback)(int fd,void * buf,size_t len),int timeout,int shutdown_timeout)99 int pollinate(int in1, int in2, int out1, int out2,
100 void (*callback)(int fd, void *buf, size_t len),
101 int timeout, int shutdown_timeout)
102 {
103 struct pollfd pollfds[2];
104 int i, pollcount = 2;
105
106 memset(pollfds, 0, 2*sizeof(struct pollfd));
107 pollfds[0].events = pollfds[1].events = POLLIN;
108 pollfds[0].fd = in1;
109 pollfds[1].fd = in2;
110
111 // Poll loop copying data from each fd to the other one.
112 for (;;) {
113 if (!xpoll(pollfds, pollcount, timeout)) return pollcount;
114
115 for (i=0; i<pollcount; i++) {
116 if (pollfds[i].revents & POLLIN) {
117 int len = read(pollfds[i].fd, libbuf, sizeof(libbuf));
118 if (len<1) pollfds[i].revents = POLLHUP;
119 else {
120 callback(i ? out2 : out1, libbuf, len);
121 continue;
122 }
123 }
124 if (pollfds[i].revents & POLLHUP) {
125 // Close half-connection. This is needed for things like
126 // "echo GET / | netcat landley.net 80"
127 // Note that in1 closing triggers timeout, in2 returns now.
128 if (i) {
129 shutdown(pollfds[0].fd, SHUT_WR);
130 pollcount--;
131 timeout = shutdown_timeout;
132 } else return 0;
133 }
134 }
135 }
136 }
137
138 // Return converted ipv4/ipv6 numeric address in libbuf
ntop(struct sockaddr * sa)139 char *ntop(struct sockaddr *sa)
140 {
141 void *addr;
142
143 if (sa->sa_family == AF_INET) addr = &((struct sockaddr_in *)sa)->sin_addr;
144 else addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
145
146 inet_ntop(sa->sa_family, addr, libbuf, sizeof(libbuf));
147
148 return libbuf;
149 }
150
xsendto(int sockfd,void * buf,size_t len,struct sockaddr * dest)151 void xsendto(int sockfd, void *buf, size_t len, struct sockaddr *dest)
152 {
153 int rc = sendto(sockfd, buf, len, 0, dest,
154 dest->sa_family == AF_INET ? sizeof(struct sockaddr_in) :
155 sizeof(struct sockaddr_in6));
156
157 if (rc != len) perror_exit("sendto");
158 }
159
160 // xrecvfrom with timeout in milliseconds
xrecvwait(int fd,char * buf,int len,union socksaddr * sa,int timeout)161 int xrecvwait(int fd, char *buf, int len, union socksaddr *sa, int timeout)
162 {
163 socklen_t sl = sizeof(*sa);
164
165 if (timeout >= 0) {
166 struct pollfd pfd;
167
168 pfd.fd = fd;
169 pfd.events = POLLIN;
170 if (!xpoll(&pfd, 1, timeout)) return 0;
171 }
172
173 len = recvfrom(fd, buf, len, 0, (void *)sa, &sl);
174 if (len<0) perror_exit("recvfrom");
175
176 return len;
177 }
178
179 // Convert space/low ascii to %XX escapes, plus any chars in "and" string.
180 // Returns newly allocated copy of string (even if no changes)
escape_url(char * str,char * and)181 char *escape_url(char *str, char *and)
182 {
183 int i, j , count;
184 char *ret QUIET, *ss QUIET;
185
186 for (j = count = 0;;) {
187 for (i = 0;;) {
188 if (str[i] && (str[i]<=' ' || (and && strchr(and, str[i])))) {
189 if (j) ss += sprintf(ss, "%%%02x", str[i]);
190 else count++;
191 } else if (j) *ss++ = str[i];
192 if (!str[i++]) break;
193 }
194 if (j++) break;
195 ret = ss = xmalloc(i+count*2);
196 }
197
198 return ret;
199 }
200
201 // Convert %XX escapes to character (in place)
unescape_url(char * str,int do_cut)202 char *unescape_url(char *str, int do_cut)
203 {
204 char *to, *cut = do_cut ? strchr(str, '?') : 0;
205 int i;
206
207 for (to = str;;) {
208 if (*str!='%' || !isxdigit(str[1]) || !isxdigit(str[2])) {
209 if (str==cut) {
210 *to = 0;
211 cut++;
212
213 break;
214 } else if (!(*to++ = *str++)) break;
215 } else {
216 sscanf(++str, "%2x", &i);
217 *to++ = i;
218 str += 2;
219 }
220 }
221
222 return cut;
223 }
224