#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;
uint8_t *bitfield;
size_t bitfield_size;
+ struct piece_request *piece_requests;
+ struct hash_request *hash_requests;
+
void *in;
size_t in_size;
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*);
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;
}
#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;
}