]> git.samba.org - metze/wireshark/wip.git/blob - wiretap/network_instruments.c
Clean up 802.11 radio information handling.
[metze/wireshark/wip.git] / wiretap / network_instruments.c
1 /***************************************************************************
2                           network_instruments.c  -  description
3                              -------------------
4     begin                : Wed Oct 29 2003
5     copyright            : (C) 2003 by root
6     email                : scotte[AT}netinst.com
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18 #include "config.h"
19
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <string.h>
23 #include "wtap-int.h"
24 #include "file_wrappers.h"
25 #include "network_instruments.h"
26
27 static const char network_instruments_magic[] = {"ObserverPktBufferVersion=15.00"};
28 static const int true_magic_length = 17;
29
30 static const guint32 observer_packet_magic = 0x88888888;
31
32 /*
33  * This structure is used to keep state when writing files. An instance is
34  * allocated for each file, and its address is stored in the wtap_dumper.priv
35  * pointer field.
36  */
37 typedef struct {
38     guint64 packet_count;
39     guint8  network_type;
40     guint32 time_format;
41 } observer_dump_private_state;
42
43 /*
44  * Some time offsets are calculated in advance here, when the first Observer
45  * file is opened for reading or writing, and are then used to adjust frame
46  * timestamps as they are read or written.
47  *
48  * The Wiretap API expects timestamps in nanoseconds relative to
49  * January 1, 1970, 00:00:00 GMT (the Wiretap epoch).
50  *
51  * Observer versions before 13.10 encode frame timestamps in nanoseconds
52  * relative to January 1, 2000, 00:00:00 local time (the Observer epoch).
53  * Versions 13.10 and later switch over to GMT encoding. Which encoding was used
54  * when saving the file is identified via the time format TLV following
55  * the file header.
56  *
57  * Unfortunately, even though Observer versions before 13.10 saved in local
58  * time, they didn't include the timezone from which the frames were captured,
59  * so converting to GMT correctly from all timezones is impossible. So an
60  * assumption is made that the file is being read from within the same timezone
61  * that it was written.
62  *
63  * All code herein is normalized to versions 13.10 and later, special casing for
64  * versions earlier. In other words, timestamps are worked with as if
65  * they are GMT-encoded, and adjustments from local time are made only if
66  * the source file warrants it.
67  *
68  * All destination files are saved in GMT format.
69  */
70 static const time_t ansi_to_observer_epoch_offset = 946684800;
71 static time_t gmt_to_localtime_offset = (time_t) -1;
72
73 static void init_gmt_to_localtime_offset(void)
74 {
75     if (gmt_to_localtime_offset == (time_t) -1) {
76         time_t ansi_epoch_plus_one_day = 86400;
77         struct tm gmt_tm;
78         struct tm local_tm;
79
80         /*
81          * Compute the local time zone offset as the number of seconds west
82          * of GMT. There's no obvious cross-platform API for querying this
83          * directly. As a workaround, GMT and local tm structures are populated
84          * relative to the ANSI time_t epoch (plus one day to ensure that
85          * local time stays after 1970/1/1 00:00:00). They are then converted
86          * back to time_t as if they were both local times, resulting in the
87          * time zone offset being the difference between them.
88          */
89         gmt_tm = *gmtime(&ansi_epoch_plus_one_day);
90         local_tm = *localtime(&ansi_epoch_plus_one_day);
91         local_tm.tm_isdst = 0;
92         gmt_to_localtime_offset = mktime(&gmt_tm) - mktime(&local_tm);
93     }
94 }
95
96 static gboolean observer_read(wtap *wth, int *err, gchar **err_info,
97     gint64 *data_offset);
98 static gboolean observer_seek_read(wtap *wth, gint64 seek_off,
99     struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
100 static int read_packet_header(wtap *wth, FILE_T fh, union wtap_pseudo_header *pseudo_header,
101     packet_entry_header *packet_header, int *err, gchar **err_info);
102 static gboolean process_packet_header(wtap *wth,
103     packet_entry_header *packet_header, struct wtap_pkthdr *phdr, int *err,
104     gchar **err_info);
105 static int read_packet_data(FILE_T fh, int offset_to_frame, int current_offset_from_packet_header,
106     Buffer *buf, int length, int *err, char **err_info);
107 static gboolean skip_to_next_packet(wtap *wth, int offset_to_next_packet,
108     int current_offset_from_packet_header, int *err, char **err_info);
109 static gboolean observer_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
110     const guint8 *pd, int *err, gchar **err_info);
111 static gint observer_to_wtap_encap(int observer_encap);
112 static gint wtap_to_observer_encap(int wtap_encap);
113
114 wtap_open_return_val network_instruments_open(wtap *wth, int *err, gchar **err_info)
115 {
116     int offset;
117     capture_file_header file_header;
118     guint i;
119     tlv_header tlvh;
120     int seek_increment;
121     int header_offset;
122     packet_entry_header packet_header;
123     observer_dump_private_state * private_state = NULL;
124
125     offset = 0;
126
127     /* read in the buffer file header */
128     if (!wtap_read_bytes(wth->fh, &file_header, sizeof file_header,
129                          err, err_info)) {
130         if (*err != WTAP_ERR_SHORT_READ)
131             return WTAP_OPEN_ERROR;
132         return WTAP_OPEN_NOT_MINE;
133     }
134     offset += (int)sizeof file_header;
135     CAPTURE_FILE_HEADER_FROM_LE_IN_PLACE(file_header);
136
137     /* check if version info is present */
138     if (memcmp(file_header.observer_version, network_instruments_magic, true_magic_length)!=0) {
139         return WTAP_OPEN_NOT_MINE;
140     }
141
142     /* initialize the private state */
143     private_state = (observer_dump_private_state *) g_malloc(sizeof(observer_dump_private_state));
144     private_state->time_format = TIME_INFO_LOCAL;
145     wth->priv = (void *) private_state;
146
147     /* get the location of the first packet */
148     /* v15 and newer uses high byte offset, in previous versions it will be 0 */
149     header_offset = file_header.offset_to_first_packet + ((int)(file_header.offset_to_first_packet_high_byte)<<16);
150
151     /* process extra information */
152     for (i = 0; i < file_header.number_of_information_elements; i++) {
153         /* for safety break if we've reached the first packet */
154         if (offset >= header_offset)
155             break;
156
157         /* read the TLV header */
158         if (!wtap_read_bytes(wth->fh, &tlvh, sizeof tlvh, err, err_info))
159             return WTAP_OPEN_ERROR;
160         offset += (int)sizeof tlvh;
161         TLV_HEADER_FROM_LE_IN_PLACE(tlvh);
162
163         if (tlvh.length < sizeof tlvh) {
164             *err = WTAP_ERR_BAD_FILE;
165             *err_info = g_strdup_printf("Observer: bad record (TLV length %u < %lu)",
166                 tlvh.length, (unsigned long)sizeof tlvh);
167             return WTAP_OPEN_ERROR;
168         }
169
170         /* process (or skip over) the current TLV */
171         switch (tlvh.type) {
172         case INFORMATION_TYPE_TIME_INFO:
173             if (!wtap_read_bytes(wth->fh, &private_state->time_format,
174                                  sizeof private_state->time_format,
175                                  err, err_info))
176                 return WTAP_OPEN_ERROR;
177             private_state->time_format = GUINT32_FROM_LE(private_state->time_format);
178             offset += (int)sizeof private_state->time_format;
179             break;
180         default:
181             seek_increment = tlvh.length - (int)sizeof tlvh;
182             if (seek_increment > 0) {
183                 if (file_seek(wth->fh, seek_increment, SEEK_CUR, err) == -1)
184                     return WTAP_OPEN_ERROR;
185             }
186             offset += seek_increment;
187         }
188     }
189
190     /* get to the first packet */
191     if (header_offset < offset) {
192         *err = WTAP_ERR_BAD_FILE;
193         *err_info = g_strdup_printf("Observer: bad record (offset to first packet %d < %d)",
194             header_offset, offset);
195         return WTAP_OPEN_ERROR;
196     }
197     seek_increment = header_offset - offset;
198     if (seek_increment > 0) {
199         if (file_seek(wth->fh, seek_increment, SEEK_CUR, err) == -1)
200             return WTAP_OPEN_ERROR;
201     }
202
203     /* pull off the packet header */
204     if (!wtap_read_bytes(wth->fh, &packet_header, sizeof packet_header,
205                          err, err_info))
206         return WTAP_OPEN_ERROR;
207     PACKET_ENTRY_HEADER_FROM_LE_IN_PLACE(packet_header);
208
209     /* check the packet's magic number */
210     if (packet_header.packet_magic != observer_packet_magic) {
211         *err = WTAP_ERR_UNSUPPORTED;
212         *err_info = g_strdup_printf("Observer: unsupported packet version %ul", packet_header.packet_magic);
213         return WTAP_OPEN_ERROR;
214     }
215
216     /* check the data link type */
217     if (observer_to_wtap_encap(packet_header.network_type) == WTAP_ENCAP_UNKNOWN) {
218         *err = WTAP_ERR_UNSUPPORTED;
219         *err_info = g_strdup_printf("Observer: network type %u unknown or unsupported", packet_header.network_type);
220         return WTAP_OPEN_ERROR;
221     }
222     wth->file_encap = observer_to_wtap_encap(packet_header.network_type);
223
224     /* set up the rest of the capture parameters */
225     private_state->packet_count = 0;
226     private_state->network_type = wtap_to_observer_encap(wth->file_encap);
227     wth->subtype_read = observer_read;
228     wth->subtype_seek_read = observer_seek_read;
229     wth->subtype_close = NULL;
230     wth->subtype_sequential_close = NULL;
231     wth->snapshot_length = 0;    /* not available in header */
232     wth->file_tsprec = WTAP_TSPREC_NSEC;
233     wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_NETWORK_INSTRUMENTS;
234
235     /* reset the pointer to the first packet */
236     if (file_seek(wth->fh, header_offset, SEEK_SET, err) == -1)
237         return WTAP_OPEN_ERROR;
238
239     init_gmt_to_localtime_offset();
240
241     return WTAP_OPEN_MINE;
242 }
243
244 /* Reads the next packet. */
245 static gboolean observer_read(wtap *wth, int *err, gchar **err_info,
246     gint64 *data_offset)
247 {
248     int header_bytes_consumed;
249     int data_bytes_consumed;
250     packet_entry_header packet_header;
251
252     /* skip records other than data records */
253     for (;;) {
254         *data_offset = file_tell(wth->fh);
255
256         /* process the packet header, including TLVs */
257         header_bytes_consumed = read_packet_header(wth, wth->fh, &wth->phdr.pseudo_header, &packet_header, err,
258             err_info);
259         if (header_bytes_consumed <= 0)
260             return FALSE;    /* EOF or error */
261
262         if (packet_header.packet_type == PACKET_TYPE_DATA_PACKET)
263             break;
264
265         /* skip to next packet */
266         if (!skip_to_next_packet(wth, packet_header.offset_to_next_packet,
267                 header_bytes_consumed, err, err_info)) {
268             return FALSE;    /* EOF or error */
269         }
270     }
271
272     if (!process_packet_header(wth, &packet_header, &wth->phdr, err, err_info))
273         return FALSE;
274
275     /* read the frame data */
276     data_bytes_consumed = read_packet_data(wth->fh, packet_header.offset_to_frame,
277             header_bytes_consumed, wth->frame_buffer,
278             wth->phdr.caplen, err, err_info);
279     if (data_bytes_consumed < 0) {
280         return FALSE;
281     }
282
283     /* skip over any extra bytes following the frame data */
284     if (!skip_to_next_packet(wth, packet_header.offset_to_next_packet,
285             header_bytes_consumed + data_bytes_consumed, err, err_info)) {
286         return FALSE;
287     }
288
289     return TRUE;
290 }
291
292 /* Reads a packet at an offset. */
293 static gboolean observer_seek_read(wtap *wth, gint64 seek_off,
294     struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
295 {
296     union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
297     packet_entry_header packet_header;
298     int offset;
299     int data_bytes_consumed;
300
301     if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
302         return FALSE;
303
304     /* process the packet header, including TLVs */
305     offset = read_packet_header(wth, wth->random_fh, pseudo_header, &packet_header, err,
306         err_info);
307     if (offset <= 0)
308         return FALSE;    /* EOF or error */
309
310     if (!process_packet_header(wth, &packet_header, phdr, err, err_info))
311         return FALSE;
312
313     /* read the frame data */
314     data_bytes_consumed = read_packet_data(wth->random_fh, packet_header.offset_to_frame,
315         offset, buf, phdr->caplen, err, err_info);
316     if (data_bytes_consumed < 0) {
317         return FALSE;
318     }
319
320     return TRUE;
321 }
322
323 static int
324 read_packet_header(wtap *wth, FILE_T fh, union wtap_pseudo_header *pseudo_header,
325     packet_entry_header *packet_header, int *err, gchar **err_info)
326 {
327     int offset;
328     guint i;
329     tlv_header tlvh;
330     int seek_increment;
331     tlv_wireless_info wireless_header;
332
333     offset = 0;
334
335     /* pull off the packet header */
336     if (!wtap_read_bytes_or_eof(fh, packet_header, sizeof *packet_header,
337                                 err, err_info)) {
338         if (*err != 0)
339             return -1;
340         return 0;    /* EOF */
341     }
342     offset += (int)sizeof *packet_header;
343     PACKET_ENTRY_HEADER_FROM_LE_IN_PLACE(*packet_header);
344
345     /* check the packet's magic number */
346     if (packet_header->packet_magic != observer_packet_magic) {
347
348         /*
349          * Some files are zero-padded at the end. There is no warning of this
350          * in the previous packet header information, such as setting
351          * offset_to_next_packet to zero. So detect this situation by treating
352          * an all-zero header as a sentinel. Return EOF when it is encountered,
353          * rather than treat it as a bad record.
354          */
355         for (i = 0; i < sizeof *packet_header; i++) {
356             if (((guint8*) packet_header)[i] != 0)
357                 break;
358         }
359         if (i == sizeof *packet_header) {
360             *err = 0;
361             return 0;    /* EOF */
362         }
363
364         *err = WTAP_ERR_BAD_FILE;
365         *err_info = g_strdup_printf("Observer: bad record: Invalid magic number 0x%08x",
366             packet_header->packet_magic);
367         return -1;
368     }
369
370     /* initialize the pseudo header */
371     switch (wth->file_encap) {
372     case WTAP_ENCAP_ETHERNET:
373         /* There is no FCS in the frame */
374         pseudo_header->eth.fcs_len = 0;
375         break;
376     case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
377         pseudo_header->ieee_802_11.fcs_len = 0;
378         pseudo_header->ieee_802_11.decrypted = FALSE;
379         pseudo_header->ieee_802_11.datapad = FALSE;
380         pseudo_header->ieee_802_11.phy = PHDR_802_11_PHY_UNKNOWN;
381         pseudo_header->ieee_802_11.presence_flags = 0;
382         /* Updated below */
383         break;
384     }
385
386     /* process extra information */
387     for (i = 0; i < packet_header->number_of_information_elements; i++) {
388         /* read the TLV header */
389         if (!wtap_read_bytes(fh, &tlvh, sizeof tlvh, err, err_info))
390             return -1;
391         offset += (int)sizeof tlvh;
392         TLV_HEADER_FROM_LE_IN_PLACE(tlvh);
393
394         if (tlvh.length < sizeof tlvh) {
395             *err = WTAP_ERR_BAD_FILE;
396             *err_info = g_strdup_printf("Observer: bad record (TLV length %u < %lu)",
397                 tlvh.length, (unsigned long)sizeof tlvh);
398             return -1;
399         }
400
401         /* process (or skip over) the current TLV */
402         switch (tlvh.type) {
403         case INFORMATION_TYPE_WIRELESS:
404             if (!wtap_read_bytes(fh, &wireless_header, sizeof wireless_header,
405                                  err, err_info))
406                 return -1;
407             /* update the pseudo header */
408             pseudo_header->ieee_802_11.presence_flags |=
409                 PHDR_802_11_HAS_CHANNEL |
410                 PHDR_802_11_HAS_DATA_RATE |
411                 PHDR_802_11_HAS_SIGNAL_PERCENT;
412             /* set decryption status */
413             /* XXX - what other bits are there in conditions? */
414             pseudo_header->ieee_802_11.decrypted = (wireless_header.conditions & WIRELESS_WEP_SUCCESS) != 0;
415             pseudo_header->ieee_802_11.channel = wireless_header.frequency;
416             pseudo_header->ieee_802_11.data_rate = wireless_header.rate;
417             pseudo_header->ieee_802_11.signal_percent = wireless_header.strengthPercent;
418             offset += (int)sizeof wireless_header;
419             break;
420         default:
421             /* skip the TLV data */
422             seek_increment = tlvh.length - (int)sizeof tlvh;
423             if (seek_increment > 0) {
424                 if (file_seek(fh, seek_increment, SEEK_CUR, err) == -1)
425                     return -1;
426             }
427             offset += seek_increment;
428         }
429     }
430
431     return offset;
432 }
433
434 static gboolean
435 process_packet_header(wtap *wth, packet_entry_header *packet_header,
436     struct wtap_pkthdr *phdr, int *err, gchar **err_info)
437 {
438     /* set the wiretap packet header fields */
439     phdr->rec_type = REC_TYPE_PACKET;
440     phdr->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
441     phdr->pkt_encap = observer_to_wtap_encap(packet_header->network_type);
442     if(wth->file_encap == WTAP_ENCAP_FIBRE_CHANNEL_FC2_WITH_FRAME_DELIMS) {
443         phdr->len = packet_header->network_size;
444         phdr->caplen = packet_header->captured_size;
445     } else {
446         /*
447          * XXX - what are those 4 bytes?
448          *
449          * The comment in the code said "neglect frame markers for wiretap",
450          * but in the captures I've seen, there's no actual data corresponding
451          * to them that might be a "frame marker".
452          *
453          * Instead, the packets had a network_size 4 bytes larger than the
454          * captured_size; does the network_size include the CRC, even
455          * though it's not included in a capture?  If so, most other
456          * network analyzers that have a "network size" and a "captured
457          * size" don't include the CRC in the "network size" if they
458          * don't include the CRC in a full-length captured packet; the
459          * "captured size" is less than the "network size" only if a
460          * user-specified "snapshot length" caused the packet to be
461          * sliced at a particular point.
462          *
463          * That's the model that wiretap and Wireshark/TShark use, so
464          * we implement that model here.
465          */
466         if (packet_header->network_size < 4) {
467             *err = WTAP_ERR_BAD_FILE;
468             *err_info = g_strdup_printf("Observer: bad record: Packet length %u < 4",
469                                         packet_header->network_size);
470             return FALSE;
471         }
472
473         phdr->len = packet_header->network_size - 4;
474         phdr->caplen = MIN(packet_header->captured_size, phdr->len);
475     }
476
477     /* set the wiretap timestamp, assuming for the moment that Observer encoded it in GMT */
478     phdr->ts.secs = (time_t) ((packet_header->nano_seconds_since_2000 / 1000000000) + ansi_to_observer_epoch_offset);
479     phdr->ts.nsecs = (int) (packet_header->nano_seconds_since_2000 % 1000000000);
480
481     /* adjust to local time, if necessary, also accounting for DST if the frame
482        was captured while it was in effect */
483     if (((observer_dump_private_state*)wth->priv)->time_format == TIME_INFO_LOCAL)
484     {
485         struct tm daylight_tm;
486         struct tm standard_tm;
487         time_t    dst_offset;
488
489         /* the Observer timestamp was encoded as local time, so add a
490            correction from local time to GMT */
491         phdr->ts.secs += gmt_to_localtime_offset;
492
493         /* perform a DST adjustment if necessary */
494         standard_tm = *localtime(&phdr->ts.secs);
495         if (standard_tm.tm_isdst > 0) {
496             daylight_tm = standard_tm;
497             standard_tm.tm_isdst = 0;
498             dst_offset = mktime(&standard_tm) - mktime(&daylight_tm);
499             phdr->ts.secs -= dst_offset;
500         }
501     }
502
503     return TRUE;
504 }
505
506 static int
507 read_packet_data(FILE_T fh, int offset_to_frame, int current_offset_from_packet_header, Buffer *buf,
508     int length, int *err, char **err_info)
509 {
510     int seek_increment;
511     int bytes_consumed = 0;
512
513     /* validate offsets */
514     if (offset_to_frame < current_offset_from_packet_header) {
515         *err = WTAP_ERR_BAD_FILE;
516         *err_info = g_strdup_printf("Observer: bad record (offset to packet data %d < %d)",
517             offset_to_frame, current_offset_from_packet_header);
518         return -1;
519     }
520
521     /* skip to the packet data */
522     seek_increment = offset_to_frame - current_offset_from_packet_header;
523     if (seek_increment > 0) {
524         if (file_seek(fh, seek_increment, SEEK_CUR, err) == -1) {
525             return -1;
526         }
527         bytes_consumed += seek_increment;
528     }
529
530     /* set-up the packet buffer */
531     ws_buffer_assure_space(buf, length);
532
533     /* read in the packet data */
534     if (!wtap_read_packet_bytes(fh, buf, length, err, err_info))
535         return FALSE;
536     bytes_consumed += length;
537
538     return bytes_consumed;
539 }
540
541 static gboolean
542 skip_to_next_packet(wtap *wth, int offset_to_next_packet, int current_offset_from_packet_header, int *err,
543     char **err_info)
544 {
545     int seek_increment;
546
547     /* validate offsets */
548     if (offset_to_next_packet < current_offset_from_packet_header) {
549         *err = WTAP_ERR_BAD_FILE;
550         *err_info = g_strdup_printf("Observer: bad record (offset to next packet %d < %d)",
551             offset_to_next_packet, current_offset_from_packet_header);
552         return FALSE;
553     }
554
555     /* skip to the next packet header */
556     seek_increment = offset_to_next_packet - current_offset_from_packet_header;
557     if (seek_increment > 0) {
558         if (file_seek(wth->fh, seek_increment, SEEK_CUR, err) == -1)
559             return FALSE;
560     }
561
562     return TRUE;
563 }
564
565 /* Returns 0 if we could write the specified encapsulation type,
566    an error indication otherwise. */
567 int network_instruments_dump_can_write_encap(int encap)
568 {
569     /* per-packet encapsulations aren't supported */
570     if (encap == WTAP_ENCAP_PER_PACKET)
571         return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
572
573     if (encap < 0 || (wtap_to_observer_encap(encap) == OBSERVER_UNDEFINED))
574         return WTAP_ERR_UNWRITABLE_ENCAP;
575
576     return 0;
577 }
578
579 /* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
580    failure. */
581 gboolean network_instruments_dump_open(wtap_dumper *wdh, int *err)
582 {
583     observer_dump_private_state * private_state = NULL;
584     capture_file_header file_header;
585
586     tlv_header comment_header;
587     tlv_time_info time_header;
588     char comment[64];
589     size_t comment_length;
590     struct tm * current_time;
591     time_t system_time;
592
593     /* initialize the private state */
594     private_state = (observer_dump_private_state *) g_malloc(sizeof(observer_dump_private_state));
595     private_state->packet_count = 0;
596     private_state->network_type = wtap_to_observer_encap(wdh->encap);
597     private_state->time_format = TIME_INFO_GMT;
598
599     /* populate the fields of wdh */
600     wdh->priv = (void *) private_state;
601     wdh->subtype_write = observer_dump;
602
603     /* initialize the file header */
604     memset(&file_header, 0x00, sizeof(file_header));
605     g_strlcpy(file_header.observer_version, network_instruments_magic, 31);
606     file_header.offset_to_first_packet = (guint16)sizeof(file_header);
607     file_header.offset_to_first_packet_high_byte = 0;
608
609     /* create the file comment TLV */
610     {
611         time(&system_time);
612         current_time = localtime(&system_time);
613         memset(&comment, 0x00, sizeof(comment));
614         g_snprintf(comment, 64, "This capture was saved from Wireshark on %s", asctime(current_time));
615         comment_length = strlen(comment);
616
617         comment_header.type = INFORMATION_TYPE_COMMENT;
618         comment_header.length = (guint16) (sizeof(comment_header) + comment_length);
619
620         /* update the file header to account for the comment TLV */
621         file_header.number_of_information_elements++;
622         file_header.offset_to_first_packet += comment_header.length;
623     }
624
625     /* create the timestamp encoding TLV */
626     {
627         time_header.type = INFORMATION_TYPE_TIME_INFO;
628         time_header.length = (guint16) (sizeof(time_header));
629         time_header.time_format = TIME_INFO_GMT;
630
631         /* update the file header to account for the timestamp encoding TLV */
632         file_header.number_of_information_elements++;
633         file_header.offset_to_first_packet += time_header.length;
634     }
635
636     /* write the file header, swapping any multibyte fields first */
637     CAPTURE_FILE_HEADER_TO_LE_IN_PLACE(file_header);
638     if (!wtap_dump_file_write(wdh, &file_header, sizeof(file_header), err)) {
639         return FALSE;
640     }
641     wdh->bytes_dumped += sizeof(file_header);
642
643     /* write the comment TLV */
644     {
645         TLV_HEADER_TO_LE_IN_PLACE(comment_header);
646         if (!wtap_dump_file_write(wdh, &comment_header, sizeof(comment_header), err)) {
647             return FALSE;
648         }
649         wdh->bytes_dumped += sizeof(comment_header);
650
651         if (!wtap_dump_file_write(wdh, &comment, comment_length, err)) {
652             return FALSE;
653         }
654         wdh->bytes_dumped += comment_length;
655     }
656
657     /* write the time info TLV */
658     {
659         TLV_TIME_INFO_TO_LE_IN_PLACE(time_header);
660         if (!wtap_dump_file_write(wdh, &time_header, sizeof(time_header), err)) {
661             return FALSE;
662         }
663         wdh->bytes_dumped += sizeof(time_header);
664     }
665
666     init_gmt_to_localtime_offset();
667
668     return TRUE;
669 }
670
671 /* Write a record for a packet to a dump file.
672    Returns TRUE on success, FALSE on failure. */
673 static gboolean observer_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
674     const guint8 *pd,
675     int *err, gchar **err_info _U_)
676 {
677     observer_dump_private_state * private_state = NULL;
678     packet_entry_header           packet_header;
679     guint64                       seconds_since_2000;
680
681     /* We can only write packet records. */
682     if (phdr->rec_type != REC_TYPE_PACKET) {
683         *err = WTAP_ERR_UNWRITABLE_REC_TYPE;
684         return FALSE;
685     }
686
687     /* The captured size field is 16 bits, so there's a hard limit of
688        65535. */
689     if (phdr->caplen > 65535) {
690         *err = WTAP_ERR_PACKET_TOO_LARGE;
691         return FALSE;
692     }
693
694     /* convert the number of seconds since epoch from ANSI-relative to
695        Observer-relative */
696     if (phdr->ts.secs < ansi_to_observer_epoch_offset) {
697         if(phdr->ts.secs > (time_t) 0) {
698             seconds_since_2000 = phdr->ts.secs;
699         } else {
700             seconds_since_2000 = (time_t) 0;
701         }
702     } else {
703         seconds_since_2000 = phdr->ts.secs - ansi_to_observer_epoch_offset;
704     }
705
706     /* populate the fields of the packet header */
707     private_state = (observer_dump_private_state *) wdh->priv;
708
709     memset(&packet_header, 0x00, sizeof(packet_header));
710     packet_header.packet_magic = observer_packet_magic;
711     packet_header.network_speed = 1000000;
712     packet_header.captured_size = (guint16) phdr->caplen;
713     packet_header.network_size = (guint16) (phdr->len + 4);
714     packet_header.offset_to_frame = sizeof(packet_header);
715     /* XXX - what if this doesn't fit in 16 bits?  It's not guaranteed to... */
716     packet_header.offset_to_next_packet = (guint16)sizeof(packet_header) + phdr->caplen;
717     packet_header.network_type = private_state->network_type;
718     packet_header.flags = 0x00;
719     packet_header.number_of_information_elements = 0;
720     packet_header.packet_type = PACKET_TYPE_DATA_PACKET;
721     packet_header.packet_number = private_state->packet_count;
722     packet_header.original_packet_number = packet_header.packet_number;
723     packet_header.nano_seconds_since_2000 = seconds_since_2000 * 1000000000 + phdr->ts.nsecs;
724
725     private_state->packet_count++;
726
727     /* write the packet header */
728     PACKET_ENTRY_HEADER_TO_LE_IN_PLACE(packet_header);
729     if (!wtap_dump_file_write(wdh, &packet_header, sizeof(packet_header), err)) {
730         return FALSE;
731     }
732     wdh->bytes_dumped += sizeof(packet_header);
733
734     /* write the packet data */
735     if (!wtap_dump_file_write(wdh, pd, phdr->caplen, err)) {
736         return FALSE;
737     }
738     wdh->bytes_dumped += phdr->caplen;
739
740     return TRUE;
741 }
742
743 static gint observer_to_wtap_encap(int observer_encap)
744 {
745     switch(observer_encap) {
746     case OBSERVER_ETHERNET:
747         return WTAP_ENCAP_ETHERNET;
748     case OBSERVER_TOKENRING:
749         return WTAP_ENCAP_TOKEN_RING;
750     case OBSERVER_FIBRE_CHANNEL:
751         return WTAP_ENCAP_FIBRE_CHANNEL_FC2_WITH_FRAME_DELIMS;
752     case OBSERVER_WIRELESS_802_11:
753         return WTAP_ENCAP_IEEE_802_11_WITH_RADIO;
754     case OBSERVER_UNDEFINED:
755         return WTAP_ENCAP_UNKNOWN;
756     }
757     return WTAP_ENCAP_UNKNOWN;
758 }
759
760 static gint wtap_to_observer_encap(int wtap_encap)
761 {
762     switch(wtap_encap) {
763     case WTAP_ENCAP_ETHERNET:
764         return OBSERVER_ETHERNET;
765     case WTAP_ENCAP_TOKEN_RING:
766         return OBSERVER_TOKENRING;
767     case WTAP_ENCAP_FIBRE_CHANNEL_FC2_WITH_FRAME_DELIMS:
768         return OBSERVER_FIBRE_CHANNEL;
769     case WTAP_ENCAP_UNKNOWN:
770         return OBSERVER_UNDEFINED;
771     }
772     return OBSERVER_UNDEFINED;
773 }
774
775 /*
776  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
777  *
778  * Local variables:
779  * c-basic-offset: 4
780  * tab-width: 8
781  * indent-tabs-mode: nil
782  * End:
783  *
784  * vi: set shiftwidth=4 tabstop=8 expandtab:
785  * :indentSize=4:tabSize=8:noTabs=true:
786  */