source: examples/capture/capture.c @ aca3ff4

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

changes in the 'capture' program

  • Property mode set to 100644
File size: 4.2 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 "zlib.h"
43#include "libtrace.h"
44#include "dagformat.h"
45
46struct libtrace_t *trace;
47
48
49#define ALPHA 0.9
50
51static int docalc = 0;
52
53typedef enum counter_type {
54        BYTES = 0,
55        PACKETS = 1,
56        LOSS = 2,
57} counter_type_t;
58
59#define MAXCOUNTERTYPE (LOSS + 1)
60
61uint32_t counter[MAXCOUNTERTYPE];
62
63struct timeval current,last,diff,total;
64
65void alarmsig(int sig) {
66        docalc++;
67}
68
69#define BUFSIZE 65536
70
71
72void secondreport() {
73
74        static int hdrcount = 10;
75
76        if (hdrcount >= 10) {
77                fprintf(stderr,"Byte count:     Packet count:           Loss count\n");
78                hdrcount = 0;
79        }
80        hdrcount++;
81        fprintf(stderr,"\t\t%d\t\t%d\t\t%d \n", 
82                        counter[BYTES],
83                        counter[PACKETS],
84                        counter[LOSS]);
85        counter[BYTES] = 0;
86        counter[PACKETS] = 0;
87        counter[LOSS] = 0;
88        docalc=0;
89}
90int main(int argc, char *argv[]) {
91
92        char *uri = 0;
93        char *filename = 0;
94        gzFile *fout = 0;
95        int psize = 0;
96        int offset = 0;
97        struct sigaction sigact;
98        dag_record_t *erfptr = 0;
99        struct libtrace_packet_t packet;
100
101        struct itimerval itv;
102        void *buffer, *buffer2;
103
104
105        buffer = malloc(BUFSIZE);
106        buffer2 = buffer;
107
108        /*
109         * Set up a timer to expire every second, for reporting
110         */
111        sigact.sa_handler = alarmsig;
112        sigact.sa_flags = SA_RESTART;
113        if(sigaction(SIGALRM, &sigact, NULL) < 0)
114                perror("sigaction");
115        itv.it_interval.tv_sec = 1;
116        itv.it_interval.tv_usec = 0;
117        itv.it_value.tv_sec = 1;
118        itv.it_value.tv_usec = 0;
119        if (setitimer(ITIMER_REAL, &itv, NULL) < 0)
120                perror("setitimer");
121
122        if (argc == 1) {
123                printf("usage: capture <uri> [<output filename>]\n");
124                exit(0);
125        }
126        if (argc == 2) {
127                uri = strdup(argv[1]);
128                filename = 0;
129        }
130
131        if (argc == 3) {
132                uri = strdup(argv[1]);
133                filename = strdup(argv[2]);
134                fout = gzopen(filename,"ab");
135        }
136       
137        // create an trace to uri
138        trace = trace_create(uri);
139
140
141        for (;;) {
142                if ((psize = trace_read_packet(trace,&packet)) == -1) {
143                        // terminate
144                        break;
145                }
146                if (psize == 0) {
147                        continue;
148                }
149
150                erfptr = (dag_record_t *)(&packet.buffer);
151
152                counter[BYTES] += ntohs(erfptr->rlen);
153                counter[PACKETS] ++;
154                counter[LOSS] += ntohs(erfptr->lctr);
155
156                if(docalc) {
157                        secondreport();
158                }
159               
160                if (filename) {
161                        if (offset + psize > BUFSIZE) {
162                                //gzwrite(fout,erfptr,psize);
163                                fprintf(stderr,"writing to disk\n");
164                                gzwrite(fout,buffer,offset);
165                                offset = 0;
166                        }
167                       
168                        memcpy(buffer + offset,erfptr,psize);
169                        offset += psize;
170
171
172                }
173
174
175        }
176
177        trace_destroy(trace);
178        gzclose(fout);
179        return 0;
180}
Note: See TracBrowser for help on using the repository browser.