2 * Do the low-level work of a capture
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 * Do the low-level work of a capture.
32 #ifndef __CAPTURE_LOOP_H__
33 #define __CAPTURE_LOOP_H__
37 * Get information about libpcap format from "wiretap/libpcap.h".
38 * XXX - can we just use pcap_open_offline() to read the pipe?
40 #include "wiretap/libpcap.h"
43 /** Do the low-level work of a capture.
44 * Returns TRUE if it succeeds, FALSE otherwise. */
45 extern int capture_loop_start(capture_options *capture_opts, gboolean *stats_known, struct pcap_stat *stats);
47 /** Stop a low-level capture (stops the capture child). */
48 extern void capture_loop_stop(void);
51 /*** the following is internal only (should be moved to capture_loop_int.h) ***/
55 * We don't want to do a "select()" on the pcap_t's file descriptor on
56 * BSD (because "select()" doesn't work correctly on BPF devices on at
57 * least some releases of some flavors of BSD), and we don't want to do
58 * it on Windows (because "select()" is something for sockets, not for
59 * arbitrary handles). (Note that "Windows" here includes Cygwin;
60 * even in its pretend-it's-UNIX environment, we're using WinPcap, not
63 * We *do* want to do it on other platforms, as, on other platforms (with
64 * the possible exception of Ultrix and Digital UNIX), the read timeout
65 * doesn't expire if no packets have arrived, so a "pcap_dispatch()" call
66 * will block until packets arrive, causing the UI to hang.
68 * XXX - the various BSDs appear to define BSD in <sys/param.h>; we don't
69 * want to include it if it's not present on this platform, however.
71 #if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__) && \
72 !defined(__bsdi__) && !defined(__APPLE__) && !defined(_WIN32) && \
74 # define MUST_DO_SELECT
77 typedef void (*capture_packet_cb_fct)(u_char *, const struct pcap_pkthdr *, const u_char *);
80 /* moved from capture_loop.c here, so we can combine it (and the related functions) with tethereal */
81 /* XXX - should be moved back to capture_loop.c */
82 /* E: capture_loop.c only (Ethereal/dumpcap) T: tethereal only */
83 typedef struct _loop_data {
85 gboolean go; /* TRUE as long as we're supposed to keep capturing */
86 int err; /* E: if non-zero, error seen while capturing */
87 gint packet_count; /* Number of packets we have already captured */
88 gint packet_max; /* E: Number of packets we're supposed to capture - 0 means infinite */
90 jmp_buf stopenv; /* T: starting point of loop (jump back this point on SIG...) */
92 char *save_file; /* T: Name of file to which we're writing */
93 capture_packet_cb_fct packet_cb; /* callback for a single captured packet */
95 /* pcap "input file" */
96 pcap_t *pcap_h; /* pcap handle */
97 gboolean pcap_err; /* E: TRUE if error from pcap */
99 int pcap_fd; /* pcap file descriptor */
102 /* capture pipe (unix only "input file") */
103 gboolean from_cap_pipe; /* TRUE if we are capturing data from a capture pipe */
105 struct pcap_hdr cap_pipe_hdr; /* ? */
106 struct pcaprec_modified_hdr cap_pipe_rechdr; /* ? */
107 int cap_pipe_fd; /* the file descriptor of the capture pipe */
108 gboolean cap_pipe_modified; /* TRUE if data in the pipe uses modified pcap headers */
109 gboolean cap_pipe_byte_swapped; /* TRUE if data in the pipe is byte swapped */
110 unsigned int cap_pipe_bytes_to_read;/* Used by cap_pipe_dispatch */
111 unsigned int cap_pipe_bytes_read; /* Used by cap_pipe_dispatch */
113 STATE_EXPECT_REC_HDR,
118 enum { PIPOK, PIPEOF, PIPERR, PIPNEXIST } cap_pipe_err;
131 /** init the capture filter */
134 INITFILTER_BAD_FILTER,
135 INITFILTER_OTHER_ERROR
136 } initfilter_status_t;
138 extern initfilter_status_t
139 capture_loop_init_filter(pcap_t *pcap_h, gboolean from_cap_pipe, const gchar * iface, gchar * cfilter);
144 cap_pipe_dispatch(loop_data *, guchar *, char *, int);
149 capture_loop_open_input(capture_options *capture_opts, loop_data *ld,
150 char *errmsg, size_t errmsg_len,
151 char *secondary_errmsg, size_t secondary_errmsg_len);
154 capture_loop_open_output(capture_options *capture_opts, int *save_file_fd, char *errmsg, int errmsg_len);
157 capture_loop_init_output(capture_options *capture_opts, int save_file_fd, loop_data *ld, char *errmsg, int errmsg_len);
160 capture_loop_close_output(capture_options *capture_opts, loop_data *ld, int *err_close);
163 * Routines called by the capture loop code to report things.
166 /** Report a new capture file having been opened. */
168 report_new_capture_file(const char *filename);
170 /** Report a number of new packets captured. */
172 report_packet_count(int packet_count);
174 /** Report the packet drops once the capture finishes. */
176 report_packet_drops(int drops);
178 /** Report an error in the capture. */
180 report_capture_error(const char *error_msg, const char *secondary_error_msg);
182 /** Report an error with a capture filter. */
184 report_cfilter_error(const char *cfilter, const char *errmsg);
187 #endif /* capture_loop.h */