xref: /third_party/toybox/lib/net.c (revision 0f66f451)
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// if !host bind to all local interfaces
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  // 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
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// 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
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  // Poll loop copying data from each fd to the other one.
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 xwrite(i ? out2 : out1, libbuf, len);
118      }
119      if (pollfds[i].revents & POLLHUP) {
120        // Close half-connection.  This is needed for things like
121        // "echo GET / | netcat landley.net 80"
122        // Note that in1 closing triggers timeout, in2 returns now.
123        if (i) {
124          shutdown(pollfds[0].fd, SHUT_WR);
125          pollcount--;
126          timeout = shutdown_timeout;
127        } else return 0;
128      }
129    }
130  }
131}
132
133// Return converted ipv4/ipv6 numeric address in libbuf
134char *ntop(struct sockaddr *sa)
135{
136  void *addr;
137
138  if (sa->sa_family == AF_INET) addr = &((struct sockaddr_in *)sa)->sin_addr;
139  else addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
140
141  inet_ntop(sa->sa_family, addr, libbuf, sizeof(libbuf));
142
143  return libbuf;
144}
145
146void xsendto(int sockfd, void *buf, size_t len, struct sockaddr *dest)
147{
148  int rc = sendto(sockfd, buf, len, 0, dest,
149    dest->sa_family == AF_INET ? sizeof(struct sockaddr_in) :
150      sizeof(struct sockaddr_in6));
151
152  if (rc != len) perror_exit("sendto");
153}
154
155// xrecvfrom with timeout in milliseconds
156int xrecvwait(int fd, char *buf, int len, union socksaddr *sa, int timeout)
157{
158  socklen_t sl = sizeof(*sa);
159
160  if (timeout >= 0) {
161    struct pollfd pfd;
162
163    pfd.fd = fd;
164    pfd.events = POLLIN;
165    if (!xpoll(&pfd, 1, timeout)) return 0;
166  }
167
168  len = recvfrom(fd, buf, len, 0, (void *)sa, &sl);
169  if (len<0) perror_exit("recvfrom");
170
171  return len;
172}
173