460136fe2a674fe4edd80909fdb2427c5eb36e29
[sfrench/samba-autobuild/.git] / examples / pcap2nbench / main.cpp
1 /*\
2  *  pcap2nbench - Converts libpcap network traces to nbench input
3  *  Copyright (C) 2004  Jim McDonough <jmcd@us.ibm.com>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  *  Written by Anthony Liguori <aliguori@us.ibm.com>
20 \*/
21
22 #include <iostream>
23 #include <pcap.h>
24 #include <getopt.h>
25 #include <stdint.h>
26 #include <netinet/in.h>
27
28 #include "ethernet.hpp"
29 #include "ip.hpp"
30 #include "tcp.hpp"
31 #include "smb.hpp"
32 #include "ntcreateandxrequest.hpp"
33 #include "ntcreateandxresponse.hpp"
34 #include "readandxrequest.hpp"
35 #include "writeandxrequest.hpp"
36 #include "closerequest.hpp"
37
38 #include <vector>
39 #include <set>
40
41
42 /* derived from source/include/nterr.h */
43 const char *nt_status_to_string[] = {
44   "NT_STATUS_OK", /* 0x0000 */
45   "NT_STATUS_UNSUCCESSFUL", /* 0x0001 */
46   "NT_STATUS_NOT_IMPLEMENTED", /* 0x0002 */
47   "NT_STATUS_INVALID_INFO_CLASS", /* 0x0003 */
48   "NT_STATUS_INFO_LENGTH_MISMATCH", /* 0x0004 */
49   "NT_STATUS_ACCESS_VIOLATION", /* 0x0005 */
50   "NT_STATUS_IN_PAGE_ERROR", /* 0x0006 */
51   "NT_STATUS_PAGEFILE_QUOTA", /* 0x0007 */
52   "NT_STATUS_INVALID_HANDLE", /* 0x0008 */
53   "NT_STATUS_BAD_INITIAL_STACK", /* 0x0009 */
54   "NT_STATUS_BAD_INITIAL_PC", /* 0x000a */
55   "NT_STATUS_INVALID_CID", /* 0x000b */
56   "NT_STATUS_TIMER_NOT_CANCELED", /* 0x000c */
57   "NT_STATUS_INVALID_PARAMETER", /* 0x000d */
58   "NT_STATUS_NO_SUCH_DEVICE", /* 0x000e */
59   "NT_STATUS_NO_SUCH_FILE", /* 0x000f */
60   "NT_STATUS_INVALID_DEVICE_REQUEST", /* 0x0010 */
61   "NT_STATUS_END_OF_FILE", /* 0x0011 */
62   "NT_STATUS_WRONG_VOLUME", /* 0x0012 */
63   "NT_STATUS_NO_MEDIA_IN_DEVICE", /* 0x0013 */
64   "NT_STATUS_UNRECOGNIZED_MEDIA", /* 0x0014 */
65   "NT_STATUS_NONEXISTENT_SECTOR", /* 0x0015 */
66   "NT_STATUS_MORE_PROCESSING_REQUIRED", /* 0x0016 */
67   "NT_STATUS_NO_MEMORY", /* 0x0017 */
68   "NT_STATUS_CONFLICTING_ADDRESSES", /* 0x0018 */
69   "NT_STATUS_NOT_MAPPED_VIEW", /* 0x0019 */
70   "NT_STATUS_UNABLE_TO_FREE_VM", /* 0x001a */
71   "NT_STATUS_UNABLE_TO_DELETE_SECTION", /* 0x001b */
72   "NT_STATUS_INVALID_SYSTEM_SERVICE", /* 0x001c */
73   "NT_STATUS_ILLEGAL_INSTRUCTION", /* 0x001d */
74   "NT_STATUS_INVALID_LOCK_SEQUENCE", /* 0x001e */
75   "NT_STATUS_INVALID_VIEW_SIZE", /* 0x001f */
76   "NT_STATUS_INVALID_FILE_FOR_SECTION", /* 0x0020 */
77   "NT_STATUS_ALREADY_COMMITTED", /* 0x0021 */
78   "NT_STATUS_ACCESS_DENIED", /* 0x0022 */
79   "NT_STATUS_BUFFER_TOO_SMALL", /* 0x0023 */
80   "NT_STATUS_OBJECT_TYPE_MISMATCH", /* 0x0024 */
81   "NT_STATUS_NONCONTINUABLE_EXCEPTION", /* 0x0025 */
82   "NT_STATUS_INVALID_DISPOSITION", /* 0x0026 */
83   "NT_STATUS_UNWIND", /* 0x0027 */
84   "NT_STATUS_BAD_STACK", /* 0x0028 */
85   "NT_STATUS_INVALID_UNWIND_TARGET", /* 0x0029 */
86   "NT_STATUS_NOT_LOCKED", /* 0x002a */
87   "NT_STATUS_PARITY_ERROR", /* 0x002b */
88   "NT_STATUS_UNABLE_TO_DECOMMIT_VM", /* 0x002c */
89   "NT_STATUS_NOT_COMMITTED", /* 0x002d */
90   "NT_STATUS_INVALID_PORT_ATTRIBUTES", /* 0x002e */
91   "NT_STATUS_PORT_MESSAGE_TOO_LONG", /* 0x002f */
92   "NT_STATUS_INVALID_PARAMETER_MIX", /* 0x0030 */
93   "NT_STATUS_INVALID_QUOTA_LOWER", /* 0x0031 */
94   "NT_STATUS_DISK_CORRUPT_ERROR", /* 0x0032 */
95   "NT_STATUS_OBJECT_NAME_INVALID", /* 0x0033 */
96   "NT_STATUS_OBJECT_NAME_NOT_FOUND", /* 0x0034 */
97   "NT_STATUS_OBJECT_NAME_COLLISION", /* 0x0035 */
98   "NT_STATUS_HANDLE_NOT_WAITABLE", /* 0x0036 */
99   "NT_STATUS_PORT_DISCONNECTED", /* 0x0037 */
100   "NT_STATUS_DEVICE_ALREADY_ATTACHED", /* 0x0038 */
101   "NT_STATUS_OBJECT_PATH_INVALID", /* 0x0039 */
102   "NT_STATUS_OBJECT_PATH_NOT_FOUND", /* 0x003a */
103   "NT_STATUS_OBJECT_PATH_SYNTAX_BAD", /* 0x003b */
104   "NT_STATUS_DATA_OVERRUN", /* 0x003c */
105   "NT_STATUS_DATA_LATE_ERROR", /* 0x003d */
106   "NT_STATUS_DATA_ERROR", /* 0x003e */
107   "NT_STATUS_CRC_ERROR", /* 0x003f */
108   "NT_STATUS_SECTION_TOO_BIG", /* 0x0040 */
109   "NT_STATUS_PORT_CONNECTION_REFUSED", /* 0x0041 */
110   "NT_STATUS_INVALID_PORT_HANDLE", /* 0x0042 */
111   "NT_STATUS_SHARING_VIOLATION", /* 0x0043 */
112   "NT_STATUS_QUOTA_EXCEEDED", /* 0x0044 */
113   "NT_STATUS_INVALID_PAGE_PROTECTION", /* 0x0045 */
114   "NT_STATUS_MUTANT_NOT_OWNED", /* 0x0046 */
115   "NT_STATUS_SEMAPHORE_LIMIT_EXCEEDED", /* 0x0047 */
116   "NT_STATUS_PORT_ALREADY_SET", /* 0x0048 */
117   "NT_STATUS_SECTION_NOT_IMAGE", /* 0x0049 */
118   "NT_STATUS_SUSPEND_COUNT_EXCEEDED", /* 0x004a */
119   "NT_STATUS_THREAD_IS_TERMINATING", /* 0x004b */
120   "NT_STATUS_BAD_WORKING_SET_LIMIT", /* 0x004c */
121   "NT_STATUS_INCOMPATIBLE_FILE_MAP", /* 0x004d */
122   "NT_STATUS_SECTION_PROTECTION", /* 0x004e */
123   "NT_STATUS_EAS_NOT_SUPPORTED", /* 0x004f */
124   "NT_STATUS_EA_TOO_LARGE", /* 0x0050 */
125   "NT_STATUS_NONEXISTENT_EA_ENTRY", /* 0x0051 */
126   "NT_STATUS_NO_EAS_ON_FILE", /* 0x0052 */
127   "NT_STATUS_EA_CORRUPT_ERROR", /* 0x0053 */
128   "NT_STATUS_FILE_LOCK_CONFLICT", /* 0x0054 */
129   "NT_STATUS_LOCK_NOT_GRANTED", /* 0x0055 */
130   "NT_STATUS_DELETE_PENDING", /* 0x0056 */
131   "NT_STATUS_CTL_FILE_NOT_SUPPORTED", /* 0x0057 */
132   "NT_STATUS_UNKNOWN_REVISION", /* 0x0058 */
133   "NT_STATUS_REVISION_MISMATCH", /* 0x0059 */
134   "NT_STATUS_INVALID_OWNER", /* 0x005a */
135   "NT_STATUS_INVALID_PRIMARY_GROUP", /* 0x005b */
136   "NT_STATUS_NO_IMPERSONATION_TOKEN", /* 0x005c */
137   "NT_STATUS_CANT_DISABLE_MANDATORY", /* 0x005d */
138   "NT_STATUS_NO_LOGON_SERVERS", /* 0x005e */
139   "NT_STATUS_NO_SUCH_LOGON_SESSION", /* 0x005f */
140   "NT_STATUS_NO_SUCH_PRIVILEGE", /* 0x0060 */
141   "NT_STATUS_PRIVILEGE_NOT_HELD", /* 0x0061 */
142   "NT_STATUS_INVALID_ACCOUNT_NAME", /* 0x0062 */
143   "NT_STATUS_USER_EXISTS", /* 0x0063 */
144   "NT_STATUS_NO_SUCH_USER", /* 0x0064 */
145   "NT_STATUS_GROUP_EXISTS", /* 0x0065 */
146   "NT_STATUS_NO_SUCH_GROUP", /* 0x0066 */
147   "NT_STATUS_MEMBER_IN_GROUP", /* 0x0067 */
148   "NT_STATUS_MEMBER_NOT_IN_GROUP", /* 0x0068 */
149   "NT_STATUS_LAST_ADMIN", /* 0x0069 */
150   "NT_STATUS_WRONG_PASSWORD", /* 0x006a */
151   "NT_STATUS_ILL_FORMED_PASSWORD", /* 0x006b */
152   "NT_STATUS_PASSWORD_RESTRICTION", /* 0x006c */
153   "NT_STATUS_LOGON_FAILURE", /* 0x006d */
154   "NT_STATUS_ACCOUNT_RESTRICTION", /* 0x006e */
155   "NT_STATUS_INVALID_LOGON_HOURS", /* 0x006f */
156   "NT_STATUS_INVALID_WORKSTATION", /* 0x0070 */
157   "NT_STATUS_PASSWORD_EXPIRED", /* 0x0071 */
158   "NT_STATUS_ACCOUNT_DISABLED", /* 0x0072 */
159   "NT_STATUS_NONE_MAPPED", /* 0x0073 */
160   "NT_STATUS_TOO_MANY_LUIDS_REQUESTED", /* 0x0074 */
161   "NT_STATUS_LUIDS_EXHAUSTED", /* 0x0075 */
162   "NT_STATUS_INVALID_SUB_AUTHORITY", /* 0x0076 */
163   "NT_STATUS_INVALID_ACL", /* 0x0077 */
164   "NT_STATUS_INVALID_SID", /* 0x0078 */
165   "NT_STATUS_INVALID_SECURITY_DESCR", /* 0x0079 */
166   "NT_STATUS_PROCEDURE_NOT_FOUND", /* 0x007a */
167   "NT_STATUS_INVALID_IMAGE_FORMAT", /* 0x007b */
168   "NT_STATUS_NO_TOKEN", /* 0x007c */
169   "NT_STATUS_BAD_INHERITANCE_ACL", /* 0x007d */
170   "NT_STATUS_RANGE_NOT_LOCKED", /* 0x007e */
171   "NT_STATUS_DISK_FULL", /* 0x007f */
172   "NT_STATUS_SERVER_DISABLED", /* 0x0080 */
173   "NT_STATUS_SERVER_NOT_DISABLED", /* 0x0081 */
174   "NT_STATUS_TOO_MANY_GUIDS_REQUESTED", /* 0x0082 */
175   "NT_STATUS_GUIDS_EXHAUSTED", /* 0x0083 */
176   "NT_STATUS_INVALID_ID_AUTHORITY", /* 0x0084 */
177   "NT_STATUS_AGENTS_EXHAUSTED", /* 0x0085 */
178   "NT_STATUS_INVALID_VOLUME_LABEL", /* 0x0086 */
179   "NT_STATUS_SECTION_NOT_EXTENDED", /* 0x0087 */
180   "NT_STATUS_NOT_MAPPED_DATA", /* 0x0088 */
181   "NT_STATUS_RESOURCE_DATA_NOT_FOUND", /* 0x0089 */
182   "NT_STATUS_RESOURCE_TYPE_NOT_FOUND", /* 0x008a */
183   "NT_STATUS_RESOURCE_NAME_NOT_FOUND", /* 0x008b */
184   "NT_STATUS_ARRAY_BOUNDS_EXCEEDED", /* 0x008c */
185   "NT_STATUS_FLOAT_DENORMAL_OPERAND", /* 0x008d */
186   "NT_STATUS_FLOAT_DIVIDE_BY_ZERO", /* 0x008e */
187   "NT_STATUS_FLOAT_INEXACT_RESULT", /* 0x008f */
188   "NT_STATUS_FLOAT_INVALID_OPERATION", /* 0x0090 */
189   "NT_STATUS_FLOAT_OVERFLOW", /* 0x0091 */
190   "NT_STATUS_FLOAT_STACK_CHECK", /* 0x0092 */
191   "NT_STATUS_FLOAT_UNDERFLOW", /* 0x0093 */
192   "NT_STATUS_INTEGER_DIVIDE_BY_ZERO", /* 0x0094 */
193   "NT_STATUS_INTEGER_OVERFLOW", /* 0x0095 */
194   "NT_STATUS_PRIVILEGED_INSTRUCTION", /* 0x0096 */
195   "NT_STATUS_TOO_MANY_PAGING_FILES", /* 0x0097 */
196   "NT_STATUS_FILE_INVALID", /* 0x0098 */
197   "NT_STATUS_ALLOTTED_SPACE_EXCEEDED", /* 0x0099 */
198   "NT_STATUS_INSUFFICIENT_RESOURCES", /* 0x009a */
199   "NT_STATUS_DFS_EXIT_PATH_FOUND", /* 0x009b */
200   "NT_STATUS_DEVICE_DATA_ERROR", /* 0x009c */
201   "NT_STATUS_DEVICE_NOT_CONNECTED", /* 0x009d */
202   "NT_STATUS_DEVICE_POWER_FAILURE", /* 0x009e */
203   "NT_STATUS_FREE_VM_NOT_AT_BASE", /* 0x009f */
204   "NT_STATUS_MEMORY_NOT_ALLOCATED", /* 0x00a0 */
205   "NT_STATUS_WORKING_SET_QUOTA", /* 0x00a1 */
206   "NT_STATUS_MEDIA_WRITE_PROTECTED", /* 0x00a2 */
207   "NT_STATUS_DEVICE_NOT_READY", /* 0x00a3 */
208   "NT_STATUS_INVALID_GROUP_ATTRIBUTES", /* 0x00a4 */
209   "NT_STATUS_BAD_IMPERSONATION_LEVEL", /* 0x00a5 */
210   "NT_STATUS_CANT_OPEN_ANONYMOUS", /* 0x00a6 */
211   "NT_STATUS_BAD_VALIDATION_CLASS", /* 0x00a7 */
212   "NT_STATUS_BAD_TOKEN_TYPE", /* 0x00a8 */
213   "NT_STATUS_BAD_MASTER_BOOT_RECORD", /* 0x00a9 */
214   "NT_STATUS_INSTRUCTION_MISALIGNMENT", /* 0x00aa */
215   "NT_STATUS_INSTANCE_NOT_AVAILABLE", /* 0x00ab */
216   "NT_STATUS_PIPE_NOT_AVAILABLE", /* 0x00ac */
217   "NT_STATUS_INVALID_PIPE_STATE", /* 0x00ad */
218   "NT_STATUS_PIPE_BUSY", /* 0x00ae */
219   "NT_STATUS_ILLEGAL_FUNCTION", /* 0x00af */
220   "NT_STATUS_PIPE_DISCONNECTED", /* 0x00b0 */
221   "NT_STATUS_PIPE_CLOSING", /* 0x00b1 */
222   "NT_STATUS_PIPE_CONNECTED", /* 0x00b2 */
223   "NT_STATUS_PIPE_LISTENING", /* 0x00b3 */
224   "NT_STATUS_INVALID_READ_MODE", /* 0x00b4 */
225   "NT_STATUS_IO_TIMEOUT", /* 0x00b5 */
226   "NT_STATUS_FILE_FORCED_CLOSED", /* 0x00b6 */
227   "NT_STATUS_PROFILING_NOT_STARTED", /* 0x00b7 */
228   "NT_STATUS_PROFILING_NOT_STOPPED", /* 0x00b8 */
229   "NT_STATUS_COULD_NOT_INTERPRET", /* 0x00b9 */
230   "NT_STATUS_FILE_IS_A_DIRECTORY", /* 0x00ba */
231   "NT_STATUS_NOT_SUPPORTED", /* 0x00bb */
232   "NT_STATUS_REMOTE_NOT_LISTENING", /* 0x00bc */
233   "NT_STATUS_DUPLICATE_NAME", /* 0x00bd */
234   "NT_STATUS_BAD_NETWORK_PATH", /* 0x00be */
235   "NT_STATUS_NETWORK_BUSY", /* 0x00bf */
236   "NT_STATUS_DEVICE_DOES_NOT_EXIST", /* 0x00c0 */
237   "NT_STATUS_TOO_MANY_COMMANDS", /* 0x00c1 */
238   "NT_STATUS_ADAPTER_HARDWARE_ERROR", /* 0x00c2 */
239   "NT_STATUS_INVALID_NETWORK_RESPONSE", /* 0x00c3 */
240   "NT_STATUS_UNEXPECTED_NETWORK_ERROR", /* 0x00c4 */
241   "NT_STATUS_BAD_REMOTE_ADAPTER", /* 0x00c5 */
242   "NT_STATUS_PRINT_QUEUE_FULL", /* 0x00c6 */
243   "NT_STATUS_NO_SPOOL_SPACE", /* 0x00c7 */
244   "NT_STATUS_PRINT_CANCELLED", /* 0x00c8 */
245   "NT_STATUS_NETWORK_NAME_DELETED", /* 0x00c9 */
246   "NT_STATUS_NETWORK_ACCESS_DENIED", /* 0x00ca */
247   "NT_STATUS_BAD_DEVICE_TYPE", /* 0x00cb */
248   "NT_STATUS_BAD_NETWORK_NAME", /* 0x00cc */
249   "NT_STATUS_TOO_MANY_NAMES", /* 0x00cd */
250   "NT_STATUS_TOO_MANY_SESSIONS", /* 0x00ce */
251   "NT_STATUS_SHARING_PAUSED", /* 0x00cf */
252   "NT_STATUS_REQUEST_NOT_ACCEPTED", /* 0x00d0 */
253   "NT_STATUS_REDIRECTOR_PAUSED", /* 0x00d1 */
254   "NT_STATUS_NET_WRITE_FAULT", /* 0x00d2 */
255   "NT_STATUS_PROFILING_AT_LIMIT", /* 0x00d3 */
256   "NT_STATUS_NOT_SAME_DEVICE", /* 0x00d4 */
257   "NT_STATUS_FILE_RENAMED", /* 0x00d5 */
258   "NT_STATUS_VIRTUAL_CIRCUIT_CLOSED", /* 0x00d6 */
259   "NT_STATUS_NO_SECURITY_ON_OBJECT", /* 0x00d7 */
260   "NT_STATUS_CANT_WAIT", /* 0x00d8 */
261   "NT_STATUS_PIPE_EMPTY", /* 0x00d9 */
262   "NT_STATUS_CANT_ACCESS_DOMAIN_INFO", /* 0x00da */
263   "NT_STATUS_CANT_TERMINATE_SELF", /* 0x00db */
264   "NT_STATUS_INVALID_SERVER_STATE", /* 0x00dc */
265   "NT_STATUS_INVALID_DOMAIN_STATE", /* 0x00dd */
266   "NT_STATUS_INVALID_DOMAIN_ROLE", /* 0x00de */
267   "NT_STATUS_NO_SUCH_DOMAIN", /* 0x00df */
268   "NT_STATUS_DOMAIN_EXISTS", /* 0x00e0 */
269   "NT_STATUS_DOMAIN_LIMIT_EXCEEDED", /* 0x00e1 */
270   "NT_STATUS_OPLOCK_NOT_GRANTED", /* 0x00e2 */
271   "NT_STATUS_INVALID_OPLOCK_PROTOCOL", /* 0x00e3 */
272   "NT_STATUS_INTERNAL_DB_CORRUPTION", /* 0x00e4 */
273   "NT_STATUS_INTERNAL_ERROR", /* 0x00e5 */
274   "NT_STATUS_GENERIC_NOT_MAPPED", /* 0x00e6 */
275   "NT_STATUS_BAD_DESCRIPTOR_FORMAT", /* 0x00e7 */
276   "NT_STATUS_INVALID_USER_BUFFER", /* 0x00e8 */
277   "NT_STATUS_UNEXPECTED_IO_ERROR", /* 0x00e9 */
278   "NT_STATUS_UNEXPECTED_MM_CREATE_ERR", /* 0x00ea */
279   "NT_STATUS_UNEXPECTED_MM_MAP_ERROR", /* 0x00eb */
280   "NT_STATUS_UNEXPECTED_MM_EXTEND_ERR", /* 0x00ec */
281   "NT_STATUS_NOT_LOGON_PROCESS", /* 0x00ed */
282   "NT_STATUS_LOGON_SESSION_EXISTS", /* 0x00ee */
283   "NT_STATUS_INVALID_PARAMETER_1", /* 0x00ef */
284   "NT_STATUS_INVALID_PARAMETER_2", /* 0x00f0 */
285   "NT_STATUS_INVALID_PARAMETER_3", /* 0x00f1 */
286   "NT_STATUS_INVALID_PARAMETER_4", /* 0x00f2 */
287   "NT_STATUS_INVALID_PARAMETER_5", /* 0x00f3 */
288   "NT_STATUS_INVALID_PARAMETER_6", /* 0x00f4 */
289   "NT_STATUS_INVALID_PARAMETER_7", /* 0x00f5 */
290   "NT_STATUS_INVALID_PARAMETER_8", /* 0x00f6 */
291   "NT_STATUS_INVALID_PARAMETER_9", /* 0x00f7 */
292   "NT_STATUS_INVALID_PARAMETER_10", /* 0x00f8 */
293   "NT_STATUS_INVALID_PARAMETER_11", /* 0x00f9 */
294   "NT_STATUS_INVALID_PARAMETER_12", /* 0x00fa */
295   "NT_STATUS_REDIRECTOR_NOT_STARTED", /* 0x00fb */
296   "NT_STATUS_REDIRECTOR_STARTED", /* 0x00fc */
297   "NT_STATUS_STACK_OVERFLOW", /* 0x00fd */
298   "NT_STATUS_NO_SUCH_PACKAGE", /* 0x00fe */
299   "NT_STATUS_BAD_FUNCTION_TABLE", /* 0x00ff */
300   "NT_STATUS_DIRECTORY_NOT_EMPTY", /* 0x0101 */
301   "NT_STATUS_FILE_CORRUPT_ERROR", /* 0x0102 */
302   "NT_STATUS_NOT_A_DIRECTORY", /* 0x0103 */
303   "NT_STATUS_BAD_LOGON_SESSION_STATE", /* 0x0104 */
304   "NT_STATUS_LOGON_SESSION_COLLISION", /* 0x0105 */
305   "NT_STATUS_NAME_TOO_LONG", /* 0x0106 */
306   "NT_STATUS_FILES_OPEN", /* 0x0107 */
307   "NT_STATUS_CONNECTION_IN_USE", /* 0x0108 */
308   "NT_STATUS_MESSAGE_NOT_FOUND", /* 0x0109 */
309   "NT_STATUS_PROCESS_IS_TERMINATING", /* 0x010a */
310   "NT_STATUS_INVALID_LOGON_TYPE", /* 0x010b */
311   "NT_STATUS_NO_GUID_TRANSLATION", /* 0x010c */
312   "NT_STATUS_CANNOT_IMPERSONATE", /* 0x010d */
313   "NT_STATUS_IMAGE_ALREADY_LOADED", /* 0x010e */
314   "NT_STATUS_ABIOS_NOT_PRESENT", /* 0x010f */
315   "NT_STATUS_ABIOS_LID_NOT_EXIST", /* 0x0110 */
316   "NT_STATUS_ABIOS_LID_ALREADY_OWNED", /* 0x0111 */
317   "NT_STATUS_ABIOS_NOT_LID_OWNER", /* 0x0112 */
318   "NT_STATUS_ABIOS_INVALID_COMMAND", /* 0x0113 */
319   "NT_STATUS_ABIOS_INVALID_LID", /* 0x0114 */
320   "NT_STATUS_ABIOS_SELECTOR_NOT_AVAILABLE", /* 0x0115 */
321   "NT_STATUS_ABIOS_INVALID_SELECTOR", /* 0x0116 */
322   "NT_STATUS_NO_LDT", /* 0x0117 */
323   "NT_STATUS_INVALID_LDT_SIZE", /* 0x0118 */
324   "NT_STATUS_INVALID_LDT_OFFSET", /* 0x0119 */
325   "NT_STATUS_INVALID_LDT_DESCRIPTOR", /* 0x011a */
326   "NT_STATUS_INVALID_IMAGE_NE_FORMAT", /* 0x011b */
327   "NT_STATUS_RXACT_INVALID_STATE", /* 0x011c */
328   "NT_STATUS_RXACT_COMMIT_FAILURE", /* 0x011d */
329   "NT_STATUS_MAPPED_FILE_SIZE_ZERO", /* 0x011e */
330   "NT_STATUS_TOO_MANY_OPENED_FILES", /* 0x011f */
331   "NT_STATUS_CANCELLED", /* 0x0120 */
332   "NT_STATUS_CANNOT_DELETE", /* 0x0121 */
333   "NT_STATUS_INVALID_COMPUTER_NAME", /* 0x0122 */
334   "NT_STATUS_FILE_DELETED", /* 0x0123 */
335   "NT_STATUS_SPECIAL_ACCOUNT", /* 0x0124 */
336   "NT_STATUS_SPECIAL_GROUP", /* 0x0125 */
337   "NT_STATUS_SPECIAL_USER", /* 0x0126 */
338   "NT_STATUS_MEMBERS_PRIMARY_GROUP", /* 0x0127 */
339   "NT_STATUS_FILE_CLOSED", /* 0x0128 */
340   "NT_STATUS_TOO_MANY_THREADS", /* 0x0129 */
341   "NT_STATUS_THREAD_NOT_IN_PROCESS", /* 0x012a */
342   "NT_STATUS_TOKEN_ALREADY_IN_USE", /* 0x012b */
343   "NT_STATUS_PAGEFILE_QUOTA_EXCEEDED", /* 0x012c */
344   "NT_STATUS_COMMITMENT_LIMIT", /* 0x012d */
345   "NT_STATUS_INVALID_IMAGE_LE_FORMAT", /* 0x012e */
346   "NT_STATUS_INVALID_IMAGE_NOT_MZ", /* 0x012f */
347   "NT_STATUS_INVALID_IMAGE_PROTECT", /* 0x0130 */
348   "NT_STATUS_INVALID_IMAGE_WIN_16", /* 0x0131 */
349   "NT_STATUS_LOGON_SERVER_CONFLICT", /* 0x0132 */
350   "NT_STATUS_TIME_DIFFERENCE_AT_DC", /* 0x0133 */
351   "NT_STATUS_SYNCHRONIZATION_REQUIRED", /* 0x0134 */
352   "NT_STATUS_DLL_NOT_FOUND", /* 0x0135 */
353   "NT_STATUS_OPEN_FAILED", /* 0x0136 */
354   "NT_STATUS_IO_PRIVILEGE_FAILED", /* 0x0137 */
355   "NT_STATUS_ORDINAL_NOT_FOUND", /* 0x0138 */
356   "NT_STATUS_ENTRYPOINT_NOT_FOUND", /* 0x0139 */
357   "NT_STATUS_CONTROL_C_EXIT", /* 0x013a */
358   "NT_STATUS_LOCAL_DISCONNECT", /* 0x013b */
359   "NT_STATUS_REMOTE_DISCONNECT", /* 0x013c */
360   "NT_STATUS_REMOTE_RESOURCES", /* 0x013d */
361   "NT_STATUS_LINK_FAILED", /* 0x013e */
362   "NT_STATUS_LINK_TIMEOUT", /* 0x013f */
363   "NT_STATUS_INVALID_CONNECTION", /* 0x0140 */
364   "NT_STATUS_INVALID_ADDRESS", /* 0x0141 */
365   "NT_STATUS_DLL_INIT_FAILED", /* 0x0142 */
366   "NT_STATUS_MISSING_SYSTEMFILE", /* 0x0143 */
367   "NT_STATUS_UNHANDLED_EXCEPTION", /* 0x0144 */
368   "NT_STATUS_APP_INIT_FAILURE", /* 0x0145 */
369   "NT_STATUS_PAGEFILE_CREATE_FAILED", /* 0x0146 */
370   "NT_STATUS_NO_PAGEFILE", /* 0x0147 */
371   "NT_STATUS_INVALID_LEVEL", /* 0x0148 */
372   "NT_STATUS_WRONG_PASSWORD_CORE", /* 0x0149 */
373   "NT_STATUS_ILLEGAL_FLOAT_CONTEXT", /* 0x014a */
374   "NT_STATUS_PIPE_BROKEN", /* 0x014b */
375   "NT_STATUS_REGISTRY_CORRUPT", /* 0x014c */
376   "NT_STATUS_REGISTRY_IO_FAILED", /* 0x014d */
377   "NT_STATUS_NO_EVENT_PAIR", /* 0x014e */
378   "NT_STATUS_UNRECOGNIZED_VOLUME", /* 0x014f */
379   "NT_STATUS_SERIAL_NO_DEVICE_INITED", /* 0x0150 */
380   "NT_STATUS_NO_SUCH_ALIAS", /* 0x0151 */
381   "NT_STATUS_MEMBER_NOT_IN_ALIAS", /* 0x0152 */
382   "NT_STATUS_MEMBER_IN_ALIAS", /* 0x0153 */
383   "NT_STATUS_ALIAS_EXISTS", /* 0x0154 */
384   "NT_STATUS_LOGON_NOT_GRANTED", /* 0x0155 */
385   "NT_STATUS_TOO_MANY_SECRETS", /* 0x0156 */
386   "NT_STATUS_SECRET_TOO_LONG", /* 0x0157 */
387   "NT_STATUS_INTERNAL_DB_ERROR", /* 0x0158 */
388   "NT_STATUS_FULLSCREEN_MODE", /* 0x0159 */
389   "NT_STATUS_TOO_MANY_CONTEXT_IDS", /* 0x015a */
390   "NT_STATUS_LOGON_TYPE_NOT_GRANTED", /* 0x015b */
391   "NT_STATUS_NOT_REGISTRY_FILE", /* 0x015c */
392   "NT_STATUS_NT_CROSS_ENCRYPTION_REQUIRED", /* 0x015d */
393   "NT_STATUS_DOMAIN_CTRLR_CONFIG_ERROR", /* 0x015e */
394   "NT_STATUS_FT_MISSING_MEMBER", /* 0x015f */
395   "NT_STATUS_ILL_FORMED_SERVICE_ENTRY", /* 0x0160 */
396   "NT_STATUS_ILLEGAL_CHARACTER", /* 0x0161 */
397   "NT_STATUS_UNMAPPABLE_CHARACTER", /* 0x0162 */
398   "NT_STATUS_UNDEFINED_CHARACTER", /* 0x0163 */
399   "NT_STATUS_FLOPPY_VOLUME", /* 0x0164 */
400   "NT_STATUS_FLOPPY_ID_MARK_NOT_FOUND", /* 0x0165 */
401   "NT_STATUS_FLOPPY_WRONG_CYLINDER", /* 0x0166 */
402   "NT_STATUS_FLOPPY_UNKNOWN_ERROR", /* 0x0167 */
403   "NT_STATUS_FLOPPY_BAD_REGISTERS", /* 0x0168 */
404   "NT_STATUS_DISK_RECALIBRATE_FAILED", /* 0x0169 */
405   "NT_STATUS_DISK_OPERATION_FAILED", /* 0x016a */
406   "NT_STATUS_DISK_RESET_FAILED", /* 0x016b */
407   "NT_STATUS_SHARED_IRQ_BUSY", /* 0x016c */
408   "NT_STATUS_FT_ORPHANING", /* 0x016d */
409   "NT_STATUS_PARTITION_FAILURE", /* 0x0172 */
410   "NT_STATUS_INVALID_BLOCK_LENGTH", /* 0x0173 */
411   "NT_STATUS_DEVICE_NOT_PARTITIONED", /* 0x0174 */
412   "NT_STATUS_UNABLE_TO_LOCK_MEDIA", /* 0x0175 */
413   "NT_STATUS_UNABLE_TO_UNLOAD_MEDIA", /* 0x0176 */
414   "NT_STATUS_EOM_OVERFLOW", /* 0x0177 */
415   "NT_STATUS_NO_MEDIA", /* 0x0178 */
416   "NT_STATUS_NO_SUCH_MEMBER", /* 0x017a */
417   "NT_STATUS_INVALID_MEMBER", /* 0x017b */
418   "NT_STATUS_KEY_DELETED", /* 0x017c */
419   "NT_STATUS_NO_LOG_SPACE", /* 0x017d */
420   "NT_STATUS_TOO_MANY_SIDS", /* 0x017e */
421   "NT_STATUS_LM_CROSS_ENCRYPTION_REQUIRED", /* 0x017f */
422   "NT_STATUS_KEY_HAS_CHILDREN", /* 0x0180 */
423   "NT_STATUS_CHILD_MUST_BE_VOLATILE", /* 0x0181 */
424   "NT_STATUS_DEVICE_CONFIGURATION_ERROR", /* 0x0182 */
425   "NT_STATUS_DRIVER_INTERNAL_ERROR", /* 0x0183 */
426   "NT_STATUS_INVALID_DEVICE_STATE", /* 0x0184 */
427   "NT_STATUS_IO_DEVICE_ERROR", /* 0x0185 */
428   "NT_STATUS_DEVICE_PROTOCOL_ERROR", /* 0x0186 */
429   "NT_STATUS_BACKUP_CONTROLLER", /* 0x0187 */
430   "NT_STATUS_LOG_FILE_FULL", /* 0x0188 */
431   "NT_STATUS_TOO_LATE", /* 0x0189 */
432   "NT_STATUS_NO_TRUST_LSA_SECRET", /* 0x018a */
433   "NT_STATUS_NO_TRUST_SAM_ACCOUNT", /* 0x018b */
434   "NT_STATUS_TRUSTED_DOMAIN_FAILURE", /* 0x018c */
435   "NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE", /* 0x018d */
436   "NT_STATUS_EVENTLOG_FILE_CORRUPT", /* 0x018e */
437   "NT_STATUS_EVENTLOG_CANT_START", /* 0x018f */
438   "NT_STATUS_TRUST_FAILURE", /* 0x0190 */
439   "NT_STATUS_MUTANT_LIMIT_EXCEEDED", /* 0x0191 */
440   "NT_STATUS_NETLOGON_NOT_STARTED", /* 0x0192 */
441   "NT_STATUS_ACCOUNT_EXPIRED", /* 0x0193 */
442   "NT_STATUS_POSSIBLE_DEADLOCK", /* 0x0194 */
443   "NT_STATUS_NETWORK_CREDENTIAL_CONFLICT", /* 0x0195 */
444   "NT_STATUS_REMOTE_SESSION_LIMIT", /* 0x0196 */
445   "NT_STATUS_EVENTLOG_FILE_CHANGED", /* 0x0197 */
446   "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT", /* 0x0198 */
447   "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT", /* 0x0199 */
448   "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT", /* 0x019a */
449   "NT_STATUS_DOMAIN_TRUST_INCONSISTENT", /* 0x019b */
450   "NT_STATUS_FS_DRIVER_REQUIRED", /* 0x019c */
451   "NT_STATUS_NO_USER_SESSION_KEY", /* 0x0202 */
452   "NT_STATUS_USER_SESSION_DELETED", /* 0x0203 */
453   "NT_STATUS_RESOURCE_LANG_NOT_FOUND", /* 0x0204 */
454   "NT_STATUS_INSUFF_SERVER_RESOURCES", /* 0x0205 */
455   "NT_STATUS_INVALID_BUFFER_SIZE", /* 0x0206 */
456   "NT_STATUS_INVALID_ADDRESS_COMPONENT", /* 0x0207 */
457   "NT_STATUS_INVALID_ADDRESS_WILDCARD", /* 0x0208 */
458   "NT_STATUS_TOO_MANY_ADDRESSES", /* 0x0209 */
459   "NT_STATUS_ADDRESS_ALREADY_EXISTS", /* 0x020a */
460   "NT_STATUS_ADDRESS_CLOSED", /* 0x020b */
461   "NT_STATUS_CONNECTION_DISCONNECTED", /* 0x020c */
462   "NT_STATUS_CONNECTION_RESET", /* 0x020d */
463   "NT_STATUS_TOO_MANY_NODES", /* 0x020e */
464   "NT_STATUS_TRANSACTION_ABORTED", /* 0x020f */
465   "NT_STATUS_TRANSACTION_TIMED_OUT", /* 0x0210 */
466   "NT_STATUS_TRANSACTION_NO_RELEASE", /* 0x0211 */
467   "NT_STATUS_TRANSACTION_NO_MATCH", /* 0x0212 */
468   "NT_STATUS_TRANSACTION_RESPONDED", /* 0x0213 */
469   "NT_STATUS_TRANSACTION_INVALID_ID", /* 0x0214 */
470   "NT_STATUS_TRANSACTION_INVALID_TYPE", /* 0x0215 */
471   "NT_STATUS_NOT_SERVER_SESSION", /* 0x0216 */
472   "NT_STATUS_NOT_CLIENT_SESSION", /* 0x0217 */
473   "NT_STATUS_CANNOT_LOAD_REGISTRY_FILE", /* 0x0218 */
474   "NT_STATUS_DEBUG_ATTACH_FAILED", /* 0x0219 */
475   "NT_STATUS_SYSTEM_PROCESS_TERMINATED", /* 0x021a */
476   "NT_STATUS_DATA_NOT_ACCEPTED", /* 0x021b */
477   "NT_STATUS_NO_BROWSER_SERVERS_FOUND", /* 0x021c */
478   "NT_STATUS_VDM_HARD_ERROR", /* 0x021d */
479   "NT_STATUS_DRIVER_CANCEL_TIMEOUT", /* 0x021e */
480   "NT_STATUS_REPLY_MESSAGE_MISMATCH", /* 0x021f */
481   "NT_STATUS_MAPPED_ALIGNMENT", /* 0x0220 */
482   "NT_STATUS_IMAGE_CHECKSUM_MISMATCH", /* 0x0221 */
483   "NT_STATUS_LOST_WRITEBEHIND_DATA", /* 0x0222 */
484   "NT_STATUS_CLIENT_SERVER_PARAMETERS_INVALID", /* 0x0223 */
485   "NT_STATUS_PASSWORD_MUST_CHANGE", /* 0x0224 */
486   "NT_STATUS_NOT_FOUND", /* 0x0225 */
487   "NT_STATUS_NOT_TINY_STREAM", /* 0x0226 */
488   "NT_STATUS_RECOVERY_FAILURE", /* 0x0227 */
489   "NT_STATUS_STACK_OVERFLOW_READ", /* 0x0228 */
490   "NT_STATUS_FAIL_CHECK", /* 0x0229 */
491   "NT_STATUS_DUPLICATE_OBJECTID", /* 0x022a */
492   "NT_STATUS_OBJECTID_EXISTS", /* 0x022b */
493   "NT_STATUS_CONVERT_TO_LARGE", /* 0x022c */
494   "NT_STATUS_RETRY", /* 0x022d */
495   "NT_STATUS_FOUND_OUT_OF_SCOPE", /* 0x022e */
496   "NT_STATUS_ALLOCATE_BUCKET", /* 0x022f */
497   "NT_STATUS_PROPSET_NOT_FOUND", /* 0x0230 */
498   "NT_STATUS_MARSHALL_OVERFLOW", /* 0x0231 */
499   "NT_STATUS_INVALID_VARIANT", /* 0x0232 */
500   "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND", /* 0x0233 */
501   "NT_STATUS_ACCOUNT_LOCKED_OUT", /* 0x0234 */
502   "NT_STATUS_HANDLE_NOT_CLOSABLE", /* 0x0235 */
503   "NT_STATUS_CONNECTION_REFUSED", /* 0x0236 */
504   "NT_STATUS_GRACEFUL_DISCONNECT", /* 0x0237 */
505   "NT_STATUS_ADDRESS_ALREADY_ASSOCIATED", /* 0x0238 */
506   "NT_STATUS_ADDRESS_NOT_ASSOCIATED", /* 0x0239 */
507   "NT_STATUS_CONNECTION_INVALID", /* 0x023a */
508   "NT_STATUS_CONNECTION_ACTIVE", /* 0x023b */
509   "NT_STATUS_NETWORK_UNREACHABLE", /* 0x023c */
510   "NT_STATUS_HOST_UNREACHABLE", /* 0x023d */
511   "NT_STATUS_PROTOCOL_UNREACHABLE", /* 0x023e */
512   "NT_STATUS_PORT_UNREACHABLE", /* 0x023f */
513   "NT_STATUS_REQUEST_ABORTED", /* 0x0240 */
514   "NT_STATUS_CONNECTION_ABORTED", /* 0x0241 */
515   "NT_STATUS_BAD_COMPRESSION_BUFFER", /* 0x0242 */
516   "NT_STATUS_USER_MAPPED_FILE", /* 0x0243 */
517   "NT_STATUS_AUDIT_FAILED", /* 0x0244 */
518   "NT_STATUS_TIMER_RESOLUTION_NOT_SET", /* 0x0245 */
519   "NT_STATUS_CONNECTION_COUNT_LIMIT", /* 0x0246 */
520   "NT_STATUS_LOGIN_TIME_RESTRICTION", /* 0x0247 */
521   "NT_STATUS_LOGIN_WKSTA_RESTRICTION", /* 0x0248 */
522   "NT_STATUS_IMAGE_MP_UP_MISMATCH", /* 0x0249 */
523   "NT_STATUS_INSUFFICIENT_LOGON_INFO", /* 0x0250 */
524   "NT_STATUS_BAD_DLL_ENTRYPOINT", /* 0x0251 */
525   "NT_STATUS_BAD_SERVICE_ENTRYPOINT", /* 0x0252 */
526   "NT_STATUS_LPC_REPLY_LOST", /* 0x0253 */
527   "NT_STATUS_IP_ADDRESS_CONFLICT1", /* 0x0254 */
528   "NT_STATUS_IP_ADDRESS_CONFLICT2", /* 0x0255 */
529   "NT_STATUS_REGISTRY_QUOTA_LIMIT", /* 0x0256 */
530   "NT_STATUS_PATH_NOT_COVERED", /* 0x0257 */
531   "NT_STATUS_NO_CALLBACK_ACTIVE", /* 0x0258 */
532   "NT_STATUS_LICENSE_QUOTA_EXCEEDED", /* 0x0259 */
533   "NT_STATUS_PWD_TOO_SHORT", /* 0x025a */
534   "NT_STATUS_PWD_TOO_RECENT", /* 0x025b */
535   "NT_STATUS_PWD_HISTORY_CONFLICT", /* 0x025c */
536   "NT_STATUS_PLUGPLAY_NO_DEVICE", /* 0x025e */
537   "NT_STATUS_UNSUPPORTED_COMPRESSION", /* 0x025f */
538   "NT_STATUS_INVALID_HW_PROFILE", /* 0x0260 */
539   "NT_STATUS_INVALID_PLUGPLAY_DEVICE_PATH", /* 0x0261 */
540   "NT_STATUS_DRIVER_ORDINAL_NOT_FOUND", /* 0x0262 */
541   "NT_STATUS_DRIVER_ENTRYPOINT_NOT_FOUND", /* 0x0263 */
542   "NT_STATUS_RESOURCE_NOT_OWNED", /* 0x0264 */
543   "NT_STATUS_TOO_MANY_LINKS", /* 0x0265 */
544   "NT_STATUS_QUOTA_LIST_INCONSISTENT", /* 0x0266 */
545   "NT_STATUS_FILE_IS_OFFLINE" /* 0x0267 */
546 };
547
548 #define NT_STATUS(a) nt_status_to_string[a & 0x3FFFFFFF]
549
550 struct Packet
551 {
552   size_t frame;
553   
554   uint8_t magic[4];
555
556   bool valid_smb() {
557     return !memcmp(smb_hdr.magic, magic, 4);
558   }
559
560   Packet(const uint8_t *data, size_t size) : 
561     ip_hdr(data + 14, size - 14),
562     tcp_hdr(data + 14 + ip_hdr.header_length,
563             size - 14 - ip_hdr.header_length),
564     smb_hdr(data + 14 + ip_hdr.header_length + tcp_hdr.length,
565             size - 14 - ip_hdr.header_length - tcp_hdr.length)
566   {
567     const uint8_t da[] = { 0xFF, 'S', 'M', 'B' };
568
569     memcpy(magic, da, sizeof(da));
570
571     if (valid_smb()) {
572       size_t len = 14 + ip_hdr.header_length + tcp_hdr.length + 36;
573
574       switch (smb_hdr.command) {
575       case NtCreateAndXRequest::COMMAND:
576         if (smb_hdr.flags & 0x80) {
577           ntcreate_resp = NtCreateAndXResponse(data+len, size - len);
578         } else {
579           ntcreate_req = NtCreateAndXRequest(data + len, size - len);
580         }
581         break;
582       case ReadAndXRequest::COMMAND:
583         if (!(smb_hdr.flags & 0x80)) {
584           read_req = ReadAndXRequest(data + len, size - len);
585         }
586         break;
587       case WriteAndXRequest::COMMAND:
588         if (!(smb_hdr.flags & 0x80)) {
589           write_req = WriteAndXRequest(data + len, size - len);
590         }
591         break;
592       case CloseRequest::COMMAND:
593         if (!(smb_hdr.flags & 0x80)) {
594           close_req = CloseRequest(data + len, size - len);
595         }
596         break;
597       }
598     }
599   }
600
601   ip ip_hdr;
602   tcp tcp_hdr;
603   smb smb_hdr;
604
605   NtCreateAndXRequest ntcreate_req;
606   NtCreateAndXResponse ntcreate_resp;
607   ReadAndXRequest read_req;
608   WriteAndXRequest write_req;
609   CloseRequest close_req;
610 };
611
612 int main(int argc, char **argv)
613 {
614   char errbuf[PCAP_ERRBUF_SIZE];
615   pcap_t *cap;
616   struct pcap_pkthdr *pkt_hdr;
617   const uint8_t *data;
618
619   static struct option long_opts[] = {
620     { "show-files", 0, 0, 's' },
621     { "drop-incomplete-sessions", 0, 0, 'i' },
622     { 0,            0, 0, 0   }
623   };
624   const char *short_opts = "si";
625   int opt_ind;
626   char ch;
627   int show_files = 0;
628   int drop_incomplete_sessions = 0;
629
630   while ((ch = getopt_long(argc,argv,short_opts,long_opts,&opt_ind)) != -1) {
631     switch (ch) {
632     case 's':
633       show_files = 1;
634       break;
635     case 'i':
636       drop_incomplete_sessions = 1;
637       break;
638     default:
639       break;
640     }
641   }
642
643   if ((argc - optind) != 1) {
644     std::cout << "Usage: " << argv[0] << " [OPTIONS] FILE" << std::endl;
645     exit(1);
646   }
647
648   cap = pcap_open_offline(argv[optind], errbuf);
649
650   if (!cap) {
651     std::cout << "pcap_open_offline(" << argv[optind] << "): " << errbuf
652               << std::endl;
653     exit(1);
654   }
655
656   std::vector<Packet> packets;
657   size_t frame = 0;
658   std::set<uint16_t> current_fids;
659
660   while (1 == pcap_next_ex(cap, &pkt_hdr, &data)) {
661     Packet packet(data, pkt_hdr->len);
662
663     ++frame;
664
665     if (packet.valid_smb()) {
666       packet.frame = frame;
667       packets.push_back(packet);
668     }
669   }
670
671   pcap_close(cap);
672
673   for (std::vector<Packet>::iterator i = packets.begin();
674        i != packets.end(); ++i) {
675     if (!(i->smb_hdr.flags & 0x80)) {
676       /* we have a request */
677       std::vector<Packet>::iterator j;
678
679       /* look ahead for the response */
680       for (j = i; j != packets.end(); ++j) {
681         if (j->smb_hdr.flags & 0x80 && // response
682             j->smb_hdr.command == i->smb_hdr.command &&
683             j->smb_hdr.tid == i->smb_hdr.tid &&
684             j->smb_hdr.pid == i->smb_hdr.pid &&
685             j->smb_hdr.uid == i->smb_hdr.uid &&
686             j->smb_hdr.mid == i->smb_hdr.mid) {
687           break;
688         }
689       }
690
691       /* no response?  guess we can't display this command */
692       if (j == packets.end()) continue;
693
694       size_t len;
695
696       switch (i->smb_hdr.command) {
697       case NtCreateAndXRequest::COMMAND:
698         std::cout << "NTCreateX \"" << i->ntcreate_req.file_name << "\" "
699                   << i->ntcreate_req.create_options << " "
700                   << i->ntcreate_req.disposition << " "
701                   << j->ntcreate_resp.fid << " "
702                   << NT_STATUS(j->smb_hdr.nt_status) << std::endl;
703         current_fids.insert(j->ntcreate_resp.fid);
704         break;
705       case ReadAndXRequest::COMMAND:
706         len = i->read_req.max_count_high * 64 * 1024 +
707           i->read_req.max_count_low;
708
709         if (!drop_incomplete_sessions || current_fids.count(i->read_req.fid)) {
710           std::cout << "ReadX " << i->read_req.fid << " "
711                     << i->read_req.offset << " "
712                     << len << " " << len << " "
713                     << NT_STATUS(j->smb_hdr.nt_status) << std::endl;
714         }
715         break;
716       case WriteAndXRequest::COMMAND:
717         len = i->write_req.data_length_hi * 64 * 1024 +
718           i->write_req.data_length_lo;
719
720         if (!drop_incomplete_sessions||current_fids.count(i->write_req.fid)) {
721           std::cout << "WriteX " << i->write_req.fid << " "
722                     << i->write_req.offset << " "
723                     << len << " " << len << " "
724                     << NT_STATUS(j->smb_hdr.nt_status) << std::endl;
725         }
726         break;
727       case CloseRequest::COMMAND:
728         if (!drop_incomplete_sessions||current_fids.count(i->close_req.fid)) {
729           std::cout << "Close " << i->close_req.fid << " "
730                     << NT_STATUS(j->smb_hdr.nt_status) << std::endl;
731         }
732         current_fids.erase(i->close_req.fid);
733         break;
734       }
735     }
736   }
737
738   return 0;
739 }