structs.go 3.1 KB

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