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.
28 /* #define DEBUG_K12 NULL */
36 void k12_dbg(char* fmt, ...) {
40 vfprintf(dbg_out, fmt, ap);
54 #include "file_wrappers.h"
59 * the 32 bits .rf5 file contains:
60 * an 8 byte magic number
62 * 32bit number of records
63 * other 0x200 bytes bytes of uncharted territory
64 * 1 or more copies of the num_of_records in there
65 * the records whose first 32bits word is the length
66 * they are stuffed by one to four words every 0x2000 bytes
67 * and a 2 byte terminator FFFF
70 static const guint8 k12_file_magic[] = { 0x00, 0x00, 0x02, 0x00 ,0x12, 0x05, 0x00, 0x10 };
74 guint32 num_of_records; /* XXX: not sure about this */
76 GHashTable* src_by_id; /* k12_srcdsc_recs by input */
77 GHashTable* src_by_name; /* k12_srcdsc_recs by stack_name */
80 typedef struct _k12_src_desc_t {
85 k12_input_info_t input_info;
89 /* so far we've seen only 7 types of records */
90 #define K12_REC_PACKET 0x00010020
91 #define K12_REC_SRCDSC 0x00070041 /* port-stack mapping + more, the key of the whole thing */
92 #define K12_REC_SCENARIO 0x00070040 /* what appears as the window's title */
93 #define K12_REC_70042 0x00070042 /* XXX: ??? */
94 #define K12_REC_70044 0x00070044 /* text with a grammar (conditions/responses) */
95 #define K12_REC_20030 0x00020030 /* human readable start time */
96 #define K12_REC_20032 0x00020031 /* human readable stop time */
98 #define K12_MASK_PACKET 0xfffffff0
101 #define K12_RECORD_LEN 0x0
102 #define K12_RECORD_TYPE 0x4
103 #define K12_RECORD_FRAME_LEN 0x8
104 #define K12_RECORD_INPUT 0xc
106 #define K12_PACKET_TIMESTAMP 0x18
107 #define K12_PACKET_FRAME 0x20
109 #define K12_SRCDESC_EXTRALEN 0x1e
110 #define K12_SRCDESC_NAMELEN 0x20
111 #define K12_SRCDESC_STACKLEN 0x22
113 #define K12_SRCDESC_EXTRATYPE 0x24
114 #define K12_SRCDESC_ATM_VPI 0x38
115 #define K12_SRCDESC_ATM_VCI 0x3a
116 #define K12_SRCDESC_DS0_MASK 0x3c
121 * get_record: Get the next record into a buffer
122 * Every about 0x2000 bytes 0x10 bytes are inserted in the file,
123 * even in the middle of a record.
124 * This reads the next record without the eventual 0x10 bytes.
125 * returns the lenght of the record + the stuffing (if any)
127 * XXX: works at most with 0x1FFF bytes per record
129 static gint get_record(guint8* buffer, FILE* fh, guint file_offset) {
133 long junky_offset = 0x2000 - ( (file_offset - 0x200) % 0x2000 );
136 k12_dbg("k12:get_record: ENTER offset=%u",file_offset);
139 if ( junky_offset != 0x2000 ) {
142 read = file_read(buffer,1, 0x4,fh);
144 if (read == 2 && buffer[0] == 0xff && buffer[1] == 0xff) {
146 k12_dbg("k12:get_record: EOF");
150 } else if ( read != 0x4 ) {
152 k12_dbg("k12:get_record: SHORT READ");
158 len = pntohl(buffer) & 0x00001FFF;
160 if (junky_offset > len) {
162 if (len - 0x4 <= 0) {
164 k12_dbg("k12:get_record: TOO SHORT");
169 if ( file_read(buffer+0x4, 1, len - 0x4, fh) < len - 0x4 ) {
171 k12_dbg("k12:get_record: SHORT READ");
177 k12_dbg("k12:get_record: GOT len=%u",len);
183 if ( file_read(buffer+0x4, 1, len + 0xC, fh) < len ) {
187 for (i = junky_offset; i < len; i++) {
188 buffer[i] = buffer[i+0x10];
191 k12_dbg("k12:get_record: GOT stuffed_len=%u",len + 0x10);
199 read = file_read(buffer,1,0x14,fh);
201 if (read == 2 && buffer[0] == 0xff && buffer[1] == 0xff) {
203 k12_dbg("k12:get_record: EOF",len + 0x10);
207 } else if ( read < 0x14 ){
209 k12_dbg("k12:get_record: SHORT READ",len + 0x10);
215 for (i = 0; i < 0x10; i++) {
216 buffer[i] = buffer[i+0x10];
219 len = pntohl(buffer) & 0x00001FFF;
221 if (len - 0x4 <= 0) {
223 k12_dbg("k12:get_record: TOO SHORT");
228 if ( file_read(buffer + 0x4, 1, len - 0x4,fh) < len - 0x4 ) {
230 k12_dbg("k12:get_record: READ ERROR");
236 k12_dbg("k12:get_record: GOT stuffed_len=%u",len + 0x10);
244 static gboolean k12_read(wtap *wth, int *err, gchar **err_info _U_, long *data_offset) {
245 k12_src_desc_t* src_desc;
246 guint8 buffer[0x2000];
252 offset = wth->data_offset;
255 k12_dbg("k12_read: offset=%i",offset);
258 /* ignore the record if it isn't a packet */
260 *data_offset = offset;
262 len = get_record(buffer, wth->fh, offset);
265 *err = WTAP_ERR_SHORT_READ;
267 } else if (len == 0) {
272 type = pntohl(buffer + K12_RECORD_TYPE);
275 k12_dbg("k12_read: record type=%i",offset);
280 } while ( (type & K12_MASK_PACKET) != K12_REC_PACKET );
282 wth->data_offset = offset;
284 ts = pntohll(buffer + K12_PACKET_TIMESTAMP);
286 wth->phdr.ts.secs = (guint32) ( (ts % 2000000) / 2);
287 wth->phdr.ts.nsecs = (guint32) ((ts / 2000000) + 631152000) * 1000;
289 wth->phdr.len = wth->phdr.caplen = pntohl(buffer + K12_RECORD_FRAME_LEN) & 0x00001FFF;
292 buffer_assure_space(wth->frame_buffer, wth->phdr.caplen);
293 memcpy(buffer_start_ptr(wth->frame_buffer), buffer + K12_PACKET_FRAME, wth->phdr.caplen);
295 wth->pseudo_header.k12.input = pntohl(buffer + K12_RECORD_INPUT);
298 k12_dbg("k12_read: wth->pseudo_header.k12.input=%x wth->phdr.len=%i",wth->pseudo_header.k12.input,wth->phdr.len);
301 src_desc = g_hash_table_lookup(wth->capture.k12->src_by_id,GUINT_TO_POINTER(wth->pseudo_header.k12.input));
306 k12_dbg("k12_read: input_name='%s' stack_file='%s' type=%x",src_desc->input_name,src_desc->stack_file,src_desc->input_type);
308 wth->pseudo_header.k12.input_name = src_desc->input_name;
309 wth->pseudo_header.k12.stack_file = src_desc->stack_file;
310 wth->pseudo_header.k12.input_type = src_desc->input_type;
312 memcpy(&(wth->pseudo_header.k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
316 k12_dbg("k12_read: NO RECORD FOUND");
319 memset(&(wth->pseudo_header),0,sizeof(wth->pseudo_header));
320 wth->pseudo_header.k12.input_name = "unknown port";
321 wth->pseudo_header.k12.stack_file = "unknown stack file";
324 wth->pseudo_header.k12.stuff = wth->capture.k12;
330 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_) {
331 k12_src_desc_t* src_desc;
332 guint8 buffer[0x2000];
335 k12_dbg("k12_seek_read: ENTER");
338 if ( file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) {
340 k12_dbg("k12_seek_read: SEEK ERROR");
345 if (get_record(buffer, wth->random_fh, seek_off) < 1) {
347 k12_dbg("k12_seek_read: READ ERROR");
352 memcpy(pd, buffer + K12_PACKET_FRAME, length);
354 wth->pseudo_header.k12.input = pseudo_header->k12.input = pntohl(buffer + K12_RECORD_INPUT);
357 k12_dbg("k12_seek_read: input=%.8x",pseudo_header->k12.input);
360 src_desc = g_hash_table_lookup(wth->capture.k12->src_by_id,GUINT_TO_POINTER(pseudo_header->k12.input));
365 k12_dbg("k12_seek_read: input_name='%s' stack_file='%s' type=%x",src_desc->input_name,src_desc->stack_file,src_desc->input_type);
367 wth->pseudo_header.k12.input_name = pseudo_header->k12.input_name = src_desc->input_name;
368 wth->pseudo_header.k12.stack_file = pseudo_header->k12.stack_file = src_desc->stack_file;
369 wth->pseudo_header.k12.input_type = pseudo_header->k12.input_type = src_desc->input_type;
371 memcpy(&(pseudo_header->k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
372 memcpy(&(wth->pseudo_header.k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
376 k12_dbg("k12_seek_read: NO SRC_RECORD FOUND");
379 memset(&(wth->pseudo_header),0,sizeof(wth->pseudo_header));
380 memset(&(pseudo_header),0,sizeof(pseudo_header));
381 wth->pseudo_header.k12.input_name = pseudo_header->k12.input_name = "unknown port";
382 wth->pseudo_header.k12.stack_file = pseudo_header->k12.stack_file = "unknown stack file";
385 wth->pseudo_header.k12.stuff = pseudo_header->k12.stuff = wth->capture.k12;
388 k12_dbg("k12_seek_read: DONE OK");
395 static k12_t* new_k12_file_data() {
396 k12_t* fd = g_malloc(sizeof(k12_t));
399 fd->num_of_records = 0;
400 fd->src_by_name = g_hash_table_new(g_str_hash,g_str_equal);
401 fd->src_by_id = g_hash_table_new(g_direct_hash,g_direct_equal);
406 static gboolean destroy_srcdsc(gpointer k _U_, gpointer v, gpointer p _U_) {
407 k12_src_desc_t* rec = v;
410 g_free(rec->input_name);
413 g_free(rec->stack_file);
420 static void destroy_k12_file_data(k12_t* fd) {
421 g_hash_table_destroy(fd->src_by_id);
422 g_hash_table_foreach_remove(fd->src_by_name,destroy_srcdsc,NULL);
423 g_hash_table_destroy(fd->src_by_name);
427 static void k12_close(wtap *wth) {
429 k12_dbg("k12_close: ENTER");
437 destroy_k12_file_data(wth->capture.k12);
441 int k12_open(wtap *wth, int *err, gchar **err_info _U_) {
443 guint8 read_buffer[0x2000];
457 dbg_out = fopen(DEBUG_K12,"w");
462 k12_dbg("k12_open: ENTER");
465 if ( file_read(read_buffer,1,0x200,wth->fh) != 0x200 ) {
467 k12_dbg("k12_open: FILE HEADER TOO SHORT");
471 if ( memcmp(read_buffer,k12_file_magic,8) != 0 ) {
473 k12_dbg("k12_open: BAD MAGIC");
481 file_data = new_k12_file_data();
483 file_data->file_len = pntohl( read_buffer + 0x8);
484 file_data->num_of_records = pntohl( read_buffer + 0xC );
487 k12_dbg("k12_open: FILE_HEADER OK: offset=%x file_len=%i records=%i",
490 file_data->num_of_records );
495 len = get_record(read_buffer, wth->fh, offset);
499 k12_dbg("k12_open: BAD HEADER RECORD");
505 type = pntohl( read_buffer + K12_RECORD_TYPE );
507 if ( (type & K12_MASK_PACKET) == K12_REC_PACKET) {
509 * we are at the first packet record, rewind and leave.
511 if (file_seek(wth->fh, offset, SEEK_SET, err) == -1) {
512 destroy_k12_file_data(file_data);
516 k12_dbg("k12_open: FIRST PACKET offset=%x",offset);
519 } else if (type == K12_REC_SRCDSC) {
520 rec = g_malloc0(sizeof(k12_src_desc_t));
522 rec_len = pntohl( read_buffer + K12_RECORD_LEN );
523 extra_len = pntohs( read_buffer + K12_SRCDESC_EXTRALEN );
524 name_len = pntohs( read_buffer + K12_SRCDESC_NAMELEN );
525 stack_len = pntohs( read_buffer + K12_SRCDESC_STACKLEN );
527 rec->input = pntohl( read_buffer + K12_RECORD_INPUT );
530 k12_dbg("k12_open: INTERFACE RECORD offset=%x interface=%x",offset,rec->input);
533 if (extra_len == 0 || name_len == 0 || stack_len == 0
534 || 0x20 + extra_len + name_len + stack_len > rec_len ) {
537 k12_dbg("k12_open: failed (extra_len == 0 || name_len == 0 || stack_len == 0 "
538 "|| 0x20 + extra_len + name_len + stack_len > rec_len) extra_len=%i name_len=%i stack_len=%i");
544 switch(( rec->input_type = pntohl( read_buffer + K12_SRCDESC_EXTRATYPE ) )) {
546 rec->input_info.ds0mask = 0x00000000;
548 for (i = 0; i < 32; i++) {
549 rec->input_info.ds0mask |= ( *(read_buffer + K12_SRCDESC_DS0_MASK + i) == 0xff ) ? 0x1<<(31-i) : 0x0;
553 case K12_PORT_ATMPVC:
554 rec->input_info.atm.vp = pntohs( read_buffer + K12_SRCDESC_ATM_VPI );
555 rec->input_info.atm.vc = pntohs( read_buffer + K12_SRCDESC_ATM_VCI );
561 rec->input_name = g_memdup(read_buffer + K12_SRCDESC_EXTRATYPE + extra_len, name_len);
562 rec->stack_file = g_memdup(read_buffer + K12_SRCDESC_EXTRATYPE + extra_len + name_len, stack_len);
564 g_strdown(rec->stack_file);
566 g_hash_table_insert(file_data->src_by_id,GUINT_TO_POINTER(rec->input),rec);
567 g_hash_table_insert(file_data->src_by_name,rec->stack_file,rec);
577 wth->data_offset = offset;
578 wth->file_type = WTAP_FILE_K12;
579 wth->file_encap = WTAP_ENCAP_K12;
580 wth->snapshot_length = 0;
581 wth->subtype_read = k12_read;
582 wth->subtype_seek_read = k12_seek_read;
583 wth->subtype_close = k12_close;
584 wth->capture.k12 = file_data;
585 wth->tsprecision = WTAP_FILE_TSPREC_NSEC;
587 /* if we use just one encapsulation for all the file
588 we will use that for the whole file so we can
589 use more formats to save to */
595 int k12_dump_can_write_encap(int encap) {
597 if (encap == WTAP_ENCAP_PER_PACKET)
598 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
600 if (encap != WTAP_ENCAP_K12)
601 return WTAP_ERR_UNSUPPORTED_ENCAP;
606 static const gchar dumpy_junk[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
608 static void k12_dump_record(wtap_dumper *wdh, long len, guint8* buffer) {
609 long junky_offset = (0x2000 - ( (wdh->dump.k12->file_offset - 0x200) % 0x2000 )) % 0x2000;
611 if (len > junky_offset) {
614 fwrite(buffer, 1, junky_offset, wdh->fh);
616 fwrite(dumpy_junk, 1, 0x10, wdh->fh);
618 fwrite(buffer+junky_offset, 1, len - junky_offset, wdh->fh);
620 wdh->dump.k12->file_offset += len + 0x10;
622 fwrite(buffer, 1, len, wdh->fh);
623 wdh->dump.k12->file_offset += len;
626 wdh->dump.k12->num_of_records++;
629 static void k12_dump_src_setting(gpointer k _U_, gpointer v, gpointer p) {
630 k12_src_desc_t* src_desc = v;
631 wtap_dumper *wdh = p;
637 guint8 buffer[0x2000];
664 guint8 unk_data[0x10];
675 obj.record.type = g_htonl(K12_REC_SRCDSC);
676 obj.record.unk32_1 = g_htonl(0x00000001);
677 obj.record.input = g_htonl(src_desc->input);
679 obj.record.unk32_2 = g_htonl(0x0000060f);
680 obj.record.unk32_3 = g_htonl(0x00000003);
681 obj.record.unk32_4 = g_htonl(0x01000100);
683 obj.record.unk16_1 = g_htons(0x0000);
684 obj.record.name_len = strlen(src_desc->input_name) + 1;
685 obj.record.stack_len = strlen(src_desc->stack_file) + 1;
687 obj.record.extra.type = g_htonl(src_desc->input_type);
689 switch (src_desc->input_type) {
690 case K12_PORT_ATMPVC:
691 obj.record.extra_len = g_htons(0x18);
692 obj.record.extra.desc.atm.vp = g_htons(src_desc->input_info.atm.vp);
693 obj.record.extra.desc.atm.vc = g_htons(src_desc->input_info.atm.vc);
697 obj.record.extra_len = g_htons(0x18);
698 for( i=0; i<32; i++ ) {
699 obj.record.extra.desc.ds0mask.mask[i] =
700 (src_desc->input_info.ds0mask & (1 << i)) ? 0xff : 0x00;
705 obj.record.extra_len = g_htons(0x08);
710 memcpy(obj.buffer + offset,
711 src_desc->input_name,
712 obj.record.name_len);
714 memcpy(obj.buffer + offset + obj.record.name_len,
715 src_desc->stack_file,
716 obj.record.stack_len);
718 len = offset + obj.record.name_len + obj.record.stack_len;
719 len += (len % 4) ? 4 - (len % 4) : 0;
721 obj.record.len = g_htonl(len);
722 obj.record.name_len = g_htons(obj.record.name_len);
723 obj.record.stack_len = g_htons(obj.record.stack_len);
725 k12_dump_record(wdh,len,obj.buffer);
728 static gboolean k12_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
729 const union wtap_pseudo_header *pseudo_header,
730 const guchar *pd, int *err _U_) {
733 guint8 buffer[0x2000];
744 guint8 frame[0x1fc0];
748 if (wdh->dump.k12->num_of_records == 0) {
749 k12_t* file_data = pseudo_header->k12.stuff;
750 g_hash_table_foreach(file_data->src_by_id,k12_dump_src_setting,wdh);
753 obj.record.len = 0x20 + phdr->len;
754 obj.record.len += (obj.record.len % 4) ? 4 - obj.record.len % 4 : 0;
756 len = obj.record.len;
758 obj.record.len = g_htonl(obj.record.len);
760 obj.record.type = g_htonl(K12_REC_PACKET);
761 obj.record.frame_len = g_htonl(phdr->len);
762 obj.record.input = g_htonl(pseudo_header->k12.input);
764 obj.record.ts = GUINT64_TO_BE((((guint64)phdr->ts.secs - 631152000) * 2000000) + (phdr->ts.nsecs / 1000 * 2));
766 memcpy(obj.record.frame,pd,phdr->len);
768 k12_dump_record(wdh,len,obj.buffer);
774 static const guint8 k12_eof[] = {0xff,0xff};
776 static gboolean k12_dump_close(wtap_dumper *wdh, int *err) {
778 guint8 b[sizeof(guint32)];
782 fwrite(k12_eof, 1, 2, wdh->fh);
784 if (fseek(wdh->fh, 8, SEEK_SET) == -1) {
789 d.u = g_htonl(wdh->dump.k12->file_len);
791 fwrite(d.b, 1, 4, wdh->fh);
793 d.u = g_htonl(wdh->dump.k12->num_of_records);
795 fwrite(d.b, 1, 4, wdh->fh);
801 gboolean k12_dump_open(wtap_dumper *wdh, gboolean cant_seek, int *err) {
804 *err = WTAP_ERR_CANT_WRITE_TO_PIPE;
808 if ( fwrite(k12_file_magic, 1, 8, wdh->fh) != 8 ) {
813 if (fseek(wdh->fh, 0x200, SEEK_SET) == -1) {
818 wdh->subtype_write = k12_dump;
819 wdh->subtype_close = k12_dump_close;
821 wdh->dump.k12 = g_malloc(sizeof(k12_dump_t));
822 wdh->dump.k12->file_len = 0x200;
823 wdh->dump.k12->num_of_records = 0;
824 wdh->dump.k12->file_offset = 0x200;