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