Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding MapPort function to BasicHost #1

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package host

import (
"context"
"net"

"github.com/libp2p/go-libp2p/core/connmgr"
"github.com/libp2p/go-libp2p/core/event"
Expand Down Expand Up @@ -72,4 +73,7 @@ type Host interface {

// EventBus returns the hosts eventbus
EventBus() event.Bus

// MapPort is a utility function that attempts to set up a port mapping
MapPort(protocol string, internalPort int) (net.Addr, int, error)
}
15 changes: 15 additions & 0 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,21 @@ func (h *BasicHost) Close() error {
return nil
}

func (h *BasicHost) MapPort(protocol string, internalPort int) (net.Addr, int, error) {
for h.natmgr == nil || h.natmgr.NAT() == nil {
time.Sleep(time.Millisecond * 100)
}
mapping, err := h.natmgr.NAT().NewMapping(protocol, internalPort)
if err != nil {
return nil, 0, err
}
addr, err := mapping.ExternalAddr()
if err != nil {
return nil, 0, err
}
return addr, mapping.ExternalPort(), nil
}

type streamWrapper struct {
network.Stream
rw io.ReadWriteCloser
Expand Down
21 changes: 18 additions & 3 deletions p2p/host/basic/natmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import (
ma "github.com/multiformats/go-multiaddr"
)

// discoveryNATPeriod is the period at which we try to discover NATs.
var discoveryNATPeriod = 3 * time.Second

// discoveryTry is the number of times we try to discover NATs.
const discoveryTry = 5

// NATManager is a simple interface to manage NAT devices.
type NATManager interface {
// NAT gets the NAT device managed by the NAT manager.
Expand Down Expand Up @@ -88,11 +94,20 @@ func (nmgr *natManager) background(ctx context.Context) {

discoverCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
tryCount := 0
natInstance, err := inat.DiscoverNAT(discoverCtx)
if err != nil {
tryCount++
for err != nil {
log.Info("DiscoverNAT error:", err)
close(nmgr.ready)
return
if tryCount > discoveryTry {
log.Info("DiscoverNAT failed after ", tryCount, " tries")
return
}
time.Sleep(discoveryNATPeriod)
discoverCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
natInstance, err = inat.DiscoverNAT(discoverCtx)
tryCount++
}

nmgr.natMx.Lock()
Expand Down
5 changes: 5 additions & 0 deletions p2p/host/blank/blank.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"net"

"github.com/libp2p/go-libp2p/core/connmgr"
"github.com/libp2p/go-libp2p/core/event"
Expand Down Expand Up @@ -227,3 +228,7 @@ func (bh *BlankHost) ConnManager() connmgr.ConnManager {
func (bh *BlankHost) EventBus() event.Bus {
return bh.eventbus
}

func (bh *BlankHost) MapPort(protocol string, internalPort int) (net.Addr, int, error) {
return nil, 0, nil
}
5 changes: 5 additions & 0 deletions p2p/host/routed/routed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package routedhost
import (
"context"
"fmt"
"net"
"time"

"github.com/libp2p/go-libp2p/core/connmgr"
Expand Down Expand Up @@ -219,4 +220,8 @@ func (rh *RoutedHost) ConnManager() connmgr.ConnManager {
return rh.host.ConnManager()
}

func (rh *RoutedHost) MapPort(protocol string, internalPort int) (net.Addr, int, error) {
return rh.host.MapPort(protocol, internalPort)
}

var _ (host.Host) = (*RoutedHost)(nil)