[a79ddbe] | 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 | |
---|
[2c060e3] | 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> |
---|
[20482800] | 41 | #include <sys/types.h> |
---|
[2c060e3] | 42 | #include <time.h> |
---|
| 43 | |
---|
| 44 | #include <netinet/in.h> |
---|
[20482800] | 45 | #include <netinet/in_systm.h> |
---|
[2c060e3] | 46 | #include <netinet/tcp.h> |
---|
| 47 | #include <netinet/ip.h> |
---|
| 48 | #include <netinet/ip_icmp.h> |
---|
| 49 | #include <arpa/inet.h> |
---|
| 50 | #include <sys/socket.h> |
---|
| 51 | #include "dagformat.h" |
---|
| 52 | #include "libtrace.h" |
---|
| 53 | |
---|
| 54 | struct libtrace_t *trace; |
---|
| 55 | |
---|
| 56 | #define SCANSIZE 4096 |
---|
| 57 | |
---|
| 58 | char *buffer[SCANSIZE]; |
---|
| 59 | |
---|
| 60 | int main(int argc, char *argv[]) { |
---|
| 61 | |
---|
[e9f521e] | 62 | char *uri = 0; |
---|
[53de8c0] | 63 | char *filterstring = 0; |
---|
[2c060e3] | 64 | int psize = 0; |
---|
| 65 | struct libtrace_ip *ipptr = 0; |
---|
[d56089a] | 66 | struct libtrace_packet_t *packet = trace_create_packet(); |
---|
[53de8c0] | 67 | struct libtrace_filter_t *filter = 0; |
---|
[2c060e3] | 68 | |
---|
| 69 | if (argc == 2) { |
---|
| 70 | uri = strdup(argv[1]); |
---|
[53de8c0] | 71 | } else if (argc == 3) { |
---|
| 72 | uri = strdup(argv[1]); |
---|
| 73 | filterstring = strdup(argv[2]); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | if (filterstring) |
---|
| 77 | filter = trace_bpf_setfilter(filterstring); |
---|
[2c060e3] | 78 | |
---|
| 79 | // open a trace |
---|
[b535184] | 80 | trace = trace_create(uri); |
---|
| 81 | |
---|
[2c060e3] | 82 | |
---|
| 83 | for (;;) { |
---|
| 84 | unsigned char *x; |
---|
| 85 | int i; |
---|
[de67322] | 86 | if ((psize = trace_read_packet(trace, &packet)) <0) { |
---|
| 87 | printf("Error in trace_read_packet\n"); |
---|
[2c060e3] | 88 | break; |
---|
| 89 | } |
---|
[de67322] | 90 | if (psize == 0) { |
---|
| 91 | break; |
---|
| 92 | } |
---|
[2c060e3] | 93 | |
---|
[53de8c0] | 94 | if(filter && !trace_bpf_filter(filter,&packet)) { |
---|
| 95 | continue; |
---|
| 96 | } |
---|
| 97 | |
---|
[b535184] | 98 | printf("TS %f: ",trace_get_seconds(&packet)); |
---|
[2c060e3] | 99 | |
---|
[b535184] | 100 | ipptr = trace_get_ip(&packet); |
---|
[2c060e3] | 101 | if (!ipptr) { |
---|
| 102 | printf("Non IP\n"); |
---|
| 103 | continue; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | printf("%s -> ",inet_ntoa(ipptr->ip_src)); |
---|
| 107 | printf("%s protocol %02x\n", |
---|
| 108 | inet_ntoa(ipptr->ip_dst), |
---|
| 109 | ipptr->ip_p); |
---|
| 110 | x=(void*)ipptr; |
---|
[b535184] | 111 | for(i=0;i<trace_get_capture_length(&packet);i++) { |
---|
[2c060e3] | 112 | if (i%4==0 && i!=0) |
---|
| 113 | printf("\n"); |
---|
| 114 | printf("%02x ",x[i]); |
---|
| 115 | } |
---|
| 116 | printf("\n\n"); |
---|
| 117 | } |
---|
| 118 | |
---|
[b535184] | 119 | trace_destroy(trace); |
---|
[2c060e3] | 120 | return 0; |
---|
| 121 | } |
---|