-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathoctet.go
68 lines (58 loc) · 900 Bytes
/
octet.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
package main
import (
"bytes"
"fmt"
"strconv"
"strings"
)
func ip2octet(ip string) [4]int {
oc := [4]int{0, 0, 0, 0}
var err error
for i, o := range strings.Split(ip, ".") {
oc[i], err = strconv.Atoi(o)
check(err, "bad ip")
}
return oc
}
func octet2ip(oc [4]int) string {
var ip bytes.Buffer
for i, o := range oc {
ip.WriteString(strconv.Itoa(o))
if i < 3 {
ip.WriteByte('.')
}
}
return ip.String()
}
func equalOctets(oc1 [4]int, oc2 [4]int) bool {
return oc1[0] == oc2[0] &&
oc1[1] == oc2[1] &&
oc1[2] == oc2[2] &&
oc1[3] == oc2[3]
}
func incOctet(oc [4]int) [4]int {
if oc[3] < 255 {
oc[3]++
return oc
}
if oc[2] < 255 {
oc[2]++
oc[3] = 0
return oc
}
if oc[1] < 255 {
oc[1]++
oc[2] = 0
oc[3] = 0
return oc
}
if oc[0] < 255 {
oc[0]++
oc[3] = 0
oc[2] = 0
oc[1] = 0
return oc
}
fmt.Println("wrong ip range")
return oc
}