source: examples/capture/capture.c @ aa27e1e

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since aa27e1e was aa27e1e, checked in by Daniel Lawson <dlawson@…>, 16 years ago

added loss detection to 'capture'

  • Property mode set to 100644
File size: 3.7 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#include <stdio.h>
32#include <stdlib.h>
33#include <assert.h>
34#include <string.h>
35#include <netinet/in.h>
36#include <netinet/ip.h>
37#include <signal.h>
38#include <setjmp.h>
39#include <unistd.h>
40#include <sys/time.h>
41#include <stdint.h>
42#include "libtrace.h"
43#include "dagformat.h"
44
45struct libtrace_t *trace;
46
47
48#define ALPHA 0.9
49
50static int docalc = 0;
51
52typedef enum counter_type {
53        BYTES = 0,
54        PACKETS = 1,
55        LOSS = 2,
56} counter_type_t;
57
58#define MAXCOUNTERTYPE (LOSS + 1)
59
60uint32_t counter[MAXCOUNTERTYPE];
61
62struct timeval current,last,diff,total;
63
64void alarmsig(int sig) {
65        docalc++;
66}
67
68void secondreport() {
69
70        static int hdrcount = 10;
71
72        if (hdrcount >= 10) {
73                printf("Byte count:     Packet count:           Loss count\n");
74                hdrcount = 0;
75        }
76        hdrcount++;
77        printf("\t\t%d\t\t%d\t\t%d \n", 
78                        counter[BYTES],
79                        counter[PACKETS],
80                        counter[LOSS]);
81        counter[BYTES] = 0;
82        counter[PACKETS] = 0;
83        counter[LOSS] = 0;
84        docalc=0;
85}
86int main(int argc, char *argv[]) {
87
88        char *uri = 0;
89        char *filename = 0;
90        FILE *fout = 0;
91        int psize = 0;
92        struct sigaction sigact;
93        dag_record_t *erfptr = 0;
94        struct libtrace_packet_t packet;
95
96        struct itimerval itv;
97
98        /*
99         * Set up a timer to expire every second, for reporting
100         */
101        sigact.sa_handler = alarmsig;
102        sigact.sa_flags = SA_RESTART;
103        if(sigaction(SIGALRM, &sigact, NULL) < 0)
104                perror("sigaction");
105        itv.it_interval.tv_sec = 1;
106        itv.it_interval.tv_usec = 0;
107        itv.it_value.tv_sec = 1;
108        itv.it_value.tv_usec = 0;
109        if (setitimer(ITIMER_REAL, &itv, NULL) < 0)
110                perror("setitimer");
111
112        if (argc == 2) {
113                uri = strdup(argv[1]);
114                filename = 0;
115        }
116
117        if (argc == 3) {
118                uri = strdup(argv[1]);
119                filename = strdup(argv[2]);
120                fout = fopen(filename,"w");
121        }
122       
123        // create an trace to uri
124        trace = trace_create(uri);
125
126
127        for (;;) {
128                if ((psize = trace_read_packet(trace,&packet)) == -1) {
129                        // terminate
130                        break;
131                }
132                if (psize == 0) {
133                        continue;
134                }
135
136                erfptr = (dag_record_t *)(&packet.buffer);
137
138                counter[BYTES] += ntohs(erfptr->rlen);
139                counter[PACKETS] ++;
140                counter[LOSS] += ntohs(erfptr->lctr);
141
142                if(docalc) {
143                        secondreport();
144                }
145               
146                if (filename) 
147                        fwrite(erfptr,psize,1,fout);
148
149
150        }
151
152        trace_destroy(trace);
153        fclose(fout);
154        return 0;
155}
Note: See TracBrowser for help on using the repository browser.