aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/natpmp.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2014-11-22 04:48:49 +0800
committerFelix Lange <fjl@twurst.com>2014-11-22 04:52:45 +0800
commit59b63caf5e4de64ceb7dcdf01551a080f53b1672 (patch)
treea4e79590284c5afe4d6927b422a5092b074e7938 /p2p/natpmp.go
parente4a601c6444afdc11ce0cb80d7fd83116de2c8b9 (diff)
downloadgo-tangerine-59b63caf5e4de64ceb7dcdf01551a080f53b1672.tar
go-tangerine-59b63caf5e4de64ceb7dcdf01551a080f53b1672.tar.gz
go-tangerine-59b63caf5e4de64ceb7dcdf01551a080f53b1672.tar.bz2
go-tangerine-59b63caf5e4de64ceb7dcdf01551a080f53b1672.tar.lz
go-tangerine-59b63caf5e4de64ceb7dcdf01551a080f53b1672.tar.xz
go-tangerine-59b63caf5e4de64ceb7dcdf01551a080f53b1672.tar.zst
go-tangerine-59b63caf5e4de64ceb7dcdf01551a080f53b1672.zip
p2p: API cleanup and PoC 7 compatibility
Whoa, one more big commit. I didn't manage to untangle the changes while working towards compatibility.
Diffstat (limited to 'p2p/natpmp.go')
-rw-r--r--p2p/natpmp.go34
1 files changed, 17 insertions, 17 deletions
diff --git a/p2p/natpmp.go b/p2p/natpmp.go
index ff966d070..6714678c4 100644
--- a/p2p/natpmp.go
+++ b/p2p/natpmp.go
@@ -3,6 +3,7 @@ package p2p
import (
"fmt"
"net"
+ "time"
natpmp "github.com/jackpal/go-nat-pmp"
)
@@ -13,38 +14,37 @@ import (
// + Register for changes to the external address.
// + Re-register port mapping when router reboots.
// + A mechanism for keeping a port mapping registered.
+// + Discover gateway address automatically.
type natPMPClient struct {
client *natpmp.Client
}
-func NewNatPMP(gateway net.IP) (nat NAT) {
+// PMP returns a NAT traverser that uses NAT-PMP. The provided gateway
+// address should be the IP of your router.
+func PMP(gateway net.IP) (nat NAT) {
return &natPMPClient{natpmp.NewClient(gateway)}
}
-func (n *natPMPClient) GetExternalAddress() (addr net.IP, err error) {
+func (*natPMPClient) String() string {
+ return "NAT-PMP"
+}
+
+func (n *natPMPClient) GetExternalAddress() (net.IP, error) {
response, err := n.client.GetExternalAddress()
if err != nil {
- return
+ return nil, err
}
- ip := response.ExternalIPAddress
- addr = net.IPv4(ip[0], ip[1], ip[2], ip[3])
- return
+ return response.ExternalIPAddress[:], nil
}
-func (n *natPMPClient) AddPortMapping(protocol string, externalPort, internalPort int,
- description string, timeout int) (mappedExternalPort int, err error) {
- if timeout <= 0 {
- err = fmt.Errorf("timeout must not be <= 0")
- return
+func (n *natPMPClient) AddPortMapping(protocol string, extport, intport int, name string, lifetime time.Duration) error {
+ if lifetime <= 0 {
+ return fmt.Errorf("lifetime must not be <= 0")
}
// Note order of port arguments is switched between our AddPortMapping and the client's AddPortMapping.
- response, err := n.client.AddPortMapping(protocol, internalPort, externalPort, timeout)
- if err != nil {
- return
- }
- mappedExternalPort = int(response.MappedExternalPort)
- return
+ _, err := n.client.AddPortMapping(protocol, intport, extport, int(lifetime/time.Second))
+ return err
}
func (n *natPMPClient) DeletePortMapping(protocol string, externalPort, internalPort int) (err error) {