1
2
3
4
5
6
7 package socket
8
9 import (
10 "errors"
11 "net"
12 "runtime"
13 "unsafe"
14 )
15
16 var errNotImplemented = errors.New("not implemented on " + runtime.GOOS + "/" + runtime.GOARCH)
17
18
19 type Option struct {
20 Level int
21 Name int
22 Len int
23 }
24
25
26
27 func (o *Option) Get(c *Conn, b []byte) (int, error) {
28 if o.Name < 1 || o.Len < 1 {
29 return 0, errors.New("invalid option")
30 }
31 if len(b) < o.Len {
32 return 0, errors.New("short buffer")
33 }
34 return o.get(c, b)
35 }
36
37
38
39
40 func (o *Option) GetInt(c *Conn) (int, error) {
41 if o.Len != 1 && o.Len != 4 {
42 return 0, errors.New("invalid option")
43 }
44 var b []byte
45 var bb [4]byte
46 if o.Len == 1 {
47 b = bb[:1]
48 } else {
49 b = bb[:4]
50 }
51 n, err := o.get(c, b)
52 if err != nil {
53 return 0, err
54 }
55 if n != o.Len {
56 return 0, errors.New("invalid option length")
57 }
58 if o.Len == 1 {
59 return int(b[0]), nil
60 }
61 return int(NativeEndian.Uint32(b[:4])), nil
62 }
63
64
65 func (o *Option) Set(c *Conn, b []byte) error {
66 if o.Name < 1 || o.Len < 1 {
67 return errors.New("invalid option")
68 }
69 if len(b) < o.Len {
70 return errors.New("short buffer")
71 }
72 return o.set(c, b)
73 }
74
75
76
77
78 func (o *Option) SetInt(c *Conn, v int) error {
79 if o.Len != 1 && o.Len != 4 {
80 return errors.New("invalid option")
81 }
82 var b []byte
83 if o.Len == 1 {
84 b = []byte{byte(v)}
85 } else {
86 var bb [4]byte
87 NativeEndian.PutUint32(bb[:o.Len], uint32(v))
88 b = bb[:4]
89 }
90 return o.set(c, b)
91 }
92
93
94 func ControlMessageSpace(dataLen int) int {
95 return controlMessageSpace(dataLen)
96 }
97
98
99
100
101
102
103
104
105 type ControlMessage []byte
106
107
108
109 func (m ControlMessage) Data(dataLen int) []byte {
110 l := controlHeaderLen()
111 if len(m) < l || len(m) < l+dataLen {
112 return nil
113 }
114 return m[l : l+dataLen]
115 }
116
117
118
119
120 func (m ControlMessage) Next(dataLen int) ControlMessage {
121 l := ControlMessageSpace(dataLen)
122 if len(m) < l {
123 return nil
124 }
125 return m[l:]
126 }
127
128
129
130 func (m ControlMessage) MarshalHeader(lvl, typ, dataLen int) error {
131 if len(m) < controlHeaderLen() {
132 return errors.New("short message")
133 }
134 h := (*cmsghdr)(unsafe.Pointer(&m[0]))
135 h.set(controlMessageLen(dataLen), lvl, typ)
136 return nil
137 }
138
139
140
141 func (m ControlMessage) ParseHeader() (lvl, typ, dataLen int, err error) {
142 l := controlHeaderLen()
143 if len(m) < l {
144 return 0, 0, 0, errors.New("short message")
145 }
146 h := (*cmsghdr)(unsafe.Pointer(&m[0]))
147 return h.lvl(), h.typ(), int(uint64(h.len()) - uint64(l)), nil
148 }
149
150
151
152 func (m ControlMessage) Marshal(lvl, typ int, data []byte) (ControlMessage, error) {
153 l := len(data)
154 if len(m) < ControlMessageSpace(l) {
155 return nil, errors.New("short message")
156 }
157 h := (*cmsghdr)(unsafe.Pointer(&m[0]))
158 h.set(controlMessageLen(l), lvl, typ)
159 if l > 0 {
160 copy(m.Data(l), data)
161 }
162 return m.Next(l), nil
163 }
164
165
166
167
168 func (m ControlMessage) Parse() ([]ControlMessage, error) {
169 var ms []ControlMessage
170 for len(m) >= controlHeaderLen() {
171 h := (*cmsghdr)(unsafe.Pointer(&m[0]))
172 l := h.len()
173 if l <= 0 {
174 return nil, errors.New("invalid header length")
175 }
176 if uint64(l) < uint64(controlHeaderLen()) {
177 return nil, errors.New("invalid message length")
178 }
179 if uint64(l) > uint64(len(m)) {
180 return nil, errors.New("short buffer")
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 ms = append(ms, ControlMessage(m[:l]))
199 ll := l - controlHeaderLen()
200 if len(m) >= ControlMessageSpace(ll) {
201 m = m[ControlMessageSpace(ll):]
202 } else {
203 m = m[controlMessageLen(ll):]
204 }
205 }
206 return ms, nil
207 }
208
209
210 func NewControlMessage(dataLen []int) ControlMessage {
211 var l int
212 for i := range dataLen {
213 l += ControlMessageSpace(dataLen[i])
214 }
215 return make([]byte, l)
216 }
217
218
219 type Message struct {
220
221
222
223
224 Buffers [][]byte
225
226
227
228 OOB []byte
229
230
231
232
233
234
235 Addr net.Addr
236
237 N int
238 NN int
239 Flags int
240 }
241
242
243
244
245
246 func (c *Conn) RecvMsg(m *Message, flags int) error {
247 return c.recvMsg(m, flags)
248 }
249
250
251
252
253
254 func (c *Conn) SendMsg(m *Message, flags int) error {
255 return c.sendMsg(m, flags)
256 }
257
258
259
260
261
262
263
264
265
266 func (c *Conn) RecvMsgs(ms []Message, flags int) (int, error) {
267 return c.recvMsgs(ms, flags)
268 }
269
270
271
272
273
274
275
276
277
278 func (c *Conn) SendMsgs(ms []Message, flags int) (int, error) {
279 return c.sendMsgs(ms, flags)
280 }
281
View as plain text