-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
60 lines (51 loc) · 951 Bytes
/
util.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
package goturn
import (
"encoding/binary"
"fmt"
"math/rand"
"strings"
"time"
"unsafe"
)
const (
IPV4 = 0
IPV6 = 1
)
var nativeEndian binary.ByteOrder
func init() {
rand.Seed(time.Now().Unix())
buf := [2]byte{}
*(*uint16)(unsafe.Pointer(&buf[0])) = uint16(0xABCD)
switch buf {
case [2]byte{0xCD, 0xAB}:
fmt.Print("little endian")
nativeEndian = binary.LittleEndian
case [2]byte{0xAB, 0xCD}:
nativeEndian = binary.BigEndian
default:
panic("Could not determine native endianness.")
}
}
func checkIPAddressType(ip string) int {
if strings.Contains(ip, ":") {
return IPV6
} else {
return IPV4
}
}
func align4Bytes(length uint16) uint16 {
return (length + 3) / 4 * 4
}
func genReservedToken() (token []byte) {
token = make([]byte, 8)
for i := 0; i < 8; i++ {
token[i] = byte(rand.Uint32())
}
return
}
func genTransactionId() (id [12]byte) {
for i := 0; i < 8; i++ {
id[i] = byte(rand.Uint32())
}
return
}