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
= -1;
54 static int hf_coap_payload_desc
= -1;
55 static int hf_coap_opt_name
= -1;
56 static int hf_coap_opt_desc
= -1;
57 static int hf_coap_opt_delta
= -1;
58 static int hf_coap_opt_delta_ext
= -1;
59 static int hf_coap_opt_length
= -1;
60 static int hf_coap_opt_length_ext
= -1;
61 static int hf_coap_opt_end_marker
= -1;
62 static int hf_coap_opt_ctype
= -1;
63 static int hf_coap_opt_max_age
= -1;
64 static int hf_coap_opt_proxy_uri
= -1;
65 static int hf_coap_opt_etag
= -1;
66 static int hf_coap_opt_uri_host
= -1;
67 static int hf_coap_opt_location_path
= -1;
68 static int hf_coap_opt_uri_port
= -1;
69 static int hf_coap_opt_location_query
= -1;
70 static int hf_coap_opt_uri_path
= -1;
71 static int hf_coap_opt_observe
= -1;
72 static int hf_coap_opt_accept
= -1;
73 static int hf_coap_opt_if_match
= -1;
74 static int hf_coap_opt_block_number
= -1;
75 static int hf_coap_opt_block_mflag
= -1;
76 static int hf_coap_opt_block_size
= -1;
77 static int hf_coap_opt_uri_query
= -1;
78 static int hf_coap_opt_unknown
= -1;
80 static gint ett_coap
= -1;
81 static gint ett_coap_option
= -1;
82 static gint ett_coap_payload
= -1;
84 static expert_field ei_coap_invalid_option_number
= EI_INIT
;
85 static expert_field ei_coap_invalid_option_range
= EI_INIT
;
86 static expert_field ei_coap_option_length_bad
= EI_INIT
;
88 /* CoAP's IANA-assigned port number */
89 #define DEFAULT_COAP_PORT 5683
91 /* indicators whether those are to be showed or not */
92 #define DEFAULT_COAP_CTYPE_VALUE ~0
93 #define DEFAULT_COAP_BLOCK_NUMBER ~0
95 static const gchar
*coap_ctype_str
= NULL
;
96 static gint coap_ctype_value
= DEFAULT_COAP_CTYPE_VALUE
;
97 static guint global_coap_port_number
= DEFAULT_COAP_PORT
;
98 static gint coap_block_number
= DEFAULT_COAP_BLOCK_NUMBER
;
99 static guint coap_block_mflag
= 0;
100 static gchar coap_uri_str
[1024]; /* the maximum is 1024 > 510 = Uri-Host:255 + Uri-Path:255 x 2 */
101 static gchar coap_uri_query
[1024]; /* the maximum is 1024 > 765 = Uri-Query:255 x 3 */
102 static gchar coap_token_str
[128];
107 static const value_string vals_ttype
[] = {
108 { 0, "Confirmable" },
109 { 1, "Non-Confirmable" },
110 { 2, "Acknowledgement" },
114 static const value_string vals_ttype_short
[] = {
126 static const value_string vals_code
[] = {
127 { 0, "Empty Message" },
136 { 65, "2.01 Created" },
137 { 66, "2.02 Deleted" },
138 { 67, "2.03 Valid" },
139 { 68, "2.04 Changed" },
140 { 69, "2.05 Content" },
141 { 128, "4.00 Bad Request" },
142 { 129, "4.01 Unauthorized" },
143 { 130, "4.02 Bad Option" },
144 { 131, "4.03 Forbidden" },
145 { 132, "4.04 Not Found" },
146 { 133, "4.05 Method Not Allowed" },
147 { 134, "4.06 Not Acceptable" },
148 { 136, "4.08 Request Entity Incomplete" }, /* core-block-10 */
149 { 140, "4.12 Precondition Failed" },
150 { 141, "4.13 Request Entity Too Large" },
151 { 143, "4.15 Unsupported Content-Format" },
152 { 160, "5.00 Internal Server Error" },
153 { 161, "5.01 Not Implemented" },
154 { 162, "5.02 Bad Gateway" },
155 { 163, "5.03 Service Unavailable" },
156 { 164, "5.04 Gateway Timeout" },
157 { 165, "5.05 Proxying Not Supported" },
164 * No-Option must not be included in this structure, is handled in the function
165 * of the dissector, especially.
167 #define COAP_OPT_IF_MATCH 1
168 #define COAP_OPT_URI_HOST 3
169 #define COAP_OPT_ETAG 4
170 #define COAP_OPT_IF_NONE_MATCH 5
171 #define COAP_OPT_OBSERVE 6 /* core-observe-07 */
172 #define COAP_OPT_URI_PORT 7
173 #define COAP_OPT_LOCATION_PATH 8
174 #define COAP_OPT_URI_PATH 11
175 #define COAP_OPT_CONTENT_TYPE 12
176 #define COAP_OPT_MAX_AGE 14
177 #define COAP_OPT_URI_QUERY 15
178 #define COAP_OPT_ACCEPT 16
179 #define COAP_OPT_LOCATION_QUERY 20
180 #define COAP_OPT_BLOCK2 23 /* core-block-10 */
181 #define COAP_OPT_BLOCK_SIZE 28 /* core-block-10 */
182 #define COAP_OPT_BLOCK1 27 /* core-block-10 */
183 #define COAP_OPT_PROXY_URI 35
185 static const value_string vals_opt_type
[] = {
186 { COAP_OPT_IF_MATCH
, "If-Match" },
187 { COAP_OPT_URI_HOST
, "Uri-Host" },
188 { COAP_OPT_ETAG
, "Etag" },
189 { COAP_OPT_IF_NONE_MATCH
, "If-None-Match" },
190 { COAP_OPT_URI_PORT
, "Uri-Port" },
191 { COAP_OPT_LOCATION_PATH
, "Location-Path" },
192 { COAP_OPT_URI_PATH
, "Uri-Path" },
193 { COAP_OPT_CONTENT_TYPE
, "Content-Format" },
194 { COAP_OPT_MAX_AGE
, "Max-age" },
195 { COAP_OPT_URI_QUERY
, "Uri-Query" },
196 { COAP_OPT_ACCEPT
, "Accept" },
197 { COAP_OPT_LOCATION_QUERY
, "Location-Query" },
198 { COAP_OPT_PROXY_URI
, "Proxy-Uri" },
199 { COAP_OPT_OBSERVE
, "Observe" },
200 { COAP_OPT_BLOCK2
, "Block2" },
201 { COAP_OPT_BLOCK1
, "Block1" },
202 { COAP_OPT_BLOCK_SIZE
, "Block Size" },
206 struct coap_option_range_t
{
211 { COAP_OPT_IF_MATCH
, 0, 8 },
212 { COAP_OPT_URI_HOST
, 1, 255 },
213 { COAP_OPT_ETAG
, 1, 8 },
214 { COAP_OPT_IF_NONE_MATCH
, 0, 0 },
215 { COAP_OPT_URI_PORT
, 0, 2 },
216 { COAP_OPT_LOCATION_PATH
, 0, 255 },
217 { COAP_OPT_URI_PATH
, 0, 255 },
218 { COAP_OPT_CONTENT_TYPE
, 0, 2 },
219 { COAP_OPT_MAX_AGE
, 0, 4 },
220 { COAP_OPT_URI_QUERY
, 1, 255 },
221 { COAP_OPT_ACCEPT
, 0, 2 },
222 { COAP_OPT_LOCATION_QUERY
, 0, 255 },
223 { COAP_OPT_PROXY_URI
, 1,1034 },
224 { COAP_OPT_OBSERVE
, 0, 2 },
225 { COAP_OPT_BLOCK2
, 0, 3 },
226 { COAP_OPT_BLOCK1
, 0, 3 },
227 { COAP_OPT_BLOCK_SIZE
, 0, 4 },
231 static const value_string vals_ctype
[] = {
232 { 0, "text/plain; charset=utf-8" },
233 { 40, "application/link-format" },
234 { 41, "application/xml" },
235 { 42, "application/octet-stream" },
236 { 47, "application/exi" },
237 { 50, "application/json" },
241 static const char *nullstr
= "(null)";
243 void proto_reg_handoff_coap(void);
246 coap_is_str_ipv6addr(guint8
*str
)
248 size_t len
= strlen(str
);
256 return colon
> 1 ? 1 : 0;
260 coap_get_opt_uint(tvbuff_t
*tvb
, gint offset
, gint length
)
266 return (guint
)tvb_get_guint8(tvb
, offset
);
268 return (guint
)tvb_get_ntohs(tvb
, offset
);
270 return (guint
)tvb_get_ntoh24(tvb
, offset
);
272 return (guint
)tvb_get_ntohl(tvb
, offset
);
279 coap_opt_check(packet_info
*pinfo
, proto_tree
*subtree
, guint opt_num
, gint opt_length
)
283 for (i
= 0; i
< (int)(array_length(coi
)); i
++) {
284 if (coi
[i
].type
== opt_num
)
287 if (i
== (int)(array_length(coi
))) {
288 expert_add_info_format(pinfo
, subtree
, &ei_coap_invalid_option_number
,
289 "Invalid Option Number %d", opt_num
);
292 if (opt_length
< coi
[i
].min
|| opt_length
> coi
[i
].max
) {
293 expert_add_info_format(pinfo
, subtree
, &ei_coap_invalid_option_range
,
294 "Invalid Option Range: %d (%d < x < %d)", opt_length
, coi
[i
].min
, coi
[i
].max
);
301 dissect_coap_opt_hex_string(tvbuff_t
*tvb
, proto_item
*item
, proto_tree
*subtree
, gint offset
, gint opt_length
, int hf
)
308 str
= tvb_bytes_to_str_punct(tvb
, offset
, opt_length
, ' ');
310 proto_tree_add_item(subtree
, hf
, tvb
, offset
, opt_length
, ENC_NA
);
312 /* add info to the head of the packet detail */
313 proto_item_append_text(item
, ": %s", str
);
317 dissect_coap_opt_uint(tvbuff_t
*tvb
, proto_item
*head_item
, proto_tree
*subtree
, gint offset
, gint opt_length
, int hf
)
321 if (opt_length
!= 0) {
322 i
= coap_get_opt_uint(tvb
, offset
, opt_length
);
325 proto_tree_add_uint(subtree
, hf
, tvb
, offset
, opt_length
, i
);
327 /* add info to the head of the packet detail */
328 proto_item_append_text(head_item
, ": %d", i
);
332 dissect_coap_opt_uri_host(tvbuff_t
*tvb
, proto_item
*head_item
, proto_tree
*subtree
, gint offset
, gint opt_length
)
336 str
= tvb_get_string(wmem_packet_scope(), tvb
, offset
, opt_length
);
338 proto_tree_add_string(subtree
, hf_coap_opt_uri_host
, tvb
, offset
, opt_length
, str
);
340 /* add info to the head of the packet detail */
341 proto_item_append_text(head_item
, ": %s", str
);
343 /* forming a uri-string */
344 g_strlcat(coap_uri_str
, "coap://", sizeof(coap_uri_str
));
346 * if the string looks an IPv6 address, assuming that it has
347 * to be enclosed by brackets.
349 if (coap_is_str_ipv6addr(str
)) {
350 g_strlcat(coap_uri_str
, "[", sizeof(coap_uri_str
));
351 g_strlcat(coap_uri_str
, str
, sizeof(coap_uri_str
));
352 g_strlcat(coap_uri_str
, "]", sizeof(coap_uri_str
));
354 g_strlcat(coap_uri_str
, str
, sizeof(coap_uri_str
));
358 dissect_coap_opt_uri_path(tvbuff_t
*tvb
, proto_item
*head_item
, proto_tree
*subtree
, gint offset
, gint opt_length
)
360 const guint8
*str
= NULL
;
362 g_strlcat(coap_uri_str
, "/", sizeof(coap_uri_str
));
364 if (opt_length
== 0) {
367 str
= tvb_get_string(wmem_packet_scope(), tvb
, offset
, opt_length
);
368 g_strlcat(coap_uri_str
, str
, sizeof(coap_uri_str
));
371 proto_tree_add_string(subtree
, hf_coap_opt_uri_path
, tvb
, offset
, opt_length
, str
);
373 /* add info to the head of the packet detail */
374 proto_item_append_text(head_item
, ": %s", str
);
378 dissect_coap_opt_uri_query(tvbuff_t
*tvb
, proto_item
*head_item
,proto_tree
*subtree
, gint offset
, gint opt_length
)
380 const guint8
*str
= NULL
;
382 if (coap_uri_query
[0] == '\0')
383 g_strlcat(coap_uri_query
, "?", sizeof(coap_uri_str
));
385 g_strlcat(coap_uri_query
, "&", sizeof(coap_uri_str
));
387 if (opt_length
== 0) {
390 str
= tvb_get_string(wmem_packet_scope(), tvb
, offset
, opt_length
);
391 g_strlcat(coap_uri_str
, str
, sizeof(coap_uri_str
));
394 proto_tree_add_string(subtree
, hf_coap_opt_uri_query
, tvb
, offset
, opt_length
, str
);
396 /* add info to the head of the packet detail */
397 proto_item_append_text(head_item
, ": %s", str
);
401 dissect_coap_opt_location_path(tvbuff_t
*tvb
, proto_item
*head_item
, proto_tree
*subtree
, gint offset
, gint opt_length
)
403 const guint8
*str
= NULL
;
405 if (opt_length
== 0) {
408 str
= tvb_get_string(wmem_packet_scope(), tvb
, offset
, opt_length
);
411 proto_tree_add_string(subtree
, hf_coap_opt_location_path
, tvb
, offset
, opt_length
, str
);
413 /* add info to the head of the packet detail */
414 proto_item_append_text(head_item
, ": %s", str
);
418 dissect_coap_opt_location_query(tvbuff_t
*tvb
, proto_item
*head_item
, proto_tree
*subtree
, gint offset
, gint opt_length
)
420 const guint8
*str
= NULL
;
422 if (opt_length
== 0) {
425 str
= tvb_get_string(wmem_packet_scope(), tvb
, offset
, opt_length
);
428 proto_tree_add_string(subtree
, hf_coap_opt_location_query
, tvb
, offset
, opt_length
, str
);
430 /* add info to the head of the packet detail */
431 proto_item_append_text(head_item
, ": %s", str
);
435 dissect_coap_opt_proxy_uri(tvbuff_t
*tvb
, proto_item
*head_item
, proto_tree
*subtree
, gint offset
, gint opt_length
)
437 const guint8
*str
= NULL
;
439 if (opt_length
== 0) {
442 str
= tvb_get_string(wmem_packet_scope(), tvb
, offset
, opt_length
);
445 proto_tree_add_string(subtree
, hf_coap_opt_proxy_uri
, tvb
, offset
, opt_length
, str
);
447 /* add info to the head of the packet detail */
448 proto_item_append_text(head_item
, ": %s", str
);
452 dissect_coap_opt_ctype(tvbuff_t
*tvb
, proto_item
*head_item
, proto_tree
*subtree
, gint offset
, gint opt_length
, int hf
)
454 if (opt_length
== 0) {
455 coap_ctype_value
= 0;
457 coap_ctype_value
= coap_get_opt_uint(tvb
, offset
, opt_length
);
460 coap_ctype_str
= val_to_str(coap_ctype_value
, vals_ctype
, "Unknown Type %d");
462 proto_tree_add_string(subtree
, hf
, tvb
, offset
, opt_length
, coap_ctype_str
);
464 /* add info to the head of the packet detail */
465 proto_item_append_text(head_item
, ": %s", coap_ctype_str
);
469 dissect_coap_opt_block(tvbuff_t
*tvb
, proto_item
*head_item
, proto_tree
*subtree
, gint offset
, gint opt_length
)
472 guint encoded_block_size
;
475 if (opt_length
== 0) {
476 coap_block_number
= 0;
479 coap_block_number
= coap_get_opt_uint(tvb
, offset
, opt_length
) >> 4;
480 val
= tvb_get_guint8(tvb
, offset
+ opt_length
- 1) & 0x0f;
483 proto_tree_add_uint(subtree
, hf_coap_opt_block_number
,
484 tvb
, offset
, opt_length
, coap_block_number
);
486 /* More flag in the end of the option */
487 coap_block_mflag
= val
& 0x08;
488 proto_tree_add_uint(subtree
, hf_coap_opt_block_mflag
,
489 tvb
, offset
+ opt_length
- 1, 1, coap_block_mflag
);
492 encoded_block_size
= val
& 0x07;
493 block_esize
= 1 << (encoded_block_size
+ 4);
494 proto_tree_add_uint_format(subtree
, hf_coap_opt_block_size
,
495 tvb
, offset
+ opt_length
- 1, 1, encoded_block_size
, "Block Size: %d (%d encoded)", block_esize
, encoded_block_size
);
497 /* add info to the head of the packet detail */
498 proto_item_append_text(head_item
, ": NUM:%d, M:%d, SZX:%d",
499 coap_block_number
, coap_block_mflag
, block_esize
);
503 dissect_coap_opt_uri_port(tvbuff_t
*tvb
, proto_item
*head_item
, proto_tree
*subtree
, gint offset
, gint opt_length
)
508 memset(portstr
, '\0', sizeof(portstr
));
510 if (opt_length
!= 0) {
511 port
= coap_get_opt_uint(tvb
, offset
, opt_length
);
514 proto_tree_add_uint(subtree
, hf_coap_opt_uri_port
, tvb
, offset
, opt_length
, port
);
516 proto_item_append_text(head_item
, ": %d", port
);
518 /* forming a uri-string */
519 g_snprintf(portstr
, sizeof(portstr
), "%u", port
);
520 g_strlcat(coap_uri_str
, ":", sizeof(coap_uri_str
));
521 g_strlcat(coap_uri_str
, portstr
, sizeof(coap_uri_str
));
525 * dissector for each option of CoAP.
526 * return the total length of the option including the header (e.g. delta and length).
529 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
)
532 gint opt_length
, opt_length_ext
, opt_delta
, opt_delta_ext
;
533 gint opt_length_ext_off
= 0;
534 gint8 opt_length_ext_len
= 0;
535 gint opt_delta_ext_off
= 0;
536 gint8 opt_delta_ext_len
= 0;
537 gint orig_offset
= offset
;
542 opt_jump
= tvb_get_guint8(tvb
, offset
);
543 if (0xff == opt_jump
)
548 * section 3.1 in coap-17:
549 * Option Delta: 4-bit unsigned integer. A value between 0 and 12
550 * indicates the Option Delta. Three values are reserved for special
553 * 13: An 8-bit unsigned integer follows the initial byte and
554 * indicates the Option Delta minus 13.
556 * 14: A 16-bit unsigned integer in network byte order follows the
557 * initial byte and indicates the Option Delta minus 269.
559 * 15: Reserved for the Payload Marker. If the field is set to this
560 * value but the entire byte is not the payload marker, this MUST
561 * be processed as a message format error.
563 switch (opt_jump
& 0xf0) {
565 opt_delta_ext
= tvb_get_guint8(tvb
, offset
);
566 opt_delta_ext_off
= offset
;
567 opt_delta_ext_len
= 1;
571 opt_delta
+= opt_delta_ext
;
574 opt_delta_ext
= coap_get_opt_uint(tvb
, offset
, 2);
575 opt_delta_ext_off
= offset
;
576 opt_delta_ext_len
= 2;
580 opt_delta
+= opt_delta_ext
;
583 expert_add_info_format(pinfo
, coap_tree
, &ei_coap_option_length_bad
,
584 "end-of-options marker found, but option length isn't 15");
587 opt_delta
= ((opt_jump
& 0xf0) >> 4);
590 *opt_num
+= opt_delta
;
593 * section 3.1 in coap-17:
594 * Option Length: 4-bit unsigned integer. A value between 0 and 12
595 * indicates the length of the Option Value, in bytes. Three values
596 * are reserved for special constructs:
598 * 13: An 8-bit unsigned integer precedes the Option Value and
599 * indicates the Option Length minus 13.
601 * 14: A 16-bit unsigned integer in network byte order precedes the
602 * Option Value and indicates the Option Length minus 269.
604 * 15: Reserved for future use. If the field is set to this value,
605 * it MUST be processed as a message format error.
607 switch (opt_jump
& 0x0f) {
609 opt_length_ext
= tvb_get_guint8(tvb
, offset
);
610 opt_length_ext_off
= offset
;
611 opt_length_ext_len
= 1;
615 opt_length
+= opt_length_ext
;
618 opt_length_ext
= coap_get_opt_uint(tvb
, offset
, 2);
619 opt_length_ext_off
= offset
;
620 opt_length_ext_len
= 2;
624 opt_length
+= opt_length_ext
;
627 expert_add_info_format(pinfo
, coap_tree
, &ei_coap_option_length_bad
,
628 "end-of-options marker found, but option delta isn't 15");
631 opt_length
= (opt_jump
& 0x0f);
634 if (offset
+ opt_length
> coap_length
) {
635 expert_add_info_format(pinfo
, coap_tree
, &ei_coap_option_length_bad
,
636 "option longer than the package");
640 coap_opt_check(pinfo
, coap_tree
, *opt_num
, opt_length
);
642 g_snprintf(strbuf
, sizeof(strbuf
),
643 "#%u: %s", opt_count
, val_to_str_const(*opt_num
, vals_opt_type
,
644 *opt_num
% 14 == 0 ? "No-Op" : "Unknown Option"));
645 item
= proto_tree_add_string(coap_tree
, hf_coap_opt_name
,
646 tvb
, orig_offset
, offset
- orig_offset
+ opt_length
, strbuf
);
647 subtree
= proto_item_add_subtree(item
, ett_coap_option
);
649 g_snprintf(strbuf
, sizeof(strbuf
),
650 "Type %u, %s, %s%s", *opt_num
,
651 (*opt_num
& 1) ? "Critical" : "Elective",
652 (*opt_num
& 2) ? "Unsafe" : "Safe",
653 ((*opt_num
& 0x1e) == 0x1c) ? ", NoCacheKey" : "");
654 proto_tree_add_string(subtree
, hf_coap_opt_desc
,
655 tvb
, orig_offset
, offset
- orig_offset
+ opt_length
, strbuf
);
657 proto_tree_add_item(subtree
, hf_coap_opt_delta
, tvb
, orig_offset
, 1, ENC_BIG_ENDIAN
);
658 proto_tree_add_item(subtree
, hf_coap_opt_length
, tvb
, orig_offset
, 1, ENC_BIG_ENDIAN
);
660 if (opt_delta_ext_off
&& opt_delta_ext_len
)
661 proto_tree_add_item(subtree
, hf_coap_opt_delta_ext
, tvb
, opt_delta_ext_off
, opt_delta_ext_len
, ENC_BIG_ENDIAN
);
663 if (opt_length_ext_off
&& opt_length_ext_len
)
664 proto_tree_add_item(subtree
, hf_coap_opt_length_ext
, tvb
, opt_length_ext_off
, opt_length_ext_len
, ENC_BIG_ENDIAN
);
666 /* offset points the next to its option header */
668 case COAP_OPT_CONTENT_TYPE
:
669 dissect_coap_opt_ctype(tvb
, item
, subtree
, offset
,
670 opt_length
, hf_coap_opt_ctype
);
672 case COAP_OPT_MAX_AGE
:
673 dissect_coap_opt_uint(tvb
, item
, subtree
, offset
,
674 opt_length
, hf_coap_opt_max_age
);
676 case COAP_OPT_PROXY_URI
:
677 dissect_coap_opt_proxy_uri(tvb
, item
, subtree
, offset
,
681 dissect_coap_opt_hex_string(tvb
, item
, subtree
, offset
,
682 opt_length
, hf_coap_opt_etag
);
684 case COAP_OPT_URI_HOST
:
685 dissect_coap_opt_uri_host(tvb
, item
, subtree
, offset
,
688 case COAP_OPT_LOCATION_PATH
:
689 dissect_coap_opt_location_path(tvb
, item
, subtree
, offset
,
692 case COAP_OPT_URI_PORT
:
693 dissect_coap_opt_uri_port(tvb
, item
, subtree
, offset
,
696 case COAP_OPT_LOCATION_QUERY
:
697 dissect_coap_opt_location_query(tvb
, item
, subtree
, offset
,
700 case COAP_OPT_URI_PATH
:
701 dissect_coap_opt_uri_path(tvb
, item
, subtree
, offset
,
704 case COAP_OPT_OBSERVE
:
705 dissect_coap_opt_uint(tvb
, item
, subtree
, offset
,
706 opt_length
, hf_coap_opt_observe
);
708 case COAP_OPT_ACCEPT
:
709 dissect_coap_opt_ctype(tvb
, item
, subtree
, offset
,
710 opt_length
, hf_coap_opt_accept
);
712 case COAP_OPT_IF_MATCH
:
713 dissect_coap_opt_hex_string(tvb
, item
, subtree
, offset
,
714 opt_length
, hf_coap_opt_if_match
);
716 case COAP_OPT_URI_QUERY
:
717 dissect_coap_opt_uri_query(tvb
, item
, subtree
, offset
,
720 case COAP_OPT_BLOCK2
:
721 dissect_coap_opt_block(tvb
, item
, subtree
, offset
,
724 case COAP_OPT_BLOCK1
:
725 dissect_coap_opt_block(tvb
, item
, subtree
, offset
,
728 case COAP_OPT_IF_NONE_MATCH
:
730 case COAP_OPT_BLOCK_SIZE
:
731 dissect_coap_opt_uint(tvb
, item
, subtree
, offset
,
732 opt_length
, hf_coap_opt_block_size
);
735 dissect_coap_opt_hex_string(tvb
, item
, subtree
, offset
,
736 opt_length
, hf_coap_opt_unknown
);
740 return offset
+ opt_length
;
745 * return offset pointing the next of options. (i.e. the top of the paylaod
746 * or the end of the data.
749 dissect_coap_options(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*coap_tree
, gint offset
, gint coap_length
)
755 /* loop for dissecting options */
756 for (i
= 1; offset
< coap_length
; i
++) {
757 offset
= dissect_coap_options_main(tvb
, pinfo
, coap_tree
,
758 offset
, i
, &opt_num
, coap_length
);
761 if (offset
>= coap_length
)
763 endmarker
= tvb_get_guint8(tvb
, offset
);
764 if (endmarker
== 0xff) {
765 proto_tree_add_item(coap_tree
, hf_coap_opt_end_marker
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
775 dissect_coap(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*parent_tree
)
778 proto_item
*coap_root
;
779 proto_tree
*coap_tree
;
786 /* initialize the CoAP length and the content-Format */
788 * the length of CoAP message is not specified in the CoAP header.
789 * It has to be from the lower layer.
790 * Currently, the length is just copied from the reported length of the tvbuffer.
792 coap_length
= tvb_reported_length(tvb
);
794 coap_ctype_value
= DEFAULT_COAP_CTYPE_VALUE
;
796 coap_root
= proto_tree_add_item(parent_tree
, proto_coap
, tvb
, offset
, -1, ENC_NA
);
797 coap_tree
= proto_item_add_subtree(coap_root
, ett_coap
);
799 proto_tree_add_item(coap_tree
, hf_coap_version
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
801 proto_tree_add_item(coap_tree
, hf_coap_ttype
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
802 ttype
= (tvb_get_guint8(tvb
, offset
) & 0x30) >> 4;
804 proto_tree_add_item(coap_tree
, hf_coap_token_len
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
805 token_len
= tvb_get_guint8(tvb
, offset
) & 0x0f;
808 proto_tree_add_item(coap_tree
, hf_coap_code
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
809 code
= tvb_get_guint8(tvb
, offset
);
812 proto_tree_add_item(coap_tree
, hf_coap_mid
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
813 mid
= tvb_get_ntohs(tvb
, offset
);
816 /* append the header information */
817 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
);
819 /* initialize the external value */
820 coap_block_number
= DEFAULT_COAP_BLOCK_NUMBER
;
821 coap_block_mflag
= 0;
822 coap_uri_str
[0] = '\0';
823 coap_uri_query
[0] = '\0';
824 coap_token_str
[0] = '\0';
828 g_strlcat(coap_token_str
, tvb_bytes_to_str_punct(tvb
, offset
, token_len
, ' '), sizeof(coap_token_str
));
829 proto_tree_add_item(coap_tree
, hf_coap_token
,
830 tvb
, offset
, token_len
, ENC_NA
);
834 /* process options */
835 offset
= dissect_coap_options(tvb
, pinfo
, coap_tree
, offset
, coap_length
);
839 /* add informations to the packet list */
840 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "CoAP");
841 col_add_fstr(pinfo
->cinfo
, COL_INFO
, "%s", val_to_str(ttype
, vals_ttype_short
, "Unknown %d"));
842 col_append_fstr(pinfo
->cinfo
, COL_INFO
, ", MID:%u", mid
);
843 col_append_fstr(pinfo
->cinfo
, COL_INFO
, ", %s", val_to_str(code
, vals_code
, "Unknown %d"));
844 if (coap_token_str
[0] != '\0')
845 col_append_fstr(pinfo
->cinfo
, COL_INFO
, ", TKN:%s", coap_token_str
);
846 if (coap_block_number
!= DEFAULT_COAP_BLOCK_NUMBER
)
847 col_append_fstr(pinfo
->cinfo
, COL_INFO
, ", %sBlock #%d",
848 coap_block_mflag
? "" : "End of ", coap_block_number
);
849 if (coap_uri_str
[0] != '\0')
850 col_append_fstr(pinfo
->cinfo
, COL_INFO
, ", %s", coap_uri_str
);
851 if (coap_uri_query
[0] != '\0')
852 col_append_fstr(pinfo
->cinfo
, COL_INFO
, "%s", coap_uri_query
);
854 /* dissect the payload */
855 if (coap_length
> offset
) {
856 proto_tree
*payload_tree
;
857 proto_item
*payload_item
;
858 tvbuff_t
*payload_tvb
;
859 guint payload_length
= coap_length
- offset
;
860 const char *coap_ctype_str_dis
;
861 char str_payload
[80];
864 * 5.5.2. Diagnostic Payload
866 * If no Content-Format option is given, the payload of responses
867 * indicating a client or server error is a brief human-readable
868 * diagnostic message, explaining the error situation. This diagnostic
869 * message MUST be encoded using UTF-8 [RFC3629], more specifically
870 * using Net-Unicode form [RFC5198].
872 if (coap_ctype_value
== DEFAULT_COAP_CTYPE_VALUE
)
873 coap_ctype_str
= "text/plain; charset=utf-8";
875 g_snprintf(str_payload
, sizeof(str_payload
),
876 "Payload Content-Format: %s%s, Length: %u",
877 coap_ctype_str
, coap_ctype_value
== DEFAULT_COAP_CTYPE_VALUE
?
878 " (no Content-Format)" : "", payload_length
);
880 payload_item
= proto_tree_add_string(coap_tree
, hf_coap_payload
,
881 tvb
, offset
, payload_length
,
883 payload_tree
= proto_item_add_subtree(payload_item
, ett_coap_payload
);
885 proto_tree_add_string(payload_tree
, hf_coap_payload_desc
, tvb
, offset
, -1, coap_ctype_str
);
886 payload_tvb
= tvb_new_subset(tvb
, offset
, payload_length
, payload_length
);
888 if (coap_ctype_value
== DEFAULT_COAP_CTYPE_VALUE
|| coap_ctype_value
== 0) {
889 coap_ctype_str_dis
= "text/plain";
891 coap_ctype_str_dis
= coap_ctype_str
;
894 dissector_try_string(media_type_dissector_table
, coap_ctype_str_dis
,
895 payload_tvb
, pinfo
, payload_tree
, NULL
);
900 * Protocol initialization
903 proto_register_coap(void)
905 static hf_register_info hf
[] = {
907 { "Version", "coap.version",
908 FT_UINT8
, BASE_DEC
, NULL
, 0xc0,
912 { "Type", "coap.type",
913 FT_UINT8
, BASE_DEC
, VALS(vals_ttype
), 0x30,
916 { &hf_coap_token_len
,
917 { "Token Length", "coap.token_len",
918 FT_UINT8
, BASE_DEC
, NULL
, 0x0f,
922 { "Token", "coap.token",
923 FT_BYTES
, BASE_NONE
, NULL
, 0x0,
927 { "Code", "coap.code",
928 FT_UINT8
, BASE_DEC
, VALS(vals_code
), 0x0,
932 { "Message ID", "coap.mid",
933 FT_UINT16
, BASE_DEC
, NULL
, 0x0,
937 { "Payload", "coap.payload",
938 FT_STRING
, BASE_NONE
, NULL
, 0x0,
941 { &hf_coap_payload_desc
,
942 { "Payload Desc", "coap.opt.payload_desc",
943 FT_STRING
, BASE_NONE
, NULL
, 0x0,
947 { "Opt Name", "coap.opt.name",
948 FT_STRING
, BASE_NONE
, NULL
, 0x0,
952 { "Opt Desc", "coap.opt.desc",
953 FT_STRING
, BASE_NONE
, NULL
, 0x0,
956 { &hf_coap_opt_delta
,
957 { "Opt Delta", "coap.opt.delta",
958 FT_UINT8
, BASE_DEC
, NULL
, 0xf0,
961 { &hf_coap_opt_delta_ext
,
962 { "Opt Delta extended", "coap.opt.delta_ext",
963 FT_UINT16
, BASE_DEC
, NULL
, 0x0,
966 { &hf_coap_opt_length
,
967 { "Opt Length", "coap.opt.length",
968 FT_UINT8
, BASE_DEC
, NULL
, 0x0f,
969 "CoAP Option Length", HFILL
}
971 { &hf_coap_opt_length_ext
,
972 { "Opt Length extended", "coap.opt.length_ext",
973 FT_UINT16
, BASE_DEC
, NULL
, 0x0,
976 { &hf_coap_opt_end_marker
,
977 { "End of options marker", "coap.opt.end_marker",
978 FT_UINT8
, BASE_DEC
, NULL
, 0x00,
981 { &hf_coap_opt_ctype
,
982 { "Content-type", "coap.opt.ctype",
983 FT_STRING
, BASE_NONE
, NULL
, 0x0,
986 { &hf_coap_opt_max_age
,
987 { "Max-age", "coap.opt.max_age",
988 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
991 { &hf_coap_opt_proxy_uri
,
992 { "Proxy-Uri", "coap.opt.proxy_uri",
993 FT_STRING
, BASE_NONE
, NULL
, 0x0,
997 { "Etag", "coap.opt.etag",
998 FT_BYTES
, BASE_NONE
, NULL
, 0x0,
999 "CoAP Option Etag", HFILL
}
1001 { &hf_coap_opt_uri_host
,
1002 { "Uri-Host", "coap.opt.uri_host",
1003 FT_STRING
, BASE_NONE
, NULL
, 0x0,
1006 { &hf_coap_opt_location_path
,
1007 { "Location-Path", "coap.opt.location_path",
1008 FT_STRING
, BASE_NONE
, NULL
, 0x0,
1011 { &hf_coap_opt_uri_port
,
1012 { "Uri-Port", "coap.opt.uri_port",
1013 FT_UINT16
, BASE_DEC
, NULL
, 0x0,
1016 { &hf_coap_opt_location_query
,
1017 { "Location-Query", "coap.opt.location_query",
1018 FT_STRING
, BASE_NONE
, NULL
, 0x0,
1021 { &hf_coap_opt_uri_path
,
1022 { "Uri-Path", "coap.opt.uri_path",
1023 FT_STRING
, BASE_NONE
, NULL
, 0x0,
1026 { &hf_coap_opt_observe
,
1027 { "Lifetime", "coap.opt.subscr_lifetime",
1028 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
1031 { &hf_coap_opt_accept
,
1032 { "Accept", "coap.opt.accept",
1033 FT_STRING
, BASE_NONE
, NULL
, 0x0,
1036 { &hf_coap_opt_if_match
,
1037 { "If-Match", "coap.opt.if_match",
1038 FT_BYTES
, BASE_NONE
, NULL
, 0x0,
1041 { &hf_coap_opt_block_number
,
1042 { "Block Number", "coap.opt.block_number",
1043 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
1046 { &hf_coap_opt_block_mflag
,
1047 { "More Flag", "coap.opt.block_mflag",
1048 FT_UINT8
, BASE_DEC
, NULL
, 0x08,
1051 { &hf_coap_opt_block_size
,
1052 { "Encoded Block Size", "coap.opt.block_size",
1053 FT_UINT8
, BASE_DEC
, NULL
, 0x07,
1056 { &hf_coap_opt_uri_query
,
1057 { "Uri-Query", "coap.opt.uri_query",
1058 FT_STRING
, BASE_NONE
, NULL
, 0x0,
1061 { &hf_coap_opt_unknown
,
1062 { "Unknown", "coap.opt.unknown",
1063 FT_BYTES
, BASE_NONE
, NULL
, 0x0,
1068 static gint
*ett
[] = {
1074 static ei_register_info ei
[] = {
1075 { &ei_coap_invalid_option_number
,
1076 { "coap.invalid_option_number", PI_MALFORMED
, PI_WARN
, "Invalid Option Number", EXPFILL
}},
1077 { &ei_coap_invalid_option_range
,
1078 { "coap.invalid_option_range", PI_MALFORMED
, PI_WARN
, "Invalid Option Range", EXPFILL
}},
1079 { &ei_coap_option_length_bad
,
1080 { "coap.option_length_bad", PI_MALFORMED
, PI_WARN
, "Option length bad", EXPFILL
}},
1083 module_t
*coap_module
;
1084 expert_module_t
*expert_coap
;
1086 proto_coap
= proto_register_protocol("Constrained Application Protocol", "CoAP", "coap");
1087 proto_register_field_array(proto_coap
, hf
, array_length(hf
));
1088 proto_register_subtree_array(ett
, array_length(ett
));
1089 expert_coap
= expert_register_protocol(proto_coap
);
1090 expert_register_field_array(expert_coap
, ei
, array_length(ei
));
1092 register_dissector("coap", dissect_coap
, proto_coap
);
1094 /* Register our configuration options */
1095 coap_module
= prefs_register_protocol (proto_coap
, proto_reg_handoff_coap
);
1097 prefs_register_uint_preference (coap_module
, "udp_port",
1099 "Port number used for CoAP traffic",
1100 10, &global_coap_port_number
);
1104 proto_reg_handoff_coap(void)
1106 static gboolean coap_prefs_initialized
= FALSE
;
1107 static dissector_handle_t coap_handle
;
1108 static guint coap_port_number
;
1110 if (!coap_prefs_initialized
) {
1111 coap_handle
= find_dissector("coap");
1112 media_type_dissector_table
= find_dissector_table("media_type");
1113 coap_prefs_initialized
= TRUE
;
1115 dissector_delete_uint("udp.port", coap_port_number
, coap_handle
);
1116 dissector_delete_uint("tcp.port", coap_port_number
, coap_handle
);
1119 coap_port_number
= global_coap_port_number
;
1120 dissector_add_uint("udp.port", coap_port_number
, coap_handle
);
1121 dissector_add_uint("tcp.port", coap_port_number
, coap_handle
);
1125 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1130 * indent-tabs-mode: t
1133 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
1134 * :indentSize=8:tabSize=8:noTabs=false: