From 3f503ffc7f85287fc3716afb704f90a1a4e7b21b Mon Sep 17 00:00:00 2001 From: obscuren Date: Sun, 2 Feb 2014 19:22:39 +0100 Subject: Implemented support for UPnP --- natpmp.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 natpmp.go (limited to 'natpmp.go') diff --git a/natpmp.go b/natpmp.go new file mode 100644 index 000000000..9a1fb652b --- /dev/null +++ b/natpmp.go @@ -0,0 +1,54 @@ +package eth + +import ( + natpmp "code.google.com/p/go-nat-pmp" + "fmt" + "net" +) + +// Adapt the NAT-PMP protocol to the NAT interface + +// TODO: +// + Register for changes to the external address. +// + Re-register port mapping when router reboots. +// + A mechanism for keeping a port mapping registered. + +type natPMPClient struct { + client *natpmp.Client +} + +func NewNatPMP(gateway net.IP) (nat NAT) { + return &natPMPClient{natpmp.NewClient(gateway)} +} + +func (n *natPMPClient) GetExternalAddress() (addr net.IP, err error) { + response, err := n.client.GetExternalAddress() + if err != nil { + return + } + ip := response.ExternalIPAddress + addr = net.IPv4(ip[0], ip[1], ip[2], ip[3]) + return +} + +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 + } + // 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 +} + +func (n *natPMPClient) DeletePortMapping(protocol string, externalPort, internalPort int) (err error) { + // To destroy a mapping, send an add-port with + // an internalPort of the internal port to destroy, an external port of zero and a time of zero. + _, err = n.client.AddPortMapping(protocol, internalPort, 0, 0) + return +} -- cgit v1.2.3 From ccea5fa9481f86f007701bee8e8850e4fbb38ad5 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 23 Aug 2014 19:01:06 +0200 Subject: changed to new nat-pmp repo --- natpmp.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'natpmp.go') diff --git a/natpmp.go b/natpmp.go index 9a1fb652b..badbaf9eb 100644 --- a/natpmp.go +++ b/natpmp.go @@ -1,9 +1,11 @@ package eth import ( - natpmp "code.google.com/p/go-nat-pmp" + //natpmp "code.google.com/p/go-nat-pmp" "fmt" "net" + + natpmp "github.com/jackpal/go-nat-pmp" ) // Adapt the NAT-PMP protocol to the NAT interface -- cgit v1.2.3 From 3f904bf3acb5779f68834ebca95825ea1990f85b Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 25 Aug 2014 11:29:42 +0200 Subject: Implemented POST --- natpmp.go | 1 - 1 file changed, 1 deletion(-) (limited to 'natpmp.go') diff --git a/natpmp.go b/natpmp.go index badbaf9eb..489342a4b 100644 --- a/natpmp.go +++ b/natpmp.go @@ -1,7 +1,6 @@ package eth import ( - //natpmp "code.google.com/p/go-nat-pmp" "fmt" "net" -- cgit v1.2.3