Enable Lua tcp tap userdata.
[obnox/wireshark/wip.git] / epan / dissectors / packet-sqloracle.c
1 /* packet-sqloracle.c
2  * Routines for SQL ORcle packet dissection
3  *
4  * The initial Wireshark version of this file was imported from the
5  * ClearSight source code package.
6  * No author/copyright given in the original file.
7  *
8  * $Id$
9  *
10  * Wireshark - Network traffic analyzer
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 #define TCP_PORT_TNS 1522 /* XXX 1521 collides with packet-tns.c */
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <glib.h>
32 #include <ctype.h>
33 #include <string.h>
34 #include <epan/packet.h>
35 #include "packet-sqloracle.h"
36
37 #define SWAP_UI16(ui16) (((ui16)>>8 & 0xff) | ((ui16)<<8 & 0xff00))
38
39 /* option flag 1 */
40 #define OPTION_CANCEL 0x80
41 #define OPTION_FETCH  0x40
42 #define OPTION_EXCUTE  0x20
43 #define OPTION_DEFINE  0x10
44 #define OPTION_BIND  0x08
45 #define OPTION_DESC_BIND  0x04
46 #define OPTION_PARSE  0x02
47
48 /* option flag 2 */
49 #define OPTION_PLSQL 0x80
50 #define OPTION_PARSE2  0x40
51 #define OPTION_VECTOR  0x04
52 #define OPTION_FETCH2  0x02
53 #define OPTION_COMMIT  0x01
54
55
56
57 /* protocol */
58 static gint proto_sqloracle = -1;
59 static gint ett_sqloracle = -1;
60 static gint ett_sqloracle_operation = -1;
61
62 static int hf_sqloracle_operation = -1;
63 static int hf_sqloracle_func_type = -1;
64 /* static int hf_sqloracle_stmt_length = -1; */
65 static int hf_sqloracle_stmt = -1;
66 /* static int hf_sqloracle_v8ttluds_tname = -1;
67 static int hf_sqloracle_v8ttluds_sname = -1;
68 static int hf_sqloracle_v8ttluds_columnname = -1;
69 static int hf_sqloracle_v8ttluds_scrlength = -1;
70 static int hf_sqloracle_v8ttluds_nullallow = -1;
71 static int hf_sqloracle_v8ttluds_header= -1;
72 static int hf_sqloracle_v8ttloac_formuse = -1;
73 static int hf_sqloracle_v8ttloac_ncs = -1;
74 static int hf_sqloracle_v8ttloac_oid = -1;
75 static int hf_sqloracle_v8ttloac_header =-1;
76 static int hf_sqloracle_ttloac_flag2 = -1;
77 static int hf_sqloracle_ttloac_mal = -1;
78 static int hf_sqloracle_ttloac_varcharlen = -1;
79 static int hf_sqloracle_ttloac_dummy = -1;
80 static int hf_sqloracle_ttloac_scalesize = -1;
81 static int hf_sqloracle_ttloac_prefix = -1;
82 static int hf_sqloracle_ttloac_flag1 = -1;
83 static int hf_sqloracle_ttloac_type = -1;
84 static int hf_sqloracle_ttloac_header = -1;
85 */
86 static int hf_sqloracle_flag = -1;
87 static int hf_sqloracle_num_column = -1;
88 /* static int hf_sqloracle_v8ttloac_vsn = -1; */
89 static int hf_sqloracle_itemNum = -1;
90 static int hf_sqloracle_numItersThisTime = -1;
91 static int hf_sqloracle_uacBufLength = -1;
92
93 static char m_pCurQuery[2025];
94
95 static int m_numOfUpdate =0;
96 /* static int m_tnsErrors = 0; */
97 static int m_numOfSelect = 0;
98 static int m_numOfInsert = 0;
99 static int m_numOfDelete = 0;
100 static int m_numOfRollback = 0;
101 static int m_numOfSet = 0;
102 static int m_numOfStart = 0;
103 static int m_numOfCommit = 0;
104 static int m_numOfOtherStatement = 0;
105 static int m_numOfTransaction = 0;
106 /* static int m_bReAssembling = 0; */
107 /* SQLORACLE flags */
108 /*static const true_false_string flags_set_truth =
109 {
110   " ",
111   "No "
112 }; */
113 static const value_string sqloracle_operation_type[] = {
114         {NET8_TYPE_SETPROP,   "Set protocol" },
115         {NET8_TYPE_SETDATAREP,    "Set data representations" },
116         {NET8_TYPE_USERTOSERVER,       "User request" },
117         {NET8_TYPE_ERRORSTATUS,    "Error: No data found" },
118         {NET8_TYPE_AUAS,  "Access user address space" },
119         {NET8_TYPE_ROWTRANSFER,      "Row transfer header" },
120         {NET8_TYPE_ROWDATA,      "Row transfer data follows" },
121         {NET8_TYPE_OPIPARAM,     "Return OPI parameter" },
122         {NET8_TYPE_FUNCCOMPLETE,    "Oracle function complete"},
123         {NET8_TYPE_TTINOER,    "N Error return definitions follow"},
124         {NET8_TYPE_TTIIOV, "Sending I/O Vec only for fast UPI"},
125         {NET8_TYPE_TTISLG,   "Send long for fast UPI"},
126         {NET8_TYPE_TTIICA,   "Invoke user callback"},
127         {NET8_TYPE_TTILOBD,   "LOB/FILE data follows"},
128         {NET8_TYPE_TTIWRN,   "Warning messages - may be a set of them"},
129         {NET8_TYPE_DESCINFO,   "Describe information"},
130         {NET8_TYPE_PIGGYBACKFUNC,   "Piggyback function follows"},
131         {NET8_TYPE_TTI3GL,   "signals special action for untrusted callout support"},
132         {NET8_TYPE_TTIFOB,   "Flush Out Bind data in DML/w RETURN when error"},
133         {NET8_TYPE_SECURENEG,   "Secure Network Services Negotiation"},
134         {0,     "Unknown"},
135         {0, NULL}
136 };
137 static const value_string sql_func_type[] = {
138     {NET8_USER_FUNC_OLOGON,   " Logon to Oracle"},
139     {NET8_USER_FUNC_OPENCURSOR,    "Open cursor" },              
140     {NET8_USER_FUNC_PARSE,     "Parse statement" },                
141     {NET8_USER_FUNC_EXECUTE,    "Execute statement" },               
142     {NET8_USER_FUNC_OFETCH,   " Fetch row" },              
143     {NET8_USER_FUNC_CLOSECURSOR,   "Close cursor" },              
144     {NET8_USER_FUNC_OLOGOFF,  "Logoff" },             
145     {NET8_USER_FUNC_ODSCRIBE, "Describe select list column" },            
146     {NET8_USER_FUNC_ODEFIN,   "Define where column goes" },              
147     {NET8_USER_FUNC_OCOMON,   "Autocommit On" },              
148     {NET8_USER_FUNC_OCOMOFF,  "Autocommit Off" },             
149     {NET8_USER_FUNC_OCOMMIT,  "Commit" },             
150     {NET8_USER_FUNC_OROLLBACK,    "Rollback" },               
151     {NET8_USER_FUNC_OSFE,     "Set fatal error options" },                
152     {NET8_USER_FUNC_ORESUME,  "Resume current operation" },             
153     {NET8_USER_FUNC_OVERSN,   "Get version-date string" },              
154     {NET8_USER_FUNC_OTEMP,    "(Obsolete)" },               
155     {NET8_USER_FUNC_CANCEL,  "Cancel Operation" },             
156     {NET8_USER_FUNC_OGEM,     "Get error message" },                
157     {NET8_USER_FUNC_OSPECIAL, "Special function" },            
158     {NET8_USER_FUNC_OABORT,   "Abort" },              
159     {NET8_USER_FUNC_ODQRID,   "Dequeue by rowid" },              
160     {NET8_USER_FUNC_OLNGF6,   "Fetch long value" },              
161     {NET8_USER_FUNC_OHOWMANY, "How Many Items?" },            
162     {NET8_USER_FUNC_OINIT,    "Initialize Database" },               
163     {NET8_USER_FUNC_OCHANGEU, "Change user_id" },            
164     {NET8_USER_FUNC_OBINDRP,  "Bind by reference positional" },             
165     {NET8_USER_FUNC_OGETBV,   "Get n'th Bind Variable" },              
166     {NET8_USER_FUNC_OGETIV,   "Get n'th Into Variable" },              
167     {NET8_USER_FUNC_OBINDRV,  "Bind by reference" },             
168     {NET8_USER_FUNC_OBINDRN,  "Bind by reference numeric" },             
169     {NET8_USER_FUNC_OPARSEX,  "Parse And Execute" },             
170     {NET8_USER_FUNC_OPARSYN,  "Parse for Syntax only" },             
171     {NET8_USER_FUNC_OPARSDI,  "Parse for Syntax & SQL Dictionary lookup" },
172     {NET8_USER_FUNC_OCONTINUE, "Continue serving after eof" },            
173     {NET8_USER_FUNC_ODSCRARR, "Describe Columns" },            
174     {NET8_USER_FUNC_OLCCINI,  "Init sys pars command table" },             
175     {NET8_USER_FUNC_OLCCFIN,  "Finalize sys pars command table" },             
176     {NET8_USER_FUNC_OLCCPUT,  "Put sys par in command table" },             
177     {NET8_USER_FUNC_OV6STRT,  "Start Oracle (V6)" },             
178     {NET8_USER_FUNC_OV6STOP,  "Poll for shut down Oracle (V6)" },             
179     {NET8_USER_FUNC_ORIP,     "Run independent process (V6)" },                
180     {NET8_USER_FUNC_OARCHIVE, "Archive op (V6)" },            
181     {NET8_USER_FUNC_OMRSTART, "Media recovery - start (V6)" },            
182     {NET8_USER_FUNC_OMRRECTS, "Media recovery - record tablespace to recover (V6)"},          
183     {NET8_USER_FUNC_OMRGSLSQ, "Media recovery - get starting log seq # (V6)" },
184     {NET8_USER_FUNC_OMRREC,   "Media recovery - recover using offline log (V6)" },
185     {NET8_USER_FUNC_OMRCAN,   "Media recovery - cancel media recovery (V6)" },
186     {NET8_USER_FUNC_O2LOGON,  "Logon to ORACLE" },             
187     {NET8_USER_FUNC_OVERSION, "Get Version/Date String" },
188     {NET8_USER_FUNC_OINIT2,   "New init call (supersedes OINIT)" },              
189     {NET8_USER_FUNC_OCLOALL,  "Reserved for MAC; close all cursors" },             
190     {NET8_USER_FUNC_OALL,     "Bundled execution call" },                
191     {NET8_USER_FUNC_OTEX,     "Transaction execute call (OS/2)" },                
192     {NET8_USER_FUNC_OSDAUTH,  "Set DBA authorization call (OS/2)" },             
193     {NET8_USER_FUNC_OUDLFUN,  "Direct loader: functions" },             
194     {NET8_USER_FUNC_OUDLBUF,  "Direct loader: buffer transfer" },             
195     {NET8_USER_FUNC_OK2RPC,   "Distrib. trans. mgr. RPC" },              
196     {NET8_USER_FUNC_ODSCIDX,  "Describe indexes for distributed query" },
197     {NET8_USER_FUNC_OSESOPN,  "Session operations" },             
198     {NET8_USER_FUNC_OEXECSCN, "Execute using synchronized system commit numbers" },
199     {NET8_USER_FUNC_OALL7,    "New V8 Bundle call" },               
200     {NET8_USER_FUNC_OLONGF,   "Long fetch version 7" },              
201     {NET8_USER_FUNC_OEXECA,   "Call opiexe from opiall" },             
202     {NET8_USER_FUNC_OSQL7,    "Parse call" },               
203     {NET8_USER_FUNC_OOBS,     "(Obsolete)" },
204     {NET8_USER_FUNC_ORPC,     "RPC Call from pl" },                
205     {NET8_USER_FUNC_OEXFEN,   "OEXFEN" },
206     {NET8_USER_FUNC_OXAOPN,   "XA operation" },              
207     {NET8_USER_FUNC_OKGL,     "KGL call" },
208     {NET8_USER_FUNC_03LOGON,  "LogonB"},
209     {NET8_USER_FUNC_03LOGA,   "LogonA"},
210     {NET8_USER_FUNC_OFNSTM,   "Do Streaming Operation"},
211     {NET8_USER_FUNC_OPENSESS, "Open Session"},  
212     {NET8_USER_FUNC_O71XAOPN,  "X/Open XA operations"},
213     {NET8_USER_FUNC_ODEBUG,   "Debug"},
214     {NET8_USER_FUNC_ODEBUGS,  "Special Debug"},
215     {NET8_USER_FUNC_OXAST,    "XA Start"},
216     {NET8_USER_FUNC_OXACM,    "XA Commit"},
217     {NET8_USER_FUNC_OXAPR,    "XA Prepare"},
218     {NET8_USER_FUNC_OXDP,     "XA Import"},
219     {NET8_USER_FUNC_OKOD,     "Get Object Value From Reference"},
220     {NET8_USER_FUNC_OCONNECT, "Connect"},
221         {NET8_USER_FUNC_OCBK,           "call (kernel side only)"},
222         {NET8_USER_FUNC_OALL8,     "OALL8"},
223         {NET8_USER_FUNC_OFNSTM2,   "OFNSTM without the begintxn"},
224         {NET8_USER_FUNC_OLOBOPS,   "LOB Operation"},
225         {NET8_USER_FUNC_OFILECRT,  "FILE create call"},
226         {NET8_USER_FUNC_ODNY,      "New Describe"},
227         {NET8_USER_FUNC_OCONNECT,  "code for non blocking attach host"},
228         {NET8_USER_FUNC_OOPENRCS,  "Open a recursive cursor"},
229         {NET8_USER_FUNC_OKPRALL,   "Bundled KPR execution"},
230         {NET8_USER_FUNC_OPLS,      "Bundled PL/SQL execution"},
231         {NET8_USER_FUNC_OTXSE,      "transaction start, attach, detach"},
232         {NET8_USER_FUNC_OTXEN,      "transaction commit, rollback, recover"},
233         {NET8_USER_FUNC_OCCA,      "Cursor Close All"},
234         {NET8_USER_FUNC_OFOI,      "Failover info piggyback"},
235         {NET8_USER_FUNC_O80SES,    "V8 session switching piggyback"},
236         {NET8_USER_FUNC_ODDF,      "Do Dummy Defines"},
237         {NET8_USER_FUNC_OLRMINI,   "init sys pars"},
238         {NET8_USER_FUNC_OLRMFIN,   "finalize sys pars"},
239         {NET8_USER_FUNC_OLRMPUT,   "put sys par in par space"},
240         {NET8_USER_FUNC_OLRMTRM,   "terminate sys pars"},
241         {NET8_USER_FUNC_OEXFENA,   "execute but don't unmap (used from opiall0)"},
242         {NET8_USER_FUNC_OINIUCB,   "OINIT for Untrusted CallBacks"},
243         {NET8_USER_FUNC_AUTH,     "Authentication Call"},
244         {NET8_USER_FUNC_OFGI,      "FailOver Get Instance Info"},
245         {NET8_USER_FUNC_OOTCO,      "Oracle Transaction service COmmit remote sites"},
246         {NET8_USER_FUNC_GETSESSKEY,  "Get the session key"},
247         {NET8_USER_FUNC_ODSY,      "V8 Describe Any"},
248         {NET8_USER_FUNC_OCANA,     "Cancel All"},
249         {NET8_USER_FUNC_OAQEQ,      "AQ EnQueue"},
250         {NET8_USER_FUNC_OAQDQ,      "AQ Dequeue"},
251         {NET8_USER_FUNC_ORFS,       "RFS call"},
252         {NET8_USER_FUNC_OKPN,      "Kernel Programmatic Notification"},
253     {0,       "Unknown type"},
254         {0, NULL}
255 };
256
257
258 #if 0
259 /* jtse */
260 /*+------------------------------------------------------
261  * 
262  * Convert hex data to character string
263  *--------------------------------------------------------
264 -*/
265 char * convertHexToString(BYTE *pSrc, UI16_T length)
266 {
267     char buf[150];
268         char * hexString;
269         char hex[17] = "0123456789ABCDEF";
270         int i;
271     int limit = 2*length;
272     if (limit >= sizeof(buf) - 3)
273         limit = sizeof(buf) - 3;
274
275
276     for( i=0; i<limit; i+=2)
277     {
278         buf[i] = hex[*pSrc>>4];
279         buf[i+1] = hex[*pSrc&0x0F];
280                 pSrc++;
281     }
282         buf[i] = '\0';
283
284     /*
285         for(int i=0; i<length*2; i++)
286         {
287                 buf[i]   = hex[*pSrc>>4];
288                 buf[++i] = hex[*pSrc&0x0F];
289                 pSrc++;
290         }
291         buf[length*2] = '\0';
292     */
293
294     /* hexString = buf; */
295 /*      strcpy (hexString, buf); */
296         return hexString;
297 }
298 #endif
299
300 static void ParseSqlStatement(/*char  *appMsg,*/ UI8_P pSqlData, UI16_T dataLen)
301 {
302         char  *pSqlModifyData = (I8_P)m_pCurQuery;
303         int   i = 0;
304
305         while (*pSqlData != '\1' && *pSqlData != '\0' && i < dataLen)
306         {
307                 if (*pSqlData < ' ')
308                 {
309                         *pSqlModifyData = ' ';
310                 }
311                 else
312                 {
313                         *pSqlModifyData = *pSqlData;
314                 }
315
316                 pSqlModifyData++;
317                 pSqlData++;
318                 i++;
319         }
320
321         *pSqlModifyData = '\0';
322
323 #if 0
324         appMsg = (I8_P)m_pCurQuery;
325 #endif
326
327         if (g_ascii_strncasecmp((I8_P)m_pCurQuery, "update", 6) == 0)
328         {
329                 m_numOfUpdate++;
330 #if 0
331                 pSummaryStat->m_numOfUpdate++;
332                 if (m_pServerNode != NULL)
333                     m_pServerNode->m_numOfUpdate++;
334 #endif
335         }
336         else if (g_ascii_strncasecmp((I8_P)m_pCurQuery, "select", 6) == 0)
337         {
338                 m_numOfSelect++;
339 #if 0
340                 pSummaryStat->m_numOfSelect++;
341                 if (m_pServerNode != NULL)
342                     m_pServerNode->m_numOfSelect++;
343 #endif
344         }
345         else if (g_ascii_strncasecmp((I8_P)m_pCurQuery, "insert", 6) == 0)
346         {
347                 m_numOfInsert++;
348 #if 0
349                 pSummaryStat->m_numOfInsert++;
350                 if (m_pServerNode != NULL)
351                         m_pServerNode->m_numOfInsert++;
352 #endif
353         }
354         else if (g_ascii_strncasecmp((I8_P)m_pCurQuery, "delete", 6) == 0)
355         {
356                 m_numOfDelete++;
357 #if 0
358                 pSummaryStat->m_numOfDelete++;
359                 if (m_pServerNode != NULL)
360                         m_pServerNode->m_numOfDelete++;
361 #endif
362         }
363         else if (g_ascii_strncasecmp((I8_P)m_pCurQuery, "rollback", 8) == 0)
364         {
365                 m_numOfRollback++;
366 #if 0
367                 pSummaryStat->m_numOfRollback++;
368                 if (m_pServerNode != NULL)
369                         m_pServerNode->m_numOfRollback++;
370 #endif
371         }
372         else if (g_ascii_strncasecmp((I8_P)m_pCurQuery, "set", 3) == 0)
373         {
374                 m_numOfSet++;
375 #if 0
376                 pSummaryStat->m_numOfSet++;
377                 if (m_pServerNode != NULL)
378                         m_pServerNode->m_numOfSet++;
379 #endif
380         }
381         else if (g_ascii_strncasecmp((I8_P)m_pCurQuery, "start", 5) == 0)
382         {
383                 m_numOfStart++;
384 #if 0
385                 pSummaryStat->m_numOfStart++;
386                 if (m_pServerNode != NULL)
387                         m_pServerNode->m_numOfStart++;
388 #endif
389         }
390         else if (g_ascii_strncasecmp((I8_P)m_pCurQuery, "commit", 6) == 0)
391         {
392                 m_numOfCommit++;
393 #if 0
394                 pSummaryStat->m_numOfCommit++;
395                 if (m_pServerNode != NULL)
396                         m_pServerNode->m_numOfCommit++;
397 #endif
398         }
399         else 
400         {
401                 m_numOfOtherStatement++;
402 #if 0
403                 pSummaryStat->m_numOfOtherStatement++;
404                 if (m_pServerNode != NULL)
405                         m_pServerNode->m_numOfOtherStatement++;
406 #endif
407         }
408
409         m_numOfTransaction++;
410 #if 0
411         m_pSummaryStat->m_numOfTransaction++;
412         if (m_pServerNode != NULL)
413                 m_pServerNode->m_numOfTransaction++;
414 #endif
415 }
416
417
418 static gboolean FindBeginningSQLString(UI8_P *pBuf, UI16_T *pDataLen, int lookSize)
419 {
420         /* the position could still be off by x bytes, check if it happened to be landing on an address */
421 /*      int i = 31;     /+ allow upto 8 bad bytes */
422         UI8_P pString = *pBuf;
423         gboolean bAlpha1 = isalpha(pString[0]) != 0;
424         gboolean bAlpha2 = isalpha(pString[1]) != 0;
425         gboolean bAlpha3 = isalpha(pString[2]) != 0;
426         gboolean bAlpha4 = isalpha(pString[3]) != 0;
427         gboolean bComment = FALSE;
428         UI16_T dataLen = *pDataLen;
429         while ( (dataLen > 2) && (lookSize > 0) && ((bAlpha1 == FALSE) || (bAlpha2 == FALSE) || (bAlpha3 == FALSE) || (bAlpha4 == FALSE)))
430         {
431                 /* check if we need to find the ending comment */
432                 if (bComment)
433                 {
434                         if (*((UI16_P)pString) == 0x2F2A)       /* ending comment */
435                         {
436                                 bComment = FALSE;
437                                 pString ++;     /* skip the comment */
438                                 dataLen --;
439                         }
440                         pString ++;
441                         dataLen --;
442                 }
443                 else
444                 {
445                         /* check if there is a comment string prepended to the statement */
446                         if (*((UI16_P)pString) == 0x2A2F)       /* beginning of comment */
447                         {
448                                 bComment = TRUE;
449                                 dataLen -= 2;
450                                 pString += 2;
451                                 bAlpha2 = isalpha(pString[1]) != 0;
452                                 bAlpha3 = isalpha(pString[2]) != 0;
453                                 bAlpha4 = isalpha(pString[3]) != 0;
454                                 continue;
455                         }
456                         pString++;
457                         bAlpha1 = bAlpha2;
458                         bAlpha2 = isalpha(pString[1]) != 0;
459                         bAlpha3 = isalpha(pString[2]) != 0;
460                         bAlpha4 = isalpha(pString[3]) != 0;
461                         dataLen --;
462             /* don't count the zeros */
463             if (*((UI8_P)pString) != 0x0)
464                             lookSize--;
465                 }
466         }
467         if (bAlpha1 && bAlpha2 && bAlpha3 && bAlpha4)
468         {
469                 *pBuf = pString;
470                 *pDataLen = dataLen;
471                 return TRUE;
472         }
473         else
474                 return FALSE;
475 }
476
477 static gboolean ParseCommand(proto_tree *tree,tvbuff_t *tvb, int offset, packet_info *pinfo,UI16_T dataLen)
478 {
479         UI8_T pAddress[1024];
480         UI16_T SQLDataLen = dataLen;
481         int i;
482         UI8_P pAddr;
483         for (i=0; i<1024;i++)
484         {
485                 pAddress[i] = '\0';
486         }
487         tvb_memcpy (tvb, pAddress,offset, dataLen);
488         pAddr = (UI8_P)pAddress;
489         /* see if SQL statement is there */
490         if (FindBeginningSQLString((UI8_P*)&pAddr, &SQLDataLen, 0x30) == TRUE)
491         {
492                 ParseSqlStatement( pAddr, dataLen);
493                 if (tree)
494                         proto_tree_add_text(tree, tvb, offset+dataLen-SQLDataLen, SQLDataLen,
495                             "SQL statement = %s",m_pCurQuery);
496     if (check_col(pinfo->cinfo, COL_INFO))
497         col_clear(pinfo->cinfo, COL_INFO);
498             if (check_col(pinfo->cinfo, COL_INFO))
499                    col_add_str(pinfo->cinfo, COL_INFO, m_pCurQuery );
500                 return TRUE;
501         }
502         return FALSE;
503 }
504
505 #if 0
506 static gboolean ParseNewCommand( proto_tree *tree,tvbuff_t *tvb, int offset, packet_info *pinfo, UI16_T dataLen)
507 {
508         UI8_T pAddress[1024];
509         /* find the first sequence of zeros */
510         int amount = dataLen - 12;
511         int i = 0, sqlamount;
512         UI8_P pAddr;
513         tvb_memcpy (tvb, pAddress,offset, dataLen);
514         pAddr = (UI8_P)&pAddress;
515         for (; i < amount; i++)
516         {
517                 if (*((UI32_P)((UI8_P)pAddr++)) == 0x0000)
518                         break;
519         }
520         /* was there a sequence of 4 zeros */
521         if (i >= amount)
522         {
523         /*      free(pAddr); */
524                 return FALSE;           /* went past, can not be a sql command */
525         }
526         /* look for the end of the zeros */
527         amount = dataLen - i - 4;       /* rest of the data */
528         pAddr += 3;
529         for (i = 0; *pAddr++ == 0 && i < amount; i++);
530         if (i >= amount)
531         {
532                 /* free (pAddr); */
533                 return FALSE;   /* no values after zeros */
534         }
535
536         amount -= i + 1;        /* rest of the data */
537
538         /* see if SQL statement is there */
539         sqlamount = amount;
540         if (FindBeginningSQLString((UI8_P*)&pAddr, (UI16_P)&sqlamount, 13) == TRUE)
541         {
542                 ParseSqlStatement( pAddr, amount);
543     if (check_col(pinfo->cinfo, COL_INFO))
544         col_clear(pinfo->cinfo, COL_INFO);
545             if (check_col(pinfo->cinfo, COL_INFO))
546                    col_add_str(pinfo->cinfo, COL_INFO, m_pCurQuery );
547                 proto_tree_add_text(tree, tvb, offset+amount-sqlamount, sqlamount,
548                             "SQL statement = %s",m_pCurQuery);
549                 return TRUE;
550         }
551         return FALSE;
552 }
553 #endif
554
555
556
557
558 static void
559 dissect_sqloracle(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) 
560 {
561         proto_item      *ti = NULL;
562         proto_tree      *sqloracle_tree = NULL;
563         int offset = 0,dataLen,nocol,numItersThisTime,flag,iterNum,uacBufLength;
564         guint8  header_operation,func_type=0;
565         m_pCurQuery[0] = '0';
566
567
568         pinfo->current_proto = "SQLORACLE";
569         if ( check_col( pinfo->cinfo, COL_PROTOCOL ))
570                 col_set_str( pinfo->cinfo, COL_PROTOCOL, "SQL" );
571     if (check_col(pinfo->cinfo, COL_INFO))
572         col_clear(pinfo->cinfo, COL_INFO);
573
574         header_operation = tvb_get_guint8(tvb, offset);
575         dataLen = tvb_reported_length_remaining(tvb, offset);
576         if (header_operation != NET8_TYPE_FUNCCOMPLETE)
577                 func_type = tvb_get_guint8(tvb, offset+1);
578
579         if ( check_col(pinfo->cinfo, COL_INFO))
580         {
581            col_add_fstr(pinfo->cinfo, COL_INFO, "%s ", val_to_str(header_operation, sqloracle_operation_type, ""));
582         }
583
584         if ( tree ) 
585         { 
586                 ti = proto_tree_add_item(tree, proto_sqloracle, tvb, 0, -1, FALSE);
587                 sqloracle_tree = proto_item_add_subtree(ti, ett_sqloracle);
588             proto_tree_add_uint(sqloracle_tree, hf_sqloracle_operation, tvb, offset, 1,header_operation);
589                 if (func_type && header_operation !=NET8_TYPE_ROWTRANSFER)
590                         proto_tree_add_uint(sqloracle_tree, hf_sqloracle_func_type, tvb, offset+1, 1,func_type);
591         }
592
593         switch (header_operation)
594         {
595                 case NET8_TYPE_USERTOSERVER: /* 0x3 */
596                         if ( check_col(pinfo->cinfo, COL_INFO))
597                         {
598                 col_append_fstr(pinfo->cinfo, COL_INFO, ":%s ", val_to_str(func_type, sql_func_type, ""));
599                         }
600                         switch (func_type)
601                         {
602                                 case NET8_USER_FUNC_PARSE:
603                                         ParseCommand(sqloracle_tree,tvb,offset+0x0B,pinfo,dataLen-0x0B);
604                                         break;
605                                 case NET8_USER_FUNC_OALL:
606                                 case NET8_USER_FUNC_OALL8:
607                                         /* command could be embedded in this packet
608                                          * filtered_for_hh02_and_hh05.enc has commands that are not 0x2f offset
609                                          * try to detect the difference by looking at the offset 0x12 for 6 zeros
610                                          */
611                                         if (dataLen > (0x19 + 8))       /* assume minimum of 8 chars for the command */
612                                         {
613                                                 /* piggybacked functions will recursive call this routine to process the command */
614                                                 if (ParseCommand(sqloracle_tree,tvb, offset+0x12, pinfo,dataLen - 0x12) == TRUE)
615                                                         break;
616                                         }
617                                         break;
618                                 case NET8_USER_FUNC_OSQL7:                      /* 0x4A */
619                                         /* command could be embedded in this packet */
620                                         /* aig oracle.enc has smaller data */
621                                         if (dataLen > (0x2A /*0x30/0x14*/ + 8)) /* minimum of 8 chars */
622                                         {
623                                                 if (ParseCommand(sqloracle_tree,tvb, offset + 0x2A /*0x30/0x14*/, pinfo,dataLen - 0x2A /*0x30/0x14*/) == TRUE)
624                                                                 break;
625                                         }
626                                         break;
627
628                                 case NET8_USER_FUNC_OALL7:                      /* 0x47 */
629                                         /* command could be embedded in this packet */
630                                         if (dataLen > (0x2A /*0x30/0x14*/ + 8)) /* minimum of 8 chars */
631                                         {
632                                                 if (ParseCommand(sqloracle_tree,tvb, offset + 0x14, pinfo,dataLen - 0x14) == TRUE)
633                                                 {
634                                                         if (check_col(pinfo->cinfo, COL_INFO))
635                                                                 col_add_str(pinfo->cinfo, COL_INFO, m_pCurQuery );
636                                                         break;
637                                                 }
638                                                 else
639                                                         /* appdncr.enc has this smaller command */
640                                                 if (ParseCommand(sqloracle_tree,tvb, offset + 0x30, pinfo,dataLen - 0x30) == TRUE)
641                                                         break;
642                                         }
643                                         break;
644                         }
645                         break;
646                 case NET8_TYPE_ROWTRANSFER:             /* 0x06 */
647                         flag = func_type;
648                         proto_tree_add_uint(sqloracle_tree, hf_sqloracle_flag, tvb, offset+1, 1,flag);
649                         nocol = tvb_get_guint8(tvb, offset+2);
650                         iterNum = tvb_get_guint8(tvb, offset+3);
651                         numItersThisTime = tvb_get_ntohs(tvb, offset+5);
652                         uacBufLength = tvb_get_ntohs(tvb, offset+7);
653                         proto_tree_add_uint(sqloracle_tree, hf_sqloracle_num_column, tvb, offset+2, 1,nocol);
654                         proto_tree_add_uint(sqloracle_tree, hf_sqloracle_itemNum, tvb, offset+3, 1,iterNum);
655                         proto_tree_add_uint(sqloracle_tree, hf_sqloracle_numItersThisTime, tvb, offset+5, 2,numItersThisTime);
656                         proto_tree_add_uint(sqloracle_tree, hf_sqloracle_uacBufLength, tvb, offset+7, 2,uacBufLength);
657
658                         break;
659                 default:
660                         return;
661                         break;
662         }
663
664 } /* dissect_sqloracle */
665
666 void
667 proto_register_sqloracle(void)
668 {
669         static hf_register_info hf[] =
670         {
671                 { &hf_sqloracle_operation,
672                   { "Basic Operation", "sqloracle.operation", FT_UINT8, BASE_DEC, VALS(sqloracle_operation_type), 0x0, "", HFILL }
673                 },
674                 { &hf_sqloracle_func_type,
675                   { "Function Type", "sqloracle.type", FT_UINT8, BASE_DEC, VALS(sql_func_type), 0x0, "", HFILL }
676                 },
677                 { &hf_sqloracle_flag,
678                   { "flag", "sqloracle.flag", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
679                 },
680                 { &hf_sqloracle_num_column,
681                         { "Number of Columns", "sqloracle.nocolumn", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
682                 },
683                 { &hf_sqloracle_itemNum,
684                   { "Iteration Number", "sqloracle.itemNum", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL }
685                 },
686                 { &hf_sqloracle_numItersThisTime,
687                   { "# of iterations this time", "sqloracle.numItersThisTime", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL }
688                 },
689                 { &hf_sqloracle_uacBufLength,
690                   { "user access buffer length", "sqloracle.uacBufLength", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL }
691                 },
692 /*              { &hf_sqloracle_ttloac_header,
693                         { "TTLOAC Header", "sqloracle.ttloac_header", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
694                 },
695
696                 { &hf_sqloracle_ttloac_type,
697                         { "type", "sqloracle.ttloac_type", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
698                 },
699                 { &hf_sqloracle_ttloac_flag1,
700                   { "flag1", "sqloracle.ttloac_flag1", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
701                 },
702                 { &hf_sqloracle_ttloac_prefix,
703                 { "prefix",             "sqloracle.ttloac_prefix", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
704                 },
705                 { &hf_sqloracle_ttloac_scalesize,
706                 { "scale size",         "sqloracle.ttloac_scalesize", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
707                 },
708                 { &hf_sqloracle_ttloac_dummy,
709                 { "dummy",              "sqloracle.ottloac_dummy", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
710                 },
711                 { &hf_sqloracle_ttloac_varcharlen,
712                 { "varcharlen",         "sqloracle.ttloac_varcharlen", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
713                 },
714                 { &hf_sqloracle_ttloac_mal,
715                 { "mal",                "sqloracle.ttloac_mal", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
716                 },
717                 { &hf_sqloracle_ttloac_flag2,
718                 { "flag2",              "sqloracle.ttloac_flag2", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
719                 },
720                 { &hf_sqloracle_v8ttloac_header,
721                         { "V8TTLOAC Header", "sqloracle.v8ttloac_header", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
722                 },
723
724                 { &hf_sqloracle_v8ttloac_oid,
725                         { "oid", "sqloracle.v8ttloac_oid", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
726                 },
727                 { &hf_sqloracle_v8ttloac_vsn,
728                   { "vsn", "sqloracle.v8ttloac_vsn", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
729                 },
730                 { &hf_sqloracle_v8ttloac_ncs,
731                 { "ncs",                "sqloracle.v8ttloac_ncs", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
732                 },
733                 { &hf_sqloracle_v8ttloac_formuse,
734                 { "FormUse",            "sqloracle.v8ttloac_formuse", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
735                 },
736                 { &hf_sqloracle_v8ttluds_header,
737                         { "V8TTLUDS Header", "sqloracle.v8ttluds_header", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
738                 },
739
740                 { &hf_sqloracle_v8ttluds_nullallow,
741                         { "null allowed", "sqloracle.v8ttluds_nullallow", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
742                 },
743                 { &hf_sqloracle_v8ttluds_scrlength,
744                   { "screen length", "sqloracle.v8ttluds_scrlength", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
745                 },
746                 { &hf_sqloracle_v8ttluds_columnname,
747                   { "column name", "sqloracle.v8ttluds_columnname", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
748                 },
749                 { &hf_sqloracle_v8ttluds_sname,
750                   { "sName", "sqloracle.v8ttluds_snname", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
751                 },
752                 { &hf_sqloracle_v8ttluds_tname,
753                   { "tName", "sqloracle.v8ttluds_tname", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
754                 },
755                 { &hf_sqloracle_stmt_length,
756                 { "SQL Statement Length",       "sqloracle.stmtlength", FT_UINT8, BASE_DEC, NULL, 0x0,
757                         "", HFILL }},
758 */              { &hf_sqloracle_stmt,
759                         { "SQL statement", "sqloracle.stmt", FT_BYTES, BASE_HEX, NULL, 0x0, "", HFILL }
760                 },
761                 };
762
763         static gint *ett[] =
764         {
765                 &ett_sqloracle,
766                 &ett_sqloracle_operation,
767         };
768
769         proto_sqloracle = proto_register_protocol("SQL -Net8 Data", "SQL", "sqloracle");
770         proto_register_field_array(proto_sqloracle, hf, array_length(hf));
771         proto_register_subtree_array(ett, array_length(ett));
772
773         
774         register_dissector("sqloracle", dissect_sqloracle, proto_sqloracle);
775 }
776
777 void
778 proto_reg_handoff_sqloracle(void)
779 {
780         dissector_handle_t sqloracle_handle;
781
782         sqloracle_handle = find_dissector("sqloracle");
783         dissector_add("tns.port", TCP_PORT_TNS, sqloracle_handle);
784 }
785