31c3306f82d2a061a5a708dc239f4b7ef58a6ebc
[obnox/wireshark/wip.git] / epan / dissectors / packet-coap.c
1 #include <stdio.h>
2 /* packet-coap.c
3  * Routines for COAP packet disassembly
4  * draft-ietf-core-coap-07.txt
5  * draft-ietf-core-block-04.txt
6  * draft-ietf-core-observe-02.txt
7  * draft-ietf-core-link-format-06.txt
8  * Shoichi Sakane <sakane@tanu.org>
9  *
10  * $Id$
11  *
12  * Wireshark - Network traffic analyzer
13  * By Gerald Combs <gerald@wireshark.org>
14  * Copyright 1998 Gerald Combs
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <glib.h>
36
37 #include <epan/packet.h>
38 #include <epan/prefs.h>
39 #include <expert.h>
40
41 static dissector_table_t media_type_dissector_table;
42
43 static int proto_coap = -1;
44
45 static int hf_coap_version              = -1;
46 static int hf_coap_ttype                = -1;
47 static int hf_coap_opt_count            = -1;
48 static int hf_coap_code                 = -1;
49 static int hf_coap_tid                  = -1;
50 static int hf_coap_opt_delta            = -1;
51 static int hf_coap_opt_length           = -1;
52 static int hf_coap_opt_ctype            = -1;
53 static int hf_coap_opt_max_age          = -1;
54 static int hf_coap_opt_proxy_uri        = -1;
55 static int hf_coap_opt_etag             = -1;
56 static int hf_coap_opt_uri_host         = -1;
57 static int hf_coap_opt_location_path    = -1;
58 static int hf_coap_opt_uri_port         = -1;
59 static int hf_coap_opt_location_query   = -1;
60 static int hf_coap_opt_uri_path         = -1;
61 static int hf_coap_opt_observe          = -1;
62 static int hf_coap_opt_token            = -1;
63 static int hf_coap_opt_accept           = -1;
64 static int hf_coap_opt_if_match         = -1;
65 static int hf_coap_opt_block_number     = -1;
66 static int hf_coap_opt_block_mflag      = -1;
67 static int hf_coap_opt_block_size       = -1;
68 static int hf_coap_opt_uri_query        = -1;
69 static int hf_coap_opt_if_none_match    = -1;
70
71 static gint ett_coap                    = -1;
72 static gint ett_coap_option             = -1;
73 static gint ett_coap_payload            = -1;
74
75 /* COAP's IANA-assigned port number */
76 #define DEFAULT_COAP_PORT       5683
77
78 static const gchar *coap_content_type = NULL;
79 static gint coap_content_type_value = ~0;
80 static guint global_coap_port_number = DEFAULT_COAP_PORT;
81
82 static gint block_number = ~0;
83 static guint block_mflag = 0;
84 static gchar uri_string[256]; /* 256 is probably enough to display in the screen */
85
86 /*
87  * Transaction Type
88  */
89 static const value_string vals_ttype[] = {
90         { 0, "Confirmable" },
91         { 1, "Non-Confirmable" },
92         { 2, "Acknowledgement" },
93         { 3, "Reset" },
94         { 0, NULL },
95 };
96
97 /*
98  * Method Code
99  * Response Code
100  */
101 static const value_string vals_code[] = {
102         { 0, "Empty Message" },
103
104         /* method code */
105         { 1, "GET" },
106         { 2, "POST" },
107         { 3, "PUT" },
108         { 4, "DELETE" },
109
110         /* response code */
111         {  65, "2.01 Created" },
112         {  66, "2.02 Deleted" },
113         {  67, "2.03 Valid" },
114         {  68, "2.04 Changed" },
115         {  69, "2.05 Content" },
116         { 128, "4.00 Bad Request" },
117         { 129, "4.01 Unauthorized" },
118         { 130, "4.02 Bad Option" },
119         { 131, "4.03 Forbidden" },
120         { 132, "4.04 Not Found" },
121         { 133, "4.05 Method Not Allowed" },
122         { 136, "4.08 Request Entity Incomplete" },      /* core-block-03 */
123         { 140, "4.12 Precondition Failed" },
124         { 141, "4.13 Request Entity Too Large" },
125         { 143, "4.15 Unsupported Media Type" },
126         { 160, "5.00 Internal Server Error" },
127         { 161, "5.01 Not Implemented" },
128         { 162, "5.02 Bad Gateway" },
129         { 163, "5.03 Service Unavailable" },
130         { 164, "5.04 Gateway Timeout" },
131         { 165, "5.05 Proxying Not Supported" },
132
133         { 0, NULL },
134 };
135
136 /*
137  * Option Headers
138  * No-Option must not be included in this structure, is handled in the function
139  * of the dissector, especially.
140  */
141 #define COAP_OPT_CONTENT_TYPE   1
142 #define COAP_OPT_MAX_AGE        2
143 #define COAP_OPT_PROXY_URI      3
144 #define COAP_OPT_ETAG           4
145 #define COAP_OPT_URI_HOST       5
146 #define COAP_OPT_LOCATION_PATH  6
147 #define COAP_OPT_URI_PORT       7
148 #define COAP_OPT_LOCATION_QUERY 8
149 #define COAP_OPT_URI_PATH       9
150 #define COAP_OPT_OBSERVE        10      /* core-observe */
151 #define COAP_OPT_TOKEN          11
152 #define COAP_OPT_ACCEPT         12
153 #define COAP_OPT_IF_MATCH       13
154 #define COAP_OPT_URI_QUERY      15
155 #define COAP_OPT_BLOCK2         17      /* core-block-03 */
156 #define COAP_OPT_BLOCK1         19      /* core-block-03 */
157 #define COAP_OPT_IF_NONE_MATCH  21
158
159 static const value_string vals_opt_type[] = {
160         { COAP_OPT_CONTENT_TYPE, "Content-Type" },
161         { COAP_OPT_MAX_AGE, "Max-age" },
162         { COAP_OPT_PROXY_URI, "Proxy-Uri" },
163         { COAP_OPT_ETAG, "Etag" },
164         { COAP_OPT_URI_HOST, "Uri-Host" },
165         { COAP_OPT_LOCATION_PATH, "Location-Path" },
166         { COAP_OPT_URI_PORT, "Uri-Port" },
167         { COAP_OPT_LOCATION_QUERY, "Location-Query" },
168         { COAP_OPT_URI_PATH, "Uri-Path" },
169         { COAP_OPT_OBSERVE, "Observe" },
170         { COAP_OPT_TOKEN, "Token" },
171         { COAP_OPT_ACCEPT, "Accept" },
172         { COAP_OPT_IF_MATCH, "If-Match" },
173         { COAP_OPT_URI_QUERY, "Uri-Query" },
174         { COAP_OPT_BLOCK2, "Block2" },
175         { COAP_OPT_BLOCK1, "Block1" },
176         { COAP_OPT_IF_NONE_MATCH, "If-None-Match" },
177         { 0, NULL },
178 };
179
180 static const value_string vals_ctype[] = {
181         { 0, "text/plain" },
182         { 40, "application/link-format" },
183         { 41, "application/xml" },
184         { 42, "application/octet-stream" },
185         { 47, "application/exi" },
186         { 50, "application/json" },
187         { 0, NULL },
188 };
189
190 void proto_reg_handoff_coap(void);
191
192 static int
193 coap_is_str_ipv6addr(guint8 *str)
194 {
195         size_t len = strlen(str);
196         int colon = 0;
197
198         while (len--) {
199                 if (*str++ == ':')
200                         colon++;
201         }
202
203         return colon > 1 ? 1 : 0;
204 }
205
206 static void
207 dissect_coap_opt_string(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *subtree, gint offset, gint opt_length, int hfindex, int opt_code)
208 {
209         guint8 *hoststr = NULL;
210
211         proto_tree_add_item(subtree, hfindex, tvb, offset, opt_length, FALSE);
212
213         /* forming a uri-string */
214         switch (opt_code) {
215         case COAP_OPT_URI_HOST:
216                 g_strlcat(uri_string, "coap://", sizeof(uri_string));
217                 hoststr = tvb_get_ephemeral_string(tvb, offset, opt_length);
218                 /* if the string looks an IPv6 address, it has to be enclosed by brackets. */
219                 if (coap_is_str_ipv6addr(hoststr)) {
220                         g_strlcat(uri_string, "[", sizeof(uri_string));
221                         g_strlcat(uri_string, hoststr, sizeof(uri_string));
222                         g_strlcat(uri_string, "]", sizeof(uri_string));
223                 } else
224                         g_strlcat(uri_string, hoststr, sizeof(uri_string));
225                 break;
226         case COAP_OPT_URI_PATH:
227                 g_strlcat(uri_string, "/", sizeof(uri_string));
228                 g_strlcat(uri_string, tvb_get_ephemeral_string(tvb, offset, opt_length), sizeof(uri_string));
229                 break;
230         case COAP_OPT_URI_QUERY:
231                 g_strlcat(uri_string, "/?", sizeof(uri_string));
232                 g_strlcat(uri_string, tvb_get_ephemeral_string(tvb, offset, opt_length), sizeof(uri_string));
233                 break;
234         }
235 }
236
237 static void
238 dissect_coap_opt_ctype(tvbuff_t *tvb, packet_info *pinfo, proto_tree *subtree, gint offset, gint opt_length, int hfindex)
239 {
240         guint32 opt_ctype = 0;
241
242         if (opt_length != 1) {
243                 expert_add_info_format(pinfo, subtree, PI_MALFORMED, PI_WARN, "Invalid Option Length: %d", opt_length);
244                 return;
245         }
246
247         opt_ctype = tvb_get_guint8(tvb, offset);
248         coap_content_type_value = (gint)opt_ctype;
249         coap_content_type = val_to_str(opt_ctype, vals_ctype, "Unknown %d");
250
251         proto_tree_add_item(subtree, hfindex, tvb, offset, 1, FALSE);
252 }
253
254 static void
255 dissect_coap_opt_time(tvbuff_t *tvb, packet_info *pinfo, proto_tree *subtree, gint offset, gint opt_length, int hfindex)
256 {
257         proto_item *item = NULL;
258
259         if (opt_length <= 0 || opt_length > 4) {
260                 expert_add_info_format(pinfo, subtree, PI_MALFORMED, PI_WARN, "Invalid Option Length: %d", opt_length);
261                 return;
262         }
263
264         item = proto_tree_add_item(subtree, hfindex, tvb, offset, opt_length, FALSE);
265         proto_item_append_text(item, " (s)");
266
267         return;
268 }
269
270 static void
271 dissect_coap_opt_block(tvbuff_t *tvb, packet_info *pinfo, proto_tree *subtree, gint offset, gint opt_length, int hfindex)
272 {
273         guint8 val = 0;
274         guint encoded_block_size = 0;
275         guint block_size;
276         proto_item *item = NULL;
277
278         switch (opt_length) {
279         case 1:
280                 block_number = (guint)(tvb_get_guint8(tvb, offset) >> 4);
281                 break;
282         case 2:
283                 block_number = (guint)(tvb_get_ntohs(tvb, offset) >> 4);
284                 break;
285         case 3:
286                 block_number = (guint)(tvb_get_ntoh24(tvb, offset) >> 4);
287                 break;
288         default:
289                 expert_add_info_format(pinfo, subtree, PI_MALFORMED, PI_WARN, "Invalid Option Length: %d", opt_length);
290                 return;
291         }
292
293         val = tvb_get_guint8(tvb, offset + opt_length - 1) & 0x0f;
294         encoded_block_size = val & 0x07;
295         block_mflag = val & 0x08;
296
297         proto_tree_add_int(subtree, hf_coap_opt_block_number, tvb, offset, opt_length, block_number);
298         proto_tree_add_item(subtree, hfindex, tvb, offset + opt_length - 1, 1, FALSE);
299
300         block_size = 1 << (encoded_block_size + 4);
301         item = proto_tree_add_item(subtree, hf_coap_opt_block_size, tvb, offset + opt_length - 1, 1, ENC_BIG_ENDIAN);
302         proto_item_append_text(item, ", Result: %d", block_size);
303 }
304
305 static void
306 dissect_coap_opt_port(tvbuff_t *tvb, packet_info *pinfo, proto_tree *subtree, gint offset, gint opt_length, int hfindex)
307 {
308         proto_item *item = NULL;
309         char portstr[6];
310
311         memset(portstr, '\0', sizeof(portstr));
312
313         switch (opt_length) {
314         case 0:
315                 item = proto_tree_add_uint(subtree, hfindex, tvb, offset, opt_length, pinfo->destport);
316                 proto_item_append_text(item, " (default)");
317                 return;
318         case 1:
319                 g_snprintf(portstr, sizeof(portstr), "%u", tvb_get_guint8(tvb, offset));
320                 break;
321         case 2:
322                 g_snprintf(portstr, sizeof(portstr), "%u", tvb_get_ntohs(tvb, offset));
323                 break;
324         default:
325                 expert_add_info_format(pinfo, subtree, PI_MALFORMED, PI_WARN, "Invalid Option Length: %d", opt_length);
326                 return;
327         }
328         (void)proto_tree_add_item(subtree, hfindex, tvb, offset, opt_length, FALSE);
329
330         /* forming a uri-string */
331         if (uri_string[0] == '\0')
332                 g_strlcat(uri_string, ep_address_to_str(&pinfo->net_dst), sizeof(uri_string));
333         g_strlcat(uri_string, ":", sizeof(uri_string));
334         g_strlcat(uri_string, portstr, sizeof(uri_string));
335
336         return;
337 }
338
339 /*
340  * dissector for each option of COAP.
341  * return the total length of the option including the header (e.g. delta and length).
342  */
343 static int
344 dissect_coap_options(tvbuff_t *tvb, packet_info *pinfo, proto_tree *coap_tree, gint offset, guint8 opt_count, guint8 *opt_code)
345 {
346         guint8 opt_delta;
347         gint opt_length;
348         proto_tree *subtree = NULL;
349         proto_item *item = NULL;
350         gint opt_hlen = 0;
351         tvbuff_t *tvb_lenbuf = NULL;
352
353         opt_delta = (tvb_get_guint8(tvb, offset) & 0xf0) >> 4;
354         *opt_code += opt_delta;
355
356         /*
357          * Length:
358          *   Normally Length is a 4-bit unsigned integer
359          *   allowing values of 0-14 octets.  When the length is 15 or more,
360          *   another byte is added as an 8-bit unsigned integer plus 15
361          *   allowing values of 15-270 octets.
362          */
363         opt_length = (tvb_get_guint8(tvb, offset) & 0x0f);
364         opt_hlen = 1;
365         if (opt_length == 0x0f) {
366                 opt_length += tvb_get_guint8(tvb, offset + 1);
367                 opt_hlen = 2;
368         }
369
370         item = proto_tree_add_text(coap_tree, tvb, offset, opt_hlen + opt_length,
371                                    "Option #%u: %s (Type: %u)",
372                                    opt_count, val_to_str(*opt_code, vals_opt_type, *opt_code % 14 == 0 ? "No-Op" : "Unknown Option"), *opt_code);
373
374         subtree = proto_item_add_subtree(item, ett_coap_option);
375         proto_tree_add_item(subtree, hf_coap_opt_delta, tvb, offset, 1, ENC_BIG_ENDIAN);
376
377         tvb_lenbuf = tvb_new_subset(tvb, offset, opt_hlen, opt_hlen);
378         proto_tree_add_uint_bits_format_value(subtree, hf_coap_opt_length, tvb_lenbuf, 4, opt_hlen == 1 ? 4 : 12, opt_length, "%d", opt_length);
379         offset += opt_hlen;
380
381         switch (*opt_code) {
382         case COAP_OPT_CONTENT_TYPE:
383                 dissect_coap_opt_ctype(tvb, pinfo, subtree, offset, opt_length, hf_coap_opt_ctype);
384                 break;
385         case COAP_OPT_MAX_AGE:
386                 dissect_coap_opt_time(tvb, pinfo, subtree, offset, opt_length, hf_coap_opt_max_age);
387                 break;
388         case COAP_OPT_PROXY_URI:
389                 dissect_coap_opt_string(tvb, pinfo, subtree, offset, opt_length, hf_coap_opt_proxy_uri, COAP_OPT_PROXY_URI);
390                 break;
391         case COAP_OPT_ETAG:
392                 dissect_coap_opt_string(tvb, pinfo, subtree, offset, opt_length, hf_coap_opt_etag, COAP_OPT_ETAG);
393                 break;
394         case COAP_OPT_URI_HOST:
395                 dissect_coap_opt_string(tvb, pinfo, subtree, offset, opt_length, hf_coap_opt_uri_host, COAP_OPT_URI_HOST);
396                 break;
397         case COAP_OPT_LOCATION_PATH:
398                 dissect_coap_opt_string(tvb, pinfo, subtree, offset, opt_length, hf_coap_opt_location_path, COAP_OPT_LOCATION_PATH);
399                 break;
400         case COAP_OPT_URI_PORT:
401                 dissect_coap_opt_port(tvb, pinfo, subtree, offset, opt_length, hf_coap_opt_uri_port);
402                 break;
403         case COAP_OPT_LOCATION_QUERY:
404                 dissect_coap_opt_string(tvb, pinfo, subtree, offset, opt_length, hf_coap_opt_location_query, COAP_OPT_LOCATION_QUERY);
405                 break;
406         case COAP_OPT_URI_PATH:
407                 dissect_coap_opt_string(tvb, pinfo, subtree, offset, opt_length, hf_coap_opt_uri_path, COAP_OPT_URI_PATH);
408                 break;
409         case COAP_OPT_OBSERVE:
410                 dissect_coap_opt_time(tvb, pinfo, subtree, offset, opt_length, hf_coap_opt_observe);
411                 break;
412         case COAP_OPT_TOKEN:
413                 dissect_coap_opt_string(tvb, pinfo, subtree, offset, opt_length, hf_coap_opt_token, COAP_OPT_TOKEN);
414                 break;
415         case COAP_OPT_ACCEPT:
416                 dissect_coap_opt_ctype(tvb, pinfo, subtree, offset, opt_length, hf_coap_opt_accept);
417                 break;
418         case COAP_OPT_IF_MATCH:
419                 dissect_coap_opt_string(tvb, pinfo, subtree, offset, opt_length, hf_coap_opt_if_match, COAP_OPT_IF_MATCH);
420                 break;
421         case COAP_OPT_URI_QUERY:
422                 dissect_coap_opt_string(tvb, pinfo, subtree, offset, opt_length, hf_coap_opt_uri_query, COAP_OPT_URI_QUERY);
423                 break;
424         case COAP_OPT_BLOCK2:
425                 dissect_coap_opt_block(tvb, pinfo, subtree, offset, opt_length, hf_coap_opt_block_mflag);
426                 break;
427         case COAP_OPT_BLOCK1:
428                 dissect_coap_opt_block(tvb, pinfo, subtree, offset, opt_length, hf_coap_opt_block_mflag);
429                 break;
430         case COAP_OPT_IF_NONE_MATCH:
431                 dissect_coap_opt_string(tvb, pinfo, subtree, offset, opt_length, hf_coap_opt_if_none_match, COAP_OPT_IF_NONE_MATCH);
432                 break;
433         default:
434                 /* In case of unknown opt_code, just ignore it here. A message is displayed beforehand. */
435                 break;
436         }
437
438         return offset + opt_length;
439 }
440
441 static void
442 dissect_coap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
443 {
444         guint offset = 0;
445         proto_item *coap_root = NULL;
446         proto_tree *coap_tree = NULL;
447         guint8 ttype = 0;
448         guint8 opt_count = 0;
449         guint8 code = 0;
450         guint16 tid = 0;
451         guint coap_length = 0;
452         guint8 opt_code = 0;
453         int i;
454
455         col_set_str(pinfo->cinfo, COL_PROTOCOL, "COAP");
456         col_clear(pinfo->cinfo, COL_INFO);
457
458         if (!parent_tree)
459                 return;
460
461         /* initialize the COAP length and the content-type */
462         /*
463          * the length of COAP message is not specified in the COAP header.
464          * It has to be from the lower layer.  the iplen of packet_info is not accurate.
465          * Currently, the length is just copied from the reported length of the tvbuffer.
466          */
467         coap_length = tvb_reported_length(tvb);
468         coap_content_type = NULL;
469         coap_content_type_value = ~0;
470
471         coap_root = proto_tree_add_item(parent_tree, proto_coap, tvb, offset, -1, ENC_NA);
472         coap_tree = proto_item_add_subtree(coap_root, ett_coap);
473
474         proto_tree_add_item(coap_tree, hf_coap_version, tvb, offset, 1, ENC_BIG_ENDIAN);
475
476         proto_tree_add_item(coap_tree, hf_coap_ttype, tvb, offset, 1, ENC_BIG_ENDIAN);
477         ttype = (tvb_get_guint8(tvb, offset) & 0x30) >> 4;
478         col_add_fstr(pinfo->cinfo, COL_INFO, "%s", val_to_str(ttype, vals_ttype, "Unknown %d"));
479
480         proto_tree_add_item(coap_tree, hf_coap_opt_count, tvb, offset, 1, ENC_BIG_ENDIAN);
481         opt_count = tvb_get_guint8(tvb, offset) & 0x0f;
482         offset += 1;
483
484         proto_tree_add_item(coap_tree, hf_coap_code, tvb, offset, 1, ENC_BIG_ENDIAN);
485         code = tvb_get_guint8(tvb, offset);
486         col_append_fstr(pinfo->cinfo, COL_INFO, ", %s", val_to_str(code, vals_code, "Unknown %d"));
487         offset += 1;
488
489         proto_tree_add_item(coap_tree, hf_coap_tid, tvb, offset, 2, ENC_BIG_ENDIAN);
490         tid = tvb_get_ntohs(tvb, offset);
491         offset += 2;
492
493         /* append the header information */
494         proto_item_append_text(coap_tree, ", TID: %u, Length: %u", tid, coap_length);
495
496         /* initialize the external value */
497         block_number = ~0;
498         block_mflag = 0;
499         uri_string[0] = 0;
500
501         /* dissect the options */
502         for (i = 1; i <= opt_count; i++) {
503                 offset = dissect_coap_options(tvb, pinfo, coap_tree, offset, i, &opt_code);
504                 if (coap_length < offset) {
505                         /* error */
506                         proto_tree_add_text(coap_tree, tvb, 0, 0, "Invalid length: coap_length(%d) < offset(%d)", coap_length, offset);
507                         return;
508                 }
509         }
510         if (block_number != ~0) {
511                 col_append_fstr(pinfo->cinfo, COL_INFO, ", %sBlock #%d", block_mflag ? "" : "End of ", block_number);
512         }
513         if (uri_string[0] != '\0') {
514                 col_append_fstr(pinfo->cinfo, COL_INFO, ", %s", uri_string);
515         }
516
517         /* dissect the payload */
518         if (coap_length > offset) {
519                 proto_tree *payload_tree = NULL;
520                 proto_item *payload_item = NULL;
521                 tvbuff_t *payload_tvb;
522                 guint payload_length = coap_length - offset;
523                 char *ctype_str_default = "";
524
525                 /*
526                  * TODO: should the content type be canonicalized ?
527                  * currently assuming it be small.
528                  */
529                 if (coap_content_type_value == ~0) {
530                         /* default: coap-02 section 3.2.1 */
531                         coap_content_type = "text/plain";
532                         ctype_str_default = " (default)";
533                 }
534
535                 payload_item = proto_tree_add_text(coap_tree, tvb, offset, -1, "Payload Content-Type: %s%s, Length: %u, offset: %u",
536                                                    coap_content_type, ctype_str_default, payload_length, offset);
537                 payload_tree = proto_item_add_subtree(payload_item, ett_coap_payload);
538                 payload_tvb = tvb_new_subset(tvb, offset, payload_length, payload_length);
539
540                 dissector_try_string(media_type_dissector_table, coap_content_type, payload_tvb, pinfo, payload_tree);
541         }
542 }
543
544 /*
545  * Protocol initialization
546  */
547 void
548 proto_register_coap(void)
549 {
550         static hf_register_info hf[] = {
551                 { &hf_coap_version, { "Version", "coap.version", FT_UINT8, BASE_DEC, NULL, 0xc0, "COAP Version", HFILL }},
552                 { &hf_coap_ttype, { "Type", "coap.type", FT_UINT8, BASE_DEC, VALS(vals_ttype), 0x30, "COAP Transaction Type", HFILL }},
553                 { &hf_coap_opt_count, { "Option Count", "coap.optcount", FT_UINT8, BASE_DEC, NULL, 0x0f, "COAP Option Count", HFILL }},
554                 { &hf_coap_code, { "Code", "coap.code", FT_UINT8, BASE_DEC, VALS(vals_code), 0x0, "COAP Method or Response Code", HFILL }},
555                 { &hf_coap_tid, { "Transaction ID", "coap.tid", FT_UINT16, BASE_DEC, NULL, 0x0, "COAP Transaction ID", HFILL }},
556                 { &hf_coap_opt_delta, { "Delta", "coap.opt.delta", FT_UINT8, BASE_DEC, NULL, 0xf0, "COAP Option Delta", HFILL }},
557                 { &hf_coap_opt_length, { "Length", "coap.opt.length", FT_UINT16, BASE_DEC, NULL, 0x0, "COAP Option Length", HFILL }},
558                 { &hf_coap_opt_ctype, { "Content-type", "coap.opt.ctype", FT_UINT8, BASE_DEC, VALS(vals_ctype), 0x0, "COAP Content Type", HFILL }},
559                 { &hf_coap_opt_max_age, { "Max-age", "coap.opt.max_age", FT_UINT32, BASE_DEC, NULL, 0x0, "COAP Max-age", HFILL }},
560                 { &hf_coap_opt_proxy_uri, { "Proxy-Uri", "coap.opt.proxy_uri", FT_STRING, BASE_NONE, NULL, 0x0, "COAP Proxy-Uri", HFILL }},
561                 { &hf_coap_opt_etag, { "Etag", "coap.opt.etag", FT_BYTES, BASE_NONE, NULL, 0x0, "COAP Etag", HFILL }},
562                 { &hf_coap_opt_uri_host, { "Uri-Host", "coap.opt.uri_host", FT_STRING, BASE_NONE, NULL, 0x0, "COAP Uri-Host", HFILL }},
563                 { &hf_coap_opt_location_path, { "Location-Path", "coap.opt.location_path", FT_STRING, BASE_NONE, NULL, 0x0, "COAP URI Path", HFILL }},
564                 { &hf_coap_opt_uri_port, { "Uri-Port", "coap.opt.uri_port", FT_UINT16, BASE_DEC, NULL, 0x0, "COAP Uri-Port", HFILL }},
565                 { &hf_coap_opt_location_query, { "Location-Query", "coap.opt.location_query", FT_STRING, BASE_NONE, NULL, 0x0, "COAP URI Query", HFILL }},
566                 { &hf_coap_opt_uri_path, { "Uri-Path", "coap.opt.uri_path", FT_STRING, BASE_NONE, NULL, 0x0, "COAP Uri-Path", HFILL }},
567                 { &hf_coap_opt_observe, { "Lifetime", "coap.opt.subscr_lifetime", FT_INT32, BASE_DEC, NULL, 0x0, "COAP Observe", HFILL }},
568                 { &hf_coap_opt_token, { "Token", "coap.opt.token", FT_BYTES, BASE_NONE, NULL, 0x0, "COAP Token", HFILL }},
569                 { &hf_coap_opt_accept, { "Accept", "coap.opt.accept", FT_UINT8, BASE_DEC, VALS(vals_ctype), 0x0, "COAP Acceptable Content Type", HFILL }},
570                 { &hf_coap_opt_if_match, { "If-Match", "coap.opt.if_match", FT_BYTES, BASE_NONE, NULL, 0x0, "COAP If-Match", HFILL }},
571                 { &hf_coap_opt_block_number, { "Block Number", "coap.opt.block_number", FT_INT32, BASE_DEC, NULL, 0x0, "COAP Block Number", HFILL }},
572                 { &hf_coap_opt_block_mflag, { "More Flag", "coap.opt.block_mflag", FT_UINT8, BASE_DEC, NULL, 0x08, "COAP Block More Size", HFILL }},
573                 { &hf_coap_opt_block_size, { "Encoded Block Size", "coap.opt.block_size", FT_UINT8, BASE_DEC, NULL, 0x07, "COAP Encoded Block Size", HFILL }},
574                 { &hf_coap_opt_uri_query, { "Uri-Query", "coap.opt.uri_query", FT_STRING, BASE_NONE, NULL, 0x0, "COAP Uri-Query", HFILL }},
575                 { &hf_coap_opt_if_none_match, { "If-None-Match", "coap.opt.if_none_match", FT_BYTES, BASE_NONE, NULL, 0x0, "COAP If-None-Match", HFILL }},
576         };
577
578         static gint *ett[] = {
579                 &ett_coap,
580                 &ett_coap_option,
581                 &ett_coap_payload,
582         };
583
584         module_t *coap_module;
585
586         proto_coap = proto_register_protocol("Constrained Application Protocol", "COAP", "coap");
587         proto_register_field_array(proto_coap, hf, array_length(hf));
588         proto_register_subtree_array(ett, array_length(ett));
589
590         register_dissector("coap", dissect_coap, proto_coap);
591
592         /* Register our configuration options */
593         coap_module = prefs_register_protocol (proto_coap, proto_reg_handoff_coap);
594
595         prefs_register_uint_preference (coap_module, "udp_port",
596                                         "COAP port number",
597                                         "Port number used for COAP traffic",
598                                         10, &global_coap_port_number);
599 }
600
601 void
602 proto_reg_handoff_coap(void)
603 {
604         static gboolean coap_prefs_initialized = FALSE;
605         static dissector_handle_t coap_handle;
606         static guint coap_port_number;
607
608         if (!coap_prefs_initialized) {
609                 coap_handle = find_dissector("coap");
610                 media_type_dissector_table = find_dissector_table("media_type");
611                 coap_prefs_initialized = TRUE;
612         } else {
613                 dissector_delete_uint("udp.port", coap_port_number, coap_handle);
614                 dissector_delete_uint("tcp.port", coap_port_number, coap_handle);
615         }
616
617         coap_port_number = global_coap_port_number;
618         dissector_add_uint("udp.port", coap_port_number, coap_handle);
619         dissector_add_uint("tcp.port", coap_port_number, coap_handle);
620 }