structs.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright (C) 2015 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package nat
  7. import (
  8. "fmt"
  9. "net"
  10. "time"
  11. "github.com/syncthing/syncthing/lib/sync"
  12. )
  13. type MappingChangeSubscriber func()
  14. type Mapping struct {
  15. protocol Protocol
  16. address Address
  17. extAddresses map[string]Address // NAT ID -> Address
  18. expires time.Time
  19. subscribers []MappingChangeSubscriber
  20. mut sync.RWMutex
  21. }
  22. func (m *Mapping) setAddressLocked(id string, address Address) {
  23. l.Infof("New NAT port mapping: external %s address %s to local address %s.", m.protocol, address, m.address)
  24. m.extAddresses[id] = address
  25. }
  26. func (m *Mapping) removeAddressLocked(id string) {
  27. addr, ok := m.extAddresses[id]
  28. if ok {
  29. l.Infof("Removing NAT port mapping: external %s address %s, NAT %s is no longer available.", m.protocol, addr, id)
  30. delete(m.extAddresses, id)
  31. }
  32. }
  33. func (m *Mapping) clearAddresses() {
  34. m.mut.Lock()
  35. change := len(m.extAddresses) > 0
  36. for id, addr := range m.extAddresses {
  37. l.Debugf("Clearing mapping %s: ID: %s Address: %s", m, id, addr)
  38. delete(m.extAddresses, id)
  39. }
  40. m.expires = time.Time{}
  41. m.mut.Unlock()
  42. if change {
  43. m.notify()
  44. }
  45. }
  46. func (m *Mapping) notify() {
  47. m.mut.RLock()
  48. for _, subscriber := range m.subscribers {
  49. subscriber()
  50. }
  51. m.mut.RUnlock()
  52. }
  53. func (m *Mapping) Protocol() Protocol {
  54. return m.protocol
  55. }
  56. func (m *Mapping) Address() Address {
  57. return m.address
  58. }
  59. func (m *Mapping) ExternalAddresses() []Address {
  60. m.mut.RLock()
  61. addrs := make([]Address, 0, len(m.extAddresses))
  62. for _, addr := range m.extAddresses {
  63. addrs = append(addrs, addr)
  64. }
  65. m.mut.RUnlock()
  66. return addrs
  67. }
  68. func (m *Mapping) OnChanged(subscribed MappingChangeSubscriber) {
  69. m.mut.Lock()
  70. m.subscribers = append(m.subscribers, subscribed)
  71. m.mut.Unlock()
  72. }
  73. func (m *Mapping) String() string {
  74. return fmt.Sprintf("%s %s", m.protocol, m.address)
  75. }
  76. func (m *Mapping) GoString() string {
  77. return m.String()
  78. }
  79. // Checks if the mappings local IP address matches the IP address of the gateway
  80. // For example, if we are explicitly listening on 192.168.0.12, there is no
  81. // point trying to acquire a mapping on a gateway to which the local IP is
  82. // 10.0.0.1. Fallback to true if any of the IPs is not there.
  83. func (m *Mapping) validGateway(ip net.IP) bool {
  84. if m.address.IP == nil || ip == nil || m.address.IP.IsUnspecified() || ip.IsUnspecified() {
  85. return true
  86. }
  87. return m.address.IP.Equal(ip)
  88. }
  89. // Address is essentially net.TCPAddr yet is more general, and has a few helper
  90. // methods which reduce boilerplate code.
  91. type Address struct {
  92. IP net.IP
  93. Port int
  94. }
  95. func (a Address) Equal(b Address) bool {
  96. return a.Port == b.Port && a.IP.Equal(b.IP)
  97. }
  98. func (a Address) String() string {
  99. var ipStr string
  100. if a.IP == nil {
  101. ipStr = net.IPv4zero.String()
  102. } else {
  103. ipStr = a.IP.String()
  104. }
  105. return net.JoinHostPort(ipStr, fmt.Sprintf("%d", a.Port))
  106. }
  107. func (a Address) GoString() string {
  108. return a.String()
  109. }