1 | // $Id$ |
---|
2 | |
---|
3 | #include <stdio.h> |
---|
4 | #include <stdlib.h> |
---|
5 | #include <assert.h> |
---|
6 | #include <string.h> |
---|
7 | #include <netinet/in.h> |
---|
8 | #include <netinet/ip.h> |
---|
9 | #include <signal.h> |
---|
10 | #include <setjmp.h> |
---|
11 | #include <unistd.h> |
---|
12 | #include <sys/time.h> |
---|
13 | #include <stdint.h> |
---|
14 | #include "libtrace.h" |
---|
15 | #include "dagformat.h" |
---|
16 | struct libtrace_t *trace; |
---|
17 | |
---|
18 | #define SCANSIZE 4096 |
---|
19 | |
---|
20 | #define ALPHA 0.9 |
---|
21 | |
---|
22 | char *buffer[SCANSIZE]; |
---|
23 | |
---|
24 | static int docalc = 0; |
---|
25 | |
---|
26 | typedef enum counter_type { |
---|
27 | BYTES = 0, |
---|
28 | PACKETS = 1 |
---|
29 | } counter_type_t; |
---|
30 | |
---|
31 | typedef enum counter_frame { |
---|
32 | INSTANT = 0, |
---|
33 | SMOOTHED = 1 |
---|
34 | } counter_frame_t; |
---|
35 | |
---|
36 | #define MAXCOUNTERTYPE (PACKETS + 1) |
---|
37 | #define MAXCOUNTERFRAME (SMOOTHED + 1) |
---|
38 | |
---|
39 | int32_t counter[MAXCOUNTERTYPE][MAXCOUNTERFRAME]; |
---|
40 | |
---|
41 | struct timeval current,last,diff,total; |
---|
42 | |
---|
43 | void alarmsig(int sig) { |
---|
44 | docalc++; |
---|
45 | } |
---|
46 | |
---|
47 | void secondreport() { |
---|
48 | |
---|
49 | static int hdrcount = 10; |
---|
50 | |
---|
51 | if (hdrcount >= 10) { |
---|
52 | printf("Byte count: smoothed[instant] Packet count: smoothed[instant]\n"); |
---|
53 | hdrcount = 0; |
---|
54 | } |
---|
55 | hdrcount++; |
---|
56 | counter[BYTES][SMOOTHED] = ALPHA * counter[BYTES][SMOOTHED] + (1 - ALPHA) * counter[BYTES][INSTANT]; |
---|
57 | counter[PACKETS][SMOOTHED] = ALPHA * counter[PACKETS][SMOOTHED] + (1 - ALPHA) * counter[PACKETS][INSTANT]; |
---|
58 | |
---|
59 | printf("\t\t%d[%d]\t\t\t%d[%d] \n", |
---|
60 | counter[BYTES][SMOOTHED], |
---|
61 | counter[BYTES][INSTANT], |
---|
62 | counter[PACKETS][SMOOTHED], |
---|
63 | counter[PACKETS][INSTANT]); |
---|
64 | counter[BYTES][INSTANT] = 0; |
---|
65 | counter[PACKETS][INSTANT] = 0; |
---|
66 | docalc=0; |
---|
67 | } |
---|
68 | int main(int argc, char *argv[]) { |
---|
69 | |
---|
70 | char *hostname = "rtclient:chasm.cs.waikato.ac.nz"; |
---|
71 | int psize = 0; |
---|
72 | int status = 0; |
---|
73 | struct sigaction sigact; |
---|
74 | struct libtrace_ip *ipptr = 0; |
---|
75 | |
---|
76 | struct itimerval itv; |
---|
77 | |
---|
78 | /* |
---|
79 | * Set up a timer to expire every second, for reporting |
---|
80 | */ |
---|
81 | sigact.sa_handler = alarmsig; |
---|
82 | sigact.sa_flags = SA_RESTART; |
---|
83 | if(sigaction(SIGALRM, &sigact, NULL) < 0) |
---|
84 | perror("sigaction"); |
---|
85 | itv.it_interval.tv_sec = 1; |
---|
86 | itv.it_interval.tv_usec = 0; |
---|
87 | itv.it_value.tv_sec = 1; |
---|
88 | itv.it_value.tv_usec = 0; |
---|
89 | if (setitimer(ITIMER_REAL, &itv, NULL) < 0) |
---|
90 | perror("setitimer"); |
---|
91 | |
---|
92 | if (argc == 2) { |
---|
93 | hostname = strdup(argv[1]); |
---|
94 | } |
---|
95 | |
---|
96 | // create an trace to hostname, on the default port |
---|
97 | trace = create_trace(hostname); |
---|
98 | |
---|
99 | |
---|
100 | for (;;) { |
---|
101 | if ((psize = libtrace_read_packet(trace, buffer,SCANSIZE, &status)) == -1) { |
---|
102 | // terminate |
---|
103 | break; |
---|
104 | } |
---|
105 | if (psize == 0) { |
---|
106 | continue; |
---|
107 | } |
---|
108 | |
---|
109 | //erfptr = (dag_record_t *)buffer; |
---|
110 | //ipptr = (struct ip *)erfptr->rec.eth.pload; |
---|
111 | ipptr = get_ip(trace,buffer,SCANSIZE); |
---|
112 | counter[BYTES][INSTANT] += ntohs(ipptr->ip_len); |
---|
113 | counter[PACKETS][INSTANT] ++; |
---|
114 | |
---|
115 | if(docalc) { |
---|
116 | secondreport(); |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | } |
---|
121 | |
---|
122 | destroy_trace(trace); |
---|
123 | return 0; |
---|
124 | } |
---|