-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
149 lines (127 loc) · 3.52 KB
/
utils.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
package main
import (
"encoding/binary"
"math/rand"
"net"
"strings"
"time"
"github.com/go-ini/ini"
"github.com/inverse-inc/packetfence/go/log"
"github.com/inverse-inc/packetfence/go/sharedutils"
)
type NodeInfo struct {
Mac string
Status string
Category string
}
func InterfaceScopeFromMac(MAC string) string {
var NetWork string
if index, found := GlobalMacCache.Get(MAC); found {
for _, v := range DHCPConfig.intsNet {
v := v
for network := range v.network {
if v.network[network].network.Contains(net.ParseIP(index.(string))) {
NetWork = v.network[network].network.String()
if x, found := v.network[network].dhcpHandler.hwcache.Get(MAC); found {
v.network[network].dhcpHandler.hwcache.Replace(MAC, x.(int), 3*time.Second)
log.LoggerWContext(ctx).Info(MAC + " removed")
}
}
}
}
}
return NetWork
}
func ShuffleDNS(sec *ini.Section) (r []byte) {
return Shuffle(sec.Key("dns").String())
}
func ShuffleGateway(sec *ini.Section) (r []byte) {
return []byte(net.ParseIP(sec.Key("gateway").String()).To4())
}
func Shuffle(addresses string) (r []byte) {
var array []net.IP
for _, adresse := range strings.Split(addresses, ",") {
array = append(array, net.ParseIP(adresse).To4())
}
slice := make([]byte, 0, len(array))
random := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := len(array) - 1; i > 0; i-- {
j := random.Intn(i + 1)
array[i], array[j] = array[j], array[i]
}
for _, element := range array {
elem := []byte(element)
slice = append(slice, elem...)
}
return slice
}
func ShuffleNetIP(array []net.IP, randSrc int64) (r []byte) {
slice := make([]byte, 0, len(array))
if randSrc == 0 {
randSrc = time.Now().UnixNano()
}
random := rand.New(rand.NewSource(randSrc))
for i := len(array) - 1; i > 0; i-- {
j := random.Intn(i + 1)
array[i], array[j] = array[j], array[i]
}
for _, element := range array {
elem := []byte(element)
slice = append(slice, elem...)
}
return slice
}
func ShuffleIP(a []byte, randSrc int64) (r []byte) {
var array []net.IP
for len(a) != 0 {
array = append(array, net.IPv4(a[0], a[1], a[2], a[3]).To4())
_, a = a[0], a[4:]
}
return ShuffleNetIP(array, randSrc)
}
func IPsFromRange(ip_range string) (r []net.IP, i int) {
var iplist []net.IP
iprange := strings.Split(ip_range, ",")
if len(iprange) >= 1 {
for _, rangeip := range iprange {
ips := strings.Split(rangeip, "-")
if len(ips) == 1 {
iplist = append(iplist, net.ParseIP(ips[0]))
} else {
start := net.ParseIP(ips[0])
end := net.ParseIP(ips[1])
for {
iplist = append(iplist, net.ParseIP(start.String()))
if start.Equal(end) {
break
}
sharedutils.Inc(start)
}
}
}
}
return iplist, len(iplist)
}
// ExcludeIP remove IP from the pool
func ExcludeIP(dhcpHandler *DHCPHandler, ip_range string) {
excludeIPs, _ := IPsFromRange(ip_range)
for _, excludeIP := range excludeIPs {
if excludeIP != nil {
// Calculate the position for the dhcp pool
position := uint32(binary.BigEndian.Uint32(excludeIP.To4())) - uint32(binary.BigEndian.Uint32(dhcpHandler.start.To4()))
dhcpHandler.available.ReserveIPIndex(uint64(position), FakeMac)
}
}
}
func IsIPv4(address net.IP) bool {
return strings.Count(address.String(), ":") < 2
}
func IsIPv6(address net.IP) bool {
return strings.Count(address.String(), ":") >= 2
}
func setOptionServerIdentifier(srvIP net.IP, handlerIP net.IP) net.IP {
if srvIP.Equal(handlerIP) || srvIP.Equal(net.IPv4zero) || srvIP.Equal(net.IPv4bcast) {
return handlerIP
}
return srvIP
}