natpmp.go 3.4 KB

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