forked from dustin/go-coap
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmessagetcp.go
286 lines (240 loc) · 7.17 KB
/
messagetcp.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package coap
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"sort"
)
const (
TCP_MESSAGE_LEN13_BASE = 13
TCP_MESSAGE_LEN14_BASE = 269
TCP_MESSAGE_LEN15_BASE = 65805
TCP_MESSAGE_MAX_LEN = 0x7fff0000 // Large number that works in 32-bit builds.
)
// TcpMessage is a CoAP MessageBase that can encode itself for TCP
// transport.
type TcpMessage struct {
MessageBase
}
func NewTcpMessage(p MessageParams) *TcpMessage {
return &TcpMessage{
MessageBase{
typ: p.Type,
code: p.Code,
messageID: p.MessageID,
token: p.Token,
payload: p.Payload,
},
}
}
func (m *TcpMessage) MarshalBinary() ([]byte, error) {
/*
A CoAP TCP message looks like:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Len | TKL | Extended Length ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Code | TKL bytes ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options (if any) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|1 1 1 1 1 1 1 1| Payload (if any) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
The size of the Extended Length field is inferred from the value of the
Len field as follows:
| Len value | Extended Length size | Total length |
+------------+-----------------------+---------------------------+
| 0-12 | 0 | Len |
| 13 | 1 | Extended Length + 13 |
| 14 | 2 | Extended Length + 269 |
| 15 | 4 | Extended Length + 65805 |
*/
buf := bytes.Buffer{}
sort.Stable(&m.MessageBase.opts)
writeOpts(&buf, m.MessageBase.opts)
if len(m.MessageBase.payload) > 0 {
buf.Write([]byte{0xff})
buf.Write(m.MessageBase.payload)
}
var lenNib uint8
var extLenBytes []byte
if buf.Len() < TCP_MESSAGE_LEN13_BASE {
lenNib = uint8(buf.Len())
} else if buf.Len() < TCP_MESSAGE_LEN14_BASE {
lenNib = 13
extLen := buf.Len() - TCP_MESSAGE_LEN13_BASE
extLenBytes = []byte{uint8(extLen)}
} else if buf.Len() < TCP_MESSAGE_LEN15_BASE {
lenNib = 14
extLen := buf.Len() - TCP_MESSAGE_LEN14_BASE
extLenBytes = make([]byte, 2)
binary.BigEndian.PutUint16(extLenBytes, uint16(extLen))
} else if buf.Len() < TCP_MESSAGE_MAX_LEN {
lenNib = 15
extLen := buf.Len() - TCP_MESSAGE_LEN15_BASE
extLenBytes = make([]byte, 4)
binary.BigEndian.PutUint32(extLenBytes, uint32(extLen))
}
hdr := make([]byte, 1+len(extLenBytes)+len(m.MessageBase.token)+1)
hdrOff := 0
// Length and TKL nibbles.
hdr[hdrOff] = uint8(0xf&len(m.MessageBase.token)) | (lenNib << 4)
hdrOff++
// Extended length, if present.
if len(extLenBytes) > 0 {
copy(hdr[hdrOff:hdrOff+len(extLenBytes)], extLenBytes)
hdrOff += len(extLenBytes)
}
// Code.
hdr[hdrOff] = byte(m.MessageBase.code)
hdrOff++
// Token.
if len(m.MessageBase.token) > 0 {
copy(hdr[hdrOff:hdrOff+len(m.MessageBase.token)], m.MessageBase.token)
hdrOff += len(m.MessageBase.token)
}
return append(hdr, buf.Bytes()...), nil
}
// msgTcpInfo describes a single TCP CoAP message. Used during reassembly.
type msgTcpInfo struct {
typ uint8
token []byte
code uint8
hdrLen int
totLen int
}
// readTcpMsgInfo infers information about a TCP CoAP message from the first
// fragment.
func readTcpMsgInfo(r io.Reader) (msgTcpInfo, error) {
mti := msgTcpInfo{}
hdrOff := 0
var firstByte byte
if err := binary.Read(r, binary.BigEndian, &firstByte); err != nil {
return mti, err
}
hdrOff++
lenNib := (firstByte & 0xf0) >> 4
tkl := firstByte & 0x0f
var opLen int
if lenNib < TCP_MESSAGE_LEN13_BASE {
opLen = int(lenNib)
} else if lenNib == 13 {
var extLen byte
if err := binary.Read(r, binary.BigEndian, &extLen); err != nil {
return mti, err
}
hdrOff++
opLen = TCP_MESSAGE_LEN13_BASE + int(extLen)
} else if lenNib == 14 {
var extLen uint16
if err := binary.Read(r, binary.BigEndian, &extLen); err != nil {
return mti, err
}
hdrOff += 2
opLen = TCP_MESSAGE_LEN14_BASE + int(extLen)
} else if lenNib == 15 {
var extLen uint32
if err := binary.Read(r, binary.BigEndian, &extLen); err != nil {
return mti, err
}
hdrOff += 4
opLen = TCP_MESSAGE_LEN15_BASE + int(extLen)
}
mti.totLen = hdrOff + 1 + int(tkl) + opLen
if err := binary.Read(r, binary.BigEndian, &mti.code); err != nil {
return mti, err
}
hdrOff++
mti.token = make([]byte, tkl)
if _, err := io.ReadFull(r, mti.token); err != nil {
return mti, err
}
hdrOff += int(tkl)
mti.hdrLen = hdrOff
return mti, nil
}
func readTcpMsgBody(mti msgTcpInfo, r io.Reader) (options, []byte, error) {
bodyLen := mti.totLen - mti.hdrLen
b := make([]byte, bodyLen)
if _, err := io.ReadFull(r, b); err != nil {
return nil, nil, err
}
o, p, err := parseBody(b)
if err != nil {
return nil, nil, err
}
return o, p, nil
}
func (m *TcpMessage) fill(mti msgTcpInfo, o options, p []byte) {
m.MessageBase.typ = COAPType(mti.typ)
m.MessageBase.code = COAPCode(mti.code)
m.MessageBase.token = mti.token
m.MessageBase.opts = o
m.MessageBase.payload = p
}
func (m *TcpMessage) UnmarshalBinary(data []byte) error {
r := bytes.NewReader(data)
mti, err := readTcpMsgInfo(r)
if err != nil {
return fmt.Errorf("Error reading TCP CoAP header; %s", err.Error())
}
if len(data) != mti.totLen {
return fmt.Errorf("CoAP length mismatch (hdr=%d pkt=%d)",
mti.totLen, len(data))
}
o, p, err := readTcpMsgBody(mti, r)
if err != nil {
return err
}
m.fill(mti, o, p)
return nil
}
// PullTcp extracts a complete TCP CoAP message from the front of a byte queue.
//
// Return values:
// *TcpMessage: On success, points to the extracted message; nil if a complete
// message could not be extracted.
// []byte: The unread portion of of the supplied byte buffer. If a message
// was not extracted, this is the unchanged buffer that was passed in.
// error: Non-nil if the buffer contains an invalid CoAP message.
//
// Note: It is not an error if the supplied buffer does not contain a complete
// message. In such a case, nil *TclMessage and error values are returned
// along with the original buffer.
func PullTcp(data []byte) (*TcpMessage, []byte, error) {
r := bytes.NewReader(data)
m, err := Decode(r)
if err != nil {
if err == io.EOF || err == io.ErrUnexpectedEOF {
// Packet is incomplete.
return nil, data, nil
} else {
// Some other error.
return nil, data, err
}
}
// Determine the number of bytes read. These bytes get trimmed from the
// front of the returned data slice.
sz, err := r.Seek(0, io.SeekCurrent)
if err != nil {
// This should never happen.
return nil, data, err
}
return m, data[sz:], nil
}
// Decode reads a single message from its input.
func Decode(r io.Reader) (*TcpMessage, error) {
mti, err := readTcpMsgInfo(r)
if err != nil {
return nil, err
}
o, p, err := readTcpMsgBody(mti, r)
if err != nil {
return nil, err
}
m := &TcpMessage{}
m.fill(mti, o, p)
return m, nil
}