Modified code to allow compilation under IBM's C compiler for AIX.
[obnox/wireshark/wip.git] / wiretap / lanalyzer.c
1 /* lanalyzer.c
2  *
3  * $Id: lanalyzer.c,v 1.8 1999/01/29 17:06:56 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 #include <stdlib.h>
24 #include <time.h>
25 #include "wtap.h"
26 #include "lanalyzer.h"
27
28 int lanalyzer_open(wtap *wth)
29 {
30         int bytes_read;
31         char record_type[2];
32         char record_length[2];
33         char summary[210];
34         guint16 board_type, mxslc;
35         guint16 type, length;
36         guint8 cr_day, cr_month, cr_year;
37         struct tm tm;
38
39         fseek(wth->fh, 0, SEEK_SET);
40         bytes_read = fread(record_type, 1, 2, wth->fh);
41         bytes_read += fread(record_length, 1, 2, wth->fh);
42         type = pletohs(record_type);
43         length = pletohs(record_length); /* make sure to do this for while() loop */
44
45         if (bytes_read != 4) {
46                 return WTAP_FILE_UNKNOWN;
47         }
48
49         if (type != 0x1001 && type != 0x1007) {
50                 return WTAP_FILE_UNKNOWN;
51         }
52
53         /* If we made it this far, then the file is a LANAlyzer file.
54          * Let's get some info from it */
55         wth->capture.lanalyzer = g_malloc(sizeof(lanalyzer_t));
56         wth->subtype_read = lanalyzer_read;
57 /*      wth->snapshot_length = 16384; */ /* available in header as 'mxslc' */
58
59         /* Read records until we find the start of packets */
60
61         while (1) {
62                 fseek(wth->fh, length, SEEK_CUR);
63                 bytes_read = fread(record_type, 1, 2, wth->fh);
64                 bytes_read += fread(record_length, 1, 2, wth->fh);
65                 if (bytes_read != 4) {
66                         free(wth->capture.lanalyzer);
67                         return WTAP_FILE_UNKNOWN;
68                 }
69
70                 type = pletohs(record_type);
71                 length = pletohs(record_length);
72
73 /*              g_message("Record 0x%04X Length %d", type, length);*/
74                 switch (type) {
75                         /* Trace Summary Record */
76                         case 0x1002:
77                                 fread(summary, 1, 210, wth->fh);
78
79                                 /* Assume that the date of the creation of the trace file
80                                  * is the same date of the trace. Lanalyzer doesn't
81                                  * store the creation date/time of the trace, but only of
82                                  * the file. Unless you traced at 11:55 PM and saved at 00:05
83                                  * AM, the assumption that trace.date == file.date is true.
84                                  */
85                                 cr_day = summary[0];
86                                 cr_month = summary[1];
87                                 cr_year = pletohs(&summary[2]);
88                                 /*g_message("Day %d Month %d Year %d (%04X)", cr_day, cr_month,
89                                                 cr_year, cr_year);*/
90
91                                 /* Get capture start time. I learned how to do
92                                  * this from Guy's code in ngsniffer.c
93                                  */
94                                 /* this strange year offset is not in the
95                                  * lanalyzer file format documentation, but it
96                                  * works. */
97                                 tm.tm_year = cr_year - (1900 - 1792);
98                                 tm.tm_mon = cr_month - 1;
99                                 tm.tm_mday = cr_day;
100                                 tm.tm_hour = 0;
101                                 tm.tm_min = 0;
102                                 tm.tm_sec = 0;
103                                 tm.tm_isdst = -1;
104                                 wth->capture.lanalyzer->start = mktime(&tm);
105                                 /*g_message("Day %d Month %d Year %d", tm.tm_mday,
106                                                 tm.tm_mon, tm.tm_year);*/
107                                 mxslc = pletohs(&summary[30]);
108                                 wth->snapshot_length = mxslc;
109
110                                 length = 0; /* to fake the next iteration of while() */
111                                 board_type = pletohs(&summary[188]);
112                                 switch (board_type) {
113                                         case 226:
114                                                 wth->encapsulation = WTAP_ENCAP_ETHERNET;
115                                                 break;
116                                         case 227:
117                                                 wth->encapsulation = WTAP_ENCAP_TR;
118                                                 break;
119                                         default:
120                                                 wth->encapsulation = WTAP_ENCAP_NONE;
121                                 }
122                                 break;
123
124                         /* Trace Packet Data Record */
125                         case 0x1005:
126                                 wth->capture.lanalyzer->pkt_len = length - 32;
127                                 return WTAP_FILE_LANALYZER;
128
129                 /*      default: no default action */
130                 /*              printf("Record 0x%04X Length %d\n", type, length);*/
131                 }
132         } 
133
134         /* never gets here */
135         return WTAP_FILE_LANALYZER;
136 }
137
138 /* Read the next packet */
139 int lanalyzer_read(wtap *wth)
140 {
141         int packet_size = wth->capture.lanalyzer->pkt_len; /* slice, really */
142         int bytes_read;
143         char record_type[2];
144         char record_length[2];
145         guint16 type, length;
146         gchar descriptor[32];
147         int     data_offset;
148         guint16 time_low, time_med, time_high, true_size;
149         double t;
150
151         /* If this is the very first packet, then the fh cursor will already
152          * be at the start of the packet data instead of at the start of the Trace
153          * Packet Data Record. Check for this */
154         if (!packet_size) {
155
156                 /* Increment fh cursor to next record */
157                 bytes_read = fread(record_type, 1, 2, wth->fh);
158                 bytes_read += fread(record_length, 1, 2, wth->fh);
159                 if (bytes_read != 4) {
160                         return 0;
161                 }
162
163                 type = pletohs(record_type);
164                 length = pletohs(record_length);
165
166                 if (type != 0x1005) {
167                         return 0;
168                 }
169                 else {
170                         packet_size = length - 32;
171                 }
172         }
173         else {
174                 wth->capture.lanalyzer->pkt_len = 0;
175         }       
176
177         /* Read the descriptor data */
178         bytes_read = fread(descriptor, 1, 32, wth->fh);
179         if (bytes_read != 32) {
180                 g_error("lanalyzer_read: not enough descriptor data (%d bytes)",
181                                 bytes_read);
182                 return 0;
183         }
184
185         /* Read the packet data */
186         buffer_assure_space(&wth->frame_buffer, packet_size);
187         data_offset = ftell(wth->fh);
188         bytes_read = fread(buffer_start_ptr(&wth->frame_buffer), 1,
189                 packet_size, wth->fh);
190
191         if (bytes_read != packet_size) {
192                 if (ferror(wth->fh)) {
193                         g_error("lanalyzer_read: fread for data: read error\n");
194                 } else {
195                         g_error("lanalyzer_read: fread for data: %d bytes out of %d read",
196                                 bytes_read, packet_size);
197                 }
198                 return -1;
199         }
200
201         true_size = pletohs(&descriptor[4]);
202         time_low = pletohs(&descriptor[8]);
203         time_med = pletohs(&descriptor[10]);
204         time_high = pletohs(&descriptor[12]);
205
206         t = (double)time_low+(double)(time_med)*65536.0 +
207                 (double)time_high*4294967296.0;
208         t = t/1000000.0 * 0.5; /* t = # of secs */
209         t += wth->capture.lanalyzer->start;
210
211         wth->phdr.ts.tv_sec = (long)t;
212         wth->phdr.ts.tv_usec = (unsigned long)((t-(double)(wth->phdr.ts.tv_sec))
213                         *1.0e6);
214
215         wth->phdr.len = true_size - 4;
216         wth->phdr.caplen = packet_size;
217         wth->phdr.pkt_encap = wth->encapsulation;
218
219         return data_offset;
220 }