Move some headers for UI stuff, and the alert_box.c UI-specific file, to
[metze/wireshark/wip.git] / ui / gtk / file_import_dlg.c
1 /* file_import_dlg.c
2  * Dialog to setup for import of a text file, like text2pcap
3  * November 2010, Jaap Keuter <jaap.keuter@xs4all.nl>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
24  * USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #include <ctype.h>
35 #include <string.h>
36
37 #include "globals.h"
38 #include "wtap.h"
39 #include "pcap-encap.h"
40
41 #include <epan/prefs.h>
42
43 #include <gtk/gtk.h>
44
45 #include "filters.h"
46
47 #include "ui/simple_dialog.h"
48 #include "ui/alert_box.h"
49
50 #include "ui/gtk/gtkglobals.h"
51 #include "ui/gtk/stock_icons.h"
52 #include "ui/gtk/dlg_utils.h"
53 #include "ui/gtk/gui_utils.h"
54 #include "ui/gtk/file_dlg.h"
55 #include "ui/gtk/capture_file_dlg.h"
56 #include "ui/gtk/help_dlg.h"
57
58 #include "ui/gtk/file_import_dlg.h"
59 #include "ui/gtk/text_import.h"
60 #include "ui/gtk/text_import_scanner.h"
61
62 #include "file.h"
63 #include "wsutil/file_util.h"
64 #include "tempfile.h"
65
66 #define INPUT_FRM_KEY                   "input_frame"
67
68 #define INPUT_FILENAME_TE_KEY           "input_filename_text"
69
70 #define INPUT_OFFSET_HEX_RB_KEY         "input_offset_hex_radio"
71 #define INPUT_OFFSET_OCT_RB_KEY         "input_offset_oct_radio"
72 #define INPUT_OFFSET_DEC_RB_KEY         "input_offset_dec_radio"
73
74 #define INPUT_DATETIME_CB_KEY           "input_datetime_checkbox"
75 #define INPUT_TIMEFMT_LBL_KEY           "input_timeformat_label"
76 #define INPUT_TIMEFMT_TE_KEY            "input_timeformat_entry"
77
78 #define IMPORT_FRM_KEY                  "import_frame"
79 #define IMPORT_ENCAP_CO_KEY             "import_encap_combo"
80
81 #define IMPORT_HEADER_FRM_KEY           "import_header_frame"
82 #define IMPORT_HEADER_CB_KEY            "import_header_checkbox"
83 #define IMPORT_HEADER_ETH_RB_KEY        "import_header_ethernet_radio"
84 #define IMPORT_HEADER_ETYPE_LBL_KEY     "import_header_etype_label"
85 #define IMPORT_HEADER_ETYPE_TE_KEY      "import_header_etype_text"
86 #define IMPORT_HEADER_IPV4_RB_KEY       "import_header_ipv4_radio"
87 #define IMPORT_HEADER_PROT_LBL_KEY      "import_header_prot_label"
88 #define IMPORT_HEADER_PROT_TE_KEY       "import_header_prot_text"
89 #define IMPORT_HEADER_UDP_RB_KEY        "import_header_udp_radio"
90 #define IMPORT_HEADER_SRC_PORT_LBL_KEY  "import_header_src_port_label"
91 #define IMPORT_HEADER_SRC_PORT_TE_KEY   "import_header_src_port_text"
92 #define IMPORT_HEADER_TCP_RB_KEY        "import_header_tcp_radio"
93 #define IMPORT_HEADER_DST_PORT_LBL_KEY  "import_header_dst_port_label"
94 #define IMPORT_HEADER_DST_PORT_TE_KEY   "import_header_dst_port_text"
95 #define IMPORT_HEADER_SCTP_RB_KEY       "import_header_sctp_radio"
96 #define IMPORT_HEADER_TAG_LBL_KEY       "import_header_tag_label"
97 #define IMPORT_HEADER_TAG_TE_KEY        "import_header_tag_text"
98 #define IMPORT_HEADER_SCTP_D_RB_KEY     "import_header_sctp_data_radio"
99 #define IMPORT_HEADER_PPI_LBL_KEY       "import_header_ppi_label"
100 #define IMPORT_HEADER_PPI_TE_KEY        "import_header_ppi_text"
101
102 #define IMPORT_FRAME_LENGTH_TE_KEY      "import_frame_length_text"
103
104 static GtkWidget *file_import_dlg_w = NULL;
105 static GtkListStore *encap_list_store = NULL;
106
107 /*****************************************************************************/
108
109 static void
110 file_import_dlg_destroy_cb(GtkWidget *win _U_, gpointer user_data _U_)
111 {
112     file_import_dlg_w = NULL;
113 }
114
115 /*****************************************************************************/
116
117 static void
118 browse_file_cb(GtkWidget *browse_bt, GtkWidget *filename_te)
119 {
120     file_selection_browse(browse_bt, filename_te, "Wireshark: Import from Text",
121         FILE_SELECTION_READ_BROWSE);
122 }
123
124 static void
125 timefmt_cb_toggle(GtkWidget *widget, gpointer data _U_)
126 {
127     GtkWidget *timefmt_lbl, *timefmt_te;
128     gboolean apply_fmt;
129
130     timefmt_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(widget), INPUT_TIMEFMT_LBL_KEY));
131     timefmt_te = GTK_WIDGET(g_object_get_data(G_OBJECT(widget), INPUT_TIMEFMT_TE_KEY));
132
133     apply_fmt = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
134     gtk_widget_set_sensitive(timefmt_lbl, apply_fmt);
135     gtk_widget_set_sensitive(timefmt_te, apply_fmt);
136 }
137
138 /*****************************************************************************/
139 static void
140 create_encap_list_store(void)
141 {
142     GtkTreeIter iter;
143     gint encap;
144     const gchar *name;
145
146     encap_list_store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_UINT);
147
148     /* Scan all Wiretap encapsulation types */
149     for (encap = 1; encap < wtap_get_num_encap_types(); encap++)
150     {
151         /* Check if we can write it to a PCAP file */
152         if ((wtap_wtap_encap_to_pcap_encap(encap) > 0) &&
153             /*
154              * Exclude wtap encapsulations that require a pseudo header,
155              * because we won't setup one from the text we import and
156              * wiretap doesn't allow us to write 'raw' frames
157              */
158             !((encap == WTAP_ENCAP_ATM_PDUS) ||
159               (encap == WTAP_ENCAP_IRDA) ||
160               (encap == WTAP_ENCAP_MTP2_WITH_PHDR) ||
161               (encap == WTAP_ENCAP_LINUX_LAPD) ||
162               (encap == WTAP_ENCAP_SITA) ||
163               (encap == WTAP_ENCAP_ERF) ||
164               (encap == WTAP_ENCAP_I2C) ||
165               (encap == WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR) ||
166               (encap == WTAP_ENCAP_PPP_WITH_PHDR)
167            ) )
168         {
169             /* If it has got a name */
170             if ((name = wtap_encap_string(encap)))
171             {
172                 gtk_list_store_append(encap_list_store, &iter);
173                 gtk_list_store_set(encap_list_store, &iter, 0, name, 1, encap, -1);
174             }
175         }
176     }
177
178 }
179
180 static GtkWidget *
181 fill_encap_combo(void)
182 {
183     GtkWidget *encap_co = NULL;
184     GtkCellRenderer *cell;
185
186     encap_co = gtk_combo_box_new_with_model(GTK_TREE_MODEL(encap_list_store));
187     cell = gtk_cell_renderer_text_new();
188     gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(encap_co), cell, TRUE);
189     gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(encap_co), cell, "text", 0, NULL);
190
191     return encap_co;
192 }
193
194 static void header_frm_child_set(GtkWidget *widget, gpointer data);
195
196 static void
197 encap_co_changed(GtkComboBox *widget, gpointer data)
198 {
199     GtkTreeIter iter;
200     gboolean result;
201     GtkWidget *header_cb;
202
203     result = gtk_combo_box_get_active_iter(widget, &iter);
204
205     if (result)
206     {
207         guint encap;
208         GtkTreeModel *model = gtk_combo_box_get_model(widget);
209         gtk_tree_model_get(model, &iter, 1, &encap, -1);
210
211         if (encap != WTAP_ENCAP_ETHERNET)
212             result = FALSE;
213     }
214
215     if (result)
216     {
217         header_cb = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_CB_KEY));
218         g_signal_emit_by_name(G_OBJECT(header_cb), "toggled", data);
219     } else {
220         gtk_container_foreach(GTK_CONTAINER(data), header_frm_child_set, GUINT_TO_POINTER(result));
221     }
222 }
223
224 /*****************************************************************************/
225
226 static void
227 header_frm_child_set(GtkWidget *widget, gpointer data)
228 {
229     gtk_widget_set_sensitive(widget, GPOINTER_TO_UINT(data));
230 }
231
232 static void
233 header_cb_toggle(GtkWidget *widget, gpointer data)
234 {
235     gtk_container_foreach(GTK_CONTAINER(data), header_frm_child_set,
236         GUINT_TO_POINTER(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))));
237     /* The frame's checkbox must stay sensitive, of course... */
238     gtk_widget_set_sensitive(widget, TRUE);
239 }
240
241 /*
242  * Header radio button toggle handlers
243  */
244 static void
245 header_eth_rb_toggle(GtkWidget *widget, gpointer data)
246 {
247     GtkWidget *etype_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_ETYPE_LBL_KEY));
248     GtkWidget *etype_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_ETYPE_TE_KEY));
249     GtkWidget *prot_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PROT_LBL_KEY));
250     GtkWidget *prot_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PROT_TE_KEY));
251     GtkWidget *src_port_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_SRC_PORT_LBL_KEY));
252     GtkWidget *src_port_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_SRC_PORT_TE_KEY));
253     GtkWidget *dst_port_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_DST_PORT_LBL_KEY));
254     GtkWidget *dst_port_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_DST_PORT_TE_KEY));
255     GtkWidget *tag_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_TAG_LBL_KEY));
256     GtkWidget *tag_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_TAG_TE_KEY));
257     GtkWidget *ppi_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PPI_LBL_KEY));
258     GtkWidget *ppi_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PPI_TE_KEY));
259
260     if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
261     {
262         gtk_widget_set_sensitive(etype_lbl, TRUE);
263         gtk_widget_set_sensitive(etype_te, TRUE);
264         gtk_widget_set_sensitive(prot_lbl, FALSE);
265         gtk_widget_set_sensitive(prot_te, FALSE);
266         gtk_widget_set_sensitive(src_port_lbl, FALSE);
267         gtk_widget_set_sensitive(src_port_te, FALSE);
268         gtk_widget_set_sensitive(dst_port_lbl, FALSE);
269         gtk_widget_set_sensitive(dst_port_te, FALSE);
270         gtk_widget_set_sensitive(tag_lbl, FALSE);
271         gtk_widget_set_sensitive(tag_te, FALSE);
272         gtk_widget_set_sensitive(ppi_lbl, FALSE);
273         gtk_widget_set_sensitive(ppi_te, FALSE);
274     }
275     else
276     {
277         gtk_widget_set_sensitive(etype_lbl, FALSE);
278         gtk_widget_set_sensitive(etype_te, FALSE);
279     }
280 }
281
282 static void
283 header_ipv4_rb_toggle(GtkWidget *widget, gpointer data)
284 {
285     GtkWidget *etype_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_ETYPE_LBL_KEY));
286     GtkWidget *etype_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_ETYPE_TE_KEY));
287     GtkWidget *prot_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PROT_LBL_KEY));
288     GtkWidget *prot_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PROT_TE_KEY));
289     GtkWidget *src_port_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_SRC_PORT_LBL_KEY));
290     GtkWidget *src_port_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_SRC_PORT_TE_KEY));
291     GtkWidget *dst_port_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_DST_PORT_LBL_KEY));
292     GtkWidget *dst_port_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_DST_PORT_TE_KEY));
293     GtkWidget *tag_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_TAG_LBL_KEY));
294     GtkWidget *tag_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_TAG_TE_KEY));
295     GtkWidget *ppi_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PPI_LBL_KEY));
296     GtkWidget *ppi_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PPI_TE_KEY));
297
298     if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
299     {
300         gtk_widget_set_sensitive(etype_lbl, FALSE);
301         gtk_widget_set_sensitive(etype_te, FALSE);
302         gtk_widget_set_sensitive(prot_lbl, TRUE);
303         gtk_widget_set_sensitive(prot_te, TRUE);
304         gtk_widget_set_sensitive(src_port_lbl, FALSE);
305         gtk_widget_set_sensitive(src_port_te, FALSE);
306         gtk_widget_set_sensitive(dst_port_lbl, FALSE);
307         gtk_widget_set_sensitive(dst_port_te, FALSE);
308         gtk_widget_set_sensitive(tag_lbl, FALSE);
309         gtk_widget_set_sensitive(tag_te, FALSE);
310         gtk_widget_set_sensitive(ppi_lbl, FALSE);
311         gtk_widget_set_sensitive(ppi_te, FALSE);
312     }
313     else
314     {
315         gtk_widget_set_sensitive(prot_lbl, FALSE);
316         gtk_widget_set_sensitive(prot_te, FALSE);
317     }
318 }
319
320 static void
321 header_udp_rb_toggle(GtkWidget *widget, gpointer data)
322 {
323     GtkWidget *etype_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_ETYPE_LBL_KEY));
324     GtkWidget *etype_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_ETYPE_TE_KEY));
325     GtkWidget *prot_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PROT_LBL_KEY));
326     GtkWidget *prot_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PROT_TE_KEY));
327     GtkWidget *src_port_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_SRC_PORT_LBL_KEY));
328     GtkWidget *src_port_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_SRC_PORT_TE_KEY));
329     GtkWidget *dst_port_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_DST_PORT_LBL_KEY));
330     GtkWidget *dst_port_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_DST_PORT_TE_KEY));
331     GtkWidget *tag_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_TAG_LBL_KEY));
332     GtkWidget *tag_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_TAG_TE_KEY));
333     GtkWidget *ppi_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PPI_LBL_KEY));
334     GtkWidget *ppi_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PPI_TE_KEY));
335
336     if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
337     {
338         gtk_widget_set_sensitive(etype_lbl, FALSE);
339         gtk_widget_set_sensitive(etype_te, FALSE);
340         gtk_widget_set_sensitive(prot_lbl, FALSE);
341         gtk_widget_set_sensitive(prot_te, FALSE);
342         gtk_widget_set_sensitive(src_port_lbl, TRUE);
343         gtk_widget_set_sensitive(src_port_te, TRUE);
344         gtk_widget_set_sensitive(dst_port_lbl, TRUE);
345         gtk_widget_set_sensitive(dst_port_te, TRUE);
346         gtk_widget_set_sensitive(tag_lbl, FALSE);
347         gtk_widget_set_sensitive(tag_te, FALSE);
348         gtk_widget_set_sensitive(ppi_lbl, FALSE);
349         gtk_widget_set_sensitive(ppi_te, FALSE);
350     }
351     else
352     {
353         gtk_widget_set_sensitive(src_port_lbl, FALSE);
354         gtk_widget_set_sensitive(src_port_te, FALSE);
355         gtk_widget_set_sensitive(dst_port_lbl, FALSE);
356         gtk_widget_set_sensitive(dst_port_te, FALSE);
357     }
358 }
359
360 static void
361 header_tcp_rb_toggle(GtkWidget *widget, gpointer data)
362 {
363     GtkWidget *etype_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_ETYPE_LBL_KEY));
364     GtkWidget *etype_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_ETYPE_TE_KEY));
365     GtkWidget *prot_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PROT_LBL_KEY));
366     GtkWidget *prot_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PROT_TE_KEY));
367     GtkWidget *src_port_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_SRC_PORT_LBL_KEY));
368     GtkWidget *src_port_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_SRC_PORT_TE_KEY));
369     GtkWidget *dst_port_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_DST_PORT_LBL_KEY));
370     GtkWidget *dst_port_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_DST_PORT_TE_KEY));
371     GtkWidget *tag_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_TAG_LBL_KEY));
372     GtkWidget *tag_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_TAG_TE_KEY));
373     GtkWidget *ppi_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PPI_LBL_KEY));
374     GtkWidget *ppi_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PPI_TE_KEY));
375
376     if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
377     {
378         gtk_widget_set_sensitive(etype_lbl, FALSE);
379         gtk_widget_set_sensitive(etype_te, FALSE);
380         gtk_widget_set_sensitive(prot_lbl, FALSE);
381         gtk_widget_set_sensitive(prot_te, FALSE);
382         gtk_widget_set_sensitive(src_port_lbl, TRUE);
383         gtk_widget_set_sensitive(src_port_te, TRUE);
384         gtk_widget_set_sensitive(dst_port_lbl, TRUE);
385         gtk_widget_set_sensitive(dst_port_te, TRUE);
386         gtk_widget_set_sensitive(tag_lbl, FALSE);
387         gtk_widget_set_sensitive(tag_te, FALSE);
388         gtk_widget_set_sensitive(ppi_lbl, FALSE);
389         gtk_widget_set_sensitive(ppi_te, FALSE);
390     }
391     else
392     {
393         gtk_widget_set_sensitive(src_port_lbl, FALSE);
394         gtk_widget_set_sensitive(src_port_te, FALSE);
395         gtk_widget_set_sensitive(dst_port_lbl, FALSE);
396         gtk_widget_set_sensitive(dst_port_te, FALSE);
397     }
398 }
399
400 static void
401 header_sctp_rb_toggle(GtkWidget *widget, gpointer data)
402 {
403     GtkWidget *etype_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_ETYPE_LBL_KEY));
404     GtkWidget *etype_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_ETYPE_TE_KEY));
405     GtkWidget *prot_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PROT_LBL_KEY));
406     GtkWidget *prot_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PROT_TE_KEY));
407     GtkWidget *src_port_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_SRC_PORT_LBL_KEY));
408     GtkWidget *src_port_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_SRC_PORT_TE_KEY));
409     GtkWidget *dst_port_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_DST_PORT_LBL_KEY));
410     GtkWidget *dst_port_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_DST_PORT_TE_KEY));
411     GtkWidget *tag_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_TAG_LBL_KEY));
412     GtkWidget *tag_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_TAG_TE_KEY));
413     GtkWidget *ppi_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PPI_LBL_KEY));
414     GtkWidget *ppi_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PPI_TE_KEY));
415
416     if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
417     {
418         gtk_widget_set_sensitive(etype_lbl, FALSE);
419         gtk_widget_set_sensitive(etype_te, FALSE);
420         gtk_widget_set_sensitive(prot_lbl, FALSE);
421         gtk_widget_set_sensitive(prot_te, FALSE);
422         gtk_widget_set_sensitive(src_port_lbl, TRUE);
423         gtk_widget_set_sensitive(src_port_te, TRUE);
424         gtk_widget_set_sensitive(dst_port_lbl, TRUE);
425         gtk_widget_set_sensitive(dst_port_te, TRUE);
426         gtk_widget_set_sensitive(tag_lbl, TRUE);
427         gtk_widget_set_sensitive(tag_te, TRUE);
428         gtk_widget_set_sensitive(ppi_lbl, FALSE);
429         gtk_widget_set_sensitive(ppi_te, FALSE);
430     }
431     else
432     {
433         gtk_widget_set_sensitive(src_port_lbl, FALSE);
434         gtk_widget_set_sensitive(src_port_te, FALSE);
435         gtk_widget_set_sensitive(dst_port_lbl, FALSE);
436         gtk_widget_set_sensitive(dst_port_te, FALSE);
437         gtk_widget_set_sensitive(tag_lbl, FALSE);
438         gtk_widget_set_sensitive(tag_te, FALSE);
439     }
440 }
441
442 static void
443 header_sctp_data_rb_toggle(GtkWidget *widget, gpointer data)
444 {
445     GtkWidget *etype_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_ETYPE_LBL_KEY));
446     GtkWidget *etype_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_ETYPE_TE_KEY));
447     GtkWidget *prot_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PROT_LBL_KEY));
448     GtkWidget *prot_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PROT_TE_KEY));
449     GtkWidget *src_port_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_SRC_PORT_LBL_KEY));
450     GtkWidget *src_port_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_SRC_PORT_TE_KEY));
451     GtkWidget *dst_port_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_DST_PORT_LBL_KEY));
452     GtkWidget *dst_port_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_DST_PORT_TE_KEY));
453     GtkWidget *tag_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_TAG_LBL_KEY));
454     GtkWidget *tag_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_TAG_TE_KEY));
455     GtkWidget *ppi_lbl = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PPI_LBL_KEY));
456     GtkWidget *ppi_te  = GTK_WIDGET(g_object_get_data(G_OBJECT(data), IMPORT_HEADER_PPI_TE_KEY));
457
458     if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
459     {
460         gtk_widget_set_sensitive(etype_lbl, FALSE);
461         gtk_widget_set_sensitive(etype_te, FALSE);
462         gtk_widget_set_sensitive(prot_lbl, FALSE);
463         gtk_widget_set_sensitive(prot_te, FALSE);
464         gtk_widget_set_sensitive(src_port_lbl, TRUE);
465         gtk_widget_set_sensitive(src_port_te, TRUE);
466         gtk_widget_set_sensitive(dst_port_lbl, TRUE);
467         gtk_widget_set_sensitive(dst_port_te, TRUE);
468         gtk_widget_set_sensitive(tag_lbl, FALSE);
469         gtk_widget_set_sensitive(tag_te, FALSE);
470         gtk_widget_set_sensitive(ppi_lbl, TRUE);
471         gtk_widget_set_sensitive(ppi_te, TRUE);
472     }
473     else
474     {
475         gtk_widget_set_sensitive(src_port_lbl, FALSE);
476         gtk_widget_set_sensitive(src_port_te, FALSE);
477         gtk_widget_set_sensitive(dst_port_lbl, FALSE);
478         gtk_widget_set_sensitive(dst_port_te, FALSE);
479         gtk_widget_set_sensitive(ppi_lbl, FALSE);
480         gtk_widget_set_sensitive(ppi_te, FALSE);
481     }
482 }
483
484 /*****************************************************************************/
485
486 static void
487 file_import_open(text_import_info_t *info)
488 {
489     int import_file_fd;
490     char *tmpname, *capfile_name = NULL;
491     int err;
492
493     /* Choose a random name for the temporary import buffer */
494     import_file_fd = create_tempfile(&tmpname, "import");
495     capfile_name = g_strdup(tmpname);
496
497     info->wdh = wtap_dump_fdopen(import_file_fd, WTAP_FILE_PCAP, info->encapsulation, info->max_frame_length, FALSE, &err);
498     if (info->wdh == NULL) {
499         open_failure_alert_box(capfile_name, err, TRUE);
500         fclose(info->import_text_file);
501         goto end;
502     }
503
504     text_import_setup(info);
505
506     text_importin = info->import_text_file;
507
508     text_importlex();
509
510     text_import_cleanup();
511
512     if (fclose(info->import_text_file))
513     {
514         read_failure_alert_box(info->import_text_filename, errno);
515     }
516
517     if (!wtap_dump_close(info->wdh, &err))
518     {
519         write_failure_alert_box(capfile_name, err);
520     }
521
522     if (cf_open(&cfile, capfile_name, TRUE /* temporary file */, &err) != CF_OK)
523     {
524         open_failure_alert_box(capfile_name, err, FALSE);
525         goto end;
526     }
527
528     switch (cf_read(&cfile, FALSE)) {
529     case CF_READ_OK:
530     case CF_READ_ERROR:
531     /* Just because we got an error, that doesn't mean we were unable
532        to read any of the file; we handle what we could get from the
533        file. */
534     break;
535
536     case CF_READ_ABORTED:
537     /* The user bailed out of re-reading the capture file; the
538        capture file has been closed - just free the capture file name
539        string and return (without changing the last containing
540        directory). */
541     break;
542     }
543
544 end:
545     g_free(info->import_text_filename);
546     g_free(info->date_timestamp_format);
547     g_free(info);
548     g_free(capfile_name);
549     window_destroy(file_import_dlg_w);
550 }
551
552 static text_import_info_t *
553 setup_file_import(GtkWidget *main_w)
554 {
555     GtkWidget *input_frm, *import_frm;
556
557     text_import_info_t *text_import_info = g_malloc0(sizeof(text_import_info_t));
558
559     /* Retrieve the input and import settings from the dialog */
560
561     /* First the main components */
562     input_frm = GTK_WIDGET(g_object_get_data(G_OBJECT(main_w), INPUT_FRM_KEY));
563     import_frm = GTK_WIDGET(g_object_get_data(G_OBJECT(main_w), IMPORT_FRM_KEY));
564
565     /* Then the input frame controls of interest */
566     {
567         GtkWidget *filename_te = GTK_WIDGET(g_object_get_data(G_OBJECT(input_frm), INPUT_FILENAME_TE_KEY));
568         GtkWidget *offset_hex_rb = GTK_WIDGET(g_object_get_data(G_OBJECT(input_frm), INPUT_OFFSET_HEX_RB_KEY));
569         GtkWidget *offset_oct_rb = GTK_WIDGET(g_object_get_data(G_OBJECT(input_frm), INPUT_OFFSET_OCT_RB_KEY));
570         GtkWidget *offset_dec_rb = GTK_WIDGET(g_object_get_data(G_OBJECT(input_frm), INPUT_OFFSET_DEC_RB_KEY));
571         GtkWidget *timefmt_cb = GTK_WIDGET(g_object_get_data(G_OBJECT(input_frm), INPUT_DATETIME_CB_KEY));
572         GtkWidget *timefmt_te = GTK_WIDGET(g_object_get_data(G_OBJECT(input_frm), INPUT_TIMEFMT_TE_KEY));
573
574         text_import_info->import_text_filename = g_strdup(gtk_entry_get_text(GTK_ENTRY(filename_te)));
575
576         /* Try to open the input file */
577         text_import_info->import_text_file = ws_fopen(text_import_info->import_text_filename, "rb");
578         if (!text_import_info->import_text_file) {
579             open_failure_alert_box(text_import_info->import_text_filename, errno, FALSE);
580             g_free(text_import_info->import_text_filename);
581             g_free(text_import_info->date_timestamp_format);
582             g_free(text_import_info);
583             return NULL;
584         }
585
586         text_import_info->offset_type =
587             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(offset_hex_rb)) ? OFFSET_HEX :
588             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(offset_oct_rb)) ? OFFSET_OCT :
589             gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(offset_dec_rb)) ? OFFSET_DEC :
590             0;
591         text_import_info->date_timestamp = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(timefmt_cb));
592         text_import_info->date_timestamp_format = g_strdup(gtk_entry_get_text(GTK_ENTRY(timefmt_te)));
593     }
594
595     /* Then the import frame controls of interest */
596     {
597         GtkWidget *encap_co = GTK_WIDGET(g_object_get_data(G_OBJECT(import_frm), IMPORT_ENCAP_CO_KEY));
598         GtkWidget *header_frm = GTK_WIDGET(g_object_get_data(G_OBJECT(import_frm), IMPORT_HEADER_FRM_KEY));
599         GtkWidget *framelen_te = GTK_WIDGET(g_object_get_data(G_OBJECT(import_frm), IMPORT_FRAME_LENGTH_TE_KEY));
600
601         /* Then the header frame controls of interest */
602         GtkWidget *header_cb = GTK_WIDGET(g_object_get_data(G_OBJECT(header_frm), IMPORT_HEADER_CB_KEY));
603
604         GtkWidget *header_eth_rb = GTK_WIDGET(g_object_get_data(G_OBJECT(header_frm), IMPORT_HEADER_ETH_RB_KEY));
605         GtkWidget *header_ipv4_rb = GTK_WIDGET(g_object_get_data(G_OBJECT(header_frm), IMPORT_HEADER_IPV4_RB_KEY));
606         GtkWidget *header_udp_rb = GTK_WIDGET(g_object_get_data(G_OBJECT(header_frm), IMPORT_HEADER_UDP_RB_KEY));
607         GtkWidget *header_tcp_rb = GTK_WIDGET(g_object_get_data(G_OBJECT(header_frm), IMPORT_HEADER_TCP_RB_KEY));
608         GtkWidget *header_sctp_rb = GTK_WIDGET(g_object_get_data(G_OBJECT(header_frm), IMPORT_HEADER_SCTP_RB_KEY));
609         GtkWidget *header_sctp_data_rb = GTK_WIDGET(g_object_get_data(G_OBJECT(header_frm), IMPORT_HEADER_SCTP_D_RB_KEY));
610
611         GtkWidget *etype_te = GTK_WIDGET(g_object_get_data(G_OBJECT(header_frm), IMPORT_HEADER_ETYPE_TE_KEY));
612         GtkWidget *protocol_te = GTK_WIDGET(g_object_get_data(G_OBJECT(header_frm), IMPORT_HEADER_PROT_TE_KEY));
613         GtkWidget *src_port_te = GTK_WIDGET(g_object_get_data(G_OBJECT(header_frm), IMPORT_HEADER_SRC_PORT_TE_KEY));
614         GtkWidget *dst_port_te = GTK_WIDGET(g_object_get_data(G_OBJECT(header_frm), IMPORT_HEADER_DST_PORT_TE_KEY));
615         GtkWidget *tag_te = GTK_WIDGET(g_object_get_data(G_OBJECT(header_frm), IMPORT_HEADER_TAG_TE_KEY));
616         GtkWidget *ppi_te = GTK_WIDGET(g_object_get_data(G_OBJECT(header_frm), IMPORT_HEADER_PPI_TE_KEY));
617
618         GtkTreeIter iter;
619
620         if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(encap_co), &iter))
621         {
622             GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX(encap_co));
623             gtk_tree_model_get(model, &iter, 1, &text_import_info->encapsulation, -1);
624         }
625
626         if ((text_import_info->encapsulation == WTAP_ENCAP_ETHERNET) &&
627             (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(header_cb))))
628         {
629             text_import_info->dummy_header_type =
630                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(header_eth_rb)) ? HEADER_ETH :
631                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(header_ipv4_rb)) ? HEADER_IPV4 :
632                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(header_udp_rb)) ? HEADER_UDP :
633                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(header_tcp_rb)) ? HEADER_TCP :
634                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(header_sctp_rb)) ? HEADER_SCTP :
635                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(header_sctp_data_rb)) ? HEADER_SCTP_DATA :
636                 HEADER_NONE;
637
638             switch (text_import_info->dummy_header_type)
639             {
640             case HEADER_ETH:
641                 text_import_info->pid = strtol(gtk_entry_get_text(GTK_ENTRY(etype_te)), NULL, 16);
642                 if (text_import_info->pid > 0xffff)
643                 {
644                     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "The Ethertype (%x) is too large.",
645                         text_import_info->pid);
646                     g_free(text_import_info->import_text_filename);
647                     fclose(text_import_info->import_text_file);
648                     g_free(text_import_info->date_timestamp_format);
649                     g_free(text_import_info);
650                     return NULL;
651                 }
652                 break;
653
654             case HEADER_IPV4:
655                 text_import_info->protocol = strtol(gtk_entry_get_text(GTK_ENTRY(protocol_te)), NULL, 10);
656                 if (text_import_info->protocol > 0xff)
657                 {
658                     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "The IPv4 protocol (%u) is too large.",
659                         text_import_info->protocol);
660                     g_free(text_import_info->import_text_filename);
661                     fclose(text_import_info->import_text_file);
662                     g_free(text_import_info->date_timestamp_format);
663                     g_free(text_import_info);
664                     return NULL;
665                 }
666                 break;
667
668             case HEADER_UDP:
669             case HEADER_TCP:
670                 text_import_info->src_port = strtol(gtk_entry_get_text(GTK_ENTRY(src_port_te)), NULL, 10);
671                 if (text_import_info->src_port > 0xffff)
672                 {
673                     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "The source port (%u) is too large.",
674                         text_import_info->src_port);
675                     g_free(text_import_info->import_text_filename);
676                     fclose(text_import_info->import_text_file);
677                     g_free(text_import_info->date_timestamp_format);
678                     g_free(text_import_info);
679                     return NULL;
680                 }
681                 text_import_info->dst_port = strtol(gtk_entry_get_text(GTK_ENTRY(dst_port_te)), NULL, 10);
682                 if (text_import_info->dst_port > 0xffff)
683                 {
684                     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "The destination port (%u) is too large.",
685                         text_import_info->dst_port);
686                     g_free(text_import_info->import_text_filename);
687                     fclose(text_import_info->import_text_file);
688                     g_free(text_import_info->date_timestamp_format);
689                     g_free(text_import_info);
690                     return NULL;
691                 }
692                 break;
693
694             case HEADER_SCTP:
695                 text_import_info->src_port = strtol(gtk_entry_get_text(GTK_ENTRY(src_port_te)), NULL, 10);
696                 if (text_import_info->src_port > 0xffff)
697                 {
698                     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "The source port (%u) is too large.",
699                         text_import_info->src_port);
700                     g_free(text_import_info->import_text_filename);
701                     fclose(text_import_info->import_text_file);
702                     g_free(text_import_info->date_timestamp_format);
703                     g_free(text_import_info);
704                     return NULL;
705                 }
706                 text_import_info->dst_port = strtol(gtk_entry_get_text(GTK_ENTRY(dst_port_te)), NULL, 10);
707                 if (text_import_info->dst_port > 0xffff)
708                 {
709                     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "The destination port (%u) is too large.",
710                         text_import_info->dst_port);
711                     g_free(text_import_info->import_text_filename);
712                     fclose(text_import_info->import_text_file);
713                     g_free(text_import_info->date_timestamp_format);
714                     g_free(text_import_info);
715                     return NULL;
716                 }
717                 text_import_info->tag = strtol(gtk_entry_get_text(GTK_ENTRY(tag_te)), NULL, 10);
718                 break;
719
720             case HEADER_SCTP_DATA:
721                 text_import_info->src_port = strtol(gtk_entry_get_text(GTK_ENTRY(src_port_te)), NULL, 10);
722                 if (text_import_info->src_port > 0xffff)
723                 {
724                     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "The source port (%u) is too large.",
725                         text_import_info->src_port);
726                     g_free(text_import_info->import_text_filename);
727                     fclose(text_import_info->import_text_file);
728                     g_free(text_import_info->date_timestamp_format);
729                     g_free(text_import_info);
730                     return NULL;
731                 }
732                 text_import_info->dst_port = strtol(gtk_entry_get_text(GTK_ENTRY(dst_port_te)), NULL, 10);
733                 if (text_import_info->dst_port > 0xffff)
734                 {
735                     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "The destination port (%u) is too large.",
736                         text_import_info->dst_port);
737                     g_free(text_import_info->import_text_filename);
738                     fclose(text_import_info->import_text_file);
739                     g_free(text_import_info->date_timestamp_format);
740                     g_free(text_import_info);
741                     return NULL;
742                 }
743                 text_import_info->ppi = strtol(gtk_entry_get_text(GTK_ENTRY(ppi_te)), NULL, 10);
744                 break;
745
746             default:
747                 break;
748             }
749         } else {
750             text_import_info->dummy_header_type = HEADER_NONE;
751         }
752
753         text_import_info->max_frame_length = strtol(gtk_entry_get_text(GTK_ENTRY(framelen_te)), NULL, 10);
754         if (text_import_info->max_frame_length == 0) {
755             text_import_info->max_frame_length = IMPORT_MAX_PACKET;
756         }
757         else if (text_import_info->max_frame_length > IMPORT_MAX_PACKET)
758         {
759             simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "The maximum frame length (%u) is too long.",
760                 text_import_info->max_frame_length);
761             g_free(text_import_info->import_text_filename);
762             fclose(text_import_info->import_text_file);
763             g_free(text_import_info->date_timestamp_format);
764             g_free(text_import_info);
765             return NULL;
766         }
767     }
768
769     return text_import_info;
770 }
771
772 /*****************************************************************************/
773
774 static void
775 file_import_answered_cb(gpointer dialog _U_, gint btn, gpointer data)
776 {
777     text_import_info_t *text_import_info;
778
779     switch (btn) {
780     case ESD_BTN_SAVE:
781         /* save file first */
782         file_save_as_cmd(after_save_no_action, NULL, FALSE);
783         break;
784     case ESD_BTN_DONT_SAVE:
785         cf_close(&cfile);
786         break;
787     case ESD_BTN_CANCEL:
788         return;
789     default:
790         g_assert_not_reached();
791     }
792
793     text_import_info = setup_file_import(data);
794     if (text_import_info)
795         file_import_open(text_import_info);
796 }
797
798 static void
799 file_import_ok_cb(GtkWidget *widget _U_, gpointer data)
800 {
801     text_import_info_t *text_import_info;
802
803     if((cfile.state != FILE_CLOSED) && !cfile.user_saved && prefs.gui_ask_unsaved) {
804         gpointer dialog;
805         /* user didn't save his current file, ask him */
806         dialog = simple_dialog(ESD_TYPE_CONFIRMATION, ESD_BTNS_SAVE_DONTSAVE_CANCEL,
807                 "%sSave capture file before opening a new one?%s\n\n"
808                 "If you open a new capture file without saving, your capture data will be discarded.",
809                 simple_dialog_primary_start(), simple_dialog_primary_end());
810         simple_dialog_set_cb(dialog, file_import_answered_cb, data);
811     } else {
812         /* unchanged file, just open a new one */
813         text_import_info = setup_file_import(data);
814         if (text_import_info)
815             file_import_open(text_import_info);
816     }
817
818 }
819
820 /*****************************************************************************/
821
822 /*
823  * Dialog creator
824  */
825 static GtkWidget *
826 file_import_dlg_new(void)
827 {
828     GtkWidget  *main_w, *main_vb,
829                *input_frm, *input_tb, *input_vb,
830                *filename_hb, *filename_lbl, *filename_te, *browse_bt,
831                *offset_hb, *offset_lbl_vb, *offset_lbl, *offset_rb_vb,
832                *offset_hex_rb, *offset_oct_rb, *offset_dec_rb,
833                *timefmt_hb, *timefmt_cb, *timefmt_lbl, *timefmt_te,
834                *import_frm, *import_vb,
835                *encap_hb, *encap_lbl, *encap_co,
836                *header_cb, *header_frm, *header_hb,
837                *header_eth_rb, *header_ipv4_rb, *header_udp_rb,
838                *header_tcp_rb, *header_sctp_rb, *header_sctp_data_rb,
839                *header_rblbl_vb,
840                *header_rblbl_1_hb, *header_rblbl_1_lbl,
841                *header_rblbl_2_hb, *header_rblbl_2_lbl,
842                *header_rblbl_3_hb, *header_rblbl_3_lbl,
843                *header_rblbl_4_hb, *header_rblbl_4_lbl,
844                *header_rblbl_5_hb, *header_rblbl_5_lbl,
845                *header_rblbl_6_hb, *header_rblbl_6_lbl,
846                *etype_te, *protocol_te, *src_port_te,
847                *dst_port_te, *tag_te, *ppi_te,
848                *framelen_hb, *framelen_lbl, *framelen_te,
849                *bbox, *help_bt, *close_bt, *ok_bt;
850
851     /* Setup the dialog */
852
853     main_w = dlg_window_new("Wireshark: Import from Text");
854     gtk_window_set_default_size(GTK_WINDOW(main_w), 400, 300);
855
856     main_vb = gtk_vbox_new(FALSE, 0);
857     gtk_container_set_border_width(GTK_CONTAINER(main_vb), 3);
858     gtk_container_add(GTK_CONTAINER(main_w), main_vb);
859
860     /* Setup the input frame */
861
862     input_frm = gtk_frame_new("Input");
863     gtk_box_pack_start(GTK_BOX(main_vb), input_frm, FALSE, FALSE, 0);
864
865     g_object_set_data(G_OBJECT(main_w), INPUT_FRM_KEY, input_frm);
866
867     input_vb = gtk_vbox_new(FALSE, 0);
868     gtk_container_add(GTK_CONTAINER(input_frm), input_vb);
869
870     input_tb = gtk_table_new(2, 3, FALSE);
871     gtk_container_set_border_width(GTK_CONTAINER(input_tb), 5);
872     gtk_box_pack_start(GTK_BOX(input_vb), input_tb, FALSE, FALSE, 0);
873     gtk_table_set_row_spacings(GTK_TABLE(input_tb), 5);
874     gtk_table_set_col_spacings(GTK_TABLE(input_tb), 5);
875
876     /* Filename */
877     filename_hb = gtk_hbox_new(FALSE, 3);
878     gtk_container_set_border_width(GTK_CONTAINER(filename_hb), 3);
879
880     filename_lbl = gtk_label_new("Filename:");
881     gtk_table_attach(GTK_TABLE(input_tb), filename_lbl, 0, 1, 0, 1, 0, 0, 0, 0);
882
883     filename_te = gtk_entry_new();
884     gtk_widget_set_tooltip_text(filename_te, "Set name of text file to import");
885     gtk_table_attach_defaults(GTK_TABLE(input_tb), filename_te, 1, 2, 0, 1);
886
887     g_object_set_data(G_OBJECT(input_frm), INPUT_FILENAME_TE_KEY, filename_te);
888
889     browse_bt = gtk_button_new_from_stock(WIRESHARK_STOCK_BROWSE);
890     gtk_widget_set_tooltip_text(browse_bt, "Browse for text file to import");
891     gtk_table_attach(GTK_TABLE(input_tb), browse_bt, 2, 3, 0, 1, 0, 0, 0, 0);
892
893     g_signal_connect(browse_bt, "clicked", G_CALLBACK(browse_file_cb), filename_te);
894
895     /* Offsets */
896     offset_hb = gtk_hbox_new(FALSE, 3);
897     gtk_container_set_border_width(GTK_CONTAINER(offset_hb), 3);
898     gtk_box_pack_start(GTK_BOX(input_vb), offset_hb, FALSE, FALSE, 0);
899
900     offset_lbl_vb = gtk_vbox_new(FALSE, 0);
901     gtk_box_pack_start(GTK_BOX(offset_hb), offset_lbl_vb, FALSE, FALSE, 0);
902
903     offset_lbl = gtk_label_new("Offsets:");
904     gtk_misc_set_alignment(GTK_MISC(offset_lbl), 1.0f, 0.0f);
905     gtk_table_attach(GTK_TABLE(input_tb), offset_lbl, 0, 1, 1, 2, 0, 0, 0, 0);
906
907     offset_rb_vb = gtk_vbox_new(FALSE, 0);
908     gtk_table_attach_defaults(GTK_TABLE(input_tb), offset_rb_vb, 1, 2, 1, 2);
909
910     /* First entry in the group */
911     offset_hex_rb = gtk_radio_button_new_with_label(NULL, "Hexadecimal");
912     gtk_widget_set_tooltip_text(offset_hex_rb, "Offsets in the text file are in hexadecimal notation");
913     gtk_box_pack_start(GTK_BOX(offset_rb_vb), offset_hex_rb, FALSE, FALSE, 0);
914
915     g_object_set_data(G_OBJECT(input_frm), INPUT_OFFSET_HEX_RB_KEY, offset_hex_rb);
916
917     offset_oct_rb = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(offset_hex_rb), "Octal");
918     gtk_widget_set_tooltip_text(offset_oct_rb, "Offsets in the text file are in octal notation");
919     gtk_box_pack_start(GTK_BOX(offset_rb_vb), offset_oct_rb, FALSE, FALSE, 0);
920
921     g_object_set_data(G_OBJECT(input_frm), INPUT_OFFSET_OCT_RB_KEY, offset_oct_rb);
922
923     offset_dec_rb = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(offset_hex_rb), "Decimal");
924     gtk_widget_set_tooltip_text(offset_dec_rb, "Offsets in the text file are in decimal notation");
925     gtk_box_pack_start(GTK_BOX(offset_rb_vb), offset_dec_rb, FALSE, FALSE, 0);
926
927     g_object_set_data(G_OBJECT(input_frm), INPUT_OFFSET_DEC_RB_KEY, offset_dec_rb);
928
929     /* Time format */
930     timefmt_hb = gtk_hbox_new(FALSE, 3);
931     gtk_container_set_border_width(GTK_CONTAINER(timefmt_hb), 3);
932     gtk_box_pack_start(GTK_BOX(input_vb), timefmt_hb, FALSE, FALSE, 0);
933
934     timefmt_cb = gtk_check_button_new_with_label("Date/Time");
935     gtk_widget_set_tooltip_text(timefmt_cb, "Whether or not the text file contains timestamp information");
936     gtk_box_pack_start(GTK_BOX(timefmt_hb), timefmt_cb, FALSE, FALSE, 0);
937
938     g_object_set_data(G_OBJECT(input_frm), INPUT_DATETIME_CB_KEY, timefmt_cb);
939
940     timefmt_lbl = gtk_label_new("   Format:");
941     gtk_box_pack_start(GTK_BOX(timefmt_hb), timefmt_lbl, FALSE, FALSE, 0);
942
943     g_object_set_data(G_OBJECT(timefmt_cb), INPUT_TIMEFMT_LBL_KEY, timefmt_lbl);
944
945     timefmt_te = gtk_entry_new();
946     gtk_widget_set_tooltip_text(timefmt_te, "The format in which to parse timestamps in the text file (eg. %H:%M:%S.). Format specifiers are based on strptime(3)");
947     gtk_box_pack_start(GTK_BOX(timefmt_hb), timefmt_te, FALSE, FALSE, 0);
948
949     g_object_set_data(G_OBJECT(timefmt_cb), INPUT_TIMEFMT_TE_KEY, timefmt_te);
950     g_object_set_data(G_OBJECT(input_frm), INPUT_TIMEFMT_TE_KEY, timefmt_te);
951
952     g_signal_connect(timefmt_cb, "toggled", G_CALLBACK(timefmt_cb_toggle), NULL);
953     g_signal_emit_by_name(G_OBJECT(timefmt_cb), "toggled", NULL);
954
955     /* Setup the import frame */
956
957     import_frm = gtk_frame_new("Import");
958     gtk_box_pack_start(GTK_BOX(main_vb), import_frm, TRUE, TRUE, 3);
959
960     g_object_set_data(G_OBJECT(main_w), IMPORT_FRM_KEY, import_frm);
961
962     import_vb = gtk_vbox_new(FALSE, 0);
963     gtk_container_add(GTK_CONTAINER(import_frm), import_vb);
964
965     /* Encapsulation */
966     encap_hb = gtk_hbox_new(FALSE, 3);
967     gtk_container_set_border_width(GTK_CONTAINER(encap_hb), 3);
968     gtk_box_pack_start(GTK_BOX(import_vb), encap_hb, FALSE, FALSE, 0);
969
970     encap_lbl = gtk_label_new("Encapsulation type:");
971     gtk_box_pack_start(GTK_BOX(encap_hb), encap_lbl, FALSE, FALSE, 0);
972
973     encap_co = fill_encap_combo();
974     gtk_widget_set_tooltip_text(encap_co, "Encapsulation type for the frames in the import capture file");
975     gtk_box_pack_start(GTK_BOX(encap_hb), encap_co, FALSE, FALSE, 0);
976
977     g_object_set_data(G_OBJECT(import_frm), IMPORT_ENCAP_CO_KEY, encap_co);
978
979     /* Dummy header */
980     header_frm = gtk_frame_new(NULL);
981     header_cb = gtk_check_button_new_with_label("Dummy header");
982     gtk_widget_set_tooltip_text(header_cb, "Whether or not to prefix a dummy header to the frames");
983     gtk_frame_set_label_widget(GTK_FRAME(header_frm), header_cb);
984     gtk_container_set_border_width(GTK_CONTAINER(header_frm), 3);
985     gtk_box_pack_start(GTK_BOX(import_vb), header_frm, FALSE, FALSE, 0);
986
987     g_object_set_data(G_OBJECT(import_frm), IMPORT_HEADER_FRM_KEY, header_frm);
988     g_object_set_data(G_OBJECT(header_frm), IMPORT_HEADER_CB_KEY, header_cb);
989
990     header_hb = gtk_hbox_new(FALSE, 3);
991     gtk_container_set_border_width(GTK_CONTAINER(header_hb), 3);
992     gtk_container_add(GTK_CONTAINER(header_frm), header_hb);
993
994     header_rblbl_vb = gtk_vbox_new(FALSE, 0);
995     gtk_box_pack_start(GTK_BOX(header_hb), header_rblbl_vb, TRUE, TRUE, 0);
996
997     /* Line 1 */
998     header_rblbl_1_hb = gtk_hbox_new(FALSE, 0);
999     gtk_box_pack_start(GTK_BOX(header_rblbl_vb), header_rblbl_1_hb, FALSE, FALSE, 2);
1000
1001     /* First entry in the group */
1002     header_eth_rb = gtk_radio_button_new_with_label(NULL, "Ethernet");
1003     gtk_widget_set_tooltip_text(header_eth_rb, "Prefix an Ethernet header to the frames");
1004     g_signal_connect(header_eth_rb, "toggled", G_CALLBACK(header_eth_rb_toggle), header_frm);
1005     gtk_box_pack_start(GTK_BOX(header_rblbl_1_hb), header_eth_rb, FALSE, FALSE, 0);
1006
1007     g_object_set_data(G_OBJECT(header_frm), IMPORT_HEADER_ETH_RB_KEY, header_eth_rb);
1008
1009     header_rblbl_1_lbl = gtk_label_new("  Ethertype (hex):");
1010     gtk_box_pack_start(GTK_BOX(header_rblbl_1_hb), header_rblbl_1_lbl, TRUE, TRUE, 0);
1011     gtk_misc_set_alignment(GTK_MISC(header_rblbl_1_lbl), 1.0f, 0.5f);
1012
1013     etype_te = gtk_entry_new();
1014     gtk_widget_set_tooltip_text(etype_te, "The type to set in the Ethernet header");
1015     gtk_box_pack_end(GTK_BOX(header_rblbl_1_hb), etype_te, FALSE, FALSE, 0);
1016
1017     g_object_set_data(G_OBJECT(header_frm), IMPORT_HEADER_ETYPE_LBL_KEY, header_rblbl_1_lbl);
1018     g_object_set_data(G_OBJECT(header_frm), IMPORT_HEADER_ETYPE_TE_KEY, etype_te);
1019
1020     /* Line 2 */
1021     header_rblbl_2_hb = gtk_hbox_new(FALSE, 0);
1022     gtk_box_pack_start(GTK_BOX(header_rblbl_vb), header_rblbl_2_hb, FALSE, FALSE, 2);
1023
1024     header_ipv4_rb = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(header_eth_rb), "IPv4");
1025     gtk_widget_set_tooltip_text(header_ipv4_rb, "Prefix an Ethernet and IPv4 header to the frames");
1026     g_signal_connect(header_ipv4_rb, "toggled", G_CALLBACK(header_ipv4_rb_toggle), header_frm);
1027     gtk_box_pack_start(GTK_BOX(header_rblbl_2_hb), header_ipv4_rb, FALSE, FALSE, 0);
1028
1029     g_object_set_data(G_OBJECT(header_frm), IMPORT_HEADER_IPV4_RB_KEY, header_ipv4_rb);
1030
1031     header_rblbl_2_lbl = gtk_label_new("  Protocol (dec):");
1032     gtk_box_pack_start(GTK_BOX(header_rblbl_2_hb), header_rblbl_2_lbl, TRUE, TRUE, 0);
1033     gtk_misc_set_alignment(GTK_MISC(header_rblbl_2_lbl), 1.0f, 0.5f);
1034
1035     protocol_te = gtk_entry_new();
1036     gtk_widget_set_tooltip_text(protocol_te, "The protocol id to set in the IPv4 header");
1037     gtk_box_pack_end(GTK_BOX(header_rblbl_2_hb), protocol_te, FALSE, FALSE, 0);
1038
1039     g_object_set_data(G_OBJECT(header_frm), IMPORT_HEADER_PROT_LBL_KEY, header_rblbl_2_lbl);
1040     g_object_set_data(G_OBJECT(header_frm), IMPORT_HEADER_PROT_TE_KEY, protocol_te);
1041
1042     /* Line 3 */
1043     header_rblbl_3_hb = gtk_hbox_new(FALSE, 0);
1044     gtk_box_pack_start(GTK_BOX(header_rblbl_vb), header_rblbl_3_hb, FALSE, FALSE, 2);
1045
1046     header_udp_rb = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(header_eth_rb), "UDP");
1047     gtk_widget_set_tooltip_text(header_udp_rb, "Prefix an Ethernet, IPv4 and UDP header to the frames");
1048     g_signal_connect(header_udp_rb, "toggled", G_CALLBACK(header_udp_rb_toggle), header_frm);
1049     gtk_box_pack_start(GTK_BOX(header_rblbl_3_hb), header_udp_rb, FALSE, FALSE, 0);
1050
1051     g_object_set_data(G_OBJECT(header_frm), IMPORT_HEADER_UDP_RB_KEY, header_udp_rb);
1052
1053     header_rblbl_3_lbl = gtk_label_new("  Source port:");
1054     gtk_box_pack_start(GTK_BOX(header_rblbl_3_hb), header_rblbl_3_lbl, TRUE, TRUE, 0);
1055     gtk_misc_set_alignment(GTK_MISC(header_rblbl_3_lbl), 1.0f, 0.5f);
1056
1057     src_port_te = gtk_entry_new();
1058     gtk_widget_set_tooltip_text(src_port_te, "The source port to set in the UDP, TCP or SCTP header");
1059     gtk_box_pack_end(GTK_BOX(header_rblbl_3_hb), src_port_te, FALSE, FALSE, 0);
1060
1061     g_object_set_data(G_OBJECT(header_frm), IMPORT_HEADER_SRC_PORT_LBL_KEY, header_rblbl_3_lbl);
1062     g_object_set_data(G_OBJECT(header_frm), IMPORT_HEADER_SRC_PORT_TE_KEY, src_port_te);
1063
1064     /* Line 4 */
1065     header_rblbl_4_hb = gtk_hbox_new(FALSE, 0);
1066     gtk_box_pack_start(GTK_BOX(header_rblbl_vb), header_rblbl_4_hb, FALSE, FALSE, 2);
1067
1068     header_tcp_rb = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(header_eth_rb), "TCP");
1069     gtk_widget_set_tooltip_text(header_tcp_rb, "Prefix an Ethernet, IPv4 and TCP header to the frames");
1070     g_signal_connect(header_tcp_rb, "toggled", G_CALLBACK(header_tcp_rb_toggle), header_frm);
1071     gtk_box_pack_start(GTK_BOX(header_rblbl_4_hb), header_tcp_rb, FALSE, FALSE, 0);
1072
1073     g_object_set_data(G_OBJECT(header_frm), IMPORT_HEADER_TCP_RB_KEY, header_tcp_rb);
1074
1075     header_rblbl_4_lbl = gtk_label_new("  Destination port:");
1076     gtk_box_pack_start(GTK_BOX(header_rblbl_4_hb), header_rblbl_4_lbl, TRUE, TRUE, 0);
1077     gtk_misc_set_alignment(GTK_MISC(header_rblbl_4_lbl), 1.0f, 0.5f);
1078
1079     dst_port_te = gtk_entry_new();
1080     gtk_widget_set_tooltip_text(dst_port_te, "The destination port to set in the UDP, TCP or SCTP header");
1081     gtk_box_pack_end(GTK_BOX(header_rblbl_4_hb), dst_port_te, FALSE, FALSE, 0);
1082
1083     g_object_set_data(G_OBJECT(header_frm), IMPORT_HEADER_DST_PORT_LBL_KEY, header_rblbl_4_lbl);
1084     g_object_set_data(G_OBJECT(header_frm), IMPORT_HEADER_DST_PORT_TE_KEY, dst_port_te);
1085
1086     /* Line 5 */
1087     header_rblbl_5_hb = gtk_hbox_new(FALSE, 0);
1088     gtk_box_pack_start(GTK_BOX(header_rblbl_vb), header_rblbl_5_hb, FALSE, FALSE, 2);
1089
1090     header_sctp_rb = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(header_eth_rb), "SCTP");
1091     gtk_widget_set_tooltip_text(header_sctp_rb, "Prefix an Ethernet, IPv4 and SCTP header to the frames");
1092     g_signal_connect(header_sctp_rb, "toggled", G_CALLBACK(header_sctp_rb_toggle), header_frm);
1093     gtk_box_pack_start(GTK_BOX(header_rblbl_5_hb), header_sctp_rb, FALSE, FALSE, 0);
1094
1095     g_object_set_data(G_OBJECT(header_frm), IMPORT_HEADER_SCTP_RB_KEY, header_sctp_rb);
1096
1097     header_rblbl_5_lbl = gtk_label_new("  Tag:");
1098     gtk_box_pack_start(GTK_BOX(header_rblbl_5_hb), header_rblbl_5_lbl, TRUE, TRUE, 0);
1099     gtk_misc_set_alignment(GTK_MISC(header_rblbl_5_lbl), 1.0f, 0.5f);
1100
1101     tag_te = gtk_entry_new();
1102     gtk_widget_set_tooltip_text(tag_te, "The verification tag to set in the SCTP header");
1103     gtk_box_pack_end(GTK_BOX(header_rblbl_5_hb), tag_te, FALSE, FALSE, 0);
1104
1105     g_object_set_data(G_OBJECT(header_frm), IMPORT_HEADER_TAG_LBL_KEY, header_rblbl_5_lbl);
1106     g_object_set_data(G_OBJECT(header_frm), IMPORT_HEADER_TAG_TE_KEY, tag_te);
1107
1108     /* Line 6 */
1109     header_rblbl_6_hb = gtk_hbox_new(FALSE, 0);
1110     gtk_box_pack_start(GTK_BOX(header_rblbl_vb), header_rblbl_6_hb, FALSE, FALSE, 2);
1111
1112     header_sctp_data_rb = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(header_eth_rb), "SCTP (DATA)");
1113     gtk_widget_set_tooltip_text(header_sctp_data_rb, "Prefix an Ethernet, IPv4 and SCTP DATA header to the frames");
1114     g_signal_connect(header_sctp_data_rb, "toggled", G_CALLBACK(header_sctp_data_rb_toggle), header_frm);
1115     gtk_box_pack_start(GTK_BOX(header_rblbl_6_hb), header_sctp_data_rb, FALSE, FALSE, 0);
1116
1117     g_object_set_data(G_OBJECT(header_frm), IMPORT_HEADER_SCTP_D_RB_KEY, header_sctp_data_rb);
1118
1119     header_rblbl_6_lbl = gtk_label_new("  PPI:");
1120     gtk_box_pack_start(GTK_BOX(header_rblbl_6_hb), header_rblbl_6_lbl, TRUE, TRUE, 0);
1121     gtk_misc_set_alignment(GTK_MISC(header_rblbl_6_lbl), 1.0f, 0.5f);
1122
1123     ppi_te = gtk_entry_new();
1124     gtk_widget_set_tooltip_text(ppi_te, "The payload protocol identifier to set in the SCTP DATA header");
1125     gtk_box_pack_end(GTK_BOX(header_rblbl_6_hb), ppi_te, FALSE, FALSE, 0);
1126
1127     g_object_set_data(G_OBJECT(header_frm), IMPORT_HEADER_PPI_LBL_KEY, header_rblbl_6_lbl);
1128     g_object_set_data(G_OBJECT(header_frm), IMPORT_HEADER_PPI_TE_KEY, ppi_te);
1129
1130     /* Set sensitivity */
1131     g_signal_connect(header_cb, "toggled", G_CALLBACK(header_cb_toggle), header_frm);
1132     g_signal_emit_by_name(G_OBJECT(header_cb), "toggled", header_frm);
1133
1134     g_signal_emit_by_name(G_OBJECT(header_eth_rb), "toggled", header_frm);
1135
1136     gtk_combo_box_set_active(GTK_COMBO_BOX(encap_co), 0);
1137     g_signal_connect(encap_co, "changed", G_CALLBACK(encap_co_changed), header_frm);
1138
1139     /* Frame length */
1140     framelen_hb = gtk_hbox_new(FALSE, 3);
1141     gtk_container_set_border_width(GTK_CONTAINER(framelen_hb), 3);
1142     gtk_box_pack_start(GTK_BOX(import_vb), framelen_hb, FALSE, FALSE, 0);
1143
1144     framelen_lbl = gtk_label_new("Max. frame length:");
1145     gtk_box_pack_start(GTK_BOX(framelen_hb), framelen_lbl, FALSE, FALSE, 0);
1146
1147     framelen_te = gtk_entry_new();
1148     gtk_widget_set_tooltip_text(framelen_te, "The maximum size of the frames to write to the import capture file (max 64000)");
1149     gtk_box_pack_start(GTK_BOX(framelen_hb), framelen_te, FALSE, FALSE, 0);
1150
1151     g_object_set_data(G_OBJECT(import_frm), IMPORT_FRAME_LENGTH_TE_KEY, framelen_te);
1152
1153     /* Setup the button row */
1154
1155     bbox = dlg_button_row_new(GTK_STOCK_HELP, GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL);
1156     gtk_box_pack_end(GTK_BOX(main_vb), bbox, FALSE, FALSE, 3);
1157
1158     help_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_HELP);
1159     g_signal_connect(help_bt, "clicked", G_CALLBACK(topic_cb), (gpointer)HELP_IMPORT_DIALOG);
1160     gtk_widget_set_tooltip_text(help_bt, "Show topic specific help");
1161
1162     close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CANCEL);
1163     window_set_cancel_button(main_w, close_bt, window_cancel_button_cb);
1164     gtk_widget_set_tooltip_text(close_bt, "Close this dialog");
1165
1166     ok_bt =  g_object_get_data(G_OBJECT(bbox), GTK_STOCK_OK);
1167     g_signal_connect(ok_bt, "clicked", G_CALLBACK(file_import_ok_cb), main_w);
1168     gtk_widget_grab_default(ok_bt);
1169     gtk_widget_set_tooltip_text(ok_bt, "Import the selected file into a temporary capture file");
1170
1171     /* Setup widget handling */
1172
1173     g_signal_connect(main_w, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
1174     g_signal_connect(main_w, "destroy", G_CALLBACK(file_import_dlg_destroy_cb), NULL);
1175
1176     gtk_widget_show_all(main_w);
1177     window_present(main_w);
1178
1179     return main_w;
1180 }
1181
1182 void
1183 file_import_cmd_cb(GtkWidget *widget _U_)
1184 {
1185     /* Do we have an encapsulation type list? */
1186     if (!encap_list_store)
1187     {
1188         /* No. Create one. */
1189         create_encap_list_store();
1190     }
1191
1192     /* Has a file import dialog already been opened? */
1193     if (file_import_dlg_w)
1194     {
1195         /* Yes. Just re-activate that dialog box. */
1196         reactivate_window(file_import_dlg_w);
1197     } else {
1198         /* No. Create one */
1199         file_import_dlg_w = file_import_dlg_new();
1200     }
1201
1202     return;
1203 }