conn.go 3.61 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1
2
3
4
package conn

import (
	"fmt"
5
	"io"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6
7
8
	"net"
	"time"

Jeromy's avatar
Jeromy committed
9
	manet "QmU5s159q8cZuM1f9Vqti4LHu6y8zyVc5dxv2py81sdp6Q/go-multiaddr-net"
Jeromy's avatar
Jeromy committed
10
	logging "QmWRypnfEwrgH4k93KEHN5hng7VjKYkWmzDYRuTZeh2Mgh/go-log"
Jeromy's avatar
Jeromy committed
11
	context "QmacZi9WygGK7Me8mH53pypyscHzU386aUZXpr28GZgUct/context"
Jeromy's avatar
Jeromy committed
12
	ma "QmbWxL1aXQhBjc1XGjGF1f2KGBMCBYSuT2ThA8YXnXJK83/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13
14
	ic "github.com/ipfs/go-libp2p/p2p/crypto"
	peer "github.com/ipfs/go-libp2p/p2p/peer"
Jeromy's avatar
Jeromy committed
15
	mpool "github.com/jbenet/go-msgio/mpool"
Jeromy's avatar
Jeromy committed
16
17
	u "util"
	lgbl "util/eventlog/loggables"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18
19
)

Jeromy's avatar
Jeromy committed
20
var log = logging.Logger("conn")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21
22
23
24
25
26
27
28
29
30
31
32
33

// ReleaseBuffer puts the given byte array back into the buffer pool,
// first verifying that it is the correct size
func ReleaseBuffer(b []byte) {
	log.Debugf("Releasing buffer! (cap,size = %d, %d)", cap(b), len(b))
	mpool.ByteSlicePool.Put(uint32(cap(b)), b)
}

// singleConn represents a single connection to another Peer (IPFS Node).
type singleConn struct {
	local  peer.ID
	remote peer.ID
	maconn manet.Conn
34
	event  io.Closer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35
36
37
38
}

// newConn constructs a new connection
func newSingleConn(ctx context.Context, local, remote peer.ID, maconn manet.Conn) (Conn, error) {
39
	ml := lgbl.Dial("conn", local, remote, maconn.LocalMultiaddr(), maconn.RemoteMultiaddr())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40
41
42
43
44

	conn := &singleConn{
		local:  local,
		remote: remote,
		maconn: maconn,
45
		event:  log.EventBegin(ctx, "connLifetime", ml),
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46
47
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
48
	log.Debugf("newSingleConn %p: %v to %v", conn, local, remote)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49
50
51
52
53
	return conn, nil
}

// close is the internal close function, called by ContextCloser.Close
func (c *singleConn) Close() error {
54
55
56
57
58
59
60
	defer func() {
		if c.event != nil {
			c.event.Close()
			c.event = nil
		}
	}()

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
61
	// close underlying connection
Jeromy's avatar
Jeromy committed
62
	return c.maconn.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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
}

// ID is an identifier unique to this connection.
func (c *singleConn) ID() string {
	return ID(c)
}

func (c *singleConn) String() string {
	return String(c, "singleConn")
}

func (c *singleConn) LocalAddr() net.Addr {
	return c.maconn.LocalAddr()
}

func (c *singleConn) RemoteAddr() net.Addr {
	return c.maconn.RemoteAddr()
}

func (c *singleConn) LocalPrivateKey() ic.PrivKey {
	return nil
}

func (c *singleConn) RemotePublicKey() ic.PubKey {
	return nil
}

func (c *singleConn) SetDeadline(t time.Time) error {
	return c.maconn.SetDeadline(t)
}
func (c *singleConn) SetReadDeadline(t time.Time) error {
	return c.maconn.SetReadDeadline(t)
}

func (c *singleConn) SetWriteDeadline(t time.Time) error {
	return c.maconn.SetWriteDeadline(t)
}

// LocalMultiaddr is the Multiaddr on this side
func (c *singleConn) LocalMultiaddr() ma.Multiaddr {
	return c.maconn.LocalMultiaddr()
}

// RemoteMultiaddr is the Multiaddr on the remote side
func (c *singleConn) RemoteMultiaddr() ma.Multiaddr {
	return c.maconn.RemoteMultiaddr()
}

// LocalPeer is the Peer on this side
func (c *singleConn) LocalPeer() peer.ID {
	return c.local
}

// RemotePeer is the Peer on the remote side
func (c *singleConn) RemotePeer() peer.ID {
	return c.remote
}

// Read reads data, net.Conn style
func (c *singleConn) Read(buf []byte) (int, error) {
Jeromy's avatar
Jeromy committed
123
	return c.maconn.Read(buf)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
124
125
126
127
}

// Write writes data, net.Conn style
func (c *singleConn) Write(buf []byte) (int, error) {
Jeromy's avatar
Jeromy committed
128
	return c.maconn.Write(buf)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
129
130
131
132
133
134
135
136
137
}

// ID returns the ID of a given Conn.
func ID(c Conn) string {
	l := fmt.Sprintf("%s/%s", c.LocalMultiaddr(), c.LocalPeer().Pretty())
	r := fmt.Sprintf("%s/%s", c.RemoteMultiaddr(), c.RemotePeer().Pretty())
	lh := u.Hash([]byte(l))
	rh := u.Hash([]byte(r))
	ch := u.XOR(lh, rh)
138
	return peer.ID(ch).Pretty()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
139
140
141
142
143
144
145
}

// String returns the user-friendly String representation of a conn
func String(c Conn, typ string) string {
	return fmt.Sprintf("%s (%s) <-- %s %p --> (%s) %s",
		c.LocalPeer(), c.LocalMultiaddr(), typ, c, c.RemoteMultiaddr(), c.RemotePeer())
}