-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathipalloc.go
183 lines (165 loc) · 4.11 KB
/
ipalloc.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* TVPN: A Peer-to-Peer VPN solution for traversing NAT firewalls
* Copyright (C) 2013 Joshua Chase <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package tvpn
import (
"math"
"net"
"strconv"
"github.com/Pursuit92/LeveledLogger/log"
)
type IPConfig map[string]string
type IPReq struct {
Req bool
IP net.IP
Resp chan net.IP
}
type IPManager struct {
reqs chan IPReq
reinit bool
Start net.IP
Tuns int
}
func (ipman *IPManager) Init() {
if ipman.reinit {
ipman.Stop()
}
ipman.reqs = make(chan IPReq)
ipman.reinit = true
go ipAllocator(ipman.reqs,ipman.Start,ipman.Tuns)
}
func (ipman *IPManager) Configure(conf IPConfig) bool {
num,err := strconv.Atoi(conf["Num"])
if err != nil {
panic(err)
}
var newIP bool
confIP := net.ParseIP(conf["Start"])
for i,v := range ipman.Start {
if v != confIP[i] {
newIP = true
break
}
}
if newIP || ipman.Tuns != num {
ipman.Start = confIP
ipman.Tuns = num
return true
}
return false
}
func (ipman IPManager) RequestAny() net.IP {
resp := make(chan net.IP)
req := IPReq{Req: true, Resp: resp}
ipman.reqs <- req
return <-resp
}
func (ipman IPManager) Request(ip net.IP) net.IP {
log.Out.Lprintln(3,"Attempting to allocate ",ip)
resp := make(chan net.IP)
req := IPReq{Req: true, IP: ip, Resp: resp}
ipman.reqs <- req
ret := <-resp
log.Out.Lprintln(3,"Returning ",ret)
return ret
}
func (ipman IPManager) Release(ip net.IP) net.IP {
resp := make(chan net.IP)
req := IPReq{Req: false, IP: ip, Resp: resp}
ipman.reqs <- req
return <-resp
}
func (ipman *IPManager) Stop() {
ipman.reinit = false
close(ipman.reqs)
}
func ipAllocator(ipReqs chan IPReq, min net.IP, n int) {
allocList := make([]bool, n)
for req := range ipReqs {
// is it a request for an IP or relinquishing one?
if req.Req {
// is it a request for any ip or a specific one?
if req.IP == nil {
log.Out.Lprintln(4,"Got request for first available!")
// any case, pick the first unallocated
for i, v := range allocList {
if !v {
allocList[i] = true
req.Resp <- indexToIP(min, i)
break
}
}
} else {
log.Out.Lprintln(4,"Attempting to allocate ",req.IP)
// specific: if the requested isn't available, pick the next
i := ipToIndex(min, req.IP)
if !allocList[i] {
log.Out.Lprintln(4,"Allocated ",indexToIP(min,i))
req.Resp <- indexToIP(min, i)
allocList[i] = true
} else {
for j := i; j < len(allocList); j++ {
if !allocList[j] {
log.Out.Lprintln(4,"Allocated ",indexToIP(min,j))
req.Resp <- indexToIP(min, j)
allocList[j] = true
break
}
}
}
}
} else {
//relinquish case
i := ipToIndex(min, req.IP)
allocList[i] = false
req.Resp <- indexToIP(min, i)
}
}
}
func ipToIndex(start, ip net.IP) int {
start4 := start.To4()
ip4 := ip.To4()
dif := net.IPv4(ip4[0]-start4[0],
ip4[1]-start4[1],
ip4[2]-start4[2],
ip4[3]-start4[3])
dif4 := dif.To4()
var sum int
for i, v := range dif4 {
sum += int(float64(v) * math.Pow(256, float64(3-i)))
}
return sum / 4
}
func indexToIP(start net.IP, index int) net.IP {
index *= 4
bs := make([]byte, 4)
for i, v := range start.To4() {
bs[i] = v + byte(float64(index)/math.Pow(256, float64(3-i)))
}
return net.IPv4(bs[0], bs[1], bs[2], bs[3])
}
func isGreater(lhs,rhs net.IP) bool {
lhs4 := lhs.To4()
rhs4 := rhs.To4()
for i := range lhs4 {
if lhs4[i] > rhs4[i] {
return true
}
}
return false
}