K15 phase 1
[obnox/wireshark/wip.git] / wiretap / k12.c
1 /*
2  * k12.c
3  *
4  *  routines for importing tektronix k12xx *.rf5 files
5  *
6  *  Copyright (c) 2005, Luis E. Garia Ontanon <luis.ontanon@gmail.com>
7  *
8  * $Id$
9  *
10  * Wiretap Library
11  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
12  *
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.
17  *
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.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 /* #define DEBUG_K12 */
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #include "wtap-int.h"
36 #include "wtap.h"
37 #include "file_wrappers.h"
38 #include "buffer.h"
39 #include "k12.h"
40
41 /*#define DEBUG_K12*/
42 #ifdef DEBUG_K12
43 #include <stdio.h>
44 #include <ctype.h>
45 #include <stdarg.h>
46
47
48 FILE* dbg_out = NULL;
49 char* env_file = NULL;
50
51 static unsigned debug_level = 0;
52
53 void k12_fprintf(char* fmt, ...) {
54     va_list ap;
55     
56     va_start(ap,fmt);
57     vfprintf(dbg_out, fmt, ap);
58     va_end(ap);    
59 }
60
61 #define CAT(a,b) a##b
62 #define K12_DBG(level,args) do { if (level <= debug_level) { \
63         fprintf(dbg_out,"%s:%d: ",CAT(__FI,LE__),CAT(__LI,NE__)); \
64         k12_fprintf args ; \
65         fprintf(dbg_out,"\n"); \
66 } } while(0)
67
68 void k12_hexdump(guint level, gint64 offset, char* label, unsigned char* b, unsigned len) {
69     static const char* c2t[] = {
70         "00","01","02","03","04","05","06","07","08","09","0a","0b","0c","0d","0e","0f", 
71         "10","11","12","13","14","15","16","17","18","19","1a","1b","1c","1d","1e","1f", 
72         "20","21","22","23","24","25","26","27","28","29","2a","2b","2c","2d","2e","2f", 
73         "30","31","32","33","34","35","36","37","38","39","3a","3b","3c","3d","3e","3f", 
74         "40","41","42","43","44","45","46","47","48","49","4a","4b","4c","4d","4e","4f", 
75         "50","51","52","53","54","55","56","57","58","59","5a","5b","5c","5d","5e","5f", 
76         "60","61","62","63","64","65","66","67","68","69","6a","6b","6c","6d","6e","6f", 
77         "70","71","72","73","74","75","76","77","78","79","7a","7b","7c","7d","7e","7f", 
78         "80","81","82","83","84","85","86","87","88","89","8a","8b","8c","8d","8e","8f", 
79         "90","91","92","93","94","95","96","97","98","99","9a","9b","9c","9d","9e","9f", 
80         "a0","a1","a2","a3","a4","a5","a6","a7","a8","a9","aa","ab","ac","ad","ae","af", 
81         "b0","b1","b2","b3","b4","b5","b6","b7","b8","b9","ba","bb","bc","bd","be","bf", 
82         "c0","c1","c2","c3","c4","c5","c6","c7","c8","c9","ca","cb","cc","cd","ce","cf", 
83         "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","da","db","dc","dd","de","df", 
84         "e0","e1","e2","e3","e4","e5","e6","e7","e8","e9","ea","eb","ec","ed","ee","ef", 
85         "f0","f1","f2","f3","f4","f5","f6","f7","f8","f9","fa","fb","fc","fd","fe","ff"
86     };
87     unsigned i;
88         
89     if (debug_level < level) return;
90     
91     fprintf(dbg_out,"%s(%.8llx,%.4x): ",label,offset,len);
92     
93     for (i=0 ; i<len ; i++) {
94                 
95         if (!(i%32))
96             fprintf(dbg_out,"\n");
97         else if (!(i%4))
98             fprintf(dbg_out," ");
99                 
100         fprintf(dbg_out,c2t[b[i]]);
101     }
102         
103         fprintf(dbg_out,"\n");
104 }
105
106 #define K12_HEXDMP(x,a,b,c,d) k12_hexdump(x,a,b,c,d)
107
108 #else
109 #define K12_DBG(level,args) (void)0
110 #define K12_HEXDMP(x,a,b,c,d)
111 #endif
112
113
114
115 /*
116  * the 32 bits .rf5 file contains:
117  *  an 8 byte magic number
118  *  32bit lenght
119  *  32bit number of records
120  *  other 0x200 bytes bytes of uncharted territory
121  *     1 or more copies of the num_of_records in there
122  *  the records whose first 32bits word is the length
123  *     they are stuffed by one to four words every 0x2000 bytes
124  *  and a 2 byte terminator FFFF
125  */
126
127 typedef struct _k12_mode_t {
128         const gchar* name;
129         struct {
130                 guint len;
131                 const guint8* bytes;
132         } magic;
133 } k12_mode_t;
134
135 static const k12_mode_t k12_modes[] = {
136         { "k1297", { 8, (guint8*)"\x00\x00\x02\x00\x12\x05\x00\x10" } },
137         { "k15",   { 8, (guint8*)"\x00\x00\x02\x00\x12\x05\x00\x10" } },
138         { NULL, { 0, NULL } }
139 };
140
141 static const guint8 k12_file_magic[] = { 0x00, 0x00, 0x02, 0x00 ,0x12, 0x05, 0x00, 0x10 };
142
143 struct _k12_t {
144     guint32 file_len;
145     guint32 num_of_records; /* XXX: not sure about this */
146     
147     GHashTable* src_by_id; /* k12_srcdsc_recs by input */
148     GHashTable* src_by_name; /* k12_srcdsc_recs by stack_name */
149
150     Buffer extra_info; /* Buffer to hold per packet extra information */
151         
152         /*k12_mode_t mode;*/
153 };
154
155 typedef struct _k12_src_desc_t {
156     guint32 input;
157     guint32 input_type;
158     gchar* input_name;
159     gchar* stack_file;
160     k12_input_info_t input_info;
161 } k12_src_desc_t;
162
163
164 /* so far we've seen only 7 types of records */
165 #define K12_REC_PACKET        0x00010020 /* an actual packet */
166 #define K12_REC_SRCDSC        0x00070041 /* port-stack mapping + more, the key of the whole thing */
167 #define K12_REC_SCENARIO      0x00070040 /* what appears as the window's title */
168 #define K12_REC_UNK01         0x00070042 /* UNKNOWN content */
169 #define K12_REC_TEXT          0x00070044 /* a string containing something with a grammar (conditions/responses?) */
170 #define K12_REC_START         0x00020030 /* a string containing human readable start time  */ 
171 #define K12_REC_STOP          0x00020031 /* a string containing human readable stop time */
172
173 #define K12_MASK_PACKET       0xfffffff0  /* the last nibble in packet records somentimes change (not yet understood why) */
174
175 /* offsets of elements in the records */
176 #define K12_RECORD_LEN         0x0 /* uint32, in bytes */ 
177 #define K12_RECORD_TYPE        0x4 /* uint32, see above */
178 #define K12_RECORD_FRAME_LEN   0x8 /* uint32, in bytes */
179 #define K12_RECORD_SRC_ID      0xc /* uint32 */
180
181 /* elements of packet records */
182 #define K12_PACKET_TIMESTAMP  0x18 /* int64 (8b) representing 1/2us since 01-01-1990 Z00:00:00 */
183
184 #define K12_PACKET_FRAME      0x20 /* start of the actual frame in the record */
185
186 #define K12_PACKET_OFFSET_VP  0x08 /* 2 bytes, big endian */ 
187 #define K12_PACKET_OFFSET_VC  0x0a /* 2 bytes, big endian */ 
188 #define K12_PACKET_OFFSET_CID 0x0c /* 1 byte */
189
190 /* elements of the source description records */
191 #define K12_SRCDESC_COLOR_FOREGROUND 0x12 /* 1 byte */
192 #define K12_SRCDESC_COLOR_BACKGROUND 0x13 /* 1 byte */
193
194 #define K12_SRCDESC_PORT_TYPE  0x1a   /* 1 byte */
195 #define K12_SRCDESC_EXTRALEN   0x1e   /* uint16, big endian */
196 #define K12_SRCDESC_NAMELEN    0x20   /* uint16, big endian */
197 #define K12_SRCDESC_STACKLEN   0x22   /* uint16, big endian */
198
199 #define K12_SRCDESC_EXTRATYPE  0x24   /* uint32, big endian */
200 #define K12_SRCDESC_ATM_VPI    0x38   /* uint16, big endian */
201 #define K12_SRCDESC_ATM_VCI    0x3a   /* uint16, big endian */
202
203 #define K12_SRCDESC_ATM_AAL    0x3c    /* 1 byte */
204 #define K12_SRCDESC_DS0_MASK   0x3c    /* 1 byte */
205
206
207 /*
208  * get_record: Get the next record into a buffer
209  *   Every about 0x2000 bytes 0x10 bytes are inserted in the file,
210  *   even in the middle of a record.
211  *   This reads the next record without the eventual 0x10 bytes.
212  *   returns the lenght of the record + the stuffing (if any)
213  *
214  * XXX: works at most with 0x1FFF bytes per record 
215  */
216 static gint get_record(guint8** bufferp, FILE* fh, gint64 file_offset) {
217         static guint8* buffer = NULL;
218         static guint buffer_len = 0x2000 ;
219     guint read;
220         guint last_read;
221     guint actual_len, left;
222         guint8 junk[0x14];
223         guint8* readp;
224         
225         /* where the next unknown 0x10 bytes are stuffed to the file */
226         gint64 junky_offset = 0x2000 - ( (file_offset - 0x200) % 0x2000 );
227         
228         K12_DBG(6,("get_record: ENTER: junky_offset=%lld, file_offset=%lld",junky_offset,file_offset));
229
230         /* no buffer is given, lets create it */
231         if (buffer == NULL) {
232                 buffer = g_malloc(0x2000);
233                 buffer_len = 0x2000;
234         }
235         
236         *bufferp = buffer;
237         
238         if  ( junky_offset == 0x2000 ) {
239                 /* the lenght of the record is 0x10 bytes ahead from we are reading */
240                 read = file_read(junk,1,0x14,fh);
241         
242         if (read == 2 && junk[0] == 0xff && junk[1] == 0xff) {
243             K12_DBG(1,("get_record: EOF"));            
244             return 0;
245         } else if ( read < 0x14 ){
246             K12_DBG(1,("get_record: SHORT READ"));
247             return -1;
248         }
249                 
250                 memcpy(buffer,&(junk[0x10]),4);
251         } else {
252                 /* the lenght of the record is right where we are reading */
253                 read = file_read(buffer,1, 0x4, fh);
254                 
255                 if (read == 2 && buffer[0] == 0xff && buffer[1] == 0xff) {
256             K12_DBG(1,("get_record: EOF"));
257             return 0;
258         } else if ( read != 0x4 ) {
259             K12_DBG(1,("get_record: SHORT READ"));
260             return -1;
261         }
262         }
263         
264         actual_len = left = pntohl(buffer);
265         junky_offset -= 0x4;
266         readp = buffer + 4;
267         
268         K12_DBG(5,("get_record: GET length=%d",left));
269
270         g_assert(left >= 4);
271         
272         while (left > buffer_len) *bufferp = buffer = g_realloc(buffer,buffer_len*=2);
273         
274         left -= 4;
275         
276         do {
277                 K12_DBG(6,("get_record: looping left=%d junky_offset=%lld",left,junky_offset));
278
279                 if (junky_offset > left) {
280                         read += last_read = file_read(readp,1, left, fh);
281                         
282                         if ( last_read != left ) {
283                                 K12_DBG(1,("get_record: SHORT READ"));
284                                 return -1;
285                         } else {
286                                 K12_HEXDMP(5,file_offset, "GOT record", buffer, actual_len);
287                                 return read;
288                         }
289                 } else {
290                         read += last_read = file_read(readp,1, junky_offset, fh);
291                         
292                         if ( last_read != junky_offset ) {
293                                 K12_DBG(1,("get_record: SHORT READ, read=%d expected=%d",last_read, junky_offset));
294                                 return -1;
295                         }
296                         
297                         readp += last_read;
298
299                         read += last_read = file_read(junk,1, 0x10, fh);
300                         
301                         if ( last_read != 0x10 ) {
302                                 K12_DBG(1,("get_record: SHORT READ"));
303                                 return -1;
304                         }
305                         
306                         left -= junky_offset;
307                         junky_offset = 0x2000;
308                 }
309                 
310         } while(left);
311         
312         K12_HEXDMP(5,file_offset, "GOT record", buffer, actual_len);
313         return read;
314 }
315
316 static gboolean k12_read(wtap *wth, int *err, gchar **err_info _U_, gint64 *data_offset) {
317     k12_src_desc_t* src_desc;
318     guint8* buffer = NULL;
319     gint64 offset;
320     long len;
321     guint32 type;
322         guint32 src_id;
323     guint64 ts;
324     guint32 extra_len;
325     
326     offset = wth->data_offset;
327     
328     /* ignore the record if it isn't a packet */    
329     do {
330                 K12_DBG(5,("k12_read: offset=%i",offset));
331                 
332         *data_offset = offset;
333         
334         len = get_record(&buffer, wth->fh, offset);
335         
336         if (len < 0) {
337             *err = WTAP_ERR_SHORT_READ;
338             return FALSE;
339         } else if (len == 0) {
340             *err = 0;
341             return FALSE;
342         }
343         
344         type = pntohl(buffer + K12_RECORD_TYPE);
345         src_id = pntohl(buffer + K12_RECORD_SRC_ID);
346                 src_desc = g_hash_table_lookup(wth->capture.k12->src_by_id,GUINT_TO_POINTER(src_id));
347
348         K12_DBG(5,("k12_read: record type=%x src_id=%x",type,src_id));
349         
350         offset += len;
351         
352     } while ( ((type & K12_MASK_PACKET) != K12_REC_PACKET) || !src_id || !src_desc );
353     
354     wth->data_offset = offset;
355     
356     ts = pntohll(buffer + K12_PACKET_TIMESTAMP);
357     
358     wth->phdr.ts.secs = (guint32) ((ts / 2000000) + 631152000);
359     wth->phdr.ts.nsecs = (guint32) ( (ts % 2000000) * 500 );
360     
361     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));
362     
363     wth->phdr.len = wth->phdr.caplen = pntohl(buffer + K12_RECORD_FRAME_LEN) & 0x00001FFF;
364     extra_len = len - K12_PACKET_FRAME - wth->phdr.caplen;
365     
366     /* the frame */
367     buffer_assure_space(wth->frame_buffer, wth->phdr.caplen);
368     memcpy(buffer_start_ptr(wth->frame_buffer), buffer + K12_PACKET_FRAME, wth->phdr.caplen);
369     
370     /* extra information need by some protocols */
371     buffer_assure_space(&(wth->capture.k12->extra_info), extra_len);
372     memcpy(buffer_start_ptr(&(wth->capture.k12->extra_info)),
373            buffer + K12_PACKET_FRAME + wth->phdr.caplen, extra_len);
374     wth->pseudo_header.k12.extra_info = (void*)buffer_start_ptr(&(wth->capture.k12->extra_info));
375     wth->pseudo_header.k12.extra_length = extra_len;
376
377     wth->pseudo_header.k12.input = src_id;
378     
379     K12_DBG(5,("k12_read: wth->pseudo_header.k12.input=%x wth->phdr.len=%i",wth->pseudo_header.k12.input,wth->phdr.len));
380         
381     if (src_desc) {
382         
383         K12_DBG(5,("k12_read: input_name='%s' stack_file='%s' type=%x",src_desc->input_name,src_desc->stack_file,src_desc->input_type));
384         wth->pseudo_header.k12.input_name = src_desc->input_name;
385         wth->pseudo_header.k12.stack_file = src_desc->stack_file;
386         wth->pseudo_header.k12.input_type = src_desc->input_type;
387         
388         switch(src_desc->input_type) {
389             case K12_PORT_ATMPVC:
390             if ((long)(K12_PACKET_FRAME + wth->phdr.len + K12_PACKET_OFFSET_CID) < len) {
391                 wth->pseudo_header.k12.input_info.atm.vp =  pntohs(buffer + (K12_PACKET_FRAME + wth->phdr.caplen + K12_PACKET_OFFSET_VP));
392                 wth->pseudo_header.k12.input_info.atm.vc =  pntohs(buffer + (K12_PACKET_FRAME + wth->phdr.caplen + K12_PACKET_OFFSET_VC));
393                 wth->pseudo_header.k12.input_info.atm.cid =  *((unsigned char*)(buffer + K12_PACKET_FRAME + wth->phdr.len + K12_PACKET_OFFSET_CID));
394                 break;
395             }
396             /* Fall through */
397             default:
398             memcpy(&(wth->pseudo_header.k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
399             break;
400             
401         }
402     } else {
403         K12_DBG(5,("k12_read: NO RECORD FOUND"));
404
405         memset(&(wth->pseudo_header),0,sizeof(wth->pseudo_header));
406         wth->pseudo_header.k12.input_name = "unknown port";
407         wth->pseudo_header.k12.stack_file = "unknown stack file";
408     }
409     
410     wth->pseudo_header.k12.stuff = wth->capture.k12;
411     
412     return TRUE;
413 }
414
415
416 static gboolean k12_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header *pseudo_header, guchar *pd, int length, int *err _U_, gchar **err_info _U_) {
417     k12_src_desc_t* src_desc;
418     guint8* buffer;
419     long len;
420     guint32 extra_len;
421     guint32 input;
422     
423     K12_DBG(5,("k12_seek_read: ENTER"));
424     
425     if ( file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) {
426         K12_DBG(5,("k12_seek_read: SEEK ERROR"));
427         return FALSE;
428     }
429     
430     if ((len = get_record(&buffer, wth->random_fh, seek_off)) < 1) {
431         K12_DBG(5,("k12_seek_read: READ ERROR"));
432         return FALSE;
433     }
434     
435     memcpy(pd, buffer + K12_PACKET_FRAME, length);
436
437     extra_len = len - K12_PACKET_FRAME - length;
438     buffer_assure_space(&(wth->capture.k12->extra_info), extra_len);
439     memcpy(buffer_start_ptr(&(wth->capture.k12->extra_info)),
440            buffer + K12_PACKET_FRAME + length, extra_len);
441     wth->pseudo_header.k12.extra_info = (void*)buffer_start_ptr(&(wth->capture.k12->extra_info));
442     wth->pseudo_header.k12.extra_length = extra_len;
443     if (pseudo_header) {
444         pseudo_header->k12.extra_info = (void*)buffer_start_ptr(&(wth->capture.k12->extra_info));
445         pseudo_header->k12.extra_length = extra_len;
446     }
447     
448     input = pntohl(buffer + K12_RECORD_SRC_ID);
449     K12_DBG(5,("k12_seek_read: input=%.8x",input));
450     
451     src_desc = g_hash_table_lookup(wth->capture.k12->src_by_id,GUINT_TO_POINTER(input));
452     
453     if (src_desc) {
454         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));
455         if (pseudo_header) {
456             pseudo_header->k12.input_name = src_desc->input_name;
457             pseudo_header->k12.stack_file = src_desc->stack_file;
458             pseudo_header->k12.input_type = src_desc->input_type;
459             
460             switch(src_desc->input_type) {
461             case K12_PORT_ATMPVC:
462                 if ((long)(K12_PACKET_FRAME + length + K12_PACKET_OFFSET_CID) < len) {
463                 pseudo_header->k12.input_info.atm.vp =  pntohs(buffer + K12_PACKET_FRAME + length + K12_PACKET_OFFSET_VP);
464                 pseudo_header->k12.input_info.atm.vc =  pntohs(buffer + K12_PACKET_FRAME + length + K12_PACKET_OFFSET_VC);
465                 pseudo_header->k12.input_info.atm.cid =  *((unsigned char*)(buffer + K12_PACKET_FRAME + length + K12_PACKET_OFFSET_CID));
466                 break;
467                 }
468                 /* Fall through */
469             default:
470                 memcpy(&(pseudo_header->k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
471                 break;
472             }
473         }
474         
475         wth->pseudo_header.k12.input_name = src_desc->input_name;
476         wth->pseudo_header.k12.stack_file = src_desc->stack_file;
477         wth->pseudo_header.k12.input_type = src_desc->input_type;
478             
479         switch(src_desc->input_type) {
480             case K12_PORT_ATMPVC:
481             if ((long)(K12_PACKET_FRAME + length + K12_PACKET_OFFSET_CID) < len) {
482                 wth->pseudo_header.k12.input_info.atm.vp =  pntohs(buffer + K12_PACKET_FRAME + length + K12_PACKET_OFFSET_VP);
483                 wth->pseudo_header.k12.input_info.atm.vc =  pntohs(buffer + K12_PACKET_FRAME + length + K12_PACKET_OFFSET_VC);
484                 wth->pseudo_header.k12.input_info.atm.cid =  *((unsigned char*)(buffer + K12_PACKET_FRAME + length + K12_PACKET_OFFSET_CID));
485             }
486             break;
487             /* Fall through */
488             default:
489             memcpy(&(wth->pseudo_header.k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
490             break;
491         }
492         
493     } else {
494         K12_DBG(5,("k12_seek_read: NO SRC_RECORD FOUND"));
495
496         if (pseudo_header) {
497             memset(&(pseudo_header->k12),0,sizeof(pseudo_header->k12));
498             pseudo_header->k12.input_name = "unknown port";
499             pseudo_header->k12.stack_file = "unknown stack file";
500         }
501
502         memset(&(wth->pseudo_header.k12),0,sizeof(wth->pseudo_header.k12));
503         wth->pseudo_header.k12.input_name = "unknown port";
504         wth->pseudo_header.k12.stack_file = "unknown stack file";
505         
506     }
507     
508     if (pseudo_header) {
509         pseudo_header->k12.input = input;
510         pseudo_header->k12.stuff = wth->capture.k12;
511     }
512     
513     wth->pseudo_header.k12.input = input;
514     wth->pseudo_header.k12.stuff = wth->capture.k12;
515
516     K12_DBG(5,("k12_seek_read: DONE OK"));
517
518     return TRUE;
519 }
520
521
522 static k12_t* new_k12_file_data() {
523     k12_t* fd = g_malloc(sizeof(k12_t));
524     
525     fd->file_len = 0;
526     fd->num_of_records = 0;
527     fd->src_by_name = g_hash_table_new(g_str_hash,g_str_equal);
528     fd->src_by_id = g_hash_table_new(g_direct_hash,g_direct_equal);
529
530     buffer_init(&(fd->extra_info), 100);
531     
532     return fd;
533 }
534
535 static gboolean destroy_srcdsc(gpointer k _U_, gpointer v, gpointer p _U_) {
536     k12_src_desc_t* rec = v;
537     
538     if(rec->input_name)
539         g_free(rec->input_name);
540     
541     if(rec->stack_file)
542         g_free(rec->stack_file);
543     
544     g_free(rec);
545     
546     return TRUE;
547 }
548
549 static void destroy_k12_file_data(k12_t* fd) {
550     g_hash_table_destroy(fd->src_by_id);
551     g_hash_table_foreach_remove(fd->src_by_name,destroy_srcdsc,NULL);    
552     g_hash_table_destroy(fd->src_by_name);
553     buffer_free(&(fd->extra_info));
554     g_free(fd);
555 }
556
557 static void k12_close(wtap *wth) {
558     destroy_k12_file_data(wth->capture.k12);
559 #ifdef DEBUG_K12
560     K12_DBG(5,("k12_close: CLOSED"));
561     if (env_file) fclose(dbg_out);
562 #endif
563 }
564
565
566 int k12_open(wtap *wth, int *err, gchar **err_info _U_) {
567     k12_src_desc_t* rec;
568     guint8 header_buffer[0x200];
569     guint8* read_buffer;
570     guint32 type;
571     long offset;
572     long len;
573     guint32 rec_len;
574     guint32 extra_len;
575     guint32 name_len;
576     guint32 stack_len;
577     guint i;
578     k12_t* file_data;
579     
580 #ifdef DEBUG_K12
581     gchar* env_level = getenv("K12_DEBUG_LEVEL");
582     env_file = getenv("K12_DEBUG_FILENAME");
583     if ( env_file ) dbg_out = fopen(env_file,"w");
584     else dbg_out = stderr;
585     if ( env_level ) debug_level = strtoul(env_level,NULL,10);
586     K12_DBG(1,("k12_open: ENTER debug_level=%u",debug_level));
587 #endif
588     
589     if ( file_read(header_buffer,1,0x200,wth->fh) != 0x200 ) {
590         K12_DBG(1,("k12_open: FILE HEADER TOO SHORT"));
591         return 0;
592     } else {
593         if ( memcmp(header_buffer,k12_file_magic,8) != 0 ) {
594             K12_DBG(1,("k12_open: BAD MAGIC"));
595             return 0;
596         }
597     }
598     
599     offset = 0x200;
600     
601     file_data = new_k12_file_data();
602     
603     file_data->file_len = pntohl( header_buffer + 0x8);
604     file_data->num_of_records = pntohl( header_buffer + 0xC );
605     
606     K12_DBG(5,("k12_open: FILE_HEADER OK: offset=%x file_len=%i records=%i",
607             offset,
608             file_data->file_len,
609             file_data->num_of_records ));
610     
611     do {
612         
613         len = get_record(&read_buffer, wth->fh, offset);
614         
615         if ( len <= 0 ) {
616             K12_DBG(1,("k12_open: BAD HEADER RECORD",len));
617             return -1;
618         }
619         
620         
621         type = pntohl( read_buffer + K12_RECORD_TYPE );
622         
623         if ( (type & K12_MASK_PACKET) == K12_REC_PACKET) {
624             /*
625              * we are at the first packet record, rewind and leave.
626              */
627             if (file_seek(wth->fh, offset, SEEK_SET, err) == -1) {
628                 destroy_k12_file_data(file_data);
629                 return -1;
630             }
631             K12_DBG(5,("k12_open: FIRST PACKET offset=%x",offset));
632             break;
633         } else if (type == K12_REC_SRCDSC) {
634             rec = g_malloc0(sizeof(k12_src_desc_t));
635             
636             rec_len = pntohl( read_buffer + K12_RECORD_LEN );
637             extra_len = pntohs( read_buffer + K12_SRCDESC_EXTRALEN );
638             name_len = pntohs( read_buffer + K12_SRCDESC_NAMELEN );
639             stack_len = pntohs( read_buffer + K12_SRCDESC_STACKLEN );
640             
641             rec->input = pntohl( read_buffer + K12_RECORD_SRC_ID );
642             
643             K12_DBG(5,("k12_open: INTERFACE RECORD offset=%x interface=%x",offset,rec->input));
644             
645             if (name_len == 0 || stack_len == 0
646                 || 0x20 + extra_len + name_len + stack_len > rec_len ) {
647                 g_free(rec);
648                 K12_DBG(5,("k12_open: failed (name_len == 0 || stack_len == 0 "
649                         "|| 0x20 + extra_len + name_len + stack_len > rec_len)  extra_len=%i name_len=%i stack_len=%i"));
650                 return 0;
651             }
652
653             if (extra_len)
654                 switch(( rec->input_type = pntohl( read_buffer + K12_SRCDESC_EXTRATYPE ) )) {
655                     case K12_PORT_DS0S:
656                         rec->input_info.ds0mask = 0x00000000;
657                         
658                         for (i = 0; i < 32; i++) {
659                             rec->input_info.ds0mask |= ( *(read_buffer + K12_SRCDESC_DS0_MASK + i) == 0xff ) ? 0x1<<(31-i) : 0x0; 
660                         }
661                         
662                             break;
663                     case K12_PORT_ATMPVC:
664                         rec->input_info.atm.vp = pntohs( read_buffer + K12_SRCDESC_ATM_VPI );
665                         rec->input_info.atm.vc = pntohs( read_buffer + K12_SRCDESC_ATM_VCI );
666                         break;
667                     default:
668                         break;
669                 }
670             else {    /* Record viewer generated files
671                    don't have this information */
672                 if (read_buffer[K12_SRCDESC_PORT_TYPE] >= 0x14
673                     && read_buffer[K12_SRCDESC_PORT_TYPE] <= 0x17)
674                     /* For ATM2_E1DS1, ATM2_E3DS3, 
675                        ATM2_STM1EL and ATM2_STM1OP */
676                     rec->input_type = K12_PORT_ATMPVC;
677             }
678
679             rec->input_name = g_memdup(read_buffer + K12_SRCDESC_EXTRATYPE + extra_len, name_len);
680             rec->stack_file = g_memdup(read_buffer + K12_SRCDESC_EXTRATYPE + extra_len + name_len, stack_len);
681             
682             g_strdown(rec->stack_file);
683             
684             g_hash_table_insert(file_data->src_by_id,GUINT_TO_POINTER(rec->input),rec);
685             g_hash_table_insert(file_data->src_by_name,rec->stack_file,rec);
686             
687             offset += len;
688             continue;
689         } else {
690             offset += len;
691             continue;
692         }
693     } while(1);
694     
695     wth->data_offset = offset;
696     wth->file_type = WTAP_FILE_K12;
697     wth->file_encap = WTAP_ENCAP_K12;
698     wth->snapshot_length = 0;
699     wth->subtype_read = k12_read;
700     wth->subtype_seek_read = k12_seek_read;
701     wth->subtype_close = k12_close;
702     wth->capture.k12 = file_data;
703     wth->tsprecision = WTAP_FILE_TSPREC_NSEC;    
704
705     return 1;
706 }
707
708 int k12_dump_can_write_encap(int encap) {
709     
710     if (encap == WTAP_ENCAP_PER_PACKET)
711         return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
712     
713     if (encap != WTAP_ENCAP_K12)
714         return WTAP_ERR_UNSUPPORTED_ENCAP;
715     
716     return 0;
717 }
718
719 static const gchar dumpy_junk[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
720
721 static void k12_dump_record(wtap_dumper *wdh, long len,  guint8* buffer) {
722     long junky_offset = (0x2000 - ( (wdh->dump.k12->file_offset - 0x200) % 0x2000 )) % 0x2000;
723     
724     if (len > junky_offset) {
725         
726         if (junky_offset)
727             fwrite(buffer, 1, junky_offset, wdh->fh);
728         
729         fwrite(dumpy_junk, 1, 0x10, wdh->fh);
730         
731         fwrite(buffer+junky_offset, 1, len - junky_offset, wdh->fh);
732         
733         wdh->dump.k12->file_offset += len + 0x10;
734     } else {
735         fwrite(buffer, 1, len, wdh->fh);
736         wdh->dump.k12->file_offset += len;
737     }
738     
739     wdh->dump.k12->num_of_records++;
740 }
741
742 static void k12_dump_src_setting(gpointer k _U_, gpointer v, gpointer p) {
743     k12_src_desc_t* src_desc = v;
744     wtap_dumper *wdh = p;
745     guint32 len;
746     guint offset;
747     guint i;
748     
749     union {
750         guint8 buffer[0x2000];
751         
752         struct {
753             guint32 len;
754             guint32 type;
755             guint32 unk32_1;
756             guint32 input;
757             
758             guint16 unk32_2;
759             guint16 color;
760             guint32 unk32_3;
761             guint32 unk32_4;
762             guint16 unk16_1;
763             guint16 extra_len;
764             
765             guint16 name_len;
766             guint16 stack_len;
767             
768             struct {
769                 guint32 type;
770                 
771                 union {
772                     struct {
773                         guint32 unk32;
774                         guint8 mask[32];
775                     } ds0mask;
776                     
777                     struct {
778                         guint8 unk_data[0x10];
779                         guint16 vp;
780                         guint16 vc;
781                     } atm;
782                     
783                     guint32 unk;
784                 } desc;
785             } extra;
786         } record;
787     } obj;
788     
789     obj.record.type = g_htonl(K12_REC_SRCDSC);
790     obj.record.unk32_1 = g_htonl(0x00000001);
791     obj.record.input = g_htonl(src_desc->input);
792     
793     obj.record.unk32_2 = g_htons(0x0000);
794     obj.record.color = g_htons(0x060f);
795     obj.record.unk32_3 = g_htonl(0x00000003);
796     switch (src_desc->input_type) {
797         case K12_PORT_ATMPVC:
798             obj.record.unk32_4 = g_htonl(0x01001400);
799             break;
800         default:
801             obj.record.unk32_4 = g_htonl(0x01000100);
802     }
803     
804     obj.record.unk16_1 = g_htons(0x0000);
805     obj.record.name_len = strlen(src_desc->input_name) + 1;
806     obj.record.stack_len = strlen(src_desc->stack_file) + 1;
807     
808     obj.record.extra.type = g_htonl(src_desc->input_type);
809     
810     switch (src_desc->input_type) {
811         case K12_PORT_ATMPVC:
812             obj.record.extra_len = g_htons(0x18);
813             obj.record.extra.desc.atm.vp = g_htons(src_desc->input_info.atm.vp);
814             obj.record.extra.desc.atm.vc = g_htons(src_desc->input_info.atm.vc);
815             offset = 0x3c;
816             break;
817         case K12_PORT_DS0S:
818             obj.record.extra_len = g_htons(0x18);
819             for( i=0; i<32; i++ ) {
820                 obj.record.extra.desc.ds0mask.mask[i] =
821                 (src_desc->input_info.ds0mask & (1 << i)) ? 0xff : 0x00;
822             }
823                 offset = 0x3c;
824             break;
825         default:
826             obj.record.extra_len = g_htons(0x08);
827             offset = 0x2c;
828             break;
829     }
830     
831     memcpy(obj.buffer + offset,
832            src_desc->input_name,
833            obj.record.name_len);
834     
835     memcpy(obj.buffer + offset + obj.record.name_len,
836            src_desc->stack_file,
837            obj.record.stack_len);
838     
839     len = offset + obj.record.name_len + obj.record.stack_len;
840     len += (len % 4) ? 4 - (len % 4) : 0;
841     
842     obj.record.len = g_htonl(len);
843     obj.record.name_len =  g_htons(obj.record.name_len);
844     obj.record.stack_len = g_htons(obj.record.stack_len);
845     
846     k12_dump_record(wdh,len,obj.buffer);
847 }
848
849 static gboolean k12_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
850                          const union wtap_pseudo_header *pseudo_header,
851                          const guchar *pd, int *err _U_) {
852     long len;
853     union {
854         guint8 buffer[0x2000];
855         struct {
856             guint32 len;
857             guint32 type;
858             guint32 frame_len;
859             guint32 input;
860             
861             guint32 datum_1;
862             guint32 datum_2;
863             guint64 ts;
864             
865             guint8 frame[0x1fc0];
866         } record;
867     } obj;
868     
869     if (wdh->dump.k12->num_of_records == 0) {
870         k12_t* file_data = pseudo_header->k12.stuff;
871         g_hash_table_foreach(file_data->src_by_id,k12_dump_src_setting,wdh);
872     }
873     
874     obj.record.len = 0x20 + phdr->len;
875     obj.record.len += (obj.record.len % 4) ? 4 - obj.record.len % 4 : 0;
876     
877     len = obj.record.len;
878     
879     obj.record.len = g_htonl(obj.record.len);
880     
881     obj.record.type = g_htonl(K12_REC_PACKET);
882     obj.record.frame_len = g_htonl(phdr->len);
883     obj.record.input = g_htonl(pseudo_header->k12.input);
884     
885     obj.record.ts = GUINT64_TO_BE((((guint64)phdr->ts.secs - 631152000) * 2000000) + (phdr->ts.nsecs / 1000 * 2));
886     
887     memcpy(obj.record.frame,pd,phdr->len);
888     
889     k12_dump_record(wdh,len,obj.buffer);
890     
891     /* XXX if OK */
892     return TRUE;
893 }
894
895 static const guint8 k12_eof[] = {0xff,0xff};
896
897 static gboolean k12_dump_close(wtap_dumper *wdh, int *err) {
898     union {
899         guint8 b[sizeof(guint32)];
900         guint32 u;
901     } d;
902     
903     fwrite(k12_eof, 1, 2, wdh->fh);
904     
905     if (fseek(wdh->fh, 8, SEEK_SET) == -1) {
906         *err = errno;
907         return FALSE;
908     }
909     
910     d.u = g_htonl(wdh->dump.k12->file_len);
911     
912     fwrite(d.b, 1, 4, wdh->fh);
913     
914     d.u = g_htonl(wdh->dump.k12->num_of_records);
915     
916     fwrite(d.b, 1, 4, wdh->fh);
917     
918     return TRUE;
919 }
920
921
922 gboolean k12_dump_open(wtap_dumper *wdh, gboolean cant_seek, int *err) {
923     
924     if (cant_seek) {
925         *err = WTAP_ERR_CANT_WRITE_TO_PIPE;
926         return FALSE;
927     }
928     
929     if ( fwrite(k12_file_magic, 1, 8, wdh->fh) != 8 ) {
930         *err = errno;
931         return FALSE;
932     }
933     
934     if (fseek(wdh->fh, 0x200, SEEK_SET) == -1) {
935         *err = errno;
936         return FALSE;
937     }
938     
939     wdh->subtype_write = k12_dump;
940     wdh->subtype_close = k12_dump_close;
941     
942     wdh->dump.k12 = g_malloc(sizeof(k12_dump_t));
943     wdh->dump.k12->file_len = 0x200;
944     wdh->dump.k12->num_of_records = 0;
945     wdh->dump.k12->file_offset  = 0x200;
946     
947     return TRUE;
948 }
949
950