forked from aler9/landiscover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
161 lines (135 loc) · 2.87 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
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"bytes"
"encoding/binary"
"fmt"
"math/rand"
"net"
"strings"
"github.com/google/gopacket/macs"
)
func defaultInterfaceName() (string, error) {
intfs, err := net.Interfaces()
if err != nil {
return "", err
}
for _, in := range intfs {
// must not be loopback
if (in.Flags & net.FlagLoopback) != 0 {
continue
}
// must be broadcast capable
if (in.Flags & net.FlagBroadcast) == 0 {
continue
}
addrs, err := in.Addrs()
if err != nil {
continue
}
// must have a valid ipv4
for _, a := range addrs {
if ipn, ok := a.(*net.IPNet); ok {
if ip4 := ipn.IP.To4(); ip4 != nil {
return in.Name, nil
}
}
}
}
return "", fmt.Errorf("no interfaces found")
}
func macVendor(mac net.HardwareAddr) string {
var pref [3]byte
copy(pref[:], mac[:3])
if v, ok := macs.ValidMACPrefixMap[pref]; ok {
return v
}
return "unknown"
}
func copyMac(in net.HardwareAddr) net.HardwareAddr {
ret := net.HardwareAddr(make([]byte, 6))
copy(ret, in)
return ret
}
func copyIP(in net.IP) net.IP {
ret := net.IP(make([]byte, 4))
copy(ret, in)
return ret
}
func randUint16() uint16 {
return uint16(rand.Uint32())
}
func randAvailableIps(ownIP net.IP) []net.IP {
var entries []net.IP
for i := byte(1); i <= 254; i++ {
eip := make([]byte, 4)
copy(eip, ownIP)
eip[3] = i
if bytes.Equal(eip, ownIP) { // skip own ip
continue
}
entries = append(entries, eip)
}
rand.Shuffle(len(entries), func(i, j int) {
entries[i], entries[j] = entries[j], entries[i]
})
return entries
}
// <size>part<size>part
func dnsQueryDecode(data []byte, start int) (string, int) {
var read []byte
toread := uint8(0)
pos := start
for ; true; pos++ {
if pos >= len(data) { // decoding terminated before null character
return "", -1
}
if data[pos] == 0x00 {
if toread > 0 { // decoding terminated before part parsing
return "", -1
}
break // query correctly decoded
}
if toread == 0 { // we need a size or pointer
if len(read) > 0 { // add separator
read = append(read, '.')
}
if (data[pos] & 0xC0) == 0xC0 { // pointer
ptr := int(binary.BigEndian.Uint16(data[pos:pos+2]) & 0x3FFF)
pos++ // skip next byte
substr, subread := dnsQueryDecode(data, ptr)
if subread <= 0 {
return "", -1
}
read = append(read, []byte(substr)...)
break // query correctly decoded
} else { // size
toread = data[pos]
}
} else { // byte inside part
read = append(read, data[pos])
toread--
}
}
return string(read), (pos + 1 - start)
}
func dnsQueryEncode(in string) []byte {
tmp := strings.Split(in, ".")
l := 0
for _, part := range tmp {
bpart := []byte(part)
l++
l += len(bpart)
}
l++
ret := make([]byte, l)
i := 0
for _, part := range tmp {
bpart := []byte(part)
ret[i] = uint8(len(bpart))
i++
copy(ret[i:], bpart)
i += len(bpart)
}
ret[i] = uint8(0)
return ret
}