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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
41 #include <epan/packet.h>
42 #include <epan/prefs.h>
43 #include <wsutil/type_util.h>
46 static gboolean nasdaq_itch_chi_x = TRUE;
48 static const value_string message_types_val[] = {
49 { 'A', "Add Order " },
50 { 'X', "Order Cancel " },
51 { 'M', "Milliseconds " },
52 { 'E', "Order Executed " },
54 { 'P', "Trade Message Identifier " },
55 { 'C', "Order Executed With Price " },
56 { 'D', "Order Delete " },
57 { 'Q', "Cross Trade " },
58 { 'S', "System Event " },
59 { 'R' , "Stock Directory " },
60 { 'H', "Stock Trading Action " },
61 { 'F', "Add Order (MPID) " },
62 { 'I', "Net Order Imbalance Indicator (NOII) " },
63 { 'B', "Broken Trade " },
64 /* Chi-X msg with big size,price */
65 { 'a', "Add Order (big)" },
66 { 'p', "Trade Message Identifier (big)" },
67 { 'e', "Order Executed (big)" },
68 { 'x', "Order Cancel (big)" },
72 static char chix_msg[] = "apex";
74 static const value_string system_event_val[] = {
75 { 'O', "Start of Messages" },
76 { 'S', "Start of System hours" },
77 { 'Q', "Start of Market hours" },
78 { 'M', "End of Market hours" },
79 { 'E', "End of System hours" },
80 { 'C', "End of Messages" },
84 static const value_string market_category_val[] = {
85 { 'T', "CQS (NYSE, Amex or regional exchange)" },
86 { 'Q', "NASDAQ Global Select MarketSM" },
87 { 'G', "NASDAQ Global MarketSM" },
88 { 'S', "NASDAQ Capital Market" },
89 { ' ', "Not available" },
93 static const value_string financial_status_val[] = {
95 { 'E', "Delinquent" },
98 { 'G', "Deficient and Bankrupt" },
99 { 'H', "Deficient and Delinquent" },
100 { 'J', "Delinquent and Bankrupt" },
101 { 'K', "Deficient, Delinquent and Bankrupt" },
102 { ' ', "Company is in compliance" },
106 static const value_string round_lots_only_val[] = {
107 { 'Y', "only round lots are accepted in this stock" },
108 { 'N', "odd/mixed lots are allowed" },
112 /* Initialize the protocol and registered fields */
113 static int proto_nasdaq_itch = -1;
115 /* Initialize the subtree pointers */
116 static gint ett_nasdaq_itch = -1;
118 static int hf_nasdaq_itch_version = -1;
120 static int hf_nasdaq_itch_message_type = -1;
121 static int hf_nasdaq_itch_market_category = -1;
122 static int hf_nasdaq_itch_financial_status = -1;
123 static int hf_nasdaq_itch_stock = -1;
124 static int hf_nasdaq_itch_round_lot_size = -1;
125 static int hf_nasdaq_itch_round_lots_only = -1;
127 static int hf_nasdaq_itch_system_event = -1;
128 static int hf_nasdaq_itch_second = -1;
129 static int hf_nasdaq_itch_millisecond = -1;
131 static int hf_nasdaq_itch_message = -1;
133 static int hf_nasdaq_itch_trading_state = -1;
134 static int hf_nasdaq_itch_reserved = -1;
135 static int hf_nasdaq_itch_reason = -1;
136 static int hf_nasdaq_itch_order_reference = -1;
137 static int hf_nasdaq_itch_buy_sell = -1;
138 static int hf_nasdaq_itch_shares = -1;
139 static int hf_nasdaq_itch_price = -1;
140 static int hf_nasdaq_itch_attribution = -1;
141 static int hf_nasdaq_itch_executed = -1;
142 static int hf_nasdaq_itch_match = -1;
143 static int hf_nasdaq_itch_printable = -1;
144 static int hf_nasdaq_itch_execution_price = -1;
145 static int hf_nasdaq_itch_canceled = -1;
146 static int hf_nasdaq_itch_cross = -1;
148 #define PINFO_COL(a) (check_col((a)->cinfo, COL_INFO))
150 /* atou(ll) like functions for NOT 0 terminated string
151 assume it doesn't overflow
154 static guint32 nasdaq_itch_atou(const char *str_value, int size)
157 const char *ptr = str_value;
160 for (i = 0; i < size && *ptr == ' '; i++, ptr++) {
164 for (; i < size; i++, ptr++) {
165 value = value*10 + *ptr - '0';
170 /* ---------------------- */
171 static guint64 nasdaq_itch_atoull(const char *str_value, int size)
174 const char *ptr = str_value;
177 for (i = 0; i < size && *ptr == ' '; i++, ptr++) {
181 for (; i < size; i++, ptr++) {
182 value = value*10 + *ptr - '0';
187 /* ---------------------- */
189 order_ref_number(tvbuff_t *tvb, packet_info *pinfo, proto_tree *nasdaq_itch_tree, int offset)
191 gint col_info = PINFO_COL(pinfo);
192 const char *str_value = tvb_get_ptr(tvb, offset, 9);
194 if (nasdaq_itch_tree || col_info) {
195 guint32 value = nasdaq_itch_atou(str_value, 9);
197 proto_tree_add_uint(nasdaq_itch_tree, hf_nasdaq_itch_order_reference, tvb, offset, 9, value);
199 col_append_fstr(pinfo->cinfo, COL_INFO, "%u ", value);
205 /* -------------------------- */
207 time_stamp(tvbuff_t *tvb, proto_tree *nasdaq_itch_tree, int id, int offset, int size)
209 const char *str_value = tvb_get_ptr(tvb, offset, size);
211 if (nasdaq_itch_tree) {
215 ms = val = nasdaq_itch_atou(str_value, size);
218 display = ep_strdup_printf(" %03u" , val);
222 case 8: /* 0 86 400 000 */
223 display = ep_strdup_printf(" %u (%02u:%02u:%02u.%03u)", val,
224 ms/3600000, (ms % 3600000)/60000, (ms % 60000)/1000, ms %1000);
227 proto_tree_add_uint_format_value(nasdaq_itch_tree, id, tvb, offset, size, val, "%s", display);
232 /* -------------------------- */
234 number_of_shares(tvbuff_t *tvb, packet_info *pinfo, proto_tree *nasdaq_itch_tree, int id, int offset, int big)
236 gint col_info = PINFO_COL(pinfo);
237 gint size = (big)?10:6;
238 const char *str_value = tvb_get_ptr(tvb, offset, size);
240 if (nasdaq_itch_tree || col_info) {
241 guint32 value = nasdaq_itch_atou(str_value, size);
243 proto_tree_add_uint(nasdaq_itch_tree, id, tvb, offset, size, value);
245 col_append_fstr(pinfo->cinfo, COL_INFO, "qty %u ", value);
251 /* -------------------------- */
253 price(tvbuff_t *tvb, packet_info *pinfo, proto_tree *nasdaq_itch_tree, int id, int offset, int big)
255 gint col_info = PINFO_COL(pinfo);
256 gint size = (big)?19:10;
258 if (nasdaq_itch_tree || col_info) {
259 const char *str_value = tvb_get_ptr(tvb, offset, size);
260 gdouble value = guint64_to_gdouble(nasdaq_itch_atoull(str_value, size))/((big)?1000000.0:10000.0);
262 proto_tree_add_double(nasdaq_itch_tree, id, tvb, offset, size, value);
264 col_append_fstr(pinfo->cinfo, COL_INFO, "price %g ", value);
270 /* -------------------------- */
272 stock(tvbuff_t *tvb, packet_info *pinfo, proto_tree *nasdaq_itch_tree, int offset)
274 gint col_info = PINFO_COL(pinfo);
275 if (nasdaq_itch_tree || col_info) {
276 char *stock = tvb_get_ephemeral_string(tvb, offset, 6);
278 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_stock, tvb, offset, 6, FALSE);
280 col_append_fstr(pinfo->cinfo, COL_INFO, "<%s> ", stock);
286 /* -------------------------- */
288 order(tvbuff_t *tvb, packet_info *pinfo, proto_tree *nasdaq_itch_tree, int offset, int big)
290 gint col_info = PINFO_COL(pinfo);
293 offset = order_ref_number(tvb, pinfo, nasdaq_itch_tree, offset);
295 value = tvb_get_guint8(tvb, offset);
297 col_append_fstr(pinfo->cinfo, COL_INFO, "%c ", value);
299 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_buy_sell, tvb, offset, 1, FALSE);
302 offset = number_of_shares(tvb, pinfo, nasdaq_itch_tree, hf_nasdaq_itch_shares, offset, big);
304 offset = stock(tvb, pinfo, nasdaq_itch_tree, offset);
306 offset = price(tvb, pinfo, nasdaq_itch_tree, hf_nasdaq_itch_price, offset, big);
310 /* -------------------------- */
312 executed(tvbuff_t *tvb, packet_info *pinfo, proto_tree *nasdaq_itch_tree, int offset, int big)
314 offset = order_ref_number(tvb, pinfo, nasdaq_itch_tree, offset);
316 offset = number_of_shares(tvb, pinfo, nasdaq_itch_tree, hf_nasdaq_itch_executed, offset, big);
318 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_match, tvb, offset, 9, FALSE);
323 /* ---------------------------- */
325 dissect_nasdaq_itch(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
328 proto_tree *nasdaq_itch_tree = NULL;
329 guint8 nasdaq_itch_type;
335 col_info = PINFO_COL(pinfo);
337 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Nasdaq-ITCH");
339 nasdaq_itch_type = tvb_get_guint8(tvb, offset);
340 if (nasdaq_itch_type >= '0' && nasdaq_itch_type <= '9') {
342 nasdaq_itch_type = tvb_get_guint8(tvb, offset +8);
345 if ((!nasdaq_itch_chi_x || version == 3) && strchr(chix_msg, nasdaq_itch_type)) {
346 nasdaq_itch_type = 0; /* unknown */
348 if (col_info || tree) {
349 const gchar *rep = val_to_str(nasdaq_itch_type, message_types_val, "Unknown packet type (0x%02x) ");
351 col_clear(pinfo->cinfo, COL_INFO);
352 col_add_str(pinfo->cinfo, COL_INFO, rep);
357 ti = proto_tree_add_protocol_format(tree, proto_nasdaq_itch, tvb, offset, -1, "Nasdaq TotalView-ITCH %s, %s",
358 version == 2?"2.0":"3.0", rep);
360 nasdaq_itch_tree = proto_item_add_subtree(ti, ett_nasdaq_itch);
362 item=proto_tree_add_uint(nasdaq_itch_tree, hf_nasdaq_itch_version, tvb, 0, 0, version);
363 PROTO_ITEM_SET_GENERATED(item);
368 offset = time_stamp (tvb, nasdaq_itch_tree, hf_nasdaq_itch_millisecond, offset, 8);
371 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_message_type, tvb, offset, 1, FALSE);
375 switch (nasdaq_itch_type) {
376 case 'T': /* seconds */
377 offset = time_stamp (tvb, nasdaq_itch_tree, hf_nasdaq_itch_second, offset, 5);
380 case 'M': /* milliseconds */
381 offset = time_stamp (tvb, nasdaq_itch_tree, hf_nasdaq_itch_millisecond, offset, 3);
386 switch (nasdaq_itch_type) {
387 case 'S': /* system event */
388 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_system_event, tvb, offset, 1, FALSE);
392 case 'R': /* Stock Directory */
393 offset = stock(tvb, pinfo, nasdaq_itch_tree, offset);
395 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_market_category, tvb, offset, 1, FALSE);
397 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_financial_status, tvb, offset, 1, FALSE);
399 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_round_lot_size, tvb, offset, 6, FALSE);
401 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_round_lots_only, tvb, offset, 1, FALSE);
405 case 'H': /* Stock trading action */
406 offset = stock(tvb, pinfo, nasdaq_itch_tree, offset);
408 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_trading_state, tvb, offset, 1, FALSE);
410 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_reserved, tvb, offset, 1, FALSE);
412 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_reason, tvb, offset, 4, FALSE);
418 case 'A': /* Add order, no MPID */
419 offset = order(tvb, pinfo, nasdaq_itch_tree, offset, big);
421 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_printable, tvb, offset, 1, FALSE);
426 case 'F': /* Add order, MPID */
427 offset = order(tvb, pinfo, nasdaq_itch_tree, offset, big);
428 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_attribution, tvb, offset, 4, FALSE);
434 case 'E' : /* Order executed */
435 offset = executed(tvb, pinfo, nasdaq_itch_tree, offset, big);
438 case 'C' : /* Order executed with price */
439 offset = executed(tvb, pinfo, nasdaq_itch_tree, offset, big);
440 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_printable, tvb, offset, 1, FALSE);
443 offset = price(tvb, pinfo, nasdaq_itch_tree, hf_nasdaq_itch_execution_price, offset, big);
448 case 'X' : /* Order cancel */
449 offset = order_ref_number(tvb, pinfo, nasdaq_itch_tree, offset);
450 offset = number_of_shares(tvb, pinfo, nasdaq_itch_tree, hf_nasdaq_itch_canceled, offset, big);
453 case 'D' : /* Order delete */
454 offset = order_ref_number(tvb, pinfo, nasdaq_itch_tree, offset);
460 case 'P' : /* Trade identifier */
461 offset = order(tvb, pinfo, nasdaq_itch_tree, offset, big);
462 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_match, tvb, offset, 9, FALSE);
466 case 'Q' : /* Cross Trade */
467 offset = number_of_shares(tvb, pinfo, nasdaq_itch_tree, hf_nasdaq_itch_shares, offset, big);
469 offset = stock(tvb, pinfo, nasdaq_itch_tree, offset);
471 offset = price(tvb, pinfo, nasdaq_itch_tree, hf_nasdaq_itch_price, offset, big);
473 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_match, tvb, offset, 9, FALSE);
475 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_cross, tvb, offset, 1, FALSE);
479 case 'B' : /* Broken Trade */
480 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_match, tvb, offset, 9, FALSE);
484 case 'I': /* NOII, FIXME */
485 offset = stock(tvb, pinfo, nasdaq_itch_tree, offset);
487 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_cross, tvb, offset, 1, FALSE);
493 proto_tree_add_item(nasdaq_itch_tree, hf_nasdaq_itch_message, tvb, offset, -1, FALSE);
499 /* Register the protocol with Wireshark */
502 proto_register_nasdaq_itch(void)
505 /* Setup list of header fields See Section 1.6.1 for details*/
506 static hf_register_info hf[] = {
507 { &hf_nasdaq_itch_version,
508 { "Version", "nasdaq-itch.version",
509 FT_UINT8, BASE_DEC, NULL, 0x0,
512 { &hf_nasdaq_itch_message_type,
513 { "Message Type", "nasdaq-itch.message_type",
514 FT_UINT8, BASE_DEC, VALS(message_types_val), 0x0,
517 { &hf_nasdaq_itch_second,
518 { "Second", "nasdaq-itch.second",
519 FT_UINT32, BASE_DEC, NULL, 0x0,
522 { &hf_nasdaq_itch_millisecond,
523 { "Millisecond", "nasdaq-itch.millisecond",
524 FT_UINT32, BASE_DEC, NULL, 0x0,
527 { &hf_nasdaq_itch_system_event,
528 { "System Event", "nasdaq-itch.system_event",
529 FT_UINT8, BASE_DEC, VALS(system_event_val), 0x0,
532 { &hf_nasdaq_itch_market_category,
533 { "Market Category", "nasdaq-itch.market_category",
534 FT_UINT8, BASE_DEC, VALS(market_category_val), 0x0,
537 { &hf_nasdaq_itch_financial_status,
538 { "Financial Status Indicator", "nasdaq-itch.financial_status",
539 FT_UINT8, BASE_DEC, VALS(financial_status_val), 0x0,
542 { &hf_nasdaq_itch_stock,
543 { "Stock", "nasdaq-itch.stock",
544 FT_STRING, BASE_NONE, NULL, 0x0,
547 { &hf_nasdaq_itch_round_lot_size,
548 { "Round Lot Size", "nasdaq-itch.round_lot_size",
549 FT_STRING, BASE_NONE, NULL, 0x0,
552 { &hf_nasdaq_itch_round_lots_only,
553 { "Round Lots Only", "nasdaq-itch.round_lots_only",
554 FT_UINT8, BASE_DEC, VALS(round_lots_only_val), 0x0,
557 { &hf_nasdaq_itch_trading_state,
558 { "Trading State", "nasdaq-itch.trading_state",
559 FT_STRING, BASE_NONE, NULL, 0x0,
562 { &hf_nasdaq_itch_reserved,
563 { "Reserved", "nasdaq-itch.reserved",
564 FT_STRING, BASE_NONE, NULL, 0x0,
567 { &hf_nasdaq_itch_reason,
568 { "Reason", "nasdaq-itch.reason",
569 FT_STRING, BASE_NONE, NULL, 0x0,
572 { &hf_nasdaq_itch_order_reference,
573 { "Order Reference", "nasdaq-itch.order_reference",
574 FT_UINT32, BASE_DEC, NULL, 0x0,
575 "Order reference number", HFILL }},
577 { &hf_nasdaq_itch_buy_sell,
578 { "Buy/Sell", "nasdaq-itch.buy_sell",
579 FT_STRING, BASE_NONE, NULL, 0x0,
580 "Buy/Sell indicator", HFILL }},
582 { &hf_nasdaq_itch_shares,
583 { "Shares", "nasdaq-itch.shares",
584 FT_UINT32, BASE_DEC, NULL, 0x0,
585 "Number of shares", HFILL }},
587 { &hf_nasdaq_itch_price,
588 { "Price", "nasdaq-itch.price",
589 FT_DOUBLE, BASE_NONE, NULL, 0x0,
592 { &hf_nasdaq_itch_attribution,
593 { "Attribution", "nasdaq-itch.attribution",
594 FT_STRING, BASE_NONE, NULL, 0x0,
595 "Market participant identifier", HFILL }},
597 { &hf_nasdaq_itch_executed,
598 { "Executed Shares", "nasdaq-itch.executed",
599 FT_UINT32, BASE_DEC, NULL, 0x0,
600 "Number of shares executed", HFILL }},
602 { &hf_nasdaq_itch_match,
603 { "Matched", "nasdaq-itch.match",
604 FT_STRING, BASE_NONE, NULL, 0x0,
605 "Match number", HFILL }},
607 { &hf_nasdaq_itch_printable,
608 { "Printable", "nasdaq-itch.printable",
609 FT_STRING, BASE_NONE, NULL, 0x0,
612 { &hf_nasdaq_itch_execution_price,
613 { "Execution Price", "nasdaq-itch.execution_price",
614 FT_DOUBLE, BASE_NONE, NULL, 0x0,
617 { &hf_nasdaq_itch_canceled,
618 { "Canceled Shares", "nasdaq-itch.canceled",
619 FT_UINT32, BASE_DEC, NULL, 0x0,
620 "Number of shares to be removed", HFILL }},
622 { &hf_nasdaq_itch_cross,
623 { "Cross Type", "nasdaq-itch.cross",
624 FT_STRING, BASE_NONE, NULL, 0x0,
625 "Cross trade type", HFILL }},
627 { &hf_nasdaq_itch_message,
628 { "Message", "nasdaq-itch.message",
629 FT_STRING, BASE_NONE, NULL, 0x0,
633 /* Setup protocol subtree array */
634 static gint *ett[] = {
638 module_t *nasdaq_itch_module;
640 /* Register the protocol name and description */
641 proto_nasdaq_itch = proto_register_protocol("Nasdaq TotalView-ITCH", "NASDAQ-ITCH", "nasdaq_itch");
643 /* Required function calls to register the header fields and subtrees used */
644 proto_register_field_array(proto_nasdaq_itch, hf, array_length(hf));
645 proto_register_subtree_array(ett, array_length(ett));
647 nasdaq_itch_module = prefs_register_protocol(proto_nasdaq_itch, NULL);
648 prefs_register_bool_preference(nasdaq_itch_module, "chi_x", "Decode Chi X extensions",
649 "Whether the Nasdaq ITCH dissector should decode Chi X extensions.",
652 register_dissector("nasdaq-itch", dissect_nasdaq_itch, proto_nasdaq_itch);
656 /* If this dissector uses sub-dissector registration add a registration routine.
657 This format is required because a script is used to find these routines and
658 create the code that calls these routines.
661 proto_reg_handoff_nasdaq_itch(void)