make sure tham when more than one stack file is used each channel uses the right...
[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 NULL */
29
30 #ifdef DEBUG_K12
31 #include <stdio.h>
32 #include <stdarg.h>
33
34 FILE* dbg_out;
35
36 void k12_dbg(char* fmt, ...) {
37     va_list ap;
38     
39     va_start(ap,fmt);
40     vfprintf(dbg_out, fmt, ap);
41     va_end(ap);
42 }
43
44 #endif
45
46 #ifdef HAVE_CONFIG_H
47 #include "config.h"
48 #endif
49 #include <stdlib.h>
50 #include <string.h>
51 #include <errno.h>
52 #include "wtap-int.h"
53 #include "wtap.h"
54 #include "file_wrappers.h"
55 #include "buffer.h"
56 #include "k12.h"
57
58 /*
59  * the 32 bits .rf5 file contains:
60  *  an 8 byte magic number
61  *  32bit lenght
62  *  32bit number of records
63  *  other 0x200 bytes bytes of uncharted territory
64  *     1 or more copies of the num_of_records in there
65  *  the records whose first 32bits word is the length
66  *     they are stuffed by one to four words every 0x2000 bytes
67  *  and a 2 byte terminator FFFF
68  */
69
70 static const guint8 k12_file_magic[] = { 0x00, 0x00, 0x02, 0x00 ,0x12, 0x05, 0x00, 0x10 };
71
72 struct _k12_t {
73         guint32 file_len;
74         guint32 num_of_records; /* XXX: not sure about this */
75         
76         GHashTable* src_by_id; /* k12_srcdsc_recs by input */
77         GHashTable* src_by_name; /* k12_srcdsc_recs by stack_name */
78 };
79
80 typedef struct _k12_src_desc_t {
81         guint32 input;
82         guint32 input_type;
83         gchar* input_name;
84         gchar* stack_file;
85         k12_input_info_t input_info;
86 } k12_src_desc_t;
87
88
89 /* so far we've seen only 7 types of records */
90 #define K12_REC_PACKET          0x00010020
91 #define K12_REC_SRCDSC          0x00070041 /* port-stack mapping + more, the key of the whole thing */
92 #define K12_REC_SCENARIO        0x00070040 /* what appears as the window's title */
93 #define K12_REC_70042           0x00070042 /* XXX: ??? */ 
94 #define K12_REC_70044           0x00070044 /* text with a grammar (conditions/responses) */
95 #define K12_REC_20030           0x00020030 /* human readable start time  */ 
96 #define K12_REC_20032           0x00020031 /* human readable stop time */
97
98 #define K12_MASK_PACKET         0xfffffff0
99
100
101 #define K12_RECORD_LEN         0x0
102 #define K12_RECORD_TYPE        0x4
103 #define K12_RECORD_FRAME_LEN   0x8
104 #define K12_RECORD_INPUT      0xc
105
106 #define K12_PACKET_TIMESTAMP  0x18
107 #define K12_PACKET_FRAME      0x20
108
109 #define K12_SRCDESC_EXTRALEN   0x1e
110 #define K12_SRCDESC_NAMELEN    0x20
111 #define K12_SRCDESC_STACKLEN   0x22
112
113 #define K12_SRCDESC_EXTRATYPE  0x24
114 #define K12_SRCDESC_ATM_VPI    0x38
115 #define K12_SRCDESC_ATM_VCI    0x3a
116 #define K12_SRCDESC_DS0_MASK   0x3c
117
118
119
120 /*
121  * get_record: Get the next record into a buffer
122  *   Every about 0x2000 bytes 0x10 bytes are inserted in the file,
123  *   even in the middle of a record.
124  *   This reads the next record without the eventual 0x10 bytes.
125  *   returns the lenght of the record + the stuffing (if any)
126  *
127  * XXX: works at most with 0x1FFF bytes per record 
128  */
129 static gint get_record(guint8* buffer, FILE* fh, guint file_offset) {
130         long read;
131         long len;
132         int i;
133         long junky_offset = 0x2000 - ( (file_offset - 0x200) % 0x2000 );
134
135 #ifdef DEBUG_K12
136     k12_dbg("k12:get_record: ENTER offset=%u",file_offset);
137 #endif
138     
139         if  ( junky_offset != 0x2000 ) {
140                 
141                 /* safe header */
142                 read = file_read(buffer,1, 0x4,fh);
143                 
144                 if (read == 2 && buffer[0] == 0xff && buffer[1] == 0xff) {
145 #ifdef DEBUG_K12
146             k12_dbg("k12:get_record: EOF");
147 #endif
148             
149                         return 0;
150                 } else if ( read != 0x4 ) {
151 #ifdef DEBUG_K12
152             k12_dbg("k12:get_record: SHORT READ");
153 #endif
154             
155                         return -1;
156                 }
157                 
158                 len = pntohl(buffer) & 0x00001FFF;
159                 
160                 if (junky_offset > len) {
161                         /* safe body */
162                         if (len - 0x4 <= 0) {
163 #ifdef DEBUG_K12
164                 k12_dbg("k12:get_record: TOO SHORT");
165 #endif                
166                                 return -1;
167                         }
168                         
169                         if ( file_read(buffer+0x4, 1, len - 0x4, fh) < len - 0x4 ) {
170 #ifdef DEBUG_K12
171                 k12_dbg("k12:get_record: SHORT READ");
172 #endif
173                 
174                                 return -1;
175                         } else {
176 #ifdef DEBUG_K12
177                 k12_dbg("k12:get_record: GOT len=%u",len);
178 #endif
179                 
180                                 return len;
181                         }
182                 } else {
183                         if ( file_read(buffer+0x4, 1, len + 0xC, fh) < len ) {
184                                 return -1;
185                         }
186                         
187                         for (i = junky_offset; i < len; i++) {
188                                 buffer[i] = buffer[i+0x10];
189                         }
190 #ifdef DEBUG_K12
191             k12_dbg("k12:get_record: GOT stuffed_len=%u",len + 0x10);
192 #endif
193             
194                         return len + 0x10;
195                 }
196         } else {
197                 /* unsafe header */
198                 
199                 read = file_read(buffer,1,0x14,fh);
200                 
201                 if (read == 2 && buffer[0] == 0xff && buffer[1] == 0xff) {
202 #ifdef DEBUG_K12
203             k12_dbg("k12:get_record: EOF",len + 0x10);
204 #endif
205             
206                         return 0;
207                 } else if ( read < 0x14 ){
208 #ifdef DEBUG_K12
209             k12_dbg("k12:get_record: SHORT READ",len + 0x10);
210 #endif
211             
212                         return -1;
213                 }
214                 
215                 for (i = 0; i < 0x10; i++) {
216                         buffer[i] = buffer[i+0x10];
217                 }
218                 
219                 len = pntohl(buffer) & 0x00001FFF;
220                 
221                 if (len - 0x4 <= 0) {
222 #ifdef DEBUG_K12
223             k12_dbg("k12:get_record: TOO SHORT");
224 #endif
225                         return -1;
226         }
227                 /* safe body */
228                 if ( file_read(buffer + 0x4, 1, len - 0x4,fh) < len - 0x4 ) {
229 #ifdef DEBUG_K12
230             k12_dbg("k12:get_record: READ ERROR");
231 #endif
232                         return -1;
233                 } else {
234
235 #ifdef DEBUG_K12
236             k12_dbg("k12:get_record: GOT stuffed_len=%u",len + 0x10);
237 #endif
238             
239                         return len + 0x10;
240                 }
241         }
242 }
243
244 static gboolean k12_read(wtap *wth, int *err, gchar **err_info _U_, long *data_offset) {
245         k12_src_desc_t* src_desc;
246         guint8 buffer[0x2000];
247         long offset;
248         long len;
249         guint32 type;
250         guint64 ts;
251         
252         offset = wth->data_offset;
253
254 #ifdef DEBUG_K12
255     k12_dbg("k12_read: offset=%i",offset);
256 #endif
257     
258         /* ignore the record if it isn't a packet */    
259         do {
260                 *data_offset = offset;
261                 
262                 len = get_record(buffer, wth->fh, offset);
263                 
264                 if (len < 0) {
265                         *err = WTAP_ERR_SHORT_READ;
266                         return FALSE;
267                 } else if (len == 0) {
268                         *err = 0;
269                         return FALSE;
270                 }
271                 
272                 type = pntohl(buffer + K12_RECORD_TYPE);
273
274 #ifdef DEBUG_K12
275         k12_dbg("k12_read: record type=%i",offset);
276 #endif
277         
278                 offset += len;
279                 
280         } while ( (type & K12_MASK_PACKET) != K12_REC_PACKET );
281         
282         wth->data_offset = offset;
283         
284         ts = pntohll(buffer + K12_PACKET_TIMESTAMP);
285         
286         wth->phdr.ts.secs = (guint32) ( (ts % 2000000) / 2);
287         wth->phdr.ts.nsecs = (guint32) ((ts / 2000000) + 631152000) * 1000;
288             
289         wth->phdr.len = wth->phdr.caplen = pntohl(buffer + K12_RECORD_FRAME_LEN) & 0x00001FFF;
290         
291         /* the frame */
292         buffer_assure_space(wth->frame_buffer, wth->phdr.caplen);
293         memcpy(buffer_start_ptr(wth->frame_buffer), buffer + K12_PACKET_FRAME, wth->phdr.caplen);
294         
295         wth->pseudo_header.k12.input = pntohl(buffer + K12_RECORD_INPUT);
296
297 #ifdef DEBUG_K12
298     k12_dbg("k12_read: wth->pseudo_header.k12.input=%x wth->phdr.len=%i",wth->pseudo_header.k12.input,wth->phdr.len);
299 #endif
300     
301     src_desc = g_hash_table_lookup(wth->capture.k12->src_by_id,GUINT_TO_POINTER(wth->pseudo_header.k12.input));
302         
303         if (src_desc) {
304
305 #ifdef DEBUG_K12
306         k12_dbg("k12_read: input_name='%s' stack_file='%s' type=%x",src_desc->input_name,src_desc->stack_file,src_desc->input_type);
307 #endif
308         wth->pseudo_header.k12.input_name = src_desc->input_name;
309                 wth->pseudo_header.k12.stack_file = src_desc->stack_file;
310                 wth->pseudo_header.k12.input_type = src_desc->input_type;
311         
312                 memcpy(&(wth->pseudo_header.k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
313         } else {
314         
315 #ifdef DEBUG_K12
316         k12_dbg("k12_read: NO RECORD FOUND");
317 #endif
318         
319                 memset(&(wth->pseudo_header),0,sizeof(wth->pseudo_header));
320                 wth->pseudo_header.k12.input_name = "unknown port";
321                 wth->pseudo_header.k12.stack_file = "unknown stack file";
322         }
323         
324         wth->pseudo_header.k12.stuff = wth->capture.k12;
325
326         return TRUE;
327 }
328
329
330 static gboolean k12_seek_read(wtap *wth, long seek_off, union wtap_pseudo_header *pseudo_header, guchar *pd, int length, int *err _U_, gchar **err_info _U_) {
331         k12_src_desc_t* src_desc;
332         guint8 buffer[0x2000];
333
334 #ifdef DEBUG_K12
335     k12_dbg("k12_seek_read: ENTER");
336 #endif
337     
338         if ( file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) {
339 #ifdef DEBUG_K12
340         k12_dbg("k12_seek_read: SEEK ERROR");
341 #endif
342                 return FALSE;
343         }
344         
345         if (get_record(buffer, wth->random_fh, seek_off) < 1) {
346 #ifdef DEBUG_K12
347         k12_dbg("k12_seek_read: READ ERROR");
348 #endif
349                 return FALSE;
350         }
351         
352         memcpy(pd, buffer + K12_PACKET_FRAME, length);
353         
354         wth->pseudo_header.k12.input = pseudo_header->k12.input = pntohl(buffer + K12_RECORD_INPUT);
355         
356 #ifdef DEBUG_K12
357     k12_dbg("k12_seek_read: input=%.8x",pseudo_header->k12.input);
358 #endif
359     
360         src_desc = g_hash_table_lookup(wth->capture.k12->src_by_id,GUINT_TO_POINTER(pseudo_header->k12.input));
361     
362         if (src_desc) {
363
364 #ifdef DEBUG_K12
365         k12_dbg("k12_seek_read: input_name='%s' stack_file='%s' type=%x",src_desc->input_name,src_desc->stack_file,src_desc->input_type);
366 #endif
367         wth->pseudo_header.k12.input_name = pseudo_header->k12.input_name = src_desc->input_name;
368                 wth->pseudo_header.k12.stack_file = pseudo_header->k12.stack_file = src_desc->stack_file;
369                 wth->pseudo_header.k12.input_type = pseudo_header->k12.input_type = src_desc->input_type;
370                 
371                 memcpy(&(pseudo_header->k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
372                 memcpy(&(wth->pseudo_header.k12.input_info),&(src_desc->input_info),sizeof(src_desc->input_info));
373         } else {
374         
375 #ifdef DEBUG_K12
376         k12_dbg("k12_seek_read: NO SRC_RECORD FOUND");
377 #endif
378         
379                 memset(&(wth->pseudo_header),0,sizeof(wth->pseudo_header));
380                 memset(&(pseudo_header),0,sizeof(pseudo_header));
381                 wth->pseudo_header.k12.input_name = pseudo_header->k12.input_name = "unknown port";
382                 wth->pseudo_header.k12.stack_file = pseudo_header->k12.stack_file = "unknown stack file";
383         }
384         
385         wth->pseudo_header.k12.stuff = pseudo_header->k12.stuff = wth->capture.k12;
386
387 #ifdef DEBUG_K12
388     k12_dbg("k12_seek_read: DONE OK");
389 #endif
390     
391         return TRUE;
392 }
393
394
395 static k12_t* new_k12_file_data() {
396         k12_t* fd = g_malloc(sizeof(k12_t));
397         
398         fd->file_len = 0;
399         fd->num_of_records = 0;
400         fd->src_by_name = g_hash_table_new(g_str_hash,g_str_equal);
401         fd->src_by_id = g_hash_table_new(g_direct_hash,g_direct_equal);
402         
403         return fd;
404 }
405
406 static gboolean destroy_srcdsc(gpointer k _U_, gpointer v, gpointer p _U_) {
407         k12_src_desc_t* rec = v;
408         
409         if(rec->input_name)
410                 g_free(rec->input_name);
411         
412         if(rec->stack_file)
413                 g_free(rec->stack_file);
414         
415         g_free(rec);
416         
417         return TRUE;
418 }
419
420 static void destroy_k12_file_data(k12_t* fd) {
421         g_hash_table_destroy(fd->src_by_id);
422         g_hash_table_foreach_remove(fd->src_by_name,destroy_srcdsc,NULL);       
423         g_hash_table_destroy(fd->src_by_name);
424         g_free(fd);
425 }
426
427 static void k12_close(wtap *wth) {
428 #ifdef DEBUG_K12
429     k12_dbg("k12_close: ENTER");
430     
431     if (DEBUG_K12) {
432         fclose(dbg_out);
433     }
434     
435 #endif
436
437         destroy_k12_file_data(wth->capture.k12);
438 }
439
440
441 int k12_open(wtap *wth, int *err, gchar **err_info _U_) {
442         k12_src_desc_t* rec;
443         guint8 read_buffer[0x2000];
444         guint32 type;
445         long offset;
446         long len;
447         guint32 rec_len;
448         guint32 extra_len;
449         guint32 name_len;
450         guint32 stack_len;
451         guint i;
452         k12_t* file_data;
453         
454     
455 #ifdef DEBUG_K12
456     if (DEBUG_K12) {
457         dbg_out = fopen(DEBUG_K12,"w");
458     } else {
459         dbg_out = stderr;
460     }
461     
462     k12_dbg("k12_open: ENTER");
463 #endif
464     
465         if ( file_read(read_buffer,1,0x200,wth->fh) != 0x200 ) {
466 #ifdef DEBUG_K12
467         k12_dbg("k12_open: FILE HEADER TOO SHORT");
468 #endif
469                 return 0;
470         } else {
471                 if ( memcmp(read_buffer,k12_file_magic,8) != 0 ) {
472 #ifdef DEBUG_K12
473             k12_dbg("k12_open: BAD MAGIC");
474 #endif
475                         return 0;
476                 }
477         }
478         
479         offset = 0x200;
480             
481         file_data = new_k12_file_data();
482         
483         file_data->file_len = pntohl( read_buffer + 0x8);
484         file_data->num_of_records = pntohl( read_buffer + 0xC );
485
486 #ifdef DEBUG_K12
487     k12_dbg("k12_open: FILE_HEADER OK: offset=%x file_len=%i records=%i",
488             offset,
489             file_data->file_len,
490             file_data->num_of_records );
491 #endif
492     
493         do {
494                 
495                 len = get_record(read_buffer, wth->fh, offset);
496         
497                 if ( len <= 0 ) {
498 #ifdef DEBUG_K12
499             k12_dbg("k12_open: BAD HEADER RECORD");
500 #endif            
501                         return -1;
502                 }
503
504
505                 type = pntohl( read_buffer + K12_RECORD_TYPE );
506
507                 if ( (type & K12_MASK_PACKET) == K12_REC_PACKET) {
508                         /*
509                          * we are at the first packet record, rewind and leave.
510                          */
511                         if (file_seek(wth->fh, offset, SEEK_SET, err) == -1) {
512                                 destroy_k12_file_data(file_data);
513                                 return -1;
514                         }
515 #ifdef DEBUG_K12
516     k12_dbg("k12_open: FIRST PACKET offset=%x",offset);
517 #endif                                  
518                         break;
519                 } else if (type == K12_REC_SRCDSC) {
520                         rec = g_malloc0(sizeof(k12_src_desc_t));
521                         
522                         rec_len = pntohl( read_buffer + K12_RECORD_LEN );
523                         extra_len = pntohs( read_buffer + K12_SRCDESC_EXTRALEN );
524                         name_len = pntohs( read_buffer + K12_SRCDESC_NAMELEN );
525                         stack_len = pntohs( read_buffer + K12_SRCDESC_STACKLEN );
526
527             rec->input = pntohl( read_buffer + K12_RECORD_INPUT );
528
529 #ifdef DEBUG_K12
530             k12_dbg("k12_open: INTERFACE RECORD offset=%x interface=%x",offset,rec->input);
531 #endif
532             
533                         if (extra_len == 0 || name_len == 0 || stack_len == 0
534                                 || 0x20 + extra_len + name_len + stack_len > rec_len ) {
535                                 g_free(rec);
536 #ifdef DEBUG_K12
537                 k12_dbg("k12_open: failed (extra_len == 0 || name_len == 0 || stack_len == 0 "
538                         "|| 0x20 + extra_len + name_len + stack_len > rec_len)  extra_len=%i name_len=%i stack_len=%i");
539 #endif
540                 
541                                 return 0;
542                         }
543                         
544                         switch(( rec->input_type = pntohl( read_buffer + K12_SRCDESC_EXTRATYPE ) )) {
545                                 case K12_PORT_DS0S:
546                                         rec->input_info.ds0mask = 0x00000000;
547                                         
548                                         for (i = 0; i < 32; i++) {
549                                                 rec->input_info.ds0mask |= ( *(read_buffer + K12_SRCDESC_DS0_MASK + i) == 0xff ) ? 0x1<<(31-i) : 0x0; 
550                                         }
551                                                 
552                                                 break;
553                                 case K12_PORT_ATMPVC:
554                                         rec->input_info.atm.vp = pntohs( read_buffer + K12_SRCDESC_ATM_VPI );
555                                         rec->input_info.atm.vc = pntohs( read_buffer + K12_SRCDESC_ATM_VCI );
556                                         break;
557                                 default:
558                                         break;
559                         }
560                         
561                         rec->input_name = g_memdup(read_buffer + K12_SRCDESC_EXTRATYPE + extra_len, name_len);
562                         rec->stack_file = g_memdup(read_buffer + K12_SRCDESC_EXTRATYPE + extra_len + name_len, stack_len);
563                         
564                         g_strdown(rec->stack_file);
565                                 
566                         g_hash_table_insert(file_data->src_by_id,GUINT_TO_POINTER(rec->input),rec);
567                         g_hash_table_insert(file_data->src_by_name,rec->stack_file,rec);
568                         
569                         offset += len;
570                         continue;
571                 } else {
572                         offset += len;
573                         continue;
574                 }
575         } while(1);
576         
577         wth->data_offset = offset;
578         wth->file_type = WTAP_FILE_K12;
579         wth->file_encap = WTAP_ENCAP_K12;
580         wth->snapshot_length = 0;
581         wth->subtype_read = k12_read;
582         wth->subtype_seek_read = k12_seek_read;
583         wth->subtype_close = k12_close;
584         wth->capture.k12 = file_data;
585         wth->tsprecision = WTAP_FILE_TSPREC_NSEC;
586         
587         /* if we use just one encapsulation for all the file
588                 we will use that for the whole file so we can
589                 use more formats to save to */
590         
591         
592         return 1;
593 }
594
595 int k12_dump_can_write_encap(int encap) {
596         
597         if (encap == WTAP_ENCAP_PER_PACKET)
598                 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
599         
600         if (encap != WTAP_ENCAP_K12)
601                 return WTAP_ERR_UNSUPPORTED_ENCAP;
602         
603         return 0;
604 }
605
606 static const gchar dumpy_junk[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
607
608 static void k12_dump_record(wtap_dumper *wdh, long len,  guint8* buffer) {
609         long junky_offset = (0x2000 - ( (wdh->dump.k12->file_offset - 0x200) % 0x2000 )) % 0x2000;
610         
611         if (len > junky_offset) {
612                 
613                 if (junky_offset)
614                         fwrite(buffer, 1, junky_offset, wdh->fh);
615                 
616                 fwrite(dumpy_junk, 1, 0x10, wdh->fh);
617                 
618                 fwrite(buffer+junky_offset, 1, len - junky_offset, wdh->fh);
619                 
620                 wdh->dump.k12->file_offset += len + 0x10;
621         } else {
622                 fwrite(buffer, 1, len, wdh->fh);
623                 wdh->dump.k12->file_offset += len;
624         }
625         
626         wdh->dump.k12->num_of_records++;
627 }
628
629 static void k12_dump_src_setting(gpointer k _U_, gpointer v, gpointer p) {
630         k12_src_desc_t* src_desc = v;
631         wtap_dumper *wdh = p;
632         guint32 len;
633         guint offset;
634         guint i;
635         
636         union {
637                 guint8 buffer[0x2000];
638                 
639                 struct {
640                         guint32 len;
641                         guint32 type;
642                         guint32 unk32_1;
643                         guint32 input;
644                         
645                         guint32 unk32_2;
646                         guint32 unk32_3;
647                         guint32 unk32_4;
648                         guint16 unk16_1;
649                         guint16 extra_len;
650                         
651                         guint16 name_len;
652                         guint16 stack_len;
653                         
654                         struct {
655                                 guint32 type;
656                                 
657                                 union {
658                                         struct {
659                                                 guint32 unk32;
660                                                 guint8 mask[32];
661                                         } ds0mask;
662                                         
663                                         struct {
664                                                 guint8 unk_data[0x10];
665                                                 guint16 vp;
666                                                 guint16 vc;
667                                         } atm;
668                                         
669                                         guint32 unk;
670                                 } desc;
671                         } extra;
672                 } record;
673         } obj;
674         
675         obj.record.type = g_htonl(K12_REC_SRCDSC);
676         obj.record.unk32_1 = g_htonl(0x00000001);
677         obj.record.input = g_htonl(src_desc->input);
678         
679         obj.record.unk32_2 = g_htonl(0x0000060f);
680         obj.record.unk32_3 = g_htonl(0x00000003);
681         obj.record.unk32_4 = g_htonl(0x01000100);
682
683         obj.record.unk16_1 = g_htons(0x0000);
684         obj.record.name_len = strlen(src_desc->input_name) + 1;
685         obj.record.stack_len = strlen(src_desc->stack_file) + 1;
686
687         obj.record.extra.type = g_htonl(src_desc->input_type);
688         
689         switch (src_desc->input_type) {
690                 case K12_PORT_ATMPVC:
691                         obj.record.extra_len = g_htons(0x18);
692                         obj.record.extra.desc.atm.vp = g_htons(src_desc->input_info.atm.vp);
693                         obj.record.extra.desc.atm.vc = g_htons(src_desc->input_info.atm.vc);
694                         offset = 0x3c;
695                         break;
696                 case K12_PORT_DS0S:
697                         obj.record.extra_len = g_htons(0x18);
698                         for( i=0; i<32; i++ ) {
699                                 obj.record.extra.desc.ds0mask.mask[i] =
700                                         (src_desc->input_info.ds0mask & (1 << i)) ? 0xff : 0x00;
701                         }
702                         offset = 0x3c;
703                         break;
704                 default:
705                         obj.record.extra_len = g_htons(0x08);
706                         offset = 0x2c;
707                         break;
708         }
709         
710         memcpy(obj.buffer + offset,
711                    src_desc->input_name,
712                    obj.record.name_len);
713         
714         memcpy(obj.buffer + offset + obj.record.name_len,
715                    src_desc->stack_file,
716                    obj.record.stack_len);
717         
718         len = offset + obj.record.name_len + obj.record.stack_len;
719         len += (len % 4) ? 4 - (len % 4) : 0;
720         
721         obj.record.len = g_htonl(len);
722         obj.record.name_len =  g_htons(obj.record.name_len);
723         obj.record.stack_len = g_htons(obj.record.stack_len);
724
725         k12_dump_record(wdh,len,obj.buffer);
726 }
727
728 static gboolean k12_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
729                                                         const union wtap_pseudo_header *pseudo_header,
730                                                         const guchar *pd, int *err _U_) {
731         long len;
732         union {
733                 guint8 buffer[0x2000];
734                 struct {
735                         guint32 len;
736                         guint32 type;
737                         guint32 frame_len;
738                         guint32 input;
739                         
740                         guint32 datum_1;
741                         guint32 datum_2;
742                         guint64 ts;
743                         
744                         guint8 frame[0x1fc0];
745                 } record;
746         } obj;
747         
748         if (wdh->dump.k12->num_of_records == 0) {
749                 k12_t* file_data = pseudo_header->k12.stuff;
750                 g_hash_table_foreach(file_data->src_by_id,k12_dump_src_setting,wdh);
751         }
752         
753         obj.record.len = 0x20 + phdr->len;
754         obj.record.len += (obj.record.len % 4) ? 4 - obj.record.len % 4 : 0;
755
756         len = obj.record.len;
757         
758         obj.record.len = g_htonl(obj.record.len);
759
760         obj.record.type = g_htonl(K12_REC_PACKET);
761         obj.record.frame_len = g_htonl(phdr->len);
762         obj.record.input = g_htonl(pseudo_header->k12.input);
763         
764         obj.record.ts = GUINT64_TO_BE((((guint64)phdr->ts.secs - 631152000) * 2000000) + (phdr->ts.nsecs / 1000 * 2));
765
766         memcpy(obj.record.frame,pd,phdr->len);
767         
768         k12_dump_record(wdh,len,obj.buffer);
769         
770         /* XXX if OK */
771         return TRUE;
772 }
773
774 static const guint8 k12_eof[] = {0xff,0xff};
775
776 static gboolean k12_dump_close(wtap_dumper *wdh, int *err) {
777         union {
778                 guint8 b[sizeof(guint32)];
779                 guint32 u;
780         } d;
781         
782         fwrite(k12_eof, 1, 2, wdh->fh);
783
784         if (fseek(wdh->fh, 8, SEEK_SET) == -1) {
785                 *err = errno;
786                 return FALSE;
787         }
788         
789         d.u = g_htonl(wdh->dump.k12->file_len);
790         
791         fwrite(d.b, 1, 4, wdh->fh);
792
793         d.u = g_htonl(wdh->dump.k12->num_of_records);
794         
795         fwrite(d.b, 1, 4, wdh->fh);
796
797         return TRUE;
798 }
799
800
801 gboolean k12_dump_open(wtap_dumper *wdh, gboolean cant_seek, int *err) {
802         
803         if (cant_seek) {
804                 *err = WTAP_ERR_CANT_WRITE_TO_PIPE;
805                 return FALSE;
806         }
807         
808         if ( fwrite(k12_file_magic, 1, 8, wdh->fh) != 8 ) {
809                 *err = errno;
810                 return FALSE;
811         }
812         
813         if (fseek(wdh->fh, 0x200, SEEK_SET) == -1) {
814                 *err = errno;
815                 return FALSE;
816         }
817
818         wdh->subtype_write = k12_dump;
819         wdh->subtype_close = k12_dump_close;
820         
821         wdh->dump.k12 = g_malloc(sizeof(k12_dump_t));
822         wdh->dump.k12->file_len = 0x200;
823         wdh->dump.k12->num_of_records = 0;
824         wdh->dump.k12->file_offset  = 0x200;
825         
826         return TRUE;
827 }
828
829