2 * Routines for CoAP packet disassembly
3 * draft-ietf-core-coap-14.txt
4 * draft-ietf-core-block-10.txt
5 * draft-ietf-core-observe-07.txt
6 * draft-ietf-core-link-format-06.txt
7 * Shoichi Sakane <sakane@tanu.org>
9 * Changes for draft-ietf-core-coap-17.txt
10 * Hauke Mehrtens <hauke@hauke-m.de>
14 * Wireshark - Network traffic analyzer
15 * By Gerald Combs <gerald@wireshark.org>
16 * Copyright 1998 Gerald Combs
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version 2
21 * of the License, or (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
37 #include <epan/packet.h>
38 #include <epan/prefs.h>
41 void proto_register_coap(void);
43 static dissector_table_t media_type_dissector_table;
45 static int proto_coap = -1;
47 static int hf_coap_version = -1;
48 static int hf_coap_ttype = -1;
49 static int hf_coap_token_len = -1;
50 static int hf_coap_token = -1;
51 static int hf_coap_code = -1;
52 static int hf_coap_mid = -1;
53 static int hf_coap_payload_desc = -1;
54 static int hf_coap_opt_name = -1;
55 static int hf_coap_opt_desc = -1;
56 static int hf_coap_opt_delta = -1;
57 static int hf_coap_opt_delta_ext = -1;
58 static int hf_coap_opt_length = -1;
59 static int hf_coap_opt_length_ext = -1;
60 static int hf_coap_opt_end_marker = -1;
61 static int hf_coap_opt_ctype = -1;
62 static int hf_coap_opt_max_age = -1;
63 static int hf_coap_opt_proxy_uri = -1;
64 static int hf_coap_opt_etag = -1;
65 static int hf_coap_opt_uri_host = -1;
66 static int hf_coap_opt_location_path = -1;
67 static int hf_coap_opt_uri_port = -1;
68 static int hf_coap_opt_location_query = -1;
69 static int hf_coap_opt_uri_path = -1;
70 static int hf_coap_opt_observe = -1;
71 static int hf_coap_opt_accept = -1;
72 static int hf_coap_opt_if_match = -1;
73 static int hf_coap_opt_block_number = -1;
74 static int hf_coap_opt_block_mflag = -1;
75 static int hf_coap_opt_block_size = -1;
76 static int hf_coap_opt_uri_query = -1;
77 static int hf_coap_opt_unknown = -1;
79 static gint ett_coap = -1;
80 static gint ett_coap_option = -1;
81 static gint ett_coap_payload = -1;
83 /* CoAP's IANA-assigned port number */
84 #define DEFAULT_COAP_PORT 5683
86 /* indicators whether those are to be showed or not */
87 #define DEFAULT_COAP_CTYPE_VALUE ~0
88 #define DEFAULT_COAP_BLOCK_NUMBER ~0
90 static const gchar *coap_ctype_str = NULL;
91 static gint coap_ctype_value = DEFAULT_COAP_CTYPE_VALUE;
92 static guint global_coap_port_number = DEFAULT_COAP_PORT;
93 static gint coap_block_number = DEFAULT_COAP_BLOCK_NUMBER;
94 static guint coap_block_mflag = 0;
95 static gchar coap_uri_str[1024]; /* the maximum is 1024 > 510 = Uri-Host:255 + Uri-Path:255 x 2 */
96 static gchar coap_uri_query[1024]; /* the maximum is 1024 > 765 = Uri-Query:255 x 3 */
97 static gchar coap_token_str[128];
102 static const value_string vals_ttype[] = {
103 { 0, "Confirmable" },
104 { 1, "Non-Confirmable" },
105 { 2, "Acknowledgement" },
109 static const value_string vals_ttype_short[] = {
121 static const value_string vals_code[] = {
122 { 0, "Empty Message" },
131 { 65, "2.01 Created" },
132 { 66, "2.02 Deleted" },
133 { 67, "2.03 Valid" },
134 { 68, "2.04 Changed" },
135 { 69, "2.05 Content" },
136 { 128, "4.00 Bad Request" },
137 { 129, "4.01 Unauthorized" },
138 { 130, "4.02 Bad Option" },
139 { 131, "4.03 Forbidden" },
140 { 132, "4.04 Not Found" },
141 { 133, "4.05 Method Not Allowed" },
142 { 134, "4.06 Not Acceptable" },
143 { 136, "4.08 Request Entity Incomplete" }, /* core-block-10 */
144 { 140, "4.12 Precondition Failed" },
145 { 141, "4.13 Request Entity Too Large" },
146 { 143, "4.15 Unsupported Content-Format" },
147 { 160, "5.00 Internal Server Error" },
148 { 161, "5.01 Not Implemented" },
149 { 162, "5.02 Bad Gateway" },
150 { 163, "5.03 Service Unavailable" },
151 { 164, "5.04 Gateway Timeout" },
152 { 165, "5.05 Proxying Not Supported" },
159 * No-Option must not be included in this structure, is handled in the function
160 * of the dissector, especially.
162 #define COAP_OPT_IF_MATCH 1
163 #define COAP_OPT_URI_HOST 3
164 #define COAP_OPT_ETAG 4
165 #define COAP_OPT_IF_NONE_MATCH 5
166 #define COAP_OPT_OBSERVE 6 /* core-observe-07 */
167 #define COAP_OPT_URI_PORT 7
168 #define COAP_OPT_LOCATION_PATH 8
169 #define COAP_OPT_URI_PATH 11
170 #define COAP_OPT_CONTENT_TYPE 12
171 #define COAP_OPT_MAX_AGE 14
172 #define COAP_OPT_URI_QUERY 15
173 #define COAP_OPT_ACCEPT 16
174 #define COAP_OPT_LOCATION_QUERY 20
175 #define COAP_OPT_BLOCK2 23 /* core-block-10 */
176 #define COAP_OPT_BLOCK_SIZE 28 /* core-block-10 */
177 #define COAP_OPT_BLOCK1 27 /* core-block-10 */
178 #define COAP_OPT_PROXY_URI 35
180 static const value_string vals_opt_type[] = {
181 { COAP_OPT_IF_MATCH, "If-Match" },
182 { COAP_OPT_URI_HOST, "Uri-Host" },
183 { COAP_OPT_ETAG, "Etag" },
184 { COAP_OPT_IF_NONE_MATCH, "If-None-Match" },
185 { COAP_OPT_URI_PORT, "Uri-Port" },
186 { COAP_OPT_LOCATION_PATH, "Location-Path" },
187 { COAP_OPT_URI_PATH, "Uri-Path" },
188 { COAP_OPT_CONTENT_TYPE, "Content-Format" },
189 { COAP_OPT_MAX_AGE, "Max-age" },
190 { COAP_OPT_URI_QUERY, "Uri-Query" },
191 { COAP_OPT_ACCEPT, "Accept" },
192 { COAP_OPT_LOCATION_QUERY, "Location-Query" },
193 { COAP_OPT_PROXY_URI, "Proxy-Uri" },
194 { COAP_OPT_OBSERVE, "Observe" },
195 { COAP_OPT_BLOCK2, "Block2" },
196 { COAP_OPT_BLOCK1, "Block1" },
197 { COAP_OPT_BLOCK_SIZE, "Block Size" },
201 struct coap_option_range_t {
206 { COAP_OPT_IF_MATCH, 0, 8 },
207 { COAP_OPT_URI_HOST, 1, 255 },
208 { COAP_OPT_ETAG, 1, 8 },
209 { COAP_OPT_IF_NONE_MATCH, 0, 0 },
210 { COAP_OPT_URI_PORT, 0, 2 },
211 { COAP_OPT_LOCATION_PATH, 0, 255 },
212 { COAP_OPT_URI_PATH, 0, 255 },
213 { COAP_OPT_CONTENT_TYPE, 0, 2 },
214 { COAP_OPT_MAX_AGE, 0, 4 },
215 { COAP_OPT_URI_QUERY, 1, 255 },
216 { COAP_OPT_ACCEPT, 0, 2 },
217 { COAP_OPT_LOCATION_QUERY, 0, 255 },
218 { COAP_OPT_PROXY_URI, 1,1034 },
219 { COAP_OPT_OBSERVE, 0, 2 },
220 { COAP_OPT_BLOCK2, 0, 3 },
221 { COAP_OPT_BLOCK1, 0, 3 },
222 { COAP_OPT_BLOCK_SIZE, 0, 4 },
226 static const value_string vals_ctype[] = {
227 { 0, "text/plain; charset=utf-8" },
228 { 40, "application/link-format" },
229 { 41, "application/xml" },
230 { 42, "application/octet-stream" },
231 { 47, "application/exi" },
232 { 50, "application/json" },
236 static const char *nullstr = "(null)";
238 void proto_reg_handoff_coap(void);
241 coap_is_str_ipv6addr(guint8 *str)
243 size_t len = strlen(str);
251 return colon > 1 ? 1 : 0;
255 coap_get_opt_uint(tvbuff_t *tvb, gint offset, gint length)
261 return (guint)tvb_get_guint8(tvb, offset);
263 return (guint)tvb_get_ntohs(tvb, offset);
265 return (guint)tvb_get_ntoh24(tvb, offset);
267 return (guint)tvb_get_ntohl(tvb, offset);
274 coap_opt_check(packet_info *pinfo, proto_tree *subtree, guint opt_num, gint opt_length)
278 for (i = 0; i < (int)(array_length(coi)); i++) {
279 if (coi[i].type == opt_num)
282 if (i == (int)(array_length(coi))) {
283 expert_add_info_format(pinfo, subtree, PI_MALFORMED, PI_WARN,
284 "Invalid Option Number %d", opt_num);
287 if (opt_length < coi[i].min || opt_length > coi[i].max) {
288 expert_add_info_format(pinfo, subtree, PI_MALFORMED,
289 PI_WARN, "Invalid Option Range: %d (%d < x < %d)",
290 opt_length, coi[i].min, coi[i].max);
297 dissect_coap_opt_hex_string(tvbuff_t *tvb, proto_item *item, proto_tree *subtree, gint offset, gint opt_length, int hf)
304 str = tvb_bytes_to_str_punct(tvb, offset, opt_length, ' ');
306 proto_tree_add_string(subtree, hf, tvb, offset, opt_length, str);
308 /* add info to the head of the packet detail */
309 proto_item_append_text(item, ": %s", str);
313 dissect_coap_opt_uint(tvbuff_t *tvb, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length, int hf)
317 if (opt_length != 0) {
318 i = coap_get_opt_uint(tvb, offset, opt_length);
321 proto_tree_add_uint(subtree, hf, tvb, offset, opt_length, i);
323 /* add info to the head of the packet detail */
324 proto_item_append_text(head_item, ": %d", i);
328 dissect_coap_opt_uri_host(tvbuff_t *tvb, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length)
332 str = tvb_get_ephemeral_string(tvb, offset, opt_length);
334 proto_tree_add_string(subtree, hf_coap_opt_uri_host, tvb, offset, opt_length, str);
336 /* add info to the head of the packet detail */
337 proto_item_append_text(head_item, ": %s", str);
339 /* forming a uri-string */
340 g_strlcat(coap_uri_str, "coap://", sizeof(coap_uri_str));
342 * if the string looks an IPv6 address, assuming that it has
343 * to be enclosed by brackets.
345 if (coap_is_str_ipv6addr(str)) {
346 g_strlcat(coap_uri_str, "[", sizeof(coap_uri_str));
347 g_strlcat(coap_uri_str, str, sizeof(coap_uri_str));
348 g_strlcat(coap_uri_str, "]", sizeof(coap_uri_str));
350 g_strlcat(coap_uri_str, str, sizeof(coap_uri_str));
354 dissect_coap_opt_uri_path(tvbuff_t *tvb, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length)
356 const guint8 *str = NULL;
358 g_strlcat(coap_uri_str, "/", sizeof(coap_uri_str));
360 if (opt_length == 0) {
363 str = tvb_get_ephemeral_string(tvb, offset, opt_length);
364 g_strlcat(coap_uri_str, str, sizeof(coap_uri_str));
367 proto_tree_add_string(subtree, hf_coap_opt_uri_path, tvb, offset, opt_length, str);
369 /* add info to the head of the packet detail */
370 proto_item_append_text(head_item, ": %s", str);
374 dissect_coap_opt_uri_query(tvbuff_t *tvb, proto_item *head_item,proto_tree *subtree, gint offset, gint opt_length)
376 const guint8 *str = NULL;
378 if (coap_uri_query[0] == '\0')
379 g_strlcat(coap_uri_query, "?", sizeof(coap_uri_str));
381 g_strlcat(coap_uri_query, "&", sizeof(coap_uri_str));
383 if (opt_length == 0) {
386 str = tvb_get_ephemeral_string(tvb, offset, opt_length);
387 g_strlcat(coap_uri_str, str, sizeof(coap_uri_str));
390 proto_tree_add_string(subtree, hf_coap_opt_uri_query, tvb, offset, opt_length, str);
392 /* add info to the head of the packet detail */
393 proto_item_append_text(head_item, ": %s", str);
397 dissect_coap_opt_location_path(tvbuff_t *tvb, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length)
399 const guint8 *str = NULL;
401 if (opt_length == 0) {
404 str = tvb_get_ephemeral_string(tvb, offset, opt_length);
407 proto_tree_add_string(subtree, hf_coap_opt_location_path, tvb, offset, opt_length, str);
409 /* add info to the head of the packet detail */
410 proto_item_append_text(head_item, ": %s", str);
414 dissect_coap_opt_location_query(tvbuff_t *tvb, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length)
416 const guint8 *str = NULL;
418 if (opt_length == 0) {
421 str = tvb_get_ephemeral_string(tvb, offset, opt_length);
424 proto_tree_add_string(subtree, hf_coap_opt_location_query, tvb, offset, opt_length, str);
426 /* add info to the head of the packet detail */
427 proto_item_append_text(head_item, ": %s", str);
431 dissect_coap_opt_proxy_uri(tvbuff_t *tvb, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length)
433 const guint8 *str = NULL;
435 if (opt_length == 0) {
438 str = tvb_get_ephemeral_string(tvb, offset, opt_length);
441 proto_tree_add_string(subtree, hf_coap_opt_proxy_uri, tvb, offset, opt_length, str);
443 /* add info to the head of the packet detail */
444 proto_item_append_text(head_item, ": %s", str);
448 dissect_coap_opt_ctype(tvbuff_t *tvb, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length, int hf)
450 if (opt_length == 0) {
451 coap_ctype_value = 0;
453 coap_ctype_value = coap_get_opt_uint(tvb, offset, opt_length);
456 coap_ctype_str = val_to_str(coap_ctype_value, vals_ctype, "Unknown Type %d");
458 proto_tree_add_string(subtree, hf, tvb, offset, opt_length, coap_ctype_str);
460 /* add info to the head of the packet detail */
461 proto_item_append_text(head_item, ": %s", coap_ctype_str);
465 dissect_coap_opt_block(tvbuff_t *tvb, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length)
468 guint encoded_block_size = 0;
471 if (opt_length == 0) {
472 coap_block_number = 0;
475 coap_block_number = coap_get_opt_uint(tvb, offset, opt_length) >> 4;
476 val = tvb_get_guint8(tvb, offset + opt_length - 1) & 0x0f;
479 proto_tree_add_uint(subtree, hf_coap_opt_block_number,
480 tvb, offset, opt_length, coap_block_number);
482 /* More flag in the end of the option */
483 coap_block_mflag = val & 0x08;
484 proto_tree_add_uint(subtree, hf_coap_opt_block_mflag,
485 tvb, offset + opt_length - 1, 1, coap_block_mflag);
488 encoded_block_size = val & 0x07;
489 block_esize = 1 << (encoded_block_size + 4);
490 proto_tree_add_uint_format(subtree, hf_coap_opt_block_size,
491 tvb, offset + opt_length - 1, 1, encoded_block_size, "Block Size: %d (%d encoded)", block_esize, encoded_block_size);
493 /* add info to the head of the packet detail */
494 proto_item_append_text(head_item, ": NUM:%d, M:%d, SZX:%d",
495 coap_block_number, coap_block_mflag, block_esize);
499 dissect_coap_opt_uri_port(tvbuff_t *tvb, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length)
504 memset(portstr, '\0', sizeof(portstr));
506 if (opt_length != 0) {
507 port = coap_get_opt_uint(tvb, offset, opt_length);
510 proto_tree_add_uint(subtree, hf_coap_opt_uri_port, tvb, offset, opt_length, port);
512 proto_item_append_text(head_item, ": %d", port);
514 /* forming a uri-string */
515 g_snprintf(portstr, sizeof(portstr), "%u", port);
516 g_strlcat(coap_uri_str, ":", sizeof(coap_uri_str));
517 g_strlcat(coap_uri_str, portstr, sizeof(coap_uri_str));
521 * dissector for each option of CoAP.
522 * return the total length of the option including the header (e.g. delta and length).
525 dissect_coap_options_main(tvbuff_t *tvb, packet_info *pinfo, proto_tree *coap_tree, gint offset, guint8 opt_count, guint *opt_num, gint coap_length)
528 gint opt_length, opt_length_ext, opt_delta, opt_delta_ext;
529 gint opt_length_ext_off = 0;
530 gint8 opt_length_ext_len = 0;
531 gint opt_delta_ext_off = 0;
532 gint8 opt_delta_ext_len = 0;
533 gint orig_offset = offset;
534 proto_tree *subtree = NULL;
535 proto_item *item = NULL;
538 opt_jump = tvb_get_guint8(tvb, offset);
542 * seciton 3.1 in coap-17:
543 * Option Delta: 4-bit unsigned integer. A value between 0 and 12
544 * indicates the Option Delta. Three values are reserved for special
547 * 13: An 8-bit unsigned integer follows the initial byte and
548 * indicates the Option Delta minus 13.
550 * 14: A 16-bit unsigned integer in network byte order follows the
551 * initial byte and indicates the Option Delta minus 269.
553 * 15: Reserved for the Payload Marker. If the field is set to this
554 * value but the entire byte is not the payload marker, this MUST
555 * be processed as a message format error.
557 switch (opt_jump & 0xf0) {
559 opt_delta_ext = tvb_get_guint8(tvb, offset);
560 opt_delta_ext_off = offset;
561 opt_delta_ext_len = 1;
565 opt_delta += opt_delta_ext;
568 opt_delta_ext = coap_get_opt_uint(tvb, offset, 2);
569 opt_delta_ext_off = offset;
570 opt_delta_ext_len = 2;
574 opt_delta += opt_delta_ext;
577 expert_add_info_format(pinfo, coap_tree,
578 PI_MALFORMED, PI_WARN,
579 "end-of-options marker found, but option length isn't 15");
582 opt_delta = ((opt_jump & 0xf0) >> 4);
585 *opt_num += opt_delta;
588 * seciton 3.1 in coap-17:
589 * Option Length: 4-bit unsigned integer. A value between 0 and 12
590 * indicates the length of the Option Value, in bytes. Three values
591 * are reserved for special constructs:
593 * 13: An 8-bit unsigned integer precedes the Option Value and
594 * indicates the Option Length minus 13.
596 * 14: A 16-bit unsigned integer in network byte order precedes the
597 * Option Value and indicates the Option Length minus 269.
599 * 15: Reserved for future use. If the field is set to this value,
600 * it MUST be processed as a message format error.
602 switch (opt_jump & 0x0f) {
604 opt_length_ext = tvb_get_guint8(tvb, offset);
605 opt_length_ext_off = offset;
606 opt_length_ext_len = 1;
610 opt_length += opt_length_ext;
613 opt_length_ext = coap_get_opt_uint(tvb, offset, 2);
614 opt_length_ext_off = offset;
615 opt_length_ext_len = 2;
619 opt_length += opt_length_ext;
622 expert_add_info_format(pinfo, coap_tree,
623 PI_MALFORMED, PI_WARN,
624 "end-of-options marker found, but option delta isn't 15");
627 opt_length = (opt_jump & 0x0f);
630 if (offset + opt_length > coap_length) {
631 expert_add_info_format(pinfo, coap_tree,
632 PI_MALFORMED, PI_WARN,
633 "option longer than the package");
637 coap_opt_check(pinfo, coap_tree, *opt_num, opt_length);
639 g_snprintf(strbuf, sizeof(strbuf),
640 "#%u: %s", opt_count, val_to_str_const(*opt_num, vals_opt_type,
641 *opt_num % 14 == 0 ? "No-Op" : "Unknown Option"));
642 item = proto_tree_add_string(coap_tree, hf_coap_opt_name,
643 tvb, orig_offset, offset - orig_offset + opt_length, strbuf);
644 subtree = proto_item_add_subtree(item, ett_coap_option);
646 g_snprintf(strbuf, sizeof(strbuf),
647 "Type %u, %s, %s%s", *opt_num,
648 (*opt_num & 1) ? "Critical" : "Elective",
649 (*opt_num & 2) ? "Unsafe" : "Safe",
650 ((*opt_num & 0x1e) == 0x1c) ? ", NoCacheKey" : "");
651 proto_tree_add_string(subtree, hf_coap_opt_desc,
652 tvb, orig_offset, offset - orig_offset + opt_length, strbuf);
654 proto_tree_add_item(subtree, hf_coap_opt_delta, tvb, orig_offset, 1, ENC_BIG_ENDIAN);
655 proto_tree_add_item(subtree, hf_coap_opt_length, tvb, orig_offset, 1, ENC_BIG_ENDIAN);
657 if (opt_delta_ext_off && opt_delta_ext_len)
658 proto_tree_add_item(subtree, hf_coap_opt_delta_ext, tvb, opt_delta_ext_off, opt_delta_ext_len, ENC_BIG_ENDIAN);
660 if (opt_length_ext_off && opt_length_ext_len)
661 proto_tree_add_item(subtree, hf_coap_opt_length_ext, tvb, opt_length_ext_off, opt_length_ext_len, ENC_BIG_ENDIAN);
663 /* offset points the next to its option header */
665 case COAP_OPT_CONTENT_TYPE:
666 dissect_coap_opt_ctype(tvb, item, subtree, offset,
667 opt_length, hf_coap_opt_ctype);
669 case COAP_OPT_MAX_AGE:
670 dissect_coap_opt_uint(tvb, item, subtree, offset,
671 opt_length, hf_coap_opt_max_age);
673 case COAP_OPT_PROXY_URI:
674 dissect_coap_opt_proxy_uri(tvb, item, subtree, offset,
678 dissect_coap_opt_hex_string(tvb, item, subtree, offset,
679 opt_length, hf_coap_opt_etag);
681 case COAP_OPT_URI_HOST:
682 dissect_coap_opt_uri_host(tvb, item, subtree, offset,
685 case COAP_OPT_LOCATION_PATH:
686 dissect_coap_opt_location_path(tvb, item, subtree, offset,
689 case COAP_OPT_URI_PORT:
690 dissect_coap_opt_uri_port(tvb, item, subtree, offset,
693 case COAP_OPT_LOCATION_QUERY:
694 dissect_coap_opt_location_query(tvb, item, subtree, offset,
697 case COAP_OPT_URI_PATH:
698 dissect_coap_opt_uri_path(tvb, item, subtree, offset,
701 case COAP_OPT_OBSERVE:
702 dissect_coap_opt_uint(tvb, item, subtree, offset,
703 opt_length, hf_coap_opt_observe);
705 case COAP_OPT_ACCEPT:
706 dissect_coap_opt_ctype(tvb, item, subtree, offset,
707 opt_length, hf_coap_opt_accept);
709 case COAP_OPT_IF_MATCH:
710 dissect_coap_opt_hex_string(tvb, item, subtree, offset,
711 opt_length, hf_coap_opt_if_match);
713 case COAP_OPT_URI_QUERY:
714 dissect_coap_opt_uri_query(tvb, item, subtree, offset,
717 case COAP_OPT_BLOCK2:
718 dissect_coap_opt_block(tvb, item, subtree, offset,
721 case COAP_OPT_BLOCK1:
722 dissect_coap_opt_block(tvb, item, subtree, offset,
725 case COAP_OPT_IF_NONE_MATCH:
727 case COAP_OPT_BLOCK_SIZE:
728 dissect_coap_opt_uint(tvb, item, subtree, offset,
729 opt_length, hf_coap_opt_block_size);
732 dissect_coap_opt_hex_string(tvb, item, subtree, offset,
733 opt_length, hf_coap_opt_unknown);
737 return offset + opt_length;
742 * return offset pointing the next of options. (i.e. the top of the paylaod
743 * or the end of the data.
746 dissect_coap_options(tvbuff_t *tvb, packet_info *pinfo, proto_tree *coap_tree, gint offset, gint coap_length)
752 /* loop for dissecting options */
753 for (i = 1; offset < coap_length; i++) {
754 offset = dissect_coap_options_main(tvb, pinfo, coap_tree,
755 offset, i, &opt_num, coap_length);
758 if (offset >= coap_length)
760 endmarker = tvb_get_guint8(tvb, offset);
761 if (endmarker == 0xff) {
762 proto_tree_add_item(coap_tree, hf_coap_opt_end_marker, tvb, offset, 1, ENC_BIG_ENDIAN);
772 dissect_coap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
775 proto_item *coap_root = NULL;
776 proto_tree *coap_tree = NULL;
778 guint8 token_len = 0;
781 gint coap_length = 0;
783 /* initialize the CoAP length and the content-Format */
785 * the length of CoAP message is not specified in the CoAP header.
786 * It has to be from the lower layer. the iplen of packet_info is not accurate.
787 * Currently, the length is just copied from the reported length of the tvbuffer.
789 coap_length = tvb_reported_length(tvb);
791 coap_ctype_value = DEFAULT_COAP_CTYPE_VALUE;
793 coap_root = proto_tree_add_item(parent_tree, proto_coap, tvb, offset, -1, ENC_NA);
794 coap_tree = proto_item_add_subtree(coap_root, ett_coap);
796 proto_tree_add_item(coap_tree, hf_coap_version, tvb, offset, 1, ENC_BIG_ENDIAN);
798 proto_tree_add_item(coap_tree, hf_coap_ttype, tvb, offset, 1, ENC_BIG_ENDIAN);
799 ttype = (tvb_get_guint8(tvb, offset) & 0x30) >> 4;
801 proto_tree_add_item(coap_tree, hf_coap_token_len, tvb, offset, 1, ENC_BIG_ENDIAN);
802 token_len = tvb_get_guint8(tvb, offset) & 0x0f;
805 proto_tree_add_item(coap_tree, hf_coap_code, tvb, offset, 1, ENC_BIG_ENDIAN);
806 code = tvb_get_guint8(tvb, offset);
809 proto_tree_add_item(coap_tree, hf_coap_mid, tvb, offset, 2, ENC_BIG_ENDIAN);
810 mid = tvb_get_ntohs(tvb, offset);
813 /* append the header information */
814 proto_item_append_text(coap_tree, ", %s, %s, MID:%u", val_to_str(ttype, vals_ttype, "Unkown %d"), val_to_str(code, vals_code, "Unknown %d"), mid);
816 /* initialize the external value */
817 coap_block_number = DEFAULT_COAP_BLOCK_NUMBER;
818 coap_block_mflag = 0;
819 coap_uri_str[0] = '\0';
820 coap_uri_query[0] = '\0';
821 coap_token_str[0] = '\0';
825 g_strlcat(coap_token_str, tvb_bytes_to_str_punct(tvb, offset, token_len, ' '), sizeof(coap_token_str));
826 proto_tree_add_item(coap_tree, hf_coap_token,
827 tvb, offset, token_len, ENC_NA);
831 /* process options */
832 offset = dissect_coap_options(tvb, pinfo, coap_tree, offset, coap_length);
836 /* add informations to the packet list */
837 col_set_str(pinfo->cinfo, COL_PROTOCOL, "CoAP");
838 col_clear(pinfo->cinfo, COL_INFO);
839 col_add_fstr(pinfo->cinfo, COL_INFO, "%s", val_to_str(ttype, vals_ttype_short, "Unknown %d"));
840 col_append_fstr(pinfo->cinfo, COL_INFO, ", MID:%u", mid);
841 col_append_fstr(pinfo->cinfo, COL_INFO, ", %s", val_to_str(code, vals_code, "Unknown %d"));
842 if (coap_token_str[0] != '\0')
843 col_append_fstr(pinfo->cinfo, COL_INFO, ", TKN:%s", coap_token_str);
844 if (coap_block_number != DEFAULT_COAP_BLOCK_NUMBER)
845 col_append_fstr(pinfo->cinfo, COL_INFO, ", %sBlock #%d", coap_block_mflag ? "" : "End of ", coap_block_number);
846 if (coap_uri_str[0] != '\0')
847 col_append_fstr(pinfo->cinfo, COL_INFO, ", %s", coap_uri_str);
848 if (coap_uri_query[0] != '\0')
849 col_append_fstr(pinfo->cinfo, COL_INFO, "%s", coap_uri_query);
851 /* dissect the payload */
852 if (coap_length > offset) {
853 proto_tree *payload_tree = NULL;
854 proto_item *payload_item = NULL;
855 tvbuff_t *payload_tvb;
856 guint payload_length = coap_length - offset;
857 char str_payload[80];
860 * 5.5.2. Diagnostic Payload
862 * If no Content-Format option is given, the payload of responses
863 * indicating a client or server error is a brief human-readable
864 * diagnostic message, explaining the error situation. This diagnostic
865 * message MUST be encoded using UTF-8 [RFC3629], more specifically
866 * using Net-Unicode form [RFC5198].
868 if (coap_ctype_value == DEFAULT_COAP_CTYPE_VALUE)
869 coap_ctype_str = "text/plain; charset=utf-8";
871 g_snprintf(str_payload, sizeof(str_payload),
872 "Payload Content-Format: %s%s, Length: %u, offset: %u",
873 coap_ctype_str, coap_ctype_value == DEFAULT_COAP_CTYPE_VALUE ?
874 " (no Content-Format)" : "", payload_length, offset);
875 proto_tree_add_string(coap_tree, hf_coap_payload_desc,
876 tvb, offset, -1, coap_ctype_str);
877 payload_tree = proto_item_add_subtree(payload_item, ett_coap_payload);
878 payload_tvb = tvb_new_subset(tvb, offset, payload_length, payload_length);
880 dissector_try_string(media_type_dissector_table, coap_ctype_str, payload_tvb, pinfo, payload_tree);
885 * Protocol initialization
888 proto_register_coap(void)
890 static hf_register_info hf[] = {
892 { "Version", "coap.version",
893 FT_UINT8, BASE_DEC, NULL, 0xc0,
894 "CoAP Version", HFILL }
897 { "Type", "coap.type",
898 FT_UINT8, BASE_DEC, VALS(vals_ttype), 0x30,
899 "CoAP Transaction Type", HFILL }
901 { &hf_coap_token_len,
902 { "Token Length", "coap.token_len",
903 FT_UINT8, BASE_DEC, NULL, 0x0f,
904 "CoAP Token Length", HFILL }
907 { "Token", "coap.token",
908 FT_BYTES, BASE_NONE, NULL, 0x0,
909 "CoAP Token", HFILL }
912 { "Code", "coap.code",
913 FT_UINT8, BASE_DEC, VALS(vals_code), 0x0,
914 "CoAP Method or Response Code", HFILL }
917 { "Message ID", "coap.mid",
918 FT_UINT16, BASE_DEC, NULL, 0x0,
919 "CoAP Message ID", HFILL }
921 { &hf_coap_payload_desc,
922 { "Payload Desc", "coap.opt.payload_desc",
923 FT_STRING, BASE_NONE, NULL, 0x0,
924 "CoAP Payload Description", HFILL }
927 { "Opt Name", "coap.opt.name",
928 FT_STRING, BASE_NONE, NULL, 0x0,
929 "CoAP Option Name", HFILL }
932 { "Opt Desc", "coap.opt.desc",
933 FT_STRING, BASE_NONE, NULL, 0x0,
934 "CoAP Option Description", HFILL }
936 { &hf_coap_opt_delta,
937 { "Opt Delta", "coap.opt.delta",
938 FT_UINT8, BASE_DEC, NULL, 0xf0,
939 "CoAP Option Delta", HFILL }
941 { &hf_coap_opt_delta_ext,
942 { "Opt Delta extended", "coap.opt.delta_ext",
943 FT_UINT16, BASE_DEC, NULL, 0x0,
944 "CoAP Option Delta extended", HFILL }
946 { &hf_coap_opt_length,
947 { "Opt Length", "coap.opt.length",
948 FT_UINT8, BASE_DEC, NULL, 0x0f,
949 "CoAP Option Length", HFILL }
951 { &hf_coap_opt_length_ext,
952 { "Opt Length extended", "coap.opt.length_ext",
953 FT_UINT16, BASE_DEC, NULL, 0x0,
954 "CoAP Option Length extended", HFILL }
956 { &hf_coap_opt_end_marker,
957 { "End of options marker", "coap.opt.end_marker",
958 FT_UINT8, BASE_DEC, NULL, 0x00,
959 "CoAP End of options marker", HFILL }
961 { &hf_coap_opt_ctype,
962 { "Content-type", "coap.opt.ctype",
963 FT_STRING, BASE_NONE, NULL, 0x0,
964 "CoAP Option Content Type", HFILL }
966 { &hf_coap_opt_max_age,
967 { "Max-age", "coap.opt.max_age",
968 FT_UINT32, BASE_DEC, NULL, 0x0,
969 "CoAP Option Max-age", HFILL }
971 { &hf_coap_opt_proxy_uri,
972 { "Proxy-Uri", "coap.opt.proxy_uri",
973 FT_STRING, BASE_NONE, NULL, 0x0,
974 "CoAP Option Proxy-Uri", HFILL }
977 { "Etag", "coap.opt.etag",
978 FT_BYTES, BASE_NONE, NULL, 0x0,
979 "CoAP Option Etag", HFILL }
981 { &hf_coap_opt_uri_host,
982 { "Uri-Host", "coap.opt.uri_host",
983 FT_STRING, BASE_NONE, NULL, 0x0,
984 "CoAP Option Uri-Host", HFILL }
986 { &hf_coap_opt_location_path,
987 { "Location-Path", "coap.opt.location_path",
988 FT_STRING, BASE_NONE, NULL, 0x0,
989 "CoAP Option Uri-Path", HFILL }
991 { &hf_coap_opt_uri_port,
992 { "Uri-Port", "coap.opt.uri_port",
993 FT_UINT16, BASE_DEC, NULL, 0x0,
994 "CoAP Option Uri-Port", HFILL }
996 { &hf_coap_opt_location_query,
997 { "Location-Query", "coap.opt.location_query",
998 FT_STRING, BASE_NONE, NULL, 0x0,
999 "CoAP Option Uri-Query", HFILL }
1001 { &hf_coap_opt_uri_path,
1002 { "Uri-Path", "coap.opt.uri_path",
1003 FT_STRING, BASE_NONE, NULL, 0x0,
1004 "CoAP Option Uri-Path", HFILL }
1006 { &hf_coap_opt_observe,
1007 { "Lifetime", "coap.opt.subscr_lifetime",
1008 FT_UINT32, BASE_DEC, NULL, 0x0,
1009 "CoAP Option Observe", HFILL }
1011 { &hf_coap_opt_accept,
1012 { "Accept", "coap.opt.accept",
1013 FT_STRING, BASE_NONE, NULL, 0x0,
1014 "CoAP Option Acceptable Content Type", HFILL }
1016 { &hf_coap_opt_if_match,
1017 { "If-Match", "coap.opt.if_match",
1018 FT_BYTES, BASE_NONE, NULL, 0x0,
1019 "CoAP Option If-Match", HFILL }
1021 { &hf_coap_opt_block_number,
1022 { "Block Number", "coap.opt.block_number",
1023 FT_UINT32, BASE_DEC, NULL, 0x0,
1024 "CoAP Option Block Number", HFILL }
1026 { &hf_coap_opt_block_mflag,
1027 { "More Flag", "coap.opt.block_mflag",
1028 FT_UINT8, BASE_DEC, NULL, 0x08,
1029 "CoAP Option Block More Size", HFILL }
1031 { &hf_coap_opt_block_size,
1032 { "Encoded Block Size", "coap.opt.block_size",
1033 FT_UINT8, BASE_DEC, NULL, 0x07,
1034 "CoAP Option Encoded Block Size", HFILL }
1036 { &hf_coap_opt_uri_query,
1037 { "Uri-Query", "coap.opt.uri_query",
1038 FT_STRING, BASE_NONE, NULL, 0x0,
1039 "CoAP Option Uri-Query", HFILL }
1041 { &hf_coap_opt_unknown,
1042 { "Unknown", "coap.opt.unknown",
1043 FT_STRING, BASE_NONE, NULL, 0x0,
1044 "Coap Unknown Option", HFILL }
1048 static gint *ett[] = {
1054 module_t *coap_module;
1056 proto_coap = proto_register_protocol("Constrained Application Protocol", "CoAP", "coap");
1057 proto_register_field_array(proto_coap, hf, array_length(hf));
1058 proto_register_subtree_array(ett, array_length(ett));
1060 register_dissector("coap", dissect_coap, proto_coap);
1062 /* Register our configuration options */
1063 coap_module = prefs_register_protocol (proto_coap, proto_reg_handoff_coap);
1065 prefs_register_uint_preference (coap_module, "udp_port",
1067 "Port number used for CoAP traffic",
1068 10, &global_coap_port_number);
1072 proto_reg_handoff_coap(void)
1074 static gboolean coap_prefs_initialized = FALSE;
1075 static dissector_handle_t coap_handle;
1076 static guint coap_port_number;
1078 if (!coap_prefs_initialized) {
1079 coap_handle = find_dissector("coap");
1080 media_type_dissector_table = find_dissector_table("media_type");
1081 coap_prefs_initialized = TRUE;
1083 dissector_delete_uint("udp.port", coap_port_number, coap_handle);
1084 dissector_delete_uint("tcp.port", coap_port_number, coap_handle);
1087 coap_port_number = global_coap_port_number;
1088 dissector_add_uint("udp.port", coap_port_number, coap_handle);
1089 dissector_add_uint("tcp.port", coap_port_number, coap_handle);