-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipv4.go
336 lines (290 loc) · 6.65 KB
/
ipv4.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package ipv4
import (
"crypto/rand"
"fmt"
"log"
"net/netip"
"strings"
)
const (
// bitsPerByte is the number of bits per byte
bitsPerByte = 8
)
// IPv4 is an IPv4 address
type IPv4 struct {
b [4]byte
// pl is the prefix length
pl int
}
// Addr returns ip as Addr
func (ip *IPv4) Addr() netip.Addr {
return netip.AddrFrom4(ip.b)
}
// Prefix returns ip as Prefix
func (ip *IPv4) Prefix() netip.Prefix {
return netip.PrefixFrom(ip.Addr(), ip.pl)
}
// Decimal returns ip as dotted decimal
func (ip *IPv4) Decimal() string {
return ip.Addr().String()
}
// Binary returns ip as a binary string
func (ip *IPv4) Binary() string {
return fmt.Sprintf("%08b.%08b.%08b.%08b",
ip.b[0], ip.b[1], ip.b[2], ip.b[3])
}
// Network returns the network part of ip
func (ip *IPv4) Network() string {
return ip.Prefix().Masked().Addr().String()
}
// Host returns the host part of ip
func (ip *IPv4) Host() string {
// create temporary array with only host bits set
b := [4]byte{}
bits := ip.pl
for i := 0; i < len(b); i++ {
if bits >= bitsPerByte {
// full byte, skip to next byte
bits -= bitsPerByte
continue
}
// skip remaining network bits, set host bits
ipBits := ip.b[i] & (0xff >> bits)
b[i] |= ipBits
bits = 0
}
return netip.AddrFrom4(b).String()
}
// Loopback returns wether ip is a loopback address
func (ip *IPv4) Loopback() bool {
return ip.Addr().IsLoopback()
}
// Private returns wether ip is a private address
func (ip *IPv4) Private() bool {
return ip.Addr().IsPrivate()
}
// Unspecified returns wether ip is the unspecified address
func (ip *IPv4) Unspecified() bool {
return ip.Addr().IsUnspecified()
}
// Multicast returns wether ip is a multicast address
func (ip *IPv4) Multicast() bool {
return ip.Addr().IsMulticast()
}
// Broadcast returns wether ip is a broadcast address
func (ip *IPv4) Broadcast() bool {
// skip prefix bits
bits := ip.pl
for i := 0; i < len(ip.b); i++ {
if bits >= bitsPerByte {
// full byte, skip to next byte
bits -= bitsPerByte
continue
}
// skip remaining network bits, check if host bits are set to 1
ipBits := ip.b[i] & (0xff >> bits)
if ipBits != (0xff >> bits) {
return false
}
bits = 0
}
return true
}
// Unicast returns wether ip is a unicast address
func (ip *IPv4) Unicast() bool {
return !ip.Multicast() && !ip.Broadcast()
}
// Type returns the type of ip
func (ip *IPv4) Type() string {
if ip.Loopback() {
return "loopback"
}
if ip.Unspecified() {
return "unspecified"
}
pp := "public"
if ip.Private() {
pp = "private"
}
ubm := "unicast"
if ip.Broadcast() {
ubm = "broadcast"
}
if ip.Multicast() {
ubm = "multicast"
}
return fmt.Sprintf("%s %s", pp, ubm)
}
// aaBracketTop returns the top part of an ascii art bracket with length l
func aaBracketTop(l int) string {
if l < 0 {
return ""
}
switch l {
case 0:
return ""
case 1:
return "|"
case 2:
return "|\\"
case 3:
return "|\\ "
case 4:
return " /\\ "
}
left := (l - 4) / 2
right := l - 4 - left
return " " + strings.Repeat("_", left) + "/\\" + strings.Repeat("_", right) + " "
}
// aaBracketBottom returns the bottom part of an ascii art bracket with length l
func aaBracketBottom(l int) string {
if l < 0 {
return ""
}
switch l {
case 0:
return ""
case 1:
return "|"
case 2:
return "||"
}
return "|" + strings.Repeat(" ", l-2) + "|"
}
// getPLHLSkip returns prefix length, host length and skip
func (ip *IPv4) getPLHLSkip() (pl, hl int, skip string) {
// consider up to 3 dots in 32 bit address,
// calculate prefix and host length for ascii art bracket creation
pl = ip.pl + (ip.pl / bitsPerByte)
hl = 35 - pl
if ip.pl != 0 && ip.pl%bitsPerByte == 0 {
// prefix ends exacly at a dot,
// omit this dot in ascii art bracket for prefix,
// set skip to start ascii art bracket for host after this dot
pl -= 1
skip = " "
}
return
}
// ExplainBin returns an explanation of the IP and its structure as string
func (ip *IPv4) ExplainBin() string {
pl, hl, skip := ip.getPLHLSkip()
return fmt.Sprintf(`Network: %s Host: %s
%s%s%s
%s%s%s
Bin: %s
Type: %s
`,
ip.Network(), ip.Host(),
aaBracketTop(pl), skip, aaBracketTop(hl),
aaBracketBottom(pl), skip, aaBracketBottom(hl),
ip.Binary(),
ip.Type(),
)
}
// getBinLengthDec returns a decimal representation of ip that has the same
// length as the binary representation, decimal numbers are padded with spaces
// to align them with the binary representation
func (ip *IPv4) getBinLengthDec() string {
s := [4]string{}
for i := 0; i < len(ip.b); i++ {
l := 1
if ip.b[i] > 9 {
l += 1
}
if ip.b[i] > 99 {
l += 1
}
left := (8 - l) / 2
right := 8 - l - left
s[i] = fmt.Sprintf("%s%d%s",
strings.Repeat(" ", left),
ip.b[i],
strings.Repeat(" ", right))
}
return fmt.Sprintf("%s.%s.%s.%s", s[0], s[1], s[2], s[3])
}
// ExplainDecimal returns an explanation of the IP and its structure as string
func (ip *IPv4) ExplainDecimal() string {
pl, hl, skip := ip.getPLHLSkip()
return fmt.Sprintf(`Network: %s Host: %s
%s%s%s
%s%s%s
Bin: %s
Dec: %s
Type: %s
`,
ip.Network(), ip.Host(),
aaBracketTop(pl), skip, aaBracketTop(hl),
aaBracketBottom(pl), skip, aaBracketBottom(hl),
ip.Binary(),
ip.getBinLengthDec(),
ip.Type(),
)
}
// String returns ip as String
func (ip *IPv4) String() string {
return ip.Decimal()
}
// SetPrefix sets prefix in ip
func (ip *IPv4) SetPrefix(prefix string) {
// parse prefix
p, err := netip.ParsePrefix(prefix)
if err != nil {
log.Fatal(err)
}
// get prefix bytes,
// get number of bits to be overwritten
b := p.Addr().As4()
bits := p.Bits()
// overwrite bits
// try to overwrite full bytes first, then single bits
for i := 0; i < len(b); i++ {
if bits < bitsPerByte {
// last byte, not full, overwrite bits
ipBits := ip.b[i] & (0xff >> bits)
bBits := b[i] & (0xff << (bitsPerByte - bits))
ip.b[i] = ipBits | bBits
break
}
// full byte, overwrite byte
ip.b[i] = b[i]
bits -= bitsPerByte
}
// set new prefix length
ip.pl = p.Bits()
}
// SetPrefixLength sets prefix length of ip in number of bits
func (ip *IPv4) SetPrefixLength(numBits int) {
ip.pl = numBits
}
// Random returns a random IPv4 address
func Random() *IPv4 {
ip := &IPv4{}
_, err := rand.Read(ip.b[:])
if err != nil {
log.Fatal(err)
}
return ip
}
// Parse parses and returns the IPv4 address in s
func Parse(s string) *IPv4 {
ip := &IPv4{}
// parse ip with prefix
if strings.Contains(s, "/") {
p, err := netip.ParsePrefix(s)
if err != nil {
log.Fatal(err)
}
ip.b = p.Addr().As4()
ip.pl = p.Bits()
return ip
}
// parse ip without prefix
a, err := netip.ParseAddr(s)
if err != nil {
log.Fatal(err)
}
ip.b = a.As4()
return ip
}