tRename tcpconnect() to netconn() - libeech - bittorrent library
 (HTM) git clone git://z3bra.org/libeech.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 0d1a23c249eff31fecba9735ac0a4d537e3144e6
 (DIR) parent 7e9e6a0571c4e55eaa9668eda0da0595ebce8931
 (HTM) Author: z3bra <contactatz3bradotorg>
       Date:   Mon,  6 Nov 2017 12:47:03 +0100
       
       Rename tcpconnect() to netconn()
       
       Diffstat:
         M libeech.c                           |      70 ++++++++++++++++---------------
       
       1 file changed, 36 insertions(+), 34 deletions(-)
       ---
 (DIR) diff --git a/libeech.c b/libeech.c
       t@@ -25,7 +25,6 @@ static long torrentfiles(struct torrent *);
        static int chktorrent(struct be *);
        
        /* manage a list of peers */
       -static int tcpconnect(char *, int);
        static struct peer * addpeer(struct peer *, char *, int);
        static struct peer * getpeer(struct peer *, char *, int);
        static struct peer * delpeer(struct peer *, struct peer *);
       t@@ -37,6 +36,7 @@ static ssize_t pwphsrecv(struct peer *);
        static int pwphsck(struct torrent *, char *, long);
        
        /* p2p network management */
       +static int netconn(char *, int);
        static int netloop(struct torrent *, int);
        
        static char *
       t@@ -168,37 +168,6 @@ chktorrent(struct be *b)
                return 0;
        }
        
       -static int
       -tcpconnect(char *host, int port)
       -{
       -        int fd = -1;
       -        int flags = 0;
       -        struct hostent *he;
       -        struct sockaddr_in in;
       -
       -        if (!(he = gethostbyname(host)))
       -                return -1;
       -
       -        memset(&in, 0, sizeof(in));
       -        memcpy(&in.sin_addr, he->h_addr_list[0], he->h_length);
       -        in.sin_family = AF_INET;
       -        in.sin_port   = htons(port);
       -
       -        fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
       -        if (fd < 0)
       -                return -1;
       -
       -        flags = fcntl(fd, F_GETFL, 0);
       -        fcntl(fd, F_SETFL, flags|O_NONBLOCK);
       -
       -        if (connect(fd, (struct sockaddr *)&in, sizeof(in)) < 0 && errno != EINPROGRESS) {
       -                close(fd);
       -                return -1;
       -        }
       -
       -        return fd;
       -}
       -
        static struct peer *
        addpeer(struct peer *pl, char *host, int port)
        {
       t@@ -208,13 +177,13 @@ addpeer(struct peer *pl, char *host, int port)
                if (!p)
                        return NULL;
        
       +        p->fd = -1;
                p->state = 0;
                p->next = pl;
                p->port = port;
                p->buflen = 0;
                memcpy(p->host, host, HOST_NAME_MAX);
                memset(p->buf, 0, MSGSIZ);
       -        p->fd = tcpconnect(host, port);
        
                return p;
        }
       t@@ -300,6 +269,37 @@ pwphsck(struct torrent *t, char *hs, long l)
        }
        
        static int
       +netconn(char *host, int port)
       +{
       +        int fd = -1;
       +        int flags = 0;
       +        struct hostent *he;
       +        struct sockaddr_in in;
       +
       +        if (!(he = gethostbyname(host)))
       +                return -1;
       +
       +        memset(&in, 0, sizeof(in));
       +        memcpy(&in.sin_addr, he->h_addr_list[0], he->h_length);
       +        in.sin_family = AF_INET;
       +        in.sin_port   = htons(port);
       +
       +        fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
       +        if (fd < 0)
       +                return -1;
       +
       +        flags = fcntl(fd, F_GETFL, 0);
       +        fcntl(fd, F_SETFL, flags|O_NONBLOCK);
       +
       +        if (connect(fd, (struct sockaddr *)&in, sizeof(in)) < 0 && errno != EINPROGRESS) {
       +                close(fd);
       +                return -1;
       +        }
       +
       +        return fd;
       +}
       +
       +static int
        netloop(struct torrent *t, int timeout)
        {
                int i, npeer = 0;
       t@@ -313,6 +313,7 @@ netloop(struct torrent *t, int timeout)
                        return -1;
        
                for (i = 0, p = t->peers; p; p = p->next) {
       +                if (p->fd < 0)
                        fds[i].fd = p->fd;
                        fds[i].events = POLLIN|POLLOUT;
                }
       t@@ -385,7 +386,8 @@ glch_addpeer(struct torrent *t, char *host, int port)
                        return 0;
        
                p = addpeer(t->peers, host, port);
       -        if (!p) {
       +        p->fd = netconn(p->host, p->port);
       +        if (!p || p->fd < 0) {
                        free(p);
                        return -1;
                }