EVERYTHING IN THE BUILDBOT IS GOING TO BE RED!!! Sorry!
[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 #include "k12.h"
39
40 /*
41  * the 32 bits .rf5 file contains:
42  *  an 8 byte magic number
43  *  32bit lenght
44  *  32bit number of records
45  *  other 0x200 bytes bytes of uncharted territory
46  *     1 or more copies of the num_of_records in there
47  *  the records whose first 32bits word is the length
48  *     they are stuffed by one to four words every 0x2000 bytes
49  *  and a 2 byte terminator FFFF
50  */
51
52 static const guint8 k12_file_magic[] = { 0x00, 0x00, 0x02, 0x00 ,0x12, 0x05, 0x00, 0x10 };
53
54 struct _k12_t {
55         guint32 file_len;
56         guint32 num_of_records; /* XXX: not sure about this */
57         
58         GHashTable* src_by_id; /* k12_srcdsc_recs by input */
59         GHashTable* src_by_name; /* k12_srcdsc_recs by stack_name */
60 };
61
62 typedef struct _k12_src_desc_t {
63         guint32 input;
64         guint32 input_type;
65         gchar* input_name;
66         gchar* stack_file;
67         k12_input_info_t input_info;
68 } k12_src_desc_t;
69
70
71 /* so far we've seen only 7 types of records */
72 #define K12_REC_PACKET          0x00010020
73 #define K12_REC_SRCDSC          0x00070041 /* port-stack mapping + more, the key of the whole thing */
74 #define K12_REC_SCENARIO        0x00070040 /* what appears as the window's title */
75 #define K12_REC_70042           0x00070042 /* XXX: ??? */ 
76 #define K12_REC_70044           0x00070044 /* text with a grammar (conditions/responses) */
77 #define K12_REC_20030           0x00020030 /* human readable start time  */ 
78 #define K12_REC_20032           0x00020031 /* human readable stop time */
79
80 #define K12_MASK_PACKET         0xfffffff0
81
82
83 #define K12_RECORD_LEN         0x0
84 #define K12_RECORD_TYPE        0x4
85 #define K12_RECORD_FRAME_LEN   0x8
86 #define K12_RECORD_INPUT      0xc
87
88 #define K12_PACKET_TIMESTAMP  0x18
89 #define K12_PACKET_FRAME      0x20
90
91 #define K12_SRCDESC_EXTRALEN   0x1e
92 #define K12_SRCDESC_NAMELEN    0x20
93 #define K12_SRCDESC_STACKLEN   0x22
94
95 #define K12_SRCDESC_EXTRATYPE  0x24
96 #define K12_SRCDESC_ATM_VPI    0x38
97 #define K12_SRCDESC_ATM_VCI    0x3a
98 #define K12_SRCDESC_DS0_MASK   0x3c
99
100
101
102 /*
103  * get_record: Get the next record into a buffer
104  *   Every about 0x2000 bytes 0x10 bytes are inserted in the file,
105  *   even in the middle of a record.
106  *   This reads the next record without the eventual 0x10 bytes.
107  *   returns the lenght of the record + the stuffing (if any)
108  *
109  * XXX: works at most with 0x1FFF bytes per record 
110  */
111 static gint get_record(guint8* buffer, FILE* fh, guint file_offset) {
112         long read;
113         long len;
114         int i;
115         long junky_offset = 0x2000 - ( (file_offset - 0x200) % 0x2000 );
116
117         if  ( junky_offset != 0x2000 ) {
118                 
119                 /* safe header */
120                 read = file_read(buffer,1, 0x4,fh);
121                 
122                 if (read == 2 && buffer[0] == 0xff && buffer[1] == 0xff) {
123                         return 0;
124                 } else if ( read != 0x4 ) {
125                         return -1;
126                 }
127                 
128                 len = pntohl(buffer) & 0x00001FFF;
129                 
130                 if (junky_offset > len) {
131                         /* safe body */
132                         if (len - 0x4 <= 0) {
133                                 return -1;
134                         }
135                         
136                         if ( file_read(buffer+0x4, 1, len - 0x4, fh) < len - 0x4 ) {
137                                 return -1;
138                         } else {
139                                 return len;
140                         }
141                 } else {
142                         if ( file_read(buffer+0x4, 1, len + 0xC, fh) < len ) {
143                                 return -1;
144                         }
145                         
146                         for (i = junky_offset; i < len; i++) {
147                                 buffer[i] = buffer[i+0x10];
148                         }
149                         
150                         return len + 0x10;
151                 }
152         } else {
153                 /* unsafe header */
154                 
155                 read = file_read(buffer,1,0x14,fh);
156                 
157                 if (read == 2 && buffer[0] == 0xff && buffer[1] == 0xff) {
158                         return 0;
159                 } else if ( read < 0x14 ){
160                         return -1;
161                 }
162                 
163                 for (i = 0; i < 0x10; i++) {
164                         buffer[i] = buffer[i+0x10];
165                 }
166                 
167                 len = pntohl(buffer) & 0x00001FFF;
168                 
169                 if (len - 0x4 <= 0)
170                         return -1;
171
172                 /* safe body */
173                 if ( file_read(buffer + 0x4, 1, len - 0x4,fh) < len - 0x4 ) {
174                         return -1;
175                 } else {
176                         return len + 0x10;
177                 }
178         }
179 }
180
181 static gboolean k12_read(wtap *wth, int *err, gchar **err_info _U_, long *data_offset) {
182         k12_src_desc_t* src_desc;
183         guint8 buffer[0x2000];
184         long offset;
185         long len;
186         guint32 type;
187         guint64 ts;
188         
189         offset = wth->data_offset;
190
191         /* ignore the record if it isn't a packet */    
192         do {
193                 *data_offset = offset;
194                 
195                 len = get_record(buffer, wth->fh, offset);
196                 
197                 if (len < 0) {
198                         *err = WTAP_ERR_SHORT_READ;
199                         return FALSE;
200                 } else if (len == 0) {
201                         *err = 0;
202                         return FALSE;
203                 }
204                 
205                 type = pntohl(buffer + K12_RECORD_TYPE);
206                 
207                 offset += len;
208                 
209         } while ( (type & K12_MASK_PACKET) != K12_REC_PACKET );
210         
211         wth->data_offset = offset;
212         
213         ts = pntohll(buffer + K12_PACKET_TIMESTAMP);
214         
215         wth->phdr.ts.secs = (guint32) ( (ts % 2000000) / 2);
216         wth->phdr.ts.nsecs = (guint32) ((ts / 2000000) + 631152000) * 1000;
217         
218         wth->phdr.len = wth->phdr.caplen = pntohl(buffer + K12_RECORD_FRAME_LEN) & 0x00001FFF;
219         
220         /* the frame */
221         buffer_assure_space(wth->frame_buffer, wth->phdr.caplen);
222         memcpy(buffer_start_ptr(wth->frame_buffer), buffer + K12_PACKET_FRAME, wth->phdr.caplen);
223         
224         wth->pseudo_header.k12.input = pntohl(buffer + K12_RECORD_INPUT);
225
226         src_desc = g_hash_table_lookup(wth->capture.k12->src_by_id,GUINT_TO_POINTER(wth->pseudo_header.k12.input));
227         
228         if (src_desc) {
229                 wth->pseudo_header.k12.input_name = src_desc->input_name;
230                 wth->pseudo_header.k12.stack_file = src_desc->stack_file;
231                 wth->pseudo_header.k12.input_type = src_desc->input_type;
232         
233                 memcpy(&(wth->pseudo_header.k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
234         } else {
235                 memset(&(wth->pseudo_header),0,sizeof(wth->pseudo_header));
236                 wth->pseudo_header.k12.input_name = "unknown port";
237                 wth->pseudo_header.k12.stack_file = "unknown stack file";
238         }
239         
240         wth->pseudo_header.k12.stuff = wth->capture.k12;
241
242         return TRUE;
243 }
244
245
246 static gboolean k12_seek_read(wtap *wth, long seek_off, union wtap_pseudo_header *pseudo_header, guchar *pd, int length, int *err _U_, gchar **err_info _U_) {
247         k12_src_desc_t* src_desc;
248         guint8 buffer[0x2000];
249
250         if ( file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) {
251                 return FALSE;
252         }
253         
254         if (get_record(buffer, wth->random_fh, seek_off) < 1) {
255                 return FALSE;
256         }
257         
258         memcpy(pd, buffer + K12_PACKET_FRAME, length);
259         
260         pseudo_header->k12.input = pntohl(buffer + K12_RECORD_INPUT);
261         
262         src_desc = g_hash_table_lookup(wth->capture.k12->src_by_id,GUINT_TO_POINTER(wth->pseudo_header.k12.input));
263         
264         if (src_desc) {
265                 pseudo_header->k12.input_name = src_desc->input_name;
266                 pseudo_header->k12.stack_file = src_desc->stack_file;
267                 pseudo_header->k12.input_type = src_desc->input_type;
268                 
269                 memcpy(&(pseudo_header->k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
270         } else {
271                 memset(&(wth->pseudo_header),0,sizeof(wth->pseudo_header));
272                 pseudo_header->k12.input_name = "unknown port";
273                 pseudo_header->k12.stack_file = "unknown stack file";
274         }
275         
276         pseudo_header->k12.stuff = wth->capture.k12;
277         return TRUE;
278 }
279
280
281 static k12_t* new_k12_file_data() {
282         k12_t* fd = g_malloc(sizeof(k12_t));
283         
284         fd->file_len = 0;
285         fd->num_of_records = 0;
286         fd->src_by_name = g_hash_table_new(g_str_hash,g_str_equal);
287         fd->src_by_id = g_hash_table_new(g_direct_hash,g_direct_equal);
288         
289         return fd;
290 }
291
292 static gboolean destroy_srcdsc(gpointer k _U_, gpointer v, gpointer p _U_) {
293         k12_src_desc_t* rec = v;
294         
295         if(rec->input_name)
296                 g_free(rec->input_name);
297         
298         if(rec->stack_file)
299                 g_free(rec->stack_file);
300         
301         g_free(rec);
302         
303         return TRUE;
304 }
305
306 static void destroy_k12_file_data(k12_t* fd) {
307         g_hash_table_destroy(fd->src_by_id);
308         g_hash_table_foreach_remove(fd->src_by_name,destroy_srcdsc,NULL);       
309         g_hash_table_destroy(fd->src_by_name);
310         g_free(fd);
311 }
312
313 static void k12_close(wtap *wth) {
314         destroy_k12_file_data(wth->capture.k12);
315 }
316
317
318 int k12_open(wtap *wth, int *err, gchar **err_info _U_) {
319         k12_src_desc_t* rec;
320         guint8 read_buffer[0x2000];
321         guint32 type;
322         long offset;
323         long len;
324         guint32 rec_len;
325         guint32 extra_len;
326         guint32 name_len;
327         guint32 stack_len;
328         guint i;
329         k12_t* file_data;
330         
331         if ( file_read(read_buffer,1,0x200,wth->fh) != 0x200 ) {
332                 return 0;
333         } else {
334                 if ( memcmp(read_buffer,k12_file_magic,8) != 0 ) {
335                         return 0;
336                 }
337         }
338         
339         offset = 0x200;
340         
341         file_data = new_k12_file_data();
342         
343         file_data->file_len = pntohl( read_buffer + 0x8);
344         file_data->num_of_records = pntohl( read_buffer + 0xC );
345         
346         do {
347                 
348                 len = get_record(read_buffer, wth->fh, offset);
349         
350                 if ( len <= 0 ) {
351                         return -1;
352                 }
353
354
355                 type = pntohl( read_buffer + K12_RECORD_TYPE );
356                 
357                 if ( (type & K12_MASK_PACKET) == K12_REC_PACKET) {
358                         /*
359                          * we are at the first packet record, rewind and leave.
360                          */
361                         if (file_seek(wth->fh, offset, SEEK_SET, err) == -1) {
362                                 destroy_k12_file_data(file_data);
363                                 return -1;
364                         }
365                         
366                         break;
367                 } else if (type == K12_REC_SRCDSC) {
368                         rec = g_malloc0(sizeof(k12_src_desc_t));
369                         
370                         rec_len = pntohl( read_buffer + K12_RECORD_LEN );
371                         rec->input = pntohl( read_buffer + K12_RECORD_INPUT );
372                         extra_len = pntohs( read_buffer + K12_SRCDESC_EXTRALEN );
373                         name_len = pntohs( read_buffer + K12_SRCDESC_NAMELEN );
374                         stack_len = pntohs( read_buffer + K12_SRCDESC_STACKLEN );
375                         
376                         if (extra_len == 0 || name_len == 0 || stack_len == 0
377                                 || 0x20 + extra_len + name_len + stack_len > rec_len ) {
378                                 return 0;
379                         }
380                         
381                         switch(( rec->input_type = pntohl( read_buffer + K12_SRCDESC_EXTRATYPE ) )) {
382                                 case K12_PORT_DS0S:
383                                         rec->input_info.ds0mask = 0x00000000;
384                                         
385                                         for (i = 0; i < 32; i++) {
386                                                 rec->input_info.ds0mask |= ( *(read_buffer + K12_SRCDESC_DS0_MASK + i) == 0xff ) ? 0x1<<(31-i) : 0x0; 
387                                         }
388                                                 
389                                                 break;
390                                 case K12_PORT_ATMPVC:
391                                         rec->input_info.atm.vp = pntohs( read_buffer + K12_SRCDESC_ATM_VPI );
392                                         rec->input_info.atm.vc = pntohs( read_buffer + K12_SRCDESC_ATM_VCI );
393                                         break;
394                                 default:
395                                         break;
396                         }
397                         
398                         rec->input_name = g_memdup(read_buffer + K12_SRCDESC_EXTRATYPE + extra_len, name_len);
399                         rec->stack_file = g_memdup(read_buffer + K12_SRCDESC_EXTRATYPE + extra_len + name_len, stack_len);
400                         
401                         g_strdown(rec->stack_file);
402                                 
403                         g_hash_table_insert(file_data->src_by_id,GUINT_TO_POINTER(rec->input),rec);
404                         g_hash_table_insert(file_data->src_by_name,rec->stack_file,rec);
405                         
406                         offset += len;
407                         continue;
408                 } else {
409                         offset += len;
410                         continue;
411                 }
412         } while(1);
413         
414         wth->data_offset = offset;
415         wth->file_type = WTAP_FILE_K12;
416         wth->file_encap = WTAP_ENCAP_K12;
417         wth->snapshot_length = 0;
418         wth->subtype_read = k12_read;
419         wth->subtype_seek_read = k12_seek_read;
420         wth->subtype_close = k12_close;
421         wth->capture.k12 = file_data;
422         
423         /* if we use just one encapsulation for all the file
424                 we will use that for the whole file so we can
425                 use more formats to save to */
426         
427         
428         return 1;
429 }
430
431 int k12_dump_can_write_encap(int encap) {
432         
433         if (encap == WTAP_ENCAP_PER_PACKET)
434                 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
435         
436         if (encap != WTAP_ENCAP_K12)
437                 return WTAP_ERR_UNSUPPORTED_ENCAP;
438         
439         return 0;
440 }
441
442 static const gchar dumpy_junk[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
443
444 static void k12_dump_record(wtap_dumper *wdh, long len,  guint8* buffer) {
445         long junky_offset = (0x2000 - ( (wdh->dump.k12->file_offset - 0x200) % 0x2000 )) % 0x2000;
446         
447         if (len > junky_offset) {
448                 
449                 if (junky_offset)
450                         fwrite(buffer, 1, junky_offset, wdh->fh);
451                 
452                 fwrite(dumpy_junk, 1, 0x10, wdh->fh);
453                 
454                 fwrite(buffer+junky_offset, 1, len - junky_offset, wdh->fh);
455                 
456                 wdh->dump.k12->file_offset += len + 0x10;
457         } else {
458                 fwrite(buffer, 1, len, wdh->fh);
459                 wdh->dump.k12->file_offset += len;
460         }
461         
462         wdh->dump.k12->num_of_records++;
463 }
464
465 static void k12_dump_src_setting(gpointer k _U_, gpointer v, gpointer p) {
466         k12_src_desc_t* src_desc = v;
467         wtap_dumper *wdh = p;
468         guint32 len;
469         guint offset;
470         guint i;
471         
472         union {
473                 guint8 buffer[0x2000];
474                 
475                 struct {
476                         guint32 len;
477                         guint32 type;
478                         guint32 unk32_1;
479                         guint32 input;
480                         
481                         guint32 unk32_2;
482                         guint32 unk32_3;
483                         guint32 unk32_4;
484                         guint16 unk16_1;
485                         guint16 extra_len;
486                         
487                         guint16 name_len;
488                         guint16 stack_len;
489                         
490                         struct {
491                                 guint32 type;
492                                 
493                                 union {
494                                         struct {
495                                                 guint32 unk32;
496                                                 guint8 mask[32];
497                                         } ds0mask;
498                                         
499                                         struct {
500                                                 guint8 unk_data[0x10];
501                                                 guint16 vp;
502                                                 guint16 vc;
503                                         } atm;
504                                         
505                                         guint32 unk;
506                                 } desc;
507                         } extra;
508                 } record;
509         } obj;
510         
511         obj.record.type = g_htonl(K12_REC_SRCDSC);
512         obj.record.unk32_1 = g_htonl(0x00000001);
513         obj.record.input = g_htonl(src_desc->input);
514         
515         obj.record.unk32_2 = g_htonl(0x0000060f);
516         obj.record.unk32_3 = g_htonl(0x00000003);
517         obj.record.unk32_4 = g_htonl(0x01000100);
518
519         obj.record.unk16_1 = g_htons(0x0000);
520         obj.record.name_len = strlen(src_desc->input_name) + 1;
521         obj.record.stack_len = strlen(src_desc->stack_file) + 1;
522
523         obj.record.extra.type = g_htonl(src_desc->input_type);
524         
525         switch (src_desc->input_type) {
526                 case K12_PORT_ATMPVC:
527                         obj.record.extra_len = g_htons(0x18);
528                         obj.record.extra.desc.atm.vp = g_htons(src_desc->input_info.atm.vp);
529                         obj.record.extra.desc.atm.vc = g_htons(src_desc->input_info.atm.vc);
530                         offset = 0x3c;
531                         break;
532                 case K12_PORT_DS0S:
533                         obj.record.extra_len = g_htons(0x18);
534                         for( i=0; i<32; i++ ) {
535                                 obj.record.extra.desc.ds0mask.mask[i] =
536                                         (src_desc->input_info.ds0mask & (1 << i)) ? 0xff : 0x00;
537                         }
538                         offset = 0x3c;
539                         break;
540                 default:
541                         obj.record.extra_len = g_htons(0x08);
542                         offset = 0x2c;
543                         break;
544         }
545         
546         memcpy(obj.buffer + offset,
547                    src_desc->input_name,
548                    obj.record.name_len);
549         
550         memcpy(obj.buffer + offset + obj.record.name_len,
551                    src_desc->stack_file,
552                    obj.record.stack_len);
553         
554         len = offset + obj.record.name_len + obj.record.stack_len;
555         len += (len % 4) ? 4 - (len % 4) : 0;
556         
557         obj.record.len = g_htonl(len);
558         obj.record.name_len =  g_htons(obj.record.name_len);
559         obj.record.stack_len = g_htons(obj.record.stack_len);
560
561         k12_dump_record(wdh,len,obj.buffer);
562 }
563
564 static gboolean k12_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
565                                                         const union wtap_pseudo_header *pseudo_header,
566                                                         const guchar *pd, int *err _U_) {
567         long len;
568         union {
569                 guint8 buffer[0x2000];
570                 struct {
571                         guint32 len;
572                         guint32 type;
573                         guint32 frame_len;
574                         guint32 input;
575                         
576                         guint32 datum_1;
577                         guint32 datum_2;
578                         guint64 ts;
579                         
580                         guint8 frame[0x1fc0];
581                 } record;
582         } obj;
583         
584         if (wdh->dump.k12->num_of_records == 0) {
585                 k12_t* file_data = pseudo_header->k12.stuff;
586                 g_hash_table_foreach(file_data->src_by_id,k12_dump_src_setting,wdh);
587         }
588         
589         obj.record.len = 0x20 + phdr->len;
590         obj.record.len += (obj.record.len % 4) ? 4 - obj.record.len % 4 : 0;
591
592         len = obj.record.len;
593         
594         obj.record.len = g_htonl(obj.record.len);
595
596         obj.record.type = g_htonl(K12_REC_PACKET);
597         obj.record.frame_len = g_htonl(phdr->len);
598         obj.record.input = g_htonl(pseudo_header->k12.input);
599         
600         obj.record.ts = GUINT64_TO_BE((((guint64)phdr->ts.secs - 631152000) * 2000000) + (phdr->ts.nsecs / 1000 * 2));
601
602         memcpy(obj.record.frame,pd,phdr->len);
603         
604         k12_dump_record(wdh,len,obj.buffer);
605         
606         /* XXX if OK */
607         return TRUE;
608 }
609
610 static const guint8 k12_eof[] = {0xff,0xff};
611
612 static gboolean k12_dump_close(wtap_dumper *wdh, int *err) {
613         union {
614                 guint8 b[sizeof(guint32)];
615                 guint32 u;
616         } d;
617         
618         fwrite(k12_eof, 1, 2, wdh->fh);
619
620         if (fseek(wdh->fh, 8, SEEK_SET) == -1) {
621                 *err = errno;
622                 return FALSE;
623         }
624         
625         d.u = g_htonl(wdh->dump.k12->file_len);
626         
627         fwrite(d.b, 1, 4, wdh->fh);
628
629         d.u = g_htonl(wdh->dump.k12->num_of_records);
630         
631         fwrite(d.b, 1, 4, wdh->fh);
632
633         return TRUE;
634 }
635
636
637 gboolean k12_dump_open(wtap_dumper *wdh, gboolean cant_seek, int *err) {
638         
639         if (cant_seek) {
640                 *err = WTAP_ERR_CANT_WRITE_TO_PIPE;
641                 return FALSE;
642         }
643         
644         if ( fwrite(k12_file_magic, 1, 8, wdh->fh) != 8 ) {
645                 *err = errno;
646                 return FALSE;
647         }
648         
649         if (fseek(wdh->fh, 0x200, SEEK_SET) == -1) {
650                 *err = errno;
651                 return FALSE;
652         }
653
654         wdh->subtype_write = k12_dump;
655         wdh->subtype_close = k12_dump_close;
656         
657         wdh->dump.k12 = g_malloc(sizeof(k12_dump_t));
658         wdh->dump.k12->file_len = 0x200;
659         wdh->dump.k12->num_of_records = 0;
660         wdh->dump.k12->file_offset  = 0x200;
661         
662         return TRUE;
663 }
664
665