3 * $Id: lanalyzer.c,v 1.28 2001/04/16 22:06:34 guy Exp $
6 * Copyright (c) 1998 by Gilbert Ramirez <gram@xiexie.org>
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.
29 #include "file_wrappers.h"
31 #include "lanalyzer.h"
33 /* The LANalyzer format is documented (at least in part) in Novell document
34 TID022037, which can be found at, among other places:
36 http://www.hackzone.ru/nsp/info/nw/lan/trace.txt
40 #define REC_TRACE_HEADER 0x1001
41 #define REC_CYCLIC_TRACE_HEADER 0x1007
42 #define REC_TRACE_SUMMARY 0x1002
43 #define REC_TRACE_PACKET_DATA 0x1005
45 /* LANalyzer board types (which indicate the type of network on which
46 the capture was done). */
47 #define BOARD_325 226 /* LANalyzer 325 (Ethernet) */
48 #define BOARD_325TR 227 /* LANalyzer 325TR (Token-ring) */
50 static gboolean lanalyzer_read(wtap *wth, int *err, int *data_offset);
51 static void lanalyzer_close(wtap *wth);
53 int lanalyzer_open(wtap *wth, int *err)
56 char LE_record_type[2];
57 char LE_record_length[2];
59 guint16 board_type, mxslc;
60 guint16 record_type, record_length;
61 guint8 cr_day, cr_month, cr_year;
64 errno = WTAP_ERR_CANT_READ;
65 bytes_read = file_read(LE_record_type, 1, 2, wth->fh);
66 bytes_read += file_read(LE_record_length, 1, 2, wth->fh);
67 if (bytes_read != 4) {
68 *err = file_error(wth->fh);
73 wth->data_offset += 4;
74 record_type = pletohs(LE_record_type);
75 record_length = pletohs(LE_record_length); /* make sure to do this for while() loop */
77 if (record_type != REC_TRACE_HEADER && record_type != REC_CYCLIC_TRACE_HEADER) {
81 /* If we made it this far, then the file is a LANAlyzer file.
82 * Let's get some info from it. Note that we get wth->snapshot_length
83 * from a record later in the file. */
84 wth->file_type = WTAP_FILE_LANALYZER;
85 wth->capture.lanalyzer = g_malloc(sizeof(lanalyzer_t));
86 wth->subtype_read = lanalyzer_read;
87 wth->subtype_seek_read = wtap_def_seek_read;
88 wth->subtype_close = lanalyzer_close;
89 wth->snapshot_length = 0;
91 /* Read records until we find the start of packets */
93 file_seek(wth->fh, record_length, SEEK_CUR);
94 wth->data_offset += record_length;
95 errno = WTAP_ERR_CANT_READ;
96 bytes_read = file_read(LE_record_type, 1, 2, wth->fh);
97 bytes_read += file_read(LE_record_length, 1, 2, wth->fh);
98 if (bytes_read != 4) {
99 *err = file_error(wth->fh);
101 g_free(wth->capture.lanalyzer);
104 g_free(wth->capture.lanalyzer);
107 wth->data_offset += 4;
109 record_type = pletohs(LE_record_type);
110 record_length = pletohs(LE_record_length);
112 /*g_message("Record 0x%04X Length %d", record_type, record_length);*/
113 switch (record_type) {
114 /* Trace Summary Record */
115 case REC_TRACE_SUMMARY:
116 errno = WTAP_ERR_CANT_READ;
117 bytes_read = file_read(summary, 1, sizeof summary,
119 if (bytes_read != sizeof summary) {
120 *err = file_error(wth->fh);
122 g_free(wth->capture.lanalyzer);
125 g_free(wth->capture.lanalyzer);
128 wth->data_offset += sizeof summary;
130 /* Assume that the date of the creation of the trace file
131 * is the same date of the trace. Lanalyzer doesn't
132 * store the creation date/time of the trace, but only of
133 * the file. Unless you traced at 11:55 PM and saved at 00:05
134 * AM, the assumption that trace.date == file.date is true.
137 cr_month = summary[1];
138 cr_year = pletohs(&summary[2]);
139 /*g_message("Day %d Month %d Year %d (%04X)", cr_day, cr_month,
142 /* Get capture start time. I learned how to do
143 * this from Guy's code in ngsniffer.c
145 /* this strange year offset is not in the
146 * lanalyzer file format documentation, but it
148 tm.tm_year = cr_year - (1900 - 1792);
149 tm.tm_mon = cr_month - 1;
155 wth->capture.lanalyzer->start = mktime(&tm);
156 /*g_message("Day %d Month %d Year %d", tm.tm_mday,
157 tm.tm_mon, tm.tm_year);*/
158 mxslc = pletohs(&summary[30]);
159 wth->snapshot_length = mxslc;
161 record_length = 0; /* to fake the next iteration of while() */
162 board_type = pletohs(&summary[188]);
163 switch (board_type) {
165 wth->file_encap = WTAP_ENCAP_ETHERNET;
168 wth->file_encap = WTAP_ENCAP_TOKEN_RING;
171 g_message("lanalyzer: board type %u unknown",
173 g_free(wth->capture.lanalyzer);
174 *err = WTAP_ERR_UNSUPPORTED;
179 /* Trace Packet Data Record */
180 case REC_TRACE_PACKET_DATA:
181 /* Go back header number ob ytes so that lanalyzer_read
182 * can read this header */
183 file_seek(wth->fh, -bytes_read, SEEK_CUR);
184 wth->data_offset -= bytes_read;
193 #define DESCRIPTOR_LEN 32
195 /* Read the next packet */
196 static gboolean lanalyzer_read(wtap *wth, int *err, int *data_offset)
200 char LE_record_type[2];
201 char LE_record_length[2];
202 guint16 record_type, record_length;
203 gchar descriptor[DESCRIPTOR_LEN];
204 guint16 time_low, time_med, time_high, true_size;
207 /* read the record type and length. */
208 errno = WTAP_ERR_CANT_READ;
209 bytes_read = file_read(LE_record_type, 1, 2, wth->fh);
210 if (bytes_read != 2) {
211 *err = file_error(wth->fh);
212 if (*err == 0 && bytes_read != 0) {
213 *err = WTAP_ERR_SHORT_READ;
217 wth->data_offset += 2;
218 bytes_read = file_read(LE_record_length, 1, 2, wth->fh);
219 if (bytes_read != 2) {
220 *err = file_error(wth->fh);
222 *err = WTAP_ERR_SHORT_READ;
225 wth->data_offset += 2;
227 record_type = pletohs(LE_record_type);
228 record_length = pletohs(LE_record_length);
230 /* Only Trace Packet Data Records should occur now that we're in
231 * the middle of reading packets. If any other record type exists
232 * after a Trace Packet Data Record, mark it as an error. */
233 if (record_type != REC_TRACE_PACKET_DATA) {
234 g_message("lanalyzer: record type %u seen after trace summary record",
236 *err = WTAP_ERR_BAD_RECORD;
240 packet_size = record_length - DESCRIPTOR_LEN;
243 /* Read the descriptor data */
244 errno = WTAP_ERR_CANT_READ;
245 bytes_read = file_read(descriptor, 1, DESCRIPTOR_LEN, wth->fh);
246 if (bytes_read != DESCRIPTOR_LEN) {
247 *err = file_error(wth->fh);
249 *err = WTAP_ERR_SHORT_READ;
252 wth->data_offset += DESCRIPTOR_LEN;
254 /* Read the packet data */
255 buffer_assure_space(wth->frame_buffer, packet_size);
256 *data_offset = wth->data_offset;
257 errno = WTAP_ERR_CANT_READ;
258 bytes_read = file_read(buffer_start_ptr(wth->frame_buffer), 1,
259 packet_size, wth->fh);
261 if (bytes_read != packet_size) {
262 *err = file_error(wth->fh);
264 *err = WTAP_ERR_SHORT_READ;
267 wth->data_offset += packet_size;
269 true_size = pletohs(&descriptor[4]);
270 time_low = pletohs(&descriptor[8]);
271 time_med = pletohs(&descriptor[10]);
272 time_high = pletohs(&descriptor[12]);
274 t = (double)time_low+(double)(time_med)*65536.0 +
275 (double)time_high*4294967296.0;
276 t = t/1000000.0 * 0.5; /* t = # of secs */
277 t += wth->capture.lanalyzer->start;
279 wth->phdr.ts.tv_sec = (long)t;
280 wth->phdr.ts.tv_usec = (unsigned long)((t-(double)(wth->phdr.ts.tv_sec))
283 wth->phdr.len = true_size - 4;
284 wth->phdr.caplen = packet_size;
285 wth->phdr.pkt_encap = wth->file_encap;
291 lanalyzer_close(wtap *wth)
293 g_free(wth->capture.lanalyzer);