@@ -26,7 +26,7 @@ use futures::prelude::*;
2626use futures_timer:: Delay ;
2727use libp2p_core:: either:: { EitherError , EitherOutput } ;
2828use libp2p_core:: upgrade:: { EitherUpgrade , SelectUpgrade } ;
29- use libp2p_core:: { ConnectedPoint , PeerId } ;
29+ use libp2p_core:: { ConnectedPoint , Multiaddr , PeerId , PublicKey } ;
3030use libp2p_swarm:: handler:: {
3131 ConnectionEvent , DialUpgradeError , FullyNegotiatedInbound , FullyNegotiatedOutbound ,
3232} ;
@@ -42,22 +42,47 @@ use std::{io, pin::Pin, task::Context, task::Poll, time::Duration};
4242pub struct Proto {
4343 initial_delay : Duration ,
4444 interval : Duration ,
45+ public_key : PublicKey ,
46+ protocol_version : String ,
47+ agent_version : String ,
4548}
4649
4750impl 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 {
4958 Proto {
5059 initial_delay,
5160 interval,
61+ public_key,
62+ protocol_version,
63+ agent_version,
5264 }
5365 }
5466}
5567
5668impl IntoConnectionHandler for Proto {
5769 type Handler = Handler ;
5870
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+ )
6186 }
6287
6388 fn inbound_protocol ( & self ) -> <Self :: Handler as ConnectionHandler >:: InboundProtocol {
@@ -94,9 +119,6 @@ pub struct Handler {
94119 > ; 4 ] ,
95120 > ,
96121
97- /// Identify request information.
98- info : Option < Info > ,
99-
100122 /// Pending replies to send.
101123 pending_replies : VecDeque < Pending > ,
102124
@@ -108,6 +130,33 @@ pub struct Handler {
108130
109131 /// The interval of `trigger_next_identify`, i.e. the recurrent delay.
110132 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 > ,
111160}
112161
113162/// Event produced by the `Handler`.
@@ -132,21 +181,33 @@ pub enum InEvent {
132181 /// Identifying information of the local node that is pushed to a remote.
133182 Push ( Info ) ,
134183 /// Identifying information requested from this node.
135- Identify ( Info ) ,
184+ Identify ( BehaviourInfo ) ,
136185}
137186
138187impl Handler {
139188 /// 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 {
141198 Self {
142199 remote_peer_id,
143200 inbound_identify_push : Default :: default ( ) ,
144201 events : SmallVec :: new ( ) ,
145- info : None ,
146202 pending_replies : VecDeque :: new ( ) ,
147203 trigger_next_identify : Delay :: new ( initial_delay) ,
148204 keep_alive : KeepAlive :: Yes ,
149205 interval,
206+ public_key,
207+ protocol_version,
208+ agent_version,
209+ observed_addr,
210+ behaviour_info : None ,
150211 }
151212 }
152213
@@ -161,9 +222,9 @@ impl Handler {
161222 ) {
162223 match output {
163224 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 ( ) {
167228 self . events
168229 . push ( ConnectionHandlerEvent :: Custom ( Event :: Identify ) ) ;
169230 }
@@ -259,7 +320,7 @@ impl ConnectionHandler for Handler {
259320 } ) ;
260321 }
261322 InEvent :: Identify ( info) => {
262- self . info = Some ( info) ;
323+ self . behaviour_info = Some ( info) ;
263324 }
264325 }
265326 }
@@ -303,11 +364,19 @@ impl ConnectionHandler for Handler {
303364 }
304365
305366 // Check for pending replies to send.
306- if let Some ( ref info) = self . info {
367+ if let Some ( ref info) = self . behaviour_info {
307368 if let Some ( mut pending) = self . pending_replies . pop_front ( ) {
308369 loop {
309370 match pending {
310371 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+ } ;
311380 let io = Box :: pin ( io. send ( info. clone ( ) ) ) ;
312381 pending = Pending :: Sending {
313382 peer : self . remote_peer_id ,
0 commit comments