-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
193 lines (174 loc) · 4.41 KB
/
main.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
package main
import (
"fmt"
"io"
"math/big"
"net"
"os"
"strconv"
"strings"
)
type Result struct {
IP net.IP
IsV6 bool
IPBits int
Network net.IP
NetMask net.IPMask
NetMaskSize int
HostMask net.IPMask
HostMaskSize int
Max net.IP
IPCount *big.Int
Tags []string
}
func main() {
if len(os.Args) != 2 {
exitUsage()
}
if err := report(os.Stdout, os.Args[1]); err != nil {
exitUsage()
}
}
func exitUsage() {
fmt.Fprintln(os.Stderr, "specify a CIDR e.g. 10.20.30.40/22")
os.Exit(1)
}
func report(out io.Writer, cidr string) error {
p := func(format string, args ...interface{}) { fmt.Fprintf(out, format, args...) }
nl := func() { out.Write([]byte("\n")) }
r, err := calc(cidr)
if err != nil {
return err
}
var ipWidth string
var ipVer string
if r.IsV6 {
ipWidth = strconv.Itoa(39) // ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
ipVer = "IPv6"
} else {
ipWidth = strconv.Itoa(15) // 255.255.255.255
ipVer = "IPv4"
}
hostMaskOffset := strings.Repeat(" ", r.NetMaskSize+r.NetMaskSize/8)
nl()
p(" CIDR: %s\n", cidr)
if len(r.Tags) > 0 {
p(" Type: %s\n", strings.Join(r.Tags, ", "))
}
nl()
p(" IP bits: %-"+ipWidth+"s %s\n", fmt.Sprintf("%d (%s)", r.IPBits, ipVer), maskLine(r.IPBits))
p(" IP address: %-"+ipWidth+"s %s\n", r.IP, bin(r.IP))
nl()
p(" Network bits: %-"+ipWidth+"s %s\n", fmt.Sprintf("%d (..../%d)", r.NetMaskSize, r.NetMaskSize), maskLine(r.NetMaskSize))
p(" Network mask: %-"+ipWidth+"s %s\n", net.IP(r.NetMask), bin(net.IP(r.NetMask)))
nl()
p(" Host bits: %-"+ipWidth+"s %s%s\n", fmt.Sprintf("%d (%d - %d)", r.HostMaskSize, r.IPBits, r.NetMaskSize), hostMaskOffset, maskLine(r.HostMaskSize))
p(" Host mask: %-"+ipWidth+"s %s\n", net.IP(r.HostMask), bin(net.IP(r.HostMask)))
nl()
p(" Number of IPs: %s\n", fmt.Sprintf("%d (2 ^ %d)", r.IPCount, r.HostMaskSize))
p(" First IP: %-"+ipWidth+"s %s\n", r.Network, bin(r.Network))
p(" Last IP: %-"+ipWidth+"s %s\n", r.Max, bin(r.Max))
nl()
return nil
}
func calc(cidr string) (Result, error) {
ip, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return Result{}, err
}
if ipv4 := ip.To4(); ipv4 != nil {
ip = ipv4 // 16 -> 4 byte slice
}
netMask := ipnet.Mask
netMaskSize, netMaskBits := netMask.Size()
hostMask := maskComplement(ipnet.Mask)
hostMaskSize := netMaskBits - netMaskSize
tags := []string{}
if ip.IsLoopback() {
tags = append(tags, "loopback")
}
if ip.IsMulticast() {
tags = append(tags, "multicast")
}
if ip.IsLinkLocalMulticast() {
tags = append(tags, "link local multicast")
}
if ip.IsInterfaceLocalMulticast() {
tags = append(tags, "interface local multicast")
}
if ip.IsGlobalUnicast() {
// tags = append(tags, "global unicast")
}
if ip.IsLinkLocalUnicast() {
tags = append(tags, "link local unicast")
}
if ip.IsUnspecified() {
tags = append(tags, "unspecified")
}
return Result{
IP: ip,
IsV6: len(ip) == 16,
IPBits: len(ip) * 8,
NetMask: netMask,
NetMaskSize: netMaskSize,
HostMask: hostMask,
HostMaskSize: hostMaskSize,
Network: ipnet.IP,
Max: maxIP(ipnet),
IPCount: new(big.Int).Lsh(big.NewInt(1), uint(hostMaskSize)),
Tags: tags,
}, nil
}
func maxIP(network *net.IPNet) net.IP {
mask := network.Mask
bcst := make(net.IP, len(network.IP))
copy(bcst, network.IP)
for i := 0; i < len(mask); i++ {
ipIdx := len(bcst) - i - 1
bcst[ipIdx] = network.IP[ipIdx] | ^mask[len(mask)-i-1]
}
return bcst
}
func bin(ip net.IP) string {
return strings.Join(binaryOctets(ip), " ")
}
func binaryOctets(ip net.IP) []string {
octets := []string{}
for i := 0; i < len(ip); i++ {
octets = append(octets, fmt.Sprintf("%08b", ip[i]))
}
return octets
}
func maskLine(n int) string {
switch n {
case 0:
return ""
case 1:
return "1"
case 2:
return "2 "
case 3:
return "|3|"
case 4:
return "|4 |"
default:
return maskLineDynamic(n)
}
}
func maskLineDynamic(n int) string {
len := n - (2 * len("|")) - (2 * len(" ")) - len(strconv.Itoa(n)) + ((n - 1) / 8)
if len < 0 {
len = 0
}
lineL := strings.Repeat("-", len/2)
lineR := strings.Repeat("-", len/2+len%2)
return "|" + lineL + " " + strconv.Itoa(n) + " " + lineR + "|"
}
func maskComplement(m net.IPMask) net.IPMask {
comp := make(net.IPMask, len(m))
copy(comp, m)
for i := 0; i < len(comp); i++ {
comp[i] = ^comp[i]
}
return comp
}