MSWSP: fix dissect_mswsp_smb()
[wireshark-wip.git] / epan / dissectors / packet-coap.c
blob40d60d89b67a3db21b51bb0059fb17087cb8eb7a
1 /* packet-coap.c
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>
12 * $Id$
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.
33 #include "config.h"
35 #include <glib.h>
37 #include <epan/packet.h>
38 #include <epan/prefs.h>
39 #include <expert.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];
105 * Transaction Type
107 static const value_string vals_ttype[] = {
108 { 0, "Confirmable" },
109 { 1, "Non-Confirmable" },
110 { 2, "Acknowledgement" },
111 { 3, "Reset" },
112 { 0, NULL },
114 static const value_string vals_ttype_short[] = {
115 { 0, "CON" },
116 { 1, "NON" },
117 { 2, "ACK" },
118 { 3, "RST" },
119 { 0, NULL },
123 * Method Code
124 * Response Code
126 static const value_string vals_code[] = {
127 { 0, "Empty Message" },
129 /* method code */
130 { 1, "GET" },
131 { 2, "POST" },
132 { 3, "PUT" },
133 { 4, "DELETE" },
135 /* response code */
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" },
159 { 0, NULL },
163 * Option Headers
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" },
203 { 0, NULL },
206 struct coap_option_range_t {
207 guint type;
208 gint min;
209 gint max;
210 } coi[] = {
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 },
228 { 0, 0, 0 },
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" },
238 { 0, NULL },
241 static const char *nullstr = "(null)";
243 void proto_reg_handoff_coap(void);
245 static int
246 coap_is_str_ipv6addr(guint8 *str)
248 size_t len = strlen(str);
249 int colon = 0;
251 while (len--) {
252 if (*str++ == ':')
253 colon++;
256 return colon > 1 ? 1 : 0;
259 static gint
260 coap_get_opt_uint(tvbuff_t *tvb, gint offset, gint length)
262 switch (length) {
263 case 0:
264 return 0;
265 case 1:
266 return (guint)tvb_get_guint8(tvb, offset);
267 case 2:
268 return (guint)tvb_get_ntohs(tvb, offset);
269 case 3:
270 return (guint)tvb_get_ntoh24(tvb, offset);
271 case 4:
272 return (guint)tvb_get_ntohl(tvb, offset);
273 default:
274 return -1;
278 static gint
279 coap_opt_check(packet_info *pinfo, proto_tree *subtree, guint opt_num, gint opt_length)
281 int i;
283 for (i = 0; i < (int)(array_length(coi)); i++) {
284 if (coi[i].type == opt_num)
285 break;
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);
290 return -1;
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);
297 return 0;
300 static void
301 dissect_coap_opt_hex_string(tvbuff_t *tvb, proto_item *item, proto_tree *subtree, gint offset, gint opt_length, int hf)
303 const guint8 *str;
305 if (opt_length == 0)
306 str = nullstr;
307 else
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);
316 static void
317 dissect_coap_opt_uint(tvbuff_t *tvb, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length, int hf)
319 guint i = 0;
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);
331 static void
332 dissect_coap_opt_uri_host(tvbuff_t *tvb, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length)
334 guint8 *str;
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));
353 } else
354 g_strlcat(coap_uri_str, str, sizeof(coap_uri_str));
357 static void
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) {
365 str = nullstr;
366 } else {
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);
377 static void
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));
384 else
385 g_strlcat(coap_uri_query, "&", sizeof(coap_uri_str));
387 if (opt_length == 0) {
388 str = nullstr;
389 } else {
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);
400 static void
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) {
406 str = nullstr;
407 } else {
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);
417 static void
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) {
423 str = nullstr;
424 } else {
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);
434 static void
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) {
440 str = nullstr;
441 } else {
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);
451 static void
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;
456 } else {
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);
468 static void
469 dissect_coap_opt_block(tvbuff_t *tvb, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length)
471 guint8 val = 0;
472 guint encoded_block_size;
473 guint block_esize;
475 if (opt_length == 0) {
476 coap_block_number = 0;
477 val = 0;
478 } else {
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);
491 /* block size */
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);
502 static void
503 dissect_coap_opt_uri_port(tvbuff_t *tvb, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length)
505 guint port = 0;
506 char portstr[6];
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).
528 static int
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)
531 guint8 opt_jump;
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;
538 proto_tree *subtree;
539 proto_item *item;
540 char strbuf[56];
542 opt_jump = tvb_get_guint8(tvb, offset);
543 if (0xff == opt_jump)
544 return offset;
545 offset += 1;
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
551 * constructs:
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) {
564 case 0xd0:
565 opt_delta_ext = tvb_get_guint8(tvb, offset);
566 opt_delta_ext_off = offset;
567 opt_delta_ext_len = 1;
568 offset += 1;
570 opt_delta = 13;
571 opt_delta += opt_delta_ext;
572 break;
573 case 0xe0:
574 opt_delta_ext = coap_get_opt_uint(tvb, offset, 2);
575 opt_delta_ext_off = offset;
576 opt_delta_ext_len = 2;
577 offset += 2;
579 opt_delta = 269;
580 opt_delta += opt_delta_ext;
581 break;
582 case 0xf0:
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");
585 return -1;
586 default:
587 opt_delta = ((opt_jump & 0xf0) >> 4);
588 break;
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) {
608 case 0x0d:
609 opt_length_ext = tvb_get_guint8(tvb, offset);
610 opt_length_ext_off = offset;
611 opt_length_ext_len = 1;
612 offset += 1;
614 opt_length = 13;
615 opt_length += opt_length_ext;
616 break;
617 case 0x0e:
618 opt_length_ext = coap_get_opt_uint(tvb, offset, 2);
619 opt_length_ext_off = offset;
620 opt_length_ext_len = 2;
621 offset += 2;
623 opt_length = 269;
624 opt_length += opt_length_ext;
625 break;
626 case 0x0f:
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");
629 return -1;
630 default:
631 opt_length = (opt_jump & 0x0f);
632 break;
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");
637 return -1;
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 */
667 switch (*opt_num) {
668 case COAP_OPT_CONTENT_TYPE:
669 dissect_coap_opt_ctype(tvb, item, subtree, offset,
670 opt_length, hf_coap_opt_ctype);
671 break;
672 case COAP_OPT_MAX_AGE:
673 dissect_coap_opt_uint(tvb, item, subtree, offset,
674 opt_length, hf_coap_opt_max_age);
675 break;
676 case COAP_OPT_PROXY_URI:
677 dissect_coap_opt_proxy_uri(tvb, item, subtree, offset,
678 opt_length);
679 break;
680 case COAP_OPT_ETAG:
681 dissect_coap_opt_hex_string(tvb, item, subtree, offset,
682 opt_length, hf_coap_opt_etag);
683 break;
684 case COAP_OPT_URI_HOST:
685 dissect_coap_opt_uri_host(tvb, item, subtree, offset,
686 opt_length);
687 break;
688 case COAP_OPT_LOCATION_PATH:
689 dissect_coap_opt_location_path(tvb, item, subtree, offset,
690 opt_length);
691 break;
692 case COAP_OPT_URI_PORT:
693 dissect_coap_opt_uri_port(tvb, item, subtree, offset,
694 opt_length);
695 break;
696 case COAP_OPT_LOCATION_QUERY:
697 dissect_coap_opt_location_query(tvb, item, subtree, offset,
698 opt_length);
699 break;
700 case COAP_OPT_URI_PATH:
701 dissect_coap_opt_uri_path(tvb, item, subtree, offset,
702 opt_length);
703 break;
704 case COAP_OPT_OBSERVE:
705 dissect_coap_opt_uint(tvb, item, subtree, offset,
706 opt_length, hf_coap_opt_observe);
707 break;
708 case COAP_OPT_ACCEPT:
709 dissect_coap_opt_ctype(tvb, item, subtree, offset,
710 opt_length, hf_coap_opt_accept);
711 break;
712 case COAP_OPT_IF_MATCH:
713 dissect_coap_opt_hex_string(tvb, item, subtree, offset,
714 opt_length, hf_coap_opt_if_match);
715 break;
716 case COAP_OPT_URI_QUERY:
717 dissect_coap_opt_uri_query(tvb, item, subtree, offset,
718 opt_length);
719 break;
720 case COAP_OPT_BLOCK2:
721 dissect_coap_opt_block(tvb, item, subtree, offset,
722 opt_length);
723 break;
724 case COAP_OPT_BLOCK1:
725 dissect_coap_opt_block(tvb, item, subtree, offset,
726 opt_length);
727 break;
728 case COAP_OPT_IF_NONE_MATCH:
729 break;
730 case COAP_OPT_BLOCK_SIZE:
731 dissect_coap_opt_uint(tvb, item, subtree, offset,
732 opt_length, hf_coap_opt_block_size);
733 break;
734 default:
735 dissect_coap_opt_hex_string(tvb, item, subtree, offset,
736 opt_length, hf_coap_opt_unknown);
737 break;
740 return offset + opt_length;
744 * options dissector.
745 * return offset pointing the next of options. (i.e. the top of the paylaod
746 * or the end of the data.
748 static int
749 dissect_coap_options(tvbuff_t *tvb, packet_info *pinfo, proto_tree *coap_tree, gint offset, gint coap_length)
751 guint opt_num = 0;
752 int i;
753 guint8 endmarker;
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);
759 if (offset == -1)
760 return -1;
761 if (offset >= coap_length)
762 break;
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);
766 offset += 1;
767 break;
771 return offset;
774 static void
775 dissect_coap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
777 gint offset = 0;
778 proto_item *coap_root;
779 proto_tree *coap_tree;
780 guint8 ttype;
781 guint8 token_len;
782 guint8 code;
783 guint16 mid;
784 gint coap_length;
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);
793 coap_ctype_str = "";
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;
806 offset += 1;
808 proto_tree_add_item(coap_tree, hf_coap_code, tvb, offset, 1, ENC_BIG_ENDIAN);
809 code = tvb_get_guint8(tvb, offset);
810 offset += 1;
812 proto_tree_add_item(coap_tree, hf_coap_mid, tvb, offset, 2, ENC_BIG_ENDIAN);
813 mid = tvb_get_ntohs(tvb, offset);
814 offset += 2;
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';
826 if (token_len > 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);
831 offset += token_len;
834 /* process options */
835 offset = dissect_coap_options(tvb, pinfo, coap_tree, offset, coap_length);
836 if (offset == -1)
837 return;
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,
882 str_payload);
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";
890 } else {
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
902 void
903 proto_register_coap(void)
905 static hf_register_info hf[] = {
906 { &hf_coap_version,
907 { "Version", "coap.version",
908 FT_UINT8, BASE_DEC, NULL, 0xc0,
909 NULL, HFILL }
911 { &hf_coap_ttype,
912 { "Type", "coap.type",
913 FT_UINT8, BASE_DEC, VALS(vals_ttype), 0x30,
914 NULL, HFILL }
916 { &hf_coap_token_len,
917 { "Token Length", "coap.token_len",
918 FT_UINT8, BASE_DEC, NULL, 0x0f,
919 NULL, HFILL }
921 { &hf_coap_token,
922 { "Token", "coap.token",
923 FT_BYTES, BASE_NONE, NULL, 0x0,
924 NULL, HFILL }
926 { &hf_coap_code,
927 { "Code", "coap.code",
928 FT_UINT8, BASE_DEC, VALS(vals_code), 0x0,
929 NULL, HFILL }
931 { &hf_coap_mid,
932 { "Message ID", "coap.mid",
933 FT_UINT16, BASE_DEC, NULL, 0x0,
934 NULL, HFILL }
936 { &hf_coap_payload,
937 { "Payload", "coap.payload",
938 FT_STRING, BASE_NONE, NULL, 0x0,
939 NULL, HFILL }
941 { &hf_coap_payload_desc,
942 { "Payload Desc", "coap.opt.payload_desc",
943 FT_STRING, BASE_NONE, NULL, 0x0,
944 NULL, HFILL }
946 { &hf_coap_opt_name,
947 { "Opt Name", "coap.opt.name",
948 FT_STRING, BASE_NONE, NULL, 0x0,
949 NULL, HFILL }
951 { &hf_coap_opt_desc,
952 { "Opt Desc", "coap.opt.desc",
953 FT_STRING, BASE_NONE, NULL, 0x0,
954 NULL, HFILL }
956 { &hf_coap_opt_delta,
957 { "Opt Delta", "coap.opt.delta",
958 FT_UINT8, BASE_DEC, NULL, 0xf0,
959 NULL, HFILL }
961 { &hf_coap_opt_delta_ext,
962 { "Opt Delta extended", "coap.opt.delta_ext",
963 FT_UINT16, BASE_DEC, NULL, 0x0,
964 NULL, HFILL }
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,
974 NULL, HFILL }
976 { &hf_coap_opt_end_marker,
977 { "End of options marker", "coap.opt.end_marker",
978 FT_UINT8, BASE_DEC, NULL, 0x00,
979 NULL, HFILL }
981 { &hf_coap_opt_ctype,
982 { "Content-type", "coap.opt.ctype",
983 FT_STRING, BASE_NONE, NULL, 0x0,
984 NULL, HFILL }
986 { &hf_coap_opt_max_age,
987 { "Max-age", "coap.opt.max_age",
988 FT_UINT32, BASE_DEC, NULL, 0x0,
989 NULL, HFILL }
991 { &hf_coap_opt_proxy_uri,
992 { "Proxy-Uri", "coap.opt.proxy_uri",
993 FT_STRING, BASE_NONE, NULL, 0x0,
994 NULL, HFILL }
996 { &hf_coap_opt_etag,
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,
1004 NULL, HFILL }
1006 { &hf_coap_opt_location_path,
1007 { "Location-Path", "coap.opt.location_path",
1008 FT_STRING, BASE_NONE, NULL, 0x0,
1009 NULL, HFILL }
1011 { &hf_coap_opt_uri_port,
1012 { "Uri-Port", "coap.opt.uri_port",
1013 FT_UINT16, BASE_DEC, NULL, 0x0,
1014 NULL, HFILL }
1016 { &hf_coap_opt_location_query,
1017 { "Location-Query", "coap.opt.location_query",
1018 FT_STRING, BASE_NONE, NULL, 0x0,
1019 NULL, HFILL }
1021 { &hf_coap_opt_uri_path,
1022 { "Uri-Path", "coap.opt.uri_path",
1023 FT_STRING, BASE_NONE, NULL, 0x0,
1024 NULL, HFILL }
1026 { &hf_coap_opt_observe,
1027 { "Lifetime", "coap.opt.subscr_lifetime",
1028 FT_UINT32, BASE_DEC, NULL, 0x0,
1029 NULL, HFILL }
1031 { &hf_coap_opt_accept,
1032 { "Accept", "coap.opt.accept",
1033 FT_STRING, BASE_NONE, NULL, 0x0,
1034 NULL, HFILL }
1036 { &hf_coap_opt_if_match,
1037 { "If-Match", "coap.opt.if_match",
1038 FT_BYTES, BASE_NONE, NULL, 0x0,
1039 NULL, HFILL }
1041 { &hf_coap_opt_block_number,
1042 { "Block Number", "coap.opt.block_number",
1043 FT_UINT32, BASE_DEC, NULL, 0x0,
1044 NULL, HFILL }
1046 { &hf_coap_opt_block_mflag,
1047 { "More Flag", "coap.opt.block_mflag",
1048 FT_UINT8, BASE_DEC, NULL, 0x08,
1049 NULL, HFILL }
1051 { &hf_coap_opt_block_size,
1052 { "Encoded Block Size", "coap.opt.block_size",
1053 FT_UINT8, BASE_DEC, NULL, 0x07,
1054 NULL, HFILL }
1056 { &hf_coap_opt_uri_query,
1057 { "Uri-Query", "coap.opt.uri_query",
1058 FT_STRING, BASE_NONE, NULL, 0x0,
1059 NULL, HFILL }
1061 { &hf_coap_opt_unknown,
1062 { "Unknown", "coap.opt.unknown",
1063 FT_BYTES, BASE_NONE, NULL, 0x0,
1064 NULL, HFILL }
1068 static gint *ett[] = {
1069 &ett_coap,
1070 &ett_coap_option,
1071 &ett_coap_payload,
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",
1098 "CoAP port number",
1099 "Port number used for CoAP traffic",
1100 10, &global_coap_port_number);
1103 void
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;
1114 } else {
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
1127 * Local variables:
1128 * c-basic-offset: 8
1129 * tab-width: 8
1130 * indent-tabs-mode: t
1131 * End:
1133 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
1134 * :indentSize=8:tabSize=8:noTabs=false: