-
Notifications
You must be signed in to change notification settings - Fork 1
/
bytes.go
135 lines (117 loc) · 2.68 KB
/
bytes.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//gonfapi Package sort provides primitives for sorting slices and user-defined
//collections.
package gonfapi
import (
"encoding/binary"
"fmt"
"net"
"reflect"
"unsafe"
"github.com/mynuolr/gonfapi/basetype"
)
var hostByteOrder binary.ByteOrder
func init() {
var i int32 = 0x01020304
if *(*byte)(unsafe.Pointer(&i)) == 0x04 {
hostByteOrder = binary.LittleEndian
} else {
hostByteOrder = binary.BigEndian
}
}
func printAsBinary(bytes []byte) {
for i := 0; i < len(bytes); i++ {
for j := 0; j < 8; j++ {
zeroOrOne := bytes[i] >> (7 - j) & 1
fmt.Printf("%c", '0'+zeroOrOne)
}
fmt.Printf(" %p\n", &bytes[i])
}
fmt.Println()
}
type INT16 = basetype.INT16
type INT32 = basetype.INT32
type UINT16 = basetype.UINT16
type UINT32 = basetype.UINT32
type UINT64 = basetype.UINT64
//sockaddr_in4/6
type SockaddrInx struct {
Family UINT16 //AF_INT or AF_INT6. LittleEndian
Port UINT16 //Port. BigEndian
Data1 [4]byte //ipv4 Adder,ipv6 is zero. BigEndian
Data2 [16]byte //ipv6 Adder,ipv4 is zero. BigEndian
IPV6ScopeId UINT32 //ipv6 scope id
}
var emptyBytes16 = make([]byte, 16)
func (s *SockaddrInx) String() string {
_, ip := s.GetIP()
return fmt.Sprintf("[%s]:%d", ip, s.GetPort())
}
func (s *SockaddrInx) ToBytes() (data []byte) {
sh := (*reflect.SliceHeader)(unsafe.Pointer(&data))
sh.Data = uintptr(unsafe.Pointer(s))
sh.Len = 23
return
}
func (s *SockaddrInx) SetIP(v4 bool, ip net.IP) {
if v4 {
s.Family.Set(AF_INET)
copy(s.Data2[:], emptyBytes16)
copy(s.Data1[:], ip.To4())
s.IPV6ScopeId.Set(0)
} else {
s.Family.Set(AF_INET6)
copy(s.Data1[:], emptyBytes16)
copy(s.Data2[:], ip.To16())
}
}
func (s *SockaddrInx) GetIP() (v4 bool, ip net.IP) {
if !s.IsIpv6() {
return true, net.IP(s.Data1[:])
} else {
return false, net.IP(s.Data2[:])
}
}
func (s *SockaddrInx) IsIpv6() bool {
return AF_INET6 == s.Family.Get()
}
func (s *SockaddrInx) GetPort() uint16 {
return s.Port.BigEndianGet()
}
func (s *SockaddrInx) SetPort(p uint16) {
s.Port.BigEndianSet(p)
}
// IP Addres
//
// |0000|0000|0000|0000|
//
// |ipv4|
//
// |------ ipv6 -------|
type IpAddress [16]byte
func (s *IpAddress) SetIP(v4 bool, ip net.IP) {
if v4 {
copy(s[:], emptyBytes16)
copy(s[:4], ip.To4())
} else {
copy(s[:], ip.To16())
}
}
func (s *IpAddress) GetIP(v4 bool) (ip net.IP) {
if v4 {
return net.IP(s[:4])
} else {
return net.IP(s[:])
}
}
//指针转到数组切片
func PtrToBytes(b *byte, len int) (data []byte) {
sh := (*reflect.SliceHeader)(unsafe.Pointer(&data))
sh.Data = uintptr(unsafe.Pointer(b))
sh.Cap = len
sh.Len = len
return
}
//指针转到SockaddrInx
func PtrToAddress(b *byte) *SockaddrInx {
return (*SockaddrInx)(unsafe.Pointer(b))
}