From: alex Date: Tue, 29 Mar 2022 23:20:47 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=c34acb4839692c7ed00c61424d553afbe1513907;p=seeder ... --- diff --git a/inc/peer.h b/inc/peer.h index ccc9ee4..212e424 100644 --- a/inc/peer.h +++ b/inc/peer.h @@ -46,21 +46,36 @@ enum peer_message { #include 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 diff --git a/src/net/expected.c b/src/net/expected.c index 87c5518..5796a43 100644 --- a/src/net/expected.c +++ b/src/net/expected.c @@ -35,5 +35,5 @@ ssize_t net_expected(int sock, enum peer_message *type) { i += buf[j]; } - return i; + return i+4; } diff --git a/src/net/handler.c b/src/net/handler.c index 71c25fe..2442df1 100644 --- a/src/net/handler.c +++ b/src/net/handler.c @@ -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; diff --git a/src/peer/bitfield.c b/src/peer/bitfield.c index 3df3b54..0aed34f 100644 --- a/src/peer/bitfield.c +++ b/src/peer/bitfield.c @@ -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; +} diff --git a/src/peer/cancel.c b/src/peer/cancel.c index bbb3f4a..e6f641a 100644 --- a/src/peer/cancel.c +++ b/src/peer/cancel.c @@ -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; +} diff --git a/src/peer/choke.c b/src/peer/choke.c index 0efb9d6..660635f 100644 --- a/src/peer/choke.c +++ b/src/peer/choke.c @@ -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; +} diff --git a/src/peer/hash.c b/src/peer/hash.c index fe8d77e..efccb0d 100644 --- a/src/peer/hash.c +++ b/src/peer/hash.c @@ -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; +} diff --git a/src/peer/hashes.c b/src/peer/hashes.c index 323a625..e4b20cf 100644 --- a/src/peer/hashes.c +++ b/src/peer/hashes.c @@ -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; +} diff --git a/src/peer/have.c b/src/peer/have.c index e27f818..d0a183b 100644 --- a/src/peer/have.c +++ b/src/peer/have.c @@ -1,5 +1,33 @@ #include -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; } diff --git a/src/peer/interest.c b/src/peer/interest.c index b41e06f..b9b605b 100644 --- a/src/peer/interest.c +++ b/src/peer/interest.c @@ -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; +} diff --git a/src/peer/keepalive.c b/src/peer/keepalive.c index 922e979..0df83fa 100644 --- a/src/peer/keepalive.c +++ b/src/peer/keepalive.c @@ -1,5 +1,18 @@ #include 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; } diff --git a/src/peer/piece.c b/src/peer/piece.c index 92d60ad..8715ea3 100644 --- a/src/peer/piece.c +++ b/src/peer/piece.c @@ -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; +} diff --git a/src/peer/reject.c b/src/peer/reject.c index f3e0153..8ee1611 100644 --- a/src/peer/reject.c +++ b/src/peer/reject.c @@ -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; +} diff --git a/src/peer/request.c b/src/peer/request.c index 1a42c80..1a71142 100644 --- a/src/peer/request.c +++ b/src/peer/request.c @@ -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; +} diff --git a/test/unit/net.tests.c b/test/unit/net.tests.c index 766c20e..f3557ed 100644 --- a/test/unit/net.tests.c +++ b/test/unit/net.tests.c @@ -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)));