2 * Routines for TDS NetLib dissection
3 * Copyright 2000-2002, Brian Bruns <camber@ais.org>
4 * Copyright 2002, Steve Langasek <vorlon@netexpress.net>
6 * $Id: packet-tds.c,v 1.27 2004/02/20 08:40:30 guy Exp $
8 * Ethereal - Network traffic analyzer
9 * By Gerald Combs <gerald@ethereal.com>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 * The NETLIB protocol is a small blocking protocol designed to allow TDS
29 * to be placed within different transports (TCP, DECNet, IPX/SPX). A
30 * NETLIB packet starts with an eight byte header containing:
32 * a one-byte packet type field;
34 * a one-byte status field;
36 * a two-byte big-endian size field giving the size of the packet,
37 * including the header;
39 * a two-byte big-endian channel number, used when multiple sessions
40 * are being multiplexed on a single connection;
42 * a one-byte packet number, giving "the frame number of a multiplexed
43 * message, modulo 256";
45 * a one-byte window, which is the number of frames to be sent
46 * before an acknowledgment message is received.
48 * followed by payload whose size is the value in the size field minus
51 * Microsoft Network Monitor 2.x dissects the 4 byte field (and indicates
52 * that the one-byte last packet indicator also contains other bits).
54 * The TDS protocol consists of a number of protocol data units (PDUs) that
55 * appear to be assembled from NETLIB packets, in the form of zero or more
56 * NETLIB packets with the last packet indicator clear and a final NETLIB
57 * packet with the last packet indicator set. The type of the TDS PDU is
58 * specified by the packet type field of the NETLIB header (presumably that
59 * field has the same value for all NETLIB packets that make up a TDS PDU).
61 * The "server response" PDU consists of a sequence of multiple items, each
62 * one beginning with a one byte type field at the start of the PDU. Some
63 * items are fixed length, some are variable length with a two byte size
64 * field following the item type, and then there is TDS_ROW_TOKEN in which
65 * size is determined by analyzing the result set returned from the server.
66 * This in effect means that we are hopelessly lost if we haven't seen the
67 * result set. Also, TDS 4/5 is byte order negotiable, which is specified
68 * in the login packet. We can attempt to determine it later on, but not
71 * Some preliminary documentation on the packet format can be found at
72 * http://www.freetds.org/tds.html
74 * Some more information can be found in
75 * http://download.nai.com/products/media/sniffer/support/sdos/sybase.pdf
77 * Much of this code was originally developed for the FreeTDS project.
78 * http://www.freetds.org
82 * Excerpts from Brian's posting to ethereal-dev:
84 * The TDS Protocol is actually a protocol within a protocol. On the outside
85 * there is netlib which is not so much a encapsulation as a blocking of the
86 * data, typically to 512 or 4096 bytes. Between this are the protocol data
87 * units for TDS. Netlib packets may be split over real packets, multiple
88 * netlib packets may appear in single real packets. TDS PDUs may be split
89 * over netlib packets (and real packets) and most certainly can appear
90 * multiple times within a netlib packet.
92 * Because of this, I abandoned my earlier attempt at making two dissectors,
93 * one for netlib and one for TDS. Counterintuitively, a single dissector
94 * turned out to be simpler than splitting it up.
96 * Here are some of the (hefty) limitations of the current code
98 * . We currently do not handle netlib headers that cross packet boundaries.
99 * This should be an easy fix.
100 * . I probably could have used the packet reassembly stuff, but I started
101 * this at version 0.8.20, so c'est la vie. It wouldn't have covered the
102 * netlib stuff anyway, so no big loss.
103 * . The older two layer version of the code dissected the PDU's, but the new
104 * version does not yet, it only labels the names. I need an elegant way to
105 * deal with dissecting data crossing (netlib and tcp) packet boundries. I
106 * think I have one, but ran out of time to do it.
107 * . It will only work on little endian platforms. Or rather I should say,
108 * the client that was captured must be little endian. TDS 7.0/8.0 is
109 * always LE; for TDS 4.2/5.0 look in the code for tvb_get_le*() functions,
110 * there are fields in the login packet which determine byte order.
111 * . result sets that span netlib packets are not working
112 * . TDS 7 and 4.2 result sets are not working yet
114 * All that said, the code does deal gracefully with different boudary
115 * conditions and what remains are the easier bits, IMHO.
117 * XXX - "real packets" means "TCP segments", for TCP.
119 * XXX - is it *REALLY* true that you can have more than one TDS PDU (as
120 * opposed to more than one server response item) per NETLIB packet? Or is
121 * all the data in a NETLIB packet put into a single TDS PDU? If so, then
122 * we can reassemble NETLIB packets using the standard TCP desegmentation
123 * code, and can reassemble TDS PDUs using "fragment_add_seq_check()",
124 * and more cleanly separate the NETLIB and TDS dissectors (although the
125 * "is this NETLIB" heuristic would have to look at TDS information past
126 * the NETLIB header, in order to make the heuristic strong enough not
127 * to get too many false positives; note that the heuristic should reject
128 * any putative NETLIB packet with a length field with a value < 8).
130 * That would substantially clean the dissector up, eliminating most of
131 * the per-packet data (we might still need information to handle
132 * TDS_ROW_TOKEN), getting rid of the stuff to handle data split across
133 * TCP segment boundaries in favor of simple reassembly code, and
134 * fixing some otherwise nasty-looking crashing bugs.
136 * NOTE: we assume that all the data in a NETLIB packet *can* be put into
137 * a single TDS PTU, so that we have separate reassembly of NETLIB
138 * packets and TDS PDUs; it seems to work, and it really did clean stuff
139 * up and fix crashes.
153 #include <epan/packet.h>
154 #include <epan/conversation.h>
155 #include <epan/strutil.h>
157 #include "packet-smb-common.h"
158 #include "packet-frame.h"
159 #include "reassemble.h"
162 #define TDS_QUERY_PKT 1
163 #define TDS_LOGIN_PKT 2
164 #define TDS_RPC_PKT 3
165 #define TDS_RESP_PKT 4
166 #define TDS_RAW_PKT 5
167 #define TDS_CANCEL_PKT 6
168 #define TDS_BULK_DATA_PKT 7
169 #define TDS_OPEN_CHN_PKT 8
170 #define TDS_CLOSE_CHN_PKT 9
171 #define TDS_RES_ERROR_PKT 10
172 #define TDS_LOG_CHN_ACK_PKT 11
173 #define TDS_ECHO_PKT 12
174 #define TDS_LOGOUT_CHN_PKT 13
175 #define TDS_QUERY5_PKT 15 /* or "Normal tokenized request or response */
176 #define TDS_LOGIN7_PKT 16 /* or "Urgent tokenized request or response */
177 #define TDS_XXX7_PKT 18 /* seen in one capture */
179 #define is_valid_tds_type(x) ((x) >= TDS_QUERY_PKT && (x) <= TDS_XXX7_PKT)
181 /* The following constants are imported more or less directly from FreeTDS */
183 #define TDS5_DYN_TOKEN 231 /* 0xE7 TDS 5.0 only */
184 #define TDS5_DYNRES_TOKEN 236 /* 0xEC TDS 5.0 only */
185 #define TDS5_DYN3_TOKEN 215 /* 0xD7 TDS 5.0 only */
186 #define TDS_LANG_TOKEN 33 /* 0x21 TDS 5.0 only */
187 #define TDS_CLOSE_TOKEN 113 /* 0x71 TDS 5.0 only? ct_close() */
188 #define TDS_RET_STAT_TOKEN 121 /* 0x79 */
189 #define TDS_124_TOKEN 124 /* 0x7C TDS 4.2 only - TDS_PROCID */
190 #define TDS7_RESULT_TOKEN 129 /* 0x81 TDS 7.0 only */
191 #define TDS_COL_NAME_TOKEN 160 /* 0xA0 TDS 4.2 only */
192 #define TDS_COL_INFO_TOKEN 161 /* 0xA1 TDS 4.2 only - TDS_COLFMT */
193 /*#define TDS_TABNAME 164 */
194 /*#define TDS_COL_INFO 165 */
195 #define TDS_167_TOKEN 167 /* 0xA7 */
196 #define TDS_168_TOKEN 168 /* 0xA8 */
197 #define TDS_ORDER_BY_TOKEN 169 /* 0xA9 TDS_ORDER */
198 #define TDS_ERR_TOKEN 170 /* 0xAA */
199 #define TDS_MSG_TOKEN 171 /* 0xAB */
200 #define TDS_PARAM_TOKEN 172 /* 0xAC RETURNVALUE? */
201 #define TDS_LOGIN_ACK_TOKEN 173 /* 0xAD */
202 #define TDS_174_TOKEN 174 /* 0xAE TDS_CONTROL */
203 #define TDS_ROW_TOKEN 209 /* 0xD1 */
204 #define TDS_CMP_ROW_TOKEN 211 /* 0xD3 */
205 #define TDS_CAP_TOKEN 226 /* 0xE2 */
206 #define TDS_ENV_CHG_TOKEN 227 /* 0xE3 */
207 #define TDS_EED_TOKEN 229 /* 0xE5 */
208 #define TDS_AUTH_TOKEN 237 /* 0xED */
209 #define TDS_RESULT_TOKEN 238 /* 0xEE */
210 #define TDS_DONE_TOKEN 253 /* 0xFD TDS_DONE */
211 #define TDS_DONEPROC_TOKEN 254 /* 0xFE TDS_DONEPROC */
212 #define TDS_DONEINPROC_TOKEN 255 /* 0xFF TDS_DONEINPROC */
214 #define SYBCHAR 47 /* 0x2F */
215 #define SYBVARCHAR 39 /* 0x27 */
216 #define SYBINTN 38 /* 0x26 */
217 #define SYBINT1 48 /* 0x30 */
218 #define SYBINT2 52 /* 0x34 */
219 #define SYBINT4 56 /* 0x38 */
220 #define SYBINT8 127 /* 0x7F */
221 #define SYBFLT8 62 /* 0x3E */
222 #define SYBDATETIME 61 /* 0x3D */
223 #define SYBBIT 50 /* 0x32 */
224 #define SYBTEXT 35 /* 0x23 */
225 #define SYBNTEXT 99 /* 0x63 */
226 #define SYBIMAGE 34 /* 0x22 */
227 #define SYBMONEY4 122 /* 0x7A */
228 #define SYBMONEY 60 /* 0x3C */
229 #define SYBDATETIME4 58 /* 0x3A */
230 #define SYBREAL 59 /* 0x3B */
231 #define SYBBINARY 45 /* 0x2D */
232 #define SYBVOID 31 /* 0x1F */
233 #define SYBVARBINARY 37 /* 0x25 */
234 #define SYBNVARCHAR 103 /* 0x67 */
235 #define SYBBITN 104 /* 0x68 */
236 #define SYBNUMERIC 108 /* 0x6C */
237 #define SYBDECIMAL 106 /* 0x6A */
238 #define SYBFLTN 109 /* 0x6D */
239 #define SYBMONEYN 110 /* 0x6E */
240 #define SYBDATETIMN 111 /* 0x6F */
241 #define XSYBCHAR 167 /* 0xA7 */
242 #define XSYBVARCHAR 175 /* 0xAF */
243 #define XSYBNVARCHAR 231 /* 0xE7 */
244 #define XSYBNCHAR 239 /* 0xEF */
245 #define SYBUNIQUE 0x24
246 #define SYBVARIANT 0x62
248 #define is_fixed_coltype(x) (x==SYBINT1 || \
261 /* Initialize the protocol and registered fields */
262 static int proto_tds = -1;
263 static int hf_tds_type = -1;
264 static int hf_tds_status = -1;
265 static int hf_tds_size = -1;
266 static int hf_tds_channel = -1;
267 static int hf_tds_packet_number = -1;
268 static int hf_tds_window = -1;
269 static int hf_tds_reassembled_in = -1;
270 static int hf_tds_fragments = -1;
271 static int hf_tds_fragment = -1;
272 static int hf_tds_fragment_overlap = -1;
273 static int hf_tds_fragment_overlap_conflict = -1;
274 static int hf_tds_fragment_multiple_tails = -1;
275 static int hf_tds_fragment_too_long_fragment = -1;
276 static int hf_tds_fragment_error = -1;
278 static int hf_tds7_login_total_size = -1;
279 static int hf_tds7_version = -1;
280 static int hf_tds7_packet_size = -1;
281 static int hf_tds7_client_version = -1;
282 static int hf_tds7_client_pid = -1;
283 static int hf_tds7_connection_id = -1;
284 static int hf_tds7_option_flags1 = -1;
285 static int hf_tds7_option_flags2 = -1;
286 static int hf_tds7_sql_type_flags = -1;
287 static int hf_tds7_reserved_flags = -1;
288 static int hf_tds7_time_zone = -1;
289 static int hf_tds7_collation = -1;
290 static int hf_tds7_message = -1;
292 /* Initialize the subtree pointers */
293 static gint ett_tds = -1;
294 static gint ett_tds_fragments = -1;
295 static gint ett_tds_fragment = -1;
296 static gint ett_tds_token = -1;
297 static gint ett_tds7_login = -1;
298 static gint ett_tds7_query = 0;
299 static gint ett_tds7_hdr = -1;
301 /* Desegmentation of Netlib buffers crossing TCP segment boundaries. */
302 static gboolean tds_desegment = TRUE;
304 static const fragment_items tds_frag_items = {
309 &hf_tds_fragment_overlap,
310 &hf_tds_fragment_overlap_conflict,
311 &hf_tds_fragment_multiple_tails,
312 &hf_tds_fragment_too_long_fragment,
313 &hf_tds_fragment_error,
314 &hf_tds_reassembled_in,
318 /* Tables for reassembly of fragments. */
319 static GHashTable *tds_fragment_table = NULL;
320 static GHashTable *tds_reassembled_table = NULL;
322 /* defragmentation of multi-buffer TDS PDUs */
323 static gboolean tds_defragment = TRUE;
325 static dissector_handle_t tds_tcp_handle;
326 static dissector_handle_t ntlmssp_handle;
327 static dissector_handle_t data_handle;
329 /* These correspond to the netlib packet type field */
330 static const value_string packet_type_names[] = {
331 {TDS_QUERY_PKT, "Query Packet"},
332 {TDS_LOGIN_PKT, "Login Packet"},
333 {TDS_RPC_PKT, "Remote Procedure Call Packet"},
334 {TDS_RESP_PKT, "Response Packet"},
335 {TDS_CANCEL_PKT, "Cancel Packet"},
336 {TDS_QUERY5_PKT, "TDS5 Query Packet"},
337 {TDS_LOGIN7_PKT, "TDS7/8 Login Packet"},
341 /* The status field */
343 #define is_valid_tds_status(x) ((x) <= STATUS_EVENT_NOTIFICATION)
345 #define STATUS_NOT_LAST_BUFFER 0x00
346 #define STATUS_LAST_BUFFER 0x01
347 #define STATUS_ATTN_REQUEST_ACK 0x02
348 #define STATUS_ATTN_REQUEST 0x03
349 #define STATUS_EVENT_NOTIFICATION 0x04
351 static const value_string status_names[] = {
352 {STATUS_NOT_LAST_BUFFER, "Not last buffer"},
353 {STATUS_LAST_BUFFER, "Last buffer in request or response"},
354 {STATUS_ATTN_REQUEST_ACK, "Acknowledgment of last attention request"},
355 {STATUS_ATTN_REQUEST, "Attention request"},
356 {STATUS_EVENT_NOTIFICATION, "Event notification"},
360 /* The one byte token at the start of each TDS PDU */
361 static const value_string token_names[] = {
362 {TDS5_DYN_TOKEN, "Dynamic SQL"},
363 {TDS5_DYNRES_TOKEN, "Dynamic Results"},
364 {TDS5_DYN3_TOKEN, "Dynamic (Unknown)"},
365 {TDS_LANG_TOKEN, "Language"},
366 {TDS_CLOSE_TOKEN, "Close Connection"},
367 {TDS_RET_STAT_TOKEN, "Return Status"},
368 {TDS_124_TOKEN, "Proc ID"},
369 {TDS7_RESULT_TOKEN, "TDS7+ Results"},
370 {TDS_COL_NAME_TOKEN, "Column Names"},
371 {TDS_COL_INFO_TOKEN, "Column Info"},
372 {TDS_167_TOKEN, "Unknown (167)"},
373 {TDS_168_TOKEN, "Unknown (168)"},
374 {TDS_ORDER_BY_TOKEN, "Order By"},
375 {TDS_ERR_TOKEN, "Error Message"},
376 {TDS_MSG_TOKEN, "Info Message"},
377 {TDS_PARAM_TOKEN, "Paramater"},
378 {TDS_LOGIN_ACK_TOKEN, "Login Acknowledgement"},
379 {TDS_174_TOKEN, "Unknown (174)"},
380 {TDS_ROW_TOKEN, "Row"},
381 {TDS_CMP_ROW_TOKEN, "Compute Row"},
382 {TDS_CAP_TOKEN, "Capabilities"},
383 {TDS_ENV_CHG_TOKEN, "Environment Change"},
384 {TDS_EED_TOKEN, "Extended Error"},
385 {TDS_AUTH_TOKEN, "Authentication"},
386 {TDS_RESULT_TOKEN, "Results"},
387 {TDS_DONE_TOKEN, "Done"},
388 {TDS_DONEPROC_TOKEN, "Done Proc"},
389 {TDS_DONEINPROC_TOKEN, "Done In Proc"},
393 static const value_string env_chg_names[] = {
398 {5, "Unicode Locale ID"},
399 {6, "Unicode Comparison Style"},
400 {7, "Collation Info"},
404 static const value_string login_field_names[] = {
418 #define MAX_COLUMNS 256
421 * This is where we store the column information to be used in decoding the
422 * TDS_ROW_TOKEN tokens.
431 struct _netlib_data {
433 struct _tds_col *columns[MAX_COLUMNS];
436 struct tds7_login_packet_hdr {
437 guint32 total_packet_size;
440 guint32 client_version;
442 guint32 connection_id;
443 guint8 option_flags1;
444 guint8 option_flags2;
445 guint8 sql_type_flags;
446 guint8 reserved_flags;
451 /* all the standard memory management stuff */
452 #define tds_column_length (sizeof(struct _tds_col))
453 #define tds_column_init_count 10
455 static GMemChunk *tds_column = NULL;
457 /* support routines */
459 dissect_tds_ntlmssp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
460 guint offset, guint length)
462 tvbuff_t *ntlmssp_tvb;
464 ntlmssp_tvb = tvb_new_subset(tvb, offset, length, length);
465 call_dissector(ntlmssp_handle, ntlmssp_tvb, pinfo, tree);
469 dissect_tds_query_packet(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
472 gboolean is_unicode = TRUE;
475 proto_item *query_hdr;
476 proto_tree *query_tree;
479 query_hdr = proto_tree_add_text(tree, tvb, offset, -1, "TDS Query Packet");
480 query_tree = proto_item_add_subtree(query_hdr, ett_tds7_query);
481 len = tvb_reported_length_remaining(tvb, offset);
482 if((len < 2) || tvb_get_guint8(tvb, offset+1) !=0)
486 msg = tvb_fake_unicode(tvb, offset, len/2, TRUE);
487 proto_tree_add_text(query_tree, tvb, offset, len, "Query: %s", msg);
494 dissect_tds7_login(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
496 guint offset, i, offset2, len;
497 gboolean is_unicode = TRUE;
500 proto_item *login_hdr;
501 proto_tree *login_tree;
502 proto_item *header_hdr;
503 proto_tree *header_tree;
504 proto_item *length_hdr;
505 proto_tree *length_tree;
507 struct tds7_login_packet_hdr td7hdr;
508 gint length_remaining;
511 /* create display subtree for the protocol */
513 login_hdr = proto_tree_add_text(tree, tvb, offset, -1, "TDS7 Login Packet");
514 login_tree = proto_item_add_subtree(login_hdr, ett_tds7_login);
515 header_hdr = proto_tree_add_text(login_tree, tvb, offset, 36, "Login Packet Header");
516 header_tree = proto_item_add_subtree(header_hdr, ett_tds7_hdr);
518 td7hdr.total_packet_size = tvb_get_letohl(tvb, offset);
519 proto_tree_add_uint(header_tree, hf_tds7_login_total_size, tvb, offset, sizeof(td7hdr.total_packet_size), td7hdr.total_packet_size);
520 offset += sizeof(td7hdr.total_packet_size);
522 td7hdr.tds_version = tvb_get_ntohl(tvb, offset);
523 proto_tree_add_uint(header_tree, hf_tds7_version, tvb, offset, sizeof(td7hdr.tds_version), td7hdr.tds_version);
524 offset += sizeof(td7hdr.tds_version);
526 td7hdr.packet_size = tvb_get_ntohl(tvb, offset);
527 proto_tree_add_uint(header_tree, hf_tds7_packet_size, tvb, offset, sizeof(td7hdr.packet_size), td7hdr.packet_size);
528 offset += sizeof(td7hdr.packet_size);
530 td7hdr.client_version = tvb_get_ntohl(tvb, offset);
531 proto_tree_add_uint(header_tree, hf_tds7_client_version, tvb, offset, sizeof(td7hdr.client_version), td7hdr.client_version);
532 offset += sizeof(td7hdr.client_version);
534 td7hdr.client_pid = tvb_get_letohl(tvb, offset);
535 proto_tree_add_uint(header_tree, hf_tds7_client_pid, tvb, offset, sizeof(td7hdr.client_pid), td7hdr.client_pid);
536 offset += sizeof(td7hdr.client_pid);
538 td7hdr.connection_id= tvb_get_letohl(tvb, offset);
539 proto_tree_add_uint(header_tree, hf_tds7_connection_id, tvb, offset, sizeof(td7hdr.connection_id), td7hdr.connection_id);
540 offset += sizeof(td7hdr.connection_id);
542 td7hdr.option_flags1 = tvb_get_guint8(tvb, offset);
543 proto_tree_add_uint(header_tree, hf_tds7_option_flags1, tvb, offset, sizeof(td7hdr.option_flags1), td7hdr.option_flags1);
544 offset += sizeof(td7hdr.option_flags1);
546 td7hdr.option_flags2 = tvb_get_guint8(tvb, offset);
547 proto_tree_add_uint(header_tree, hf_tds7_option_flags2, tvb, offset, sizeof(td7hdr.option_flags2), td7hdr.option_flags2);
548 offset += sizeof(td7hdr.option_flags2);
550 td7hdr.sql_type_flags = tvb_get_guint8(tvb, offset);
551 proto_tree_add_uint(header_tree, hf_tds7_sql_type_flags, tvb, offset, sizeof(td7hdr.sql_type_flags), td7hdr.sql_type_flags);
552 offset += sizeof(td7hdr.sql_type_flags);
554 td7hdr.reserved_flags = tvb_get_guint8(tvb, offset);
555 proto_tree_add_uint(header_tree, hf_tds7_reserved_flags, tvb, offset, sizeof(td7hdr.reserved_flags), td7hdr.reserved_flags);
556 offset += sizeof(td7hdr.reserved_flags);
558 td7hdr.time_zone = tvb_get_ntohl(tvb, offset);
559 proto_tree_add_uint(header_tree, hf_tds7_time_zone, tvb, offset, sizeof(td7hdr.time_zone), td7hdr.time_zone);
560 offset += sizeof(td7hdr.time_zone);
562 td7hdr.collation = tvb_get_ntohl(tvb, offset);
563 proto_tree_add_uint(header_tree, hf_tds7_collation, tvb, offset, sizeof(td7hdr.collation), td7hdr.collation);
564 offset += sizeof(td7hdr.collation);
566 length_hdr = proto_tree_add_text(login_tree, tvb, offset, 50, "Lengths and offsets");
567 length_tree = proto_item_add_subtree(length_hdr, ett_tds7_hdr);
569 for (i = 0; i < 9; i++) {
570 offset2 = tvb_get_letohs(tvb, offset + i*4);
571 len = tvb_get_letohs(tvb, offset + i*4 + 2);
572 proto_tree_add_text(length_tree, tvb, offset + i*4, 2,
574 val_to_str(i, login_field_names, "Unknown"),
576 proto_tree_add_text(length_tree, tvb, offset + i*4 + 2, 2,
578 val_to_str(i, login_field_names, "Unknown"),
582 if (is_unicode == TRUE) {
583 val = tvb_fake_unicode(tvb, offset2, len, TRUE);
586 val = tvb_get_string(tvb, offset2, len);
587 proto_tree_add_text(login_tree, tvb, offset2, len, "%s: %s", val_to_str(i, login_field_names, "Unknown"), val);
593 proto_tree_add_text(login_tree, tvb, offset2, len, "%s", val_to_str(i, login_field_names, "Unknown"));
598 length_remaining = tvb_reported_length_remaining(tvb, offset2 + len);
599 if (length_remaining > 0) {
600 dissect_tds_ntlmssp(tvb, pinfo, login_tree, offset2 + len,
605 static int get_size_by_coltype(int servertype)
609 case SYBINT1: return 1; break;
610 case SYBINT2: return 2; break;
611 case SYBINT4: return 4; break;
612 case SYBINT8: return 8; break;
613 case SYBREAL: return 4; break;
614 case SYBFLT8: return 8; break;
615 case SYBDATETIME: return 8; break;
616 case SYBDATETIME4: return 4; break;
617 case SYBBIT: return 1; break;
618 case SYBBITN: return 1; break;
619 case SYBMONEY: return 8; break;
620 case SYBMONEY4: return 4; break;
621 case SYBUNIQUE: return 16; break;
622 default: return -1; break;
625 static int tds_is_fixed_token(int token)
629 case TDS_DONEPROC_TOKEN:
630 case TDS_DONEINPROC_TOKEN:
631 case TDS_RET_STAT_TOKEN:
632 case TDS7_RESULT_TOKEN:
638 static int tds_get_token_size(int token)
642 case TDS_DONEPROC_TOKEN:
643 case TDS_DONEINPROC_TOKEN:
645 case TDS_RET_STAT_TOKEN:
656 * data_to_string should take column data and turn it into something we can
657 * display on the tree.
659 static char *data_to_string(void *data, guint col_type, guint col_size)
661 static char result[256];
666 /* strncpy(result, (char *)data, col_size); */
667 for (i=0;i<col_size && i<(256-1);i++)
668 if (!isprint(((char *)data)[i])) result[i]='.';
669 else result[i]=((char *)data)[i];
673 sprintf(result, "%d", *(short *)data);
676 sprintf(result, "%d", *(int *)data);
679 sprintf(result, "Unexpected column_type %d", col_type);
687 * Since rows are special PDUs in that they are not fixed and lack a size field,
688 * the length must be computed using the column information seen in the result
689 * PDU. This function does just that.
692 tds_get_row_size(tvbuff_t *tvb, struct _netlib_data *nl_data, guint offset)
697 for (i = 0; i < nl_data->num_cols; i++) {
698 if (!is_fixed_coltype(nl_data->columns[i]->ctype)) {
699 csize = tvb_get_guint8(tvb, cur);
702 csize = get_size_by_coltype(nl_data->columns[i]->ctype);
706 return (cur - offset + 1);
710 * Read the results token and store the relevant information in the
711 * _netlib_data structure for later use (see tds_get_row_size).
714 read_results_tds5(tvbuff_t *tvb, struct _netlib_data *nl_data, guint offset)
720 len = tvb_get_letohs(tvb, offset+1);
724 * This would be the logical place to check for little/big endianess
725 * if we didn't see the login packet.
727 nl_data->num_cols = tvb_get_letohs(tvb, cur);
728 if (nl_data->num_cols > MAX_COLUMNS) {
729 nl_data->num_cols = 0;
735 for (i = 0; i < nl_data->num_cols; i++) {
736 nl_data->columns[i] = g_mem_chunk_alloc(tds_column);
737 name_len = tvb_get_guint8(tvb,cur);
743 nl_data->columns[i]->utype = tvb_get_letohs(tvb, cur);
746 cur += 2; /* unknown */
748 nl_data->columns[i]->ctype = tvb_get_guint8(tvb,cur);
751 if (!is_fixed_coltype(nl_data->columns[i]->ctype)) {
752 nl_data->columns[i]->csize = tvb_get_guint8(tvb,cur);
755 nl_data->columns[i]->csize =
756 get_size_by_coltype(nl_data->columns[i]->ctype);
764 * If the packet type from the netlib header is a login packet, then dig into
765 * the packet to see if this is a supported TDS version and verify the otherwise
766 * weak heuristics of the netlib check.
769 netlib_check_login_pkt(tvbuff_t *tvb, guint offset, packet_info *pinfo, guint8 type)
771 guint tds_major, bytes_avail;
773 bytes_avail = tvb_length(tvb) - offset;
775 * we have two login packet styles, one for TDS 4.2 and 5.0
777 if (type==TDS_LOGIN_PKT) {
778 /* Use major version number to validate TDS 4/5 login
781 /* Login packet is first in stream and should not be fragmented...
782 * if it is we are screwed */
783 if (bytes_avail < 467) return FALSE;
784 tds_major = tvb_get_guint8(tvb, 466);
785 if (tds_major != 4 && tds_major != 5) {
789 * and one added by Microsoft in SQL Server 7
791 } else if (type==TDS_LOGIN7_PKT) {
792 if (bytes_avail < 16) return FALSE;
793 tds_major = tvb_get_guint8(tvb, 15);
794 if (tds_major != 0x70 && tds_major != 0x80) {
797 } else if (type==TDS_QUERY5_PKT) {
798 if (bytes_avail < 9) return FALSE;
799 /* if this is a TDS 5.0 query check the token */
800 if (tvb_get_guint8(tvb, 8) != TDS_LANG_TOKEN) {
803 /* check if it is MS SQL default port */
804 } else if (pinfo->srcport != 1433 &&
805 pinfo->destport != 1433) {
806 /* otherwise, we can not ensure this is netlib */
807 /* beyond a reasonable doubt. */
814 dissect_tds_env_chg(tvbuff_t *tvb, guint offset, guint token_sz,
818 guint old_len, new_len, old_len_offset;
819 char *new_val = NULL, *old_val = NULL;
820 guint32 string_offset;
821 gboolean is_unicode = FALSE;
822 guint16 collate_codepage, collate_flags;
823 guint8 collate_charset_id;
825 env_type = tvb_get_guint8(tvb, offset);
826 proto_tree_add_text(tree, tvb, offset, 1, "Type: %u (%s)", env_type,
827 val_to_str(env_type, env_chg_names, "Unknown"));
829 new_len = tvb_get_guint8(tvb, offset+1);
830 old_len_offset = offset + new_len + 2;
831 old_len = tvb_get_guint8(tvb, old_len_offset);
834 * If our lengths plus the lengths of the type and the lengths
835 * don't add up to the token size, it must be UCS2.
837 if (old_len + new_len + 3 != token_sz) {
839 old_len_offset = offset + (new_len * 2) + 2;
840 old_len = tvb_get_guint8(tvb, old_len_offset);
843 proto_tree_add_text(tree, tvb, offset + 1, 1, "New Value Length: %u",
846 if (env_type != 7) { /* if it's not 'Collation Info - which is not textual! */
847 string_offset = offset + 2;
848 if (is_unicode == TRUE) {
849 new_val = tvb_fake_unicode(tvb, string_offset,
853 new_val = tvb_get_string(tvb, string_offset, new_len);
854 proto_tree_add_text(tree, tvb, string_offset, new_len,
855 "New Value: %s", new_val);
858 else { /* parse collation info structure. From http://www.freetds.org/tds.html#collate */
860 collate_codepage = tvb_get_letohs(tvb, offset);
861 proto_tree_add_text(tree, tvb, offset, 2, "Codepage: %u" , collate_codepage);
863 collate_flags = tvb_get_letohs(tvb, offset);
864 proto_tree_add_text(tree, tvb, offset, 2, "Flags: 0x%x", collate_flags);
866 collate_charset_id = tvb_get_guint8(tvb, offset);
867 proto_tree_add_text(tree, tvb, offset, 1, "Charset ID: %u", collate_charset_id);
872 proto_tree_add_text(tree, tvb, old_len_offset, 1, "Old Value Length: %u",
875 string_offset = old_len_offset + 1;
876 if (is_unicode == TRUE) {
877 old_val = tvb_fake_unicode(tvb, string_offset,
881 old_val = tvb_get_string(tvb, string_offset, old_len);
882 proto_tree_add_text(tree, tvb, string_offset, old_len,
883 "Old Value: %s", old_val);
889 dissect_tds_msg_token(tvbuff_t *tvb, guint offset, guint token_sz, proto_tree *tree)
894 gboolean is_unicode = FALSE;
896 proto_tree_add_text(tree, tvb, offset, 4, "SQL Message Number: %d", tvb_get_letohl(tvb, offset));
898 proto_tree_add_text(tree, tvb, offset, 1, "State: %u", tvb_get_guint8(tvb, offset));
900 proto_tree_add_text(tree, tvb, offset, 1, "Level: %u", tvb_get_guint8(tvb, offset));
903 msg_len = tvb_get_letohs(tvb, offset);
904 proto_tree_add_text(tree, tvb, offset, 2, "Message length: %u characters", msg_len);
907 srvr_len = tvb_get_guint8(tvb, offset + msg_len);
909 if(msg_len + srvr_len + 9U + 3U != token_sz) /* 9 is the length of message number (4), state (1), level (1), msg_len (2), srvr_len (1) fields */
913 msg = tvb_fake_unicode(tvb, offset, msg_len, TRUE);
916 msg = tvb_get_string(tvb, offset, msg_len);
918 proto_tree_add_string(tree, hf_tds7_message, tvb, offset, msg_len, msg);
922 proto_tree_add_text(tree, tvb, offset, 1, "Server name length: %u characters", srvr_len);
926 msg = tvb_fake_unicode(tvb, offset, srvr_len, TRUE);
929 msg = tvb_get_string(tvb, offset, srvr_len);
931 proto_tree_add_text(tree, tvb, offset, srvr_len, "Server name: %s", msg);
936 dissect_tds_err_token(tvbuff_t *tvb, guint offset, guint token_sz, proto_tree *tree)
941 gboolean is_unicode = FALSE;
943 proto_tree_add_text(tree, tvb, offset, 4, "SQL Error Number: %d", tvb_get_letohl(tvb, offset));
945 proto_tree_add_text(tree, tvb, offset, 1, "State: %u", tvb_get_guint8(tvb, offset));
947 proto_tree_add_text(tree, tvb, offset, 1, "Level: %u", tvb_get_guint8(tvb, offset));
950 msg_len = tvb_get_letohs(tvb, offset);
951 proto_tree_add_text(tree, tvb, offset, 1, "Error length: %u characters", msg_len);
954 srvr_len = tvb_get_guint8(tvb, offset + msg_len);
956 if(msg_len + srvr_len + 9U + 3U != token_sz) /* 9 is the length of message number (4), state (1), level (1), msg_len (2), srvr_len (1) fields */
960 msg = tvb_fake_unicode(tvb, offset, msg_len, TRUE);
963 msg = tvb_get_string(tvb, offset, msg_len);
965 proto_tree_add_text(tree, tvb, offset, msg_len, "Error: %s", format_text(msg, strlen(msg)));
969 proto_tree_add_text(tree, tvb, offset, 1, "Server name length: %u characters", srvr_len);
973 msg = tvb_fake_unicode(tvb, offset, srvr_len, TRUE);
976 msg = tvb_get_string(tvb, offset, srvr_len);
978 proto_tree_add_text(tree, tvb, offset, srvr_len, "Server name: %s", msg);
983 dissect_tds_login_ack_token(tvbuff_t *tvb, guint offset, guint token_sz, proto_tree *tree)
987 gboolean is_unicode = FALSE;
989 proto_tree_add_text(tree, tvb, offset, 1, "Ack: %u", tvb_get_guint8(tvb, offset));
991 proto_tree_add_text(tree, tvb, offset, 1, "Major version (may be incorrect): %d", tvb_get_guint8(tvb, offset));
993 proto_tree_add_text(tree, tvb, offset, 1, "Minor version (may be incorrect): %d", tvb_get_guint8(tvb, offset));
995 proto_tree_add_text(tree, tvb, offset, 2, "zero usually");
998 msg_len = tvb_get_guint8(tvb, offset);
999 proto_tree_add_text(tree, tvb, offset, 1, "Text length: %u characters", msg_len);
1002 if(msg_len + 6U + 3U != token_sz - 1) /* 6 is the length of ack(1), version (4), text length (1) fields */
1004 proto_tree_add_text(tree, tvb, offset, 0, "msg_len: %d, token_sz: %d, total: %d",msg_len, token_sz, msg_len + 6U + 3U);
1006 msg = tvb_fake_unicode(tvb, offset, msg_len, TRUE);
1009 msg = tvb_get_string(tvb, offset, msg_len);
1011 proto_tree_add_text(tree, tvb, offset, msg_len, "Text: %s", format_text(msg, strlen(msg)));
1015 proto_tree_add_text(tree, tvb, offset, 4, "Server Version");
1020 dissect_tds7_results_token(tvbuff_t *tvb, guint offset, proto_tree *tree)
1022 guint16 num_columns, table_len;
1023 guint8 type, msg_len;
1026 guint16 collate_codepage, collate_flags;
1027 guint8 collate_charset_id;
1029 num_columns = tvb_get_letohs(tvb, offset);
1030 proto_tree_add_text(tree, tvb, offset, 2, "Columns: %u", tvb_get_letohs(tvb, offset));
1032 for(i=0; i != num_columns; i++) {
1033 proto_tree_add_text(tree, tvb, offset, 0, "Column %d", i + 1);
1034 proto_tree_add_text(tree, tvb, offset, 2, "usertype: %d", tvb_get_letohs(tvb, offset));
1036 proto_tree_add_text(tree, tvb, offset, 2, "flags: %d", tvb_get_letohs(tvb, offset));
1038 type = tvb_get_guint8(tvb, offset);
1039 proto_tree_add_text(tree, tvb, offset, 1, "Type: %d", type);
1041 if(type == 38 || type == 104) { /* ugly, ugly hack. Wish I knew what it really means!*/
1042 proto_tree_add_text(tree, tvb, offset, 1, "unknown 1 byte (%x)", tvb_get_guint8(tvb, offset));
1045 else if (type == 35) {
1046 proto_tree_add_text(tree, tvb, offset, 4, "unknown 4 bytes (%x)", tvb_get_letohl(tvb, offset));
1048 collate_codepage = tvb_get_letohs(tvb, offset);
1049 proto_tree_add_text(tree, tvb, offset, 2, "Codepage: %u" , collate_codepage);
1051 collate_flags = tvb_get_letohs(tvb, offset);
1052 proto_tree_add_text(tree, tvb, offset, 2, "Flags: 0x%x", collate_flags);
1054 collate_charset_id = tvb_get_guint8(tvb, offset);
1055 proto_tree_add_text(tree, tvb, offset, 1, "Charset ID: %u", collate_charset_id);
1057 table_len = tvb_get_letohs(tvb, offset);
1059 if(table_len != 0) {
1060 msg = tvb_fake_unicode(tvb, offset, table_len, TRUE);
1061 proto_tree_add_text(tree, tvb, offset, table_len*2, "Table name: %s", msg);
1063 offset += table_len*2;
1066 else if (type == 106) {
1067 proto_tree_add_text(tree, tvb, offset, 3, "unknown 3 bytes");
1071 proto_tree_add_text(tree, tvb, offset, 2, "Large type size: 0x%x", tvb_get_letohs(tvb, offset));
1073 collate_codepage = tvb_get_letohs(tvb, offset);
1074 proto_tree_add_text(tree, tvb, offset, 2, "Codepage: %u" , collate_codepage);
1076 collate_flags = tvb_get_letohs(tvb, offset);
1077 proto_tree_add_text(tree, tvb, offset, 2, "Flags: 0x%x", collate_flags);
1079 collate_charset_id = tvb_get_guint8(tvb, offset);
1080 proto_tree_add_text(tree, tvb, offset, 1, "Charset ID: %u", collate_charset_id);
1083 msg_len = tvb_get_guint8(tvb, offset);
1084 proto_tree_add_text(tree, tvb, offset, 1, "message length: %d",msg_len);
1087 msg = tvb_fake_unicode(tvb, offset, msg_len, TRUE);
1088 proto_tree_add_text(tree, tvb, offset, msg_len*2, "Text: %s", msg);
1090 offset += msg_len*2;
1097 dissect_tds_done_token(tvbuff_t *tvb, guint offset, proto_tree *tree)
1099 proto_tree_add_text(tree, tvb, offset, 2, "bit flag");
1101 proto_tree_add_text(tree, tvb, offset, 2, "unknown");
1103 proto_tree_add_text(tree, tvb, offset, 4, "row count: %u", tvb_get_letohl(tvb, offset));
1108 dissect_tds_rpc(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
1116 * XXX - how can we determine whether this is ASCII or Unicode?
1118 len = tvb_get_letohs(tvb, offset);
1119 proto_tree_add_text(tree, tvb, offset, 2, "RPC Name Length: %u", len);
1122 val = tvb_fake_unicode(tvb, offset, len, TRUE);
1124 proto_tree_add_text(tree, tvb, offset, len, "RPC Name: %s",
1129 proto_tree_add_text(tree, tvb, offset, -1, "Unknown data");
1133 dissect_tds_resp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1136 proto_item *token_item;
1137 proto_tree *token_tree;
1138 guint pos, token_sz = 0;
1140 struct _netlib_data nl_data;
1141 gint length_remaining;
1143 memset(&nl_data, '\0', sizeof nl_data);
1146 * Until we reach the end of the packet, read tokens.
1149 while (tvb_reported_length_remaining(tvb, pos) > 0) {
1151 token = tvb_get_guint8(tvb, pos);
1153 if (tds_is_fixed_token(token)) {
1154 token_sz = tds_get_token_size(token) + 1;
1155 } else if (token == TDS_ROW_TOKEN) {
1157 * Rows are special; they have no size field and
1158 * aren't fixed length.
1160 token_sz = tds_get_row_size(tvb, &nl_data, pos + 1);
1162 token_sz = tvb_get_letohs(tvb, pos + 1) + 3;
1164 length_remaining = tvb_ensure_length_remaining(tvb, pos);
1165 if (token_sz > (guint)length_remaining)
1166 token_sz = (guint)length_remaining;
1168 token_item = proto_tree_add_text(tree, tvb, pos, token_sz,
1169 "Token 0x%02x %s", token,
1170 val_to_str(token, token_names, "Unknown Token Type"));
1171 token_tree = proto_item_add_subtree(token_item, ett_tds_token);
1174 * If it's a variable token, put the length field in here
1175 * instead of replicating this for each token subdissector.
1177 if (!tds_is_fixed_token(token) && token != TDS_ROW_TOKEN) {
1178 proto_tree_add_text(token_tree, tvb, pos+1, 2,
1179 "Length: %u", tvb_get_letohs(tvb, pos+1));
1184 case TDS_RESULT_TOKEN:
1186 * If it's a result token, we need to stash the
1189 read_results_tds5(tvb, &nl_data, pos);
1192 case TDS_ENV_CHG_TOKEN:
1193 dissect_tds_env_chg(tvb, pos + 3, token_sz - 3,
1197 case TDS_AUTH_TOKEN:
1198 dissect_tds_ntlmssp(tvb, pinfo, token_tree, pos + 3,
1202 dissect_tds_msg_token(tvb, pos + 3, token_sz - 3, token_tree);
1205 dissect_tds_err_token(tvb, pos + 3, token_sz - 3, token_tree);
1207 case TDS_DONE_TOKEN:
1208 dissect_tds_done_token(tvb, pos + 1, token_tree);
1210 case TDS_LOGIN_ACK_TOKEN:
1211 dissect_tds_login_ack_token(tvb, pos + 3, token_sz - 3, token_tree);
1213 case TDS7_RESULT_TOKEN:
1214 pos = (dissect_tds7_results_token(tvb, pos + 1, token_tree)-1);
1218 /* and step to the end of the token, rinse, lather, repeat */
1224 dissect_netlib_buffer(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1227 proto_item *tds_item = NULL;
1228 proto_tree *tds_tree = NULL;
1233 guint8 packet_number;
1234 gboolean save_fragmented;
1236 fragment_data *fd_head;
1240 /* create display subtree for the protocol */
1241 tds_item = proto_tree_add_item(tree, proto_tds, tvb, offset, -1,
1244 tds_tree = proto_item_add_subtree(tds_item, ett_tds);
1246 type = tvb_get_guint8(tvb, offset);
1248 proto_tree_add_uint(tds_tree, hf_tds_type, tvb, offset, 1,
1251 status = tvb_get_guint8(tvb, offset + 1);
1253 proto_tree_add_uint(tds_tree, hf_tds_status, tvb, offset + 1, 1,
1256 size = tvb_get_ntohs(tvb, offset + 2);
1258 proto_tree_add_uint(tds_tree, hf_tds_size, tvb, offset + 2, 2,
1261 channel = tvb_get_ntohs(tvb, offset + 4);
1263 proto_tree_add_uint(tds_tree, hf_tds_channel, tvb, offset + 4, 2,
1266 packet_number = tvb_get_guint8(tvb, offset + 6);
1268 proto_tree_add_uint(tds_tree, hf_tds_packet_number, tvb, offset + 6, 1,
1270 proto_tree_add_item(tds_tree, hf_tds_window, tvb, offset + 7, 1,
1273 offset += 8; /* skip Netlib header */
1276 * Deal with fragmentation.
1278 save_fragmented = pinfo->fragmented;
1279 if (tds_defragment &&
1280 (packet_number > 1 || status == STATUS_NOT_LAST_BUFFER)) {
1281 if (status == STATUS_NOT_LAST_BUFFER) {
1282 if (check_col(pinfo->cinfo, COL_INFO))
1283 col_append_str(pinfo->cinfo, COL_INFO,
1284 " (Not last buffer)");
1286 len = tvb_reported_length_remaining(tvb, offset);
1288 * XXX - I've seen captures that start with a login
1289 * packet with a sequence number of 2.
1291 fd_head = fragment_add_seq_check(tvb, offset, pinfo, channel,
1292 tds_fragment_table, tds_reassembled_table,
1293 packet_number - 1, len, status == STATUS_NOT_LAST_BUFFER);
1294 next_tvb = process_reassembled_data(tvb, offset, pinfo,
1295 "Reassembled TDS", fd_head, &tds_frag_items, NULL,
1299 * If this isn't the last buffer, just show it as a fragment.
1300 * (XXX - it'd be nice to dissect it if it's the first
1301 * buffer, but we'd need to do reassembly in order to
1304 * If this is the last buffer, dissect it.
1305 * (XXX - it'd be nice to show it as a fragment if it's part
1306 * of a fragmented message, but we'd need to do reassembly
1307 * in order to discover that.)
1309 if (status == STATUS_NOT_LAST_BUFFER)
1312 next_tvb = tvb_new_subset(tvb, offset, -1, -1);
1315 if (next_tvb != NULL) {
1319 dissect_tds_rpc(next_tvb, pinfo, tds_tree);
1323 dissect_tds_resp(next_tvb, pinfo, tds_tree);
1326 case TDS_LOGIN7_PKT:
1327 dissect_tds7_login(next_tvb, pinfo, tds_tree);
1330 dissect_tds_query_packet(next_tvb, pinfo, tds_tree);
1333 proto_tree_add_text(tds_tree, next_tvb, 0, -1,
1338 next_tvb = tvb_new_subset (tvb, offset, -1, -1);
1339 call_dissector(data_handle, next_tvb, pinfo, tds_tree);
1344 dissect_tds_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1346 volatile gboolean first_time = TRUE;
1347 volatile int offset = 0;
1348 guint length_remaining;
1353 proto_item *tds_item = NULL;
1354 proto_tree *tds_tree = NULL;
1356 while (tvb_reported_length_remaining(tvb, offset) != 0) {
1357 length_remaining = tvb_ensure_length_remaining(tvb, offset);
1360 * Can we do reassembly?
1362 if (tds_desegment && pinfo->can_desegment) {
1364 * Yes - is the fixed-length part of the PDU
1365 * split across segment boundaries?
1367 if (length_remaining < 8) {
1369 * Yes. Tell the TCP dissector where the
1370 * data for this message starts in the data
1371 * it handed us, and how many more bytes we
1374 pinfo->desegment_offset = offset;
1375 pinfo->desegment_len = 8 - length_remaining;
1380 type = tvb_get_guint8(tvb, offset);
1383 * Get the length of the PDU.
1385 plen = tvb_get_ntohs(tvb, offset + 2);
1388 * The length is less than the header length.
1389 * Put in the type, status, and length, and
1390 * report the length as bogus.
1393 /* create display subtree for the protocol */
1394 tds_item = proto_tree_add_item(tree, proto_tds,
1395 tvb, offset, -1, FALSE);
1397 tds_tree = proto_item_add_subtree(tds_item,
1399 proto_tree_add_uint(tds_tree, hf_tds_type, tvb,
1401 proto_tree_add_item(tds_tree, hf_tds_status,
1402 tvb, offset + 1, 1, FALSE);
1403 proto_tree_add_uint_format(tds_tree,
1404 hf_tds_size, tvb, offset + 2, 2, plen,
1405 "Size: %u (bogus, should be >= 8)", plen);
1409 * Give up - we can't dissect any more of this
1416 * Can we do reassembly?
1418 if (tds_desegment && pinfo->can_desegment) {
1420 * Yes - is the PDU split across segment boundaries?
1422 if (length_remaining < plen) {
1424 * Yes. Tell the TCP dissector where the
1425 * data for this message starts in the data
1426 * it handed us, and how many more bytes we
1429 pinfo->desegment_offset = offset;
1430 pinfo->desegment_len = plen - length_remaining;
1436 if (check_col(pinfo->cinfo, COL_PROTOCOL))
1437 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TDS");
1440 * Set the packet description based on its TDS packet
1443 if (check_col(pinfo->cinfo, COL_INFO)) {
1444 col_add_str(pinfo->cinfo, COL_INFO,
1445 val_to_str(type, packet_type_names,
1446 "Unknown Packet Type: %u"));
1452 * Construct a tvbuff containing the amount of the payload
1453 * we have available. Make its reported length the amount
1454 * of data in the PDU.
1456 * XXX - if reassembly isn't enabled. the subdissector will
1457 * throw a BoundsError exception, rather than a
1458 * ReportedBoundsError exception. We really want a tvbuff
1459 * where the length is "length", the reported length is
1460 * "plen", and the "if the snapshot length were infinite"
1461 * length is the minimum of the reported length of the tvbuff
1462 * handed to us and "plen", with a new type of exception
1463 * thrown if the offset is within the reported length but
1464 * beyond that third length, with that exception getting the
1465 * "Unreassembled Packet" error.
1467 length = length_remaining;
1470 next_tvb = tvb_new_subset(tvb, offset, length, plen);
1473 * Dissect the Netlib buffer.
1475 * Catch the ReportedBoundsError exception; if this
1476 * particular Netlib buffer happens to get a
1477 * ReportedBoundsError exception, that doesn't mean
1478 * that we should stop dissecting PDUs within this frame
1479 * or chunk of reassembled data.
1481 * If it gets a BoundsError, we can stop, as there's nothing
1482 * more to see, so we just re-throw it.
1485 dissect_netlib_buffer(next_tvb, pinfo, tree);
1487 CATCH(BoundsError) {
1490 CATCH(ReportedBoundsError) {
1491 show_reported_bounds_error(tvb, pinfo, tree);
1496 * Step to the next Netlib buffer.
1503 dissect_tds_tcp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1509 conversation_t *conv;
1512 * If we don't have even enough data for a Netlib header,
1513 * just say it's not TDS.
1515 if (!tvb_bytes_exist(tvb, offset, 8))
1519 * Quickly scan all the data we have in order to see if
1520 * everything in it looks like Netlib traffic.
1522 while (tvb_bytes_exist(tvb, offset, 1)) {
1524 * Check the type field.
1526 type = tvb_get_guint8(tvb, offset);
1527 if (!is_valid_tds_type(type))
1531 * Check the status field, if it's present.
1533 if (!tvb_bytes_exist(tvb, offset + 1, 1))
1535 status = tvb_get_guint8(tvb, offset + 1);
1536 if (!is_valid_tds_status(status))
1540 * Get the length of the PDU.
1542 if (!tvb_bytes_exist(tvb, offset + 2, 2))
1544 plen = tvb_get_ntohs(tvb, offset + 2);
1547 * The length is less than the header length.
1554 * If we're at the beginning of the segment, check the
1555 * payload if it's a login packet.
1558 if (!netlib_check_login_pkt(tvb, offset, pinfo, type))
1563 * Step to the next Netlib buffer.
1569 * OK, it passes the test; assume the rest of this conversation
1572 conv = find_conversation(&pinfo->src, &pinfo->dst, pinfo->ptype,
1573 pinfo->srcport, pinfo->destport, 0);
1576 * No conversation exists yet - create one.
1578 conv = conversation_new(&pinfo->src, &pinfo->dst,
1579 pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
1581 conversation_set_dissector(conv, tds_tcp_handle);
1584 * Now dissect it as TDS.
1586 dissect_tds_tcp(tvb, pinfo, tree);
1594 * Initialize the fragment and reassembly tables.
1596 fragment_table_init(&tds_fragment_table);
1597 reassembled_table_init(&tds_reassembled_table);
1600 * Reinitialize the chunks of data for remembering row
1604 g_mem_chunk_destroy(tds_column);
1606 tds_column = g_mem_chunk_new("tds_column", tds_column_length,
1607 tds_column_init_count * tds_column_length,
1611 /* Register the protocol with Ethereal */
1613 /* this format is required because a script is used to build the C function
1614 that calls all the protocol registration.
1618 proto_register_netlib(void)
1620 static hf_register_info hf[] = {
1622 { "Type", "tds.type",
1623 FT_UINT8, BASE_HEX, VALS(packet_type_names), 0x0,
1624 "Packet Type", HFILL }
1627 { "Status", "tds.status",
1628 FT_UINT8, BASE_DEC, VALS(status_names), 0x0,
1629 "Frame status", HFILL }
1632 { "Size", "tds.size",
1633 FT_UINT16, BASE_DEC, NULL, 0x0,
1634 "Packet Size", HFILL }
1637 { "Channel", "tds.channel",
1638 FT_UINT16, BASE_DEC, NULL, 0x0,
1639 "Channel Number", HFILL }
1641 { &hf_tds_packet_number,
1642 { "Packet Number", "tds.packet_number",
1643 FT_UINT8, BASE_DEC, NULL, 0x0,
1644 "Packet Number", HFILL }
1647 { "Window", "tds.window",
1648 FT_UINT8, BASE_DEC, NULL, 0x0,
1651 { &hf_tds_fragment_overlap,
1652 { "Segment overlap", "tds.fragment.overlap",
1653 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1654 "Fragment overlaps with other fragments", HFILL }
1656 { &hf_tds_fragment_overlap_conflict,
1657 { "Conflicting data in fragment overlap", "tds.fragment.overlap.conflict",
1658 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1659 "Overlapping fragments contained conflicting data", HFILL }
1661 { &hf_tds_fragment_multiple_tails,
1662 { "Multiple tail fragments found", "tds.fragment.multipletails",
1663 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1664 "Several tails were found when defragmenting the packet", HFILL }
1666 { &hf_tds_fragment_too_long_fragment,
1667 { "Segment too long", "tds.fragment.toolongfragment",
1668 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1669 "Segment contained data past end of packet", HFILL }
1671 { &hf_tds_fragment_error,
1672 { "Defragmentation error", "tds.fragment.error",
1673 FT_FRAMENUM, BASE_NONE, NULL, 0x0,
1674 "Defragmentation error due to illegal fragments", HFILL }
1677 { "TDS Fragment", "tds.fragment",
1678 FT_FRAMENUM, BASE_NONE, NULL, 0x0,
1679 "TDS Fragment", HFILL }
1681 { &hf_tds_fragments,
1682 { "TDS Fragments", "tds.fragments",
1683 FT_NONE, BASE_NONE, NULL, 0x0,
1684 "TDS Fragments", HFILL }
1686 { &hf_tds_reassembled_in,
1687 { "Reassembled TDS in frame", "tds.reassembled_in",
1688 FT_FRAMENUM, BASE_NONE, NULL, 0x0,
1689 "This TDS packet is reassembled in this frame", HFILL }
1691 { &hf_tds7_login_total_size,
1692 { "Total Packet Length", "tds7login.total_len",
1693 FT_UINT32, BASE_DEC, NULL, 0x0,
1694 "TDS7 Login Packet total packet length", HFILL }
1697 { "TDS version", "tds7login.version",
1698 FT_UINT32, BASE_HEX, NULL, 0x0,
1699 "TDS version", HFILL }
1701 { &hf_tds7_packet_size,
1702 { "Packet Size", "tds7login.packet_size",
1703 FT_UINT32, BASE_DEC, NULL, 0x0,
1704 "Packet size", HFILL }
1706 { &hf_tds7_client_version,
1707 { "Client version", "tds7login.client_version",
1708 FT_UINT32, BASE_DEC, NULL, 0x0,
1709 "Client version", HFILL }
1711 { &hf_tds7_client_pid,
1712 { "Client PID", "tds7login.client_pid",
1713 FT_UINT32, BASE_DEC, NULL, 0x0,
1714 "Client PID", HFILL }
1716 { &hf_tds7_connection_id,
1717 { "Connection ID", "tds7login.connection_id",
1718 FT_UINT32, BASE_DEC, NULL, 0x0,
1719 "Connection ID", HFILL }
1721 { &hf_tds7_option_flags1,
1722 { "Option Flags 1", "tds7login.option_flags1",
1723 FT_UINT8, BASE_HEX, NULL, 0x0,
1724 "Option Flags 1", HFILL }
1726 { &hf_tds7_option_flags2,
1727 { "Option Flags 2", "tds7login.option_flags2",
1728 FT_UINT8, BASE_HEX, NULL, 0x0,
1729 "Option Flags 2", HFILL }
1731 { &hf_tds7_sql_type_flags,
1732 { "SQL Type Flags", "tds7login.sql_type_flags",
1733 FT_UINT8, BASE_HEX, NULL, 0x0,
1734 "SQL Type Flags", HFILL }
1736 { &hf_tds7_reserved_flags,
1737 { "Reserved Flags", "tds7login.reserved_flags",
1738 FT_UINT8, BASE_HEX, NULL, 0x0,
1739 "reserved flags", HFILL }
1741 { &hf_tds7_time_zone,
1742 { "Time Zone", "tds7login.time_zone",
1743 FT_UINT32, BASE_HEX, NULL, 0x0,
1744 "Time Zone", HFILL }
1746 { &hf_tds7_collation,
1747 { "Collation", "tds7login.collation",
1748 FT_UINT32, BASE_HEX, NULL, 0x0,
1749 "Collation", HFILL }
1752 { "Message", "tds7.message",
1753 FT_STRING, BASE_NONE, NULL, 0x0,
1757 static gint *ett[] = {
1765 module_t *tds_module;
1767 /* Register the protocol name and description */
1768 proto_tds = proto_register_protocol("Tabular Data Stream",
1771 /* Required function calls to register the header fields and subtrees used */
1772 proto_register_field_array(proto_tds, hf, array_length(hf));
1773 proto_register_subtree_array(ett, array_length(ett));
1775 tds_tcp_handle = create_dissector_handle(dissect_tds_tcp, proto_tds);
1777 tds_module = prefs_register_protocol(proto_tds, NULL);
1778 prefs_register_bool_preference(tds_module, "desegment_buffers",
1779 "Desegment all TDS buffers spanning multiple TCP segments",
1780 "Whether the TDS dissector should desegment all TDS buffers spanning multiple TCP segments",
1782 prefs_register_bool_preference(tds_module, "defragment",
1783 "Defragment all TDS messages with multiple buffers",
1784 "Whether the TDS dissector should defragment all messages spanning multiple Netlib buffers",
1787 register_init_routine(tds_init);
1790 /* If this dissector uses sub-dissector registration add a registration routine.
1791 This format is required because a script is used to find these routines and
1792 create the code that calls these routines.
1795 proto_reg_handoff_tds(void)
1797 /* dissector_add("tcp.port", 1433, dissect_tds,
1799 heur_dissector_add("tcp", dissect_tds_tcp_heur, proto_tds);
1801 ntlmssp_handle = find_dissector("ntlmssp");
1802 data_handle = find_dissector("data");