~fnux/yggdrasil-go-coap

yggdrasil-go-coap/message.go -rw-r--r-- 24.1 KiB
614f652bTimothée Floure Add syntax highlighting to code snippets in README 1 year, 6 months ago
                                                                                
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
package coap

import (
	"encoding/binary"
	"fmt"
	"io"
	"log"
	"reflect"
	"strconv"
	"strings"
)

// COAPType represents the message type.
type COAPType uint8

// MaxTokenSize maximum of token size that can be used in message
const MaxTokenSize = 8

const (
	// Confirmable messages require acknowledgements.
	Confirmable COAPType = 0
	// NonConfirmable messages do not require acknowledgements.
	NonConfirmable COAPType = 1
	// Acknowledgement is a message indicating a response to confirmable message.
	Acknowledgement COAPType = 2
	// Reset indicates a permanent negative acknowledgement.
	Reset COAPType = 3
)

var typeNames = [256]string{
	Confirmable:     "Confirmable",
	NonConfirmable:  "NonConfirmable",
	Acknowledgement: "Acknowledgement",
	Reset:           "Reset",
}

const (
	max1ByteNumber = uint32(^uint8(0))
	max2ByteNumber = uint32(^uint16(0))
	max3ByteNumber = uint32(0xffffff)
)

func init() {
	for i := range typeNames {
		if typeNames[i] == "" {
			typeNames[i] = fmt.Sprintf("Unknown (0x%x)", i)
		}
	}
}

func (t COAPType) String() string {
	return typeNames[t]
}

// COAPCode is the type used for both request and response codes.
type COAPCode uint8

// Request Codes
const (
	GET    COAPCode = 1
	POST   COAPCode = 2
	PUT    COAPCode = 3
	DELETE COAPCode = 4
)

// Response Codes
const (
	Empty                   COAPCode = 0
	Created                 COAPCode = 65
	Deleted                 COAPCode = 66
	Valid                   COAPCode = 67
	Changed                 COAPCode = 68
	Content                 COAPCode = 69
	Continue                COAPCode = 95
	BadRequest              COAPCode = 128
	Unauthorized            COAPCode = 129
	BadOption               COAPCode = 130
	Forbidden               COAPCode = 131
	NotFound                COAPCode = 132
	MethodNotAllowed        COAPCode = 133
	NotAcceptable           COAPCode = 134
	RequestEntityIncomplete COAPCode = 136
	PreconditionFailed      COAPCode = 140
	RequestEntityTooLarge   COAPCode = 141
	UnsupportedMediaType    COAPCode = 143
	InternalServerError     COAPCode = 160
	NotImplemented          COAPCode = 161
	BadGateway              COAPCode = 162
	ServiceUnavailable      COAPCode = 163
	GatewayTimeout          COAPCode = 164
	ProxyingNotSupported    COAPCode = 165
)

//Signaling Codes for TCP
const (
	CSM     COAPCode = 225
	Ping    COAPCode = 226
	Pong    COAPCode = 227
	Release COAPCode = 228
	Abort   COAPCode = 229
)

var codeNames = [256]string{
	GET:                   "GET",
	POST:                  "POST",
	PUT:                   "PUT",
	DELETE:                "DELETE",
	Created:               "Created",
	Deleted:               "Deleted",
	Valid:                 "Valid",
	Changed:               "Changed",
	Content:               "Content",
	BadRequest:            "BadRequest",
	Unauthorized:          "Unauthorized",
	BadOption:             "BadOption",
	Forbidden:             "Forbidden",
	NotFound:              "NotFound",
	MethodNotAllowed:      "MethodNotAllowed",
	NotAcceptable:         "NotAcceptable",
	PreconditionFailed:    "PreconditionFailed",
	RequestEntityTooLarge: "RequestEntityTooLarge",
	UnsupportedMediaType:  "UnsupportedMediaType",
	InternalServerError:   "InternalServerError",
	NotImplemented:        "NotImplemented",
	BadGateway:            "BadGateway",
	ServiceUnavailable:    "ServiceUnavailable",
	GatewayTimeout:        "GatewayTimeout",
	ProxyingNotSupported:  "ProxyingNotSupported",
	CSM:                   "Capabilities and Settings Messages",
	Ping:                  "Ping",
	Pong:                  "Pong",
	Release:               "Release",
	Abort:                 "Abort",
}

