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 do_w_cksum = 0; |
---|
92 | static void usage(); |
---|
93 | static void parse_cmdline(int argc, char **argv); |
---|
94 | |
---|
95 | int main(int argc, char **argv) { |
---|
96 | |
---|
97 | struct libtrace_ip *ipptr = 0; |
---|
98 | |
---|
99 | int status; // need to pass to rtclient_read_packet |
---|
100 | int psize; |
---|
101 | |
---|
102 | parse_cmdline(argc,argv); |
---|
103 | |
---|
104 | // create an rtclient to hostname, on the default port |
---|
105 | trace = create_trace(uri); |
---|
106 | if (filterstring) { |
---|
107 | filter = libtrace_bpf_setfilter(filterstring); |
---|
108 | } |
---|
109 | |
---|
110 | for (;;) { |
---|
111 | if ((psize = libtrace_read_packet(trace, buffer,4096, &status)) <= 0) { |
---|
112 | // terminate |
---|
113 | break; |
---|
114 | } |
---|
115 | if (filter) { |
---|
116 | if (!libtrace_bpf_filter(trace, filter, buffer, 4096)) { |
---|
117 | continue; |
---|
118 | } |
---|
119 | } |
---|
120 | ipptr = get_ip(trace,buffer,4096); |
---|
121 | |
---|
122 | if (ipptr) { |
---|
123 | if(do_cksum && IN_CHKSUM(ipptr)) { |
---|
124 | badchksum ++; |
---|
125 | } else if (do_w_cksum && ipptr->ip_sum) { |
---|
126 | badchksum ++; |
---|
127 | } else { |
---|
128 | printf("%d:%d\n",ipptr->ip_p,get_link_type(trace,buffer,4096)); |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | if (do_cksum || do_w_cksum) { |
---|
133 | printf("Bad checksums seen: %llu\n",badchksum); |
---|
134 | } |
---|
135 | destroy_trace(trace); |
---|
136 | return 0; |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | static void usage(char *prog) { |
---|
141 | printf("usage: %s [-h] [-c | -w] [-u <uri>] [-f <filterstring>]\n",prog); |
---|
142 | printf(" -h this help message\n"); |
---|
143 | printf(" -c perform ip checksum test\n"); |
---|
144 | printf(" -w check WDCAPd ip checksum value\n"); |
---|
145 | printf(" -u uri uri to connect to\n"); |
---|
146 | printf(" -f filterstring BPF filterstring to apply\n"); |
---|
147 | printf("\n"); |
---|
148 | 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"); |
---|
149 | } |
---|
150 | |
---|
151 | static void parse_cmdline(int argc, char **argv){ |
---|
152 | int opt; |
---|
153 | if (argc == 1) { |
---|
154 | usage(argv[0]); |
---|
155 | exit(0); |
---|
156 | } |
---|
157 | |
---|
158 | while ((opt = getopt(argc,argv, "hcwu:f:")) != EOF) { |
---|
159 | switch(opt) { |
---|
160 | case 'h': |
---|
161 | usage(argv[0]); |
---|
162 | exit(0); |
---|
163 | case 'c': |
---|
164 | do_cksum = 1; |
---|
165 | break; |
---|
166 | case 'w': |
---|
167 | do_w_cksum = 1; |
---|
168 | break; |
---|
169 | case 'u': |
---|
170 | uri = strdup(optarg); |
---|
171 | break; |
---|
172 | case 'f': |
---|
173 | filterstring = strdup(optarg); |
---|
174 | break; |
---|
175 | default: |
---|
176 | usage(argv[0]); |
---|
177 | exit(0); |
---|
178 | } |
---|
179 | |
---|
180 | } |
---|
181 | |
---|
182 | if (do_cksum && do_w_cksum) { |
---|
183 | usage(argv[0]); |
---|
184 | exit(0); |
---|
185 | } |
---|
186 | |
---|
187 | } |
---|