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> /* printf */ |
---|
32 | #include <netinet/in.h> /* ntohs */ |
---|
33 | #include <netdb.h> |
---|
34 | #include "dagformat.h" |
---|
35 | #include <getopt.h> |
---|
36 | |
---|
37 | #include <stdlib.h> |
---|
38 | #include <string.h> |
---|
39 | |
---|
40 | #include <errno.h> |
---|
41 | |
---|
42 | #include <linux/types.h> |
---|
43 | //#include <linux/linkage.h> |
---|
44 | //#define access_ok(type,addr,size) 1 |
---|
45 | |
---|
46 | static inline unsigned short ip_fast_csum(unsigned char * iph, |
---|
47 | unsigned int ihl) { |
---|
48 | unsigned int sum; |
---|
49 | |
---|
50 | __asm__ __volatile__(" |
---|
51 | movl (%1), %0 |
---|
52 | subl $4, %2 |
---|
53 | jbe 2f |
---|
54 | addl 4(%1), %0 |
---|
55 | adcl 8(%1), %0 |
---|
56 | adcl 12(%1), %0 |
---|
57 | 1: adcl 16(%1), %0 |
---|
58 | lea 4(%1), %1 |
---|
59 | decl %2 |
---|
60 | jne 1b |
---|
61 | adcl $0, %0 |
---|
62 | movl %0, %2 |
---|
63 | shrl $16, %0 |
---|
64 | addw %w2, %w0 |
---|
65 | adcl $0, %0 |
---|
66 | notl %0 |
---|
67 | 2: |
---|
68 | " |
---|
69 | /* Since the input registers which are loaded with iph and ipl |
---|
70 | are modified, we must also specify them as outputs, or gcc |
---|
71 | will assume they contain their original values. */ |
---|
72 | : "=r" (sum), "=r" (iph), "=r" (ihl) |
---|
73 | : "1" (iph), "2" (ihl)); |
---|
74 | return(sum); |
---|
75 | } |
---|
76 | |
---|
77 | #define IN_CHKSUM(IP) ip_fast_csum((unsigned char *)(IP), 5) |
---|
78 | |
---|
79 | |
---|
80 | #include "libtrace.h" |
---|
81 | |
---|
82 | struct libtrace_t *trace; |
---|
83 | struct libtrace_filter_t *filter; |
---|
84 | |
---|
85 | char *buffer[4096]; |
---|
86 | uint64_t badchksum = 0; |
---|
87 | char *uri = 0; |
---|
88 | char *filterstring = 0; |
---|
89 | |
---|
90 | int do_cksum = 0; |
---|
91 | int loop = 0; |
---|
92 | int do_w_cksum = 0; |
---|
93 | uint64_t rxerr = 0; |
---|
94 | static void usage(); |
---|
95 | static void parse_cmdline(int argc, char **argv); |
---|
96 | |
---|
97 | int main(int argc, char **argv) { |
---|
98 | |
---|
99 | struct libtrace_ip *ipptr = 0; |
---|
100 | struct libtrace_packet_t *packet = trace_create_packet(); |
---|
101 | |
---|
102 | int psize; |
---|
103 | |
---|
104 | parse_cmdline(argc,argv); |
---|
105 | |
---|
106 | if (!uri) { |
---|
107 | printf("Incorrect usage: need a URI\n"); |
---|
108 | usage(argv[0]); |
---|
109 | } |
---|
110 | do { |
---|
111 | trace = trace_create(uri); |
---|
112 | if (filterstring) { |
---|
113 | filter = trace_bpf_setfilter(filterstring); |
---|
114 | } |
---|
115 | |
---|
116 | for (;;) { |
---|
117 | if ((psize = trace_read_packet(trace, &packet)) <= 0) { |
---|
118 | // terminate |
---|
119 | break; |
---|
120 | } |
---|
121 | |
---|
122 | if (filter) { |
---|
123 | if (!trace_bpf_filter(filter,&packet)) { |
---|
124 | continue; |
---|
125 | } |
---|
126 | } |
---|
127 | ipptr = trace_get_ip(&packet); |
---|
128 | |
---|
129 | if (ipptr) { |
---|
130 | if(do_cksum && IN_CHKSUM(ipptr)) { |
---|
131 | badchksum ++; |
---|
132 | } else if (do_w_cksum && ipptr->ip_sum) { |
---|
133 | badchksum ++; |
---|
134 | } else { |
---|
135 | printf("%d:%d\n",ipptr->ip_p,trace_get_link_type(&packet)); |
---|
136 | } |
---|
137 | } |
---|
138 | } |
---|
139 | if (do_cksum || do_w_cksum) { |
---|
140 | printf("Bad checksums seen: %llu\n",badchksum); |
---|
141 | printf("RX Errors seen: %llu\n",rxerr); |
---|
142 | } |
---|
143 | trace_destroy(trace); |
---|
144 | } while (loop); |
---|
145 | return 0; |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | static void usage(char *prog) { |
---|
150 | printf("usage: %s [-h] [-c | -w] [-u <uri>] [-f <filterstring>]\n",prog); |
---|
151 | printf(" -h this help message\n"); |
---|
152 | printf(" -c perform ip checksum test\n"); |
---|
153 | printf(" -w check WDCAPd ip checksum value\n"); |
---|
154 | printf(" -u uri uri to connect to\n"); |
---|
155 | printf(" -f filterstring BPF filterstring to apply\n"); |
---|
156 | printf(" -l loop the input\n"); |
---|
157 | printf("\n"); |
---|
158 | printf(" The use of -c and -w are exclusive: -c is used for normal traces, while -w applies to traces taken from the Waikato Capture point\n"); |
---|
159 | } |
---|
160 | |
---|
161 | static void parse_cmdline(int argc, char **argv){ |
---|
162 | int opt; |
---|
163 | if (argc == 1) { |
---|
164 | usage(argv[0]); |
---|
165 | exit(0); |
---|
166 | } |
---|
167 | |
---|
168 | while ((opt = getopt(argc,argv, "hcwu:f:l")) != EOF) { |
---|
169 | switch(opt) { |
---|
170 | case 'h': |
---|
171 | usage(argv[0]); |
---|
172 | exit(0); |
---|
173 | case 'c': |
---|
174 | do_cksum = 1; |
---|
175 | break; |
---|
176 | case 'w': |
---|
177 | do_w_cksum = 1; |
---|
178 | break; |
---|
179 | case 'u': |
---|
180 | uri = strdup(optarg); |
---|
181 | break; |
---|
182 | case 'f': |
---|
183 | filterstring = strdup(optarg); |
---|
184 | break; |
---|
185 | case 'l': |
---|
186 | loop = 1; |
---|
187 | break; |
---|
188 | default: |
---|
189 | usage(argv[0]); |
---|
190 | exit(0); |
---|
191 | } |
---|
192 | |
---|
193 | } |
---|
194 | |
---|
195 | if (do_cksum && do_w_cksum) { |
---|
196 | usage(argv[0]); |
---|
197 | exit(0); |
---|
198 | } |
---|
199 | |
---|
200 | } |
---|