dcerpc-nt: make use of cb_str_postprocess_options() in cb_byte_array_postprocess()
[wireshark-sm.git] / wiretap / k12.c
blob79e758db43dd3073ce3edecdd244dbce9b29acfa
1 /*
2 * k12.c
4 * routines for importing tektronix k12xx *.rf5 files
6 * Copyright (c) 2005, Luis E. Garia Ontanon <luis@ontanon.org>
8 * Wiretap Library
9 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
11 * SPDX-License-Identifier: GPL-2.0-or-later
14 #include "config.h"
15 #include "k12.h"
17 #include <stdlib.h>
18 #include <string.h>
20 #include "wtap-int.h"
21 #include "file_wrappers.h"
23 #include <wsutil/str_util.h>
24 #include <wsutil/glib-compat.h>
27 * See
29 * https://www.tek.com/manual/record-file-api-programmer-manual
31 * for some information about the file format. You may have to fill in
32 * a form to download the document ("Record File API Programmer Manual").
34 * Unfortunately, it describes an API that delivers records from an rf5
35 * file, not the raw format of an rf5 file, so, while it gives the formats
36 * of the records with various types, it does not indicate how those records
37 * are stored in the file.
40 static int k12_file_type_subtype = -1;
42 void register_k12(void);
44 /* #define DEBUG_K12 */
45 #ifdef DEBUG_K12
46 #include <stdio.h>
47 #include <stdarg.h>
48 #include <wsutil/file_util.h>
50 FILE* dbg_out;
51 char* env_file;
53 static unsigned int debug_level;
55 void k12_fprintf(const char* fmt, ...) {
56 va_list ap;
58 va_start(ap,fmt);
59 vfprintf(dbg_out, fmt, ap);
60 va_end(ap);
63 #define CAT(a,b) a##b
64 #define K12_DBG(level,args) do { if (level <= debug_level) { \
65 fprintf(dbg_out,"%s:%d: ",CAT(__FI,LE__),CAT(__LI,NE__)); \
66 k12_fprintf args ; \
67 fprintf(dbg_out,"\n"); \
68 } } while(0)
70 void k12_hex_ascii_dump(unsigned level, int64_t offset, const char* label, const unsigned char* b, unsigned int len) {
71 static const char* c2t[] = {
72 "00","01","02","03","04","05","06","07","08","09","0a","0b","0c","0d","0e","0f",
73 "10","11","12","13","14","15","16","17","18","19","1a","1b","1c","1d","1e","1f",
74 "20","21","22","23","24","25","26","27","28","29","2a","2b","2c","2d","2e","2f",
75 "30","31","32","33","34","35","36","37","38","39","3a","3b","3c","3d","3e","3f",
76 "40","41","42","43","44","45","46","47","48","49","4a","4b","4c","4d","4e","4f",
77 "50","51","52","53","54","55","56","57","58","59","5a","5b","5c","5d","5e","5f",
78 "60","61","62","63","64","65","66","67","68","69","6a","6b","6c","6d","6e","6f",
79 "70","71","72","73","74","75","76","77","78","79","7a","7b","7c","7d","7e","7f",
80 "80","81","82","83","84","85","86","87","88","89","8a","8b","8c","8d","8e","8f",
81 "90","91","92","93","94","95","96","97","98","99","9a","9b","9c","9d","9e","9f",
82 "a0","a1","a2","a3","a4","a5","a6","a7","a8","a9","aa","ab","ac","ad","ae","af",
83 "b0","b1","b2","b3","b4","b5","b6","b7","b8","b9","ba","bb","bc","bd","be","bf",
84 "c0","c1","c2","c3","c4","c5","c6","c7","c8","c9","ca","cb","cc","cd","ce","cf",
85 "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","da","db","dc","dd","de","df",
86 "e0","e1","e2","e3","e4","e5","e6","e7","e8","e9","ea","eb","ec","ed","ee","ef",
87 "f0","f1","f2","f3","f4","f5","f6","f7","f8","f9","fa","fb","fc","fd","fe","ff"
89 unsigned int i, j;
91 if (debug_level < level) return;
93 fprintf(dbg_out,"%s(%.8" PRIx64 ",%.4x):\n",label,offset,len);
95 for (i=0 ; i<len ; i += 16) {
96 for (j=0; j<16; j++) {
97 if ((j%4)==0)
98 fprintf(dbg_out," ");
99 if ((i+j)<len)
100 fprintf(dbg_out, "%s", c2t[b[i+j]]);
101 else
102 fprintf(dbg_out, " ");
104 fprintf(dbg_out, " ");
105 for (j=0; j<16; j++) {
106 if ((i+j)<len)
107 fprintf(dbg_out, "%c", g_ascii_isprint(b[i+j]) ? b[i+j] : '.');
109 fprintf(dbg_out,"\n");
113 #define K12_HEX_ASCII_DUMP(x,a,b,c,d) k12_hex_ascii_dump(x,a,b,c,d)
115 void k12_ascii_dump(unsigned level, uint8_t *buf, uint32_t len, uint32_t buf_offset) {
116 uint32_t i;
118 if (debug_level < level) return;
120 for (i = buf_offset; i < len; i++) {
121 if (g_ascii_isprint(buf[i]) || buf[i] == '\n' || buf[i] == '\t')
122 putc(buf[i], dbg_out);
123 else if (buf[i] == '\0')
124 fprintf(dbg_out, "(NUL)\n");
128 #define K12_ASCII_DUMP(x,a,b,c) k12_ascii_dump(x,a,b,c)
130 #else
131 #define K12_DBG(level,args) (void)0
132 #define K12_HEX_ASCII_DUMP(x,a,b,c,d)
133 #define K12_ASCII_DUMP(x,a,b,c)
134 #endif
139 * A 32-bit .rf5 file begins with a 512-byte file header, containing:
141 * a 32-bit big-endian file header length, in bytes - always 512 in
142 * the files we've seen;
144 * 4 unknown bytes, always 0x12 0x05 0x00 0x10;
146 * a 32-bit big-endian file length, giving the total length of the file,
147 * in bytes;
149 * a 32-bit big-endian number giving the "page size" of the file, in
150 * bytes, which is normally 8192;
152 * 20 unknown bytes;
154 * a 32-bit count of the number of records in the file;
156 * 4 unknown bytes;
158 * a 32-bit count of the number of records in the file;
160 * 464 unknown bytes;
162 * followed by a sequence of records containing:
164 * a 32-bit big-endian record length;
166 * a 32-bit big-endian record type;
168 * a 32-bit big-endian frame length;
170 * a 32-bit big-endian source ID.
172 * Every 8192 bytes, starting immediately after the 512-byte header,
173 * there's a 16-byte blob; it's not part of the record data.
174 * There's no obvious pattern to the data; it might be junk left
175 * in memory as the file was being written.
177 * There's a 16-bit terminator FFFF at the end.
179 * Older versions of the Wireshark .rf5 writing code incorrectly wrote
180 * the header - they put 512 in the file length field (counting only the
181 * header), put a count of records into the "page size" field, and wrote
182 * out zeroes in the rest of the header. We detect those files by
183 * checking whether the rest of the header is zero.
187 * We use the first 8 bytes of the file header as a magic number.
189 static const uint8_t k12_file_magic[] = { 0x00, 0x00, 0x02, 0x00 ,0x12, 0x05, 0x00, 0x10 };
191 #define K12_FILE_HDR_LEN 512
194 * Offsets in the file header.
196 #define K12_FILE_HDR_MAGIC_NUMBER 0x00
197 #define K12_FILE_HDR_FILE_SIZE 0x08
198 #define K12_FILE_HDR_PAGE_SIZE 0x0C
199 #define K12_FILE_HDR_RECORD_COUNT_1 0x24
200 #define K12_FILE_HDR_RECORD_COUNT_2 0x2C
202 #define K12_FILE_BLOB_LEN 16
204 typedef struct {
205 uint32_t file_len;
206 uint32_t num_of_records; /* XXX: not sure about this */
208 GHashTable* src_by_id; /* k12_srcdsc_recs by input */
209 GHashTable* src_by_name; /* k12_srcdsc_recs by stack_name */
211 uint8_t *seq_read_buff; /* read buffer for sequential reading */
212 unsigned seq_read_buff_len; /* length of that buffer */
213 uint8_t *rand_read_buff; /* read buffer for random reading */
214 unsigned rand_read_buff_len; /* length of that buffer */
216 Buffer extra_info; /* Buffer to hold per packet extra information */
217 } k12_t;
219 typedef struct _k12_src_desc_t {
220 uint32_t input;
221 uint32_t input_type;
222 char* input_name;
223 char* stack_file;
224 k12_input_info_t input_info;
225 } k12_src_desc_t;
229 * According to the Tektronix documentation, this value is a combination of
230 * a "group" code and a "type" code, with both being 2-byte values and
231 * with the "group" code followe by the "type" code. The "group" values
232 * are:
234 * 0x0001 - "data event"
235 * 0x0002 - "text or L1 event"
236 * 0x0007 - "configuration event"
238 * and the "type" values are:
240 * data events:
241 * 0x0020 - "frame" (i.e., "an actual packet")
242 * 0x0021 - "transparent frame"
243 * 0x0022 - "bit data (TRAU frame)"
244 * 0x0024 - "used to mark the frame which is a fragment"
245 * 0x0026 - "used to mark the frame which is a fragment"
246 * 0x0028 - "used to mark the frame which is generated by the LSA"
247 * 0x002A - "used to mark the frame which is generated by the LSA"
249 * text or L1 events:
250 * 0x0030 - "text event"
251 * 0x0031 - "L1 event"
252 * 0x0032 - "L1 event (BAI)"
253 * 0x0033 - "L1 event (VX)"
255 * configuration events:
256 * 0x0040 - Logical Data Source configuration event
257 * 0x0041 - Logical Link configuration event
259 /* so far we've seen these types of records */
260 #define K12_REC_PACKET 0x00010020 /* an actual packet */
261 #define K12_REC_D0020 0x000d0020 /* an actual packet, seen in a k18 file */
262 #define K12_REC_SCENARIO 0x00070040 /* what appears as the window's title */
263 #define K12_REC_SRCDSC 0x00070041 /* port-stack mapping + more, the key of the whole thing */
264 #define K12_REC_STK_FILE 0x00070042 /* a dump of an stk file */
265 #define K12_REC_SRCDSC2 0x00070043 /* another port-stack mapping */
266 #define K12_REC_TEXT 0x00070044 /* a string containing something with a grammar (conditions/responses?) */
267 #define K12_REC_START 0x00020030 /* a string containing human readable start time */
268 #define K12_REC_STOP 0x00020031 /* a string containing human readable stop time */
271 * According to the Tektronix documentation, packets, i.e. "data events",
272 * have several different group/type values, which differ in the last
273 * nibble of the type code. For now, we just mask that nibble off; the
274 * format of the items are different, so we might have to treat different
275 * data event types differently.
277 #define K12_MASK_PACKET 0xfffffff0
279 /* offsets of elements in the records */
280 #define K12_RECORD_LEN 0x0 /* uint32, in bytes */
281 #define K12_RECORD_TYPE 0x4 /* uint32, see above */
282 #define K12_RECORD_FRAME_LEN 0x8 /* uint32, in bytes */
283 #define K12_RECORD_SRC_ID 0xc /* uint32 */
286 * Some records from K15 files have a port ID of an undeclared
287 * interface which happens to be the only one with the first byte changed.
288 * It is still unknown how to recognize when this happens.
289 * If the lookup of the interface record fails we'll mask it
290 * and retry.
292 #define K12_RECORD_SRC_ID_MASK 0x00ffffff
294 /* elements of packet records */
295 #define K12_PACKET_TIMESTAMP 0x18 /* int64 (8b) representing 1/2us since 01-01-1990 Z00:00:00 */
297 #define K12_PACKET_FRAME 0x20 /* start of the actual frame in the record */
298 #define K12_PACKET_FRAME_D0020 0x34 /* start of the actual frame in the record */
300 #define K12_PACKET_OFFSET_VP 0x08 /* 2 bytes, big endian */
301 #define K12_PACKET_OFFSET_VC 0x0a /* 2 bytes, big endian */
302 #define K12_PACKET_OFFSET_CID 0x0c /* 1 byte */
304 /* elements of the source description records */
305 #define K12_SRCDESC_COLOR_FOREGROUND 0x12 /* 1 byte */
306 #define K12_SRCDESC_COLOR_BACKGROUND 0x13 /* 1 byte */
308 #define K12_SRCDESC_PORT_TYPE 0x1a /* 1 byte */
309 #define K12_SRCDESC_HWPARTLEN 0x1e /* uint16, big endian */
310 #define K12_SRCDESC_NAMELEN 0x20 /* uint16, big endian */
311 #define K12_SRCDESC_STACKLEN 0x22 /* uint16, big endian */
313 /* Hardware part of the record */
314 #define K12_SRCDESC_HWPART 0x24 /* offset of the hardware part */
316 /* Offsets relative to the beginning of the hardware part */
317 #define K12_SRCDESC_HWPARTTYPE 0 /* uint32, big endian */
319 #define K12_SRCDESC_DS0_MASK 24 /* variable-length */
321 #define K12_SRCDESC_ATM_VPI 20 /* uint16, big endian */
322 #define K12_SRCDESC_ATM_VCI 22 /* uint16, big endian */
323 #define K12_SRCDESC_ATM_AAL 24 /* 1 byte */
326 * A "stack file", as appears in a K12_REC_STK_FILE record, is a text
327 * file (with CR-LF line endings) with a sequence of lines, each of
328 * which begins with a keyword, and has white-space-separated tokens
329 * after that.
331 * They appear to be:
333 * STKVER, which is followed by a number (presumably a version number
334 * for the stack file format)
336 * STACK, which is followed by a quoted string ("ProtocolStack" in one
337 * file) and two numbers
339 * PATH, which is followed by a non-quoted string giving the pathname
340 * of the directory containing the stack file
342 * HLAYER, which is followed by a quoted string, a path for something
343 * (protocol module?), a keyword ("LOADED", in one file), and a
344 * quoted string giving a description - this is probably a protocol
345 * layer of some sort
347 * LAYER, which has a similar syntax to HLAYER - the first quoted
348 * string is a protocol name
350 * RELATION, which has a quoted string giving a protocol name,
351 * another quoted string giving a protocol name, and a condition
352 * specifier of some sort, which probably says the second protocol
353 * is layered atop the first protocol if the condition is true.
354 * The first protocol can also be "BASE", which means that the
355 * second protocol is the lowest-level protocol.
356 * The conditions are:
358 * CPLX, which may mean "complex" - it has parenthesized expressions
359 * including "&", presumably a boolean AND, with the individual
360 * tests being L:expr, where L is a letter such as "L", "D", or "P",
361 * and expr is:
363 * 0x........ for L, where each . is a hex digit or a ?, presumably
364 * meaning "don't care"
366 * 0;0{=,!=}0b........ for D, where . is presumably a bit or a ?
368 * param=value for P, where param is something such as "src_port"
369 * and value is a value, presumably to test, for example, TCP or
370 * UDP ports
372 * UNCOND, presumably meaning "always"
374 * PARAM, followed by a parameter name (as with P:) and a value,
375 * possibly followed by LAYPARAM and a hex value
377 * DECKRNL, followed by a quoted string protocol name, un-quoted
378 * "LSBF" or "MSBF" (Least/Most Significant Byte First?), and
379 * an un-quoted string ending with _DK
381 * LAYPARAM, followed by a quoted protocol name and a number (-2147221504
382 * in one file, which is 0x80040000)
384 * SPC_CONF, folloed by a number, a quoted string with numbers separated
385 * by hyphens, and another number
387 * CIC_CONF, with a similar syntax to SPC_CONF
389 * LAYPOS, followed by a protocol name or "BASE" and 3 numbers.
391 * Most of this is probably not useful, but the RELATION lines with
392 * "BASE" could be used to figure out how to start the dissection
393 * (if we knew what "L" and "D" did), and *some* of the others might
394 * be useful if they don't match what's already in various dissector
395 * tables (the ones for IP and a higher-level protocol, for example,
396 * aren't very useful, as those are standardized, but the ones for
397 * TCP, UDP, and SCTP ports, and SCTP PPIs, might be useful).
401 * get_record: Get the next record into a buffer
402 * Every 8192 bytes 16 bytes are inserted in the file,
403 * even in the middle of a record.
404 * This reads the next record without the eventual 16 bytes.
405 * returns the length of the record + the stuffing (if any)
407 * Returns number of bytes read on success, 0 on EOF, -1 on error;
408 * if -1 is returned, *err is set to the error indication and, for
409 * errors where that's appropriate, *err_info is set to an additional
410 * error string.
412 * XXX: works at most with 8191 bytes per record
414 static int get_record(k12_t *file_data, FILE_T fh, int64_t file_offset,
415 bool is_random, int *err, char **err_info) {
416 uint8_t *buffer = is_random ? file_data->rand_read_buff : file_data->seq_read_buff;
417 unsigned buffer_len = is_random ? file_data->rand_read_buff_len : file_data->seq_read_buff_len;
418 unsigned total_read = 0;
419 unsigned left;
420 uint8_t* writep;
421 #ifdef DEBUG_K12
422 unsigned actual_len;
423 #endif
426 * Where the next unknown 16 bytes are stuffed to the file.
427 * Following the file header, they appear every 8192 bytes,
428 * starting right after the file header, so if the file offset
429 * relative to the file header is a multiple of 8192, the
430 * 16-byte blob is there.
432 unsigned junky_offset = 8192 - (int) ( (file_offset - K12_FILE_HDR_LEN) % 8192 );
434 K12_DBG(6,("get_record: ENTER: junky_offset=%" PRId64 ", file_offset=%" PRId64,junky_offset,file_offset));
436 /* no buffer is given, lets create it */
437 if (buffer == NULL) {
438 buffer = (uint8_t*)g_malloc(8192);
439 buffer_len = 8192;
440 if (is_random) {
441 file_data->rand_read_buff = buffer;
442 file_data->rand_read_buff_len = buffer_len;
443 } else {
444 file_data->seq_read_buff = buffer;
445 file_data->seq_read_buff_len = buffer_len;
449 if ( junky_offset == 8192 ) {
451 * We're at the beginning of one of the 16-byte blobs,
452 * so we first need to skip the blob.
454 * XXX - what if the blob is in the middle of the record
455 * length? If the record length is always a multiple of
456 * 4 bytes, that won't happen.
458 if ( ! wtap_read_bytes( fh, NULL, K12_FILE_BLOB_LEN, err, err_info ) )
459 return -1;
460 total_read += K12_FILE_BLOB_LEN;
464 * Read the record length.
466 if ( !wtap_read_bytes( fh, buffer, 4, err, err_info ) )
467 return -1;
468 total_read += 4;
470 left = pntoh32(buffer + K12_RECORD_LEN);
471 #ifdef DEBUG_K12
472 actual_len = left;
473 #endif
474 junky_offset -= 4;
476 K12_DBG(5,("get_record: GET length=%u",left));
479 * Record length must be at least large enough for the length
480 * and type, hence 8 bytes.
482 * XXX - is WTAP_MAX_PACKET_SIZE_STANDARD the right check for a maximum
483 * record size? Should we report this error differently?
485 if (left < 8) {
486 *err = WTAP_ERR_BAD_FILE;
487 *err_info = ws_strdup_printf("k12: Record length %u is less than 8 bytes long",left);
488 return -1;
490 if (left > WTAP_MAX_PACKET_SIZE_STANDARD) {
491 *err = WTAP_ERR_BAD_FILE;
492 *err_info = ws_strdup_printf("k12: Record length %u is greater than the maximum %u",left,WTAP_MAX_PACKET_SIZE_STANDARD);
493 return -1;
497 * XXX - calculate the lowest power of 2 >= left, rather than just
498 * looping.
500 while (left > buffer_len) {
501 buffer = (uint8_t*)g_realloc(buffer,buffer_len*=2);
502 if (is_random) {
503 file_data->rand_read_buff = buffer;
504 file_data->rand_read_buff_len = buffer_len;
505 } else {
506 file_data->seq_read_buff = buffer;
507 file_data->seq_read_buff_len = buffer_len;
511 writep = buffer + 4;
512 left -= 4;
514 /* Read the rest of the record. */
515 do {
516 K12_DBG(6,("get_record: looping left=%d junky_offset=%" PRId64,left,junky_offset));
518 if (junky_offset > left) {
520 * The next 16-byte blob is past the end of this record.
521 * Just read the rest of the record.
523 if ( !wtap_read_bytes( fh, writep, left, err, err_info ) )
524 return -1;
525 total_read += left;
526 break;
527 } else {
529 * The next 16-byte blob is part of this record.
530 * Read up to the blob.
532 if ( !wtap_read_bytes( fh, writep, junky_offset, err, err_info ) )
533 return -1;
535 total_read += junky_offset;
536 writep += junky_offset;
539 * Skip the blob.
541 if ( !wtap_read_bytes( fh, NULL, K12_FILE_BLOB_LEN, err, err_info ) )
542 return -1;
543 total_read += K12_FILE_BLOB_LEN;
545 left -= junky_offset;
546 junky_offset = 8192;
549 } while(left);
551 K12_HEX_ASCII_DUMP(5,file_offset, "GOT record", buffer, actual_len);
552 return total_read;
555 static bool
556 memiszero(const void *ptr, size_t count)
558 const uint8_t *p = (const uint8_t *)ptr;
560 while (count != 0) {
561 if (*p != 0)
562 return false;
563 p++;
564 count--;
566 return true;
569 static bool
570 process_packet_data(wtap_rec *rec, Buffer *target, uint8_t *buffer,
571 unsigned record_len, k12_t *k12, int *err, char **err_info)
573 uint32_t type;
574 unsigned buffer_offset;
575 uint64_t ts;
576 uint32_t length;
577 uint32_t extra_len;
578 uint32_t src_id;
579 k12_src_desc_t* src_desc;
581 type = pntoh32(buffer + K12_RECORD_TYPE);
582 buffer_offset = (type == K12_REC_D0020) ? K12_PACKET_FRAME_D0020 : K12_PACKET_FRAME;
583 if (buffer_offset > record_len) {
584 *err = WTAP_ERR_BAD_FILE;
585 *err_info = ws_strdup_printf("k12: Frame data offset %u > record length %u",
586 buffer_offset, record_len);
587 return false;
590 length = pntoh32(buffer + K12_RECORD_FRAME_LEN) & 0x00001FFF;
591 if (length > record_len - buffer_offset) {
592 *err = WTAP_ERR_BAD_FILE;
593 *err_info = ws_strdup_printf("k12: Frame length %u > record frame data %u",
594 length, record_len - buffer_offset);
595 return false;
598 rec->rec_type = REC_TYPE_PACKET;
599 rec->block = wtap_block_create(WTAP_BLOCK_PACKET);
600 rec->presence_flags = WTAP_HAS_TS;
602 ts = pntoh64(buffer + K12_PACKET_TIMESTAMP);
604 rec->ts.secs = (time_t) ((ts / 2000000) + 631152000);
605 rec->ts.nsecs = (uint32_t) ( (ts % 2000000) * 500 );
607 rec->rec_header.packet_header.len = rec->rec_header.packet_header.caplen = length;
609 ws_buffer_assure_space(target, length);
610 memcpy(ws_buffer_start_ptr(target), buffer + buffer_offset, length);
612 /* extra information need by some protocols */
613 extra_len = record_len - buffer_offset - length;
614 ws_buffer_assure_space(&(k12->extra_info), extra_len);
615 memcpy(ws_buffer_start_ptr(&(k12->extra_info)),
616 buffer + buffer_offset + length, extra_len);
617 rec->rec_header.packet_header.pseudo_header.k12.extra_info = (uint8_t*)ws_buffer_start_ptr(&(k12->extra_info));
618 rec->rec_header.packet_header.pseudo_header.k12.extra_length = extra_len;
620 src_id = pntoh32(buffer + K12_RECORD_SRC_ID);
621 K12_DBG(5,("process_packet_data: src_id=%.8x",src_id));
622 rec->rec_header.packet_header.pseudo_header.k12.input = src_id;
624 if ( ! (src_desc = (k12_src_desc_t*)g_hash_table_lookup(k12->src_by_id,GUINT_TO_POINTER(src_id))) ) {
626 * Some records from K15 files have a port ID of an undeclared
627 * interface which happens to be the only one with the first byte changed.
628 * It is still unknown how to recognize when this happens.
629 * If the lookup of the interface record fails we'll mask it
630 * and retry.
632 src_desc = (k12_src_desc_t*)g_hash_table_lookup(k12->src_by_id,GUINT_TO_POINTER(src_id&K12_RECORD_SRC_ID_MASK));
635 if (src_desc) {
636 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));
637 rec->rec_header.packet_header.pseudo_header.k12.input_name = src_desc->input_name;
638 rec->rec_header.packet_header.pseudo_header.k12.stack_file = src_desc->stack_file;
639 rec->rec_header.packet_header.pseudo_header.k12.input_type = src_desc->input_type;
641 switch(src_desc->input_type) {
642 case K12_PORT_ATMPVC:
643 if (buffer_offset + length + K12_PACKET_OFFSET_CID < record_len) {
644 rec->rec_header.packet_header.pseudo_header.k12.input_info.atm.vp = pntoh16(buffer + buffer_offset + length + K12_PACKET_OFFSET_VP);
645 rec->rec_header.packet_header.pseudo_header.k12.input_info.atm.vc = pntoh16(buffer + buffer_offset + length + K12_PACKET_OFFSET_VC);
646 rec->rec_header.packet_header.pseudo_header.k12.input_info.atm.cid = *((unsigned char*)(buffer + buffer_offset + length + K12_PACKET_OFFSET_CID));
647 break;
649 /* Fall through */
650 default:
651 memcpy(&(rec->rec_header.packet_header.pseudo_header.k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
652 break;
654 } else {
655 K12_DBG(5,("process_packet_data: NO SRC_RECORD FOUND"));
657 memset(&(rec->rec_header.packet_header.pseudo_header.k12),0,sizeof(rec->rec_header.packet_header.pseudo_header.k12));
658 rec->rec_header.packet_header.pseudo_header.k12.input_name = "unknown port";
659 rec->rec_header.packet_header.pseudo_header.k12.stack_file = "unknown stack file";
662 rec->rec_header.packet_header.pseudo_header.k12.input = src_id;
663 rec->rec_header.packet_header.pseudo_header.k12.stuff = k12;
664 return true;
667 static bool k12_read(wtap *wth, wtap_rec *rec, Buffer *buf, int *err, char **err_info, int64_t *data_offset) {
668 k12_t *k12 = (k12_t *)wth->priv;
669 k12_src_desc_t* src_desc;
670 uint8_t* buffer;
671 int64_t offset;
672 int len;
673 uint32_t type;
674 uint32_t src_id;
676 offset = file_tell(wth->fh);
678 /* ignore the record if it isn't a packet */
679 do {
680 if ( k12->num_of_records == 0 ) {
681 /* No more records */
682 *err = 0;
683 return false;
686 K12_DBG(5,("k12_read: offset=%i",offset));
688 *data_offset = offset;
690 len = get_record(k12, wth->fh, offset, false, err, err_info);
692 if (len < 0) {
693 /* read error */
694 return false;
695 } else if (len == 0) {
696 /* EOF */
697 *err = WTAP_ERR_SHORT_READ;
698 return false;
699 } else if (len < K12_RECORD_SRC_ID + 4) {
700 /* Record not large enough to contain a src ID */
701 *err = WTAP_ERR_BAD_FILE;
702 *err_info = ws_strdup_printf("k12: Data record length %d too short", len);
703 return false;
705 k12->num_of_records--;
707 buffer = k12->seq_read_buff;
709 type = pntoh32(buffer + K12_RECORD_TYPE);
710 src_id = pntoh32(buffer + K12_RECORD_SRC_ID);
713 if ( ! (src_desc = (k12_src_desc_t*)g_hash_table_lookup(k12->src_by_id,GUINT_TO_POINTER(src_id))) ) {
715 * Some records from K15 files have a port ID of an undeclared
716 * interface which happens to be the only one with the first byte changed.
717 * It is still unknown how to recognize when this happens.
718 * If the lookup of the interface record fails we'll mask it
719 * and retry.
721 src_desc = (k12_src_desc_t*)g_hash_table_lookup(k12->src_by_id,GUINT_TO_POINTER(src_id&K12_RECORD_SRC_ID_MASK));
724 K12_DBG(5,("k12_read: record type=%x src_id=%x",type,src_id));
726 offset += len;
728 } while ( ((type & K12_MASK_PACKET) != K12_REC_PACKET && (type & K12_MASK_PACKET) != K12_REC_D0020) || !src_id || !src_desc );
730 return process_packet_data(rec, buf, buffer, (unsigned)len, k12, err, err_info);
734 static bool k12_seek_read(wtap *wth, int64_t seek_off, wtap_rec *rec, Buffer *buf, int *err, char **err_info) {
735 k12_t *k12 = (k12_t *)wth->priv;
736 uint8_t* buffer;
737 int len;
738 bool status;
740 K12_DBG(5,("k12_seek_read: ENTER"));
742 if ( file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) {
743 K12_DBG(5,("k12_seek_read: SEEK ERROR"));
744 return false;
747 len = get_record(k12, wth->random_fh, seek_off, true, err, err_info);
748 if (len < 0) {
749 K12_DBG(5,("k12_seek_read: READ ERROR"));
750 return false;
751 } else if (len < K12_RECORD_SRC_ID + 4) {
752 /* Record not large enough to contain a src ID */
753 K12_DBG(5,("k12_seek_read: SHORT READ"));
754 *err = WTAP_ERR_SHORT_READ;
755 return false;
758 buffer = k12->rand_read_buff;
760 status = process_packet_data(rec, buf, buffer, (unsigned)len, k12, err, err_info);
762 K12_DBG(5,("k12_seek_read: DONE OK"));
764 return status;
768 static k12_t* new_k12_file_data(void) {
769 k12_t* fd = g_new(k12_t,1);
771 fd->file_len = 0;
772 fd->num_of_records = 0;
773 fd->src_by_name = g_hash_table_new(g_str_hash,g_str_equal);
774 fd->src_by_id = g_hash_table_new(g_direct_hash,g_direct_equal);
775 fd->seq_read_buff = NULL;
776 fd->seq_read_buff_len = 0;
777 fd->rand_read_buff = NULL;
778 fd->rand_read_buff_len = 0;
780 ws_buffer_init(&(fd->extra_info), 100);
782 return fd;
785 static gboolean destroy_srcdsc(void *k _U_, void *v, void *p _U_) {
786 k12_src_desc_t* rec = (k12_src_desc_t*)v;
788 g_free(rec->input_name);
789 g_free(rec->stack_file);
790 g_free(rec);
792 return true;
795 static void destroy_k12_file_data(k12_t* fd) {
796 g_hash_table_destroy(fd->src_by_id);
797 g_hash_table_foreach_remove(fd->src_by_name,destroy_srcdsc,NULL);
798 g_hash_table_destroy(fd->src_by_name);
799 ws_buffer_free(&(fd->extra_info));
800 g_free(fd->seq_read_buff);
801 g_free(fd->rand_read_buff);
802 g_free(fd);
805 static void k12_close(wtap *wth) {
806 k12_t *k12 = (k12_t *)wth->priv;
808 destroy_k12_file_data(k12);
809 wth->priv = NULL; /* destroy_k12_file_data freed it */
810 #ifdef DEBUG_K12
811 K12_DBG(5,("k12_close: CLOSED"));
812 if (env_file) fclose(dbg_out);
813 #endif
817 wtap_open_return_val k12_open(wtap *wth, int *err, char **err_info) {
818 k12_src_desc_t* rec;
819 uint8_t header_buffer[K12_FILE_HDR_LEN];
820 uint8_t* read_buffer;
821 uint32_t type;
822 long offset;
823 long len;
824 unsigned port_type;
825 uint32_t rec_len;
826 uint32_t hwpart_len;
827 uint32_t name_len;
828 uint32_t stack_len;
829 unsigned i;
830 k12_t* file_data;
832 #ifdef DEBUG_K12
833 char* env_level = getenv("K12_DEBUG_LEVEL");
834 env_file = getenv("K12_DEBUG_FILENAME");
835 if ( env_file ) {
836 dbg_out = ws_fopen(env_file,"w");
837 if (dbg_out == NULL) {
838 dbg_out = stderr;
839 K12_DBG(1,("unable to open K12 DEBUG FILENAME for writing! Logging to standard error"));
842 else
843 dbg_out = stderr;
844 if ( env_level ) debug_level = (unsigned int)strtoul(env_level,NULL,10);
845 K12_DBG(1,("k12_open: ENTER debug_level=%u",debug_level));
846 #endif
848 if ( !wtap_read_bytes(wth->fh,header_buffer,K12_FILE_HDR_LEN,err,err_info) ) {
849 K12_DBG(1,("k12_open: FILE HEADER TOO SHORT OR READ ERROR"));
850 if (*err != WTAP_ERR_SHORT_READ) {
851 return WTAP_OPEN_ERROR;
853 return WTAP_OPEN_NOT_MINE;
856 if ( memcmp(header_buffer,k12_file_magic,8) != 0 ) {
857 K12_DBG(1,("k12_open: BAD MAGIC"));
858 return WTAP_OPEN_NOT_MINE;
861 offset = K12_FILE_HDR_LEN;
863 file_data = new_k12_file_data();
865 file_data->file_len = pntoh32( header_buffer + 0x8);
866 if (memiszero(header_buffer + 0x10, K12_FILE_HDR_LEN - 0x10)) {
868 * The rest of the file header is all zeroes. That means
869 * this is a file written by the old Wireshark code, and
870 * a count of records in the file is at an offset of 0x0C.
872 file_data->num_of_records = pntoh32( header_buffer + 0x0C );
873 } else {
875 * There's at least one non-zero byte in the rest of the
876 * header. The value 8192 is at 0xC (page size?), and
877 * what appears to be the number of records in the file
878 * is at an offset of 0x24 and at an offset of 0x2c.
880 * If the two values are not the same, we fail; if that's
881 * the case, we need to see the file to figure out which
882 * of those two values, if any, is the count.
884 file_data->num_of_records = pntoh32( header_buffer + K12_FILE_HDR_RECORD_COUNT_1 );
885 if ( file_data->num_of_records != pntoh32( header_buffer + K12_FILE_HDR_RECORD_COUNT_2 ) ) {
886 *err = WTAP_ERR_BAD_FILE;
887 *err_info = ws_strdup_printf("k12: two different record counts, %u at 0x%02x and %u at 0x%02x",
888 file_data->num_of_records,
889 K12_FILE_HDR_RECORD_COUNT_1,
890 pntoh32( header_buffer + K12_FILE_HDR_RECORD_COUNT_2 ),
891 K12_FILE_HDR_RECORD_COUNT_2 );
892 destroy_k12_file_data(file_data);
893 return WTAP_OPEN_ERROR;
897 K12_DBG(5,("k12_open: FILE_HEADER OK: offset=%x file_len=%i records=%i",
898 offset,
899 file_data->file_len,
900 file_data->num_of_records ));
902 do {
903 if ( file_data->num_of_records == 0 ) {
904 *err = WTAP_ERR_SHORT_READ;
905 destroy_k12_file_data(file_data);
906 return WTAP_OPEN_ERROR;
909 len = get_record(file_data, wth->fh, offset, false, err, err_info);
911 if ( len < 0 ) {
912 K12_DBG(1,("k12_open: BAD HEADER RECORD",len));
913 destroy_k12_file_data(file_data);
914 return WTAP_OPEN_ERROR;
916 if ( len == 0 ) {
917 K12_DBG(1,("k12_open: BAD HEADER RECORD",len));
918 *err = WTAP_ERR_SHORT_READ;
919 destroy_k12_file_data(file_data);
920 return WTAP_OPEN_ERROR;
923 read_buffer = file_data->seq_read_buff;
925 rec_len = pntoh32( read_buffer + K12_RECORD_LEN );
926 if (rec_len < K12_RECORD_TYPE + 4) {
927 /* Record isn't long enough to have a type field */
928 *err = WTAP_ERR_BAD_FILE;
929 *err_info = ws_strdup_printf("k12: record length %u < %u",
930 rec_len, K12_RECORD_TYPE + 4);
931 destroy_k12_file_data(file_data);
932 return WTAP_OPEN_ERROR;
934 type = pntoh32( read_buffer + K12_RECORD_TYPE );
936 if ( (type & K12_MASK_PACKET) == K12_REC_PACKET ||
937 (type & K12_MASK_PACKET) == K12_REC_D0020) {
939 * we are at the first packet record, rewind and leave.
941 if (file_seek(wth->fh, offset, SEEK_SET, err) == -1) {
942 destroy_k12_file_data(file_data);
943 return WTAP_OPEN_ERROR;
945 K12_DBG(5,("k12_open: FIRST PACKET offset=%x",offset));
946 break;
949 switch (type) {
951 case K12_REC_SRCDSC:
952 case K12_REC_SRCDSC2:
953 rec = g_new0(k12_src_desc_t,1);
955 if (rec_len < K12_SRCDESC_HWPART) {
957 * Record isn't long enough to have the fixed-length portion
958 * of the source descriptor field.
960 *err = WTAP_ERR_BAD_FILE;
961 *err_info = ws_strdup_printf("k12: source descriptor record length %u < %u",
962 rec_len, K12_SRCDESC_HWPART);
963 destroy_k12_file_data(file_data);
964 g_free(rec);
965 return WTAP_OPEN_ERROR;
967 port_type = read_buffer[K12_SRCDESC_PORT_TYPE];
968 hwpart_len = pntoh16( read_buffer + K12_SRCDESC_HWPARTLEN );
969 name_len = pntoh16( read_buffer + K12_SRCDESC_NAMELEN );
970 stack_len = pntoh16( read_buffer + K12_SRCDESC_STACKLEN );
972 rec->input = pntoh32( read_buffer + K12_RECORD_SRC_ID );
974 K12_DBG(5,("k12_open: INTERFACE RECORD offset=%x interface=%x",offset,rec->input));
976 if (name_len == 0) {
977 K12_DBG(5,("k12_open: failed (name_len == 0 in source description"));
978 destroy_k12_file_data(file_data);
979 g_free(rec);
980 return WTAP_OPEN_NOT_MINE;
982 if (stack_len == 0) {
983 K12_DBG(5,("k12_open: failed (stack_len == 0 in source description"));
984 destroy_k12_file_data(file_data);
985 g_free(rec);
986 return WTAP_OPEN_NOT_MINE;
988 if (rec_len < K12_SRCDESC_HWPART + hwpart_len + name_len + stack_len) {
990 * Record isn't long enough to have the full source descriptor
991 * field, including the variable-length parts.
993 *err = WTAP_ERR_BAD_FILE;
994 *err_info = ws_strdup_printf("k12: source descriptor record length %u < %u (%u + %u + %u + %u)",
995 rec_len,
996 K12_SRCDESC_HWPART + hwpart_len + name_len + stack_len,
997 K12_SRCDESC_HWPART, hwpart_len, name_len, stack_len);
998 destroy_k12_file_data(file_data);
999 g_free(rec);
1000 return WTAP_OPEN_ERROR;
1003 if (hwpart_len) {
1004 if (hwpart_len < 4) {
1005 /* Hardware part isn't long enough to have a type field */
1006 *err = WTAP_ERR_BAD_FILE;
1007 *err_info = ws_strdup_printf("k12: source descriptor hardware part length %u < 4",
1008 hwpart_len);
1009 destroy_k12_file_data(file_data);
1010 g_free(rec);
1011 return WTAP_OPEN_ERROR;
1013 switch(( rec->input_type = pntoh32( read_buffer + K12_SRCDESC_HWPART + K12_SRCDESC_HWPARTTYPE ) )) {
1014 case K12_PORT_DS0S:
1015 /* This appears to be variable-length */
1016 rec->input_info.ds0mask = 0x00000000;
1017 if (hwpart_len > K12_SRCDESC_DS0_MASK) {
1018 for (i = 0; i < hwpart_len - K12_SRCDESC_DS0_MASK; i++) {
1019 rec->input_info.ds0mask |= ( *(read_buffer + K12_SRCDESC_HWPART + K12_SRCDESC_DS0_MASK + i) == 0xff ) ? 1U<<(31-i) : 0x0;
1022 break;
1023 case K12_PORT_ATMPVC:
1024 if (hwpart_len < K12_SRCDESC_ATM_VCI + 2) {
1025 /* Hardware part isn't long enough to have ATM information */
1026 *err = WTAP_ERR_BAD_FILE;
1027 *err_info = ws_strdup_printf("k12: source descriptor hardware part length %u < %u",
1028 hwpart_len,
1029 K12_SRCDESC_ATM_VCI + 2);
1030 destroy_k12_file_data(file_data);
1031 g_free(rec);
1032 return WTAP_OPEN_ERROR;
1035 rec->input_info.atm.vp = pntoh16( read_buffer + K12_SRCDESC_HWPART + K12_SRCDESC_ATM_VPI );
1036 rec->input_info.atm.vc = pntoh16( read_buffer + K12_SRCDESC_HWPART + K12_SRCDESC_ATM_VCI );
1037 break;
1038 default:
1039 break;
1041 } else {
1042 /* Record viewer generated files don't have this information */
1043 if (port_type >= 0x14
1044 && port_type <= 0x17) {
1045 /* For ATM2_E1DS1, ATM2_E3DS3,
1046 ATM2_STM1EL and ATM2_STM1OP */
1047 rec->input_type = K12_PORT_ATMPVC;
1048 rec->input_info.atm.vp = 0;
1049 rec->input_info.atm.vc = 0;
1053 if (read_buffer[K12_SRCDESC_HWPART + hwpart_len + name_len - 1] != '\0') {
1054 *err = WTAP_ERR_BAD_FILE;
1055 *err_info = g_strdup("k12_open: source descriptor record contains non-null-terminated link-layer name");
1056 destroy_k12_file_data(file_data);
1057 g_free(rec);
1058 return WTAP_OPEN_ERROR;
1060 if (read_buffer[K12_SRCDESC_HWPART + hwpart_len + name_len + stack_len - 1] != '\0') {
1061 *err = WTAP_ERR_BAD_FILE;
1062 *err_info = g_strdup("k12_open: source descriptor record contains non-null-terminated stack path");
1063 destroy_k12_file_data(file_data);
1064 g_free(rec);
1065 return WTAP_OPEN_ERROR;
1067 rec->input_name = (char *)g_memdup2(read_buffer + K12_SRCDESC_HWPART + hwpart_len, name_len);
1068 rec->stack_file = (char *)g_memdup2(read_buffer + K12_SRCDESC_HWPART + hwpart_len + name_len, stack_len);
1070 ascii_strdown_inplace (rec->stack_file);
1072 g_hash_table_insert(file_data->src_by_id,GUINT_TO_POINTER(rec->input),rec);
1073 g_hash_table_insert(file_data->src_by_name,rec->stack_file,rec);
1074 break;
1076 case K12_REC_STK_FILE:
1077 K12_DBG(1,("k12_open: K12_REC_STK_FILE"));
1078 K12_DBG(1,("Field 1: 0x%08x",pntoh32( read_buffer + 0x08 )));
1079 K12_DBG(1,("Field 2: 0x%08x",pntoh32( read_buffer + 0x0c )));
1080 K12_ASCII_DUMP(1, read_buffer, rec_len, 16);
1081 break;
1083 default:
1084 K12_DBG(1,("k12_open: RECORD TYPE 0x%08x",type));
1085 break;
1087 offset += len;
1088 file_data->num_of_records--;
1089 } while(1);
1091 wth->file_type_subtype = k12_file_type_subtype;
1092 wth->file_encap = WTAP_ENCAP_K12;
1093 wth->snapshot_length = 0;
1094 wth->subtype_read = k12_read;
1095 wth->subtype_seek_read = k12_seek_read;
1096 wth->subtype_close = k12_close;
1097 wth->priv = (void *)file_data;
1098 wth->file_tsprec = WTAP_TSPREC_NSEC;
1101 * Add an IDB; we don't know how many interfaces were
1102 * involved, so we just say one interface, about which
1103 * we only know the link-layer type, snapshot length,
1104 * and time stamp resolution.
1106 wtap_add_generated_idb(wth);
1108 return WTAP_OPEN_MINE;
1111 typedef struct {
1112 uint32_t file_len;
1113 uint32_t num_of_records;
1114 uint32_t file_offset;
1115 } k12_dump_t;
1117 static int k12_dump_can_write_encap(int encap) {
1119 if (encap == WTAP_ENCAP_PER_PACKET)
1120 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
1122 if (encap != WTAP_ENCAP_K12)
1123 return WTAP_ERR_UNWRITABLE_ENCAP;
1125 return 0;
1128 static const char dumpy_junk[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
1130 static bool k12_dump_record(wtap_dumper *wdh, uint32_t len, uint8_t* buffer, int *err_p) {
1131 k12_dump_t *k12 = (k12_dump_t *)wdh->priv;
1132 uint32_t junky_offset = (8192 - ( (k12->file_offset - K12_FILE_HDR_LEN) % 8192 )) % 8192;
1134 if (len > junky_offset) {
1135 if (junky_offset) {
1136 if (! wtap_dump_file_write(wdh, buffer, junky_offset, err_p))
1137 return false;
1139 if (! wtap_dump_file_write(wdh, dumpy_junk, K12_FILE_BLOB_LEN, err_p))
1140 return false;
1142 if (! wtap_dump_file_write(wdh, buffer+junky_offset, len - junky_offset, err_p))
1143 return false;
1145 k12->file_offset += len + K12_FILE_BLOB_LEN;
1146 k12->file_len += len + K12_FILE_BLOB_LEN;
1147 } else {
1148 if (! wtap_dump_file_write(wdh, buffer, len, err_p))
1149 return false;
1150 k12->file_offset += len;
1151 k12->file_len += len;
1154 k12->num_of_records++;
1155 return true;
1158 static void k12_dump_src_setting(void *k _U_, void *v, void *p) {
1159 k12_src_desc_t* src_desc = (k12_src_desc_t*)v;
1160 wtap_dumper *wdh = (wtap_dumper *)p;
1161 uint32_t len;
1162 unsigned offset;
1163 unsigned i;
1164 int errxxx; /* dummy */
1166 union {
1167 uint8_t buffer[8192];
1169 struct {
1170 uint32_t len;
1171 uint32_t type;
1172 uint32_t unk32_1;
1173 uint32_t input;
1175 uint16_t unk32_2;
1176 uint16_t color;
1177 uint32_t unk32_3;
1178 uint32_t unk32_4;
1179 uint16_t unk16_1;
1180 uint16_t hwpart_len;
1182 uint16_t name_len;
1183 uint16_t stack_len;
1185 struct {
1186 uint32_t type;
1188 union {
1189 struct {
1190 uint32_t unk32;
1191 uint8_t mask[32];
1192 } ds0mask;
1194 struct {
1195 uint8_t unk_data[16];
1196 uint16_t vp;
1197 uint16_t vc;
1198 } atm;
1200 uint32_t unk;
1201 } desc;
1202 } extra;
1203 } record;
1204 } obj;
1206 obj.record.type = g_htonl(K12_REC_SRCDSC);
1207 obj.record.unk32_1 = g_htonl(0x00000001);
1208 obj.record.input = g_htonl(src_desc->input);
1210 obj.record.unk32_2 = g_htons(0x0000);
1211 obj.record.color = g_htons(0x060f);
1212 obj.record.unk32_3 = g_htonl(0x00000003);
1213 switch (src_desc->input_type) {
1214 case K12_PORT_ATMPVC:
1215 obj.record.unk32_4 = g_htonl(0x01001400);
1216 break;
1217 default:
1218 obj.record.unk32_4 = g_htonl(0x01000100);
1221 obj.record.unk16_1 = g_htons(0x0000);
1222 obj.record.name_len = (uint16_t) strlen(src_desc->input_name) + 1;
1223 obj.record.stack_len = (uint16_t) strlen(src_desc->stack_file) + 1;
1225 obj.record.extra.type = g_htonl(src_desc->input_type);
1227 switch (src_desc->input_type) {
1228 case K12_PORT_ATMPVC:
1229 obj.record.hwpart_len = g_htons(0x18);
1230 obj.record.extra.desc.atm.vp = g_htons(src_desc->input_info.atm.vp);
1231 obj.record.extra.desc.atm.vc = g_htons(src_desc->input_info.atm.vc);
1232 offset = 0x3c;
1233 break;
1234 case K12_PORT_DS0S:
1235 obj.record.hwpart_len = g_htons(0x18);
1236 for( i=0; i<32; i++ ) {
1237 obj.record.extra.desc.ds0mask.mask[i] =
1238 (src_desc->input_info.ds0mask & (1UL << i)) ? 0xff : 0x00;
1240 offset = 0x3c;
1241 break;
1242 default:
1243 obj.record.hwpart_len = g_htons(0x08);
1244 offset = 0x2c;
1245 break;
1248 memcpy(obj.buffer + offset,
1249 src_desc->input_name,
1250 obj.record.name_len);
1252 memcpy(obj.buffer + offset + obj.record.name_len,
1253 src_desc->stack_file,
1254 obj.record.stack_len);
1256 len = offset + obj.record.name_len + obj.record.stack_len;
1257 len += (len % 4) ? 4 - (len % 4) : 0;
1259 obj.record.len = g_htonl(len);
1260 obj.record.name_len = g_htons(obj.record.name_len);
1261 obj.record.stack_len = g_htons(obj.record.stack_len);
1263 k12_dump_record(wdh,len,obj.buffer, &errxxx); /* fwrite errs ignored: see k12_dump below */
1266 static bool k12_dump(wtap_dumper *wdh, const wtap_rec *rec,
1267 const uint8_t *pd, int *err, char **err_info _U_) {
1268 const union wtap_pseudo_header *pseudo_header = &rec->rec_header.packet_header.pseudo_header;
1269 k12_dump_t *k12 = (k12_dump_t *)wdh->priv;
1270 uint32_t len;
1271 union {
1272 uint8_t buffer[8192];
1273 struct {
1274 uint32_t len;
1275 uint32_t type;
1276 uint32_t frame_len;
1277 uint32_t input;
1279 uint32_t datum_1;
1280 uint32_t datum_2;
1281 uint64_t ts;
1283 uint8_t frame[0x1fc0];
1284 } record;
1285 } obj;
1287 /* We can only write packet records. */
1288 if (rec->rec_type != REC_TYPE_PACKET) {
1289 *err = WTAP_ERR_UNWRITABLE_REC_TYPE;
1290 return false;
1294 * Make sure this packet doesn't have a link-layer type that
1295 * differs from the one for the file.
1297 if (wdh->file_encap != rec->rec_header.packet_header.pkt_encap) {
1298 *err = WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
1299 return false;
1302 if (k12->num_of_records == 0) {
1303 k12_t* file_data = (k12_t*)pseudo_header->k12.stuff;
1304 /* XXX: We'll assume that any fwrite errors in k12_dump_src_setting will */
1305 /* repeat during the final k12_dump_record at the end of k12_dump */
1306 /* (and thus cause an error return from k12_dump). */
1307 /* (I don't see a reasonably clean way to handle any fwrite errors */
1308 /* encountered in k12_dump_src_setting). */
1309 g_hash_table_foreach(file_data->src_by_id,k12_dump_src_setting,wdh);
1311 obj.record.len = 0x20 + rec->rec_header.packet_header.caplen;
1312 obj.record.len += (obj.record.len % 4) ? 4 - obj.record.len % 4 : 0;
1314 len = obj.record.len;
1316 obj.record.len = g_htonl(obj.record.len);
1318 obj.record.type = g_htonl(K12_REC_PACKET);
1319 obj.record.frame_len = g_htonl(rec->rec_header.packet_header.caplen);
1320 obj.record.input = g_htonl(pseudo_header->k12.input);
1322 obj.record.ts = GUINT64_TO_BE((((uint64_t)rec->ts.secs - 631152000) * 2000000) + (rec->ts.nsecs / 1000 * 2));
1324 memcpy(obj.record.frame,pd,rec->rec_header.packet_header.caplen);
1326 return k12_dump_record(wdh,len,obj.buffer, err);
1329 static const uint8_t k12_eof[] = {0xff,0xff};
1331 static bool k12_dump_finish(wtap_dumper *wdh, int *err, char **err_info _U_) {
1332 k12_dump_t *k12 = (k12_dump_t *)wdh->priv;
1333 union {
1334 uint8_t b[sizeof(uint32_t)];
1335 uint32_t u;
1336 } d;
1338 if (! wtap_dump_file_write(wdh, k12_eof, 2, err))
1339 return false;
1340 k12->file_len += 2;
1342 if (wtap_dump_file_seek(wdh, K12_FILE_HDR_FILE_SIZE, SEEK_SET, err) == -1)
1343 return false;
1345 d.u = g_htonl(k12->file_len);
1347 if (! wtap_dump_file_write(wdh, d.b, 4, err))
1348 return false;
1350 if (wtap_dump_file_seek(wdh, K12_FILE_HDR_PAGE_SIZE, SEEK_SET, err) == -1)
1351 return false;
1353 d.u = g_htonl(8192);
1355 if (! wtap_dump_file_write(wdh, d.b, 4, err))
1356 return false;
1358 if (wtap_dump_file_seek(wdh, K12_FILE_HDR_RECORD_COUNT_1, SEEK_SET, err) == -1)
1359 return false;
1361 d.u = g_htonl(k12->num_of_records);
1363 if (! wtap_dump_file_write(wdh, d.b, 4, err))
1364 return false;
1366 if (wtap_dump_file_seek(wdh, K12_FILE_HDR_RECORD_COUNT_2, SEEK_SET, err) == -1)
1367 return false;
1369 d.u = g_htonl(k12->num_of_records);
1371 if (! wtap_dump_file_write(wdh, d.b, 4, err))
1372 return false;
1374 /* Prevent the above calls to wtap_dump_file_write() from
1375 * double-counting the header length
1377 wdh->bytes_dumped = k12->file_len;
1378 return true;
1382 static bool k12_dump_open(wtap_dumper *wdh, int *err, char **err_info _U_) {
1383 k12_dump_t *k12;
1385 if ( ! wtap_dump_file_write(wdh, k12_file_magic, 8, err)) {
1386 return false;
1389 if (wtap_dump_file_seek(wdh, K12_FILE_HDR_LEN, SEEK_SET, err) == -1)
1390 return false;
1392 wdh->bytes_dumped = K12_FILE_HDR_LEN;
1393 wdh->subtype_write = k12_dump;
1394 wdh->subtype_finish = k12_dump_finish;
1396 k12 = g_new(k12_dump_t, 1);
1397 wdh->priv = (void *)k12;
1398 k12->file_len = K12_FILE_HDR_LEN;
1399 k12->num_of_records = 0;
1400 k12->file_offset = K12_FILE_HDR_LEN;
1402 return true;
1405 static const struct supported_block_type k12_blocks_supported[] = {
1407 * We support packet blocks, with no comments or other options.
1409 { WTAP_BLOCK_PACKET, MULTIPLE_BLOCKS_SUPPORTED, NO_OPTIONS_SUPPORTED }
1412 static const struct file_type_subtype_info k12_info = {
1413 "Tektronix K12xx 32-bit .rf5 format", "rf5", "rf5", NULL,
1414 true, BLOCKS_SUPPORTED(k12_blocks_supported),
1415 k12_dump_can_write_encap, k12_dump_open, NULL
1418 void register_k12(void)
1420 k12_file_type_subtype = wtap_register_file_type_subtype(&k12_info);
1423 * Register name for backwards compatibility with the
1424 * wtap_filetypes table in Lua.
1426 wtap_register_backwards_compatibility_lua_name("K12",
1427 k12_file_type_subtype);
1431 * Editor modelines - https://www.wireshark.org/tools/modelines.html
1433 * Local variables:
1434 * c-basic-offset: 4
1435 * tab-width: 8
1436 * indent-tabs-mode: nil
1437 * End:
1439 * vi: set shiftwidth=4 tabstop=8 expandtab:
1440 * :indentSize=4:tabSize=8:noTabs=true: