interface.go 2.72 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1
2
3
4
5
6
7
package conn

import (
	"io"
	"net"
	"time"

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8
9
	ic "github.com/ipfs/go-libp2p/p2p/crypto"
	filter "github.com/ipfs/go-libp2p/p2p/net/filter"
Jeromy's avatar
Jeromy committed
10
	transport "github.com/ipfs/go-libp2p/p2p/net/transport"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11
	peer "github.com/ipfs/go-libp2p/p2p/peer"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12

Jeromy's avatar
Jeromy committed
13
	ma "gx/ipfs/QmR3JkmZBKYXgNMNsNZawm914455Qof3PEopwuVSeXG7aV/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14
15
16
)

type PeerConn interface {
17
18
	io.Closer

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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
	// LocalPeer (this side) ID, PrivateKey, and Address
	LocalPeer() peer.ID
	LocalPrivateKey() ic.PrivKey
	LocalMultiaddr() ma.Multiaddr

	// RemotePeer ID, PublicKey, and Address
	RemotePeer() peer.ID
	RemotePublicKey() ic.PubKey
	RemoteMultiaddr() ma.Multiaddr
}

// Conn is a generic message-based Peer-to-Peer connection.
type Conn interface {
	PeerConn

	// ID is an identifier unique to this connection.
	ID() string

	// can't just say "net.Conn" cause we have duplicate methods.
	LocalAddr() net.Addr
	RemoteAddr() net.Addr
	SetDeadline(t time.Time) error
	SetReadDeadline(t time.Time) error
	SetWriteDeadline(t time.Time) error

Jeromy's avatar
Jeromy committed
44
45
	io.Reader
	io.Writer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46
47
48
49
50
51
52
53
54
55
}

// Dialer is an object that can open connections. We could have a "convenience"
// Dial function as before, but it would have many arguments, as dialing is
// no longer simple (need a peerstore, a local peer, a context, a network, etc)
type Dialer struct {
	// LocalPeer is the identity of the local Peer.
	LocalPeer peer.ID

	// LocalAddrs is a set of local addresses to use.
Jeromy's avatar
Jeromy committed
56
57
58
59
60
	//LocalAddrs []ma.Multiaddr

	// Dialers are the sub-dialers usable by this dialer
	// selected in order based on the address being dialed
	Dialers []transport.Dialer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
61
62
63
64

	// PrivateKey used to initialize a secure connection.
	// Warning: if PrivateKey is nil, connection will not be secured.
	PrivateKey ic.PrivKey
Jeromy's avatar
Jeromy committed
65
66

	// Wrapper to wrap the raw connection (optional)
Jeromy's avatar
Jeromy committed
67
	Wrapper WrapFunc
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
}

// Listener is an object that can accept connections. It matches net.Listener
type Listener interface {

	// Accept waits for and returns the next connection to the listener.
	Accept() (net.Conn, error)

	// Addr is the local address
	Addr() net.Addr

	// Multiaddr is the local multiaddr address
	Multiaddr() ma.Multiaddr

	// LocalPeer is the identity of the local Peer.
	LocalPeer() peer.ID

85
86
	SetAddrFilters(*filter.Filters)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87
88
89
90
	// Close closes the listener.
	// Any blocked Accept operations will be unblocked and return errors.
	Close() error
}
91
92
93
94
95
96
97
98

// EncryptConnections is a global parameter because it should either be
// enabled or _completely disabled_. I.e. a node should only be able to talk
// to proper (encrypted) networks if it is encrypting all its transports.
// Running a node with disabled transport encryption is useful to debug the
// protocols, achieve implementation interop, or for private networks which
// -- for whatever reason -- _must_ run unencrypted.
var EncryptConnections = true