1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2004 The University of Waikato, Hamilton, New Zealand. |
---|
5 | * Authors: Daniel Lawson |
---|
6 | * Perry Lorier |
---|
7 | * |
---|
8 | * All rights reserved. |
---|
9 | * |
---|
10 | * This code has been developed by the University of Waikato WAND |
---|
11 | * research group. For further information please see http://www.wand.net.nz/ |
---|
12 | * |
---|
13 | * libtrace is free software; you can redistribute it and/or modify |
---|
14 | * it under the terms of the GNU General Public License as published by |
---|
15 | * the Free Software Foundation; either version 2 of the License, or |
---|
16 | * (at your option) any later version. |
---|
17 | * |
---|
18 | * libtrace is distributed in the hope that it will be useful, |
---|
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
21 | * GNU General Public License for more details. |
---|
22 | * |
---|
23 | * You should have received a copy of the GNU General Public License |
---|
24 | * along with libtrace; if not, write to the Free Software |
---|
25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
26 | * |
---|
27 | * $Id$ |
---|
28 | * |
---|
29 | */ |
---|
30 | |
---|
31 | // |
---|
32 | // This program takes a trace and outputs every packet that it sees to standard |
---|
33 | // out, decoding source/dest IP's, protocol type, and the timestamp of this |
---|
34 | // packet. |
---|
35 | |
---|
36 | #include <stdio.h> |
---|
37 | #include <stdlib.h> |
---|
38 | #include <assert.h> |
---|
39 | #include <string.h> |
---|
40 | #include <sys/time.h> |
---|
41 | #include <time.h> |
---|
42 | |
---|
43 | #include <netinet/in.h> |
---|
44 | #include <netinet/tcp.h> |
---|
45 | #include <netinet/ip.h> |
---|
46 | #include <netinet/ip_icmp.h> |
---|
47 | #include <arpa/inet.h> |
---|
48 | #include <sys/socket.h> |
---|
49 | #include "dagformat.h" |
---|
50 | #include "libtrace.h" |
---|
51 | |
---|
52 | struct libtrace_t *trace; |
---|
53 | |
---|
54 | #define SCANSIZE 4096 |
---|
55 | |
---|
56 | char *buffer[SCANSIZE]; |
---|
57 | |
---|
58 | int main(int argc, char *argv[]) { |
---|
59 | |
---|
60 | char *uri = 0; |
---|
61 | int psize = 0; |
---|
62 | int status = 0; |
---|
63 | struct libtrace_ip *ipptr = 0; |
---|
64 | struct libtrace_packet_t packet; |
---|
65 | |
---|
66 | if (argc == 2) { |
---|
67 | uri = strdup(argv[1]); |
---|
68 | } |
---|
69 | |
---|
70 | // open a trace |
---|
71 | trace = trace_create(uri); |
---|
72 | |
---|
73 | |
---|
74 | for (;;) { |
---|
75 | unsigned char *x; |
---|
76 | int i; |
---|
77 | if ((psize = trace_read_packet(trace, &packet)) <1) { |
---|
78 | break; |
---|
79 | } |
---|
80 | |
---|
81 | printf("TS %f: ",trace_get_seconds(&packet)); |
---|
82 | |
---|
83 | ipptr = trace_get_ip(&packet); |
---|
84 | if (!ipptr) { |
---|
85 | printf("Non IP\n"); |
---|
86 | continue; |
---|
87 | } |
---|
88 | |
---|
89 | printf("%s -> ",inet_ntoa(ipptr->ip_src)); |
---|
90 | printf("%s protocol %02x\n", |
---|
91 | inet_ntoa(ipptr->ip_dst), |
---|
92 | ipptr->ip_p); |
---|
93 | x=(void*)ipptr; |
---|
94 | for(i=0;i<trace_get_capture_length(&packet);i++) { |
---|
95 | if (i%4==0 && i!=0) |
---|
96 | printf("\n"); |
---|
97 | printf("%02x ",x[i]); |
---|
98 | } |
---|
99 | printf("\n\n"); |
---|
100 | } |
---|
101 | |
---|
102 | trace_destroy(trace); |
---|
103 | return 0; |
---|
104 | } |
---|