forked from pion/ice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
external_ip_mapper.go
143 lines (118 loc) · 3.13 KB
/
external_ip_mapper.go
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
131
132
133
134
135
136
137
138
139
140
141
142
143
package ice
import (
"net"
"strings"
)
func validateIPString(ipStr string) (net.IP, bool, error) {
ip := net.ParseIP(ipStr)
if ip == nil {
return nil, false, ErrInvalidNAT1To1IPMapping
}
return ip, (ip.To4() != nil), nil
}
// ipMapping holds the mapping of local and external IP address for a particular IP family
type ipMapping struct {
ipSole net.IP // when non-nil, this is the sole external IP for one local IP assumed
ipMap map[string]net.IP // local-to-external IP mapping (k: local, v: external)
}
func (m *ipMapping) setSoleIP(ip net.IP) error {
if m.ipSole != nil || len(m.ipMap) > 0 {
return ErrInvalidNAT1To1IPMapping
}
m.ipSole = ip
return nil
}
func (m *ipMapping) addIPMapping(locIP, extIP net.IP) error {
if m.ipSole != nil {
return ErrInvalidNAT1To1IPMapping
}
locIPStr := locIP.String()
// check if dup of local IP
if _, ok := m.ipMap[locIPStr]; ok {
return ErrInvalidNAT1To1IPMapping
}
m.ipMap[locIPStr] = extIP
return nil
}
func (m *ipMapping) findExternalIP(locIP net.IP) (net.IP, error) {
if m.ipSole != nil {
return m.ipSole, nil
}
extIP, ok := m.ipMap[locIP.String()]
if !ok {
return nil, ErrExternalMappedIPNotFound
}
return extIP, nil
}
type externalIPMapper struct {
ipv4Mapping ipMapping
ipv6Mapping ipMapping
candidateType CandidateType
}
func newExternalIPMapper(candidateType CandidateType, ips []string) (*externalIPMapper, error) { //nolint:gocognit
if len(ips) == 0 {
return nil, nil
}
if candidateType == CandidateTypeUnspecified {
candidateType = CandidateTypeHost // defaults to host
} else if candidateType != CandidateTypeHost && candidateType != CandidateTypeServerReflexive {
return nil, ErrUnsupportedNAT1To1IPCandidateType
}
m := &externalIPMapper{
ipv4Mapping: ipMapping{ipMap: map[string]net.IP{}},
ipv6Mapping: ipMapping{ipMap: map[string]net.IP{}},
candidateType: candidateType,
}
for _, extIPStr := range ips {
ipPair := strings.Split(extIPStr, "/")
if len(ipPair) == 0 || len(ipPair) > 2 {
return nil, ErrInvalidNAT1To1IPMapping
}
extIP, isExtIPv4, err := validateIPString(ipPair[0])
if err != nil {
return nil, err
}
if len(ipPair) == 1 {
if isExtIPv4 {
if err := m.ipv4Mapping.setSoleIP(extIP); err != nil {
return nil, err
}
} else {
if err := m.ipv6Mapping.setSoleIP(extIP); err != nil {
return nil, err
}
}
} else {
locIP, isLocIPv4, err := validateIPString(ipPair[1])
if err != nil {
return nil, err
}
if isExtIPv4 {
if !isLocIPv4 {
return nil, ErrInvalidNAT1To1IPMapping
}
if err := m.ipv4Mapping.addIPMapping(locIP, extIP); err != nil {
return nil, err
}
} else {
if isLocIPv4 {
return nil, ErrInvalidNAT1To1IPMapping
}
if err := m.ipv6Mapping.addIPMapping(locIP, extIP); err != nil {
return nil, err
}
}
}
}
return m, nil
}
func (m *externalIPMapper) findExternalIP(localIPStr string) (net.IP, error) {
locIP, isLocIPv4, err := validateIPString(localIPStr)
if err != nil {
return nil, err
}
if isLocIPv4 {
return m.ipv4Mapping.findExternalIP(locIP)
}
return m.ipv6Mapping.findExternalIP(locIP)
}