Fix for bug 881. Adding processing of error packet when server greeting is expected.
[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  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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 /*
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
61  * a UNIX libpcap.)
62  *
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.
67  *
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.
70  */
71 #if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__) && \
72     !defined(__bsdi__) && !defined(__APPLE__) && !defined(_WIN32) && \
73     !defined(__CYGWIN__)
74 # define MUST_DO_SELECT
75 #endif
76
77 typedef void (*capture_packet_cb_fct)(u_char *, const struct pcap_pkthdr *, const u_char *);
78
79
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 {
84   /* common */
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 */
89
90   jmp_buf        stopenv;               /* T: starting point of loop (jump back this point on SIG...) */
91
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 */
94
95   /* pcap "input file" */
96   pcap_t        *pcap_h;                /* pcap handle */
97   gboolean       pcap_err;              /* E: TRUE if error from pcap */
98 #ifdef MUST_DO_SELECT
99   int            pcap_fd;               /* pcap file descriptor */
100 #endif
101
102   /* capture pipe (unix only "input file") */
103   gboolean       from_cap_pipe;         /* TRUE if we are capturing data from a capture pipe */
104 #ifndef _WIN32
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 */
112   enum {
113          STATE_EXPECT_REC_HDR,
114          STATE_READ_REC_HDR,
115          STATE_EXPECT_DATA,
116          STATE_READ_DATA
117        } cap_pipe_state;
118   enum { PIPOK, PIPEOF, PIPERR, PIPNEXIST } cap_pipe_err;
119 #endif
120
121   /* output file */
122   FILE          *pdh;
123   int            linktype;
124   gint           wtap_linktype;
125   long           bytes_written;
126
127 } loop_data;
128
129
130
131 /** init the capture filter */
132 typedef enum {
133   INITFILTER_NO_ERROR,
134   INITFILTER_BAD_FILTER,
135   INITFILTER_OTHER_ERROR
136 } initfilter_status_t;
137
138 extern initfilter_status_t
139 capture_loop_init_filter(pcap_t *pcap_h, gboolean from_cap_pipe, const gchar * iface, gchar * cfilter);
140
141 #ifdef HAVE_LIBPCAP
142 #ifndef _WIN32
143 extern int 
144 cap_pipe_dispatch(loop_data *, guchar *, char *, int);
145 #endif /* _WIN32 */
146 #endif
147
148 extern gboolean 
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);
152
153 extern gboolean
154 capture_loop_open_output(capture_options *capture_opts, int *save_file_fd, char *errmsg, int errmsg_len);
155
156 extern gboolean
157 capture_loop_init_output(capture_options *capture_opts, int save_file_fd, loop_data *ld, char *errmsg, int errmsg_len);
158
159 extern gboolean 
160 capture_loop_close_output(capture_options *capture_opts, loop_data *ld, int *err_close);
161
162 /*
163  * Routines called by the capture loop code to report things.
164  */
165
166 /** Report a new capture file having been opened. */
167 extern void
168 report_new_capture_file(const char *filename);
169
170 /** Report a number of new packets captured. */
171 extern void
172 report_packet_count(int packet_count);
173
174 /** Report the packet drops once the capture finishes. */
175 extern void
176 report_packet_drops(int drops);
177
178 /** Report an error in the capture. */
179 extern void 
180 report_capture_error(const char *error_msg, const char *secondary_error_msg);
181
182 /** Report an error with a capture filter. */
183 extern void
184 report_cfilter_error(const char *cfilter, const char *errmsg);
185
186
187 #endif /* capture_loop.h */