-
Notifications
You must be signed in to change notification settings - Fork 48
/
helper.go
56 lines (47 loc) · 1.36 KB
/
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
package tc
import (
"net"
)
// ipToUint32 converts a legacy ip object to its uint32 representative.
// For IPv6 addresses it returns ErrInvalidArg.
func ipToUint32(ip net.IP) (uint32, error) {
tmp := ip.To4()
if tmp == nil {
return 0, ErrInvalidArg
}
return nativeEndian.Uint32(tmp), nil
}
// uint32ToIP converts a legacy ip to a net.IP object.
func uint32ToIP(ip uint32) net.IP {
netIP := make(net.IP, 4)
nativeEndian.PutUint32(netIP, ip)
return netIP
}
// bytesToIP converts a slice of bytes into a net.IP object.
func bytesToIP(ip []byte) (net.IP, error) {
if len(ip) != net.IPv4len && len(ip) != net.IPv6len {
return nil, ErrInvalidArg
}
return net.IP(ip), nil
}
// ipToBytes casts a ip object into its byte slice representative.
func ipToBytes(ip net.IP) []byte {
return []byte(ip)
}
// bytesToHardwareAddr converts a slice of bytes into a net.HardwareAddr object.
func bytesToHardwareAddr(mac []byte) net.HardwareAddr {
return net.HardwareAddr(mac[:])
}
// hardwareAddrToBytes casts a net.HardwareAddr object into its byte slice representative.
func hardwareAddrToBytes(mac net.HardwareAddr) []byte {
return []byte(mac)
}
func endianSwapUint16(in uint16) uint16 {
return (in << 8) | (in >> 8)
}
func endianSwapUint32(in uint32) uint32 {
return ((in & 0x000000FF) << 24) |
((in & 0x0000FF00) << 8) |
((in & 0x00FF0000) >> 8) |
((in & 0xFF000000) >> 24)
}