forked from tehnerd/gnl2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadr_conv_utils.go
103 lines (78 loc) · 1.92 KB
/
adr_conv_utils.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
package gnl2go
import (
"errors"
"fmt"
"strconv"
"strings"
)
func IPv4ToUint32(ipv4 string) (uint32, error) {
octets := strings.Split(ipv4, ".")
if len(octets) != 4 {
return 0, errors.New("cant convert ipv4 to string")
}
ipv4int := uint32(0)
for cntr := 0; cntr < 4; cntr++ {
tmpVal, err := strconv.Atoi(octets[cntr])
if err != nil {
return 0, errors.New("cant convert ipv4 to string")
}
ipv4int += uint32(tmpVal << uint((3-cntr)*8))
}
return ipv4int, nil
}
func Uint32IPv4ToString(ipv4 uint32) string {
ipv4addr := ""
octet := 0
for cntr := 0; cntr < 4; cntr++ {
octet = int((ipv4 >> ((3 - uint(cntr)) * 8)) & 255)
if cntr == 0 {
ipv4addr = strconv.Itoa(octet)
} else {
ipv4addr = strings.Join([]string{ipv4addr, strconv.Itoa(octet)}, ".")
}
}
return ipv4addr
}
type IPv6Addr [4]uint32
func IPv6StringToAddr(ipv6 string) (IPv6Addr, error) {
var ipv6addr IPv6Addr
ipv6fields := strings.Split(ipv6, ":")
//parts in string's represent of V6 address
if len(ipv6fields) != 8 {
tmpAddrPart := make([]string, 0)
for n, val := range ipv6fields {
if len(val) == 0 {
tmpAddrPart = append(tmpAddrPart, ipv6fields[n+1:]...)
ipv6fields = ipv6fields[:n]
break
}
}
for len(ipv6fields) != (8 - len(tmpAddrPart)) {
ipv6fields = append(ipv6fields, "0")
}
ipv6fields = append(ipv6fields, tmpAddrPart...)
}
for n, part := range ipv6fields {
for len(part) < 4 {
part = "0" + part
}
ipv6fields[n] = part
}
//in ipv6 we have 4parts of 32bits each
for i := 0; i < 4; i++ {
val, err := strconv.ParseUint(ipv6fields[2*i]+ipv6fields[2*i+1], 16, 32)
if err != nil {
return ipv6addr, fmt.Errorf("cant convert string to v6 addr: %v\n", err)
}
ipv6addr[i] = uint32(val)
}
return ipv6addr, nil
}
func IPv6AddrToString(ipv6addr IPv6Addr) string {
ipv6 := ""
for i := 0; i < 4; i++ {
ipv6 += strconv.FormatUint(uint64(ipv6addr[i]), 16)
ipv6 += ":"
}
return ipv6
}