Skip to content

Commit 1543833

Browse files
committed
p2p/nat: new package for port mapping stuff
I have verified that UPnP and NAT-PMP work against an older version of the MiniUPnP daemon running on pfSense. This code is kind of hard to test automatically.
1 parent 4242b05 commit 1543833

3 files changed

Lines changed: 499 additions & 0 deletions

File tree

‎p2p/nat/nat.go‎

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
// Package nat provides access to common port mapping protocols.
2+
package nat
3+
4+
import (
5+
"errors"
6+
"fmt"
7+
"net"
8+
"strings"
9+
"sync"
10+
"time"
11+
12+
"github.com/ethereum/go-ethereum/logger"
13+
"github.com/jackpal/go-nat-pmp"
14+
)
15+
16+
var log = logger.NewLogger("P2P NAT")
17+
18+
// An implementation of nat.Interface can map local ports to ports
19+
// accessible from the Internet.
20+
type Interface interface {
21+
// These methods manage a mapping between a port on the local
22+
// machine to a port that can be connected to from the internet.
23+
//
24+
// protocol is "UDP" or "TCP". Some implementations allow setting
25+
// a display name for the mapping. The mapping may be removed by
26+
// the gateway when its lifetime ends.
27+
AddMapping(protocol string, extport, intport int, name string, lifetime time.Duration) error
28+
DeleteMapping(protocol string, extport, intport int) error
29+
30+
// This method should return the external (Internet-facing)
31+
// address of the gateway device.
32+
ExternalIP() (net.IP, error)
33+
34+
// Should return name of the method. This is used for logging.
35+
String() string
36+
}
37+
38+
// Parse parses a NAT interface description.
39+
// The following formats are currently accepted.
40+
// Note that mechanism names are not case-sensitive.
41+
//
42+
// "" or "none" return nil
43+
// "extip:77.12.33.4" will assume the local machine is reachable on the given IP
44+
// "any" uses the first auto-detected mechanism
45+
// "upnp" uses the Universal Plug and Play protocol
46+
// "pmp" uses NAT-PMP with an auto-detected gateway address
47+
// "pmp:192.168.0.1" uses NAT-PMP with the given gateway address
48+
func Parse(spec string) (Interface, error) {
49+
var (
50+
parts = strings.SplitN(spec, ":", 2)
51+
mech = strings.ToLower(parts[0])
52+
ip net.IP
53+
)
54+
if len(parts) > 1 {
55+
ip = net.ParseIP(parts[1])
56+
if ip == nil {
57+
return nil, errors.New("invalid IP address")
58+
}
59+
}
60+
switch mech {
61+
case "", "none", "off":
62+
return nil, nil
63+
case "any", "auto", "on":
64+
return Any(), nil
65+
case "extip", "ip":
66+
if ip == nil {
67+
return nil, errors.New("missing IP address")
68+
}
69+
return ExtIP(ip), nil
70+
case "upnp":
71+
return UPnP(), nil
72+
case "pmp", "natpmp", "nat-pmp":
73+
return PMP(ip), nil
74+
default:
75+
return nil, fmt.Errorf("unknown mechanism %q", parts[0])
76+
}
77+
}
78+
79+
const (
80+
mapTimeout = 20 * time.Minute
81+
mapUpdateInterval = 15 * time.Minute
82+
)
83+
84+
// Map adds a port mapping on m and keeps it alive until c is closed.
85+
// This function is typically invoked in its own goroutine.
86+
func Map(m Interface, c chan struct{}, protocol string, extport, intport int, name string) {
87+
refresh := time.NewTimer(mapUpdateInterval)
88+
defer func() {
89+
refresh.Stop()
90+
log.Debugf("Deleting port mapping: %s %d -> %d (%s) using %s\n", protocol, extport, intport, name, m)
91+
m.DeleteMapping(protocol, extport, intport)
92+
}()
93+
log.Debugf("add mapping: %s %d -> %d (%s) using %s\n", protocol, extport, intport, name, m)
94+
if err := m.AddMapping(protocol, intport, extport, name, mapTimeout); err != nil {
95+
log.Errorf("mapping error: %v\n", err)
96+
}
97+
for {
98+
select {
99+
case _, ok := <-c:
100+
if !ok {
101+
return
102+
}
103+
case <-refresh.C:
104+
log.DebugDetailf("refresh mapping: %s %d -> %d (%s) using %s\n", protocol, extport, intport, name, m)
105+
if err := m.AddMapping(protocol, intport, extport, name, mapTimeout); err != nil {
106+
log.Errorf("mapping error: %v\n", err)
107+
}
108+
refresh.Reset(mapUpdateInterval)
109+
}
110+
}
111+
}
112+
113+
// ExtIP assumes that the local machine is reachable on the given
114+
// external IP address, and that any required ports were mapped manually.
115+
// Mapping operations will not return an error but won't actually do anything.
116+
func ExtIP(ip net.IP) Interface {
117+
if ip == nil {
118+
panic("IP must not be nil")
119+
}
120+
return extIP(ip)
121+
}
122+
123+
type extIP net.IP
124+
125+
func (n extIP) ExternalIP() (net.IP, error) { return net.IP(n), nil }
126+
func (n extIP) String() string { return fmt.Sprintf("ExtIP(%v)", net.IP(n)) }
127+
128+
// These do nothing.
129+
func (extIP) AddMapping(string, int, int, string, time.Duration) error { return nil }
130+
func (extIP) DeleteMapping(string, int, int) error { return nil }
131+
132+
// Any returns a port mapper that tries to discover any supported
133+
// mechanism on the local network.
134+
func Any() Interface {
135+
// TODO: attempt to discover whether the local machine has an
136+
// Internet-class address. Return ExtIP in this case.
137+
return startautodisc("UPnP or NAT-PMP", func() Interface {
138+
found := make(chan Interface, 2)
139+
go func() { found <- discoverUPnP() }()
140+
go func() { found <- discoverPMP() }()
141+
for i := 0; i < cap(found); i++ {
142+
if c := <-found; c != nil {
143+
return c
144+
}
145+
}
146+
return nil
147+
})
148+
}
149+
150+
// UPnP returns a port mapper that uses UPnP. It will attempt to
151+
// discover the address of your router using UDP broadcasts.
152+
func UPnP() Interface {
153+
return startautodisc("UPnP", discoverUPnP)
154+
}
155+
156+
// PMP returns a port mapper that uses NAT-PMP. The provided gateway
157+
// address should be the IP of your router. If the given gateway
158+
// address is nil, PMP will attempt to auto-discover the router.
159+
func PMP(gateway net.IP) Interface {
160+
if gateway != nil {
161+
return &pmp{gw: gateway, c: natpmp.NewClient(gateway)}
162+
}
163+
return startautodisc("NAT-PMP", discoverPMP)
164+
}
165+
166+
// autodisc represents a port mapping mechanism that is still being
167+
// auto-discovered. Calls to the Interface methods on this type will
168+
// wait until the discovery is done and then call the method on the
169+
// discovered mechanism.
170+
//
171+
// This type is useful because discovery can take a while but we
172+
// want return an Interface value from UPnP, PMP and Auto immediately.
173+
type autodisc struct {
174+
what string
175+
done <-chan Interface
176+
177+
mu sync.Mutex
178+
found Interface
179+
}
180+
181+
func startautodisc(what string, doit func() Interface) Interface {
182+
// TODO: monitor network configuration and rerun doit when it changes.
183+
done := make(chan Interface)
184+
ad := &autodisc{what: what, done: done}
185+
go func() { done <- doit(); close(done) }()
186+
return ad
187+
}
188+
189+
func (n *autodisc) AddMapping(protocol string, extport, intport int, name string, lifetime time.Duration) error {
190+
if err := n.wait(); err != nil {
191+
return err
192+
}
193+
return n.found.AddMapping(protocol, extport, intport, name, lifetime)
194+
}
195+
196+
func (n *autodisc) DeleteMapping(protocol string, extport, intport int) error {
197+
if err := n.wait(); err != nil {
198+
return err
199+
}
200+
return n.found.DeleteMapping(protocol, extport, intport)
201+
}
202+
203+
func (n *autodisc) ExternalIP() (net.IP, error) {
204+
if err := n.wait(); err != nil {
205+
return nil, err
206+
}
207+
return n.found.ExternalIP()
208+
}
209+
210+
func (n *autodisc) String() string {
211+
n.mu.Lock()
212+
defer n.mu.Unlock()
213+
if n.found == nil {
214+
return n.what
215+
} else {
216+
return n.found.String()
217+
}
218+
}
219+
220+
func (n *autodisc) wait() error {
221+
n.mu.Lock()
222+
found := n.found
223+
n.mu.Unlock()
224+
if found != nil {
225+
// already discovered
226+
return nil
227+
}
228+
if found = <-n.done; found == nil {
229+
return errors.New("no devices discovered")
230+
}
231+
n.mu.Lock()
232+
n.found = found
233+
n.mu.Unlock()
234+
return nil
235+
}

