From 673d0d903ae3f9ee520de7c34d195cc1ae7d3813 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 8 Apr 2022 18:26:52 -0700 Subject: [PATCH] ... --- Makefile.am | 2 ++ inc/peer.h | 4 +++- inc/piece.h | 19 +++++++++++++++++++ src/peer/handshake.c | 5 ++--- src/peer/piece.c | 17 ++++++++++++++++- src/piece.c | 21 +++++++++++++++++++++ 6 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 inc/piece.h create mode 100644 src/piece.c diff --git a/Makefile.am b/Makefile.am index 230a388..8f6d016 100644 --- a/Makefile.am +++ b/Makefile.am @@ -72,6 +72,7 @@ seederd_SOURCES = \ src/peer/queue.c \ src/peer/reject.c \ src/peer/request.c \ + src/piece.c \ src/pqueue.c \ src/rss/entry.c \ src/rss/footer.c \ @@ -111,6 +112,7 @@ seederd_SOURCES += \ inc/meta.h \ inc/net.h \ inc/opt.h \ + inc/piece.h \ inc/pqueue.h \ inc/rss.h \ inc/session.h \ diff --git a/inc/peer.h b/inc/peer.h index ddcc763..7084bd2 100644 --- a/inc/peer.h +++ b/inc/peer.h @@ -39,9 +39,10 @@ struct peer { unsigned int interested: 1; unsigned char peer_id[PEER_PEER_ID_SIZE]; - uint8_t infohash[PEER_INFOHASH_SIZE]; + struct torrent *torrent; + uint8_t *bitfield; size_t bitfield_size; @@ -73,6 +74,7 @@ enum peer_message { }; #include +#include #include int peer_bitfield(struct peer*); diff --git a/inc/piece.h b/inc/piece.h new file mode 100644 index 0000000..de80352 --- /dev/null +++ b/inc/piece.h @@ -0,0 +1,19 @@ +#ifndef __PIECE_H_ +#define __PIECE_H_ + +#include +#include + +struct piece { + const char *path; + uint64_t start; + + uint8_t *data; + size_t data_size; +}; + +#include + +struct piece* piece_load(const struct torrent*,size_t); + +#endif diff --git a/src/peer/handshake.c b/src/peer/handshake.c index 59aaff9..98d44f7 100644 --- a/src/peer/handshake.c +++ b/src/peer/handshake.c @@ -32,7 +32,6 @@ int peer_handshake(struct peer *info) { static int peer_handshake_receive(struct peer *info) { char header[HEADER_LENGTH]; - struct torrent *p; int i; if((i = net_wait(info,header,HEADER_LENGTH))<=0) { @@ -49,8 +48,8 @@ static int peer_handshake_receive(struct peer *info) { &(header[HANDSHAKE_STRING_LENGTH+PEER_INFOHASH_SIZE]), PEER_INFOHASH_SIZE); - p = session_find_torrent(info->infohash,PEER_INFOHASH_SIZE); - if(NULL==p) { return -1; } + info->torrent = session_find_torrent(info->infohash,PEER_INFOHASH_SIZE); + if(NULL==info->torrent) { return -1; } info->handshake = 1; diff --git a/src/peer/piece.c b/src/peer/piece.c index 7180f93..ef6cc78 100644 --- a/src/peer/piece.c +++ b/src/peer/piece.c @@ -1,7 +1,22 @@ #include int peer_piece(struct peer *info) { - return -1; + struct piece_request *p; + struct piece *piece; + + p = info->piece_requests; + + piece = piece_load(info->torrent,p->index); + if(NULL==piece) { return -1; } + + if( p->begin + p->length > piece->data_size ) { return -1; } + + if(net_send(info,&(piece->data[p->begin]),p->length)<0) { return -1; } + if(info->out!=NULL) { return 0; } + + info->piece_requests = info->piece_requests->next; + + return 1; } int peer_piece_queue(struct peer *info, struct piece_request *to_insert) { diff --git a/src/piece.c b/src/piece.c new file mode 100644 index 0000000..9044829 --- /dev/null +++ b/src/piece.c @@ -0,0 +1,21 @@ +#include + +int piece_cache_access() { + return -1; +} + +int piece_cache_find() { + return -1; +} + +int piece_cache_init() { + return -1; +} + +int piece_cache_remove() { + return -1; +} + +struct piece *piece_load(const struct torrent *torrent, size_t index) { + return NULL; +} -- 2.30.2