libeech

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

commit 51e55f77965f2d224d31fcf839f763631726b633
parent dfdb0fe7098b4c2789baac70e528ec734cfe82b5
Author: z3bra <contactatz3bradotorg>
Date:   Mon Nov  6 12:47:03 +0100

Rename tcpconnect() to netconn()

Diffstat:
libeech.c | 70++++++++++++++++++++++++++++++++++++----------------------------------
1 file changed, 36 insertions(+), 34 deletions(-)
diff --git a/libeech.c b/libeech.c @@ -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 *); @@ -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 * @@ -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) { @@ -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; } @@ -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; @@ -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; } @@ -384,7 +385,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; }