-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapping.go
More file actions
152 lines (128 loc) · 3.33 KB
/
Copy pathmapping.go
File metadata and controls
152 lines (128 loc) · 3.33 KB
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
144
145
146
147
148
149
150
151
152
// SPDX-License-Identifier: AGPL-3.0-or-later
package gateway
import (
"fmt"
"net"
"sync"
"github.com/pilot-protocol/common/protocol"
)
// MappingTable maps local IPs to Pilot addresses and vice versa.
type MappingTable struct {
mu sync.RWMutex
forward map[string]protocol.Addr // local IP → pilot addr
reverse map[protocol.Addr]net.IP // pilot addr → local IP
subnet *net.IPNet
nextIP net.IP
}
// NewMappingTable creates a mapping table for the given subnet (e.g. "10.4.0.0/16").
func NewMappingTable(cidr string) (*MappingTable, error) {
_, subnet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, fmt.Errorf("parse subnet: %w", err)
}
// Start allocation at .0.1
startIP := make(net.IP, len(subnet.IP))
copy(startIP, subnet.IP)
startIP[len(startIP)-1] = 1
return &MappingTable{
forward: make(map[string]protocol.Addr),
reverse: make(map[protocol.Addr]net.IP),
subnet: subnet,
nextIP: startIP,
}, nil
}
// Map registers a mapping between a Pilot address and a local IP.
// If localIP is nil, the next available IP in the subnet is assigned.
func (mt *MappingTable) Map(pilotAddr protocol.Addr, localIP net.IP) (net.IP, error) {
mt.mu.Lock()
defer mt.mu.Unlock()
// Check if already mapped
if existing, ok := mt.reverse[pilotAddr]; ok {
return existing, nil
}
if localIP == nil {
localIP = mt.allocNextIP()
if localIP == nil {
return nil, fmt.Errorf("subnet exhausted")
}
} else {
if !mt.subnet.Contains(localIP) {
return nil, fmt.Errorf("IP %s not in subnet %s", localIP, mt.subnet)
}
}
ipStr := localIP.String()
if _, exists := mt.forward[ipStr]; exists {
return nil, fmt.Errorf("IP %s already mapped", ipStr)
}
mt.forward[ipStr] = pilotAddr
mt.reverse[pilotAddr] = localIP
return localIP, nil
}
// Unmap removes a mapping by local IP.
func (mt *MappingTable) Unmap(localIP net.IP) error {
mt.mu.Lock()
defer mt.mu.Unlock()
ipStr := localIP.String()
addr, ok := mt.forward[ipStr]
if !ok {
return fmt.Errorf("no mapping for %s", ipStr)
}
delete(mt.forward, ipStr)
delete(mt.reverse, addr)
return nil
}
// Lookup returns the Pilot address for a local IP.
func (mt *MappingTable) Lookup(localIP net.IP) (protocol.Addr, bool) {
mt.mu.RLock()
defer mt.mu.RUnlock()
addr, ok := mt.forward[localIP.String()]
return addr, ok
}
// ReverseLookup returns the local IP for a Pilot address.
func (mt *MappingTable) ReverseLookup(addr protocol.Addr) (net.IP, bool) {
mt.mu.RLock()
defer mt.mu.RUnlock()
ip, ok := mt.reverse[addr]
return ip, ok
}
// All returns all current mappings as (localIP, pilotAddr) pairs.
type Mapping struct {
LocalIP net.IP
PilotAddr protocol.Addr
}
func (mt *MappingTable) All() []Mapping {
mt.mu.RLock()
defer mt.mu.RUnlock()
result := make([]Mapping, 0, len(mt.forward))
for ipStr, addr := range mt.forward {
result = append(result, Mapping{
LocalIP: net.ParseIP(ipStr),
PilotAddr: addr,
})
}
return result
}
func (mt *MappingTable) allocNextIP() net.IP {
for {
ip := make(net.IP, len(mt.nextIP))
copy(ip, mt.nextIP)
if !mt.subnet.Contains(ip) {
return nil
}
// Increment nextIP
incIP(mt.nextIP)
// Skip .0 and .255 for /24+ subnets
ipStr := ip.String()
if _, exists := mt.forward[ipStr]; !exists {
return ip
}
}
}
func incIP(ip net.IP) {
for i := len(ip) - 1; i >= 0; i-- {
ip[i]++
if ip[i] != 0 {
break
}
}
}