functions for encoding and decoding ip addresses to hex
Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
parent
6daa417bc0
commit
6ac1e7e406
|
@ -1,9 +1,12 @@
|
||||||
package nettools
|
package nettools
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html"
|
"html"
|
||||||
|
"log"
|
||||||
|
"math/big"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
@ -24,6 +27,34 @@ func IP(r *http.Request) (net.IP, error) {
|
||||||
return net.ParseIP(ip), nil
|
return net.ParseIP(ip), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Inet6Aton Encodes ip address to hex
|
||||||
|
func Inet6Aton(ip net.IP) string {
|
||||||
|
ipv4 := false
|
||||||
|
if ip.To4() != nil {
|
||||||
|
ipv4 = true
|
||||||
|
}
|
||||||
|
ipInt := big.NewInt(0)
|
||||||
|
if ipv4 {
|
||||||
|
ipInt.SetBytes(ip.To4())
|
||||||
|
fmt.Println(ipInt)
|
||||||
|
ipHex := hex.EncodeToString(ipInt.Bytes())
|
||||||
|
return ipHex
|
||||||
|
}
|
||||||
|
ipInt.SetBytes(ip.To16())
|
||||||
|
ipHex := hex.EncodeToString(ipInt.Bytes())
|
||||||
|
return ipHex
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inet6AtonDecode decodes ip address from hex
|
||||||
|
func Inet6AtonDecode(ipHex string) (ip net.IP) {
|
||||||
|
hex, err := hex.DecodeString(ipHex)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return net.ParseIP("0.0.0.0")
|
||||||
|
}
|
||||||
|
return net.IP(hex)
|
||||||
|
}
|
||||||
|
|
||||||
// Hostname returns hostname
|
// Hostname returns hostname
|
||||||
func Hostname(ip net.IP) ([]string, error) {
|
func Hostname(ip net.IP) ([]string, error) {
|
||||||
names, err := net.LookupAddr(ip.String())
|
names, err := net.LookupAddr(ip.String())
|
||||||
|
|
Reference in New Issue
Block a user