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>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
12 * http://www.nasdaqtrader.com/Trader.aspx?id=DPSpecs
14 * http://www.nasdaqtrader.com/content/technicalsupport/specifications/dataproducts/tv-itch2a.pdf
15 * http://www.nasdaqtrader.com/content/technicalsupport/specifications/dataproducts/tvitch-v3.pdf
18 * http://www.chi-x.com/docs/Chi-X%20CHIXMD.pdf
26 #include <epan/packet.h>
27 #include <epan/prefs.h>
28 #include <wsutil/type_util.h>
30 void proto_register_nasdaq_itch(void);
31 void proto_reg_handoff_nasdaq_itch(void);
33 static dissector_handle_t nasdaq_itch_handle
;
36 static bool nasdaq_itch_chi_x
= true;
38 static const value_string message_types_val
[] = {
39 { 'A', "Add Order " },
40 { 'X', "Order Cancel " },
41 { 'M', "Milliseconds " },
42 { 'E', "Order Executed " },
44 { 'P', "Trade Message Identifier " },
45 { 'C', "Order Executed With Price " },
46 { 'D', "Order Delete " },
47 { 'Q', "Cross Trade " },
48 { 'S', "System Event " },
49 { 'R' , "Stock Directory " },
50 { 'H', "Stock Trading Action " },
51 { 'F', "Add Order (MPID) " },
52 { 'I', "Net Order Imbalance Indicator (NOII) " },
53 { 'B', "Broken Trade " },
54 /* Chi-X msg with big size,price */
55 { 'a', "Add Order (big)" },
56 { 'p', "Trade Message Identifier (big)" },
57 { 'e', "Order Executed (big)" },
58 { 'x', "Order Cancel (big)" },
62 static const char chix_msg
[] = "apex";
64 static const value_string system_event_val
[] = {
65 { 'O', "Start of Messages" },
66 { 'S', "Start of System hours" },
67 { 'Q', "Start of Market hours" },
68 { 'M', "End of Market hours" },
69 { 'E', "End of System hours" },
70 { 'C', "End of Messages" },
74 static const value_string market_category_val
[] = {
75 { 'T', "CQS (NYSE, Amex or regional exchange)" },
76 { 'Q', "NASDAQ Global Select MarketSM" },
77 { 'G', "NASDAQ Global MarketSM" },
78 { 'S', "NASDAQ Capital Market" },
79 { ' ', "Not available" },
83 static const value_string financial_status_val
[] = {
85 { 'E', "Delinquent" },
88 { 'G', "Deficient and Bankrupt" },
89 { 'H', "Deficient and Delinquent" },
90 { 'J', "Delinquent and Bankrupt" },
91 { 'K', "Deficient, Delinquent and Bankrupt" },
92 { ' ', "Company is in compliance" },
96 static const value_string round_lots_only_val
[] = {
97 { 'Y', "only round lots are accepted in this stock" },
98 { 'N', "odd/mixed lots are allowed" },
102 /* Initialize the protocol and registered fields */
103 static int proto_nasdaq_itch
;
105 /* Initialize the subtree pointers */
106 static int ett_nasdaq_itch
;
108 static int hf_nasdaq_itch_version
;
110 static int hf_nasdaq_itch_message_type
;
111 static int hf_nasdaq_itch_market_category
;
112 static int hf_nasdaq_itch_financial_status
;
113 static int hf_nasdaq_itch_stock
;
114 static int hf_nasdaq_itch_round_lot_size
;
115 static int hf_nasdaq_itch_round_lots_only
;
117 static int hf_nasdaq_itch_system_event
;
118 static int hf_nasdaq_itch_second
;
119 static int hf_nasdaq_itch_millisecond
;
121 static int hf_nasdaq_itch_message
;
123 static int hf_nasdaq_itch_trading_state
;
124 static int hf_nasdaq_itch_reserved
;
125 static int hf_nasdaq_itch_reason
;
126 static int hf_nasdaq_itch_order_reference
;
127 static int hf_nasdaq_itch_buy_sell
;
128 static int hf_nasdaq_itch_shares
;
129 static int hf_nasdaq_itch_price
;
130 static int hf_nasdaq_itch_attribution
;
131 static int hf_nasdaq_itch_executed
;
132 static int hf_nasdaq_itch_match
;
133 static int hf_nasdaq_itch_printable
;
134 static int hf_nasdaq_itch_execution_price
;
135 static int hf_nasdaq_itch_canceled
;
136 static int hf_nasdaq_itch_cross
;
138 /* ---------------------- */
140 order_ref_number(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*nasdaq_itch_tree
, int offset
)
142 const char *str_value
= tvb_get_string_enc(pinfo
->pool
, tvb
, offset
, 9, ENC_ASCII
);
143 uint32_t value
= (uint32_t)strtoul(str_value
, NULL
, 10);
145 proto_tree_add_uint(nasdaq_itch_tree
, hf_nasdaq_itch_order_reference
, tvb
, offset
, 9, value
);
146 col_append_fstr(pinfo
->cinfo
, COL_INFO
, "%u ", value
);
151 /* -------------------------- */
153 time_stamp(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*nasdaq_itch_tree
, int id
, int offset
, int size
)
156 if (nasdaq_itch_tree
) {
158 const char *display
= "";
159 const char *str_value
= tvb_get_string_enc(pinfo
->pool
, tvb
, offset
, size
, ENC_ASCII
);
161 ms
= val
= (uint32_t)strtoul(str_value
, NULL
, 10);
164 display
= wmem_strdup_printf(pinfo
->pool
, " %03u" , val
);
169 case 8: /* 0 86 400 000 */
170 display
= wmem_strdup_printf(pinfo
->pool
, " %u (%02u:%02u:%02u.%03u)", val
,
171 ms
/3600000, (ms
% 3600000)/60000, (ms
% 60000)/1000, ms
%1000);
174 proto_tree_add_uint_format_value(nasdaq_itch_tree
, id
, tvb
, offset
, size
, val
, "%s", display
);
176 return offset
+ size
;
179 /* -------------------------- */
181 number_of_shares(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*nasdaq_itch_tree
, int id
, int offset
, int big
)
183 int size
= (big
) ? 10 : 6;
184 const char *str_value
= tvb_get_string_enc(pinfo
->pool
, tvb
, offset
, size
, ENC_ASCII
);
186 uint32_t value
= (uint32_t)strtoul(str_value
, NULL
, 10);
188 proto_tree_add_uint(nasdaq_itch_tree
, id
, tvb
, offset
, size
, value
);
189 col_append_fstr(pinfo
->cinfo
, COL_INFO
, "qty %u ", value
);
191 return offset
+ size
;
194 /* -------------------------- */
196 price(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*nasdaq_itch_tree
, int id
, int offset
, int big
)
198 int size
= (big
) ? 19 : 10;
200 const char *str_value
= tvb_get_string_enc(pinfo
->pool
, tvb
, offset
, size
, ENC_ASCII
);
201 double value
= uint64_to_double(g_ascii_strtoull(str_value
, NULL
, 10))/((big
)?1000000.0:10000.0);
203 proto_tree_add_double(nasdaq_itch_tree
, id
, tvb
, offset
, size
, value
);
204 col_append_fstr(pinfo
->cinfo
, COL_INFO
, "price %g ", value
);
206 return offset
+ size
;
209 /* -------------------------- */
211 stock(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*nasdaq_itch_tree
, int offset
)
213 char *stock_p
= tvb_get_string_enc(pinfo
->pool
, tvb
, offset
, 6, ENC_ASCII
);
215 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_stock
, tvb
, offset
, 6, ENC_ASCII
);
216 col_append_fstr(pinfo
->cinfo
, COL_INFO
, "<%s> ", stock_p
);
221 /* -------------------------- */
223 order(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*nasdaq_itch_tree
, int offset
, int big
)
227 offset
= order_ref_number(tvb
, pinfo
, nasdaq_itch_tree
, offset
);
229 value
= tvb_get_uint8(tvb
, offset
);
231 col_append_fstr(pinfo
->cinfo
, COL_INFO
, "%c ", value
);
232 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_buy_sell
, tvb
, offset
, 1, ENC_ASCII
);
235 offset
= number_of_shares(tvb
, pinfo
, nasdaq_itch_tree
, hf_nasdaq_itch_shares
, offset
, big
);
237 offset
= stock(tvb
, pinfo
, nasdaq_itch_tree
, offset
);
239 offset
= price(tvb
, pinfo
, nasdaq_itch_tree
, hf_nasdaq_itch_price
, offset
, big
);
243 /* -------------------------- */
245 executed(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*nasdaq_itch_tree
, int offset
, int big
)
247 offset
= order_ref_number(tvb
, pinfo
, nasdaq_itch_tree
, offset
);
249 offset
= number_of_shares(tvb
, pinfo
, nasdaq_itch_tree
, hf_nasdaq_itch_executed
, offset
, big
);
251 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_match
, tvb
, offset
, 9, ENC_ASCII
);
256 /* ---------------------------- */
258 dissect_nasdaq_itch(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void* data _U_
)
261 proto_tree
*nasdaq_itch_tree
= NULL
;
262 uint8_t nasdaq_itch_type
;
268 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "Nasdaq-ITCH");
270 nasdaq_itch_type
= tvb_get_uint8(tvb
, offset
);
271 if (nasdaq_itch_type
>= '0' && nasdaq_itch_type
<= '9') {
273 nasdaq_itch_type
= tvb_get_uint8(tvb
, offset
+8);
276 if ((!nasdaq_itch_chi_x
|| version
== 3) && strchr(chix_msg
, nasdaq_itch_type
)) {
277 nasdaq_itch_type
= 0; /* unknown */
280 rep
= val_to_str(nasdaq_itch_type
, message_types_val
, "Unknown packet type (0x%02x) ");
281 col_add_str(pinfo
->cinfo
, COL_INFO
, rep
);
286 ti
= proto_tree_add_protocol_format(tree
, proto_nasdaq_itch
, tvb
, offset
, -1, "Nasdaq TotalView-ITCH %s, %s",
287 version
== 2?"2.0":"3.0", rep
);
289 nasdaq_itch_tree
= proto_item_add_subtree(ti
, ett_nasdaq_itch
);
291 item
= proto_tree_add_uint(nasdaq_itch_tree
, hf_nasdaq_itch_version
, tvb
, 0, 0, version
);
292 proto_item_set_generated(item
);
296 offset
= time_stamp (tvb
, pinfo
, nasdaq_itch_tree
, hf_nasdaq_itch_millisecond
, offset
, 8);
299 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_message_type
, tvb
, offset
, 1, ENC_ASCII
|ENC_NA
);
303 switch (nasdaq_itch_type
) {
304 case 'T': /* seconds */
305 /*offset =*/ time_stamp (tvb
, pinfo
, nasdaq_itch_tree
, hf_nasdaq_itch_second
, offset
, 5);
306 return tvb_captured_length(tvb
);
308 case 'M': /* milliseconds */
309 /*offset =*/ time_stamp (tvb
, pinfo
, nasdaq_itch_tree
, hf_nasdaq_itch_millisecond
, offset
, 3);
310 return tvb_captured_length(tvb
);
314 switch (nasdaq_itch_type
) {
315 case 'S': /* system event */
316 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_system_event
, tvb
, offset
, 1, ENC_ASCII
|ENC_NA
);
320 case 'R': /* Stock Directory */
321 offset
= stock(tvb
, pinfo
, nasdaq_itch_tree
, offset
);
323 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_market_category
, tvb
, offset
, 1, ENC_ASCII
|ENC_NA
);
325 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_financial_status
, tvb
, offset
, 1, ENC_ASCII
|ENC_NA
);
327 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_round_lot_size
, tvb
, offset
, 6, ENC_ASCII
);
329 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_round_lots_only
, tvb
, offset
, 1, ENC_ASCII
|ENC_NA
);
333 case 'H': /* Stock trading action */
334 offset
= stock(tvb
, pinfo
, nasdaq_itch_tree
, offset
);
336 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_trading_state
, tvb
, offset
, 1, ENC_ASCII
);
338 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_reserved
, tvb
, offset
, 1, ENC_ASCII
);
340 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_reason
, tvb
, offset
, 4, ENC_ASCII
);
347 case 'A': /* Add order, no MPID */
348 offset
= order(tvb
, pinfo
, nasdaq_itch_tree
, offset
, big
);
350 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_printable
, tvb
, offset
, 1, ENC_ASCII
);
355 case 'F': /* Add order, MPID */
356 offset
= order(tvb
, pinfo
, nasdaq_itch_tree
, offset
, big
);
357 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_attribution
, tvb
, offset
, 4, ENC_ASCII
);
364 case 'E' : /* Order executed */
365 /*offset =*/ executed(tvb
, pinfo
, nasdaq_itch_tree
, offset
, big
);
368 case 'C' : /* Order executed with price */
369 offset
= executed(tvb
, pinfo
, nasdaq_itch_tree
, offset
, big
);
370 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_printable
, tvb
, offset
, 1, ENC_ASCII
);
373 /*offset = */price(tvb
, pinfo
, nasdaq_itch_tree
, hf_nasdaq_itch_execution_price
, offset
, big
);
379 case 'X' : /* Order cancel */
380 offset
= order_ref_number(tvb
, pinfo
, nasdaq_itch_tree
, offset
);
381 /*offset = */number_of_shares(tvb
, pinfo
, nasdaq_itch_tree
, hf_nasdaq_itch_canceled
, offset
, big
);
384 case 'D' : /* Order delete */
385 /*offset = */order_ref_number(tvb
, pinfo
, nasdaq_itch_tree
, offset
);
392 case 'P' : /* Trade identifier */
393 offset
= order(tvb
, pinfo
, nasdaq_itch_tree
, offset
, big
);
394 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_match
, tvb
, offset
, 9, ENC_ASCII
);
398 case 'Q' : /* Cross Trade */
399 offset
= number_of_shares(tvb
, pinfo
, nasdaq_itch_tree
, hf_nasdaq_itch_shares
, offset
, big
);
401 offset
= stock(tvb
, pinfo
, nasdaq_itch_tree
, offset
);
403 offset
= price(tvb
, pinfo
, nasdaq_itch_tree
, hf_nasdaq_itch_price
, offset
, big
);
405 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_match
, tvb
, offset
, 9, ENC_ASCII
);
407 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_cross
, tvb
, offset
, 1, ENC_ASCII
);
411 case 'B' : /* Broken Trade */
412 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_match
, tvb
, offset
, 9, ENC_ASCII
);
416 case 'I': /* NOII, FIXME */
417 offset
= stock(tvb
, pinfo
, nasdaq_itch_tree
, offset
);
419 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_cross
, tvb
, offset
, 1, ENC_ASCII
);
425 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_message
, tvb
, offset
, -1, ENC_ASCII
);
429 return tvb_captured_length(tvb
);
432 /* Register the protocol with Wireshark */
434 proto_register_nasdaq_itch(void)
437 /* Setup list of header fields See Section 1.6.1 for details*/
438 static hf_register_info hf
[] = {
439 { &hf_nasdaq_itch_version
,
440 { "Version", "nasdaq-itch.version",
441 FT_UINT8
, BASE_DEC
, NULL
, 0x0,
444 { &hf_nasdaq_itch_message_type
,
445 { "Message Type", "nasdaq-itch.message_type",
446 FT_CHAR
, BASE_HEX
, VALS(message_types_val
), 0x0,
449 { &hf_nasdaq_itch_second
,
450 { "Second", "nasdaq-itch.second",
451 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
454 { &hf_nasdaq_itch_millisecond
,
455 { "Millisecond", "nasdaq-itch.millisecond",
456 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
459 { &hf_nasdaq_itch_system_event
,
460 { "System Event", "nasdaq-itch.system_event",
461 FT_CHAR
, BASE_HEX
, VALS(system_event_val
), 0x0,
464 { &hf_nasdaq_itch_market_category
,
465 { "Market Category", "nasdaq-itch.market_category",
466 FT_CHAR
, BASE_HEX
, VALS(market_category_val
), 0x0,
469 { &hf_nasdaq_itch_financial_status
,
470 { "Financial Status Indicator", "nasdaq-itch.financial_status",
471 FT_CHAR
, BASE_HEX
, VALS(financial_status_val
), 0x0,
474 { &hf_nasdaq_itch_stock
,
475 { "Stock", "nasdaq-itch.stock",
476 FT_STRING
, BASE_NONE
, NULL
, 0x0,
479 { &hf_nasdaq_itch_round_lot_size
,
480 { "Round Lot Size", "nasdaq-itch.round_lot_size",
481 FT_STRING
, BASE_NONE
, NULL
, 0x0,
484 { &hf_nasdaq_itch_round_lots_only
,
485 { "Round Lots Only", "nasdaq-itch.round_lots_only",
486 FT_CHAR
, BASE_HEX
, VALS(round_lots_only_val
), 0x0,
489 { &hf_nasdaq_itch_trading_state
,
490 { "Trading State", "nasdaq-itch.trading_state",
491 FT_STRING
, BASE_NONE
, NULL
, 0x0,
494 { &hf_nasdaq_itch_reserved
,
495 { "Reserved", "nasdaq-itch.reserved",
496 FT_STRING
, BASE_NONE
, NULL
, 0x0,
499 { &hf_nasdaq_itch_reason
,
500 { "Reason", "nasdaq-itch.reason",
501 FT_STRING
, BASE_NONE
, NULL
, 0x0,
504 { &hf_nasdaq_itch_order_reference
,
505 { "Order Reference", "nasdaq-itch.order_reference",
506 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
507 "Order reference number", HFILL
}},
509 { &hf_nasdaq_itch_buy_sell
,
510 { "Buy/Sell", "nasdaq-itch.buy_sell",
511 FT_STRING
, BASE_NONE
, NULL
, 0x0,
512 "Buy/Sell indicator", HFILL
}},
514 { &hf_nasdaq_itch_shares
,
515 { "Shares", "nasdaq-itch.shares",
516 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
517 "Number of shares", HFILL
}},
519 { &hf_nasdaq_itch_price
,
520 { "Price", "nasdaq-itch.price",
521 FT_DOUBLE
, BASE_NONE
, NULL
, 0x0,
524 { &hf_nasdaq_itch_attribution
,
525 { "Attribution", "nasdaq-itch.attribution",
526 FT_STRING
, BASE_NONE
, NULL
, 0x0,
527 "Market participant identifier", HFILL
}},
529 { &hf_nasdaq_itch_executed
,
530 { "Executed Shares", "nasdaq-itch.executed",
531 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
532 "Number of shares executed", HFILL
}},
534 { &hf_nasdaq_itch_match
,
535 { "Matched", "nasdaq-itch.match",
536 FT_STRING
, BASE_NONE
, NULL
, 0x0,
537 "Match number", HFILL
}},
539 { &hf_nasdaq_itch_printable
,
540 { "Printable", "nasdaq-itch.printable",
541 FT_STRING
, BASE_NONE
, NULL
, 0x0,
544 { &hf_nasdaq_itch_execution_price
,
545 { "Execution Price", "nasdaq-itch.execution_price",
546 FT_DOUBLE
, BASE_NONE
, NULL
, 0x0,
549 { &hf_nasdaq_itch_canceled
,
550 { "Canceled Shares", "nasdaq-itch.canceled",
551 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
552 "Number of shares to be removed", HFILL
}},
554 { &hf_nasdaq_itch_cross
,
555 { "Cross Type", "nasdaq-itch.cross",
556 FT_STRING
, BASE_NONE
, NULL
, 0x0,
557 "Cross trade type", HFILL
}},
559 { &hf_nasdaq_itch_message
,
560 { "Message", "nasdaq-itch.message",
561 FT_STRING
, BASE_NONE
, NULL
, 0x0,
565 /* Setup protocol subtree array */
566 static int *ett
[] = {
570 module_t
*nasdaq_itch_module
;
572 /* Register the protocol name and description */
573 proto_nasdaq_itch
= proto_register_protocol("Nasdaq TotalView-ITCH", "NASDAQ-ITCH", "nasdaq_itch");
575 /* Required function calls to register the header fields and subtrees used */
576 proto_register_field_array(proto_nasdaq_itch
, hf
, array_length(hf
));
577 proto_register_subtree_array(ett
, array_length(ett
));
579 nasdaq_itch_module
= prefs_register_protocol(proto_nasdaq_itch
, NULL
);
580 prefs_register_bool_preference(nasdaq_itch_module
, "chi_x", "Decode Chi X extensions",
581 "Whether the Nasdaq ITCH dissector should decode Chi X extensions.",
584 nasdaq_itch_handle
= register_dissector("nasdaq-itch", dissect_nasdaq_itch
, proto_nasdaq_itch
);
588 proto_reg_handoff_nasdaq_itch(void)
590 dissector_add_for_decode_as("moldudp64.payload", nasdaq_itch_handle
);
591 dissector_add_for_decode_as("moldudp.payload", nasdaq_itch_handle
);
600 * indent-tabs-mode: nil
603 * ex: set shiftwidth=2 tabstop=8 expandtab:
604 * :indentSize=2:tabSize=8:noTabs=true: