-
Notifications
You must be signed in to change notification settings - Fork 127
/
nat.go
164 lines (138 loc) · 2.9 KB
/
nat.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// date : 2016-05-13
// author: xjdrew
//
package kone
import (
"net"
"time"
)
const (
NatSessionLifeSeconds = 600
NatSessionCheckInterval = 300
)
type NatTable struct {
from uint16
to uint16
next uint16 // next avaliable port
h2Port map[uint64]uint16
mapped []bool
}
func hashAddr(ip net.IP, port uint16) uint64 {
v := uint64(ip[0]) << 40
v += uint64(ip[1]) << 32
v += uint64(ip[2]) << 24
v += uint64(ip[3]) << 16
v += uint64(port)
return v
}
func (tbl *NatTable) Unmap(ip net.IP, port uint16) {
h := hashAddr(ip, port)
if port, ok := tbl.h2Port[h]; ok {
delete(tbl.h2Port, h)
tbl.mapped[port-tbl.from] = false
}
}
// return: mapped port, is new mapped
func (tbl *NatTable) Map(ip net.IP, port uint16) (uint16, bool) {
h := hashAddr(ip, port)
if port, ok := tbl.h2Port[h]; ok {
return port, false
}
from := tbl.from
to := tbl.to
next := tbl.next
var i uint16
for ; i < to-from; i++ {
next = next + i
if next >= to {
next = next%to + from
}
if tbl.mapped[next-from] {
continue
}
tbl.mapped[next-from] = true
tbl.h2Port[h] = next
tbl.next = next + 1
return next, true
}
return 0, false
}
func (tbl *NatTable) Count() int {
return len(tbl.h2Port)
}
type NatSession struct {
srcIP net.IP
dstIP net.IP
srcPort uint16
dstPort uint16
lastTouch int64
}
type Nat struct {
tbl *NatTable
sessions []*NatSession
checkThreshold int
lastCheck int64
}
func (nat *Nat) getSession(port uint16) *NatSession {
if port < nat.tbl.from || port >= nat.tbl.to {
return nil
}
session := nat.sessions[port-nat.tbl.from]
if session != nil {
session.lastTouch = time.Now().Unix()
}
return session
}
func (nat *Nat) allocSession(srcIP, dstIP net.IP, srcPort, dstPort uint16) (bool, uint16) {
now := time.Now().Unix()
nat.clearExpiredSessions(now)
tbl := nat.tbl
port, isNew := tbl.Map(srcIP, srcPort)
if isNew {
session := &NatSession{
srcIP: srcIP,
dstIP: dstIP,
srcPort: srcPort,
dstPort: dstPort,
lastTouch: now,
}
nat.sessions[port-tbl.from] = session
}
return isNew, port
}
func (nat *Nat) clearExpiredSessions(now int64) {
if now-nat.lastCheck < NatSessionCheckInterval {
return
}
if nat.count() < nat.checkThreshold {
return
}
nat.lastCheck = now
for index, session := range nat.sessions {
if session != nil && now-session.lastTouch >= NatSessionLifeSeconds {
nat.sessions[index] = nil
nat.tbl.Unmap(session.srcIP, session.srcPort)
}
}
}
func (nat *Nat) count() int {
return nat.tbl.Count()
}
// port range [from, to)
func NewNat(from, to uint16) *Nat {
count := to - from
tbl := &NatTable{
from: from,
to: to,
next: from,
h2Port: make(map[uint64]uint16, count),
mapped: make([]bool, count),
}
logger.Infof("nat port range [%d, %d)", from, to)
return &Nat{
tbl: tbl,
sessions: make([]*NatSession, count),
checkThreshold: int(count) / 10,
}
}