r8323: * convert RegSetValue() calls immediately beneath the printer
[Samba/gbeck.git] / source / utils / log2pcaphex.c
blobd07dc2a2115dd86cd0dce72509323a9e3fe202b2
1 /*
2 Unix SMB/CIFS implementation.
3 Utility to extract pcap files from samba (log level 10) log files
5 Copyright (C) Jelmer Vernooij 2003
6 Thanks to Tim Potter for the genial idea
8 Portions (from capconvert.c) (C) Andrew Tridgell 1997
9 Portions (from text2pcap.c) (C) Ashok Narayanan 2001
11 Example use with -h parameter:
12 log2pcaphex < samba-log-file | text2pcap -T 139,139 - foo.pcap
14 TODO: Have correct IP and TCP checksums.
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2 of the License, or
19 (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include "includes.h"
33 /* We don't care about the paranoid malloc checker in this standalone
34 program */
35 #undef malloc
37 #include <assert.h>
39 int quiet = 0;
40 int hexformat = 0;
42 #define itoa(a) ((a) < 0xa?'0'+(a):'A' + (a-0xa))
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <sys/time.h>
47 #include <stdio.h>
48 #include <fcntl.h>
50 #define TCPDUMP_MAGIC 0xa1b2c3d4
52 /* tcpdump file format */
53 struct tcpdump_file_header {
54 uint32 magic;
55 uint16 major;
56 uint16 minor;
57 int32 zone;
58 uint32 sigfigs;
59 uint32 snaplen;
60 uint32 linktype;
63 struct tcpdump_packet {
64 struct timeval ts;
65 uint32 caplen;
66 uint32 len;
69 typedef struct {
70 uint8 ver_hdrlen;
71 uint8 dscp;
72 uint16 packet_length;
73 uint16 identification;
74 uint8 flags;
75 uint8 fragment;
76 uint8 ttl;
77 uint8 protocol;
78 uint16 hdr_checksum;
79 uint32 src_addr;
80 uint32 dest_addr;
81 } hdr_ip_t;
83 static hdr_ip_t HDR_IP = {0x45, 0, 0, 0x3412, 0, 0, 0xff, 6, 0, 0x01010101, 0x02020202};
85 typedef struct {
86 uint16 source_port;
87 uint16 dest_port;
88 uint32 seq_num;
89 uint32 ack_num;
90 uint8 hdr_length;
91 uint8 flags;
92 uint16 window;
93 uint16 checksum;
94 uint16 urg;
95 } hdr_tcp_t;
97 static hdr_tcp_t HDR_TCP = {139, 139, 0, 0, 0x50, 0, 0, 0, 0};
99 void print_pcap_header(FILE *out)
101 struct tcpdump_file_header h;
102 h.magic = TCPDUMP_MAGIC;
103 h.major = 2;
104 h.minor = 4;
105 h.zone = 0;
106 h.sigfigs = 0;
107 h.snaplen = 102400; /* As long packets as possible */
108 h.linktype = 101; /* Raw IP */
109 fwrite(&h, sizeof(struct tcpdump_file_header), 1, out);
112 void print_pcap_packet(FILE *out, unsigned char *data, long length, long caplen)
114 static int i = 0;
115 struct tcpdump_packet p;
116 i++;
117 p.ts.tv_usec = 0;
118 p.ts.tv_sec = 0;
119 p.caplen = caplen;
120 p.len = length;
121 fwrite(&p, sizeof(struct tcpdump_packet), 1, out);
122 fwrite(data, sizeof(unsigned char), caplen, out);
125 void print_hex_packet(FILE *out, unsigned char *data, long length)
127 long i,cur = 0;
128 while(cur < length) {
129 fprintf(out, "%06lX ", cur);
130 for(i = cur; i < length && i < cur + 16; i++) {
131 fprintf(out, "%02x ", data[i]);
134 cur = i;
135 fprintf(out, "\n");
139 void print_netbios_packet(FILE *out, unsigned char *data, long length, long actual_length)
141 unsigned char *newdata; long offset = 0;
142 long newlen;
144 newlen = length+sizeof(HDR_IP)+sizeof(HDR_TCP);
145 newdata = malloc(newlen);
147 HDR_IP.packet_length = htons(newlen);
148 HDR_TCP.window = htons(0x2000);
149 HDR_TCP.source_port = HDR_TCP.dest_port = htons(139);
151 memcpy(newdata+offset, &HDR_IP, sizeof(HDR_IP));offset+=sizeof(HDR_IP);
152 memcpy(newdata+offset, &HDR_TCP, sizeof(HDR_TCP));offset+=sizeof(HDR_TCP);
153 memcpy(newdata+offset,data,length);
155 print_pcap_packet(out, newdata, newlen, actual_length+offset);
156 free(newdata);
159 unsigned char *curpacket = NULL;
160 long curpacket_len = 0;
162 void read_log_msg(FILE *in, unsigned char **_buffer, long *buffersize, long *data_offset, long *data_length)
164 unsigned char *buffer;
165 int tmp; long i;
166 assert(fscanf(in, " size=%ld\n", buffersize));
167 *buffersize+=4; /* for netbios */
168 buffer = malloc(*buffersize);
169 memset(buffer, 0, *buffersize);
170 /* NetBIOS */
171 buffer[0] = 0x00;
172 buffer[1] = 0x00;
173 memcpy(buffer+2, &buffersize, 2);
174 buffer[4] = 0xFF;
175 buffer[5] = 'S';
176 buffer[6] = 'M';
177 buffer[7] = 'B';
178 assert(fscanf(in, " smb_com=0x%x\n", &tmp)); buffer[smb_com] = tmp;
179 assert(fscanf(in, " smb_rcls=%d\n", &tmp)); buffer[smb_rcls] = tmp;
180 assert(fscanf(in, " smb_reh=%d\n", &tmp)); buffer[smb_reh] = tmp;
181 assert(fscanf(in, " smb_err=%d\n", &tmp)); memcpy(buffer+smb_err, &tmp, 2);
182 assert(fscanf(in, " smb_flg=%d\n", &tmp)); buffer[smb_flg] = tmp;
183 assert(fscanf(in, " smb_flg2=%d\n", &tmp)); memcpy(buffer+smb_flg2, &tmp, 2);
184 assert(fscanf(in, " smb_tid=%d\n", &tmp)); memcpy(buffer+smb_tid, &tmp, 2);
185 assert(fscanf(in, " smb_pid=%d\n", &tmp)); memcpy(buffer+smb_pid, &tmp, 2);
186 assert(fscanf(in, " smb_uid=%d\n", &tmp)); memcpy(buffer+smb_uid, &tmp, 2);
187 assert(fscanf(in, " smb_mid=%d\n", &tmp)); memcpy(buffer+smb_mid, &tmp, 2);
188 assert(fscanf(in, " smt_wct=%d\n", &tmp)); buffer[smb_wct] = tmp;
189 for(i = 0; i < buffer[smb_wct]; i++) {
190 assert(fscanf(in, " smb_vwv[%*2d]=%*5d (0x%X)\n", &tmp));
191 memcpy(buffer+smb_vwv+i*2, &tmp, 2);
194 *data_offset = smb_vwv+buffer[smb_wct]*2;
195 assert(fscanf(in, " smb_bcc=%ld\n", data_length)); buffer[(*data_offset)] = *data_length;
196 (*data_offset)+=2;
197 *_buffer = buffer;
200 long read_log_data(FILE *in, unsigned char *buffer, long data_length)
202 long i, addr; char real[2][16]; int ret;
203 unsigned char tmp;
204 for(i = 0; i < data_length; i++) {
205 if(i % 16 == 0){
206 if(i != 0) { /* Read data after each line */
207 assert(fscanf(in, "%8s %8s", real[0], real[1]) == 2);
209 ret = fscanf(in, " [%03lX]", &addr);
210 if(!ret) {
211 if(!quiet)fprintf(stderr, "Only first %ld bytes are logged, packet trace will be incomplete\nTry a higher log level\n", i);
212 return i-1;
214 assert(addr == i);
216 if(!fscanf(in, "%02lX", &tmp)) {
217 if(!quiet)fprintf(stderr, "Only first %ld bytes are logged, packet trace will be incomplete\nTry a higher log level\n", i-1);
218 return i-1;
220 buffer[i] = tmp;
222 return data_length;
225 int main (int argc, char **argv)
227 const char *infile, *outfile;
228 FILE *out, *in;
229 int opt;
230 poptContext pc;
231 char buffer[4096];
232 long data_offset, data_length;
233 long data_bytes_read;
234 int in_packet = 0;
235 struct poptOption long_options[] = {
236 POPT_AUTOHELP
237 { "quiet", 'q', POPT_ARG_NONE, &quiet, 0, "Be quiet, don't output warnings" },
238 { "hex", 'h', POPT_ARG_NONE, &hexformat, 0, "Output format readable by text2pcap" },
239 POPT_TABLEEND
242 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
243 POPT_CONTEXT_KEEP_FIRST);
244 poptSetOtherOptionHelp(pc, "[<infile> [<outfile>]]");
247 while((opt = poptGetNextOpt(pc)) != -1) {
248 switch (opt) {
252 poptGetArg(pc); /* Drop argv[0], the program name */
254 infile = poptGetArg(pc);
256 if(infile) {
257 in = fopen(infile, "r");
258 if(!in) {
259 perror("fopen");
260 return 1;
262 } else in = stdin;
264 outfile = poptGetArg(pc);
266 if(outfile) {
267 out = fopen(outfile, "w+");
268 if(!out) {
269 perror("fopen");
270 fprintf(stderr, "Can't find %s, using stdout...\n", outfile);
274 if(!outfile) out = stdout;
276 if(!hexformat)print_pcap_header(out);
278 while(!feof(in)) {
279 fgets(buffer, sizeof(buffer), in);
280 if(buffer[0] == '[') { /* Header */
281 if(strstr(buffer, "show_msg")) {
282 in_packet++;
283 if(in_packet == 1)continue;
284 read_log_msg(in, &curpacket, &curpacket_len, &data_offset, &data_length);
285 } else if(in_packet && strstr(buffer, "dump_data")) {
286 data_bytes_read = read_log_data(in, curpacket+data_offset, data_length);
287 } else {
288 if(in_packet){
289 if(hexformat) print_hex_packet(out, curpacket, curpacket_len);
290 else print_netbios_packet(out, curpacket, curpacket_len, data_bytes_read+data_offset);
291 free(curpacket);
293 in_packet = 0;
298 return 0;