tFactorize transmitting/receiving messages - libeech - bittorrent library
(HTM) git clone git://z3bra.org/libeech.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 84c079251a69ad84d676a2b0db32a8b3f8ca96c7
(DIR) parent ddf51c1ca8e2c0f84e146cf52d019a599b5c45c2
(HTM) Author: z3bra <contactatz3bradotorg>
Date: Tue, 13 Feb 2018 23:21:28 -0500
Factorize transmitting/receiving messages
Diffstat:
M libeech.c | 46 +++++++++++++++++++++----------
1 file changed, 31 insertions(+), 15 deletions(-)
---
(DIR) diff --git a/libeech.c b/libeech.c
t@@ -44,6 +44,9 @@ static ssize_t pwphs(struct torrent *, struct peer *);
static ssize_t pwphsrecv(struct peer *);
static int pwphsck(struct torrent *, char *, long);
+static int pwptx(struct torrent *, struct peer *);
+static int pwprx(struct torrent *, struct peer *);
+
/* p2p network management */
static int netconn(char *, int);
static int netloop(struct torrent *, int);
t@@ -335,6 +338,30 @@ pwphsck(struct torrent *t, char *hs, long l)
}
static int
+pwptx(struct torrent *t, struct peer *p)
+{
+ /* send handshake */
+ if (!(p->state & HANDSHAKESENT)) {
+ pwphs(t, p);
+ p->state |= HANDSHAKESENT;
+ }
+ return 0;
+}
+
+static int
+pwprx(struct torrent *t, struct peer *p)
+{
+ /* receive and verify handshake */
+ if (!(p->state & HANDSHAKERCVD)) {
+ pwphsrecv(p);
+ if (p->buflen >= 68 && pwphsck(t, p->buf, p->buflen))
+ delpeer(t->peers, p);
+ p->state |= HANDSHAKERCVD;
+ }
+ return 0;
+}
+
+static int
netconn(char *host, int port)
{
int fd = -1;
t@@ -395,22 +422,11 @@ netloop(struct torrent *t, int timeout)
return r;
for (i = 0; i < nfds; i++) {
- //printf("fd:%d ev:%d\n", pfds.p[i]->fd, pfds.fds[i].revents);
- if (pfds.fds[i].revents & POLLOUT) {
- /* send handshake */
- if (!(pfds.p[i]->state & HANDSHAKESENT)) {
- pwphs(t, pfds.p[i]);
- pfds.p[i]->state |= HANDSHAKESENT;
- }
- }
+ if (pfds.fds[i].revents & POLLOUT)
+ pwptx(t, pfds.p[i]);
- if (pfds.fds[i].revents & POLLIN) {
- /* receive and verify handshake */
- if (!(pfds.p[i]->state & HANDSHAKERCVD)) {
- if (pwphsrecv(pfds.p[i]) >= 68 && !pwphsck(t, pfds.p[i]->buf, pfds.p[i]->buflen))
- pfds.p[i]->state |= HANDSHAKERCVD;
- }
- }
+ if (pfds.fds[i].revents & POLLIN)
+ pwprx(t, pfds.p[i]);
/* peer is now ready to exchange data */
if (pfds.p[i]->state & HANDSHAKESENT && pfds.p[i]->state & HANDSHAKERCVD)