1 /* packet-nasdaq-itch.c
2 * Routines for NASDAQ TotalView-ITCH version 2.00/3.00 (with Chi-X extension) Protocol dissection
3 * Copyright 2007,2008 Didier Gautheron <dgautheron@magic.fr>
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
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.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * http://www.nasdaqtrader.com/Trader.aspx?id=DPSpecs
28 * http://www.nasdaqtrader.com/content/technicalsupport/specifications/dataproducts/tv-itch2a.pdf
29 * http://www.nasdaqtrader.com/content/technicalsupport/specifications/dataproducts/tvitch-v3.pdf
32 * http://www.chi-x.com/docs/Chi-X%20CHIXMD.pdf
43 #include <epan/packet.h>
44 #include <epan/prefs.h>
45 #include <wsutil/type_util.h>
48 static gboolean nasdaq_itch_chi_x = TRUE;
50 static const value_string message_types_val[] = {
51 { 'A', "Add Order " },
52 { 'X', "Order Cancel " },
53 { 'M', "Milliseconds " },
54 { 'E', "Order Executed " },
56 { 'P', "Trade Message Identifier " },
57 { 'C', "Order Executed With Price " },
58 { 'D', "Order Delete " },
59 { 'Q', "Cross Trade " },
60 { 'S', "System Event " },
61 { 'R' , "Stock Directory " },
62 { 'H', "Stock Trading Action " },
63 { 'F', "Add Order (MPID) " },
64 { 'I', "Net Order Imbalance Indicator (NOII) " },
65 { 'B', "Broken Trade " },
66 /* Chi-X msg with big size,price */
67 { 'a', "Add Order (big)" },
68 { 'p', "Trade Message Identifier (big)" },
69 { 'e', "Order Executed (big)" },
70 { 'x', "Order Cancel (big)" },
74 static char chix_msg[] = "apex";
76 static const value_string system_event_val[] = {
77 { 'O', "Start of Messages" },
78 { 'S', "Start of System hours" },
79 { 'Q', "Start of Market hours" },
80 { 'M', "End of Market hours" },
81 { 'E', "End of System hours" },
82 { 'C', "End of Messages" },
86 static const value_string market_category_val[] = {
87 { 'T', "CQS (NYSE, Amex or regional exchange)" },
88 { 'Q', "NASDAQ Global Select MarketSM" },
89 { 'G', "NASDAQ Global MarketSM" },
90 { 'S', "NASDAQ Capital Market" },
91 { ' ', "Not available" },
95 static const value_string financial_status_val[] = {
97 { 'E', "Delinquent" },
100 { 'G', "Deficient and Bankrupt" },
101 { 'H', "Deficient and Delinquent" },
102 { 'J', "Delinquent and Bankrupt" },
103 { 'K', "Deficient, Delinquent and Bankrupt" },
104 { ' ', "Company is in compliance" },
108 static const value_string round_lots_only_val[] = {
109 { 'Y', "only round lots are accepted in this stock" },
110 { 'N', "odd/mixed lots are allowed" },
114 /* Initialize the protocol and registered fields */
115 static int proto_nasdaq_itch = -1;
117 /* Initialize the subtree pointers */
118 static gint ett_nasdaq_itch = -1;
120 static int hf_nasdaq_itch_version = -1;
122 static int hf_nasdaq_itch_message_type = -1;
123 static int hf_nasdaq_itch_market_category = -1;
124 static int hf_nasdaq_itch_financial_status = -1;
125 static int hf_nasdaq_itch_stock = -1;
126 static int hf_nasdaq_itch_round_lot_size = -1;
127 static int hf_nasdaq_itch_round_lots_only = -1;
129 static int hf_nasdaq_itch_system_event = -1;
130 static int hf_nasdaq_itch_second = -1;
131 static int hf_nasdaq_itch_millisecond = -1;
133 static int hf_nasdaq_itch_message = -1;
135 static int hf_nasdaq_itch_trading_state = -1;
136 static int hf_nasdaq_itch_reserved = -1;
137 static int hf_nasdaq_itch_reason = -1;
138 static int hf_nasdaq_itch_order_reference = -1;
139 static int hf_nasdaq_itch_buy_sell = -1;
140 static int hf_nasdaq_itch_shares = -1;
141 static int hf_nasdaq_itch_price = -1;
142 static int hf_nasdaq_itch_attribution = -1;
143 static int hf_nasdaq_itch_executed = -1;
144 static int hf_nasdaq_itch_match = -1;
145 static int hf_nasdaq_itch_printable = -1;
146 static int hf_nasdaq_itch_execution_price = -1;
147 static int hf_nasdaq_itch_canceled = -1;
148 static int hf_nasdaq_itch_cross = -1;
150 #define PINFO_COL(a) (check_col((a)->cinfo, COL_INFO))
152 /* ---------------------- */
154 order_ref_number(tvbuff_t *tvb, packet_info *pinfo, proto_tree *nasdaq_itch_tree, int offset)
156 gint col_info = PINFO_COL(pinfo);
158 if (nasdaq_itch_tree || col_info) {
159 const char *str_value = tvb_get_ephemeral_string(tvb, offset, 9);
160 guint32 value = (guint32)strtoul(str_value, NULL, 10);
162 proto_tree_add_uint(nasdaq_itch_tree, hf_nasdaq_itch_order_reference, tvb, offset, 9, value);
164 col_append_fstr(pinfo->cinfo, COL_INFO, "%u ", value);
170 /* -------------------------- */
172 time_stamp(tvbuff_t *tvb, proto_tree *nasdaq_itch_tree, int id, int offset, int size)
175 if (nasdaq_itch_tree) {
177 const char *display = "";
178 const char *str_value = tvb_get_ephemeral_string(tvb, offset, size);
180 ms = val = (guint32)strtoul(str_value, NULL, 10);
183 display = ep_strdup_printf(" %03u" , val);
187 case 8: /* 0 86 400 000 */
188 display = ep_strdup_printf(" %u (%02u:%02u:%02u.%03u)", val,
189 ms/3600000, (ms % 3600000)/60000, (ms % 60000)/1000, ms %1000);
192 proto_tree_add_uint_format_value(nasdaq_itch_tree, id, tvb, offset, size, val, "%s", display);
197 /* -------------------------- */
199 number_of_shares(tvbuff_t *tvb, packet_info *pinfo, proto_tree *nasdaq_itch_tree, int id, int offset, int big)
201 gint col_info = PINFO_COL(pinfo);
202 gint size = (big)?10:6;
204 if (nasdaq_itch_tree || col_info) {
205 const char *str_value = tvb_get_ephemeral_string(tvb, offset, size);
206 guint32 value = (guint32)strtoul(str_value, NULL, 10);
208 proto_tree_add_uint(nasdaq_itch_tree, id, tvb, offset, size, value);
210 col_append_fstr(pinfo->cinfo, COL_INFO, "qty %u ", value);
216 /* -------------------------- */
218 price(tvbuff_t *tvb, packet_info *pinfo, proto_tree *nasdaq_itch_tree, int id, int offset, int big)
220 gint col_info = PINFO_COL(pinfo);
221 gint size = (big)?19:10;
223 if (nasdaq_itch_tree || col_info) {
224 const char *str_value = tvb_get_ephemeral_string(tvb, offset, size);
225 gdouble value = guint64_to_gdouble(g_ascii_strtoull(str_value, NULL, 10))/((big)?1000000.0:10000.0);
227 proto_tree_add_double(nasdaq_itch_tree, id, tvb, offset, size, value);
229 col_append_fstr(pinfo->cinfo, COL_INFO, "price %g ", value);
235 /* -------------------------- */
237 stock(tvbuff_t *tvb, packet_info *pinfo, proto_tree *nasdaq_itch_tree, int offset)
239 gint col_info = PINFO_COL(pinfo);
240 if (nasdaq_itch_tree || col_info) {
241 char *stock_p = tvb_get_ephemeral_string(tvb, offset, 6);
243 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_stock, tvb, offset, 6, ENC_ASCII|ENC_NA);
245 col_append_fstr(pinfo->cinfo, COL_INFO, "<%s> ", stock_p);
251 /* -------------------------- */
253 order(tvbuff_t *tvb, packet_info *pinfo, proto_tree *nasdaq_itch_tree, int offset, int big)
255 gint col_info = PINFO_COL(pinfo);
258 offset = order_ref_number(tvb, pinfo, nasdaq_itch_tree, offset);
260 value = tvb_get_guint8(tvb, offset);
262 col_append_fstr(pinfo->cinfo, COL_INFO, "%c ", value);
264 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_buy_sell, tvb, offset, 1, ENC_ASCII|ENC_NA);
267 offset = number_of_shares(tvb, pinfo, nasdaq_itch_tree, hf_nasdaq_itch_shares, offset, big);
269 offset = stock(tvb, pinfo, nasdaq_itch_tree, offset);
271 offset = price(tvb, pinfo, nasdaq_itch_tree, hf_nasdaq_itch_price, offset, big);
275 /* -------------------------- */
277 executed(tvbuff_t *tvb, packet_info *pinfo, proto_tree *nasdaq_itch_tree, int offset, int big)
279 offset = order_ref_number(tvb, pinfo, nasdaq_itch_tree, offset);
281 offset = number_of_shares(tvb, pinfo, nasdaq_itch_tree, hf_nasdaq_itch_executed, offset, big);
283 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_match, tvb, offset, 9, ENC_ASCII|ENC_NA);
288 /* ---------------------------- */
290 dissect_nasdaq_itch(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
293 proto_tree *nasdaq_itch_tree = NULL;
294 guint8 nasdaq_itch_type;
300 col_info = PINFO_COL(pinfo);
302 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Nasdaq-ITCH");
304 nasdaq_itch_type = tvb_get_guint8(tvb, offset);
305 if (nasdaq_itch_type >= '0' && nasdaq_itch_type <= '9') {
307 nasdaq_itch_type = tvb_get_guint8(tvb, offset +8);
310 if ((!nasdaq_itch_chi_x || version == 3) && strchr(chix_msg, nasdaq_itch_type)) {
311 nasdaq_itch_type = 0; /* unknown */
313 if (col_info || tree) {
314 const gchar *rep = val_to_str(nasdaq_itch_type, message_types_val, "Unknown packet type (0x%02x) ");
316 col_clear(pinfo->cinfo, COL_INFO);
317 col_add_str(pinfo->cinfo, COL_INFO, rep);
322 ti = proto_tree_add_protocol_format(tree, proto_nasdaq_itch, tvb, offset, -1, "Nasdaq TotalView-ITCH %s, %s",
323 version == 2?"2.0":"3.0", rep);
325 nasdaq_itch_tree = proto_item_add_subtree(ti, ett_nasdaq_itch);
327 item=proto_tree_add_uint(nasdaq_itch_tree, hf_nasdaq_itch_version, tvb, 0, 0, version);
328 PROTO_ITEM_SET_GENERATED(item);
333 offset = time_stamp (tvb, nasdaq_itch_tree, hf_nasdaq_itch_millisecond, offset, 8);
336 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_message_type, tvb, offset, 1, ENC_BIG_ENDIAN);
340 switch (nasdaq_itch_type) {
341 case 'T': /* seconds */
342 offset = time_stamp (tvb, nasdaq_itch_tree, hf_nasdaq_itch_second, offset, 5);
345 case 'M': /* milliseconds */
346 offset = time_stamp (tvb, nasdaq_itch_tree, hf_nasdaq_itch_millisecond, offset, 3);
351 switch (nasdaq_itch_type) {
352 case 'S': /* system event */
353 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_system_event, tvb, offset, 1, ENC_BIG_ENDIAN);
357 case 'R': /* Stock Directory */
358 offset = stock(tvb, pinfo, nasdaq_itch_tree, offset);
360 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_market_category, tvb, offset, 1, ENC_BIG_ENDIAN);
362 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_financial_status, tvb, offset, 1, ENC_BIG_ENDIAN);
364 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_round_lot_size, tvb, offset, 6, ENC_ASCII|ENC_NA);
366 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_round_lots_only, tvb, offset, 1, ENC_BIG_ENDIAN);
370 case 'H': /* Stock trading action */
371 offset = stock(tvb, pinfo, nasdaq_itch_tree, offset);
373 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_trading_state, tvb, offset, 1, ENC_ASCII|ENC_NA);
375 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_reserved, tvb, offset, 1, ENC_ASCII|ENC_NA);
377 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_reason, tvb, offset, 4, ENC_ASCII|ENC_NA);
383 case 'A': /* Add order, no MPID */
384 offset = order(tvb, pinfo, nasdaq_itch_tree, offset, big);
386 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_printable, tvb, offset, 1, ENC_ASCII|ENC_NA);
391 case 'F': /* Add order, MPID */
392 offset = order(tvb, pinfo, nasdaq_itch_tree, offset, big);
393 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_attribution, tvb, offset, 4, ENC_ASCII|ENC_NA);
399 case 'E' : /* Order executed */
400 offset = executed(tvb, pinfo, nasdaq_itch_tree, offset, big);
403 case 'C' : /* Order executed with price */
404 offset = executed(tvb, pinfo, nasdaq_itch_tree, offset, big);
405 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_printable, tvb, offset, 1, ENC_ASCII|ENC_NA);
408 offset = price(tvb, pinfo, nasdaq_itch_tree, hf_nasdaq_itch_execution_price, offset, big);
413 case 'X' : /* Order cancel */
414 offset = order_ref_number(tvb, pinfo, nasdaq_itch_tree, offset);
415 offset = number_of_shares(tvb, pinfo, nasdaq_itch_tree, hf_nasdaq_itch_canceled, offset, big);
418 case 'D' : /* Order delete */
419 offset = order_ref_number(tvb, pinfo, nasdaq_itch_tree, offset);
425 case 'P' : /* Trade identifier */
426 offset = order(tvb, pinfo, nasdaq_itch_tree, offset, big);
427 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_match, tvb, offset, 9, ENC_ASCII|ENC_NA);
431 case 'Q' : /* Cross Trade */
432 offset = number_of_shares(tvb, pinfo, nasdaq_itch_tree, hf_nasdaq_itch_shares, offset, big);
434 offset = stock(tvb, pinfo, nasdaq_itch_tree, offset);
436 offset = price(tvb, pinfo, nasdaq_itch_tree, hf_nasdaq_itch_price, offset, big);
438 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_match, tvb, offset, 9, ENC_ASCII|ENC_NA);
440 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_cross, tvb, offset, 1, ENC_ASCII|ENC_NA);
444 case 'B' : /* Broken Trade */
445 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_match, tvb, offset, 9, ENC_ASCII|ENC_NA);
449 case 'I': /* NOII, FIXME */
450 offset = stock(tvb, pinfo, nasdaq_itch_tree, offset);
452 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_cross, tvb, offset, 1, ENC_ASCII|ENC_NA);
458 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_message, tvb, offset, -1, ENC_ASCII|ENC_NA);
464 /* Register the protocol with Wireshark */
467 proto_register_nasdaq_itch(void)
470 /* Setup list of header fields See Section 1.6.1 for details*/
471 static hf_register_info hf[] = {
472 { &hf_nasdaq_itch_version,
473 { "Version", "nasdaq-itch.version",
474 FT_UINT8, BASE_DEC, NULL, 0x0,
477 { &hf_nasdaq_itch_message_type,
478 { "Message Type", "nasdaq-itch.message_type",
479 FT_UINT8, BASE_DEC, VALS(message_types_val), 0x0,
482 { &hf_nasdaq_itch_second,
483 { "Second", "nasdaq-itch.second",
484 FT_UINT32, BASE_DEC, NULL, 0x0,
487 { &hf_nasdaq_itch_millisecond,
488 { "Millisecond", "nasdaq-itch.millisecond",
489 FT_UINT32, BASE_DEC, NULL, 0x0,
492 { &hf_nasdaq_itch_system_event,
493 { "System Event", "nasdaq-itch.system_event",
494 FT_UINT8, BASE_DEC, VALS(system_event_val), 0x0,
497 { &hf_nasdaq_itch_market_category,
498 { "Market Category", "nasdaq-itch.market_category",
499 FT_UINT8, BASE_DEC, VALS(market_category_val), 0x0,
502 { &hf_nasdaq_itch_financial_status,
503 { "Financial Status Indicator", "nasdaq-itch.financial_status",
504 FT_UINT8, BASE_DEC, VALS(financial_status_val), 0x0,
507 { &hf_nasdaq_itch_stock,
508 { "Stock", "nasdaq-itch.stock",
509 FT_STRING, BASE_NONE, NULL, 0x0,
512 { &hf_nasdaq_itch_round_lot_size,
513 { "Round Lot Size", "nasdaq-itch.round_lot_size",
514 FT_STRING, BASE_NONE, NULL, 0x0,
517 { &hf_nasdaq_itch_round_lots_only,
518 { "Round Lots Only", "nasdaq-itch.round_lots_only",
519 FT_UINT8, BASE_DEC, VALS(round_lots_only_val), 0x0,
522 { &hf_nasdaq_itch_trading_state,
523 { "Trading State", "nasdaq-itch.trading_state",
524 FT_STRING, BASE_NONE, NULL, 0x0,
527 { &hf_nasdaq_itch_reserved,
528 { "Reserved", "nasdaq-itch.reserved",
529 FT_STRING, BASE_NONE, NULL, 0x0,
532 { &hf_nasdaq_itch_reason,
533 { "Reason", "nasdaq-itch.reason",
534 FT_STRING, BASE_NONE, NULL, 0x0,
537 { &hf_nasdaq_itch_order_reference,
538 { "Order Reference", "nasdaq-itch.order_reference",
539 FT_UINT32, BASE_DEC, NULL, 0x0,
540 "Order reference number", HFILL }},
542 { &hf_nasdaq_itch_buy_sell,
543 { "Buy/Sell", "nasdaq-itch.buy_sell",
544 FT_STRING, BASE_NONE, NULL, 0x0,
545 "Buy/Sell indicator", HFILL }},
547 { &hf_nasdaq_itch_shares,
548 { "Shares", "nasdaq-itch.shares",
549 FT_UINT32, BASE_DEC, NULL, 0x0,
550 "Number of shares", HFILL }},
552 { &hf_nasdaq_itch_price,
553 { "Price", "nasdaq-itch.price",
554 FT_DOUBLE, BASE_NONE, NULL, 0x0,
557 { &hf_nasdaq_itch_attribution,
558 { "Attribution", "nasdaq-itch.attribution",
559 FT_STRING, BASE_NONE, NULL, 0x0,
560 "Market participant identifier", HFILL }},
562 { &hf_nasdaq_itch_executed,
563 { "Executed Shares", "nasdaq-itch.executed",
564 FT_UINT32, BASE_DEC, NULL, 0x0,
565 "Number of shares executed", HFILL }},
567 { &hf_nasdaq_itch_match,
568 { "Matched", "nasdaq-itch.match",
569 FT_STRING, BASE_NONE, NULL, 0x0,
570 "Match number", HFILL }},
572 { &hf_nasdaq_itch_printable,
573 { "Printable", "nasdaq-itch.printable",
574 FT_STRING, BASE_NONE, NULL, 0x0,
577 { &hf_nasdaq_itch_execution_price,
578 { "Execution Price", "nasdaq-itch.execution_price",
579 FT_DOUBLE, BASE_NONE, NULL, 0x0,
582 { &hf_nasdaq_itch_canceled,
583 { "Canceled Shares", "nasdaq-itch.canceled",
584 FT_UINT32, BASE_DEC, NULL, 0x0,
585 "Number of shares to be removed", HFILL }},
587 { &hf_nasdaq_itch_cross,
588 { "Cross Type", "nasdaq-itch.cross",
589 FT_STRING, BASE_NONE, NULL, 0x0,
590 "Cross trade type", HFILL }},
592 { &hf_nasdaq_itch_message,
593 { "Message", "nasdaq-itch.message",
594 FT_STRING, BASE_NONE, NULL, 0x0,
598 /* Setup protocol subtree array */
599 static gint *ett[] = {
603 module_t *nasdaq_itch_module;
605 /* Register the protocol name and description */
606 proto_nasdaq_itch = proto_register_protocol("Nasdaq TotalView-ITCH", "NASDAQ-ITCH", "nasdaq_itch");
608 /* Required function calls to register the header fields and subtrees used */
609 proto_register_field_array(proto_nasdaq_itch, hf, array_length(hf));
610 proto_register_subtree_array(ett, array_length(ett));
612 nasdaq_itch_module = prefs_register_protocol(proto_nasdaq_itch, NULL);
613 prefs_register_bool_preference(nasdaq_itch_module, "chi_x", "Decode Chi X extensions",
614 "Whether the Nasdaq ITCH dissector should decode Chi X extensions.",
617 register_dissector("nasdaq-itch", dissect_nasdaq_itch, proto_nasdaq_itch);
621 /* If this dissector uses sub-dissector registration add a registration routine.
622 This format is required because a script is used to find these routines and
623 create the code that calls these routines.
626 proto_reg_handoff_nasdaq_itch(void)