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