Fixed indentation and removed control characters, in an attempt to understand why...
[obnox/wireshark/wip.git] / wiretap / k12.c
1 /*
2  * k12.c
3  *
4  *  routines for importing tektronix k12xx *.rf5 files
5  *
6  *  Copyright (c) 2005, Luis E. Garia Ontanon <luis.ontanon@gmail.com>
7  *
8  * $Id$
9  *
10  * Wiretap Library
11  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 #include "wtap-int.h"
35 #include "wtap.h"
36 #include "file_wrappers.h"
37 #include "buffer.h"
38
39
40 static const guint8 k12_file_magic[] = { 0x00, 0x00, 0x02, 0x00 ,0x12, 0x05, 0x00, 0x10 };
41
42 #define K12_REC_PACKET  0x00010020
43 #define K12_REC_SRCDSC  0x00070041
44
45 /* XXX: we don't know what is in these type of records */
46 #define K12_REC_UNK001  0x00070040
47 #define K12_REC_UNK002  0x00070042
48 #define K12_REC_UNK003  0x00070044
49
50 /* So far we've seen the following appear only at the end of the file */
51 #define K12_REC_UNK004  0x00020030
52 #define K12_REC_UNK005  0x00020031
53
54 #define K12_HDR_LEN 0x10
55
56 typedef struct {
57         guint32 len;
58         guint32 type;
59         guint32 frame_len;
60         guint32 port_id;
61 } k12_record_hdr_t;
62
63 typedef struct  {
64         gchar* name;
65         guint32 encap;
66 } k12_stack_encap_t;
67
68 typedef struct  {
69         guint32 port_id;
70         guint32 encap;
71 } k12_port_encap_t;
72
73 struct _k12_t {
74         k12_stack_encap_t* stack_encap;
75         guint stack_encap_p;
76         GPtrArray *port_encaps;
77         guint32 file_len;
78 };
79
80 static const k12_stack_encap_t virgin_stack_encap[] = {
81         {NULL,WTAP_ENCAP_USER0},
82         {NULL,WTAP_ENCAP_USER1},
83         {NULL,WTAP_ENCAP_USER2},
84         {NULL,WTAP_ENCAP_USER3},
85         {NULL,WTAP_ENCAP_USER4},
86         {NULL,WTAP_ENCAP_USER5},
87         {NULL,WTAP_ENCAP_USER6},
88         {NULL,WTAP_ENCAP_USER7},
89         {NULL,WTAP_ENCAP_USER8},
90         {NULL,WTAP_ENCAP_USER9},
91         {NULL,WTAP_ENCAP_USER10},
92         {NULL,WTAP_ENCAP_USER11},
93         {NULL,WTAP_ENCAP_USER12},
94         {NULL,WTAP_ENCAP_USER13},
95         {NULL,WTAP_ENCAP_USER14},
96         /*      {NULL,WTAP_ENCAP_USER15},  used for unnknown sources */
97         {NULL,0}        
98 };
99
100
101 static guint32 choose_encap(k12_t* file_data, guint32 port_id, gchar* stack_name) {
102         guint32 encap = 0;
103         k12_port_encap_t* pe;
104         guint i;
105         
106         for (i =0; i < file_data->stack_encap_p; i++) {
107                 
108                 if (strcmp(stack_name,file_data->stack_encap[i].name) == 0) {
109                         encap = file_data->stack_encap[i].encap;
110                         g_free(stack_name);
111                         break;
112                 }
113         }
114         
115         if (file_data->stack_encap_p > 14) {
116                 /* g_warning("k12_choose_encap: Cannot handle more than 15 stack types"); */
117                 return WTAP_ENCAP_USER15;
118         }
119         
120         if ( encap == 0 ) {
121                 file_data->stack_encap[file_data->stack_encap_p].name = stack_name;
122                 encap = file_data->stack_encap[file_data->stack_encap_p].encap;
123         }
124         
125         pe = g_malloc(sizeof(k12_port_encap_t));
126         pe->port_id = port_id;
127         pe->encap = encap;
128         
129         g_ptr_array_add(file_data->port_encaps,pe);
130         return encap;
131 }
132
133 static guint32 get_encap(k12_t* file_data, guint32 port_id) {
134         guint i;
135         k12_port_encap_t* pe;
136         
137         for (i = 0; i < file_data->port_encaps->len; i++) {
138                 pe = g_ptr_array_index(file_data->port_encaps,i);
139                 
140                 if (pe->port_id == port_id)
141                         return pe->encap;
142         }
143         
144         /*g_warning("k12_get_encap: BUG: found no encapsulation for source 0x%.8x\n"
145                 "please report this to ethereal-dev@ethereal.com", port_id);*/
146         
147         return WTAP_ENCAP_USER15;
148 }
149
150
151
152 /*
153  * get_k12_hdr:  hunt for the next valid header in the file.
154  *   will return:
155  *      -2 on I/O errors
156  *              -1 at EOF
157  *       the lenght of the preamble (0 if none) if OK.
158  *
159  *   Record headers are 4 4byte words long,
160  *       - the first is the lenght of the record
161  *       - the second is the type of the record
162  *       - the third is the lenght of the frame in packet records
163  *               - the last is the source id to which it refers
164  *
165  *   Every about 0x2000 bytes up to 4 words are inserted in the file,
166  *   not being able yet to understand *exactly* how and where these
167  *   are inserted we need to scan the file for the next valid header.
168  *
169  */
170 gboolean get_k12_hdr(k12_record_hdr_t* hdr, wtap* wth, int* err, gchar **err_info) {
171         guint8 hdr_buf[0x14]; /* five 32bit "slots" */
172         guint32 magic;
173         guint i;
174         guint len;
175         
176         /*
177          * XXX: as most records are contiguous we could
178          * avoid hunting when not in the "risky zones".
179          *
180          * gboolean risky = ( (file_offset-0x210) % 0x2000) > 0x1f00 || 
181          *                    (file_offset-0x210) % 0x2000) < 0x0100   );
182          *
183          * We'll take the conservative approach and avoid trouble altogether.
184          */
185         
186         /* read the first three words inserting them from the second slot on */
187         
188         if ((len = file_read(hdr_buf + 0x4, 1, 0xC, wth->fh)) != 0xC) {
189                 if (len == 2) {
190                         if (hdr_buf[0x4] == 0xff && hdr_buf[0x5] == 0xff) {
191                                 return -1;
192                         }
193                 }
194                 
195                 *err = file_error(wth->fh);
196                 if (*err == 0)
197                         *err = WTAP_ERR_SHORT_READ;
198                 return -2;
199         }
200         
201         do {
202                 
203                 /*
204                  * XXX: The stuffing should be be at most 0x10.
205                  *
206                  * We do not know if the record types we know are all of them.
207                  *
208                  * Instead of failing we could  try to skip a record whose type we do
209                  * not know yet. In that case however it is possible that a "magic"
210                  * number appears in the record and unpredictable things would happen.
211                  * We won't try, we'll fail and ask for feedback.
212                  */
213                 if ( len > 0x20) {
214                         /*
215                          g_warning("get_k12_hdr: found more than 4 words of stuffing, this should not happen!\n"
216                                            "please report this issue to ethereal-dev@ethereal.com");
217                          */
218                         return -2;
219                 }
220                 
221                 /* read the next word into the last slot */
222                 if ( file_read( hdr_buf + K12_HDR_LEN, 1, 0x4, wth->fh) != 0x4 ) {
223                         *err = WTAP_ERR_SHORT_READ;
224                         *err_info = "record too short while reading .rf5 file";
225                         return -2;
226                 }
227                 
228                 len += 0x4;
229                 
230                 /* shift the buffer one word left */
231                 /* XXX: working with words this would be faster */
232                 for ( i = 0 ; i < 16 ; i++ )
233                         hdr_buf[i] = hdr_buf[i + 0x4]; 
234                 
235                 /* we'll be done if the second word is a magic number */
236                 magic = pntohl( hdr_buf + 0x4 );
237                 
238         } while (magic != K12_REC_PACKET &&
239                          magic != K12_REC_SRCDSC &&
240                          magic != K12_REC_UNK001 &&
241                          magic != K12_REC_UNK002 &&
242                          magic != K12_REC_UNK003 &&
243                          magic != K12_REC_UNK004 &&
244                          magic != K12_REC_UNK005 );
245         
246         hdr->len = 0x0000FFFF & pntohl( hdr_buf );      /* the first two bytes off the record len may contain junk */
247         hdr->type = magic;
248         hdr->frame_len = 0x0000FFFF & pntohl( hdr_buf + 0x8 );
249         hdr->port_id = pntohl( hdr_buf + 0xC );
250         
251         return len - K12_HDR_LEN;
252 }
253
254 static gboolean k12_read(wtap *wth, int *err, gchar **err_info, long *data_offset) {
255         guint64 ts;
256         guint8 b[8];
257         guint8* junk[0x1000];
258         k12_record_hdr_t hdr;
259         gint stuffing = -1;
260         
261         *data_offset = wth->data_offset;
262         
263         /* ignore the record if it isn't a packet */    
264         do {
265                 gint s;
266                 
267                 if (stuffing >= 0) {
268                         stuffing += hdr.len;
269                         
270                         /* skip the whole record */
271                         
272                         if ( file_read(junk,1, hdr.len - K12_HDR_LEN , wth->fh) != (gint) (hdr.len - K12_HDR_LEN) ) {
273                                 *err = WTAP_ERR_SHORT_READ;
274                                 *err_info = "record too short while reading .rf5 file";
275                                 return FALSE;           
276                         }
277                         
278                 } else if (stuffing < 0) {
279                         stuffing = 0;
280                 }
281                 
282                 switch ( s = get_k12_hdr(&hdr, wth, err, err_info) ) {
283                         case -1:
284                                 /* eof */
285                                 *err = 0;
286                                 *data_offset = wth->data_offset = wth->capture.k12->file_len;
287                                 return FALSE;
288                         case -2:
289                                 /* io_error */
290                                 return FALSE;
291                         default:
292                                 break;
293                 }
294                 
295                 stuffing += s;
296                 
297         } while ( hdr.type != K12_REC_PACKET
298                           || hdr.len < hdr.frame_len + 0x20 );
299         
300         wth->data_offset += stuffing + 0x10;
301         
302         if ( wth->file_encap == WTAP_ENCAP_PER_PACKET) {
303                 wth->phdr.pkt_encap = get_encap(wth->capture.k12,hdr.port_id);
304         } else {
305                 wth->phdr.pkt_encap = WTAP_ENCAP_USER0;
306         }
307         
308         /* XXX: is in there something useful in these 8 bytes ? */
309         if ( file_read(b,1,8,wth->fh) != 8 ) {
310                 *err = WTAP_ERR_SHORT_READ;
311                 *err_info = "record too short while reading .rf5 file";
312                 return FALSE; 
313         }
314         
315         wth->data_offset += 8;
316         
317         
318         /* the next 8 bytes are the timestamp */
319         if ( file_read(b,1,8,wth->fh) != 8 ) {
320                 *err = WTAP_ERR_SHORT_READ;
321                 *err_info = "record too short while reading .rf5 file";
322                 return FALSE; 
323         }
324         
325         wth->data_offset += 8;
326         
327         ts = pntohll(b);
328         
329         wth->phdr.ts.tv_usec = (guint32) ( (ts % 2000000) / 2);
330         wth->phdr.ts.tv_sec = (guint32) ((ts / 2000000) + 631152000);
331         
332         wth->phdr.caplen = wth->phdr.len = hdr.frame_len;
333         
334         /* the frame */
335         buffer_assure_space(wth->frame_buffer, hdr.frame_len);
336         wtap_file_read_expected_bytes(buffer_start_ptr(wth->frame_buffer), hdr.frame_len, wth->fh, err);
337         wth->data_offset += hdr.frame_len;
338         
339         /* XXX: should we read to a junk buffer instead of seeking? */
340         /* XXX: is there useful stuff in the trailer? */
341         if ( file_read(junk,1, hdr.len - ( hdr.frame_len + 0x20) , wth->fh) != (gint) ( hdr.len - ( hdr.frame_len + 0x20)) ) {
342                 *err = WTAP_ERR_SHORT_READ;
343                 *err_info = "record too short while reading .rf5 file";
344                 return FALSE;           
345         }
346         
347         wth->data_offset += hdr.len - ( hdr.frame_len + 0x20);
348         
349         return TRUE;
350 }
351
352 static gboolean k12_seek_read(wtap *wth, long seek_off, union wtap_pseudo_header *pseudo_header _U_, guchar *pd, int length, int *err _U_, gchar **err_info _U_) {
353         
354         if ( file_seek(wth->random_fh, seek_off+0x20, SEEK_SET, err) == -1)
355                 return FALSE;
356         
357         if ( file_read(pd, 1, length, wth->random_fh) != length) {
358                 *err = file_error(wth->random_fh);
359                 if (*err == 0)
360                         *err = WTAP_ERR_SHORT_READ;
361                 return FALSE;
362         }
363         
364         return TRUE;
365 }
366
367 static void destroy_k12_file_data(k12_t* file_data) {
368         guint i;
369         for (i =0; i<=file_data->stack_encap_p; i++) {
370                 if (file_data->stack_encap[i].name) {
371                         g_free(file_data->stack_encap[i].name);
372                         file_data->stack_encap[i].name = NULL;
373                 }
374         }
375         
376         if (file_data->port_encaps) {
377                 g_ptr_array_free(file_data->port_encaps,TRUE);
378         }
379         
380 }
381
382 static void k12_close(wtap *wth) {
383         destroy_k12_file_data(wth->capture.k12);
384 }
385
386 /*
387  * The first few records of a file contain a description of the file:
388  *   - the description of the sources (ports or circuits)
389  *   - some other useless or yet unknown data.
390  *
391  * After that we'll find the packet records. At the end sometimes we find
392  * some other (summary?) records.
393  */
394
395 int k12_open(wtap *wth, int *err, gchar **err_info) {
396         gchar read_buffer[0x1000];
397         k12_record_hdr_t hdr;
398         long offset = 0;
399         gchar* stack_file;
400         gchar* port_name;
401         guint32 name_len;
402         guint32 stack_len;
403         gint read_len;
404         guint stuffing;
405         k12_t* file_data;
406         
407         /*
408          *  let's check the magic number.
409          */
410         if ( file_read(read_buffer,1,8,wth->fh) != 8 ) {
411                 return -1;
412         } else {
413                 if ( memcmp(read_buffer,k12_file_magic,8) != 0 )
414                         return 0;
415         }
416         
417         /* the lenght of the file is in the next 4byte word */
418         if ( file_read(read_buffer,1,4,wth->fh) != 4 ) {
419                 return -1;
420         } 
421         
422         file_data = g_malloc(sizeof(k12_t));
423         
424         file_data->stack_encap_p = 0;
425         file_data->port_encaps = g_ptr_array_new();
426         file_data->stack_encap = g_memdup(virgin_stack_encap,sizeof(virgin_stack_encap));
427         file_data->file_len = pntohl( read_buffer );
428         
429         /*
430          * we don't know yet what's in the file header
431          */
432         if (file_read(read_buffer,1,0x204,wth->fh) != 0x204 ) {
433                 destroy_k12_file_data(file_data);
434                 return -1;
435         }
436         
437         wth->data_offset = offset = 0x210;
438         
439         /*
440          * start reading the records until we hit the first packet record
441          */
442         
443         do {
444                 if (offset > 0x10000) {
445                         /* too much to be ok. */
446                         return 0;
447                 }
448                 
449                 stuffing = get_k12_hdr(&hdr, wth, err, err_info);
450                 
451                 offset += stuffing;
452                 
453                 if ( hdr.type == K12_REC_PACKET) {
454                         /*
455                          * we are at the first packet record, rewind and leave.
456                          */
457                         if (file_seek(wth->fh, -0x10, SEEK_CUR, err) == -1) {
458                                 destroy_k12_file_data(file_data);
459                                 return -1;
460                         }
461                         
462                         break;
463                 } else if (hdr.type == K12_REC_SRCDSC) {
464                         
465                         if ( file_read( read_buffer, 1, 0x14, wth->fh) != 0x14 ) {
466                                 *err = WTAP_ERR_SHORT_READ;
467                                 return FALSE;
468                         }
469                         
470                         name_len = pntohs( read_buffer + 0x10 );
471                         stack_len = pntohs( read_buffer + 0x12 );
472                         
473                         
474                         read_len = hdr.len - (0x10 + 0x14 + name_len + stack_len);
475                         
476                         if (read_len > 0) {
477                                 /* skip the still unknown part */
478                                 if (file_read(read_buffer,1, read_len,wth->fh) != read_len ) {
479                                         destroy_k12_file_data(file_data);
480                                         *err = WTAP_ERR_SHORT_READ;
481                                         return -1;
482                                 }
483                         } else if (read_len < 0) {
484                                 destroy_k12_file_data(file_data);
485                                 *err = WTAP_ERR_BAD_RECORD;
486                                 return -1;
487                         }
488                         
489                         /* the rest of the record contains two null terminated strings:
490                                 the source label and the "stack" filename */
491                         if ( file_read(read_buffer, 1, name_len, wth->fh) != (int)name_len ) {
492                                 destroy_k12_file_data(file_data);
493                                 *err = WTAP_ERR_SHORT_READ;
494                                 *err_info = "record too short while reading .rf5 file";
495                                 return -1;
496                         }
497                         
498                         port_name = g_strndup(read_buffer,stack_len);
499                         
500                         if ( file_read(read_buffer, 1, stack_len, wth->fh) != (int)stack_len ) {
501                                 destroy_k12_file_data(file_data);
502                                 *err = WTAP_ERR_SHORT_READ;
503                                 *err_info = "record too short while reading .rf5 file";
504                                 return -1;
505                         }
506                         
507                         stack_file =g_strndup(read_buffer,stack_len);
508                         
509                         if (choose_encap(file_data,hdr.port_id,stack_file) == WTAP_NUM_ENCAP_TYPES ) {
510                                 destroy_k12_file_data(file_data);
511                                 /* more encapsulation types than we can handle */
512                                 return 0;
513                         }
514                         
515                         offset += hdr.len;
516                         continue;
517                 } else {
518                         /* we don't need these other fellows */
519                         
520                         if (file_read(read_buffer,1, hdr.len - K12_HDR_LEN, wth->fh) != (int) hdr.len - K12_HDR_LEN ) {
521                                 destroy_k12_file_data(file_data);
522                                 return -1;
523                         }
524                         
525                         offset += hdr.len;
526                         
527                         continue;
528                 }
529         } while(1);
530         
531         wth->data_offset = offset;
532         wth->file_type = WTAP_FILE_K12;
533         wth->snapshot_length = 0;
534         wth->subtype_read = k12_read;
535         wth->subtype_seek_read = k12_seek_read;
536         wth->subtype_close = k12_close;
537         wth->capture.k12 = file_data;
538         
539         /* if we use just one encapsulation for all the file
540                 we will use that for the whole file so we can
541                 use more formats to save to */
542         
543         if (file_data->port_encaps->len == 1) {
544                 wth->file_encap = ((k12_stack_encap_t*)g_ptr_array_index(file_data->port_encaps,0))->encap;
545         } else {
546                 wth->file_encap = WTAP_ENCAP_PER_PACKET;
547         }
548         
549         return 1;
550 }
551