Get rid of some unused variables, as per Josef Korelus.
[obnox/wireshark/wip.git] / packet-tds.c
1 /* packet-tds.c
2  * Routines for TDS NetLib dissection
3  * Copyright 2000-2002, Brian Bruns <camber@ais.org>
4  * Copyright 2002, Steve Langasek <vorlon@netexpress.net>
5  *
6  * $Id: packet-tds.c,v 1.27 2004/02/20 08:40:30 guy Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  *
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.
16  *
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.
21  *
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.
25  */
26
27 /*
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:
31  *
32  *      a one-byte packet type field;
33  *
34  *      a one-byte status field;
35  *
36  *      a two-byte big-endian size field giving the size of the packet,
37  *      including the header;
38  *
39  *      a two-byte big-endian channel number, used when multiple sessions
40  *      are being multiplexed on a single connection;
41  *
42  *      a one-byte packet number, giving "the frame number of a multiplexed
43  *      message, modulo 256";
44  *
45  *      a one-byte window, which is the number of frames to be sent
46  *      before an acknowledgment message is received.
47  *
48  * followed by payload whose size is the value in the size field minus
49  * 8.
50  *
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).
53  *
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).
60  *
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
69  * with 100% accuracy.
70  *
71  * Some preliminary documentation on the packet format can be found at
72  * http://www.freetds.org/tds.html
73  *
74  * Some more information can be found in
75  * http://download.nai.com/products/media/sniffer/support/sdos/sybase.pdf
76  *
77  * Much of this code was originally developed for the FreeTDS project.
78  * http://www.freetds.org
79  */
80
81 /*
82  * Excerpts from Brian's posting to ethereal-dev:
83  *
84  * The TDS Protocol is actually a protocol within a protocol.  On the outside
85  * there is netlib which is not so much a encapsulation as a blocking of the
86  * data, typically to 512 or 4096 bytes.  Between this are the protocol data
87  * units for TDS.  Netlib packets may be split over real packets, multiple
88  * netlib packets may appear in single real packets.  TDS PDUs may be split
89  * over netlib packets (and real packets) and most certainly can appear
90  * multiple times within a netlib packet.
91  *
92  * Because of this, I abandoned my earlier attempt at making two dissectors,
93  * one for netlib and one for TDS. Counterintuitively, a single dissector
94  * turned out to be simpler than splitting it up.
95  *
96  * Here are some of the (hefty) limitations of the current code
97  *
98  * . We currently do not handle netlib headers that cross packet boundaries.
99  *   This should be an easy fix.
100  * . I probably could have used the packet reassembly stuff, but I started
101  *   this at version 0.8.20, so c'est la vie. It wouldn't have covered the
102  *   netlib stuff anyway, so no big loss.
103  * . The older two layer version of the code dissected the PDU's, but the new
104  *   version does not yet, it only labels the names. I need an elegant way to
105  *   deal with dissecting data crossing (netlib and tcp) packet boundries.  I
106  *   think I have one, but ran out of time to do it.
107  * . It will only work on little endian platforms.  Or rather I should say,
108  *   the client that was captured must be little endian.  TDS 7.0/8.0 is
109  *   always LE; for TDS 4.2/5.0 look in the code for tvb_get_le*() functions,
110  *   there are fields in the login packet which determine byte order.
111  * . result sets that span netlib packets are not working
112  * . TDS 7 and 4.2 result sets are not working yet
113  *
114  * All that said, the code does deal gracefully with different boudary
115  * conditions and what remains are the easier bits, IMHO.
116  *
117  * XXX - "real packets" means "TCP segments", for TCP.
118  *
119  * XXX - is it *REALLY* true that you can have more than one TDS PDU (as
120  * opposed to more than one server response item) per NETLIB packet?  Or is
121  * all the data in a NETLIB packet put into a single TDS PDU?  If so, then
122  * we can reassemble NETLIB packets using the standard TCP desegmentation
123  * code, and can reassemble TDS PDUs using "fragment_add_seq_check()",
124  * and more cleanly separate the NETLIB and TDS dissectors (although the
125  * "is this NETLIB" heuristic would have to look at TDS information past
126  * the NETLIB header, in order to make the heuristic strong enough not
127  * to get too many false positives; note that the heuristic should reject
128  * any putative NETLIB packet with a length field with a value < 8).
129  *
130  * That would substantially clean the dissector up, eliminating most of
131  * the per-packet data (we might still need information to handle
132  * TDS_ROW_TOKEN), getting rid of the stuff to handle data split across
133  * TCP segment boundaries in favor of simple reassembly code, and
134  * fixing some otherwise nasty-looking crashing bugs.
135  *
136  * NOTE: we assume that all the data in a NETLIB packet *can* be put into
137  * a single TDS PTU, so that we have separate reassembly of NETLIB
138  * packets and TDS PDUs; it seems to work, and it really did clean stuff
139  * up and fix crashes.
140  */
141
142 #ifdef HAVE_CONFIG_H
143 # include "config.h"
144 #endif
145
146 #include <stdio.h>
147 #include <stdlib.h>
148 #include <string.h>
149 #include <ctype.h>
150
151 #include <glib.h>
152
153 #include <epan/packet.h>
154 #include <epan/conversation.h>
155 #include <epan/strutil.h>
156
157 #include "packet-smb-common.h"
158 #include "packet-frame.h"
159 #include "reassemble.h"
160 #include "prefs.h"
161
162 #define TDS_QUERY_PKT        1
163 #define TDS_LOGIN_PKT        2
164 #define TDS_RPC_PKT          3
165 #define TDS_RESP_PKT         4
166 #define TDS_RAW_PKT          5
167 #define TDS_CANCEL_PKT       6
168 #define TDS_BULK_DATA_PKT    7
169 #define TDS_OPEN_CHN_PKT     8
170 #define TDS_CLOSE_CHN_PKT    9
171 #define TDS_RES_ERROR_PKT   10
172 #define TDS_LOG_CHN_ACK_PKT 11
173 #define TDS_ECHO_PKT        12
174 #define TDS_LOGOUT_CHN_PKT  13
175 #define TDS_QUERY5_PKT      15  /* or "Normal tokenized request or response */
176 #define TDS_LOGIN7_PKT      16  /* or "Urgent tokenized request or response */
177 #define TDS_XXX7_PKT        18  /* seen in one capture */
178
179 #define is_valid_tds_type(x) ((x) >= TDS_QUERY_PKT && (x) <= TDS_XXX7_PKT)
180
181 /* The following constants are imported more or less directly from FreeTDS */
182
183 #define TDS5_DYN_TOKEN      231  /* 0xE7    TDS 5.0 only              */
184 #define TDS5_DYNRES_TOKEN   236  /* 0xEC    TDS 5.0 only              */
185 #define TDS5_DYN3_TOKEN     215  /* 0xD7    TDS 5.0 only              */
186 #define TDS_LANG_TOKEN       33  /* 0x21    TDS 5.0 only              */
187 #define TDS_CLOSE_TOKEN     113  /* 0x71    TDS 5.0 only? ct_close()  */
188 #define TDS_RET_STAT_TOKEN  121  /* 0x79                              */
189 #define TDS_124_TOKEN       124  /* 0x7C    TDS 4.2 only - TDS_PROCID */
190 #define TDS7_RESULT_TOKEN   129  /* 0x81    TDS 7.0 only              */
191 #define TDS_COL_NAME_TOKEN  160  /* 0xA0    TDS 4.2 only              */
192 #define TDS_COL_INFO_TOKEN  161  /* 0xA1    TDS 4.2 only - TDS_COLFMT */
193 /*#define  TDS_TABNAME   164 */
194 /*#define  TDS_COL_INFO   165 */
195 #define TDS_167_TOKEN       167  /* 0xA7                              */
196 #define TDS_168_TOKEN       168  /* 0xA8                              */
197 #define TDS_ORDER_BY_TOKEN  169  /* 0xA9    TDS_ORDER                 */
198 #define TDS_ERR_TOKEN       170  /* 0xAA                              */
199 #define TDS_MSG_TOKEN       171  /* 0xAB                              */
200 #define TDS_PARAM_TOKEN     172  /* 0xAC    RETURNVALUE?              */
201 #define TDS_LOGIN_ACK_TOKEN 173  /* 0xAD                              */
202 #define TDS_174_TOKEN       174  /* 0xAE    TDS_CONTROL               */
203 #define TDS_ROW_TOKEN       209  /* 0xD1                              */
204 #define TDS_CMP_ROW_TOKEN   211  /* 0xD3                              */
205 #define TDS_CAP_TOKEN       226  /* 0xE2                              */
206 #define TDS_ENV_CHG_TOKEN   227  /* 0xE3                              */
207 #define TDS_EED_TOKEN       229  /* 0xE5                              */
208 #define TDS_AUTH_TOKEN      237  /* 0xED                              */
209 #define TDS_RESULT_TOKEN    238  /* 0xEE                              */
210 #define TDS_DONE_TOKEN      253  /* 0xFD    TDS_DONE                  */
211 #define TDS_DONEPROC_TOKEN  254  /* 0xFE    TDS_DONEPROC              */
212 #define TDS_DONEINPROC_TOKEN 255  /* 0xFF    TDS_DONEINPROC            */
213
214 #define SYBCHAR      47   /* 0x2F */
215 #define SYBVARCHAR   39   /* 0x27 */
216 #define SYBINTN      38   /* 0x26 */
217 #define SYBINT1      48   /* 0x30 */
218 #define SYBINT2      52   /* 0x34 */
219 #define SYBINT4      56   /* 0x38 */
220 #define SYBINT8     127   /* 0x7F */
221 #define SYBFLT8      62   /* 0x3E */
222 #define SYBDATETIME  61   /* 0x3D */
223 #define SYBBIT       50   /* 0x32 */
224 #define SYBTEXT      35   /* 0x23 */
225 #define SYBNTEXT     99   /* 0x63 */
226 #define SYBIMAGE     34   /* 0x22 */
227 #define SYBMONEY4    122  /* 0x7A */
228 #define SYBMONEY     60   /* 0x3C */
229 #define SYBDATETIME4 58   /* 0x3A */
230 #define SYBREAL      59   /* 0x3B */
231 #define SYBBINARY    45   /* 0x2D */
232 #define SYBVOID      31   /* 0x1F */
233 #define SYBVARBINARY 37   /* 0x25 */
234 #define SYBNVARCHAR  103  /* 0x67 */
235 #define SYBBITN      104  /* 0x68 */
236 #define SYBNUMERIC   108  /* 0x6C */
237 #define SYBDECIMAL   106  /* 0x6A */
238 #define SYBFLTN      109  /* 0x6D */
239 #define SYBMONEYN    110  /* 0x6E */
240 #define SYBDATETIMN  111  /* 0x6F */
241 #define XSYBCHAR     167  /* 0xA7 */
242 #define XSYBVARCHAR  175  /* 0xAF */
243 #define XSYBNVARCHAR 231  /* 0xE7 */
244 #define XSYBNCHAR    239  /* 0xEF */
245 #define SYBUNIQUE    0x24
246 #define SYBVARIANT   0x62
247
248 #define is_fixed_coltype(x) (x==SYBINT1    || \
249                         x==SYBINT2      || \
250                         x==SYBINT4      || \
251                         x==SYBINT8      || \
252                         x==SYBREAL       || \
253                         x==SYBFLT8      || \
254                         x==SYBDATETIME  || \
255                         x==SYBDATETIME4 || \
256                         x==SYBBIT       || \
257                         x==SYBMONEY     || \
258                         x==SYBMONEY4    || \
259                         x==SYBUNIQUE)
260
261 /* Initialize the protocol and registered fields */
262 static int proto_tds = -1;
263 static int hf_tds_type = -1;
264 static int hf_tds_status = -1;
265 static int hf_tds_size = -1;
266 static int hf_tds_channel = -1;
267 static int hf_tds_packet_number = -1;
268 static int hf_tds_window = -1;
269 static int hf_tds_reassembled_in = -1;
270 static int hf_tds_fragments = -1;
271 static int hf_tds_fragment = -1;
272 static int hf_tds_fragment_overlap = -1;
273 static int hf_tds_fragment_overlap_conflict = -1;
274 static int hf_tds_fragment_multiple_tails = -1;
275 static int hf_tds_fragment_too_long_fragment = -1;
276 static int hf_tds_fragment_error = -1;
277
278 static int hf_tds7_login_total_size = -1;
279 static int hf_tds7_version = -1;
280 static int hf_tds7_packet_size = -1;
281 static int hf_tds7_client_version = -1;
282 static int hf_tds7_client_pid = -1;
283 static int hf_tds7_connection_id = -1;
284 static int hf_tds7_option_flags1 = -1;
285 static int hf_tds7_option_flags2 = -1;
286 static int hf_tds7_sql_type_flags = -1;
287 static int hf_tds7_reserved_flags = -1;
288 static int hf_tds7_time_zone = -1;
289 static int hf_tds7_collation = -1;
290 static int hf_tds7_message = -1;
291
292 /* Initialize the subtree pointers */
293 static gint ett_tds = -1;
294 static gint ett_tds_fragments = -1;
295 static gint ett_tds_fragment = -1;
296 static gint ett_tds_token = -1;
297 static gint ett_tds7_login = -1;
298 static gint ett_tds7_query = 0;
299 static gint ett_tds7_hdr = -1;
300
301 /* Desegmentation of Netlib buffers crossing TCP segment boundaries. */
302 static gboolean tds_desegment = TRUE;
303
304 static const fragment_items tds_frag_items = {
305         &ett_tds_fragment,
306         &ett_tds_fragments,
307         &hf_tds_fragments,
308         &hf_tds_fragment,
309         &hf_tds_fragment_overlap,
310         &hf_tds_fragment_overlap_conflict,
311         &hf_tds_fragment_multiple_tails,
312         &hf_tds_fragment_too_long_fragment,
313         &hf_tds_fragment_error,
314         &hf_tds_reassembled_in,
315         "fragments"
316 };
317
318 /* Tables for reassembly of fragments. */
319 static GHashTable *tds_fragment_table = NULL;
320 static GHashTable *tds_reassembled_table = NULL;
321
322 /* defragmentation of multi-buffer TDS PDUs */
323 static gboolean tds_defragment = TRUE;
324
325 static dissector_handle_t tds_tcp_handle;
326 static dissector_handle_t ntlmssp_handle;
327 static dissector_handle_t data_handle;
328
329 /* These correspond to the netlib packet type field */
330 static const value_string packet_type_names[] = {
331         {TDS_QUERY_PKT,  "Query Packet"},
332         {TDS_LOGIN_PKT,  "Login Packet"},
333         {TDS_RPC_PKT,    "Remote Procedure Call Packet"},
334         {TDS_RESP_PKT,   "Response Packet"},
335         {TDS_CANCEL_PKT, "Cancel Packet"},
336         {TDS_QUERY5_PKT, "TDS5 Query Packet"},
337         {TDS_LOGIN7_PKT, "TDS7/8 Login Packet"},
338         {0, NULL},
339 };
340
341 /* The status field */
342
343 #define is_valid_tds_status(x) ((x) <= STATUS_EVENT_NOTIFICATION)
344
345 #define STATUS_NOT_LAST_BUFFER          0x00
346 #define STATUS_LAST_BUFFER              0x01
347 #define STATUS_ATTN_REQUEST_ACK         0x02
348 #define STATUS_ATTN_REQUEST             0x03
349 #define STATUS_EVENT_NOTIFICATION       0x04
350
351 static const value_string status_names[] = {
352         {STATUS_NOT_LAST_BUFFER,    "Not last buffer"},
353         {STATUS_LAST_BUFFER,        "Last buffer in request or response"},
354         {STATUS_ATTN_REQUEST_ACK,   "Acknowledgment of last attention request"},
355         {STATUS_ATTN_REQUEST,       "Attention request"},
356         {STATUS_EVENT_NOTIFICATION, "Event notification"},
357         {0, NULL},
358 };
359
360 /* The one byte token at the start of each TDS PDU */
361 static const value_string token_names[] = {
362         {TDS5_DYN_TOKEN, "Dynamic SQL"},
363         {TDS5_DYNRES_TOKEN, "Dynamic Results"},
364         {TDS5_DYN3_TOKEN, "Dynamic (Unknown)"},
365         {TDS_LANG_TOKEN, "Language"},
366         {TDS_CLOSE_TOKEN, "Close Connection"},
367         {TDS_RET_STAT_TOKEN, "Return Status"},
368         {TDS_124_TOKEN, "Proc ID"},
369         {TDS7_RESULT_TOKEN, "TDS7+ Results"},
370         {TDS_COL_NAME_TOKEN, "Column Names"},
371         {TDS_COL_INFO_TOKEN, "Column Info"},
372         {TDS_167_TOKEN, "Unknown (167)"},
373         {TDS_168_TOKEN, "Unknown (168)"},
374         {TDS_ORDER_BY_TOKEN, "Order By"},
375         {TDS_ERR_TOKEN, "Error Message"},
376         {TDS_MSG_TOKEN, "Info Message"},
377         {TDS_PARAM_TOKEN, "Paramater"},
378         {TDS_LOGIN_ACK_TOKEN, "Login Acknowledgement"},
379         {TDS_174_TOKEN, "Unknown (174)"},
380         {TDS_ROW_TOKEN, "Row"},
381         {TDS_CMP_ROW_TOKEN, "Compute Row"},
382         {TDS_CAP_TOKEN, "Capabilities"},
383         {TDS_ENV_CHG_TOKEN, "Environment Change"},
384         {TDS_EED_TOKEN, "Extended Error"},
385         {TDS_AUTH_TOKEN, "Authentication"},
386         {TDS_RESULT_TOKEN, "Results"},
387         {TDS_DONE_TOKEN, "Done"},
388         {TDS_DONEPROC_TOKEN, "Done Proc"},
389         {TDS_DONEINPROC_TOKEN, "Done In Proc"},
390         {0, NULL},
391 };
392
393 static const value_string env_chg_names[] = {
394         {1, "Database"},
395         {2, "Language"},
396         {3, "Sort Order"},
397         {4, "Blocksize"},
398         {5, "Unicode Locale ID"},
399         {6, "Unicode Comparison Style"},
400         {7, "Collation Info"},
401         {0, NULL},
402 };
403
404 static const value_string login_field_names[] = {
405         {0, "Client Name"},
406         {1, "Username"},
407         {2, "Password"},
408         {3, "App Name"},
409         {4, "Server Name"},
410         {5, "Unknown1"},
411         {6, "Library Name"},
412         {7, "Locale"},
413         {8, "Unknown2"},
414         {0, NULL},
415 };
416
417
418 #define MAX_COLUMNS 256
419
420 /*
421  * This is where we store the column information to be used in decoding the
422  * TDS_ROW_TOKEN tokens.
423  */
424 struct _tds_col {
425      gchar name[256];
426      guint16 utype;
427      guint8 ctype;
428      guint csize;
429 };
430
431 struct _netlib_data {
432         guint num_cols;
433         struct _tds_col *columns[MAX_COLUMNS];
434 };
435
436 struct tds7_login_packet_hdr {
437         guint32 total_packet_size;
438         guint32 tds_version;
439         guint32 packet_size;
440         guint32 client_version;
441         guint32 client_pid;
442         guint32 connection_id;
443         guint8  option_flags1;
444         guint8  option_flags2;
445         guint8  sql_type_flags;
446         guint8  reserved_flags;
447         guint32 time_zone;
448         guint32 collation;
449 };
450
451 /* all the standard memory management stuff */
452 #define tds_column_length (sizeof(struct _tds_col))
453 #define tds_column_init_count 10
454
455 static GMemChunk *tds_column = NULL;
456
457 /* support routines */
458 static void
459 dissect_tds_ntlmssp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
460     guint offset, guint length)
461 {
462         tvbuff_t *ntlmssp_tvb;
463
464         ntlmssp_tvb = tvb_new_subset(tvb, offset, length, length);
465         call_dissector(ntlmssp_handle, ntlmssp_tvb, pinfo, tree);
466 }
467
468 static void
469 dissect_tds_query_packet(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
470 {
471         guint offset, len;
472         gboolean is_unicode = TRUE;
473         char *msg;
474
475         proto_item *query_hdr;
476         proto_tree *query_tree;
477         
478         offset = 0;
479         query_hdr = proto_tree_add_text(tree, tvb, offset, -1, "TDS Query Packet");
480         query_tree = proto_item_add_subtree(query_hdr, ett_tds7_query);
481         len = tvb_reported_length_remaining(tvb, offset);
482         if((len < 2) || tvb_get_guint8(tvb, offset+1) !=0)
483                 is_unicode = FALSE;
484         
485         if (is_unicode) {
486                 msg = tvb_fake_unicode(tvb, offset, len/2, TRUE);
487                 proto_tree_add_text(query_tree, tvb, offset, len, "Query: %s", msg);
488                 g_free(msg);
489                 offset += len;
490         }
491 }
492
493 static void
494 dissect_tds7_login(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
495 {
496         guint offset, i, offset2, len;
497         gboolean is_unicode = TRUE;
498         char *val;
499
500         proto_item *login_hdr;
501         proto_tree *login_tree;
502         proto_item *header_hdr;
503         proto_tree *header_tree;
504         proto_item *length_hdr;
505         proto_tree *length_tree;
506         
507         struct tds7_login_packet_hdr td7hdr;
508         gint length_remaining;
509
510
511         /* create display subtree for the protocol */
512         offset = 0;
513         login_hdr = proto_tree_add_text(tree, tvb, offset, -1, "TDS7 Login Packet");
514         login_tree = proto_item_add_subtree(login_hdr, ett_tds7_login);
515         header_hdr = proto_tree_add_text(login_tree, tvb, offset, 36, "Login Packet Header");
516         header_tree = proto_item_add_subtree(header_hdr, ett_tds7_hdr);
517         
518         td7hdr.total_packet_size = tvb_get_letohl(tvb, offset);
519         proto_tree_add_uint(header_tree, hf_tds7_login_total_size, tvb, offset, sizeof(td7hdr.total_packet_size), td7hdr.total_packet_size);
520         offset += sizeof(td7hdr.total_packet_size);
521         
522         td7hdr.tds_version = tvb_get_ntohl(tvb, offset);
523         proto_tree_add_uint(header_tree, hf_tds7_version, tvb, offset, sizeof(td7hdr.tds_version), td7hdr.tds_version);
524         offset += sizeof(td7hdr.tds_version);
525         
526         td7hdr.packet_size = tvb_get_ntohl(tvb, offset);
527         proto_tree_add_uint(header_tree, hf_tds7_packet_size, tvb, offset, sizeof(td7hdr.packet_size), td7hdr.packet_size);
528         offset += sizeof(td7hdr.packet_size);
529         
530         td7hdr.client_version = tvb_get_ntohl(tvb, offset);
531         proto_tree_add_uint(header_tree, hf_tds7_client_version, tvb, offset, sizeof(td7hdr.client_version), td7hdr.client_version);
532         offset += sizeof(td7hdr.client_version);
533         
534         td7hdr.client_pid = tvb_get_letohl(tvb, offset);
535         proto_tree_add_uint(header_tree, hf_tds7_client_pid, tvb, offset, sizeof(td7hdr.client_pid), td7hdr.client_pid);
536         offset += sizeof(td7hdr.client_pid);
537
538         td7hdr.connection_id= tvb_get_letohl(tvb, offset);
539         proto_tree_add_uint(header_tree, hf_tds7_connection_id, tvb, offset, sizeof(td7hdr.connection_id), td7hdr.connection_id);
540         offset += sizeof(td7hdr.connection_id);
541         
542         td7hdr.option_flags1 = tvb_get_guint8(tvb, offset);
543         proto_tree_add_uint(header_tree, hf_tds7_option_flags1, tvb, offset, sizeof(td7hdr.option_flags1), td7hdr.option_flags1);
544         offset += sizeof(td7hdr.option_flags1);
545         
546         td7hdr.option_flags2 = tvb_get_guint8(tvb, offset);
547         proto_tree_add_uint(header_tree, hf_tds7_option_flags2, tvb, offset, sizeof(td7hdr.option_flags2), td7hdr.option_flags2);
548         offset += sizeof(td7hdr.option_flags2);
549
550         td7hdr.sql_type_flags = tvb_get_guint8(tvb, offset);    
551         proto_tree_add_uint(header_tree, hf_tds7_sql_type_flags, tvb, offset, sizeof(td7hdr.sql_type_flags), td7hdr.sql_type_flags);
552         offset += sizeof(td7hdr.sql_type_flags);
553
554         td7hdr.reserved_flags = tvb_get_guint8(tvb, offset);
555         proto_tree_add_uint(header_tree, hf_tds7_reserved_flags, tvb, offset, sizeof(td7hdr.reserved_flags), td7hdr.reserved_flags);
556         offset += sizeof(td7hdr.reserved_flags);
557         
558         td7hdr.time_zone = tvb_get_ntohl(tvb, offset);
559         proto_tree_add_uint(header_tree, hf_tds7_time_zone, tvb, offset, sizeof(td7hdr.time_zone), td7hdr.time_zone);
560         offset += sizeof(td7hdr.time_zone);
561
562         td7hdr.collation = tvb_get_ntohl(tvb, offset);
563         proto_tree_add_uint(header_tree, hf_tds7_collation, tvb, offset, sizeof(td7hdr.collation), td7hdr.collation);
564         offset += sizeof(td7hdr.collation);
565
566         length_hdr = proto_tree_add_text(login_tree, tvb, offset, 50, "Lengths and offsets");
567         length_tree = proto_item_add_subtree(length_hdr, ett_tds7_hdr);
568
569         for (i = 0; i < 9; i++) {
570                 offset2 = tvb_get_letohs(tvb, offset + i*4);
571                 len = tvb_get_letohs(tvb, offset + i*4 + 2);
572                 proto_tree_add_text(length_tree, tvb, offset + i*4, 2,
573                     "%s offset: %u",
574                     val_to_str(i, login_field_names, "Unknown"),
575                     offset2);
576                 proto_tree_add_text(length_tree, tvb, offset + i*4 + 2, 2,
577                         "%s length: %u",
578                         val_to_str(i, login_field_names, "Unknown"),
579                         len);
580                 if (len != 0) {
581                         if( i != 2) {
582                                 if (is_unicode == TRUE) {
583                                         val = tvb_fake_unicode(tvb, offset2, len, TRUE);
584                                         len *= 2;
585                                 } else
586                                         val = tvb_get_string(tvb, offset2, len);
587                                 proto_tree_add_text(login_tree, tvb, offset2, len, "%s: %s", val_to_str(i, login_field_names, "Unknown"), val);
588                                 g_free(val);
589                         }
590                         else {
591                                 if (is_unicode)
592                                         len *= 2;
593                                 proto_tree_add_text(login_tree, tvb, offset2, len, "%s", val_to_str(i, login_field_names, "Unknown"));
594                         }
595                 }
596         }
597
598         length_remaining = tvb_reported_length_remaining(tvb, offset2 + len);
599         if (length_remaining > 0) {
600                 dissect_tds_ntlmssp(tvb, pinfo, login_tree, offset2 + len,
601                     length_remaining);
602         }
603 }
604
605 static int get_size_by_coltype(int servertype)
606 {
607    switch(servertype)
608    {
609       case SYBINT1:        return 1;  break;
610       case SYBINT2:        return 2;  break;
611       case SYBINT4:        return 4;  break;
612       case SYBINT8:        return 8;  break;
613       case SYBREAL:        return 4;  break;
614       case SYBFLT8:        return 8;  break;
615       case SYBDATETIME:    return 8;  break;
616       case SYBDATETIME4:   return 4;  break;
617       case SYBBIT:         return 1;  break;
618       case SYBBITN:        return 1;  break;
619       case SYBMONEY:       return 8;  break;
620       case SYBMONEY4:      return 4;  break;
621       case SYBUNIQUE:      return 16; break;
622       default:             return -1; break;
623    }
624 }
625 static int tds_is_fixed_token(int token)
626 {
627      switch (token) {
628           case TDS_DONE_TOKEN:
629           case TDS_DONEPROC_TOKEN:
630           case TDS_DONEINPROC_TOKEN:
631           case TDS_RET_STAT_TOKEN:
632           case TDS7_RESULT_TOKEN:
633                return 1;
634           default:
635                return 0;
636      }
637 }
638 static int tds_get_token_size(int token)
639 {
640      switch(token) {
641           case TDS_DONE_TOKEN:
642           case TDS_DONEPROC_TOKEN:
643           case TDS_DONEINPROC_TOKEN:
644                return 8;
645           case TDS_RET_STAT_TOKEN:
646                return 4;
647           case TDS_124_TOKEN:
648                return 8;
649           default:
650                return 0;
651      }
652 }
653
654 # if 0
655 /*
656  * data_to_string should take column data and turn it into something we can
657  * display on the tree.
658  */
659 static char *data_to_string(void *data, guint col_type, guint col_size)
660 {
661    static char  result[256];
662    guint i;
663
664    switch(col_type) {
665       case SYBVARCHAR:
666          /* strncpy(result, (char *)data, col_size); */
667          for (i=0;i<col_size && i<(256-1);i++)
668                 if (!isprint(((char *)data)[i])) result[i]='.';
669                 else result[i]=((char *)data)[i];
670          result[i] = '\0';
671          break;
672       case SYBINT2:
673          sprintf(result, "%d", *(short *)data);
674          break;
675       case SYBINT4:
676          sprintf(result, "%d", *(int *)data);
677          break;
678       default:
679          sprintf(result, "Unexpected column_type %d", col_type);
680          break;
681    }
682    return result;
683 }
684 #endif
685
686 /*
687  * Since rows are special PDUs in that they are not fixed and lack a size field,
688  * the length must be computed using the column information seen in the result
689  * PDU. This function does just that.
690  */
691 static size_t
692 tds_get_row_size(tvbuff_t *tvb, struct _netlib_data *nl_data, guint offset)
693 {
694         guint cur, i, csize;
695
696         cur = offset;
697         for (i = 0; i < nl_data->num_cols; i++) {
698                 if (!is_fixed_coltype(nl_data->columns[i]->ctype)) {
699                         csize = tvb_get_guint8(tvb, cur);
700                         cur++;
701                 } else
702                         csize = get_size_by_coltype(nl_data->columns[i]->ctype);
703                 cur += csize;
704         }
705
706         return (cur - offset + 1);
707 }
708
709 /*
710  * Read the results token and store the relevant information in the
711  * _netlib_data structure for later use (see tds_get_row_size).
712  */
713 static gboolean
714 read_results_tds5(tvbuff_t *tvb, struct _netlib_data *nl_data, guint offset)
715 {
716         guint len, name_len;
717         guint cur;
718         guint i;
719
720         len = tvb_get_letohs(tvb, offset+1);
721         cur = offset + 3;
722
723         /*
724          * This would be the logical place to check for little/big endianess
725          * if we didn't see the login packet.
726          */
727         nl_data->num_cols = tvb_get_letohs(tvb, cur);
728         if (nl_data->num_cols > MAX_COLUMNS) {
729                 nl_data->num_cols = 0;
730                 return FALSE;
731         }
732
733         cur += 2;
734
735         for (i = 0; i < nl_data->num_cols; i++) {
736                 nl_data->columns[i] = g_mem_chunk_alloc(tds_column);
737                 name_len = tvb_get_guint8(tvb,cur);
738                 cur ++;
739                 cur += name_len;
740
741                 cur++; /* unknown */
742
743                 nl_data->columns[i]->utype = tvb_get_letohs(tvb, cur);
744                 cur += 2;
745
746                 cur += 2; /* unknown */
747
748                 nl_data->columns[i]->ctype = tvb_get_guint8(tvb,cur);
749                 cur++;
750
751                 if (!is_fixed_coltype(nl_data->columns[i]->ctype)) {
752                         nl_data->columns[i]->csize = tvb_get_guint8(tvb,cur);
753                         cur ++;
754                 } else {
755                         nl_data->columns[i]->csize =
756                             get_size_by_coltype(nl_data->columns[i]->ctype);
757                 }
758                 cur++; /* unknown */
759         }
760         return TRUE;
761 }
762
763 /*
764  * If the packet type from the netlib header is a login packet, then dig into
765  * the packet to see if this is a supported TDS version and verify the otherwise
766  * weak heuristics of the netlib check.
767  */
768 static gboolean
769 netlib_check_login_pkt(tvbuff_t *tvb, guint offset, packet_info *pinfo, guint8 type)
770 {
771         guint tds_major, bytes_avail;
772
773         bytes_avail = tvb_length(tvb) - offset;
774         /*
775          * we have two login packet styles, one for TDS 4.2 and 5.0
776          */
777         if (type==TDS_LOGIN_PKT) {
778                 /* Use major version number to validate TDS 4/5 login
779                  * packet */
780
781                 /* Login packet is first in stream and should not be fragmented...
782                  * if it is we are screwed */
783                 if (bytes_avail < 467) return FALSE;
784                 tds_major = tvb_get_guint8(tvb, 466);
785                 if (tds_major != 4 && tds_major != 5) {
786                         return FALSE;
787                 }
788         /*
789          * and one added by Microsoft in SQL Server 7
790          */
791         } else if (type==TDS_LOGIN7_PKT) {
792                 if (bytes_avail < 16) return FALSE;
793                 tds_major = tvb_get_guint8(tvb, 15);
794                 if (tds_major != 0x70 && tds_major != 0x80) {
795                         return FALSE;
796                 }
797         } else if (type==TDS_QUERY5_PKT) {
798                 if (bytes_avail < 9) return FALSE;
799                 /* if this is a TDS 5.0 query check the token */
800                 if (tvb_get_guint8(tvb, 8) != TDS_LANG_TOKEN) {
801                         return FALSE;
802                 }
803         /* check if it is MS SQL default port */
804         } else if (pinfo->srcport != 1433 &&
805                 pinfo->destport != 1433) {
806                 /* otherwise, we can not ensure this is netlib */
807                 /* beyond a reasonable doubt.                  */
808                         return FALSE;
809         }
810         return TRUE;
811 }
812
813 static void
814 dissect_tds_env_chg(tvbuff_t *tvb, guint offset, guint token_sz,
815     proto_tree *tree)
816 {
817         guint8 env_type;
818         guint old_len, new_len, old_len_offset;
819         char *new_val = NULL, *old_val = NULL;
820         guint32 string_offset;
821         gboolean is_unicode = FALSE;
822         guint16 collate_codepage, collate_flags;
823         guint8 collate_charset_id;
824
825         env_type = tvb_get_guint8(tvb, offset);
826         proto_tree_add_text(tree, tvb, offset, 1, "Type: %u (%s)", env_type,
827                 val_to_str(env_type, env_chg_names, "Unknown"));
828
829         new_len = tvb_get_guint8(tvb, offset+1);
830         old_len_offset = offset + new_len + 2;
831         old_len = tvb_get_guint8(tvb, old_len_offset);
832
833         /*
834          * If our lengths plus the lengths of the type and the lengths
835          * don't add up to the token size, it must be UCS2.
836          */
837         if (old_len + new_len + 3 != token_sz) {
838                 is_unicode = TRUE;
839                 old_len_offset = offset + (new_len * 2) + 2;
840                 old_len = tvb_get_guint8(tvb, old_len_offset);
841         }
842
843         proto_tree_add_text(tree, tvb, offset + 1, 1, "New Value Length: %u",
844             new_len);
845         if (new_len) {
846                 if (env_type != 7) { /* if it's not 'Collation Info - which is not textual! */
847                         string_offset = offset + 2;
848                         if (is_unicode == TRUE) {
849                                 new_val = tvb_fake_unicode(tvb, string_offset,
850                                         new_len, TRUE);
851                                 new_len *= 2;
852                         } else
853                                 new_val = tvb_get_string(tvb, string_offset, new_len);
854                         proto_tree_add_text(tree, tvb, string_offset, new_len,
855                                 "New Value: %s", new_val);
856                         g_free(new_val);
857                 }
858                 else { /* parse collation info structure. From http://www.freetds.org/tds.html#collate */
859                         offset +=2;
860                         collate_codepage = tvb_get_letohs(tvb, offset);
861                         proto_tree_add_text(tree, tvb, offset, 2, "Codepage: %u" , collate_codepage);
862                         offset += 2;
863                         collate_flags = tvb_get_letohs(tvb, offset);
864                         proto_tree_add_text(tree, tvb, offset, 2, "Flags: 0x%x", collate_flags);
865                         offset += 2;
866                         collate_charset_id = tvb_get_guint8(tvb, offset);
867                         proto_tree_add_text(tree, tvb, offset, 1, "Charset ID: %u", collate_charset_id);
868                         offset +=1;
869                 }
870         }
871
872         proto_tree_add_text(tree, tvb, old_len_offset, 1, "Old Value Length: %u",
873             old_len);
874         if (old_len) {
875                 string_offset = old_len_offset + 1;
876                 if (is_unicode == TRUE) {
877                         old_val = tvb_fake_unicode(tvb, string_offset,
878                             old_len, TRUE);
879                         old_len *= 2;
880                 } else
881                         old_val = tvb_get_string(tvb, string_offset, old_len);
882                 proto_tree_add_text(tree, tvb, string_offset, old_len,
883                     "Old Value: %s", old_val);
884                 g_free(old_val);
885          }
886 }
887
888 static void
889 dissect_tds_msg_token(tvbuff_t *tvb, guint offset, guint token_sz, proto_tree *tree)
890 {
891         guint16 msg_len;
892         guint8 srvr_len;
893         char *msg;
894         gboolean is_unicode = FALSE;
895
896         proto_tree_add_text(tree, tvb, offset, 4, "SQL Message Number: %d", tvb_get_letohl(tvb, offset));
897         offset += 4;
898         proto_tree_add_text(tree, tvb, offset, 1, "State: %u", tvb_get_guint8(tvb, offset));
899         offset +=1;
900         proto_tree_add_text(tree, tvb, offset, 1, "Level: %u", tvb_get_guint8(tvb, offset));
901         offset +=1;
902         
903         msg_len = tvb_get_letohs(tvb, offset);
904         proto_tree_add_text(tree, tvb, offset, 2, "Message length: %u characters", msg_len);
905         offset +=2;
906         
907         srvr_len = tvb_get_guint8(tvb, offset + msg_len);
908         
909         if(msg_len + srvr_len + 9U + 3U != token_sz) /* 9 is the length of message number (4), state (1), level (1), msg_len (2), srvr_len (1) fields */
910                 is_unicode = TRUE;
911
912         if(is_unicode) {
913                 msg = tvb_fake_unicode(tvb, offset, msg_len, TRUE);
914                 msg_len *= 2;
915         } else {
916                 msg = tvb_get_string(tvb, offset, msg_len);
917         }
918         proto_tree_add_string(tree, hf_tds7_message, tvb, offset, msg_len, msg);
919         g_free(msg);
920         offset += msg_len;
921
922         proto_tree_add_text(tree, tvb, offset, 1, "Server name length: %u characters", srvr_len);
923         offset +=1;
924         
925         if (is_unicode) {
926                 msg = tvb_fake_unicode(tvb, offset, srvr_len, TRUE);
927                 srvr_len *=2;
928         } else {
929                 msg = tvb_get_string(tvb, offset, srvr_len);
930         }
931         proto_tree_add_text(tree, tvb, offset, srvr_len, "Server name: %s", msg);
932         g_free(msg);
933 }
934
935 static void
936 dissect_tds_err_token(tvbuff_t *tvb, guint offset, guint token_sz, proto_tree *tree)
937 {
938         guint16 msg_len;
939         guint8 srvr_len;
940         char *msg;
941         gboolean is_unicode = FALSE;
942
943         proto_tree_add_text(tree, tvb, offset, 4, "SQL Error Number: %d", tvb_get_letohl(tvb, offset));
944         offset += 4;
945         proto_tree_add_text(tree, tvb, offset, 1, "State: %u", tvb_get_guint8(tvb, offset));
946         offset +=1;
947         proto_tree_add_text(tree, tvb, offset, 1, "Level: %u", tvb_get_guint8(tvb, offset));
948         offset +=1;
949
950         msg_len = tvb_get_letohs(tvb, offset);
951         proto_tree_add_text(tree, tvb, offset, 1, "Error length: %u characters", msg_len);
952         offset +=2;
953
954         srvr_len = tvb_get_guint8(tvb, offset + msg_len);
955         
956         if(msg_len + srvr_len + 9U + 3U != token_sz) /* 9 is the length of message number (4), state (1), level (1), msg_len (2), srvr_len (1) fields */
957                 is_unicode = TRUE;
958
959         if(is_unicode) {
960                 msg = tvb_fake_unicode(tvb, offset, msg_len, TRUE);
961                 msg_len *= 2;
962         } else {
963                 msg = tvb_get_string(tvb, offset, msg_len);
964         }
965         proto_tree_add_text(tree, tvb, offset, msg_len, "Error: %s", format_text(msg, strlen(msg)));
966         g_free(msg);
967         offset += msg_len;
968
969         proto_tree_add_text(tree, tvb, offset, 1, "Server name length: %u characters", srvr_len);
970         offset +=1;
971         
972         if (is_unicode) {
973                 msg = tvb_fake_unicode(tvb, offset, srvr_len, TRUE);
974                 srvr_len *=2;
975         } else {
976                 msg = tvb_get_string(tvb, offset, srvr_len);
977         }
978         proto_tree_add_text(tree, tvb, offset, srvr_len, "Server name: %s", msg);
979         g_free(msg);
980 }
981
982 static void
983 dissect_tds_login_ack_token(tvbuff_t *tvb, guint offset, guint token_sz, proto_tree *tree)
984 {
985         guint8 msg_len;
986         char *msg;
987         gboolean is_unicode = FALSE;
988
989         proto_tree_add_text(tree, tvb, offset, 1, "Ack: %u", tvb_get_guint8(tvb, offset));
990         offset +=1;
991         proto_tree_add_text(tree, tvb, offset, 1, "Major version (may be incorrect): %d", tvb_get_guint8(tvb, offset));
992         offset +=1;
993         proto_tree_add_text(tree, tvb, offset, 1, "Minor version (may be incorrect): %d", tvb_get_guint8(tvb, offset));
994         offset +=1;
995         proto_tree_add_text(tree, tvb, offset, 2, "zero usually");
996         offset +=2;
997
998         msg_len = tvb_get_guint8(tvb, offset);
999         proto_tree_add_text(tree, tvb, offset, 1, "Text length: %u characters", msg_len);
1000         offset +=1;
1001
1002         if(msg_len + 6U + 3U != token_sz - 1) /* 6 is the length of ack(1), version (4), text length (1) fields */
1003                 is_unicode = TRUE;
1004         proto_tree_add_text(tree, tvb, offset, 0, "msg_len: %d, token_sz: %d, total: %d",msg_len, token_sz, msg_len + 6U + 3U);
1005         if(is_unicode) {
1006                 msg = tvb_fake_unicode(tvb, offset, msg_len, TRUE);
1007                 msg_len *= 2;
1008         } else {
1009                 msg = tvb_get_string(tvb, offset, msg_len);
1010         }
1011         proto_tree_add_text(tree, tvb, offset, msg_len, "Text: %s", format_text(msg, strlen(msg)));
1012         g_free(msg);
1013         offset += msg_len;
1014         
1015         proto_tree_add_text(tree, tvb, offset, 4, "Server Version");
1016         offset += 4;
1017 }
1018
1019 int 
1020 dissect_tds7_results_token(tvbuff_t *tvb, guint offset, proto_tree *tree)
1021 {
1022         guint16 num_columns, table_len;
1023         guint8 type, msg_len;
1024         int i;
1025         char *msg;
1026         guint16 collate_codepage, collate_flags;
1027         guint8 collate_charset_id;
1028
1029         num_columns = tvb_get_letohs(tvb, offset);
1030         proto_tree_add_text(tree, tvb, offset, 2, "Columns: %u", tvb_get_letohs(tvb, offset));
1031         offset +=2;
1032         for(i=0; i != num_columns; i++) {
1033                 proto_tree_add_text(tree, tvb, offset, 0, "Column %d", i + 1);
1034                 proto_tree_add_text(tree, tvb, offset, 2, "usertype: %d", tvb_get_letohs(tvb, offset));
1035                 offset +=2;
1036                 proto_tree_add_text(tree, tvb, offset, 2, "flags: %d", tvb_get_letohs(tvb, offset));
1037                 offset +=2;
1038                 type  = tvb_get_guint8(tvb, offset);
1039                 proto_tree_add_text(tree, tvb, offset, 1, "Type: %d", type);
1040                 offset +=1;
1041                 if(type == 38 || type == 104) { /* ugly, ugly hack. Wish I knew what it really means!*/
1042                         proto_tree_add_text(tree, tvb, offset, 1, "unknown 1 byte (%x)", tvb_get_guint8(tvb, offset));
1043                         offset +=1;
1044                 }
1045                 else if (type == 35) {
1046                         proto_tree_add_text(tree, tvb, offset, 4, "unknown 4 bytes (%x)", tvb_get_letohl(tvb, offset));
1047                         offset += 4;
1048                         collate_codepage = tvb_get_letohs(tvb, offset);
1049                         proto_tree_add_text(tree, tvb, offset, 2, "Codepage: %u" , collate_codepage);
1050                         offset += 2;
1051                         collate_flags = tvb_get_letohs(tvb, offset);
1052                         proto_tree_add_text(tree, tvb, offset, 2, "Flags: 0x%x", collate_flags);
1053                         offset += 2;
1054                         collate_charset_id = tvb_get_guint8(tvb, offset);
1055                         proto_tree_add_text(tree, tvb, offset, 1, "Charset ID: %u", collate_charset_id);
1056                         offset +=1;
1057                         table_len = tvb_get_letohs(tvb, offset);
1058                         offset +=2;
1059                         if(table_len != 0) {
1060                                 msg = tvb_fake_unicode(tvb, offset, table_len, TRUE);
1061                                 proto_tree_add_text(tree, tvb, offset, table_len*2, "Table name: %s", msg);
1062                                 g_free(msg);
1063                                 offset += table_len*2;
1064                         }
1065                 }
1066                 else if (type == 106) {
1067                         proto_tree_add_text(tree, tvb, offset, 3, "unknown 3 bytes");
1068                         offset +=3;
1069                 }
1070                 if(type > 128) {
1071                         proto_tree_add_text(tree, tvb, offset, 2, "Large type size: 0x%x", tvb_get_letohs(tvb, offset));
1072                         offset += 2;
1073                         collate_codepage = tvb_get_letohs(tvb, offset);
1074                         proto_tree_add_text(tree, tvb, offset, 2, "Codepage: %u" , collate_codepage);
1075                         offset += 2;
1076                         collate_flags = tvb_get_letohs(tvb, offset);
1077                         proto_tree_add_text(tree, tvb, offset, 2, "Flags: 0x%x", collate_flags);
1078                         offset += 2;
1079                         collate_charset_id = tvb_get_guint8(tvb, offset);
1080                         proto_tree_add_text(tree, tvb, offset, 1, "Charset ID: %u", collate_charset_id);
1081                         offset +=1;
1082                 }
1083                 msg_len = tvb_get_guint8(tvb, offset);
1084                 proto_tree_add_text(tree, tvb, offset, 1, "message length: %d",msg_len);
1085                 offset += 1;
1086                 if(msg_len != 0) {
1087                         msg = tvb_fake_unicode(tvb, offset, msg_len, TRUE);
1088                         proto_tree_add_text(tree, tvb, offset, msg_len*2, "Text: %s", msg);
1089                         g_free(msg);
1090                         offset += msg_len*2;
1091                 }
1092         }
1093         return offset;
1094 }
1095
1096 static void
1097 dissect_tds_done_token(tvbuff_t *tvb, guint offset, proto_tree *tree)
1098 {
1099         proto_tree_add_text(tree, tvb, offset, 2, "bit flag");
1100         offset += 2;
1101         proto_tree_add_text(tree, tvb, offset, 2, "unknown");
1102         offset += 2;
1103         proto_tree_add_text(tree, tvb, offset, 4, "row count: %u", tvb_get_letohl(tvb, offset));
1104         offset += 2;
1105 }
1106
1107 static void
1108 dissect_tds_rpc(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
1109 {
1110         int offset = 0;
1111         guint len;
1112         const char *val;
1113
1114         /*
1115          * RPC name.
1116          * XXX - how can we determine whether this is ASCII or Unicode?
1117          */
1118         len = tvb_get_letohs(tvb, offset);
1119         proto_tree_add_text(tree, tvb, offset, 2, "RPC Name Length: %u", len);
1120         offset += 2;
1121         if (len != 0) {
1122                 val = tvb_fake_unicode(tvb, offset, len, TRUE);
1123                 len *= 2;
1124                 proto_tree_add_text(tree, tvb, offset, len, "RPC Name: %s",
1125                     val);
1126                 offset += len;
1127         }
1128         
1129         proto_tree_add_text(tree, tvb, offset, -1, "Unknown data");
1130 }
1131
1132 static void
1133 dissect_tds_resp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1134 {
1135         int offset = 0;
1136         proto_item *token_item;
1137         proto_tree *token_tree;
1138         guint pos, token_sz = 0;
1139         guint8 token;
1140         struct _netlib_data nl_data;
1141         gint length_remaining;
1142
1143         memset(&nl_data, '\0', sizeof nl_data);
1144
1145         /*
1146          * Until we reach the end of the packet, read tokens.
1147          */
1148         pos = offset;
1149         while (tvb_reported_length_remaining(tvb, pos) > 0) {
1150                 /* our token */
1151                 token = tvb_get_guint8(tvb, pos);
1152
1153                 if (tds_is_fixed_token(token)) {
1154                         token_sz = tds_get_token_size(token) + 1;
1155                 } else if (token == TDS_ROW_TOKEN) {
1156                         /*
1157                          * Rows are special; they have no size field and
1158                          * aren't fixed length.
1159                          */
1160                         token_sz = tds_get_row_size(tvb, &nl_data, pos + 1);
1161                 } else
1162                         token_sz = tvb_get_letohs(tvb, pos + 1) + 3;
1163
1164                 length_remaining = tvb_ensure_length_remaining(tvb, pos);
1165                 if (token_sz > (guint)length_remaining)
1166                         token_sz = (guint)length_remaining;
1167
1168                 token_item = proto_tree_add_text(tree, tvb, pos, token_sz,
1169                     "Token 0x%02x %s", token,
1170                     val_to_str(token, token_names, "Unknown Token Type"));
1171                 token_tree = proto_item_add_subtree(token_item, ett_tds_token);
1172
1173                 /*
1174                  * If it's a variable token, put the length field in here
1175                  * instead of replicating this for each token subdissector.
1176                  */
1177                 if (!tds_is_fixed_token(token) && token != TDS_ROW_TOKEN) {
1178                         proto_tree_add_text(token_tree, tvb, pos+1, 2,
1179                             "Length: %u", tvb_get_letohs(tvb, pos+1));
1180                 }
1181
1182                 switch (token) {
1183
1184                 case TDS_RESULT_TOKEN:
1185                         /*
1186                          * If it's a result token, we need to stash the
1187                          * column info.
1188                          */
1189                         read_results_tds5(tvb, &nl_data, pos);
1190                         break;
1191
1192                 case TDS_ENV_CHG_TOKEN:
1193                         dissect_tds_env_chg(tvb, pos + 3, token_sz - 3,
1194                             token_tree);
1195                         break;
1196
1197                 case TDS_AUTH_TOKEN:
1198                         dissect_tds_ntlmssp(tvb, pinfo, token_tree, pos + 3,
1199                             token_sz - 3);
1200                         break;
1201                 case TDS_MSG_TOKEN:
1202                         dissect_tds_msg_token(tvb, pos + 3, token_sz - 3, token_tree);
1203                         break;
1204                 case TDS_ERR_TOKEN:
1205                         dissect_tds_err_token(tvb, pos + 3, token_sz - 3, token_tree);
1206                         break;
1207                 case TDS_DONE_TOKEN:
1208                         dissect_tds_done_token(tvb, pos + 1, token_tree);
1209                         break;
1210                 case TDS_LOGIN_ACK_TOKEN:
1211                         dissect_tds_login_ack_token(tvb, pos + 3, token_sz - 3, token_tree);
1212                         break;
1213                 case TDS7_RESULT_TOKEN:
1214                         pos = (dissect_tds7_results_token(tvb, pos + 1, token_tree)-1);
1215                         break;
1216                 }
1217
1218                 /* and step to the end of the token, rinse, lather, repeat */
1219                 pos += token_sz;
1220         }
1221 }
1222
1223 static void
1224 dissect_netlib_buffer(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1225 {
1226         int offset = 0;
1227         proto_item *tds_item = NULL;
1228         proto_tree *tds_tree = NULL;
1229         guint8 type;
1230         guint8 status;
1231         guint16 size;
1232         guint16 channel;
1233         guint8 packet_number;
1234         gboolean save_fragmented;
1235         int len;
1236         fragment_data *fd_head;
1237         tvbuff_t *next_tvb;
1238
1239         if (tree) {
1240                 /* create display subtree for the protocol */
1241                 tds_item = proto_tree_add_item(tree, proto_tds, tvb, offset, -1,
1242                     FALSE);
1243
1244                 tds_tree = proto_item_add_subtree(tds_item, ett_tds);
1245         }
1246         type = tvb_get_guint8(tvb, offset);
1247         if (tree) {
1248                 proto_tree_add_uint(tds_tree, hf_tds_type, tvb, offset, 1,
1249                     type);
1250         }
1251         status = tvb_get_guint8(tvb, offset + 1);
1252         if (tree) {
1253                 proto_tree_add_uint(tds_tree, hf_tds_status, tvb, offset + 1, 1,
1254                     status);
1255         }
1256         size = tvb_get_ntohs(tvb, offset + 2);
1257         if (tree) {
1258                 proto_tree_add_uint(tds_tree, hf_tds_size, tvb, offset + 2, 2,
1259                         size);
1260         }
1261         channel = tvb_get_ntohs(tvb, offset + 4);
1262         if (tree) {
1263                 proto_tree_add_uint(tds_tree, hf_tds_channel, tvb, offset + 4, 2,
1264                         channel);
1265         }
1266         packet_number = tvb_get_guint8(tvb, offset + 6);
1267         if (tree) {
1268                 proto_tree_add_uint(tds_tree, hf_tds_packet_number, tvb, offset + 6, 1,
1269                         packet_number);
1270                 proto_tree_add_item(tds_tree, hf_tds_window, tvb, offset + 7, 1,
1271                         FALSE);
1272         }
1273         offset += 8;    /* skip Netlib header */
1274
1275         /*
1276          * Deal with fragmentation.
1277          */
1278         save_fragmented = pinfo->fragmented;
1279         if (tds_defragment &&
1280             (packet_number > 1 || status == STATUS_NOT_LAST_BUFFER)) {
1281                 if (status == STATUS_NOT_LAST_BUFFER) {
1282                         if (check_col(pinfo->cinfo, COL_INFO))
1283                                 col_append_str(pinfo->cinfo, COL_INFO,
1284                                     " (Not last buffer)");
1285                 }
1286                 len = tvb_reported_length_remaining(tvb, offset);
1287                 /*
1288                  * XXX - I've seen captures that start with a login
1289                  * packet with a sequence number of 2.
1290                  */
1291                 fd_head = fragment_add_seq_check(tvb, offset, pinfo, channel,
1292                     tds_fragment_table, tds_reassembled_table,
1293                     packet_number - 1, len, status == STATUS_NOT_LAST_BUFFER);
1294                 next_tvb = process_reassembled_data(tvb, offset, pinfo,
1295                     "Reassembled TDS", fd_head, &tds_frag_items, NULL,
1296                     tds_tree);
1297         } else {
1298                 /*
1299                  * If this isn't the last buffer, just show it as a fragment.
1300                  * (XXX - it'd be nice to dissect it if it's the first
1301                  * buffer, but we'd need to do reassembly in order to
1302                  * discover that.)
1303                  *
1304                  * If this is the last buffer, dissect it.
1305                  * (XXX - it'd be nice to show it as a fragment if it's part
1306                  * of a fragmented message, but we'd need to do reassembly
1307                  * in order to discover that.)
1308                  */
1309                 if (status == STATUS_NOT_LAST_BUFFER)
1310                         next_tvb = NULL;
1311                 else {
1312                         next_tvb = tvb_new_subset(tvb, offset, -1, -1);
1313                 }
1314         }
1315         if (next_tvb != NULL) {
1316                 switch (type) {
1317
1318                 case TDS_RPC_PKT:
1319                         dissect_tds_rpc(next_tvb, pinfo, tds_tree);
1320                         break;
1321
1322                 case TDS_RESP_PKT:
1323                         dissect_tds_resp(next_tvb, pinfo, tds_tree);
1324                         break;
1325
1326                 case TDS_LOGIN7_PKT:
1327                         dissect_tds7_login(next_tvb, pinfo, tds_tree);
1328                         break;
1329                 case TDS_QUERY_PKT:
1330                         dissect_tds_query_packet(next_tvb, pinfo, tds_tree);
1331                         break;
1332                 default:
1333                         proto_tree_add_text(tds_tree, next_tvb, 0, -1,
1334                             "TDS Packet");
1335                         break;
1336                 }
1337         } else {
1338                 next_tvb = tvb_new_subset (tvb, offset, -1, -1);
1339                 call_dissector(data_handle, next_tvb, pinfo, tds_tree);
1340         }
1341 }
1342
1343 static void
1344 dissect_tds_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1345 {
1346         volatile gboolean first_time = TRUE;
1347         volatile int offset = 0;
1348         guint length_remaining;
1349         guint8 type;
1350         guint16 plen;
1351         guint length;
1352         tvbuff_t *next_tvb;
1353         proto_item *tds_item = NULL;
1354         proto_tree *tds_tree = NULL;
1355
1356         while (tvb_reported_length_remaining(tvb, offset) != 0) {
1357                 length_remaining = tvb_ensure_length_remaining(tvb, offset);
1358
1359                 /*
1360                  * Can we do reassembly?
1361                  */
1362                 if (tds_desegment && pinfo->can_desegment) {
1363                         /*
1364                          * Yes - is the fixed-length part of the PDU
1365                          * split across segment boundaries?
1366                          */
1367                         if (length_remaining < 8) {
1368                                 /*
1369                                  * Yes.  Tell the TCP dissector where the
1370                                  * data for this message starts in the data
1371                                  * it handed us, and how many more bytes we
1372                                  * need, and return.
1373                                  */
1374                                 pinfo->desegment_offset = offset;
1375                                 pinfo->desegment_len = 8 - length_remaining;
1376                                 return;
1377                         }
1378                 }
1379
1380                 type = tvb_get_guint8(tvb, offset);
1381
1382                 /*
1383                  * Get the length of the PDU.
1384                  */
1385                 plen = tvb_get_ntohs(tvb, offset + 2);
1386                 if (plen < 8) {
1387                         /*
1388                          * The length is less than the header length.
1389                          * Put in the type, status, and length, and
1390                          * report the length as bogus.
1391                          */
1392                         if (tree) {
1393                                 /* create display subtree for the protocol */
1394                                 tds_item = proto_tree_add_item(tree, proto_tds,
1395                                     tvb, offset, -1, FALSE);
1396
1397                                 tds_tree = proto_item_add_subtree(tds_item,
1398                                     ett_tds);
1399                                 proto_tree_add_uint(tds_tree, hf_tds_type, tvb,
1400                                     offset, 1, type);
1401                                 proto_tree_add_item(tds_tree, hf_tds_status,
1402                                     tvb, offset + 1, 1, FALSE);
1403                                 proto_tree_add_uint_format(tds_tree,
1404                                     hf_tds_size, tvb, offset + 2, 2, plen,
1405                                     "Size: %u (bogus, should be >= 8)", plen);
1406                         }
1407
1408                         /*
1409                          * Give up - we can't dissect any more of this
1410                          * data.
1411                          */
1412                         break;
1413                 }
1414
1415                 /*
1416                  * Can we do reassembly?
1417                  */
1418                 if (tds_desegment && pinfo->can_desegment) {
1419                         /*
1420                          * Yes - is the PDU split across segment boundaries?
1421                          */
1422                         if (length_remaining < plen) {
1423                                 /*
1424                                  * Yes.  Tell the TCP dissector where the
1425                                  * data for this message starts in the data
1426                                  * it handed us, and how many more bytes we
1427                                  * need, and return.
1428                                  */
1429                                 pinfo->desegment_offset = offset;
1430                                 pinfo->desegment_len = plen - length_remaining;
1431                                 return;
1432                         }
1433                 }
1434
1435                 if (first_time) {
1436                         if (check_col(pinfo->cinfo, COL_PROTOCOL))
1437                                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TDS");
1438
1439                         /*
1440                          * Set the packet description based on its TDS packet
1441                          * type.
1442                          */
1443                         if (check_col(pinfo->cinfo, COL_INFO)) {
1444                                 col_add_str(pinfo->cinfo, COL_INFO,
1445                                     val_to_str(type, packet_type_names,
1446                                       "Unknown Packet Type: %u"));
1447                         }
1448                         first_time = FALSE;
1449                 }
1450
1451                 /*
1452                  * Construct a tvbuff containing the amount of the payload
1453                  * we have available.  Make its reported length the amount
1454                  * of data in the PDU.
1455                  *
1456                  * XXX - if reassembly isn't enabled. the subdissector will
1457                  * throw a BoundsError exception, rather than a
1458                  * ReportedBoundsError exception.  We really want a tvbuff
1459                  * where the length is "length", the reported length is
1460                  * "plen", and the "if the snapshot length were infinite"
1461                  * length is the minimum of the reported length of the tvbuff
1462                  * handed to us and "plen", with a new type of exception
1463                  * thrown if the offset is within the reported length but
1464                  * beyond that third length, with that exception getting the
1465                  * "Unreassembled Packet" error.
1466                  */
1467                 length = length_remaining;
1468                 if (length > plen)
1469                         length = plen;
1470                 next_tvb = tvb_new_subset(tvb, offset, length, plen);
1471
1472                 /*
1473                  * Dissect the Netlib buffer.
1474                  *
1475                  * Catch the ReportedBoundsError exception; if this
1476                  * particular Netlib buffer happens to get a
1477                  * ReportedBoundsError exception, that doesn't mean
1478                  * that we should stop dissecting PDUs within this frame
1479                  * or chunk of reassembled data.
1480                  *
1481                  * If it gets a BoundsError, we can stop, as there's nothing
1482                  * more to see, so we just re-throw it.
1483                  */
1484                 TRY {
1485                         dissect_netlib_buffer(next_tvb, pinfo, tree);
1486                 }
1487                 CATCH(BoundsError) {
1488                         RETHROW;
1489                 }
1490                 CATCH(ReportedBoundsError) {
1491                         show_reported_bounds_error(tvb, pinfo, tree);
1492                 }
1493                 ENDTRY;
1494
1495                 /*
1496                  * Step to the next Netlib buffer.
1497                  */
1498                 offset += plen;
1499         }
1500 }
1501
1502 static gboolean
1503 dissect_tds_tcp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1504 {
1505         int offset = 0;
1506         guint8 type;
1507         guint8 status;
1508         guint16 plen;
1509         conversation_t *conv;
1510
1511         /*
1512          * If we don't have even enough data for a Netlib header,
1513          * just say it's not TDS.
1514          */
1515         if (!tvb_bytes_exist(tvb, offset, 8))
1516                 return FALSE;
1517
1518         /*
1519          * Quickly scan all the data we have in order to see if
1520          * everything in it looks like Netlib traffic.
1521          */
1522         while (tvb_bytes_exist(tvb, offset, 1)) {
1523                 /*
1524                  * Check the type field.
1525                  */
1526                 type = tvb_get_guint8(tvb, offset);
1527                 if (!is_valid_tds_type(type))
1528                         return FALSE;
1529
1530                 /*
1531                  * Check the status field, if it's present.
1532                  */
1533                 if (!tvb_bytes_exist(tvb, offset + 1, 1))
1534                         break;
1535                 status = tvb_get_guint8(tvb, offset + 1);
1536                 if (!is_valid_tds_status(status))
1537                         return FALSE;
1538
1539                 /*
1540                  * Get the length of the PDU.
1541                  */
1542                 if (!tvb_bytes_exist(tvb, offset + 2, 2))
1543                         break;
1544                 plen = tvb_get_ntohs(tvb, offset + 2);
1545                 if (plen < 8) {
1546                         /*
1547                          * The length is less than the header length.
1548                          * That's bogus.
1549                          */
1550                         return FALSE;
1551                 }
1552
1553                 /*
1554                  * If we're at the beginning of the segment, check the
1555                  * payload if it's a login packet.
1556                  */
1557                 if (offset == 0) {
1558                         if (!netlib_check_login_pkt(tvb, offset, pinfo, type))
1559                                 return FALSE;
1560                 }
1561
1562                 /*
1563                  * Step to the next Netlib buffer.
1564                  */
1565                 offset += plen;
1566         }
1567
1568         /*
1569          * OK, it passes the test; assume the rest of this conversation
1570          * is TDS.
1571          */
1572         conv = find_conversation(&pinfo->src, &pinfo->dst, pinfo->ptype,
1573             pinfo->srcport, pinfo->destport, 0);
1574         if (conv == NULL) {
1575                 /*
1576                  * No conversation exists yet - create one.
1577                  */
1578                 conv = conversation_new(&pinfo->src, &pinfo->dst,
1579                     pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
1580         }
1581         conversation_set_dissector(conv, tds_tcp_handle);
1582
1583         /*
1584          * Now dissect it as TDS.
1585          */
1586         dissect_tds_tcp(tvb, pinfo, tree);
1587         return TRUE;
1588 }
1589
1590 static void
1591 tds_init(void)
1592 {
1593         /*
1594          * Initialize the fragment and reassembly tables.
1595          */
1596         fragment_table_init(&tds_fragment_table);
1597         reassembled_table_init(&tds_reassembled_table);
1598
1599         /*
1600          * Reinitialize the chunks of data for remembering row
1601          * information.
1602          */
1603         if (tds_column)
1604                 g_mem_chunk_destroy(tds_column);
1605
1606         tds_column = g_mem_chunk_new("tds_column", tds_column_length,
1607                 tds_column_init_count * tds_column_length,
1608                 G_ALLOC_AND_FREE);
1609 }
1610
1611 /* Register the protocol with Ethereal */
1612
1613 /* this format is required because a script is used to build the C function
1614    that calls all the protocol registration.
1615 */
1616
1617 void
1618 proto_register_netlib(void)
1619 {
1620         static hf_register_info hf[] = {
1621                 { &hf_tds_type,
1622                         { "Type",           "tds.type",
1623                         FT_UINT8, BASE_HEX, VALS(packet_type_names), 0x0,
1624                         "Packet Type", HFILL }
1625                 },
1626                 { &hf_tds_status,
1627                         { "Status",         "tds.status",
1628                         FT_UINT8, BASE_DEC, VALS(status_names), 0x0,
1629                         "Frame status", HFILL }
1630                 },
1631                 { &hf_tds_size,
1632                         { "Size",           "tds.size",
1633                         FT_UINT16, BASE_DEC, NULL, 0x0,
1634                         "Packet Size", HFILL }
1635                 },
1636                 { &hf_tds_channel,
1637                         { "Channel",        "tds.channel",
1638                         FT_UINT16, BASE_DEC, NULL, 0x0,
1639                         "Channel Number", HFILL }
1640                 },
1641                 { &hf_tds_packet_number,
1642                         { "Packet Number",  "tds.packet_number",
1643                         FT_UINT8, BASE_DEC, NULL, 0x0,
1644                         "Packet Number", HFILL }
1645                 },
1646                 { &hf_tds_window,
1647                         { "Window",         "tds.window",
1648                         FT_UINT8, BASE_DEC, NULL, 0x0,
1649                         "Window", HFILL }
1650                 },
1651                 { &hf_tds_fragment_overlap,
1652                         { "Segment overlap",    "tds.fragment.overlap",
1653                         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1654                         "Fragment overlaps with other fragments", HFILL }
1655                 },
1656                 { &hf_tds_fragment_overlap_conflict,
1657                         { "Conflicting data in fragment overlap", "tds.fragment.overlap.conflict",
1658                         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1659                         "Overlapping fragments contained conflicting data", HFILL }
1660                 },
1661                 { &hf_tds_fragment_multiple_tails,
1662                         { "Multiple tail fragments found", "tds.fragment.multipletails",
1663                         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1664                         "Several tails were found when defragmenting the packet", HFILL }
1665                 },
1666                 { &hf_tds_fragment_too_long_fragment,
1667                         { "Segment too long",   "tds.fragment.toolongfragment",
1668                         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1669                         "Segment contained data past end of packet", HFILL }
1670                 },
1671                 { &hf_tds_fragment_error,
1672                         { "Defragmentation error",      "tds.fragment.error",
1673                         FT_FRAMENUM, BASE_NONE, NULL, 0x0,
1674                         "Defragmentation error due to illegal fragments", HFILL }
1675                 },
1676                 { &hf_tds_fragment,
1677                         { "TDS Fragment",       "tds.fragment",
1678                         FT_FRAMENUM, BASE_NONE, NULL, 0x0,
1679                         "TDS Fragment", HFILL }
1680                 },
1681                 { &hf_tds_fragments,
1682                         { "TDS Fragments",      "tds.fragments",
1683                         FT_NONE, BASE_NONE, NULL, 0x0,
1684                         "TDS Fragments", HFILL }
1685                 },
1686                 { &hf_tds_reassembled_in,
1687                         { "Reassembled TDS in frame", "tds.reassembled_in",
1688                         FT_FRAMENUM, BASE_NONE, NULL, 0x0,
1689                         "This TDS packet is reassembled in this frame", HFILL }
1690                 },
1691                 { &hf_tds7_login_total_size,
1692                         { "Total Packet Length", "tds7login.total_len",
1693                         FT_UINT32, BASE_DEC, NULL, 0x0,
1694                         "TDS7 Login Packet total packet length", HFILL }
1695                 },
1696                 { &hf_tds7_version,
1697                         { "TDS version", "tds7login.version",
1698                         FT_UINT32, BASE_HEX, NULL, 0x0,
1699                         "TDS version", HFILL }
1700                 },
1701                 { &hf_tds7_packet_size,
1702                         { "Packet Size", "tds7login.packet_size",
1703                         FT_UINT32, BASE_DEC, NULL, 0x0,
1704                         "Packet size", HFILL }
1705                 },
1706                 { &hf_tds7_client_version,
1707                         { "Client version", "tds7login.client_version",
1708                         FT_UINT32, BASE_DEC, NULL, 0x0,
1709                         "Client version", HFILL }
1710                 },
1711                 { &hf_tds7_client_pid,
1712                         { "Client PID", "tds7login.client_pid",
1713                         FT_UINT32, BASE_DEC, NULL, 0x0,
1714                         "Client PID", HFILL }
1715                 },
1716                 { &hf_tds7_connection_id,
1717                         { "Connection ID", "tds7login.connection_id",
1718                         FT_UINT32, BASE_DEC, NULL, 0x0,
1719                         "Connection ID", HFILL }
1720                 },
1721                 { &hf_tds7_option_flags1,
1722                         { "Option Flags 1", "tds7login.option_flags1",
1723                         FT_UINT8, BASE_HEX, NULL, 0x0,
1724                         "Option Flags 1", HFILL }
1725                 },
1726                 { &hf_tds7_option_flags2,
1727                         { "Option Flags 2", "tds7login.option_flags2",
1728                         FT_UINT8, BASE_HEX, NULL, 0x0,
1729                         "Option Flags 2", HFILL }
1730                 },
1731                 { &hf_tds7_sql_type_flags,
1732                         { "SQL Type Flags", "tds7login.sql_type_flags",
1733                         FT_UINT8, BASE_HEX, NULL, 0x0,
1734                         "SQL Type Flags", HFILL }
1735                 },
1736                 { &hf_tds7_reserved_flags,
1737                         { "Reserved Flags", "tds7login.reserved_flags",
1738                         FT_UINT8, BASE_HEX, NULL, 0x0,
1739                         "reserved flags", HFILL }
1740                 },
1741                 { &hf_tds7_time_zone,
1742                         { "Time Zone", "tds7login.time_zone",
1743                         FT_UINT32, BASE_HEX, NULL, 0x0,
1744                         "Time Zone", HFILL }
1745                 },
1746                 { &hf_tds7_collation,
1747                         { "Collation", "tds7login.collation",
1748                         FT_UINT32, BASE_HEX, NULL, 0x0,
1749                         "Collation", HFILL }
1750                 },
1751                 { &hf_tds7_message,
1752                         { "Message", "tds7.message", 
1753                         FT_STRING, BASE_NONE, NULL, 0x0, 
1754                         "", HFILL }
1755                 },
1756         };
1757         static gint *ett[] = {
1758                 &ett_tds,
1759                 &ett_tds_fragments,
1760                 &ett_tds_fragment,
1761                 &ett_tds_token,
1762                 &ett_tds7_login,
1763                 &ett_tds7_hdr,
1764         };
1765         module_t *tds_module;
1766
1767 /* Register the protocol name and description */
1768         proto_tds = proto_register_protocol("Tabular Data Stream",
1769             "TDS", "tds");
1770
1771 /* Required function calls to register the header fields and subtrees used */
1772         proto_register_field_array(proto_tds, hf, array_length(hf));
1773         proto_register_subtree_array(ett, array_length(ett));
1774
1775         tds_tcp_handle = create_dissector_handle(dissect_tds_tcp, proto_tds);
1776
1777         tds_module = prefs_register_protocol(proto_tds, NULL);
1778         prefs_register_bool_preference(tds_module, "desegment_buffers",
1779             "Desegment all TDS buffers spanning multiple TCP segments",
1780             "Whether the TDS dissector should desegment all TDS buffers spanning multiple TCP segments",
1781             &tds_desegment);
1782         prefs_register_bool_preference(tds_module, "defragment",
1783             "Defragment all TDS messages with multiple buffers",
1784             "Whether the TDS dissector should defragment all messages spanning multiple Netlib buffers",
1785             &tds_defragment);
1786
1787         register_init_routine(tds_init);
1788 }
1789
1790 /* If this dissector uses sub-dissector registration add a registration routine.
1791    This format is required because a script is used to find these routines and
1792    create the code that calls these routines.
1793 */
1794 void
1795 proto_reg_handoff_tds(void)
1796 {
1797         /* dissector_add("tcp.port", 1433, dissect_tds,
1798             proto_tds); */
1799         heur_dissector_add("tcp", dissect_tds_tcp_heur, proto_tds);
1800
1801         ntlmssp_handle = find_dissector("ntlmssp");
1802         data_handle = find_dissector("data");
1803 }