-
Notifications
You must be signed in to change notification settings - Fork 23
/
example_test.go
202 lines (172 loc) · 5.08 KB
/
example_test.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
package iplib
import (
"fmt"
"math/big"
"net"
"lukechampine.com/uint128"
)
func ExampleBigintToIP6() {
z := big.Int{}
z.SetString("42540766452641154071740215577757643572", 10)
fmt.Println(BigintToIP6(&z))
// Output: 2001:db8:85a3::8a2e:370:7334
}
func ExampleCompareIPs() {
fmt.Println(CompareIPs(net.ParseIP("192.168.1.0"), net.ParseIP("192.168.1.1")))
fmt.Println(CompareIPs(net.ParseIP("10.0.0.0"), net.ParseIP("10.0.0.0")))
fmt.Println(CompareIPs(net.ParseIP("2001:db8::100"), net.ParseIP("2001:db8::99")))
// Output:
// -1
// 0
// 1
}
func ExampleDecrementIP4By() {
ip := net.ParseIP("192.168.2.0")
fmt.Println(DecrementIP4By(ip, 255))
// Output: 192.168.1.1
}
func ExampleDecrementIP6By() {
z := uint128.New(16777215, 0)
ip := net.ParseIP("2001:db8::ffff:ffff")
fmt.Println(DecrementIP6By(ip, z))
// Output: 2001:db8::ff00:0
}
func ExampleDecrementIP6WithinHostmask() {
ip := net.ParseIP("2001:db8:1000::")
ip1, _ := DecrementIP6WithinHostmask(ip, NewHostMask(0), uint128.New(1, 0))
ip2, _ := DecrementIP6WithinHostmask(ip, NewHostMask(56), uint128.New(1, 0))
fmt.Println(ip1)
fmt.Println(ip2)
// Output:
// 2001:db8:fff:ffff:ffff:ffff:ffff:ffff
// 2001:db8:fff:ffff:ff00::
}
func ExampleDeltaIP4() {
ipa := net.ParseIP("192.168.1.1")
ipb := net.ParseIP("192.168.2.0")
fmt.Println(DeltaIP4(ipa, ipb))
// Output: 255
}
func ExampleDeltaIP6() {
ipa := net.ParseIP("2001:db8::ffff:ffff")
ipb := net.ParseIP("2001:db8::ff00:0")
fmt.Println(DeltaIP6(ipa, ipb))
// Output: 16777215
}
func ExampleEffectiveVersion() {
fmt.Println(EffectiveVersion(net.ParseIP("192.168.1.1")))
fmt.Println(EffectiveVersion(net.ParseIP("::ffff:c0a8:101")))
fmt.Println(EffectiveVersion(net.ParseIP("2001:db8::c0a8:101")))
// Output:
// 4
// 4
// 6
}
func ExampleExpandIP6() {
fmt.Println(ExpandIP6(net.ParseIP("2001:db8::1")))
// Output: 2001:0db8:0000:0000:0000:0000:0000:0001
}
func ExampleForceIP4() {
fmt.Println(len(ForceIP4(net.ParseIP("::ffff:c0a8:101"))))
// Output: 4
}
func ExampleHexStringToIP() {
ip := HexStringToIP("c0a80101")
fmt.Println(ip.String())
// Output: 192.168.1.1
}
func ExampleIncrementIP4By() {
ip := net.ParseIP("192.168.1.1")
fmt.Println(IncrementIP4By(ip, 255))
// Output: 192.168.2.0
}
func ExampleIncrementIP6By() {
z := uint128.New(16777215, 0)
ip := net.ParseIP("2001:db8::ff00:0")
fmt.Println(IncrementIP6By(ip, z))
// Output: 2001:db8::ffff:ffff
}
func ExampleIncrementIP6WithinHostmask() {
ip := net.ParseIP("2001:db8:1000::")
ip1, _ := IncrementIP6WithinHostmask(ip, NewHostMask(0), uint128.New(1, 0))
ip2, _ := IncrementIP6WithinHostmask(ip, NewHostMask(56), uint128.New(1, 0))
fmt.Println(ip1)
fmt.Println(ip2)
// Output:
// 2001:db8:1000::1
// 2001:db8:1000:0:100::
}
func ExampleIPToBigint() {
fmt.Println(IPToBigint(net.ParseIP("2001:db8:85a3::8a2e:370:7334")))
// Output: 42540766452641154071740215577757643572
}
func ExampleIP4ToARPA() {
fmt.Println(IP4ToARPA(net.ParseIP("192.168.1.1")))
// Output: 1.1.168.192.in-addr.arpa
}
func ExampleIP6ToARPA() {
fmt.Println(IP6ToARPA(net.ParseIP("2001:db8::ffff:ffff")))
// Output: f.f.f.f.f.f.f.f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa
}
func ExampleIP4ToUint32() {
fmt.Println(IP4ToUint32(net.ParseIP("192.168.1.1")))
// Output: 3232235777
}
func ExampleIP6ToUint128() {
fmt.Println(IP6ToUint128(net.ParseIP("2001:db8:85a3::8a2e:370:7334")))
// Output: 42540766452641154071740215577757643572
}
func ExampleIPToBinaryString() {
fmt.Println(IPToBinaryString(net.ParseIP("192.168.1.1")))
fmt.Println(IPToBinaryString(net.ParseIP("2001:db8::ffff:ffff")))
// Output:
// 11000000.10101000.00000001.00000001
// 00100000.00000001.00001101.10111000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.11111111.11111111.11111111.11111111
}
func ExampleIPToHexString() {
fmt.Println(IPToHexString(net.ParseIP("192.168.1.1")))
// Output:
// c0a80101
}
func ExampleNextIP() {
fmt.Println(NextIP(net.ParseIP("192.168.1.1")))
fmt.Println(NextIP(net.ParseIP("2001:db8::ffff:fffe")))
// Output:
// 192.168.1.2
// 2001:db8::ffff:ffff
}
func ExampleNextIP6WithinHostmask() {
ip, _ := NextIP6WithinHostmask(net.ParseIP("2001:db8:1234:5678::"), NewHostMask(56))
fmt.Println(ip)
// Output: 2001:db8:1234:5678:100::
}
func ExamplePreviousIP() {
fmt.Println(PreviousIP(net.ParseIP("192.168.1.2")))
fmt.Println(PreviousIP(net.ParseIP("2001:db8::ffff:ffff")))
// Output:
// 192.168.1.1
// 2001:db8::ffff:fffe
}
func ExamplePreviousIP6WithinHostmask() {
ip, _ := PreviousIP6WithinHostmask(net.ParseIP("2001:db8:1234:5678::"), NewHostMask(56))
fmt.Println(ip)
// Output: 2001:db8:1234:5677:ff00::
}
func ExampleUint32ToIP4() {
fmt.Println(Uint32ToIP4(3232235777))
// Output: 192.168.1.1
}
func ExampleUint128ToIP6() {
z, _ := uint128.FromString("42540766452641154071740215577757643572")
fmt.Println(Uint128ToIP6(z))
// Output: 2001:db8:85a3::8a2e:370:7334
}
func ExampleVersion() {
fmt.Println(Version(ForceIP4(net.ParseIP("192.168.1.1"))))
fmt.Println(Version(net.ParseIP("::ffff:c0a8:101")))
fmt.Println(Version(net.ParseIP("2001:db8::c0a8:101")))
// Output:
// 4
// 6
// 6
}