Skip to content

Commit 99f950a

Browse files
authored
Add IPToBinarySlice function (#17)
This commmit adds IPToBinarySlice. Given a net.IP, it will return a []byte whose values are the binary representation of the IP.
1 parent 0dc8da2 commit 99f950a

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

iplib.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,22 @@ func IPToBigint(ip net.IP) *big.Int {
378378
return z
379379
}
380380

381+
// IPToBinarySlice returns the given net.IP as a []byte whose
382+
// values are the binary representation of the IP
383+
func IPToBinarySlice(ip net.IP) []byte {
384+
var bits []byte
385+
if EffectiveVersion(ip) == 4 {
386+
ip = ForceIP4(ip)
387+
}
388+
for _, octet := range ip {
389+
for i := 7; i >= 0; i-- {
390+
bit := (octet >> i) & 1
391+
bits = append(bits, bit)
392+
}
393+
}
394+
return bits
395+
}
396+
381397
// IPToBinaryString returns the given net.IP as a binary string
382398
func IPToBinaryString(ip net.IP) string {
383399
var sa []string

iplib_test.go

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package iplib
22

33
import (
4+
"bytes"
45
"math/big"
56
"net"
67
"reflect"
@@ -22,13 +23,14 @@ func TestCopyIP(t *testing.T) {
2223
}
2324

2425
var IPTests = []struct {
25-
ipaddr net.IP
26-
next net.IP
27-
prev net.IP
28-
intval uint32
29-
hexval string
30-
inarpa string
31-
binval string
26+
ipaddr net.IP
27+
next net.IP
28+
prev net.IP
29+
intval uint32
30+
hexval string
31+
inarpa string
32+
binval string
33+
binslice []byte
3234
}{
3335
{
3436
net.ParseIP("10.1.2.3"),
@@ -38,6 +40,7 @@ var IPTests = []struct {
3840
"0a010203",
3941
"3.2.1.10.in-addr.arpa",
4042
"00001010.00000001.00000010.00000011",
43+
[]byte{0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1},
4144
},
4245
{
4346
net.ParseIP("10.1.2.255"),
@@ -47,6 +50,7 @@ var IPTests = []struct {
4750
"0a0102ff",
4851
"255.2.1.10.in-addr.arpa",
4952
"00001010.00000001.00000010.11111111",
53+
[]byte{0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1},
5054
},
5155
{
5256
net.ParseIP("10.1.2.0"),
@@ -56,6 +60,7 @@ var IPTests = []struct {
5660
"0a010200",
5761
"0.2.1.10.in-addr.arpa",
5862
"00001010.00000001.00000010.00000000",
63+
[]byte{0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
5964
},
6065
{
6166
net.ParseIP("255.255.255.255"),
@@ -65,6 +70,7 @@ var IPTests = []struct {
6570
"ffffffff",
6671
"255.255.255.255.in-addr.arpa",
6772
"11111111.11111111.11111111.11111111",
73+
[]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
6874
},
6975
{
7076
net.ParseIP("0.0.0.0"),
@@ -74,6 +80,7 @@ var IPTests = []struct {
7480
"00000000",
7581
"0.0.0.0.in-addr.arpa",
7682
"00000000.00000000.00000000.00000000",
83+
[]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
7784
},
7885
}
7986

@@ -113,6 +120,15 @@ func TestIPToHexString(t *testing.T) {
113120
}
114121
}
115122

123+
func TestIPToBinarySlice(t *testing.T) {
124+
for i, tt := range IPTests {
125+
b := IPToBinarySlice(tt.ipaddr)
126+
if !bytes.Equal(b, tt.binslice) {
127+
t.Errorf("[%d] expected %v, got %v", i, tt.binslice, b)
128+
}
129+
}
130+
}
131+
116132
func TestIPToBinaryString(t *testing.T) {
117133
for i, tt := range IPTests {
118134
s := IPToBinaryString(tt.ipaddr)
@@ -183,6 +199,7 @@ var IP6Tests = []struct {
183199
expand string
184200
inarpa string
185201
binval string
202+
binslice []byte
186203
}{
187204
{
188205
"2001:db8:85a3::8a2e:370:7334",
@@ -195,6 +212,7 @@ var IP6Tests = []struct {
195212
"2001:0db8:85a3:0000:0000:8a2e:0370:7334",
196213
"4.3.3.7.0.7.3.0.e.2.a.8.0.0.0.0.0.0.0.0.3.a.5.8.8.b.d.0.1.0.0.2.ip6.arpa",
197214
"00100000.00000001.00001101.10111000.10000101.10100011.00000000.00000000.00000000.00000000.10001010.00101110.00000011.01110000.01110011.00110100",
215+
[]byte{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0},
198216
},
199217
{
200218
"::",
@@ -207,6 +225,7 @@ var IP6Tests = []struct {
207225
"0000:0000:0000:0000:0000:0000:0000:0000",
208226
"0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa",
209227
"00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000",
228+
[]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
210229
},
211230
{
212231
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
@@ -219,6 +238,7 @@ var IP6Tests = []struct {
219238
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
220239
"f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.ip6.arpa",
221240
"11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111.11111111",
241+
[]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
222242
},
223243
}
224244

@@ -268,6 +288,15 @@ func TestUint64ToIP6(t *testing.T) {
268288
}
269289
}
270290

291+
func TestIP6ToBinarySlice(t *testing.T) {
292+
for i, tt := range IP6Tests {
293+
b := IPToBinarySlice(net.ParseIP(tt.ipaddr))
294+
if !bytes.Equal(b, tt.binslice) {
295+
t.Errorf("[%d] expected %v, got %v", i, tt.binslice, b)
296+
}
297+
}
298+
}
299+
271300
func TestIP6ToBinaryString(t *testing.T) {
272301
for i, tt := range IP6Tests {
273302
s := IPToBinaryString(net.ParseIP(tt.ipaddr))

0 commit comments

Comments
 (0)