@@ -26,7 +26,7 @@ use futures::prelude::*;
26
26
use futures_timer:: Delay ;
27
27
use libp2p_core:: either:: { EitherError , EitherOutput } ;
28
28
use libp2p_core:: upgrade:: { EitherUpgrade , SelectUpgrade } ;
29
- use libp2p_core:: { ConnectedPoint , PeerId } ;
29
+ use libp2p_core:: { ConnectedPoint , Multiaddr , PeerId , PublicKey } ;
30
30
use libp2p_swarm:: handler:: {
31
31
ConnectionEvent , DialUpgradeError , FullyNegotiatedInbound , FullyNegotiatedOutbound ,
32
32
} ;
@@ -42,22 +42,47 @@ use std::{io, pin::Pin, task::Context, task::Poll, time::Duration};
42
42
pub struct Proto {
43
43
initial_delay : Duration ,
44
44
interval : Duration ,
45
+ public_key : PublicKey ,
46
+ protocol_version : String ,
47
+ agent_version : String ,
45
48
}
46
49
47
50
impl Proto {
48
- pub fn new ( initial_delay : Duration , interval : Duration ) -> Self {
51
+ pub fn new (
52
+ initial_delay : Duration ,
53
+ interval : Duration ,
54
+ public_key : PublicKey ,
55
+ protocol_version : String ,
56
+ agent_version : String ,
57
+ ) -> Self {
49
58
Proto {
50
59
initial_delay,
51
60
interval,
61
+ public_key,
62
+ protocol_version,
63
+ agent_version,
52
64
}
53
65
}
54
66
}
55
67
56
68
impl IntoConnectionHandler for Proto {
57
69
type Handler = Handler ;
58
70
59
- fn into_handler ( self , remote_peer_id : & PeerId , _endpoint : & ConnectedPoint ) -> Self :: Handler {
60
- Handler :: new ( self . initial_delay , self . interval , * remote_peer_id)
71
+ fn into_handler ( self , remote_peer_id : & PeerId , endpoint : & ConnectedPoint ) -> Self :: Handler {
72
+ let observed_addr = match endpoint {
73
+ ConnectedPoint :: Dialer { address, .. } => address,
74
+ ConnectedPoint :: Listener { send_back_addr, .. } => send_back_addr,
75
+ } ;
76
+
77
+ Handler :: new (
78
+ self . initial_delay ,
79
+ self . interval ,
80
+ * remote_peer_id,
81
+ self . public_key ,
82
+ self . protocol_version ,
83
+ self . agent_version ,
84
+ observed_addr. clone ( ) ,
85
+ )
61
86
}
62
87
63
88
fn inbound_protocol ( & self ) -> <Self :: Handler as ConnectionHandler >:: InboundProtocol {
@@ -94,9 +119,6 @@ pub struct Handler {
94
119
> ; 4 ] ,
95
120
> ,
96
121
97
- /// Identify request information.
98
- info : Option < Info > ,
99
-
100
122
/// Pending replies to send.
101
123
pending_replies : VecDeque < Pending > ,
102
124
@@ -108,6 +130,33 @@ pub struct Handler {
108
130
109
131
/// The interval of `trigger_next_identify`, i.e. the recurrent delay.
110
132
interval : Duration ,
133
+
134
+ /// The public key of the local peer.
135
+ public_key : PublicKey ,
136
+
137
+ /// Application-specific version of the protocol family used by the peer,
138
+ /// e.g. `ipfs/1.0.0` or `polkadot/1.0.0`.
139
+ protocol_version : String ,
140
+
141
+ /// Name and version of the peer, similar to the `User-Agent` header in
142
+ /// the HTTP protocol.
143
+ agent_version : String ,
144
+
145
+ /// Address observed by or for the remote.
146
+ observed_addr : Multiaddr ,
147
+
148
+ /// Information provided by the `Behaviour` upon requesting.
149
+ behaviour_info : Option < BehaviourInfo > ,
150
+ }
151
+
152
+ /// Information provided by the `Behaviour` upon requesting.
153
+ #[ derive( Debug ) ]
154
+ pub struct BehaviourInfo {
155
+ /// The addresses that the peer is listening on.
156
+ pub listen_addrs : Vec < Multiaddr > ,
157
+
158
+ /// The list of protocols supported by the peer, e.g. `/ipfs/ping/1.0.0`.
159
+ pub protocols : Vec < String > ,
111
160
}
112
161
113
162
/// Event produced by the `Handler`.
@@ -132,21 +181,33 @@ pub enum InEvent {
132
181
/// Identifying information of the local node that is pushed to a remote.
133
182
Push ( Info ) ,
134
183
/// Identifying information requested from this node.
135
- Identify ( Info ) ,
184
+ Identify ( BehaviourInfo ) ,
136
185
}
137
186
138
187
impl Handler {
139
188
/// Creates a new `Handler`.
140
- pub fn new ( initial_delay : Duration , interval : Duration , remote_peer_id : PeerId ) -> Self {
189
+ pub fn new (
190
+ initial_delay : Duration ,
191
+ interval : Duration ,
192
+ remote_peer_id : PeerId ,
193
+ public_key : PublicKey ,
194
+ protocol_version : String ,
195
+ agent_version : String ,
196
+ observed_addr : Multiaddr ,
197
+ ) -> Self {
141
198
Self {
142
199
remote_peer_id,
143
200
inbound_identify_push : Default :: default ( ) ,
144
201
events : SmallVec :: new ( ) ,
145
- info : None ,
146
202
pending_replies : VecDeque :: new ( ) ,
147
203
trigger_next_identify : Delay :: new ( initial_delay) ,
148
204
keep_alive : KeepAlive :: Yes ,
149
205
interval,
206
+ public_key,
207
+ protocol_version,
208
+ agent_version,
209
+ observed_addr,
210
+ behaviour_info : None ,
150
211
}
151
212
}
152
213
@@ -161,9 +222,9 @@ impl Handler {
161
222
) {
162
223
match output {
163
224
EitherOutput :: First ( substream) => {
164
- // If we already have `Info ` we can proceed responding to the Identify request,
165
- // if not, we request `Info` from the behaviour .
166
- if self . info . is_none ( ) {
225
+ // If we already have `BehaviourInfo ` we can proceed responding to the Identify request,
226
+ // if not, we request it .
227
+ if self . behaviour_info . is_none ( ) {
167
228
self . events
168
229
. push ( ConnectionHandlerEvent :: Custom ( Event :: Identify ) ) ;
169
230
}
@@ -259,7 +320,7 @@ impl ConnectionHandler for Handler {
259
320
} ) ;
260
321
}
261
322
InEvent :: Identify ( info) => {
262
- self . info = Some ( info) ;
323
+ self . behaviour_info = Some ( info) ;
263
324
}
264
325
}
265
326
}
@@ -303,11 +364,19 @@ impl ConnectionHandler for Handler {
303
364
}
304
365
305
366
// Check for pending replies to send.
306
- if let Some ( ref info) = self . info {
367
+ if let Some ( ref info) = self . behaviour_info {
307
368
if let Some ( mut pending) = self . pending_replies . pop_front ( ) {
308
369
loop {
309
370
match pending {
310
371
Pending :: Queued ( io) => {
372
+ let info = Info {
373
+ public_key : self . public_key . clone ( ) ,
374
+ protocol_version : self . protocol_version . clone ( ) ,
375
+ agent_version : self . agent_version . clone ( ) ,
376
+ listen_addrs : info. listen_addrs . clone ( ) ,
377
+ protocols : info. protocols . clone ( ) ,
378
+ observed_addr : self . observed_addr . clone ( ) ,
379
+ } ;
311
380
let io = Box :: pin ( io. send ( info. clone ( ) ) ) ;
312
381
pending = Pending :: Sending {
313
382
peer : self . remote_peer_id ,
0 commit comments