func init() {
	for i := range codeNames {
		if codeNames[i] == "" {
			codeNames[i] = fmt.Sprintf("Unknown (0x%x)", i)
		}
	}
}

func (c COAPCode) String() string {
	return codeNames[c]
}

// OptionID identifies an option in a message.
type OptionID uint16

/*
   +-----+----+---+---+---+----------------+--------+--------+---------+
   | No. | C  | U | N | R | Name           | Format | Length | Default |
   +-----+----+---+---+---+----------------+--------+--------+---------+
   |   1 | x  |   |   | x | If-Match       | opaque | 0-8    | (none)  |
   |   3 | x  | x | - |   | Uri-Host       | string | 1-255  | (see    |
   |     |    |   |   |   |                |        |        | below)  |
   |   4 |    |   |   | x | ETag           | opaque | 1-8    | (none)  |
   |   5 | x  |   |   |   | If-None-Match  | empty  | 0      | (none)  |
   |   7 | x  | x | - |   | Uri-Port       | uint   | 0-2    | (see    |
   |     |    |   |   |   |                |        |        | below)  |
   |   8 |    |   |   | x | Location-Path  | string | 0-255  | (none)  |
   |  11 | x  | x | - | x | Uri-Path       | string | 0-255  | (none)  |
   |  12 |    |   |   |   | Content-Format | uint   | 0-2    | (none)  |
   |  14 |    | x | - |   | Max-Age        | uint   | 0-4    | 60      |
   |  15 | x  | x | - | x | Uri-Query      | string | 0-255  | (none)  |
   |  17 | x  |   |   |   | Accept         | uint   | 0-2    | (none)  |
   |  20 |    |   |   | x | Location-Query | string | 0-255  | (none)  |
   |  23 | x  | x | - | - | Block2         | uint   | 0-3    | (none)  |
   |  27 | x  | x | - | - | Block1         | uint   | 0-3    | (none)  |
   |  28 |    |   | x |   | Size2          | uint   | 0-4    | (none)  |
   |  35 | x  | x | - |   | Proxy-Uri      | string | 1-1034 | (none)  |
   |  39 | x  | x | - |   | Proxy-Scheme   | string | 1-255  | (none)  |
   |  60 |    |   | x |   | Size1          | uint   | 0-4    | (none)  |
   +-----+----+---+---+---+----------------+--------+--------+---------+
   C=Critical, U=Unsafe, N=NoCacheKey, R=Repeatable
*/

// Option IDs.
const (
	IfMatch       OptionID = 1
	URIHost       OptionID = 3
	ETag          OptionID = 4
	IfNoneMatch   OptionID = 5
	Observe       OptionID = 6
	URIPort       OptionID = 7
	LocationPath  OptionID = 8
	URIPath       OptionID = 11
	ContentFormat OptionID = 12
	MaxAge        OptionID = 14
	URIQuery      OptionID = 15
	Accept        OptionID = 17
	LocationQuery OptionID = 20
	Block2        OptionID = 23
	Block1        OptionID = 27
	Size2         OptionID = 28
	ProxyURI      OptionID = 35
	ProxyScheme   OptionID = 39
	Size1         OptionID = 60
	NoResponse    OptionID = 258
)

// Option value format (RFC7252 section 3.2)
type valueFormat uint8

const (
	valueUnknown valueFormat = iota
	valueEmpty
	valueOpaque
	valueUint
	valueString
)

type optionDef struct {
	valueFormat valueFormat
	minLen      int
	maxLen      int
}

