source: examples/sample/sample.c @ afd0b73

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since afd0b73 was d56089a, checked in by Shane Alcock <salcock@…>, 16 years ago

Updated examples and tools to use new function names

  • Property mode set to 100644
File size: 3.0 KB
Line 
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 <sys/types.h>
42#include <time.h>
43
44#include <netinet/in.h>
45#include <netinet/in_systm.h>
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
54struct libtrace_t *trace;
55
56#define SCANSIZE 4096
57
58char *buffer[SCANSIZE];
59
60int main(int argc, char *argv[]) {
61
62        char *uri = 0;
63        char *filterstring = 0;
64        int psize = 0;
65        struct libtrace_ip *ipptr = 0;
66        struct libtrace_packet_t *packet = trace_create_packet();
67        struct libtrace_filter_t *filter = 0;
68
69        if (argc == 2) {
70                uri = strdup(argv[1]);
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);
78
79        // open a trace
80        trace = trace_create(uri);
81       
82
83        for (;;) {
84                unsigned char *x;
85                int i;
86                if ((psize = trace_read_packet(trace, &packet)) <0) {
87                        printf("Error in trace_read_packet\n");
88                        break;
89                }
90                if (psize == 0) {
91                        break;
92                }
93
94                if(filter && !trace_bpf_filter(filter,&packet)) {
95                        continue;
96                }
97
98                printf("TS %f: ",trace_get_seconds(&packet));
99
100                ipptr = trace_get_ip(&packet);
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;
111                for(i=0;i<trace_get_capture_length(&packet);i++) {
112                        if (i%4==0 && i!=0)
113                                printf("\n");
114                        printf("%02x ",x[i]);
115                }
116                printf("\n\n");
117        }
118
119        trace_destroy(trace);
120        return 0;
121}
Note: See TracBrowser for help on using the repository browser.