-
Notifications
You must be signed in to change notification settings - Fork 160
/
utils.go
196 lines (150 loc) · 3.84 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* NETCAP - Traffic Analysis Framework
* Copyright (c) 2017-2020 Philipp Mieden <dreadl0ck [at] protonmail [dot] ch>
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package types
import (
"math/big"
"net"
"reflect"
"strconv"
"strings"
"github.com/davecgh/go-spew/spew"
"github.com/dreadl0ck/netcap/utils"
)
var (
// StructureBegin marks the beginning of a structure in CSV.
StructureBegin = "("
// StructureEnd marks the end of a structure in CSV.
StructureEnd = ")"
// FieldSeparator separates fields within a structure in CSV.
FieldSeparator = "-"
)
type stringer interface {
toString() string
}
// panic: value method github.com/dreadl0ck/netcap/types.LSUpdate.ToString called using nil *LSUpdate pointer
// func toString(v stringer) string {
// if v != nil {
// return v.toString()
// }
// return ""
// }
// this function wraps the toString() function call with a nil pointer check.
func toString(c stringer) string {
// make sure its not a nil pointer
// a simple nil check is apparently not enough here
if c == nil || (reflect.ValueOf(c).Kind() == reflect.Ptr && reflect.ValueOf(c).IsNil()) {
return ""
}
// now check if the stringer interface is implemented
if str, ok := c.(stringer); ok {
return str.toString()
}
// in case the stringer interface is not implemented: fail
spew.Dump(c)
panic("toString called with an instance that does not implement the types.stringer interface")
}
func joinInts(a []int32) string {
var (
b strings.Builder
lastIndex = len(a) - 1
)
b.WriteString(StructureBegin)
for i, num := range a {
b.WriteString(formatInt32(num))
if i != lastIndex {
b.WriteString(FieldSeparator)
}
}
b.WriteString(StructureEnd)
return b.String()
}
func joinUints(a []uint32) string {
var (
b strings.Builder
lastIndex = len(a) - 1
)
b.WriteString(StructureBegin)
for i, num := range a {
b.WriteString(formatUint32(num))
if i != lastIndex {
b.WriteString(FieldSeparator)
}
}
b.WriteString(StructureEnd)
return b.String()
}
func join(a ...string) string {
var (
b strings.Builder
lastIndex = len(a) - 1
)
b.WriteString(StructureBegin)
for i, v := range a {
b.WriteString(v)
if i != lastIndex {
b.WriteString(FieldSeparator)
}
}
b.WriteString(StructureEnd)
return b.String()
}
func formatTimestamp(ts int64) string {
if UTC {
return utils.UnixTimeToUTC(ts)
}
return strconv.FormatInt(ts, 10)
}
func formatInt32(v int32) string {
return strconv.FormatInt(int64(v), 10)
}
func formatInt64(v int64) string {
return strconv.FormatInt(v, 10)
}
func formatUint32(v uint32) string {
return strconv.FormatUint(uint64(v), 10)
}
func formatUint64(v uint64) string {
return strconv.FormatUint(v, 10)
}
func formatFloat64(v float64) string {
return strconv.FormatFloat(v, 'f', 6, 64)
}
func ipToInt64(addr string) int64 {
ip := net.ParseIP(addr)
n := big.NewInt(0)
if ip.To4() != nil {
n.SetBytes(ip.To4())
} else {
n.SetBytes(ip.To16())
}
// TODO: IPv6, first half of the address will be ignored...
return n.Int64()
}
func macToUint64(addr string) uint64 {
var mac uint64
for j, b := range strings.ReplaceAll(addr, ":", "") {
if j >= 12 {
break
}
mac <<= 12
mac += uint64(b)
}
return mac
}
func portToInt(p string) int {
n, _ := strconv.Atoi(p)
return n
}
//func IP4ToUint32(ip string) uint32 {
// return binary.LittleEndian.Uint32(net.ParseIP(ip).To4())
//}