var coapOptionDefs = map[OptionID]optionDef{
	IfMatch:       optionDef{valueFormat: valueOpaque, minLen: 0, maxLen: 8},
	URIHost:       optionDef{valueFormat: valueString, minLen: 1, maxLen: 255},
	ETag:          optionDef{valueFormat: valueOpaque, minLen: 1, maxLen: 8},
	IfNoneMatch:   optionDef{valueFormat: valueEmpty, minLen: 0, maxLen: 0},
	Observe:       optionDef{valueFormat: valueUint, minLen: 0, maxLen: 3},
	URIPort:       optionDef{valueFormat: valueUint, minLen: 0, maxLen: 2},
	LocationPath:  optionDef{valueFormat: valueString, minLen: 0, maxLen: 255},
	URIPath:       optionDef{valueFormat: valueString, minLen: 0, maxLen: 255},
	ContentFormat: optionDef{valueFormat: valueUint, minLen: 0, maxLen: 2},
	MaxAge:        optionDef{valueFormat: valueUint, minLen: 0, maxLen: 4},
	URIQuery:      optionDef{valueFormat: valueString, minLen: 0, maxLen: 255},
	Accept:        optionDef{valueFormat: valueUint, minLen: 0, maxLen: 2},
	LocationQuery: optionDef{valueFormat: valueString, minLen: 0, maxLen: 255},
	Block2:        optionDef{valueFormat: valueUint, minLen: 0, maxLen: 3},
	Block1:        optionDef{valueFormat: valueUint, minLen: 0, maxLen: 3},
	Size2:         optionDef{valueFormat: valueUint, minLen: 0, maxLen: 4},
	ProxyURI:      optionDef{valueFormat: valueString, minLen: 1, maxLen: 1034},
	ProxyScheme:   optionDef{valueFormat: valueString, minLen: 1, maxLen: 255},
	Size1:         optionDef{valueFormat: valueUint, minLen: 0, maxLen: 4},
	NoResponse:    optionDef{valueFormat: valueUint, minLen: 0, maxLen: 1},
}

// MediaType specifies the content format of a message.
type MediaType uint16

// Content formats.
const (
	TextPlain         MediaType = 0     // text/plain;charset=utf-8
	AppCoseEncrypt0   MediaType = 16    // application/cose; cose-type="cose-encrypt0" (RFC 8152)
	AppCoseMac0       MediaType = 17    // application/cose; cose-type="cose-mac0" (RFC 8152)
	AppCoseSign1      MediaType = 18    // application/cose; cose-type="cose-sign1" (RFC 8152)
	AppLinkFormat     MediaType = 40    // application/link-format
	AppXML            MediaType = 41    // application/xml
	AppOctets         MediaType = 42    // application/octet-stream
	AppExi            MediaType = 47    // application/exi
	AppJSON           MediaType = 50    // application/json
	AppJsonPatch      MediaType = 51    //application/json-patch+json (RFC6902)
	AppJsonMergePatch MediaType = 52    //application/merge-patch+json (RFC7396)
	AppCBOR           MediaType = 60    //application/cbor (RFC 7049)
	AppCWT            MediaType = 61    //application/cwt
	AppCoseEncrypt    MediaType = 96    //application/cose; cose-type="cose-encrypt" (RFC 8152)
	AppCoseMac        MediaType = 97    //application/cose; cose-type="cose-mac" (RFC 8152)
	AppCoseSign       MediaType = 98    //application/cose; cose-type="cose-sign" (RFC 8152)
	AppCoseKey        MediaType = 101   //application/cose-key (RFC 8152)
	AppCoseKeySet     MediaType = 102   //application/cose-key-set (RFC 8152)
	AppCoapGroup      MediaType = 256   //coap-group+json (RFC 7390)
	AppOcfCbor        MediaType = 10000 //application/vnd.ocf+cbor
	AppLwm2mTLV       MediaType = 11542 //application/vnd.oma.lwm2m+tlv
	AppLwm2mJSON      MediaType = 11543 //application/vnd.oma.lwm2m+json
)

