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://www.tek.com/manual/record-file-api-programmer-manual
46 * for some information about the file format. You may have to fill in
47 * a form to download the document ("Record 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(const 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_hex_ascii_dump(guint level, gint64 offset, const char* label, const 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):\n",label,offset,len);
107 for (i=0 ; i<len ; i += 16) {
108 for (j=0; j<16; j++) {
110 fprintf(dbg_out," ");
112 fprintf(dbg_out, "%s", c2t[b[i+j]]);
114 fprintf(dbg_out, " ");
116 fprintf(dbg_out, " ");
117 for (j=0; j<16; j++) {
119 fprintf(dbg_out, "%c", g_ascii_isprint(b[i+j]) ? b[i+j] : '.');
121 fprintf(dbg_out,"\n");
125 #define K12_HEX_ASCII_DUMP(x,a,b,c,d) k12_hex_ascii_dump(x,a,b,c,d)
127 void k12_ascii_dump(guint level, guint8 *buf, guint32 len, guint32 buf_offset) {
130 if (debug_level < level) return;
132 for (i = buf_offset; i < len; i++) {
133 if (isprint(buf[i]) || buf[i] == '\n' || buf[i] == '\t')
134 putc(buf[i], dbg_out);
135 else if (buf[i] == '\0')
136 fprintf(dbg_out, "(NUL)\n");
140 #define K12_ASCII_DUMP(x,a,b,c) k12_ascii_dump(x,a,b,c)
143 #define K12_DBG(level,args) (void)0
144 #define K12_HEX_ASCII_DUMP(x,a,b,c,d)
145 #define K12_ASCII_DUMP(x,a,b,c)
151 * the 32 bits .rf5 file contains:
152 * an 8 byte magic number
154 * 32bit number of records
155 * other 0x200 bytes bytes of uncharted territory
156 * 1 or more copies of the num_of_records in there
157 * the records whose first 32bits word is the length
158 * they are stuffed by one to four words every 0x2000 bytes
159 * and a 2 byte terminator FFFF
162 static const guint8 k12_file_magic[] = { 0x00, 0x00, 0x02, 0x00 ,0x12, 0x05, 0x00, 0x10 };
166 guint32 num_of_records; /* XXX: not sure about this */
168 GHashTable* src_by_id; /* k12_srcdsc_recs by input */
169 GHashTable* src_by_name; /* k12_srcdsc_recs by stack_name */
171 guint8 *seq_read_buff; /* read buffer for sequential reading */
172 guint seq_read_buff_len; /* length of that buffer */
173 guint8 *rand_read_buff; /* read buffer for random reading */
174 guint rand_read_buff_len; /* length of that buffer */
176 Buffer extra_info; /* Buffer to hold per packet extra information */
179 typedef struct _k12_src_desc_t {
184 k12_input_info_t input_info;
189 * According to the Tektronix documentation, this value is a combination of
190 * a "group" code and a "type" code, with both being 2-byte values and
191 * with the "group" code followe by the "type" code. The "group" values
194 * 0x0001 - "data event"
195 * 0x0002 - "text or L1 event"
196 * 0x0007 - "configuration event"
198 * and the "type" values are:
201 * 0x0020 - "frame" (i.e., "an actual packet")
202 * 0x0021 - "transparent frame"
203 * 0x0022 - "bit data (TRAU frame)"
204 * 0x0024 - "used to mark the frame which is a fragment"
205 * 0x0026 - "used to mark the frame which is a fragment"
206 * 0x0028 - "used to mark the frame which is generated by the LSA"
207 * 0x002A - "used to mark the frame which is generated by the LSA"
210 * 0x0030 - "text event"
211 * 0x0031 - "L1 event"
212 * 0x0032 - "L1 event (BAI)"
213 * 0x0033 - "L1 event (VX)"
215 * configuration events:
216 * 0x0040 - Logical Data Source configuration event
217 * 0x0041 - Logical Link configuration event
219 /* so far we've seen these types of records */
220 #define K12_REC_PACKET 0x00010020 /* an actual packet */
221 #define K12_REC_D0020 0x000d0020 /* an actual packet, seen in a k18 file */
222 #define K12_REC_SCENARIO 0x00070040 /* what appears as the window's title */
223 #define K12_REC_SRCDSC 0x00070041 /* port-stack mapping + more, the key of the whole thing */
224 #define K12_REC_STK_FILE 0x00070042 /* a dump of an stk file */
225 #define K12_REC_SRCDSC2 0x00070043 /* another port-stack mapping */
226 #define K12_REC_TEXT 0x00070044 /* a string containing something with a grammar (conditions/responses?) */
227 #define K12_REC_START 0x00020030 /* a string containing human readable start time */
228 #define K12_REC_STOP 0x00020031 /* a string containing human readable stop time */
231 * According to the Tektronix documentation, packets, i.e. "data events",
232 * have several different group/type values, which differ in the last
233 * nibble of the type code. For now, we just mask that nibble off; the
234 * format of the items are different, so we might have to treat different
235 * data event types differently.
237 #define K12_MASK_PACKET 0xfffffff0
239 /* offsets of elements in the records */
240 #define K12_RECORD_LEN 0x0 /* uint32, in bytes */
241 #define K12_RECORD_TYPE 0x4 /* uint32, see above */
242 #define K12_RECORD_FRAME_LEN 0x8 /* uint32, in bytes */
243 #define K12_RECORD_SRC_ID 0xc /* uint32 */
246 * Some records from K15 files have a port ID of an undeclared
247 * interface which happens to be the only one with the first byte changed.
248 * It is still unknown how to recognize when this happens.
249 * If the lookup of the interface record fails we'll mask it
252 #define K12_RECORD_SRC_ID_MASK 0x00ffffff
254 /* elements of packet records */
255 #define K12_PACKET_TIMESTAMP 0x18 /* int64 (8b) representing 1/2us since 01-01-1990 Z00:00:00 */
257 #define K12_PACKET_FRAME 0x20 /* start of the actual frame in the record */
258 #define K12_PACKET_FRAME_D0020 0x34 /* start of the actual frame in the record */
260 #define K12_PACKET_OFFSET_VP 0x08 /* 2 bytes, big endian */
261 #define K12_PACKET_OFFSET_VC 0x0a /* 2 bytes, big endian */
262 #define K12_PACKET_OFFSET_CID 0x0c /* 1 byte */
264 /* elements of the source description records */
265 #define K12_SRCDESC_COLOR_FOREGROUND 0x12 /* 1 byte */
266 #define K12_SRCDESC_COLOR_BACKGROUND 0x13 /* 1 byte */
268 #define K12_SRCDESC_PORT_TYPE 0x1a /* 1 byte */
269 #define K12_SRCDESC_EXTRALEN 0x1e /* uint16, big endian */
270 #define K12_SRCDESC_NAMELEN 0x20 /* uint16, big endian */
271 #define K12_SRCDESC_STACKLEN 0x22 /* uint16, big endian */
273 #define K12_SRCDESC_EXTRATYPE 0x24 /* uint32, big endian */
275 #define K12_SRCDESC_ATM_VPI 0x38 /* uint16, big endian */
276 #define K12_SRCDESC_ATM_VCI 0x3a /* uint16, big endian */
277 #define K12_SRCDESC_ATM_AAL 0x3c /* 1 byte */
279 #define K12_SRCDESC_DS0_MASK 0x3c /* 32 bytes */
282 * A "stack file", as appears in a K12_REC_STK_FILE record, is a text
283 * file (with CR-LF line endings) with a sequence of lines, each of
284 * which begins with a keyword, and has white-space-separated tokens
289 * STKVER, which is followed by a number (presumably a version number
290 * for the stack file format)
292 * STACK, which is followed by a quoted string ("ProtocolStack" in one
293 * file) and two numbers
295 * PATH, which is followed by a non-quoted string giving the pathname
296 * of the directory containing the stack file
298 * HLAYER, which is followed by a quoted string, a path for something
299 * (protocol module?), a keyword ("LOADED", in one file), and a
300 * quoted string giving a description - this is probably a protocol
303 * LAYER, which has a similar syntax to HLAYER - the first quoted
304 * string is a protocol name
306 * RELATION, which has a quoted string giving a protocol name,
307 * another quoted string giving a protocol name, and a condition
308 * specifier of some sort, which probably says the second protocol
309 * is layered atop the first protocol if the condition is true.
310 * The first protocol can also be "BASE", which means that the
311 * second protocol is the lowest-level protocol.
312 * The conditions are:
314 * CPLX, which may mean "complex" - it has parenthesized expressions
315 * including "&", presumably a boolean AND, with the individual
316 * tests being L:expr, where L is a letter such as "L", "D", or "P",
319 * 0x........ for L, where each . is a hex digit or a ?, presumably
320 * meaning "don't care"
322 * 0;0{=,!=}0b........ for D, where . is presumably a bit or a ?
324 * param=value for P, where param is something such as "src_port"
325 * and value is a value, presumably to test, for example, TCP or
328 * UNCOND, presumably meaning "always"
330 * PARAM, followed by a parameter name (as with P:) and a value,
331 * possibly followed by LAYPARAM and a hex value
333 * DECKRNL, followed by a quoted string protocol name, un-quoted
334 * "LSBF" or "MSBF" (Least/Most Significant Byte First?), and
335 * an un-quoted string ending with _DK
337 * LAYPARAM, followed by a quoted protocol name and a number (-2147221504
338 * in one file, which is 0x80040000)
340 * SPC_CONF, folloed by a number, a quoted string with numbers separated
341 * by hyphens, and another number
343 * CIC_CONF, with a similar syntax to SPC_CONF
345 * LAYPOS, followed by a protocol name or "BASE" and 3 numbers.
347 * Most of this is probably not useful, but the RELATION lines with
348 * "BASE" could be used to figure out how to start the dissection
349 * (if we knew what "L" and "D" did), and *some* of the others might
350 * be useful if they don't match what's already in various dissector
351 * tables (the ones for IP and a higher-level protocol, for example,
352 * aren't very useful, as those are standardized, but the ones for
353 * TCP, UDP, and SCTP ports, and SCTP PPIs, might be useful).
357 * get_record: Get the next record into a buffer
358 * Every about 0x2000 bytes 0x10 bytes are inserted in the file,
359 * even in the middle of a record.
360 * This reads the next record without the eventual 0x10 bytes.
361 * returns the length of the record + the stuffing (if any)
363 * Returns number of bytes read on success, 0 on EOF, -1 on error;
364 * if -1 is returned, *err is set to the error indication and, for
365 * errors where that's appropriate, *err_info is set to an additional
368 * XXX: works at most with 0x1FFF bytes per record
370 static gint get_record(k12_t *file_data, FILE_T fh, gint64 file_offset,
371 gboolean is_random, int *err, gchar **err_info) {
372 guint8 *buffer = is_random ? file_data->rand_read_buff : file_data->seq_read_buff;
373 guint buffer_len = is_random ? file_data->rand_read_buff_len : file_data->seq_read_buff_len;
383 /* where the next unknown 0x10 bytes are stuffed to the file */
384 guint junky_offset = 0x2000 - (gint) ( (file_offset - 0x200) % 0x2000 );
386 K12_DBG(6,("get_record: ENTER: junky_offset=%" G_GINT64_MODIFIER "d, file_offset=%" G_GINT64_MODIFIER "d",junky_offset,file_offset));
388 /* no buffer is given, lets create it */
389 if (buffer == NULL) {
390 buffer = (guint8*)g_malloc(0x2000);
393 file_data->rand_read_buff = buffer;
394 file_data->rand_read_buff_len = buffer_len;
396 file_data->seq_read_buff = buffer;
397 file_data->seq_read_buff_len = buffer_len;
401 /* Get the record length. */
402 if ( junky_offset == 0x2000 ) {
403 /* the length of the record is 0x10 bytes ahead from we are reading */
404 bytes_read = file_read(junk,0x14,fh);
406 if (bytes_read == 2 && junk[0] == 0xff && junk[1] == 0xff) {
407 K12_DBG(1,("get_record: EOF"));
409 } else if ( bytes_read < 0x14 ){
410 K12_DBG(1,("get_record: SHORT READ OR ERROR"));
411 *err = file_error(fh, err_info);
413 *err = WTAP_ERR_SHORT_READ;
418 memcpy(buffer,&(junk[0x10]),4);
420 /* the length of the record is right where we are reading */
421 bytes_read = file_read(buffer, 0x4, fh);
423 if (bytes_read == 2 && buffer[0] == 0xff && buffer[1] == 0xff) {
424 K12_DBG(1,("get_record: EOF"));
426 } else if (bytes_read == 4 && buffer[0] == 0xff && buffer[1] == 0xff
427 && buffer[2] == 0x00 && buffer[3] == 0x00) {
429 * In at least one k18 RF5 file, there appears to be a "record"
430 * with a length value of 0xffff0000, followed by a bunch of
431 * data that doesn't appear to be records, including a long
434 * We treat a length value of 0xffff0000 as an end-of-file
437 * XXX - is this a length indication, or will it appear
438 * at the beginning of an 8KB block, so that we should
439 * check for it above?
441 K12_DBG(1,("get_record: EOF"));
443 } else if ( bytes_read != 0x4 ) {
444 K12_DBG(1,("get_record: SHORT READ OR ERROR"));
445 *err = file_error(fh, err_info);
447 *err = WTAP_ERR_SHORT_READ;
453 left = pntoh32(buffer + K12_RECORD_LEN);
459 K12_DBG(5,("get_record: GET length=%u",left));
462 * Record length must be at least large enough for the length,
465 * XXX - Is WTAP_MAX_PACKET_SIZE the right check for a maximum
466 * record size? Should we report this error differently?
468 if (left < 4 || left > WTAP_MAX_PACKET_SIZE) {
469 K12_DBG(1,("get_record: Invalid GET length=%u",left));
470 *err = WTAP_ERR_BAD_FILE;
471 *err_info = g_strdup_printf("get_record: Invalid GET length=%u",left);
476 * XXX - calculate the lowest power of 2 >= left, rather than just
479 while (left > buffer_len) {
480 buffer = (guint8*)g_realloc(buffer,buffer_len*=2);
482 file_data->rand_read_buff = buffer;
483 file_data->rand_read_buff_len = buffer_len;
485 file_data->seq_read_buff = buffer;
486 file_data->seq_read_buff_len = buffer_len;
493 /* Read the rest of the record. */
495 K12_DBG(6,("get_record: looping left=%d junky_offset=%" G_GINT64_MODIFIER "d",left,junky_offset));
497 if (junky_offset > left) {
498 bytes_read += last_read = file_read(writep, left, fh);
500 if ( last_read != left ) {
501 K12_DBG(1,("get_record: SHORT READ OR ERROR"));
502 *err = file_error(fh, err_info);
504 *err = WTAP_ERR_SHORT_READ;
508 K12_HEX_ASCII_DUMP(5,file_offset, "GOT record", buffer, actual_len);
512 bytes_read += last_read = file_read(writep, junky_offset, fh);
514 if ( last_read != junky_offset ) {
515 K12_DBG(1,("get_record: SHORT READ OR ERROR, read=%d expected=%d",last_read, junky_offset));
516 *err = file_error(fh, err_info);
518 *err = WTAP_ERR_SHORT_READ;
525 bytes_read += last_read = file_read(junk, 0x10, fh);
527 if ( last_read != 0x10 ) {
528 K12_DBG(1,("get_record: SHORT READ OR ERROR"));
529 *err = file_error(fh, err_info);
531 *err = WTAP_ERR_SHORT_READ;
536 left -= junky_offset;
537 junky_offset = 0x2000;
542 K12_HEX_ASCII_DUMP(5,file_offset, "GOT record", buffer, actual_len);
547 process_packet_data(struct wtap_pkthdr *phdr, Buffer *target, guint8 *buffer,
548 gint len, k12_t *k12)
556 k12_src_desc_t* src_desc;
558 phdr->presence_flags = WTAP_HAS_TS;
560 ts = pntoh64(buffer + K12_PACKET_TIMESTAMP);
562 phdr->ts.secs = (guint32) ((ts / 2000000) + 631152000);
563 phdr->ts.nsecs = (guint32) ( (ts % 2000000) * 500 );
565 length = pntoh32(buffer + K12_RECORD_FRAME_LEN) & 0x00001FFF;
566 phdr->len = phdr->caplen = length;
568 type = pntoh32(buffer + K12_RECORD_TYPE);
569 buffer_offset = (type == K12_REC_D0020) ? K12_PACKET_FRAME_D0020 : K12_PACKET_FRAME;
571 buffer_assure_space(target, length);
572 memcpy(buffer_start_ptr(target), buffer + buffer_offset, length);
574 /* extra information need by some protocols */
575 extra_len = len - buffer_offset - length;
576 buffer_assure_space(&(k12->extra_info), extra_len);
577 memcpy(buffer_start_ptr(&(k12->extra_info)),
578 buffer + buffer_offset + length, extra_len);
579 phdr->pseudo_header.k12.extra_info = (guint8*)buffer_start_ptr(&(k12->extra_info));
580 phdr->pseudo_header.k12.extra_length = extra_len;
582 src_id = pntoh32(buffer + K12_RECORD_SRC_ID);
583 K12_DBG(5,("process_packet_data: src_id=%.8x",src_id));
584 phdr->pseudo_header.k12.input = src_id;
586 if ( ! (src_desc = (k12_src_desc_t*)g_hash_table_lookup(k12->src_by_id,GUINT_TO_POINTER(src_id))) ) {
588 * Some records from K15 files have a port ID of an undeclared
589 * interface which happens to be the only one with the first byte changed.
590 * It is still unknown how to recognize when this happens.
591 * If the lookup of the interface record fails we'll mask it
594 src_desc = (k12_src_desc_t*)g_hash_table_lookup(k12->src_by_id,GUINT_TO_POINTER(src_id&K12_RECORD_SRC_ID_MASK));
598 K12_DBG(5,("process_packet_data: input_name='%s' stack_file='%s' type=%x",src_desc->input_name,src_desc->stack_file,src_desc->input_type));
599 phdr->pseudo_header.k12.input_name = src_desc->input_name;
600 phdr->pseudo_header.k12.stack_file = src_desc->stack_file;
601 phdr->pseudo_header.k12.input_type = src_desc->input_type;
603 switch(src_desc->input_type) {
604 case K12_PORT_ATMPVC:
605 if ((long)(buffer_offset + length + K12_PACKET_OFFSET_CID) < len) {
606 phdr->pseudo_header.k12.input_info.atm.vp = pntoh16(buffer + buffer_offset + length + K12_PACKET_OFFSET_VP);
607 phdr->pseudo_header.k12.input_info.atm.vc = pntoh16(buffer + buffer_offset + length + K12_PACKET_OFFSET_VC);
608 phdr->pseudo_header.k12.input_info.atm.cid = *((unsigned char*)(buffer + buffer_offset + length + K12_PACKET_OFFSET_CID));
613 memcpy(&(phdr->pseudo_header.k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
617 K12_DBG(5,("process_packet_data: NO SRC_RECORD FOUND"));
619 memset(&(phdr->pseudo_header.k12),0,sizeof(phdr->pseudo_header.k12));
620 phdr->pseudo_header.k12.input_name = "unknown port";
621 phdr->pseudo_header.k12.stack_file = "unknown stack file";
624 phdr->pseudo_header.k12.input = src_id;
625 phdr->pseudo_header.k12.stuff = k12;
628 static gboolean k12_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset) {
629 k12_t *k12 = (k12_t *)wth->priv;
630 k12_src_desc_t* src_desc;
637 offset = file_tell(wth->fh);
639 /* ignore the record if it isn't a packet */
641 K12_DBG(5,("k12_read: offset=%i",offset));
643 *data_offset = offset;
645 len = get_record(k12, wth->fh, offset, FALSE, err, err_info);
650 } else if (len == 0) {
654 } else if (len < K12_RECORD_SRC_ID + 4) {
655 /* Record not large enough to contain a src ID */
656 *err = WTAP_ERR_BAD_FILE;
657 *err_info = g_strdup_printf("data record length %d too short", len);
661 buffer = k12->seq_read_buff;
663 type = pntoh32(buffer + K12_RECORD_TYPE);
664 src_id = pntoh32(buffer + K12_RECORD_SRC_ID);
667 if ( ! (src_desc = (k12_src_desc_t*)g_hash_table_lookup(k12->src_by_id,GUINT_TO_POINTER(src_id))) ) {
669 * Some records from K15 files have a port ID of an undeclared
670 * interface which happens to be the only one with the first byte changed.
671 * It is still unknown how to recognize when this happens.
672 * If the lookup of the interface record fails we'll mask it
675 src_desc = (k12_src_desc_t*)g_hash_table_lookup(k12->src_by_id,GUINT_TO_POINTER(src_id&K12_RECORD_SRC_ID_MASK));
678 K12_DBG(5,("k12_read: record type=%x src_id=%x",type,src_id));
682 } while ( ((type & K12_MASK_PACKET) != K12_REC_PACKET && (type & K12_MASK_PACKET) != K12_REC_D0020) || !src_id || !src_desc );
684 process_packet_data(&wth->phdr, wth->frame_buffer, buffer, len, k12);
690 static gboolean k12_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info) {
691 k12_t *k12 = (k12_t *)wth->priv;
695 K12_DBG(5,("k12_seek_read: ENTER"));
697 if ( file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) {
698 K12_DBG(5,("k12_seek_read: SEEK ERROR"));
702 len = get_record(k12, wth->random_fh, seek_off, TRUE, err, err_info);
704 K12_DBG(5,("k12_seek_read: READ ERROR"));
706 } else if (len < K12_RECORD_SRC_ID + 4) {
707 /* Record not large enough to contain a src ID */
708 K12_DBG(5,("k12_seek_read: SHORT READ"));
709 *err = WTAP_ERR_SHORT_READ;
713 buffer = k12->rand_read_buff;
715 process_packet_data(phdr, buf, buffer, len, k12);
717 K12_DBG(5,("k12_seek_read: DONE OK"));
723 static k12_t* new_k12_file_data(void) {
724 k12_t* fd = g_new(k12_t,1);
727 fd->num_of_records = 0;
728 fd->src_by_name = g_hash_table_new(g_str_hash,g_str_equal);
729 fd->src_by_id = g_hash_table_new(g_direct_hash,g_direct_equal);
730 fd->seq_read_buff = NULL;
731 fd->seq_read_buff_len = 0;
732 fd->rand_read_buff = NULL;
733 fd->rand_read_buff_len = 0;
735 buffer_init(&(fd->extra_info), 100);
740 static gboolean destroy_srcdsc(gpointer k _U_, gpointer v, gpointer p _U_) {
741 k12_src_desc_t* rec = (k12_src_desc_t*)v;
743 g_free(rec->input_name);
744 g_free(rec->stack_file);
750 static void destroy_k12_file_data(k12_t* fd) {
751 g_hash_table_destroy(fd->src_by_id);
752 g_hash_table_foreach_remove(fd->src_by_name,destroy_srcdsc,NULL);
753 g_hash_table_destroy(fd->src_by_name);
754 buffer_free(&(fd->extra_info));
755 g_free(fd->seq_read_buff);
756 g_free(fd->rand_read_buff);
760 static void k12_close(wtap *wth) {
761 k12_t *k12 = (k12_t *)wth->priv;
763 destroy_k12_file_data(k12);
764 wth->priv = NULL; /* destroy_k12_file_data freed it */
766 K12_DBG(5,("k12_close: CLOSED"));
767 if (env_file) fclose(dbg_out);
772 int k12_open(wtap *wth, int *err, gchar **err_info) {
774 guint8 header_buffer[0x200];
787 gchar* env_level = getenv("K12_DEBUG_LEVEL");
788 env_file = getenv("K12_DEBUG_FILENAME");
790 dbg_out = ws_fopen(env_file,"w");
791 if (dbg_out == NULL) {
793 K12_DBG(1,("unable to open K12 DEBUG FILENAME for writing! Logging to standard error"));
798 if ( env_level ) debug_level = (unsigned int)strtoul(env_level,NULL,10);
799 K12_DBG(1,("k12_open: ENTER debug_level=%u",debug_level));
802 if ( file_read(header_buffer,0x200,wth->fh) != 0x200 ) {
803 K12_DBG(1,("k12_open: FILE HEADER TOO SHORT OR READ ERROR"));
804 *err = file_error(wth->fh, err_info);
805 if (*err != 0 && *err != WTAP_ERR_SHORT_READ) {
810 if ( memcmp(header_buffer,k12_file_magic,8) != 0 ) {
811 K12_DBG(1,("k12_open: BAD MAGIC"));
818 file_data = new_k12_file_data();
820 file_data->file_len = pntoh32( header_buffer + 0x8);
821 file_data->num_of_records = pntoh32( header_buffer + 0xC );
823 K12_DBG(5,("k12_open: FILE_HEADER OK: offset=%x file_len=%i records=%i",
826 file_data->num_of_records ));
830 len = get_record(file_data, wth->fh, offset, FALSE, err, err_info);
833 K12_DBG(1,("k12_open: BAD HEADER RECORD",len));
834 destroy_k12_file_data(file_data);
838 K12_DBG(1,("k12_open: BAD HEADER RECORD",len));
839 *err = WTAP_ERR_SHORT_READ;
840 destroy_k12_file_data(file_data);
845 K12_DBG(1,("k12_open: BAD HEADER RECORD",len));
846 *err = WTAP_ERR_SHORT_READ;
847 destroy_k12_file_data(file_data);
851 read_buffer = file_data->seq_read_buff;
853 rec_len = pntoh32( read_buffer + K12_RECORD_LEN );
854 if (rec_len < K12_RECORD_TYPE + 4) {
855 /* Record isn't long enough to have a type field */
856 *err = WTAP_ERR_BAD_FILE;
857 *err_info = g_strdup_printf("k12_open: record length %u < %u",
858 rec_len, K12_RECORD_TYPE + 4);
861 type = pntoh32( read_buffer + K12_RECORD_TYPE );
863 if ( (type & K12_MASK_PACKET) == K12_REC_PACKET ||
864 (type & K12_MASK_PACKET) == K12_REC_D0020) {
866 * we are at the first packet record, rewind and leave.
868 if (file_seek(wth->fh, offset, SEEK_SET, err) == -1) {
869 destroy_k12_file_data(file_data);
872 K12_DBG(5,("k12_open: FIRST PACKET offset=%x",offset));
874 } else if (type == K12_REC_SRCDSC || type == K12_REC_SRCDSC2 ) {
875 rec = g_new0(k12_src_desc_t,1);
877 if (rec_len < K12_SRCDESC_STACKLEN + 2) {
878 /* Record isn't long enough to have a stack length field */
879 *err = WTAP_ERR_BAD_FILE;
880 *err_info = g_strdup_printf("k12_open: source descriptor record length %u < %u",
881 rec_len, K12_SRCDESC_STACKLEN + 2);
884 extra_len = pntoh16( read_buffer + K12_SRCDESC_EXTRALEN );
885 name_len = pntoh16( read_buffer + K12_SRCDESC_NAMELEN );
886 stack_len = pntoh16( read_buffer + K12_SRCDESC_STACKLEN );
888 rec->input = pntoh32( read_buffer + K12_RECORD_SRC_ID );
890 K12_DBG(5,("k12_open: INTERFACE RECORD offset=%x interface=%x",offset,rec->input));
892 if (name_len == 0 || stack_len == 0
893 || 0x20 + extra_len + name_len + stack_len > rec_len ) {
895 K12_DBG(5,("k12_open: failed (name_len == 0 || stack_len == 0 "
896 "|| 0x20 + extra_len + name_len + stack_len > rec_len) extra_len=%i name_len=%i stack_len=%i"));
897 destroy_k12_file_data(file_data);
902 if (rec_len < K12_SRCDESC_EXTRATYPE + 4) {
903 /* Record isn't long enough to have a source descriptor extra type field */
904 *err = WTAP_ERR_BAD_FILE;
905 *err_info = g_strdup_printf("k12_open: source descriptor record length %u < %u",
906 rec_len, K12_SRCDESC_EXTRATYPE + 4);
909 switch(( rec->input_type = pntoh32( read_buffer + K12_SRCDESC_EXTRATYPE ) )) {
911 if (rec_len < K12_SRCDESC_DS0_MASK + 32) {
912 /* Record isn't long enough to have a source descriptor extra type field */
913 *err = WTAP_ERR_BAD_FILE;
914 *err_info = g_strdup_printf("k12_open: source descriptor record length %u < %u",
915 rec_len, K12_SRCDESC_DS0_MASK + 12);
919 rec->input_info.ds0mask = 0x00000000;
921 for (i = 0; i < 32; i++) {
922 rec->input_info.ds0mask |= ( *(read_buffer + K12_SRCDESC_DS0_MASK + i) == 0xff ) ? 0x1<<(31-i) : 0x0;
926 case K12_PORT_ATMPVC:
927 if (rec_len < K12_SRCDESC_ATM_VCI + 2) {
928 /* Record isn't long enough to have a source descriptor extra type field */
929 *err = WTAP_ERR_BAD_FILE;
930 *err_info = g_strdup_printf("k12_open: source descriptor record length %u < %u",
931 rec_len, K12_SRCDESC_DS0_MASK + 12);
935 rec->input_info.atm.vp = pntoh16( read_buffer + K12_SRCDESC_ATM_VPI );
936 rec->input_info.atm.vc = pntoh16( read_buffer + K12_SRCDESC_ATM_VCI );
942 /* Record viewer generated files don't have this information */
943 if (rec_len < K12_SRCDESC_PORT_TYPE + 1) {
944 /* Record isn't long enough to have a source descriptor extra type field */
945 *err = WTAP_ERR_BAD_FILE;
946 *err_info = g_strdup_printf("k12_open: source descriptor record length %u < %u",
947 rec_len, K12_SRCDESC_DS0_MASK + 12);
950 if (read_buffer[K12_SRCDESC_PORT_TYPE] >= 0x14
951 && read_buffer[K12_SRCDESC_PORT_TYPE] <= 0x17) {
952 /* For ATM2_E1DS1, ATM2_E3DS3,
953 ATM2_STM1EL and ATM2_STM1OP */
954 rec->input_type = K12_PORT_ATMPVC;
955 rec->input_info.atm.vp = 0;
956 rec->input_info.atm.vc = 0;
960 /* XXX - this is assumed, in a number of places (not just in the
961 ascii_strdown_inplace() call below) to be null-terminated;
962 is that guaranteed (even with a corrupt file)?
963 Obviously not, as a corrupt file could contain anything
964 here; the Tektronix document says the strings "must end
965 with \0", but a bad file could fail to add the \0. */
966 if (rec_len < K12_SRCDESC_EXTRATYPE + extra_len + name_len + stack_len) {
967 /* Record isn't long enough to have a source descriptor extra type field */
968 *err = WTAP_ERR_BAD_FILE;
969 *err_info = g_strdup_printf("k12_open: source descriptor record length %u < %u",
970 rec_len, K12_SRCDESC_EXTRATYPE + extra_len + name_len + stack_len);
973 rec->input_name = (gchar *)g_memdup(read_buffer + K12_SRCDESC_EXTRATYPE + extra_len, name_len);
974 rec->stack_file = (gchar *)g_memdup(read_buffer + K12_SRCDESC_EXTRATYPE + extra_len + name_len, stack_len);
976 ascii_strdown_inplace (rec->stack_file);
978 g_hash_table_insert(file_data->src_by_id,GUINT_TO_POINTER(rec->input),rec);
979 g_hash_table_insert(file_data->src_by_name,rec->stack_file,rec);
983 } else if (type == K12_REC_STK_FILE) {
984 K12_DBG(1,("k12_open: K12_REC_STK_FILE"));
985 K12_DBG(1,("Field 1: 0x%08x",pntoh32( read_buffer + 0x08 )));
986 K12_DBG(1,("Field 2: 0x%08x",pntoh32( read_buffer + 0x0c )));
987 K12_ASCII_DUMP(1, read_buffer, rec_len, 0x10);
992 K12_DBG(1,("k12_open: RECORD TYPE 0x%08x",type));
998 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_K12;
999 wth->file_encap = WTAP_ENCAP_K12;
1000 wth->snapshot_length = 0;
1001 wth->subtype_read = k12_read;
1002 wth->subtype_seek_read = k12_seek_read;
1003 wth->subtype_close = k12_close;
1004 wth->priv = (void *)file_data;
1005 wth->tsprecision = WTAP_FILE_TSPREC_NSEC;
1012 guint32 num_of_records;
1013 guint32 file_offset;
1016 int k12_dump_can_write_encap(int encap) {
1018 if (encap == WTAP_ENCAP_PER_PACKET)
1019 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
1021 if (encap != WTAP_ENCAP_K12)
1022 return WTAP_ERR_UNSUPPORTED_ENCAP;
1027 static const gchar dumpy_junk[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
1029 static gboolean k12_dump_record(wtap_dumper *wdh, guint32 len, guint8* buffer, int *err_p) {
1030 k12_dump_t *k12 = (k12_dump_t *)wdh->priv;
1031 guint32 junky_offset = (0x2000 - ( (k12->file_offset - 0x200) % 0x2000 )) % 0x2000;
1033 if (len > junky_offset) {
1035 if (! wtap_dump_file_write(wdh, buffer, junky_offset, err_p))
1038 if (! wtap_dump_file_write(wdh, dumpy_junk, 0x10, err_p))
1041 if (! wtap_dump_file_write(wdh, buffer+junky_offset, len - junky_offset, err_p))
1044 k12->file_offset += len + 0x10;
1046 if (! wtap_dump_file_write(wdh, buffer, len, err_p))
1048 k12->file_offset += len;
1051 k12->num_of_records++;
1055 static void k12_dump_src_setting(gpointer k _U_, gpointer v, gpointer p) {
1056 k12_src_desc_t* src_desc = (k12_src_desc_t*)v;
1057 wtap_dumper *wdh = (wtap_dumper *)p;
1061 int errxxx; /* dummy */
1064 guint8 buffer[0x2000];
1092 guint8 unk_data[0x10];
1103 obj.record.type = g_htonl(K12_REC_SRCDSC);
1104 obj.record.unk32_1 = g_htonl(0x00000001);
1105 obj.record.input = g_htonl(src_desc->input);
1107 obj.record.unk32_2 = g_htons(0x0000);
1108 obj.record.color = g_htons(0x060f);
1109 obj.record.unk32_3 = g_htonl(0x00000003);
1110 switch (src_desc->input_type) {
1111 case K12_PORT_ATMPVC:
1112 obj.record.unk32_4 = g_htonl(0x01001400);
1115 obj.record.unk32_4 = g_htonl(0x01000100);
1118 obj.record.unk16_1 = g_htons(0x0000);
1119 obj.record.name_len = (guint16) strlen(src_desc->input_name) + 1;
1120 obj.record.stack_len = (guint16) strlen(src_desc->stack_file) + 1;
1122 obj.record.extra.type = g_htonl(src_desc->input_type);
1124 switch (src_desc->input_type) {
1125 case K12_PORT_ATMPVC:
1126 obj.record.extra_len = g_htons(0x18);
1127 obj.record.extra.desc.atm.vp = g_htons(src_desc->input_info.atm.vp);
1128 obj.record.extra.desc.atm.vc = g_htons(src_desc->input_info.atm.vc);
1132 obj.record.extra_len = g_htons(0x18);
1133 for( i=0; i<32; i++ ) {
1134 obj.record.extra.desc.ds0mask.mask[i] =
1135 (src_desc->input_info.ds0mask & (1 << i)) ? 0xff : 0x00;
1140 obj.record.extra_len = g_htons(0x08);
1145 memcpy(obj.buffer + offset,
1146 src_desc->input_name,
1147 obj.record.name_len);
1149 memcpy(obj.buffer + offset + obj.record.name_len,
1150 src_desc->stack_file,
1151 obj.record.stack_len);
1153 len = offset + obj.record.name_len + obj.record.stack_len;
1154 len += (len % 4) ? 4 - (len % 4) : 0;
1156 obj.record.len = g_htonl(len);
1157 obj.record.name_len = g_htons(obj.record.name_len);
1158 obj.record.stack_len = g_htons(obj.record.stack_len);
1160 k12_dump_record(wdh,len,obj.buffer, &errxxx); /* fwrite errs ignored: see k12_dump below */
1163 static gboolean k12_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
1164 const guint8 *pd, int *err) {
1165 const union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
1166 k12_dump_t *k12 = (k12_dump_t *)wdh->priv;
1169 guint8 buffer[0x2000];
1180 guint8 frame[0x1fc0];
1184 if (k12->num_of_records == 0) {
1185 k12_t* file_data = (k12_t*)pseudo_header->k12.stuff;
1186 /* XXX: We'll assume that any fwrite errors in k12_dump_src_setting will */
1187 /* repeat during the final k12_dump_record at the end of k12_dump */
1188 /* (and thus cause an error return from k12_dump). */
1189 /* (I don't see a reasonably clean way to handle any fwrite errors */
1190 /* encountered in k12_dump_src_setting). */
1191 g_hash_table_foreach(file_data->src_by_id,k12_dump_src_setting,wdh);
1193 obj.record.len = 0x20 + phdr->len;
1194 obj.record.len += (obj.record.len % 4) ? 4 - obj.record.len % 4 : 0;
1196 len = obj.record.len;
1198 obj.record.len = g_htonl(obj.record.len);
1200 obj.record.type = g_htonl(K12_REC_PACKET);
1201 obj.record.frame_len = g_htonl(phdr->len);
1202 obj.record.input = g_htonl(pseudo_header->k12.input);
1204 obj.record.ts = GUINT64_TO_BE((((guint64)phdr->ts.secs - 631152000) * 2000000) + (phdr->ts.nsecs / 1000 * 2));
1206 memcpy(obj.record.frame,pd,phdr->len);
1208 return k12_dump_record(wdh,len,obj.buffer, err);
1211 static const guint8 k12_eof[] = {0xff,0xff};
1213 static gboolean k12_dump_close(wtap_dumper *wdh, int *err) {
1214 k12_dump_t *k12 = (k12_dump_t *)wdh->priv;
1216 guint8 b[sizeof(guint32)];
1220 if (! wtap_dump_file_write(wdh, k12_eof, 2, err))
1223 if (wtap_dump_file_seek(wdh, 8, SEEK_SET, err) == -1)
1226 d.u = g_htonl(k12->file_len);
1228 if (! wtap_dump_file_write(wdh, d.b, 4, err))
1231 d.u = g_htonl(k12->num_of_records);
1233 if (! wtap_dump_file_write(wdh, d.b, 4, err))
1240 gboolean k12_dump_open(wtap_dumper *wdh, int *err) {
1243 if ( ! wtap_dump_file_write(wdh, k12_file_magic, 8, err)) {
1247 if (wtap_dump_file_seek(wdh, 0x200, SEEK_SET, err) == -1)
1250 wdh->subtype_write = k12_dump;
1251 wdh->subtype_close = k12_dump_close;
1253 k12 = (k12_dump_t *)g_malloc(sizeof(k12_dump_t));
1254 wdh->priv = (void *)k12;
1255 k12->file_len = 0x200;
1256 k12->num_of_records = 0;
1257 k12->file_offset = 0x200;