Fix files that had Gilbert's old e-mail address or that didn't have my
[obnox/wireshark/wip.git] / wiretap / iptrace.c
1 /* iptrace.c
2  *
3  * $Id: iptrace.c,v 1.24 2000/01/22 06:22:37 guy Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@xiexie.org>
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_wrappers.h"
32 #include "buffer.h"
33 #include "iptrace.h"
34
35 static int iptrace_read_1_0(wtap *wth, int *err);
36 static int iptrace_read_2_0(wtap *wth, int *err);
37 static int wtap_encap_ift(unsigned int  ift);
38 static void get_atm_pseudo_header(wtap *wth, guint8 *header, guint8 *pd);
39
40 int iptrace_open(wtap *wth, int *err)
41 {
42         int bytes_read;
43         char name[12];
44
45         file_seek(wth->fh, 0, SEEK_SET);
46         wth->data_offset = 0;
47         errno = WTAP_ERR_CANT_READ;
48         bytes_read = file_read(name, 1, 11, wth->fh);
49         if (bytes_read != 11) {
50                 *err = file_error(wth->fh);
51                 if (*err != 0)
52                         return -1;
53                 return 0;
54         }
55         wth->data_offset += 11;
56         name[11] = 0;
57
58         if (strcmp(name, "iptrace 1.0") == 0) {
59                 wth->file_type = WTAP_FILE_IPTRACE_1_0;
60                 wth->subtype_read = iptrace_read_1_0;
61         }
62         else if (strcmp(name, "iptrace 2.0") == 0) {
63                 wth->file_type = WTAP_FILE_IPTRACE_2_0;
64                 wth->subtype_read = iptrace_read_2_0;
65         }
66         else {
67                 return 0;
68         }
69
70         return 1;
71 }
72
73 /***********************************************************
74  * iptrace 1.0                                             *
75  ***********************************************************/
76
77 /* iptrace 1.0, discovered through inspection */
78 typedef struct {
79 /* 0-3 */       guint32         pkt_length;     /* packet length + 0x16 */
80 /* 4-7 */       guint32         tv_sec;         /* time stamp, seconds since the Epoch */
81 /* 8-11 */      guint32         junk1;          /* ???, not time */
82 /* 12-15 */     char            if_name[4];     /* null-terminated */
83 /* 16-27 */     char            junk2[12];      /* ??? */
84 /* 28 */        guint8          if_type;        /* BSD net/if_types.h */
85 /* 29 */        guint8          tx_flag;        /* 0=receive, 1=transmit */
86 } iptrace_1_0_phdr;
87
88 /* Read the next packet */
89 static int iptrace_read_1_0(wtap *wth, int *err)
90 {
91         int                     bytes_read;
92         int                     data_offset;
93         guint32                 packet_size;
94         guint8                  header[30];
95         guint8                  *data_ptr;
96         iptrace_1_0_phdr        pkt_hdr;
97
98         /* Read the descriptor data */
99         errno = WTAP_ERR_CANT_READ;
100         bytes_read = file_read(header, 1, 30, wth->fh);
101         if (bytes_read != 30) {
102                 *err = file_error(wth->fh);
103                 if (*err != 0)
104                         return -1;
105                 if (bytes_read != 0) {
106                         *err = WTAP_ERR_SHORT_READ;
107                         return -1;
108                 }
109                 return 0;
110         }
111         wth->data_offset += 30;
112
113         /* Read the packet data */
114         packet_size = pntohl(&header[0]) - 0x16;
115         buffer_assure_space( wth->frame_buffer, packet_size );
116         data_offset = wth->data_offset;
117         errno = WTAP_ERR_CANT_READ;
118         data_ptr = buffer_start_ptr( wth->frame_buffer );
119         bytes_read = file_read( data_ptr, 1, packet_size, wth->fh );
120
121         if (bytes_read != packet_size) {
122                 *err = file_error(wth->fh);
123                 if (*err == 0)
124                         *err = WTAP_ERR_SHORT_READ;
125                 return -1;
126         }
127         wth->data_offset += packet_size;
128
129
130         /* AIX saves time in nsec, not usec. It's easier to make iptrace
131          * files more Unix-compliant here than try to get the calling
132          * program to know when to use nsec or usec */
133
134         wth->phdr.len = packet_size;
135         wth->phdr.caplen = packet_size;
136         wth->phdr.ts.tv_sec = pntohl(&header[4]);
137         wth->phdr.ts.tv_usec = 0;
138
139         /*
140          * Byte 28 of the frame header appears to be a BSD-style IFT_xxx
141          * value giving the type of the interface.  Check out the
142          * <net/if_types.h> header file.
143          */
144         pkt_hdr.if_type = header[28];
145         wth->phdr.pkt_encap = wtap_encap_ift(pkt_hdr.if_type);
146
147         if (wth->phdr.pkt_encap == WTAP_ENCAP_UNKNOWN) {
148                 g_message("iptrace: interface type IFT=0x%02x unknown or unsupported",
149                     pkt_hdr.if_type);
150                 *err = WTAP_ERR_UNSUPPORTED;
151                 return -1;
152         }
153
154         if ( wth->phdr.pkt_encap == WTAP_ENCAP_ATM_SNIFFER ) {
155                 get_atm_pseudo_header(wth, header, data_ptr);
156         }
157
158         /* If the per-file encapsulation isn't known, set it to this
159            packet's encapsulation.
160
161            If it *is* known, and it isn't this packet's encapsulation,
162            set it to WTAP_ENCAP_PER_PACKET, as this file doesn't
163            have a single encapsulation for all packets in the file. */
164         if (wth->file_encap == WTAP_ENCAP_UNKNOWN)
165                 wth->file_encap = wth->phdr.pkt_encap;
166         else {
167                 if (wth->file_encap != wth->phdr.pkt_encap)
168                         wth->file_encap = WTAP_ENCAP_PER_PACKET;
169         }
170
171         return data_offset;
172 }
173
174 /***********************************************************
175  * iptrace 2.0                                             *
176  ***********************************************************/
177
178 /* iptrace 2.0, discovered through inspection */
179 typedef struct {
180 /* 0-3 */       guint32         pkt_length;     /* packet length + 32 */
181 /* 4-7 */       guint32         tv_sec0;        /* time stamp, seconds since the Epoch */
182 /* 8-11 */      guint32         junk1;          /* ?? */
183 /* 12-15 */     char            if_name[4];     /* null-terminated */
184 /* 16-27 */     char            if_desc[12];    /* interface description. */
185 /* 28 */        guint8          if_type;        /* BSD net/if_types.h */
186 /* 29 */        guint8          tx_flag;        /* 0=receive, 1=transmit */
187 /* 30-31 */     guint16         junk3;
188 /* 32-35 */     guint32         tv_sec;         /* time stamp, seconds since the Epoch */
189 /* 36-39 */     guint32         tv_nsec;        /* nanoseconds since that second */
190 } iptrace_2_0_phdr;
191
192 /* Read the next packet */
193 static int iptrace_read_2_0(wtap *wth, int *err)
194 {
195         int                     bytes_read;
196         int                     data_offset;
197         guint32                 packet_size;
198         guint8                  header[40];
199         guint8                  *data_ptr;
200         iptrace_2_0_phdr        pkt_hdr;
201
202         /* Read the descriptor data */
203         errno = WTAP_ERR_CANT_READ;
204         bytes_read = file_read(header, 1, 40, wth->fh);
205         if (bytes_read != 40) {
206                 *err = file_error(wth->fh);
207                 if (*err != 0)
208                         return -1;
209                 if (bytes_read != 0) {
210                         *err = WTAP_ERR_SHORT_READ;
211                         return -1;
212                 }
213                 return 0;
214         }
215         wth->data_offset += 40;
216
217         /* Read the packet data */
218         packet_size = pntohl(&header[0]) - 32;
219         buffer_assure_space( wth->frame_buffer, packet_size );
220         data_offset = wth->data_offset;
221         errno = WTAP_ERR_CANT_READ;
222         data_ptr = buffer_start_ptr( wth->frame_buffer );
223         bytes_read = file_read( data_ptr, 1, packet_size, wth->fh );
224
225         if (bytes_read != packet_size) {
226                 *err = file_error(wth->fh);
227                 if (*err == 0)
228                         *err = WTAP_ERR_SHORT_READ;
229                 return -1;
230         }
231         wth->data_offset += packet_size;
232
233
234         /* AIX saves time in nsec, not usec. It's easier to make iptrace
235          * files more Unix-compliant here than try to get the calling
236          * program to know when to use nsec or usec */
237
238         wth->phdr.len = packet_size;
239         wth->phdr.caplen = packet_size;
240         wth->phdr.ts.tv_sec = pntohl(&header[32]);
241         wth->phdr.ts.tv_usec = pntohl(&header[36]) / 1000;
242
243         /*
244          * Byte 28 of the frame header appears to be a BSD-style IFT_xxx
245          * value giving the type of the interface.  Check out the
246          * <net/if_types.h> header file.
247          */
248         pkt_hdr.if_type = header[28];
249         wth->phdr.pkt_encap = wtap_encap_ift(pkt_hdr.if_type);
250
251         if (wth->phdr.pkt_encap == WTAP_ENCAP_UNKNOWN) {
252                 g_message("iptrace: interface type IFT=0x%02x unknown or unsupported",
253                     pkt_hdr.if_type);
254                 *err = WTAP_ERR_UNSUPPORTED;
255                 return -1;
256         }
257
258         if ( wth->phdr.pkt_encap == WTAP_ENCAP_ATM_SNIFFER ) {
259                 get_atm_pseudo_header(wth, header, data_ptr);
260         }
261
262         /* If the per-file encapsulation isn't known, set it to this
263            packet's encapsulation.
264
265            If it *is* known, and it isn't this packet's encapsulation,
266            set it to WTAP_ENCAP_PER_PACKET, as this file doesn't
267            have a single encapsulation for all packets in the file. */
268         if (wth->file_encap == WTAP_ENCAP_UNKNOWN)
269                 wth->file_encap = wth->phdr.pkt_encap;
270         else {
271                 if (wth->file_encap != wth->phdr.pkt_encap)
272                         wth->file_encap = WTAP_ENCAP_PER_PACKET;
273         }
274
275         return data_offset;
276 }
277
278 /*
279  * Fill in the pseudo-header information we can; alas, "iptrace" doesn't
280  * tell us what type of traffic is in the packet - it was presumably
281  * run on a machine that was one of the endpoints of the connection, so
282  * in theory it could presumably have told us, but, for whatever reason,
283  * it failed to do so - perhaps the low-level mechanism that feeds the
284  * presumably-AAL5 frames to us doesn't have access to that information
285  * (e.g., because it's in the ATM driver, and the ATM driver merely knows
286  * that stuff on VPI/VCI X.Y should be handed up to some particular
287  * client, it doesn't know what that client is).
288  *
289  * We let our caller try to figure out what kind of traffic it is, either
290  * by guessing based on the VPI/VCI, guessing based on the header of the
291  * packet, seeing earlier traffic that set up the circuit and specified
292  * in some fashion what sort of traffic it is, or being told by the user.
293  */
294 static void
295 get_atm_pseudo_header(wtap *wth, guint8 *header, guint8 *pd)
296 {
297         char    if_text[9];
298         char    *decimal;
299         int     Vpi = 0;
300         int     Vci = 0;
301
302         /* Rip apart the "x.y" text into Vpi/Vci numbers */
303         memcpy(if_text, &header[20], 8);
304         if_text[8] = '\0';
305         decimal = strchr(if_text, '.');
306         if (decimal) {
307                 *decimal = '\0';
308                 Vpi = strtoul(if_text, NULL, 10);
309                 decimal++;
310                 Vci = strtoul(decimal, NULL, 10);
311         }
312         wth->phdr.pseudo_header.ngsniffer_atm.Vpi = Vpi;
313         wth->phdr.pseudo_header.ngsniffer_atm.Vci = Vci;
314
315         /*
316          * OK, which value means "DTE->DCE" and which value means
317          * "DCE->DTE"?
318          */
319         wth->phdr.pseudo_header.ngsniffer_atm.channel = header[29];
320
321         /* We don't have this information */
322         wth->phdr.pseudo_header.ngsniffer_atm.cells = 0;
323         wth->phdr.pseudo_header.ngsniffer_atm.aal5t_u2u = 0;
324         wth->phdr.pseudo_header.ngsniffer_atm.aal5t_len = 0;
325         wth->phdr.pseudo_header.ngsniffer_atm.aal5t_chksum = 0;
326
327         /* Assume it's AAL5 traffic, but indicate that we don't know what
328            it is beyond that. */
329         wth->phdr.pseudo_header.ngsniffer_atm.AppTrafType =
330             ATT_AAL5|ATT_HL_UNKNOWN;
331         wth->phdr.pseudo_header.ngsniffer_atm.AppHLType = AHLT_UNKNOWN;
332 }
333
334 /* Given an RFC1573 (SNMP ifType) interface type,
335  * return the appropriate Wiretap Encapsulation Type.
336  */
337 static int
338 wtap_encap_ift(unsigned int  ift)
339 {
340
341         static const int ift_encap[] = {
342 /* 0x0 */       WTAP_ENCAP_UNKNOWN,     /* nothing */
343 /* 0x1 */       WTAP_ENCAP_UNKNOWN,     /* IFT_OTHER */
344 /* 0x2 */       WTAP_ENCAP_UNKNOWN,     /* IFT_1822 */
345 /* 0x3 */       WTAP_ENCAP_UNKNOWN,     /* IFT_HDH1822 */
346 /* 0x4 */       WTAP_ENCAP_RAW_IP,      /* IFT_X25DDN */
347 /* 0x5 */       WTAP_ENCAP_UNKNOWN,     /* IFT_X25 */
348 /* 0x6 */       WTAP_ENCAP_ETHERNET,    /* IFT_ETHER */
349 /* 0x7 */       WTAP_ENCAP_UNKNOWN,     /* IFT_ISO88023 */
350 /* 0x8 */       WTAP_ENCAP_UNKNOWN,     /* IFT_ISO88024 */
351 /* 0x9 */       WTAP_ENCAP_TR,          /* IFT_ISO88025 */
352 /* 0xa */       WTAP_ENCAP_UNKNOWN,     /* IFT_ISO88026 */
353 /* 0xb */       WTAP_ENCAP_UNKNOWN,     /* IFT_STARLAN */
354 /* 0xc */       WTAP_ENCAP_UNKNOWN,     /* IFT_P10 */
355 /* 0xd */       WTAP_ENCAP_UNKNOWN,     /* IFT_P80 */
356 /* 0xe */       WTAP_ENCAP_UNKNOWN,     /* IFT_HY */
357 /* 0xf */       WTAP_ENCAP_FDDI_BITSWAPPED,     /* IFT_FDDI */
358 /* 0x10 */      WTAP_ENCAP_LAPB,        /* IFT_LAPB */  /* no data to back this up */
359 /* 0x11 */      WTAP_ENCAP_UNKNOWN,     /* IFT_SDLC */
360 /* 0x12 */      WTAP_ENCAP_UNKNOWN,     /* IFT_T1 */
361 /* 0x13 */      WTAP_ENCAP_UNKNOWN,     /* IFT_CEPT */
362 /* 0x14 */      WTAP_ENCAP_UNKNOWN,     /* IFT_ISDNBASIC */
363 /* 0x15 */      WTAP_ENCAP_UNKNOWN,     /* IFT_ISDNPRIMARY */
364 /* 0x16 */      WTAP_ENCAP_UNKNOWN,     /* IFT_PTPSERIAL */
365 /* 0x17 */      WTAP_ENCAP_UNKNOWN,     /* IFT_PPP */
366 /* 0x18 */      WTAP_ENCAP_RAW_IP,      /* IFT_LOOP */
367 /* 0x19 */      WTAP_ENCAP_UNKNOWN,     /* IFT_EON */
368 /* 0x1a */      WTAP_ENCAP_UNKNOWN,     /* IFT_XETHER */
369 /* 0x1b */      WTAP_ENCAP_UNKNOWN,     /* IFT_NSIP */
370 /* 0x1c */      WTAP_ENCAP_UNKNOWN,     /* IFT_SLIP */
371 /* 0x1d */      WTAP_ENCAP_UNKNOWN,     /* IFT_ULTRA */
372 /* 0x1e */      WTAP_ENCAP_UNKNOWN,     /* IFT_DS3 */
373 /* 0x1f */      WTAP_ENCAP_UNKNOWN,     /* IFT_SIP */
374 /* 0x20 */      WTAP_ENCAP_UNKNOWN,     /* IFT_FRELAY */
375 /* 0x21 */      WTAP_ENCAP_UNKNOWN,     /* IFT_RS232 */
376 /* 0x22 */      WTAP_ENCAP_UNKNOWN,     /* IFT_PARA */
377 /* 0x23 */      WTAP_ENCAP_UNKNOWN,     /* IFT_ARCNET */
378 /* 0x24 */      WTAP_ENCAP_UNKNOWN,     /* IFT_ARCNETPLUS */
379 /* 0x25 */      WTAP_ENCAP_ATM_SNIFFER, /* IFT_ATM */
380         };
381         #define NUM_IFT_ENCAPS (sizeof ift_encap / sizeof ift_encap[0])
382
383         if (ift < NUM_IFT_ENCAPS) {
384                 return ift_encap[ift];
385         }
386         else {
387                 return WTAP_ENCAP_UNKNOWN;
388         }
389 }