-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipv4.go
150 lines (129 loc) · 3.48 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
package main
import (
"fmt"
"github.com/deanishe/awgo"
"github.com/pkg/errors"
"math"
"net"
"strconv"
"strings"
)
// struct for /22
type ipv4Prefix struct {
n *network
}
// struct for 192.168.33.12/21 or 192.146.11.22/255.255.255.0
type ipv4CIDR struct {
n *network
}
// struct for 255.255.252.0
type ipv4SubnetMask struct {
n *network
}
const maxPrefix = 32
const ipSliceLen = 4
func (self *ipv4Prefix) calculate() error {
var err error
self.n.prefix, err = strconv.Atoi(self.n.query[1:])
if err != nil {
return err
}
if self.n.prefix > maxPrefix {
return errors.New("入力形式が不正です。")
}
subnetMask := net.CIDRMask(self.n.prefix, maxPrefix)
self.n.subnetMask = net.IP(subnetMask)
buildWildCardMask(self.n, subnetMask)
return nil
}
func (self *ipv4Prefix) addNewItem() {
addDefaultItem(self.n)
}
func (self *ipv4CIDR) calculate() error {
var err error
var ipv4Net *net.IPNet
cidr := self.n.query
t := strings.Split(self.n.query, "/")
if strings.Contains(t[1], ".") {
subnetMask := net.ParseIP(t[1]).To4()
ipMask := net.IPMask(subnetMask)
prefix, b := ipMask.Size()
if prefix == 0 && b == 0 {
return errors.New("入力形式が不正です。")
}
cidr = t[0] + "/" + strconv.Itoa(prefix)
}
self.n.ip, ipv4Net, err = net.ParseCIDR(cidr)
if err != nil {
return err
}
self.n.networkAddr = ipv4Net.IP
self.n.subnetMask = net.IP(ipv4Net.Mask)
self.n.prefix, _ = ipv4Net.Mask.Size()
self.n.broadcastAddr = make([]byte, ipSliceLen)
copy(self.n.broadcastAddr, self.n.ip.To4())
for i, v := range ipv4Net.Mask {
self.n.broadcastAddr[i] |= v ^ 255
}
buildWildCardMask(self.n, ipv4Net.Mask)
return nil
}
func (self *ipv4CIDR) addNewItem() {
wf.NewItem(self.n.ip.String()).
Subtitle(`ip`).
Arg(self.n.ip.String()).
Valid(true).
Icon(&aw.Icon{Value: "./img/ip.png", Type: ""})
wf.NewItem(self.n.networkAddr.String() + " ~ " + self.n.broadcastAddr.String()).
Subtitle(`network range`).
Arg(self.n.networkAddr.String() + " ~ " + self.n.broadcastAddr.String()).
Valid(true).
Icon(&aw.Icon{Value: "./img/range.png", Type: ""})
addDefaultItem(self.n)
}
func (self *ipv4SubnetMask) calculate() error {
self.n.subnetMask = net.ParseIP(self.n.query).To4()
if self.n.subnetMask == nil {
return errors.New("入力形式が不正です。")
}
var b int
ipMask := net.IPMask(self.n.subnetMask)
self.n.prefix, b = ipMask.Size()
if self.n.prefix == 0 && b == 0 {
return errors.New("入力形式が不正です。")
}
buildWildCardMask(self.n, ipMask)
return nil
}
func (self *ipv4SubnetMask) addNewItem() {
addDefaultItem(self.n)
}
func addDefaultItem(n *network) {
wf.NewItem(`/` + strconv.Itoa(n.prefix)).
Subtitle(`network prefix`).
Arg(`/` + strconv.Itoa(n.prefix)).
Valid(true).
Icon(&aw.Icon{Value: "./img/prefix.png", Type: ""})
wf.NewItem(n.subnetMask.String()).
Subtitle(`subnet mask`).
Arg(n.subnetMask.String()).
Valid(true).
Icon(&aw.Icon{Value: "./img/subnet_mask.png", Type: ""})
wf.NewItem(n.wildcardMask.String()).
Subtitle(`wildcard mask`).
Arg(n.wildcardMask.String()).
Valid(true).
Icon(&aw.Icon{Value: "./img/wildcard_mask.png", Type: ""})
v := int(math.Exp2(float64(maxPrefix - n.prefix)))
wf.NewItem(fmt.Sprintf("%d", v)).
Subtitle(`ip count`).
Arg(fmt.Sprintf("%d", v)).
Valid(true).
Icon(&aw.Icon{Value: "./img/ip_count.png", Type: ""})
}
func buildWildCardMask(n *network, mask net.IPMask) {
n.wildcardMask = make([]byte, ipSliceLen)
for i, v := range mask {
n.wildcardMask[i] = v ^ 255
}
}