Check for errors in seeks, "tell"s, and "stat()"s/"fstat()"s.
[obnox/wireshark/wip.git] / wiretap / pppdump.c
1 /* pppdump.c
2  *
3  * $Id: pppdump.c,v 1.15 2002/03/04 00:25:35 guy Exp $
4  *
5  * Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu>
6  * 
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.
11  * 
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.
16  * 
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include "wtap-int.h"
26 #include "buffer.h"
27 #include "pppdump.h"
28 #include "file_wrappers.h"
29
30 #include <glib.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <string.h>
35
36 /*
37 pppdump records
38 Daniel Thompson (STMicroelectronics) <daniel.thompson@st.com>
39
40 +------+
41 | 0x07 +------+------+------+         Reset time
42 |  t3  |  t2  |  t1  |  t0  |         t = time_t
43 +------+------+------+------+
44
45 +------+
46 | 0x06 |                              Time step (short)
47 |  ts  |                              ts = time step (tenths)
48 +------+
49
50 +------+
51 | 0x05 +------+------+------+         Time step (long)
52 | ts3  | ts2  | ts1  | ts0  |         ts = time step (tenths)
53 +------+------+------+------+
54
55 +------+
56 | 0x04 |                              Receive deliminator (not seen in practice)
57 +------+
58
59 +------+
60 | 0x03 |                              Send deliminator (not seen in practice)
61 +------+
62
63 +------+
64 | 0x02 +------+                       Received data
65 |  n1  |  n0  |                       n = number of bytes following
66 |    data     |
67 |             |
68
69 +------+
70 | 0x01 +------+                       Sent data
71 |  n1  |  n0  |                       n = number of bytes following
72 |    data     |
73 |             |
74 */
75
76 #define PPPD_SENT_DATA          0x01
77 #define PPPD_RECV_DATA          0x02
78 #define PPPD_SEND_DELIM         0x03
79 #define PPPD_RECV_DELIM         0x04
80 #define PPPD_TIME_STEP_LONG     0x05
81 #define PPPD_TIME_STEP_SHORT    0x06
82 #define PPPD_RESET_TIME         0x07
83
84 #define PPPD_NULL               0x00    /* For my own use */
85
86 /* this buffer must be at least (2*PPPD_MTU) + sizeof(ppp_header) + sizeof(lcp_header) + 
87  * sizeof(ipcp_header). PPPD_MTU is *very* rarely larger than 1500 so this value is fine
88  */
89 #define PPPD_BUF_SIZE           8192
90
91 typedef enum {
92         DIRECTION_SENT,
93         DIRECTION_RECV
94 } direction_enum;
95
96 static gboolean pppdump_read(wtap *wth, int *err, long *data_offset);
97 static int pppdump_seek_read(wtap *wth, long seek_off,
98         union wtap_pseudo_header *pseudo_header, guint8 *pd, int len);
99
100 typedef struct {
101         long            offset;
102         int             num_saved_states;
103         direction_enum  dir;
104 } pkt_id;
105
106 typedef struct {
107         direction_enum  dir;
108         int             cnt;
109         gboolean        esc;
110         guint8          buf[PPPD_BUF_SIZE];
111         long            id_offset;
112 } pkt_t;
113
114 /* Partial-record state */
115 typedef struct {
116         int             num_bytes;
117         pkt_t           *pkt;
118 } prec_state;
119
120 struct _pppdump_t;
121
122 typedef struct _pppdump_t {
123         time_t                  timestamp;
124         guint                   tenths;
125         pkt_t                   spkt;
126         pkt_t                   rpkt;
127         long                    offset;
128         GList                   *precs;
129         struct _pppdump_t       *seek_state;
130         GPtrArray               *pids;
131         guint                   pkt_cnt;
132         int                     num_saved_states;
133 } pppdump_t;
134
135 static int
136 process_data(pppdump_t *state, FILE_T fh, pkt_t *pkt, int n, guint8 *pd, int *err,
137                 gboolean *state_saved);
138
139 static gboolean
140 collate(pppdump_t*, FILE_T fh, int *err, guint8 *pd, int *num_bytes,
141                 direction_enum *direction, pkt_id *pid);
142
143 static void
144 pppdump_close(wtap *wth);
145
146 static void
147 init_state(pppdump_t *state)
148 {
149
150         state->precs = NULL;
151
152         state->spkt.dir = DIRECTION_SENT;
153         state->spkt.cnt = 0;
154         state->spkt.esc = FALSE;
155         state->spkt.id_offset = 0;
156
157         state->rpkt.dir = DIRECTION_RECV;
158         state->rpkt.cnt = 0;
159         state->rpkt.esc = FALSE;
160         state->rpkt.id_offset = 0;
161
162         state->seek_state = NULL;
163         state->offset = 0x100000; /* to detect errors during development */
164 }
165
166         
167 int
168 pppdump_open(wtap *wth, int *err)
169 {
170         guint8          buffer[6];      /* Looking for: 0x07 t3 t2 t1 t0 ID */
171         pppdump_t       *state;
172
173         /* There is no file header, only packet records. Fortunately for us,
174         * timestamp records are separated from packet records, so we should
175         * find an "initial time stamp" (i.e., a "reset time" record, or
176         * record type 0x07) at the beginning of the file. We'll check for
177         * that, plus a valid record following the 0x07 and the four bytes
178         * representing the timestamp.
179         */
180
181         wtap_file_read_unknown_bytes(buffer, sizeof(buffer), wth->fh, err);
182
183         if (buffer[0] == PPPD_RESET_TIME &&
184                         (buffer[5] == PPPD_SENT_DATA ||
185                          buffer[5] == PPPD_RECV_DATA ||
186                          buffer[5] == PPPD_TIME_STEP_LONG ||
187                          buffer[5] == PPPD_TIME_STEP_SHORT ||
188                          buffer[5] == PPPD_RESET_TIME)) {
189
190                 goto my_file_type;
191         }
192         else {
193                 return 0;
194         }
195
196   my_file_type:
197
198         if (file_seek(wth->fh, 5, SEEK_SET) == -1) {
199                 *err = file_error(wth->fh);
200                 return -1;
201         }
202
203         state = wth->capture.generic = g_malloc(sizeof(pppdump_t));
204         state->timestamp = pntohl(&buffer[1]);
205         state->tenths = 0;
206
207         init_state(state);
208
209         state->offset = 5; 
210         wth->file_encap = WTAP_ENCAP_PPP_WITH_PHDR; 
211         wth->file_type = WTAP_FILE_PPPDUMP; 
212
213         wth->snapshot_length = PPPD_BUF_SIZE; /* just guessing */ 
214         wth->subtype_read = pppdump_read; 
215         wth->subtype_seek_read = pppdump_seek_read; 
216         wth->subtype_close = pppdump_close;
217
218         state->seek_state = g_malloc(sizeof(pppdump_t));
219
220         state->pids = g_ptr_array_new();
221         state->pkt_cnt = 0;
222         state->num_saved_states = 0;
223
224         return 1;
225 }
226
227 /* Find the next packet and parse it; called from wtap_loop(). */
228 static gboolean
229 pppdump_read(wtap *wth, int *err, long *data_offset)
230 {
231         gboolean        retval;
232         int             num_bytes;
233         direction_enum  direction;
234         guint8          *buf;
235         pppdump_t       *state;
236         pkt_id          *pid;
237
238         buffer_assure_space(wth->frame_buffer, PPPD_BUF_SIZE);
239         buf = buffer_start_ptr(wth->frame_buffer);
240
241         state = wth->capture.generic;
242         pid = g_new(pkt_id, 1);
243         if (!pid) {
244                 *err = errno;   /* assume a malloc failed and set "errno" */
245                 return FALSE;
246         }
247         pid->offset = 0;
248         pid->num_saved_states = 0;
249
250         retval = collate(state, wth->fh, err, buf, &num_bytes, &direction, pid);
251
252         if (!retval) {
253                 g_free(pid);
254                 return FALSE;
255         }
256
257         pid->dir = direction;
258
259         g_ptr_array_add(state->pids, pid);
260         /* The user's data_offset is not really an offset, but a packet number. */
261         *data_offset = state->pkt_cnt;
262         state->pkt_cnt++;
263
264         wth->phdr.len           = num_bytes;
265         wth->phdr.caplen        = num_bytes;
266         wth->phdr.ts.tv_sec     = state->timestamp;
267         wth->phdr.ts.tv_usec    = state->tenths * 100000;
268         wth->phdr.pkt_encap     = WTAP_ENCAP_PPP_WITH_PHDR;
269
270         wth->pseudo_header.p2p.sent = (direction == DIRECTION_SENT ? TRUE : FALSE);
271
272         return TRUE;
273 }
274
275 #define PKT(x)  (x)->dir == DIRECTION_SENT ? "SENT" : "RECV"
276
277 static gboolean
278 save_prec_state(pppdump_t *state, int num_bytes, pkt_t *pkt)
279 {
280         prec_state      *prec;
281
282         prec = g_new(prec_state, 1);
283         if (!prec) {
284                 return FALSE;
285         }
286         prec->num_bytes = num_bytes;
287         prec->pkt = pkt;
288
289         state->precs = g_list_append(state->precs, prec);
290         return TRUE;
291 }
292
293 static int
294 process_data_from_prec_state(pppdump_t *state, FILE_T fh, guint8* pd, int *err,
295                 gboolean *state_saved, pkt_t **ppkt)
296 {
297         prec_state      *prec;
298
299         prec = state->precs->data;
300
301         state->precs = g_list_remove(state->precs, prec);
302
303         *ppkt = prec->pkt;
304
305         return process_data(state, fh, prec->pkt, prec->num_bytes, pd, err, state_saved);
306 }
307         
308
309
310 /* Returns number of bytes copied for record, -1 if failure.
311  *
312  * This is modeled after pppdump.c, the utility to parse pppd log files; it comes with the ppp
313  * distribution.
314  */
315 static int
316 process_data(pppdump_t *state, FILE_T fh, pkt_t *pkt, int n, guint8 *pd, int *err,
317                 gboolean *state_saved)
318 {
319         int     c;
320         int     num_bytes = n;
321         int     num_written;
322
323         *state_saved = FALSE;
324         for (; num_bytes > 0; --num_bytes) {
325                 c = file_getc(fh);
326                 state->offset++;
327                 switch (c) {
328                         case EOF:
329                                 if (*err == 0) {
330                                         *err = WTAP_ERR_SHORT_READ;
331                                 }
332                                 return -1;
333                                 break;
334
335                         case '~':
336                                 if (pkt->cnt > 0) {
337                                         pkt->esc = FALSE;
338
339                                         num_written = pkt->cnt;
340                                         pkt->cnt = 0;
341                                         if (num_written <= 0) {
342                                                 return 0;
343                                         }
344
345                                         if (num_written > PPPD_BUF_SIZE) {
346                                                 *err = WTAP_ERR_UNC_OVERFLOW;
347                                                 return -1;
348                                         }
349
350                                         memcpy(pd, pkt->buf, num_written);
351
352                                         num_bytes--;
353                                         if (num_bytes > 0) {
354                                                 if (!save_prec_state(state, num_bytes, pkt)) {
355                                                         *err = errno;
356                                                         return -1;
357                                                 }
358                                                 *state_saved = TRUE;
359                                         }
360                                         return num_written;
361                                 }
362                                 break;
363
364                         case '}':
365                                 if (!pkt->esc) {
366                                         pkt->esc = TRUE;
367                                         break;
368                                 }
369                                 /* else fall through */
370
371                         default:
372                                 if (pkt->esc) {
373                                         c ^= 0x20;
374                                         pkt->esc = FALSE;
375                                 }
376                 
377                                 pkt->buf[pkt->cnt++] = c;
378                                 if (pkt->cnt > PPPD_BUF_SIZE) {
379                                         *err = WTAP_ERR_UNC_OVERFLOW;
380                                         return -1;
381                                 }
382                                 break;
383                 }
384         }
385
386         /* we could have run out of bytes to read */
387         return 0;
388
389 }
390
391
392
393
394 /* Returns TRUE if packet data copied, FALSE if error occurred or EOF (no more records). */
395 static gboolean
396 collate(pppdump_t* state, FILE_T fh, int *err, guint8 *pd, int *num_bytes,
397                 direction_enum *direction, pkt_id *pid)
398 {
399         int             id;
400         pkt_t           *pkt = NULL;
401         int             n, num_written = 0;
402         gboolean        ss = FALSE;
403         guint32         time_long;
404         guint8          time_short;
405
406         if (!state->precs) {
407                 state->num_saved_states = 0;
408         }
409         if (pid) {
410                 pid->num_saved_states = state->num_saved_states;
411         }
412
413
414         while (state->precs) {
415                 num_written = process_data_from_prec_state(state, fh, pd, err, &ss, &pkt);
416                 state->num_saved_states++;
417                 if (pid) {
418                         pid->num_saved_states++;
419                 }
420
421                 if (num_written < 0) {
422                         return FALSE;
423                 }
424                 else if (num_written > 0) {
425                         *num_bytes = num_written;
426                         *direction = pkt->dir;
427                         if (pid) {
428                                 pid->offset = pkt->id_offset;
429                         }
430                         if (!ss) {
431                                 pkt->id_offset = 0;
432                         }
433                         return TRUE;
434                 }
435                 /* if 0 bytes written, keep processing */
436         }
437
438         while ((id = file_getc(fh)) != EOF) {
439                 state->offset++;
440                 switch (id) {
441                         case PPPD_SENT_DATA:
442                         case PPPD_RECV_DATA:
443                                 pkt = id == PPPD_SENT_DATA ? &state->spkt : &state->rpkt;
444
445                                 if (pkt->id_offset == 0) {
446                                         pkt->id_offset = state->offset - 1;
447                                 }
448
449                                 n = file_getc(fh);
450                                 n = (n << 8) + file_getc(fh);
451                                 state->offset += 2;
452
453                                 num_written = process_data(state, fh, pkt, n, pd, err, &ss);
454
455                                 if (num_written < 0) {
456                                         return FALSE;
457                                 }
458                                 else if (num_written > 0) {
459                                         *num_bytes = num_written;
460                                         *direction = pkt->dir;
461                                         if (pid) {
462                                                 pid->offset = pkt->id_offset;
463                                         }
464                                         if (!ss) {
465                                                 pkt->id_offset = 0;
466                                         }
467                                         return TRUE;
468                                 }
469                                 /* if 0 bytes written, keep looping */
470                                 
471                                 break;
472
473                         case PPPD_SEND_DELIM:
474                         case PPPD_RECV_DELIM:
475                                 /* What can we do? */
476                                 break;
477
478                         case PPPD_RESET_TIME:
479                                 wtap_file_read_unknown_bytes(&time_long, sizeof(guint32), fh, err);
480                                 state->offset += sizeof(guint32);
481                                 state->timestamp = pntohl(&time_long);
482                                 state->tenths = 0;
483                                 break;
484
485                         case PPPD_TIME_STEP_LONG:
486                                 wtap_file_read_unknown_bytes(&time_long, sizeof(guint32), fh, err);
487                                 state->offset += sizeof(guint32);
488                                 state->tenths += pntohl(&time_long);
489
490                                 if (state->tenths >= 10) {
491                                         state->timestamp += state->tenths / 10;
492                                         state->tenths = state->tenths % 10;
493                                 }
494
495                                 break;
496
497                         case PPPD_TIME_STEP_SHORT:
498                                 wtap_file_read_unknown_bytes(&time_short, sizeof(guint8), fh, err);
499                                 state->offset += sizeof(guint8);
500                                 state->tenths += time_short;
501
502                                 if (state->tenths >= 10) {
503                                         state->timestamp += state->tenths / 10;
504                                         state->tenths = state->tenths % 10;
505                                 }
506
507                                 break;
508
509                         default:
510                                 /* XXX - bad file */
511                                 g_assert_not_reached();
512                 }
513
514         }
515
516         return FALSE;
517 }
518
519
520
521 /* Used to read packets in random-access fashion */
522 static int
523 pppdump_seek_read (wtap *wth,
524                  long seek_off,
525                  union wtap_pseudo_header *pseudo_header,
526                  guint8 *pd,
527                  int len)
528 {
529         int             err = 0;
530         int             num_bytes;
531         direction_enum  direction;
532         gboolean        retval;
533         pppdump_t       *state;
534         pkt_id          *pid;
535         int             i;
536
537
538         state = wth->capture.generic;
539
540         pid = g_ptr_array_index(state->pids, seek_off);
541         if (!pid) {
542                 return -1;
543         }
544
545         file_seek(wth->random_fh, pid->offset, SEEK_SET);
546
547         init_state(state->seek_state);
548
549         for (i = 0 ; i <= pid->num_saved_states; i++) {
550           again:
551                 retval = collate(state->seek_state, wth->random_fh, &err, pd, &num_bytes,
552                                 &direction, NULL);
553
554                 if (!retval) {
555                         return -1;
556                 }
557
558                 if (direction != pid->dir) {
559                         goto again;
560                 }
561         }
562
563         if (len != num_bytes) {
564                 return -1;
565         }
566
567         pseudo_header->p2p.sent = (pid->dir == DIRECTION_SENT ? TRUE : FALSE);
568
569         return 0;
570 }
571
572 static void
573 simple_g_free(gpointer data, gpointer dummy _U_)
574 {
575         if (data)
576                 g_free(data);
577 }
578
579 static void
580 pppdump_close(wtap *wth)
581 {
582         pppdump_t       *state;
583
584         state = wth->capture.generic;
585
586         if (state->precs) {
587                 g_list_foreach(state->precs, simple_g_free, NULL);
588                 g_list_free(state->precs);
589         }
590
591         if (state->seek_state) { /* should always be TRUE */
592                 g_free(state->seek_state);
593         }
594
595         if (state->pids) { /* should always be TRUE */
596                 unsigned int i;
597                 for (i = 0; i < g_ptr_array_len(state->pids); i++) {
598                         g_free(g_ptr_array_index(state->pids, i));
599                 }
600                 g_ptr_array_free(state->pids, TRUE);
601         }
602
603         g_free(state);
604
605 }