Fix it so that it builds with "--disable-zlib".
[metze/wireshark/wip.git] / wiretap / iptrace.c
1 /* iptrace.c
2  *
3  * $Id: iptrace.c,v 1.12 1999/09/24 05:49:50 guy 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 <errno.h>
28 #include <time.h>
29 #include <string.h>
30 #include "wtap.h"
31 #include "file.h"
32 #include "buffer.h"
33 #include "iptrace.h"
34
35 static int iptrace_read(wtap *wth, int *err);
36
37 int iptrace_open(wtap *wth, int *err)
38 {
39         int bytes_read;
40         char name[12];
41
42         file_seek(wth->fh, 0, SEEK_SET);
43         wth->data_offset = 0;
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)) {
48                         *err = errno;
49                         return -1;
50                 }
51                 return 0;
52         }
53         wth->data_offset += 11;
54         name[11] = 0;
55         if (strcmp(name, "iptrace 2.0") != 0) {
56                 return 0;
57         }
58
59         wth->file_type = WTAP_FILE_IPTRACE;
60         wth->subtype_read = iptrace_read;
61         return 1;
62 }
63
64 /* Read the next packet */
65 static int iptrace_read(wtap *wth, int *err)
66 {
67         int bytes_read;
68         int data_offset;
69         guint16 packet_size;
70         guint8 header[40];
71         char if_name1, if_name2;
72
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)) {
78                         *err = errno;
79                         return -1;
80                 }
81                 if (bytes_read != 0) {
82                         *err = WTAP_ERR_SHORT_READ;
83                         return -1;
84                 }
85                 return 0;
86         }
87         wth->data_offset += 40;
88
89         packet_size = pntohs(&header[2]) - 32;
90
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);
97
98         if (bytes_read != packet_size) {
99                 if (file_error(wth->fh))
100                         *err = errno;
101                 else
102                         *err = WTAP_ERR_SHORT_READ;
103                 return -1;
104         }
105         wth->data_offset += packet_size;
106
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;
114
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;
119         }
120         else if (if_name1 == 'e' && if_name2 == 'n') {
121                 wth->phdr.pkt_encap = WTAP_ENCAP_ETHERNET;
122         }
123         else if (if_name1 == 'f' && if_name2 == 'd') {
124                 wth->phdr.pkt_encap = WTAP_ENCAP_FDDI_BITSWAPPED;
125         }
126         else if (if_name1 == 'l' && if_name2 == 'o') { /* loopback */
127                 wth->phdr.pkt_encap = WTAP_ENCAP_RAW_IP;
128         }
129         else if (if_name1 == 'x' && if_name2 == 'd') { /* X.25 */
130                 wth->phdr.pkt_encap = WTAP_ENCAP_RAW_IP;
131         }
132         else {
133                 g_message("iptrace: interface type %c%c unknown or unsupported",
134                     if_name1, if_name2);
135                 *err = WTAP_ERR_BAD_RECORD;
136                 return -1;
137         }
138         return data_offset;
139 }