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