Skip to content
This repository was archived by the owner on May 26, 2022. It is now read-only.

Commit baef5d3

Browse files
make Protocols return TCP and the security protocol, fix CanDial
1 parent 649bdea commit baef5d3

File tree

4 files changed

+86
-44
lines changed

4 files changed

+86
-44
lines changed

go.mod

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ go 1.16
44

55
require (
66
github.com/ipfs/go-log v1.0.4
7-
github.com/libp2p/go-conn-security-multistream v0.2.1
8-
github.com/libp2p/go-libp2p-core v0.8.6
7+
github.com/libp2p/go-conn-security-multistream v0.3.0
8+
github.com/libp2p/go-libp2p-core v0.10.1-0.20210921170543-f829c09c1ca0
99
github.com/libp2p/go-libp2p-mplex v0.4.1
10-
github.com/libp2p/go-libp2p-testing v0.4.2
11-
github.com/libp2p/go-libp2p-transport-upgrader v0.4.6
10+
github.com/libp2p/go-libp2p-testing v0.5.1-0.20210921173022-d2d7433a5068
11+
github.com/libp2p/go-libp2p-transport-upgrader v0.5.1-0.20210921171606-6c77d6ff1b57
1212
github.com/libp2p/go-netroute v0.1.5 // indirect
1313
github.com/libp2p/go-reuseport v0.0.2
1414
github.com/libp2p/go-reuseport-transport v0.0.5
1515
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd
1616
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b
17-
github.com/multiformats/go-multiaddr v0.3.3
17+
github.com/multiformats/go-multiaddr v0.4.1
1818
github.com/multiformats/go-multiaddr-fmt v0.1.0
1919
github.com/multiformats/go-multihash v0.0.15 // indirect
2020
github.com/prometheus/client_golang v1.10.0

go.sum

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5D
167167
github.com/jbenet/go-temp-err-catcher v0.1.0 h1:zpb3ZH6wIE8Shj2sKS+khgRvf7T7RABoLk/+KKHggpk=
168168
github.com/jbenet/go-temp-err-catcher v0.1.0/go.mod h1:0kJRvmDZXNMIiJirNPEYfhpPwbGVtZVWC34vc5WLsDk=
169169
github.com/jbenet/goprocess v0.1.3/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4=
170-
github.com/jbenet/goprocess v0.1.4 h1:DRGOFReOMqqDNXwW70QkacFW0YN9QnwLV0Vqk+3oU0o=
171170
github.com/jbenet/goprocess v0.1.4/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4=
172171
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
173172
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
@@ -197,27 +196,26 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
197196
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
198197
github.com/libp2p/go-buffer-pool v0.0.2 h1:QNK2iAFa8gjAe1SPz6mHSMuCcjs+X1wlHzeOSqcmlfs=
199198
github.com/libp2p/go-buffer-pool v0.0.2/go.mod h1:MvaB6xw5vOrDl8rYZGLFdKAuk/hRoRZd1Vi32+RXyFM=
200-
github.com/libp2p/go-conn-security-multistream v0.2.1 h1:ft6/POSK7F+vl/2qzegnHDaXFU0iWB4yVTYrioC6Zy0=
201-
github.com/libp2p/go-conn-security-multistream v0.2.1/go.mod h1:cR1d8gA0Hr59Fj6NhaTpFhJZrjSYuNmhpT2r25zYR70=
199+
github.com/libp2p/go-conn-security-multistream v0.3.0 h1:9UCIKlBL1hC9u7nkMXpD1nkc/T53PKMAn3/k9ivBAVc=
200+
github.com/libp2p/go-conn-security-multistream v0.3.0/go.mod h1:EEP47t4fw/bTelVmEzIDqSe69hO/ip52xBEhZMLWAHM=
202201
github.com/libp2p/go-flow-metrics v0.0.3/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs=
203202
github.com/libp2p/go-libp2p-core v0.3.0/go.mod h1:ACp3DmS3/N64c2jDzcV429ukDpicbL6+TrrxANBjPGw=
204203
github.com/libp2p/go-libp2p-core v0.5.0/go.mod h1:49XGI+kc38oGVwqSBhDEwytaAxgZasHhFfQKibzTls0=
205-
github.com/libp2p/go-libp2p-core v0.7.0/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8=
206204
github.com/libp2p/go-libp2p-core v0.8.0/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8=
207-
github.com/libp2p/go-libp2p-core v0.8.1/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8=
208-
github.com/libp2p/go-libp2p-core v0.8.6 h1:3S8g006qG6Tjpj1JdRK2S+TWc2DJQKX/RG9fdLeiLSU=
209-
github.com/libp2p/go-libp2p-core v0.8.6/go.mod h1:dgHr0l0hIKfWpGpqAMbpo19pen9wJfdCGv51mTmdpmM=
205+
github.com/libp2p/go-libp2p-core v0.10.0/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg=
206+
github.com/libp2p/go-libp2p-core v0.10.1-0.20210921170543-f829c09c1ca0 h1:W1EqIm0+QVnfQ2SV138//D9NyAhseDxqLMoWvvhYsQQ=
207+
github.com/libp2p/go-libp2p-core v0.10.1-0.20210921170543-f829c09c1ca0/go.mod h1:KlkHsZ0nKerWsXLZJm3LfFQwusI5k3iN4BgtYTE4IYE=
210208
github.com/libp2p/go-libp2p-mplex v0.4.1 h1:/pyhkP1nLwjG3OM+VuaNJkQT/Pqq73WzB3aDN3Fx1sc=
211209
github.com/libp2p/go-libp2p-mplex v0.4.1/go.mod h1:cmy+3GfqfM1PceHTLL7zQzAAYaryDu6iPSC+CIb094g=
212210
github.com/libp2p/go-libp2p-pnet v0.2.0 h1:J6htxttBipJujEjz1y0a5+eYoiPcFHhSYHH6na5f0/k=
213211
github.com/libp2p/go-libp2p-pnet v0.2.0/go.mod h1:Qqvq6JH/oMZGwqs3N1Fqhv8NVhrdYcO0BW4wssv21LA=
214212
github.com/libp2p/go-libp2p-testing v0.1.2-0.20200422005655-8775583591d8/go.mod h1:Qy8sAncLKpwXtS2dSnDOP8ktexIAHKu+J+pnZOFZLTc=
215-
github.com/libp2p/go-libp2p-testing v0.3.0/go.mod h1:efZkql4UZ7OVsEfaxNHZPzIehtsBXMrXnCfJIgDti5g=
216213
github.com/libp2p/go-libp2p-testing v0.4.0/go.mod h1:Q+PFXYoiYFN5CAEG2w3gLPEzotlKsNSbKQ/lImlOWF0=
217-
github.com/libp2p/go-libp2p-testing v0.4.2 h1:IOiA5mMigi+eEjf4J+B7fepDhsjtsoWA9QbsCqbNp5U=
218-
github.com/libp2p/go-libp2p-testing v0.4.2/go.mod h1:Q+PFXYoiYFN5CAEG2w3gLPEzotlKsNSbKQ/lImlOWF0=
219-
github.com/libp2p/go-libp2p-transport-upgrader v0.4.6 h1:SHt3g0FslnqIkEWF25YOB8UCOCTpGAVvHRWQYJ+veiI=
220-
github.com/libp2p/go-libp2p-transport-upgrader v0.4.6/go.mod h1:JE0WQuQdy+uLZ5zOaI3Nw9dWGYJIA7mywEtP2lMvnyk=
214+
github.com/libp2p/go-libp2p-testing v0.5.0/go.mod h1:QBk8fqIL1XNcno/l3/hhaIEn4aLRijpYOR+zVjjlh+A=
215+
github.com/libp2p/go-libp2p-testing v0.5.1-0.20210921173022-d2d7433a5068 h1:Uhzao0ft5xsws67/qzZNlK1kvQKMwuDj/jr0RHfpU98=
216+
github.com/libp2p/go-libp2p-testing v0.5.1-0.20210921173022-d2d7433a5068/go.mod h1:zcQ6B90GUqkXx0xdQ0EyBH8XUvlP36pxEdpUFtPtWNk=
217+
github.com/libp2p/go-libp2p-transport-upgrader v0.5.1-0.20210921171606-6c77d6ff1b57 h1:Tb0L3v+0YubPIVduck3vSmnNl4RqcZu1z6bsUl2E1FU=
218+
github.com/libp2p/go-libp2p-transport-upgrader v0.5.1-0.20210921171606-6c77d6ff1b57/go.mod h1:4T3Km0CC97JF4T4KH6BjSScdZLvGk/W0WKAm7MAlwYI=
221219
github.com/libp2p/go-maddr-filter v0.1.0/go.mod h1:VzZhTXkMucEGGEOSKddrwGiOv0tUhgnKqNEmIAz/bPU=
222220
github.com/libp2p/go-mplex v0.3.0 h1:U1T+vmCYJaEoDJPV1aq31N56hS+lJgb397GsylNSgrU=
223221
github.com/libp2p/go-mplex v0.3.0/go.mod h1:0Oy/A9PQlwBytDRp4wSkFnzHYDKcpLot35JQ6msjvYQ=
@@ -291,8 +289,9 @@ github.com/multiformats/go-multiaddr v0.2.1/go.mod h1:s/Apk6IyxfvMjDafnhJgJ3/46z
291289
github.com/multiformats/go-multiaddr v0.2.2/go.mod h1:NtfXiOtHvghW9KojvtySjH5y0u0xW5UouOmQQrn6a3Y=
292290
github.com/multiformats/go-multiaddr v0.3.0/go.mod h1:dF9kph9wfJ+3VLAaeBqo9Of8x4fJxp6ggJGteB8HQTI=
293291
github.com/multiformats/go-multiaddr v0.3.1/go.mod h1:uPbspcUPd5AfaP6ql3ujFY+QWzmBD8uLLL4bXW0XfGc=
294-
github.com/multiformats/go-multiaddr v0.3.3 h1:vo2OTSAqnENB2rLk79pLtr+uhj+VAzSe3uef5q0lRSs=
295292
github.com/multiformats/go-multiaddr v0.3.3/go.mod h1:lCKNGP1EQ1eZ35Za2wlqnabm9xQkib3fyB+nZXHLag0=
293+
github.com/multiformats/go-multiaddr v0.4.1 h1:Pq37uLx3hsyNlTDir7FZyU8+cFCTqd5y1KiM2IzOutI=
294+
github.com/multiformats/go-multiaddr v0.4.1/go.mod h1:3afI9HfVW8csiF8UZqtpYRiDyew8pRX7qLIGHu9FLuM=
296295
github.com/multiformats/go-multiaddr-fmt v0.1.0 h1:WLEFClPycPkp4fnIzoFoV9FVd49/eQsuaL3/CWe167E=
297296
github.com/multiformats/go-multiaddr-fmt v0.1.0/go.mod h1:hGtDIW4PU4BqJ50gW2quDuPVjyWNZxToGUh/HwTZYJo=
298297
github.com/multiformats/go-multiaddr-net v0.2.0/go.mod h1:gGdH3UXny6U3cKKYCvpXI5rnK7YaOIEOPVDI9tsJbEA=
@@ -410,8 +409,9 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
410409
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
411410
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
412411
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
413-
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
414412
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
413+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
414+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
415415
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
416416
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
417417
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
@@ -599,8 +599,9 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
599599
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
600600
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
601601
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
602-
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
603602
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
603+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
604+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
604605
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
605606
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
606607
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

tcp.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ type TcpTransport struct {
9999
// TCP connect timeout
100100
ConnectTimeout time.Duration
101101

102+
protos []ma.Protocol
103+
dialMatcher mafmt.Pattern
104+
102105
reuse rtpt.Transport
103106
}
104107

@@ -107,15 +110,27 @@ var _ transport.Transport = &TcpTransport{}
107110
// NewTCPTransport creates a tcp transport object that tracks dialers and listeners
108111
// created. It represents an entire tcp stack (though it might not necessarily be)
109112
func NewTCPTransport(upgrader *tptu.Upgrader) *TcpTransport {
110-
return &TcpTransport{Upgrader: upgrader, ConnectTimeout: DefaultConnectTimeout}
113+
protos := []ma.Protocol{ma.ProtocolWithCode(ma.P_TCP)}
114+
var dialMatcher mafmt.Pattern
115+
// In case the upgrader muxes security protocols, it returns the zero value of ma.Protocol.
116+
// This check can be removed once we remove support for non-security-enabled multiaddrs.
117+
if secProto := upgrader.SecurityProtocol(); secProto.Code != 0 {
118+
protos = append(protos, secProto)
119+
dialMatcher = mafmt.And(mafmt.IP, mafmt.Base(ma.P_TCP), mafmt.Base(secProto.Code))
120+
} else {
121+
dialMatcher = mafmt.And(mafmt.IP, mafmt.Base(ma.P_TCP))
122+
}
123+
return &TcpTransport{
124+
Upgrader: upgrader,
125+
ConnectTimeout: DefaultConnectTimeout,
126+
protos: protos,
127+
dialMatcher: dialMatcher,
128+
}
111129
}
112130

113-
var dialMatcher = mafmt.And(mafmt.IP, mafmt.Base(ma.P_TCP))
114-
115-
// CanDial returns true if this transport believes it can dial the given
116-
// multiaddr.
131+
// CanDial returns true if this transport believes it can dial the given multiaddr.
117132
func (t *TcpTransport) CanDial(addr ma.Multiaddr) bool {
118-
return dialMatcher.Matches(addr)
133+
return t.dialMatcher.Matches(addr)
119134
}
120135

121136
func (t *TcpTransport) maDial(ctx context.Context, raddr ma.Multiaddr) (manet.Conn, error) {
@@ -173,9 +188,9 @@ func (t *TcpTransport) Listen(laddr ma.Multiaddr) (transport.Listener, error) {
173188
return t.Upgrader.UpgradeListener(t, list), nil
174189
}
175190

176-
// Protocols returns the list of terminal protocols this transport can dial.
177-
func (t *TcpTransport) Protocols() []int {
178-
return []int{ma.P_TCP}
191+
// Protocols returns the protocols this transport can dial.
192+
func (t *TcpTransport) Protocols() []ma.Protocol {
193+
return t.protos
179194
}
180195

181196
// Proxy always returns false for the TCP transport.

tcp_test.go

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ import (
1515
ma "github.com/multiformats/go-multiaddr"
1616
)
1717

18-
func TestTcpTransport(t *testing.T) {
18+
func TestTcpTransportWithSecureMuxer(t *testing.T) {
1919
for i := 0; i < 2; i++ {
2020
peerA, ia := makeInsecureMuxer(t)
2121
_, ib := makeInsecureMuxer(t)
2222

2323
ta := NewTCPTransport(&tptu.Upgrader{
24-
Secure: ia,
25-
Muxer: new(mplex.Transport),
24+
SecureMuxer: ia,
25+
Muxer: new(mplex.Transport),
2626
})
2727
tb := NewTCPTransport(&tptu.Upgrader{
28-
Secure: ib,
29-
Muxer: new(mplex.Transport),
28+
SecureMuxer: ib,
29+
Muxer: new(mplex.Transport),
3030
})
3131

3232
zero := "/ip4/127.0.0.1/tcp/0"
@@ -37,6 +37,28 @@ func TestTcpTransport(t *testing.T) {
3737
envReuseportVal = true
3838
}
3939

40+
func TestTcpTransportWithSecureTransport(t *testing.T) {
41+
for i := 0; i < 2; i++ {
42+
peerA, ia := makeInsecureTransport(t)
43+
_, ib := makeInsecureTransport(t)
44+
45+
ta := NewTCPTransport(&tptu.Upgrader{
46+
SecureTransport: ia,
47+
Muxer: new(mplex.Transport),
48+
})
49+
tb := NewTCPTransport(&tptu.Upgrader{
50+
SecureTransport: ib,
51+
Muxer: new(mplex.Transport),
52+
})
53+
54+
zero := "/ip4/127.0.0.1/tcp/0/plaintextv2"
55+
ttransport.SubtestTransport(t, ta, tb, zero, peerA)
56+
57+
envReuseportVal = false
58+
}
59+
envReuseportVal = true
60+
}
61+
4062
func TestTcpTransportCantDialDNS(t *testing.T) {
4163
for i := 0; i < 2; i++ {
4264
dnsa, err := ma.NewMultiaddr("/dns4/example.com/tcp/1234")
@@ -46,8 +68,8 @@ func TestTcpTransportCantDialDNS(t *testing.T) {
4668

4769
_, sm := makeInsecureMuxer(t)
4870
tpt := NewTCPTransport(&tptu.Upgrader{
49-
Secure: sm,
50-
Muxer: new(mplex.Transport),
71+
SecureMuxer: sm,
72+
Muxer: new(mplex.Transport),
5173
})
5274

5375
if tpt.CanDial(dnsa) {
@@ -68,12 +90,10 @@ func TestTcpTransportCantListenUtp(t *testing.T) {
6890

6991
_, sm := makeInsecureMuxer(t)
7092
tpt := NewTCPTransport(&tptu.Upgrader{
71-
Secure: sm,
72-
Muxer: new(mplex.Transport),
93+
SecureMuxer: sm,
94+
Muxer: new(mplex.Transport),
7395
})
74-
75-
_, err = tpt.Listen(utpa)
76-
if err == nil {
96+
if _, err := tpt.Listen(utpa); err == nil {
7797
t.Fatal("shouldnt be able to listen on utp addr with tcp transport")
7898
}
7999

@@ -83,6 +103,14 @@ func TestTcpTransportCantListenUtp(t *testing.T) {
83103
}
84104

85105
func makeInsecureMuxer(t *testing.T) (peer.ID, sec.SecureMuxer) {
106+
t.Helper()
107+
id, tr := makeInsecureTransport(t)
108+
var secMuxer csms.SSMuxer
109+
secMuxer.AddTransport(insecure.ID, tr)
110+
return id, &secMuxer
111+
}
112+
113+
func makeInsecureTransport(t *testing.T) (peer.ID, sec.SecureTransport) {
86114
t.Helper()
87115
priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, 256)
88116
if err != nil {
@@ -92,7 +120,5 @@ func makeInsecureMuxer(t *testing.T) (peer.ID, sec.SecureMuxer) {
92120
if err != nil {
93121
t.Fatal(err)
94122
}
95-
var secMuxer csms.SSMuxer
96-
secMuxer.AddTransport(insecure.ID, insecure.NewWithIdentity(id, priv))
97-
return id, &secMuxer
123+
return id, insecure.NewWithIdentity(id, priv)
98124
}

0 commit comments

Comments
 (0)