]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Sat, 23 Jul 2022 17:15:02 +0000 (10:15 -0700)
committeralex <[email protected]>
Sat, 23 Jul 2022 17:15:02 +0000 (10:15 -0700)
Makefile.am
inc/peer.h
inc/session.h
src/peer/handshake.c
src/peer/id.c [new file with mode: 0644]
src/session.c
test/integration/Makefile.am
test/integration/qbittorrent.tests.c

index f54c93c82f9da138d90675ff18973d89f82d6d70..62732997c07d590d94d8df2627b0e955e659574b 100644 (file)
@@ -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
index 323ea6654661c7d9b758939ff463dcbfde7ed845..b2b7b8d97a93ca1326dcb138edfe1f25ae54fec1 100644 (file)
@@ -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);
index 2b3b4049bbed4992cddf8e089d75c71a3b62586d..43be8c68a784b50ddc12ff82df9f9a46b2af47c8 100644 (file)
@@ -5,6 +5,7 @@
 
 #include<hashmap.h>
 #include<log.h>
+#include<peer.h>
 #include<torrent.h>
 
 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;
index 98d44f7c68a9759d131044d7fc189ad2bd6e2895..a85567fe41b8f1651a277997882c038aa09fb152 100644 (file)
@@ -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 (file)
index 0000000..7df5682
--- /dev/null
@@ -0,0 +1,7 @@
+#include<peer.h>
+
+void peer_id(void *buf, size_t size) {
+       assert(PEER_PEER_ID_SIZE==size);
+
+       randombytes_buf(buf,PEER_PEER_ID_SIZE);
+}
index a9659b6f1d9f760fde7f4ac14ce730b5d15c44b6..f1fec05abf7c5e6e98d86998b7ebccb9f4301f85 100644 (file)
@@ -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;
index a9e83d4ed0298c581125724e9a22477380f1d6d2..470659059611d438113beb068d94b18d4e663e0a 100644 (file)
@@ -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)
index 4924fdb073e2b4e79abe4c1794d00716193f3df7..0e1b34000cd28ee9116630a4d5f0e7e460ec5e48 100644 (file)
@@ -1,13 +1,18 @@
 #include<test_utils.h>
 
+#include<peer.h>
+
 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;
 }