-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertIPv4.go
84 lines (76 loc) · 2.1 KB
/
convertIPv4.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
package convertipv4
import (
"fmt"
"math"
"strconv"
"strings"
)
func toHex(n int64) string {
return strconv.FormatInt(n, 16)
}
func compactGroup(values []string) string {
str := strings.Join(values[:4], "")
// remove leading zeros (refer to: https://en.wikipedia.org/wiki/IPv6#Address_representation)
ret := strings.TrimLeft(str, "0")
if len(ret) == 0 {
return "0"
}
return ret
}
func ipv4ToHex(nums []int64) string {
// Hint from: https://www.researchgate.net/figure/IPv4-to-IPv6-Conversion-Method1-In-this-method-firstly-to-convert-the-Decimal-IPv4_fig1_271294793
// Details: https://zh.wikipedia.org/wiki/IPv6
hexValues := make([]string, 0, 8)
for _, n := range nums {
leftBits := n >> 4
rightBits := n & 0x0F
hexValues = append(hexValues, toHex(leftBits), toHex(rightBits))
}
return fmt.Sprintf("0:0:0:0:0:ffff:%v:%v", compactGroup(hexValues[:4]), compactGroup(hexValues[4:]))
}
func equivalentInterger(nums []int64) string {
// Useful tool:
// http://www.aboutmyip.com/AboutMyXApp/IP2Integer.jsp?ipAddress=216.58.200.14
// golang int/string convert hints:
// https://yourbasic.org/golang/convert-int-to-string/
//
var ret float64 = 0
for i, n := range nums {
// [216 58 200 14]
// [11011000, 00111010, 11001000, 00001110]
// [ 2^(8*3), 2^(8*2), 2^(8*1), 2^(8*0) ]
multiplier := math.Pow(float64(2), float64((len(nums)-i-1)*8))
ret += float64(multiplier) * float64(n)
}
return strconv.Itoa(int(ret))
}
func convertIPv4(ipv4 string) []string {
values := strings.Split(ipv4, ".")
if len(values) != 4 {
return []string{}
}
nums := make([]int64, 0, 4)
for _, s := range values {
if n, err := strconv.Atoi(s); err == nil {
if n > 255 || n < 0 {
return []string{}
}
nums = append(nums, int64(n))
} else {
return []string{}
}
}
eqInt := equivalentInterger(nums)
ipv6 := ipv4ToHex(nums)
return []string{eqInt, ipv6}
}
func ipv4Toipv6(ipv4 string) string {
values := strings.Split(ipv4, ".")
nums := make([]int64, 0, 4)
for _, s := range values {
if n, err := strconv.Atoi(s); err == nil {
nums = append(nums, int64(n))
}
}
return ipv4ToHex(nums)
}