Home | History | Annotate | Download | only in lib
      1 #include "toys.h"
      2 
      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 
     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 struct addrinfo *xgetaddrinfo(char *host, char *port, int family, int socktype,
     19   int protocol, int flags)
     20 {
     21   struct addrinfo info, *ai;
     22   int rc;
     23 
     24   memset(&info, 0, sizeof(struct addrinfo));
     25   info.ai_family = family;
     26   info.ai_socktype = socktype;
     27   info.ai_protocol = protocol;
     28   info.ai_flags = flags;
     29 
     30   rc = getaddrinfo(host, port, &info, &ai);
     31   if (rc || !ai)
     32     error_exit("%s%s%s: %s", host, port ? ":" : "", port ? port : "",
     33       rc ? gai_strerror(rc) : "not found");
     34 
     35   return ai;
     36 }
     37 
     38 int xconnect(struct addrinfo *ai_arg)
     39 {
     40   struct addrinfo *ai;
     41   int fd = -1;
     42 
     43   // Try all the returned addresses. Report errors if last entry can't connect.
     44   for (ai = ai_arg; ai; ai = ai->ai_next) {
     45     fd = (ai->ai_next ? socket : xsocket)(ai->ai_family, ai->ai_socktype,
     46       ai->ai_protocol);
     47     if (!connect(fd, ai->ai_addr, ai->ai_addrlen)) break;
     48     else if (!ai->ai_next) perror_exit("connect");
     49     close(fd);
     50   }
     51   freeaddrinfo(ai_arg);
     52 
     53   return fd;
     54 }
     55 
     56 int xpoll(struct pollfd *fds, int nfds, int timeout)
     57 {
     58   int i;
     59 
     60   for (;;) {
     61     if (0>(i = poll(fds, nfds, timeout))) {
     62       if (toys.signal) return i;
     63       if (errno != EINTR && errno != ENOMEM) perror_exit("xpoll");
     64       else if (timeout>0) timeout--;
     65     } else return i;
     66   }
     67 }
     68 
     69 // Loop forwarding data from in1 to out1 and in2 to out2, handling
     70 // half-connection shutdown. timeouts return if no data for X miliseconds.
     71 // Returns 0: both closed, 1 shutdown_timeout, 2 timeout
     72 int pollinate(int in1, int in2, int out1, int out2, int timeout, int shutdown_timeout)
     73 {
     74   struct pollfd pollfds[2];
     75   int i, pollcount = 2;
     76 
     77   memset(pollfds, 0, 2*sizeof(struct pollfd));
     78   pollfds[0].events = pollfds[1].events = POLLIN;
     79   pollfds[0].fd = in1;
     80   pollfds[1].fd = in2;
     81 
     82   // Poll loop copying data from each fd to the other one.
     83   for (;;) {
     84     if (!xpoll(pollfds, pollcount, timeout)) return pollcount;
     85 
     86     for (i=0; i<pollcount; i++) {
     87       if (pollfds[i].revents & POLLIN) {
     88         int len = read(pollfds[i].fd, libbuf, sizeof(libbuf));
     89         if (len<1) pollfds[i].revents = POLLHUP;
     90         else xwrite(i ? out2 : out1, libbuf, len);
     91       }
     92       if (pollfds[i].revents & POLLHUP) {
     93         // Close half-connection.  This is needed for things like
     94         // "echo GET / | netcat landley.net 80"
     95         // Note that in1 closing triggers timeout, in2 returns now.
     96         if (i) {
     97           shutdown(pollfds[0].fd, SHUT_WR);
     98           pollcount--;
     99           timeout = shutdown_timeout;
    100         } else return 0;
    101       }
    102     }
    103   }
    104 }
    105 
    106 // Return converted numeric address in libbuf
    107 char *ntop(struct sockaddr *sa)
    108 {
    109   void *addr;
    110 
    111   if (sa->sa_family == AF_INET) addr = &((struct sockaddr_in *)sa)->sin_addr;
    112   else addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
    113 
    114   inet_ntop(sa->sa_family, addr, libbuf, sizeof(libbuf));
    115 
    116   return libbuf;
    117 }
    118