...
authoralex <[email protected]>
Sat, 9 Apr 2022 01:26:52 +0000 (18:26 -0700)
committeralex <[email protected]>
Sat, 9 Apr 2022 01:26:52 +0000 (18:26 -0700)
Makefile.am
inc/peer.h
inc/piece.h [new file with mode: 0644]
src/peer/handshake.c
src/peer/piece.c
src/piece.c [new file with mode: 0644]

index 230a3887ffc797860d4885a68146b895ea9f54a5..8f6d016dd94b9f0f2b9534a2d08f5de920c9df80 100644 (file)
@@ -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 \
index ddcc763451599c1bb89324ebf9b8b652edd8affc..7084bd20d7e7dbf7d084540926c38bf062302760 100644 (file)
@@ -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<net.h>
+#include<piece.h>
 #include<session.h>
 
 int peer_bitfield(struct peer*);
diff --git a/inc/piece.h b/inc/piece.h
new file mode 100644 (file)
index 0000000..de80352
--- /dev/null
@@ -0,0 +1,19 @@
+#ifndef __PIECE_H_
+#define __PIECE_H_
+
+#include<stddef.h>
+#include<stdint.h>
+
+struct piece {
+       const char *path;
+       uint64_t start;
+
+       uint8_t *data;
+       size_t data_size;
+};
+
+#include<torrent.h>
+
+struct piece* piece_load(const struct torrent*,size_t);
+
+#endif
index 59aaff97d0717e228594b40c5706de2779831eec..98d44f7c68a9759d131044d7fc189ad2bd6e2895 100644 (file)
@@ -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;
 
index 7180f93dd0cf18218c2ddb014055c974c4f401b2..ef6cc789398c9955b92f578e1fd4efa0c52e9fa6 100644 (file)
@@ -1,7 +1,22 @@
 #include<peer.h>
 
 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 (file)
index 0000000..9044829
--- /dev/null
@@ -0,0 +1,21 @@
+#include<piece.h>
+
+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;
+}