Introduce a preference to select how Websocket text should be dissected.
[metze/wireshark/wip.git] / epan / dissectors / packet-websocket.c
1 /* packet-websocket.c
2  * Routines for WebSocket dissection
3  * Copyright 2012, Alexis La Goutte <alexis.lagoutte@gmail.com>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "config.h"
27
28 #include <glib.h>
29
30 #include <epan/packet.h>
31 #include <epan/expert.h>
32 #include <epan/prefs.h>
33
34
35 /*
36  * The information used comes from:
37  * RFC6455: The WebSocket Protocol
38  * http://www.iana.org/assignments/websocket (last updated 2012-04-12)
39  */
40
41 void proto_reg_handoff_websocket(void);
42
43 dissector_handle_t text_lines_handle;
44 dissector_handle_t json_handle;
45
46 #define WEBSOCKET_NONE 0
47 #define WEBSOCKET_TEXT 1
48 #define WEBSOCKET_JSON 2
49
50 static gint  pref_text_type             = WEBSOCKET_NONE;
51
52 /* Initialize the protocol and registered fields */
53 static int proto_websocket = -1;
54 static int hf_ws_fin = -1;
55 static int hf_ws_reserved = -1;
56 static int hf_ws_opcode = -1;
57 static int hf_ws_mask = -1;
58 static int hf_ws_payload_length = -1;
59 static int hf_ws_payload_length_ext_16 = -1;
60 static int hf_ws_payload_length_ext_64 = -1;
61 static int hf_ws_masking_key = -1;
62 static int hf_ws_payload = -1;
63 static int hf_ws_payload_unmask = -1;
64 static int hf_ws_payload_continue = -1;
65 static int hf_ws_payload_text = -1;
66 static int hf_ws_payload_text_mask = -1;
67 static int hf_ws_payload_text_unmask = -1;
68 static int hf_ws_payload_binary = -1;
69 static int hf_ws_payload_binary_mask = -1;
70 static int hf_ws_payload_binary_unmask = -1;
71 static int hf_ws_payload_close = -1;
72 static int hf_ws_payload_close_mask = -1;
73 static int hf_ws_payload_close_unmask = -1;
74 static int hf_ws_payload_close_status_code = -1;
75 static int hf_ws_payload_close_reason = -1;
76 static int hf_ws_payload_ping = -1;
77 static int hf_ws_payload_ping_mask = -1;
78 static int hf_ws_payload_ping_unmask = -1;
79 static int hf_ws_payload_pong = -1;
80 static int hf_ws_payload_pong_mask = -1;
81 static int hf_ws_payload_pong_unmask = -1;
82 static int hf_ws_payload_unknown = -1;
83
84 static gint ett_ws = -1;
85 static gint ett_ws_pl = -1;
86 static gint ett_ws_mask = -1;
87
88 #define WS_CONTINUE 0x0
89 #define WS_TEXT     0x1
90 #define WS_BINARY   0x2
91 #define WS_CLOSE    0x8
92 #define WS_PING     0x9
93 #define WS_PONG     0xA
94
95 static const value_string ws_opcode_vals[] = {
96   { WS_CONTINUE, "Continuation" },
97   { WS_TEXT, "Text" },
98   { WS_BINARY, "Binary" },
99   { WS_CLOSE, "Connection Close" },
100   { WS_PING, "Ping" },
101   { WS_PONG, "Pong" },
102   { 0, NULL}
103 };
104
105 #define MASK_WS_FIN 0x80
106 #define MASK_WS_RSV 0x70
107 #define MASK_WS_OPCODE 0x0F
108 #define MASK_WS_MASK 0x80
109 #define MASK_WS_PAYLOAD_LEN 0x7F
110
111 static const value_string ws_close_status_code_vals[] = {
112   { 1000, "Normal Closure" },
113   { 1001, "Going Away" },
114   { 1002, "Protocol error" },
115   { 1003, "Unsupported Data" },
116   { 1004, "---Reserved----" },
117   { 1005, "No Status Rcvd" },
118   { 1006, "Abnormal Closure" },
119   { 1007, "Invalid frame payload data" },
120   { 1008, "Policy Violation" },
121   { 1009, "Message Too Big" },
122   { 1010, "Mandatory Ext." },
123   { 1011, "Internal Server" },
124   { 1015, "TLS handshake" },
125   { 0,    NULL}
126 };
127
128 static dissector_table_t port_subdissector_table;
129 static heur_dissector_list_t heur_subdissector_list;
130
131 #define MAX_UNMASKED_LEN (1024 * 64)
132 tvbuff_t *
133 tvb_unmasked(tvbuff_t *tvb, const guint offset, guint payload_length, const guint8 *masking_key)
134 {
135
136   gchar *data_unmask;
137   tvbuff_t *tvb_unmask = NULL;
138   guint i;
139   const guint8 *data_mask;
140   guint unmasked_length = payload_length > MAX_UNMASKED_LEN ? MAX_UNMASKED_LEN : payload_length;
141
142   data_unmask = (gchar *)g_malloc(unmasked_length);
143   data_mask = tvb_get_ptr(tvb, offset, unmasked_length);
144   /* Unmasked(XOR) Data... */
145   for(i=0; i < unmasked_length; i++){
146     data_unmask[i] = data_mask[i] ^ masking_key[i%4];
147   }
148
149   tvb_unmask = tvb_new_real_data(data_unmask, unmasked_length, unmasked_length);
150   tvb_set_free_cb(tvb_unmask, g_free);
151   return tvb_unmask;
152 }
153
154 static int
155 dissect_websocket_payload(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, proto_tree *ws_tree, guint8 opcode, guint payload_length, guint8 mask, const guint8* masking_key)
156 {
157   guint offset = 0;
158   proto_item *ti_unmask, *ti;
159   dissector_handle_t handle;
160   proto_tree *pl_tree, *mask_tree = NULL;
161   tvbuff_t *payload_tvb = NULL;
162
163   /* Payload */
164   ti = proto_tree_add_item(ws_tree, hf_ws_payload, tvb, offset, payload_length, ENC_NA);
165   pl_tree = proto_item_add_subtree(ti, ett_ws_pl);
166   if(mask){
167     payload_tvb = tvb_unmasked(tvb, offset, payload_length, masking_key);
168     tvb_set_child_real_data_tvbuff(tvb, payload_tvb);
169     add_new_data_source(pinfo, payload_tvb, payload_length > tvb_length(payload_tvb) ? "Unmasked Data (truncated)" : "Unmasked Data");
170     ti = proto_tree_add_item(ws_tree, hf_ws_payload_unmask, payload_tvb, offset, payload_length, ENC_NA);
171     mask_tree = proto_item_add_subtree(ti, ett_ws_mask);
172   }else{
173     payload_tvb = tvb_new_subset(tvb, offset, payload_length, -1);
174   }
175
176   handle = dissector_get_uint_handle(port_subdissector_table, pinfo->match_uint);
177   if(handle != NULL){
178     call_dissector_only(handle, payload_tvb, pinfo, tree, NULL);
179   }else{
180     dissector_try_heuristic(heur_subdissector_list, payload_tvb, pinfo, tree, NULL);
181   }
182
183   /* Extension Data */
184   /* TODO: Add dissector of Extension (not extension available for the moment...) */
185
186   /* Application Data */
187   switch(opcode){
188
189     case WS_CONTINUE: /* Continue */
190       proto_tree_add_item(pl_tree, hf_ws_payload_continue, tvb, offset, payload_length, ENC_NA);
191       /* TODO: Add Fragmentation support... */
192     break;
193
194     case WS_TEXT: /* Text */
195     if(mask){
196
197       proto_tree_add_item(pl_tree, hf_ws_payload_text_mask, tvb, offset, payload_length, ENC_NA);
198       ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_text_unmask, payload_tvb, offset, payload_length, ENC_UTF_8|ENC_NA);
199       PROTO_ITEM_SET_GENERATED(ti_unmask);
200       ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_text, payload_tvb, offset, payload_length, ENC_UTF_8|ENC_NA);
201       PROTO_ITEM_SET_HIDDEN(ti_unmask);
202     }else{
203       const gchar  *saved_match_string = pinfo->match_string;
204       void *save_private_data = pinfo->private_data;
205
206       pinfo->match_string = NULL;
207       pinfo->private_data = NULL;
208       switch(pref_text_type){
209       case WEBSOCKET_TEXT:
210           call_dissector(text_lines_handle, payload_tvb, pinfo, pl_tree);
211           break;
212       case WEBSOCKET_JSON:
213           call_dissector(json_handle, payload_tvb, pinfo, pl_tree);
214           break;
215       case WEBSOCKET_NONE:
216           /* falltrough */
217       default:
218           proto_tree_add_item(pl_tree, hf_ws_payload_text, tvb, offset, payload_length, ENC_UTF_8|ENC_NA);
219           break;
220       }
221       pinfo->match_string = saved_match_string;
222       pinfo->private_data = save_private_data;
223     }
224     offset += payload_length;
225     break;
226
227     case WS_BINARY: /* Binary */
228     if(mask){
229       proto_tree_add_item(pl_tree, hf_ws_payload_binary_mask, tvb, offset, payload_length, ENC_NA);
230       ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_binary_unmask, payload_tvb, offset, payload_length, ENC_NA);
231       PROTO_ITEM_SET_GENERATED(ti_unmask);
232       ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_binary, payload_tvb, offset, payload_length, ENC_NA);
233       PROTO_ITEM_SET_HIDDEN(ti_unmask);
234     }else{
235       proto_tree_add_item(pl_tree, hf_ws_payload_binary, tvb, offset, payload_length, ENC_NA);
236     }
237     offset += payload_length;
238     break;
239
240     case WS_CLOSE: /* Close */
241     if(mask){
242       proto_tree_add_item(pl_tree, hf_ws_payload_close_mask, tvb, offset, payload_length, ENC_NA);
243       ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_close_unmask, payload_tvb, offset, payload_length, ENC_NA);
244       PROTO_ITEM_SET_GENERATED(ti_unmask);
245       ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_close, payload_tvb, offset, payload_length, ENC_NA);
246       PROTO_ITEM_SET_HIDDEN(ti_unmask);
247       ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_close_status_code, payload_tvb, offset, 2, ENC_BIG_ENDIAN);
248       PROTO_ITEM_SET_GENERATED(ti_unmask);
249
250       if(payload_length > 2){
251         ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_close_reason, payload_tvb, offset+2, payload_length-2, ENC_ASCII|ENC_NA);
252         PROTO_ITEM_SET_GENERATED(ti_unmask);
253       }
254     }else{
255       proto_tree_add_item(pl_tree, hf_ws_payload_close, tvb, offset, payload_length, ENC_NA);
256       proto_tree_add_item(pl_tree, hf_ws_payload_close_status_code, tvb, offset, 2, ENC_BIG_ENDIAN);
257       if(payload_length > 2){
258         proto_tree_add_item(pl_tree, hf_ws_payload_close_reason, tvb, offset+2, payload_length-2, ENC_ASCII|ENC_NA);
259       }
260     }
261     offset += payload_length;
262     break;
263
264     case WS_PING: /* Ping */
265     if(mask){
266       proto_tree_add_item(pl_tree, hf_ws_payload_ping_mask, tvb, offset, payload_length, ENC_NA);
267       ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_ping_unmask, payload_tvb, offset, payload_length, ENC_NA);
268       PROTO_ITEM_SET_GENERATED(ti_unmask);
269       ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_ping, payload_tvb, offset, payload_length, ENC_NA);
270       PROTO_ITEM_SET_HIDDEN(ti_unmask);
271     }else{
272       proto_tree_add_item(pl_tree, hf_ws_payload_ping, tvb, offset, payload_length, ENC_NA);
273     }
274     offset += payload_length;
275     break;
276
277     case WS_PONG: /* Pong */
278     if(mask){
279       proto_tree_add_item(pl_tree, hf_ws_payload_pong_mask, tvb, offset, payload_length, ENC_NA);
280       ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_pong_unmask, payload_tvb, offset, payload_length, ENC_NA);
281       PROTO_ITEM_SET_GENERATED(ti_unmask);
282       ti_unmask = proto_tree_add_item(mask_tree, hf_ws_payload_pong, payload_tvb, offset, payload_length, ENC_NA);
283       PROTO_ITEM_SET_HIDDEN(ti_unmask);
284     }else{
285       proto_tree_add_item(pl_tree, hf_ws_payload_pong, tvb, offset, payload_length, ENC_NA);
286     }
287     offset += payload_length;
288     break;
289
290     default: /* Unknown */
291       ti = proto_tree_add_item(pl_tree, hf_ws_payload_unknown, tvb, offset, payload_length, ENC_NA);
292       expert_add_info_format(pinfo, ti, PI_UNDECODED, PI_NOTE, "Dissector for Websocket Opcode (%d)"
293         " code not implemented, Contact Wireshark developers"
294         " if you want this supported", opcode);
295     break;
296   }
297   return offset;
298 }
299
300
301 static int
302 dissect_websocket(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
303 {
304   proto_item *ti, *ti_len;
305   guint8 fin, opcode, mask;
306   guint length, short_length, payload_length, recurse_length;
307   guint payload_offset, mask_offset, recurse_offset;
308   proto_tree *ws_tree = NULL;
309   const guint8 *masking_key = NULL;
310   tvbuff_t *tvb_payload = NULL;
311
312   length = tvb_length(tvb);
313   if(length<2){
314     pinfo->desegment_len = 2;
315     return 0;
316   }
317
318   short_length = tvb_get_guint8(tvb, 1) & MASK_WS_PAYLOAD_LEN;
319   if(short_length==126){
320     if(length < 2+2){
321       pinfo->desegment_len = 2+2;
322       return 0;
323     }
324     payload_length = tvb_get_ntohs(tvb, 2);
325     mask_offset = 2+2;
326   }
327   else if(short_length==127){
328     if(length < 2+8){
329       pinfo->desegment_len = 2+8;
330       return 0;
331     }
332         /* warning C4244: '=' : conversion from 'guint64' to 'guint ', possible loss of data */
333     payload_length = (guint)tvb_get_ntoh64(tvb, 2);
334     mask_offset = 2+8;
335   }
336   else{
337     payload_length = short_length;
338     mask_offset = 2;
339   }
340
341   /* Mask */
342   mask = (tvb_get_guint8(tvb, 1) & MASK_WS_MASK) >> 4;
343   payload_offset = mask_offset + (mask ? 4 : 0);
344
345   if(length < payload_offset + payload_length){
346     /* XXXX Warning desegment_len is 32 bits */
347     pinfo->desegment_len = payload_offset + payload_length - length;
348     return 0;
349   }
350
351   /* We've got the entire message! */
352
353   col_set_str(pinfo->cinfo, COL_PROTOCOL, "WebSocket");
354   col_set_str(pinfo->cinfo, COL_INFO, "WebSocket");
355
356   if(tree){
357     ti = proto_tree_add_item(tree, proto_websocket, tvb, 0, payload_offset, ENC_NA);
358     ws_tree = proto_item_add_subtree(ti, ett_ws);
359   }
360
361   /* Flags */
362   proto_tree_add_item(ws_tree, hf_ws_fin, tvb, 0, 1, ENC_NA);
363   fin = (tvb_get_guint8(tvb, 0) & MASK_WS_FIN) >> 4;
364   proto_tree_add_item(ws_tree, hf_ws_reserved, tvb, 0, 1, ENC_NA);
365
366   /* Opcode */
367   proto_tree_add_item(ws_tree, hf_ws_opcode, tvb, 0, 1, ENC_NA);
368   opcode = tvb_get_guint8(tvb, 0) & MASK_WS_OPCODE;
369   col_append_fstr(pinfo->cinfo, COL_INFO, " %s", val_to_str_const(opcode, ws_opcode_vals, "Unknown Opcode"));
370   col_append_fstr(pinfo->cinfo, COL_INFO, " %s", fin ? "[FIN]" : "");
371
372   /* Add Mask bit to the tree */
373   proto_tree_add_item(ws_tree, hf_ws_mask, tvb, 1, 1, ENC_NA);
374   col_append_fstr(pinfo->cinfo, COL_INFO, " %s", mask ? "[MASKED]" : "");
375
376   /* (Extended) Payload Length */
377   ti_len = proto_tree_add_item(ws_tree, hf_ws_payload_length, tvb, 1, 1, ENC_NA);
378   if(short_length==126){
379     proto_item_append_text(ti_len, " Extended Payload Length (16 bits)");
380     proto_tree_add_item(ws_tree, hf_ws_payload_length_ext_16, tvb, 2, 2, ENC_BIG_ENDIAN);
381   }
382   else if(short_length==127){
383     proto_item_append_text(ti_len, " Extended Payload Length (64 bits)");
384     proto_tree_add_item(ws_tree, hf_ws_payload_length_ext_64, tvb, 2, 8, ENC_BIG_ENDIAN);
385   }
386
387   /* Masking-key */
388   if(mask){
389     proto_tree_add_item(ws_tree, hf_ws_masking_key, tvb, mask_offset, 4, ENC_NA);
390     masking_key = tvb_get_ptr(tvb, mask_offset, 4);
391   }
392
393   tvb_payload = tvb_new_subset_remaining(tvb, payload_offset);
394   dissect_websocket_payload(tvb_payload, pinfo, tree, ws_tree, opcode, payload_length, mask, masking_key);
395
396   /* Call this function recursively, to see if we have enough data to parse another websocket message */
397
398   recurse_offset = payload_offset + payload_length;
399   if(length > recurse_offset){
400     recurse_length = dissect_websocket(tvb_new_subset_remaining(tvb, payload_offset+payload_length), pinfo, tree, data);
401     if(pinfo->desegment_len) pinfo->desegment_offset += recurse_offset;
402     return recurse_offset + recurse_length;
403   }
404   return recurse_offset;
405 }
406
407
408 void
409 proto_register_websocket(void)
410 {
411
412   static hf_register_info hf[] = {
413     { &hf_ws_fin,
414       { "Fin", "websocket.fin",
415       FT_BOOLEAN, 8, NULL, MASK_WS_FIN,
416       "Indicates that this is the final fragment in a message", HFILL }
417     },
418     { &hf_ws_reserved,
419       { "Reserved", "websocket.rsv",
420       FT_UINT8, BASE_HEX, NULL, MASK_WS_RSV,
421       "Must be zero", HFILL }
422     },
423     { &hf_ws_opcode,
424       { "Opcode", "websocket.opcode",
425       FT_UINT8, BASE_DEC, VALS(ws_opcode_vals), MASK_WS_OPCODE,
426       "Defines the interpretation of the Payload data", HFILL }
427     },
428     { &hf_ws_mask,
429       { "Mask", "websocket.mask",
430       FT_BOOLEAN, 8, NULL, MASK_WS_MASK,
431       "Defines whether the Payload data is masked", HFILL }
432     },
433     { &hf_ws_payload_length,
434       { "Payload length", "websocket.payload_length",
435       FT_UINT8, BASE_DEC, NULL, MASK_WS_PAYLOAD_LEN,
436       "The length of the Payload data", HFILL }
437     },
438     { &hf_ws_payload_length_ext_16,
439       { "Extended Payload length (16 bits)", "websocket.payload_length_ext_16",
440       FT_UINT16, BASE_DEC, NULL, 0x0,
441       "The length (16 bits) of the Payload data", HFILL }
442     },
443     { &hf_ws_payload_length_ext_64,
444       { "Extended Payload length (64 bits)", "websocket.payload_length_ext_64",
445       FT_UINT64, BASE_DEC, NULL, 0x0,
446       "The length (64 bits) of the Payload data", HFILL }
447     },
448     { &hf_ws_masking_key,
449       { "Masking-Key", "websocket.masking_key",
450       FT_BYTES, BASE_NONE, NULL, 0x0,
451       "All frames sent from the client to the server are masked by a 32-bit value that is contained within the frame", HFILL }
452     },
453     { &hf_ws_payload,
454       { "Payload", "websocket.payload",
455       FT_NONE, BASE_NONE, NULL, 0x0,
456       NULL, HFILL }
457     },
458     { &hf_ws_payload_unmask,
459       { "Unmask Payload", "websocket.payload.unmask",
460       FT_NONE, BASE_NONE, NULL, 0x0,
461       NULL, HFILL }
462     },
463     { &hf_ws_payload_continue,
464       { "Continue", "websocket.payload.continue",
465       FT_BYTES, BASE_NONE, NULL, 0x0,
466       NULL, HFILL }
467     },
468     { &hf_ws_payload_text,
469       { "Text", "websocket.payload.text",
470       FT_STRING, BASE_NONE, NULL, 0x0,
471       NULL, HFILL }
472     },
473     { &hf_ws_payload_text_mask,
474       { "Text", "websocket.payload.text_mask",
475       FT_BYTES, BASE_NONE, NULL, 0x0,
476       NULL, HFILL }
477     },
478     { &hf_ws_payload_text_unmask,
479       { "Text unmask", "websocket.payload.text_unmask",
480       FT_STRING, BASE_NONE, NULL, 0x0,
481       NULL, HFILL }
482     },
483     { &hf_ws_payload_binary,
484       { "Binary", "websocket.payload.binary",
485       FT_BYTES, BASE_NONE, NULL, 0x0,
486       NULL, HFILL }
487     },
488     { &hf_ws_payload_binary_mask,
489       { "Binary", "websocket.payload.binary_mask",
490       FT_BYTES, BASE_NONE, NULL, 0x0,
491       NULL, HFILL }
492     },
493     { &hf_ws_payload_binary_unmask,
494       { "Binary", "websocket.payload.binary_unmask",
495       FT_BYTES, BASE_NONE, NULL, 0x0,
496       NULL, HFILL }
497     },
498     { &hf_ws_payload_close,
499       { "Close", "websocket.payload.close",
500       FT_BYTES, BASE_NONE, NULL, 0x0,
501       NULL, HFILL }
502     },
503     { &hf_ws_payload_close_mask,
504       { "Close", "websocket.payload.close_mask",
505       FT_BYTES, BASE_NONE, NULL, 0x0,
506       NULL, HFILL }
507     },
508     { &hf_ws_payload_close_unmask,
509       { "Unmask Close", "websocket.payload.close_unmask",
510       FT_BYTES, BASE_NONE, NULL, 0x0,
511       NULL, HFILL }
512     },
513     { &hf_ws_payload_close_status_code,
514       { "Close", "websocket.payload.close.status_code",
515       FT_UINT16, BASE_DEC, VALS(ws_close_status_code_vals), 0x0,
516       NULL, HFILL }
517     },
518     { &hf_ws_payload_close_reason,
519       { "Reason", "websocket.payload.close.reason",
520       FT_STRING, BASE_NONE, NULL, 0x0,
521       NULL, HFILL }
522     },
523     { &hf_ws_payload_ping,
524       { "Ping", "websocket.payload.ping",
525       FT_BYTES, BASE_NONE, NULL, 0x0,
526       NULL, HFILL }
527     },
528     { &hf_ws_payload_ping_mask,
529       { "Ping", "websocket.payload.ping_mask",
530       FT_BYTES, BASE_NONE, NULL, 0x0,
531       NULL, HFILL }
532     },
533     { &hf_ws_payload_ping_unmask,
534       { "Ping", "websocket.payload.ping_unmask",
535       FT_BYTES, BASE_NONE, NULL, 0x0,
536       NULL, HFILL }
537     },
538     { &hf_ws_payload_pong,
539       { "Pong", "websocket.payload.pong",
540       FT_BYTES, BASE_NONE, NULL, 0x0,
541       NULL, HFILL }
542     },
543     { &hf_ws_payload_pong_mask,
544       { "Pong", "websocket.payload.pong_mask",
545       FT_BYTES, BASE_NONE, NULL, 0x0,
546       NULL, HFILL }
547     },
548     { &hf_ws_payload_pong_unmask,
549       { "Pong", "websocket.payload.pong_unmask",
550       FT_BYTES, BASE_NONE, NULL, 0x0,
551       NULL, HFILL }
552     },
553     { &hf_ws_payload_unknown,
554       { "Unknown", "websocket.payload.unknown",
555       FT_BYTES, BASE_NONE, NULL, 0x0,
556       NULL, HFILL }
557     },
558   };
559
560
561   static gint *ett[] = {
562     &ett_ws,
563     &ett_ws_pl,
564     &ett_ws_mask
565   };
566
567   static const enum_val_t text_types[] = {
568       {"None",            "No subdissection", WEBSOCKET_NONE},
569       {"Line based text", "Line based text",  WEBSOCKET_TEXT},
570       {"As JSON",         "As json",          WEBSOCKET_JSON},
571       {NULL, NULL, -1}
572   };
573
574   module_t *websocket_module;
575
576   proto_websocket = proto_register_protocol("WebSocket",
577       "WebSocket", "websocket");
578   
579   /*
580    * Heuristic dissectors SHOULD register themselves in
581    * this table using the standard heur_dissector_add()
582    * function.
583    */
584   register_heur_dissector_list("ws", &heur_subdissector_list);
585
586   port_subdissector_table = register_dissector_table("ws.port",
587       "TCP port for protocols using WebSocket", FT_UINT16, BASE_DEC);
588
589   proto_register_field_array(proto_websocket, hf, array_length(hf));
590   proto_register_subtree_array(ett, array_length(ett));
591
592   new_register_dissector("websocket", dissect_websocket, proto_websocket);
593
594   websocket_module = prefs_register_protocol(proto_websocket, proto_reg_handoff_websocket);
595
596   prefs_register_enum_preference(websocket_module, "text_type",
597         "Dissect websocket text as",
598         "Select dissector for websocket text",
599         &pref_text_type, text_types, WEBSOCKET_NONE);
600
601
602 }
603
604 void
605 proto_reg_handoff_websocket(void)
606 {
607         text_lines_handle = find_dissector("data-text-lines");
608         json_handle = find_dissector("json");
609 }
610 /*
611  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
612  *
613  * Local variables:
614  * c-basic-offset: 2
615  * tab-width: 8
616  * indent-tabs-mode: nil
617  * End:
618  *
619  * vi: set shiftwidth=2 tabstop=8 expandtab:
620  * :indentSize=2:tabSize=8:noTabs=true:
621  */