69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
package converter
|
|
|
|
import (
|
|
log "github.com/cihub/seelog"
|
|
"net"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func MacToInt(IPv4Addr net.HardwareAddr) int64 {
|
|
bits := strings.Split(IPv4Addr.String(), ":")
|
|
|
|
b0, _ := strconv.ParseInt(bits[0], 16, 64)
|
|
b1, _ := strconv.ParseInt(bits[1], 16, 64)
|
|
b2, _ := strconv.ParseInt(bits[2], 16, 64)
|
|
b3, _ := strconv.ParseInt(bits[3], 16, 64)
|
|
b4, _ := strconv.ParseInt(bits[4], 16, 64)
|
|
b5, _ := strconv.ParseInt(bits[5], 16, 64)
|
|
|
|
var sum int64
|
|
|
|
//52242420297
|
|
//52242420297
|
|
sum += int64(b0) << 40
|
|
sum += int64(b1) << 32
|
|
sum += int64(b2) << 24
|
|
sum += int64(b3) << 16
|
|
sum += int64(b4) << 8
|
|
sum += int64(b5)
|
|
|
|
return sum
|
|
}
|
|
|
|
func IntToMac(ipInt int64) net.HardwareAddr {
|
|
|
|
b0 := strconv.FormatInt((ipInt>>40)&0xff, 16)
|
|
b1 := strconv.FormatInt((ipInt>>32)&0xff, 16)
|
|
b2 := strconv.FormatInt((ipInt>>24)&0xff, 16)
|
|
b3 := strconv.FormatInt((ipInt>>16)&0xff, 16)
|
|
b4 := strconv.FormatInt((ipInt>>8)&0xff, 16)
|
|
b5 := strconv.FormatInt((ipInt & 0xff), 16)
|
|
|
|
if len(b0) == 1 {
|
|
b0 = "0" + b0
|
|
}
|
|
if len(b1) == 1 {
|
|
b1 = "0" + b1
|
|
}
|
|
if len(b2) == 1 {
|
|
b2 = "0" + b2
|
|
}
|
|
if len(b3) == 1 {
|
|
b3 = "0" + b3
|
|
}
|
|
if len(b4) == 1 {
|
|
b4 = "0" + b4
|
|
}
|
|
if len(b5) == 1 {
|
|
b5 = "0" + b5
|
|
}
|
|
|
|
str := b0 + ":" + b1 + ":" + b2 + ":" + b3 + ":" + b4 + ":" + b5
|
|
|
|
log.Debug("mac string: ", str)
|
|
nt, _ := net.ParseMAC(str)
|
|
|
|
return nt
|
|
}
|