message.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. package coap
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "errors"
  6. "fmt"
  7. "reflect"
  8. "sort"
  9. "strconv"
  10. "strings"
  11. )
  12. // COAPType 代表消息类型
  13. type COAPType uint8
  14. // MaxTokenSize 最大token size
  15. const MaxTokenSize = 8
  16. const (
  17. // CON 需要被确认的请求
  18. CON COAPType = 0
  19. // NON 不需要被确认的请求
  20. NON COAPType = 1
  21. // ACK 应答消息,接受到CON消息的响应
  22. ACK COAPType = 2
  23. // RST 复位消息,当接收者接受到的消息包含一个错误,接受者解析消息或者不再关心发送者发送的内容,那么复位消息将会被发送
  24. RST COAPType = 3
  25. )
  26. // COAPCode 请求方法的类型
  27. type COAPCode uint8
  28. // request codes
  29. const (
  30. GET COAPCode = iota + 1
  31. POST
  32. PUT
  33. DELETE
  34. )
  35. // Response codes
  36. const (
  37. Empty COAPCode = 0
  38. Created COAPCode = 65
  39. Deleted COAPCode = 66
  40. Valid COAPCode = 67
  41. Changed COAPCode = 68
  42. Content COAPCode = 69
  43. Continue COAPCode = 95
  44. BadRequest COAPCode = 128
  45. Unauthorized COAPCode = 129
  46. BadOption COAPCode = 130
  47. Forbidden COAPCode = 131
  48. NotFound COAPCode = 132
  49. MethodNotAllowed COAPCode = 133
  50. NotAcceptable COAPCode = 134
  51. RequestEntityIncomplete COAPCode = 136
  52. PreconditionFailed COAPCode = 140
  53. RequestEntityTooLarge COAPCode = 141
  54. UnsupportedMediaType COAPCode = 143
  55. InternalServerError COAPCode = 160
  56. NotImplemented COAPCode = 161
  57. BadGateway COAPCode = 162
  58. ServiceUnavailable COAPCode = 163
  59. GatewayTimeout COAPCode = 164
  60. ProxyingNotSupported COAPCode = 165
  61. )
  62. // MediaType 请求消息的媒体类型 对应Content-Format
  63. type MediaType uint16
  64. // Content formats.
  65. const (
  66. TextPlain MediaType = 0 // text/plain;charset=utf-8
  67. AppXML MediaType = 41 // application/xml
  68. AppOctets MediaType = 42 // application/octet-stream
  69. AppExi MediaType = 47 // application/exi
  70. AppJSON MediaType = 50 // application/json
  71. AppCBOR MediaType = 60 //application/cbor (RFC 7049)
  72. )
  73. func (c MediaType) String() string {
  74. switch c {
  75. case TextPlain:
  76. return "text/plain;charset=utf-8"
  77. case AppXML:
  78. return "application/xml"
  79. case AppOctets:
  80. return "application/octet-stream"
  81. case AppExi:
  82. return "application/exi"
  83. case AppJSON:
  84. return "application/json"
  85. case AppCBOR:
  86. return "application/cbor (RFC 7049)"
  87. }
  88. return "Unknown media type: 0x" + strconv.FormatInt(int64(c), 16)
  89. }
  90. // OptionID Option编号
  91. type OptionID uint8
  92. // Option IDs.
  93. const (
  94. IfMatch OptionID = 1
  95. URIHost OptionID = 3
  96. ETag OptionID = 4
  97. IfNoneMatch OptionID = 5
  98. Observe OptionID = 6
  99. URIPort OptionID = 7
  100. LocationPath OptionID = 8
  101. URIPath OptionID = 11
  102. ContentFormat OptionID = 12
  103. MaxAge OptionID = 14
  104. URIQuery OptionID = 15
  105. Accept OptionID = 17
  106. LocationQuery OptionID = 20
  107. Block2 OptionID = 23
  108. Block1 OptionID = 27
  109. Size2 OptionID = 28
  110. ProxyURI OptionID = 35
  111. ProxyScheme OptionID = 39
  112. Size1 OptionID = 60
  113. )
  114. // Option value format (RFC7252 section 3.2)
  115. type valueFormat uint8
  116. const (
  117. valueUnknown valueFormat = iota
  118. valueEmpty
  119. valueOpaque
  120. valueUint
  121. valueString
  122. )
  123. type optionDef struct {
  124. valueFormat valueFormat
  125. minLen int
  126. maxLen int
  127. }
  128. var coapOptionDefs = map[OptionID]optionDef{
  129. IfMatch: optionDef{valueFormat: valueOpaque, minLen: 0, maxLen: 8},
  130. URIHost: optionDef{valueFormat: valueString, minLen: 1, maxLen: 255},
  131. ETag: optionDef{valueFormat: valueOpaque, minLen: 1, maxLen: 8},
  132. IfNoneMatch: optionDef{valueFormat: valueEmpty, minLen: 0, maxLen: 0},
  133. Observe: optionDef{valueFormat: valueUint, minLen: 0, maxLen: 3},
  134. URIPort: optionDef{valueFormat: valueUint, minLen: 0, maxLen: 2},
  135. LocationPath: optionDef{valueFormat: valueString, minLen: 0, maxLen: 255},
  136. URIPath: optionDef{valueFormat: valueString, minLen: 0, maxLen: 255},
  137. ContentFormat: optionDef{valueFormat: valueUint, minLen: 0, maxLen: 2},
  138. MaxAge: optionDef{valueFormat: valueUint, minLen: 0, maxLen: 4},
  139. URIQuery: optionDef{valueFormat: valueString, minLen: 0, maxLen: 255},
  140. Accept: optionDef{valueFormat: valueUint, minLen: 0, maxLen: 2},
  141. LocationQuery: optionDef{valueFormat: valueString, minLen: 0, maxLen: 255},
  142. Block2: optionDef{valueFormat: valueUint, minLen: 0, maxLen: 3},
  143. Block1: optionDef{valueFormat: valueUint, minLen: 0, maxLen: 3},
  144. Size2: optionDef{valueFormat: valueUint, minLen: 0, maxLen: 4},
  145. ProxyURI: optionDef{valueFormat: valueString, minLen: 1, maxLen: 1034},
  146. ProxyScheme: optionDef{valueFormat: valueString, minLen: 1, maxLen: 255},
  147. Size1: optionDef{valueFormat: valueUint, minLen: 0, maxLen: 4},
  148. }
  149. type option struct {
  150. ID OptionID
  151. Value interface{}
  152. }
  153. func encodeInt(v uint32) []byte {
  154. switch {
  155. case v == 0:
  156. return nil
  157. case v < 256:
  158. return []byte{byte(v)}
  159. case v < 65536:
  160. rv := []byte{0, 0}
  161. binary.BigEndian.PutUint16(rv, uint16(v))
  162. return rv
  163. case v < 16777216:
  164. rv := []byte{0, 0, 0, 0}
  165. binary.BigEndian.PutUint32(rv, uint32(v))
  166. return rv[1:]
  167. default:
  168. rv := []byte{0, 0, 0, 0}
  169. binary.BigEndian.PutUint32(rv, uint32(v))
  170. return rv
  171. }
  172. }
  173. func decodeInt(b []byte) uint32 {
  174. tmp := []byte{0, 0, 0, 0}
  175. copy(tmp[4-len(b):], b)
  176. return binary.BigEndian.Uint32(tmp)
  177. }
  178. func (o option) toBytes() []byte {
  179. var v uint32
  180. switch i := o.Value.(type) {
  181. case string:
  182. return []byte(i)
  183. case []byte:
  184. return i
  185. case MediaType:
  186. v = uint32(i)
  187. case int:
  188. v = uint32(i)
  189. case int32:
  190. v = uint32(i)
  191. case uint:
  192. v = uint32(i)
  193. case uint32:
  194. v = i
  195. default:
  196. panic(fmt.Errorf("invalid type for option %x: %T (%v)",
  197. o.ID, o.Value, o.Value))
  198. }
  199. return encodeInt(v)
  200. }
  201. type options []option
  202. func (o options) Len() int {
  203. return len(o)
  204. }
  205. func (o options) Less(i, j int) bool {
  206. if o[i].ID == o[j].ID {
  207. return i < j
  208. }
  209. return o[i].ID < o[j].ID
  210. }
  211. func (o options) Swap(i, j int) {
  212. o[i], o[j] = o[j], o[i]
  213. }
  214. func (o options) Remove(oid OptionID) options {
  215. idx := 0
  216. for i := 0; i < len(o); i++ {
  217. if o[i].ID != oid {
  218. o[idx] = o[i]
  219. idx++
  220. }
  221. }
  222. return o[:idx]
  223. }
  224. const (
  225. extoptByteCode = 13
  226. extoptByteAddend = 13
  227. extoptWordCode = 14
  228. extoptWordAddend = 269
  229. extoptError = 15
  230. )
  231. // Message interface
  232. type Message interface {
  233. Encode() ([]byte, error)
  234. Decode(data []byte) error
  235. AllOptions() options
  236. Option(opid OptionID) interface{}
  237. Path() []string
  238. PathString() string
  239. SetPath([]string)
  240. SetPathString(s string)
  241. AddOption(opid OptionID, val interface{})
  242. RemoveOption(opid OptionID)
  243. IsConfirmable() bool
  244. OptionStrings(opid OptionID) []string
  245. GetMessageID() uint16
  246. GetToken() []byte
  247. GetCode() COAPCode
  248. GetPayload() []byte
  249. }
  250. // BaseMessage COAP 消息体
  251. type BaseMessage struct {
  252. Type COAPType
  253. Code COAPCode
  254. MessageID uint16
  255. Token []byte
  256. Payload []byte
  257. Opts options
  258. }
  259. func (m *BaseMessage) GetToken() []byte {
  260. return m.Token
  261. }
  262. func (m *BaseMessage) GetMessageID() uint16 {
  263. return m.MessageID
  264. }
  265. func (m *BaseMessage) GetCode() COAPCode {
  266. return m.Code
  267. }
  268. func (m *BaseMessage) Encode() ([]byte, error) {
  269. tmpbuf := []byte{0, 0}
  270. binary.BigEndian.PutUint16(tmpbuf, m.MessageID)
  271. buf := bytes.Buffer{}
  272. buf.Write([]byte{
  273. (1 << 6) | (uint8(m.Type) << 4) | uint8(0xf&len(m.Token)),
  274. byte(m.Code),
  275. tmpbuf[0], tmpbuf[1],
  276. })
  277. buf.Write(m.Token)
  278. extendOpt := func(opt int) (int, int) {
  279. ext := 0
  280. if opt >= extoptByteAddend {
  281. if opt >= extoptWordAddend {
  282. ext = opt - extoptWordAddend
  283. opt = extoptWordCode
  284. } else {
  285. ext = opt - extoptByteAddend
  286. opt = extoptByteCode
  287. }
  288. }
  289. return opt, ext
  290. }
  291. writeOptHeader := func(delta, length int) {
  292. d, dx := extendOpt(delta)
  293. l, lx := extendOpt(length)
  294. buf.WriteByte(byte(d<<4) | byte(l))
  295. tmp := []byte{0, 0}
  296. writeExt := func(opt, ext int) {
  297. switch opt {
  298. case extoptByteCode:
  299. buf.WriteByte(byte(ext))
  300. case extoptWordCode:
  301. binary.BigEndian.PutUint16(tmp, uint16(ext))
  302. buf.Write(tmp)
  303. }
  304. }
  305. writeExt(d, dx)
  306. writeExt(l, lx)
  307. }
  308. sort.Stable(&m.Opts)
  309. prev := 0
  310. for _, o := range m.Opts {
  311. b := o.toBytes()
  312. writeOptHeader(int(o.ID)-prev, len(b))
  313. buf.Write(b)
  314. prev = int(o.ID)
  315. }
  316. if len(m.Payload) > 0 {
  317. buf.Write([]byte{0xff})
  318. }
  319. buf.Write(m.Payload)
  320. return buf.Bytes(), nil
  321. }
  322. func (m *BaseMessage) Decode(data []byte) error {
  323. if len(data) < 4 {
  324. return errors.New("short packet")
  325. }
  326. if data[0]>>6 != 1 {
  327. return errors.New("invalid version")
  328. }
  329. m.Type = COAPType((data[0] >> 4) & 0x3)
  330. tokenLen := int(data[0] & 0xf)
  331. if tokenLen > 8 {
  332. return ErrInvalidTokenLen
  333. }
  334. m.Code = COAPCode(data[1])
  335. m.MessageID = binary.BigEndian.Uint16(data[2:4])
  336. if tokenLen > 0 {
  337. m.Token = make([]byte, tokenLen)
  338. }
  339. if len(data) < 4+tokenLen {
  340. return errors.New("truncated")
  341. }
  342. copy(m.Token, data[4:4+tokenLen])
  343. b := data[4+tokenLen:]
  344. prev := 0
  345. parseExtOpt := func(opt int) (int, error) {
  346. switch opt {
  347. case extoptByteCode:
  348. if len(b) < 1 {
  349. return -1, errors.New("truncated")
  350. }
  351. opt = int(b[0]) + extoptByteAddend
  352. b = b[1:]
  353. case extoptWordCode:
  354. if len(b) < 2 {
  355. return -1, errors.New("truncated")
  356. }
  357. opt = int(binary.BigEndian.Uint16(b[:2])) + extoptWordAddend
  358. b = b[2:]
  359. }
  360. return opt, nil
  361. }
  362. for len(b) > 0 {
  363. if b[0] == 0xff {
  364. b = b[1:]
  365. break
  366. }
  367. delta := int(b[0] >> 4)
  368. length := int(b[0] & 0x0f)
  369. if delta == extoptError || length == extoptError {
  370. return errors.New("unexpected extended option marker")
  371. }
  372. b = b[1:]
  373. delta, err := parseExtOpt(delta)
  374. if err != nil {
  375. return err
  376. }
  377. length, err = parseExtOpt(length)
  378. if err != nil {
  379. return err
  380. }
  381. if len(b) < length {
  382. return errors.New("truncated")
  383. }
  384. oid := OptionID(prev + delta)
  385. opval := parseOptionValue(oid, b[:length])
  386. b = b[length:]
  387. prev = int(oid)
  388. if opval != nil {
  389. m.Opts = append(m.Opts, option{ID: oid, Value: opval})
  390. }
  391. }
  392. m.Payload = b
  393. return nil
  394. }
  395. // AllOptions get message opts
  396. func (m *BaseMessage) AllOptions() options {
  397. return m.Opts
  398. }
  399. // IsConfirmable 如果是CON类型的消息类型,返回true
  400. func (m *BaseMessage) IsConfirmable() bool {
  401. return m.Type == CON
  402. }
  403. func (m *BaseMessage) GetPayload() []byte {
  404. return m.Payload
  405. }
  406. // Option get option by id
  407. func (m *BaseMessage) Option(o OptionID) interface{} {
  408. for _, v := range m.Opts {
  409. if o == v.ID {
  410. return v.Value
  411. }
  412. }
  413. return nil
  414. }
  415. // Options 获取所的option value
  416. func (m *BaseMessage) Options(o OptionID) []interface{} {
  417. var rv []interface{}
  418. for _, v := range m.Opts {
  419. if o == v.ID {
  420. rv = append(rv, v.Value)
  421. }
  422. }
  423. return rv
  424. }
  425. // OptionStrings get option strings by id
  426. func (m *BaseMessage) OptionStrings(o OptionID) []string {
  427. var rv []string
  428. for _, o := range m.Options(o) {
  429. rv = append(rv, o.(string))
  430. }
  431. return rv
  432. }
  433. // Path 获取URIPath
  434. func (m *BaseMessage) Path() []string {
  435. return m.OptionStrings(URIPath)
  436. }
  437. // PathString gets a path as a / separated string.
  438. func (m *BaseMessage) PathString() string {
  439. return strings.Join(m.Path(), "/")
  440. }
  441. // SetPathString sets a path by a / separated string.
  442. func (m *BaseMessage) SetPathString(s string) {
  443. switch s {
  444. case "", "/":
  445. //root path is not set as option
  446. return
  447. default:
  448. if s[0] == '/' {
  449. s = s[1:]
  450. }
  451. m.SetPath(strings.Split(s, "/"))
  452. }
  453. }
  454. //RemoveOption remove a given opid
  455. func (m *BaseMessage) RemoveOption(opID OptionID) {
  456. m.Opts = m.Opts.Remove(opID)
  457. }
  458. // AddOption ``
  459. func (m *BaseMessage) AddOption(opID OptionID, val interface{}) {
  460. iv := reflect.ValueOf(val)
  461. if (iv.Kind() == reflect.Slice || iv.Kind() == reflect.Array) &&
  462. iv.Type().Elem().Kind() == reflect.String {
  463. for i := 0; i < iv.Len(); i++ {
  464. m.Opts = append(m.Opts, option{opID, iv.Index(i).Interface()})
  465. }
  466. return
  467. }
  468. m.Opts = append(m.Opts, option{opID, val})
  469. }
  470. // SetPath ``
  471. func (m *BaseMessage) SetPath(s []string) {
  472. m.SetOption(URIPath, s)
  473. }
  474. // SetOption sets an option, discarding any previous value
  475. func (m *BaseMessage) SetOption(opID OptionID, val interface{}) {
  476. m.RemoveOption(opID)
  477. m.AddOption(opID, val)
  478. }
  479. func parseOptionValue(optionID OptionID, valueBuf []byte) interface{} {
  480. def := coapOptionDefs[optionID]
  481. if def.valueFormat == valueUnknown {
  482. // Skip unrecognized options (RFC7252 section 5.4.1)
  483. return nil
  484. }
  485. if len(valueBuf) < def.minLen || len(valueBuf) > def.maxLen {
  486. // Skip options with illegal value length (RFC7252 section 5.4.3)
  487. return nil
  488. }
  489. switch def.valueFormat {
  490. case valueUint:
  491. intValue := decodeInt(valueBuf)
  492. if optionID == ContentFormat || optionID == Accept {
  493. return MediaType(intValue)
  494. } else {
  495. return intValue
  496. }
  497. case valueString:
  498. return string(valueBuf)
  499. case valueOpaque, valueEmpty:
  500. return valueBuf
  501. }
  502. // Skip unrecognized options (should never be reached)
  503. return nil
  504. }
  505. func ParseMessage(data []byte) (Message, error) {
  506. rv := &BaseMessage{}
  507. return rv, rv.Decode(data)
  508. }