4 * routines for importing tektronix k12xx *.rf5 files
6 * Copyright (c) 2005, Luis E. Garia Ontanon <luis.ontanon@gmail.com>
11 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
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.
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.
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.
36 #include "file_wrappers.h"
41 * the 32 bits .rf5 file contains:
42 * an 8 byte magic number
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
52 static const guint8 k12_file_magic[] = { 0x00, 0x00, 0x02, 0x00 ,0x12, 0x05, 0x00, 0x10 };
56 guint32 num_of_records; /* XXX: not sure about this */
58 GHashTable* src_by_id; /* k12_srcdsc_recs by input */
59 GHashTable* src_by_name; /* k12_srcdsc_recs by stack_name */
62 typedef struct _k12_src_desc_t {
67 k12_input_info_t input_info;
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 */
80 #define K12_MASK_PACKET 0xfffffff0
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
88 #define K12_PACKET_TIMESTAMP 0x18
89 #define K12_PACKET_FRAME 0x20
91 #define K12_SRCDESC_EXTRALEN 0x1e
92 #define K12_SRCDESC_NAMELEN 0x20
93 #define K12_SRCDESC_STACKLEN 0x22
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
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)
109 * XXX: works at most with 0x1FFF bytes per record
111 static gint get_record(guint8* buffer, FILE* fh, guint file_offset) {
115 long junky_offset = 0x2000 - ( (file_offset - 0x200) % 0x2000 );
117 if ( junky_offset != 0x2000 ) {
120 read = file_read(buffer,1, 0x4,fh);
122 if (read == 2 && buffer[0] == 0xff && buffer[1] == 0xff) {
124 } else if ( read != 0x4 ) {
128 len = pntohl(buffer) & 0x00001FFF;
130 if (junky_offset > len) {
132 if (len - 0x4 <= 0) {
136 if ( file_read(buffer+0x4, 1, len - 0x4, fh) < len - 0x4 ) {
142 if ( file_read(buffer+0x4, 1, len + 0xC, fh) < len ) {
146 for (i = junky_offset; i < len; i++) {
147 buffer[i] = buffer[i+0x10];
155 read = file_read(buffer,1,0x14,fh);
157 if (read == 2 && buffer[0] == 0xff && buffer[1] == 0xff) {
159 } else if ( read < 0x14 ){
163 for (i = 0; i < 0x10; i++) {
164 buffer[i] = buffer[i+0x10];
167 len = pntohl(buffer) & 0x00001FFF;
173 if ( file_read(buffer + 0x4, 1, len - 0x4,fh) < len - 0x4 ) {
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];
189 offset = wth->data_offset;
191 /* ignore the record if it isn't a packet */
193 *data_offset = offset;
195 len = get_record(buffer, wth->fh, offset);
198 *err = WTAP_ERR_SHORT_READ;
200 } else if (len == 0) {
205 type = pntohl(buffer + K12_RECORD_TYPE);
209 } while ( (type & K12_MASK_PACKET) != K12_REC_PACKET );
211 wth->data_offset = offset;
213 ts = pntohll(buffer + K12_PACKET_TIMESTAMP);
215 wth->phdr.ts.tv_usec = (guint32) ( (ts % 2000000) / 2);
216 wth->phdr.ts.tv_sec = (guint32) ((ts / 2000000) + 631152000);
218 wth->phdr.len = wth->phdr.caplen = pntohl(buffer + K12_RECORD_FRAME_LEN) & 0x00001FFF;
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);
224 wth->pseudo_header.k12.input = pntohl(buffer + K12_RECORD_INPUT);
226 src_desc = g_hash_table_lookup(wth->capture.k12->src_by_id,GUINT_TO_POINTER(wth->pseudo_header.k12.input));
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;
233 memcpy(&(wth->pseudo_header.k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
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";
240 wth->pseudo_header.k12.stuff = wth->capture.k12;
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];
250 if ( file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) {
254 if (get_record(buffer, wth->random_fh, seek_off) < 1) {
258 memcpy(pd, buffer + K12_PACKET_FRAME, length);
260 pseudo_header->k12.input = pntohl(buffer + K12_RECORD_INPUT);
262 src_desc = g_hash_table_lookup(wth->capture.k12->src_by_id,GUINT_TO_POINTER(wth->pseudo_header.k12.input));
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;
269 memcpy(&(pseudo_header->k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
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";
276 pseudo_header->k12.stuff = wth->capture.k12;
281 static k12_t* new_k12_file_data() {
282 k12_t* fd = g_malloc(sizeof(k12_t));
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);
292 static gboolean destroy_srcdsc(gpointer k _U_, gpointer v, gpointer p _U_) {
293 k12_src_desc_t* rec = v;
296 g_free(rec->input_name);
299 g_free(rec->stack_file);
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);
313 static void k12_close(wtap *wth) {
314 destroy_k12_file_data(wth->capture.k12);
318 int k12_open(wtap *wth, int *err, gchar **err_info _U_) {
320 guint8 read_buffer[0x2000];
331 if ( file_read(read_buffer,1,0x200,wth->fh) != 0x200 ) {
334 if ( memcmp(read_buffer,k12_file_magic,8) != 0 ) {
341 file_data = new_k12_file_data();
343 file_data->file_len = pntohl( read_buffer + 0x8);
344 file_data->num_of_records = pntohl( read_buffer + 0xC );
348 len = get_record(read_buffer, wth->fh, offset);
355 type = pntohl( read_buffer + K12_RECORD_TYPE );
357 if ( (type & K12_MASK_PACKET) == K12_REC_PACKET) {
359 * we are at the first packet record, rewind and leave.
361 if (file_seek(wth->fh, offset, SEEK_SET, err) == -1) {
362 destroy_k12_file_data(file_data);
367 } else if (type == K12_REC_SRCDSC) {
368 rec = g_malloc0(sizeof(k12_src_desc_t));
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 );
376 if (extra_len == 0 || name_len == 0 || stack_len == 0
377 || 0x20 + extra_len + name_len + stack_len > rec_len ) {
381 switch(( rec->input_type = pntohl( read_buffer + K12_SRCDESC_EXTRATYPE ) )) {
383 rec->input_info.ds0mask = 0x00000000;
385 for (i = 0; i < 32; i++) {
386 rec->input_info.ds0mask |= ( *(read_buffer + K12_SRCDESC_DS0_MASK + i) == 0xff ) ? 0x1<<(31-i) : 0x0;
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 );
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);
401 g_strdown(rec->stack_file);
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);
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;
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 */
431 int k12_dump_can_write_encap(int encap) {
433 if (encap == WTAP_ENCAP_PER_PACKET)
434 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
436 if (encap != WTAP_ENCAP_K12)
437 return WTAP_ERR_UNSUPPORTED_ENCAP;
442 static const gchar dumpy_junk[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
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;
447 if (len > junky_offset) {
450 fwrite(buffer, 1, junky_offset, wdh->fh);
452 fwrite(dumpy_junk, 1, 0x10, wdh->fh);
454 fwrite(buffer+junky_offset, 1, len - junky_offset, wdh->fh);
456 wdh->dump.k12->file_offset += len + 0x10;
458 fwrite(buffer, 1, len, wdh->fh);
459 wdh->dump.k12->file_offset += len;
462 wdh->dump.k12->num_of_records++;
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;
473 guint8 buffer[0x2000];
500 guint8 unk_data[0x10];
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);
515 obj.record.unk32_2 = g_htonl(0x0000060f);
516 obj.record.unk32_3 = g_htonl(0x00000003);
517 obj.record.unk32_4 = g_htonl(0x01000100);
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;
523 obj.record.extra.type = g_htonl(src_desc->input_type);
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);
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;
541 obj.record.extra_len = g_htons(0x08);
546 memcpy(obj.buffer + offset,
547 src_desc->input_name,
548 obj.record.name_len);
550 memcpy(obj.buffer + offset + obj.record.name_len,
551 src_desc->stack_file,
552 obj.record.stack_len);
554 len = offset + obj.record.name_len + obj.record.stack_len;
555 len += (len % 4) ? 4 - (len % 4) : 0;
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);
561 k12_dump_record(wdh,len,obj.buffer);
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_) {
569 guint8 buffer[0x2000];
580 guint8 frame[0x1fc0];
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);
589 obj.record.len = 0x20 + phdr->len;
590 obj.record.len += (obj.record.len % 4) ? 4 - obj.record.len % 4 : 0;
592 len = obj.record.len;
594 obj.record.len = g_htonl(obj.record.len);
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);
600 obj.record.ts = GUINT64_TO_BE((((guint64)phdr->ts.tv_sec - 631152000) * 2000000) + (phdr->ts.tv_usec * 2));
602 memcpy(obj.record.frame,pd,phdr->len);
604 k12_dump_record(wdh,len,obj.buffer);
610 static const guint8 k12_eof[] = {0xff,0xff};
612 static gboolean k12_dump_close(wtap_dumper *wdh, int *err) {
614 guint8 b[sizeof(guint32)];
618 fwrite(k12_eof, 1, 2, wdh->fh);
620 if (fseek(wdh->fh, 8, SEEK_SET) == -1) {
625 d.u = g_htonl(wdh->dump.k12->file_len);
627 fwrite(d.b, 1, 4, wdh->fh);
629 d.u = g_htonl(wdh->dump.k12->num_of_records);
631 fwrite(d.b, 1, 4, wdh->fh);
637 gboolean k12_dump_open(wtap_dumper *wdh, gboolean cant_seek, int *err) {
640 *err = WTAP_ERR_CANT_WRITE_TO_PIPE;
644 if ( fwrite(k12_file_magic, 1, 8, wdh->fh) != 8 ) {
649 if (fseek(wdh->fh, 0x200, SEEK_SET) == -1) {
654 wdh->subtype_write = k12_dump;
655 wdh->subtype_close = k12_dump_close;
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;