-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.go
57 lines (43 loc) · 833 Bytes
/
helper.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
// SPDX-FileCopyrightText: (c) Mauve Mailorder Software GmbH & Co. KG, 2020. Licensed under MIT license.
//
// SPDX-License-Identifier: MIT
package main
import (
"fmt"
"net"
"strconv"
)
func reverseIP(ip net.IP) string {
if ip.To4() != nil {
return reverseIPv4(ip.To4())
}
return reverseIPv6(ip)
}
func reverseIPv4(ip net.IP) string {
s := ""
for i := 3; i >= 0; i-- {
s += strconv.Itoa(int(ip[i])) + "."
}
return s
}
func reverseIPv6(ip net.IP) string {
s := ""
a := make([]uint8, 16)
for i, v := range ip {
a[i] = v
}
fmt.Println(a)
for i := 0; i < 8; i++ {
idx := 14 - (2 * i)
s += revserseBlock(ip[idx : idx+2])
}
return s
}
func revserseBlock(b []uint8) string {
hex := fmt.Sprintf("%02x%02x", b[0], b[1])
s := ""
for i := 3; i >= 0; i-- {
s += string(hex[i]) + "."
}
return s
}