Last set of trivial fixes for "no previous declaration" warnings.
[obnox/wireshark/wip.git] / epan / dissectors / 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$
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 "isprint.h"
154
155 #include <epan/packet.h>
156 #include <epan/conversation.h>
157 #include <epan/strutil.h>
158
159 #include "packet-frame.h"
160 #include <epan/reassemble.h>
161 #include <epan/prefs.h>
162
163 #define TDS_QUERY_PKT        1
164 #define TDS_LOGIN_PKT        2
165 #define TDS_RPC_PKT          3
166 #define TDS_RESP_PKT         4
167 #define TDS_RAW_PKT          5
168 #define TDS_CANCEL_PKT       6
169 #define TDS_BULK_DATA_PKT    7
170 #define TDS_OPEN_CHN_PKT     8
171 #define TDS_CLOSE_CHN_PKT    9
172 #define TDS_RES_ERROR_PKT   10
173 #define TDS_LOG_CHN_ACK_PKT 11
174 #define TDS_ECHO_PKT        12
175 #define TDS_LOGOUT_CHN_PKT  13
176 #define TDS_QUERY5_PKT      15  /* or "Normal tokenized request or response */
177 #define TDS_LOGIN7_PKT      16  /* or "Urgent tokenized request or response */
178 #define TDS_NTLMAUTH_PKT    17
179 #define TDS_XXX7_PKT        18  /* seen in one capture */
180
181 #define is_valid_tds_type(x) ((x) >= TDS_QUERY_PKT && (x) <= TDS_XXX7_PKT)
182
183 /* The following constants are imported more or less directly from FreeTDS */
184 /* TODO Update from current version of FreeTDS tds.h                       */
185
186 #define TDS5_PARAMS_TOKEN   215  /* 0xD7    TDS 5.0 only              */
187 #define TDS5_DYNAMIC_TOKEN  231  /* 0xE7    TDS 5.0 only              */
188 #define TDS5_PARAMFMT_TOKEN 236  /* 0xEC    TDS 5.0 only              */
189 #define TDS5_PARAMFMT2_TOKEN 32  /* 0x20    TDS 5.0 only              */
190 #define TDS_LANG_TOKEN       33  /* 0x21    TDS 5.0 only              */
191 #define TDS5_ORDERBY2_TOKEN  34  /* 0x22    TDS 5.0 only              */
192 #define TDS5_CURDECLARE2_TOKEN  35  /* 0x23    TDS 5.0 only              */
193 #define TDS5_ROWFMT2_TOKEN   97  /* 0x61    TDS 5.0 only              */
194 #define TDS5_MSG_TOKEN      101  /* 0x65    TDS 5.0 only              */
195 #define TDS_LOGOUT_TOKEN    113  /* 0x71    TDS 5.0 only? ct_close()  */
196 #define TDS_RET_STAT_TOKEN  121  /* 0x79                              */
197 #define TDS_PROCID_TOKEN    124  /* 0x7C    TDS 4.2 only - TDS_PROCID */
198 #define TDS7_RESULT_TOKEN   129  /* 0x81    TDS 7.0 only              */
199 #define TDS_COL_NAME_TOKEN  160  /* 0xA0    TDS 4.2 only              */
200 #define TDS_COL_INFO_TOKEN  161  /* 0xA1    TDS 4.2 only - TDS_COLFMT */
201 #define TDS5_DYNAMIC2_TOKEN 163  /* 0xA3    TDS 5.0 only              */
202 /*#define  TDS_TABNAME   164 */
203 /*#define  TDS_COL_INFO   165 */
204 #define TDS_COMPUTE_NAMES_TOKEN   167   /* 0xA7 */
205 #define TDS_COMPUTE_RESULT_TOKEN  168   /* 0xA8 */
206 #define TDS_ORDER_BY_TOKEN  169  /* 0xA9    TDS_ORDER                 */
207 #define TDS_ERR_TOKEN       170  /* 0xAA                              */
208 #define TDS_MSG_TOKEN       171  /* 0xAB                              */
209 #define TDS_PARAM_TOKEN     172  /* 0xAC    RETURNVALUE?              */
210 #define TDS_LOGIN_ACK_TOKEN 173  /* 0xAD                              */
211 #define TDS_CONTROL_TOKEN   174  /* 0xAE    TDS_CONTROL               */
212 #define TDS_KEY_TOKEN       202  /* 0xCA                              */
213 #define TDS_ROW_TOKEN       209  /* 0xD1                              */
214 #define TDS_CMP_ROW_TOKEN   211  /* 0xD3                              */
215 #define TDS_CAP_TOKEN       226  /* 0xE2                              */
216 #define TDS_ENV_CHG_TOKEN   227  /* 0xE3                              */
217 #define TDS_EED_TOKEN       229  /* 0xE5                              */
218 #define TDS_AUTH_TOKEN      237  /* 0xED                              */
219 #define TDS_RESULT_TOKEN    238  /* 0xEE                              */
220 #define TDS_DONE_TOKEN      253  /* 0xFD    TDS_DONE                  */
221 #define TDS_DONEPROC_TOKEN  254  /* 0xFE    TDS_DONEPROC              */
222 #define TDS_DONEINPROC_TOKEN 255  /* 0xFF    TDS_DONEINPROC            */
223
224 /* Microsoft internal stored procedure id's */
225
226 #define TDS_SP_CURSOR           1
227 #define TDS_SP_CURSOROPEN       2
228 #define TDS_SP_CURSORPREPARE    3
229 #define TDS_SP_CURSOREXECUTE    4
230 #define TDS_SP_CURSORPREPEXEC   5
231 #define TDS_SP_CURSORUNPREPARE  6
232 #define TDS_SP_CURSORFETCH      7
233 #define TDS_SP_CURSOROPTION     8
234 #define TDS_SP_CURSORCLOSE      9
235 #define TDS_SP_EXECUTESQL      10
236 #define TDS_SP_PREPARE         11
237 #define TDS_SP_EXECUTE         12
238 #define TDS_SP_PREPEXEC        13
239 #define TDS_SP_PREPEXECRPC     14
240 #define TDS_SP_UNPREPARE       15
241
242 /* Sybase Data Types */
243
244 #define SYBCHAR      47   /* 0x2F */
245 #define SYBVARCHAR   39   /* 0x27 */
246 #define SYBINTN      38   /* 0x26 */
247 #define SYBINT1      48   /* 0x30 */
248 #define SYBINT2      52   /* 0x34 */
249 #define SYBINT4      56   /* 0x38 */
250 #define SYBINT8     127   /* 0x7F */
251 #define SYBFLT8      62   /* 0x3E */
252 #define SYBDATETIME  61   /* 0x3D */
253 #define SYBBIT       50   /* 0x32 */
254 #define SYBTEXT      35   /* 0x23 */
255 #define SYBNTEXT     99   /* 0x63 */
256 #define SYBIMAGE     34   /* 0x22 */
257 #define SYBMONEY4    122  /* 0x7A */
258 #define SYBMONEY     60   /* 0x3C */
259 #define SYBDATETIME4 58   /* 0x3A */
260 #define SYBREAL      59   /* 0x3B */
261 #define SYBBINARY    45   /* 0x2D */
262 #define SYBVOID      31   /* 0x1F */
263 #define SYBVARBINARY 37   /* 0x25 */
264 #define SYBNVARCHAR  103  /* 0x67 */
265 #define SYBBITN      104  /* 0x68 */
266 #define SYBNUMERIC   108  /* 0x6C */
267 #define SYBDECIMAL   106  /* 0x6A */
268 #define SYBFLTN      109  /* 0x6D */
269 #define SYBMONEYN    110  /* 0x6E */
270 #define SYBDATETIMN  111  /* 0x6F */
271 #define XSYBCHAR     167  /* 0xA7 */
272 #define XSYBVARCHAR  175  /* 0xAF */
273 #define XSYBNVARCHAR 231  /* 0xE7 */
274 #define XSYBNCHAR    239  /* 0xEF */
275 #define SYBUNIQUE    0x24
276 #define SYBVARIANT   0x62
277
278 #define is_fixed_coltype(x) (x==SYBINT1    || \
279                         x==SYBINT2      || \
280                         x==SYBINT4      || \
281                         x==SYBINT8      || \
282                         x==SYBREAL       || \
283                         x==SYBFLT8      || \
284                         x==SYBDATETIME  || \
285                         x==SYBDATETIME4 || \
286                         x==SYBBIT       || \
287                         x==SYBMONEY     || \
288                         x==SYBMONEY4    || \
289                         x==SYBUNIQUE)
290
291 /* Initialize the protocol and registered fields */
292 static int proto_tds = -1;
293 static int hf_tds_type = -1;
294 static int hf_tds_status = -1;
295 static int hf_tds_size = -1;
296 static int hf_tds_channel = -1;
297 static int hf_tds_packet_number = -1;
298 static int hf_tds_window = -1;
299 static int hf_tds_reassembled_in = -1;
300 static int hf_tds_fragments = -1;
301 static int hf_tds_fragment = -1;
302 static int hf_tds_fragment_overlap = -1;
303 static int hf_tds_fragment_overlap_conflict = -1;
304 static int hf_tds_fragment_multiple_tails = -1;
305 static int hf_tds_fragment_too_long_fragment = -1;
306 static int hf_tds_fragment_error = -1;
307
308 static int hf_tds7_login_total_size = -1;
309 static int hf_tds7_version = -1;
310 static int hf_tds7_packet_size = -1;
311 static int hf_tds7_client_version = -1;
312 static int hf_tds7_client_pid = -1;
313 static int hf_tds7_connection_id = -1;
314 static int hf_tds7_option_flags1 = -1;
315 static int hf_tds7_option_flags2 = -1;
316 static int hf_tds7_sql_type_flags = -1;
317 static int hf_tds7_reserved_flags = -1;
318 static int hf_tds7_time_zone = -1;
319 static int hf_tds7_collation = -1;
320 static int hf_tds7_message = -1;
321
322 /* Initialize the subtree pointers */
323 static gint ett_tds = -1;
324 static gint ett_tds_fragments = -1;
325 static gint ett_tds_fragment = -1;
326 static gint ett_tds_token = -1;
327 static gint ett_tds7_login = -1;
328 static gint ett_tds7_query = 0;
329 static gint ett_tds7_hdr = -1;
330
331 /* Desegmentation of Netlib buffers crossing TCP segment boundaries. */
332 static gboolean tds_desegment = TRUE;
333
334 static const fragment_items tds_frag_items = {
335         &ett_tds_fragment,
336         &ett_tds_fragments,
337         &hf_tds_fragments,
338         &hf_tds_fragment,
339         &hf_tds_fragment_overlap,
340         &hf_tds_fragment_overlap_conflict,
341         &hf_tds_fragment_multiple_tails,
342         &hf_tds_fragment_too_long_fragment,
343         &hf_tds_fragment_error,
344         &hf_tds_reassembled_in,
345         "fragments"
346 };
347
348 /* Tables for reassembly of fragments. */
349 static GHashTable *tds_fragment_table = NULL;
350 static GHashTable *tds_reassembled_table = NULL;
351
352 /* defragmentation of multi-buffer TDS PDUs */
353 static gboolean tds_defragment = TRUE;
354
355 static dissector_handle_t tds_tcp_handle;
356 static dissector_handle_t ntlmssp_handle;
357 static dissector_handle_t gssapi_handle;
358 static dissector_handle_t data_handle;
359
360 /* TDS protocol type preference */
361 /*   XXX: This preference is used as a 'hint' for cases where interpretation is ambiguous */
362 /*        Currently the hint is global                                                    */
363 /*   TODO: Consider storing protocol type with each conversation                          */
364 /*        (when type is determined and using the preference as a default) ??              */
365
366 #define TDS_PROTOCOL_NOT_SPECIFIED   0
367 #define TDS_PROTOCOL_4      4
368 #define TDS_PROTOCOL_5      5
369 #define TDS_PROTOCOL_7      7
370 #define TDS_PROTOCOL_8      8
371
372 static gint tds_protocol_type = TDS_PROTOCOL_NOT_SPECIFIED;
373
374 const enum_val_t tds_protocol_type_options[] = {
375   {"not_specified", "Not Specified", TDS_PROTOCOL_NOT_SPECIFIED},
376   {"tds4", "TDS 4", TDS_PROTOCOL_4},  /* TDS 4.2 and TDS 4.6 */
377   {"tds5", "TDS 5", TDS_PROTOCOL_5},
378   {"tds7", "TDS 7", TDS_PROTOCOL_7},
379   {"tds8", "TDS 8", TDS_PROTOCOL_8},
380   {NULL, NULL, -1}
381 };
382
383 #define TDS_PROTO_PREF_NOT_SPECIFIED (tds_protocol_type == TDS_NOT_SPECIFIED)
384 #define TDS_PROTO_PREF_TDS4 (tds_protocol_type == TDS_PROTOCOL_4)
385 #define TDS_PROTO_PREF_TDS5 (tds_protocol_type == TDS_PROTOCOL_5)
386 #define TDS_PROTO_PREF_TDS7 (tds_protocol_type == TDS_PROTOCOL_7)
387 #define TDS_PROTO_PREF_TDS8 (tds_protocol_type == TDS_PROTOCOL_8)
388 #define TDS_PROTO_PREF_TDS7_TDS8 ( TDS_PROTO_PREF_TDS7 || TDS_PROTO_PREF_TDS8 )
389
390 /* TDS "endian type" */
391 /*   XXX: Assumption is that all TDS conversations being decoded in a particular capture */
392 /*        have the same endian type                                                      */
393 /*   TODO: consider storing endian type with each conversation                           */
394 /*         (using pref as the default)                                                   */
395
396 static gint tds_little_endian = TRUE;
397
398 const enum_val_t tds_endian_type_options[] = {
399     {"little_endian", "Little Endian", TRUE},
400     {"big_endian"   , "Big Endian"   , FALSE},
401     {NULL, NULL, -1}
402 };
403
404
405 /* TCP port preferences for TDS decode */
406
407 static range_t *tds_tcp_ports = NULL;
408
409 /* These correspond to the netlib packet type field */
410 static const value_string packet_type_names[] = {
411         {TDS_QUERY_PKT,  "Query Packet"},
412         {TDS_LOGIN_PKT,  "Login Packet"},
413         {TDS_RPC_PKT,    "Remote Procedure Call Packet"},
414         {TDS_RESP_PKT,   "Response Packet"},
415         {TDS_CANCEL_PKT, "Cancel Packet"},
416         {TDS_QUERY5_PKT, "TDS5 Query Packet"},
417         {TDS_LOGIN7_PKT, "TDS7/8 Login Packet"},
418         {TDS_XXX7_PKT, "TDS7/8 0x12 Packet"},
419         {TDS_NTLMAUTH_PKT, "NT Authentication Packet"},
420         {0, NULL},
421 };
422
423 /* The status field */
424
425 #define is_valid_tds_status(x) ((x) <= STATUS_EVENT_NOTIFICATION)
426
427 #define STATUS_NOT_LAST_BUFFER          0x00
428 #define STATUS_LAST_BUFFER              0x01
429 #define STATUS_ATTN_REQUEST_ACK         0x02
430 #define STATUS_ATTN_REQUEST             0x03
431 #define STATUS_EVENT_NOTIFICATION       0x04
432
433 static const value_string status_names[] = {
434         {STATUS_NOT_LAST_BUFFER,    "Not last buffer"},
435         {STATUS_LAST_BUFFER,        "Last buffer in request or response"},
436         {STATUS_ATTN_REQUEST_ACK,   "Acknowledgment of last attention request"},
437         {STATUS_ATTN_REQUEST,       "Attention request"},
438         {STATUS_EVENT_NOTIFICATION, "Event notification"},
439         {0, NULL},
440 };
441
442 /* The one byte token at the start of each TDS PDU */
443 static const value_string token_names[] = {
444         {TDS5_DYNAMIC_TOKEN, "TDS5 Dynamic SQL"},
445         {TDS5_PARAMFMT_TOKEN, "TDS5 Parameter Format"},
446         {TDS5_PARAMFMT2_TOKEN, "TDS5 Parameter2 Format"},
447         {TDS5_PARAMS_TOKEN, "TDS5 Parameters"},
448         {TDS_LANG_TOKEN, "Language"},
449         {TDS_LOGOUT_TOKEN, "Logout"},
450         {TDS_RET_STAT_TOKEN, "Return Status"},
451         {TDS_PROCID_TOKEN, "Proc ID"},
452         {TDS7_RESULT_TOKEN, "TDS7+ Results"},
453         {TDS_COL_NAME_TOKEN, "Column Names"},
454         {TDS_COL_INFO_TOKEN, "Column Info"},
455         {TDS_COMPUTE_NAMES_TOKEN, "Compute Names"},
456         {TDS_COMPUTE_RESULT_TOKEN, "Compute Results"},
457         {TDS_ORDER_BY_TOKEN, "Order By"},
458         {TDS_ERR_TOKEN, "Error Message"},
459         {TDS_MSG_TOKEN, "Info Message"},
460         {TDS_PARAM_TOKEN, "Parameter"},
461         {TDS_LOGIN_ACK_TOKEN, "Login Acknowledgement"},
462         {TDS_CONTROL_TOKEN, "TDS Control"},
463         {TDS_KEY_TOKEN, "TDS Key"},
464         {TDS_ROW_TOKEN, "Row"},
465         {TDS_CMP_ROW_TOKEN, "Compute Row"},
466         {TDS_CAP_TOKEN, "Capabilities"},
467         {TDS_ENV_CHG_TOKEN, "Environment Change"},
468         {TDS_EED_TOKEN, "Extended Error"},
469         {TDS_AUTH_TOKEN, "Authentication"},
470         {TDS_RESULT_TOKEN, "Results"},
471         {TDS_DONE_TOKEN, "Done"},
472         {TDS_DONEPROC_TOKEN, "Done Proc"},
473         {TDS_DONEINPROC_TOKEN, "Done In Proc"},
474         {TDS5_DYNAMIC2_TOKEN, "TDS5 Dynamic2"},
475         {TDS5_ORDERBY2_TOKEN, "TDS5 OrderBy2"},
476         {TDS5_CURDECLARE2_TOKEN, "TDS5 CurDeclare2"},
477         {TDS5_ROWFMT2_TOKEN, "TDS5 RowFmt2"},
478         {TDS5_MSG_TOKEN, "TDS5 Msg"},
479         {0, NULL},
480 };
481
482
483 static const value_string internal_stored_proc_id_names[] = {
484     {TDS_SP_CURSOR,          "sp_cursor"         },
485     {TDS_SP_CURSOROPEN,      "sp_cursoropen"     },
486     {TDS_SP_CURSORPREPARE,   "sp_cursorprepare"  },
487     {TDS_SP_CURSOREXECUTE,   "sp_cursorexecute"  },
488     {TDS_SP_CURSORPREPEXEC,  "sp_cursorprepexec" },
489     {TDS_SP_CURSORUNPREPARE, "sp_cursorunprepare"},
490     {TDS_SP_CURSORFETCH,     "sp_cursorfetch"    },
491     {TDS_SP_CURSOROPTION,    "sp_cursoroption"   },
492     {TDS_SP_CURSORCLOSE,     "sp_cursorclose"    },
493     {TDS_SP_EXECUTESQL,      "sp_executesql"     },
494     {TDS_SP_PREPARE,         "sp_prepare"        },
495     {TDS_SP_EXECUTE,         "sp_execute"        },
496     {TDS_SP_PREPEXEC,        "sp_prepexec"       },
497     {TDS_SP_PREPEXECRPC,     "sp_prepexecrpc"    },
498     {TDS_SP_UNPREPARE,       "sp_unprepare"      },
499         {0,                      NULL                },
500 };
501
502 static const value_string env_chg_names[] = {
503         {1, "Database"},
504         {2, "Language"},
505         {3, "Sort Order"},
506         {4, "Blocksize"},
507         {5, "Unicode Locale ID"},
508         {6, "Unicode Comparison Style"},
509         {7, "Collation Info"},
510         {0, NULL},
511 };
512
513 static const value_string login_field_names[] = {
514         {0, "Client Name"},
515         {1, "Username"},
516         {2, "Password"},
517         {3, "App Name"},
518         {4, "Server Name"},
519         {5, "Unknown1"},
520         {6, "Library Name"},
521         {7, "Locale"},
522         {8, "Database Name"},
523         {0, NULL},
524 };
525
526
527 #define MAX_COLUMNS 256
528
529 /*
530  * This is where we store the column information to be used in decoding the
531  * TDS_ROW_TOKEN tokens.
532  */
533 struct _tds_col {
534      gchar name[256];
535      guint16 utype;
536      guint8 ctype;
537      guint csize;
538 };
539
540 struct _netlib_data {
541         guint num_cols;
542         struct _tds_col *columns[MAX_COLUMNS];
543 };
544
545 struct tds7_login_packet_hdr {
546         guint32 total_packet_size;
547         guint32 tds_version;
548         guint32 packet_size;
549         guint32 client_version;
550         guint32 client_pid;
551         guint32 connection_id;
552         guint8  option_flags1;
553         guint8  option_flags2;
554         guint8  sql_type_flags;
555         guint8  reserved_flags;
556         guint32 time_zone;
557         guint32 collation;
558 };
559
560 /* all the standard memory management stuff */
561 #define tds_column_length (sizeof(struct _tds_col))
562 #define tds_column_init_count 40
563
564 static GMemChunk *tds_column = NULL;
565
566 /* support routines */
567
568 static void
569 dissect_tds_nt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
570     guint offset, guint length)
571 {
572         tvbuff_t *nt_tvb;
573
574         nt_tvb = tvb_new_subset(tvb, offset, -1, length);
575         if(tvb_strneql(tvb, offset, "NTLMSSP", 7) == 0)
576                 call_dissector(ntlmssp_handle, nt_tvb, pinfo, tree);
577         else
578                 call_dissector(gssapi_handle, nt_tvb, pinfo, tree);
579 }
580
581 /*  */
582
583 static guint16
584 tds_tvb_get_xxtohs(tvbuff_t *tvb, gint offset, gint tds_little_endian) {
585     if (tds_little_endian)
586         return tvb_get_letohs(tvb, offset);
587     else
588         return tvb_get_ntohs(tvb, offset);
589 }
590
591 static guint32
592 tds_tvb_get_xxtohl(tvbuff_t *tvb, gint offset, gint tds_little_endian) {
593     if (tds_little_endian)
594         return tvb_get_letohl(tvb, offset);
595     else
596         return tvb_get_ntohl(tvb, offset);
597 }
598
599
600 static int tds_token_is_fixed_size(guint8 token)
601 {
602      switch (token) {
603           case TDS_DONE_TOKEN:
604           case TDS_DONEPROC_TOKEN:
605           case TDS_DONEINPROC_TOKEN:
606           case TDS_RET_STAT_TOKEN:
607           case TDS7_RESULT_TOKEN:
608           case TDS_PROCID_TOKEN:
609           case TDS_LOGOUT_TOKEN:
610                return 1;
611           default:
612                return 0;
613      }
614 }
615
616
617 static int tds_get_fixed_token_size(guint8 token)
618 {
619      switch(token) {
620           case TDS_DONE_TOKEN:
621           case TDS_DONEPROC_TOKEN:
622           case TDS_DONEINPROC_TOKEN:
623           case TDS_PROCID_TOKEN:
624                return 8;
625           case TDS_RET_STAT_TOKEN:
626                return 4;
627           case TDS_LOGOUT_TOKEN:
628                return 1;
629           case TDS7_RESULT_TOKEN:
630           default:
631                return 0;
632      }
633 }
634
635 static guint
636 tds_get_variable_token_size(tvbuff_t *tvb, gint offset, guint8 token,
637                             guint *len_field_size_p, guint *len_field_val_p)
638 {
639     switch(token) {
640         /* some tokens have a 4 byte length field */
641         case TDS5_PARAMFMT2_TOKEN:
642         case TDS_LANG_TOKEN:
643         case TDS5_ORDERBY2_TOKEN:
644         case TDS5_CURDECLARE2_TOKEN:
645         case TDS5_ROWFMT2_TOKEN:
646         case TDS5_DYNAMIC2_TOKEN:
647             *len_field_size_p = 4;
648             *len_field_val_p = tds_tvb_get_xxtohl(tvb, offset, tds_little_endian);
649             break;
650         /* some have a 1 byte length field */
651         case TDS5_MSG_TOKEN:
652             *len_field_size_p = 1;
653             *len_field_val_p = tvb_get_guint8(tvb, offset);
654             break;
655         /* and most have a 2 byte length field */
656         default:
657             *len_field_size_p = 2;
658             *len_field_val_p = tds_tvb_get_xxtohs(tvb, offset, tds_little_endian);
659             break;
660     }
661     return *len_field_val_p + *len_field_size_p + 1;
662 }
663
664
665 static void
666 dissect_tds_query_packet(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
667 {
668         guint offset, len;
669         gboolean is_unicode = TRUE;
670         char *msg;
671
672         proto_item *query_hdr;
673         proto_tree *query_tree;
674         
675         offset = 0;
676         query_hdr = proto_tree_add_text(tree, tvb, offset, -1, "TDS Query Packet");
677         query_tree = proto_item_add_subtree(query_hdr, ett_tds7_query);
678         len = tvb_reported_length_remaining(tvb, offset);
679
680         if (TDS_PROTO_PREF_TDS4 || 
681             (!TDS_PROTO_PREF_TDS7_TDS8 &&
682              ((len < 2) || tvb_get_guint8(tvb, offset+1) != 0)))
683                 is_unicode = FALSE;
684         
685         if (is_unicode)
686                 msg = ep_tvb_fake_unicode(tvb, offset, len/2, TRUE);
687         else
688                 msg = ep_tvb_get_string(tvb, offset, len);
689         
690         proto_tree_add_text(query_tree, tvb, offset, len, "Query: %s", msg);
691         offset += len;
692 }
693
694
695 static void 
696 dissect_tds5_lang_token(tvbuff_t *tvb, guint offset, guint len, proto_tree *tree) {
697     gboolean is_unicode = FALSE;
698     char *msg;
699
700     proto_tree_add_text(tree, tvb, offset, 1 , "Status: %u", tvb_get_guint8(tvb, offset));
701     offset += 1;
702     len    -= 1;
703
704     if (is_unicode)
705         msg = ep_tvb_fake_unicode(tvb, offset, (len)/2, TRUE);
706     else
707         msg = ep_tvb_get_string(tvb, offset, len);
708         
709     proto_tree_add_text(tree, tvb, offset, len, "Language text: %s", msg);
710 }
711
712 static void
713 dissect_tds_query5_packet(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
714 {
715     guint offset;
716     guint pos;
717     guint token_len_field_size = 2;
718     guint token_len_field_val;
719     guint8 token;
720     guint token_sz;
721     proto_item *query_hdr;
722     proto_tree *query_tree;
723     proto_item *token_item;
724     proto_tree *token_tree;
725     
726     offset = 0;
727     query_hdr = proto_tree_add_text(tree, tvb, offset, -1, "TDS5 Query Packet");
728     query_tree = proto_item_add_subtree(query_hdr, ett_tds7_query);
729
730     /*
731      * Until we reach the end of the packet, read tokens.
732      */
733     pos = offset;
734     while (tvb_reported_length_remaining(tvb, pos) > 0) {
735
736         /* our token */
737         token = tvb_get_guint8(tvb, pos);
738         if (tds_token_is_fixed_size(token))
739             token_sz = tds_get_fixed_token_size(token) + 1;
740         else
741             token_sz = tds_get_variable_token_size(tvb, pos+1, token, &token_len_field_size,
742                                                    &token_len_field_val);
743
744         /* XXX - Should this check be done in tds_get_variable_token_size()
745          * instead? */
746         if ((int) token_sz < 0) {
747             proto_tree_add_text(tree, tvb, 0, 0, "Bogus token size: %u",
748                 token_sz);
749             break;
750         }
751
752         token_item = proto_tree_add_text(tree, tvb, pos, token_sz,
753                     "Token 0x%02x %s", token,
754                     val_to_str(token, token_names, "Unknown Token Type"));
755         token_tree = proto_item_add_subtree(token_item, ett_tds_token);
756
757         /*
758          * If it's a variable token, put the length field in here
759          * instead of replicating this for each token subdissector.
760          */
761         if (!tds_token_is_fixed_size(token))
762             proto_tree_add_text(token_tree, tvb, pos+1, token_len_field_size, "Length: %u", token_len_field_val);
763
764         switch (token) {
765             case TDS_LANG_TOKEN:
766                 dissect_tds5_lang_token(tvb, pos + 5, token_sz -5, token_tree);
767                 break;
768             default:
769                 break;
770         }
771
772         pos += token_sz;
773
774     }  /* while */
775 }
776
777
778 static void
779 dissect_tds7_login(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
780 {
781         guint offset, i, offset2, len;
782         gboolean is_unicode = TRUE;
783         char *val;
784
785         proto_item *login_hdr;
786         proto_tree *login_tree;
787         proto_item *header_hdr;
788         proto_tree *header_tree;
789         proto_item *length_hdr;
790         proto_tree *length_tree;
791         
792         struct tds7_login_packet_hdr td7hdr;
793         gint length_remaining;
794
795
796         /* create display subtree for the protocol */
797         offset = 0;
798         login_hdr = proto_tree_add_text(tree, tvb, offset, -1, "TDS7 Login Packet");
799         login_tree = proto_item_add_subtree(login_hdr, ett_tds7_login);
800         header_hdr = proto_tree_add_text(login_tree, tvb, offset, 36, "Login Packet Header");
801         header_tree = proto_item_add_subtree(header_hdr, ett_tds7_hdr);
802         
803         td7hdr.total_packet_size = tvb_get_letohl(tvb, offset);
804         proto_tree_add_uint(header_tree, hf_tds7_login_total_size, tvb, offset, 
805         sizeof(td7hdr.total_packet_size), td7hdr.total_packet_size);
806         offset += sizeof(td7hdr.total_packet_size);
807         
808         td7hdr.tds_version = tvb_get_ntohl(tvb, offset);
809         proto_tree_add_uint(header_tree, hf_tds7_version, tvb, offset, sizeof(td7hdr.tds_version), td7hdr.tds_version);
810         offset += sizeof(td7hdr.tds_version);
811         
812         td7hdr.packet_size = tvb_get_ntohl(tvb, offset);
813         proto_tree_add_uint(header_tree, hf_tds7_packet_size, tvb, offset, sizeof(td7hdr.packet_size), td7hdr.packet_size);
814         offset += sizeof(td7hdr.packet_size);
815         
816         td7hdr.client_version = tvb_get_ntohl(tvb, offset);
817         proto_tree_add_uint(header_tree, hf_tds7_client_version, tvb, offset, sizeof(td7hdr.client_version), td7hdr.client_version);
818         offset += sizeof(td7hdr.client_version);
819         
820         td7hdr.client_pid = tvb_get_letohl(tvb, offset);
821         proto_tree_add_uint(header_tree, hf_tds7_client_pid, tvb, offset, sizeof(td7hdr.client_pid), td7hdr.client_pid);
822         offset += sizeof(td7hdr.client_pid);
823
824         td7hdr.connection_id= tvb_get_letohl(tvb, offset);
825         proto_tree_add_uint(header_tree, hf_tds7_connection_id, tvb, offset, sizeof(td7hdr.connection_id), td7hdr.connection_id);
826         offset += sizeof(td7hdr.connection_id);
827         
828         td7hdr.option_flags1 = tvb_get_guint8(tvb, offset);
829         proto_tree_add_uint(header_tree, hf_tds7_option_flags1, tvb, offset, sizeof(td7hdr.option_flags1), td7hdr.option_flags1);
830         offset += sizeof(td7hdr.option_flags1);
831         
832         td7hdr.option_flags2 = tvb_get_guint8(tvb, offset);
833         proto_tree_add_uint(header_tree, hf_tds7_option_flags2, tvb, offset, sizeof(td7hdr.option_flags2), td7hdr.option_flags2);
834         offset += sizeof(td7hdr.option_flags2);
835
836         td7hdr.sql_type_flags = tvb_get_guint8(tvb, offset);    
837         proto_tree_add_uint(header_tree, hf_tds7_sql_type_flags, tvb, offset, sizeof(td7hdr.sql_type_flags), td7hdr.sql_type_flags);
838         offset += sizeof(td7hdr.sql_type_flags);
839
840         td7hdr.reserved_flags = tvb_get_guint8(tvb, offset);
841         proto_tree_add_uint(header_tree, hf_tds7_reserved_flags, tvb, offset, sizeof(td7hdr.reserved_flags), td7hdr.reserved_flags);
842         offset += sizeof(td7hdr.reserved_flags);
843         
844         td7hdr.time_zone = tvb_get_ntohl(tvb, offset);
845         proto_tree_add_uint(header_tree, hf_tds7_time_zone, tvb, offset, sizeof(td7hdr.time_zone), td7hdr.time_zone);
846         offset += sizeof(td7hdr.time_zone);
847
848         td7hdr.collation = tvb_get_ntohl(tvb, offset);
849         proto_tree_add_uint(header_tree, hf_tds7_collation, tvb, offset, sizeof(td7hdr.collation), td7hdr.collation);
850         offset += sizeof(td7hdr.collation);
851
852         length_hdr = proto_tree_add_text(login_tree, tvb, offset, 50, "Lengths and offsets");
853         length_tree = proto_item_add_subtree(length_hdr, ett_tds7_hdr);
854
855         for (i = 0; i < 9; i++) {
856                 offset2 = tvb_get_letohs(tvb, offset + i*4);
857                 len = tvb_get_letohs(tvb, offset + i*4 + 2);
858                 proto_tree_add_text(length_tree, tvb, offset + i*4, 2,
859                     "%s offset: %u",
860                     val_to_str(i, login_field_names, "Unknown"),
861                     offset2);
862                 proto_tree_add_text(length_tree, tvb, offset + i*4 + 2, 2,
863                         "%s length: %u",
864                         val_to_str(i, login_field_names, "Unknown"),
865                         len);
866                 if (len != 0) {
867                         if( i != 2) {
868                                 if (is_unicode == TRUE) {
869                                         val = ep_tvb_fake_unicode(tvb, offset2, len, TRUE);
870                                         len *= 2;
871                                 } else
872                                         val = ep_tvb_get_string(tvb, offset2, len);
873                                 proto_tree_add_text(login_tree, tvb, offset2, len, "%s: %s", val_to_str(i, login_field_names, "Unknown"), val);
874                         }
875                         else {
876                                 if (is_unicode)
877                                         len *= 2;
878                                 proto_tree_add_text(login_tree, tvb, offset2, len, "%s", val_to_str(i, login_field_names, "Unknown"));
879                         }
880                 }
881         }
882
883         /*
884          * XXX - what about the client MAC address, etc.?
885          */
886         length_remaining = tvb_reported_length_remaining(tvb, offset2 + len);
887         if (length_remaining > 0) {
888                 dissect_tds_nt(tvb, pinfo, login_tree, offset2 + len,
889                     length_remaining);
890         }
891 }
892
893 static int get_size_by_coltype(int servertype)
894 {
895    switch(servertype)
896    {
897       case SYBINT1:        return 1;  break;
898       case SYBINT2:        return 2;  break;
899       case SYBINT4:        return 4;  break;
900       case SYBINT8:        return 8;  break;
901       case SYBREAL:        return 4;  break;
902       case SYBFLT8:        return 8;  break;
903       case SYBDATETIME:    return 8;  break;
904       case SYBDATETIME4:   return 4;  break;
905       case SYBBIT:         return 1;  break;
906       case SYBBITN:        return 1;  break;
907       case SYBMONEY:       return 8;  break;
908       case SYBMONEY4:      return 4;  break;
909       case SYBUNIQUE:      return 16; break;
910       default:             return -1; break;
911    }
912 }
913 # if 0
914 /*
915  * data_to_string should take column data and turn it into something we can
916  * display on the tree.
917  */
918 static char *data_to_string(void *data, guint col_type, guint col_size)
919 {
920    static char  result[256];
921    guint i;
922
923    switch(col_type) {
924       case SYBVARCHAR:
925          /* strncpy(result, (char *)data, col_size); */
926          for (i=0;i<col_size && i<(256-1);i++)
927                 if (!isprint(((char *)data)[i])) result[i]='.';
928                 else result[i]=((char *)data)[i];
929          result[i] = '\0';
930          break;
931       case SYBINT2:
932          sprintf(result, "%d", *(short *)data);
933          break;
934       case SYBINT4:
935          sprintf(result, "%d", *(int *)data);
936          break;
937       default:
938          sprintf(result, "Unexpected column_type %d", col_type);
939          break;
940    }
941    return result;
942 }
943 #endif
944
945 /*
946  * Since rows are special PDUs in that they are not fixed and lack a size field,
947  * the length must be computed using the column information seen in the result
948  * PDU. This function does just that.
949  */
950 static size_t
951 tds_get_row_size(tvbuff_t *tvb, struct _netlib_data *nl_data, guint offset)
952 {
953         guint cur, i, csize;
954
955         cur = offset;
956         for (i = 0; i < nl_data->num_cols; i++) {
957                 if (!is_fixed_coltype(nl_data->columns[i]->ctype)) {
958                         csize = tvb_get_guint8(tvb, cur);
959                         cur++;
960                 } else
961                         csize = get_size_by_coltype(nl_data->columns[i]->ctype);
962                 cur += csize;
963         }
964
965         return (cur - offset + 1);
966 }
967
968 /*
969  * Process TDS 4 "COL_INFO" token and store relevant information in the 
970  * _netlib_data structure for later use (see tds_get_row_size)
971  * 
972  * XXX Can TDS 4 be "big-endian" ? we'll assume yes.
973  *
974  */
975 static gboolean
976 dissect_tds_col_info_token(tvbuff_t *tvb, struct _netlib_data *nl_data, guint offset)
977 {
978     guint next, cur;
979     guint col;
980
981     next = offset + tds_tvb_get_xxtohs(tvb, offset+1, tds_little_endian) + 3;
982     cur = offset + 3;
983
984     col = 0;
985     while (cur < next) {
986
987         if (col >= MAX_COLUMNS) {
988             nl_data->num_cols = 0;
989             return FALSE;
990         }
991         
992         nl_data->columns[col] = g_mem_chunk_alloc(tds_column);
993
994         nl_data->columns[col]->name[0] ='\0'; 
995
996         nl_data->columns[col]->utype = tds_tvb_get_xxtohs(tvb, cur, tds_little_endian);
997         cur += 2;
998
999         cur += 2; /* unknown */
1000
1001         nl_data->columns[col]->ctype = tvb_get_guint8(tvb,cur);
1002         cur++;
1003
1004         if (!is_fixed_coltype(nl_data->columns[col]->ctype)) {
1005             nl_data->columns[col]->csize = tvb_get_guint8(tvb,cur);
1006             cur ++;
1007         } else {
1008             nl_data->columns[col]->csize =
1009                 get_size_by_coltype(nl_data->columns[col]->ctype);
1010         }
1011
1012         col += 1;
1013
1014     } /* while */
1015
1016     nl_data->num_cols = col;
1017     return TRUE;
1018 }
1019
1020
1021 /*
1022  * Read the results token and store the relevant information in the
1023  * _netlib_data structure for later use (see tds_get_row_size).
1024  *
1025  * TODO: check we don't go past end of the token
1026  */
1027 static gboolean
1028 read_results_tds5(tvbuff_t *tvb, struct _netlib_data *nl_data, guint offset, guint len _U_)
1029 {
1030         guint name_len;
1031         guint cur;
1032         guint i;
1033
1034         cur = offset;
1035
1036         /*
1037          * This would be the logical place to check for little/big endianess
1038          * if we didn't see the login packet.
1039          * XXX: We'll take a hint
1040          */
1041         nl_data->num_cols = tds_tvb_get_xxtohs(tvb, cur, tds_little_endian);
1042         if (nl_data->num_cols > MAX_COLUMNS) {
1043                 nl_data->num_cols = 0;
1044                 return FALSE;
1045         }
1046
1047         cur += 2;
1048
1049         for (i = 0; i < nl_data->num_cols; i++) {
1050                 nl_data->columns[i] = g_mem_chunk_alloc(tds_column);
1051                 name_len = tvb_get_guint8(tvb,cur);
1052                 cur ++;
1053                 cur += name_len;
1054
1055                 cur++; /* unknown */
1056
1057                 nl_data->columns[i]->utype = tds_tvb_get_xxtohs(tvb, cur, tds_little_endian);
1058                 cur += 2;
1059
1060                 cur += 2; /* unknown */
1061
1062                 nl_data->columns[i]->ctype = tvb_get_guint8(tvb,cur);
1063                 cur++;
1064
1065                 if (!is_fixed_coltype(nl_data->columns[i]->ctype)) {
1066                         nl_data->columns[i]->csize = tvb_get_guint8(tvb,cur);
1067                         cur ++;
1068                 } else {
1069                         nl_data->columns[i]->csize =
1070                             get_size_by_coltype(nl_data->columns[i]->ctype);
1071                 }
1072                 cur++; /* unknown */
1073         }
1074         return TRUE;
1075 }
1076
1077 /*
1078  * If the packet type from the netlib header is a login packet, then dig into
1079  * the packet to see if this is a supported TDS version and verify the otherwise
1080  * weak heuristics of the netlib check.
1081  */
1082 static gboolean
1083 netlib_check_login_pkt(tvbuff_t *tvb, guint offset, packet_info *pinfo, guint8 type)
1084 {
1085         guint tds_major, bytes_avail;
1086
1087         bytes_avail = tvb_length(tvb) - offset;
1088         /*
1089          * we have two login packet styles, one for TDS 4.2 and 5.0
1090          */
1091         if (type==TDS_LOGIN_PKT) {
1092                 /* Use major version number to validate TDS 4/5 login
1093                  * packet */
1094
1095                 /* Login packet is first in stream and should not be fragmented...
1096                  * if it is we are screwed */
1097                 if (bytes_avail < 467) return FALSE;
1098                 tds_major = tvb_get_guint8(tvb, 466);
1099                 if (tds_major != 4 && tds_major != 5) {
1100                         return FALSE;
1101                 }
1102         /*
1103          * and one added by Microsoft in SQL Server 7
1104          */
1105         } else if (type==TDS_LOGIN7_PKT) {
1106                 if (bytes_avail < 16) return FALSE;
1107                 tds_major = tvb_get_guint8(tvb, 15);
1108                 if (tds_major != 0x70 && tds_major != 0x80) {
1109                         return FALSE;
1110                 }
1111         } else if (type==TDS_QUERY5_PKT) {
1112                 if (bytes_avail < 9) return FALSE;
1113                 /* if this is a TDS 5.0 query check the token */
1114                 if (tvb_get_guint8(tvb, 8) != TDS_LANG_TOKEN) {
1115                         return FALSE;
1116                 }
1117         }
1118         /*
1119          * See if either tcp.destport or tcp.srcport is specified
1120          * in the preferences as being a TDS port.
1121          */
1122         else if (!value_is_in_range(tds_tcp_ports, pinfo->srcport) && 
1123                  !value_is_in_range(tds_tcp_ports, pinfo->destport)) {
1124                 return FALSE;
1125         }
1126
1127         return TRUE;
1128 }
1129
1130 static void
1131 dissect_tds_env_chg(tvbuff_t *tvb, guint offset, guint token_sz,
1132     proto_tree *tree)
1133 {
1134         guint8 env_type;
1135         guint old_len, new_len, old_len_offset;
1136         char *new_val = NULL, *old_val = NULL;
1137         guint32 string_offset;
1138         gboolean is_unicode = FALSE;
1139         guint16 collate_codepage, collate_flags;
1140         guint8 collate_charset_id;
1141
1142         env_type = tvb_get_guint8(tvb, offset);
1143         proto_tree_add_text(tree, tvb, offset, 1, "Type: %u (%s)", env_type,
1144                 val_to_str(env_type, env_chg_names, "Unknown"));
1145
1146         new_len = tvb_get_guint8(tvb, offset+1);
1147         old_len_offset = offset + new_len + 2;
1148         old_len = tvb_get_guint8(tvb, old_len_offset);
1149
1150         /*
1151          * If our lengths plus the lengths of the type and the lengths
1152          * don't add up to the token size, it must be UCS2.
1153          */
1154         if (old_len + new_len + 3 != token_sz) {
1155                 is_unicode = TRUE;
1156                 old_len_offset = offset + (new_len * 2) + 2;
1157                 old_len = tvb_get_guint8(tvb, old_len_offset);
1158         }
1159
1160         proto_tree_add_text(tree, tvb, offset + 1, 1, "New Value Length: %u",
1161             new_len);
1162         if (new_len) {
1163                 if (env_type != 7) { /* if it's not 'Collation Info - which is not textual! */
1164                         string_offset = offset + 2;
1165                         if (is_unicode == TRUE) {
1166                                 new_val = ep_tvb_fake_unicode(tvb, string_offset,
1167                                         new_len, TRUE);
1168                                 new_len *= 2;
1169                         } else
1170                                 new_val = ep_tvb_get_string(tvb, string_offset, new_len);
1171                         proto_tree_add_text(tree, tvb, string_offset, new_len,
1172                                 "New Value: %s", new_val);
1173                 }
1174                 else { /* parse collation info structure. From http://www.freetds.org/tds.html#collate */
1175                         offset +=2;
1176                         collate_codepage = tvb_get_letohs(tvb, offset);
1177                         proto_tree_add_text(tree, tvb, offset, 2, "Codepage: %u" , collate_codepage);
1178                         offset += 2;
1179                         collate_flags = tvb_get_letohs(tvb, offset);
1180                         proto_tree_add_text(tree, tvb, offset, 2, "Flags: 0x%x", collate_flags);
1181                         offset += 2;
1182                         collate_charset_id = tvb_get_guint8(tvb, offset);
1183                         proto_tree_add_text(tree, tvb, offset, 1, "Charset ID: %u", collate_charset_id);
1184                         offset +=1;
1185                 }
1186         }
1187
1188         proto_tree_add_text(tree, tvb, old_len_offset, 1, "Old Value Length: %u",
1189             old_len);
1190         if (old_len) {
1191                 string_offset = old_len_offset + 1;
1192                 if (is_unicode == TRUE) {
1193                         old_val = ep_tvb_fake_unicode(tvb, string_offset,
1194                             old_len, TRUE);
1195                         old_len *= 2;
1196                 } else
1197                         old_val = ep_tvb_get_string(tvb, string_offset, old_len);
1198                 proto_tree_add_text(tree, tvb, string_offset, old_len,
1199                     "Old Value: %s", old_val);
1200          }
1201 }
1202
1203 static void
1204 dissect_tds_err_token(tvbuff_t *tvb, guint offset, guint token_sz _U_, proto_tree *tree)
1205 {
1206         guint16 msg_len;
1207         guint8 srvr_len, proc_len;
1208         char *msg;
1209         gboolean is_unicode = FALSE;
1210
1211         proto_tree_add_text(tree, tvb, offset, 4, "SQL Error Number: %d", tds_tvb_get_xxtohl(tvb, offset, tds_little_endian));
1212         offset += 4;
1213         proto_tree_add_text(tree, tvb, offset, 1, "State: %u", tvb_get_guint8(tvb, offset));
1214         offset +=1;
1215         proto_tree_add_text(tree, tvb, offset, 1, "Severity Level: %u", tvb_get_guint8(tvb, offset));
1216         offset +=1;
1217
1218         msg_len = tds_tvb_get_xxtohs(tvb, offset, tds_little_endian);
1219         proto_tree_add_text(tree, tvb, offset, 1, "Error message length: %u characters", msg_len);
1220         offset +=2;
1221
1222         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 */
1223                 is_unicode = TRUE;
1224
1225         if(is_unicode) {
1226                 msg = ep_tvb_fake_unicode(tvb, offset, msg_len, TRUE);
1227                 msg_len *= 2;
1228         } else {
1229                 msg = ep_tvb_get_string(tvb, offset, msg_len);
1230         }
1231         proto_tree_add_text(tree, tvb, offset, msg_len, "Error: %s", format_text(msg, strlen(msg)));
1232         offset += msg_len;
1233         
1234         srvr_len = tvb_get_guint8(tvb, offset);
1235         
1236         proto_tree_add_text(tree, tvb, offset, 1, "Server name length: %u characters", srvr_len);
1237         offset +=1;
1238         if(srvr_len) {
1239                 if (is_unicode) {
1240                         msg = ep_tvb_fake_unicode(tvb, offset, srvr_len, TRUE);
1241                         srvr_len *=2;
1242                 } else {
1243                         msg = ep_tvb_get_string(tvb, offset, srvr_len);
1244                 }
1245                 proto_tree_add_text(tree, tvb, offset, srvr_len, "Server name: %s", msg);
1246                 offset += srvr_len;
1247         }
1248
1249         proc_len = tvb_get_guint8(tvb, offset);
1250         
1251         proto_tree_add_text(tree, tvb, offset, 1, "Process name length: %u characters", proc_len);
1252         offset +=1;
1253         if(proc_len) {
1254                 if (is_unicode) {
1255                         msg = ep_tvb_fake_unicode(tvb, offset, proc_len, TRUE);
1256                         proc_len *=2;
1257                 } else {
1258                         msg = ep_tvb_get_string(tvb, offset, proc_len);
1259                 }
1260                 proto_tree_add_text(tree, tvb, offset, proc_len, "Process name: %s", msg);
1261                 offset += proc_len;
1262         }
1263
1264         proto_tree_add_text(tree, tvb, offset, 2, "line number: %d", tds_tvb_get_xxtohs(tvb, offset, tds_little_endian));
1265 }
1266
1267 static void
1268 dissect_tds_login_ack_token(tvbuff_t *tvb, guint offset, guint token_sz, proto_tree *tree)
1269 {
1270         guint8 msg_len;
1271         char *msg;
1272         gboolean is_unicode = FALSE;
1273
1274         proto_tree_add_text(tree, tvb, offset, 1, "Ack: %u", tvb_get_guint8(tvb, offset));
1275         offset +=1;
1276         proto_tree_add_text(tree, tvb, offset, 1, "Major version (may be incorrect): %d", tvb_get_guint8(tvb, offset));
1277         offset +=1;
1278         proto_tree_add_text(tree, tvb, offset, 1, "Minor version (may be incorrect): %d", tvb_get_guint8(tvb, offset));
1279         offset +=1;
1280         proto_tree_add_text(tree, tvb, offset, 2, "zero usually");
1281         offset +=2;
1282
1283         msg_len = tvb_get_guint8(tvb, offset);
1284         proto_tree_add_text(tree, tvb, offset, 1, "Text length: %u characters", msg_len);
1285         offset +=1;
1286
1287         if(msg_len + 6U + 3U != token_sz - 1) /* 6 is the length of ack(1), version (4), text length (1) fields */
1288                 is_unicode = TRUE;
1289         proto_tree_add_text(tree, tvb, offset, 0, "msg_len: %d, token_sz: %d, total: %d",msg_len, token_sz, msg_len + 6U + 3U);
1290         if(is_unicode) {
1291                 msg = ep_tvb_fake_unicode(tvb, offset, msg_len, TRUE);
1292                 msg_len *= 2;
1293         } else {
1294                 msg = ep_tvb_get_string(tvb, offset, msg_len);
1295         }
1296         proto_tree_add_text(tree, tvb, offset, msg_len, "Text: %s", format_text(msg, strlen(msg)));
1297         offset += msg_len;
1298         
1299         proto_tree_add_text(tree, tvb, offset, 4, "Server Version");
1300         offset += 4;
1301 }
1302
1303 static int 
1304 dissect_tds7_results_token(tvbuff_t *tvb, guint offset, proto_tree *tree)
1305 {
1306         guint16 num_columns, table_len;
1307         guint8 type, msg_len;
1308         int i;
1309         char *msg;
1310         guint16 collate_codepage, collate_flags;
1311         guint8 collate_charset_id;
1312
1313         num_columns = tvb_get_letohs(tvb, offset);
1314         proto_tree_add_text(tree, tvb, offset, 2, "Columns: %u", tvb_get_letohs(tvb, offset));
1315         offset +=2;
1316         for(i=0; i != num_columns; i++) {
1317                 proto_tree_add_text(tree, tvb, offset, 0, "Column %d", i + 1);
1318                 proto_tree_add_text(tree, tvb, offset, 2, "usertype: %d", tvb_get_letohs(tvb, offset));
1319                 offset +=2;
1320                 proto_tree_add_text(tree, tvb, offset, 2, "flags: %d", tvb_get_letohs(tvb, offset));
1321                 offset +=2;
1322                 type  = tvb_get_guint8(tvb, offset);
1323                 proto_tree_add_text(tree, tvb, offset, 1, "Type: %d", type);
1324                 offset +=1;
1325                 if(type == 38 || type == 104) { /* ugly, ugly hack. Wish I knew what it really means!*/
1326                         proto_tree_add_text(tree, tvb, offset, 1, "unknown 1 byte (%x)", tvb_get_guint8(tvb, offset));
1327                         offset +=1;
1328                 }
1329                 else if (type == 35) {
1330                         proto_tree_add_text(tree, tvb, offset, 4, "unknown 4 bytes (%x)", tvb_get_letohl(tvb, offset));
1331                         offset += 4;
1332                         collate_codepage = tvb_get_letohs(tvb, offset);
1333                         proto_tree_add_text(tree, tvb, offset, 2, "Codepage: %u" , collate_codepage);
1334                         offset += 2;
1335                         collate_flags = tvb_get_letohs(tvb, offset);
1336                         proto_tree_add_text(tree, tvb, offset, 2, "Flags: 0x%x", collate_flags);
1337                         offset += 2;
1338                         collate_charset_id = tvb_get_guint8(tvb, offset);
1339                         proto_tree_add_text(tree, tvb, offset, 1, "Charset ID: %u", collate_charset_id);
1340                         offset +=1;
1341                         table_len = tvb_get_letohs(tvb, offset);
1342                         offset +=2;
1343                         if(table_len != 0) {
1344                                 msg = ep_tvb_fake_unicode(tvb, offset, table_len, TRUE);
1345                                 proto_tree_add_text(tree, tvb, offset, table_len*2, "Table name: %s", msg);
1346                                 offset += table_len*2;
1347                         }
1348                 }
1349                 else if (type == 106) {
1350                         proto_tree_add_text(tree, tvb, offset, 3, "unknown 3 bytes");
1351                         offset +=3;
1352                 }
1353                 if(type > 128) {
1354                         proto_tree_add_text(tree, tvb, offset, 2, "Large type size: 0x%x", tvb_get_letohs(tvb, offset));
1355                         offset += 2;
1356                         collate_codepage = tvb_get_letohs(tvb, offset);
1357                         proto_tree_add_text(tree, tvb, offset, 2, "Codepage: %u" , collate_codepage);
1358                         offset += 2;
1359                         collate_flags = tvb_get_letohs(tvb, offset);
1360                         proto_tree_add_text(tree, tvb, offset, 2, "Flags: 0x%x", collate_flags);
1361                         offset += 2;
1362                         collate_charset_id = tvb_get_guint8(tvb, offset);
1363                         proto_tree_add_text(tree, tvb, offset, 1, "Charset ID: %u", collate_charset_id);
1364                         offset +=1;
1365                 }
1366                 msg_len = tvb_get_guint8(tvb, offset);
1367                 proto_tree_add_text(tree, tvb, offset, 1, "message length: %d",msg_len);
1368                 offset += 1;
1369                 if(msg_len != 0) {
1370                         msg = ep_tvb_fake_unicode(tvb, offset, msg_len, TRUE);
1371                         proto_tree_add_text(tree, tvb, offset, msg_len*2, "Text: %s", msg);
1372                         offset += msg_len*2;
1373                 }
1374         }
1375         return offset;
1376 }
1377
1378 static void
1379 dissect_tds_done_token(tvbuff_t *tvb, guint offset, proto_tree *tree)
1380 {
1381         proto_tree_add_text(tree, tvb, offset, 2, "Status flags");
1382         offset += 2;
1383         proto_tree_add_text(tree, tvb, offset, 2, "Operation");
1384         offset += 2;
1385         proto_tree_add_text(tree, tvb, offset, 4, "row count: %u", tds_tvb_get_xxtohl(tvb, offset, tds_little_endian));
1386         offset += 2;
1387 }
1388
1389 static void
1390 dissect_tds_rpc(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
1391 {
1392         int offset = 0;
1393         guint len;
1394         guint16 sp_id;
1395         char *val;
1396
1397         /*
1398          * RPC name.
1399          */
1400         switch(tds_protocol_type) {
1401                 case TDS_PROTOCOL_4:
1402                         len = tvb_get_guint8(tvb, offset);
1403                         proto_tree_add_text(tree, tvb, offset, 1, "RPC Name Length: %u", len);
1404                         offset += 1;
1405                         val = ep_tvb_get_string(tvb, offset, len);
1406                         proto_tree_add_text(tree, tvb, offset, len, "RPC Name: %s", val);
1407                         offset += len;
1408                         break;
1409
1410                 case TDS_PROTOCOL_7:
1411                 case TDS_PROTOCOL_8:
1412                 default:          /* unspecified: try as if TDS7/TDS8 */
1413                         len = tvb_get_letohs(tvb, offset);
1414                         proto_tree_add_text(tree, tvb, offset, 2, "RPC Name Length: %u", len);
1415                         offset += 2;
1416                         if (len == 0xFFFF) {
1417                                 sp_id = tvb_get_letohs(tvb, offset);
1418                                 proto_tree_add_text(tree, tvb, offset, 2, "RPC Stored Proc ID: %u (%s)", 
1419                                     sp_id,
1420                                     val_to_str(sp_id, internal_stored_proc_id_names, "Unknown"));
1421                                 offset += 2;
1422                         }
1423                         else if (len != 0) {
1424                                 val = ep_tvb_fake_unicode(tvb, offset, len, TRUE);
1425                                 len *= 2;
1426                                 proto_tree_add_text(tree, tvb, offset, len, "RPC Name: %s", val);
1427                                 offset += len;
1428                         }
1429                         break;
1430         }
1431         proto_tree_add_text(tree, tvb, offset, -1, "Params (not dissected)");
1432 }
1433
1434 static void
1435 dissect_tds_resp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1436 {
1437         int offset = 0;
1438         proto_item *token_item;
1439         proto_tree *token_tree;
1440         guint pos, token_sz = 0;
1441         guint token_len_field_size = 2;
1442         guint token_len_field_val;
1443         guint8 token;
1444         struct _netlib_data nl_data;
1445         gint length_remaining;
1446
1447         g_mem_chunk_reset(tds_column);   /* in case exception thrown the previous time thru this code */
1448
1449         memset(&nl_data, '\0', sizeof nl_data);
1450
1451         /*
1452          * Until we reach the end of the packet, read tokens.
1453          */
1454         pos = offset;
1455         while (tvb_reported_length_remaining(tvb, pos) > 0) {
1456                 /* our token */
1457                 token = tvb_get_guint8(tvb, pos);
1458
1459                 /* TODO Handle TDS_PARAMFMT, TDS_PARAMS [similar to TDS_RESULTS, TDS_ROW] */
1460                 if (tds_token_is_fixed_size(token)) {
1461                         token_sz = tds_get_fixed_token_size(token) + 1;
1462                 } else if (token == TDS_ROW_TOKEN) {
1463                         /*
1464                          * Rows are special; they have no size field and
1465                          * aren't fixed length.
1466                          */
1467                         token_sz = tds_get_row_size(tvb, &nl_data, pos + 1);
1468                 } else
1469                         token_sz = tds_get_variable_token_size(tvb, pos + 1,
1470                             token, &token_len_field_size, &token_len_field_val);
1471
1472                 length_remaining = tvb_ensure_length_remaining(tvb, pos);
1473
1474                 if ((int) token_sz < 0) {
1475                     proto_tree_add_text(tree, tvb, pos, 0, "Bogus token size: %u",
1476                         token_sz);
1477                     break;
1478                 }
1479                 if ((int) token_len_field_size < 0) {
1480                     proto_tree_add_text(tree, tvb, pos, 0, "Bogus token length field size: %u",
1481                         token_len_field_size);
1482                     break;
1483                 }
1484                 token_item = proto_tree_add_text(tree, tvb, pos, token_sz,
1485                     "Token 0x%02x %s", token,
1486                     val_to_str(token, token_names, "Unknown Token Type"));
1487                 token_tree = proto_item_add_subtree(token_item, ett_tds_token);
1488
1489                 /*
1490                  * If it's a variable token, put the length field in here
1491                  * instead of replicating this for each token subdissector.
1492                  */
1493                 if (!tds_token_is_fixed_size(token) && token != TDS_ROW_TOKEN) {
1494                         proto_tree_add_text(token_tree, tvb, pos + 1,
1495                             token_len_field_size, "Length: %u",
1496                             token_len_field_val);
1497                 }
1498
1499                 if (token_sz > (guint)length_remaining)
1500                         token_sz = (guint)length_remaining;
1501
1502                 switch (token) {
1503
1504                 case TDS_COL_NAME_TOKEN:
1505                         /*
1506                          * TDS 4.2
1507                          * TODO dissect token to get "column names" to fill in _netlib_data
1508                          */
1509                         break;
1510
1511                 case TDS_COL_INFO_TOKEN:
1512                         /*
1513                          * TDS 4.2: get the column info 
1514                          */
1515                         dissect_tds_col_info_token(tvb, &nl_data, pos);
1516                         break;
1517
1518                 case TDS_RESULT_TOKEN:
1519                         /*
1520                          * If it's a result token, we need to stash the
1521                          * column info.
1522                          */
1523                         read_results_tds5(tvb, &nl_data, pos + 3, token_sz - 3);
1524                         break;
1525
1526                 case TDS_ENV_CHG_TOKEN:
1527                         dissect_tds_env_chg(tvb, pos + 3, token_sz - 3, token_tree);
1528                         break;
1529
1530                 case TDS_AUTH_TOKEN:
1531                         dissect_tds_nt(tvb, pinfo, token_tree, pos + 3, token_sz - 3);
1532                         break;
1533                 case TDS_ERR_TOKEN:
1534                 case TDS_MSG_TOKEN:
1535                         dissect_tds_err_token(tvb, pos + 3, token_sz - 3, token_tree);
1536                         break;
1537
1538                 case TDS_DONE_TOKEN:
1539                 case TDS_DONEPROC_TOKEN:
1540                 case TDS_DONEINPROC_TOKEN:
1541                         dissect_tds_done_token(tvb, pos + 1, token_tree);
1542                         break;
1543                 case TDS_LOGIN_ACK_TOKEN:
1544                         dissect_tds_login_ack_token(tvb, pos + 3, token_sz - 3, token_tree);
1545                         break;
1546                 case TDS7_RESULT_TOKEN:
1547                         pos = (dissect_tds7_results_token(tvb, pos + 1, token_tree)-1);
1548                         break;
1549                 }
1550
1551                 /* and step to the end of the token, rinse, lather, repeat */
1552                 pos += token_sz;
1553         }
1554         /* reset tds_column mem_chunk since finished processing response */
1555         /*  (pointers to atoms in nl_data being discarded) */
1556         g_mem_chunk_reset(tds_column);
1557 }
1558
1559 static void
1560 dissect_netlib_buffer(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1561 {
1562         int offset = 0;
1563         proto_item *tds_item = NULL;
1564         proto_tree *tds_tree = NULL;
1565         guint8 type;
1566         guint8 status;
1567         guint16 size;
1568         guint16 channel;
1569         guint8 packet_number;
1570         gboolean save_fragmented;
1571         int len;
1572         fragment_data *fd_head;
1573         tvbuff_t *next_tvb;
1574
1575         if (tree) {
1576                 /* create display subtree for the protocol */
1577                 tds_item = proto_tree_add_item(tree, proto_tds, tvb, offset, -1,
1578                     FALSE);
1579
1580                 tds_tree = proto_item_add_subtree(tds_item, ett_tds);
1581         }
1582         type = tvb_get_guint8(tvb, offset);
1583         if (tree) {
1584                 proto_tree_add_uint(tds_tree, hf_tds_type, tvb, offset, 1,
1585                     type);
1586         }
1587         status = tvb_get_guint8(tvb, offset + 1);
1588         if (tree) {
1589                 proto_tree_add_uint(tds_tree, hf_tds_status, tvb, offset + 1, 1,
1590                     status);
1591         }
1592         size = tvb_get_ntohs(tvb, offset + 2);
1593         if (tree) {
1594                 proto_tree_add_uint(tds_tree, hf_tds_size, tvb, offset + 2, 2,
1595                         size);
1596         }
1597         channel = tvb_get_ntohs(tvb, offset + 4);
1598         if (tree) {
1599                 proto_tree_add_uint(tds_tree, hf_tds_channel, tvb, offset + 4, 2,
1600                         channel);
1601         }
1602         packet_number = tvb_get_guint8(tvb, offset + 6);
1603         if (tree) {
1604                 proto_tree_add_uint(tds_tree, hf_tds_packet_number, tvb, offset + 6, 1,
1605                         packet_number);
1606                 proto_tree_add_item(tds_tree, hf_tds_window, tvb, offset + 7, 1,
1607                         FALSE);
1608         }
1609         offset += 8;    /* skip Netlib header */
1610
1611         /*
1612          * Deal with fragmentation.
1613          *
1614          * TODO: handle case where netlib headers 'packet-number'.is always 0
1615          *       use fragment_add_seq_next in this case ?
1616          *       
1617          */
1618         save_fragmented = pinfo->fragmented;
1619         if (tds_defragment &&
1620             (packet_number > 1 || status == STATUS_NOT_LAST_BUFFER)) {
1621                 if (status == STATUS_NOT_LAST_BUFFER) {
1622                         if (check_col(pinfo->cinfo, COL_INFO))
1623                                 col_append_str(pinfo->cinfo, COL_INFO,
1624                                     " (Not last buffer)");
1625                 }
1626                 len = tvb_reported_length_remaining(tvb, offset);
1627                 /*
1628                  * XXX - I've seen captures that start with a login
1629                  * packet with a sequence number of 2.
1630                  */
1631                 fd_head = fragment_add_seq_check(tvb, offset, pinfo, channel,
1632                     tds_fragment_table, tds_reassembled_table,
1633                     packet_number - 1, len, status == STATUS_NOT_LAST_BUFFER);
1634                 next_tvb = process_reassembled_data(tvb, offset, pinfo,
1635                     "Reassembled TDS", fd_head, &tds_frag_items, NULL,
1636                     tds_tree);
1637         } else {
1638                 /*
1639                  * If this isn't the last buffer, just show it as a fragment.
1640                  * (XXX - it'd be nice to dissect it if it's the first
1641                  * buffer, but we'd need to do reassembly in order to
1642                  * discover that.)
1643                  *
1644                  * If this is the last buffer, dissect it.
1645                  * (XXX - it'd be nice to show it as a fragment if it's part
1646                  * of a fragmented message, but we'd need to do reassembly
1647                  * in order to discover that.)
1648                  */
1649                 if (status == STATUS_NOT_LAST_BUFFER)
1650                         next_tvb = NULL;
1651                 else {
1652                         next_tvb = tvb_new_subset(tvb, offset, -1, -1);
1653                 }
1654         }
1655
1656         if (next_tvb != NULL) {
1657
1658                 switch (type) {
1659
1660                 case TDS_RPC_PKT:
1661                         dissect_tds_rpc(next_tvb, pinfo, tds_tree);
1662                         break;
1663
1664                 case TDS_RESP_PKT:
1665                         dissect_tds_resp(next_tvb, pinfo, tds_tree);
1666                         break;
1667
1668                 case TDS_LOGIN7_PKT:
1669                         dissect_tds7_login(next_tvb, pinfo, tds_tree);
1670                         break;
1671                 case TDS_QUERY_PKT:
1672                         dissect_tds_query_packet(next_tvb, pinfo, tds_tree);
1673                         break;
1674                 case TDS_QUERY5_PKT:
1675                         dissect_tds_query5_packet(next_tvb, pinfo, tds_tree);
1676                         break;
1677                 case TDS_NTLMAUTH_PKT:
1678                         dissect_tds_nt(next_tvb, pinfo, tds_tree, offset - 8, -1);
1679                         break;
1680                 default:
1681                         proto_tree_add_text(tds_tree, next_tvb, 0, -1,
1682                             "TDS Packet");
1683                         break;
1684                 }
1685         } else {
1686                 next_tvb = tvb_new_subset (tvb, offset, -1, -1);
1687                 call_dissector(data_handle, next_tvb, pinfo, tds_tree);
1688         }
1689 }
1690
1691 static void
1692 dissect_tds_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1693 {
1694         volatile gboolean first_time = TRUE;
1695         volatile int offset = 0;
1696         guint length_remaining;
1697         guint8 type;
1698         guint16 plen;
1699         guint length;
1700         tvbuff_t *next_tvb;
1701         proto_item *tds_item = NULL;
1702         proto_tree *tds_tree = NULL;
1703
1704         while (tvb_reported_length_remaining(tvb, offset) != 0) {
1705                 length_remaining = tvb_ensure_length_remaining(tvb, offset);
1706
1707                 /*
1708                  * Can we do reassembly?
1709                  */
1710                 if (tds_desegment && pinfo->can_desegment) {
1711                         /*
1712                          * Yes - is the fixed-length part of the PDU
1713                          * split across segment boundaries?
1714                          */
1715                         if (length_remaining < 8) {
1716                                 /*
1717                                  * Yes.  Tell the TCP dissector where the
1718                                  * data for this message starts in the data
1719                                  * it handed us, and how many more bytes we
1720                                  * need, and return.
1721                                  */
1722                                 pinfo->desegment_offset = offset;
1723                                 pinfo->desegment_len = 8 - length_remaining;
1724                                 return;
1725                         }
1726                 }
1727
1728                 type = tvb_get_guint8(tvb, offset);
1729
1730                 /*
1731                  * Get the length of the PDU.
1732                  */
1733                 plen = tvb_get_ntohs(tvb, offset + 2);
1734                 if (plen < 8) {
1735                         /*
1736                          * The length is less than the header length.
1737                          * Put in the type, status, and length, and
1738                          * report the length as bogus.
1739                          */
1740                         if (tree) {
1741                                 /* create display subtree for the protocol */
1742                                 tds_item = proto_tree_add_item(tree, proto_tds,
1743                                     tvb, offset, -1, FALSE);
1744
1745                                 tds_tree = proto_item_add_subtree(tds_item,
1746                                     ett_tds);
1747                                 proto_tree_add_uint(tds_tree, hf_tds_type, tvb,
1748                                     offset, 1, type);
1749                                 proto_tree_add_item(tds_tree, hf_tds_status,
1750                                     tvb, offset + 1, 1, FALSE);
1751                                 proto_tree_add_uint_format(tds_tree,
1752                                     hf_tds_size, tvb, offset + 2, 2, plen,
1753                                     "Size: %u (bogus, should be >= 8)", plen);
1754                         }
1755
1756                         /*
1757                          * Give up - we can't dissect any more of this
1758                          * data.
1759                          */
1760                         break;
1761                 }
1762
1763                 /*
1764                  * Can we do reassembly?
1765                  */
1766                 if (tds_desegment && pinfo->can_desegment) {
1767                         /*
1768                          * Yes - is the PDU split across segment boundaries?
1769                          */
1770                         if (length_remaining < plen) {
1771                                 /*
1772                                  * Yes.  Tell the TCP dissector where the
1773                                  * data for this message starts in the data
1774                                  * it handed us, and how many more bytes we
1775                                  * need, and return.
1776                                  */
1777                                 pinfo->desegment_offset = offset;
1778                                 pinfo->desegment_len = plen - length_remaining;
1779                                 return;
1780                         }
1781                 }
1782
1783                 if (first_time) {
1784                         if (check_col(pinfo->cinfo, COL_PROTOCOL))
1785                                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TDS");
1786
1787                         /*
1788                          * Set the packet description based on its TDS packet
1789                          * type.
1790                          */
1791                         if (check_col(pinfo->cinfo, COL_INFO)) {
1792                                 col_add_str(pinfo->cinfo, COL_INFO,
1793                                     val_to_str(type, packet_type_names,
1794                                       "Unknown Packet Type: %u"));
1795                         }
1796                         first_time = FALSE;
1797                 }
1798
1799                 /*
1800                  * Construct a tvbuff containing the amount of the payload
1801                  * we have available.  Make its reported length the amount
1802                  * of data in the PDU.
1803                  *
1804                  * XXX - if reassembly isn't enabled. the subdissector will
1805                  * throw a BoundsError exception, rather than a
1806                  * ReportedBoundsError exception.  We really want a tvbuff
1807                  * where the length is "length", the reported length is
1808                  * "plen", and the "if the snapshot length were infinite"
1809                  * length is the minimum of the reported length of the tvbuff
1810                  * handed to us and "plen", with a new type of exception
1811                  * thrown if the offset is within the reported length but
1812                  * beyond that third length, with that exception getting the
1813                  * "Unreassembled Packet" error.
1814                  */
1815                 length = length_remaining;
1816                 if (length > plen)
1817                         length = plen;
1818                 next_tvb = tvb_new_subset(tvb, offset, length, plen);
1819
1820                 /*
1821                  * Dissect the Netlib buffer.
1822                  *
1823                  * Catch the ReportedBoundsError exception; if this
1824                  * particular Netlib buffer happens to get a
1825                  * ReportedBoundsError exception, that doesn't mean
1826                  * that we should stop dissecting PDUs within this frame
1827                  * or chunk of reassembled data.
1828                  *
1829                  * If it gets a BoundsError, we can stop, as there's nothing
1830                  * more to see, so we just re-throw it.
1831                  */
1832                 TRY {
1833                         dissect_netlib_buffer(next_tvb, pinfo, tree);
1834                 }
1835                 CATCH(BoundsError) {
1836                         RETHROW;
1837                 }
1838                 CATCH(ReportedBoundsError) {
1839                         show_reported_bounds_error(tvb, pinfo, tree);
1840                 }
1841                 ENDTRY;
1842
1843                 /*
1844                  * Step to the next Netlib buffer.
1845                  */
1846                 offset += plen;
1847         }
1848 }
1849
1850 static gboolean
1851 dissect_tds_tcp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1852 {
1853         int offset = 0;
1854         guint8 type;
1855         guint8 status;
1856         guint16 plen;
1857         conversation_t *conv;
1858
1859         /*
1860          * If we don't have even enough data for a Netlib header,
1861          * just say it's not TDS.
1862          */
1863         if (!tvb_bytes_exist(tvb, offset, 8))
1864                 return FALSE;
1865
1866         /*
1867          * Quickly scan all the data we have in order to see if
1868          * everything in it looks like Netlib traffic.
1869          */
1870         while (tvb_bytes_exist(tvb, offset, 1)) {
1871                 /*
1872                  * Check the type field.
1873                  */
1874                 type = tvb_get_guint8(tvb, offset);
1875                 if (!is_valid_tds_type(type))
1876                         return FALSE;
1877
1878                 /*
1879                  * Check the status field, if it's present.
1880                  */
1881                 if (!tvb_bytes_exist(tvb, offset + 1, 1))
1882                         break;
1883                 status = tvb_get_guint8(tvb, offset + 1);
1884                 if (!is_valid_tds_status(status))
1885                         return FALSE;
1886
1887                 /*
1888                  * Get the length of the PDU.
1889                  */
1890                 if (!tvb_bytes_exist(tvb, offset + 2, 2))
1891                         break;
1892                 plen = tvb_get_ntohs(tvb, offset + 2);
1893                 if (plen < 8) {
1894                         /*
1895                          * The length is less than the header length.
1896                          * That's bogus.
1897                          */
1898                         return FALSE;
1899                 }
1900
1901                 /*
1902                  * If we're at the beginning of the segment, check the
1903                  * payload if it's a login packet.
1904                  */
1905                 if (offset == 0) {
1906                         if (!netlib_check_login_pkt(tvb, offset, pinfo, type))
1907                                 return FALSE;
1908                 }
1909
1910                 /*
1911                  * Step to the next Netlib buffer.
1912                  */
1913                 offset += plen;
1914         }
1915
1916         /*
1917          * OK, it passes the test; assume the rest of this conversation
1918          * is TDS.
1919          */
1920         conv = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype,
1921             pinfo->srcport, pinfo->destport, 0);
1922         if (conv == NULL) {
1923                 /*
1924                  * No conversation exists yet - create one.
1925                  */
1926                 conv = conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst,
1927                     pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
1928         }
1929         conversation_set_dissector(conv, tds_tcp_handle);
1930
1931         /*
1932          * Now dissect it as TDS.
1933          */
1934         dissect_tds_tcp(tvb, pinfo, tree);
1935         return TRUE;
1936 }
1937
1938 static void
1939 tds_init(void)
1940 {
1941         /*
1942          * Initialize the fragment and reassembly tables.
1943          */
1944         fragment_table_init(&tds_fragment_table);
1945         reassembled_table_init(&tds_reassembled_table);
1946
1947         /*
1948          * Reinitialize the chunks of data for remembering row
1949          * information.
1950          */
1951         if (tds_column)
1952                 g_mem_chunk_destroy(tds_column);
1953
1954         tds_column = g_mem_chunk_new("tds_column", tds_column_length,
1955                 tds_column_init_count * tds_column_length,
1956                 G_ALLOC_AND_FREE);
1957 }
1958
1959 /* Register the protocol with Ethereal */
1960
1961 /* this format is required because a script is used to build the C function
1962    that calls all the protocol registration.
1963 */
1964
1965 void
1966 proto_register_netlib(void)
1967 {
1968         static hf_register_info hf[] = {
1969                 { &hf_tds_type,
1970                         { "Type",           "tds.type",
1971                         FT_UINT8, BASE_HEX, VALS(packet_type_names), 0x0,
1972                         "Packet Type", HFILL }
1973                 },
1974                 { &hf_tds_status,
1975                         { "Status",         "tds.status",
1976                         FT_UINT8, BASE_DEC, VALS(status_names), 0x0,
1977                         "Frame status", HFILL }
1978                 },
1979                 { &hf_tds_size,
1980                         { "Size",           "tds.size",
1981                         FT_UINT16, BASE_DEC, NULL, 0x0,
1982                         "Packet Size", HFILL }
1983                 },
1984                 { &hf_tds_channel,
1985                         { "Channel",        "tds.channel",
1986                         FT_UINT16, BASE_DEC, NULL, 0x0,
1987                         "Channel Number", HFILL }
1988                 },
1989                 { &hf_tds_packet_number,
1990                         { "Packet Number",  "tds.packet_number",
1991                         FT_UINT8, BASE_DEC, NULL, 0x0,
1992                         "Packet Number", HFILL }
1993                 },
1994                 { &hf_tds_window,
1995                         { "Window",         "tds.window",
1996                         FT_UINT8, BASE_DEC, NULL, 0x0,
1997                         "Window", HFILL }
1998                 },
1999                 { &hf_tds_fragment_overlap,
2000                         { "Segment overlap",    "tds.fragment.overlap",
2001                         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2002                         "Fragment overlaps with other fragments", HFILL }
2003                 },
2004                 { &hf_tds_fragment_overlap_conflict,
2005                         { "Conflicting data in fragment overlap", "tds.fragment.overlap.conflict",
2006                         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2007                         "Overlapping fragments contained conflicting data", HFILL }
2008                 },
2009                 { &hf_tds_fragment_multiple_tails,
2010                         { "Multiple tail fragments found", "tds.fragment.multipletails",
2011                         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2012                         "Several tails were found when defragmenting the packet", HFILL }
2013                 },
2014                 { &hf_tds_fragment_too_long_fragment,
2015                         { "Segment too long",   "tds.fragment.toolongfragment",
2016                         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2017                         "Segment contained data past end of packet", HFILL }
2018                 },
2019                 { &hf_tds_fragment_error,
2020                         { "Defragmentation error",      "tds.fragment.error",
2021                         FT_FRAMENUM, BASE_NONE, NULL, 0x0,
2022                         "Defragmentation error due to illegal fragments", HFILL }
2023                 },
2024                 { &hf_tds_fragment,
2025                         { "TDS Fragment",       "tds.fragment",
2026                         FT_FRAMENUM, BASE_NONE, NULL, 0x0,
2027                         "TDS Fragment", HFILL }
2028                 },
2029                 { &hf_tds_fragments,
2030                         { "TDS Fragments",      "tds.fragments",
2031                         FT_NONE, BASE_NONE, NULL, 0x0,
2032                         "TDS Fragments", HFILL }
2033                 },
2034                 { &hf_tds_reassembled_in,
2035                         { "Reassembled TDS in frame", "tds.reassembled_in",
2036                         FT_FRAMENUM, BASE_NONE, NULL, 0x0,
2037                         "This TDS packet is reassembled in this frame", HFILL }
2038                 },
2039                 { &hf_tds7_login_total_size,
2040                         { "Total Packet Length", "tds7login.total_len",
2041                         FT_UINT32, BASE_DEC, NULL, 0x0,
2042                         "TDS7 Login Packet total packet length", HFILL }
2043                 },
2044                 { &hf_tds7_version,
2045                         { "TDS version", "tds7login.version",
2046                         FT_UINT32, BASE_HEX, NULL, 0x0,
2047                         "TDS version", HFILL }
2048                 },
2049                 { &hf_tds7_packet_size,
2050                         { "Packet Size", "tds7login.packet_size",
2051                         FT_UINT32, BASE_DEC, NULL, 0x0,
2052                         "Packet size", HFILL }
2053                 },
2054                 { &hf_tds7_client_version,
2055                         { "Client version", "tds7login.client_version",
2056                         FT_UINT32, BASE_DEC, NULL, 0x0,
2057                         "Client version", HFILL }
2058                 },
2059                 { &hf_tds7_client_pid,
2060                         { "Client PID", "tds7login.client_pid",
2061                         FT_UINT32, BASE_DEC, NULL, 0x0,
2062                         "Client PID", HFILL }
2063                 },
2064                 { &hf_tds7_connection_id,
2065                         { "Connection ID", "tds7login.connection_id",
2066                         FT_UINT32, BASE_DEC, NULL, 0x0,
2067                         "Connection ID", HFILL }
2068                 },
2069                 { &hf_tds7_option_flags1,
2070                         { "Option Flags 1", "tds7login.option_flags1",
2071                         FT_UINT8, BASE_HEX, NULL, 0x0,
2072                         "Option Flags 1", HFILL }
2073                 },
2074                 { &hf_tds7_option_flags2,
2075                         { "Option Flags 2", "tds7login.option_flags2",
2076                         FT_UINT8, BASE_HEX, NULL, 0x0,
2077                         "Option Flags 2", HFILL }
2078                 },
2079                 { &hf_tds7_sql_type_flags,
2080                         { "SQL Type Flags", "tds7login.sql_type_flags",
2081                         FT_UINT8, BASE_HEX, NULL, 0x0,
2082                         "SQL Type Flags", HFILL }
2083                 },
2084                 { &hf_tds7_reserved_flags,
2085                         { "Reserved Flags", "tds7login.reserved_flags",
2086                         FT_UINT8, BASE_HEX, NULL, 0x0,
2087                         "reserved flags", HFILL }
2088                 },
2089                 { &hf_tds7_time_zone,
2090                         { "Time Zone", "tds7login.time_zone",
2091                         FT_UINT32, BASE_HEX, NULL, 0x0,
2092                         "Time Zone", HFILL }
2093                 },
2094                 { &hf_tds7_collation,
2095                         { "Collation", "tds7login.collation",
2096                         FT_UINT32, BASE_HEX, NULL, 0x0,
2097                         "Collation", HFILL }
2098                 },
2099                 { &hf_tds7_message,
2100                         { "Message", "tds7.message", 
2101                         FT_STRING, BASE_NONE, NULL, 0x0, 
2102                         "", HFILL }
2103                 },
2104         };
2105
2106         static gint *ett[] = {
2107                 &ett_tds,
2108                 &ett_tds_fragments,
2109                 &ett_tds_fragment,
2110                 &ett_tds_token,
2111                 &ett_tds7_login,
2112                 &ett_tds7_hdr,
2113         };
2114         module_t *tds_module;
2115
2116 /* Register the protocol name and description */
2117         proto_tds = proto_register_protocol("Tabular Data Stream",
2118             "TDS", "tds");
2119
2120 /* Required function calls to register the header fields and subtrees used */
2121         proto_register_field_array(proto_tds, hf, array_length(hf));
2122         proto_register_subtree_array(ett, array_length(ett));
2123
2124         tds_module = prefs_register_protocol(proto_tds, NULL);
2125         prefs_register_bool_preference(tds_module, "desegment_buffers",
2126             "Reassemble TDS buffers spanning multiple TCP segments",
2127             "Whether the TDS dissector should reassemble TDS buffers spanning multiple TCP segments. "
2128             "To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
2129             &tds_desegment);
2130         prefs_register_bool_preference(tds_module, "defragment",
2131             "Reassemble fragmented TDS messages with multiple buffers",
2132             "Whether the TDS dissector should defragment messages spanning multiple Netlib buffers",
2133             &tds_defragment);
2134         prefs_register_enum_preference(tds_module, "protocol_type",
2135             "TDS Protocol Type",
2136             "Hint as to version of TDS protocol being decoded",
2137             &tds_protocol_type, tds_protocol_type_options, FALSE);
2138         prefs_register_enum_preference(tds_module, "endian_type",
2139             "TDS decode as",
2140             "Hint as to whether to decode TDS protocol as little-endian or big-endian. (TDS7/8 always decoded as little-endian)",
2141             &tds_little_endian, tds_endian_type_options, FALSE);
2142         prefs_register_range_preference(tds_module, "tcp_ports",
2143             "TDS TCP ports",
2144             "Additional TCP ports to decode as TDS",
2145             &tds_tcp_ports, 0xFFFF);
2146
2147         register_init_routine(tds_init);
2148 }
2149
2150 /* If this dissector uses sub-dissector registration add a registration routine.
2151    This format is required because a script is used to find these routines and
2152    create the code that calls these routines.
2153 */
2154 void
2155 proto_reg_handoff_tds(void)
2156 {
2157         tds_tcp_handle = create_dissector_handle(dissect_tds_tcp, proto_tds);
2158
2159         /* Initial TDS ports: MS SQL default ports */
2160         dissector_add("tcp.port", 1433, tds_tcp_handle);
2161         dissector_add("tcp.port", 2433, tds_tcp_handle);
2162
2163         heur_dissector_add("tcp", dissect_tds_tcp_heur, proto_tds);
2164
2165         ntlmssp_handle = find_dissector("ntlmssp");
2166         gssapi_handle = find_dissector("gssapi");
2167         data_handle = find_dissector("data");
2168 }