Global variables considered harmful - we don't want to rule out two
[metze/wireshark/wip.git] / wiretap / catapult_dct2000.c
1 /* catapult_dct2000.c
2  *
3  * $Id$
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include <errno.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <ctype.h>
30
31 #include "wtap-int.h"
32 #include "file_wrappers.h"
33 #include "buffer.h"
34
35 #include "catapult_dct2000.h"
36
37 #define MAX_FIRST_LINE_LENGTH      200
38 #define MAX_TIMESTAMP_LINE_LENGTH  100
39 #define MAX_LINE_LENGTH            65536
40 #define MAX_TIMESTAMP_LEN          32
41 #define MAX_SECONDS_CHARS          16
42 #define MAX_SUBSECOND_DECIMALS     4
43 #define MAX_CONTEXT_NAME           64
44 #define MAX_PROTOCOL_NAME          64
45 #define MAX_PORT_DIGITS            2
46 #define MAX_VARIANT_DIGITS         32
47 #define MAX_OUTHDR_NAME            256
48 #define AAL_HEADER_CHARS           12
49
50 /* TODO:
51    - support for FP over AAL0
52    - support for IuR interface FP
53    - support for x.25?
54 */
55
56 /* 's' or 'r' of a packet as read from .out file */
57 typedef enum packet_direction_t
58 {
59     sent,
60     received
61 } packet_direction_t;
62
63
64 /***********************************************************************/
65 /* For each line, store (in case we need to dump):                     */
66 /* - String before time field                                          */
67 /* - String beween time field and data (if NULL assume " l ")          */
68 typedef struct
69 {
70     gchar *before_time;
71     gchar *after_time;
72 } line_prefix_info_t;
73
74
75 /*******************************************************************/
76 /* Information stored external to a file (wtap) needed for dumping */
77 typedef struct dct2000_file_externals
78 {
79     /* Buffer to hold first line, including magic and format number */
80     gchar firstline[MAX_FIRST_LINE_LENGTH];
81     gint  firstline_length;
82
83     /* Buffer to hold second line with formatted file creation data/time */
84     gchar secondline[MAX_TIMESTAMP_LINE_LENGTH];
85     gint  secondline_length;
86
87     /* Hash table to store text prefix data part of displayed packets.
88        Records (file offset -> line_prefix_info_t)
89        N.B. This is only needed for dumping
90     */
91     GHashTable *packet_prefix_table;
92 } dct2000_file_externals_t;
93
94 /* 'Magic number' at start of Catapult DCT2000 .out files. */
95 static const gchar catapult_dct2000_magic[] = "Session Transcript";
96
97 /************************************************************/
98 /* Functions called from wiretap core                       */
99 static gboolean catapult_dct2000_read(wtap *wth, int *err, gchar **err_info,
100                                       gint64 *data_offset);
101 static gboolean catapult_dct2000_seek_read(wtap *wth, gint64 seek_off,
102                                            union wtap_pseudo_header *pseudo_header,
103                                            guchar *pd, int length,
104                                            int *err, gchar **err_info);
105 static void catapult_dct2000_close(wtap *wth);
106
107 static gboolean catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
108                                       const union wtap_pseudo_header *pseudo_header,
109                                       const guchar *pd, int *err);
110 static gboolean catapult_dct2000_dump_close(wtap_dumper *wdh, int *err);
111
112
113 /************************************************************/
114 /* Private helper functions                                 */
115 static gboolean read_new_line(FILE_T fh, gint64 *offset, gint *length,
116                               gchar *buf, size_t bufsize);
117 static gboolean parse_line(char *linebuff, gint line_length,
118                            gint *seconds, gint *useconds,
119                            long *before_time_offset, long *after_time_offset,
120                            long *data_offset,
121                            gint *data_chars,
122                            packet_direction_t *direction,
123                            int *encap, int *is_comment,
124                            gchar *aal_header_chars,
125                            gchar *context_name, guint8 *context_portp,
126                            gchar *protocol_name, gchar *variant_name,
127                            gchar *outhdr_name);
128 static int write_stub_header(guchar *frame_buffer, char *timestamp_string,
129                              packet_direction_t direction, int encap,
130                              gchar *context_name, guint8 context_port,
131                              gchar *protocol_name, gchar *variant_name,
132                              gchar *outhdr_name);
133 static guchar hex_from_char(gchar c);
134 static gchar char_from_hex(guchar hex);
135
136 static void set_pseudo_header_info(wtap *wth,
137                                    int pkt_encap,
138                                    gint64 file_offset,
139                                    union wtap_pseudo_header *pseudo_header,
140                                    packet_direction_t direction,
141                                    gchar *aal_header_chars);
142 static void set_aal_info(union wtap_pseudo_header *pseudo_header,
143                          packet_direction_t direction,
144                          gchar *aal_header_chars);
145 static void set_isdn_info(union wtap_pseudo_header *pseudo_header,
146                           packet_direction_t direction);
147 static void set_ppp_info(union wtap_pseudo_header *pseudo_header,
148                          packet_direction_t direction);
149
150 static gint packet_offset_equal(gconstpointer v, gconstpointer v2);
151 static guint packet_offset_hash_func(gconstpointer v);
152
153 static gboolean get_file_time_stamp(gchar *linebuff, time_t *secs, guint32 *usecs);
154 static gboolean free_line_prefix_info(gpointer key, gpointer value, gpointer user_data);
155
156
157
158 /********************************************/
159 /* Open file (for reading)                 */
160 /********************************************/
161 int catapult_dct2000_open(wtap *wth, int *err, gchar **err_info _U_)
162 {
163     gint64  offset = 0;
164     time_t  timestamp;
165     guint32 usecs;
166     gint firstline_length = 0;
167     dct2000_file_externals_t *file_externals;
168     gchar linebuff[MAX_LINE_LENGTH];
169
170     /* Clear errno before reading from the file */
171     errno = 0;
172
173
174     /********************************************************************/
175     /* First line needs to contain at least as many characters as magic */
176
177     read_new_line(wth->fh, &offset, &firstline_length, linebuff,
178                   sizeof linebuff);
179     if (((size_t)firstline_length < strlen(catapult_dct2000_magic)) ||
180         firstline_length >= MAX_FIRST_LINE_LENGTH) {
181
182         return 0;
183     }
184
185     /* This file is not for us if it doesn't match our signature */
186     if (memcmp(catapult_dct2000_magic, linebuff, strlen(catapult_dct2000_magic)) != 0) {
187         return 0;
188     }
189
190
191     /*********************************************************************/
192     /* Need entry in file_externals table                                */
193
194     /* Allocate a new file_externals structure for this file */
195     file_externals = g_malloc(sizeof(dct2000_file_externals_t));
196     memset((void*)file_externals, '\0', sizeof(dct2000_file_externals_t));
197
198     /* Copy this first line into buffer so could write out later */
199     g_strlcpy(file_externals->firstline, linebuff, firstline_length+1);
200     file_externals->firstline_length = firstline_length;
201
202
203     /***********************************************************/
204     /* Second line contains file timestamp                     */
205     /* Store this offset in in wth->capture->catapult_dct2000  */
206
207     read_new_line(wth->fh, &offset, &(file_externals->secondline_length),
208                   linebuff, sizeof linebuff);
209     if ((file_externals->secondline_length >= MAX_TIMESTAMP_LINE_LENGTH) ||
210         (!get_file_time_stamp(linebuff, &timestamp, &usecs))) {
211
212         /* Give up if file time line wasn't valid */
213         g_free(file_externals);
214         return 0;
215     }
216
217     /* Allocate struct and fill in timestamp */
218     wth->capture.catapult_dct2000 = g_malloc(sizeof(catapult_dct2000_t));
219     wth->capture.catapult_dct2000->start_secs = timestamp;
220     wth->capture.catapult_dct2000->start_usecs = usecs;
221
222     /* Copy this second line into buffer so could write out later */
223     g_strlcpy(file_externals->secondline, linebuff, file_externals->secondline_length+1);
224
225
226     /************************************************************/
227     /* File is for us. Fill in details so packets can be read   */
228
229     /* Set our file type */
230     wth->file_type = WTAP_FILE_CATAPULT_DCT2000;
231
232     /* Use our own encapsulation to send all packets to our stub dissector */
233     wth->file_encap = WTAP_ENCAP_CATAPULT_DCT2000;
234
235     /* Callbacks for reading operations */
236     wth->subtype_read = catapult_dct2000_read;
237     wth->subtype_seek_read = catapult_dct2000_seek_read;
238     wth->subtype_close = catapult_dct2000_close;
239
240     /* Choose microseconds (have 4 decimal places...) */
241     wth->tsprecision = WTAP_FILE_TSPREC_USEC;
242
243
244     /***************************************************************/
245     /* Initialise packet_prefix_table (index is offset into file)  */
246     file_externals->packet_prefix_table =
247         g_hash_table_new(packet_offset_hash_func, packet_offset_equal);
248
249     /* Set this wtap to point to the file_externals */
250     wth->capture.generic = (void*)file_externals;
251
252     *err = errno;
253     return 1;
254 }
255
256
257 /**************************************************/
258 /* Read packet function.                          */
259 /* Look for and read the next usable packet       */
260 /* - return TRUE and details if found             */
261 /**************************************************/
262 gboolean catapult_dct2000_read(wtap *wth, int *err, gchar **err_info _U_,
263                                gint64 *data_offset)
264 {
265     gint64 offset = wth->data_offset;
266     long dollar_offset, before_time_offset, after_time_offset;
267     packet_direction_t direction;
268     int encap;
269
270     /* Get wtap external structure for this wtap */
271     dct2000_file_externals_t *file_externals =
272         (dct2000_file_externals_t*)wth->capture.generic;
273
274     /* There *has* to be an entry for this wth */
275     if (!file_externals) {
276         return FALSE;
277     }
278
279     /* Search for a line containing a usable packet */
280     while (1) {
281         int line_length, seconds, useconds, data_chars;
282         int is_comment = FALSE;
283         gint64 this_offset = offset;
284         gchar linebuff[MAX_LINE_LENGTH+1];
285         gchar aal_header_chars[AAL_HEADER_CHARS];
286         gchar context_name[MAX_CONTEXT_NAME];
287         guint8 context_port;
288         gchar protocol_name[MAX_PROTOCOL_NAME+1];
289         gchar variant_name[MAX_VARIANT_DIGITS+1];
290         gchar outhdr_name[MAX_OUTHDR_NAME+1];
291
292         /* Are looking for first packet after 2nd line */
293         if (wth->data_offset == 0) {
294             this_offset += (file_externals->firstline_length+1+
295                             file_externals->secondline_length+1);
296         }
297
298         /* Clear errno before reading from the file */
299         errno = 0;
300
301         /* Read a new line from file into linebuff */
302         if (read_new_line(wth->fh, &offset, &line_length, linebuff,
303                           sizeof linebuff) == FALSE) {
304             /* Get out if no more lines can be read */
305             break;
306         }
307
308         /* Try to parse the line as a frame record */
309         if (parse_line(linebuff, line_length, &seconds, &useconds,
310                        &before_time_offset, &after_time_offset,
311                        &dollar_offset,
312                        &data_chars, &direction, &encap, &is_comment,
313                        aal_header_chars,
314                        context_name, &context_port,
315                        protocol_name, variant_name, outhdr_name)) {
316             guchar *frame_buffer;
317             int n;
318             int stub_offset = 0;
319             line_prefix_info_t *line_prefix_info;
320             char timestamp_string[MAX_TIMESTAMP_LEN+1];
321             gint64 *pkey = NULL;
322
323             g_snprintf(timestamp_string, 32, "%d.%04d", seconds, useconds/100);
324
325             /* All packets go to Catapult DCT2000 stub dissector */
326             wth->phdr.pkt_encap = WTAP_ENCAP_CATAPULT_DCT2000;
327
328             /* Set data_offset to the beginning of the line we're returning.
329                This will be the seek_off parameter when this frame is re-read.
330             */
331             *data_offset = this_offset;
332
333             /* This is the position in the file where the next _read() will be called from */
334             wth->data_offset = this_offset + line_length + 1;
335
336             /* Fill in timestamp (capture base + packet offset) */
337             wth->phdr.ts.secs = wth->capture.catapult_dct2000->start_secs + seconds;
338             if ((wth->capture.catapult_dct2000->start_usecs + useconds) >= 1000000) {
339                 wth->phdr.ts.secs++;
340             }
341             wth->phdr.ts.nsecs =
342                 ((wth->capture.catapult_dct2000->start_usecs + useconds) % 1000000) *1000;
343
344             /* Get buffer pointer ready */
345             buffer_assure_space(wth->frame_buffer,
346                                 strlen(context_name)+1 +     /* Context name */
347                                 1 +                          /* port */
348                                 strlen(timestamp_string)+1 + /* timestamp */
349                                 strlen(variant_name)+1 +     /* variant */
350                                 strlen(outhdr_name)+1 +      /* outhdr */
351                                 strlen(protocol_name)+1 +    /* Protocol name */
352                                 1 +                          /* direction */
353                                 1 +                          /* encap */
354                                 is_comment ? data_chars : (data_chars/2));
355             frame_buffer = buffer_start_ptr(wth->frame_buffer);
356
357
358             /*********************/
359             /* Write stub header */
360             stub_offset = write_stub_header(frame_buffer, timestamp_string,
361                                             direction, encap, context_name,
362                                             context_port,
363                                             protocol_name, variant_name,
364                                             outhdr_name);
365
366             /* Binary data length is half bytestring length + stub header */
367             wth->phdr.len = stub_offset + (is_comment ? data_chars : (data_chars/2));
368             wth->phdr.caplen = stub_offset + (is_comment ? data_chars : (data_chars/2));
369
370
371             if (!is_comment) {
372                 /****************************************************/
373                 /* Copy data into buffer, converting from ascii hex */
374                 for (n=0; n <= data_chars; n+=2) {
375                     frame_buffer[stub_offset + n/2] =
376                         (hex_from_char(linebuff[dollar_offset+n]) << 4) |
377                          hex_from_char(linebuff[dollar_offset+n+1]);
378                 }
379             }
380             else {
381                 /***********************************************************/
382                 /* Copy packet data into buffer, just copying ascii chars  */
383                 for (n=0; n <= data_chars; n++) {
384                     frame_buffer[stub_offset + n] = linebuff[dollar_offset+n];
385                 }
386             }
387
388             /* Store the packet prefix in the hash table */
389             line_prefix_info = g_malloc(sizeof(line_prefix_info_t));
390
391             /* Create and use buffer for contents before time */
392             line_prefix_info->before_time = g_malloc(before_time_offset+2);
393             g_strlcpy(line_prefix_info->before_time, linebuff, before_time_offset+1);
394             line_prefix_info->before_time[before_time_offset+1] = '\0';
395
396             /* Create and use buffer for contents before time.
397                Do this only if it doesn't correspond to " l ", which is by far the most
398                common case. */
399             if (((size_t)(dollar_offset - after_time_offset -1) == strlen(" l ")) &&
400                 (strncmp(linebuff+after_time_offset, " l ", strlen(" l ")) == 0)) {
401
402                 line_prefix_info->after_time = NULL;
403             }
404             else {
405                 /* Allocate & write buffer for line between timestamp and data */
406                 line_prefix_info->after_time = g_malloc(dollar_offset - after_time_offset);
407                 g_strlcpy(line_prefix_info->after_time, linebuff+after_time_offset,
408                           dollar_offset - after_time_offset);
409                 line_prefix_info->after_time[dollar_offset - after_time_offset-1] = '\0';
410             }
411
412             /* Add packet entry into table */
413             pkey = g_malloc(sizeof(*pkey));
414             *pkey = this_offset;
415             g_hash_table_insert(file_externals->packet_prefix_table, pkey, line_prefix_info);
416
417             /* Set pseudo-header if necessary */
418             set_pseudo_header_info(wth, encap, this_offset, &wth->pseudo_header,
419                                    direction, aal_header_chars);
420
421             /* OK, we have packet details to return */
422             *err = errno;
423             return TRUE;
424         }
425     }
426
427     /* No packet details to return... */
428     *err = errno;
429     return FALSE;
430 }
431
432
433 /**************************************************/
434 /* Read & seek function.                          */
435 /**************************************************/
436 static gboolean
437 catapult_dct2000_seek_read(wtap *wth, gint64 seek_off,
438                            union wtap_pseudo_header *pseudo_header, guchar *pd,
439                            int length, int *err, gchar **err_info)
440 {
441     gint64 offset = wth->data_offset;
442     long dollar_offset, before_time_offset, after_time_offset;
443     gchar linebuff[MAX_LINE_LENGTH+1];
444     gchar aal_header_chars[AAL_HEADER_CHARS];
445     gchar context_name[MAX_CONTEXT_NAME];
446     guint8 context_port;
447     gchar protocol_name[MAX_PROTOCOL_NAME+1];
448     gchar variant_name[MAX_VARIANT_DIGITS+1];
449     gchar outhdr_name[MAX_OUTHDR_NAME+1];
450     int  is_comment = FALSE;
451     packet_direction_t direction;
452     int encap;
453     int seconds, useconds, data_chars;
454
455     /* Reset errno */
456     *err = errno = 0;
457
458     /* Seek to beginning of packet */
459     if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) {
460         return FALSE;
461     }
462
463     /* Re-read whole line (this really should succeed) */
464     if (read_new_line(wth->random_fh, &offset, &length, linebuff,
465                       sizeof linebuff) == FALSE) {
466         return FALSE;
467     }
468
469     /* Try to parse this line again (should succeed as re-reading...) */
470     if (parse_line(linebuff, length, &seconds, &useconds,
471                    &before_time_offset, &after_time_offset,
472                    &dollar_offset,
473                    &data_chars, &direction, &encap, &is_comment,
474                    aal_header_chars,
475                    context_name, &context_port,
476                    protocol_name, variant_name, outhdr_name)) {
477         int n;
478         int stub_offset = 0;
479         char timestamp_string[32];
480         g_snprintf(timestamp_string, 32, "%d.%04d", seconds, useconds/100);
481
482         /* Make sure all packets go to catapult dct2000 dissector */
483         wth->phdr.pkt_encap = WTAP_ENCAP_CATAPULT_DCT2000;
484
485
486         /*********************/
487         /* Write stub header */
488         stub_offset = write_stub_header(pd, timestamp_string,
489                                         direction, encap, context_name,
490                                         context_port,
491                                         protocol_name, variant_name,
492                                         outhdr_name);
493
494
495         if (!is_comment) {
496             /***********************************************************/
497             /* Copy packet data into buffer, converting from ascii hex */
498             for (n=0; n <= data_chars; n+=2) {
499                 pd[stub_offset + n/2] = (hex_from_char(linebuff[dollar_offset+n]) << 4) |
500                                          hex_from_char(linebuff[dollar_offset+n+1]);
501             }
502         }
503         else {
504             /***********************************************************/
505             /* Copy packet data into buffer, just copying ascii chars  */
506             for (n=0; n <= data_chars; n++) {
507                 pd[stub_offset+n] = linebuff[dollar_offset+n];
508             }
509         }
510
511         /* Set packet pseudo-header if necessary */
512         set_pseudo_header_info(wth, encap, seek_off, pseudo_header, direction,
513                                aal_header_chars);
514
515         *err = errno = 0;
516         return TRUE;
517     }
518
519     /* If get here, must have failed */
520     *err = errno;
521     *err_info = g_strdup_printf("catapult dct2000: seek_read failed to read/parse "
522                                 "line at position %" G_GINT64_MODIFIER "d",
523                                 seek_off);
524     return FALSE;
525 }
526
527
528 /***************************************************************************/
529 /* Free dct2000-specific capture info from file that was open for reading  */
530 /***************************************************************************/
531 void catapult_dct2000_close(wtap *wth)
532 {
533     /* Get externals for this file */
534     dct2000_file_externals_t *file_externals =
535         (dct2000_file_externals_t*)wth->capture.generic;
536
537     /* The entry *has* to be found */
538     if (!file_externals) {
539         return;
540     }
541
542     /* Free up its line prefix values */
543     g_hash_table_foreach_remove(file_externals->packet_prefix_table,
544                                 free_line_prefix_info, NULL);
545     /* Free up its line prefix table */
546     g_hash_table_destroy(file_externals->packet_prefix_table);
547
548     /* And free up file_externals itself */
549     g_free(file_externals);
550
551     /* Also free this capture info */
552     g_free(wth->capture.catapult_dct2000);
553 }
554
555
556
557
558 /***************************/
559 /* Dump functions          */
560 /***************************/
561
562 /*****************************************************/
563 /* The file that we are writing to has been opened.  */
564 /* Set other dump callbacks.                         */
565 /*****************************************************/
566 gboolean catapult_dct2000_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err _U_)
567 {
568     /* Fill in other dump callbacks */
569     wdh->subtype_write = catapult_dct2000_dump;
570     wdh->subtype_close = catapult_dct2000_dump_close;
571
572     return TRUE;
573 }
574
575 /*********************************************************/
576 /* Respond to queries about which encap types we support */
577 /* writing to.                                           */
578 /*********************************************************/
579 int catapult_dct2000_dump_can_write_encap(int encap)
580 {
581     switch (encap) {
582         case WTAP_ENCAP_CATAPULT_DCT2000:
583             /* We support this */
584             return 0;
585
586         default:
587             /* But don't write to any other formats... */
588             return WTAP_ERR_UNSUPPORTED_ENCAP;
589     }
590 }
591
592
593 /*****************************************/
594 /* Write a single packet out to the file */
595 /*****************************************/
596
597 static gboolean do_fwrite(const void *data, size_t size, size_t count, FILE *stream, int *err_p)
598 {
599     size_t nwritten;
600
601     nwritten = fwrite(data, size, count, stream);
602     if (nwritten != count) {
603         if (nwritten == 0 && ferror(stream)) {
604             *err_p = errno;
605         }
606         else {
607             *err_p = WTAP_ERR_SHORT_WRITE;
608         }
609
610         return FALSE;
611     }
612     return TRUE;
613 }
614
615 gboolean catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
616                                const union wtap_pseudo_header *pseudo_header,
617                                const guchar *pd, int *err)
618 {
619     guint32 n;
620     line_prefix_info_t *prefix = NULL;
621     gchar time_string[16];
622     gboolean is_comment;
623
624     /******************************************************/
625     /* Get the file_externals structure for this file */
626     /* Find wtap external structure for this wtap */
627     dct2000_file_externals_t *file_externals =
628         (dct2000_file_externals_t*)pseudo_header->dct2000.wth->capture.generic;
629
630     if (wdh->dump.dct2000 == NULL) {
631         /* Allocate the dct2000-specific dump structure */
632         wdh->dump.dct2000 = g_malloc(sizeof(catapult_dct2000_t));
633
634         /* Write out saved first line */
635         if (! do_fwrite(file_externals->firstline, 1, file_externals->firstline_length, wdh->fh, err)) {
636             return FALSE;
637         }
638         if (! do_fwrite("\n", 1, 1, wdh->fh, err)) {
639             return FALSE;
640         }
641
642         /* Also write out saved second line with timestamp corresponding to the
643            opening time of the log.
644         */
645         if (! do_fwrite(file_externals->secondline, 1, file_externals->secondline_length, wdh->fh, err)) {
646             return FALSE;
647         }
648         if (! do_fwrite("\n", 1, 1, wdh->fh, err)) {
649             return FALSE;
650         }
651
652         /* Allocate the dct2000-specific dump structure */
653         wdh->dump.dct2000 = g_malloc(sizeof(catapult_dct2000_t));
654
655         /* Copy time of beginning of file */
656         wdh->dump.dct2000->start_time.secs =
657             pseudo_header->dct2000.wth->capture.catapult_dct2000->start_secs;
658         wdh->dump.dct2000->start_time.nsecs =
659             (pseudo_header->dct2000.wth->capture.catapult_dct2000->start_usecs * 1000);
660
661         /* Set flag do don't write header out again */
662         wdh->dump.dct2000->first_packet_written = TRUE;
663     }
664
665
666     /******************************************************************/
667     /* Write out this packet's prefix, including calculated timestamp */
668
669     /* Look up line data prefix using stored offset */
670     prefix = (line_prefix_info_t*)g_hash_table_lookup(file_externals->packet_prefix_table,
671                                                       (const void*)&(pseudo_header->dct2000.seek_off));
672
673     /* Write out text before timestamp */
674     if (! do_fwrite(prefix->before_time, 1, strlen(prefix->before_time), wdh->fh, err)) {
675         return FALSE;
676     }
677
678     /* Can infer from prefix if this is a comment (whose payload is displayed differently) */
679     is_comment = (strstr(prefix->before_time, "/////") != NULL);
680
681     /* Calculate time of this packet to write, relative to start of dump */
682     if (phdr->ts.nsecs >= wdh->dump.dct2000->start_time.nsecs) {
683         g_snprintf(time_string, 16, "%ld.%04d",
684                  (long)(phdr->ts.secs - wdh->dump.dct2000->start_time.secs),
685                  (phdr->ts.nsecs - wdh->dump.dct2000->start_time.nsecs) / 100000);
686     }
687     else {
688         g_snprintf(time_string, 16, "%ld.%04u",
689                  (long)(phdr->ts.secs - wdh->dump.dct2000->start_time.secs-1),
690                  ((1000000000 + (phdr->ts.nsecs / 100000)) - (wdh->dump.dct2000->start_time.nsecs / 100000)) % 10000);
691     }
692
693     /* Write out the calculated timestamp */
694     if (! do_fwrite(time_string, 1, strlen(time_string), wdh->fh, err)) {
695         return FALSE;
696     }
697
698     /* Write out text between timestamp and start of hex data */
699     if (prefix->after_time == NULL) {
700         if (!do_fwrite(" l ", 1, strlen(" l "), wdh->fh, err)) {
701             return FALSE;
702         }
703     }
704     else {
705         if (!do_fwrite(prefix->after_time, 1, strlen(prefix->after_time), wdh->fh, err)) {
706             return FALSE;
707         }
708     }
709
710
711     /****************************************************************/
712     /* Need to skip stub header at start of pd before we reach data */
713
714     /* Context name */
715     for (n=0; pd[n] != '\0'; n++);
716     n++;
717
718     /* Context port number */
719     n++;
720
721     /* Timestamp */
722     for (; pd[n] != '\0'; n++);
723     n++;
724
725     /* Protocol name */
726     for (; pd[n] != '\0'; n++);
727     n++;
728
729     /* Variant number (as string) */
730     for (; pd[n] != '\0'; n++);
731     n++;
732
733     /* Outhdr (as string) */
734     for (; pd[n] != '\0'; n++);
735     n++;
736
737     /* Direction & encap */
738     n += 2;
739
740
741     /**************************************/
742     /* Remainder is encapsulated protocol */
743     if (! do_fwrite("$", 1, 1, wdh->fh, err)) {
744         return FALSE;
745     }
746
747     if (!is_comment) {
748         /* Each binary byte is written out as 2 hex string chars */ 
749         for (; n < phdr->len; n++) {
750             gchar c[2];
751             c[0] = char_from_hex((guchar)(pd[n] >> 4));
752             c[1] = char_from_hex((guchar)(pd[n] & 0x0f));
753
754             /* Write both hex chars of byte together */
755             if (! do_fwrite(c, 1, 2, wdh->fh, err)) {
756                 return FALSE;
757             }
758         }
759     }
760     else {
761         for (; n < phdr->len; n++) {
762             char c[1];
763             c[0] = pd[n];
764
765             /* Write both hex chars of byte together */
766             if (!do_fwrite(c, 1, 1, wdh->fh, err)) {
767                 return FALSE;
768             }
769         }
770     }
771
772     /* End the line */
773     if (! do_fwrite("\n", 1, 1, wdh->fh, err)) {
774         return FALSE;
775     }
776
777     return TRUE;
778 }
779
780
781 /******************************************************/
782 /* Close a file we've been writing to.                */
783 /******************************************************/
784 static gboolean catapult_dct2000_dump_close(wtap_dumper *wdh _U_, int *err _U_)
785 {
786     return TRUE;
787 }
788
789
790
791
792 /****************************/
793 /* Private helper functions */
794 /****************************/
795
796 /**********************************************************************/
797 /* Read a new line from the file, starting at offset.                 */
798 /* - writes data to its argument linebuff                             */
799 /* - on return 'offset' will point to the next position to read from  */
800 /* - return TRUE if this read is successful                           */
801 /**********************************************************************/
802 gboolean read_new_line(FILE_T fh, gint64 *offset, gint *length,
803                        gchar *linebuff, size_t linebuffsize)
804 {
805     char *result;
806
807     /* Read in a line */
808     result = file_gets(linebuff, linebuffsize - 1, fh);
809     if (result == NULL) {
810         /* No characters found, or error */
811         return FALSE;
812     }
813
814     /* Set length and offset.. */
815     *length = (gint)strlen(linebuff);
816     *offset = *offset + *length;
817
818     /* ...but don't want to include newline in line length */
819     if (linebuff[*length-1] == '\n') {
820         linebuff[*length-1] = '\0';
821         *length = *length - 1;
822     }
823
824     return TRUE;
825 }
826
827
828 /**********************************************************************/
829 /* Parse a line from buffer, by identifying:                          */
830 /* - context, port and direction of packet                            */
831 /* - timestamp                                                        */
832 /* - data position and length                                         */
833 /* Return TRUE if this packet looks valid and can be displayed        */
834 /**********************************************************************/
835 static gboolean parse_line(gchar *linebuff, gint line_length,
836                            gint *seconds, gint *useconds,
837                            long *before_time_offset, long *after_time_offset,
838                            long *data_offset, gint *data_chars,
839                            packet_direction_t *direction,
840                            int *encap, int *is_comment,
841                            gchar *aal_header_chars,
842                            gchar *context_name, guint8 *context_portp,
843                            gchar *protocol_name, gchar *variant_name,
844                            gchar *outhdr_name)
845 {
846     int  n = 0;
847     int  port_digits = 0;
848     char port_number_string[MAX_PORT_DIGITS+1];
849     int  variant_digits = 0;
850     int  variant = 1;
851     int  protocol_chars = 0;
852     int  outhdr_chars = 0;
853
854     char seconds_buff[MAX_SECONDS_CHARS+1];
855     int  seconds_chars;
856     char subsecond_decimals_buff[MAX_SUBSECOND_DECIMALS+1];
857     int  subsecond_decimals_chars;
858     int  skip_first_byte = FALSE;
859     gboolean atm_header_present = FALSE;
860
861     *is_comment = FALSE;
862
863     /* Read context name until find '.' */
864     for (n=0; (linebuff[n] != '.') && (n < MAX_CONTEXT_NAME) && (n+1 < line_length); n++) {
865         if (linebuff[n] == '/') {
866             context_name[n] = '\0';
867
868             /* If not a comment (/////), not a valid line */
869             if (strncmp(linebuff+n, "/////", 5) != 0) {
870                 return FALSE;
871             }
872
873             /* There is no variant, outhdr, etc.  Set protocol to be a comment */
874             g_snprintf(protocol_name, MAX_PROTOCOL_NAME, "comment");
875             *is_comment = TRUE;
876             break;
877         }
878         if (!isalnum((guchar)linebuff[n]) && (linebuff[n] != '_') && (linebuff[n] != '-')) {
879             return FALSE;
880         }
881         context_name[n] = linebuff[n];
882     }
883     if (n == MAX_CONTEXT_NAME || (n+1 >= line_length)) {
884         return FALSE;
885     }
886
887     /* Reset strings (that won't be set be comments) */
888     g_strlcpy(variant_name, "0", MAX_VARIANT_DIGITS);
889     g_strlcpy(outhdr_name, "", MAX_OUTHDR_NAME);
890     port_digits = 1;
891     g_strlcpy(port_number_string, "0", MAX_PORT_DIGITS);
892
893     if (!(*is_comment)) {
894         /* '.' must follow context name */
895         if (linebuff[n] != '.') {
896             return FALSE;
897         }
898         context_name[n] = '\0';
899         /* Skip it */
900         n++;
901
902         /* Now read port number */
903         for (port_digits = 0;
904              (linebuff[n] != '/') && (port_digits <= MAX_PORT_DIGITS) && (n+1 < line_length);
905              n++, port_digits++) {
906
907             if (!isdigit((guchar)linebuff[n])) {
908                 return FALSE;
909             }
910             port_number_string[port_digits] = linebuff[n];
911         }
912         if (port_digits > MAX_PORT_DIGITS || (n+1 >= line_length)) {
913             return FALSE;
914         }
915
916         /* Slash char must follow port number */
917         if (linebuff[n] != '/')
918         {
919             return FALSE;
920         }
921         port_number_string[port_digits] = '\0';
922         *context_portp = atoi(port_number_string);
923         /* Skip it */
924         n++;
925
926         /* Now for the protocol name */
927         for (protocol_chars = 0;
928              (linebuff[n] != '/') && (protocol_chars < MAX_PROTOCOL_NAME) && (n < line_length);
929              n++, protocol_chars++) {
930
931             if (!isalnum((guchar)linebuff[n]) && linebuff[n] != '_') {
932                 return FALSE;
933             }
934             protocol_name[protocol_chars] = linebuff[n];
935         }
936         if (protocol_chars == MAX_PROTOCOL_NAME || n >= line_length) {
937             /* If doesn't fit, fail rather than truncate */
938             return FALSE;
939         }
940         protocol_name[protocol_chars] = '\0';
941
942         /* Slash char must follow protocol name */
943         if (linebuff[n] != '/') {
944             return FALSE;
945         }
946         /* Skip it */
947         n++;
948
949
950         /* Following the / is the variant number.  No digits indicate 1 */
951         for (variant_digits = 0;
952              (isdigit((guchar)linebuff[n])) && (variant_digits <= MAX_VARIANT_DIGITS) && (n+1 < line_length);
953              n++, variant_digits++) {
954
955             if (!isdigit((guchar)linebuff[n])) {
956                 return FALSE;
957             }
958             variant_name[variant_digits] = linebuff[n];
959         }
960         if (variant_digits > MAX_VARIANT_DIGITS || (n+1 >= line_length)) {
961             return FALSE;
962         }
963         if (variant_digits > 0) {
964             variant_name[variant_digits] = '\0';
965             variant = atoi(variant_name);
966         }
967         else {
968             g_strlcpy(variant_name, "1", MAX_VARIANT_DIGITS+1);
969         }
970
971
972         /* Outheader values may follow */
973         outhdr_name[0] = '\0';
974         if (linebuff[n] == ',') {
975             /* Skip , */
976             n++;
977
978             for (outhdr_chars = 0;
979                  (isdigit((guchar)linebuff[n]) || linebuff[n] == ',') &&
980                  (outhdr_chars <= MAX_OUTHDR_NAME) && (n+1 < line_length);
981                  n++, outhdr_chars++) {
982
983                 if (!isdigit((guchar)linebuff[n]) && (linebuff[n] != ',')) {
984                     return FALSE;
985                 }
986                 outhdr_name[outhdr_chars] = linebuff[n];
987             }
988             if (outhdr_chars > MAX_OUTHDR_NAME || (n+1 >= line_length)) {
989                 return FALSE;
990             }
991             /* Terminate (possibly empty) string */
992             outhdr_name[outhdr_chars] = '\0';
993         }
994     }
995
996
997     /******************************************************************/
998     /* Now check whether we know how to use a packet of this protocol */
999
1000     if ((strcmp(protocol_name, "ip") == 0) ||
1001         (strcmp(protocol_name, "sctp") == 0) ||
1002         (strcmp(protocol_name, "gre") == 0) ||
1003         (strcmp(protocol_name, "mipv6") == 0) ||
1004         (strcmp(protocol_name, "igmp") == 0)) {
1005
1006         *encap = WTAP_ENCAP_RAW_IP;
1007     }
1008     else
1009
1010     /* FP may be carried over ATM, which has separate atm header to parse */
1011     if ((strcmp(protocol_name, "fp") == 0) ||
1012         (strcmp(protocol_name, "fp_r4") == 0) ||
1013         (strcmp(protocol_name, "fp_r5") == 0) ||
1014         (strcmp(protocol_name, "fp_r6") == 0) ||
1015         (strcmp(protocol_name, "fp_r7") == 0)) {
1016
1017         if ((variant > 256) && (variant % 256 == 3)) {
1018             /* FP over udp is contained in IPPrim... */
1019             *encap = 0;
1020         }
1021         else {
1022             /* FP over AAL0 or AAL2 */
1023             *encap = WTAP_ENCAP_ATM_PDUS_UNTRUNCATED;
1024             atm_header_present = TRUE;
1025         }
1026     }
1027     else if (strcmp(protocol_name, "fpiur_r5") == 0) {
1028         /* FP (IuR) over AAL2 */
1029         *encap = WTAP_ENCAP_ATM_PDUS_UNTRUNCATED;
1030         atm_header_present = TRUE;
1031     }
1032
1033
1034     else
1035     if (strcmp(protocol_name, "ppp") == 0) {
1036         *encap = WTAP_ENCAP_PPP;
1037     }
1038     else
1039     if (strcmp(protocol_name, "isdn_l3") == 0) {
1040        /* TODO: find out what this byte means... */
1041         skip_first_byte = TRUE;
1042         *encap = WTAP_ENCAP_ISDN;
1043     }
1044     else
1045     if (strcmp(protocol_name, "isdn_l2") == 0) {
1046         *encap = WTAP_ENCAP_ISDN;
1047     }
1048     else
1049     if (strcmp(protocol_name, "ethernet") == 0) {
1050         *encap = WTAP_ENCAP_ETHERNET;
1051     }
1052     else
1053     if ((strcmp(protocol_name, "saalnni_sscop") == 0) ||
1054         (strcmp(protocol_name, "saaluni_sscop") == 0)) {
1055
1056         *encap = DCT2000_ENCAP_SSCOP;
1057     }
1058     else
1059     if (strcmp(protocol_name, "frelay_l2") == 0) {
1060         *encap = WTAP_ENCAP_FRELAY;
1061     }
1062     else
1063     if (strcmp(protocol_name, "ss7_mtp2") == 0) {
1064         *encap = DCT2000_ENCAP_MTP2;
1065     }
1066     else
1067     if ((strcmp(protocol_name, "nbap") == 0) ||
1068         (strcmp(protocol_name, "nbap_r4") == 0) ||
1069         (strncmp(protocol_name, "nbap_sscfuni", strlen("nbap_sscfuni")) == 0)) {
1070
1071         /* The entire message in these cases is nbap, so use an encap value. */
1072         *encap = DCT2000_ENCAP_NBAP;
1073     }
1074     else {
1075         /* Not a supported board port protocol/encap, but can show as raw data or
1076            in some cases find protocol embedded inside primitive */
1077         *encap = DCT2000_ENCAP_UNHANDLED;
1078     }
1079
1080
1081     /* Find separate ATM header if necessary */
1082     if (atm_header_present) {
1083         int header_chars_seen = 0;
1084
1085         /* Scan ahead to the next $ */
1086         for (; (linebuff[n] != '$') && (n+1 < line_length); n++);
1087         /* Skip it */
1088         n++;
1089         if (n+1 >= line_length) {
1090             return FALSE;
1091         }
1092
1093         /* Read consecutive hex chars into atm header buffer */
1094         for (;
1095              ((linebuff[n] >= '0') && (linebuff[n] <= '?') &&
1096               (n < line_length) &&
1097               (header_chars_seen < AAL_HEADER_CHARS));
1098              n++, header_chars_seen++) {
1099
1100             aal_header_chars[header_chars_seen] = linebuff[n];
1101             /* Next 6 characters after '9' are mapped to a->f */
1102             if (!isdigit((guchar)linebuff[n])) {
1103                 aal_header_chars[header_chars_seen] = 'a' + (linebuff[n] - '9') -1;
1104             }
1105         }
1106
1107         if (header_chars_seen != AAL_HEADER_CHARS || n >= line_length) {
1108             return FALSE;
1109         }
1110     }
1111
1112     /* Scan ahead to the next space */
1113     for (; (linebuff[n] != ' ') && (n+1 < line_length); n++);
1114     if (n+1 >= line_length) {
1115         return FALSE;
1116     }
1117     /* Skip it */
1118     n++;
1119
1120     /* Next character gives direction of message (must be 's' or 'r') */
1121     if (!(*is_comment)) {
1122         if (linebuff[n] == 's') {
1123             *direction = sent;
1124         }
1125         else
1126         if (linebuff[n] == 'r') {
1127             *direction = received;
1128         }
1129         else {
1130             return FALSE;
1131         }
1132         /* Skip it */
1133         n++;
1134     }
1135     else {
1136         *direction = received;
1137     }
1138
1139
1140     /*********************************************************************/
1141     /* Find and read the timestamp                                       */
1142
1143     /* Now scan to the next digit, which should be the start of the timestamp */
1144     /* This will involve skipping " tm "                                      */
1145
1146     for (; ((linebuff[n] != 't') || (linebuff[n+1] != 'm')) && (n+1 < line_length); n++);
1147     if (n >= line_length) {
1148         return FALSE;
1149     }
1150
1151     for (; !isdigit((guchar)linebuff[n]) && (n < line_length); n++);
1152     if (n >= line_length) {
1153         return FALSE;
1154     }
1155
1156     *before_time_offset = n;
1157
1158     /* Seconds */
1159     for (seconds_chars = 0;
1160          (linebuff[n] != '.') &&
1161          (seconds_chars <= MAX_SECONDS_CHARS) &&
1162          (n < line_length);
1163          n++, seconds_chars++) {
1164
1165         if (!isdigit((guchar)linebuff[n])) {
1166             /* Found a non-digit before decimal point. Fail */
1167             return FALSE;
1168         }
1169         seconds_buff[seconds_chars] = linebuff[n];
1170     }
1171     if (seconds_chars > MAX_SECONDS_CHARS || n >= line_length) {
1172         /* Didn't fit in buffer.  Fail rather than use truncated */
1173         return FALSE;
1174     }
1175
1176     /* Convert found value into number */
1177     seconds_buff[seconds_chars] = '\0';
1178     *seconds = atoi(seconds_buff);
1179
1180     /* The decimal point must follow the last of the seconds digits */
1181     if (linebuff[n] != '.') {
1182         return FALSE;
1183     }
1184     /* Skip it */
1185     n++;
1186
1187     /* Subsecond decimal digits (expect 4-digit accuracy) */
1188     for (subsecond_decimals_chars = 0;
1189          (linebuff[n] != ' ') &&
1190          (subsecond_decimals_chars <= MAX_SUBSECOND_DECIMALS) &&
1191          (n < line_length);
1192          n++, subsecond_decimals_chars++) {
1193
1194         if (!isdigit((guchar)linebuff[n])) {
1195             return FALSE;
1196         }
1197         subsecond_decimals_buff[subsecond_decimals_chars] = linebuff[n];
1198     }
1199     if (subsecond_decimals_chars > MAX_SUBSECOND_DECIMALS || n >= line_length) {
1200         /* More numbers than expected - give up */
1201         return FALSE;
1202     }
1203     /* Convert found value into microseconds */
1204     subsecond_decimals_buff[subsecond_decimals_chars] = '\0';
1205     *useconds = atoi(subsecond_decimals_buff) * 100;
1206
1207     /* Space character must follow end of timestamp */
1208     if (linebuff[n] != ' ') {
1209         return FALSE;
1210     }
1211
1212     *after_time_offset = n;
1213
1214     /* Now skip ahead to find start of data (marked by '$') */
1215     /* Want to avoid matching with normal sprint command output at the moment... */
1216     for (; (linebuff[n] != '$') && (linebuff[n] != '\'') && (n+1 < line_length); n++);
1217     if ((linebuff[n] == '\'') || (n+1 >= line_length)) {
1218         return FALSE;
1219     }
1220     /* Skip it */
1221     n++;
1222
1223     /* Set offset to data start within line */
1224     *data_offset = n;
1225
1226     /* Set number of chars that comprise the hex string protocol data */
1227     *data_chars = line_length - n;
1228
1229     /* May need to skip first byte (2 hex string chars) */
1230     if (skip_first_byte) {
1231         *data_offset += 2;
1232         *data_chars -= 2;
1233     }
1234
1235     return TRUE;
1236 }
1237
1238 /*****************************************************************/
1239 /* Write the stub info to the data buffer while reading a packet */
1240 /*****************************************************************/
1241 static int write_stub_header(guchar *frame_buffer, char *timestamp_string,
1242                              packet_direction_t direction, int encap,
1243                              gchar *context_name, guint8 context_port,
1244                              gchar *protocol_name, gchar *variant_name,
1245                              gchar *outhdr_name)
1246 {
1247     int stub_offset = 0;
1248
1249     g_strlcpy((char*)frame_buffer, context_name, MAX_CONTEXT_NAME+1);
1250     stub_offset += (int)(strlen(context_name) + 1);
1251
1252     /* Context port number */
1253     frame_buffer[stub_offset] = context_port;
1254     stub_offset++;
1255
1256     /* Timestamp within file */
1257     g_strlcpy((char*)&frame_buffer[stub_offset], timestamp_string, MAX_TIMESTAMP_LEN+1);
1258     stub_offset += (int)(strlen(timestamp_string) + 1);
1259
1260     /* Protocol name */
1261     g_strlcpy((char*)&frame_buffer[stub_offset], protocol_name, MAX_PROTOCOL_NAME+1);
1262     stub_offset += (int)(strlen(protocol_name) + 1);
1263
1264     /* Protocol variant number (as string) */
1265     g_strlcpy((void*)&frame_buffer[stub_offset], variant_name, MAX_VARIANT_DIGITS+1);
1266     stub_offset += (int)(strlen(variant_name) + 1);
1267
1268     /* Outhdr */
1269     g_strlcpy((char*)&frame_buffer[stub_offset], outhdr_name, MAX_OUTHDR_NAME+1);
1270     stub_offset += (int)(strlen(outhdr_name) + 1);
1271
1272     /* Direction */
1273     frame_buffer[stub_offset] = direction;
1274     stub_offset++;
1275
1276     /* Encap */
1277     frame_buffer[stub_offset] = (guint8)encap;
1278     stub_offset++;
1279
1280     return stub_offset;
1281 }
1282
1283
1284 /**************************************************************/
1285 /* Set pseudo-header info depending upon packet encapsulation */
1286 /**************************************************************/
1287 static void set_pseudo_header_info(wtap *wth,
1288                                    int pkt_encap,
1289                                    gint64 file_offset,
1290                                    union wtap_pseudo_header *pseudo_header,
1291                                    packet_direction_t direction,
1292                                    gchar *aal_header_chars)
1293 {
1294     pseudo_header->dct2000.seek_off = file_offset;
1295     pseudo_header->dct2000.wth = wth;
1296
1297     switch (pkt_encap) {
1298         case WTAP_ENCAP_ATM_PDUS_UNTRUNCATED:
1299             set_aal_info(pseudo_header, direction, aal_header_chars);
1300             break;
1301         case WTAP_ENCAP_ISDN:
1302             set_isdn_info(pseudo_header, direction);
1303             break;
1304         case WTAP_ENCAP_PPP:
1305             set_ppp_info(pseudo_header, direction);
1306             break;
1307
1308         default:
1309             /* Other supported types don't need to set anything here... */
1310             break;
1311     }
1312 }
1313
1314
1315 /*********************************************/
1316 /* Fill in atm pseudo-header with known info */
1317 /*********************************************/
1318 static void set_aal_info(union wtap_pseudo_header *pseudo_header,
1319                          packet_direction_t direction,
1320                          gchar *aal_header_chars)
1321 {
1322     /* 'aal_head_chars' has this format (for AAL2 at least):
1323        Global Flow Control (4 bits) | VPI (8 bits) | VCI (16 bits) |
1324        Payload Type (4 bits) | Padding (3 bits?) | Link? (1 bit) |
1325        Channel Identifier (8 bits) | ...
1326     */
1327
1328     /* Indicate that this is a reassembled PDU */
1329     pseudo_header->dct2000.inner_pseudo_header.atm.flags = 0x00;
1330
1331     /* Channel 0 is DTE->DCE, 1 is DCE->DTE. Always set 0 for now.
1332        TODO: Can we infer the correct value here?
1333        Meanwhile, just use the direction to make them distinguishable...
1334     */
1335     pseudo_header->dct2000.inner_pseudo_header.atm.channel = (direction == received);
1336
1337     /* Assume always AAL2 for FP */
1338     pseudo_header->dct2000.inner_pseudo_header.atm.aal = AAL_2;
1339
1340     pseudo_header->dct2000.inner_pseudo_header.atm.type = TRAF_UMTS_FP;
1341     pseudo_header->dct2000.inner_pseudo_header.atm.subtype = TRAF_ST_UNKNOWN;
1342
1343     /* vpi is 8 bits (2nd & 3rd nibble) */
1344     pseudo_header->dct2000.inner_pseudo_header.atm.vpi =
1345         ((hex_from_char(aal_header_chars[1]) << 4) |
1346           hex_from_char(aal_header_chars[2]));
1347
1348     /* vci is next 16 bits */
1349     pseudo_header->dct2000.inner_pseudo_header.atm.vci =
1350         ((hex_from_char(aal_header_chars[3]) << 12) |
1351          (hex_from_char(aal_header_chars[4]) << 8) |
1352          (hex_from_char(aal_header_chars[5]) << 4) |
1353          hex_from_char(aal_header_chars[6]));
1354
1355     /* 0 means we don't know how many cells the frame comprises. */
1356     pseudo_header->dct2000.inner_pseudo_header.atm.cells = 0;
1357
1358     /* cid is usually last byte.  Unless last char is not hex digit, in which
1359        case cid is derived from last char in ascii */
1360     if (isalnum((guchar)aal_header_chars[11])) {
1361         pseudo_header->dct2000.inner_pseudo_header.atm.aal2_cid =
1362             ((hex_from_char(aal_header_chars[10]) << 4) |
1363               hex_from_char(aal_header_chars[11]));
1364     }
1365     else {
1366         pseudo_header->dct2000.inner_pseudo_header.atm.aal2_cid =
1367             (int)aal_header_chars[11] - 48;
1368     }
1369 }
1370
1371
1372 /**********************************************/
1373 /* Fill in isdn pseudo-header with known info */
1374 /**********************************************/
1375 void set_isdn_info(union wtap_pseudo_header *pseudo_header,
1376                    packet_direction_t direction)
1377 {
1378     /* This field is used to set the 'Source' and 'Destination' columns to
1379        'User' or 'Network'. If we assume that we're simulating the network,
1380        treat Received messages as being destined for the network.
1381     */
1382     pseudo_header->dct2000.inner_pseudo_header.isdn.uton = (direction == received);
1383
1384     /* This corresponds to the circuit ID.  0 is treated as LAPD,
1385        everything else would be treated as a B-channel
1386     */
1387     pseudo_header->dct2000.inner_pseudo_header.isdn.channel = 0;
1388 }
1389
1390
1391 /*********************************************/
1392 /* Fill in ppp pseudo-header with known info */
1393 /*********************************************/
1394 static void set_ppp_info(union wtap_pseudo_header *pseudo_header,
1395                          packet_direction_t direction)
1396 {
1397     /* Set direction. */
1398     pseudo_header->dct2000.inner_pseudo_header.p2p.sent = (direction == sent);
1399 }
1400
1401
1402 /********************************************************/
1403 /* Return hex nibble equivalent of hex string character */
1404 /********************************************************/
1405 guchar hex_from_char(gchar c)
1406 {
1407     if ((c >= '0') && (c <= '9')) {
1408         return c - '0';
1409     }
1410
1411     if ((c >= 'a') && (c <= 'f')) {
1412         return 0x0a + (c - 'a');
1413     }
1414
1415     /* Not a valid hex string character */
1416     return 0xff;
1417 }
1418
1419
1420 /********************************************************/
1421 /* Return character corresponding to hex nibble value   */
1422 /********************************************************/
1423 gchar char_from_hex(guchar hex)
1424 {
1425     static char hex_lookup[16] =
1426     { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
1427
1428     if (hex > 15) {
1429         return '?';
1430     }
1431
1432     return hex_lookup[hex];
1433 }
1434
1435 /***********************************************/
1436 /* Equality test for packet prefix hash tables */
1437 /***********************************************/
1438 gint packet_offset_equal(gconstpointer v, gconstpointer v2)
1439 {
1440     /* Dereferenced pointers must have same gint64 offset value */
1441     return (*(const gint64*)v == *(const gint64*)v2);
1442 }
1443
1444
1445 /********************************************/
1446 /* Hash function for packet-prefix hash table */
1447 /********************************************/
1448 guint packet_offset_hash_func(gconstpointer v)
1449 {
1450     /* Use low-order bits of git64 offset value */
1451     return (guint)(*(const gint64*)v);
1452 }
1453
1454
1455 /************************************************************************/
1456 /* Parse year, month, day, hour, minute, seconds out of formatted line. */
1457 /* Set secs and usecs as output                                         */
1458 /* Return FALSE if no valid time can be read                            */
1459 /************************************************************************/
1460 gboolean get_file_time_stamp(gchar *linebuff, time_t *secs, guint32 *usecs)
1461 {
1462     int n;
1463     struct tm tm;
1464     #define MAX_MONTH_LETTERS 9
1465     char month[MAX_MONTH_LETTERS+1];
1466
1467     int day, year, hour, minute, second;
1468     int scan_found;
1469
1470     /* If line longer than expected, file is probably not correctly formatted */
1471     if (strlen(linebuff) > MAX_TIMESTAMP_LINE_LENGTH) {
1472         return FALSE;
1473     }
1474
1475     /**************************************************************/
1476     /* First is month. Read until get a space following the month */
1477     for (n=0; (linebuff[n] != ' ') && (n < MAX_MONTH_LETTERS); n++) {
1478         month[n] = linebuff[n];
1479     }
1480     month[n] = '\0';
1481
1482     if      (strcmp(month, "January"  ) == 0)  tm.tm_mon = 0;
1483     else if (strcmp(month, "February" ) == 0)  tm.tm_mon = 1;
1484     else if (strcmp(month, "March"    ) == 0)  tm.tm_mon = 2;
1485     else if (strcmp(month, "April"    ) == 0)  tm.tm_mon = 3;
1486     else if (strcmp(month, "May"      ) == 0)  tm.tm_mon = 4;
1487     else if (strcmp(month, "June"     ) == 0)  tm.tm_mon = 5;
1488     else if (strcmp(month, "July"     ) == 0)  tm.tm_mon = 6;
1489     else if (strcmp(month, "August"   ) == 0)  tm.tm_mon = 7;
1490     else if (strcmp(month, "September") == 0)  tm.tm_mon = 8;
1491     else if (strcmp(month, "October"  ) == 0)  tm.tm_mon = 9;
1492     else if (strcmp(month, "November" ) == 0)  tm.tm_mon = 10;
1493     else if (strcmp(month, "December" ) == 0)  tm.tm_mon = 11;
1494     else {
1495         /* Give up if not found a properly-formatted date */
1496         return FALSE;
1497     }
1498     /* Skip space char */
1499     n++;
1500
1501     /********************************************************/
1502     /* Scan for remaining numerical fields                  */
1503     scan_found = sscanf(linebuff+n, "%d, %d     %d:%d:%d.%u",
1504                         &day, &year, &hour, &minute, &second, usecs);
1505     if (scan_found != 6) {
1506         /* Give up if not all found */
1507         return FALSE;
1508     }
1509
1510     /******************************************************/
1511     /* Fill in remaining fields and return it in a time_t */
1512     tm.tm_year = year - 1900;
1513     tm.tm_mday = day;
1514     tm.tm_hour = hour;
1515     tm.tm_min = minute;
1516     tm.tm_sec = second;
1517     tm.tm_isdst = -1;    /* daylight saving time info not known */
1518
1519     /* Get seconds from this time */
1520     *secs = mktime(&tm);
1521
1522     /* Multiply 4 digits given to get micro-seconds */
1523     *usecs = *usecs * 100;
1524
1525     return TRUE;
1526 }
1527
1528 /* Free the data allocated inside a line_prefix_info_t */
1529 gboolean free_line_prefix_info(gpointer key, gpointer value,
1530                                gpointer user_data _U_)
1531 {
1532     line_prefix_info_t *info = (line_prefix_info_t*)value;
1533
1534     /* Free the 64-bit key value */
1535     g_free(key);
1536
1537     /* Free the strings inside */
1538     g_free(info->before_time);
1539     if (info->after_time) {
1540         g_free(info->after_time);
1541     }
1542
1543     /* And the structure itself */
1544     g_free(info);
1545
1546     /* Item will always be removed from table */
1547     return TRUE;
1548 }
1549