about summary refs log tree commit diff
path: root/btpd/torrent.h
blob: 9bfc11c6f6dcead4282362fb11f15d2802c65486 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef BTPD_TORRENT_H
#define BTPD_TORRENT_H

#define PIECE_BLOCKLEN (1 << 14)

struct block {
    struct piece *pc;
    struct net_buf *msg;
    struct block_request_tq reqs;
};

struct piece {
    struct torrent *tp;

    uint32_t index;

    unsigned nreqs;

    unsigned nblocks;
    unsigned ngot;
    unsigned nbusy;
    unsigned next_block;

    struct block *blocks;

    uint8_t *have_field;
    uint8_t *down_field;

    BTPDQ_ENTRY(piece) entry;
};

BTPDQ_HEAD(piece_tq, piece);

struct torrent {
    const char *relpath;
    struct metainfo meta;

    BTPDQ_ENTRY(torrent) entry;
    BTPDQ_ENTRY(torrent) net_entry;

    void *imem;
    size_t isiz;

    uint8_t *piece_field;
    uint8_t *block_field;

    uint8_t *busy_field;
    uint32_t npcs_busy;

    uint32_t have_npieces;

    unsigned *piece_count;

    uint64_t uploaded, downloaded;
    
    unsigned npeers;
    struct peer_tq peers;

    int endgame;
    struct piece_tq getlst;
};

BTPDQ_HEAD(torrent_tq, torrent);

off_t torrent_bytes_left(struct torrent *tp);

char *torrent_get_bytes(struct torrent *tp, off_t start, size_t len);
void torrent_put_bytes(struct torrent *tp, const char *buf,
		       off_t start, size_t len);

int torrent_load(const char *metafile);

void torrent_unload(struct torrent *tp);

int torrent_has_peer(struct torrent *tp, const uint8_t *id);

off_t torrent_piece_size(struct torrent *tp, uint32_t index);
uint32_t torrent_block_size(struct piece *pc, uint32_t index);

int torrent_has_all(struct torrent *tp);

#endif