-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbittorrent_constants.py
More file actions
98 lines (72 loc) · 2.37 KB
/
Copy pathbittorrent_constants.py
File metadata and controls
98 lines (72 loc) · 2.37 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""BitTorrent assigned numbers from BEP 0004.
Source: docs/bep_0004.rst_post.html
"""
from __future__ import annotations
from enum import IntEnum
PROTOCOL_STR = b"BitTorrent protocol"
PROTOCOL_STR_LEN = len(PROTOCOL_STR) # 19
HANDSHAKE_RESERVED_BYTES = 8
class CoreMessageID(IntEnum):
"""Core BitTorrent wire protocol message IDs."""
CHOKE = 0x00
UNCHOKE = 0x01
INTERESTED = 0x02
NOT_INTERESTED = 0x03
HAVE = 0x04
BITFIELD = 0x05
REQUEST = 0x06
PIECE = 0x07
CANCEL = 0x08
class DHTMessageID(IntEnum):
"""DHT extension message IDs."""
PORT = 0x09
class FastExtensionMessageID(IntEnum):
"""Fast extension message IDs."""
SUGGEST = 0x0D
HAVE_ALL = 0x0E
HAVE_NONE = 0x0F
REJECT_REQUEST = 0x10
ALLOWED_FAST = 0x11
class AdditionalDeployedMessageID(IntEnum):
"""Additional IDs used in deployed clients."""
LTEP_HANDSHAKE = 0x14
class HashTransferMessageID(IntEnum):
"""Hash transfer protocol message IDs."""
HASH_REQUEST = 0x15
HASHES = 0x16
HASH_REJECT = 0x17
# Reserved handshake capability bits from BEP 0004.
# Format: RESERVED_BITS[byte_index][bit_mask] = description
RESERVED_BITS: dict[int, dict[int, str]] = {
0: {
0x80: "Azureus Messaging Protocol",
},
2: {
0x08: "BitTorrent Location-aware Protocol (no known implementations)",
},
5: {
0x10: "LTEP (Libtorrent Extension Protocol)",
0x02: "Extension Negotiation Protocol",
0x01: "Extension Negotiation Protocol",
},
7: {
0x01: "BitTorrent DHT",
0x02: "XBT Peer Exchange",
0x04: "Fast extensions (suggest/haveall/havenone/reject/allowed fast)",
0x08: "NAT Traversal",
0x10: "Hybrid torrent legacy to v2 upgrade",
},
}
# Known collisions listed in BEP 0004.
RESERVED_BIT_COLLISIONS: dict[int, dict[int, str]] = {
0: {0xFF: "BitComet Extension Protocol"},
1: {0xFF: "BitComet Extension Protocol"},
7: {0x01: "XBT Metadata Exchange (XBT only)"},
}
ALL_MESSAGE_IDS: dict[int, str] = {
**{int(v): v.name.lower() for v in CoreMessageID},
**{int(v): f"dht_{v.name.lower()}" for v in DHTMessageID},
**{int(v): f"fast_{v.name.lower()}" for v in FastExtensionMessageID},
**{int(v): f"deployed_{v.name.lower()}" for v in AdditionalDeployedMessageID},
**{int(v): f"hash_{v.name.lower()}" for v in HashTransferMessageID},
}