]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Thu, 31 Mar 2022 00:04:54 +0000 (17:04 -0700)
committeralex <[email protected]>
Thu, 31 Mar 2022 00:04:54 +0000 (17:04 -0700)
24 files changed:
Makefile.am
inc/net.h
inc/peer.h
src/net/expected.c
src/net/handler.c
src/net/loop.c
src/net/peek.c [new file with mode: 0644]
src/net/queue.c
src/net/send.c
src/net/wait.c
src/peer/bitfield.c
src/peer/cancel.c
src/peer/choke.c
src/peer/handshake.c
src/peer/hash.c
src/peer/hashes.c
src/peer/have.c
src/peer/init.c
src/peer/interest.c
src/peer/keepalive.c
src/peer/piece.c
src/peer/reject.c
src/peer/request.c
test/unit/net.tests.c

index 4d513e6949ef6e1b64b877a031749914348b9875..e102240b540ed5aff18f7d6e65b902cd72815502 100644 (file)
@@ -38,6 +38,7 @@ seederd_SOURCES = \
        src/net/handler.c \
        src/net/listen.c \
        src/net/loop.c \
+       src/net/peek.c \
        src/net/queue.c \
        src/net/send.c \
        src/net/setup.c \
index d69828faf60877f4b19cb75ee00ad198602908d1..e5f21c744cdb146ae597a4496fe0967eac71f7a3 100644 (file)
--- a/inc/net.h
+++ b/inc/net.h
@@ -37,16 +37,17 @@ struct net_info {
 }
 
 int net_cache(void**,size_t*,void*,size_t);
-ssize_t net_expected(int,enum peer_message*);
-void net_handler(int,int,struct peer*);
+ssize_t net_expected(struct peer*,enum peer_message*);
+void net_handler(int,struct peer*);
 int net_listen_sock();
 void net_loop();
-int net_queue(int,int,struct peer*);
-int net_send(int,struct peer*,void*,size_t);
+int net_peek(struct peer*,void*,size_t);
+int net_queue(int,struct peer*);
+int net_send(struct peer*,void*,size_t);
 int net_setup(struct net_info*);
 int net_start();
 int net_tcp();
 int net_udp();
-int net_wait(int,struct peer*,void*,size_t);
+int net_wait(struct peer*,void*,size_t);
 
 #endif
