...
authoralex <[email protected]>
Tue, 29 Mar 2022 23:20:47 +0000 (16:20 -0700)
committeralex <[email protected]>
Tue, 29 Mar 2022 23:20:47 +0000 (16:20 -0700)
15 files changed:
inc/peer.h
src/net/expected.c
src/net/handler.c
src/peer/bitfield.c
src/peer/cancel.c
src/peer/choke.c
src/peer/hash.c
src/peer/hashes.c
src/peer/have.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 ccc9ee48bcc3949d3ec129b75167a6be1828cc66..212e42411feb45243cafb4b08f77fb7b28387085 100644 (file)
@@ -46,21 +46,36 @@ enum peer_message {
 #include<session.h>
 
 int peer_bitfield(int,struct peer*);
+int peer_bitfield_received(int,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);
 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_have(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_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);
 
 #endif
index 87c55181992680774968533a669ae91af708865f..5796a4381de31fad0a58bb2dda2b041c8da54de6 100644 (file)
@@ -35,5 +35,5 @@ ssize_t net_expected(int sock, enum peer_message *type) {
                i += buf[j];
        }
 
-       return i;
+       return i+4;
 }
index 71c25fe580c8c770a696273e22bdd9bdfb4170e4..2442df120c66507dbf38e751f0bb337898d94783 100644 (file)
@@ -2,10 +2,9 @@
 
 static int send_remaining(int,struct peer*);
 
