diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 8da52c0cd028dd7fae58218eb17587e4ed04e5c3..08fbf95c3586e62adf97a53717d248e0a348cf49 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -1,3 +1,6 @@ +// Package identify provides an identity service for libp2p hosts. This service +// allows to exchange information about supported protocols and observed +// addresses between hosts. package identify import ( @@ -31,6 +34,7 @@ const ID = "/ipfs/id/1.0.0" // TODO(jbenet): fix the versioning mess. const LibP2PVersion = "ipfs/0.1.0" +// ClientVersion is similar to a UserAgent string. var ClientVersion = "go-libp2p/3.3.4" // IDService is a structure that implements ProtocolIdentify. @@ -55,6 +59,9 @@ type IDService struct { observedAddrs ObservedAddrSet } +// NewIDService creates a new IDService by attaching +// a stream handler to the given host, making it ready to interact +// with other hosts running this service. func NewIDService(h host.Host) *IDService { s := &IDService{ Host: h, @@ -69,6 +76,9 @@ func (ids *IDService) OwnObservedAddrs() []ma.Multiaddr { return ids.observedAddrs.Addrs() } +// IdentifyConn handles newly stablished connections, pushes and extracts +// the necessary information so they can be used: protocol, observed addresses, +// public key... The IDService's host peerstore is updated after the handshake. func (ids *IDService) IdentifyConn(c inet.Conn) { ids.currmu.Lock() if wait, found := ids.currid[c]; found { @@ -117,6 +127,9 @@ func (ids *IDService) IdentifyConn(c inet.Conn) { } } +// RequestHandler handles an identity service request +// by writing protocol version, host addresses, observed addresses +// etc. to the stream. func (ids *IDService) RequestHandler(s inet.Stream) { defer s.Close() c := s.Conn() @@ -134,6 +147,9 @@ func (ids *IDService) RequestHandler(s inet.Stream) { c.RemotePeer(), c.RemoteMultiaddr()) } +// ResponseHandler handles an identity service response +// by reading the message information and updating the +// IDService's host accordingly. func (ids *IDService) ResponseHandler(s inet.Stream) { defer s.Close() c := s.Conn() diff --git a/p2p/protocol/identify/obsaddr.go b/p2p/protocol/identify/obsaddr.go index 72263e37631c093afbb76264bad4fae483d857e9..2ce32badf6fecdb6c6ccccd29ea5f5af3e7930c6 100644 --- a/p2p/protocol/identify/obsaddr.go +++ b/p2p/protocol/identify/obsaddr.go @@ -28,6 +28,8 @@ type ObservedAddrSet struct { ttl time.Duration } +// Addrs returns a slice of multiaddresses observed. The addresses +// must have not timed out and must have been seen more than once. func (oas *ObservedAddrSet) Addrs() []ma.Multiaddr { oas.Lock() defer oas.Unlock() @@ -62,6 +64,7 @@ func (oas *ObservedAddrSet) Addrs() []ma.Multiaddr { return addrs } +// Add includes a new multiaddress in the observed addresses set. func (oas *ObservedAddrSet) Add(addr ma.Multiaddr, observer ma.Multiaddr) { oas.Lock() defer oas.Unlock() @@ -103,12 +106,15 @@ func observerGroup(m ma.Multiaddr) string { return ma.Split(m)[0].String() } +// SetTTL sets the time to live for addresses in the ObservedAddrSet. func (oas *ObservedAddrSet) SetTTL(ttl time.Duration) { oas.Lock() defer oas.Unlock() oas.ttl = ttl } +// TTL returns the time to live for addressed tracked by the +// ObservedAddrSet. If not set, it defaults to peerstore.OwnObservedTTL. func (oas *ObservedAddrSet) TTL() time.Duration { oas.Lock() defer oas.Unlock() diff --git a/p2p/protocol/identify/pb/identify.proto b/p2p/protocol/identify/pb/identify.proto index 7d31e0474a97d3a21c692fab6c97aabbf6060daf..e70d7950d5a3576b891e8f3d60a9e4acedcb570f 100644 --- a/p2p/protocol/identify/pb/identify.proto +++ b/p2p/protocol/identify/pb/identify.proto @@ -6,7 +6,7 @@ message Identify { optional string protocolVersion = 5; // e.g. ipfs/1.0.0 // agentVersion is like a UserAgent string in browsers, or client version in bittorrent - // includes the client name and client. + // includes the client name and client version. optional string agentVersion = 6; // e.g. go-ipfs/0.1.0 // publicKey is this node's public key (which also gives its node.ID)