Split the GUI and the tap part to make it possible to adapt to Qt.
[metze/wireshark/wip.git] / ui / tap_export_pdu.c
1 /* tap_export_pdu.c
2  * Routines for exporting PDU:s to file
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include "config.h"
26
27 #include <glib.h>
28
29 #include "globals.h"
30 #include "wtap.h"
31 #include "pcap-encap.h"
32 #include "version_info.h"
33 #include "wsutil/tempfile.h"
34
35 #include <epan/tap.h>
36 #include <epan/exported_pdu.h>
37
38 #include "ui/alert_box.h"
39 #include "ui/simple_dialog.h"
40 #include "tap_export_pdu.h"
41
42 /* Main entry point to the tap */
43 int
44 export_pdu_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_, const void *data)
45 {
46     const exp_pdu_data_t *exp_pdu_data = (const exp_pdu_data_t *)data;
47     exp_pdu_t  *exp_pdu_tap_data = (exp_pdu_t *)tapdata;
48     struct wtap_pkthdr pkthdr;
49     int err;
50     int buffer_len;
51     guint8 *packet_buf;
52
53     buffer_len = exp_pdu_data->tvb_length + exp_pdu_data->tlv_buffer_len;
54     packet_buf = (guint8 *)g_malloc(buffer_len);
55
56     if(exp_pdu_data->tlv_buffer_len > 0){
57         memcpy(packet_buf, exp_pdu_data->tlv_buffer, exp_pdu_data->tlv_buffer_len);
58         g_free(exp_pdu_data->tlv_buffer);
59     }
60     if(exp_pdu_data->tvb_length > 0){
61         tvb_memcpy(exp_pdu_data->pdu_tvb, packet_buf+exp_pdu_data->tlv_buffer_len, 0, exp_pdu_data->tvb_length);
62     }
63     pkthdr.ts.secs   = pinfo->fd->abs_ts.secs;
64     pkthdr.ts.nsecs  = pinfo->fd->abs_ts.nsecs;
65     pkthdr.caplen    = pkthdr.len = buffer_len;
66
67     pkthdr.pkt_encap = exp_pdu_tap_data->pkt_encap;
68     pkthdr.interface_id = 0;
69     pkthdr.presence_flags = 0;
70     pkthdr.opt_comment = g_strdup(pinfo->pkt_comment);
71     pkthdr.drop_count = 0;
72     pkthdr.pack_flags = 0;
73     pkthdr.presence_flags = WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID|WTAP_HAS_TS|WTAP_HAS_PACK_FLAGS;
74
75     wtap_dump(exp_pdu_tap_data->wdh, &pkthdr, packet_buf, &err);
76
77     g_free(packet_buf);
78     g_free(pkthdr.opt_comment);
79
80     return FALSE; /* Do not redraw */
81 }
82
83 void
84 exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data)
85 {
86     int   import_file_fd;
87     char *tmpname, *capfile_name;
88     int   err;
89
90     /* pcapng defs */
91     wtapng_section_t            *shb_hdr;
92     wtapng_iface_descriptions_t *idb_inf;
93     wtapng_if_descr_t            int_data;
94     GString                     *os_info_str;
95     char                         appname[100];
96
97     /* Choose a random name for the temporary import buffer */
98     import_file_fd = create_tempfile(&tmpname, "Wireshark_PDU_");
99     capfile_name = g_strdup(tmpname);
100
101     /* Create data for SHB  */
102     os_info_str = g_string_new("");
103     get_os_version_info(os_info_str);
104
105     g_snprintf(appname, sizeof(appname), "Wireshark " VERSION "%s", wireshark_svnversion);
106
107     shb_hdr = g_new(wtapng_section_t,1);
108     shb_hdr->section_length = -1;
109     /* options */
110     shb_hdr->opt_comment    = g_strdup_printf("Dump of PDU:s from %s", cfile.filename);
111     shb_hdr->shb_hardware   = NULL;                    /* UTF-8 string containing the
112                                                        * description of the hardware used to create this section.
113                                                        */
114     shb_hdr->shb_os         = os_info_str->str;        /* UTF-8 string containing the name
115                                                        * of the operating system used to create this section.
116                                                        */
117     g_string_free(os_info_str, FALSE);                /* The actual string is not freed */
118     shb_hdr->shb_user_appl  = appname;                /* UTF-8 string containing the name
119                                                        *  of the application used to create this section.
120                                                        */
121
122
123     /* Create fake IDB info */
124     idb_inf = g_new(wtapng_iface_descriptions_t,1);
125     idb_inf->number_of_interfaces = 1;
126     idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t));
127
128     /* create the fake interface data */
129     int_data.wtap_encap            = WTAP_ENCAP_WIRESHARK_UPPER_PDU;
130     int_data.time_units_per_second = 1000000; /* default microsecond resolution */
131     int_data.link_type             = wtap_wtap_encap_to_pcap_encap(WTAP_ENCAP_WIRESHARK_UPPER_PDU);
132     int_data.snap_len              = WTAP_MAX_PACKET_SIZE;
133     int_data.if_name               = g_strdup("Fake IF, PDU->Export");
134     int_data.opt_comment           = NULL;
135     int_data.if_description        = NULL;
136     int_data.if_speed              = 0;
137     int_data.if_tsresol            = 6;
138     int_data.if_filter_str         = NULL;
139     int_data.bpf_filter_len        = 0;
140     int_data.if_filter_bpf_bytes   = NULL;
141     int_data.if_os                 = NULL;
142     int_data.if_fcslen             = -1;
143     int_data.num_stat_entries      = 0;          /* Number of ISB:s */
144     int_data.interface_statistics  = NULL;
145
146     g_array_append_val(idb_inf->interface_data, int_data);
147
148     exp_pdu_tap_data->wdh = wtap_dump_fdopen_ng(import_file_fd, WTAP_FILE_TYPE_SUBTYPE_PCAPNG, WTAP_ENCAP_WIRESHARK_UPPER_PDU, WTAP_MAX_PACKET_SIZE, FALSE, shb_hdr, idb_inf, &err);
149     if (exp_pdu_tap_data->wdh == NULL) {
150         open_failure_alert_box(capfile_name, err, TRUE);
151         goto end;
152     }
153
154
155     /* Run the tap */
156     cf_retap_packets(&cfile);
157
158
159     if (!wtap_dump_close(exp_pdu_tap_data->wdh, &err)) {
160         write_failure_alert_box(capfile_name, err);
161     }
162
163     remove_tap_listener(exp_pdu_tap_data);
164
165     if (cf_open(&cfile, capfile_name, TRUE /* temporary file */, &err) != CF_OK) {
166         open_failure_alert_box(capfile_name, err, FALSE);
167         goto end;
168     }
169
170     switch (cf_read(&cfile, FALSE)) {
171     case CF_READ_OK:
172     case CF_READ_ERROR:
173     /* Just because we got an error, that doesn't mean we were unable
174        to read any of the file; we handle what we could get from the
175        file. */
176     break;
177
178     case CF_READ_ABORTED:
179     /* The user bailed out of re-reading the capture file; the
180        capture file has been closed - just free the capture file name
181        string and return (without changing the last containing
182        directory). */
183     break;
184     }
185
186 end:
187     g_free(capfile_name);
188 }
189
190 gboolean
191 do_export_pdu(const char *filter, gchar *tap_name, gpointer data)
192 {
193     GString        *error_string;
194     exp_pdu_t  *exp_pdu_tap_data = (exp_pdu_t *)data;
195
196     /* Register this tap listener now */
197     error_string = register_tap_listener(tap_name,             /* The name of the tap we want to listen to */
198                                          exp_pdu_tap_data,     /* instance identifier/pointer to a struct holding
199                                                                 * all state variables */
200                                          filter,               /* pointer to a filter string */
201                                          TL_REQUIRES_NOTHING,  /* flags for the tap listener */
202                                          NULL,
203                                          export_pdu_packet,
204                                          NULL);
205     if (error_string){
206         /* Error.  We failed to attach to the tap. Clean up */
207         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
208         g_string_free(error_string, TRUE);
209         return FALSE;
210     }
211
212     exp_pdu_file_open(exp_pdu_tap_data);
213     return TRUE;
214 }
215