From: alex Date: Fri, 8 Apr 2022 01:44:17 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=c6227ae154ef063ce7fa32dec222ba5e9bc0f701;p=seeder ... --- diff --git a/inc/peer.h b/inc/peer.h index d853943..ddcc763 100644 --- a/inc/peer.h +++ b/inc/peer.h @@ -10,6 +10,27 @@ #include #include +#include + +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*); diff --git a/src/peer/piece.c b/src/peer/piece.c index 0e4a1a2..7180f93 100644 --- a/src/peer/piece.c +++ b/src/peer/piece.c @@ -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; } diff --git a/src/peer/request.c b/src/peer/request.c index d7ce11d..b372a5b 100644 --- a/src/peer/request.c +++ b/src/peer/request.c @@ -1,28 +1,40 @@ #include +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; }