Skip to content

Commit

Permalink
Merge pull request #7 from libp2p/fix/map-usable
Browse files Browse the repository at this point in the history
only map *usable* addresses
  • Loading branch information
Stebalien authored Sep 1, 2018
2 parents 0ee4048 + 0ec0019 commit 3011636
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions p2p/net/nat/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nat
import (
"errors"
"fmt"
"net"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -125,15 +126,40 @@ func (nat *NAT) NewMapping(maddr ma.Multiaddr) (Mapping, error) {
return nil, fmt.Errorf("DialArgs failed on addr: %s", maddr.String())
}

var ip net.IP
switch network {
case "tcp", "tcp4", "tcp6":
addr, err := net.ResolveTCPAddr(network, addr)
if err != nil {
return nil, err
}
ip = addr.IP
network = "tcp"
case "udp", "udp4", "udp6":
addr, err := net.ResolveUDPAddr(network, addr)
if err != nil {
return nil, err
}
ip = addr.IP
network = "udp"
default:
return nil, fmt.Errorf("transport not supported by NAT: %s", network)
}

// XXX: Known limitation: doesn't handle multiple internal addresses.
// If this applies to you, you can figure it out yourself. Ideally, the
// NAT library would allow us to handle this case but the "go way"
// appears to be to just "shrug" at edge-cases.
if !ip.IsUnspecified() {
internalAddr, err := nat.nat.GetInternalAddress()
if err != nil {
return nil, fmt.Errorf("failed to discover address on nat: %s", err)
}
if !ip.Equal(internalAddr) {
return nil, fmt.Errorf("nat address is %s, refusing to map %s", internalAddr, ip)
}
}

intports := strings.Split(addr, ":")[1]
intport, err := strconv.Atoi(intports)
if err != nil {
Expand Down

0 comments on commit 3011636

Please sign in to comment.