-
 void net_handler(int epoll_fd, int sock, struct peer *info) {
        enum peer_message type;
-       uint32_t i;
+       ssize_t i;
 
        if(epoll_fd<0) { return; }
        if(sock<0) { return; }
@@ -20,46 +19,46 @@ void net_handler(int epoll_fd, int sock, struct peer *info) {
        switch(type) {
                /* these are ordered based on enum peer_message */
                case PEER_MESSAGE_KEEPALIVE:
-                       if(peer_keepalive(sock,info)<0) { goto close; }
+                       if(peer_keepalive_received(sock,info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_CHOKE:
-                       if(peer_choke(sock,info)<0) { goto close; }
+                       if(peer_choke_received(sock,info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_UNCHOKE:
-                       if(peer_unchoke(sock,info)<0) { goto close; }
+                       if(peer_unchoke_received(sock,info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_INTERESTED:
-                       if(peer_interested(sock,info)<0) { goto close; }
+                       if(peer_interested_received(sock,info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_NOT_INTERESTED:
-                       if(peer_not_interested(sock,info)<0) { goto close; }
+                       if(peer_not_interested_received(sock,info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_HAVE:
-                       if(peer_have(sock,info)<0) { goto close; }
+                       if(peer_have_received(sock,info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_BITFIELD:
-                       if(peer_bitfield(sock,info)<0) { goto close; }
+                       if(peer_bitfield_received(sock,info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_REQUEST:
-                       if(peer_request(sock,info)<0) { goto close; }
+                       if(peer_request_received(sock,info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_PIECE:
-                       if(peer_piece(sock,info)<0) { goto close; }
+                       if(peer_piece_received(sock,info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_CANCEL:
-                       if(peer_cancel(sock,info)<0) { goto close; }
+                       if(peer_cancel_received(sock,info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_REJECT:
-                       if(peer_reject(sock,info)<0) { goto close; }
+                       if(peer_reject_received(sock,info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_HASH_REQUEST:
-                       if(peer_hash_request(sock,info)<0) { goto close; }
+                       if(peer_hash_request_received(sock,info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_HASHES:
-                       if(peer_hashes(sock,info)<0) { goto close; }
+                       if(peer_hashes_received(sock,info,i)<0) { goto close; }
                        break;
                case PEER_MESSAGE_HASH_REJECT:
-                       if(peer_hash_reject(sock,info)<0) { goto close; }
+                       if(peer_hash_reject_received(sock,info,i)<0) { goto close; }
                        break;
                default:
                        goto close;
index 3df3b547054dcf14b3fb2855d9538cbef28629a1..0aed34f334720d1dbd242bc962eb4d90b6b25b8a 100644 (file)
@@ -3,3 +3,11 @@
 int peer_bitfield(int sock, struct peer *info) {
        return -1;
 }
+
+int peer_bitfield_received(int sock, struct peer *info, ssize_t len) {
+       return -1;
+}
+
+int peer_bitfield_toggle(struct peer *info, size_t index) {
+       return -1;
+}
index bbb3f4aeb3b3ec063be83991b3a7e7d6b468a379..e6f641a11662b5beccbea0d23ef51a8a0b3edb70 100644 (file)
@@ -3,3 +3,7 @@
 int peer_cancel(int sock, struct peer *info) {
        return -1;
 }
+
+int peer_cancel_received(int sock, struct peer *info, ssize_t len) {
+       return -1;
+}
index 0efb9d6ca0a0347d3be5ec5df526904962d47ffb..660635fbc681ca70c64d0a99e5a9cb6faee727b4 100644 (file)
@@ -7,9 +7,35 @@ int peer_choke(int sock, struct peer *info) {
        return net_send(sock,info,buf,5);
 }
 
+int peer_choke_received(int sock, 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(info->in!=NULL) { return 0; }
+
+       info->choked = 1;
+
+       return 1;
+}
+
 int peer_unchoke(int sock, struct peer *info) {
        unsigned char buf[5] = "\x00\x00\x00\x01";
        buf[4] = PEER_MESSAGE_UNCHOKE;
 
        return net_send(sock,info,buf,5);
 }
+
+int peer_unchoke_received(int sock, 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(info->in!=NULL) { return 0; }
+
+       info->choked = 0;
+
+       return 1;
+}
index fe8d77ee6ebfb3922f92d872d03e999947b41a72..efccb0d26e9b02ecb7147d880870cd8400de23f1 100644 (file)
@@ -4,6 +4,14 @@ int peer_hash_request(int sock, struct peer *info) {
        return -1;
 }
 
+int peer_hash_request_received(int sock, struct peer *info, ssize_t len) {
+       return -1;
+}
+
 int peer_hash_reject(int sock, struct peer *info) {
        return -1;
 }
+
+int peer_hash_reject_received(int sock, struct peer *info, ssize_t len) {
+       return -1;
+}
index 323a625a366138ef0b5861b5c5169f211bb59a5f..e4b20cfa175140245795a425b89e4b516c97ea9c 100644 (file)
@@ -3,3 +3,7 @@
 int peer_hashes(int sock, struct peer *info) {
        return -1;
 }
+
+int peer_hashes_received(int sock, struct peer *info, ssize_t len) {
+       return -1;
+}
index e27f81886ec476ae01a169a68ee73ea051fcf144..d0a183bf5db2782ca12cc3d8b94d8950c3634821 100644 (file)
@@ -1,5 +1,33 @@
 #include<peer.h>
 
-int peer_have(int sock, struct peer *info) {
-       return -1;
+int peer_have(int sock, struct peer *info, size_t index) {
+       unsigned char buf[9] = "\x00\x00\x00\x05";
+       buf[4] = PEER_MESSAGE_HAVE;
+
+       for(size_t i=0;i<4;i++) {
+               buf[8-i] = index & 0xff;
+               index >>= 8;
+       }
+
+       return net_send(sock,info,buf,9);
+}
+
+int peer_have_received(int sock, 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(info->in!=NULL) { return 0; }
+
+       index = 0;
+       for(size_t i=5;i<9;i++) {
+               index <<= 8;
+               index += buf[i];
+       }
+
+       if(peer_bitfield_toggle(info,index)<0) { return -1; }
+
+       return 1;
 }
index b41e06f7cf475af7d1503094b448debb26239196..b9b605bfb1a10be71c6dbba3bc9465e82e7459a0 100644 (file)
@@ -7,9 +7,35 @@ int peer_interested(int sock, struct peer *info) {
        return net_send(sock,info,buf,5);
 }
 
+int peer_interested_received(int sock, 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(info->in!=NULL) { return 0; }
+
+       info->interested = 1;
+
+       return 1;
+}
+
 int peer_not_interested(int sock, struct peer *info) {
        unsigned char buf[5] = "\x00\x00\x00\x01";
        buf[4] = PEER_MESSAGE_NOT_INTERESTED;
 
        return net_send(sock,info,buf,5);
 }
+
+int peer_not_interested_received(int sock, 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(info->in!=NULL) { return 0; }
+
+       info->interested = 0;
+
+       return 1;
+}
index 922e97953425bed3741dcd7032bf244a2caa809c..0df83fa7bec218cf0d9a3e27e8e5cf006ce37e90 100644 (file)
@@ -1,5 +1,18 @@
 #include<peer.h>
 
 int peer_keepalive(int sock, struct peer *info) {
-       return -1;
+       unsigned char buf[4] = "\x00\x00\x00\x00";
+
+       return net_send(sock,info,buf,4);
+}
+
+int peer_keepalive_received(int sock, 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(info->in!=NULL) { return 0; }
+
+       return 1;
 }
index 92d60adb2f9747a5ed9f621c5b9610a1924baf74..8715ea3b27760715eb2237bda9e48525a5b5a7b9 100644 (file)
@@ -3,3 +3,7 @@
 int peer_piece(int sock, struct peer *info) {
        return -1;
 }
+
+int peer_piece_received(int sock, struct peer *info, ssize_t len) {
+       return -1;
+}
index f3e0153231c529aab384f8aad1334b166189907d..8ee16110034a59cf6235a6c046819ea81e61ea9d 100644 (file)
@@ -3,3 +3,7 @@
 int peer_reject(int sock, struct peer *info) {
        return -1;
 }
+
+int peer_reject_received(int sock, struct peer *info, ssize_t len) {
+       return -1;
+}
index 1a42c804bd2ad3e24f432ed6d4352440d74b6867..1a71142e54ac4d6ac05108ee210a710c27af6f72 100644 (file)
@@ -3,3 +3,7 @@
 int peer_request(int sock, struct peer *info) {
        return -1;
 }
+
+int peer_request_received(int sock, struct peer *info, ssize_t len) {
+       return -1;
+}
index 766c20eb1ae0b159684f24f98fb5a9e4a0cb88d2..f3557ed60aadfe2d36d01d1f5332d04cd4756e08 100644 (file)
@@ -14,21 +14,21 @@ static void net_wait_basic_test();
 
 /* dummy functions (and array) used to test net_handler */
 int message_count[25];
-int peer_bitfield(int sock,struct peer *info) { message_count[PEER_MESSAGE_BITFIELD+1]++; return 0; }
-int peer_cancel(int sock,struct peer *info) { message_count[PEER_MESSAGE_CANCEL+1]++; return 0; }
-int peer_choke(int sock,struct peer *info) { message_count[PEER_MESSAGE_CHOKE+1]++; return 0; }
+int peer_bitfield_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_BITFIELD+1]++; return 0; }
+int peer_cancel_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_CANCEL+1]++; return 0; }
+int peer_choke_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_CHOKE+1]++; return 0; }
 int peer_handshake(int sock,struct peer *info) { return 0; }
-int peer_hash_reject(int sock,struct peer *info) { message_count[PEER_MESSAGE_HASH_REJECT+1]++; return 0; }
-int peer_hash_request(int sock,struct peer *info) { message_count[PEER_MESSAGE_HASH_REQUEST+1]++; return 0; }
-int peer_hashes(int sock,struct peer *info) { message_count[PEER_MESSAGE_HASHES+1]++; return 0; }
-int peer_have(int sock,struct peer *info) { message_count[PEER_MESSAGE_HAVE+1]++; return 0; }
-int peer_interested(int sock,struct peer *info) { message_count[PEER_MESSAGE_INTERESTED+1]++; return 0; }
-int peer_keepalive(int sock,struct peer *info) { message_count[PEER_MESSAGE_KEEPALIVE+1]++; return 0; }
-int peer_not_interested(int sock,struct peer *info) { message_count[PEER_MESSAGE_NOT_INTERESTED+1]++; return 0; }
-int peer_piece(int sock,struct peer *info) { message_count[PEER_MESSAGE_PIECE+1]++; return 0; }
-int peer_reject(int sock,struct peer *info) { message_count[PEER_MESSAGE_REJECT+1]++; return 0; }
-int peer_request(int sock,struct peer *info) { message_count[PEER_MESSAGE_REQUEST+1]++; return 0; }
-int peer_unchoke(int sock,struct peer *info) { message_count[PEER_MESSAGE_UNCHOKE+1]++; return 0; }
+int peer_hash_reject_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_HASH_REJECT+1]++; return 0; }
+int peer_hash_request_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_HASH_REQUEST+1]++; return 0; }
+int peer_hashes_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_HASHES+1]++; return 0; }
+int peer_have_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_HAVE+1]++; return 0; }
+int peer_interested_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_INTERESTED+1]++; return 0; }
+int peer_keepalive_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_KEEPALIVE+1]++; return 0; }
+int peer_not_interested_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_NOT_INTERESTED+1]++; return 0; }
+int peer_piece_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_PIECE+1]++; return 0; }
+int peer_reject_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_REJECT+1]++; return 0; }
+int peer_request_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_REQUEST+1]++; return 0; }
+int peer_unchoke_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_UNCHOKE+1]++; return 0; }
 /* end dummy functions */
 
 static int read_sock;
