Add SIP response code 494 from RFC 3329
[obnox/wireshark/wip.git] / capture_loop.h
1 /* capture_loop.h
2  * Do the low-level work of a capture
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25
26 /** @file
27  *  
28  *  Do the low-level work of a capture.
29  *
30  */
31
32 #ifndef __CAPTURE_LOOP_H__
33 #define __CAPTURE_LOOP_H__
34
35 #ifndef _WIN32
36 /*
37  * Get information about libpcap format from "wiretap/libpcap.h".
38  * XXX - can we just use pcap_open_offline() to read the pipe?
39  */
40 #include "wiretap/libpcap.h"
41 #endif
42
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);
46
47 /** Stop a low-level capture (stops the capture child). */
48 extern void capture_loop_stop(void);
49
50
51 /*** the following is internal only (should be moved to capture_loop_int.h) ***/
52
53
54 #ifndef HAVE_PCAP_BREAKLOOP
55 /*
56  * We don't have pcap_breakloop(), which is the only way to ensure that
57  * pcap_dispatch(), pcap_loop(), or even pcap_next() or pcap_next_ex()
58  * won't, if the call to read the next packet or batch of packets is
59  * is interrupted by a signal on UN*X, just go back and try again to
60  * read again.
61  *
62  * On UN*X, we catch SIGUSR1 as a "stop capturing" signal, and, in
63  * the signal handler, set a flag to stop capturing; however, without
64  * a guarantee of that sort, we can't guarantee that we'll stop capturing
65  * if the read will be retried and won't time out if no packets arrive.
66  *
67  * Therefore, on at least some platforms, we work around the lack of
68  * pcap_breakloop() by doing a select() on the pcap_t's file descriptor
69  * to wait for packets to arrive, so that we're probably going to be
70  * blocked in the select() when the signal arrives, and can just bail
71  * out of the loop at that point.
72  *
73  * However, we don't want to that on BSD (because "select()" doesn't work
74  * correctly on BPF devices on at least some releases of some flavors of
75  * BSD), and we don't want to do it on Windows (because "select()" is
76  * something for sockets, not for arbitrary handles).  (Note that "Windows"
77  * here includes Cygwin; even in its pretend-it's-UNIX environment, we're
78  * using WinPcap, not a UNIX libpcap.)
79  *
80  * Fortunately, we don't need to do it on BSD, because the libpcap timeout
81  * on BSD times out even if no packets have arrived, so we'll eventually
82  * exit pcap_dispatch() with an indication that no packets have arrived,
83  * and will break out of the capture loop at that point.
84  *
85  * On Windows, we can't send a SIGUSR1 to stop capturing, so none of this
86  * applies in any case.
87  *
88  * XXX - the various BSDs appear to define BSD in <sys/param.h>; we don't
89  * want to include it if it's not present on this platform, however.
90  */
91 # if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__) && \
92     !defined(__bsdi__) && !defined(__APPLE__) && !defined(_WIN32) && \
93     !defined(__CYGWIN__)
94 #  define MUST_DO_SELECT
95 # endif /* avoid select */
96 #endif /* HAVE_PCAP_BREAKLOOP */
97
98 typedef void (*capture_packet_cb_fct)(u_char *, const struct pcap_pkthdr *, const u_char *);
99
100
101 /* moved from capture_loop.c here, so we can combine it (and the related functions) with tshark */
102 /* XXX - should be moved back to capture_loop.c */
103 /* E: capture_loop.c only (Wireshark/dumpcap) T: tshark only */
104 typedef struct _loop_data {
105   /* common */
106   gboolean       go;                    /* TRUE as long as we're supposed to keep capturing */
107   int            err;                   /* E: if non-zero, error seen while capturing */
108   gint           packet_count;          /* Number of packets we have already captured */
109   gint           packet_max;            /* E: Number of packets we're supposed to capture - 0 means infinite */
110
111   jmp_buf        stopenv;               /* T: starting point of loop (jump back this point on SIG...) */
112
113   char          *save_file;             /* T: Name of file to which we're writing */
114   capture_packet_cb_fct  packet_cb;     /* callback for a single captured packet */
115
116   /* pcap "input file" */
117   pcap_t        *pcap_h;                /* pcap handle */
118   gboolean       pcap_err;              /* E: TRUE if error from pcap */
119 #ifdef MUST_DO_SELECT
120   int            pcap_fd;               /* pcap file descriptor */
121 #endif
122
123   /* capture pipe (unix only "input file") */
124   gboolean       from_cap_pipe;         /* TRUE if we are capturing data from a capture pipe */
125 #ifndef _WIN32
126   struct pcap_hdr cap_pipe_hdr;         /* ? */
127   struct pcaprec_modified_hdr cap_pipe_rechdr;  /* ? */
128   int            cap_pipe_fd;           /* the file descriptor of the capture pipe */
129   gboolean       cap_pipe_modified;     /* TRUE if data in the pipe uses modified pcap headers */
130   gboolean       cap_pipe_byte_swapped; /* TRUE if data in the pipe is byte swapped */
131   unsigned int   cap_pipe_bytes_to_read;/* Used by cap_pipe_dispatch */
132   unsigned int   cap_pipe_bytes_read;   /* Used by cap_pipe_dispatch */
133   enum {
134          STATE_EXPECT_REC_HDR,
135          STATE_READ_REC_HDR,
136          STATE_EXPECT_DATA,
137          STATE_READ_DATA
138        } cap_pipe_state;
139   enum { PIPOK, PIPEOF, PIPERR, PIPNEXIST } cap_pipe_err;
140 #endif
141
142   /* output file */
143   FILE          *pdh;
144   int            linktype;
145   gint           wtap_linktype;
146   long           bytes_written;
147
148 } loop_data;
149
150
151
152 /** init the capture filter */
153 typedef enum {
154   INITFILTER_NO_ERROR,
155   INITFILTER_BAD_FILTER,
156   INITFILTER_OTHER_ERROR
157 } initfilter_status_t;
158
159 extern initfilter_status_t
160 capture_loop_init_filter(pcap_t *pcap_h, gboolean from_cap_pipe, const gchar * iface, gchar * cfilter);
161
162 #ifdef HAVE_LIBPCAP
163 #ifndef _WIN32
164 extern int 
165 cap_pipe_dispatch(loop_data *, guchar *, char *, int);
166 #endif /* _WIN32 */
167 #endif
168
169 extern gboolean 
170 capture_loop_open_input(capture_options *capture_opts, loop_data *ld,
171                         char *errmsg, size_t errmsg_len,
172                         char *secondary_errmsg, size_t secondary_errmsg_len);
173
174 extern gboolean
175 capture_loop_open_output(capture_options *capture_opts, int *save_file_fd, char *errmsg, int errmsg_len);
176
177 extern gboolean
178 capture_loop_init_output(capture_options *capture_opts, int save_file_fd, loop_data *ld, char *errmsg, int errmsg_len);
179
180 extern gboolean 
181 capture_loop_close_output(capture_options *capture_opts, loop_data *ld, int *err_close);
182
183 /*
184  * Routines called by the capture loop code to report things.
185  */
186
187 /** Report a new capture file having been opened. */
188 extern void
189 report_new_capture_file(const char *filename);
190
191 /** Report a number of new packets captured. */
192 extern void
193 report_packet_count(int packet_count);
194
195 /** Report the packet drops once the capture finishes. */
196 extern void
197 report_packet_drops(int drops);
198
199 /** Report an error in the capture. */
200 extern void 
201 report_capture_error(const char *error_msg, const char *secondary_error_msg);
202
203 /** Report an error with a capture filter. */
204 extern void
205 report_cfilter_error(const char *cfilter, const char *errmsg);
206
207
208 #endif /* capture_loop.h */