SimpleTorrent
peer.h
浏览该文件的文档.
1 
6 #ifndef PEER_H
7 #define PEER_H
8 
9 #include "metainfo.h"
10 
14 #define PSTR_DEFAULT "BitTorrent protocol"
15 
19 #define PSTRLEN_DEFAULT (sizeof(PSTR_DEFAULT) - 1)
20 
21 #pragma pack(1)
22 
25 typedef struct {
26  uint8_t hs_pstrlen;
27  char hs_pstr[19];
28  uint8_t hs_reserved[8];
29  uint8_t hs_info_hash[HASH_SIZE];
30  char hs_peer_id[HASH_SIZE];
32 
33 enum {
34  BT_CHOKE,
35  BT_UNCHOKE,
36  BT_INTERESTED,
37  BT_NOT_INTERESTED,
38  BT_HAVE,
39  BT_BITFIELD,
40  BT_REQUEST,
41  BT_PIECE,
42  BT_CANCEL,
43 };
44 
48 extern const char *bt_types[];
49 
64 struct PeerMsg {
65  uint32_t len;
66  uint8_t id;
67  union {
68  struct {
69  uint32_t piece_index;
70  } have;
71 
72  uint8_t bitfield[0];
73 
74  struct {
75  uint32_t index;
76  uint32_t begin;
77  uint32_t length;
78  } request;
79 
80  struct {
81  uint32_t index;
82  uint32_t begin;
83  uint8_t block[0];
84  } piece;
85 
86  struct {
87  uint32_t index;
88  uint32_t begin;
89  uint32_t length;
90  } cancel;
91  };
92 };
93 #pragma pack()
94 
102 struct Peer
103 {
104  int fd;
105  char ip[16];
106  unsigned short port;
107  uint32_t addr;
108  unsigned char *bitfield;
109  int is_choked;
118  unsigned wanted;
119  struct PeerMsg *msg;
120  time_t start_time;
121  double speed;
122 };
123 
129 struct PeerMsg *peer_get_packet(struct Peer *peer);
130 
137 struct Peer *peer_new(int fd, size_t nr_pieces);
138 
143 void peer_free(struct Peer **peer);
144 
150 void print_bit(unsigned char *bytes, size_t bit_len);
151 
157 void peer_set_bit(struct Peer *peer, unsigned bit_offset);
158 
164 unsigned char peer_get_bit(struct Peer *peer, unsigned bit_offset);
165 
174 void peer_send_msg(struct Peer *peer, struct PeerMsg *msg);
175 
176 #endif // PEER_H
int * requested_subpieces
-1 terminated
Definition: peer.h:114
unsigned wanted
期望接受的字节数
Definition: peer.h:118
int fd
连接套接字
Definition: peer.h:104
void peer_send_msg(struct Peer *peer, struct PeerMsg *msg)
向 peer 发送 BT 消息
Definition: peer.c:229
time_t start_time
记录发送请求的时刻,用于计算分片下载速度,不使用分片的 time 是因为后者可能因超时被更新。 ...
Definition: peer.h:120
操作全局信息的相关 API 声明
double speed
下载速度
Definition: peer.h:121
void peer_free(struct Peer **peer)
释放 peer
Definition: peer.c:112
int requesting_begin
请求的子分片偏移量(固定长度)
Definition: peer.h:116
struct PeerMsg * msg
记录尚未读完的 msg
Definition: peer.h:119
uint32_t length
子分片长度
Definition: peer.h:77
uint32_t begin
子分片起始偏移量
Definition: peer.h:76
int contribution
检查周期内的数据贡献
Definition: peer.h:117
unsigned short port
端口, 本地字节序
Definition: peer.h:106
int is_interested
是否对 peer 感兴趣
Definition: peer.h:110
int get_interested
peer 是否感兴趣
Definition: peer.h:112
unsigned char peer_get_bit(struct Peer *peer, unsigned bit_offset)
获取 peer 的位域
Definition: peer.c:223
int get_choked
是否被 peer 阻塞
Definition: peer.h:111
unsigned char * bitfield
piece 拥有情况
Definition: peer.h:108
uint32_t len
后续消息长度(不包含 len 自身的 4 个字节)
Definition: peer.h:65
struct PeerMsg * peer_get_packet(struct Peer *peer)
获取 BT 报文
Definition: peer.c:30
握手信息
Definition: peer.h:25
char ip[16]
ip 地址字符串, 最长不过 |255.255.255.255| + '\0' = 16
Definition: peer.h:105
uint8_t hs_pstrlen
BitTorrent 1.0: 19.
Definition: peer.h:26
int is_choked
是否阻塞 peer
Definition: peer.h:109
void peer_set_bit(struct Peer *peer, unsigned bit_offset)
设置 peer 的位域
Definition: peer.c:217
int requesting_index
请求的分片号,-1 为无效。
Definition: peer.h:115
BT 消息
Definition: peer.h:64
描述 peer 信息
Definition: peer.h:102
uint8_t id
消息编号
Definition: peer.h:66
const char * bt_types[]
对应 BT 报文类型的字符串
Definition: peer.c:13
struct Peer * peer_new(int fd, size_t nr_pieces)
peer 构造
Definition: peer.c:84
uint32_t index
分片号
Definition: peer.h:75
uint32_t addr
ip 的整数形式,用于地址比较,直接来自 sin_addr,故是网络字节序
Definition: peer.h:107
int * requested_pieces
-1 terminated
Definition: peer.h:113
void print_bit(unsigned char *bytes, size_t bit_len)
向标准输出打印 bitfield
Definition: peer.c:207
#define HASH_SIZE
SHA1 HASH 的字节数
Definition: metainfo.h:16
uint32_t piece_index
分片号
Definition: peer.h:69