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 "common.h" |
---|
32 | #include "config.h" |
---|
33 | #include "libtrace.h" |
---|
34 | #include "format.h" |
---|
35 | #include <inttypes.h> |
---|
36 | #include <sys/types.h> |
---|
37 | #include <sys/stat.h> |
---|
38 | #include <unistd.h> |
---|
39 | #include <assert.h> |
---|
40 | |
---|
41 | |
---|
42 | #if HAVE_PCAP_BPF_H |
---|
43 | # include <pcap-bpf.h> |
---|
44 | #else |
---|
45 | # ifdef HAVE_NET_BPF_H |
---|
46 | # include <net/bpf.h> |
---|
47 | # endif |
---|
48 | #endif |
---|
49 | |
---|
50 | #if HAVE_PCAP_H |
---|
51 | # include <pcap.h> |
---|
52 | # ifdef HAVE_PCAP_INT_H |
---|
53 | # include <pcap-int.h> |
---|
54 | # endif |
---|
55 | #endif |
---|
56 | |
---|
57 | #if HAVE_PCAP |
---|
58 | |
---|
59 | static int pcap_init_input(struct libtrace_t *libtrace) { |
---|
60 | char errbuf[PCAP_ERRBUF_SIZE]; |
---|
61 | struct stat buf; |
---|
62 | if (!strncmp(libtrace->conn_info.path,"-",1)) { |
---|
63 | if ((libtrace->input.pcap = |
---|
64 | pcap_open_offline(libtrace->conn_info.path, |
---|
65 | errbuf)) == NULL) { |
---|
66 | fprintf(stderr,"%s\n",errbuf); |
---|
67 | return 0; |
---|
68 | } |
---|
69 | } else { |
---|
70 | if (stat(libtrace->conn_info.path,&buf) == -1) { |
---|
71 | perror("stat"); |
---|
72 | return 0; |
---|
73 | } |
---|
74 | if (S_ISCHR(buf.st_mode)) { |
---|
75 | if ((libtrace->input.pcap = |
---|
76 | pcap_open_live(libtrace->conn_info.path, |
---|
77 | 4096, |
---|
78 | 1, |
---|
79 | 1, |
---|
80 | errbuf)) == NULL) { |
---|
81 | fprintf(stderr,"%s\n",errbuf); |
---|
82 | return 0; |
---|
83 | } |
---|
84 | } else { |
---|
85 | if ((libtrace->input.pcap = |
---|
86 | pcap_open_offline(libtrace->conn_info.path, |
---|
87 | errbuf)) == NULL) { |
---|
88 | fprintf(stderr,"%s\n",errbuf); |
---|
89 | return 0; |
---|
90 | } |
---|
91 | } |
---|
92 | } |
---|
93 | fprintf(stderr, |
---|
94 | "Unsupported scheme (%s) for format pcap\n", |
---|
95 | libtrace->conn_info.path); |
---|
96 | return 0; |
---|
97 | |
---|
98 | } |
---|
99 | |
---|
100 | static int pcapint_init_input(struct libtrace_t *libtrace) { |
---|
101 | char errbuf[PCAP_ERRBUF_SIZE]; |
---|
102 | if ((libtrace->input.pcap = |
---|
103 | pcap_open_live(libtrace->conn_info.path, |
---|
104 | 4096, |
---|
105 | 1, |
---|
106 | 1, |
---|
107 | errbuf)) == NULL) { |
---|
108 | fprintf(stderr,"%s\n",errbuf); |
---|
109 | return 0; |
---|
110 | } |
---|
111 | |
---|
112 | } |
---|
113 | |
---|
114 | static int pcap_fin_input(struct libtrace_t *libtrace) { |
---|
115 | return -1; |
---|
116 | } |
---|
117 | |
---|
118 | static void trace_pcap_handler(u_char *user, const struct pcap_pkthdr *pcaphdr, const u_char *pcappkt) { |
---|
119 | struct libtrace_packet_t *packet = (struct libtrace_packet_t *)user; |
---|
120 | void *buffer = packet->buffer; |
---|
121 | int numbytes = 0; |
---|
122 | |
---|
123 | memcpy(buffer,pcaphdr,sizeof(struct pcap_pkthdr)); |
---|
124 | numbytes = pcaphdr->len; |
---|
125 | memcpy(buffer + sizeof(struct pcap_pkthdr),pcappkt,numbytes); |
---|
126 | |
---|
127 | packet->size = numbytes + sizeof(struct pcap_pkthdr); |
---|
128 | |
---|
129 | } |
---|
130 | static int pcap_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
131 | const u_char *pcappkt; |
---|
132 | int pcapbytes = 0; |
---|
133 | |
---|
134 | while ((pcapbytes = pcap_dispatch(libtrace->input.pcap, |
---|
135 | 1, /* number of packets */ |
---|
136 | &trace_pcap_handler, |
---|
137 | (u_char *)packet)) == 0); |
---|
138 | |
---|
139 | if (pcapbytes < 0) { |
---|
140 | return -1; |
---|
141 | } |
---|
142 | return (packet->size - sizeof(struct pcap_pkthdr)); |
---|
143 | } |
---|
144 | |
---|
145 | static void *pcap_get_link(const struct libtrace_packet_t *packet) { |
---|
146 | return (void *) (packet->buffer + sizeof(struct pcap_pkthdr)); |
---|
147 | } |
---|
148 | |
---|
149 | static libtrace_linktype_t pcap_get_link_type(const struct libtrace_packet_t *packet) { |
---|
150 | struct pcap_pkthdr *pcapptr = 0; |
---|
151 | int linktype = 0; |
---|
152 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
153 | linktype = pcap_datalink(packet->trace->input.pcap); |
---|
154 | switch(linktype) { |
---|
155 | case DLT_NULL: |
---|
156 | return TRACE_TYPE_NONE; |
---|
157 | case DLT_EN10MB: |
---|
158 | return TRACE_TYPE_ETH; |
---|
159 | case DLT_ATM_RFC1483: |
---|
160 | return TRACE_TYPE_ATM; |
---|
161 | case DLT_IEEE802_11: |
---|
162 | return TRACE_TYPE_80211; |
---|
163 | #ifdef DLT_LINUX_SLL |
---|
164 | case DLT_LINUX_SLL: |
---|
165 | return TRACE_TYPE_LINUX_SLL; |
---|
166 | #endif |
---|
167 | #ifdef DLT_PFLOG |
---|
168 | case DLT_PFLOG: |
---|
169 | return TRACE_TYPE_PFLOG; |
---|
170 | #endif |
---|
171 | } |
---|
172 | return -1; |
---|
173 | } |
---|
174 | |
---|
175 | static int8_t pcap_get_direction(const struct libtrace_packet_t *packet) { |
---|
176 | int8_t direction = -1; |
---|
177 | switch(pcap_get_link_type(packet)) { |
---|
178 | case TRACE_TYPE_LINUX_SLL: |
---|
179 | { |
---|
180 | struct trace_sll_header_t *sll; |
---|
181 | sll = trace_get_link(packet); |
---|
182 | if (!sll) { |
---|
183 | return -1; |
---|
184 | } |
---|
185 | /* 0 == LINUX_SLL_HOST */ |
---|
186 | /* the Waikato Capture point defines "packets |
---|
187 | * originating locally" (ie, outbound), with a |
---|
188 | * direction of 0, and "packets destined locally" |
---|
189 | * (ie, inbound), with a direction of 1. |
---|
190 | * This is kind-of-opposite to LINUX_SLL. |
---|
191 | * We return consistent values here, however |
---|
192 | * |
---|
193 | * Note that in recent versions of pcap, you can |
---|
194 | * use "inbound" and "outbound" on ppp in linux |
---|
195 | */ |
---|
196 | if (ntohs(sll->pkttype == 0)) { |
---|
197 | direction = 1; |
---|
198 | } else { |
---|
199 | direction = 0; |
---|
200 | } |
---|
201 | break; |
---|
202 | |
---|
203 | } |
---|
204 | case TRACE_TYPE_PFLOG: |
---|
205 | { |
---|
206 | struct trace_pflog_header_t *pflog; |
---|
207 | pflog = trace_get_link(packet); |
---|
208 | if (!pflog) { |
---|
209 | return -1; |
---|
210 | } |
---|
211 | /* enum { PF_IN=0, PF_OUT=1 }; */ |
---|
212 | if (ntohs(pflog->dir==0)) { |
---|
213 | |
---|
214 | direction = 1; |
---|
215 | } |
---|
216 | else { |
---|
217 | direction = 0; |
---|
218 | } |
---|
219 | break; |
---|
220 | } |
---|
221 | default: |
---|
222 | break; |
---|
223 | } |
---|
224 | return direction; |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | static struct timeval pcap_get_timeval(const struct libtrace_packet_t *packet) { |
---|
229 | struct pcap_pkthdr *pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
230 | return pcapptr->ts; |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | static int pcap_get_capture_length(const struct libtrace_packet_t *packet) { |
---|
235 | struct pcap_pkthdr *pcapptr = 0; |
---|
236 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
237 | return pcapptr->caplen; |
---|
238 | } |
---|
239 | |
---|
240 | static int pcap_get_wire_length(const struct libtrace_packet_t *packet) { |
---|
241 | struct pcap_pkthdr *pcapptr = 0; |
---|
242 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
243 | return ntohs(pcapptr->len); |
---|
244 | } |
---|
245 | |
---|
246 | static size_t pcap_set_capture_length(struct libtrace_packet_t *packet,size_t size) { |
---|
247 | struct pcap_pkthdr *pcapptr = 0; |
---|
248 | assert(packet); |
---|
249 | if (size > packet->size) { |
---|
250 | // can't make a packet larger |
---|
251 | return packet->size; |
---|
252 | } |
---|
253 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
254 | pcapptr->caplen = size + sizeof(struct pcap_pkthdr); |
---|
255 | packet->size = pcapptr->caplen; |
---|
256 | return packet->size; |
---|
257 | } |
---|
258 | |
---|
259 | static void pcap_help() { |
---|
260 | printf("pcap format module: $Id$\n"); |
---|
261 | printf("Supported input URIs:\n"); |
---|
262 | printf("\tpcap:/path/to/file\n"); |
---|
263 | printf("\n"); |
---|
264 | printf("\te.g.: pcap:/tmp/trace.pcap\n"); |
---|
265 | printf("Supported output URIs:\n"); |
---|
266 | printf("\tnone\n"); |
---|
267 | } |
---|
268 | static void pcapint_help() { |
---|
269 | printf("pcapint format module: $Id$\n"); |
---|
270 | printf("Supported input URIs:\n"); |
---|
271 | printf("\tpcapint:interface\n"); |
---|
272 | printf("\n"); |
---|
273 | printf("\te.g.: pcapint:eth0\n"); |
---|
274 | printf("Supported output URIs:\n"); |
---|
275 | printf("\tnone\n"); |
---|
276 | } |
---|
277 | static struct format_t pcap = { |
---|
278 | "pcap", |
---|
279 | "$Id$", |
---|
280 | pcap_init_input, /* init_input */ |
---|
281 | NULL, /* init_output */ |
---|
282 | NULL, /* config_output */ |
---|
283 | pcap_fin_input, /* fin_input */ |
---|
284 | NULL, /* fin_output */ |
---|
285 | NULL, /* read */ |
---|
286 | pcap_read_packet, /* read_packet */ |
---|
287 | NULL, /* write_packet */ |
---|
288 | pcap_get_link, /* get_link */ |
---|
289 | pcap_get_link_type, /* get_link_type */ |
---|
290 | pcap_get_direction, /* get_direction */ |
---|
291 | NULL, /* set_direction */ |
---|
292 | NULL, /* get_erf_timestamp */ |
---|
293 | pcap_get_timeval, /* get_timeval */ |
---|
294 | NULL, /* get_seconds */ |
---|
295 | pcap_get_capture_length, /* get_capture_length */ |
---|
296 | pcap_get_wire_length, /* get_wire_length */ |
---|
297 | pcap_set_capture_length, /* set_capture_length */ |
---|
298 | pcap_help /* help */ |
---|
299 | }; |
---|
300 | |
---|
301 | static struct format_t pcapint = { |
---|
302 | "pcapint", |
---|
303 | "$Id$", |
---|
304 | pcapint_init_input, /* init_input */ |
---|
305 | NULL, /* init_output */ |
---|
306 | NULL, /* config_output */ |
---|
307 | pcap_fin_input, /* fin_input */ |
---|
308 | NULL, /* fin_output */ |
---|
309 | NULL, /* read */ |
---|
310 | pcap_read_packet, /* read_packet */ |
---|
311 | NULL, /* write_packet */ |
---|
312 | pcap_get_link, /* get_link */ |
---|
313 | pcap_get_link_type, /* get_link_type */ |
---|
314 | pcap_get_direction, /* get_direction */ |
---|
315 | NULL, /* set_direction */ |
---|
316 | NULL, /* get_erf_timestamp */ |
---|
317 | pcap_get_timeval, /* get_timeval */ |
---|
318 | NULL, /* get_seconds */ |
---|
319 | pcap_get_capture_length, /* get_capture_length */ |
---|
320 | pcap_get_wire_length, /* get_wire_length */ |
---|
321 | pcap_set_capture_length, /* set_capture_length */ |
---|
322 | pcapint_help /* help */ |
---|
323 | }; |
---|
324 | |
---|
325 | void __attribute__((constructor)) pcap_constructor() { |
---|
326 | register_format(&pcap); |
---|
327 | register_format(&pcapint); |
---|
328 | } |
---|
329 | |
---|
330 | |
---|
331 | #endif |
---|