github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/p2p/nat/natpmp.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package nat 18 19 import ( 20 "errors" 21 "fmt" 22 "net" 23 "strings" 24 "time" 25 26 natpmp "github.com/jackpal/go-nat-pmp" 27 ) 28 29 // pmp adapts the NAT-PMP protocol implementation so it conforms to 30 // the common interface. 31 type pmp struct { 32 gw net.IP 33 c *natpmp.Client 34 } 35 36 func (n *pmp) String() string { 37 return fmt.Sprintf("NAT-PMP(%v)", n.gw) 38 } 39 40 func (n *pmp) ExternalIP() (net.IP, error) { 41 response, err := n.c.GetExternalAddress() 42 if err != nil { 43 return nil, err 44 } 45 return response.ExternalIPAddress[:], nil 46 } 47 48 func (n *pmp) AddMapping(protocol string, extport, intport int, name string, lifetime time.Duration) (uint16, error) { 49 if lifetime <= 0 { 50 return 0, errors.New("lifetime must not be <= 0") 51 } 52 // Note order of port arguments is switched between our 53 // AddMapping and the client's AddPortMapping. 54 res, err := n.c.AddPortMapping(strings.ToLower(protocol), intport, extport, int(lifetime/time.Second)) 55 if err != nil { 56 return 0, err 57 } 58 59 // NAT-PMP maps an alternative available port number if the requested port 60 // is already mapped to another address and returns success. Handling of 61 // alternate port numbers is done by the caller. 62 return res.MappedExternalPort, nil 63 } 64 65 func (n *pmp) DeleteMapping(protocol string, extport, intport int) (err error) { 66 // To destroy a mapping, send an add-port with an internalPort of 67 // the internal port to destroy, an external port of zero and a 68 // time of zero. 69 _, err = n.c.AddPortMapping(strings.ToLower(protocol), intport, 0, 0) 70 return err 71 } 72 73 func discoverPMP() Interface { 74 // run external address lookups on all potential gateways 75 gws := potentialGateways() 76 found := make(chan *pmp, len(gws)) 77 for i := range gws { 78 gw := gws[i] 79 go func() { 80 c := natpmp.NewClient(gw) 81 if _, err := c.GetExternalAddress(); err != nil { 82 found <- nil 83 } else { 84 found <- &pmp{gw, c} 85 } 86 }() 87 } 88 // return the one that responds first. 89 // discovery needs to be quick, so we stop caring about 90 // any responses after a very short timeout. 91 timeout := time.NewTimer(1 * time.Second) 92 defer timeout.Stop() 93 for range gws { 94 select { 95 case c := <-found: 96 if c != nil { 97 return c 98 } 99 case <-timeout.C: 100 return nil 101 } 102 } 103 return nil 104 } 105 106 // TODO: improve this. We currently assume that (on most networks) 107 // the router is X.X.X.1 in a local LAN range. 108 func potentialGateways() (gws []net.IP) { 109 ifaces, err := net.Interfaces() 110 if err != nil { 111 return nil 112 } 113 for _, iface := range ifaces { 114 ifaddrs, err := iface.Addrs() 115 if err != nil { 116 return gws 117 } 118 for _, addr := range ifaddrs { 119 if x, ok := addr.(*net.IPNet); ok { 120 if x.IP.IsPrivate() { 121 ip := x.IP.Mask(x.Mask).To4() 122 if ip != nil { 123 ip[3] = ip[3] | 0x01 124 gws = append(gws, ip) 125 } 126 } 127 } 128 } 129 } 130 return gws 131 }