4 * routines for importing tektronix k12xx *.rf5 files
6 * Copyright (c) 2005, Luis E. Garia Ontanon <luis@ontanon.org>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
35 #include "file_wrappers.h"
39 #include <wsutil/str_util.h>
44 * http://www2.tek.com/cmswpt/madownload.lotr?ct=MA&cs=mpm&ci=11284&lc=EN
46 * for some information about the file format. You may have to fill in
47 * a form to download the document ("Recored File API Programmer Manual").
49 * Unfortunately, it describes an API that delivers records from an rf5
50 * file, not the raw format of an rf5 file, so, while it gives the formats
51 * of the records with various types, it does not indicate how those records
52 * are stored in the file.
55 /* #define DEBUG_K12 */
60 #include <wsutil/file_util.h>
63 char* env_file = NULL;
65 static unsigned int debug_level = 0;
67 void k12_fprintf(char* fmt, ...) {
71 vfprintf(dbg_out, fmt, ap);
76 #define K12_DBG(level,args) do { if (level <= debug_level) { \
77 fprintf(dbg_out,"%s:%d: ",CAT(__FI,LE__),CAT(__LI,NE__)); \
79 fprintf(dbg_out,"\n"); \
82 void k12_hexdump(guint level, gint64 offset, char* label, unsigned char* b, unsigned int len) {
83 static const char* c2t[] = {
84 "00","01","02","03","04","05","06","07","08","09","0a","0b","0c","0d","0e","0f",
85 "10","11","12","13","14","15","16","17","18","19","1a","1b","1c","1d","1e","1f",
86 "20","21","22","23","24","25","26","27","28","29","2a","2b","2c","2d","2e","2f",
87 "30","31","32","33","34","35","36","37","38","39","3a","3b","3c","3d","3e","3f",
88 "40","41","42","43","44","45","46","47","48","49","4a","4b","4c","4d","4e","4f",
89 "50","51","52","53","54","55","56","57","58","59","5a","5b","5c","5d","5e","5f",
90 "60","61","62","63","64","65","66","67","68","69","6a","6b","6c","6d","6e","6f",
91 "70","71","72","73","74","75","76","77","78","79","7a","7b","7c","7d","7e","7f",
92 "80","81","82","83","84","85","86","87","88","89","8a","8b","8c","8d","8e","8f",
93 "90","91","92","93","94","95","96","97","98","99","9a","9b","9c","9d","9e","9f",
94 "a0","a1","a2","a3","a4","a5","a6","a7","a8","a9","aa","ab","ac","ad","ae","af",
95 "b0","b1","b2","b3","b4","b5","b6","b7","b8","b9","ba","bb","bc","bd","be","bf",
96 "c0","c1","c2","c3","c4","c5","c6","c7","c8","c9","ca","cb","cc","cd","ce","cf",
97 "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","da","db","dc","dd","de","df",
98 "e0","e1","e2","e3","e4","e5","e6","e7","e8","e9","ea","eb","ec","ed","ee","ef",
99 "f0","f1","f2","f3","f4","f5","f6","f7","f8","f9","fa","fb","fc","fd","fe","ff"
103 if (debug_level < level) return;
105 fprintf(dbg_out,"%s(%.8" G_GINT64_MODIFIER "x,%.4x): ",label,offset,len);
107 for (i=0 ; i<len ; i++) {
110 fprintf(dbg_out,"\n");
112 fprintf(dbg_out," ");
114 fprintf(dbg_out, "%s", c2t[b[i]]);
117 fprintf(dbg_out,"\n");
120 #define K12_HEXDMP(x,a,b,c,d) k12_hexdump(x,a,b,c,d)
123 #define K12_DBG(level,args) (void)0
124 #define K12_HEXDMP(x,a,b,c,d)
130 * the 32 bits .rf5 file contains:
131 * an 8 byte magic number
133 * 32bit number of records
134 * other 0x200 bytes bytes of uncharted territory
135 * 1 or more copies of the num_of_records in there
136 * the records whose first 32bits word is the length
137 * they are stuffed by one to four words every 0x2000 bytes
138 * and a 2 byte terminator FFFF
141 static const guint8 k12_file_magic[] = { 0x00, 0x00, 0x02, 0x00 ,0x12, 0x05, 0x00, 0x10 };
145 guint32 num_of_records; /* XXX: not sure about this */
147 GHashTable* src_by_id; /* k12_srcdsc_recs by input */
148 GHashTable* src_by_name; /* k12_srcdsc_recs by stack_name */
150 Buffer extra_info; /* Buffer to hold per packet extra information */
153 typedef struct _k12_src_desc_t {
158 k12_input_info_t input_info;
163 * According to the Tektronix documentation, this value is a combination of
164 * a "group" code and a "type" code, with both being 2-byte values and
165 * with the "group" code followe by the "type" code. The "group" values
168 * 0x0001 - "data event"
169 * 0x0002 - "text or L1 event"
170 * 0x0007 - "configuration event"
172 * and the "type" values are:
175 * 0x0020 - "frame" (i.e., "an actual packet")
176 * 0x0021 - "transparent frame"
177 * 0x0022 - "bit data (TRAU frame)"
178 * 0x0024 - "used to mark the frame which is a fragment"
179 * 0x0026 - "used to mark the frame which is a fragment"
180 * 0x0028 - "used to mark the frame which is generated by the LSA"
181 * 0x002A - "used to mark the frame which is generated by the LSA"
184 * 0x0030 - "text event"
185 * 0x0031 - "L1 event"
186 * 0x0032 - "L1 event (BAI)"
187 * 0x0033 - "L1 event (VX)"
189 * configuration events:
190 * 0x0040 - Logical Data Source configuration event
191 * 0x0041 - Logical Link configuration event
193 /* so far we've seen these types of records */
194 #define K12_REC_PACKET 0x00010020 /* an actual packet */
195 #define K12_REC_SRCDSC 0x00070041 /* port-stack mapping + more, the key of the whole thing */
196 #define K12_REC_SCENARIO 0x00070040 /* what appears as the window's title */
197 #define K12_REC_STK_FILE 0x00070042 /* a dump of an stk file */
198 #define K12_REC_SRCDSC2 0x00070043 /* another port-stack mapping */
199 #define K12_REC_TEXT 0x00070044 /* a string containing something with a grammar (conditions/responses?) */
200 #define K12_REC_START 0x00020030 /* a string containing human readable start time */
201 #define K12_REC_STOP 0x00020031 /* a string containing human readable stop time */
204 * According to the Tektronix documentation, packets, i.e. "data events",
205 * have several different group/type values, which differ in the last
206 * nibble of the type code. For now, we just mask that nibble off; the
207 * format of the items are different, so we might have to treat different
208 * data event types differently.
210 #define K12_MASK_PACKET 0xfffffff0
212 /* offsets of elements in the records */
213 #define K12_RECORD_LEN 0x0 /* uint32, in bytes */
214 #define K12_RECORD_TYPE 0x4 /* uint32, see above */
215 #define K12_RECORD_FRAME_LEN 0x8 /* uint32, in bytes */
216 #define K12_RECORD_SRC_ID 0xc /* uint32 */
219 * Some records from K15 files have a port ID of an undeclared
220 * interface which happens to be the only one with the first byte changed.
221 * It is still unknown how to recognize when this happens.
222 * If the lookup of the interface record fails we'll mask it
225 #define K12_RECORD_SRC_ID_MASK 0x00ffffff
227 /* elements of packet records */
228 #define K12_PACKET_TIMESTAMP 0x18 /* int64 (8b) representing 1/2us since 01-01-1990 Z00:00:00 */
230 #define K12_PACKET_FRAME 0x20 /* start of the actual frame in the record */
232 #define K12_PACKET_OFFSET_VP 0x08 /* 2 bytes, big endian */
233 #define K12_PACKET_OFFSET_VC 0x0a /* 2 bytes, big endian */
234 #define K12_PACKET_OFFSET_CID 0x0c /* 1 byte */
236 /* elements of the source description records */
237 #define K12_SRCDESC_COLOR_FOREGROUND 0x12 /* 1 byte */
238 #define K12_SRCDESC_COLOR_BACKGROUND 0x13 /* 1 byte */
240 #define K12_SRCDESC_PORT_TYPE 0x1a /* 1 byte */
241 #define K12_SRCDESC_EXTRALEN 0x1e /* uint16, big endian */
242 #define K12_SRCDESC_NAMELEN 0x20 /* uint16, big endian */
243 #define K12_SRCDESC_STACKLEN 0x22 /* uint16, big endian */
245 #define K12_SRCDESC_EXTRATYPE 0x24 /* uint32, big endian */
246 #define K12_SRCDESC_ATM_VPI 0x38 /* uint16, big endian */
247 #define K12_SRCDESC_ATM_VCI 0x3a /* uint16, big endian */
249 #define K12_SRCDESC_ATM_AAL 0x3c /* 1 byte */
250 #define K12_SRCDESC_DS0_MASK 0x3c /* 1 byte */
254 * get_record: Get the next record into a buffer
255 * Every about 0x2000 bytes 0x10 bytes are inserted in the file,
256 * even in the middle of a record.
257 * This reads the next record without the eventual 0x10 bytes.
258 * returns the length of the record + the stuffing (if any)
260 * Returns number of bytes read on success, 0 on EOF, -1 on error;
261 * if -1 is returned, *err is set to the error indication and, for
262 * errors where that's appropriate, *err_info is set to an additional
265 * XXX: works at most with 0x1FFF bytes per record
267 static gint get_record(guint8** bufferp, FILE_T fh, gint64 file_offset,
268 int *err, gchar **err_info) {
269 static guint8* buffer = NULL;
270 static guint buffer_len = 0x2000 ;
280 /* where the next unknown 0x10 bytes are stuffed to the file */
281 guint junky_offset = 0x2000 - (gint) ( (file_offset - 0x200) % 0x2000 );
283 K12_DBG(6,("get_record: ENTER: junky_offset=%" G_GINT64_MODIFIER "d, file_offset=%" G_GINT64_MODIFIER "d",junky_offset,file_offset));
285 /* no buffer is given, lets create it */
286 if (buffer == NULL) {
287 buffer = (guint8*)g_malloc(0x2000);
293 if ( junky_offset == 0x2000 ) {
294 /* the length of the record is 0x10 bytes ahead from we are reading */
295 bytes_read = file_read(junk,0x14,fh);
297 if (bytes_read == 2 && junk[0] == 0xff && junk[1] == 0xff) {
298 K12_DBG(1,("get_record: EOF"));
300 } else if ( bytes_read < 0x14 ){
301 K12_DBG(1,("get_record: SHORT READ OR ERROR"));
302 *err = file_error(fh, err_info);
304 *err = WTAP_ERR_SHORT_READ;
309 memcpy(buffer,&(junk[0x10]),4);
311 /* the length of the record is right where we are reading */
312 bytes_read = file_read(buffer, 0x4, fh);
314 if (bytes_read == 2 && buffer[0] == 0xff && buffer[1] == 0xff) {
315 K12_DBG(1,("get_record: EOF"));
317 } else if ( bytes_read != 0x4 ) {
318 K12_DBG(1,("get_record: SHORT READ OR ERROR"));
319 *err = file_error(fh, err_info);
321 *err = WTAP_ERR_SHORT_READ;
327 left = pntohl(buffer);
333 K12_DBG(5,("get_record: GET length=%u",left));
335 /* XXX - Is WTAP_MAX_PACKET_SIZE */
336 if (left < 4 || left > WTAP_MAX_PACKET_SIZE) {
337 K12_DBG(1,("get_record: Invalid GET length=%u",left));
338 *err = WTAP_ERR_BAD_FILE;
339 *err_info = g_strdup_printf("get_record: Invalid GET length=%u",left);
343 while (left > buffer_len) *bufferp = buffer = (guint8*)g_realloc(buffer,buffer_len*=2);
349 K12_DBG(6,("get_record: looping left=%d junky_offset=%" G_GINT64_MODIFIER "d",left,junky_offset));
351 if (junky_offset > left) {
352 bytes_read += last_read = file_read(writep, left, fh);
354 if ( last_read != left ) {
355 K12_DBG(1,("get_record: SHORT READ OR ERROR"));
356 *err = file_error(fh, err_info);
358 *err = WTAP_ERR_SHORT_READ;
362 K12_HEXDMP(5,file_offset, "GOT record", buffer, actual_len);
366 bytes_read += last_read = file_read(writep, junky_offset, fh);
368 if ( last_read != junky_offset ) {
369 K12_DBG(1,("get_record: SHORT READ OR ERROR, read=%d expected=%d",last_read, junky_offset));
370 *err = file_error(fh, err_info);
372 *err = WTAP_ERR_SHORT_READ;
379 bytes_read += last_read = file_read(junk, 0x10, fh);
381 if ( last_read != 0x10 ) {
382 K12_DBG(1,("get_record: SHORT READ OR ERROR"));
383 *err = file_error(fh, err_info);
385 *err = WTAP_ERR_SHORT_READ;
390 left -= junky_offset;
391 junky_offset = 0x2000;
396 K12_HEXDMP(5,file_offset, "GOT record", buffer, actual_len);
400 static gboolean k12_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset) {
401 k12_t *k12 = (k12_t *)wth->priv;
402 k12_src_desc_t* src_desc;
403 guint8* buffer = NULL;
411 offset = file_tell(wth->fh);
413 /* ignore the record if it isn't a packet */
415 K12_DBG(5,("k12_read: offset=%i",offset));
417 *data_offset = offset;
419 len = get_record(&buffer, wth->fh, offset, err, err_info);
423 } else if (len == 0) {
428 type = pntohl(buffer + K12_RECORD_TYPE);
429 src_id = pntohl(buffer + K12_RECORD_SRC_ID);
432 if ( ! (src_desc = (k12_src_desc_t*)g_hash_table_lookup(k12->src_by_id,GUINT_TO_POINTER(src_id))) ) {
434 * Some records from K15 files have a port ID of an undeclared
435 * interface which happens to be the only one with the first byte changed.
436 * It is still unknown how to recognize when this happens.
437 * If the lookup of the interface record fails we'll mask it
440 src_desc = (k12_src_desc_t*)g_hash_table_lookup(k12->src_by_id,GUINT_TO_POINTER(src_id&K12_RECORD_SRC_ID_MASK));
443 K12_DBG(5,("k12_read: record type=%x src_id=%x",type,src_id));
447 } while ( ((type & K12_MASK_PACKET) != K12_REC_PACKET) || !src_id || !src_desc );
449 wth->phdr.presence_flags = WTAP_HAS_TS;
451 ts = pntohll(buffer + K12_PACKET_TIMESTAMP);
453 wth->phdr.ts.secs = (guint32) ((ts / 2000000) + 631152000);
454 wth->phdr.ts.nsecs = (guint32) ( (ts % 2000000) * 500 );
456 K12_DBG(3,("k12_read: PACKET RECORD type=%x src_id=%x secs=%u nsecs=%u",type,src_id, wth->phdr.ts.secs,wth->phdr.ts.nsecs));
458 wth->phdr.len = wth->phdr.caplen = pntohl(buffer + K12_RECORD_FRAME_LEN) & 0x00001FFF;
459 extra_len = len - K12_PACKET_FRAME - wth->phdr.caplen;
462 buffer_assure_space(wth->frame_buffer, wth->phdr.caplen);
463 memcpy(buffer_start_ptr(wth->frame_buffer), buffer + K12_PACKET_FRAME, wth->phdr.caplen);
465 /* extra information need by some protocols */
466 buffer_assure_space(&(k12->extra_info), extra_len);
467 memcpy(buffer_start_ptr(&(k12->extra_info)),
468 buffer + K12_PACKET_FRAME + wth->phdr.caplen, extra_len);
469 wth->pseudo_header.k12.extra_info = (void*)buffer_start_ptr(&(k12->extra_info));
470 wth->pseudo_header.k12.extra_length = extra_len;
472 wth->pseudo_header.k12.input = src_id;
474 K12_DBG(5,("k12_read: wth->pseudo_header.k12.input=%x wth->phdr.len=%i input_name='%s' stack_file='%s' type=%x",
475 wth->pseudo_header.k12.input,wth->phdr.len,src_desc->input_name,src_desc->stack_file,src_desc->input_type));\
477 wth->pseudo_header.k12.input_name = src_desc->input_name;
478 wth->pseudo_header.k12.stack_file = src_desc->stack_file;
479 wth->pseudo_header.k12.input_type = src_desc->input_type;
481 switch(src_desc->input_type) {
482 case K12_PORT_ATMPVC:
483 if ((long)(K12_PACKET_FRAME + wth->phdr.len + K12_PACKET_OFFSET_CID) < len) {
484 wth->pseudo_header.k12.input_info.atm.vp = pntohs(buffer + (K12_PACKET_FRAME + wth->phdr.caplen + K12_PACKET_OFFSET_VP));
485 wth->pseudo_header.k12.input_info.atm.vc = pntohs(buffer + (K12_PACKET_FRAME + wth->phdr.caplen + K12_PACKET_OFFSET_VC));
486 wth->pseudo_header.k12.input_info.atm.cid = *((unsigned char*)(buffer + K12_PACKET_FRAME + wth->phdr.len + K12_PACKET_OFFSET_CID));
491 memcpy(&(wth->pseudo_header.k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
495 wth->pseudo_header.k12.stuff = k12;
501 static gboolean k12_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header *pseudo_header, guint8 *pd, int length, int *err _U_, gchar **err_info) {
502 k12_t *k12 = (k12_t *)wth->priv;
503 k12_src_desc_t* src_desc;
509 K12_DBG(5,("k12_seek_read: ENTER"));
511 if ( file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) {
512 K12_DBG(5,("k12_seek_read: SEEK ERROR"));
516 len = get_record(&buffer, wth->random_fh, seek_off, err, err_info);
518 K12_DBG(5,("k12_seek_read: READ ERROR"));
522 K12_DBG(5,("k12_seek_read: SHORT READ"));
523 *err = WTAP_ERR_SHORT_READ;
527 memcpy(pd, buffer + K12_PACKET_FRAME, length);
529 extra_len = len - K12_PACKET_FRAME - length;
530 buffer_assure_space(&(k12->extra_info), extra_len);
531 memcpy(buffer_start_ptr(&(k12->extra_info)),
532 buffer + K12_PACKET_FRAME + length, extra_len);
533 wth->pseudo_header.k12.extra_info = (void*)buffer_start_ptr(&(k12->extra_info));
534 wth->pseudo_header.k12.extra_length = extra_len;
536 pseudo_header->k12.extra_info = (void*)buffer_start_ptr(&(k12->extra_info));
537 pseudo_header->k12.extra_length = extra_len;
540 input = pntohl(buffer + K12_RECORD_SRC_ID);
541 K12_DBG(5,("k12_seek_read: input=%.8x",input));
543 if ( ! (src_desc = (k12_src_desc_t*)g_hash_table_lookup(k12->src_by_id,GUINT_TO_POINTER(input))) ) {
545 * Some records from K15 files have a port ID of an undeclared
546 * interface which happens to be the only one with the first byte changed.
547 * It is still unknown how to recognize when this happens.
548 * If the lookup of the interface record fails we'll mask it
551 src_desc = (k12_src_desc_t*)g_hash_table_lookup(k12->src_by_id,GUINT_TO_POINTER(input&K12_RECORD_SRC_ID_MASK));
555 K12_DBG(5,("k12_seek_read: input_name='%s' stack_file='%s' type=%x",src_desc->input_name,src_desc->stack_file,src_desc->input_type));
557 pseudo_header->k12.input_name = src_desc->input_name;
558 pseudo_header->k12.stack_file = src_desc->stack_file;
559 pseudo_header->k12.input_type = src_desc->input_type;
561 switch(src_desc->input_type) {
562 case K12_PORT_ATMPVC:
563 if ((long)(K12_PACKET_FRAME + length + K12_PACKET_OFFSET_CID) < len) {
564 pseudo_header->k12.input_info.atm.vp = pntohs(buffer + K12_PACKET_FRAME + length + K12_PACKET_OFFSET_VP);
565 pseudo_header->k12.input_info.atm.vc = pntohs(buffer + K12_PACKET_FRAME + length + K12_PACKET_OFFSET_VC);
566 pseudo_header->k12.input_info.atm.cid = *((unsigned char*)(buffer + K12_PACKET_FRAME + length + K12_PACKET_OFFSET_CID));
571 memcpy(&(pseudo_header->k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
576 wth->pseudo_header.k12.input_name = src_desc->input_name;
577 wth->pseudo_header.k12.stack_file = src_desc->stack_file;
578 wth->pseudo_header.k12.input_type = src_desc->input_type;
580 switch(src_desc->input_type) {
581 case K12_PORT_ATMPVC:
582 if ((long)(K12_PACKET_FRAME + length + K12_PACKET_OFFSET_CID) < len) {
583 wth->pseudo_header.k12.input_info.atm.vp = pntohs(buffer + K12_PACKET_FRAME + length + K12_PACKET_OFFSET_VP);
584 wth->pseudo_header.k12.input_info.atm.vc = pntohs(buffer + K12_PACKET_FRAME + length + K12_PACKET_OFFSET_VC);
585 wth->pseudo_header.k12.input_info.atm.cid = *((unsigned char*)(buffer + K12_PACKET_FRAME + length + K12_PACKET_OFFSET_CID));
590 memcpy(&(wth->pseudo_header.k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
595 K12_DBG(5,("k12_seek_read: NO SRC_RECORD FOUND"));
598 memset(&(pseudo_header->k12),0,sizeof(pseudo_header->k12));
599 pseudo_header->k12.input_name = "unknown port";
600 pseudo_header->k12.stack_file = "unknown stack file";
603 memset(&(wth->pseudo_header.k12),0,sizeof(wth->pseudo_header.k12));
604 wth->pseudo_header.k12.input_name = "unknown port";
605 wth->pseudo_header.k12.stack_file = "unknown stack file";
610 pseudo_header->k12.input = input;
611 pseudo_header->k12.stuff = k12;
614 wth->pseudo_header.k12.input = input;
615 wth->pseudo_header.k12.stuff = k12;
617 K12_DBG(5,("k12_seek_read: DONE OK"));
623 static k12_t* new_k12_file_data(void) {
624 k12_t* fd = g_new(k12_t,1);
627 fd->num_of_records = 0;
628 fd->src_by_name = g_hash_table_new(g_str_hash,g_str_equal);
629 fd->src_by_id = g_hash_table_new(g_direct_hash,g_direct_equal);
631 buffer_init(&(fd->extra_info), 100);
636 static gboolean destroy_srcdsc(gpointer k _U_, gpointer v, gpointer p _U_) {
637 k12_src_desc_t* rec = (k12_src_desc_t*)v;
639 g_free(rec->input_name);
640 g_free(rec->stack_file);
646 static void destroy_k12_file_data(k12_t* fd) {
647 g_hash_table_destroy(fd->src_by_id);
648 g_hash_table_foreach_remove(fd->src_by_name,destroy_srcdsc,NULL);
649 g_hash_table_destroy(fd->src_by_name);
650 buffer_free(&(fd->extra_info));
654 static void k12_close(wtap *wth) {
655 k12_t *k12 = (k12_t *)wth->priv;
657 destroy_k12_file_data(k12);
658 wth->priv = NULL; /* destroy_k12_file_data freed it */
660 K12_DBG(5,("k12_close: CLOSED"));
661 if (env_file) fclose(dbg_out);
666 int k12_open(wtap *wth, int *err, gchar **err_info) {
668 guint8 header_buffer[0x200];
681 gchar* env_level = getenv("K12_DEBUG_LEVEL");
682 env_file = getenv("K12_DEBUG_FILENAME");
684 dbg_out = ws_fopen(env_file,"w");
685 if (dbg_out == NULL) {
687 K12_DBG(1,("unable to open K12 DEBUG FILENAME for writing! Logging to standard error"));
691 if ( env_level ) debug_level = strtoul(env_level,NULL,10);
692 K12_DBG(1,("k12_open: ENTER debug_level=%u",debug_level));
695 if ( file_read(header_buffer,0x200,wth->fh) != 0x200 ) {
696 K12_DBG(1,("k12_open: FILE HEADER TOO SHORT OR READ ERROR"));
697 *err = file_error(wth->fh, err_info);
703 if ( memcmp(header_buffer,k12_file_magic,8) != 0 ) {
704 K12_DBG(1,("k12_open: BAD MAGIC"));
711 file_data = new_k12_file_data();
713 file_data->file_len = pntohl( header_buffer + 0x8);
714 file_data->num_of_records = pntohl( header_buffer + 0xC );
716 K12_DBG(5,("k12_open: FILE_HEADER OK: offset=%x file_len=%i records=%i",
719 file_data->num_of_records ));
723 len = get_record(&read_buffer, wth->fh, offset, err, err_info);
726 K12_DBG(1,("k12_open: BAD HEADER RECORD",len));
727 destroy_k12_file_data(file_data);
731 K12_DBG(1,("k12_open: BAD HEADER RECORD",len));
732 *err = WTAP_ERR_SHORT_READ;
733 destroy_k12_file_data(file_data);
738 type = pntohl( read_buffer + K12_RECORD_TYPE );
740 if ( (type & K12_MASK_PACKET) == K12_REC_PACKET) {
742 * we are at the first packet record, rewind and leave.
744 if (file_seek(wth->fh, offset, SEEK_SET, err) == -1) {
745 destroy_k12_file_data(file_data);
748 K12_DBG(5,("k12_open: FIRST PACKET offset=%x",offset));
750 } else if (type == K12_REC_SRCDSC || type == K12_REC_SRCDSC2 ) {
751 rec = g_new0(k12_src_desc_t,1);
753 rec_len = pntohl( read_buffer + K12_RECORD_LEN );
754 extra_len = pntohs( read_buffer + K12_SRCDESC_EXTRALEN );
755 name_len = pntohs( read_buffer + K12_SRCDESC_NAMELEN );
756 stack_len = pntohs( read_buffer + K12_SRCDESC_STACKLEN );
758 rec->input = pntohl( read_buffer + K12_RECORD_SRC_ID );
760 K12_DBG(5,("k12_open: INTERFACE RECORD offset=%x interface=%x",offset,rec->input));
762 if (name_len == 0 || stack_len == 0
763 || 0x20 + extra_len + name_len + stack_len > rec_len ) {
765 K12_DBG(5,("k12_open: failed (name_len == 0 || stack_len == 0 "
766 "|| 0x20 + extra_len + name_len + stack_len > rec_len) extra_len=%i name_len=%i stack_len=%i"));
767 destroy_k12_file_data(file_data);
772 switch(( rec->input_type = pntohl( read_buffer + K12_SRCDESC_EXTRATYPE ) )) {
774 rec->input_info.ds0mask = 0x00000000;
776 for (i = 0; i < 32; i++) {
777 rec->input_info.ds0mask |= ( *(read_buffer + K12_SRCDESC_DS0_MASK + i) == 0xff ) ? 0x1<<(31-i) : 0x0;
781 case K12_PORT_ATMPVC:
782 rec->input_info.atm.vp = pntohs( read_buffer + K12_SRCDESC_ATM_VPI );
783 rec->input_info.atm.vc = pntohs( read_buffer + K12_SRCDESC_ATM_VCI );
788 else { /* Record viewer generated files
789 don't have this information */
790 if (read_buffer[K12_SRCDESC_PORT_TYPE] >= 0x14
791 && read_buffer[K12_SRCDESC_PORT_TYPE] <= 0x17)
792 /* For ATM2_E1DS1, ATM2_E3DS3,
793 ATM2_STM1EL and ATM2_STM1OP */
794 rec->input_type = K12_PORT_ATMPVC;
797 /* XXX - this is assumed, in a number of places (not just in the
798 ascii_strdown_inplace() call below) to be null-terminated;
799 is that guaranteed (even with a corrupt file)?
800 Obviously not, as a corrupt file could contain anything
801 here; the Tektronix document says the strings "must end
802 with \0", but a bad file could fail to add the \0. */
803 rec->input_name = (gchar *)g_memdup(read_buffer + K12_SRCDESC_EXTRATYPE + extra_len, name_len);
804 rec->stack_file = (gchar *)g_memdup(read_buffer + K12_SRCDESC_EXTRATYPE + extra_len + name_len, stack_len);
806 ascii_strdown_inplace (rec->stack_file);
808 g_hash_table_insert(file_data->src_by_id,GUINT_TO_POINTER(rec->input),rec);
809 g_hash_table_insert(file_data->src_by_name,rec->stack_file,rec);
819 wth->file_type = WTAP_FILE_K12;
820 wth->file_encap = WTAP_ENCAP_K12;
821 wth->snapshot_length = 0;
822 wth->subtype_read = k12_read;
823 wth->subtype_seek_read = k12_seek_read;
824 wth->subtype_close = k12_close;
825 wth->priv = (void *)file_data;
826 wth->tsprecision = WTAP_FILE_TSPREC_NSEC;
833 guint32 num_of_records;
837 int k12_dump_can_write_encap(int encap) {
839 if (encap == WTAP_ENCAP_PER_PACKET)
840 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
842 if (encap != WTAP_ENCAP_K12)
843 return WTAP_ERR_UNSUPPORTED_ENCAP;
848 static const gchar dumpy_junk[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
850 static gboolean k12_dump_record(wtap_dumper *wdh, guint32 len, guint8* buffer, int *err_p) {
851 k12_dump_t *k12 = (k12_dump_t *)wdh->priv;
852 guint32 junky_offset = (0x2000 - ( (k12->file_offset - 0x200) % 0x2000 )) % 0x2000;
854 if (len > junky_offset) {
856 if (! wtap_dump_file_write(wdh, buffer, junky_offset, err_p))
859 if (! wtap_dump_file_write(wdh, dumpy_junk, 0x10, err_p))
862 if (! wtap_dump_file_write(wdh, buffer+junky_offset, len - junky_offset, err_p))
865 k12->file_offset += len + 0x10;
867 if (! wtap_dump_file_write(wdh, buffer, len, err_p))
869 k12->file_offset += len;
872 k12->num_of_records++;
876 static void k12_dump_src_setting(gpointer k _U_, gpointer v, gpointer p) {
877 k12_src_desc_t* src_desc = (k12_src_desc_t*)v;
878 wtap_dumper *wdh = (wtap_dumper *)p;
882 int errxxx; /* dummy */
885 guint8 buffer[0x2000];
913 guint8 unk_data[0x10];
924 obj.record.type = g_htonl(K12_REC_SRCDSC);
925 obj.record.unk32_1 = g_htonl(0x00000001);
926 obj.record.input = g_htonl(src_desc->input);
928 obj.record.unk32_2 = g_htons(0x0000);
929 obj.record.color = g_htons(0x060f);
930 obj.record.unk32_3 = g_htonl(0x00000003);
931 switch (src_desc->input_type) {
932 case K12_PORT_ATMPVC:
933 obj.record.unk32_4 = g_htonl(0x01001400);
936 obj.record.unk32_4 = g_htonl(0x01000100);
939 obj.record.unk16_1 = g_htons(0x0000);
940 obj.record.name_len = (guint16) strlen(src_desc->input_name) + 1;
941 obj.record.stack_len = (guint16) strlen(src_desc->stack_file) + 1;
943 obj.record.extra.type = g_htonl(src_desc->input_type);
945 switch (src_desc->input_type) {
946 case K12_PORT_ATMPVC:
947 obj.record.extra_len = g_htons(0x18);
948 obj.record.extra.desc.atm.vp = g_htons(src_desc->input_info.atm.vp);
949 obj.record.extra.desc.atm.vc = g_htons(src_desc->input_info.atm.vc);
953 obj.record.extra_len = g_htons(0x18);
954 for( i=0; i<32; i++ ) {
955 obj.record.extra.desc.ds0mask.mask[i] =
956 (src_desc->input_info.ds0mask & (1 << i)) ? 0xff : 0x00;
961 obj.record.extra_len = g_htons(0x08);
966 memcpy(obj.buffer + offset,
967 src_desc->input_name,
968 obj.record.name_len);
970 memcpy(obj.buffer + offset + obj.record.name_len,
971 src_desc->stack_file,
972 obj.record.stack_len);
974 len = offset + obj.record.name_len + obj.record.stack_len;
975 len += (len % 4) ? 4 - (len % 4) : 0;
977 obj.record.len = g_htonl(len);
978 obj.record.name_len = g_htons(obj.record.name_len);
979 obj.record.stack_len = g_htons(obj.record.stack_len);
981 k12_dump_record(wdh,len,obj.buffer, &errxxx); /* fwrite errs ignored: see k12_dump below */
984 static gboolean k12_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
985 const union wtap_pseudo_header *pseudo_header,
986 const guint8 *pd, int *err) {
987 k12_dump_t *k12 = (k12_dump_t *)wdh->priv;
990 guint8 buffer[0x2000];
1001 guint8 frame[0x1fc0];
1005 if (k12->num_of_records == 0) {
1006 k12_t* file_data = (k12_t*)pseudo_header->k12.stuff;
1007 /* XXX: We'll assume that any fwrite errors in k12_dump_src_setting will */
1008 /* repeat during the final k12_dump_record at the end of k12_dump */
1009 /* (and thus cause an error return from k12_dump). */
1010 /* (I don't see a reasonably clean way to handle any fwrite errors */
1011 /* encountered in k12_dump_src_setting). */
1012 g_hash_table_foreach(file_data->src_by_id,k12_dump_src_setting,wdh);
1014 obj.record.len = 0x20 + phdr->len;
1015 obj.record.len += (obj.record.len % 4) ? 4 - obj.record.len % 4 : 0;
1017 len = obj.record.len;
1019 obj.record.len = g_htonl(obj.record.len);
1021 obj.record.type = g_htonl(K12_REC_PACKET);
1022 obj.record.frame_len = g_htonl(phdr->len);
1023 obj.record.input = g_htonl(pseudo_header->k12.input);
1025 obj.record.ts = GUINT64_TO_BE((((guint64)phdr->ts.secs - 631152000) * 2000000) + (phdr->ts.nsecs / 1000 * 2));
1027 memcpy(obj.record.frame,pd,phdr->len);
1029 return k12_dump_record(wdh,len,obj.buffer, err);
1032 static const guint8 k12_eof[] = {0xff,0xff};
1034 static gboolean k12_dump_close(wtap_dumper *wdh, int *err) {
1035 k12_dump_t *k12 = (k12_dump_t *)wdh->priv;
1037 guint8 b[sizeof(guint32)];
1041 if (! wtap_dump_file_write(wdh, k12_eof, 2, err))
1044 if (fseek(wdh->fh, 8, SEEK_SET) == -1) {
1049 d.u = g_htonl(k12->file_len);
1051 if (! wtap_dump_file_write(wdh, d.b, 4, err))
1054 d.u = g_htonl(k12->num_of_records);
1056 if (! wtap_dump_file_write(wdh, d.b, 4, err))
1063 gboolean k12_dump_open(wtap_dumper *wdh, int *err) {
1066 if ( ! wtap_dump_file_write(wdh, k12_file_magic, 8, err)) {
1070 if (fseek(wdh->fh, 0x200, SEEK_SET) == -1) {
1075 wdh->subtype_write = k12_dump;
1076 wdh->subtype_close = k12_dump_close;
1078 k12 = (k12_dump_t *)g_malloc(sizeof(k12_dump_t));
1079 wdh->priv = (void *)k12;
1080 k12->file_len = 0x200;
1081 k12->num_of_records = 0;
1082 k12->file_offset = 0x200;