[capture_info] Fold the code of capture_info_open() into ui/capture.c
[metze/wireshark/wip.git] / capture_info.c
1 /* capture_info.c
2  * capture info functions
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * SPDX-License-Identifier: GPL-2.0+
9  */
10
11 #include <config.h>
12
13 #ifdef HAVE_LIBPCAP
14
15 #include <glib.h>
16
17 #include <epan/packet.h>
18 #include <wiretap/wtap.h>
19
20 #include "capture_info.h"
21
22 #include <epan/capture_dissectors.h>
23
24 #include <wsutil/filesystem.h>
25
26
27 static const char *
28 cf_open_error_message(int err, gchar *err_info, gboolean for_writing,
29                       int file_type)
30 {
31     const char *errmsg;
32     static char errmsg_errno[1024+1];
33
34     if (err < 0) {
35         /* Wiretap error. */
36         switch (err) {
37
38         case WTAP_ERR_NOT_REGULAR_FILE:
39             errmsg = "The file \"%s\" is a \"special file\" or socket or other non-regular file.";
40             break;
41
42         case WTAP_ERR_FILE_UNKNOWN_FORMAT:
43             /* Seen only when opening a capture file for reading. */
44             errmsg = "The file \"%s\" isn't a capture file in a format Wireshark understands.";
45             break;
46
47         case WTAP_ERR_UNSUPPORTED:
48             /* Seen only when opening a capture file for reading. */
49             g_snprintf(errmsg_errno, sizeof(errmsg_errno),
50                        "The file \"%%s\" contains record data that Wireshark doesn't support.\n"
51                        "(%s)", err_info != NULL ? err_info : "no information supplied");
52             g_free(err_info);
53             errmsg = errmsg_errno;
54             break;
55
56         case WTAP_ERR_CANT_WRITE_TO_PIPE:
57             /* Seen only when opening a capture file for writing. */
58             g_snprintf(errmsg_errno, sizeof(errmsg_errno),
59                        "The file \"%%s\" is a pipe, and %s capture files can't be "
60                        "written to a pipe.", wtap_file_type_subtype_string(file_type));
61             errmsg = errmsg_errno;
62             break;
63
64         case WTAP_ERR_UNWRITABLE_FILE_TYPE:
65             /* Seen only when opening a capture file for writing. */
66             errmsg = "Wireshark doesn't support writing capture files in that format.";
67             break;
68
69         case WTAP_ERR_UNWRITABLE_ENCAP:
70             /* Seen only when opening a capture file for writing. */
71             errmsg = "Wireshark can't save this capture in that format.";
72             break;
73
74         case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
75             if (for_writing)
76                 errmsg = "Wireshark can't save this capture in that format.";
77             else
78                 errmsg = "The file \"%s\" is a capture for a network type that Wireshark doesn't support.";
79             break;
80
81         case WTAP_ERR_BAD_FILE:
82             /* Seen only when opening a capture file for reading. */
83             g_snprintf(errmsg_errno, sizeof(errmsg_errno),
84                        "The file \"%%s\" appears to be damaged or corrupt.\n"
85                        "(%s)", err_info != NULL ? err_info : "no information supplied");
86             g_free(err_info);
87             errmsg = errmsg_errno;
88             break;
89
90         case WTAP_ERR_CANT_OPEN:
91             if (for_writing)
92                 errmsg = "The file \"%s\" could not be created for some unknown reason.";
93             else
94                 errmsg = "The file \"%s\" could not be opened for some unknown reason.";
95             break;
96
97         case WTAP_ERR_SHORT_READ:
98             errmsg = "The file \"%s\" appears to have been cut short"
99                 " in the middle of a packet or other data.";
100             break;
101
102         case WTAP_ERR_SHORT_WRITE:
103             errmsg = "A full header couldn't be written to the file \"%s\".";
104             break;
105
106         case WTAP_ERR_DECOMPRESS:
107             g_snprintf(errmsg_errno, sizeof(errmsg_errno),
108                        "The compressed file \"%%s\" appears to be damaged or corrupt.\n"
109                        "(%s)", err_info != NULL ? err_info : "no information supplied");
110             g_free(err_info);
111             errmsg = errmsg_errno;
112             break;
113
114         default:
115             g_snprintf(errmsg_errno, sizeof(errmsg_errno),
116                        "The file \"%%s\" could not be %s: %s.",
117                        for_writing ? "created" : "opened",
118                        wtap_strerror(err));
119             errmsg = errmsg_errno;
120             break;
121         }
122     } else
123         errmsg = file_open_error_message(err, for_writing);
124     return errmsg;
125 }
126
127 /* new file arrived */
128 gboolean capture_info_new_file(const char *new_filename, info_data_t* cap_info)
129 {
130     int err;
131     gchar *err_info;
132     gchar *err_msg;
133
134
135     if(cap_info->wtap != NULL) {
136         wtap_close(cap_info->wtap);
137     }
138
139     cap_info->wtap = wtap_open_offline(new_filename, WTAP_TYPE_AUTO, &err, &err_info, FALSE);
140     if (!cap_info->wtap) {
141         err_msg = g_strdup_printf(cf_open_error_message(err, err_info, FALSE, WTAP_FILE_TYPE_SUBTYPE_UNKNOWN),
142                                   new_filename);
143         g_warning("capture_info_new_file: %d (%s)", err, err_msg);
144         g_free (err_msg);
145         return FALSE;
146     } else
147         return TRUE;
148 }
149
150 static void
151 capture_info_packet(info_data_t* cap_info, gint wtap_linktype, const guchar *pd, guint32 caplen, union wtap_pseudo_header *pseudo_header)
152 {
153     capture_packet_info_t cpinfo;
154
155     /* Setup the capture packet structure */
156     cpinfo.counts = cap_info->counts.counts_hash;
157
158     cap_info->counts.total++;
159     if (!try_capture_dissector("wtap_encap", wtap_linktype, pd, 0, caplen, &cpinfo, pseudo_header))
160         cap_info->counts.other++;
161 }
162
163 /* new packets arrived */
164 void capture_info_new_packets(int to_read, info_data_t* cap_info)
165 {
166     int err;
167     gchar *err_info;
168     gint64 data_offset;
169     struct wtap_pkthdr *phdr;
170     union wtap_pseudo_header *pseudo_header;
171     int wtap_linktype;
172     const guchar *buf;
173
174
175     cap_info->ui.new_packets = to_read;
176
177     /*g_warning("new packets: %u", to_read);*/
178
179     while (to_read > 0) {
180         wtap_cleareof(cap_info->wtap);
181         if (wtap_read(cap_info->wtap, &err, &err_info, &data_offset)) {
182             phdr = wtap_phdr(cap_info->wtap);
183             pseudo_header = &phdr->pseudo_header;
184             wtap_linktype = phdr->pkt_encap;
185             buf = wtap_buf_ptr(cap_info->wtap);
186
187             capture_info_packet(cap_info, wtap_linktype, buf, phdr->caplen, pseudo_header);
188
189             /*g_warning("new packet");*/
190             to_read--;
191         }
192     }
193
194     capture_info_ui_update(&cap_info->ui);
195 }
196
197
198 /* close the info */
199 void capture_info_close(info_data_t* cap_info)
200 {
201     capture_info_ui_destroy(&cap_info->ui);
202     if(cap_info->wtap)
203         wtap_close(cap_info->wtap);
204 }
205
206 #endif /* HAVE_LIBPCAP */
207
208 /*
209  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
210  *
211  * Local variables:
212  * c-basic-offset: 4
213  * tab-width: 8
214  * indent-tabs-mode: nil
215  * End:
216  *
217  * vi: set shiftwidth=4 tabstop=8 expandtab:
218  * :indentSize=4:tabSize=8:noTabs=true:
219  */