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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//! BEP 10 Extension Protocol message registry and dispatch types.
//!
//! Implements the `ExtensionMessageRegistry` + `DefaultExtensionMessageFactory`
//! pattern from the C++ aria2 codebase. The registry tracks per-peer extension
//! ID assignments negotiated via the BEP 10 extension handshake, and provides
//! dispatch logic for ut_metadata (BEP 9) and ut_pex (BEP 11).
//!
//! # Wire Protocol
//!
//! After the BT handshake, both peers send an Extended message with `ext_id = 0`
//! (the extension handshake). The payload is a bencoded dict:
//!
//! ```text
//! d 1:m d 10:ut_metadata i1e 6:ut_pex i2e e 4:reqq i500e e
//! ```
//!
//! The `m` sub-dict maps extension names to the `ext_id` values the sender
//! will use for those extensions in subsequent Extended messages.
use HashMap;
use ;
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
/// Default local ext_id for ut_metadata (matches C++ DefaultExtensionMessageFactory).
pub const DEFAULT_LOCAL_UT_METADATA_ID: u8 = 1;
/// Default local ext_id for ut_pex (matches C++ DefaultExtensionMessageFactory).
pub const DEFAULT_LOCAL_UT_PEX_ID: u8 = 2;
/// Default reqq value (max outstanding metadata requests).
pub const DEFAULT_REQQ: u32 = 500;
/// Extension name for ut_metadata (BEP 9).
pub const UT_METADATA_NAME: & = b"ut_metadata";
/// Extension name for ut_pex (BEP 11).
pub const UT_PEX_NAME: & = b"ut_pex";
// ---------------------------------------------------------------------------
// ExtensionRegistry
// ---------------------------------------------------------------------------
/// Per-peer registry mapping extension names to their negotiated ext_id values.
///
/// Mirrors C++ `ExtensionMessageRegistry` which stores the mapping between
/// extension names (ut_metadata, ut_pex) and their ext_id values as negotiated
/// in the BEP 10 extension handshake.
///
/// After receiving an Extension Handshake from a peer, the caller populates
/// this registry with the peer's ext_id assignments. Then when an Extended
/// message arrives with a given ext_id, the dispatch logic looks up the
/// registry to determine which extension handler to invoke.
///
/// # Local vs Peer IDs
///
/// - **Local IDs**: The ext_id values *we* told the peer in our handshake.
/// These are fixed for the connection lifetime (ut_metadata=1, ut_pex=2).
/// - **Peer IDs**: The ext_id values the *peer* told us in their handshake.
/// When we receive an Extended message with `ext_id = X`, we match X
/// against the peer's IDs to determine which extension it is.
// ---------------------------------------------------------------------------
// ExtensionUpdate
// ---------------------------------------------------------------------------
/// Side effect from processing an extension message.
///
/// Returned by [`dispatch_extension_message`](lookup::dispatch_extension_message)
/// so the interaction loop caller can apply the appropriate side effects
/// (e.g., storing metadata, adding discovered peers, etc.).
// ---------------------------------------------------------------------------
// Re-exports
// ---------------------------------------------------------------------------
pub use dispatch_extension_message;