2 * The actual capturing loop, getting packets and storing it
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
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 * Capture loop (internal interface).
30 * It will open the input and output files, capture the packets,
31 * change ringbuffer output files while capturing and close all files again.
33 * The input file can be a network interface or capture pipe (unix only).
34 * The output file can be a single or a ringbuffer file handled by wiretap.
54 #ifdef HAVE_SYS_TYPES_H
55 # include <sys/types.h>
58 #ifdef HAVE_SYS_STAT_H
59 # include <sys/stat.h>
73 #include "capture-pcap-util.h"
76 #include "capture_sync.h"
78 #include "conditions.h"
79 #include "capture_stop_conditions.h"
80 #include "ringbuffer.h"
82 #include "simple_dialog.h"
85 #include "file_util.h"
87 #include "epan/unicode-utils.h"
89 #include "capture_loop.h"
91 #ifdef NEED_G_ASCII_STRCASECMP_H
92 #include "epan/g_ascii_strcasecmp.h"
95 /* E: capture_loop.c only (Wireshark/dumpcap) T: tshark only */
96 typedef struct _loop_data {
98 gboolean go; /* TRUE as long as we're supposed to keep capturing */
99 int err; /* E: if non-zero, error seen while capturing */
100 gint packet_count; /* Number of packets we have already captured */
101 gint packet_max; /* E: Number of packets we're supposed to capture - 0 means infinite */
103 capture_packet_cb_fct packet_cb; /* callback for a single captured packet */
105 /* pcap "input file" */
106 pcap_t *pcap_h; /* pcap handle */
107 gboolean pcap_err; /* E: TRUE if error from pcap */
108 #ifdef MUST_DO_SELECT
109 int pcap_fd; /* pcap file descriptor */
112 /* capture pipe (unix only "input file") */
113 gboolean from_cap_pipe; /* TRUE if we are capturing data from a capture pipe */
114 struct pcap_hdr cap_pipe_hdr; /* Pcap header when capturing from a pipe */
115 struct pcaprec_modified_hdr cap_pipe_rechdr; /* Pcap record header when capturing from a pipe */
116 int cap_pipe_fd; /* the file descriptor of the capture pipe */
117 gboolean cap_pipe_modified; /* TRUE if data in the pipe uses modified pcap headers */
118 gboolean cap_pipe_byte_swapped; /* TRUE if data in the pipe is byte swapped */
119 unsigned int cap_pipe_bytes_to_read;/* Used by cap_pipe_dispatch */
120 unsigned int cap_pipe_bytes_read; /* Used by cap_pipe_dispatch */
122 STATE_EXPECT_REC_HDR,
127 enum { PIPOK, PIPEOF, PIPERR, PIPNEXIST } cap_pipe_err;
138 * Standard secondary message for unexpected errors.
140 static const char please_report[] =
141 "Please report this to the Wireshark developers.\n"
142 "(This is not a crash; please do not report it as such.)";
145 * This needs to be static, so that the SIGUSR1 handler can clear the "go"
152 * Timeout, in milliseconds, for reads from the stream of captured packets.
154 #define CAP_READ_TIMEOUT 250
155 static char *cap_pipe_err_str;
157 static void capture_loop_packet_cb(u_char *user, const struct pcap_pkthdr *phdr,
159 static void capture_loop_get_errmsg(char *errmsg, int errmsglen, const char *fname,
160 int err, gboolean is_close);
164 /* Take care of byte order in the libpcap headers read from pipes.
165 * (function taken from wiretap/libpcap.c) */
167 cap_pipe_adjust_header(gboolean byte_swapped, struct pcap_hdr *hdr, struct pcaprec_hdr *rechdr)
170 /* Byte-swap the record header fields. */
171 rechdr->ts_sec = BSWAP32(rechdr->ts_sec);
172 rechdr->ts_usec = BSWAP32(rechdr->ts_usec);
173 rechdr->incl_len = BSWAP32(rechdr->incl_len);
174 rechdr->orig_len = BSWAP32(rechdr->orig_len);
177 /* In file format version 2.3, the "incl_len" and "orig_len" fields were
178 swapped, in order to match the BPF header layout.
180 Unfortunately, some files were, according to a comment in the "libpcap"
181 source, written with version 2.3 in their headers but without the
182 interchanged fields, so if "incl_len" is greater than "orig_len" - which
183 would make no sense - we assume that we need to swap them. */
184 if (hdr->version_major == 2 &&
185 (hdr->version_minor < 3 ||
186 (hdr->version_minor == 3 && rechdr->incl_len > rechdr->orig_len))) {
189 temp = rechdr->orig_len;
190 rechdr->orig_len = rechdr->incl_len;
191 rechdr->incl_len = temp;
195 /* Provide select() functionality for a single file descriptor
196 * on both UNIX/POSIX and Windows.
198 * The Windows version calls WaitForSingleObject instead of
201 * Returns the same values as select. If an error is returned,
202 * the string cap_pipe_err_str should be used instead of errno.
205 cap_pipe_select(int pipe_fd) {
208 struct timeval timeout, *pto;
211 cap_pipe_err_str = "Unknown error";
214 FD_SET(pipe_fd, &rfds);
217 timeout.tv_usec = CAP_READ_TIMEOUT * 1000;
220 sel_ret = select(pipe_fd+1, &rfds, NULL, NULL, pto);
222 cap_pipe_err_str = strerror(errno);
226 /* XXX - Should we just use file handles exclusively under Windows?
227 * Otherwise we have to convert between file handles and file descriptors
228 * here and when we open a named pipe.
230 HANDLE hPipe = (HANDLE) _get_osfhandle(pipe_fd);
234 if (hPipe == INVALID_HANDLE_VALUE) {
235 cap_pipe_err_str = "Could not open standard input";
239 cap_pipe_err_str = "Unknown error";
241 wait_ret = WaitForSingleObject(hPipe, CAP_READ_TIMEOUT);
243 /* XXX - This probably isn't correct */
252 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
253 NULL, GetLastError(), 0, (LPTSTR) &err_str, 0, NULL);
254 cap_pipe_err_str = utf_16to8(err_str);
258 g_assert_not_reached();
265 /* Mimic pcap_open_live() for pipe captures
266 * We check if "pipename" is "-" (stdin) or a FIFO, open it, and read the
268 * N.B. : we can't read the libpcap formats used in RedHat 6.1 or SuSE 6.3
269 * because we can't seek on pipes (see wiretap/libpcap.c for details) */
271 cap_pipe_open_live(char *pipename, struct pcap_hdr *hdr, loop_data *ld,
272 char *errmsg, int errmsgl)
275 struct stat pipe_stat;
287 unsigned int bytes_read;
289 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "cap_pipe_open_live: %s", pipename);
292 * XXX (T)Wireshark blocks until we return
294 if (strcmp(pipename, "-") == 0) {
295 fd = 0; /* read from stdin */
298 * This is needed to set the stdin pipe into binary mode, otherwise
299 * CR/LF are mangled...
301 _setmode(0, _O_BINARY);
305 if (eth_stat(pipename, &pipe_stat) < 0) {
306 if (errno == ENOENT || errno == ENOTDIR)
307 ld->cap_pipe_err = PIPNEXIST;
309 g_snprintf(errmsg, errmsgl,
310 "The capture session could not be initiated "
311 "due to error on pipe: %s", strerror(errno));
312 ld->cap_pipe_err = PIPERR;
316 if (! S_ISFIFO(pipe_stat.st_mode)) {
317 if (S_ISCHR(pipe_stat.st_mode)) {
319 * Assume the user specified an interface on a system where
320 * interfaces are in /dev. Pretend we haven't seen it.
322 ld->cap_pipe_err = PIPNEXIST;
325 g_snprintf(errmsg, errmsgl,
326 "The capture session could not be initiated because\n"
327 "\"%s\" is neither an interface nor a pipe", pipename);
328 ld->cap_pipe_err = PIPERR;
332 fd = eth_open(pipename, O_RDONLY | O_NONBLOCK, 0000 /* no creation so don't matter */);
334 g_snprintf(errmsg, errmsgl,
335 "The capture session could not be initiated "
336 "due to error on pipe open: %s", strerror(errno));
337 ld->cap_pipe_err = PIPERR;
341 #define PIPE_STR "\\pipe\\"
342 /* Under Windows, named pipes _must_ have the form
343 * "\\<server>\pipe\<pipename>". <server> may be "." for localhost.
345 pncopy = g_strdup(pipename);
346 if ( (pos=strstr(pncopy, "\\\\")) == pncopy) {
347 pos = strchr(pncopy + 3, '\\');
348 if (pos && g_ascii_strncasecmp(pos, PIPE_STR, strlen(PIPE_STR)) != 0)
355 g_snprintf(errmsg, errmsgl,
356 "The capture session could not be initiated because\n"
357 "\"%s\" is neither an interface nor a pipe", pipename);
358 ld->cap_pipe_err = PIPNEXIST;
362 /* Wait for the pipe to appear */
364 hPipe = CreateFile(utf_8to16(pipename), GENERIC_READ, 0, NULL,
365 OPEN_EXISTING, 0, NULL);
367 if (hPipe != INVALID_HANDLE_VALUE)
370 if (GetLastError() != ERROR_PIPE_BUSY) {
371 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
372 NULL, GetLastError(), 0, (LPTSTR) &err_str, 0, NULL);
373 g_snprintf(errmsg, errmsgl,
374 "The capture session on \"%s\" could not be initiated "
375 "due to error on pipe open: pipe busy: %s (error %d)",
376 pipename, utf_16to8(err_str), GetLastError());
378 ld->cap_pipe_err = PIPERR;
382 if (!WaitNamedPipe(utf_8to16(pipename), 30 * 1000)) {
383 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
384 NULL, GetLastError(), 0, (LPTSTR) &err_str, 0, NULL);
385 g_snprintf(errmsg, errmsgl,
386 "The capture session could not be initiated "
387 "due to error on named pipe open: %s (error %d)",
388 utf_16to8(err_str), GetLastError());
390 ld->cap_pipe_err = PIPERR;
395 fd = _open_osfhandle((long) hPipe, _O_RDONLY);
397 g_snprintf(errmsg, errmsgl,
398 "The capture session could not be initiated "
399 "due to error on pipe open: %s", strerror(errno));
400 ld->cap_pipe_err = PIPERR;
406 ld->from_cap_pipe = TRUE;
408 /* read the pcap header */
410 while (bytes_read < sizeof magic) {
411 sel_ret = cap_pipe_select(fd);
413 g_snprintf(errmsg, errmsgl,
414 "Unexpected error from select: %s", strerror(errno));
416 } else if (sel_ret > 0) {
417 b = read(fd, ((char *)&magic)+bytes_read, sizeof magic-bytes_read);
420 g_snprintf(errmsg, errmsgl, "End of file on pipe during open");
422 g_snprintf(errmsg, errmsgl, "Error on pipe during open: %s",
432 /* Host that wrote it has our byte order, and was running
433 a program using either standard or ss990417 libpcap. */
434 ld->cap_pipe_byte_swapped = FALSE;
435 ld->cap_pipe_modified = FALSE;
437 case PCAP_MODIFIED_MAGIC:
438 /* Host that wrote it has our byte order, but was running
439 a program using either ss990915 or ss991029 libpcap. */
440 ld->cap_pipe_byte_swapped = FALSE;
441 ld->cap_pipe_modified = TRUE;
443 case PCAP_SWAPPED_MAGIC:
444 /* Host that wrote it has a byte order opposite to ours,
445 and was running a program using either standard or
447 ld->cap_pipe_byte_swapped = TRUE;
448 ld->cap_pipe_modified = FALSE;
450 case PCAP_SWAPPED_MODIFIED_MAGIC:
451 /* Host that wrote it out has a byte order opposite to
452 ours, and was running a program using either ss990915
453 or ss991029 libpcap. */
454 ld->cap_pipe_byte_swapped = TRUE;
455 ld->cap_pipe_modified = TRUE;
458 /* Not a "libpcap" type we know about. */
459 g_snprintf(errmsg, errmsgl, "Unrecognized libpcap format");
463 /* Read the rest of the header */
465 while (bytes_read < sizeof(struct pcap_hdr)) {
466 sel_ret = cap_pipe_select(fd);
468 g_snprintf(errmsg, errmsgl,
469 "Unexpected error from select: %s", strerror(errno));
471 } else if (sel_ret > 0) {
472 b = read(fd, ((char *)hdr)+bytes_read,
473 sizeof(struct pcap_hdr) - bytes_read);
476 g_snprintf(errmsg, errmsgl, "End of file on pipe during open");
478 g_snprintf(errmsg, errmsgl, "Error on pipe during open: %s",
486 if (ld->cap_pipe_byte_swapped) {
487 /* Byte-swap the header fields about which we care. */
488 hdr->version_major = BSWAP16(hdr->version_major);
489 hdr->version_minor = BSWAP16(hdr->version_minor);
490 hdr->snaplen = BSWAP32(hdr->snaplen);
491 hdr->network = BSWAP32(hdr->network);
493 ld->linktype = hdr->network;
495 if (hdr->version_major < 2) {
496 g_snprintf(errmsg, errmsgl, "Unable to read old libpcap format");
500 ld->cap_pipe_state = STATE_EXPECT_REC_HDR;
501 ld->cap_pipe_err = PIPOK;
505 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "cap_pipe_open_live: error %s", errmsg);
506 ld->cap_pipe_err = PIPERR;
513 /* We read one record from the pipe, take care of byte order in the record
514 * header, write the record to the capture file, and update capture statistics. */
516 cap_pipe_dispatch(loop_data *ld, guchar *data, char *errmsg, int errmsgl)
518 struct pcap_pkthdr phdr;
520 enum { PD_REC_HDR_READ, PD_DATA_READ, PD_PIPE_EOF, PD_PIPE_ERR,
524 #ifdef LOG_CAPTURE_VERBOSE
525 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "cap_pipe_dispatch");
528 switch (ld->cap_pipe_state) {
530 case STATE_EXPECT_REC_HDR:
531 ld->cap_pipe_bytes_to_read = ld->cap_pipe_modified ?
532 sizeof(struct pcaprec_modified_hdr) : sizeof(struct pcaprec_hdr);
533 ld->cap_pipe_bytes_read = 0;
534 ld->cap_pipe_state = STATE_READ_REC_HDR;
537 case STATE_READ_REC_HDR:
538 b = read(ld->cap_pipe_fd, ((char *)&ld->cap_pipe_rechdr)+ld->cap_pipe_bytes_read,
539 ld->cap_pipe_bytes_to_read - ld->cap_pipe_bytes_read);
542 result = PD_PIPE_EOF;
544 result = PD_PIPE_ERR;
547 if ((ld->cap_pipe_bytes_read += b) < ld->cap_pipe_bytes_to_read)
549 result = PD_REC_HDR_READ;
552 case STATE_EXPECT_DATA:
553 ld->cap_pipe_bytes_read = 0;
554 ld->cap_pipe_state = STATE_READ_DATA;
557 case STATE_READ_DATA:
558 b = read(ld->cap_pipe_fd, data+ld->cap_pipe_bytes_read,
559 ld->cap_pipe_rechdr.hdr.incl_len - ld->cap_pipe_bytes_read);
562 result = PD_PIPE_EOF;
564 result = PD_PIPE_ERR;
567 if ((ld->cap_pipe_bytes_read += b) < ld->cap_pipe_rechdr.hdr.incl_len)
569 result = PD_DATA_READ;
573 g_snprintf(errmsg, errmsgl, "cap_pipe_dispatch: invalid state");
576 } /* switch (ld->cap_pipe_state) */
579 * We've now read as much data as we were expecting, so process it.
583 case PD_REC_HDR_READ:
584 /* We've read the header. Take care of byte order. */
585 cap_pipe_adjust_header(ld->cap_pipe_byte_swapped, &ld->cap_pipe_hdr,
586 &ld->cap_pipe_rechdr.hdr);
587 if (ld->cap_pipe_rechdr.hdr.incl_len > WTAP_MAX_PACKET_SIZE) {
588 g_snprintf(errmsg, errmsgl, "Frame %u too long (%d bytes)",
589 ld->packet_count+1, ld->cap_pipe_rechdr.hdr.incl_len);
592 ld->cap_pipe_state = STATE_EXPECT_DATA;
596 /* Fill in a "struct pcap_pkthdr", and process the packet. */
597 phdr.ts.tv_sec = ld->cap_pipe_rechdr.hdr.ts_sec;
598 phdr.ts.tv_usec = ld->cap_pipe_rechdr.hdr.ts_usec;
599 phdr.caplen = ld->cap_pipe_rechdr.hdr.incl_len;
600 phdr.len = ld->cap_pipe_rechdr.hdr.orig_len;
602 ld->packet_cb((u_char *)ld, &phdr, data);
604 ld->cap_pipe_state = STATE_EXPECT_REC_HDR;
608 ld->cap_pipe_err = PIPEOF;
612 g_snprintf(errmsg, errmsgl, "Error reading from pipe: %s",
619 ld->cap_pipe_err = PIPERR;
620 /* Return here rather than inside the switch to prevent GCC warning */
625 /* open the capture input file (pcap or capture pipe) */
627 capture_loop_open_input(capture_options *capture_opts, loop_data *ld,
628 char *errmsg, size_t errmsg_len,
629 char *secondary_errmsg, size_t secondary_errmsg_len)
631 gchar open_err_str[PCAP_ERRBUF_SIZE];
633 static const char ppamsg[] = "can't find PPA for ";
634 const char *set_linktype_err_str;
635 const char *libpcap_warn;
637 gchar *sync_secondary_msg_str;
639 WORD wVersionRequested;
642 #ifdef HAVE_PCAP_REMOTE
643 struct pcap_rmtauth auth;
647 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_open_input : %s", capture_opts->iface);
650 /* XXX - opening Winsock on tshark? */
652 /* Initialize Windows Socket if we are in a WIN32 OS
653 This needs to be done before querying the interface for network/netmask */
655 /* XXX - do we really require 1.1 or earlier?
656 Are there any versions that support only 2.0 or higher? */
657 wVersionRequested = MAKEWORD(1, 1);
658 err = WSAStartup(wVersionRequested, &wsaData);
663 g_snprintf(errmsg, errmsg_len,
664 "Couldn't initialize Windows Sockets: Network system not ready for network communication");
667 case WSAVERNOTSUPPORTED:
668 g_snprintf(errmsg, errmsg_len,
669 "Couldn't initialize Windows Sockets: Windows Sockets version %u.%u not supported",
670 LOBYTE(wVersionRequested), HIBYTE(wVersionRequested));
674 g_snprintf(errmsg, errmsg_len,
675 "Couldn't initialize Windows Sockets: Blocking operation is in progress");
679 g_snprintf(errmsg, errmsg_len,
680 "Couldn't initialize Windows Sockets: Limit on the number of tasks supported by this WinSock implementation has been reached");
684 g_snprintf(errmsg, errmsg_len,
685 "Couldn't initialize Windows Sockets: Bad pointer passed to WSAStartup");
689 g_snprintf(errmsg, errmsg_len,
690 "Couldn't initialize Windows Sockets: error %d", err);
693 g_snprintf(secondary_errmsg, secondary_errmsg_len, please_report);
698 /* Open the network interface to capture from it.
699 Some versions of libpcap may put warnings into the error buffer
700 if they succeed; to tell if that's happened, we have to clear
701 the error buffer, and check if it's still a null string. */
702 open_err_str[0] = '\0';
703 #ifdef HAVE_PCAP_OPEN
704 auth.type = capture_opts->auth_type == CAPTURE_AUTH_PWD ?
705 RPCAP_RMTAUTH_PWD : RPCAP_RMTAUTH_NULL;
706 auth.username = capture_opts->auth_username;
707 auth.password = capture_opts->auth_password;
709 ld->pcap_h = pcap_open(capture_opts->iface,
710 capture_opts->has_snaplen ? capture_opts->snaplen :
711 WTAP_MAX_PACKET_SIZE,
713 (capture_opts->promisc_mode ? PCAP_OPENFLAG_PROMISCUOUS : 0) |
714 (capture_opts->datatx_udp ? PCAP_OPENFLAG_DATATX_UDP : 0) |
715 (capture_opts->nocap_rpcap ? PCAP_OPENFLAG_NOCAPTURE_RPCAP : 0),
716 CAP_READ_TIMEOUT, &auth, open_err_str);
718 ld->pcap_h = pcap_open_live(capture_opts->iface,
719 capture_opts->has_snaplen ? capture_opts->snaplen :
720 WTAP_MAX_PACKET_SIZE,
721 capture_opts->promisc_mode, CAP_READ_TIMEOUT,
725 if (ld->pcap_h != NULL) {
726 /* we've opened "iface" as a network device */
728 /* try to set the capture buffer size */
729 if (pcap_setbuff(ld->pcap_h, capture_opts->buffer_size * 1024 * 1024) != 0) {
730 sync_secondary_msg_str = g_strdup_printf(
731 "The capture buffer size of %luMB seems to be too high for your machine,\n"
732 "the default of 1MB will be used.\n"
734 "Nonetheless, the capture is started.\n",
735 capture_opts->buffer_size);
736 report_capture_error("Couldn't set the capture buffer size!",
737 sync_secondary_msg_str);
738 g_free(sync_secondary_msg_str);
742 #if defined(HAVE_PCAP_REMOTE) && defined(HAVE_PCAP_SETSAMPLING)
743 if (capture_opts->sampling_method != CAPTURE_SAMP_NONE)
745 struct pcap_samp *samp;
747 if ((samp = pcap_setsampling(ld->pcap_h)) != NULL)
749 switch (capture_opts->sampling_method)
751 case CAPTURE_SAMP_BY_COUNT:
752 samp->method = PCAP_SAMP_1_EVERY_N;
755 case CAPTURE_SAMP_BY_TIMER:
756 samp->method = PCAP_SAMP_FIRST_AFTER_N_MS;
760 sync_msg_str = g_strdup_printf(
761 "Unknown sampling method %d specified,\n"
762 "continue without packet sampling",
763 capture_opts->sampling_method);
764 report_capture_error("Couldn't set the capture "
765 "sampling", sync_msg_str);
766 g_free(sync_msg_str);
768 samp->value = capture_opts->sampling_param;
772 report_capture_error("Couldn't set the capture sampling",
773 "Cannot get packet sampling data structure");
779 /* setting the data link type only works on real interfaces */
780 if (capture_opts->linktype != -1) {
781 set_linktype_err_str = set_pcap_linktype(ld->pcap_h, capture_opts->iface,
782 capture_opts->linktype);
783 if (set_linktype_err_str != NULL) {
784 g_snprintf(errmsg, errmsg_len, "Unable to set data link type (%s).",
785 set_linktype_err_str);
786 g_snprintf(secondary_errmsg, secondary_errmsg_len, please_report);
790 ld->linktype = get_pcap_linktype(ld->pcap_h, capture_opts->iface);
792 /* We couldn't open "iface" as a network device. */
793 /* Try to open it as a pipe */
794 ld->cap_pipe_fd = cap_pipe_open_live(capture_opts->iface, &ld->cap_pipe_hdr, ld, errmsg, errmsg_len);
796 if (ld->cap_pipe_fd == -1) {
798 if (ld->cap_pipe_err == PIPNEXIST) {
799 /* Pipe doesn't exist, so output message for interface */
801 /* If we got a "can't find PPA for X" message, warn the user (who
802 is running (T)Wireshark on HP-UX) that they don't have a version
803 of libpcap that properly handles HP-UX (libpcap 0.6.x and later
804 versions, which properly handle HP-UX, say "can't find /dev/dlpi
805 PPA for X" rather than "can't find PPA for X"). */
806 if (strncmp(open_err_str, ppamsg, sizeof ppamsg - 1) == 0)
809 "You are running (T)Wireshark with a version of the libpcap library\n"
810 "that doesn't handle HP-UX network devices well; this means that\n"
811 "(T)Wireshark may not be able to capture packets.\n"
813 "To fix this, you should install libpcap 0.6.2, or a later version\n"
814 "of libpcap, rather than libpcap 0.4 or 0.5.x. It is available in\n"
815 "packaged binary form from the Software Porting And Archive Centre\n"
816 "for HP-UX; the Centre is at http://hpux.connect.org.uk/ - the page\n"
817 "at the URL lists a number of mirror sites.";
820 g_snprintf(errmsg, errmsg_len,
821 "The capture session could not be initiated (%s).", open_err_str);
823 g_snprintf(secondary_errmsg, secondary_errmsg_len,
824 "Please check to make sure you have sufficient permissions, and that you have "
825 "the proper interface or pipe specified.%s", libpcap_warn);
827 g_snprintf(secondary_errmsg, secondary_errmsg_len,
829 "Please check that \"%s\" is the proper interface.\n"
832 "Help can be found at:\n"
834 " http://wiki.wireshark.org/CaptureSetup\n"
837 "WinPcap does not support 64-bit Windows; you will have to use some other\n"
838 "tool to capture traffic, such as netcap.\n"
839 "For netcap details see: http://support.microsoft.com/?id=310875\n"
842 "Note that version 3.0 of WinPcap, and earlier versions of WinPcap, don't\n"
843 "support capturing on PPP/WAN interfaces on Windows NT 4.0 / 2000 / XP /\n"
845 "WinPcap 3.1 has support for it on Windows 2000 / XP / Server 2003, but has no\n"
846 "support for it on Windows NT 4.0 or Windows Vista (Beta 1).",
847 capture_opts->iface);
851 * Else pipe (or file) does exist and cap_pipe_open_live() has
856 /* cap_pipe_open_live() succeeded; don't want
857 error message from pcap_open_live() */
858 open_err_str[0] = '\0';
861 /* XXX - will this work for tshark? */
862 #ifdef MUST_DO_SELECT
863 if (!ld->from_cap_pipe) {
864 #ifdef HAVE_PCAP_GET_SELECTABLE_FD
865 ld->pcap_fd = pcap_get_selectable_fd(ld->pcap_h);
867 ld->pcap_fd = pcap_fileno(ld->pcap_h);
872 /* Does "open_err_str" contain a non-empty string? If so, "pcap_open_live()"
873 returned a warning; print it, but keep capturing. */
874 if (open_err_str[0] != '\0') {
875 sync_msg_str = g_strdup_printf("%s.", open_err_str);
876 report_capture_error(sync_msg_str, "");
877 g_free(sync_msg_str);
884 /* close the capture input file (pcap or capture pipe) */
885 static void capture_loop_close_input(loop_data *ld) {
887 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_close_input");
889 /* if open, close the capture pipe "input file" */
890 if (ld->cap_pipe_fd >= 0) {
891 g_assert(ld->from_cap_pipe);
892 eth_close(ld->cap_pipe_fd);
896 /* if open, close the pcap "input file" */
897 if(ld->pcap_h != NULL) {
898 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_close_input: closing %p", ld->pcap_h);
899 g_assert(!ld->from_cap_pipe);
900 pcap_close(ld->pcap_h);
907 /* Shut down windows sockets */
913 /* init the capture filter */
914 static initfilter_status_t
915 capture_loop_init_filter(pcap_t *pcap_h, gboolean from_cap_pipe, gchar * iface, gchar * cfilter) {
916 bpf_u_int32 netnum, netmask;
917 gchar lookup_net_err_str[PCAP_ERRBUF_SIZE];
918 struct bpf_program fcode;
921 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_init_filter: %s", cfilter);
923 /* capture filters only work on real interfaces */
924 if (cfilter && !from_cap_pipe) {
925 /* A capture filter was specified; set it up. */
926 if (pcap_lookupnet(iface, &netnum, &netmask, lookup_net_err_str) < 0) {
928 * Well, we can't get the netmask for this interface; it's used
929 * only for filters that check for broadcast IP addresses, so
930 * we just punt and use 0. It might be nice to warn the user,
931 * but that's a pain in a GUI application, as it'd involve popping
932 * up a message box, and it's not clear how often this would make
933 * a difference (only filters that check for IP broadcast addresses
937 "Warning: Couldn't obtain netmask info (%s).", lookup_net_err_str);*/
940 if (pcap_compile(pcap_h, &fcode, cfilter, 1, netmask) < 0) {
941 /* Treat this specially - our caller might try to compile this
942 as a display filter and, if that succeeds, warn the user that
943 the display and capture filter syntaxes are different. */
944 return INITFILTER_BAD_FILTER;
946 if (pcap_setfilter(pcap_h, &fcode) < 0) {
947 #ifdef HAVE_PCAP_FREECODE
948 pcap_freecode(&fcode);
950 return INITFILTER_OTHER_ERROR;
952 #ifdef HAVE_PCAP_FREECODE
953 pcap_freecode(&fcode);
957 return INITFILTER_NO_ERROR;
961 /* set up to write to the already-opened capture output file/files */
963 capture_loop_init_output(capture_options *capture_opts, int save_file_fd, loop_data *ld, char *errmsg, int errmsg_len) {
968 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_init_output");
971 if (ld->from_cap_pipe) {
972 file_snaplen = ld->cap_pipe_hdr.snaplen;
975 file_snaplen = pcap_snapshot(ld->pcap_h);
978 /* Set up to write to the capture file. */
979 if (capture_opts->multi_files_on) {
980 ld->pdh = ringbuf_init_libpcap_fdopen(ld->linktype, file_snaplen,
981 &ld->bytes_written, &err);
983 ld->pdh = libpcap_fdopen(save_file_fd, ld->linktype, file_snaplen,
984 &ld->bytes_written, &err);
987 if (ld->pdh == NULL) {
988 /* We couldn't set up to write to the capture file. */
989 /* XXX - use cf_open_error_message from tshark instead? */
992 case WTAP_ERR_CANT_OPEN:
993 g_snprintf(errmsg, errmsg_len, "The file to which the capture would be saved"
994 " couldn't be created for some unknown reason.");
997 case WTAP_ERR_SHORT_WRITE:
998 g_snprintf(errmsg, errmsg_len, "A full header couldn't be written to the file"
999 " to which the capture would be saved.");
1004 g_snprintf(errmsg, errmsg_len,
1005 "The file to which the capture would be"
1006 " saved (\"%s\") could not be opened: Error %d.",
1007 capture_opts->save_file, err);
1009 g_snprintf(errmsg, errmsg_len,
1010 "The file to which the capture would be"
1011 " saved (\"%s\") could not be opened: %s.",
1012 capture_opts->save_file, strerror(err));
1024 capture_loop_close_output(capture_options *capture_opts, loop_data *ld, int *err_close) {
1026 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_close_output");
1028 if (capture_opts->multi_files_on) {
1029 return ringbuf_libpcap_dump_close(&capture_opts->save_file, err_close);
1031 return libpcap_dump_close(ld->pdh, err_close);
1035 /* dispatch incoming packets (pcap or capture pipe)
1037 * Waits for incoming packets to be available, and calls pcap_dispatch()
1038 * to cause them to be processed.
1040 * Returns the number of packets which were processed.
1042 * Times out (returning zero) after CAP_READ_TIMEOUT ms; this ensures that the
1043 * packet-batching behaviour does not cause packets to get held back
1047 capture_loop_dispatch(capture_options *capture_opts _U_, loop_data *ld,
1048 char *errmsg, int errmsg_len)
1052 gint packet_count_before;
1053 guchar pcap_data[WTAP_MAX_PACKET_SIZE];
1055 packet_count_before = ld->packet_count;
1056 if (ld->from_cap_pipe) {
1057 /* dispatch from capture pipe */
1058 #ifdef LOG_CAPTURE_VERBOSE
1059 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_dispatch: from capture pipe");
1061 sel_ret = cap_pipe_select(ld->cap_pipe_fd);
1064 if (sel_ret < 0 && errno != EINTR) {
1065 g_snprintf(errmsg, errmsg_len,
1066 "Unexpected error from select: %s", strerror(errno));
1067 report_capture_error(errmsg, please_report);
1072 * "select()" says we can read from the pipe without blocking
1074 inpkts = cap_pipe_dispatch(ld, pcap_data, errmsg, errmsg_len);
1082 /* dispatch from pcap */
1083 #ifdef MUST_DO_SELECT
1085 * If we have "pcap_get_selectable_fd()", we use it to get the
1086 * descriptor on which to select; if that's -1, it means there
1087 * is no descriptor on which you can do a "select()" (perhaps
1088 * because you're capturing on a special device, and that device's
1089 * driver unfortunately doesn't support "select()", in which case
1090 * we don't do the select - which means it might not be possible
1091 * to stop a capture until a packet arrives. If that's unacceptable,
1092 * plead with whoever supplies the software for that device to add
1093 * "select()" support, or upgrade to libpcap 0.8.1 or later, and
1094 * rebuild Wireshark or get a version built with libpcap 0.8.1 or
1095 * later, so it can use pcap_breakloop().
1097 #ifdef LOG_CAPTURE_VERBOSE
1098 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_dispatch: from pcap_dispatch with select");
1100 if (ld->pcap_fd != -1) {
1101 sel_ret = cap_pipe_select(ld->pcap_fd);
1104 * "select()" says we can read from it without blocking; go for
1107 * We don't have pcap_breakloop(), so we only process one packet
1108 * per pcap_dispatch() call, to allow a signal to stop the
1109 * processing immediately, rather than processing all packets
1110 * in a batch before quitting.
1112 inpkts = pcap_dispatch(ld->pcap_h, 1, ld->packet_cb, (u_char *)ld);
1114 ld->pcap_err = TRUE;
1115 ld->go = FALSE; /* error or pcap_breakloop() - stop capturing */
1118 if (sel_ret < 0 && errno != EINTR) {
1119 g_snprintf(errmsg, errmsg_len,
1120 "Unexpected error from select: %s", strerror(errno));
1121 report_capture_error(errmsg, please_report);
1127 #endif /* MUST_DO_SELECT */
1129 /* dispatch from pcap without select */
1131 #ifdef LOG_CAPTURE_VERBOSE
1132 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_dispatch: from pcap_dispatch");
1136 * On Windows, we don't support asynchronously telling a process to
1137 * stop capturing; instead, we check for an indication on a pipe
1138 * after processing packets. We therefore process only one packet
1139 * at a time, so that we can check the pipe after every packet.
1141 inpkts = pcap_dispatch(ld->pcap_h, 1, ld->packet_cb, (u_char *) ld);
1143 inpkts = pcap_dispatch(ld->pcap_h, -1, ld->packet_cb, (u_char *) ld);
1147 /* Error, rather than pcap_breakloop(). */
1148 ld->pcap_err = TRUE;
1150 ld->go = FALSE; /* error or pcap_breakloop() - stop capturing */
1152 #else /* pcap_next_ex */
1153 #ifdef LOG_CAPTURE_VERBOSE
1154 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_dispatch: from pcap_next_ex");
1156 /* XXX - this is currently unused, as there is some confusion with pcap_next_ex() vs. pcap_dispatch() */
1159 * WinPcap's remote capturing feature doesn't work with pcap_dispatch(),
1160 * see http://wiki.wireshark.org/CaptureSetup_2fWinPcapRemote
1161 * This should be fixed in the WinPcap 4.0 alpha release.
1163 * For reference, an example remote interface:
1164 * rpcap://[1.2.3.4]/\Device\NPF_{39993D68-7C9B-4439-A329-F2D888DA7C5C}
1167 /* emulate dispatch from pcap */
1170 struct pcap_pkthdr *pkt_header;
1175 (in = pcap_next_ex(ld->pcap_h, &pkt_header, &pkt_data)) == 1)
1176 ld->packet_cb( (u_char *) ld, pkt_header, pkt_data);
1179 ld->pcap_err = TRUE;
1183 #endif /* pcap_next_ex */
1187 #ifdef LOG_CAPTURE_VERBOSE
1188 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_dispatch: %d new packet%s", inpkts, plurality(inpkts, "", "s"));
1191 return ld->packet_count - packet_count_before;
1195 /* open the output file (temporary/specified name/ringbuffer/named pipe/stdout) */
1196 /* Returns TRUE if the file opened successfully, FALSE otherwise. */
1198 capture_loop_open_output(capture_options *capture_opts, int *save_file_fd,
1199 char *errmsg, int errmsg_len) {
1201 char tmpname[128+1];
1202 gchar *capfile_name;
1203 gboolean is_tempfile;
1206 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_open_output: %s",
1207 (capture_opts->save_file) ? capture_opts->save_file : "");
1209 if (capture_opts->save_file != NULL) {
1210 /* We return to the caller while the capture is in progress.
1211 * Therefore we need to take a copy of save_file in
1212 * case the caller destroys it after we return.
1214 capfile_name = g_strdup(capture_opts->save_file);
1216 if (capture_opts->output_to_pipe == TRUE) { /* either "-" or named pipe */
1217 if (capture_opts->multi_files_on) {
1218 /* ringbuffer is enabled; that doesn't work with standard output or a named pipe */
1219 g_snprintf(errmsg, errmsg_len,
1220 "Ring buffer requested, but capture is being written to standard output or to a named pipe.");
1221 g_free(capfile_name);
1224 if (strcmp(capfile_name, "-") == 0) {
1225 /* write to stdout */
1228 /* set output pipe to binary mode to avoid Windows text-mode processing (eg: for CR/LF) */
1229 _setmode(1, O_BINARY);
1232 } /* if (...output_to_pipe ... */
1235 if (capture_opts->multi_files_on) {
1236 /* ringbuffer is enabled */
1237 *save_file_fd = ringbuf_init(capfile_name,
1238 (capture_opts->has_ring_num_files) ? capture_opts->ring_num_files : 0);
1240 /* we need the ringbuf name */
1241 if(*save_file_fd != -1) {
1242 g_free(capfile_name);
1243 capfile_name = g_strdup(ringbuf_current_filename());
1246 /* Try to open/create the specified file for use as a capture buffer. */
1247 *save_file_fd = eth_open(capfile_name, O_RDWR|O_BINARY|O_TRUNC|O_CREAT,
1251 is_tempfile = FALSE;
1253 /* Choose a random name for the temporary capture buffer */
1254 *save_file_fd = create_tempfile(tmpname, sizeof tmpname, "ether");
1255 capfile_name = g_strdup(tmpname);
1259 /* did we fail to open the output file? */
1260 if (*save_file_fd == -1) {
1262 g_snprintf(errmsg, errmsg_len,
1263 "The temporary file to which the capture would be saved (\"%s\") "
1264 "could not be opened: %s.", capfile_name, strerror(errno));
1266 if (capture_opts->multi_files_on) {
1267 ringbuf_error_cleanup();
1270 g_snprintf(errmsg, errmsg_len,
1271 "The file to which the capture would be saved (\"%s\") "
1272 "could not be opened: %s.", capfile_name,
1275 g_free(capfile_name);
1279 if(capture_opts->save_file != NULL) {
1280 g_free(capture_opts->save_file);
1282 capture_opts->save_file = capfile_name;
1283 /* capture_opts.save_file is "g_free"ed later, which is equivalent to
1284 "g_free(capfile_name)". */
1286 fchown(*save_file_fd, capture_opts->owner, capture_opts->group);
1294 capture_loop_stop_signal_handler(int signo _U_)
1296 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Signal: Stop capture");
1297 capture_loop_stop();
1301 #define TIME_GET() GetTickCount()
1303 #define TIME_GET() time(NULL)
1306 /* Do the low-level work of a capture.
1307 Returns TRUE if it succeeds, FALSE otherwise. */
1309 capture_loop_start(capture_options *capture_opts, gboolean *stats_known, struct pcap_stat *stats)
1312 struct sigaction act;
1314 time_t upd_time, cur_time;
1318 gint inpkts_to_sync_pipe = 0; /* packets not already send out to the sync_pipe */
1319 condition *cnd_file_duration = NULL;
1320 condition *cnd_autostop_files = NULL;
1321 condition *cnd_autostop_size = NULL;
1322 condition *cnd_autostop_duration = NULL;
1323 guint32 autostop_files = 0;
1326 gboolean cfilter_error = FALSE;
1327 #define MSG_MAX_LENGTH 4096
1328 char errmsg[MSG_MAX_LENGTH+1];
1329 char secondary_errmsg[MSG_MAX_LENGTH+1];
1330 int save_file_fd = -1;
1333 *secondary_errmsg = '\0';
1335 /* init the loop data */
1337 ld.packet_count = 0;
1338 if (capture_opts->has_autostop_packets)
1339 ld.packet_max = capture_opts->autostop_packets;
1341 ld.packet_max = 0; /* no limit */
1342 ld.err = 0; /* no error seen yet */
1343 ld.wtap_linktype = WTAP_ENCAP_UNKNOWN;
1344 ld.pcap_err = FALSE;
1345 ld.from_cap_pipe = FALSE;
1347 ld.cap_pipe_fd = -1;
1348 #ifdef MUST_DO_SELECT
1351 ld.packet_cb = capture_loop_packet_cb;
1354 /* We haven't yet gotten the capture statistics. */
1355 *stats_known = FALSE;
1359 * Catch SIGUSR1, so that we exit cleanly if the parent process
1360 * kills us with it due to the user selecting "Capture->Stop".
1362 act.sa_handler = capture_loop_stop_signal_handler;
1364 * Arrange that system calls not get restarted, because when
1365 * our signal handler returns we don't want to restart
1366 * a call that was waiting for packets to arrive.
1369 sigemptyset(&act.sa_mask);
1370 sigaction(SIGUSR1, &act, NULL);
1373 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Capture loop starting ...");
1374 capture_opts_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, capture_opts);
1376 /* open the "input file" from network interface or capture pipe */
1377 if (!capture_loop_open_input(capture_opts, &ld, errmsg, sizeof(errmsg),
1378 secondary_errmsg, sizeof(secondary_errmsg))) {
1382 /* init the input filter from the network interface (capture pipe will do nothing) */
1383 switch (capture_loop_init_filter(ld.pcap_h, ld.from_cap_pipe, capture_opts->iface, capture_opts->cfilter)) {
1385 case INITFILTER_NO_ERROR:
1388 case INITFILTER_BAD_FILTER:
1389 cfilter_error = TRUE;
1390 g_snprintf(errmsg, sizeof(errmsg), "%s", pcap_geterr(ld.pcap_h));
1393 case INITFILTER_OTHER_ERROR:
1394 g_snprintf(errmsg, sizeof(errmsg), "Can't install filter (%s).",
1395 pcap_geterr(ld.pcap_h));
1396 g_snprintf(secondary_errmsg, sizeof(secondary_errmsg), "%s", please_report);
1400 /* If we're supposed to write to a capture file, open it for output
1401 (temporary/specified name/ringbuffer) */
1402 if (capture_opts->saving_to_file) {
1403 if (!capture_loop_open_output(capture_opts, &save_file_fd, errmsg, sizeof(errmsg))) {
1407 /* set up to write to the already-opened capture output file/files */
1408 if (!capture_loop_init_output(capture_opts, save_file_fd, &ld, errmsg, sizeof(errmsg))) {
1412 /* XXX - capture SIGTERM and close the capture, in case we're on a
1413 Linux 2.0[.x] system and you have to explicitly close the capture
1414 stream in order to turn promiscuous mode off? We need to do that
1415 in other places as well - and I don't think that works all the
1416 time in any case, due to libpcap bugs. */
1418 /* Well, we should be able to start capturing.
1420 Sync out the capture file, so the header makes it to the file system,
1421 and send a "capture started successfully and capture file created"
1422 message to our parent so that they'll open the capture file and
1423 update its windows to indicate that we have a live capture in
1425 libpcap_dump_flush(ld.pdh, NULL);
1426 report_new_capture_file(capture_opts->save_file);
1429 /* initialize capture stop (and alike) conditions */
1430 init_capture_stop_conditions();
1431 /* create stop conditions */
1432 if (capture_opts->has_autostop_filesize)
1434 cnd_new(CND_CLASS_CAPTURESIZE,(long)capture_opts->autostop_filesize * 1024);
1435 if (capture_opts->has_autostop_duration)
1436 cnd_autostop_duration =
1437 cnd_new(CND_CLASS_TIMEOUT,(gint32)capture_opts->autostop_duration);
1439 if (capture_opts->multi_files_on) {
1440 if (capture_opts->has_file_duration)
1442 cnd_new(CND_CLASS_TIMEOUT, capture_opts->file_duration);
1444 if (capture_opts->has_autostop_files)
1445 cnd_autostop_files =
1446 cnd_new(CND_CLASS_CAPTURESIZE, capture_opts->autostop_files);
1449 /* init the time values */
1450 start_time = TIME_GET();
1451 upd_time = TIME_GET();
1453 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Capture loop running!");
1455 /* WOW, everything is prepared! */
1456 /* please fasten your seat belts, we will enter now the actual capture loop */
1458 /* dispatch incoming packets */
1459 inpkts = capture_loop_dispatch(capture_opts, &ld, errmsg, sizeof(errmsg));
1462 /* any news from our parent (signal pipe)? -> just stop the capture */
1463 if (!signal_pipe_check_running()) {
1469 inpkts_to_sync_pipe += inpkts;
1471 /* check capture size condition */
1472 if (cnd_autostop_size != NULL &&
1473 cnd_eval(cnd_autostop_size, (guint32)ld.bytes_written)){
1474 /* Capture size limit reached, do we have another file? */
1475 if (capture_opts->multi_files_on) {
1476 if (cnd_autostop_files != NULL &&
1477 cnd_eval(cnd_autostop_files, ++autostop_files)) {
1478 /* no files left: stop here */
1483 /* Switch to the next ringbuffer file */
1484 if (ringbuf_switch_file(&ld.pdh, &capture_opts->save_file,
1485 &save_file_fd, &ld.bytes_written, &ld.err)) {
1486 /* File switch succeeded: reset the conditions */
1487 cnd_reset(cnd_autostop_size);
1488 if (cnd_file_duration) {
1489 cnd_reset(cnd_file_duration);
1491 libpcap_dump_flush(ld.pdh, NULL);
1492 report_packet_count(inpkts_to_sync_pipe);
1493 inpkts_to_sync_pipe = 0;
1494 report_new_capture_file(capture_opts->save_file);
1496 /* File switch failed: stop here */
1501 /* single file, stop now */
1505 } /* cnd_autostop_size */
1506 if (capture_opts->output_to_pipe) {
1507 libpcap_dump_flush(ld.pdh, NULL);
1511 /* Only update once a second (Win32: 500ms) so as not to overload slow
1512 * displays. This also prevents too much context-switching between the
1513 * dumpcap and wireshark processes */
1514 cur_time = TIME_GET();
1516 if ( (cur_time - upd_time) > 500) {
1518 if (cur_time - upd_time > 0) {
1520 upd_time = cur_time;
1522 /*if (pcap_stats(pch, stats) >= 0) {
1523 *stats_known = TRUE;
1526 /* Let the parent process know. */
1527 if (inpkts_to_sync_pipe) {
1529 libpcap_dump_flush(ld.pdh, NULL);
1531 /* Send our parent a message saying we've written out "inpkts_to_sync_pipe"
1532 packets to the capture file. */
1533 report_packet_count(inpkts_to_sync_pipe);
1535 inpkts_to_sync_pipe = 0;
1538 /* check capture duration condition */
1539 if (cnd_autostop_duration != NULL && cnd_eval(cnd_autostop_duration)) {
1540 /* The maximum capture time has elapsed; stop the capture. */
1545 /* check capture file duration condition */
1546 if (cnd_file_duration != NULL && cnd_eval(cnd_file_duration)) {
1547 /* duration limit reached, do we have another file? */
1548 if (capture_opts->multi_files_on) {
1549 if (cnd_autostop_files != NULL &&
1550 cnd_eval(cnd_autostop_files, ++autostop_files)) {
1551 /* no files left: stop here */
1556 /* Switch to the next ringbuffer file */
1557 if (ringbuf_switch_file(&ld.pdh, &capture_opts->save_file,
1558 &save_file_fd, &ld.bytes_written, &ld.err)) {
1559 /* file switch succeeded: reset the conditions */
1560 cnd_reset(cnd_file_duration);
1561 if(cnd_autostop_size)
1562 cnd_reset(cnd_autostop_size);
1563 libpcap_dump_flush(ld.pdh, NULL);
1564 report_packet_count(inpkts_to_sync_pipe);
1565 inpkts_to_sync_pipe = 0;
1566 report_new_capture_file(capture_opts->save_file);
1568 /* File switch failed: stop here */
1573 /* single file, stop now */
1577 } /* cnd_file_duration */
1580 } /* while (ld.go) */
1582 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Capture loop stopping ...");
1584 /* delete stop conditions */
1585 if (cnd_file_duration != NULL)
1586 cnd_delete(cnd_file_duration);
1587 if (cnd_autostop_files != NULL)
1588 cnd_delete(cnd_autostop_files);
1589 if (cnd_autostop_size != NULL)
1590 cnd_delete(cnd_autostop_size);
1591 if (cnd_autostop_duration != NULL)
1592 cnd_delete(cnd_autostop_duration);
1594 /* did we had a pcap (input) error? */
1596 /* On Linux, if an interface goes down while you're capturing on it,
1597 you'll get a "recvfrom: Network is down" error (ENETDOWN).
1598 (At least you will if strerror() doesn't show a local translation
1601 On FreeBSD and OS X, if a network adapter disappears while
1602 you're capturing on it, you'll get a "read: Device not configured"
1603 error (ENXIO). (See previous parenthetical note.)
1605 On OpenBSD, you get "read: I/O error" (EIO) in the same case.
1607 These should *not* be reported to the Wireshark developers. */
1610 cap_err_str = pcap_geterr(ld.pcap_h);
1611 if (strcmp(cap_err_str, "recvfrom: Network is down") == 0 ||
1612 strcmp(cap_err_str, "read: Device not configured") == 0 ||
1613 strcmp(cap_err_str, "read: I/O error") == 0) {
1614 report_capture_error("The network adapter on which the capture was being done "
1615 "is no longer running; the capture has stopped.",
1618 g_snprintf(errmsg, sizeof(errmsg), "Error while capturing packets: %s",
1620 report_capture_error(errmsg, please_report);
1623 else if (ld.from_cap_pipe && ld.cap_pipe_err == PIPERR)
1624 report_capture_error(errmsg, "");
1626 /* did we had an error while capturing? */
1630 capture_loop_get_errmsg(errmsg, sizeof(errmsg), capture_opts->save_file, ld.err,
1632 report_capture_error(errmsg, please_report);
1636 if (capture_opts->saving_to_file) {
1637 /* close the wiretap (output) file */
1638 close_ok = capture_loop_close_output(capture_opts, &ld, &err_close);
1642 /* there might be packets not yet notified to the parent */
1643 /* (do this after closing the file, so all packets are already flushed) */
1644 if(inpkts_to_sync_pipe) {
1645 report_packet_count(inpkts_to_sync_pipe);
1646 inpkts_to_sync_pipe = 0;
1649 /* If we've displayed a message about a write error, there's no point
1650 in displaying another message about an error on close. */
1651 if (!close_ok && write_ok) {
1652 capture_loop_get_errmsg(errmsg, sizeof(errmsg), capture_opts->save_file, err_close,
1654 report_capture_error(errmsg, "");
1658 * XXX We exhibit different behaviour between normal mode and sync mode
1659 * when the pipe is stdin and not already at EOF. If we're a child, the
1660 * parent's stdin isn't closed, so if the user starts another capture,
1661 * cap_pipe_open_live() will very likely not see the expected magic bytes and
1662 * will say "Unrecognized libpcap format". On the other hand, in normal
1663 * mode, cap_pipe_open_live() will say "End of file on pipe during open".
1666 /* get packet drop statistics from pcap */
1667 if(ld.pcap_h != NULL) {
1668 g_assert(!ld.from_cap_pipe);
1669 /* Get the capture statistics, so we know how many packets were
1671 if (pcap_stats(ld.pcap_h, stats) >= 0) {
1672 *stats_known = TRUE;
1673 /* Let the parent process know. */
1674 report_packet_drops(stats->ps_drop);
1676 g_snprintf(errmsg, sizeof(errmsg),
1677 "Can't get packet-drop statistics: %s",
1678 pcap_geterr(ld.pcap_h));
1679 report_capture_error(errmsg, please_report);
1683 /* close the input file (pcap or capture pipe) */
1684 capture_loop_close_input(&ld);
1686 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Capture loop stopped!");
1688 /* ok, if the write and the close were successful. */
1689 return write_ok && close_ok;
1692 if (capture_opts->multi_files_on) {
1693 /* cleanup ringbuffer */
1694 ringbuf_error_cleanup();
1696 /* We can't use the save file, and we have no FILE * for the stream
1697 to close in order to close it, so close the FD directly. */
1698 if(save_file_fd != -1) {
1699 eth_close(save_file_fd);
1702 /* We couldn't even start the capture, so get rid of the capture
1704 if(capture_opts->save_file != NULL) {
1705 eth_unlink(capture_opts->save_file);
1706 g_free(capture_opts->save_file);
1709 capture_opts->save_file = NULL;
1711 report_cfilter_error(capture_opts->cfilter, errmsg);
1713 report_capture_error(errmsg, secondary_errmsg);
1715 /* close the input file (pcap or cap_pipe) */
1716 capture_loop_close_input(&ld);
1718 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Capture loop stopped with error");
1724 void capture_loop_stop(void)
1726 #ifdef HAVE_PCAP_BREAKLOOP
1727 if(ld.pcap_h != NULL)
1728 pcap_breakloop(ld.pcap_h);
1735 capture_loop_get_errmsg(char *errmsg, int errmsglen, const char *fname,
1736 int err, gboolean is_close)
1741 g_snprintf(errmsg, errmsglen,
1742 "Not all the packets could be written to the file"
1743 " to which the capture was being saved\n"
1744 "(\"%s\") because there is no space left on the file system\n"
1745 "on which that file resides.",
1751 g_snprintf(errmsg, errmsglen,
1752 "Not all the packets could be written to the file"
1753 " to which the capture was being saved\n"
1754 "(\"%s\") because you are too close to, or over,"
1755 " your disk quota\n"
1756 "on the file system on which that file resides.",
1761 case WTAP_ERR_CANT_CLOSE:
1762 g_snprintf(errmsg, errmsglen,
1763 "The file to which the capture was being saved"
1764 " couldn't be closed for some unknown reason.");
1767 case WTAP_ERR_SHORT_WRITE:
1768 g_snprintf(errmsg, errmsglen,
1769 "Not all the packets could be written to the file"
1770 " to which the capture was being saved\n"
1777 g_snprintf(errmsg, errmsglen,
1778 "The file to which the capture was being saved\n"
1779 "(\"%s\") could not be closed: %s.",
1780 fname, wtap_strerror(err));
1782 g_snprintf(errmsg, errmsglen,
1783 "An error occurred while writing to the file"
1784 " to which the capture was being saved\n"
1786 fname, wtap_strerror(err));
1793 /* one packet was captured, process it */
1795 capture_loop_packet_cb(u_char *user, const struct pcap_pkthdr *phdr,
1798 loop_data *ld = (void *) user;
1801 /* if the user told us to stop after x packets, do we already have enough? */
1802 if ((ld->packet_max > 0) && (ld->packet_count >= ld->packet_max))
1808 /* We may be called multiple times from pcap_dispatch(); if we've set
1809 the "stop capturing" flag, ignore this packet, as we're not
1810 supposed to be saving any more packets. */
1815 /* We're supposed to write the packet to a file; do so.
1816 If this fails, set "ld->go" to FALSE, to stop the capture, and set
1817 "ld->err" to the error. */
1818 if (!libpcap_write_packet(ld->pdh, phdr, pd, &ld->bytes_written, &err)) {
1826 #endif /* HAVE_LIBPCAP */