3 * $Id: iptrace.c,v 1.4 1999/07/13 02:53:23 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.
33 int iptrace_open(wtap *wth)
38 fseek(wth->fh, 0, SEEK_SET);
39 bytes_read = fread(name, 1, 11, wth->fh);
41 if (bytes_read != 11) {
42 return WTAP_FILE_UNKNOWN;
45 if (strcmp(name, "iptrace 2.0") != 0) {
46 return WTAP_FILE_UNKNOWN;
48 wth->subtype_read = iptrace_read;
50 return WTAP_FILE_IPTRACE;
53 /* Read the next packet */
54 int iptrace_read(wtap *wth)
60 char if_name1, if_name2;
62 /* Read the descriptor data */
63 bytes_read = fread(header, 1, 40, wth->fh);
64 if (bytes_read != 40) {
65 /* because of the way we have to kill the iptrace command,
66 * the existence of a partial header or packet is probable,
67 * and we should not complain about it. Simply return
68 * quietly and pretend that the trace file ended on
74 packet_size = pntohs(&header[2]) - 32;
76 /* Read the packet data */
77 buffer_assure_space(wth->frame_buffer, packet_size);
78 data_offset = ftell(wth->fh);
79 bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1,
80 packet_size, wth->fh);
82 if (bytes_read != packet_size) {
83 /* don't complain about a partial packet. Just
84 * pretend that we reached the end of the file
85 * normally. If, however, there was a read error
86 * because of some other reason, complain
88 if (ferror(wth->fh)) {
89 g_print("iptrace_read: fread for data: read error\n");
94 wth->phdr.len = packet_size;
95 wth->phdr.caplen = packet_size;
96 wth->phdr.ts.tv_sec = pntohl(&header[32]);
97 /* AIX saves time in nsec, not usec. It's easier to make iptrace
98 * files more Unix-compliant here than try to get the calling
99 * program to know when to use nsec or usec */
100 wth->phdr.ts.tv_usec = pntohl(&header[36]) / 1000;
102 if_name1 = header[12];
103 if_name2 = header[13];
104 if (if_name1 == 't' && if_name2 == 'r') {
105 wth->phdr.pkt_encap = WTAP_ENCAP_TR;
107 else if (if_name1 == 'e' && if_name2 == 'n') {
108 wth->phdr.pkt_encap = WTAP_ENCAP_ETHERNET;
110 else if (if_name1 == 'f' && if_name2 == 'd') {
111 wth->phdr.pkt_encap = WTAP_ENCAP_FDDI;
114 wth->phdr.pkt_encap = WTAP_ENCAP_NONE;