3 * $Id: iptrace.c,v 1.12 1999/09/24 05:49:50 guy 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.
35 static int iptrace_read(wtap *wth, int *err);
37 int iptrace_open(wtap *wth, int *err)
42 file_seek(wth->fh, 0, SEEK_SET);
44 errno = WTAP_ERR_CANT_READ;
45 bytes_read = file_read(name, 1, 11, wth->fh);
46 if (bytes_read != 11) {
47 if (file_error(wth->fh)) {
53 wth->data_offset += 11;
55 if (strcmp(name, "iptrace 2.0") != 0) {
59 wth->file_type = WTAP_FILE_IPTRACE;
60 wth->subtype_read = iptrace_read;
64 /* Read the next packet */
65 static int iptrace_read(wtap *wth, int *err)
71 char if_name1, if_name2;
73 /* Read the descriptor data */
74 errno = WTAP_ERR_CANT_READ;
75 bytes_read = file_read(header, 1, 40, wth->fh);
76 if (bytes_read != 40) {
77 if (file_error(wth->fh)) {
81 if (bytes_read != 0) {
82 *err = WTAP_ERR_SHORT_READ;
87 wth->data_offset += 40;
89 packet_size = pntohs(&header[2]) - 32;
91 /* Read the packet data */
92 buffer_assure_space(wth->frame_buffer, packet_size);
93 data_offset = wth->data_offset;
94 errno = WTAP_ERR_CANT_READ;
95 bytes_read = file_read(buffer_start_ptr(wth->frame_buffer), 1,
96 packet_size, wth->fh);
98 if (bytes_read != packet_size) {
99 if (file_error(wth->fh))
102 *err = WTAP_ERR_SHORT_READ;
105 wth->data_offset += packet_size;
107 wth->phdr.len = packet_size;
108 wth->phdr.caplen = packet_size;
109 wth->phdr.ts.tv_sec = pntohl(&header[32]);
110 /* AIX saves time in nsec, not usec. It's easier to make iptrace
111 * files more Unix-compliant here than try to get the calling
112 * program to know when to use nsec or usec */
113 wth->phdr.ts.tv_usec = pntohl(&header[36]) / 1000;
115 if_name1 = header[12];
116 if_name2 = header[13];
117 if (if_name1 == 't' && if_name2 == 'r') {
118 wth->phdr.pkt_encap = WTAP_ENCAP_TR;
120 else if (if_name1 == 'e' && if_name2 == 'n') {
121 wth->phdr.pkt_encap = WTAP_ENCAP_ETHERNET;
123 else if (if_name1 == 'f' && if_name2 == 'd') {
124 wth->phdr.pkt_encap = WTAP_ENCAP_FDDI_BITSWAPPED;
126 else if (if_name1 == 'l' && if_name2 == 'o') { /* loopback */
127 wth->phdr.pkt_encap = WTAP_ENCAP_RAW_IP;
129 else if (if_name1 == 'x' && if_name2 == 'd') { /* X.25 */
130 wth->phdr.pkt_encap = WTAP_ENCAP_RAW_IP;
133 g_message("iptrace: interface type %c%c unknown or unsupported",
135 *err = WTAP_ERR_BAD_RECORD;