func (c MediaType) String() string {
	switch c {
	case TextPlain:
		return "text/plain;charset=utf-8"
	case AppCoseEncrypt0:
		return "application/cose; cose-type=\"cose-encrypt0\"" // (RFC 8152)
	case AppCoseMac0:
		return "application/cose; cose-type=\"cose-mac0\"" // (RFC 8152)
	case AppCoseSign1:
		return "application/cose; cose-type=\"cose-sign1\"" // (RFC 8152)
	case AppLinkFormat:
		return "application/link-format"
	case AppXML:
		return "application/xml"
	case AppOctets:
		return "application/octet-stream"
	case AppExi:
		return "application/exi"
	case AppJSON:
		return "application/json"
	case AppJsonPatch:
		return "application/json-patch+json" // (RFC6902)
	case AppJsonMergePatch:
		return "application/merge-patch+json" // (RFC7396)
	case AppCBOR:
		return "application/cbor" // (RFC 7049)
	case AppCWT:
		return "application/cwt"
	case AppCoseEncrypt:
		return "application/cose; cose-type=\"cose-encrypt\"" // (RFC 8152)
	case AppCoseMac:
		return "application/cose; cose-type=\"cose-mac\"" // (RFC 8152)
	case AppCoseSign:
		return "application/cose; cose-type=\"cose-sign\"" // (RFC 8152)
	case AppCoseKey:
		return "application/cose-key" // (RFC 8152)
	case AppCoseKeySet:
		return "application/cose-key-set" // (RFC 8152)
	case AppCoapGroup:
		return "coap-group+json" // (RFC 7390)
	case AppOcfCbor:
		return "application/vnd.ocf+cbor"
	case AppLwm2mTLV:
		return "application/vnd.oma.lwm2m+tlv"
	case AppLwm2mJSON:
		return "application/vnd.oma.lwm2m+json"
	}
	return "Unknown media type: 0x" + strconv.FormatInt(int64(c), 16)
}

type option struct {
	ID    OptionID
	Value interface{}
}

func SetOptionDef(oid OptionID, format string, minLen int, maxLen int) {
	var encodedFormat valueFormat
	switch format {
	case "empty":
		encodedFormat = valueEmpty
	case "opaque":
		encodedFormat = valueOpaque
	case "uint":
		encodedFormat = valueUint
	case "string":
		encodedFormat = valueString
	default:
		encodedFormat = valueUnknown
	}

	coapOptionDefs[oid] = optionDef{valueFormat: encodedFormat, minLen: minLen, maxLen: maxLen}
}

func encodeInt(buf io.Writer, v uint32) error {
	switch {
	case v == 0:
	case v <= max1ByteNumber:
		buf.Write([]byte{byte(v)})
	case v <= max2ByteNumber:
		return binary.Write(buf, binary.BigEndian, uint16(v))
	case v <= max3ByteNumber:
		rv := []byte{0, 0, 0, 0}
		binary.BigEndian.PutUint32(rv, uint32(v))
		_, err := buf.Write(rv[1:])
		return err
	default:
		return binary.Write(buf, binary.BigEndian, uint32(v))
	}
	return nil
}

func lengthInt(v uint32) int {
	switch {
	case v == 0:
		return 0
	case v <= max1ByteNumber:
		return 1
	case v <= max2ByteNumber:
		return 2
	case v <= max3ByteNumber:
		return 3
	default:
		return 4
	}
}

func decodeInt(b []byte) uint32 {
	tmp := []byte{0, 0, 0, 0}
	copy(tmp[4-len(b):], b)
	return binary.BigEndian.Uint32(tmp)
}

func (o option) writeData(buf io.Writer) error {
	var v uint32

	switch i := o.Value.(type) {
	case string:
		_, err := buf.Write([]byte(i))
		return err
	case []byte:
		_, err := buf.Write(i)
		return err
	case MediaType:
		v = uint32(i)
	case int:
		v = uint32(i)
	case int32:
		v = uint32(i)
	case uint:
		v = uint32(i)
	case uint32:
		v = i
	default:
		return fmt.Errorf("invalid type for option %x: %T (%v)",
			o.ID, o.Value, o.Value)
	}

	return encodeInt(buf, v)
}

func (o option) toBytesLength() (int, error) {
	var v uint32

	switch i := o.Value.(type) {
	case string:
		return len(i), nil
	case []byte:
		return len(i), nil
	case MediaType:
		v = uint32(i)
	case int:
		v = uint32(i)
	case int32:
		v = uint32(i)
	case uint:
		v = uint32(i)
	case uint32:
		v = i
	default:
		return 0, fmt.Errorf("invalid type for option %x: %T (%v)",
			o.ID, o.Value, o.Value)
	}

	return lengthInt(v), nil
}

