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 | LOSS = 2, |
---|
56 | } counter_type_t; |
---|
57 | |
---|
58 | #define MAXCOUNTERTYPE (LOSS + 1) |
---|
59 | |
---|
60 | uint32_t counter[MAXCOUNTERTYPE]; |
---|
61 | |
---|
62 | struct timeval current,last,diff,total; |
---|
63 | |
---|
64 | void alarmsig(int sig) { |
---|
65 | docalc++; |
---|
66 | } |
---|
67 | |
---|
68 | void 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 | } |
---|
86 | int 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 == 1) { |
---|
113 | printf("usage: capture <uri> [<output filename>]\n"); |
---|
114 | exit(0); |
---|
115 | } |
---|
116 | if (argc == 2) { |
---|
117 | uri = strdup(argv[1]); |
---|
118 | filename = 0; |
---|
119 | } |
---|
120 | |
---|
121 | if (argc == 3) { |
---|
122 | uri = strdup(argv[1]); |
---|
123 | filename = strdup(argv[2]); |
---|
124 | fout = fopen(filename,"w"); |
---|
125 | } |
---|
126 | |
---|
127 | // create an trace to uri |
---|
128 | trace = trace_create(uri); |
---|
129 | |
---|
130 | |
---|
131 | for (;;) { |
---|
132 | if ((psize = trace_read_packet(trace,&packet)) == -1) { |
---|
133 | // terminate |
---|
134 | break; |
---|
135 | } |
---|
136 | if (psize == 0) { |
---|
137 | continue; |
---|
138 | } |
---|
139 | |
---|
140 | erfptr = (dag_record_t *)(&packet.buffer); |
---|
141 | |
---|
142 | counter[BYTES] += ntohs(erfptr->rlen); |
---|
143 | counter[PACKETS] ++; |
---|
144 | counter[LOSS] += ntohs(erfptr->lctr); |
---|
145 | |
---|
146 | if(docalc) { |
---|
147 | secondreport(); |
---|
148 | } |
---|
149 | |
---|
150 | if (filename) |
---|
151 | fwrite(erfptr,psize,1,fout); |
---|
152 | |
---|
153 | |
---|
154 | } |
---|
155 | |
---|
156 | trace_destroy(trace); |
---|
157 | fclose(fout); |
---|
158 | return 0; |
---|
159 | } |
---|