Added support for compiling on win32 with Visual C and 'nmake'. It compiles,
[obnox/wireshark/wip.git] / wiretap / iptrace.c
1 /* iptrace.c
2  *
3  * $Id: iptrace.c,v 1.4 1999/07/13 02:53:23 gram Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
7  * 
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.
12  * 
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.
17  * 
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.
21  *
22  */
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include <stdlib.h>
27 #include <time.h>
28 #include <string.h>
29 #include "wtap.h"
30 #include "buffer.h"
31 #include "iptrace.h"
32
33 int iptrace_open(wtap *wth)
34 {
35         int bytes_read;
36         char name[12];
37
38         fseek(wth->fh, 0, SEEK_SET);
39         bytes_read = fread(name, 1, 11, wth->fh);
40
41         if (bytes_read != 11) {
42                 return WTAP_FILE_UNKNOWN;
43         }
44         name[11] = 0;
45         if (strcmp(name, "iptrace 2.0") != 0) {
46                 return WTAP_FILE_UNKNOWN;
47         }
48         wth->subtype_read = iptrace_read;
49
50         return WTAP_FILE_IPTRACE;
51 }
52
53 /* Read the next packet */
54 int iptrace_read(wtap *wth)
55 {
56         int bytes_read;
57         int data_offset;
58         guint16 packet_size;
59         guint8 header[40];
60         char if_name1, if_name2;
61
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
69                  * a packet boundary
70                  */
71                 return 0;
72         }
73
74         packet_size = pntohs(&header[2]) - 32;
75
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);
81
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
87                  */
88                 if (ferror(wth->fh)) {
89                         g_print("iptrace_read: fread for data: read error\n");
90                 }
91                 return -1;
92         }
93
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;
101
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;
106         }
107         else if (if_name1 == 'e' && if_name2 == 'n') {
108                 wth->phdr.pkt_encap = WTAP_ENCAP_ETHERNET;
109         }
110         else if (if_name1 == 'f' && if_name2 == 'd') {
111                 wth->phdr.pkt_encap = WTAP_ENCAP_FDDI;
112         }
113         else {
114                 wth->phdr.pkt_encap = WTAP_ENCAP_NONE;
115         }
116         return data_offset;
117 }