source: examples/count/count.c @ 18478c1

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 18478c1 was 18478c1, checked in by Perry Lorier <perry@…>, 15 years ago

Add simple analysis tool

  • Property mode set to 100644
File size: 3.6 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 <getopt.h>
52#include <inttypes.h>
53
54#include "libtrace.h"
55
56struct libtrace_t *trace;
57
58struct filter_t {
59        char *expr;
60        struct libtrace_filter_t *filter;
61        uint64_t count;
62        uint64_t bytes;
63} *filters = NULL;
64int filter_count=0;
65uint64_t count;
66uint64_t bytes;
67
68/* Process a trace, counting packets that match filter(s) */
69void run_trace(char *uri) 
70{
71        struct libtrace_packet_t packet;
72        int i;
73
74        fprintf(stderr,"%s:\n",uri);
75
76        trace = trace_create(uri);
77
78        for (;;) {
79                int psize;
80                if ((psize = trace_read_packet(trace, &packet)) <1) {
81                        break;
82                }
83
84                for(i=0;i<filter_count;++i) {
85                        if(trace_bpf_filter(filters[i].filter,&packet)) {
86                                ++filters[i].count;
87                                filters[i].bytes+=trace_get_wire_length(&packet);
88                        }
89                }
90
91                ++count;
92                bytes+=trace_get_wire_length(&packet);
93        }
94
95        for(i=0;i<filter_count;++i) {
96                printf("%s\t%8"PRIu64"\t%8"PRIu64"\t%5.02f\n",filters[i].expr,filters[i].count,filters[i].bytes,filters[i].count*100.0/count);
97                filters[i].bytes=0;
98                filters[i].count=0;
99        }
100        printf("Total:\t%8"PRIu64"\t%8" PRIu64 "\n",count,bytes);
101        count=0;
102        bytes=0;
103
104        trace_destroy(trace);
105}
106
107void usage(char *argv0)
108{
109        fprintf(stderr,"Usage: %s [--filter|-f bpf ]... libtraceuri...\n",argv0);
110}
111
112int main(int argc, char *argv[]) {
113
114        int i;
115
116        while(1) {
117                int option_index;
118                struct option long_options[] = {
119                        { "filter",     1, 0, 'f' },
120                        { NULL,         0, 0, 0   },
121                };
122
123                int c=getopt_long(argc, argv, "f:",
124                                long_options, &option_index);
125
126                if (c==-1)
127                        break;
128
129                switch (c) {
130                        case 'f':
131                                ++filter_count;
132                                filters=realloc(filters,filter_count*sizeof(struct filter_t));
133                                filters[filter_count-1].expr=strdup(optarg);
134                                filters[filter_count-1].filter=trace_bpf_setfilter(optarg);
135                                filters[filter_count-1].count=0;
136                                filters[filter_count-1].bytes=0;
137                                break;
138                        default:
139                                fprintf(stderr,"Unknown option: %c\n",c);
140                                usage(argv[0]);
141                                return 1;
142                }
143        }
144
145        printf("filter\tcount    \tbytes   \t%%\n");
146        for(i=optind;i<argc;++i) {
147                run_trace(argv[i]);
148        }
149
150
151        return 0;
152}
Note: See TracBrowser for help on using the repository browser.