Skip to content

Commit 7f5ce40

Browse files
jpserratMarcoPolo
andauthored
cleanup dcutr legacy behavior and add fallback
fallback will get triggered during the last attempt at holepunching. Co-authored-by: Marco Munizaga <marco@marcopolo.io>
1 parent 46986fc commit 7f5ce40

File tree

3 files changed

+10
-45
lines changed

3 files changed

+10
-45
lines changed

p2p/protocol/holepunch/holepunch_test.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func TestEndToEndSimConnect(t *testing.T) {
250250

251251
h1 := MustNewHost(t,
252252
quicSimnet(false, router),
253-
libp2p.EnableHolePunching(holepunch.WithTracer(h1tr), holepunch.DirectDialTimeout(100*time.Millisecond), SetLegacyBehavior(useLegacyHolePunchingBehavior)),
253+
libp2p.EnableHolePunching(holepunch.WithTracer(h1tr), holepunch.DirectDialTimeout(100*time.Millisecond)),
254254
libp2p.ListenAddrs(ma.StringCast("/ip4/2.2.0.1/udp/8000/quic-v1")),
255255
libp2p.ResourceManager(&network.NullResourceManager{}),
256256
libp2p.ForceReachabilityPrivate(),
@@ -261,7 +261,7 @@ func TestEndToEndSimConnect(t *testing.T) {
261261
libp2p.ListenAddrs(ma.StringCast("/ip4/2.2.0.2/udp/8001/quic-v1")),
262262
libp2p.ResourceManager(&network.NullResourceManager{}),
263263
connectToRelay(&relay),
264-
libp2p.EnableHolePunching(holepunch.WithTracer(h2tr), holepunch.DirectDialTimeout(100*time.Millisecond), SetLegacyBehavior(useLegacyHolePunchingBehavior)),
264+
libp2p.EnableHolePunching(holepunch.WithTracer(h2tr), holepunch.DirectDialTimeout(100*time.Millisecond)),
265265
libp2p.ForceReachabilityPrivate(),
266266
)
267267

@@ -660,20 +660,6 @@ func waitForHolePunchingSvcActive(t *testing.T, h host.Host) {
660660
}, time.Second, 100*time.Millisecond)
661661
}
662662

663-
// setLegacyBehavior is an option that controls the isClient behavior of the hole punching service.
664-
// Prior to https://github.com/libp2p/go-libp2p/pull/3044, go-libp2p would
665-
// pick the opposite roles for client/server a hole punch. Setting this to
666-
// true preserves that behavior.
667-
//
668-
// Currently, only exposed for testing purposes.
669-
// Do not set this unless you know what you are doing.
670-
func SetLegacyBehavior(legacyBehavior bool) holepunch.Option {
671-
return func(s *holepunch.Service) error {
672-
s.SetLegacyBehavior(legacyBehavior)
673-
return nil
674-
}
675-
}
676-
677663
// TestEndToEndSimConnectQUICReuse tests that hole punching works if we are
678664
// reusing the same port for QUIC and WebTransport, and when we have multiple
679665
// QUIC listeners on different ports.

p2p/protocol/holepunch/holepuncher.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ type holePuncher struct {
4848

4949
tracer *tracer
5050
filter AddrFilter
51-
52-
// Prior to https://github.com/libp2p/go-libp2p/pull/3044, go-libp2p would
53-
// pick the opposite roles for client/server a hole punch. Setting this to
54-
// true preserves that behavior
55-
legacyBehavior bool
5651
}
5752

5853
func newHolePuncher(h host.Host, ids identify.IDService, listenAddrs func() []ma.Multiaddr, tracer *tracer, filter AddrFilter) *holePuncher {
@@ -63,8 +58,6 @@ func newHolePuncher(h host.Host, ids identify.IDService, listenAddrs func() []ma
6358
tracer: tracer,
6459
filter: filter,
6560
listenAddrs: listenAddrs,
66-
67-
legacyBehavior: true,
6861
}
6962
hp.ctx, hp.ctxCancel = context.WithCancel(context.Background())
7063
h.Network().Notify((*netNotifiee)(hp))
@@ -141,6 +134,13 @@ func (hp *holePuncher) directConnect(rp peer.ID) error {
141134

142135
// hole punch
143136
for i := 1; i <= maxRetries; i++ {
137+
isClient := false
138+
// On the last attempt we switch roles in case the connection is
139+
// being made with a client with switched roles. Common for peers
140+
// running go-libp2p prior to v0.41.
141+
if i == maxRetries {
142+
isClient = true
143+
}
144144
addrs, obsAddrs, rtt, err := hp.initiateHolePunch(rp)
145145
if err != nil {
146146
hp.tracer.ProtocolError(rp, err)
@@ -161,10 +161,6 @@ func (hp *holePuncher) directConnect(rp peer.ID) error {
161161
hp.tracer.StartHolePunch(rp, addrs, rtt)
162162
hp.tracer.HolePunchAttempt(pi.ID)
163163
ctx, cancel := context.WithTimeout(hp.ctx, hp.directDialTimeout)
164-
isClient := true
165-
if hp.legacyBehavior {
166-
isClient = false
167-
}
168164
err := holePunchConnect(ctx, hp.host, pi, isClient)
169165
cancel()
170166
dt := time.Since(start)

p2p/protocol/holepunch/svc.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,6 @@ type Service struct {
7171
filter AddrFilter
7272

7373
refCount sync.WaitGroup
74-
75-
// Prior to https://github.com/libp2p/go-libp2p/pull/3044, go-libp2p would
76-
// pick the opposite roles for client/server a hole punch. Setting this to
77-
// true preserves that behavior
78-
legacyBehavior bool
79-
}
80-
81-
// SetLegacyBehavior is only exposed for testing purposes.
82-
// Do not set this unless you know what you are doing.
83-
func (s *Service) SetLegacyBehavior(legacyBehavior bool) {
84-
s.legacyBehavior = legacyBehavior
8574
}
8675

8776
// NewService creates a new service that can be used for hole punching
@@ -105,7 +94,6 @@ func NewService(h host.Host, ids identify.IDService, listenAddrs func() []ma.Mul
10594
listenAddrs: listenAddrs,
10695
hasPublicAddrsChan: make(chan struct{}),
10796
directDialTimeout: defaultDirectDialTimeout,
108-
legacyBehavior: true,
10997
}
11098

11199
for _, opt := range opts {
@@ -161,7 +149,6 @@ func (s *Service) waitForPublicAddr() {
161149
}
162150
s.holePuncher = newHolePuncher(s.host, s.ids, s.listenAddrs, s.tracer, s.filter)
163151
s.holePuncher.directDialTimeout = s.directDialTimeout
164-
s.holePuncher.legacyBehavior = s.legacyBehavior
165152
s.holePuncherMx.Unlock()
166153
close(s.hasPublicAddrsChan)
167154
}
@@ -284,11 +271,7 @@ func (s *Service) handleNewStream(str network.Stream) {
284271
start := time.Now()
285272
s.tracer.HolePunchAttempt(pi.ID)
286273
ctx, cancel := context.WithTimeout(s.ctx, s.directDialTimeout)
287-
isClient := false
288-
if s.legacyBehavior {
289-
isClient = true
290-
}
291-
err = holePunchConnect(ctx, s.host, pi, isClient)
274+
err = holePunchConnect(ctx, s.host, pi, true) // true (Client)
292275
cancel()
293276
dt := time.Since(start)
294277
s.tracer.EndHolePunch(rp, dt, err)

0 commit comments

Comments
 (0)