5 * Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "file_wrappers.h"
36 Daniel Thompson (STMicroelectronics) <daniel.thompson@st.com>
40 +------+------+------+------+
41 | t3 | t2 | t1 | t0 | t = time_t
42 +------+------+------+------+
45 | 0x06 | Time step (short)
47 | ts | ts = time step (tenths of seconds)
51 | 0x05 | Time step (long)
52 +------+------+------+------+
53 | ts3 | ts2 | ts1 | ts0 | ts = time step (tenths of seconds)
54 +------+------+------+------+
57 | 0x04 | Receive deliminator (not seen in practice)
61 | 0x03 | Send deliminator (not seen in practice)
65 | 0x02 | Received data
67 | n1 | n0 | n = number of bytes following
75 | n1 | n0 | n = number of bytes following
81 #define PPPD_SENT_DATA 0x01
82 #define PPPD_RECV_DATA 0x02
83 #define PPPD_SEND_DELIM 0x03
84 #define PPPD_RECV_DELIM 0x04
85 #define PPPD_TIME_STEP_LONG 0x05
86 #define PPPD_TIME_STEP_SHORT 0x06
87 #define PPPD_RESET_TIME 0x07
89 /* this buffer must be at least (2*PPPD_MTU) + sizeof(ppp_header) +
90 * sizeof(lcp_header) + sizeof(ipcp_header). PPPD_MTU is *very* rarely
91 * larger than 1500 so this value is fine.
93 #define PPPD_BUF_SIZE 8192
100 static gboolean pppdump_read(wtap *wth, int *err, gchar **err_info,
101 gint64 *data_offset);
102 static gboolean pppdump_seek_read(wtap *wth, gint64 seek_off,
103 struct wtap_pkthdr *phdr, guint8 *pd, int len,
104 int *err, gchar **err_info);
107 * Information saved about a packet, during the initial sequential pass
108 * through the file, to allow us to later re-read it when randomly
111 * "offset" is the offset in the file of the first data chunk containing data
112 * from that packet; note that it may also contain data from previous
115 * "num_bytes_to_skip" is the number of bytes from previous packets in that
118 * "dir" is the direction of the packet.
122 gint64 num_bytes_to_skip;
127 * Information about a packet currently being processed. There is one of
128 * these for the sent packet being processed and one of these for the
129 * received packet being processed, as we could be in the middle of
130 * processing both a received packet and a sent packet.
132 * "dir" is the direction of the packet.
134 * "cnt" is the number of bytes of packet data we've accumulated.
136 * "esc" is TRUE if the next byte we see is escaped (and thus must be XORed
137 * with 0x20 before saving it), FALSE otherwise.
139 * "buf" is a buffer containing the packet data we've accumulated.
141 * "id_offset" is the offset in the file of the first data chunk
142 * containing data from the packet we're processing.
144 * "sd_offset" is the offset in the file of the first data byte from
145 * the packet we're processing - which isn't necessarily right after
146 * the header of the first data chunk, as we may already have assembled
147 * packets from that chunk.
149 * "cd_offset" is the offset in the file of the current data chunk we're
156 guint8 buf[PPPD_BUF_SIZE];
163 * This keeps state used while processing records.
165 * "timestamp" is the seconds portion of the current time stamp value,
166 * as updated from PPPD_RESET_TIME, PPPD_TIME_STEP_LONG, and
167 * PPPD_TIME_STEP_SHORT records. "tenths" is the tenths-of-seconds
170 * "spkt" and "rpkt" are "pkt_t" structures for the sent and received
171 * packets we're currently working on.
173 * "offset" is the current offset in the file.
175 * "num_bytes" and "pkt" are information saved when we finish accumulating
176 * the data for a packet, if the data chunk we're working on still has more
179 * "num_bytes" is the number of bytes of additional data remaining
180 * in the chunk after we've finished accumulating the data for the
183 * "pkt" is the "pkt_t" for the type of packet the data chunk is for
184 * (sent or received packet).
186 * "seek_state" is another state structure used while processing records
187 * when doing a seek-and-read. (That structure doesn't itself have a
188 * "seek_state" structure.)
190 * "pids" is a GPtrArray of pointers to "pkt_id" structures for all the
191 * packets we've seen during the initial sequential pass, to allow us to
192 * later retrieve them with random accesses.
194 * "pkt_cnt" is the number of packets we've seen up to this point in the
197 typedef struct _pppdump_t {
205 struct _pppdump_t *seek_state;
211 process_data(pppdump_t *state, FILE_T fh, pkt_t *pkt, int n, guint8 *pd,
212 int *err, gchar **err_info, pkt_id *pid);
215 collate(pppdump_t*, FILE_T fh, int *err, gchar **err_info, guint8 *pd,
216 int *num_bytes, direction_enum *direction, pkt_id *pid,
217 gint64 num_bytes_to_skip);
220 pppdump_close(wtap *wth);
223 init_state(pppdump_t *state)
226 state->num_bytes = 0;
229 state->spkt.dir = DIRECTION_SENT;
231 state->spkt.esc = FALSE;
232 state->spkt.id_offset = 0;
233 state->spkt.sd_offset = 0;
234 state->spkt.cd_offset = 0;
236 state->rpkt.dir = DIRECTION_RECV;
238 state->rpkt.esc = FALSE;
239 state->rpkt.id_offset = 0;
240 state->rpkt.sd_offset = 0;
241 state->rpkt.cd_offset = 0;
243 state->seek_state = NULL;
244 state->offset = 0x100000; /* to detect errors during development */
249 pppdump_open(wtap *wth, int *err, gchar **err_info)
251 guint8 buffer[6]; /* Looking for: 0x07 t3 t2 t1 t0 ID */
255 /* There is no file header, only packet records. Fortunately for us,
256 * timestamp records are separated from packet records, so we should
257 * find an "initial time stamp" (i.e., a "reset time" record, or
258 * record type 0x07) at the beginning of the file. We'll check for
259 * that, plus a valid record following the 0x07 and the four bytes
260 * representing the timestamp.
263 bytes_read = file_read(buffer, sizeof(buffer), wth->fh);
264 if (bytes_read != (int) sizeof(buffer)) {
265 *err = file_error(wth->fh, err_info);
266 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
271 if (buffer[0] == PPPD_RESET_TIME &&
272 (buffer[5] == PPPD_SENT_DATA ||
273 buffer[5] == PPPD_RECV_DATA ||
274 buffer[5] == PPPD_TIME_STEP_LONG ||
275 buffer[5] == PPPD_TIME_STEP_SHORT ||
276 buffer[5] == PPPD_RESET_TIME)) {
286 if (file_seek(wth->fh, 5, SEEK_SET, err) == -1)
289 state = (pppdump_t *)g_malloc(sizeof(pppdump_t));
290 wth->priv = (void *)state;
291 state->timestamp = pntohl(&buffer[1]);
297 wth->file_encap = WTAP_ENCAP_PPP_WITH_PHDR;
298 wth->file_type = WTAP_FILE_PPPDUMP;
300 wth->snapshot_length = PPPD_BUF_SIZE; /* just guessing */
301 wth->subtype_read = pppdump_read;
302 wth->subtype_seek_read = pppdump_seek_read;
303 wth->subtype_close = pppdump_close;
304 wth->tsprecision = WTAP_FILE_TSPREC_DSEC;
306 state->seek_state = g_new(pppdump_t,1);
308 /* If we have a random stream open, we're going to be reading
309 the file randomly; set up a GPtrArray of pointers to
310 information about how to retrieve the data for each packet. */
311 if (wth->random_fh != NULL)
312 state->pids = g_ptr_array_new();
320 /* Find the next packet and parse it; called from wtap_read(). */
322 pppdump_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
325 direction_enum direction;
330 buffer_assure_space(wth->frame_buffer, PPPD_BUF_SIZE);
331 buf = buffer_start_ptr(wth->frame_buffer);
333 state = (pppdump_t *)wth->priv;
335 /* If we have a random stream open, allocate a structure to hold
336 the information needed to read this packet's data again. */
337 if (wth->random_fh != NULL) {
338 pid = g_new(pkt_id, 1);
340 *err = errno; /* assume a malloc failed and set "errno" */
345 pid = NULL; /* sequential only */
347 if (!collate(state, wth->fh, err, err_info, buf, &num_bytes, &direction,
355 pid->dir = direction;
358 g_ptr_array_add(state->pids, pid);
359 /* The user's data_offset is not really an offset, but a packet number. */
360 *data_offset = state->pkt_cnt;
363 wth->phdr.presence_flags = WTAP_HAS_TS;
364 wth->phdr.len = num_bytes;
365 wth->phdr.caplen = num_bytes;
366 wth->phdr.ts.secs = state->timestamp;
367 wth->phdr.ts.nsecs = state->tenths * 100000000;
368 wth->phdr.pkt_encap = WTAP_ENCAP_PPP_WITH_PHDR;
370 wth->phdr.pseudo_header.p2p.sent = (direction == DIRECTION_SENT ? TRUE : FALSE);
375 /* Returns number of bytes copied for record, -1 if failure.
377 * This is modeled after pppdump.c, the utility to parse pppd log files; it
378 * comes with the ppp distribution.
381 process_data(pppdump_t *state, FILE_T fh, pkt_t *pkt, int n, guint8 *pd,
382 int *err, gchar **err_info, pkt_id *pid)
388 for (; num_bytes > 0; --num_bytes) {
391 *err = file_error(fh, err_info);
393 *err = WTAP_ERR_SHORT_READ;
401 * Flag Sequence for RFC 1662 HDLC-like
404 * As this is a raw trace of octets going
405 * over the wire, and that might include
406 * the login sequence, there is no
407 * guarantee that *only* PPP traffic
408 * appears in this file, so there is no
409 * guarantee that the first 0x7e we see is
410 * a start flag sequence, and therefore we
411 * cannot safely ignore all characters up
412 * to the first 0x7e, and therefore we
413 * might end up with some bogus PPP
418 * We've seen stuff before this,
419 * so this is the end of a frame.
420 * Make a frame out of that stuff.
424 num_written = pkt->cnt;
426 if (num_written <= 0) {
430 if (num_written > PPPD_BUF_SIZE) {
431 *err = WTAP_ERR_UNC_OVERFLOW;
435 memcpy(pd, pkt->buf, num_written);
438 * Remember the offset of the
439 * first record containing data
440 * for this packet, and how far
441 * into that record to skip to
442 * get to the beginning of the
443 * data for this packet; the number
444 * of bytes to skip into that record
445 * is the file offset of the first
446 * byte of this packet minus the
447 * file offset of the first byte of
448 * this record, minus 3 bytes for the
449 * header of this record (which, if
450 * we re-read this record, we will
451 * process, not skip).
454 pid->offset = pkt->id_offset;
455 pid->num_bytes_to_skip =
456 pkt->sd_offset - pkt->id_offset - 3;
457 g_assert(pid->num_bytes_to_skip >= 0);
463 * There's more data in this
465 * Set the initial data offset
466 * for the next packet.
468 pkt->id_offset = pkt->cd_offset;
469 pkt->sd_offset = state->offset;
472 * There is no more data in
474 * Thus, we don't have the
475 * initial data offset for
481 state->num_bytes = num_bytes;
489 * Control Escape octet for octet-stuffed
490 * RFC 1662 HDLC-like framing.
494 * Control Escape not preceded by
495 * Control Escape; discard it
496 * but XOR the next octet with
503 * Control Escape preceded by Control Escape;
504 * treat it as an ordinary character,
505 * by falling through.
511 * This character was preceded by
512 * Control Escape, so XOR it with
513 * 0x20, as per RFC 1662's octet-
514 * stuffed framing, and clear
515 * the flag saying that the
516 * character should be escaped.
522 if (pkt->cnt >= PPPD_BUF_SIZE) {
523 *err = WTAP_ERR_UNC_OVERFLOW;
526 pkt->buf[pkt->cnt++] = c;
531 /* we could have run out of bytes to read */
535 /* Returns TRUE if packet data copied, FALSE if error occurred or EOF (no more records). */
537 collate(pppdump_t* state, FILE_T fh, int *err, gchar **err_info, guint8 *pd,
538 int *num_bytes, direction_enum *direction, pkt_id *pid,
539 gint64 num_bytes_to_skip)
544 int n, num_written = 0;
550 * Process any data left over in the current record when doing
551 * sequential processing.
553 if (state->num_bytes > 0) {
554 g_assert(num_bytes_to_skip == 0);
556 num_written = process_data(state, fh, pkt, state->num_bytes,
557 pd, err, err_info, pid);
559 if (num_written < 0) {
562 else if (num_written > 0) {
563 *num_bytes = num_written;
564 *direction = pkt->dir;
567 /* if 0 bytes written, keep processing */
570 * We didn't have any data left over, so the packet will
571 * start at the beginning of a record.
574 pid->num_bytes_to_skip = 0;
578 * That didn't get all the data for this packet, so process
579 * subsequent records.
581 start_offset = state->offset;
582 while ((id = file_getc(fh)) != EOF) {
587 pkt = id == PPPD_SENT_DATA ? &state->spkt : &state->rpkt;
590 * Save the offset of the beginning of
591 * the current record.
593 pkt->cd_offset = state->offset - 1;
596 * Get the length of the record.
598 byte0 = file_getc(fh);
602 byte1 = file_getc(fh);
606 n = (byte0 << 8) | byte1;
608 if (pkt->id_offset == 0) {
610 * We don't have the initial data
611 * offset for this packet, which
612 * means this is the first
613 * data record for that packet.
614 * Save the offset of the
615 * beginning of that record and
616 * the offset of the first data
617 * byte in the packet, which is
618 * the first data byte in the
621 pkt->id_offset = pkt->cd_offset;
622 pkt->sd_offset = state->offset;
628 g_assert(num_bytes_to_skip < n);
629 while (num_bytes_to_skip) {
630 if (file_getc(fh) == EOF)
636 num_written = process_data(state, fh, pkt, n,
637 pd, err, err_info, pid);
639 if (num_written < 0) {
642 else if (num_written > 0) {
643 *num_bytes = num_written;
644 *direction = pkt->dir;
647 /* if 0 bytes written, keep looping */
650 case PPPD_SEND_DELIM:
651 case PPPD_RECV_DELIM:
652 /* What can we do? */
655 case PPPD_RESET_TIME:
656 wtap_file_read_unknown_bytes(&time_long, sizeof(guint32), fh, err, err_info);
657 state->offset += sizeof(guint32);
658 state->timestamp = pntohl(&time_long);
662 case PPPD_TIME_STEP_LONG:
663 wtap_file_read_unknown_bytes(&time_long, sizeof(guint32), fh, err, err_info);
664 state->offset += sizeof(guint32);
665 state->tenths += pntohl(&time_long);
667 if (state->tenths >= 10) {
668 state->timestamp += state->tenths / 10;
669 state->tenths = state->tenths % 10;
674 case PPPD_TIME_STEP_SHORT:
675 wtap_file_read_unknown_bytes(&time_short, sizeof(guint8), fh, err, err_info);
676 state->offset += sizeof(guint8);
677 state->tenths += time_short;
679 if (state->tenths >= 10) {
680 state->timestamp += state->tenths / 10;
681 state->tenths = state->tenths % 10;
688 *err = WTAP_ERR_BAD_FILE;
689 *err_info = g_strdup_printf("pppdump: bad ID byte 0x%02x", id);
696 *err = file_error(fh, err_info);
698 if (state->offset != start_offset) {
700 * We read at least one byte, so we were working
701 * on a record; an EOF means that record was
704 *err = WTAP_ERR_SHORT_READ;
712 /* Used to read packets in random-access fashion */
714 pppdump_seek_read(wtap *wth,
716 struct wtap_pkthdr *phdr,
722 union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
724 direction_enum direction;
727 gint64 num_bytes_to_skip;
729 state = (pppdump_t *)wth->priv;
731 pid = (pkt_id *)g_ptr_array_index(state->pids, seek_off);
733 *err = WTAP_ERR_BAD_FILE; /* XXX - better error? */
734 *err_info = g_strdup("pppdump: PID not found for record");
738 if (file_seek(wth->random_fh, pid->offset, SEEK_SET, err) == -1)
741 init_state(state->seek_state);
742 state->seek_state->offset = pid->offset;
745 * We'll start reading at the first record containing data from
746 * this packet; however, that doesn't mean "collate()" will
747 * stop only when we've read that packet, as there might be
748 * data for packets going in the other direction as well, and
749 * we might finish processing one of those packets before we
750 * finish processing the packet we're reading.
752 * Therefore, we keep reading until we get a packet that's
753 * going in the direction we want.
755 num_bytes_to_skip = pid->num_bytes_to_skip;
757 if (!collate(state->seek_state, wth->random_fh, err, err_info,
758 pd, &num_bytes, &direction, NULL, num_bytes_to_skip))
760 num_bytes_to_skip = 0;
761 } while (direction != pid->dir);
763 if (len != num_bytes) {
764 *err = WTAP_ERR_BAD_FILE; /* XXX - better error? */
765 *err_info = g_strdup_printf("pppdump: requested length %d doesn't match record length %d",
770 pseudo_header->p2p.sent = (pid->dir == DIRECTION_SENT ? TRUE : FALSE);
776 pppdump_close(wtap *wth)
780 state = (pppdump_t *)wth->priv;
782 if (state->seek_state) { /* should always be TRUE */
783 g_free(state->seek_state);
788 for (i = 0; i < g_ptr_array_len(state->pids); i++) {
789 g_free(g_ptr_array_index(state->pids, i));
791 g_ptr_array_free(state->pids, TRUE);