3 * $Id: lanalyzer.c,v 1.8 1999/01/29 17:06:56 gram Exp $
6 * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include "lanalyzer.h"
28 int lanalyzer_open(wtap *wth)
32 char record_length[2];
34 guint16 board_type, mxslc;
36 guint8 cr_day, cr_month, cr_year;
39 fseek(wth->fh, 0, SEEK_SET);
40 bytes_read = fread(record_type, 1, 2, wth->fh);
41 bytes_read += fread(record_length, 1, 2, wth->fh);
42 type = pletohs(record_type);
43 length = pletohs(record_length); /* make sure to do this for while() loop */
45 if (bytes_read != 4) {
46 return WTAP_FILE_UNKNOWN;
49 if (type != 0x1001 && type != 0x1007) {
50 return WTAP_FILE_UNKNOWN;
53 /* If we made it this far, then the file is a LANAlyzer file.
54 * Let's get some info from it */
55 wth->capture.lanalyzer = g_malloc(sizeof(lanalyzer_t));
56 wth->subtype_read = lanalyzer_read;
57 /* wth->snapshot_length = 16384; */ /* available in header as 'mxslc' */
59 /* Read records until we find the start of packets */
62 fseek(wth->fh, length, SEEK_CUR);
63 bytes_read = fread(record_type, 1, 2, wth->fh);
64 bytes_read += fread(record_length, 1, 2, wth->fh);
65 if (bytes_read != 4) {
66 free(wth->capture.lanalyzer);
67 return WTAP_FILE_UNKNOWN;
70 type = pletohs(record_type);
71 length = pletohs(record_length);
73 /* g_message("Record 0x%04X Length %d", type, length);*/
75 /* Trace Summary Record */
77 fread(summary, 1, 210, wth->fh);
79 /* Assume that the date of the creation of the trace file
80 * is the same date of the trace. Lanalyzer doesn't
81 * store the creation date/time of the trace, but only of
82 * the file. Unless you traced at 11:55 PM and saved at 00:05
83 * AM, the assumption that trace.date == file.date is true.
86 cr_month = summary[1];
87 cr_year = pletohs(&summary[2]);
88 /*g_message("Day %d Month %d Year %d (%04X)", cr_day, cr_month,
91 /* Get capture start time. I learned how to do
92 * this from Guy's code in ngsniffer.c
94 /* this strange year offset is not in the
95 * lanalyzer file format documentation, but it
97 tm.tm_year = cr_year - (1900 - 1792);
98 tm.tm_mon = cr_month - 1;
104 wth->capture.lanalyzer->start = mktime(&tm);
105 /*g_message("Day %d Month %d Year %d", tm.tm_mday,
106 tm.tm_mon, tm.tm_year);*/
107 mxslc = pletohs(&summary[30]);
108 wth->snapshot_length = mxslc;
110 length = 0; /* to fake the next iteration of while() */
111 board_type = pletohs(&summary[188]);
112 switch (board_type) {
114 wth->encapsulation = WTAP_ENCAP_ETHERNET;
117 wth->encapsulation = WTAP_ENCAP_TR;
120 wth->encapsulation = WTAP_ENCAP_NONE;
124 /* Trace Packet Data Record */
126 wth->capture.lanalyzer->pkt_len = length - 32;
127 return WTAP_FILE_LANALYZER;
129 /* default: no default action */
130 /* printf("Record 0x%04X Length %d\n", type, length);*/
134 /* never gets here */
135 return WTAP_FILE_LANALYZER;
138 /* Read the next packet */
139 int lanalyzer_read(wtap *wth)
141 int packet_size = wth->capture.lanalyzer->pkt_len; /* slice, really */
144 char record_length[2];
145 guint16 type, length;
146 gchar descriptor[32];
148 guint16 time_low, time_med, time_high, true_size;
151 /* If this is the very first packet, then the fh cursor will already
152 * be at the start of the packet data instead of at the start of the Trace
153 * Packet Data Record. Check for this */
156 /* Increment fh cursor to next record */
157 bytes_read = fread(record_type, 1, 2, wth->fh);
158 bytes_read += fread(record_length, 1, 2, wth->fh);
159 if (bytes_read != 4) {
163 type = pletohs(record_type);
164 length = pletohs(record_length);
166 if (type != 0x1005) {
170 packet_size = length - 32;
174 wth->capture.lanalyzer->pkt_len = 0;
177 /* Read the descriptor data */
178 bytes_read = fread(descriptor, 1, 32, wth->fh);
179 if (bytes_read != 32) {
180 g_error("lanalyzer_read: not enough descriptor data (%d bytes)",
185 /* Read the packet data */
186 buffer_assure_space(&wth->frame_buffer, packet_size);
187 data_offset = ftell(wth->fh);
188 bytes_read = fread(buffer_start_ptr(&wth->frame_buffer), 1,
189 packet_size, wth->fh);
191 if (bytes_read != packet_size) {
192 if (ferror(wth->fh)) {
193 g_error("lanalyzer_read: fread for data: read error\n");
195 g_error("lanalyzer_read: fread for data: %d bytes out of %d read",
196 bytes_read, packet_size);
201 true_size = pletohs(&descriptor[4]);
202 time_low = pletohs(&descriptor[8]);
203 time_med = pletohs(&descriptor[10]);
204 time_high = pletohs(&descriptor[12]);
206 t = (double)time_low+(double)(time_med)*65536.0 +
207 (double)time_high*4294967296.0;
208 t = t/1000000.0 * 0.5; /* t = # of secs */
209 t += wth->capture.lanalyzer->start;
211 wth->phdr.ts.tv_sec = (long)t;
212 wth->phdr.ts.tv_usec = (unsigned long)((t-(double)(wth->phdr.ts.tv_sec))
215 wth->phdr.len = true_size - 4;
216 wth->phdr.caplen = packet_size;
217 wth->phdr.pkt_encap = wth->encapsulation;