2 * Routines for TDS NetLib dissection
3 * Copyright 2000-2002, Brian Bruns <camber@ais.org>
4 * Copyright 2002, Steve Langasek <vorlon@netexpress.net>
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
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://www.sybase.com/content/1013412/tds34.pdf
76 * http://www.sybase.com/content/1040983/Sybase-tds38-102306.pdf
77 * Microsoft's [MS-TDS] protocol specification
79 * This document is no longer available here:
80 * http://download.nai.com/products/media/sniffer/support/sdos/sybase.pdf
82 * Much of this code was originally developed for the FreeTDS project.
83 * http://www.freetds.org
87 * Excerpts from Brian's posting to wireshark-dev:
89 * The TDS Protocol is actually a protocol within a protocol. On the outside
90 * there is netlib which is not so much a encapsulation as a blocking of the
91 * data, typically to 512 or 4096 bytes. Between this are the protocol data
92 * units for TDS. Netlib packets may be split over real packets, multiple
93 * netlib packets may appear in single real packets. TDS PDUs may be split
94 * over netlib packets (and real packets) and most certainly can appear
95 * multiple times within a netlib packet.
97 * Because of this, I abandoned my earlier attempt at making two dissectors,
98 * one for netlib and one for TDS. Counterintuitively, a single dissector
99 * turned out to be simpler than splitting it up.
101 * Here are some of the (hefty) limitations of the current code
103 * . We currently do not handle netlib headers that cross packet boundaries.
104 * This should be an easy fix.
105 * . I probably could have used the packet reassembly stuff, but I started
106 * this at version 0.8.20, so c'est la vie. It wouldn't have covered the
107 * netlib stuff anyway, so no big loss.
108 * . The older two layer version of the code dissected the PDU's, but the new
109 * version does not yet, it only labels the names. I need an elegant way to
110 * deal with dissecting data crossing (netlib and tcp) packet boundries. I
111 * think I have one, but ran out of time to do it.
112 * . It will only work on little endian platforms. Or rather I should say,
113 * the client that was captured must be little endian. TDS 7.0/8.0 is
114 * always LE; for TDS 4.2/5.0 look in the code for tvb_get_le*() functions,
115 * there are fields in the login packet which determine byte order.
116 * . result sets that span netlib packets are not working
117 * . TDS 7 and 4.2 result sets are not working yet
119 * All that said, the code does deal gracefully with different boudary
120 * conditions and what remains are the easier bits, IMHO.
122 * XXX - "real packets" means "TCP segments", for TCP.
124 * XXX - is it *REALLY* true that you can have more than one TDS PDU (as
125 * opposed to more than one server response item) per NETLIB packet? Or is
126 * all the data in a NETLIB packet put into a single TDS PDU? If so, then
127 * we can reassemble NETLIB packets using the standard TCP desegmentation
128 * code, and can reassemble TDS PDUs using "fragment_add_seq_check()",
129 * and more cleanly separate the NETLIB and TDS dissectors (although the
130 * "is this NETLIB" heuristic would have to look at TDS information past
131 * the NETLIB header, in order to make the heuristic strong enough not
132 * to get too many false positives; note that the heuristic should reject
133 * any putative NETLIB packet with a length field with a value < 8).
135 * That would substantially clean the dissector up, eliminating most of
136 * the per-packet data (we might still need information to handle
137 * TDS_ROW_TOKEN), getting rid of the stuff to handle data split across
138 * TCP segment boundaries in favor of simple reassembly code, and
139 * fixing some otherwise nasty-looking crashing bugs.
141 * NOTE: we assume that all the data in a NETLIB packet *can* be put into
142 * a single TDS PTU, so that we have separate reassembly of NETLIB
143 * packets and TDS PDUs; it seems to work, and it really did clean stuff
144 * up and fix crashes.
159 #include <epan/packet.h>
160 #include <epan/conversation.h>
161 #include <epan/strutil.h>
163 #include "packet-frame.h"
164 #include <epan/reassemble.h>
165 #include <epan/prefs.h>
166 #include <epan/emem.h>
167 #include <epan/expert.h>
169 #define TDS_QUERY_PKT 1
170 #define TDS_LOGIN_PKT 2
171 #define TDS_RPC_PKT 3
172 #define TDS_RESP_PKT 4
173 #define TDS_RAW_PKT 5
174 #define TDS_ATTENTION_PKT 6
175 #define TDS_BULK_DATA_PKT 7
176 #define TDS_OPEN_CHN_PKT 8
177 #define TDS_CLOSE_CHN_PKT 9
178 #define TDS_RES_ERROR_PKT 10
179 #define TDS_LOG_CHN_ACK_PKT 11
180 #define TDS_ECHO_PKT 12
181 #define TDS_LOGOUT_CHN_PKT 13
182 #define TDS_TRANS_MGR_PKT 14
183 #define TDS_QUERY5_PKT 15 /* or "Normal tokenized request or response */
184 #define TDS_LOGIN7_PKT 16 /* or "Urgent tokenized request or response */
185 #define TDS_SSPI_PKT 17
186 #define TDS_PRELOGIN_PKT 18
187 #define TDS_INVALID_PKT 19
189 #define is_valid_tds_type(x) ((x) >= TDS_QUERY_PKT && (x) < TDS_INVALID_PKT)
191 /* The following constants are imported more or less directly from FreeTDS */
192 /* Updated from FreeTDS v0.63 tds.h */
193 /* "$Id: tds.h,v 1.192 2004/10/28 12:42:12 freddy77]" */
194 /* Note: [###] below means 'not defined in FreeTDS tds.h' */
196 #define TDS5_PARAMFMT2_TOKEN 32 /* 0x20 TDS 5.0 only */
197 #define TDS_LANG_TOKEN 33 /* 0x21 TDS 5.0 only */
198 #define TDS5_ORDERBY2_TOKEN 34 /* 0x22 TDS 5.0 only */
199 #define TDS5_CURDECLARE2_TOKEN 35 /* 0x23 TDS 5.0 only [###] */
200 #define TDS5_ROWFMT2_TOKEN 97 /* 0x61 TDS 5.0 only */
201 #define TDS5_MSG_TOKEN 101 /* 0x65 TDS 5.0 only [###] */
202 #define TDS_LOGOUT_TOKEN 113 /* 0x71 TDS 5.0 only? ct_close() */
203 #define TDS_RET_STAT_TOKEN 121 /* 0x79 */
204 #define TDS_PROCID_TOKEN 124 /* 0x7C TDS 4.2 only - TDS_PROCID */
205 #define TDS_CURCLOSE_TOKEN 128 /* 0x80 TDS 5.0 only */
206 #define TDS7_RESULT_TOKEN 129 /* 0x81 TDS 7.0 only */
207 #define TDS_CURFETCH_TOKEN 130 /* 0x82 TDS 5.0 only */
208 #define TDS_CURINFO_TOKEN 131 /* 0x83 TDS 5.0 only */
209 #define TDS_CUROPEN_TOKEN 132 /* 0x84 TDS 5.0 only */
210 #define TDS_CURDECLARE_TOKEN 134 /* 0x86 TDS 5.0 only */
211 #define TDS7_COMPUTE_RESULT_TOKEN 136 /* 0x88 TDS 7.0 only */
212 #define TDS_COL_NAME_TOKEN 160 /* 0xA0 TDS 4.2 only */
213 #define TDS_COL_INFO_TOKEN 161 /* 0xA1 TDS 4.2 only - TDS_COLFMT */
214 #define TDS5_DYNAMIC2_TOKEN 163 /* 0xA3 TDS 5.0 only */
215 #if 0 /* XX: Why commented out ? These are 'live' in FreeTDS tds.h */
216 #define TDS_TABNAME 164 /* 0xA4 */
217 #define TDS_COL_INFO 165 /* 0xA5 */
219 #define TDS_OPTIONCMD_TOKEN 166 /* 0xA6 */
220 #define TDS_COMPUTE_NAMES_TOKEN 167 /* 0xA7 */
221 #define TDS_COMPUTE_RESULT_TOKEN 168 /* 0xA8 */
222 #define TDS_ORDER_BY_TOKEN 169 /* 0xA9 TDS_ORDER */
223 #define TDS_ERR_TOKEN 170 /* 0xAA */
224 #define TDS_MSG_TOKEN 171 /* 0xAB */
225 #define TDS_PARAM_TOKEN 172 /* 0xAC RETURNVALUE? */
226 #define TDS_LOGIN_ACK_TOKEN 173 /* 0xAD */
227 #define TDS_CONTROL_TOKEN 174 /* 0xAE TDS_CONTROL */
228 #define TDS_KEY_TOKEN 202 /* 0xCA [###] */
229 #define TDS_ROW_TOKEN 209 /* 0xD1 */
230 #define TDS_CMP_ROW_TOKEN 211 /* 0xD3 */
231 #define TDS5_PARAMS_TOKEN 215 /* 0xD7 TDS 5.0 only */
232 #define TDS_CAP_TOKEN 226 /* 0xE2 */
233 #define TDS_ENV_CHG_TOKEN 227 /* 0xE3 */
234 #define TDS_EED_TOKEN 229 /* 0xE5 */
235 #define TDS_DBRPC_TOKEN 230 /* 0xE6 */
236 #define TDS5_DYNAMIC_TOKEN 231 /* 0xE7 TDS 5.0 only */
237 #define TDS5_PARAMFMT_TOKEN 236 /* 0xEC TDS 5.0 only */
238 #define TDS_AUTH_TOKEN 237 /* 0xED */
239 #define TDS_RESULT_TOKEN 238 /* 0xEE */
240 #define TDS_DONE_TOKEN 253 /* 0xFD TDS_DONE */
241 #define TDS_DONEPROC_TOKEN 254 /* 0xFE TDS_DONEPROC */
242 #define TDS_DONEINPROC_TOKEN 255 /* 0xFF TDS_DONEINPROC */
244 /* Microsoft internal stored procedure id's */
246 #define TDS_SP_CURSOR 1
247 #define TDS_SP_CURSOROPEN 2
248 #define TDS_SP_CURSORPREPARE 3
249 #define TDS_SP_CURSOREXECUTE 4
250 #define TDS_SP_CURSORPREPEXEC 5
251 #define TDS_SP_CURSORUNPREPARE 6
252 #define TDS_SP_CURSORFETCH 7
253 #define TDS_SP_CURSOROPTION 8
254 #define TDS_SP_CURSORCLOSE 9
255 #define TDS_SP_EXECUTESQL 10
256 #define TDS_SP_PREPARE 11
257 #define TDS_SP_EXECUTE 12
258 #define TDS_SP_PREPEXEC 13
259 #define TDS_SP_PREPEXECRPC 14
260 #define TDS_SP_UNPREPARE 15
263 #define TDS_RPC_OPT_WITH_RECOMP 0x01
264 #define TDS_RPC_OPT_NO_METADATA 0x02
265 #define TDS_RPC_OPT_REUSE_METADATA 0x04
267 #define TDS_RPC_PARAMETER_STATUS_BY_REF 0x01
268 #define TDS_RPC_PARAMETER_STATUS_DEFAULT 0x02
270 /* Sybase Data Types */
272 #define SYBCHAR 47 /* 0x2F */
273 #define SYBVARCHAR 39 /* 0x27 */
274 #define SYBINTN 38 /* 0x26 */
275 #define SYBINT1 48 /* 0x30 */
276 #define SYBINT2 52 /* 0x34 */
277 #define SYBINT4 56 /* 0x38 */
278 #define SYBINT8 127 /* 0x7F */
279 #define SYBFLT8 62 /* 0x3E */
280 #define SYBDATETIME 61 /* 0x3D */
281 #define SYBBIT 50 /* 0x32 */
282 #define SYBTEXT 35 /* 0x23 */
283 #define SYBNTEXT 99 /* 0x63 */
284 #define SYBIMAGE 34 /* 0x22 */
285 #define SYBMONEY4 122 /* 0x7A */
286 #define SYBMONEY 60 /* 0x3C */
287 #define SYBDATETIME4 58 /* 0x3A */
288 #define SYBREAL 59 /* 0x3B */
289 #define SYBBINARY 45 /* 0x2D */
290 #define SYBVOID 31 /* 0x1F */
291 #define SYBVARBINARY 37 /* 0x25 */
292 #define SYBNVARCHAR 103 /* 0x67 */
293 #define SYBBITN 104 /* 0x68 */
294 #define SYBNUMERIC 108 /* 0x6C */
295 #define SYBDECIMAL 106 /* 0x6A */
296 #define SYBFLTN 109 /* 0x6D */
297 #define SYBMONEYN 110 /* 0x6E */
298 #define SYBDATETIMN 111 /* 0x6F */
299 #define XSYBCHAR 175 /* 0xA7 */
300 #define XSYBVARCHAR 167 /* 0xAF */
301 #define XSYBNVARCHAR 231 /* 0xE7 */
302 #define XSYBNCHAR 239 /* 0xEF */
303 #define XSYBVARBINARY 165 /* 0xA5 */
304 #define XSYBBINARY 173 /* 0xAD */
305 #define SYBLONGBINARY 225 /* 0xE1 */
306 #define SYBSINT1 64 /* 0x40 */
307 #define SYBUINT2 65 /* 0x41 */
308 #define SYBUINT4 66 /* 0x42 */
309 #define SYBUINT8 67 /* 0x43 */
310 #define SYBUNIQUE 36 /* 0x24 */
311 #define SYBVARIANT 98 /* 0x62 */
313 #define is_fixed_coltype(x) (x==SYBINT1 || \
327 #define TDS_DATA_TYPE_NULL 0x1F /* Null (no data associated with this type) */
328 #define TDS_DATA_TYPE_INT1 0x30 /* TinyInt (1 byte data representation) */
329 #define TDS_DATA_TYPE_BIT 0x32 /* Bit (1 byte data representation) */
330 #define TDS_DATA_TYPE_INT2 0x34 /* SmallInt (2 byte data representation) */
331 #define TDS_DATA_TYPE_INT4 0x38 /* Int (4 byte data representation) */
332 #define TDS_DATA_TYPE_DATETIM4 0x3A /* SmallDateTime (4 byte data representation) */
333 #define TDS_DATA_TYPE_FLT4 0x3B /* Real (4 byte data representation) */
334 #define TDS_DATA_TYPE_MONEY 0x3C /* Money (8 byte data representation) */
335 #define TDS_DATA_TYPE_DATETIME 0x3D /* DateTime (8 byte data representation) */
336 #define TDS_DATA_TYPE_FLT8 0x3E /* Float (8 byte data representation) */
337 #define TDS_DATA_TYPE_MONEY4 0x7A /* SmallMoney (4 byte data representation) */
338 #define TDS_DATA_TYPE_INT8 0x7F /* BigInt (8 byte data representation) */
340 #define TDS_DATA_TYPE_GUID 0x24 /* UniqueIdentifier */
341 #define TDS_DATA_TYPE_INTN 0x26
342 #define TDS_DATA_TYPE_DECIMAL 0x37 /* Decimal (legacy support) */
343 #define TDS_DATA_TYPE_NUMERIC 0x3F /* Numeric (legacy support) */
344 #define TDS_DATA_TYPE_BITN 0x68
345 #define TDS_DATA_TYPE_DECIMALN 0x6A /* Decimal */
346 #define TDS_DATA_TYPE_NUMERICN 0x6C /* Numeric */
347 #define TDS_DATA_TYPE_FLTN 0x6D
348 #define TDS_DATA_TYPE_MONEYN 0x6E
349 #define TDS_DATA_TYPE_DATETIMN 0x6F
350 #define TDS_DATA_TYPE_DATEN 0x28 /* (introduced in TDS 7.3) */
351 #define TDS_DATA_TYPE_TIMEN 0x29 /* (introduced in TDS 7.3) */
352 #define TDS_DATA_TYPE_DATETIME2N 0x2A /* (introduced in TDS 7.3) */
353 #define TDS_DATA_TYPE_DATETIMEOFFSETN 0x2B /* (introduced in TDS 7.3) */
354 #define TDS_DATA_TYPE_CHAR 0x2F /* Char (legacy support) */
355 #define TDS_DATA_TYPE_VARCHAR 0x27 /* VarChar (legacy support) */
356 #define TDS_DATA_TYPE_BINARY 0x2D /* Binary (legacy support) */
357 #define TDS_DATA_TYPE_VARBINARY 0x25 /* VarBinary (legacy support) */
359 #define TDS_DATA_TYPE_BIGVARBIN 0xA5 /* VarBinary */
360 #define TDS_DATA_TYPE_BIGVARCHR 0xA7 /* VarChar */
361 #define TDS_DATA_TYPE_BIGBINARY 0xAD /* Binary */
362 #define TDS_DATA_TYPE_BIGCHAR 0xAF /* Char */
363 #define TDS_DATA_TYPE_NVARCHAR 0xE7 /* NVarChar */
364 #define TDS_DATA_TYPE_NCHAR 0xEF /* NChar */
366 #define TDS_DATA_TYPE_XML 0xF1 /* XML (introduced in TDS 7.2) */
367 #define TDS_DATA_TYPE_UDT 0xF0 /* CLR-UDT (introduced in TDS 7.2) */
368 #define TDS_DATA_TYPE_TEXT 0x23 /* Text */
369 #define TDS_DATA_TYPE_IMAGE 0x22 /* Image */
370 #define TDS_DATA_TYPE_NTEXT 0x63 /* NText */
371 #define TDS_DATA_TYPE_SSVARIANT 0x62 /* Sql_Variant (introduced in TDS 7.2) */
373 static const value_string tds_data_type_names[] = {
375 {TDS_DATA_TYPE_NULL, "NULLTYPE - Null (no data associated with this type)"},
376 {TDS_DATA_TYPE_INT1, "INT1TYPE - TinyInt (1 byte data representation)"},
377 {TDS_DATA_TYPE_BIT, "BITTYPE - Bit (1 byte data representation)"},
378 {TDS_DATA_TYPE_INT2, "INT2TYPE - SmallInt (2 byte data representation)"},
379 {TDS_DATA_TYPE_INT4, "INT4TYPE - Int (4 byte data representation)"},
380 {TDS_DATA_TYPE_DATETIM4, "DATETIM4TYPE - SmallDateTime (4 byte data representation)"},
381 {TDS_DATA_TYPE_FLT4, "FLT4TYPE - Real (4 byte data representation)"},
382 {TDS_DATA_TYPE_MONEY, "MONEYTYPE - Money (8 byte data representation)"},
383 {TDS_DATA_TYPE_DATETIME, "DATETIMETYPE - DateTime (8 byte data representation)"},
384 {TDS_DATA_TYPE_FLT8, "FLT8TYPE - Float (8 byte data representation)"},
385 {TDS_DATA_TYPE_MONEY4, "MONEY4TYPE - SmallMoney (4 byte data representation)"},
386 {TDS_DATA_TYPE_INT8, "INT8TYPE - BigInt (8 byte data representation)"},
388 {TDS_DATA_TYPE_GUID, "GUIDTYPE - UniqueIdentifier"},
389 {TDS_DATA_TYPE_INTN, "INTNTYPE"},
390 {TDS_DATA_TYPE_DECIMAL, "DECIMALTYPE - Decimal (legacy support)"},
391 {TDS_DATA_TYPE_NUMERIC, "NUMERICTYPE - Numeric (legacy support)"},
392 {TDS_DATA_TYPE_BITN, "BITNTYPE"},
393 {TDS_DATA_TYPE_DECIMALN, "DECIMALNTYPE - Decimal"},
394 {TDS_DATA_TYPE_NUMERICN, "NUMERICNTYPE - Numeric"},
395 {TDS_DATA_TYPE_FLTN, "FLTNTYPE"},
396 {TDS_DATA_TYPE_MONEYN, "MONEYNTYPE"},
397 {TDS_DATA_TYPE_DATETIMN, "DATETIMNTYPE"},
398 {TDS_DATA_TYPE_DATEN, "DATENTYPE - (introduced in TDS 7.3)"},
399 {TDS_DATA_TYPE_TIMEN, "TIMENTYPE - (introduced in TDS 7.3)"},
400 {TDS_DATA_TYPE_DATETIME2N, "DATETIME2NTYPE - (introduced in TDS 7.3)"},
401 {TDS_DATA_TYPE_DATETIMEOFFSETN, "DATETIMEOFFSETNTYPE - (introduced in TDS 7.3)"},
402 {TDS_DATA_TYPE_CHAR, "CHARTYPE - Char (legacy support)"},
403 {TDS_DATA_TYPE_VARCHAR, "VARCHARTYPE - VarChar (legacy support)"},
404 {TDS_DATA_TYPE_BINARY, "BINARYTYPE - Binary (legacy support)"},
405 {TDS_DATA_TYPE_VARBINARY, "VARBINARYTYPE - VarBinary (legacy support)"},
407 {TDS_DATA_TYPE_BIGVARBIN, "BIGVARBINTYPE - VarBinary"},
408 {TDS_DATA_TYPE_BIGVARCHR, "BIGVARCHRTYPE - VarChar"},
409 {TDS_DATA_TYPE_BIGBINARY, "BIGBINARYTYPE - Binary"},
410 {TDS_DATA_TYPE_BIGCHAR, "BIGCHARTYPE - Char"},
411 {TDS_DATA_TYPE_NVARCHAR, "NVARCHARTYPE - NVarChar"},
412 {TDS_DATA_TYPE_NCHAR, "NCHARTYPE - NChar"},
414 {TDS_DATA_TYPE_XML, "XMLTYPE - XML (introduced in TDS 7.2)"},
415 {TDS_DATA_TYPE_UDT, "UDTTYPE - CLR-UDT (introduced in TDS 7.2)"},
416 {TDS_DATA_TYPE_TEXT, "TEXTTYPE - Text"},
417 {TDS_DATA_TYPE_IMAGE, "IMAGETYPE - Image"},
418 {TDS_DATA_TYPE_NTEXT, "NTEXTTYPE - NText"},
419 {TDS_DATA_TYPE_SSVARIANT, "SSVARIANTTYPE - Sql_Variant (introduced in TDS 7.2)"},
423 /* Initialize the protocol and registered fields */
424 static int proto_tds = -1;
425 static int hf_tds_type = -1;
426 static int hf_tds_status = -1;
427 static int hf_tds_status_eom = -1;
428 static int hf_tds_status_ignore = -1;
429 static int hf_tds_status_event_notif = -1;
430 static int hf_tds_status_reset_conn = -1;
431 static int hf_tds_status_reset_conn_skip_tran = -1;
432 static int hf_tds_length = -1;
433 static int hf_tds_channel = -1;
434 static int hf_tds_packet_number = -1;
435 static int hf_tds_window = -1;
436 static int hf_tds_reassembled_in = -1;
437 static int hf_tds_reassembled_length = -1;
438 static int hf_tds_fragments = -1;
439 static int hf_tds_fragment = -1;
440 static int hf_tds_fragment_overlap = -1;
441 static int hf_tds_fragment_overlap_conflict = -1;
442 static int hf_tds_fragment_multiple_tails = -1;
443 static int hf_tds_fragment_too_long_fragment = -1;
444 static int hf_tds_fragment_error = -1;
445 static int hf_tds_fragment_count = -1;
447 static int hf_tds7_login_total_size = -1;
448 static int hf_tds7_version = -1;
449 static int hf_tds7_packet_size = -1;
450 static int hf_tds7_client_version = -1;
451 static int hf_tds7_client_pid = -1;
452 static int hf_tds7_connection_id = -1;
453 static int hf_tds7_option_flags1 = -1;
454 static int hf_tds7_option_flags2 = -1;
455 static int hf_tds7_sql_type_flags = -1;
456 static int hf_tds7_reserved_flags = -1;
457 static int hf_tds7_time_zone = -1;
458 static int hf_tds7_collation = -1;
460 static int hf_tds_all_headers = -1;
461 static int hf_tds_all_headers_total_length = -1;
462 static int hf_tds_all_headers_header_length = -1;
463 static int hf_tds_all_headers_header_type = -1;
464 static int hf_tds_all_headers_trans_descr = -1;
465 static int hf_tds_all_headers_request_cnt = -1;
467 static int hf_tds_type_info = -1;
468 static int hf_tds_type_info_type = -1;
469 static int hf_tds_type_info_varlen = -1;
470 static int hf_tds_type_info_precision = -1;
471 static int hf_tds_type_info_scale = -1;
472 static int hf_tds_type_info_collation = -1;
473 static int hf_tds_type_info_collation_lcid = -1;
474 static int hf_tds_type_info_collation_ign_case = -1;
475 static int hf_tds_type_info_collation_ign_accent = -1;
476 static int hf_tds_type_info_collation_ign_kana = -1;
477 static int hf_tds_type_info_collation_ign_width = -1;
478 static int hf_tds_type_info_collation_binary = -1;
479 static int hf_tds_type_info_collation_version = -1;
480 static int hf_tds_type_info_collation_sortid = -1;
481 static int hf_tds_type_varbyte_length = -1;
482 static int hf_tds_type_varbyte_data_null = -1;
483 static int hf_tds_type_varbyte_data_boolean = -1;
484 static int hf_tds_type_varbyte_data_int1 = -1;
485 static int hf_tds_type_varbyte_data_int2 = -1;
486 static int hf_tds_type_varbyte_data_int4 = -1;
487 static int hf_tds_type_varbyte_data_int8 = -1;
488 static int hf_tds_type_varbyte_data_float = -1;
489 static int hf_tds_type_varbyte_data_double = -1;
490 static int hf_tds_type_varbyte_data_bytes = -1;
491 static int hf_tds_type_varbyte_data_guid = -1;
492 static int hf_tds_type_varbyte_data_string = -1;
493 static int hf_tds_type_varbyte_plp_len = -1;
494 static int hf_tds_type_varbyte_plp_chunk_len = -1;
496 static int hf_tds_rpc = -1;
497 static int hf_tds_rpc_name_length8 = -1;
498 static int hf_tds_rpc_name_length = -1;
499 static int hf_tds_rpc_name = -1;
500 static int hf_tds_rpc_proc_id = -1;
501 static int hf_tds_rpc_options = -1;
502 static int hf_tds_rpc_options_with_recomp = -1;
503 static int hf_tds_rpc_options_no_metadata = -1;
504 static int hf_tds_rpc_options_reuse_metadata = -1;
505 static int hf_tds_rpc_separator = -1;
506 static int hf_tds_rpc_parameter = -1;
507 static int hf_tds_rpc_parameter_name_length = -1;
508 static int hf_tds_rpc_parameter_name = -1;
509 static int hf_tds_rpc_parameter_status = -1;
510 static int hf_tds_rpc_parameter_status_by_ref = -1;
511 static int hf_tds_rpc_parameter_status_default = -1;
512 static int hf_tds_rpc_parameter_value = -1;
514 /* Initialize the subtree pointers */
515 static gint ett_tds = -1;
516 static gint ett_tds_status = -1;
517 static gint ett_tds_fragments = -1;
518 static gint ett_tds_fragment = -1;
519 static gint ett_tds_token = -1;
520 static gint ett_tds_all_headers = -1;
521 static gint ett_tds_all_headers_header = -1;
522 static gint ett_tds_type_info = -1;
523 static gint ett_tds_type_info_collation = -1;
524 static gint ett_tds_type_varbyte = -1;
525 static gint ett_tds_message = -1;
526 static gint ett_tds_rpc_options = -1;
527 static gint ett_tds_rpc_parameter = -1;
528 static gint ett_tds_rpc_parameter_status = -1;
529 static gint ett_tds7_query = -1;
530 static gint ett_tds7_login = -1;
531 static gint ett_tds7_hdr = -1;
533 /* Desegmentation of Netlib buffers crossing TCP segment boundaries. */
534 static gboolean tds_desegment = TRUE;
536 static const fragment_items tds_frag_items = {
541 &hf_tds_fragment_overlap,
542 &hf_tds_fragment_overlap_conflict,
543 &hf_tds_fragment_multiple_tails,
544 &hf_tds_fragment_too_long_fragment,
545 &hf_tds_fragment_error,
546 &hf_tds_fragment_count,
547 &hf_tds_reassembled_in,
548 &hf_tds_reassembled_length,
552 /* Tables for reassembly of fragments. */
553 static GHashTable *tds_fragment_table = NULL;
554 static GHashTable *tds_reassembled_table = NULL;
556 /* defragmentation of multi-buffer TDS PDUs */
557 static gboolean tds_defragment = TRUE;
559 static dissector_handle_t tds_tcp_handle;
560 static dissector_handle_t ntlmssp_handle;
561 static dissector_handle_t gssapi_handle;
562 static dissector_handle_t data_handle;
564 /* TDS protocol type preference */
565 /* XXX: This preference is used as a 'hint' for cases where interpretation is ambiguous */
566 /* Currently the hint is global */
567 /* TODO: Consider storing protocol type with each conversation */
568 /* (when type is determined and using the preference as a default) ?? */
570 #define TDS_PROTOCOL_NOT_SPECIFIED 0
571 #define TDS_PROTOCOL_4 0x40
572 #define TDS_PROTOCOL_5 0x50
573 #define TDS_PROTOCOL_7_0 0x70
574 #define TDS_PROTOCOL_7_1 0x71
575 #define TDS_PROTOCOL_7_2 0x72
576 #define TDS_PROTOCOL_7_3 0x73
578 static gint tds_protocol_type = TDS_PROTOCOL_NOT_SPECIFIED;
580 static const enum_val_t tds_protocol_type_options[] = {
581 {"not_specified", "Not Specified", TDS_PROTOCOL_NOT_SPECIFIED},
582 {"tds4", "TDS 4", TDS_PROTOCOL_4}, /* TDS 4.2 and TDS 4.6 */
583 {"tds5", "TDS 5", TDS_PROTOCOL_5},
584 {"tds70", "TDS 7.0", TDS_PROTOCOL_7_0},
585 {"tds71", "TDS 7.1", TDS_PROTOCOL_7_1},
586 {"tds72", "TDS 7.2", TDS_PROTOCOL_7_2},
587 {"tds73", "TDS 7.3", TDS_PROTOCOL_7_3},
591 #define TDS_PROTO_PREF_NOT_SPECIFIED (tds_protocol_type == TDS_NOT_SPECIFIED)
592 #define TDS_PROTO_PREF_TDS4 (tds_protocol_type == TDS_PROTOCOL_4)
593 #define TDS_PROTO_PREF_TDS5 (tds_protocol_type == TDS_PROTOCOL_5)
594 #define TDS_PROTO_PREF_TDS7_0 (tds_protocol_type == TDS_PROTOCOL_7_0)
595 #define TDS_PROTO_PREF_TDS7_1 (tds_protocol_type == TDS_PROTOCOL_7_1)
596 #define TDS_PROTO_PREF_TDS7_2 (tds_protocol_type == TDS_PROTOCOL_7_2)
597 #define TDS_PROTO_PREF_TDS7_3 (tds_protocol_type == TDS_PROTOCOL_7_3)
598 #define TDS_PROTO_PREF_TDS7 (tds_protocol_type >= TDS_PROTOCOL_7_0 && tds_protocol_type <= TDS_PROTOCOL_7_3)
600 /* TDS "endian type" */
601 /* XXX: Assumption is that all TDS conversations being decoded in a particular capture */
602 /* have the same endian type */
603 /* TODO: consider storing endian type with each conversation */
604 /* (using pref as the default) */
606 static gboolean tds_little_endian = TRUE;
608 static const enum_val_t tds_endian_type_options[] = {
609 {"little_endian", "Little Endian", TRUE},
610 {"big_endian" , "Big Endian" , FALSE},
615 /* TCP port preferences for TDS decode */
617 static range_t *tds_tcp_ports = NULL;
619 /* These correspond to the netlib packet type field */
620 static const value_string packet_type_names[] = {
621 {TDS_QUERY_PKT, "SQL batch"},
622 {TDS_LOGIN_PKT, "Pre-TDS7 login"},
623 {TDS_RPC_PKT, "Remote Procedure Call"},
624 {TDS_RESP_PKT, "Response"},
625 {TDS_RAW_PKT, "Unused"},
626 {TDS_ATTENTION_PKT, "Attention"},
627 {TDS_BULK_DATA_PKT, "Bulk load data"},
628 {TDS_QUERY5_PKT, "TDS5 query"},
629 {TDS_LOGIN7_PKT, "TDS7 login"},
630 {TDS_SSPI_PKT, "SSPI message"},
631 {TDS_PRELOGIN_PKT, "TDS7 pre-login message"},
636 TDS_HEADER_QUERY_NOTIF = 0x0001,
637 TDS_HEADER_TRANS_DESCR = 0x0002
640 static const value_string header_type_names[] = {
641 {TDS_HEADER_QUERY_NOTIF, "Query notifications"},
642 {TDS_HEADER_TRANS_DESCR, "Transaction descriptor"},
646 /* The status field */
648 #define is_valid_tds_status(x) ((x) <= STATUS_EVENT_NOTIFICATION)
650 #define STATUS_LAST_BUFFER 0x01
651 #define STATUS_IGNORE_EVENT 0x02
652 #define STATUS_EVENT_NOTIFICATION 0x04
653 #define STATUS_RESETCONNECTION 0x08
654 #define STATUS_RESETCONNECTIONSKIPTRAN 0x10
656 /* The one byte token at the start of each TDS PDU */
657 static const value_string token_names[] = {
658 {TDS5_DYNAMIC_TOKEN, "TDS5 Dynamic SQL"},
659 {TDS5_PARAMFMT_TOKEN, "TDS5 Parameter Format"},
660 {TDS5_PARAMFMT2_TOKEN, "TDS5 Parameter2 Format"},
661 {TDS5_PARAMS_TOKEN, "TDS5 Parameters"},
662 {TDS_LANG_TOKEN, "Language"},
663 {TDS_LOGOUT_TOKEN, "Logout"},
664 {TDS_RET_STAT_TOKEN, "Return Status"},
665 {TDS_PROCID_TOKEN, "Proc ID"},
666 {TDS7_RESULT_TOKEN, "TDS7+ Results"},
667 {TDS_COL_NAME_TOKEN, "Column Names"},
668 {TDS_COL_INFO_TOKEN, "Column Info"},
669 {TDS_COMPUTE_NAMES_TOKEN, "Compute Names"},
670 {TDS_COMPUTE_RESULT_TOKEN, "Compute Results"},
671 {TDS_ORDER_BY_TOKEN, "Order By"},
672 {TDS_ERR_TOKEN, "Error Message"},
673 {TDS_MSG_TOKEN, "Info Message"},
674 {TDS_PARAM_TOKEN, "Parameter"},
675 {TDS_LOGIN_ACK_TOKEN, "Login Acknowledgement"},
676 {TDS_CONTROL_TOKEN, "TDS Control"},
677 {TDS_KEY_TOKEN, "TDS Key"},
678 {TDS_ROW_TOKEN, "Row"},
679 {TDS_CMP_ROW_TOKEN, "Compute Row"},
680 {TDS_CAP_TOKEN, "Capabilities"},
681 {TDS_ENV_CHG_TOKEN, "Environment Change"},
682 {TDS_EED_TOKEN, "Extended Error"},
683 {TDS_AUTH_TOKEN, "Authentication"},
684 {TDS_RESULT_TOKEN, "Results"},
685 {TDS_DONE_TOKEN, "Done"},
686 {TDS_DONEPROC_TOKEN, "Done Proc"},
687 {TDS_DONEINPROC_TOKEN, "Done In Proc"},
688 {TDS5_DYNAMIC2_TOKEN, "TDS5 Dynamic2"},
689 {TDS5_ORDERBY2_TOKEN, "TDS5 OrderBy2"},
690 {TDS5_CURDECLARE2_TOKEN, "TDS5 CurDeclare2"},
691 {TDS5_ROWFMT2_TOKEN, "TDS5 RowFmt2"},
692 {TDS5_MSG_TOKEN, "TDS5 Msg"},
696 #define TDS_RPC_SEPARATOR_BATCH_FLAG 0x80
697 #define TDS_RPC_SEPARATOR_BATCH_FLAG_7_2 0xFF
698 #define TDS_RPC_SEPARATOR_NO_EXEC_FLAG 0xFE
700 static const value_string tds_rpc_separators[] = {
701 {TDS_RPC_SEPARATOR_BATCH_FLAG, "Batch flag"},
702 {TDS_RPC_SEPARATOR_BATCH_FLAG_7_2, "Batch flag 7.2"},
703 {TDS_RPC_SEPARATOR_NO_EXEC_FLAG, "No exec flag"},
707 static const value_string internal_stored_proc_id_names[] = {
708 {TDS_SP_CURSOR, "sp_cursor" },
709 {TDS_SP_CURSOROPEN, "sp_cursoropen" },
710 {TDS_SP_CURSORPREPARE, "sp_cursorprepare" },
711 {TDS_SP_CURSOREXECUTE, "sp_cursorexecute" },
712 {TDS_SP_CURSORPREPEXEC, "sp_cursorprepexec" },
713 {TDS_SP_CURSORUNPREPARE, "sp_cursorunprepare"},
714 {TDS_SP_CURSORFETCH, "sp_cursorfetch" },
715 {TDS_SP_CURSOROPTION, "sp_cursoroption" },
716 {TDS_SP_CURSORCLOSE, "sp_cursorclose" },
717 {TDS_SP_EXECUTESQL, "sp_executesql" },
718 {TDS_SP_PREPARE, "sp_prepare" },
719 {TDS_SP_EXECUTE, "sp_execute" },
720 {TDS_SP_PREPEXEC, "sp_prepexec" },
721 {TDS_SP_PREPEXECRPC, "sp_prepexecrpc" },
722 {TDS_SP_UNPREPARE, "sp_unprepare" },
726 static const value_string env_chg_names[] = {
731 {5, "Unicode Locale ID"},
732 {6, "Unicode Comparison Style"},
733 {7, "Collation Info"},
737 static const value_string login_field_names[] = {
746 {8, "Database Name"},
751 #define MAX_COLUMNS 256
754 * This is where we store the column information to be used in decoding the
755 * TDS_ROW_TOKEN tokens.
764 struct _netlib_data {
766 struct _tds_col *columns[MAX_COLUMNS];
769 struct tds7_login_packet_hdr {
770 guint32 total_packet_size;
773 guint32 client_version;
775 guint32 connection_id;
776 guint8 option_flags1;
777 guint8 option_flags2;
778 guint8 sql_type_flags;
779 guint8 reserved_flags;
784 /* support routines */
787 dissect_tds_nt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
788 guint offset, guint length)
792 nt_tvb = tvb_new_subset(tvb, offset, -1, length);
793 if(tvb_strneql(tvb, offset, "NTLMSSP", 7) == 0)
794 call_dissector(ntlmssp_handle, nt_tvb, pinfo, tree);
796 call_dissector(gssapi_handle, nt_tvb, pinfo, tree);
802 tds_tvb_get_xxtohs(tvbuff_t *tvb, gint offset, gboolean tds_little_endian_flag) {
803 if (tds_little_endian_flag)
804 return tvb_get_letohs(tvb, offset);
806 return tvb_get_ntohs(tvb, offset);
810 tds_tvb_get_xxtohl(tvbuff_t *tvb, gint offset, gboolean tds_little_endian_flag) {
811 if (tds_little_endian_flag)
812 return tvb_get_letohl(tvb, offset);
814 return tvb_get_ntohl(tvb, offset);
819 tds_token_is_fixed_size(guint8 token)
823 case TDS_DONEPROC_TOKEN:
824 case TDS_DONEINPROC_TOKEN:
825 case TDS_RET_STAT_TOKEN:
826 case TDS7_RESULT_TOKEN:
827 case TDS_PROCID_TOKEN:
828 case TDS_LOGOUT_TOKEN:
837 tds_get_fixed_token_size(guint8 token)
841 case TDS_DONEPROC_TOKEN:
842 case TDS_DONEINPROC_TOKEN:
843 case TDS_PROCID_TOKEN:
845 case TDS_RET_STAT_TOKEN:
847 case TDS_LOGOUT_TOKEN:
849 case TDS7_RESULT_TOKEN:
856 tds_get_variable_token_size(tvbuff_t *tvb, gint offset, guint8 token,
857 guint *len_field_size_p, guint *len_field_val_p)
860 /* some tokens have a 4 byte length field */
861 case TDS5_PARAMFMT2_TOKEN:
863 case TDS5_ORDERBY2_TOKEN:
864 case TDS5_CURDECLARE2_TOKEN:
865 case TDS5_ROWFMT2_TOKEN:
866 case TDS5_DYNAMIC2_TOKEN:
867 *len_field_size_p = 4;
868 *len_field_val_p = tds_tvb_get_xxtohl(tvb, offset, tds_little_endian);
870 /* some have a 1 byte length field */
872 *len_field_size_p = 1;
873 *len_field_val_p = tvb_get_guint8(tvb, offset);
875 /* and most have a 2 byte length field */
877 *len_field_size_p = 2;
878 *len_field_val_p = tds_tvb_get_xxtohs(tvb, offset, tds_little_endian);
881 return *len_field_val_p + *len_field_size_p + 1;
886 dissect_tds_all_headers(tvbuff_t *tvb, guint *offset, packet_info *pinfo, proto_tree *tree)
888 proto_item *item = NULL, *total_length_item = NULL;
889 proto_tree *sub_tree = NULL;
890 guint32 total_length;
893 total_length = tvb_get_letohl(tvb, *offset);
894 /* Try to find out heuristically whether the ALL_HEADERS rule is actually present.
895 * In practice total_length is a single byte value, so if the extracted value exceeds 1 byte,
896 * then the headers are most likely absent. */
897 if(total_length >= 0x100)
899 item = proto_tree_add_item(tree, hf_tds_all_headers, tvb, *offset, total_length, ENC_NA);
900 sub_tree = proto_item_add_subtree(item, ett_tds_all_headers);
901 total_length_item = proto_tree_add_item(sub_tree, hf_tds_all_headers_total_length, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
903 final_offset = *offset + total_length;
906 /* dissect a stream header */
907 proto_tree *header_sub_tree = NULL;
908 proto_item *length_item = NULL, *type_item = NULL;
909 guint32 header_length;
912 header_length = tvb_get_letohl(tvb, *offset);
913 item = proto_tree_add_text(sub_tree, tvb, *offset, header_length, "Header");
914 header_sub_tree = proto_item_add_subtree(item, ett_tds_all_headers_header);
915 length_item = proto_tree_add_item(header_sub_tree, hf_tds_all_headers_header_length, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
916 if(header_length == 0 ) {
917 expert_add_info_format(pinfo, length_item, PI_MALFORMED, PI_ERROR, "Empty header");
921 header_type = tvb_get_letohs(tvb, *offset + 4);
922 type_item = proto_tree_add_item(header_sub_tree, hf_tds_all_headers_header_type, tvb, *offset + 4, 2, ENC_LITTLE_ENDIAN);
924 switch(header_type) {
925 case TDS_HEADER_QUERY_NOTIF:
927 case TDS_HEADER_TRANS_DESCR:
928 if(header_length != 18)
929 expert_add_info_format(pinfo, length_item, PI_MALFORMED, PI_ERROR, "Length should equal 18");
930 proto_tree_add_item(header_sub_tree, hf_tds_all_headers_trans_descr, tvb, *offset + 6, 8, ENC_LITTLE_ENDIAN);
931 proto_tree_add_item(header_sub_tree, hf_tds_all_headers_request_cnt, tvb, *offset + 14, 4, ENC_LITTLE_ENDIAN);
934 expert_add_info_format(pinfo, type_item, PI_MALFORMED, PI_ERROR, "Invalid header type");
937 *offset += header_length;
938 } while(*offset < final_offset);
939 if(*offset != final_offset) {
940 expert_add_info_format(pinfo, total_length_item, PI_MALFORMED, PI_ERROR,
941 "Sum of headers' lengths (%d) differs from total headers length (%d)",
942 total_length + *offset - final_offset, total_length);
949 dissect_tds_query_packet(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
952 gboolean is_unicode = TRUE;
955 proto_item *query_hdr;
956 proto_tree *query_tree;
959 query_hdr = proto_tree_add_text(tree, tvb, offset, -1, "TDS Query Packet");
960 query_tree = proto_item_add_subtree(query_hdr, ett_tds7_query);
961 dissect_tds_all_headers(tvb, &offset, pinfo, query_tree);
962 len = tvb_reported_length_remaining(tvb, offset);
964 if (TDS_PROTO_PREF_TDS4 ||
965 (!TDS_PROTO_PREF_TDS7 &&
966 ((len < 2) || tvb_get_guint8(tvb, offset+1) != 0)))
970 msg = tvb_get_ephemeral_unicode_string(tvb, offset, len, ENC_LITTLE_ENDIAN);
972 msg = (gchar*)tvb_get_ephemeral_string(tvb, offset, len);
974 proto_tree_add_text(query_tree, tvb, offset, len, "Query: %s", msg);
980 dissect_tds5_lang_token(tvbuff_t *tvb, guint offset, guint len, proto_tree *tree) {
981 gboolean is_unicode = FALSE;
984 proto_tree_add_text(tree, tvb, offset, 1 , "Status: %u", tvb_get_guint8(tvb, offset));
989 msg = tvb_get_ephemeral_unicode_string(tvb, offset, len, ENC_LITTLE_ENDIAN);
991 msg = (gchar*)tvb_get_ephemeral_string(tvb, offset, len);
993 proto_tree_add_text(tree, tvb, offset, len, "Language text: %s", msg);
997 dissect_tds_query5_packet(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
1001 guint token_len_field_size = 2;
1002 guint token_len_field_val = 0;
1005 proto_item *query_hdr;
1006 proto_tree *query_tree;
1007 proto_item *token_item;
1008 proto_tree *token_tree;
1011 query_hdr = proto_tree_add_text(tree, tvb, offset, -1, "TDS5 Query Packet");
1012 query_tree = proto_item_add_subtree(query_hdr, ett_tds7_query);
1015 * Until we reach the end of the packet, read tokens.
1018 while (tvb_reported_length_remaining(tvb, pos) > 0) {
1021 token = tvb_get_guint8(tvb, pos);
1022 if (tds_token_is_fixed_size(token))
1023 token_sz = tds_get_fixed_token_size(token) + 1;
1025 token_sz = tds_get_variable_token_size(tvb, pos+1, token, &token_len_field_size,
1026 &token_len_field_val);
1028 /* XXX - Should this check be done in tds_get_variable_token_size()
1030 if ((int) token_sz < 0) {
1031 proto_tree_add_text(query_tree, tvb, 0, 0, "Bogus token size: %u",
1036 token_item = proto_tree_add_text(query_tree, tvb, pos, token_sz,
1037 "Token 0x%02x %s", token,
1038 val_to_str(token, token_names, "Unknown Token Type"));
1039 token_tree = proto_item_add_subtree(token_item, ett_tds_token);
1042 * If it's a variable token, put the length field in here
1043 * instead of replicating this for each token subdissector.
1045 if (!tds_token_is_fixed_size(token))
1046 proto_tree_add_text(token_tree, tvb, pos+1, token_len_field_size, "Length: %u", token_len_field_val);
1049 case TDS_LANG_TOKEN:
1050 dissect_tds5_lang_token(tvb, pos + 5, token_sz -5, token_tree);
1063 dissect_tds7_login(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1065 guint offset, i, j, k, offset2, len;
1068 proto_item *login_hdr;
1069 proto_tree *login_tree;
1070 proto_item *header_hdr;
1071 proto_tree *header_tree;
1072 proto_item *length_hdr;
1073 proto_tree *length_tree;
1075 struct tds7_login_packet_hdr td7hdr;
1076 gint length_remaining;
1079 /* create display subtree for the protocol */
1081 login_hdr = proto_tree_add_text(tree, tvb, offset, -1, "TDS7 Login Packet");
1082 login_tree = proto_item_add_subtree(login_hdr, ett_tds7_login);
1083 header_hdr = proto_tree_add_text(login_tree, tvb, offset, 36, "Login Packet Header");
1084 header_tree = proto_item_add_subtree(header_hdr, ett_tds7_hdr);
1086 td7hdr.total_packet_size = tvb_get_letohl(tvb, offset);
1087 proto_tree_add_uint(header_tree, hf_tds7_login_total_size, tvb, offset,
1088 sizeof(td7hdr.total_packet_size), td7hdr.total_packet_size);
1089 offset += sizeof(td7hdr.total_packet_size);
1091 td7hdr.tds_version = tvb_get_ntohl(tvb, offset);
1092 proto_tree_add_uint(header_tree, hf_tds7_version, tvb, offset, sizeof(td7hdr.tds_version), td7hdr.tds_version);
1093 offset += sizeof(td7hdr.tds_version);
1095 td7hdr.packet_size = tvb_get_ntohl(tvb, offset);
1096 proto_tree_add_uint(header_tree, hf_tds7_packet_size, tvb, offset, sizeof(td7hdr.packet_size), td7hdr.packet_size);
1097 offset += sizeof(td7hdr.packet_size);
1099 td7hdr.client_version = tvb_get_ntohl(tvb, offset);
1100 proto_tree_add_uint(header_tree, hf_tds7_client_version, tvb, offset, sizeof(td7hdr.client_version), td7hdr.client_version);
1101 offset += sizeof(td7hdr.client_version);
1103 td7hdr.client_pid = tvb_get_letohl(tvb, offset);
1104 proto_tree_add_uint(header_tree, hf_tds7_client_pid, tvb, offset, sizeof(td7hdr.client_pid), td7hdr.client_pid);
1105 offset += sizeof(td7hdr.client_pid);
1107 td7hdr.connection_id= tvb_get_letohl(tvb, offset);
1108 proto_tree_add_uint(header_tree, hf_tds7_connection_id, tvb, offset, sizeof(td7hdr.connection_id), td7hdr.connection_id);
1109 offset += sizeof(td7hdr.connection_id);
1111 td7hdr.option_flags1 = tvb_get_guint8(tvb, offset);
1112 proto_tree_add_uint(header_tree, hf_tds7_option_flags1, tvb, offset, sizeof(td7hdr.option_flags1), td7hdr.option_flags1);
1113 offset += sizeof(td7hdr.option_flags1);
1115 td7hdr.option_flags2 = tvb_get_guint8(tvb, offset);
1116 proto_tree_add_uint(header_tree, hf_tds7_option_flags2, tvb, offset, sizeof(td7hdr.option_flags2), td7hdr.option_flags2);
1117 offset += sizeof(td7hdr.option_flags2);
1119 td7hdr.sql_type_flags = tvb_get_guint8(tvb, offset);
1120 proto_tree_add_uint(header_tree, hf_tds7_sql_type_flags, tvb, offset, sizeof(td7hdr.sql_type_flags), td7hdr.sql_type_flags);
1121 offset += sizeof(td7hdr.sql_type_flags);
1123 td7hdr.reserved_flags = tvb_get_guint8(tvb, offset);
1124 proto_tree_add_uint(header_tree, hf_tds7_reserved_flags, tvb, offset, sizeof(td7hdr.reserved_flags), td7hdr.reserved_flags);
1125 offset += sizeof(td7hdr.reserved_flags);
1127 td7hdr.time_zone = tvb_get_ntohl(tvb, offset);
1128 proto_tree_add_uint(header_tree, hf_tds7_time_zone, tvb, offset, sizeof(td7hdr.time_zone), td7hdr.time_zone);
1129 offset += sizeof(td7hdr.time_zone);
1131 td7hdr.collation = tvb_get_ntohl(tvb, offset);
1132 proto_tree_add_uint(header_tree, hf_tds7_collation, tvb, offset, sizeof(td7hdr.collation), td7hdr.collation);
1133 offset += sizeof(td7hdr.collation);
1135 length_hdr = proto_tree_add_text(login_tree, tvb, offset, 50, "Lengths and offsets");
1136 length_tree = proto_item_add_subtree(length_hdr, ett_tds7_hdr);
1138 for (i = 0; i < 9; i++) {
1139 offset2 = tvb_get_letohs(tvb, offset + i*4);
1140 len = tvb_get_letohs(tvb, offset + i*4 + 2);
1141 proto_tree_add_text(length_tree, tvb, offset + i*4, 2,
1143 val_to_str(i, login_field_names, "Unknown"),
1145 proto_tree_add_text(length_tree, tvb, offset + i*4 + 2, 2,
1147 val_to_str(i, login_field_names, "Unknown"),
1151 /* tds 7 is always unicode */
1153 val = tvb_get_ephemeral_unicode_string(tvb, offset2, len, ENC_LITTLE_ENDIAN);
1154 proto_tree_add_text(login_tree, tvb, offset2, len, "%s: %s", val_to_str(i, login_field_names, "Unknown"), val);
1156 /* This field is the password. We retrieve it from the packet
1157 * as a non-unicode string and then perform two operations on it
1158 * to "decrypt" it. Finally, we create a new string that consists
1159 * of ASCII characters instead of unicode by skipping every other
1160 * byte in the original string.
1164 val = (gchar*)tvb_get_ephemeral_string(tvb, offset2, len);
1165 val2 = g_malloc((len/2)+1);
1167 for(j = 0, k = 0; j < len; j += 2, k++) {
1170 /* Swap the most and least significant bits */
1171 val[j] = ((val[j] & 0x0F) << 4) | ((val[j] & 0xF0) >> 4);
1175 val2[k] = '\0'; /* Null terminate our new string */
1177 proto_tree_add_text(login_tree, tvb, offset2, len, "%s: %s", val_to_str(i, login_field_names, "Unknown"), val2);
1184 * XXX - what about the client MAC address, etc.?
1186 length_remaining = tvb_reported_length_remaining(tvb, offset2 + len);
1187 if (length_remaining > 0) {
1188 dissect_tds_nt(tvb, pinfo, login_tree, offset2 + len,
1194 get_size_by_coltype(int servertype)
1198 case SYBINT1: return 1;
1199 case SYBINT2: return 2;
1200 case SYBINT4: return 4;
1201 case SYBINT8: return 8;
1202 case SYBREAL: return 4;
1203 case SYBFLT8: return 8;
1204 case SYBDATETIME: return 8;
1205 case SYBDATETIME4: return 4;
1206 case SYBBIT: return 1;
1207 case SYBBITN: return 1;
1208 case SYBMONEY: return 8;
1209 case SYBMONEY4: return 4;
1210 case SYBUNIQUE: return 16;
1216 * data_to_string should take column data and turn it into something we can
1217 * display on the tree.
1219 static char *data_to_string(void *data, guint col_type, guint col_size)
1224 result=ep_alloc(256);
1227 /* strncpy(result, (char *)data, col_size); */
1228 for (i=0;i<col_size && i<(256-1);i++)
1229 if (!isprint(((char *)data)[i])) result[i]='.';
1230 else result[i]=((char *)data)[i];
1234 g_snprintf(result, 256, "%d", *(short *)data);
1237 g_snprintf(result, 256, "%d", *(int *)data);
1240 g_snprintf(result, 256, "Unexpected column_type %d", col_type);
1248 * Since rows are special PDUs in that they are not fixed and lack a size field,
1249 * the length must be computed using the column information seen in the result
1250 * PDU. This function does just that.
1253 tds_get_row_size(tvbuff_t *tvb, struct _netlib_data *nl_data, guint offset)
1255 guint cur, i, csize;
1258 for (i = 0; i < nl_data->num_cols; i++) {
1259 if (!is_fixed_coltype(nl_data->columns[i]->ctype)) {
1260 csize = tvb_get_guint8(tvb, cur);
1263 csize = get_size_by_coltype(nl_data->columns[i]->ctype);
1267 return (cur - offset + 1);
1271 * Process TDS 4 "COL_INFO" token and store relevant information in the
1272 * _netlib_data structure for later use (see tds_get_row_size)
1274 * XXX Can TDS 4 be "big-endian" ? we'll assume yes.
1278 dissect_tds_col_info_token(tvbuff_t *tvb, struct _netlib_data *nl_data, guint offset)
1283 next = offset + tds_tvb_get_xxtohs(tvb, offset+1, tds_little_endian) + 3;
1287 while (cur < next) {
1289 if (col >= MAX_COLUMNS) {
1290 nl_data->num_cols = 0;
1294 nl_data->columns[col] = ep_alloc(sizeof(struct _tds_col));
1296 nl_data->columns[col]->name[0] ='\0';
1298 nl_data->columns[col]->utype = tds_tvb_get_xxtohs(tvb, cur, tds_little_endian);
1301 cur += 2; /* unknown */
1303 nl_data->columns[col]->ctype = tvb_get_guint8(tvb,cur);
1306 if (!is_fixed_coltype(nl_data->columns[col]->ctype)) {
1307 nl_data->columns[col]->csize = tvb_get_guint8(tvb,cur);
1310 nl_data->columns[col]->csize =
1311 get_size_by_coltype(nl_data->columns[col]->ctype);
1318 nl_data->num_cols = col;
1324 * Read the results token and store the relevant information in the
1325 * _netlib_data structure for later use (see tds_get_row_size).
1327 * TODO: check we don't go past end of the token
1330 read_results_tds5(tvbuff_t *tvb, struct _netlib_data *nl_data, guint offset, guint len _U_)
1339 * This would be the logical place to check for little/big endianess
1340 * if we didn't see the login packet.
1341 * XXX: We'll take a hint
1343 nl_data->num_cols = tds_tvb_get_xxtohs(tvb, cur, tds_little_endian);
1344 if (nl_data->num_cols > MAX_COLUMNS) {
1345 nl_data->num_cols = 0;
1351 for (i = 0; i < nl_data->num_cols; i++) {
1352 nl_data->columns[i] = ep_alloc(sizeof(struct _tds_col));
1353 name_len = tvb_get_guint8(tvb,cur);
1357 cur++; /* unknown */
1359 nl_data->columns[i]->utype = tds_tvb_get_xxtohs(tvb, cur, tds_little_endian);
1362 cur += 2; /* unknown */
1364 nl_data->columns[i]->ctype = tvb_get_guint8(tvb,cur);
1367 if (!is_fixed_coltype(nl_data->columns[i]->ctype)) {
1368 nl_data->columns[i]->csize = tvb_get_guint8(tvb,cur);
1371 nl_data->columns[i]->csize =
1372 get_size_by_coltype(nl_data->columns[i]->ctype);
1374 cur++; /* unknown */
1380 * If the packet type from the netlib header is a login packet, then dig into
1381 * the packet to see if this is a supported TDS version and verify the otherwise
1382 * weak heuristics of the netlib check.
1385 netlib_check_login_pkt(tvbuff_t *tvb, guint offset, packet_info *pinfo, guint8 type)
1387 guint tds_major, bytes_avail;
1389 bytes_avail = tvb_length(tvb) - offset;
1391 * we have two login packet styles, one for TDS 4.2 and 5.0
1393 if (type==TDS_LOGIN_PKT) {
1394 /* Use major version number to validate TDS 4/5 login
1397 /* Login packet is first in stream and should not be fragmented...
1398 * if it is we are screwed */
1399 if (bytes_avail < 467) return FALSE;
1400 tds_major = tvb_get_guint8(tvb, 466);
1401 if (tds_major != 4 && tds_major != 5) {
1405 * and one added by Microsoft in SQL Server 7
1407 } else if (type==TDS_LOGIN7_PKT) {
1408 if (bytes_avail < 16) return FALSE;
1409 tds_major = tvb_get_guint8(tvb, 15);
1410 if (tds_major != 0x70 && tds_major != 0x80) {
1413 } else if (type==TDS_QUERY5_PKT) {
1414 if (bytes_avail < 9) return FALSE;
1415 /* if this is a TDS 5.0 query check the token */
1416 if (tvb_get_guint8(tvb, 8) != TDS_LANG_TOKEN) {
1421 * See if either tcp.destport or tcp.srcport is specified
1422 * in the preferences as being a TDS port.
1424 else if (!value_is_in_range(tds_tcp_ports, pinfo->srcport) &&
1425 !value_is_in_range(tds_tcp_ports, pinfo->destport)) {
1433 dissect_tds_env_chg(tvbuff_t *tvb, guint offset, guint token_sz,
1437 guint old_len, new_len, old_len_offset;
1438 char *new_val = NULL, *old_val = NULL;
1439 guint32 string_offset;
1440 gboolean is_unicode = FALSE;
1441 guint16 collate_codepage, collate_flags;
1442 guint8 collate_charset_id;
1444 env_type = tvb_get_guint8(tvb, offset);
1445 proto_tree_add_text(tree, tvb, offset, 1, "Type: %u (%s)", env_type,
1446 val_to_str(env_type, env_chg_names, "Unknown"));
1448 new_len = tvb_get_guint8(tvb, offset+1);
1449 old_len_offset = offset + new_len + 2;
1450 old_len = tvb_get_guint8(tvb, old_len_offset);
1453 * If our lengths plus the lengths of the type and the lengths
1454 * don't add up to the token size, it must be UCS2.
1456 if (old_len + new_len + 3 != token_sz) {
1458 old_len_offset = offset + (new_len * 2) + 2;
1459 old_len = tvb_get_guint8(tvb, old_len_offset);
1462 proto_tree_add_text(tree, tvb, offset + 1, 1, "New Value Length: %u",
1465 if (env_type != 7) { /* if it's not 'Collation Info - which is not textual! */
1466 string_offset = offset + 2;
1467 if (is_unicode == TRUE) {
1469 new_val = tvb_get_ephemeral_unicode_string(tvb, string_offset,
1470 new_len, ENC_LITTLE_ENDIAN);
1472 new_val = (gchar*)tvb_get_ephemeral_string(tvb, string_offset, new_len);
1473 proto_tree_add_text(tree, tvb, string_offset, new_len,
1474 "New Value: %s", new_val);
1476 else { /* parse collation info structure. From http://www.freetds.org/tds.html#collate */
1478 collate_codepage = tvb_get_letohs(tvb, offset);
1479 proto_tree_add_text(tree, tvb, offset, 2, "Codepage: %u" , collate_codepage);
1481 collate_flags = tvb_get_letohs(tvb, offset);
1482 proto_tree_add_text(tree, tvb, offset, 2, "Flags: 0x%x", collate_flags);
1484 collate_charset_id = tvb_get_guint8(tvb, offset);
1485 proto_tree_add_text(tree, tvb, offset, 1, "Charset ID: %u", collate_charset_id);
1490 proto_tree_add_text(tree, tvb, old_len_offset, 1, "Old Value Length: %u",
1493 string_offset = old_len_offset + 1;
1494 if (is_unicode == TRUE) {
1496 old_val = tvb_get_ephemeral_unicode_string(tvb, string_offset,
1497 old_len, ENC_LITTLE_ENDIAN);
1499 old_val = (gchar*)tvb_get_ephemeral_string(tvb, string_offset, old_len);
1500 proto_tree_add_text(tree, tvb, string_offset, old_len,
1501 "Old Value: %s", old_val);
1506 dissect_tds_err_token(tvbuff_t *tvb, guint offset, guint token_sz _U_, proto_tree *tree)
1509 guint8 srvr_len, proc_len;
1511 gboolean is_unicode = FALSE;
1513 proto_tree_add_text(tree, tvb, offset, 4, "SQL Error Number: %d", tds_tvb_get_xxtohl(tvb, offset, tds_little_endian));
1515 proto_tree_add_text(tree, tvb, offset, 1, "State: %u", tvb_get_guint8(tvb, offset));
1517 proto_tree_add_text(tree, tvb, offset, 1, "Severity Level: %u", tvb_get_guint8(tvb, offset));
1520 msg_len = tds_tvb_get_xxtohs(tvb, offset, tds_little_endian);
1521 proto_tree_add_text(tree, tvb, offset, 1, "Error message length: %u characters", msg_len);
1524 if(tvb_get_guint8(tvb, offset+1) == 0) /* FIXME: It's probably unicode, if the 2nd byte of the message is zero. It's not a good detection method, but it works */
1529 msg = tvb_get_ephemeral_unicode_string(tvb, offset, msg_len, ENC_LITTLE_ENDIAN);
1531 msg = (gchar*)tvb_get_ephemeral_string(tvb, offset, msg_len);
1533 proto_tree_add_text(tree, tvb, offset, msg_len, "Error: %s", format_text((guchar*)msg, strlen(msg)));
1536 srvr_len = tvb_get_guint8(tvb, offset);
1538 proto_tree_add_text(tree, tvb, offset, 1, "Server name length: %u characters", srvr_len);
1543 msg = tvb_get_ephemeral_unicode_string(tvb, offset, srvr_len, ENC_LITTLE_ENDIAN);
1545 msg = (gchar*)tvb_get_ephemeral_string(tvb, offset, srvr_len);
1547 proto_tree_add_text(tree, tvb, offset, srvr_len, "Server name: %s", msg);
1551 proc_len = tvb_get_guint8(tvb, offset);
1553 proto_tree_add_text(tree, tvb, offset, 1, "Process name length: %u characters", proc_len);
1558 msg = tvb_get_ephemeral_unicode_string(tvb, offset, proc_len, ENC_LITTLE_ENDIAN);
1560 msg = (gchar*)tvb_get_ephemeral_string(tvb, offset, proc_len);
1562 proto_tree_add_text(tree, tvb, offset, proc_len, "Process name: %s", msg);
1566 proto_tree_add_text(tree, tvb, offset, 2, "line number: %d", tds_tvb_get_xxtohs(tvb, offset, tds_little_endian));
1570 dissect_tds_login_ack_token(tvbuff_t *tvb, guint offset, guint token_sz, proto_tree *tree)
1574 gboolean is_unicode = FALSE;
1576 proto_tree_add_text(tree, tvb, offset, 1, "Ack: %u", tvb_get_guint8(tvb, offset));
1578 proto_tree_add_text(tree, tvb, offset, 1, "Major version (may be incorrect): %d", tvb_get_guint8(tvb, offset));
1580 proto_tree_add_text(tree, tvb, offset, 1, "Minor version (may be incorrect): %d", tvb_get_guint8(tvb, offset));
1582 proto_tree_add_text(tree, tvb, offset, 2, "zero usually");
1585 msg_len = tvb_get_guint8(tvb, offset);
1586 proto_tree_add_text(tree, tvb, offset, 1, "Text length: %u characters", msg_len);
1589 if(msg_len + 6U + 3U != token_sz - 1) /* 6 is the length of ack(1), version (4), text length (1) fields */
1591 proto_tree_add_text(tree, tvb, offset, 0, "msg_len: %d, token_sz: %d, total: %d",msg_len, token_sz, msg_len + 6U + 3U);
1594 msg = tvb_get_ephemeral_unicode_string(tvb, offset, msg_len, ENC_LITTLE_ENDIAN);
1596 msg = (gchar*)tvb_get_ephemeral_string(tvb, offset, msg_len);
1598 proto_tree_add_text(tree, tvb, offset, msg_len, "Text: %s", format_text((guchar*)msg, strlen(msg)));
1601 proto_tree_add_text(tree, tvb, offset, 4, "Server Version");
1606 dissect_tds7_results_token(tvbuff_t *tvb, guint offset, proto_tree *tree)
1608 guint16 num_columns, table_len;
1609 guint8 type, msg_len;
1612 guint16 collate_codepage, collate_flags;
1613 guint8 collate_charset_id;
1615 num_columns = tvb_get_letohs(tvb, offset);
1616 proto_tree_add_text(tree, tvb, offset, 2, "Columns: %u", tvb_get_letohs(tvb, offset));
1618 for(i=0; i != num_columns; i++) {
1619 proto_tree_add_text(tree, tvb, offset, 0, "Column %d", i + 1);
1620 proto_tree_add_text(tree, tvb, offset, 2, "usertype: %d", tvb_get_letohs(tvb, offset));
1622 proto_tree_add_text(tree, tvb, offset, 2, "flags: %d", tvb_get_letohs(tvb, offset));
1624 type = tvb_get_guint8(tvb, offset);
1625 proto_tree_add_text(tree, tvb, offset, 1, "Type: %d", type);
1627 if(type == 38 || type == 104 || type == 109 || type == 111) { /* ugly, ugly hack. Wish I knew what it really means!*/
1628 proto_tree_add_text(tree, tvb, offset, 1, "unknown 1 byte (%x)", tvb_get_guint8(tvb, offset));
1631 else if (type == 35) {
1632 proto_tree_add_text(tree, tvb, offset, 4, "unknown 4 bytes (%x)", tvb_get_letohl(tvb, offset));
1634 collate_codepage = tvb_get_letohs(tvb, offset);
1635 proto_tree_add_text(tree, tvb, offset, 2, "Codepage: %u" , collate_codepage);
1637 collate_flags = tvb_get_letohs(tvb, offset);
1638 proto_tree_add_text(tree, tvb, offset, 2, "Flags: 0x%x", collate_flags);
1640 collate_charset_id = tvb_get_guint8(tvb, offset);
1641 proto_tree_add_text(tree, tvb, offset, 1, "Charset ID: %u", collate_charset_id);
1643 table_len = tvb_get_letohs(tvb, offset);
1645 if(table_len != 0) {
1647 msg = tvb_get_ephemeral_unicode_string(tvb, offset, table_len, ENC_LITTLE_ENDIAN);
1648 proto_tree_add_text(tree, tvb, offset, table_len, "Table name: %s", msg);
1649 offset += table_len;
1652 else if (type == 106 || type == 108) {
1653 proto_tree_add_text(tree, tvb, offset, 3, "unknown 3 bytes");
1656 else if(type > 128) {
1657 proto_tree_add_text(tree, tvb, offset, 2, "Large type size: 0x%x", tvb_get_letohs(tvb, offset));
1660 collate_codepage = tvb_get_letohs(tvb, offset);
1661 proto_tree_add_text(tree, tvb, offset, 2, "Codepage: %u" , collate_codepage);
1663 collate_flags = tvb_get_letohs(tvb, offset);
1664 proto_tree_add_text(tree, tvb, offset, 2, "Flags: 0x%x", collate_flags);
1666 collate_charset_id = tvb_get_guint8(tvb, offset);
1667 proto_tree_add_text(tree, tvb, offset, 1, "Charset ID: %u", collate_charset_id);
1671 msg_len = tvb_get_guint8(tvb, offset);
1672 proto_tree_add_text(tree, tvb, offset, 1, "message length: %d",msg_len);
1676 msg = tvb_get_ephemeral_unicode_string(tvb, offset, msg_len, ENC_LITTLE_ENDIAN);
1677 proto_tree_add_text(tree, tvb, offset, msg_len, "Text: %s", msg);
1685 dissect_tds_done_token(tvbuff_t *tvb, guint offset, proto_tree *tree)
1687 proto_tree_add_text(tree, tvb, offset, 2, "Status flags");
1689 proto_tree_add_text(tree, tvb, offset, 2, "Operation");
1691 proto_tree_add_text(tree, tvb, offset, 4, "row count: %u", tds_tvb_get_xxtohl(tvb, offset, tds_little_endian));
1696 dissect_tds_type_info(tvbuff_t *tvb, guint *offset, packet_info *pinfo, proto_tree *tree, gboolean *plp)
1698 proto_item *item = NULL, *item1 = NULL, *data_type_item = NULL;
1699 proto_tree *sub_tree = NULL, *collation_tree;
1700 guint32 varlen, varlen_len = 0;
1703 *plp = FALSE; /* most types are not Partially Length-Prefixed */
1704 item = proto_tree_add_item(tree, hf_tds_type_info, tvb, *offset, 0, ENC_NA);
1705 data_type = tvb_get_guint8(tvb, *offset);
1706 proto_item_append_text(item, " (%s)", val_to_str(data_type, tds_data_type_names, "Invalid data type: %02X"));
1707 sub_tree = proto_item_add_subtree(item, ett_tds_type_info);
1708 data_type_item = proto_tree_add_item(sub_tree, hf_tds_type_info_type, tvb, *offset, 1, ENC_LITTLE_ENDIAN);
1711 /* optional TYPE_VARLEN for variable length types */
1714 case TDS_DATA_TYPE_NULL: /* Null (no data associated with this type) */
1715 case TDS_DATA_TYPE_INT1: /* TinyInt (1 byte data representation) */
1716 case TDS_DATA_TYPE_BIT: /* Bit (1 byte data representation) */
1717 case TDS_DATA_TYPE_INT2: /* SmallInt (2 byte data representation) */
1718 case TDS_DATA_TYPE_INT4: /* Int (4 byte data representation) */
1719 case TDS_DATA_TYPE_FLT4: /* Real (4 byte data representation) */
1720 case TDS_DATA_TYPE_DATETIM4: /* SmallDateTime (4 byte data representation) */
1721 case TDS_DATA_TYPE_MONEY4: /* SmallMoney (4 byte data representation) */
1722 case TDS_DATA_TYPE_INT8: /* BigInt (8 byte data representation) */
1723 case TDS_DATA_TYPE_FLT8: /* Float (8 byte data representation) */
1724 case TDS_DATA_TYPE_MONEY: /* Money (8 byte data representation) */
1725 case TDS_DATA_TYPE_DATETIME: /* DateTime (8 byte data representation) */
1726 /* BYTELEN_TYPE with length determined by SCALE */
1727 case TDS_DATA_TYPE_TIMEN: /* (introduced in TDS 7.3) */
1728 case TDS_DATA_TYPE_DATETIME2N: /* (introduced in TDS 7.3) */
1729 case TDS_DATA_TYPE_DATETIMEOFFSETN: /* (introduced in TDS 7.3) */
1733 case TDS_DATA_TYPE_GUID: /* UniqueIdentifier */
1734 case TDS_DATA_TYPE_INTN:
1735 case TDS_DATA_TYPE_DECIMAL: /* Decimal (legacy support) */
1736 case TDS_DATA_TYPE_NUMERIC: /* Numeric (legacy support) */
1737 case TDS_DATA_TYPE_BITN:
1738 case TDS_DATA_TYPE_DECIMALN: /* Decimal */
1739 case TDS_DATA_TYPE_NUMERICN: /* Numeric */
1740 case TDS_DATA_TYPE_FLTN:
1741 case TDS_DATA_TYPE_MONEYN:
1742 case TDS_DATA_TYPE_DATETIMN:
1743 case TDS_DATA_TYPE_DATEN: /* (introduced in TDS 7.3) */
1744 case TDS_DATA_TYPE_CHAR: /* Char (legacy support) */
1745 case TDS_DATA_TYPE_VARCHAR: /* VarChar (legacy support) */
1746 case TDS_DATA_TYPE_BINARY: /* Binary (legacy support) */
1747 case TDS_DATA_TYPE_VARBINARY: /* VarBinary (legacy support) */
1749 varlen = tvb_get_guint8(tvb, *offset);
1751 /* USHORTLEN_TYPE */
1752 case TDS_DATA_TYPE_BIGVARCHR: /* VarChar */
1753 case TDS_DATA_TYPE_BIGVARBIN: /* VarBinary */
1754 case TDS_DATA_TYPE_NVARCHAR: /* NVarChar */
1756 varlen = tvb_get_letohs(tvb, *offset);
1757 /* A type with unlimited max size, known as varchar(max), varbinary(max) and nvarchar(max),
1758 which has a max size of 0xFFFF, defined by PARTLENTYPE. This class of types was introduced in TDS 7.2. */
1759 if(varlen == 0xFFFF)
1762 case TDS_DATA_TYPE_BIGBINARY: /* Binary */
1763 case TDS_DATA_TYPE_BIGCHAR: /* Char */
1764 case TDS_DATA_TYPE_NCHAR: /* NChar */
1766 varlen = tvb_get_letohs(tvb, *offset);
1769 case TDS_DATA_TYPE_XML: /* XML (introduced in TDS 7.2) */
1770 case TDS_DATA_TYPE_UDT: /* CLR-UDT (introduced in TDS 7.2) */
1772 case TDS_DATA_TYPE_TEXT: /* Text */
1773 case TDS_DATA_TYPE_IMAGE: /* Image */
1774 case TDS_DATA_TYPE_NTEXT: /* NText */
1775 case TDS_DATA_TYPE_SSVARIANT: /* Sql_Variant (introduced in TDS 7.2) */
1777 varlen = tvb_get_letohl(tvb, *offset);
1780 expert_add_info_format(pinfo, data_type_item, PI_MALFORMED, PI_ERROR, "Invalid data type");
1781 THROW(ReportedBoundsError); /* No point in continuing */
1785 item1 = proto_tree_add_uint(sub_tree, hf_tds_type_info_varlen, tvb, *offset, varlen_len, varlen);
1787 proto_item_append_text(item1, " (PLP - Partially Length-Prefixed data type)");
1788 *offset += varlen_len;
1790 /* Optional data dependent on type */
1792 /* PRECISION and SCALE */
1793 case TDS_DATA_TYPE_DECIMAL: /* Decimal (legacy support) */
1794 case TDS_DATA_TYPE_NUMERIC: /* Numeric (legacy support) */
1795 case TDS_DATA_TYPE_DECIMALN: /* Decimal */
1796 case TDS_DATA_TYPE_NUMERICN: /* Numeric */
1797 proto_tree_add_item(sub_tree, hf_tds_type_info_precision, tvb, *offset, 1, ENC_LITTLE_ENDIAN);
1800 case TDS_DATA_TYPE_TIMEN: /* (introduced in TDS 7.3) */
1801 case TDS_DATA_TYPE_DATETIME2N: /* (introduced in TDS 7.3) */
1802 case TDS_DATA_TYPE_DATETIMEOFFSETN: /* (introduced in TDS 7.3) */
1803 proto_tree_add_item(sub_tree, hf_tds_type_info_scale, tvb, *offset, 1, ENC_LITTLE_ENDIAN);
1807 case TDS_DATA_TYPE_BIGCHAR: /* Char */
1808 case TDS_DATA_TYPE_BIGVARCHR: /* VarChar */
1809 case TDS_DATA_TYPE_TEXT: /* Text */
1810 case TDS_DATA_TYPE_NTEXT: /* NText */
1811 case TDS_DATA_TYPE_NCHAR: /* NChar */
1812 case TDS_DATA_TYPE_NVARCHAR: /* NVarChar */
1813 item1 = proto_tree_add_item(sub_tree, hf_tds_type_info_collation, tvb, *offset, 5, ENC_NA);
1814 collation_tree = proto_item_add_subtree(item1, ett_tds_type_info_collation);
1815 proto_tree_add_item(collation_tree, hf_tds_type_info_collation_lcid, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
1816 proto_tree_add_item(collation_tree, hf_tds_type_info_collation_ign_case, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
1817 proto_tree_add_item(collation_tree, hf_tds_type_info_collation_ign_accent, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
1818 proto_tree_add_item(collation_tree, hf_tds_type_info_collation_ign_kana, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
1819 proto_tree_add_item(collation_tree, hf_tds_type_info_collation_ign_width, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
1820 proto_tree_add_item(collation_tree, hf_tds_type_info_collation_binary, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
1821 proto_tree_add_item(collation_tree, hf_tds_type_info_collation_version, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
1822 proto_tree_add_item(collation_tree, hf_tds_type_info_collation_sortid, tvb, *offset + 4, 1, ENC_LITTLE_ENDIAN);
1827 proto_item_set_end(item, tvb, *offset);
1832 dissect_tds_type_varbyte(tvbuff_t *tvb, guint *offset, packet_info *pinfo, proto_tree *tree, int hf, guint8 data_type, gboolean plp)
1834 enum { GEN_NULL = 0x00U, CHARBIN_NULL = 0xFFFFU, CHARBIN_NULL32 = 0xFFFFFFFFUL };
1837 proto_tree *sub_tree = NULL;
1838 proto_item *item = NULL, *length_item = NULL;
1840 item = proto_tree_add_item(tree, hf, tvb, *offset, 0, ENC_NA);
1841 sub_tree = proto_item_add_subtree(item, ett_tds_type_varbyte);
1844 enum { PLP_TERMINATOR = 0x00000000UL, UNKNOWN_PLP_LEN = 0xFFFFFFFFFFFFFFFEULL, PLP_NULL = 0xFFFFFFFFFFFFFFFFULL };
1845 guint64 plp_length = tvb_get_letoh64(tvb, *offset);
1846 length_item = proto_tree_add_item(sub_tree, hf_tds_type_varbyte_plp_len, tvb, *offset, 8, ENC_LITTLE_ENDIAN);
1848 if(plp_length == PLP_NULL)
1849 proto_item_append_text(length_item, " (PLP_NULL)");
1851 if(plp_length == UNKNOWN_PLP_LEN)
1852 proto_item_append_text(length_item, " (UNKNOWN_PLP_LEN)");
1854 length = tvb_get_letohl(tvb, *offset);
1855 length_item = proto_tree_add_item(sub_tree, hf_tds_type_varbyte_plp_chunk_len, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
1857 if(length == PLP_TERMINATOR) {
1858 proto_item_append_text(length_item, " (PLP_TERMINATOR)");
1862 case TDS_DATA_TYPE_BIGVARBIN: /* VarBinary */
1863 proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_bytes, tvb, *offset, length, ENC_NA);
1865 case TDS_DATA_TYPE_BIGVARCHR: /* VarChar */
1866 proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_string, tvb, *offset, length, ENC_ASCII|ENC_NA);
1868 case TDS_DATA_TYPE_NVARCHAR: /* NVarChar */
1869 string_value = tvb_get_ephemeral_unicode_string(tvb, *offset, length, ENC_LITTLE_ENDIAN);
1870 proto_tree_add_string(sub_tree, hf_tds_type_varbyte_data_string, tvb, *offset, length, string_value);
1872 case TDS_DATA_TYPE_XML: /* XML (introduced in TDS 7.2) */
1873 case TDS_DATA_TYPE_UDT: /* CLR-UDT (introduced in TDS 7.2) */
1874 expert_add_info_format(pinfo, length_item, PI_UNDECODED, PI_ERROR, "Data type %d not supported yet", data_type);
1875 /* No point in continuing: we need to parse the full data_type to know where it ends */
1876 THROW(ReportedBoundsError);
1878 /* no other data type sets plp = TRUE */
1879 DISSECTOR_ASSERT_NOT_REACHED();
1885 else switch(data_type) {
1887 case TDS_DATA_TYPE_NULL: /* Null (no data associated with this type) */
1889 case TDS_DATA_TYPE_BIT: /* Bit (1 byte data representation) */
1890 proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_boolean, tvb, *offset, 1, ENC_LITTLE_ENDIAN);
1893 case TDS_DATA_TYPE_INT1: /* TinyInt (1 byte data representation) */
1894 proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_int1, tvb, *offset, 1, ENC_LITTLE_ENDIAN);
1897 case TDS_DATA_TYPE_INT2: /* SmallInt (2 byte data representation) */
1898 proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_int2, tvb, *offset, 2, ENC_LITTLE_ENDIAN);
1901 case TDS_DATA_TYPE_INT4: /* Int (4 byte data representation) */
1902 proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_int4, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
1905 case TDS_DATA_TYPE_INT8: /* BigInt (8 byte data representation) */
1906 proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_int8, tvb, *offset, 8, ENC_LITTLE_ENDIAN);
1909 case TDS_DATA_TYPE_FLT4: /* Real (4 byte data representation) */
1910 proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_float, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
1913 case TDS_DATA_TYPE_FLT8: /* Float (8 byte data representation) */
1914 proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_double, tvb, *offset, 8, ENC_LITTLE_ENDIAN);
1917 case TDS_DATA_TYPE_MONEY4: /* SmallMoney (4 byte data representation) */
1918 case TDS_DATA_TYPE_DATETIM4: /* SmallDateTime (4 byte data representation) */
1922 case TDS_DATA_TYPE_MONEY: /* Money (8 byte data representation) */
1923 case TDS_DATA_TYPE_DATETIME: /* DateTime (8 byte data representation) */
1929 /* BYTELEN_TYPE - types prefixed with 1-byte length */
1930 case TDS_DATA_TYPE_GUID: /* UniqueIdentifier */
1931 length = tvb_get_guint8(tvb, *offset);
1932 length_item = proto_tree_add_uint(sub_tree, hf_tds_type_varbyte_length, tvb, *offset, 1, length);
1934 case GEN_NULL: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_null, tvb, *offset, 0, ENC_NA); break;
1935 case 16: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_guid, tvb, *offset + 1, length, ENC_LITTLE_ENDIAN); break;
1936 default: expert_add_info_format(pinfo, length_item, PI_MALFORMED, PI_ERROR, "Invalid length");
1938 *offset += 1 + length;
1940 case TDS_DATA_TYPE_BITN:
1941 length = tvb_get_guint8(tvb, *offset);
1942 length_item = proto_tree_add_uint(sub_tree, hf_tds_type_varbyte_length, tvb, *offset, 1, length);
1944 case GEN_NULL: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_null, tvb, *offset, 0, ENC_NA); break;
1945 case 1: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_boolean, tvb, *offset + 1, 1, ENC_LITTLE_ENDIAN); break;
1946 default: expert_add_info_format(pinfo, length_item, PI_MALFORMED, PI_ERROR, "Invalid length");
1948 *offset += 1 + length;
1950 case TDS_DATA_TYPE_INTN:
1951 length = tvb_get_guint8(tvb, *offset);
1952 length_item = proto_tree_add_uint(sub_tree, hf_tds_type_varbyte_length, tvb, *offset, 1, length);
1954 case GEN_NULL: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_null, tvb, *offset, 0, ENC_NA); break;
1955 case 1: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_int1, tvb, *offset + 1, 1, ENC_LITTLE_ENDIAN); break;
1956 case 2: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_int2, tvb, *offset + 1, 2, ENC_LITTLE_ENDIAN); break;
1957 case 4: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_int4, tvb, *offset + 1, 4, ENC_LITTLE_ENDIAN); break;
1958 case 8: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_int8, tvb, *offset + 1, 8, ENC_LITTLE_ENDIAN); break;
1959 default: expert_add_info_format(pinfo, length_item, PI_MALFORMED, PI_ERROR, "Invalid length");
1961 *offset += 1 + length;
1963 case TDS_DATA_TYPE_FLTN:
1964 length = tvb_get_guint8(tvb, *offset);
1965 length_item = proto_tree_add_uint(sub_tree, hf_tds_type_varbyte_length, tvb, *offset, 1, length);
1967 case GEN_NULL: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_null, tvb, *offset, 0, ENC_NA); break;
1968 case 4: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_float, tvb, *offset + 1, 4, ENC_LITTLE_ENDIAN); break;
1969 case 8: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_double, tvb, *offset + 1, 8, ENC_LITTLE_ENDIAN); break;
1970 default: expert_add_info_format(pinfo, length_item, PI_MALFORMED, PI_ERROR, "Invalid length");
1972 *offset += 1 + length;
1974 case TDS_DATA_TYPE_DECIMAL: /* Decimal (legacy support) */
1975 case TDS_DATA_TYPE_NUMERIC: /* Numeric (legacy support) */
1976 case TDS_DATA_TYPE_DECIMALN: /* Decimal */
1977 case TDS_DATA_TYPE_NUMERICN: /* Numeric */
1978 case TDS_DATA_TYPE_MONEYN:
1979 case TDS_DATA_TYPE_DATETIMN:
1980 case TDS_DATA_TYPE_DATEN: /* (introduced in TDS 7.3) */
1981 case TDS_DATA_TYPE_TIMEN: /* (introduced in TDS 7.3) */
1982 case TDS_DATA_TYPE_DATETIME2N: /* (introduced in TDS 7.3) */
1983 case TDS_DATA_TYPE_DATETIMEOFFSETN: /* (introduced in TDS 7.3) */
1984 case TDS_DATA_TYPE_CHAR: /* Char (legacy support) */
1985 case TDS_DATA_TYPE_VARCHAR: /* VarChar (legacy support) */
1986 case TDS_DATA_TYPE_BINARY: /* Binary (legacy support) */
1987 case TDS_DATA_TYPE_VARBINARY: /* VarBinary (legacy support) */
1988 length = tvb_get_guint8(tvb, *offset);
1989 proto_tree_add_uint(sub_tree, hf_tds_type_varbyte_length, tvb, *offset, 1, length);
1992 proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_bytes, tvb, *offset, length, ENC_NA);
1997 /* USHORTLEN_TYPE - types prefixed with 2-byte length */
1998 case TDS_DATA_TYPE_BIGVARBIN: /* VarBinary */
1999 case TDS_DATA_TYPE_BIGBINARY: /* Binary */
2000 case TDS_DATA_TYPE_BIGVARCHR: /* VarChar */
2001 case TDS_DATA_TYPE_BIGCHAR: /* Char */
2002 case TDS_DATA_TYPE_NVARCHAR: /* NVarChar */
2003 case TDS_DATA_TYPE_NCHAR: /* NChar */
2004 length = tvb_get_letohs(tvb, *offset);
2005 length_item = proto_tree_add_uint(sub_tree, hf_tds_type_varbyte_length, tvb, *offset, 2, length);
2007 if(length == CHARBIN_NULL) {
2008 proto_item_append_text(length_item, " (CHARBIN_NULL)");
2009 proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_null, tvb, *offset, 0, ENC_NA);
2013 case TDS_DATA_TYPE_BIGVARBIN: /* VarBinary */
2014 case TDS_DATA_TYPE_BIGBINARY: /* Binary */
2015 proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_bytes, tvb, *offset, length, ENC_NA);
2017 case TDS_DATA_TYPE_BIGVARCHR: /* VarChar */
2018 case TDS_DATA_TYPE_BIGCHAR: /* Char */
2019 proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_string, tvb, *offset, length, ENC_ASCII|ENC_NA);
2021 case TDS_DATA_TYPE_NVARCHAR: /* NVarChar */
2022 case TDS_DATA_TYPE_NCHAR: /* NChar */
2023 string_value = tvb_get_ephemeral_unicode_string(tvb, *offset, length, ENC_LITTLE_ENDIAN);
2024 proto_tree_add_string(sub_tree, hf_tds_type_varbyte_data_string, tvb, *offset, length, string_value);
2027 DISSECTOR_ASSERT_NOT_REACHED();
2033 /* LONGLEN_TYPE - types prefixed with 2-byte length */
2034 case TDS_DATA_TYPE_NTEXT: /* NText */
2035 case TDS_DATA_TYPE_XML: /* XML (introduced in TDS 7.2) */
2036 case TDS_DATA_TYPE_UDT: /* CLR-UDT (introduced in TDS 7.2) */
2037 case TDS_DATA_TYPE_TEXT: /* Text */
2038 case TDS_DATA_TYPE_IMAGE: /* Image */
2039 case TDS_DATA_TYPE_SSVARIANT: /* Sql_Variant (introduced in TDS 7.2) */
2040 length = tvb_get_letohl(tvb, *offset);
2041 length_item = proto_tree_add_uint(sub_tree, hf_tds_type_varbyte_length, tvb, *offset, 4, length);
2043 if(length == CHARBIN_NULL32) {
2044 proto_item_append_text(length_item, " (CHARBIN_NULL)");
2045 proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_null, tvb, *offset, 0, ENC_NA);
2049 case TDS_DATA_TYPE_NTEXT: /* NText */
2050 string_value = tvb_get_ephemeral_unicode_string(tvb, *offset, length, ENC_LITTLE_ENDIAN);
2051 proto_tree_add_string(sub_tree, hf_tds_type_varbyte_data_string, tvb, *offset, length, string_value);
2054 expert_add_info_format(pinfo, length_item, PI_UNDECODED, PI_ERROR, "Data type %d not supported yet", data_type);
2055 /* No point in continuing: we need to parse the full data_type to know where it ends */
2056 THROW(ReportedBoundsError);
2062 proto_item_set_end(item, tvb, *offset);
2066 dissect_tds_rpc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2068 proto_item *item = NULL, *param_item = NULL;
2069 proto_tree *sub_tree = NULL, *status_sub_tree = NULL;
2075 item = proto_tree_add_item(tree, hf_tds_rpc, tvb, 0, -1, ENC_NA);
2076 tree = proto_item_add_subtree(item, ett_tds_message);
2078 dissect_tds_all_headers(tvb, &offset, pinfo, tree);
2079 while(tvb_length_remaining(tvb, offset) > 0) {
2083 switch(tds_protocol_type) {
2084 case TDS_PROTOCOL_4:
2085 len = tvb_get_guint8(tvb, offset);
2086 proto_tree_add_item(tree, hf_tds_rpc_name_length8, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2087 proto_tree_add_item(tree, hf_tds_rpc_name, tvb, offset + 1, len, ENC_ASCII|ENC_NA);
2091 case TDS_PROTOCOL_7_0:
2092 case TDS_PROTOCOL_7_1:
2093 case TDS_PROTOCOL_7_2:
2094 case TDS_PROTOCOL_7_3:
2095 default: /* unspecified: try as if TDS7 */
2096 len = tvb_get_letohs(tvb, offset);
2097 proto_tree_add_item(tree, hf_tds_rpc_name_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2099 if (len == 0xFFFF) {
2100 proto_tree_add_item(tree, hf_tds_rpc_proc_id, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2103 else if (len != 0) {
2105 val = tvb_get_ephemeral_unicode_string(tvb, offset, len, ENC_LITTLE_ENDIAN);
2106 proto_tree_add_string(tree, hf_tds_rpc_name, tvb, offset, len, val);
2111 item = proto_tree_add_item(tree, hf_tds_rpc_options, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2112 sub_tree = proto_item_add_subtree(item, ett_tds_rpc_options);
2113 proto_tree_add_item(sub_tree, hf_tds_rpc_options_with_recomp, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2114 proto_tree_add_item(sub_tree, hf_tds_rpc_options_no_metadata, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2115 proto_tree_add_item(sub_tree, hf_tds_rpc_options_reuse_metadata, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2118 /* dissect parameters */
2119 while(tvb_length_remaining(tvb, offset) > 0) {
2122 len = tvb_get_guint8(tvb, offset);
2123 /* check for BatchFlag or NoExecFlag */
2124 if((gint8)len < 0) {
2125 proto_tree_add_item(tree, hf_tds_rpc_separator, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2129 param_item = proto_tree_add_item(tree, hf_tds_rpc_parameter, tvb, offset, 0, ENC_NA);
2130 sub_tree = proto_item_add_subtree(param_item, ett_tds_rpc_parameter);
2131 proto_tree_add_item(sub_tree, hf_tds_rpc_parameter_name_length, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2135 val = tvb_get_ephemeral_unicode_string(tvb, offset, len, ENC_LITTLE_ENDIAN);
2136 proto_tree_add_string(sub_tree, hf_tds_rpc_parameter_name, tvb, offset, len, val);
2139 item = proto_tree_add_item(sub_tree, hf_tds_rpc_parameter_status, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2140 status_sub_tree = proto_item_add_subtree(item, ett_tds_rpc_parameter_status);
2141 proto_tree_add_item(status_sub_tree, hf_tds_rpc_parameter_status_by_ref, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2142 proto_tree_add_item(status_sub_tree, hf_tds_rpc_parameter_status_default, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2144 data_type = dissect_tds_type_info(tvb, &offset, pinfo, sub_tree, &plp);
2145 dissect_tds_type_varbyte(tvb, &offset, pinfo, sub_tree, hf_tds_rpc_parameter_value, data_type, plp);
2146 proto_item_set_end(param_item, tvb, offset);
2152 dissect_tds_resp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2155 proto_item *token_item;
2156 proto_tree *token_tree;
2157 guint pos, token_sz = 0;
2158 guint token_len_field_size = 2;
2159 guint token_len_field_val = 0;
2161 struct _netlib_data nl_data;
2162 gint length_remaining;
2164 memset(&nl_data, '\0', sizeof nl_data);
2167 * Until we reach the end of the packet, read tokens.
2170 while (tvb_reported_length_remaining(tvb, pos) > 0) {
2172 token = tvb_get_guint8(tvb, pos);
2174 /* TODO Handle TDS_PARAMFMT, TDS_PARAMS [similar to TDS_RESULTS, TDS_ROW] */
2175 if (tds_token_is_fixed_size(token)) {
2176 token_sz = tds_get_fixed_token_size(token) + 1;
2177 } else if (token == TDS_ROW_TOKEN) {
2179 * Rows are special; they have no size field and
2180 * aren't fixed length.
2182 token_sz = tds_get_row_size(tvb, &nl_data, pos + 1);
2184 token_sz = tds_get_variable_token_size(tvb, pos + 1,
2185 token, &token_len_field_size, &token_len_field_val);
2187 length_remaining = tvb_ensure_length_remaining(tvb, pos);
2189 if ((int) token_sz < 0) {
2190 proto_tree_add_text(tree, tvb, pos, 0, "Bogus token size: %u",
2194 if ((int) token_len_field_size < 0) {
2195 proto_tree_add_text(tree, tvb, pos, 0, "Bogus token length field size: %u",
2196 token_len_field_size);
2199 token_item = proto_tree_add_text(tree, tvb, pos, token_sz,
2200 "Token 0x%02x %s", token,
2201 val_to_str(token, token_names, "Unknown Token Type"));
2202 token_tree = proto_item_add_subtree(token_item, ett_tds_token);
2205 * If it's a variable token, put the length field in here
2206 * instead of replicating this for each token subdissector.
2208 if (!tds_token_is_fixed_size(token) && token != TDS_ROW_TOKEN) {
2209 proto_tree_add_text(token_tree, tvb, pos + 1,
2210 token_len_field_size, "Length: %u",
2211 token_len_field_val);
2214 if (token_sz > (guint)length_remaining)
2215 token_sz = (guint)length_remaining;
2219 case TDS_COL_NAME_TOKEN:
2222 * TODO dissect token to get "column names" to fill in _netlib_data
2226 case TDS_COL_INFO_TOKEN:
2228 * TDS 4.2: get the column info
2230 dissect_tds_col_info_token(tvb, &nl_data, pos);
2233 case TDS_RESULT_TOKEN:
2235 * If it's a result token, we need to stash the
2238 read_results_tds5(tvb, &nl_data, pos + 3, token_sz - 3);
2241 case TDS_ENV_CHG_TOKEN:
2242 dissect_tds_env_chg(tvb, pos + 3, token_sz - 3, token_tree);
2245 case TDS_AUTH_TOKEN:
2246 dissect_tds_nt(tvb, pinfo, token_tree, pos + 3, token_sz - 3);
2250 dissect_tds_err_token(tvb, pos + 3, token_sz - 3, token_tree);
2253 case TDS_DONE_TOKEN:
2254 case TDS_DONEPROC_TOKEN:
2255 case TDS_DONEINPROC_TOKEN:
2256 dissect_tds_done_token(tvb, pos + 1, token_tree);
2258 case TDS_LOGIN_ACK_TOKEN:
2259 dissect_tds_login_ack_token(tvb, pos + 3, token_sz - 3, token_tree);
2261 case TDS7_RESULT_TOKEN:
2262 pos = (dissect_tds7_results_token(tvb, pos + 1, token_tree)-1);
2266 /* and step to the end of the token, rinse, lather, repeat */
2272 dissect_netlib_buffer(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2275 proto_item *tds_item = NULL;
2276 proto_tree *tds_tree = NULL;
2277 proto_tree *tds_status_tree = NULL;
2281 guint8 packet_number;
2282 gboolean save_fragmented;
2284 fragment_data *fd_head;
2287 type = tvb_get_guint8(tvb, offset);
2288 status = tvb_get_guint8(tvb, offset + 1);
2289 channel = tvb_get_ntohs(tvb, offset + 4);
2290 packet_number = tvb_get_guint8(tvb, offset + 6);
2292 /* create display subtree for the protocol */
2293 tds_item = proto_tree_add_item(tree, proto_tds, tvb, offset, -1, ENC_NA);
2294 tds_tree = proto_item_add_subtree(tds_item, ett_tds);
2295 proto_tree_add_item(tds_tree, hf_tds_type, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2296 tds_item = proto_tree_add_item(tds_tree, hf_tds_status, tvb, offset + 1, 1, ENC_LITTLE_ENDIAN);
2297 tds_status_tree = proto_item_add_subtree(tds_item, ett_tds_status);
2298 proto_tree_add_item(tds_status_tree, hf_tds_status_eom, tvb, offset + 1, 1, ENC_BIG_ENDIAN);
2299 proto_tree_add_item(tds_status_tree, hf_tds_status_ignore, tvb, offset + 1, 1, ENC_BIG_ENDIAN);
2300 proto_tree_add_item(tds_status_tree, hf_tds_status_event_notif, tvb, offset + 1, 1, ENC_BIG_ENDIAN);
2301 proto_tree_add_item(tds_status_tree, hf_tds_status_reset_conn, tvb, offset + 1, 1, ENC_BIG_ENDIAN);
2302 proto_tree_add_item(tds_status_tree, hf_tds_status_reset_conn_skip_tran,tvb, offset + 1, 1, ENC_BIG_ENDIAN);
2303 proto_tree_add_item(tds_tree, hf_tds_length, tvb, offset + 2, 2, ENC_BIG_ENDIAN);
2304 proto_tree_add_item(tds_tree, hf_tds_channel, tvb, offset + 4, 2, ENC_BIG_ENDIAN);
2305 proto_tree_add_item(tds_tree, hf_tds_packet_number, tvb, offset + 6, 1, ENC_LITTLE_ENDIAN);
2306 proto_tree_add_item(tds_tree, hf_tds_window, tvb, offset + 7, 1, ENC_LITTLE_ENDIAN);
2307 offset += 8; /* skip Netlib header */
2310 * Deal with fragmentation.
2312 * TODO: handle case where netlib headers 'packet-number'.is always 0
2313 * use fragment_add_seq_next in this case ?
2316 save_fragmented = pinfo->fragmented;
2317 if (tds_defragment &&
2318 (packet_number > 1 || (status & STATUS_LAST_BUFFER) == 0)) {
2319 if ((status & STATUS_LAST_BUFFER) == 0) {
2320 col_append_str(pinfo->cinfo, COL_INFO,
2321 " (Not last buffer)");
2323 len = tvb_reported_length_remaining(tvb, offset);
2325 * XXX - I've seen captures that start with a login
2326 * packet with a sequence number of 2.
2328 fd_head = fragment_add_seq_check(tvb, offset, pinfo, channel,
2329 tds_fragment_table, tds_reassembled_table,
2330 packet_number - 1, len, (status & STATUS_LAST_BUFFER) == 0);
2331 next_tvb = process_reassembled_data(tvb, offset, pinfo,
2332 "Reassembled TDS", fd_head, &tds_frag_items, NULL,
2336 * If this isn't the last buffer, just show it as a fragment.
2337 * (XXX - it'd be nice to dissect it if it's the first
2338 * buffer, but we'd need to do reassembly in order to
2341 * If this is the last buffer, dissect it.
2342 * (XXX - it'd be nice to show it as a fragment if it's part
2343 * of a fragmented message, but we'd need to do reassembly
2344 * in order to discover that.)
2346 if ((status & STATUS_LAST_BUFFER) == 0)
2349 next_tvb = tvb_new_subset_remaining(tvb, offset);
2353 if (next_tvb != NULL) {
2358 dissect_tds_rpc(next_tvb, pinfo, tds_tree);
2362 dissect_tds_resp(next_tvb, pinfo, tds_tree);
2365 case TDS_LOGIN7_PKT:
2366 dissect_tds7_login(next_tvb, pinfo, tds_tree);
2369 dissect_tds_query_packet(next_tvb, pinfo, tds_tree);
2371 case TDS_QUERY5_PKT:
2372 dissect_tds_query5_packet(next_tvb, pinfo, tds_tree);
2375 dissect_tds_nt(next_tvb, pinfo, tds_tree, offset - 8, -1);
2378 proto_tree_add_text(tds_tree, next_tvb, 0, -1,
2383 next_tvb = tvb_new_subset_remaining (tvb, offset);
2384 call_dissector(data_handle, next_tvb, pinfo, tds_tree);
2386 pinfo->fragmented = save_fragmented;
2390 dissect_tds_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2392 volatile gboolean first_time = TRUE;
2393 volatile int offset = 0;
2394 guint length_remaining;
2398 tvbuff_t *volatile next_tvb;
2399 proto_item *tds_item = NULL;
2400 proto_tree *tds_tree = NULL;
2403 while (tvb_reported_length_remaining(tvb, offset) != 0) {
2404 length_remaining = tvb_ensure_length_remaining(tvb, offset);
2407 * Can we do reassembly?
2409 if (tds_desegment && pinfo->can_desegment) {
2411 * Yes - is the fixed-length part of the PDU
2412 * split across segment boundaries?
2414 if (length_remaining < 8) {
2416 * Yes. Tell the TCP dissector where the data for this message
2417 * starts in the data it handed us and that we need "some more
2418 * data." Don't tell it exactly how many bytes we need because
2419 * if/when we ask for even more (after the header) that will
2422 pinfo->desegment_offset = offset;
2423 pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
2428 type = tvb_get_guint8(tvb, offset);
2431 * Get the length of the PDU.
2433 plen = tvb_get_ntohs(tvb, offset + 2);
2436 * The length is less than the header length.
2437 * Put in the type, status, and length, and
2438 * report the length as bogus.
2441 /* create display subtree for the protocol */
2442 tds_item = proto_tree_add_item(tree, proto_tds,
2443 tvb, offset, -1, ENC_NA);
2445 tds_tree = proto_item_add_subtree(tds_item,
2447 proto_tree_add_uint(tds_tree, hf_tds_type, tvb,
2449 proto_tree_add_item(tds_tree, hf_tds_status,
2450 tvb, offset + 1, 1, ENC_BIG_ENDIAN);
2451 proto_tree_add_uint_format(tds_tree,
2452 hf_tds_length, tvb, offset + 2, 2, plen,
2453 "Size: %u (bogus, should be >= 8)", plen);
2457 * Give up - we can't dissect any more of this
2464 * Can we do reassembly?
2466 if (tds_desegment && pinfo->can_desegment) {
2468 * Yes - is the PDU split across segment boundaries?
2470 if (length_remaining < plen) {
2472 * Yes. Tell the TCP dissector where the
2473 * data for this message starts in the data
2474 * it handed us, and how many more bytes we
2477 pinfo->desegment_offset = offset;
2478 pinfo->desegment_len = plen - length_remaining;
2484 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TDS");
2487 * Set the packet description based on its TDS packet
2490 col_add_str(pinfo->cinfo, COL_INFO,
2491 val_to_str(type, packet_type_names,
2492 "Unknown Packet Type: %u"));
2497 * Construct a tvbuff containing the amount of the payload
2498 * we have available. Make its reported length the amount
2499 * of data in the PDU.
2501 * XXX - if reassembly isn't enabled. the subdissector will
2502 * throw a BoundsError exception, rather than a
2503 * ReportedBoundsError exception. We really want a tvbuff
2504 * where the length is "length", the reported length is
2505 * "plen", and the "if the snapshot length were infinite"
2506 * length is the minimum of the reported length of the tvbuff
2507 * handed to us and "plen", with a new type of exception
2508 * thrown if the offset is within the reported length but
2509 * beyond that third length, with that exception getting the
2510 * "Unreassembled Packet" error.
2512 length = length_remaining;
2515 next_tvb = tvb_new_subset(tvb, offset, length, plen);
2518 * Dissect the Netlib buffer.
2520 * Catch the ReportedBoundsError exception; if this
2521 * particular Netlib buffer happens to get a
2522 * ReportedBoundsError exception, that doesn't mean
2523 * that we should stop dissecting PDUs within this frame
2524 * or chunk of reassembled data.
2526 * If it gets a BoundsError, we can stop, as there's nothing
2527 * more to see, so we just re-throw it.
2529 pd_save = pinfo->private_data;
2531 dissect_netlib_buffer(next_tvb, pinfo, tree);
2533 CATCH(BoundsError) {
2536 CATCH(ReportedBoundsError) {
2537 /* Restore the private_data structure in case one of the
2538 * called dissectors modified it (and, due to the exception,
2539 * was unable to restore it).
2541 pinfo->private_data = pd_save;
2543 show_reported_bounds_error(tvb, pinfo, tree);
2548 * Step to the next Netlib buffer.
2555 dissect_tds_tcp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2561 conversation_t *conv;
2564 * If we don't have even enough data for a Netlib header,
2565 * just say it's not TDS.
2567 if (tvb_length(tvb) < 8)
2571 * Quickly scan all the data we have in order to see if
2572 * everything in it looks like Netlib traffic.
2574 while (tvb_bytes_exist(tvb, offset, 1)) {
2576 * Check the type field.
2578 type = tvb_get_guint8(tvb, offset);
2579 if (!is_valid_tds_type(type))
2583 * Check the status field, if it's present.
2585 if (!tvb_bytes_exist(tvb, offset + 1, 1))
2587 status = tvb_get_guint8(tvb, offset + 1);
2588 if (!is_valid_tds_status(status))
2592 * Get the length of the PDU.
2594 if (!tvb_bytes_exist(tvb, offset + 2, 2))
2596 plen = tvb_get_ntohs(tvb, offset + 2);
2599 * The length is less than the header length.
2606 * If we're at the beginning of the segment, check the
2607 * payload if it's a login packet.
2610 if (!netlib_check_login_pkt(tvb, offset, pinfo, type))
2615 * Step to the next Netlib buffer.
2621 * OK, it passes the test; assume the rest of this conversation
2624 conv = find_or_create_conversation(pinfo);
2625 conversation_set_dissector(conv, tds_tcp_handle);
2628 * Now dissect it as TDS.
2630 dissect_tds_tcp(tvb, pinfo, tree);
2638 * Initialize the fragment and reassembly tables.
2640 fragment_table_init(&tds_fragment_table);
2641 reassembled_table_init(&tds_reassembled_table);
2645 /* Register the protocol with Wireshark */
2647 /* this format is required because a script is used to build the C function
2648 that calls all the protocol registration.
2652 proto_register_tds(void)
2654 static hf_register_info hf[] = {
2656 { "Type", "tds.type",
2657 FT_UINT8, BASE_DEC, VALS(packet_type_names), 0x0,
2658 "Packet type", HFILL }
2661 { "Status", "tds.status",
2662 FT_UINT8, BASE_HEX, NULL, 0x0,
2663 "Packet status", HFILL }
2665 { &hf_tds_status_eom,
2666 { "End of message", "tds.status.eom",
2667 FT_BOOLEAN, 8, NULL, STATUS_LAST_BUFFER,
2668 "The packet is the last packet in the whole request", HFILL }
2670 { &hf_tds_status_ignore,
2671 { "Ignore this event", "tds.status.ignore",
2672 FT_BOOLEAN, 8, NULL, STATUS_IGNORE_EVENT,
2673 "(From client to server) Ignore this event (EOM MUST also be set)", HFILL }
2675 { &hf_tds_status_event_notif,
2676 { "Event notification", "tds.status.event_notif",
2677 FT_BOOLEAN, 8, NULL, STATUS_EVENT_NOTIFICATION,
2680 { &hf_tds_status_reset_conn,
2681 { "Reset connection", "tds.status.reset_conn",
2682 FT_BOOLEAN, 8, NULL, STATUS_RESETCONNECTION,
2683 "(From client to server) Reset this connection before processing event", HFILL }
2685 { &hf_tds_status_reset_conn_skip_tran,
2686 { "Reset connection keeping transaction state", "tds.status.reset_conn_skip_tran",
2687 FT_BOOLEAN, 8, NULL, STATUS_RESETCONNECTIONSKIPTRAN,
2688 "(From client to server) Reset the connection before processing event but do not modify the transaction state", HFILL }
2691 { "Length", "tds.length",
2692 FT_UINT16, BASE_DEC, NULL, 0x0,
2693 "Packet length", HFILL }
2696 { "Channel", "tds.channel",
2697 FT_UINT16, BASE_DEC, NULL, 0x0,
2698 "Channel Number", HFILL }
2700 { &hf_tds_packet_number,
2701 { "Packet Number", "tds.packet_number",
2702 FT_UINT8, BASE_DEC, NULL, 0x0,
2706 { "Window", "tds.window",
2707 FT_UINT8, BASE_DEC, NULL, 0x0,
2710 { &hf_tds_fragment_overlap,
2711 { "Segment overlap", "tds.fragment.overlap",
2712 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2713 "Fragment overlaps with other fragments", HFILL }
2715 { &hf_tds_fragment_overlap_conflict,
2716 { "Conflicting data in fragment overlap", "tds.fragment.overlap.conflict",
2717 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2718 "Overlapping fragments contained conflicting data", HFILL }
2720 { &hf_tds_fragment_multiple_tails,
2721 { "Multiple tail fragments found", "tds.fragment.multipletails",
2722 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2723 "Several tails were found when defragmenting the packet", HFILL }
2725 { &hf_tds_fragment_too_long_fragment,
2726 { "Segment too long", "tds.fragment.toolongfragment",
2727 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2728 "Segment contained data past end of packet", HFILL }
2730 { &hf_tds_fragment_error,
2731 { "Defragmentation error", "tds.fragment.error",
2732 FT_FRAMENUM, BASE_NONE, NULL, 0x0,
2733 "Defragmentation error due to illegal fragments", HFILL }
2735 { &hf_tds_fragment_count,
2736 { "Segment count", "tds.fragment.count",
2737 FT_UINT32, BASE_DEC, NULL, 0x0,
2741 { "TDS Fragment", "tds.fragment",
2742 FT_FRAMENUM, BASE_NONE, NULL, 0x0,
2745 { &hf_tds_fragments,
2746 { "TDS Fragments", "tds.fragments",
2747 FT_NONE, BASE_NONE, NULL, 0x0,
2750 { &hf_tds_reassembled_in,
2751 { "Reassembled TDS in frame", "tds.reassembled_in",
2752 FT_FRAMENUM, BASE_NONE, NULL, 0x0,
2753 "This TDS packet is reassembled in this frame", HFILL }
2755 { &hf_tds_reassembled_length,
2756 { "Reassembled TDS length", "tds.reassembled.length",
2757 FT_UINT32, BASE_DEC, NULL, 0x0,
2758 "The total length of the reassembled payload", HFILL }
2760 { &hf_tds7_login_total_size,
2761 { "Total Packet Length", "tds7login.total_len",
2762 FT_UINT32, BASE_DEC, NULL, 0x0,
2763 "TDS7 Login Packet total packet length", HFILL }
2766 { "TDS version", "tds7login.version",
2767 FT_UINT32, BASE_HEX, NULL, 0x0,
2770 { &hf_tds7_packet_size,
2771 { "Packet Size", "tds7login.packet_size",
2772 FT_UINT32, BASE_DEC, NULL, 0x0,
2775 { &hf_tds7_client_version,
2776 { "Client version", "tds7login.client_version",
2777 FT_UINT32, BASE_DEC, NULL, 0x0,
2780 { &hf_tds7_client_pid,
2781 { "Client PID", "tds7login.client_pid",
2782 FT_UINT32, BASE_DEC, NULL, 0x0,
2785 { &hf_tds7_connection_id,
2786 { "Connection ID", "tds7login.connection_id",
2787 FT_UINT32, BASE_DEC, NULL, 0x0,
2790 { &hf_tds7_option_flags1,
2791 { "Option Flags 1", "tds7login.option_flags1",
2792 FT_UINT8, BASE_HEX, NULL, 0x0,
2795 { &hf_tds7_option_flags2,
2796 { "Option Flags 2", "tds7login.option_flags2",
2797 FT_UINT8, BASE_HEX, NULL, 0x0,
2800 { &hf_tds7_sql_type_flags,
2801 { "SQL Type Flags", "tds7login.sql_type_flags",
2802 FT_UINT8, BASE_HEX, NULL, 0x0,
2805 { &hf_tds7_reserved_flags,
2806 { "Reserved Flags", "tds7login.reserved_flags",
2807 FT_UINT8, BASE_HEX, NULL, 0x0,
2810 { &hf_tds7_time_zone,
2811 { "Time Zone", "tds7login.time_zone",
2812 FT_UINT32, BASE_HEX, NULL, 0x0,
2815 { &hf_tds7_collation,
2816 { "Collation", "tds7login.collation",
2817 FT_UINT32, BASE_HEX, NULL, 0x0,
2820 { &hf_tds_all_headers,
2821 { "Packet data stream headers", "tds.all_headers",
2822 FT_NONE, BASE_NONE, NULL, 0x0,
2823 "The ALL_HEADERS rule", HFILL }
2825 { &hf_tds_all_headers_total_length,
2826 { "Total length", "tds.all_headers.total_length",
2827 FT_UINT32, BASE_DEC, NULL, 0x0,
2828 "Total length of ALL_HEADERS stream", HFILL }
2830 { &hf_tds_all_headers_header_length,
2831 { "Length", "tds.all_headers.header.length",
2832 FT_UINT32, BASE_DEC, NULL, 0x0,
2833 "Total length of an individual header", HFILL }
2835 { &hf_tds_all_headers_header_type,
2836 { "Type", "tds.all_headers.header.type",
2837 FT_UINT16, BASE_HEX, VALS(header_type_names), 0x0,
2840 { &hf_tds_all_headers_trans_descr,
2841 { "Transaction descriptor", "tds.all_headers.header.trans_descr",
2842 FT_UINT64, BASE_DEC, NULL, 0x0,
2843 "For each connection, a number that uniquely identifies the transaction the request is associated with. Initially generated by the server when a new transaction is created and returned to the client as part of the ENVCHANGE token stream.", HFILL }
2845 { &hf_tds_all_headers_request_cnt,
2846 { "Outstanding request count", "tds.all_headers.header.request_cnt",
2847 FT_UINT32, BASE_DEC, NULL, 0x0,
2848 "Number of requests currently active on the connection", HFILL }
2850 { &hf_tds_type_info,
2851 { "Type info", "tds.type_info",
2852 FT_NONE, BASE_NONE, NULL, 0x0,
2853 "The TYPE_INFO rule applies to several messages used to describe column information", HFILL }
2855 { &hf_tds_type_info_type,
2856 { "Type", "tds.type_info.type",
2857 FT_UINT8, BASE_HEX, VALS(tds_data_type_names), 0x0,
2860 { &hf_tds_type_info_varlen,
2861 { "Maximal length", "tds.type_info.varlen",
2862 FT_UINT32, BASE_DEC, NULL, 0x0,
2863 "Defines the length of the data contained within the column", HFILL }
2865 { &hf_tds_type_info_precision,
2866 { "Precision", "tds.type_info.precision",
2867 FT_UINT8, BASE_DEC, NULL, 0x0,
2870 { &hf_tds_type_info_scale,
2871 { "Scale", "tds.type_info.scale",
2872 FT_UINT8, BASE_DEC, NULL, 0x0,
2875 { &hf_tds_type_info_collation,
2876 { "Collation", "tds.type_info.collation",
2877 FT_NONE, BASE_NONE, NULL, 0x0,
2878 "Specifies collation information for character data or metadata describing character data", HFILL }
2880 { &hf_tds_type_info_collation_lcid,
2881 { "LCID", "tds.type_info.collation.lcid",
2882 FT_UINT32, BASE_HEX, NULL, 0x000FFFFF,
2883 "For a SortId==0 collation, the LCID bits correspond to a LocaleId as defined by the National Language Support (NLS) functions", HFILL }
2885 { &hf_tds_type_info_collation_ign_case,
2886 { "Ignore case", "tds.type_info.collation.ignore_case",
2887 FT_BOOLEAN, 32, NULL, 0x00100000,
2890 { &hf_tds_type_info_collation_ign_accent,
2891 { "Ignore accent", "tds.type_info.collation.ignore_accent",
2892 FT_BOOLEAN, 32, NULL, 0x00200000,
2895 { &hf_tds_type_info_collation_ign_kana,
2896 { "Ignore kana", "tds.type_info.collation.ignore_kana",
2897 FT_BOOLEAN, 32, NULL, 0x00400000,
2900 { &hf_tds_type_info_collation_ign_width,
2901 { "Ignore width", "tds.type_info.collation.ignore_width",
2902 FT_BOOLEAN, 32, NULL, 0x00800000,
2905 { &hf_tds_type_info_collation_binary,
2906 { "Binary", "tds.type_info.collation.binary",
2907 FT_BOOLEAN, 32, NULL, 0x01000000,
2910 { &hf_tds_type_info_collation_version,
2911 { "Version", "tds.type_info.collation.version",
2912 FT_UINT32, BASE_DEC, NULL, 0xF0000000,
2915 { &hf_tds_type_info_collation_sortid,
2916 { "SortId", "tds.type_info.collation.sortid",
2917 FT_UINT8, BASE_DEC, NULL, 0x0,
2920 { &hf_tds_type_varbyte_length,
2921 { "Length", "tds.type_varbyte.length",
2922 FT_UINT32, BASE_DEC, NULL, 0x0,
2925 { &hf_tds_type_varbyte_data_null,
2926 { "Data: NULL", "tds.type_varbyte.data",
2927 FT_NONE, BASE_NONE, NULL, 0x0,
2930 { &hf_tds_type_varbyte_data_boolean,
2931 { "Data", "tds.type_varbyte.data",
2932 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2935 { &hf_tds_type_varbyte_data_int1,
2936 { "Data", "tds.type_varbyte.data",
2937 FT_INT8, BASE_DEC, NULL, 0x0,
2940 { &hf_tds_type_varbyte_data_int2,
2941 { "Data", "tds.type_varbyte.data",
2942 FT_INT16, BASE_DEC, NULL, 0x0,
2945 { &hf_tds_type_varbyte_data_int4,
2946 { "Data", "tds.type_varbyte.data",
2947 FT_INT32, BASE_DEC, NULL, 0x0,
2950 { &hf_tds_type_varbyte_data_int8,
2951 { "Data", "tds.type_varbyte.data",
2952 FT_INT64, BASE_DEC, NULL, 0x0,
2955 { &hf_tds_type_varbyte_data_float,
2956 { "Data", "tds.type_varbyte.data",
2957 FT_FLOAT, BASE_NONE, NULL, 0x0,
2960 { &hf_tds_type_varbyte_data_double,
2961 { "Data", "tds.type_varbyte.data",
2962 FT_DOUBLE, BASE_NONE, NULL, 0x0,
2965 { &hf_tds_type_varbyte_data_bytes,
2966 { "Data", "tds.type_varbyte.data",
2967 FT_BYTES, BASE_NONE, NULL, 0x0,
2970 { &hf_tds_type_varbyte_data_guid,
2971 { "Data", "tds.type_varbyte.data",
2972 FT_GUID, BASE_NONE, NULL, 0x0,
2975 { &hf_tds_type_varbyte_data_string,
2976 { "Data", "tds.type_varbyte.data",
2977 FT_STRING, BASE_NONE, NULL, 0x0,
2980 { &hf_tds_type_varbyte_plp_len,
2981 { "PLP length", "tds.type_varbyte.plp_len",
2982 FT_INT64, BASE_DEC, NULL, 0x0,
2985 { &hf_tds_type_varbyte_plp_chunk_len,
2986 { "PLP chunk length", "tds.type_varbyte.plp_chunk_len",
2987 FT_INT32, BASE_DEC, NULL, 0x0,
2991 { "Remote Procedure Call", "tds.rpc",
2992 FT_NONE, BASE_NONE, NULL, 0x0,
2995 { &hf_tds_rpc_name_length8,
2996 { "Procedure name length", "tds.rpc.name_length",
2997 FT_UINT8, BASE_DEC, NULL, 0x0,
3000 { &hf_tds_rpc_name_length,
3001 { "Procedure name length", "tds.rpc.name_length",
3002 FT_UINT16, BASE_DEC, NULL, 0x0,
3006 { "Procedure name", "tds.rpc.name",
3007 FT_STRING, BASE_NONE, NULL, 0x0,
3010 { &hf_tds_rpc_proc_id,
3011 { "Stored procedure ID", "tds.rpc.proc_id",
3012 FT_UINT16, BASE_DEC, VALS(internal_stored_proc_id_names), 0x0,
3013 "The number identifying the special stored procedure to be executed", HFILL }
3015 { &hf_tds_rpc_options,
3016 { "Option flags", "tds.rpc.options",
3017 FT_UINT16, BASE_HEX, NULL, 0x0,
3018 "The number identifying the special stored procedure to be executed", HFILL }
3020 { &hf_tds_rpc_options_with_recomp,
3021 { "With recompile", "tds.rpc.options.with_recomp",
3022 FT_BOOLEAN, 16, NULL, TDS_RPC_OPT_WITH_RECOMP,
3023 "The number identifying the special stored procedure to be executed", HFILL }
3025 { &hf_tds_rpc_options_no_metadata,
3026 { "No metadata", "tds.rpc.options.no_metadata",
3027 FT_BOOLEAN, 16, NULL, TDS_RPC_OPT_NO_METADATA,
3028 "The number identifying the special stored procedure to be executed", HFILL }
3030 { &hf_tds_rpc_options_reuse_metadata,
3031 { "Reuse metadata", "tds.rpc.options.reuse_metadata",
3032 FT_BOOLEAN, 16, NULL, TDS_RPC_OPT_REUSE_METADATA,
3033 "The number identifying the special stored procedure to be executed", HFILL }
3035 { &hf_tds_rpc_separator,
3036 { "RPC batch separator", "tds.rpc.separator",
3037 FT_UINT8, BASE_DEC, VALS(tds_rpc_separators), 0x0,
3040 { &hf_tds_rpc_parameter,
3041 { "Parameter", "tds.rpc.parameter",
3042 FT_NONE, BASE_NONE, NULL, 0x0,
3045 { &hf_tds_rpc_parameter_name_length,
3046 { "Name length", "tds.rpc.parameter.name_length",
3047 FT_UINT8, BASE_DEC, NULL, 0x0,
3050 { &hf_tds_rpc_parameter_name,
3051 { "Name", "tds.rpc.parameter.name",
3052 FT_STRING, BASE_NONE, NULL, 0x0,
3055 { &hf_tds_rpc_parameter_status,
3056 { "Status flags", "tds.rpc.parameter.status",
3057 FT_UINT8, BASE_HEX, NULL, 0x0,
3058 "Information on how the parameter is passed", HFILL }
3060 { &hf_tds_rpc_parameter_status_by_ref,
3061 { "By reference", "tds.rpc.parameter.status.by_ref",
3062 FT_BOOLEAN, 16, NULL, TDS_RPC_PARAMETER_STATUS_BY_REF,
3065 { &hf_tds_rpc_parameter_status_default,
3066 { "Default value", "tds.rpc.parameter.status.default",
3067 FT_BOOLEAN, 16, NULL, TDS_RPC_PARAMETER_STATUS_DEFAULT,
3070 { &hf_tds_rpc_parameter_value,
3071 { "Value", "tds.rpc.parameter.value",
3072 FT_NONE, BASE_NONE, NULL, 0x0,
3077 static gint *ett[] = {
3082 &ett_tds_all_headers,
3083 &ett_tds_all_headers_header,
3085 &ett_tds_type_info_collation,
3086 &ett_tds_type_varbyte,
3088 &ett_tds_rpc_options,
3089 &ett_tds_rpc_parameter,
3090 &ett_tds_rpc_parameter_status,
3096 module_t *tds_module;
3098 /* Register the protocol name and description */
3099 proto_tds = proto_register_protocol("Tabular Data Stream",
3102 /* Required function calls to register the header fields and subtrees used */
3103 proto_register_field_array(proto_tds, hf, array_length(hf));
3104 proto_register_subtree_array(ett, array_length(ett));
3106 /* Allow dissector to be found by name. */
3107 register_dissector("tds", dissect_tds_tcp, proto_tds);
3109 tds_module = prefs_register_protocol(proto_tds, NULL);
3110 prefs_register_bool_preference(tds_module, "desegment_buffers",
3111 "Reassemble TDS buffers spanning multiple TCP segments",
3112 "Whether the TDS dissector should reassemble TDS buffers spanning multiple TCP segments. "
3113 "To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
3115 prefs_register_bool_preference(tds_module, "defragment",
3116 "Reassemble fragmented TDS messages with multiple buffers",
3117 "Whether the TDS dissector should defragment messages spanning multiple Netlib buffers",
3119 prefs_register_enum_preference(tds_module, "protocol_type",
3120 "TDS Protocol Type",
3121 "Hint as to version of TDS protocol being decoded",
3122 &tds_protocol_type, tds_protocol_type_options, FALSE);
3123 prefs_register_enum_preference(tds_module, "endian_type",
3125 "Hint as to whether to decode TDS protocol as little-endian or big-endian. (TDS7/8 always decoded as little-endian)",
3126 &tds_little_endian, tds_endian_type_options, FALSE);
3127 prefs_register_range_preference(tds_module, "tcp_ports",
3129 "Additional TCP ports to decode as TDS",
3130 &tds_tcp_ports, 0xFFFF);
3132 register_init_routine(tds_init);
3135 /* If this dissector uses sub-dissector registration add a registration routine.
3136 This format is required because a script is used to find these routines and
3137 create the code that calls these routines.
3140 proto_reg_handoff_tds(void)
3142 tds_tcp_handle = create_dissector_handle(dissect_tds_tcp, proto_tds);
3144 /* Initial TDS ports: MS SQL default ports */
3145 dissector_add_uint("tcp.port", 1433, tds_tcp_handle);
3146 dissector_add_uint("tcp.port", 2433, tds_tcp_handle);
3148 heur_dissector_add("tcp", dissect_tds_tcp_heur, proto_tds);
3150 ntlmssp_handle = find_dissector("ntlmssp");
3151 gssapi_handle = find_dissector("gssapi");
3152 data_handle = find_dissector("data");