func parseOptionValue(optionDefs map[OptionID]optionDef, optionID OptionID, valueBuf []byte) interface{} {
	if def, ok := optionDefs[optionID]; ok {
		if def.valueFormat == valueUnknown {
			// Skip unrecognized options (RFC7252 section 5.4.1)
			return nil
		}
		if len(valueBuf) < def.minLen || len(valueBuf) > def.maxLen {
			// Skip options with illegal value length (RFC7252 section 5.4.3)
			return nil
		}
		switch def.valueFormat {
		case valueUint:
			intValue := decodeInt(valueBuf)
			if optionID == ContentFormat || optionID == Accept {
				return MediaType(intValue)
			}
			return intValue
		case valueString:
			return string(valueBuf)
		case valueOpaque, valueEmpty:
			return valueBuf
		}
	}
	// Skip unrecognized options (should never be reached)
	return nil
}

type options []option

func (o options) Len() int {
	return len(o)
}

func (o options) Less(i, j int) bool {
	if o[i].ID == o[j].ID {
		return i < j
	}
	return o[i].ID < o[j].ID
}

func (o options) Swap(i, j int) {
	o[i], o[j] = o[j], o[i]
}

func (o options) Remove(oid OptionID) options {
	idx := 0
	for i := 0; i < len(o); i++ {
		if o[i].ID != oid {
			o[idx] = o[i]
			idx++
		}
	}
	return o[:idx]
}

// Message represents the COAP message
type Message interface {
	Type() COAPType
	Code() COAPCode
	MessageID() uint16
	Token() []byte
	Payload() []byte
	AllOptions() options

	IsConfirmable() bool
	Options(o OptionID) []interface{}
	Option(o OptionID) interface{}
	optionStrings(o OptionID) []string
	Path() []string
	PathString() string
	SetPathString(s string)
	SetPath(s []string)
	SetType(typ COAPType)
	Query() []string
	QueryString() string
	SetQueryString(string)
	SetQuery([]string)
	SetURIQuery(s string)
	SetObserve(b uint32)
	SetPayload(p []byte)
	RemoveOption(opID OptionID)
	AddOption(opID OptionID, val interface{})
	SetOption(opID OptionID, val interface{})
	MarshalBinary(buf io.Writer) error
	UnmarshalBinary(data []byte) error
	SetToken(t []byte)
	SetMessageID(messageID uint16)
	ToBytesLength() (int, error)
	SetCode(code COAPCode)
}

// MessageParams params to create COAP message
type MessageParams struct {
	Type      COAPType
	Code      COAPCode
	MessageID uint16
	Token     []byte
	Payload   []byte
}

// MessageBase is a CoAP message.
type MessageBase struct {
	typ  COAPType
	code COAPCode

	token, payload []byte

	opts options
}

func (m *MessageBase) Type() COAPType {
	return m.typ
}

func (m *MessageBase) SetType(typ COAPType) {
	m.typ = typ
}

func (m *MessageBase) Code() COAPCode {
	return m.code
}

func (m *MessageBase) Token() []byte {
	return m.token
}

func (m *MessageBase) Payload() []byte {
	return m.payload
}

func (m *MessageBase) AllOptions() options {
	return m.opts
}

// IsConfirmable returns true if this message is confirmable.
func (m *MessageBase) IsConfirmable() bool {
	return m.typ == Confirmable
}

// Options gets all the values for the given option.
func (m *MessageBase) Options(o OptionID) []interface{} {
	var rv []interface{}

	for _, v := range m.opts {
		if o == v.ID {
			rv = append(rv, v.Value)
		}
	}

	return rv
}

// Option gets the first value for the given option ID.
func (m *MessageBase) Option(o OptionID) interface{} {
	for _, v := range m.opts {
		if o == v.ID {
			return v.Value
		}
	}
	return nil
}

func (m *MessageBase) optionStrings(o OptionID) []string {
	var rv []string
	for _, o := range m.Options(o) {
		rv = append(rv, o.(string))
	}
	return rv
}

