structs.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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(*Mapping, []Address, []Address)
  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) setAddress(id string, address Address) {
  23. m.mut.Lock()
  24. if existing, ok := m.extAddresses[id]; !ok || !existing.Equal(address) {
  25. l.Infof("New NAT port mapping: external %s address %s to local address %s.", m.protocol, address, m.address)
  26. m.extAddresses[id] = address
  27. }
  28. m.mut.Unlock()
  29. }
  30. func (m *Mapping) removeAddress(id string) {
  31. m.mut.Lock()
  32. addr, ok := m.extAddresses[id]
  33. if ok {
  34. l.Infof("Removing NAT port mapping: external %s address %s, NAT %s is no longer available.", m.protocol, addr, id)
  35. delete(m.extAddresses, id)
  36. }
  37. m.mut.Unlock()
  38. }
  39. func (m *Mapping) clearAddresses() {
  40. m.mut.Lock()
  41. var removed []Address
  42. for id, addr := range m.extAddresses {
  43. l.Debugf("Clearing mapping %s: ID: %s Address: %s", m, id, addr)
  44. removed = append(removed, addr)
  45. delete(m.extAddresses, id)
  46. }
  47. if len(removed) > 0 {
  48. m.notify(nil, removed)
  49. }
  50. m.expires = time.Time{}
  51. m.mut.Unlock()
  52. }
  53. func (m *Mapping) notify(added, removed []Address) {
  54. m.mut.RLock()
  55. for _, subscriber := range m.subscribers {
  56. subscriber(m, added, removed)
  57. }
  58. m.mut.RUnlock()
  59. }
  60. func (m *Mapping) addressMap() map[string]Address {
  61. m.mut.RLock()
  62. addrMap := m.extAddresses
  63. m.mut.RUnlock()
  64. return addrMap
  65. }
  66. func (m *Mapping) Protocol() Protocol {
  67. return m.protocol
  68. }
  69. func (m *Mapping) Address() Address {
  70. return m.address
  71. }
  72. func (m *Mapping) ExternalAddresses() []Address {
  73. m.mut.RLock()
  74. addrs := make([]Address, 0, len(m.extAddresses))
  75. for _, addr := range m.extAddresses {
  76. addrs = append(addrs, addr)
  77. }
  78. m.mut.RUnlock()
  79. return addrs
  80. }
  81. func (m *Mapping) OnChanged(subscribed MappingChangeSubscriber) {
  82. m.mut.Lock()
  83. m.subscribers = append(m.subscribers, subscribed)
  84. m.mut.Unlock()
  85. }
  86. func (m *Mapping) String() string {
  87. return fmt.Sprintf("%s %s", m.protocol, m.address)
  88. }
  89. func (m *Mapping) GoString() string {
  90. return m.String()
  91. }
  92. // Checks if the mappings local IP address matches the IP address of the gateway
  93. // For example, if we are explicitly listening on 192.168.0.12, there is no
  94. // point trying to acquire a mapping on a gateway to which the local IP is
  95. // 10.0.0.1. Fallback to true if any of the IPs is not there.
  96. func (m *Mapping) validGateway(ip net.IP) bool {
  97. if m.address.IP == nil || ip == nil || m.address.IP.IsUnspecified() || ip.IsUnspecified() {
  98. return true
  99. }
  100. return m.address.IP.Equal(ip)
  101. }
  102. // Address is essentially net.TCPAddr yet is more general, and has a few helper
  103. // methods which reduce boilerplate code.
  104. type Address struct {
  105. IP net.IP
  106. Port int
  107. }
  108. func (a Address) Equal(b Address) bool {
  109. return a.Port == b.Port && a.IP.Equal(b.IP)
  110. }
  111. func (a Address) String() string {
  112. var ipStr string
  113. if a.IP == nil {
  114. ipStr = net.IPv4zero.String()
  115. } else {
  116. ipStr = a.IP.String()
  117. }
  118. return net.JoinHostPort(ipStr, fmt.Sprintf("%d", a.Port))
  119. }
  120. func (a Address) GoString() string {
  121. return a.String()
  122. }