...
authoralex <[email protected]>
Fri, 8 Apr 2022 01:44:17 +0000 (18:44 -0700)
committeralex <[email protected]>
Fri, 8 Apr 2022 01:44:17 +0000 (18:44 -0700)
inc/peer.h
src/peer/piece.c
src/peer/request.c

index d853943a5aa4976bba1f4147defe88471ea3972f..ddcc763451599c1bb89324ebf9b8b652edd8affc 100644 (file)
 #include<stdlib.h>
 #include<string.h>
 
+#include<sodium.h>
+
+struct piece_request {
+       size_t index;
+       size_t begin;
+       size_t length;
+
+       struct piece_request *next;
+};
+
+struct hash_request {
+       unsigned char pieces_root[crypto_hash_sha256_BYTES];
+
+       size_t base_layer;
+       size_t index;
+       size_t length;
+       size_t proof_layers;
+
+       struct hash_request *next;
+};
+
 struct peer {
        int sock;
 
@@ -24,6 +45,9 @@ struct peer {
        uint8_t *bitfield;
        size_t bitfield_size;
 
+       struct piece_request *piece_requests;
+       struct hash_request *hash_requests;
+
        void *in;
        size_t in_size;
        
@@ -76,6 +100,7 @@ int peer_keepalive_received(struct peer*,ssize_t);
 int peer_not_interested(struct peer*);
 int peer_not_interested_received(struct peer*,ssize_t);
 int peer_piece(struct peer*);
+int peer_piece_queue(struct peer*,struct piece_request*);
 int peer_piece_received(struct peer*,ssize_t);
 int peer_queue_process(struct peer*);
 int peer_reject(struct peer*);
index 0e4a1a227417703ed74e5dc0fef83b88c2f3e6d6..7180f93dd0cf18218c2ddb014055c974c4f401b2 100644 (file)
@@ -4,6 +4,25 @@ int peer_piece(struct peer *info) {
        return -1;
 }
 
+int peer_piece_queue(struct peer *info, struct piece_request *to_insert) {
+       struct piece_request *p;
+
+       p = info->piece_requests;
+       while(p->next!=NULL) {
+               if(
+                       (to_insert->index==p->next->index) &&
+                       (to_insert->begin==p->next->begin) &&
+                       (to_insert->length==p->next->length)
+                 ) { /* duplicate */ return 0; }
+
+               p = p->next;
+       }
+
+       p->next = to_insert;
+
+       return 1;
+}
+
 int peer_piece_received(struct peer *info, ssize_t len) {
        return -1;
 }
index d7ce11dd611c512c17222c785ee17d0bfadf334d..b372a5bb29ec0b7af0c3e709d8af0fb0d71c5f92 100644 (file)
@@ -1,28 +1,40 @@
 #include<peer.h>
 
+static int peer_piece_request_init(struct piece_request**);
+
 int peer_request(struct peer *info) {
        return -1;
 }
 
+int peer_piece_request_init(struct piece_request **p) {
+       if(NULL==p) { return -1; }
+
+       (*p) = malloc(sizeof(struct piece_request));
+       if(NULL==(*p)) { return -1; }
+
+       (*p)->index = 0;
+       (*p)->begin = 0;
+       (*p)->length = 0;
+
+       (*p)->next = NULL;
+
+       return 1;
+}
+
 int peer_request_received(struct peer *info, ssize_t len) {
        unsigned char buf[17];
-//     struct peer_request *req;
-//     unsigned char *p;
-//     size_t index, begin, length;
+       struct piece_request *req;
        
        if(net_wait(info,buf,17)<0) { return -1; }
        if(info->in!=NULL) { return 0; }
 
-/*     if(peer_request_init(&req)<0) { return -1; }
+       if(peer_piece_request_init(&req)<0) { return -1; }
 
-       p = &(buf[5]);
-       req->index = BYTES_TO_UINT32_T(p);
-       p = &(buf[9]);
-       req->begin = BYTES_TO_UINT32_T(p);
-       p = &(buf[13]);
-       req->length = BYTES_TO_UINT32_T(p);
+       req->index = BYTES_TO_UINT32_T((&(buf[5])));
+       req->begin = BYTES_TO_UINT32_T((&(buf[9])));
+       req->length = BYTES_TO_UINT32_T((&(buf[13])));
 
        if(peer_piece_queue(info,req)<0) { return -1; }
-*/
-       return -1;
+
+       return 1;
 }