message.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. }
  249. // BaseMessage COAP 消息体
  250. type BaseMessage struct {
  251. Type COAPType
  252. Code COAPCode
  253. MessageID uint16
  254. Token []byte
  255. Payload []byte
  256. Opts options
  257. }
  258. func (m *BaseMessage) GetToken() []byte {
  259. return m.Token
  260. }
  261. func (m *BaseMessage) GetMessageID() uint16 {
  262. return m.MessageID
  263. }
  264. func (m *BaseMessage) GetCode() COAPCode {
  265. return m.Code
  266. }
  267. func (m *BaseMessage) Encode() ([]byte, error) {
  268. tmpbuf := []byte{0, 0}
  269. binary.BigEndian.PutUint16(tmpbuf, m.MessageID)
  270. buf := bytes.Buffer{}
  271. buf.Write([]byte{
  272. (1 << 6) | (uint8(m.Type) << 4) | uint8(0xf&len(m.Token)),
  273. byte(m.Code),
  274. tmpbuf[0], tmpbuf[1],
  275. })
  276. buf.Write(m.Token)
  277. extendOpt := func(opt int) (int, int) {
  278. ext := 0
  279. if opt >= extoptByteAddend {
  280. if opt >= extoptWordAddend {
  281. ext = opt - extoptWordAddend
  282. opt = extoptWordCode
  283. } else {
  284. ext = opt - extoptByteAddend
  285. opt = extoptByteCode
  286. }
  287. }
  288. return opt, ext
  289. }
  290. writeOptHeader := func(delta, length int) {
  291. d, dx := extendOpt(delta)
  292. l, lx := extendOpt(length)
  293. buf.WriteByte(byte(d<<4) | byte(l))
  294. tmp := []byte{0, 0}
  295. writeExt := func(opt, ext int) {
  296. switch opt {
  297. case extoptByteCode:
  298. buf.WriteByte(byte(ext))
  299. case extoptWordCode:
  300. binary.BigEndian.PutUint16(tmp, uint16(ext))
  301. buf.Write(tmp)
  302. }
  303. }
  304. writeExt(d, dx)
  305. writeExt(l, lx)
  306. }
  307. sort.Stable(&m.Opts)
  308. prev := 0
  309. for _, o := range m.Opts {
  310. b := o.toBytes()
  311. writeOptHeader(int(o.ID)-prev, len(b))
  312. buf.Write(b)
  313. prev = int(o.ID)
  314. }
  315. if len(m.Payload) > 0 {
  316. buf.Write([]byte{0xff})
  317. }
  318. buf.Write(m.Payload)
  319. return buf.Bytes(), nil
  320. }
  321. func (m *BaseMessage) Decode(data []byte) error {
  322. if len(data) < 4 {
  323. return errors.New("short packet")
  324. }
  325. if data[0]>>6 != 1 {
  326. return errors.New("invalid version")
  327. }
  328. m.Type = COAPType((data[0] >> 4) & 0x3)
  329. tokenLen := int(data[0] & 0xf)
  330. if tokenLen > 8 {
  331. return ErrInvalidTokenLen
  332. }
  333. m.Code = COAPCode(data[1])
  334. m.MessageID = binary.BigEndian.Uint16(data[2:4])
  335. if tokenLen > 0 {
  336. m.Token = make([]byte, tokenLen)
  337. }
  338. if len(data) < 4+tokenLen {
  339. return errors.New("truncated")
  340. }
  341. copy(m.Token, data[4:4+tokenLen])
  342. b := data[4+tokenLen:]
  343. prev := 0
  344. parseExtOpt := func(opt int) (int, error) {
  345. switch opt {
  346. case extoptByteCode:
  347. if len(b) < 1 {
  348. return -1, errors.New("truncated")
  349. }
  350. opt = int(b[0]) + extoptByteAddend
  351. b = b[1:]
  352. case extoptWordCode:
  353. if len(b) < 2 {
  354. return -1, errors.New("truncated")
  355. }
  356. opt = int(binary.BigEndian.Uint16(b[:2])) + extoptWordAddend
  357. b = b[2:]
  358. }
  359. return opt, nil
  360. }
  361. for len(b) > 0 {
  362. if b[0] == 0xff {
  363. b = b[1:]
  364. break
  365. }
  366. delta := int(b[0] >> 4)
  367. length := int(b[0] & 0x0f)
  368. if delta == extoptError || length == extoptError {
  369. return errors.New("unexpected extended option marker")
  370. }
  371. b = b[1:]
  372. delta, err := parseExtOpt(delta)
  373. if err != nil {
  374. return err
  375. }
  376. length, err = parseExtOpt(length)
  377. if err != nil {
  378. return err
  379. }
  380. if len(b) < length {
  381. return errors.New("truncated")
  382. }
  383. oid := OptionID(prev + delta)
  384. opval := parseOptionValue(oid, b[:length])
  385. b = b[length:]
  386. prev = int(oid)
  387. if opval != nil {
  388. m.Opts = append(m.Opts, option{ID: oid, Value: opval})
  389. }
  390. }
  391. m.Payload = b
  392. return nil
  393. }
  394. // AllOptions get message opts
  395. func (m *BaseMessage) AllOptions() options {
  396. return m.Opts
  397. }
  398. // IsConfirmable 如果是CON类型的消息类型,返回true
  399. func (m *BaseMessage) IsConfirmable() bool {
  400. return m.Type == CON
  401. }
  402. // Option get option by id
  403. func (m *BaseMessage) Option(o OptionID) interface{} {
  404. for _, v := range m.Opts {
  405. if o == v.ID {
  406. return v.Value
  407. }
  408. }
  409. return nil
  410. }
  411. // Options 获取所的option value
  412. func (m *BaseMessage) Options(o OptionID) []interface{} {
  413. var rv []interface{}
  414. for _, v := range m.Opts {
  415. if o == v.ID {
  416. rv = append(rv, v.Value)
  417. }
  418. }
  419. return rv
  420. }
  421. // OptionStrings get option strings by id
  422. func (m *BaseMessage) OptionStrings(o OptionID) []string {
  423. var rv []string
  424. for _, o := range m.Options(o) {
  425. rv = append(rv, o.(string))
  426. }
  427. return rv
  428. }
  429. // Path 获取URIPath
  430. func (m *BaseMessage) Path() []string {
  431. return m.OptionStrings(URIPath)
  432. }
  433. // PathString gets a path as a / separated string.
  434. func (m *BaseMessage) PathString() string {
  435. return strings.Join(m.Path(), "/")
  436. }
  437. // SetPathString sets a path by a / separated string.
  438. func (m *BaseMessage) SetPathString(s string) {
  439. switch s {
  440. case "", "/":
  441. //root path is not set as option
  442. return
  443. default:
  444. if s[0] == '/' {
  445. s = s[1:]
  446. }
  447. m.SetPath(strings.Split(s, "/"))
  448. }
  449. }
  450. //RemoveOption remove a given opid
  451. func (m *BaseMessage) RemoveOption(opID OptionID) {
  452. m.Opts = m.Opts.Remove(opID)
  453. }
  454. // AddOption ``
  455. func (m *BaseMessage) AddOption(opID OptionID, val interface{}) {
  456. iv := reflect.ValueOf(val)
  457. if (iv.Kind() == reflect.Slice || iv.Kind() == reflect.Array) &&
  458. iv.Type().Elem().Kind() == reflect.String {
  459. for i := 0; i < iv.Len(); i++ {
  460. m.Opts = append(m.Opts, option{opID, iv.Index(i).Interface()})
  461. }
  462. return
  463. }
  464. m.Opts = append(m.Opts, option{opID, val})
  465. }
  466. // SetPath ``
  467. func (m *BaseMessage) SetPath(s []string) {
  468. m.SetOption(URIPath, s)
  469. }
  470. // SetOption sets an option, discarding any previous value
  471. func (m *BaseMessage) SetOption(opID OptionID, val interface{}) {
  472. m.RemoveOption(opID)
  473. m.AddOption(opID, val)
  474. }
  475. func parseOptionValue(optionID OptionID, valueBuf []byte) interface{} {
  476. def := coapOptionDefs[optionID]
  477. if def.valueFormat == valueUnknown {
  478. // Skip unrecognized options (RFC7252 section 5.4.1)
  479. return nil
  480. }
  481. if len(valueBuf) < def.minLen || len(valueBuf) > def.maxLen {
  482. // Skip options with illegal value length (RFC7252 section 5.4.3)
  483. return nil
  484. }
  485. switch def.valueFormat {
  486. case valueUint:
  487. intValue := decodeInt(valueBuf)
  488. if optionID == ContentFormat || optionID == Accept {
  489. return MediaType(intValue)
  490. } else {
  491. return intValue
  492. }
  493. case valueString:
  494. return string(valueBuf)
  495. case valueOpaque, valueEmpty:
  496. return valueBuf
  497. }
  498. // Skip unrecognized options (should never be reached)
  499. return nil
  500. }
  501. func ParseMessage(data []byte) (Message, error) {
  502. rv := &BaseMessage{}
  503. return rv, rv.Decode(data)
  504. }