// Path gets the Path set on this message if any.
func (m *MessageBase) Path() []string {
	return m.optionStrings(URIPath)
}

// PathString gets a path as a / separated string.
func (m *MessageBase) PathString() string {
	return strings.Join(m.Path(), "/")
}

// SetPathString sets a path by a / separated string.
func (m *MessageBase) SetPathString(s string) {
	switch s {
	case "", "/":
		//root path is not set as option
		return
	default:
		if s[0] == '/' {
			s = s[1:]
		}
		m.SetPath(strings.Split(s, "/"))
	}
}

// SetPath updates or adds a URIPath attribute on this message.
func (m *MessageBase) SetPath(s []string) {
	m.SetOption(URIPath, s)
}

// Query gets the Query set on this message if any.
func (m *MessageBase) Query() []string {
	return m.optionStrings(URIQuery)
}

// QueryString gets a path as an ampersand separated string.
func (m *MessageBase) QueryString() string {
	return strings.Join(m.Query(), "&")
}

// SetQueryString sets a query by an ampersand separated string.
func (m *MessageBase) SetQueryString(s string) {
	m.SetQuery(strings.Split(s, "&"))
}

// SetQuery updates or adds a URIQuery attribute on this message.
func (m *MessageBase) SetQuery(s []string) {
	m.SetOption(URIQuery, s)
}

// Set URIQuery attibute to the message
func (m *MessageBase) SetURIQuery(s string) {
	m.AddOption(URIQuery, s)
}

// Set Observer attribute to the message
func (m *MessageBase) SetObserve(b uint32) {
	m.SetOption(Observe, b)
}

// SetPayload
func (m *MessageBase) SetPayload(p []byte) {
	m.payload = p
}

// SetToken
func (m *MessageBase) SetToken(p []byte) {
	m.token = p
}

// RemoveOption removes all references to an option
func (m *MessageBase) RemoveOption(opID OptionID) {
	m.opts = m.opts.Remove(opID)
}

// AddOption adds an option.
func (m *MessageBase) AddOption(opID OptionID, val interface{}) {
	iv := reflect.ValueOf(val)
	if (iv.Kind() == reflect.Slice || iv.Kind() == reflect.Array) &&
		iv.Type().Elem().Kind() == reflect.String {
		for i := 0; i < iv.Len(); i++ {
			m.opts = append(m.opts, option{opID, iv.Index(i).Interface()})
		}
		return
	}
	m.opts = append(m.opts, option{opID, val})
}

// SetOption sets an option, discarding any previous value
func (m *MessageBase) SetOption(opID OptionID, val interface{}) {
	m.RemoveOption(opID)
	m.AddOption(opID, val)
}

// SetCode sets the coap code on the message
func (m *MessageBase) SetCode(code COAPCode) {
	m.code = code
}

const (
	extoptByteCode   = 13
	extoptByteAddend = 13
	extoptWordCode   = 14
	extoptWordAddend = 269
	extoptError      = 15
)

func writeOpt(o option, buf io.Writer, delta int) {
	/*
	     0   1   2   3   4   5   6   7
	   +---------------+---------------+
	   |               |               |
	   |  Option Delta | Option Length |   1 byte
	   |               |               |
	   +---------------+---------------+
	   \                               \
	   /         Option Delta          /   0-2 bytes
	   \          (extended)           \
	   +-------------------------------+
	   \                               \
	   /         Option Length         /   0-2 bytes
	   \          (extended)           \
	   +-------------------------------+
	   \                               \
	   /                               /
	   \                               \
	   /         Option Value          /   0 or more bytes
	   \                               \
	   /                               /
	   \                               \
	   +-------------------------------+

	   See parseExtOption(), extendOption()
	   and writeOptionHeader() below for implementation details
	*/

	writeOptHeader := func(delta, length int) {
		d, dx := extendOpt(delta)
		l, lx := extendOpt(length)

		buf.Write([]byte{byte(d<<4) | byte(l)})

		writeExt := func(opt, ext int) {
			switch opt {
			case extoptByteCode:
				buf.Write([]byte{byte(ext)})
			case extoptWordCode:
				binary.Write(buf, binary.BigEndian, uint16(ext))
			}
		}

		writeExt(d, dx)
		writeExt(l, lx)
	}

	len, err := o.toBytesLength()
	if err != nil {
		log.Fatal(err)
	} else {
		writeOptHeader(delta, len)
		o.writeData(buf)
	}
}

