Skip to content

Commit 4d37689

Browse files
committed
externalize function to do operation on ip
1 parent 28546c4 commit 4d37689

File tree

7 files changed

+178
-0
lines changed

7 files changed

+178
-0
lines changed

6rd.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package netadv
2+
3+
import "net"
4+
5+
// ConvertIPv4InIPv6Network convert ipv6 network followed by ipv4
6+
func ConvertIPv4InIPv6Network(network, ip string) (*net.IPNet, error) {
7+
// parse original network
8+
_, pNet1, err := net.ParseCIDR(network)
9+
if err != nil {
10+
return nil, err
11+
}
12+
13+
// create an empty network
14+
_, pNet2, err := net.ParseCIDR("0::/63")
15+
if err != nil {
16+
return nil, err
17+
}
18+
19+
// set 2 last bytes in new network
20+
ip2 := net.ParseIP(ip)
21+
pNet2.IP[0] = ip2[14]
22+
pNet2.IP[1] = ip2[15]
23+
24+
// we shift network to follow the first ip
25+
shift, _ := pNet1.Mask.Size()
26+
pNet2.IP = SliceShiftRight(pNet2.IP, uint(shift))
27+
28+
// return the OR of joined network
29+
return IPNetOr(pNet1, pNet2), nil
30+
}

bitwise.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package netadv
2+
3+
// SliceAnd return an AND on two slice of bytes
4+
func SliceAnd(s1, s2 []byte) []byte {
5+
mini := MinI(len(s1), len(s2))
6+
s := make([]byte, mini)
7+
for i := 0; i < mini; i++ {
8+
s[i] = s1[i] & s2[i]
9+
}
10+
return s
11+
}
12+
13+
// SliceOr return an OR on two slice of bytes
14+
func SliceOr(s1, s2 []byte) []byte {
15+
mini := MinI(len(s1), len(s2))
16+
s := make([]byte, mini)
17+
for i := 0; i < mini; i++ {
18+
s[i] = s1[i] | s2[i]
19+
}
20+
return s
21+
}
22+
23+
// SliceXor return an XOR on two slice of bytes
24+
func SliceXor(s1, s2 []byte) []byte {
25+
mini := MinI(len(s1), len(s2))
26+
s := make([]byte, mini)
27+
for i := 0; i < mini; i++ {
28+
s[i] = s1[i] ^ s2[i]
29+
}
30+
return s
31+
}
32+
33+
// SliceNot return the NOT of bytes slice
34+
func SliceNot(s1 []byte) []byte {
35+
mini := len(s1)
36+
s := make([]byte, mini)
37+
for i := 0; i < mini; i++ {
38+
s[i] = ^s1[i]
39+
}
40+
return s
41+
}

common.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package netadv
2+
3+
// MinI return the minimum between two int
4+
func MinI(a, b int) int {
5+
if a < b {
6+
return a
7+
}
8+
return b
9+
}
10+
11+
// MaxI return the maximum between two int
12+
func MaxI(a, b int) int {
13+
if a > b {
14+
return a
15+
}
16+
return b
17+
}

ipnet.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package netadv
2+
3+
import "net"
4+
5+
// IPNetOr make OR bitwise operation on net.IPNet
6+
func IPNetOr(n1, n2 *net.IPNet) *net.IPNet {
7+
oip := SliceOr(n1.IP, n2.IP)
8+
omask := SliceOr(n1.Mask, n2.Mask)
9+
return &net.IPNet{IP: oip, Mask: omask}
10+
}

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package netadv

slice.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package netadv
2+
3+
// SliceShiftRight return a slive of bytes shifted by "bits" bits
4+
func SliceShiftRight(s1 []byte, bits uint) []byte {
5+
l := len(s1)
6+
s := make([]byte, l)
7+
8+
r := bits / 8
9+
rs := bits - r*8
10+
11+
for i := 0; i < l; i++ {
12+
// relative index
13+
ri := i - int(r)
14+
bl := byte(0)
15+
if ri >= 1 {
16+
bl = s1[ri-1] << (8 - rs)
17+
}
18+
19+
bu := byte(0)
20+
if ri >= 0 {
21+
bu = s1[ri] >> rs
22+
}
23+
s[i] = bl | bu
24+
}
25+
26+
return s
27+
}
28+
29+
// SliceSetOne set 1 into the b'th byte
30+
func SliceSetOne(s1 []byte, b uint) []byte {
31+
r := b / 8
32+
rs := b - r*8
33+
34+
mb := byte(0x80)
35+
mb = mb >> rs
36+
37+
s1[r] = s1[r] | mb
38+
39+
return s1
40+
}

split.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package netadv
2+
3+
import "net"
4+
5+
// SplitNetworks split network in a
6+
func SplitNetworks(network *net.IPNet, newsize uint) ([]*net.IPNet, error) {
7+
var ret []*net.IPNet
8+
oldsize, _ := network.Mask.Size()
9+
10+
// use a stack to avoid a recurvive function
11+
ret = append(ret, network)
12+
for i := oldsize; i < int(newsize); i++ {
13+
var tmp []*net.IPNet
14+
15+
// parse network array to split all of them in two
16+
for _, n := range ret {
17+
one, two := SplitNetworkInTwo(n)
18+
tmp = append(tmp, one)
19+
tmp = append(tmp, two)
20+
}
21+
ret = tmp
22+
}
23+
24+
return ret, nil
25+
}
26+
27+
// SplitNetworkInTwo split a network in two. return two sub network
28+
func SplitNetworkInTwo(network *net.IPNet) (*net.IPNet, *net.IPNet) {
29+
size, _ := network.Mask.Size()
30+
newMask := net.CIDRMask(size+1, 8*net.IPv6len)
31+
32+
// byte1 := net.IP{0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
33+
// ip2 := SliceShiftRight(byte1, uint(size))
34+
35+
ip2 := SliceSetOne(net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, uint(size))
36+
ip2 = SliceOr(network.IP, ip2)
37+
38+
return &net.IPNet{IP: network.IP, Mask: newMask}, &net.IPNet{IP: ip2, Mask: newMask}
39+
}

0 commit comments

Comments
 (0)