]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Wed, 2 Feb 2022 21:19:04 +0000 (13:19 -0800)
committeralex <[email protected]>
Wed, 2 Feb 2022 21:19:04 +0000 (13:19 -0800)
inc/session.h
inc/torrent.h
src/peer/handshake.c [new file with mode: 0644]
src/peer/ratelimit.c [new file with mode: 0644]
src/session.c

index 73d52931cb0fb65ee97b404eb9cce116487ecf5b..158e68d1584cefdbea6223e286878319e3c3edec 100644 (file)
@@ -4,13 +4,13 @@
 #include<log.h>
 #include<torrent.h>
 
-#define SESSION_MESSAGE_REALLOC_COUNT "(re)allocating memory for session torrents to %u\n"
-
-#define SESSION_REALLOC_COUNT 100
+struct session_torrents {
+       struct hash_map *infohashes;
+       struct hash_map *paths;
+};
 
 struct session {
-       struct torrent **torrents;
-       size_t torrent_count;
+       struct session_torrents torrents;
 };
 
 extern struct session session;
index 76fd5e85189ab235e16c49af9321c9d9453ad58b..d6c3b33df870a439662f6a55826fbe270c1d71d7 100644 (file)
@@ -27,6 +27,8 @@ struct torrent {
        struct tree *tree;
        struct torrent_files files;
 
+       uint8_t infohash[crypto_hash_sha256_BYTES];
+
        int watch_fd;
 };
 
diff --git a/src/peer/handshake.c b/src/peer/handshake.c
new file mode 100644 (file)
index 0000000..42ed6a3
--- /dev/null
@@ -0,0 +1,17 @@
+#include<peer.h>
+
+#define HANDSHAKE_STRING "\x13BitTorrent protocol\0\0\0\0\0\0\0\0";
+#define HANDSHAKE_INFOHASH_SIZE 20
+
+int handshake(int sock, const uint8_t *infohash, size_t infohash_size const uint8_t peer_id, size_t peer_id_size) {
+       const char header[64] = HANDSHAKE_STRING;
+
+       memcpy(&(header[24]),infohash,HANDSHAKE_INFOHASH_SIZE);
+       memcpy(&(header[44]),peer_id,PEER_ID_SIZE);
+
+       if(sock<0) { return -1; }
+
+       // send header
+
+       return -1;
+}
diff --git a/src/peer/ratelimit.c b/src/peer/ratelimit.c
new file mode 100644 (file)
index 0000000..b9c7506
--- /dev/null
@@ -0,0 +1,5 @@
+#include<peer.h>
+
+int rate_limit(const uint8_t peer_id, size_t peer_id_size) {
+       return 1;
+}
index 6b91b8332d41c9dcd131b053b083ca81879ac1e8..7b44b9774c0b7bf0418a942f372b4803b6b061da 100644 (file)
@@ -2,45 +2,73 @@
 
 struct session session;
 
+static int session_torrent_add_by_infohash(struct torrent*);
+static int session_torrent_add_by_path(struct torrent*);
+static int session_torrent_resize(struct hash_map**,size_t);
+
 void session_clean() {
-       struct torrent *torrent_p;
-       if(session.torrents!=NULL) {
-               while(session.torrent_count>0) {
-                       torrent_p = session.torrents[session.torrent_count];
-                       if(torrent_p!=NULL) { torrent_free(torrent_p); }
-                       session.torrent_count--;
-               }
-       }
-       free(session.torrents);
+       // clear infohashes so no double-free
+       hashmap_clear(session.torrent.infohashes);
+
+       hashmap_free(session.torrent.infohashes);
+       hashmap_free(session.torrent.paths);
 }
 
 int session_init() {
-       session.torrents = NULL;
-       session.torrent_count = 0;
+       if(hashmap_init(session.torrents.infohashes,SESSION_HASHMAP_INITIAL_SIZE)<0) { return -1; }
+       if(hashmap_init(session.torrents.paths,SESSION_HASHMAP_INITIAL_SIZE)<0) { return -1; }
 
        return 1;
 }
 
 int session_torrent_add(struct torrent *p) {
-       size_t new_count;
+       while((i = session_torrent_add_by_infohash(p))<=0) {
+               if(i<0) { return -1; }
+               if(session_torrent_resize(
+                       &(session.torrent.infohashes), /* map */
+                       session.torrent.infohashes.size<<1 /* new_size */
+               )<0) { return -1; }
+       }
+
+       while((i = session_torrent_add_by_path(p))<=0) {
+               if(i<0) { return -1; }
+               if(session_torrent_resize(
+                       &(session.torrent.paths), /* map */
+                       session.torrent.paths.size<<1 /* new_size */
+               )<0) { return -1; }
+       }
+
+       return 1;
+}
 
-       if((NULL==session.torrents)||(session.torrent_count%SESSION_REALLOC_COUNT==0)) {
-               new_count = session.torrent_count+SESSION_REALLOC_COUNT;
-               log_info(SESSION_MESSAGE_REALLOC_COUNT,new_count);
+static int session_torrent_add_by_infohash(struct torrent *torrent) {
+       struct torrent *p;
+       int i;
 
-               session.torrents = realloc(session.torrents,sizeof(struct torrent*)*new_count);
-               if(NULL==session.torrents) {
-                       perror("realloc");
-                       return -1;
-               }
+       if((i = hashmap_insert(session.torrents.infohashes,torrent->infohash,crypto_hash_sha_256_BYTES,p))<=0) {
+               if(i<0) { return -1; }
 
-               for(size_t i=session.torrent_count;i<new_count;i++) {
-                       session.torrents[i] = NULL;
-               }
+               p = hashmap_find(session.torrents.infohashes,torrent->infohash,crypto_hash_sha_256_BYTES);
+               if((p!=NULL)&&(memcmp(torrent->infohash,p->infohash,crypto_hash_sha256_BYTES)!=0)) { return 0; }
        }
 
-       session.torrents[session.torrent_count] = p;
-       session.torrent_count++;
+       return 1;
+}
+
+static int session_torrent_add_by_path(struct torrent *torrent) {
+       struct torrent *p;
+       int i;
+
+       if((i = hashmap_insert(session.torrents.paths,torrent->root,strlen(torrent->root),p))<=0) {
+               if(i<0) { return -1; }
+
+               p = hashmap_find(session.torrents.paths,torrent->root,strlen(torrent->root));
+               if((p!=NULL)&&(strcmp(p->root,torrent->root)!=0)) { return 0; }
+       }
 
        return 1;
 }
+
+static int session_torrent_resize(struct hash_map **map, size_t new_size) {
+       return -1;
+}