@@ -113,7 +113,7 @@ static void net_expected_basic_test() {
        /* verify keepalive returns as expected */
        assert(4==write(write_sock,expected1,4));
 
-       assert(0==net_expected(read_sock,&type));
+       assert(4==net_expected(read_sock,&type));
        assert(PEER_MESSAGE_KEEPALIVE==type);
        assert(4==read(read_sock,buf,sizeof(buf)));
 
@@ -121,7 +121,7 @@ static void net_expected_basic_test() {
        expected2[4] = PEER_MESSAGE_CHOKE;
        assert(5==write(write_sock,expected2,5));
 
-       assert(256==net_expected(read_sock,&type));
+       assert(260==net_expected(read_sock,&type));
        assert(PEER_MESSAGE_CHOKE==type);
        assert(5==read(read_sock,buf,sizeof(buf)));
 
@@ -129,7 +129,7 @@ static void net_expected_basic_test() {
        expected3[4] = PEER_MESSAGE_HASHES;
        assert(5==write(write_sock,expected3,5));
 
-       assert(1048832==net_expected(read_sock,&type));
+       assert(1048836==net_expected(read_sock,&type));
        assert(PEER_MESSAGE_HASHES==type);
        assert(5==read(read_sock,buf,sizeof(buf)));
 
@@ -137,7 +137,7 @@ static void net_expected_basic_test() {
        expected4[4] = PEER_MESSAGE_UNCHOKE;
        assert(5==write(write_sock,expected4,5));
 
-       assert(4027580672==net_expected(read_sock,&type));
+       assert(4027580676==net_expected(read_sock,&type));
        assert(PEER_MESSAGE_UNCHOKE==type);
        assert(5==read(read_sock,buf,sizeof(buf)));