From beb575f69ddbe89c32e3432e58bb34e31ef6fda2 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 23 Jul 2022 10:15:02 -0700 Subject: [PATCH] ... --- Makefile.am | 2 +- inc/peer.h | 4 +-- inc/session.h | 3 ++- src/peer/handshake.c | 15 ++++------- src/peer/id.c | 7 +++++ src/session.c | 2 ++ test/integration/Makefile.am | 9 ++++++- test/integration/qbittorrent.tests.c | 40 +++++++++++++++++++++++----- 8 files changed, 61 insertions(+), 21 deletions(-) create mode 100644 src/peer/id.c diff --git a/Makefile.am b/Makefile.am index f54c93c..6273299 100644 --- a/Makefile.am +++ b/Makefile.am @@ -112,7 +112,7 @@ SUBDIRS = . test/unit test/integration bench # recipes for integration/unit testing # NOTE: integration requires all target -integration: all +integration: $(MAKE) -C test/integration check unit: $(MAKE) -C test/unit check diff --git a/inc/peer.h b/inc/peer.h index 323ea66..b2b7b8d 100644 --- a/inc/peer.h +++ b/inc/peer.h @@ -41,8 +41,6 @@ struct peer { unsigned char peer_id[PEER_PEER_ID_SIZE]; uint8_t infohash[PEER_INFOHASH_SIZE]; - struct torrent *torrent; - uint8_t *bitfield; size_t bitfield_size; @@ -87,6 +85,7 @@ int peer_choke(struct peer*); int peer_choke_received(struct peer*,ssize_t); void peer_free(struct peer*); int peer_handshake(struct peer*); +int peer_handshake_received(struct peer*); int peer_hash_reject(struct peer*); int peer_hash_reject_received(struct peer*,ssize_t); int peer_hash_request(struct peer*); @@ -95,6 +94,7 @@ 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); +void peer_id(void*,size_t); int peer_init(struct peer**); int peer_interested(struct peer*); int peer_interested_received(struct peer*,ssize_t); diff --git a/inc/session.h b/inc/session.h index 2b3b404..43be8c6 100644 --- a/inc/session.h +++ b/inc/session.h @@ -5,6 +5,7 @@ #include #include +#include #include struct session_torrents { @@ -14,7 +15,7 @@ struct session_torrents { struct session { struct session_torrents torrents; - unsigned char peer_id[20]; + unsigned char peer_id[PEER_PEER_ID_SIZE]; }; extern struct session session; diff --git a/src/peer/handshake.c b/src/peer/handshake.c index 98d44f7..a85567f 100644 --- a/src/peer/handshake.c +++ b/src/peer/handshake.c @@ -5,13 +5,11 @@ #define HEADER_LENGTH HANDSHAKE_STRING_LENGTH+PEER_INFOHASH_SIZE+PEER_PEER_ID_SIZE -static int peer_handshake_receive(struct peer*); - int peer_handshake(struct peer *info) { char header[HEADER_LENGTH] = HANDSHAKE_STRING; - if(peer_handshake_receive(info)<0) { return -1; } - if(!info->handshake) { return 0; } +// if(peer_handshake_receive(info)<0) { return -1; } +// if(!info->handshake) { return 0; } // copy infohash to buffer memcpy( @@ -19,10 +17,10 @@ int peer_handshake(struct peer *info) { info->infohash, PEER_INFOHASH_SIZE); - // copy peer id to buffer + // copy session->peer_id to buffer memcpy( &(header[HANDSHAKE_STRING_LENGTH+PEER_INFOHASH_SIZE]), - info->peer_id, + session.peer_id, PEER_PEER_ID_SIZE); if(net_send(info,header,HEADER_LENGTH)<0) { return -1; } @@ -30,7 +28,7 @@ int peer_handshake(struct peer *info) { return 1; } -static int peer_handshake_receive(struct peer *info) { +int peer_handshake_received(struct peer *info) { char header[HEADER_LENGTH]; int i; @@ -48,9 +46,6 @@ static int peer_handshake_receive(struct peer *info) { &(header[HANDSHAKE_STRING_LENGTH+PEER_INFOHASH_SIZE]), PEER_INFOHASH_SIZE); - info->torrent = session_find_torrent(info->infohash,PEER_INFOHASH_SIZE); - if(NULL==info->torrent) { return -1; } - info->handshake = 1; return 1; diff --git a/src/peer/id.c b/src/peer/id.c new file mode 100644 index 0000000..7df5682 --- /dev/null +++ b/src/peer/id.c @@ -0,0 +1,7 @@ +#include + +void peer_id(void *buf, size_t size) { + assert(PEER_PEER_ID_SIZE==size); + + randombytes_buf(buf,PEER_PEER_ID_SIZE); +} diff --git a/src/session.c b/src/session.c index a9659b6..f1fec05 100644 --- a/src/session.c +++ b/src/session.c @@ -38,6 +38,8 @@ int session_init() { int session_setup() { if(session_init(&session)<0) { return -1; } + peer_id(session.peer_id,PEER_PEER_ID_SIZE); + if(0!=atexit(&session_clean)) { perror("atexit"); return -1; diff --git a/test/integration/Makefile.am b/test/integration/Makefile.am index a9e83d4..4706590 100644 --- a/test/integration/Makefile.am +++ b/test/integration/Makefile.am @@ -33,7 +33,14 @@ basic_tests_SOURCES = \ qbittorrent_tests_SOURCES = \ $(common_SOURCES) \ - qbittorrent.tests.c + qbittorrent.tests.c \ + $(top_srcdir)/src/net/cache.c \ + $(top_srcdir)/src/net/send.c \ + $(top_srcdir)/src/net/wait.c \ + $(top_srcdir)/src/peer/free.c \ + $(top_srcdir)/src/peer/handshake.c \ + $(top_srcdir)/src/peer/id.c \ + $(top_srcdir)/src/peer/init.c clean-local: -rm -rf $(TEST_DIRECTORY) diff --git a/test/integration/qbittorrent.tests.c b/test/integration/qbittorrent.tests.c index 4924fdb..0e1b340 100644 --- a/test/integration/qbittorrent.tests.c +++ b/test/integration/qbittorrent.tests.c @@ -1,13 +1,18 @@ #include +#include + int main(); void qbittorrent_integration_test(); +/* mocks and structs */ +struct session session; +int session_setup(); + int main() { setup_env(); qbittorrent_integration_test(); - reset_env(); return EXIT_SUCCESS; @@ -17,7 +22,10 @@ int main() { void qbittorrent_integration_test() { struct addrinfo hints, *res; - int sock_fd; + struct peer *info; + const char infohash[] = {0x25, 0x11, 0xdf, 0x9f, 0x74, 0xdc, 0x6f, 0xc1, 0x8e, 0xc9, 0x46, 0x6c, 0xdd, 0x52, 0xf5, 0xa1, 0x83, 0x27, 0xe2, 0x6e}; + + assert(1==peer_init(&info)); memset(&hints,0,sizeof(hints)); hints.ai_family = AF_UNSPEC; @@ -25,10 +33,30 @@ void qbittorrent_integration_test() { getaddrinfo("127.0.0.1",QBITTORRENT_PORT,&hints,&res); - sock_fd = socket(res->ai_family,res->ai_socktype,res->ai_protocol); - assert(sock_fd>0); + info->sock = socket(res->ai_family,res->ai_socktype,res->ai_protocol); + assert(info->sock>0); + + assert(0==connect(info->sock,res->ai_addr,res->ai_addrlen)); + + assert(1==session_setup()); + memcpy(info->infohash,infohash,PEER_INFOHASH_SIZE); + + assert(1==peer_handshake(info)); + assert(1==peer_handshake_received(info)); + + assert(1==peer_bitfield(info)); + + close(info->sock); + peer_free(info); +} + +int session_init() { + return 1; +} - assert(0==connect(sock_fd,res->ai_addr,res->ai_addrlen)); +int session_setup() { + assert(1==session_init(&session)); + peer_id(session.peer_id,PEER_PEER_ID_SIZE); - close(sock_fd); + return 1; } -- 2.30.2