‎p2p/nat/natpmp.go‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package nat
2+
3+
import (
4+
"fmt"
5+
"net"
6+
"strings"
7+
"time"
8+
9+
"github.com/jackpal/go-nat-pmp"
10+
)
11+
12+
// natPMPClient adapts the NAT-PMP protocol implementation so it conforms to
13+
// the common interface.
14+
type pmp struct {
15+
gw net.IP
16+
c *natpmp.Client
17+
}
18+
19+
func (n *pmp) String() string {
20+
return fmt.Sprintf("NAT-PMP(%v)", n.gw)
21+
}
22+
23+
func (n *pmp) ExternalIP() (net.IP, error) {
24+
response, err := n.c.GetExternalAddress()
25+
if err != nil {
26+
return nil, err
27+
}
28+
return response.ExternalIPAddress[:], nil
29+
}
30+
31+
func (n *pmp) AddMapping(protocol string, extport, intport int, name string, lifetime time.Duration) error {
32+
if lifetime <= 0 {
33+
return fmt.Errorf("lifetime must not be <= 0")
34+
}
35+
// Note order of port arguments is switched between our
36+
// AddMapping and the client's AddPortMapping.
37+
_, err := n.c.AddPortMapping(strings.ToLower(protocol), intport, extport, int(lifetime/time.Second))
38+
return err
39+
}
40+
41+
func (n *pmp) DeleteMapping(protocol string, extport, intport int) (err error) {
42+
// To destroy a mapping, send an add-port with an internalPort of
43+
// the internal port to destroy, an external port of zero and a
44+
// time of zero.
45+
_, err = n.c.AddPortMapping(strings.ToLower(protocol), intport, 0, 0)
46+
return err
47+
}
48+
49+
func discoverPMP() Interface {
50+
// run external address lookups on all potential gateways
51+
gws := potentialGateways()
52+
found := make(chan *pmp, len(gws))
53+
for i := range gws {
54+
gw := gws[i]
55+
go func() {
56+
c := natpmp.NewClient(gw)
57+
if _, err := c.GetExternalAddress(); err != nil {
58+
found <- nil
59+
} else {
60+
found <- &pmp{gw, c}
61+
}
62+
}()
63+
}
64+
// return the one that responds first.
65+
// discovery needs to be quick, so we stop caring about
66+
// any responses after a very short timeout.
67+
timeout := time.NewTimer(1 * time.Second)
68+
defer timeout.Stop()
69+
for _ = range gws {
70+
select {
71+
case c := <-found:
72+
if c != nil {
73+
return c
74+
}
75+
case <-timeout.C:
76+
return nil
77+
}
78+
}
79+
return nil
80+
}
81+
82+
var (
83+
// LAN IP ranges
84+
_, lan10, _ = net.ParseCIDR("10.0.0.0/8")
85+
_, lan176, _ = net.ParseCIDR("172.16.0.0/12")
86+
_, lan192, _ = net.ParseCIDR("192.168.0.0/16")
87+
)
88+
89+
// TODO: improve this. We currently assume that (on most networks)
90+
// the router is X.X.X.1 in a local LAN range.
91+
func potentialGateways() (gws []net.IP) {
92+
ifaces, err := net.Interfaces()
93+
if err != nil {
94+
return nil
95+
}
96+
for _, iface := range ifaces {
97+
ifaddrs, err := iface.Addrs()
98+
if err != nil {
99+
return gws
100+
}
101+
for _, addr := range ifaddrs {
102+
switch x := addr.(type) {
103+
case *net.IPNet:
104+
if lan10.Contains(x.IP) || lan176.Contains(x.IP) || lan192.Contains(x.IP) {
105+
ip := x.IP.Mask(x.Mask).To4()
106+
if ip != nil {
107+
ip[3] = ip[3] | 0x01
108+
gws = append(gws, ip)
109+
}
110+
}
111+
}
112+
}
113+
}
114+
return gws
115+
}

0 commit comments

Comments
 (0)