structs.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 http://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) notify(added, removed []Address) {
  40. m.mut.RLock()
  41. for _, subscriber := range m.subscribers {
  42. subscriber(m, added, removed)
  43. }
  44. m.mut.RUnlock()
  45. }
  46. func (m *Mapping) addressMap() map[string]Address {
  47. m.mut.RLock()
  48. addrMap := m.extAddresses
  49. m.mut.RUnlock()
  50. return addrMap
  51. }
  52. func (m *Mapping) Protocol() Protocol {
  53. return m.protocol
  54. }
  55. func (m *Mapping) Address() Address {
  56. return m.address
  57. }
  58. func (m *Mapping) ExternalAddresses() []Address {
  59. m.mut.RLock()
  60. addrs := make([]Address, 0, len(m.extAddresses))
  61. for _, addr := range m.extAddresses {
  62. addrs = append(addrs, addr)
  63. }
  64. m.mut.RUnlock()
  65. return addrs
  66. }
  67. func (m *Mapping) OnChanged(subscribed MappingChangeSubscriber) {
  68. m.mut.Lock()
  69. m.subscribers = append(m.subscribers, subscribed)
  70. m.mut.Unlock()
  71. }
  72. func (m *Mapping) String() string {
  73. return fmt.Sprintf("%s %s", m.protocol, m.address)
  74. }
  75. func (m *Mapping) GoString() string {
  76. return m.String()
  77. }
  78. // Checks if the mappings local IP address matches the IP address of the gateway
  79. // For example, if we are explicitly listening on 192.168.0.12, there is no
  80. // point trying to acquire a mapping on a gateway to which the local IP is
  81. // 10.0.0.1. Fallback to true if any of the IPs is not there.
  82. func (m *Mapping) validGateway(ip net.IP) bool {
  83. if m.address.IP == nil || ip == nil || m.address.IP.IsUnspecified() || ip.IsUnspecified() {
  84. return true
  85. }
  86. return m.address.IP.Equal(ip)
  87. }
  88. // Address is essentially net.TCPAddr yet is more general, and has a few helper
  89. // methods which reduce boilerplate code.
  90. type Address struct {
  91. IP net.IP
  92. Port int
  93. }
  94. func (a Address) Equal(b Address) bool {
  95. return a.Port == b.Port && a.IP.Equal(b.IP)
  96. }
  97. func (a Address) String() string {
  98. var ipStr string
  99. if a.IP == nil {
  100. ipStr = net.IPv4zero.String()
  101. } else {
  102. ipStr = a.IP.String()
  103. }
  104. return net.JoinHostPort(ipStr, fmt.Sprintf("%d", a.Port))
  105. }
  106. func (a Address) GoString() string {
  107. return a.String()
  108. }