1#include "toys.h"
2
3int 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
13void 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
19struct 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
40static int xconnbind(struct addrinfo *ai_arg, int dobind)
41{
42 struct addrinfo *ai;
43 int fd = -1, one = 1;
44
45
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
59int xconnectany(struct addrinfo *ai)
60{
61 return xconnbind(ai, 0);
62}
63
64
65int xbindany(struct addrinfo *ai)
66{
67 return xconnbind(ai, 1);
68}
69
70void xbind(int fd, const struct sockaddr *sa, socklen_t len)
71{
72 if (bind(fd, sa, len)) perror_exit("bind");
73}
74
75void xconnect(int fd, const struct sockaddr *sa, socklen_t len)
76{
77 if (connect(fd, sa, len)) perror_exit("connect");
78}
79
80int 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
97
98
99int pollinate(int in1, int in2, int out1, int out2, int timeout, int shutdown_timeout)
100{
101 struct pollfd pollfds[2];
102 int i, pollcount = 2;
103
104 memset(pollfds, 0, 2*sizeof(struct pollfd));
105 pollfds[0].events = pollfds[1].events = POLLIN;
106 pollfds[0].fd = in1;
107 pollfds[1].fd = in2;
108
109
110 for (;;) {
111 if (!xpoll(pollfds, pollcount, timeout)) return pollcount;
112
113 for (i=0; i<pollcount; i++) {
114 if (pollfds[i].revents & POLLIN) {
115 int len = read(pollfds[i].fd, libbuf, sizeof(libbuf));
116 if (len<1) pollfds[i].revents = POLLHUP;
117 else {
118 xwrite(i ? out2 : out1, libbuf, len);
119 continue;
120 }
121 }
122 if (pollfds[i].revents & POLLHUP) {
123
124
125
126 if (i) {
127 shutdown(pollfds[0].fd, SHUT_WR);
128 pollcount--;
129 timeout = shutdown_timeout;
130 } else return 0;
131 }
132 }
133 }
134}
135
136
137char *ntop(struct sockaddr *sa)
138{
139 void *addr;
140
141 if (sa->sa_family == AF_INET) addr = &((struct sockaddr_in *)sa)->sin_addr;
142 else addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
143
144 inet_ntop(sa->sa_family, addr, libbuf, sizeof(libbuf));
145
146 return libbuf;
147}
148
149void xsendto(int sockfd, void *buf, size_t len, struct sockaddr *dest)
150{
151 int rc = sendto(sockfd, buf, len, 0, dest,
152 dest->sa_family == AF_INET ? sizeof(struct sockaddr_in) :
153 sizeof(struct sockaddr_in6));
154
155 if (rc != len) perror_exit("sendto");
156}
157
158
159int xrecvwait(int fd, char *buf, int len, union socksaddr *sa, int timeout)
160{
161 socklen_t sl = sizeof(*sa);
162
163 if (timeout >= 0) {
164 struct pollfd pfd;
165
166 pfd.fd = fd;
167 pfd.events = POLLIN;
168 if (!xpoll(&pfd, 1, timeout)) return 0;
169 }
170
171 len = recvfrom(fd, buf, len, 0, (void *)sa, &sl);
172 if (len<0) perror_exit("recvfrom");
173
174 return len;
175}
176
177
178
179char *escape_url(char *str, char *and)
180{
181 int i, j , count;
182 char *ret QUIET, *ss QUIET;
183
184 for (j = count = 0;;) {
185 for (i = 0;;) {
186 if (str[i] && (str[i]<=' ' || (and && strchr(and, str[i])))) {
187 if (j) ss += sprintf(ss, "%%%02x", str[i]);
188 else count++;
189 } else if (j) *ss++ = str[i];
190 if (!str[i++]) break;
191 }
192 if (j++) break;
193 ret = ss = xmalloc(i+count*2);
194 }
195
196 return ret;
197}
198
199
200void unescape_url(char *str)
201{
202 char *to;
203 int i;
204
205 for (to = str;;) {
206 if (*str!='%' || !isxdigit(str[1]) || !isxdigit(str[2])) {
207 if (!(*to++ = *str++)) break;
208 } else {
209 sscanf(++str, "%2x", &i);
210 *to++ = i;
211 str += 2;
212 }
213 }
214}
215