func writeOpts(buf io.Writer, opts options) {
	prev := 0
	for _, o := range opts {
		writeOpt(o, buf, int(o.ID)-prev)
		prev = int(o.ID)
	}
}

func extendOpt(opt int) (int, int) {
	ext := 0
	if opt >= extoptByteAddend {
		if opt >= extoptWordAddend {
			ext = opt - extoptWordAddend
			opt = extoptWordCode
		} else {
			ext = opt - extoptByteAddend
			opt = extoptByteCode
		}
	}
	return opt, ext
}

func lengthOptHeaderExt(opt, ext int) int {
	switch opt {
	case extoptByteCode:
		return 1
	case extoptWordCode:
		return 2
	}
	return 0
}

func lengthOptHeader(delta, length int) int {
	d, dx := extendOpt(delta)
	l, lx := extendOpt(length)

	//buf.Write([]byte{byte(d<<4) | byte(l)})
	res := 1

	res = res + lengthOptHeaderExt(d, dx)
	res = res + lengthOptHeaderExt(l, lx)
	return res
}

func lengthOpt(o option, delta int) int {
	/*
	     0   1   2   3   4   5   6   7
	   +---------------+---------------+
	   |               |               |
	   |  Option Delta | Option Length |   1 byte
	   |               |               |
	   +---------------+---------------+
	   \                               \
	   /         Option Delta          /   0-2 bytes
	   \          (extended)           \
	   +-------------------------------+
	   \                               \
	   /         Option Length         /   0-2 bytes
	   \          (extended)           \
	   +-------------------------------+
	   \                               \
	   /                               /
	   \                               \
	   /         Option Value          /   0 or more bytes
	   \                               \
	   /                               /
	   \                               \
	   +-------------------------------+

	   See parseExtOption(), extendOption()
	   and writeOptionHeader() below for implementation details
	*/

	res, err := o.toBytesLength()
	if err != nil {
		log.Fatal(err)
	} else {
		res = res + lengthOptHeader(delta, res)
	}
	return res
}

func bytesLengthOpts(opts options) int {
	length := 0
	prev := 0
	for _, o := range opts {
		length = length + lengthOpt(o, int(o.ID)-prev)
		prev = int(o.ID)
	}
	return length
}

// parseBody extracts the options and payload from a byte slice.  The supplied
// byte slice contains everything following the message header (everything
// after the token).
func parseBody(optionDefs map[OptionID]optionDef, data []byte) (options, []byte, error) {
	prev := 0

	parseExtOpt := func(opt int) (int, error) {
		switch opt {
		case extoptByteCode:
			if len(data) < 1 {
				return -1, ErrOptionTruncated
			}
			opt = int(data[0]) + extoptByteAddend
			data = data[1:]
		case extoptWordCode:
			if len(data) < 2 {
				return -1, ErrOptionTruncated
			}
			opt = int(binary.BigEndian.Uint16(data[:2])) + extoptWordAddend
			data = data[2:]
		}
		return opt, nil
	}

	var opts options

	for len(data) > 0 {
		if data[0] == 0xff {
			data = data[1:]
			break
		}

		delta := int(data[0] >> 4)
		length := int(data[0] & 0x0f)

		if delta == extoptError || length == extoptError {
			return nil, nil, ErrOptionUnexpectedExtendMarker
		}

		data = data[1:]

		delta, err := parseExtOpt(delta)
		if err != nil {
			return nil, nil, err
		}
		length, err = parseExtOpt(length)
		if err != nil {
			return nil, nil, err
		}

		if len(data) < length {
			return nil, nil, ErrMessageTruncated
		}

		oid := OptionID(prev + delta)
		opval := parseOptionValue(optionDefs, oid, data[:length])
		data = data[length:]
		prev = int(oid)

		if opval != nil {
			opt := option{ID: oid, Value: opval}
			opts = append(opts, opt)
		}
	}

	return opts, data, nil
}