-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipv4.go
47 lines (36 loc) · 942 Bytes
/
ipv4.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
package ipformat
import (
"strconv"
"strings"
)
// ToV4 converts ipv6 to ipv4
func (ip IP) ToV4() (IP, error) {
if ip.TypeV6 == false {
return ip, nil
}
// start of ipv4
var newParts []string
// Loop through the old parts converting to ipv4
for i, part := range ip.Parts {
// turn the string into a int
intConverted, _ := strconv.ParseInt(part, 16, 0)
ip.Parts[i] = strconv.FormatInt(intConverted, 10)
// Make sure that no 0s are missing
if len(ip.Parts[i]) == 1 {
ip.Parts[i] = "0" + ip.Parts[i]
}
}
//Set the parts to ipv6
newParts = append(newParts, ip.Parts[0]+ip.Parts[1])
newParts = append(newParts, ip.Parts[2]+ip.Parts[3])
ip.Parts = newParts
ip.TypeV6 = true
ip.Address = strings.Join(ip.Parts, ":")
if ip.Range {
// Convert cidr to ipv6
ip.CIDR = 128 - (32 - ip.CIDR)
ip.Address = ip.Address + "/" + strconv.FormatInt(ip.CIDR, 10)
}
// return the new ip address
return ip, nil
}