libeech

BitTorrent library
git clone git://z3bra.org/libeech.git
Log | Files | Refs | README | LICENSE

commit 4677f08d67ca79c4bd76bd9dfa1ac161b30869f4
parent f5efe190eced0897d976e704782ea3db3a4096a0
Author: z3bra <contactatz3bradotorg>
Date:   Sat Nov  4 20:22:52 +0100

Establish connection to peer immediately

Diffstat:
libeech.c | 18++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/libeech.c b/libeech.c @@ -1,4 +1,5 @@ /* See LICENSE file for copyright and license details. */ +#include <errno.h> #include <fcntl.h> #include <limits.h> #include <netdb.h> @@ -184,14 +185,12 @@ tcpconnect(char *host, int port) return -1; flags = fcntl(fd, F_GETFL, 0); - if (flags < 0) - return -1; - - if (fcntl(fd, F_SETFL, flags|O_NONBLOCK) < 0) - return -1; + fcntl(fd, F_SETFL, flags|O_NONBLOCK); - if (connect(fd, (struct sockaddr *)&in, sizeof(in)) < 0) + if (connect(fd, (struct sockaddr *)&in, sizeof(in)) < 0 && errno != EINPROGRESS) { + close(fd); return -1; + } return fd; } @@ -205,13 +204,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; } @@ -338,9 +337,12 @@ glch_addpeer(struct torrent *t, char *host, int port) return 0; p = addpeer(t->peers, host, port); - if (!p) + if (!p) { + free(p); return -1; + } t->peers = p; + return 0; }