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