6 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "file_wrappers.h"
29 #include "lanalyzer.h"
31 /* The LANalyzer format is documented (at least in part) in Novell document
32 TID022037, which can be found at, among other places:
34 http://www.windowsecurity.com/whitepapers/Description_of_the_LANalysers_output_file.html
37 /* Record header format */
40 guint8 record_type
[2];
41 guint8 record_length
[2];
44 #define LA_RecordHeaderSize 4
46 /* Record type codes: */
48 #define RT_HeaderRegular 0x1001
49 #define RT_HeaderCyclic 0x1007
50 #define RT_RxChannelName 0x1006
51 #define RT_TxChannelName 0x100b
52 #define RT_FilterName 0x1032
53 #define RT_RxTemplateName 0x1035
54 #define RT_TxTemplateName 0x1036
55 #define RT_DisplayOptions 0x100a
56 #define RT_Summary 0x1002
57 #define RT_SubfileSummary 0x1003
58 #define RT_CyclicInformation 0x1009
59 #define RT_Index 0x1004
60 #define RT_PacketData 0x1005
62 #define LA_ProFileLimit (1024 * 1024 * 32)
64 typedef guint8 Eadr
[6];
65 typedef guint16 TimeStamp
[3]; /* 0.5 microseconds since start of trace */
68 * These records have only 2-byte alignment for 4-byte quantities,
69 * so the structures aren't necessarily valid; they're kept as comments
70 * for reference purposes.
108 * gint16 board_version;
109 * gint8 reserved[18];
113 #define SummarySize (18+22+(4*36)+6+6+6+4+4)
120 * } LA_SummaryRecord;
123 #define LA_SummaryRecordSize (SummarySize + 4)
125 /* LANalyzer board types (which indicate the type of network on which
126 the capture was done). */
127 #define BOARD_325 226 /* LANalyzer 325 (Ethernet) */
128 #define BOARD_325TR 227 /* LANalyzer 325TR (Token-ring) */
137 * } LA_SubfileSummaryRecord;
140 #define LA_SubfileSummaryRecordSize 10
143 #define LA_IndexSize 500
149 * gint16 idxsp; = LA_IndexSize
153 * gint32 trcidx[LA_IndexSize + 2]; +2 undocumented but used by La 2.2
157 #define LA_IndexRecordSize (10 + 4 * (LA_IndexSize + 2))
162 * guint16 rx_channels;
174 * gint16 hwcollschans;
179 #define LA_PacketRecordSize 32
183 struct timeval start
;
189 static const guint8 LA_HeaderRegularFake
[] = {
190 0x01,0x10,0x4c,0x00,0x01,0x05,0x54,0x72,0x61,0x63,0x65,0x20,0x44,0x69,0x73,0x70,
191 0x6c,0x61,0x79,0x20,0x54,0x72,0x61,0x63,0x65,0x20,0x46,0x69,0x6c,0x65,0x00,0x00,
192 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
193 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
194 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
197 static const guint8 LA_RxChannelNameFake
[] = {
198 0x06,0x10,0x80,0x00,0x43,0x68,0x61,0x6e ,0x6e,0x65,0x6c,0x31,0x00,0x43,0x68,0x61,
199 0x6e,0x6e,0x65,0x6c,0x32,0x00,0x43,0x68 ,0x61,0x6e,0x6e,0x65,0x6c,0x33,0x00,0x43,
200 0x68,0x61,0x6e,0x6e,0x65,0x6c,0x34,0x00 ,0x43,0x68,0x61,0x6e,0x6e,0x65,0x6c,0x35,
201 0x00,0x43,0x68,0x61,0x6e,0x6e,0x65,0x6c ,0x36,0x00,0x43,0x68,0x61,0x6e,0x6e,0x65,
202 0x6c,0x37,0x00,0x43,0x68,0x61,0x6e,0x6e ,0x65,0x6c,0x38,0x00,0x00,0x00,0x00,0x00,
203 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
204 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
205 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
209 static const guint8 LA_TxChannelNameFake
[] = {
210 0x0b,0x10,0x36,0x00 ,0x54,0x72,0x61,0x6e,0x73,0x31,0x00,0x00,
211 0x00,0x54,0x72,0x61,0x6e,0x73,0x32,0x00 ,0x00,0x00,0x54,0x72,0x61,0x6e,0x73,0x33,
212 0x00,0x00,0x00,0x54,0x72,0x61,0x6e,0x73 ,0x34,0x00,0x00,0x00,0x54,0x72,0x61,0x6e,
213 0x73,0x35,0x00,0x00,0x00,0x54,0x72,0x61 ,0x6e,0x73,0x36,0x00,0x00,0x00
216 static const guint8 LA_RxTemplateNameFake
[] = {
218 0x90,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
219 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
220 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
221 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
222 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
223 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
224 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
225 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
226 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
230 static const guint8 LA_TxTemplateNameFake
[] = {
231 0x36,0x10,0x36,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
232 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
233 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
234 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00
237 static const guint8 LA_DisplayOptionsFake
[] = {
239 0x00,0x00,0x01,0x00,0x01,0x02,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
240 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
241 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
242 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
243 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
244 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
245 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
246 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
247 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
248 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
249 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
250 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
251 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
252 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
253 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
254 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
255 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00
258 static const guint8 LA_CyclicInformationFake
[] = {
259 0x09,0x10,0x1a,0x00,0x00,0x00,
260 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
261 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
264 static const guint8 z64
[64] = {
265 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
266 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
267 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
268 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
275 static gboolean
lanalyzer_read(wtap
*wth
, int *err
, gchar
**err_info
,
276 gint64
*data_offset
);
277 static gboolean
lanalyzer_seek_read(wtap
*wth
, gint64 seek_off
,
278 struct wtap_pkthdr
*phdr
, Buffer
*buf
, int length
,
279 int *err
, gchar
**err_info
);
280 static gboolean
lanalyzer_dump_close(wtap_dumper
*wdh
, int *err
);
282 int lanalyzer_open(wtap
*wth
, int *err
, gchar
**err_info
)
285 LA_RecordHeader rec_header
;
286 char header_fixed
[2];
289 guint16 board_type
, mxslc
;
290 guint16 record_type
, record_length
;
291 guint8 cr_day
, cr_month
;
294 lanalyzer_t
*lanalyzer
;
296 errno
= WTAP_ERR_CANT_READ
;
297 bytes_read
= file_read(&rec_header
, LA_RecordHeaderSize
, wth
->fh
);
298 if (bytes_read
!= LA_RecordHeaderSize
) {
299 *err
= file_error(wth
->fh
, err_info
);
300 if (*err
!= 0 && *err
!= WTAP_ERR_SHORT_READ
)
304 record_type
= pletohs(rec_header
.record_type
);
305 record_length
= pletohs(rec_header
.record_length
); /* make sure to do this for while() loop */
307 if (record_type
!= RT_HeaderRegular
&& record_type
!= RT_HeaderCyclic
) {
311 /* Read the major and minor version numbers */
312 if (record_length
< 2) {
314 * Not enough room for the major and minor version numbers.
315 * Just treat that as a "not a LANalyzer file" indication.
319 bytes_read
= file_read(&header_fixed
, sizeof header_fixed
, wth
->fh
);
320 if (bytes_read
!= sizeof header_fixed
) {
321 *err
= file_error(wth
->fh
, err_info
);
322 if (*err
!= 0 && *err
!= WTAP_ERR_SHORT_READ
)
326 record_length
-= sizeof header_fixed
;
328 if (record_length
!= 0) {
329 /* Read the rest of the record as a comment. */
330 comment
= (char *)g_malloc(record_length
+ 1);
331 bytes_read
= file_read(comment
, record_length
, wth
->fh
);
332 if (bytes_read
!= record_length
) {
333 *err
= file_error(wth
->fh
, err_info
);
334 if (*err
!= 0 && *err
!= WTAP_ERR_SHORT_READ
)
338 comment
[record_length
] = '\0';
339 wth
->shb_hdr
.opt_comment
= comment
;
342 /* If we made it this far, then the file is a LANAlyzer file.
343 * Let's get some info from it. Note that we get wth->snapshot_length
344 * from a record later in the file. */
345 wth
->file_type_subtype
= WTAP_FILE_TYPE_SUBTYPE_LANALYZER
;
346 lanalyzer
= (lanalyzer_t
*)g_malloc(sizeof(lanalyzer_t
));
347 wth
->priv
= (void *)lanalyzer
;
348 wth
->subtype_read
= lanalyzer_read
;
349 wth
->subtype_seek_read
= lanalyzer_seek_read
;
350 wth
->snapshot_length
= 0;
351 wth
->tsprecision
= WTAP_FILE_TSPREC_NSEC
;
353 /* Read records until we find the start of packets */
355 errno
= WTAP_ERR_CANT_READ
;
356 bytes_read
= file_read(&rec_header
, LA_RecordHeaderSize
, wth
->fh
);
357 if (bytes_read
!= LA_RecordHeaderSize
) {
358 *err
= file_error(wth
->fh
, err_info
);
360 *err
= WTAP_ERR_SHORT_READ
;
364 record_type
= pletohs(rec_header
.record_type
);
365 record_length
= pletohs(rec_header
.record_length
);
367 /*g_message("Record 0x%04X Length %d", record_type, record_length);*/
368 switch (record_type
) {
369 /* Trace Summary Record */
371 errno
= WTAP_ERR_CANT_READ
;
372 bytes_read
= file_read(summary
, sizeof summary
,
374 if (bytes_read
!= sizeof summary
) {
375 *err
= file_error(wth
->fh
, err_info
);
377 *err
= WTAP_ERR_SHORT_READ
;
381 /* Assume that the date of the creation of the trace file
382 * is the same date of the trace. Lanalyzer doesn't
383 * store the creation date/time of the trace, but only of
384 * the file. Unless you traced at 11:55 PM and saved at 00:05
385 * AM, the assumption that trace.date == file.date is true.
388 cr_month
= summary
[1];
389 cr_year
= pletohs(&summary
[2]);
390 /*g_message("Day %d Month %d Year %d (%04X)", cr_day, cr_month,
393 /* Get capture start time. I learned how to do
394 * this from Guy's code in ngsniffer.c
396 tm
.tm_year
= cr_year
- 1900;
397 tm
.tm_mon
= cr_month
- 1;
403 lanalyzer
->start
= mktime(&tm
);
404 /*g_message("Day %d Month %d Year %d", tm.tm_mday,
405 tm.tm_mon, tm.tm_year);*/
406 mxslc
= pletohs(&summary
[30]);
407 wth
->snapshot_length
= mxslc
;
409 board_type
= pletohs(&summary
[188]);
410 switch (board_type
) {
412 wth
->file_encap
= WTAP_ENCAP_ETHERNET
;
415 wth
->file_encap
= WTAP_ENCAP_TOKEN_RING
;
418 *err
= WTAP_ERR_UNSUPPORTED_ENCAP
;
419 *err_info
= g_strdup_printf("lanalyzer: board type %u unknown",
425 /* Trace Packet Data Record */
427 /* Go back header number of bytes so that lanalyzer_read
428 * can read this header */
429 if (file_seek(wth
->fh
, -LA_RecordHeaderSize
, SEEK_CUR
, err
) == -1) {
435 if (file_seek(wth
->fh
, record_length
, SEEK_CUR
, err
) == -1) {
443 #define DESCRIPTOR_LEN 32
445 static gboolean
lanalyzer_read_trace_record(wtap
*wth
, FILE_T fh
,
446 struct wtap_pkthdr
*phdr
, Buffer
*buf
, int *err
, gchar
**err_info
)
449 char LE_record_type
[2];
450 char LE_record_length
[2];
451 guint16 record_type
, record_length
;
452 int record_data_size
;
454 gchar descriptor
[DESCRIPTOR_LEN
];
455 lanalyzer_t
*lanalyzer
;
456 guint16 time_low
, time_med
, time_high
, true_size
;
460 /* read the record type and length. */
461 errno
= WTAP_ERR_CANT_READ
;
462 bytes_read
= file_read(LE_record_type
, 2, fh
);
463 if (bytes_read
!= 2) {
464 *err
= file_error(fh
, err_info
);
465 if (*err
== 0 && bytes_read
!= 0) {
466 *err
= WTAP_ERR_SHORT_READ
;
470 bytes_read
= file_read(LE_record_length
, 2, fh
);
471 if (bytes_read
!= 2) {
472 *err
= file_error(fh
, err_info
);
474 *err
= WTAP_ERR_SHORT_READ
;
478 record_type
= pletohs(LE_record_type
);
479 record_length
= pletohs(LE_record_length
);
481 /* Only Trace Packet Data Records should occur now that we're in
482 * the middle of reading packets. If any other record type exists
483 * after a Trace Packet Data Record, mark it as an error. */
484 if (record_type
!= RT_PacketData
) {
485 *err
= WTAP_ERR_BAD_FILE
;
486 *err_info
= g_strdup_printf("lanalyzer: record type %u seen after trace summary record",
491 if (record_length
< DESCRIPTOR_LEN
) {
493 * Uh-oh, the record isn't big enough to even have a
496 *err
= WTAP_ERR_BAD_FILE
;
497 *err_info
= g_strdup_printf("lanalyzer: file has a %u-byte record, too small to have even a packet descriptor",
501 record_data_size
= record_length
- DESCRIPTOR_LEN
;
503 /* Read the descriptor data */
504 errno
= WTAP_ERR_CANT_READ
;
505 bytes_read
= file_read(descriptor
, DESCRIPTOR_LEN
, fh
);
506 if (bytes_read
!= DESCRIPTOR_LEN
) {
507 *err
= file_error(fh
, err_info
);
509 *err
= WTAP_ERR_SHORT_READ
;
513 true_size
= pletohs(&descriptor
[4]);
514 packet_size
= pletohs(&descriptor
[6]);
517 * OK, is the frame data size greater than than what's left of the
520 if (packet_size
> record_data_size
) {
522 * Yes - treat this as an error.
524 *err
= WTAP_ERR_BAD_FILE
;
525 *err_info
= g_strdup("lanalyzer: Record length is less than packet size");
529 phdr
->presence_flags
= WTAP_HAS_TS
|WTAP_HAS_CAP_LEN
;
531 time_low
= pletohs(&descriptor
[8]);
532 time_med
= pletohs(&descriptor
[10]);
533 time_high
= pletohs(&descriptor
[12]);
534 t
= (((guint64
)time_low
) << 0) + (((guint64
)time_med
) << 16) +
535 (((guint64
)time_high
) << 32);
536 tsecs
= (time_t) (t
/2000000);
537 lanalyzer
= (lanalyzer_t
*)wth
->priv
;
538 phdr
->ts
.secs
= tsecs
+ lanalyzer
->start
;
539 phdr
->ts
.nsecs
= ((guint32
) (t
- tsecs
*2000000)) * 500;
541 if (true_size
- 4 >= packet_size
) {
543 * It appears that the "true size" includes the FCS;
544 * make it reflect the non-FCS size (the "packet size"
545 * appears never to include the FCS, even if no slicing
550 phdr
->len
= true_size
;
551 phdr
->caplen
= packet_size
;
553 switch (wth
->file_encap
) {
555 case WTAP_ENCAP_ETHERNET
:
556 /* We assume there's no FCS in this frame. */
557 phdr
->pseudo_header
.eth
.fcs_len
= 0;
561 /* Read the packet data */
562 return wtap_read_packet_bytes(fh
, buf
, packet_size
, err
, err_info
);
565 /* Read the next packet */
566 static gboolean
lanalyzer_read(wtap
*wth
, int *err
, gchar
**err_info
,
569 *data_offset
= file_tell(wth
->fh
);
571 /* Read the record */
572 return lanalyzer_read_trace_record(wth
, wth
->fh
, &wth
->phdr
,
573 wth
->frame_buffer
, err
, err_info
);
576 static gboolean
lanalyzer_seek_read(wtap
*wth
, gint64 seek_off
,
577 struct wtap_pkthdr
*phdr
, Buffer
*buf
, int length _U_
, int *err
,
580 if (file_seek(wth
->random_fh
, seek_off
, SEEK_SET
, err
) == -1)
583 /* Read the record */
584 if (!lanalyzer_read_trace_record(wth
, wth
->random_fh
, phdr
, buf
,
587 *err
= WTAP_ERR_SHORT_READ
;
593 /*---------------------------------------------------
594 * Returns TRUE on success, FALSE on error
595 * Write "cnt" bytes of zero with error control
596 *---------------------------------------------------*/
597 static gboolean
s0write(wtap_dumper
*wdh
, size_t cnt
, int *err
)
602 snack
= cnt
> 64 ? 64 : cnt
;
604 if (!wtap_dump_file_write(wdh
, z64
, snack
, err
))
608 return TRUE
; /* ok */
611 /*---------------------------------------------------
612 * Returns TRUE on success, FALSE on error
613 * Write an 8-bit value with error control
614 *---------------------------------------------------*/
615 static gboolean
s8write(wtap_dumper
*wdh
, const guint8 s8
, int *err
)
617 return wtap_dump_file_write(wdh
, &s8
, 1, err
);
619 /*---------------------------------------------------
620 * Returns TRUE on success, FALSE on error
621 * Write a 16-bit value with error control
622 *---------------------------------------------------*/
623 static gboolean
s16write(wtap_dumper
*wdh
, const guint16 s16
, int *err
)
625 return wtap_dump_file_write(wdh
, &s16
, 2, err
);
627 /*---------------------------------------------------
628 * Returns TRUE on success, FALSE on error
629 * Write a 32-bit value with error control
630 *---------------------------------------------------*/
631 static gboolean
s32write(wtap_dumper
*wdh
, const guint32 s32
, int *err
)
633 return wtap_dump_file_write(wdh
, &s32
, 4, err
);
635 /*---------------------------------------------------
637 * calculates C.c = A.a - B.b
638 *---------------------------------------------------*/
639 static void my_timersub(const struct timeval
*a
,
640 const struct timeval
*b
,
643 gint32 usec
= (gint32
)a
->tv_usec
;
645 c
->tv_sec
= a
->tv_sec
- b
->tv_sec
;
646 if (b
->tv_usec
> usec
) {
650 c
->tv_usec
= usec
- b
->tv_usec
;
652 /*---------------------------------------------------
653 * Write a record for a packet to a dump file.
654 * Returns TRUE on success, FALSE on failure.
655 *---------------------------------------------------*/
656 static gboolean
lanalyzer_dump(wtap_dumper
*wdh
,
657 const struct wtap_pkthdr
*phdr
,
658 const guint8
*pd
, int *err
)
665 LA_TmpInfo
*itmp
= (LA_TmpInfo
*)(wdh
->priv
);
667 int thisSize
= phdr
->caplen
+ LA_PacketRecordSize
+ LA_RecordHeaderSize
;
669 if (wdh
->bytes_dumped
+ thisSize
> LA_ProFileLimit
) {
670 /* printf(" LA_ProFileLimit reached\n"); */
672 return FALSE
; /* and don't forget the header */
675 len
= phdr
->caplen
+ (phdr
->caplen
? LA_PacketRecordSize
: 0);
677 if (!s16write(wdh
, htoles(0x1005), err
))
679 if (!s16write(wdh
, htoles(len
), err
))
682 tv
.tv_sec
= (long int) phdr
->ts
.secs
;
683 tv
.tv_usec
= phdr
->ts
.nsecs
/ 1000;
686 /* collect some information for the
687 * finally written header
689 /* XXX - this conversion could probably improved, if the start uses ns */
693 itmp
->encap
= wdh
->encap
;
697 my_timersub(&(tv
),&(itmp
->start
),&td
);
699 x
= (double) td
.tv_usec
;
700 x
+= (double) td
.tv_sec
* 1000000;
703 if (!s16write(wdh
, htoles(0x0001), err
)) /* pr.rx_channels */
705 if (!s16write(wdh
, htoles(0x0008), err
)) /* pr.rx_errors */
707 if (!s16write(wdh
, htoles(phdr
->len
+ 4), err
)) /* pr.rx_frm_len */
709 if (!s16write(wdh
, htoles(phdr
->caplen
), err
)) /* pr.rx_frm_sln */
712 for (i
= 0; i
< 3; i
++) {
713 if (!s16write(wdh
, htoles((guint16
) x
), err
)) /* pr.rx_time[i] */
718 if (!s32write(wdh
, htolel(++itmp
->pkts
), err
)) /* pr.pktno */
720 if (!s16write(wdh
, htoles(itmp
->lastlen
), err
)) /* pr.prlen */
724 if (!s0write(wdh
, 12, err
))
727 if (!wtap_dump_file_write(wdh
, pd
, phdr
->caplen
, err
))
730 wdh
->bytes_dumped
+= thisSize
;
735 /*---------------------------------------------------
736 * Returns 0 if we could write the specified encapsulation type,
737 * an error indication otherwise.
738 *---------------------------------------------------*/
739 int lanalyzer_dump_can_write_encap(int encap
)
741 /* Per-packet encapsulations aren't supported. */
742 if (encap
== WTAP_ENCAP_PER_PACKET
)
743 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED
;
745 if ( encap
!= WTAP_ENCAP_ETHERNET
746 && encap
!= WTAP_ENCAP_TOKEN_RING
)
747 return WTAP_ERR_UNSUPPORTED_ENCAP
;
749 * printf("lanalyzer_dump_can_write_encap(%d)\n",encap);
754 /*---------------------------------------------------
755 * Returns TRUE on success, FALSE on failure; sets "*err" to an
756 * error code on failure
757 *---------------------------------------------------*/
758 gboolean
lanalyzer_dump_open(wtap_dumper
*wdh
, int *err
)
763 tmp
= g_malloc(sizeof(LA_TmpInfo
));
769 ((LA_TmpInfo
*)tmp
)->init
= FALSE
;
771 wdh
->subtype_write
= lanalyzer_dump
;
772 wdh
->subtype_close
= lanalyzer_dump_close
;
774 /* Some of the fields in the file header aren't known yet so
775 just skip over it for now. It will be created after all
776 of the packets have been written. */
778 jump
= sizeof (LA_HeaderRegularFake
)
779 + sizeof (LA_RxChannelNameFake
)
780 + sizeof (LA_TxChannelNameFake
)
781 + sizeof (LA_RxTemplateNameFake
)
782 + sizeof (LA_TxTemplateNameFake
)
783 + sizeof (LA_DisplayOptionsFake
)
784 + LA_SummaryRecordSize
785 + LA_SubfileSummaryRecordSize
786 + sizeof (LA_CyclicInformationFake
)
787 + LA_IndexRecordSize
;
789 if (wtap_dump_file_seek(wdh
, jump
, SEEK_SET
, err
) == -1)
792 wdh
->bytes_dumped
= jump
;
796 /*---------------------------------------------------
798 *---------------------------------------------------*/
799 static gboolean
lanalyzer_dump_header(wtap_dumper
*wdh
, int *err
)
801 LA_TmpInfo
*itmp
= (LA_TmpInfo
*)(wdh
->priv
);
802 guint16 board_type
= itmp
->encap
== WTAP_ENCAP_TOKEN_RING
803 ? BOARD_325TR
/* LANalyzer Board Type */
804 : BOARD_325
; /* LANalyzer Board Type */
808 /* The secs variable is needed to work around 32/64-bit time_t issues.
809 itmp->start is a timeval struct, which declares its tv_sec field
810 (itmp->start.tv_sec) as a long (typically 32 bits). time_t can be 32
811 or 64 bits, depending on the platform. Invoking as follows could
812 pass a pointer to a 32-bit long where a pointer to a 64-bit time_t
813 is expected: localtime((time_t*) &(itmp->start.tv_sec)) */
814 secs
= itmp
->start
.tv_sec
;
815 fT
= localtime(&secs
);
819 if (wtap_dump_file_seek(wdh
, 0, SEEK_SET
, err
) == -1)
822 if (!wtap_dump_file_write(wdh
, &LA_HeaderRegularFake
,
823 sizeof LA_HeaderRegularFake
, err
))
825 if (!wtap_dump_file_write(wdh
, &LA_RxChannelNameFake
,
826 sizeof LA_RxChannelNameFake
, err
))
828 if (!wtap_dump_file_write(wdh
, &LA_TxChannelNameFake
,
829 sizeof LA_TxChannelNameFake
, err
))
831 if (!wtap_dump_file_write(wdh
, &LA_RxTemplateNameFake
,
832 sizeof LA_RxTemplateNameFake
, err
))
834 if (!wtap_dump_file_write(wdh
, &LA_TxTemplateNameFake
,
835 sizeof LA_TxTemplateNameFake
, err
))
837 if (!wtap_dump_file_write(wdh
, &LA_DisplayOptionsFake
,
838 sizeof LA_DisplayOptionsFake
, err
))
840 /*-----------------------------------------------------------------*/
841 if (!s16write(wdh
, htoles(RT_Summary
), err
)) /* rid */
843 if (!s16write(wdh
, htoles(SummarySize
), err
)) /* rlen */
845 if (!s8write(wdh
, (guint8
) fT
->tm_mday
, err
)) /* s.datcre.day */
847 if (!s8write(wdh
, (guint8
) (fT
->tm_mon
+1), err
)) /* s.datcre.mon */
849 if (!s16write(wdh
, htoles(fT
->tm_year
+ 1900), err
)) /* s.datcre.year */
851 if (!s8write(wdh
, (guint8
) fT
->tm_mday
, err
)) /* s.datclo.day */
853 if (!s8write(wdh
, (guint8
) (fT
->tm_mon
+1), err
)) /* s.datclo.mon */
855 if (!s16write(wdh
, htoles(fT
->tm_year
+ 1900), err
)) /* s.datclo.year */
857 if (!s8write(wdh
, (guint8
) fT
->tm_sec
, err
)) /* s.timeopn.second */
859 if (!s8write(wdh
, (guint8
) fT
->tm_min
, err
)) /* s.timeopn.minute */
861 if (!s8write(wdh
, (guint8
) fT
->tm_hour
, err
)) /* s.timeopn.hour */
863 if (!s8write(wdh
, (guint8
) fT
->tm_mday
, err
)) /* s.timeopn.mday */
865 if (!s0write(wdh
, 2, err
))
867 if (!s8write(wdh
, (guint8
) fT
->tm_sec
, err
)) /* s.timeclo.second */
869 if (!s8write(wdh
, (guint8
) fT
->tm_min
, err
)) /* s.timeclo.minute */
871 if (!s8write(wdh
, (guint8
) fT
->tm_hour
, err
)) /* s.timeclo.hour */
873 if (!s8write(wdh
, (guint8
) fT
->tm_mday
, err
)) /* s.timeclo.mday */
875 if (!s0write(wdh
, 2, err
))
877 if (!s0write(wdh
, 6, err
)) /* EAddr == 0 */
879 if (!s16write(wdh
, htoles(1), err
)) /* s.mxseqno */
881 if (!s16write(wdh
, htoles(0), err
)) /* s.slcoffo */
883 if (!s16write(wdh
, htoles(1514), err
)) /* s.mxslc */
885 if (!s32write(wdh
, htolel(itmp
->pkts
), err
)) /* s.totpktt */
892 if (!s0write(wdh
, 12, err
))
894 if (!s32write(wdh
, htolel(itmp
->pkts
), err
)) /* sr.s.mxpkta[1] */
896 if (!s0write(wdh
, 34*4, err
)) /* s.mxpkta[2-33]=0 */
898 if (!s16write(wdh
, htoles(board_type
), err
))
900 if (!s0write(wdh
, 20, err
)) /* board_version == 0 */
902 /*-----------------------------------------------------------------*/
903 if (!s16write(wdh
, htoles(RT_SubfileSummary
), err
)) /* ssr.rid */
905 if (!s16write(wdh
, htoles(LA_SubfileSummaryRecordSize
-4), err
)) /* ssr.rlen */
907 if (!s16write(wdh
, htoles(1), err
)) /* ssr.seqno */
909 if (!s32write(wdh
, htolel(itmp
->pkts
), err
)) /* ssr.totpkts */
911 /*-----------------------------------------------------------------*/
912 if (!wtap_dump_file_write(wdh
, &LA_CyclicInformationFake
,
913 sizeof LA_CyclicInformationFake
, err
))
915 /*-----------------------------------------------------------------*/
916 if (!s16write(wdh
, htoles(RT_Index
), err
)) /* rid */
918 if (!s16write(wdh
, htoles(LA_IndexRecordSize
-4), err
)) /* rlen */
920 if (!s16write(wdh
, htoles(LA_IndexSize
), err
)) /* idxsp */
922 if (!s0write(wdh
, LA_IndexRecordSize
- 6, err
))
928 /*---------------------------------------------------
929 * Finish writing to a dump file.
930 * Returns TRUE on success, FALSE on failure.
931 *---------------------------------------------------*/
932 static gboolean
lanalyzer_dump_close(wtap_dumper
*wdh
, int *err
)
934 lanalyzer_dump_header(wdh
,err
);
935 return *err
? FALSE
: TRUE
;