index 212e42411feb45243cafb4b08f77fb7b28387085..5db463f1247932a5e67e47bb2e2e3db0e0c686a9 100644 (file)
 #include<string.h>
 
 struct peer {
+       int sock;
+
        unsigned int handshake : 1;
        unsigned int choked : 1;
        unsigned int interested: 1;
 
-       uint8_t infohash[PEER_INFOHASH_SIZE];
        unsigned char peer_id[PEER_PEER_ID_SIZE];
 
-       void *out;
-       size_t out_size;
+       uint8_t infohash[PEER_INFOHASH_SIZE];
+
+       uint8_t *bitfield;
+       size_t bitfield_size;
 
        void *in;
        size_t in_size;
+       
+       void *out;
+       size_t out_size;
 };
 
 enum peer_message {
@@ -45,37 +51,37 @@ enum peer_message {
 #include<net.h>
 #include<session.h>
 
-int peer_bitfield(int,struct peer*);
-int peer_bitfield_received(int,struct peer*,ssize_t);
+int peer_bitfield(struct peer*);
+int peer_bitfield_received(struct peer*,ssize_t);
 int peer_bitfield_toggle(struct peer*,size_t);
-int peer_cancel(int,struct peer*);
-int peer_cancel_received(int,struct peer*,ssize_t);
-int peer_choke(int,struct peer*);
-int peer_choke_received(int,struct peer*,ssize_t);
+int peer_cancel(struct peer*);
+int peer_cancel_received(struct peer*,ssize_t);
+int peer_choke(struct peer*);
+int peer_choke_received(struct peer*,ssize_t);
 void peer_free(struct peer*);
-int peer_handshake(int,struct peer*);
-int peer_hash_reject(int,struct peer*);
-int peer_hash_reject_received(int,struct peer*,ssize_t);
-int peer_hash_request(int,struct peer*);
-int peer_hash_request_received(int,struct peer*,ssize_t);
-int peer_hashes(int,struct peer*);
-int peer_hashes_received(int,struct peer*,ssize_t);
-int peer_have(int,struct peer*,size_t);
-int peer_have_received(int,struct peer*,ssize_t);
+int peer_handshake(struct peer*);
+int peer_hash_reject(struct peer*);
+int peer_hash_reject_received(struct peer*,ssize_t);
+int peer_hash_request(struct peer*);
+int peer_hash_request_received(struct peer*,ssize_t);
+int peer_hashes(struct peer*);
+int peer_hashes_received(struct peer*,ssize_t);
+int peer_have(struct peer*,size_t);
+int peer_have_received(struct peer*,ssize_t);
 int peer_init(struct peer**);
-int peer_interested(int,struct peer*);
-int peer_interested_received(int,struct peer*,ssize_t);
-int peer_keepalive(int,struct peer*);
-int peer_keepalive_received(int,struct peer*,ssize_t);
-int peer_not_interested(int,struct peer*);
-int peer_not_interested_received(int,struct peer*,ssize_t);
-int peer_piece(int,struct peer*);
-int peer_piece_received(int,struct peer*,ssize_t);
-int peer_reject(int,struct peer*);
-int peer_reject_received(int,struct peer*,ssize_t);
-int peer_request(int,struct peer*);
-int peer_request_received(int,struct peer*,ssize_t);
-int peer_unchoke(int,struct peer*);
-int peer_unchoke_received(int,struct peer*,ssize_t);
+int peer_interested(struct peer*);
+int peer_interested_received(struct peer*,ssize_t);
+int peer_keepalive(struct peer*);
+int peer_keepalive_received(struct peer*,ssize_t);
+int peer_not_interested(struct peer*);
+int peer_not_interested_received(struct peer*,ssize_t);
+int peer_piece(struct peer*);
+int peer_piece_received(struct peer*,ssize_t);
+int peer_reject(struct peer*);
+int peer_reject_received(struct peer*,ssize_t);
+int peer_request(struct peer*);
+int peer_request_received(struct peer*,ssize_t);
+int peer_unchoke(struct peer*);
+int peer_unchoke_received(struct peer*,ssize_t);
 
 #endif
index 5796a4381de31fad0a58bb2dda2b041c8da54de6..35a13a8e5b4ace9731890e28ffecbf966aee791a 100644 (file)
@@ -1,33 +1,36 @@
 #include<net.h>
 
-ssize_t net_expected(int sock, enum peer_message *type) {
-       unsigned char buf[4];
+ssize_t net_expected(struct peer *info, enum peer_message *type) {
+       unsigned char buf[5];
        ssize_t i;
 
+       if(NULL==info) { return -1; }
+
        (*type) = PEER_MESSAGE_KEEPALIVE;
-       
-       i = recv(sock,buf,5,MSG_PEEK);
-       if(-1==i) {
-               if(!((errno==EAGAIN)||(errno==EWOULDBLOCK))) {
-                       perror("recv");
-                       return -1;
-               }
 
-               // returning 0 here won't affect anything,
-               // will be treated as a keep alive (although
-               // message is still coming in, but since MSG_PEEK
-               // flag is used no data will have been consumed)
-               // and sock will be returned to epoll
+       /* 
+        * used to figure out whether 4 or 5 bytes
+        * received; 0xff is an invalid message
+        * type
+        */
+       buf[4] = 0xff;
+
+       if(net_peek(info,buf,5)<0) { return -1; }
+       if((info->in!=NULL)&&(info->in_size<4)) {
+               /*
+                * Message should at least have 4 bytes (length prefix of
+                * the following message).
+                *
+                * Returning 0 here won't affect anything,
+                * will be treated as a keep alive (although
+                * message is still coming in, but since net_peek 
+                * is used no data will have been consumed)
+                * and sock will be returned to epoll.
+                */
                return 0;
        }
        
-       /*
-        * message should at least have 4 bytes (length prefix of
-        * the following message)
-        */
-       if(i<4) { return 0; }
-
-       if(i==5) { (*type) = (uint8_t)buf[4]; }
+       if(buf[4]!=0xff) { (*type) = (uint8_t)buf[4]; }
 
        i = 0;
        for(size_t j=0;j<4;j++) {
index 2442df120c66507dbf38e751f0bb337898d94783..0802c4b77ded6081d9cb4722dcd99ccfb1191820 100644 (file)
@@ -1,64 +1,63 @@
 #include<net.h>
 
-static int send_remaining(int,struct peer*);
+static int send_remaining(struct peer*);
 
-void net_handler(int epoll_fd, int sock, struct peer *info) {
+void net_handler(int epoll_fd, struct peer *info) {
        enum peer_message type;
        ssize_t i;
 
        if(epoll_fd<0) { return; }
-       if(sock<0) { return; }
        if(NULL==info) { goto close; }
 
        if(info->out!=NULL) { goto remaining; }
 
        if(!info->handshake) { goto handshake; }
 
-       if((i = net_expected(sock,&type))<0) { goto close; }
+       if((i = net_expected(info,&type))<0) { goto close; }
 
        switch(type) {
                /* these are ordered based on enum peer_message */
                case PEER_MESSAGE_KEEPALIVE:
-                       if(peer_keepalive_received(sock,info,i)<0) { goto close; }
+                       if(peer_keepalive_received(info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_CHOKE:
-                       if(peer_choke_received(sock,info,i)<0) { goto close; }
+                       if(peer_choke_received(info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_UNCHOKE:
-                       if(peer_unchoke_received(sock,info,i)<0) { goto close; }
+                       if(peer_unchoke_received(info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_INTERESTED:
-                       if(peer_interested_received(sock,info,i)<0) { goto close; }
+                       if(peer_interested_received(info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_NOT_INTERESTED:
-                       if(peer_not_interested_received(sock,info,i)<0) { goto close; }
+                       if(peer_not_interested_received(info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_HAVE:
-                       if(peer_have_received(sock,info,i)<0) { goto close; }
+                       if(peer_have_received(info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_BITFIELD:
-                       if(peer_bitfield_received(sock,info,i)<0) { goto close; }
+                       if(peer_bitfield_received(info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_REQUEST:
-                       if(peer_request_received(sock,info,i)<0) { goto close; }
+                       if(peer_request_received(info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_PIECE:
-                       if(peer_piece_received(sock,info,i)<0) { goto close; }
+                       if(peer_piece_received(info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_CANCEL:
-                       if(peer_cancel_received(sock,info,i)<0) { goto close; }
+                       if(peer_cancel_received(info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_REJECT:
-                       if(peer_reject_received(sock,info,i)<0) { goto close; }
+                       if(peer_reject_received(info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_HASH_REQUEST:
-                       if(peer_hash_request_received(sock,info,i)<0) { goto close; }
+                       if(peer_hash_request_received(info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_HASHES:
-                       if(peer_hashes_received(sock,info,i)<0) { goto close; }
+                       if(peer_hashes_received(info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_HASH_REJECT:
-                       if(peer_hash_reject_received(sock,info,i)<0) { goto close; }
+                       if(peer_hash_reject_received(info,i)<0) { goto close; }
                        break;
                default:
                        goto close;
@@ -66,22 +65,22 @@ void net_handler(int epoll_fd, int sock, struct peer *info) {
 
        goto queue;
 remaining:
-       if(send_remaining(sock,info)<0) { goto close; }
+       if(send_remaining(info)<0) { goto close; }
        goto queue;
 handshake:
-       if(peer_handshake(sock,info)<0) { goto close; }
+       if(peer_handshake(info)<0) { goto close; }
        goto queue;
 queue:
-       if(net_queue(epoll_fd,sock,info)<0) { goto close; }
+       if(net_queue(epoll_fd,info)<0) { goto close; }
        return;
 close:
+       close(info->sock);
        peer_free(info);
-       close(sock);
        return;
 }
 
-static int send_remaining(int sock, struct peer *info) {
-       if(net_send(sock,info,info->out,info->out_size)<0) { return -1; }
+static int send_remaining(struct peer *info) {
+       if(net_send(info,info->out,info->out_size)<0) { return -1; }
 
        free(info->out);
        info->out = NULL;
index f230b909ca0d5d0fda98708f5cb5e72b4efa8081..0ee81bfedc787b685e47718b6857eb1f2624261a 100644 (file)
@@ -38,13 +38,15 @@ static void net_await_epoll_event(struct net_info *info) {
                               return;
                        }
 
+                       ((struct peer*)ev.data.ptr)->sock = conn_sock;
+
                        if(-1==epoll_ctl(info->epoll_fd,EPOLL_CTL_ADD,conn_sock,&ev)) {
                                peer_free(ev.data.ptr);
                                perror("epoll_ctl");
                                return;
                        }
                } else {
-                       net_handler(info->epoll_fd,events[n].data.fd,events[n].data.ptr);
+                       net_handler(info->epoll_fd,events[n].data.ptr);
                }
        }
 }
diff --git a/src/net/peek.c b/src/net/peek.c
new file mode 100644 (file)
index 0000000..b17db79
--- /dev/null
@@ -0,0 +1,20 @@
+#include<net.h>
+
+int net_peek(struct peer *info, void *p, size_t size) {
+       void *next;
+       next = malloc(size);
+       if(NULL==next) { return -1; }
+
+       if(net_wait(info,next,size)<0) { return -1; }
+       if(info->in!=NULL) {
+               free(next);
+               return 0;
+       }
+
+       info->in = next;
+       info->in_size = size;
+
+       memcpy(p,next,size);
+
+       return 1;
+}
index f4f08f061a639f63201a89f5d3f950c16163e695..d1f23675010d68e316aa4272528de874c789f77d 100644 (file)
@@ -1,17 +1,17 @@
 #include<net.h>
 
-int net_queue(int epoll_fd, int sock, struct peer *info) {
+int net_queue(int epoll_fd, struct peer *info) {
        struct epoll_event ev;
 
        if(epoll_fd<0) { return -1; }
-       if(sock<0) { return -1; }
        if(NULL==info) { return -1; }
+       if(info->sock<0) { return -1; }
 
        ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
        ev.data.ptr = info;
-       ev.data.fd = sock;
+       ev.data.fd = info->sock;
 
-       if(-1==epoll_ctl(epoll_fd,EPOLL_CTL_ADD,sock,&ev)) {
+       if(-1==epoll_ctl(epoll_fd,EPOLL_CTL_ADD,info->sock,&ev)) {
                perror("epoll_ctl");
                return -1;
        }
index 1a34cfe585bb3b569c402876f6611aa7f01d8bf3..ea1152a2d953aa8873fe0a2eeeacd0ffa9c1077a 100644 (file)
@@ -1,13 +1,13 @@
 #include<net.h>
 
-int net_send(int sock, struct peer *info, void *p, size_t size) {
+int net_send(struct peer *info, void *p, size_t size) {
        ssize_t i;
 
-       if(sock<0) { return -1; }
        if(NULL==info) { return -1; }
+       if(info->sock<0) { return -1; }
        if(NULL==p) { return -1; }
 
-       i = send(sock,p,size,0);
+       i = send(info->sock,p,size,0);
        if(-1==i) {
                if(!((errno==EAGAIN)||(errno==EWOULDBLOCK))) {
                        perror("send");
index 8a962768d60af043a92463d5395d469c5294559f..e400665dac0a5af8366dee970cd1b114cebfcf7d 100644 (file)
@@ -2,20 +2,20 @@
 
 static void* use_cached(struct peer*,void*,size_t*);
 
-int net_wait(int sock, struct peer *info, void *buf, size_t buf_size) {
+int net_wait(struct peer *info, void *buf, size_t buf_size) {
        void *p;
        size_t size;
        ssize_t i;
 
-       if(sock<0) { return -1; }
        if(NULL==info) { return -1; }
+       if(info->sock<0) { return -1; }
        if(NULL==buf) { return -1; }
        if(0==buf_size) { return 1; }
        
        size = buf_size;
        p = use_cached(info,buf,&size);
 
-       i = recv(sock,p,size,0);
+       i = recv(info->sock,p,size,0);
        if(-1==i) {
                if(!((errno==EAGAIN)||(errno==EWOULDBLOCK))) {
                        perror("recv");
index 0aed34f334720d1dbd242bc962eb4d90b6b25b8a..1c3e8eb194cbc1655349878ebaa89564d9cfaec7 100644 (file)
@@ -1,13 +1,47 @@
 #include<peer.h>
 
-int peer_bitfield(int sock, struct peer *info) {
+int peer_bitfield(struct peer *info) {
        return -1;
 }
 
-int peer_bitfield_received(int sock, struct peer *info, ssize_t len) {
+int peer_bitfield_init(struct peer *info, size_t pieces) {
+       info->bitfield_size = pieces/8;
+
+       info->bitfield = calloc(pieces,sizeof(uint8_t));
+       if(NULL==info->bitfield) { return -1; }
+
+       return 1;
+}
+
+int peer_bitfield_received(struct peer *info, ssize_t len) {
+       uint8_t *buf;
+
+       if((len-5)!=info->bitfield_size) { return -1; }
+
+       buf = malloc(len);
+       if(NULL==buf) { return -1; }
+
+       if(net_wait(info,buf,len)<0) { goto fail; }
+       if(info->in!=NULL) { goto clean; }
+
+       for(size_t i=5;i<len;i++) {
+               info->bitfield[i] = buf[i];
+       }
+
+       return 1;
+fail:
+       free(buf);
        return -1;
+clean:
+       free(buf);
+       return 0;
 }
 
 int peer_bitfield_toggle(struct peer *info, size_t index) {
-       return -1;
+       size_t byte_index = index/8;
+
+       index %= 8;
+       info->bitfield[byte_index] = info->bitfield[byte_index] ^ (1<<index); 
+
+       return 1;
 }
index e6f641a11662b5beccbea0d23ef51a8a0b3edb70..431f17e06c2ab5f99c9fbf181dfaad0a9363c639 100644 (file)
@@ -1,9 +1,9 @@
 #include<peer.h>
 
-int peer_cancel(int sock, struct peer *info) {
+int peer_cancel(struct peer *info) {
        return -1;
 }
 
-int peer_cancel_received(int sock, struct peer *info, ssize_t len) {
+int peer_cancel_received(struct peer *info, ssize_t len) {
        return -1;
 }
index 660635fbc681ca70c64d0a99e5a9cb6faee727b4..51e06738d39bb77a06ff223ec85b797d22754e63 100644 (file)
@@ -1,18 +1,18 @@
 #include<peer.h>
 
-int peer_choke(int sock, struct peer *info) {
+int peer_choke(struct peer *info) {
        unsigned char buf[5] = "\x00\x00\x00\x01";
        buf[4] = PEER_MESSAGE_CHOKE;
 
-       return net_send(sock,info,buf,5);
+       return net_send(info,buf,5);
 }
 
-int peer_choke_received(int sock, struct peer *info, ssize_t len) {
+int peer_choke_received(struct peer *info, ssize_t len) {
        unsigned char buf[5];
 
        if(5!=len) { return -1; }
 
-       if(net_wait(sock,info,buf,5)<0) { return -1; }
+       if(net_wait(info,buf,5)<0) { return -1; }
        if(info->in!=NULL) { return 0; }
 
        info->choked = 1;
@@ -20,19 +20,19 @@ int peer_choke_received(int sock, struct peer *info, ssize_t len) {
        return 1;
 }
 
-int peer_unchoke(int sock, struct peer *info) {
+int peer_unchoke(struct peer *info) {
        unsigned char buf[5] = "\x00\x00\x00\x01";
        buf[4] = PEER_MESSAGE_UNCHOKE;
 
-       return net_send(sock,info,buf,5);
+       return net_send(info,buf,5);
 }
 
-int peer_unchoke_received(int sock, struct peer *info, ssize_t len) {
+int peer_unchoke_received(struct peer *info, ssize_t len) {
        unsigned char buf[5];
 
        if(5!=len) { return -1; }
 
-       if(net_wait(sock,info,buf,5)<0) { return -1; }
+       if(net_wait(info,buf,5)<0) { return -1; }
        if(info->in!=NULL) { return 0; }
 
        info->choked = 0;
index 73967bcdcf02a8738e1c01244eb7922a2a17eae8..59aaff97d0717e228594b40c5706de2779831eec 100644 (file)
@@ -5,14 +5,12 @@
 
 #define HEADER_LENGTH HANDSHAKE_STRING_LENGTH+PEER_INFOHASH_SIZE+PEER_PEER_ID_SIZE
 
-static int peer_handshake_receive(int,struct peer*);
+static int peer_handshake_receive(struct peer*);
 
-int peer_handshake(int sock, struct peer *info) {
+int peer_handshake(struct peer *info) {
        char header[HEADER_LENGTH] = HANDSHAKE_STRING;
        
-       if(sock<0) { return -1; }
-
-       if(peer_handshake_receive(sock,info)<0) { return -1; }
+       if(peer_handshake_receive(info)<0) { return -1; }
        if(!info->handshake) { return 0; }
 
        // copy infohash to buffer
@@ -27,17 +25,17 @@ int peer_handshake(int sock, struct peer *info) {
                info->peer_id,
                PEER_PEER_ID_SIZE);
 
-       if(net_send(sock,info,header,HEADER_LENGTH)<0) { return -1; }
+       if(net_send(info,header,HEADER_LENGTH)<0) { return -1; }
 
        return 1;
 }
 
-static int peer_handshake_receive(int sock, struct peer *info) {
+static int peer_handshake_receive(struct peer *info) {
        char header[HEADER_LENGTH];
        struct torrent *p;
        int i;
 
-       if((i = net_wait(sock,info,header,HEADER_LENGTH))<=0) {
+       if((i = net_wait(info,header,HEADER_LENGTH))<=0) {
                if(i<0) { return -1; }
                return 0;
        }
index efccb0d26e9b02ecb7147d880870cd8400de23f1..88574d30ca0ddb7c45b8c2caf37c060725293420 100644 (file)
@@ -1,17 +1,17 @@
 #include<peer.h>
 
-int peer_hash_request(int sock, struct peer *info) {
+int peer_hash_request(struct peer *info) {
        return -1;
 }
 
-int peer_hash_request_received(int sock, struct peer *info, ssize_t len) {
+int peer_hash_request_received(struct peer *info, ssize_t len) {
        return -1;
 }
 
-int peer_hash_reject(int sock, struct peer *info) {
+int peer_hash_reject(struct peer *info) {
        return -1;
 }
 
-int peer_hash_reject_received(int sock, struct peer *info, ssize_t len) {
+int peer_hash_reject_received(struct peer *info, ssize_t len) {
        return -1;
 }
index e4b20cfa175140245795a425b89e4b516c97ea9c..750bba57a8f2b489714aa81fe80e4a775c174fb8 100644 (file)
@@ -1,9 +1,9 @@
 #include<peer.h>
 
-int peer_hashes(int sock, struct peer *info) {
+int peer_hashes(struct peer *info) {
        return -1;
 }
 
-int peer_hashes_received(int sock, struct peer *info, ssize_t len) {
+int peer_hashes_received(struct peer *info, ssize_t len) {
        return -1;
 }
index d0a183bf5db2782ca12cc3d8b94d8950c3634821..d4bca893985829444208671eaf558d6c6976482f 100644 (file)
@@ -1,6 +1,6 @@
 #include<peer.h>
 
-int peer_have(int sock, struct peer *info, size_t index) {
+int peer_have(struct peer *info, size_t index) {
        unsigned char buf[9] = "\x00\x00\x00\x05";
        buf[4] = PEER_MESSAGE_HAVE;
 
@@ -9,16 +9,16 @@ int peer_have(int sock, struct peer *info, size_t index) {
                index >>= 8;
        }
 
-       return net_send(sock,info,buf,9);
+       return net_send(info,buf,9);
 }
 
-int peer_have_received(int sock, struct peer *info, ssize_t len) {
+int peer_have_received(struct peer *info, ssize_t len) {
        unsigned char buf[9];
        size_t index;
 
        if(9!=len) { return -1; }
 
-       if(net_wait(sock,info,buf,9)<0) { return -1; }
+       if(net_wait(info,buf,9)<0) { return -1; }
        if(info->in!=NULL) { return 0; }
 
        index = 0;
index b51ea38c546a48cf12b57fa9877ddb359bb6c5b3..ef82f9ad8bce8f2890ec61142a64be024a7fd297 100644 (file)
@@ -10,8 +10,12 @@ int peer_init(struct peer **p) {
        (*p)->choked = 0;
        (*p)->interested = 0;
 
-       memset((*p)->infohash,0,PEER_INFOHASH_SIZE);
        memset((*p)->peer_id,0,PEER_PEER_ID_SIZE);
+       
+       memset((*p)->infohash,0,PEER_INFOHASH_SIZE);
+
+       (*p)->bitfield = NULL;
+       (*p)->bitfield_size = 0;
 
        (*p)->out = NULL;
        (*p)->out_size = 0;
index b9b605bfb1a10be71c6dbba3bc9465e82e7459a0..6fe1c8aa18a5e93d5d54da6722c1cb46159f5968 100644 (file)
@@ -1,18 +1,18 @@
 #include<peer.h>
 
-int peer_interested(int sock, struct peer *info) {
+int peer_interested(struct peer *info) {
        unsigned char buf[5] = "\x00\x00\x00\x01";
        buf[4] = PEER_MESSAGE_INTERESTED;
 
-       return net_send(sock,info,buf,5);
+       return net_send(info,buf,5);
 }
 
-int peer_interested_received(int sock, struct peer *info, ssize_t len) {
+int peer_interested_received(struct peer *info, ssize_t len) {
        unsigned char buf[5];
 
        if(5!=len) { return -1; }
 
-       if(net_wait(sock,info,buf,5)<0) { return -1; }
+       if(net_wait(info,buf,5)<0) { return -1; }
        if(info->in!=NULL) { return 0; }
 
        info->interested = 1;
@@ -20,19 +20,19 @@ int peer_interested_received(int sock, struct peer *info, ssize_t len) {
        return 1;
 }
 
-int peer_not_interested(int sock, struct peer *info) {
+int peer_not_interested(struct peer *info) {
        unsigned char buf[5] = "\x00\x00\x00\x01";
        buf[4] = PEER_MESSAGE_NOT_INTERESTED;
 
-       return net_send(sock,info,buf,5);
+       return net_send(info,buf,5);
 }
 
-int peer_not_interested_received(int sock, struct peer *info, ssize_t len) {
+int peer_not_interested_received(struct peer *info, ssize_t len) {
        unsigned char buf[5];
 
        if(5!=len) { return -1; }
 
-       if(net_wait(sock,info,buf,5)<0) { return -1; }
+       if(net_wait(info,buf,5)<0) { return -1; }
        if(info->in!=NULL) { return 0; }
 
        info->interested = 0;
index 0df83fa7bec218cf0d9a3e27e8e5cf006ce37e90..e0ac2e93ff2e0f25b49bfa0e23095865a0f67700 100644 (file)
@@ -1,17 +1,17 @@
 #include<peer.h>
 
-int peer_keepalive(int sock, struct peer *info) {
+int peer_keepalive(struct peer *info) {
        unsigned char buf[4] = "\x00\x00\x00\x00";
 
-       return net_send(sock,info,buf,4);
+       return net_send(info,buf,4);
 }
 
-int peer_keepalive_received(int sock, struct peer *info, ssize_t len) {
+int peer_keepalive_received(struct peer *info, ssize_t len) {
        unsigned char buf[4];
 
        if(4!=len) { return -1; }
 
-       if(net_wait(sock,info,buf,4)<0) { return -1; }
+       if(net_wait(info,buf,4)<0) { return -1; }
        if(info->in!=NULL) { return 0; }
 
        return 1;
index 8715ea3b27760715eb2237bda9e48525a5b5a7b9..0e4a1a227417703ed74e5dc0fef83b88c2f3e6d6 100644 (file)
@@ -1,9 +1,9 @@
 #include<peer.h>
 
-int peer_piece(int sock, struct peer *info) {
+int peer_piece(struct peer *info) {
        return -1;
 }
 
-int peer_piece_received(int sock, struct peer *info, ssize_t len) {
+int peer_piece_received(struct peer *info, ssize_t len) {
        return -1;
 }
index 8ee16110034a59cf6235a6c046819ea81e61ea9d..a256107a42b49534dc72767a0bb599322ad3edd5 100644 (file)
@@ -1,9 +1,9 @@
 #include<peer.h>
 
-int peer_reject(int sock, struct peer *info) {
+int peer_reject(struct peer *info) {
        return -1;
 }
 
-int peer_reject_received(int sock, struct peer *info, ssize_t len) {
+int peer_reject_received(struct peer *info, ssize_t len) {
        return -1;
 }
index 1a71142e54ac4d6ac05108ee210a710c27af6f72..82a527b5a88fb85a523db87374af209796fd30e4 100644 (file)
@@ -1,9 +1,9 @@
 #include<peer.h>
 
-int peer_request(int sock, struct peer *info) {
+int peer_request(struct peer *info) {
        return -1;
 }
 
-int peer_request_received(int sock, struct peer *info, ssize_t len) {
+int peer_request_received(struct peer *info, ssize_t len) {
        return -1;
 }
index f3557ed60aadfe2d36d01d1f5332d04cd4756e08..d91570fd252e1cb92c477e6d1847141872c0d883 100644 (file)
@@ -8,6 +8,7 @@ static void net_cache_basic_test();
 static void net_expected_basic_test();
 static void net_handler_basic_test();
 static void net_handler_invalid_message_test();
+static void net_peek_basic_test();
 static void net_queue_basic_test();
 static void net_send_basic_test();
 static void net_wait_basic_test();
@@ -41,6 +42,7 @@ int main() {
        net_expected_basic_test();
        net_handler_basic_test();
        net_handler_invalid_message_test();
+       net_peek_basic_test();
        net_queue_basic_test();
        net_send_basic_test();
        net_wait_basic_test();
@@ -98,22 +100,23 @@ static void net_expected_basic_test() {
        enum peer_message type;
        unsigned char buf[10];
 
-       assert(1==peer_init(&info));
-
        setup_sockets();
 
+       assert(1==peer_init(&info));
+       info->sock = read_sock;
+
        /* verify messages with <4 bytes returns keepalive */
        unsigned char expected1[4] = "\x00\x00\x00\x00";
        assert(3==write(write_sock,expected1,3));
 
-       assert(0==net_expected(read_sock,&type));
+       assert(0==net_expected(info,&type));
        assert(PEER_MESSAGE_KEEPALIVE==type);
        assert(3==read(read_sock,buf,sizeof(buf)));
 
        /* verify keepalive returns as expected */
        assert(4==write(write_sock,expected1,4));
 
-       assert(4==net_expected(read_sock,&type));
+       assert(4==net_expected(info,&type));
        assert(PEER_MESSAGE_KEEPALIVE==type);
        assert(4==read(read_sock,buf,sizeof(buf)));
 
@@ -153,7 +156,7 @@ static void net_expected_basic_test() {
 #define NET_HANDLER_HELPER(message,type) { \
        assert(sizeof(message)==write(write_sock,message,sizeof(message))); \
 \
-       net_handler(epoll_fd,read_sock,info); \
+       net_handler(epoll_fd,info); \
        assert(0==epoll_ctl(epoll_fd,EPOLL_CTL_DEL,read_sock,NULL)); \
        assert(sizeof(message)==read(read_sock,buf,sizeof(buf))); \
 \
@@ -164,19 +167,20 @@ static void net_handler_invalid_message_test() {
        struct peer *info;
        int epoll_fd;
        
+       setup_sockets();
+
        assert(1==peer_init(&info));
        info->handshake = 1;
+       info->sock = read_sock;
 
        epoll_fd = epoll_create1(0);
        assert(epoll_fd!=-1);
        
-       setup_sockets();
-       
        memset(message_count, 0, sizeof(message_count));
 
        unsigned char invalid_message_type[5] = "\x00\x00\x00\x01\xff";
        assert(5==write(write_sock,invalid_message_type,5));
-       net_handler(epoll_fd,read_sock,info);
+       net_handler(epoll_fd,info);
 
        for(size_t i=0;i<25;i++) {
                assert(0==message_count[i]);
@@ -190,16 +194,17 @@ static void net_handler_basic_test() {
        unsigned char buf[10];
        int epoll_fd;
 
+       setup_sockets();
+
        memset(message_count, 0, sizeof(message_count));
 
        assert(1==peer_init(&info));
        info->handshake = 1;
+       info->sock = read_sock;
 
        epoll_fd = epoll_create1(0);
        assert(epoll_fd!=-1);
 
-       setup_sockets();
-
        /* all peer_message enums indexes are +1 in message_count */
        unsigned char keepalive_message[4] = "\x00\x00\x00\x00";
        NET_HANDLER_HELPER(keepalive_message,PEER_MESSAGE_KEEPALIVE);
@@ -248,23 +253,26 @@ static void net_handler_basic_test() {
        close(write_sock);
 }
 
+static void net_peek_basic_test() {
+       struct peer *info;
+
+}
+
 static void net_queue_basic_test() {
        int epoll_fd;
        int sock;
        struct peer *info;
 
        assert(1==peer_init(&info));
+       info->sock = 0;
        
        epoll_fd = epoll_create1(0);
        assert(epoll_fd!=-1);
 
-       sock = 0;
-
-       assert(-1==net_queue(-1,-1,NULL));
-       assert(-1==net_queue(epoll_fd,-1,NULL));
-       assert(-1==net_queue(epoll_fd,sock,NULL));
+       assert(-1==net_queue(-1,NULL));
+       assert(-1==net_queue(epoll_fd,NULL));
 
-       assert(1==net_queue(epoll_fd,sock,info));
+       assert(1==net_queue(epoll_fd,info));
 
        peer_free(info);
        close(epoll_fd);
@@ -281,17 +289,18 @@ static void net_send_basic_test() {
        memset(expected,0,100);
 
        assert(1==peer_init(&info));
+       info->sock = write_sock;
 
        assert(-1==net_send(-1,NULL,NULL,0));
-       assert(-1==net_send(write_sock,info,NULL,0));
+       assert(-1==net_send(info,NULL,0));
        
-       assert(1==net_send(write_sock,info,buf,0));
+       assert(1==net_send(info,buf,0));
        assert(-1==read(read_sock,expected,100));
        
-       assert(1==net_send(write_sock,info,buf,50));
+       assert(1==net_send(info,buf,50));
        assert(50==read(read_sock,expected,100));
        
-       assert(1==net_send(write_sock,info,buf,100));
+       assert(1==net_send(info,buf,100));
        assert(100==read(read_sock,expected,100));
 
        close(read_sock);
@@ -312,46 +321,47 @@ static void net_wait_basic_test() {
        memset(expected,0,100);
 
        assert(1==peer_init(&info));
+       info->sock = read_sock;
 
-       assert(-1==net_wait(-1,NULL,NULL,0));
-       assert(-1==net_wait(read_sock,NULL,NULL,0));
-       assert(-1==net_wait(read_sock,info,NULL,0));
-       assert(1==net_wait(read_sock,info,expected,0));
+       assert(-1==net_wait(NULL,NULL,0));
+       assert(-1==net_wait(NULL,NULL,0));
+       assert(-1==net_wait(info,NULL,0));
+       assert(1==net_wait(info,expected,0));
        
-       assert(0==net_wait(read_sock,info,buf,100));
+       assert(0==net_wait(info,buf,100));
 
        assert(50==write(write_sock,expected,50));
        
-       assert(0==net_wait(read_sock,info,buf,100));
+       assert(0==net_wait(info,buf,100));
        assert(info->in!=NULL);
        assert(info->in_size==50);
        assert(memcmp(buf,expected,100)!=0);
        
        assert(25==write(write_sock,expected,25));
-       assert(0==net_wait(read_sock,info,buf,100));
+       assert(0==net_wait(info,buf,100));
        assert(info->in!=NULL);
        assert(info->in_size==75);
        assert(memcmp(buf,expected,100)!=0);
        
        assert(25==write(write_sock,expected,25));
 
-       assert(1==net_wait(read_sock,info,buf,100));
+       assert(1==net_wait(info,buf,100));
        assert(info->in==NULL);
        assert(memcmp(buf,expected,100)==0);
 
        i = 0;
        info->in_size = 0;
        while(i<99) {
-               assert(0==net_wait(read_sock,info,buf,100));
+               assert(0==net_wait(info,buf,100));
                increment = rand()%2;
                i += increment;
                assert(((increment>0)?1:0)==write(write_sock,expected,increment));
-               assert(0==net_wait(read_sock,info,buf,100));
+               assert(0==net_wait(info,buf,100));
                assert(info->in_size==i);
        }
        
        assert(1==write(write_sock,expected,1));
-       assert(1==net_wait(read_sock,info,buf,100));
+       assert(1==net_wait(info,buf,100));
        assert(info->in==NULL);
        assert(memcmp(buf,expected,100)==0);