e0b2a9fc3c36050e91425133c5f45a30ccc4f3bf
[metze/wireshark/wip.git] / ui / tap_export_pdu.c
1 /* tap_export_pdu.c
2  * Routines for exporting PDUs to file
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include "config.h"
24
25
26 #include "globals.h"
27 #include "wiretap/pcap-encap.h"
28 #include "wsutil/tempfile.h"
29 #include "wsutil/os_version_info.h"
30 #include "wsutil/ws_version_info.h"
31
32 #include <epan/tap.h>
33 #include <epan/exported_pdu.h>
34 #include <epan/epan_dissect.h>
35 #include <wiretap/wtap_opttypes.h>
36 #include <wiretap/pcapng.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 static gboolean
44 export_pdu_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt, 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     gchar *err_info;
51     int buffer_len;
52     guint8 *packet_buf;
53
54     memset(&pkthdr, 0, sizeof(struct wtap_pkthdr));
55     buffer_len = exp_pdu_data->tvb_captured_length + exp_pdu_data->tlv_buffer_len;
56     packet_buf = (guint8 *)g_malloc(buffer_len);
57
58     if(exp_pdu_data->tlv_buffer_len > 0){
59         memcpy(packet_buf, exp_pdu_data->tlv_buffer, exp_pdu_data->tlv_buffer_len);
60         g_free(exp_pdu_data->tlv_buffer);
61     }
62     if(exp_pdu_data->tvb_captured_length > 0){
63         tvb_memcpy(exp_pdu_data->pdu_tvb, packet_buf+exp_pdu_data->tlv_buffer_len, 0, exp_pdu_data->tvb_captured_length);
64     }
65     pkthdr.rec_type  = REC_TYPE_PACKET;
66     pkthdr.ts.secs   = pinfo->abs_ts.secs;
67     pkthdr.ts.nsecs  = pinfo->abs_ts.nsecs;
68     pkthdr.caplen    = buffer_len;
69     pkthdr.len       = exp_pdu_data->tvb_reported_length + exp_pdu_data->tlv_buffer_len;
70
71     pkthdr.pkt_encap = exp_pdu_tap_data->pkt_encap;
72
73     if (pinfo->fd->flags.has_user_comment)
74         pkthdr.opt_comment = g_strdup(epan_get_user_comment(edt->session, pinfo->fd));
75     else if (pinfo->fd->flags.has_phdr_comment)
76         pkthdr.opt_comment = g_strdup(pinfo->phdr->opt_comment);
77
78     pkthdr.presence_flags = WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID|WTAP_HAS_TS|WTAP_HAS_PACK_FLAGS;
79
80     /* XXX: should the pkthdr.pseudo_header be set to the pinfo's pseudo-header? */
81     /* XXX: report errors! */
82     if (!wtap_dump(exp_pdu_tap_data->wdh, &pkthdr, packet_buf, &err, &err_info)) {
83         switch (err) {
84
85         case WTAP_ERR_UNWRITABLE_REC_DATA:
86             g_free(err_info);
87             break;
88
89         default:
90             break;
91         }
92     }
93
94     g_free(packet_buf);
95     g_free(pkthdr.opt_comment);
96
97     return FALSE; /* Do not redraw */
98 }
99
100 static void
101 exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data)
102 {
103     char *tmpname, *capfile_name;
104     int   err;
105
106     /* pcapng defs */
107     wtap_optionblock_t           shb_hdr;
108     wtapng_iface_descriptions_t *idb_inf;
109     wtap_optionblock_t           int_data;
110     wtapng_if_descr_mandatory_t *int_data_mand;
111     GString                     *os_info_str;
112     gchar                       *opt_comment, *wireshark_ver;
113
114     /* Create data for SHB  */
115     os_info_str = g_string_new("");
116     get_os_version_info(os_info_str);
117
118     shb_hdr = wtap_optionblock_create(WTAP_OPTION_BLOCK_NG_SECTION);
119
120     /* options */
121     opt_comment = g_strdup_printf("Dump of PDUs from %s", cfile.filename);
122     wtap_optionblock_set_option_string(shb_hdr, OPT_COMMENT, opt_comment);
123     g_free(opt_comment);
124
125     /*
126      * UTF-8 string containing the name of the operating system used to create
127      * this section.
128      */
129     wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_OS, g_string_free(os_info_str, TRUE));
130     /*
131      * UTF-8 string containing the name of the application used to create
132      * this section.
133      */
134     wireshark_ver = g_strdup_printf("Wireshark %s", get_ws_vcs_version_info());
135     wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_USERAPPL, wireshark_ver);
136     g_free(wireshark_ver);
137
138     /* Create fake IDB info */
139     idb_inf = g_new(wtapng_iface_descriptions_t,1);
140     idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtap_optionblock_t));
141
142     /* create the fake interface data */
143     int_data = wtap_optionblock_create(WTAP_OPTION_BLOCK_IF_DESCR);
144     int_data_mand = (wtapng_if_descr_mandatory_t*)wtap_optionblock_get_mandatory_data(int_data);
145     int_data_mand->wtap_encap      = WTAP_ENCAP_WIRESHARK_UPPER_PDU;
146     int_data_mand->time_units_per_second = 1000000000; /* default nanosecond resolution */
147     int_data_mand->link_type       = wtap_wtap_encap_to_pcap_encap(WTAP_ENCAP_WIRESHARK_UPPER_PDU);
148     int_data_mand->snap_len        = WTAP_MAX_PACKET_SIZE;
149
150     wtap_optionblock_set_option_string(int_data, OPT_IDB_NAME, "Fake IF, PDU->Export");
151     wtap_optionblock_set_option_uint8(int_data, OPT_IDB_TSRESOL, 9);
152
153     g_array_append_val(idb_inf->interface_data, int_data);
154
155     /* Use a random name for the temporary import buffer */
156     exp_pdu_tap_data->wdh = wtap_dump_open_tempfile_ng(&tmpname, "Wireshark_PDU_",
157                                                        WTAP_FILE_TYPE_SUBTYPE_PCAPNG,
158                                                        WTAP_ENCAP_WIRESHARK_UPPER_PDU, WTAP_MAX_PACKET_SIZE,
159                                                        FALSE, shb_hdr, idb_inf, NULL, &err);
160     capfile_name = g_strdup(tmpname);
161     if (exp_pdu_tap_data->wdh == NULL) {
162         open_failure_alert_box(capfile_name ? capfile_name : "temporary file", err, TRUE);
163         goto end;
164     }
165
166     /* Run the tap */
167     cf_retap_packets(&cfile);
168
169
170     if (!wtap_dump_close(exp_pdu_tap_data->wdh, &err)) {
171         write_failure_alert_box(capfile_name, err);
172     }
173
174     remove_tap_listener(exp_pdu_tap_data);
175
176     /* XXX: should this use the open_routine type in the cfile instead of WTAP_TYPE_AUTO? */
177     if (cf_open(&cfile, capfile_name, WTAP_TYPE_AUTO, TRUE /* temporary file */, &err) != CF_OK) {
178         open_failure_alert_box(capfile_name, err, FALSE);
179         goto end;
180     }
181
182     switch (cf_read(&cfile, FALSE)) {
183     case CF_READ_OK:
184     case CF_READ_ERROR:
185     /* Just because we got an error, that doesn't mean we were unable
186        to read any of the file; we handle what we could get from the
187        file. */
188     break;
189
190     case CF_READ_ABORTED:
191     /* The user bailed out of re-reading the capture file; the
192        capture file has been closed - just free the capture file name
193        string and return (without changing the last containing
194        directory). */
195     break;
196     }
197
198 end:
199     g_free(capfile_name);
200     wtap_optionblock_free(shb_hdr);
201     wtap_free_idb_info(idb_inf);
202 }
203
204 gboolean
205 do_export_pdu(const char *filter, gchar *tap_name, exp_pdu_t *exp_pdu_tap_data)
206 {
207     GString        *error_string;
208
209     /* Register this tap listener now */
210     error_string = register_tap_listener(tap_name,             /* The name of the tap we want to listen to */
211                                          exp_pdu_tap_data,     /* instance identifier/pointer to a struct holding
212                                                                 * all state variables */
213                                          filter,               /* pointer to a filter string */
214                                          TL_REQUIRES_NOTHING,  /* flags for the tap listener */
215                                          NULL,
216                                          export_pdu_packet,
217                                          NULL);
218     if (error_string){
219         /* Error.  We failed to attach to the tap. Clean up */
220         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
221         g_string_free(error_string, TRUE);
222         return FALSE;
223     }
224
225     exp_pdu_file_open(exp_pdu_tap_data);
226     return TRUE;
227 }
228
229 /*
230  * Editor modelines
231  *
232  * Local Variables:
233  * c-basic-offset: 4
234  * tab-width: 8
235  * indent-tabs-mode: nil
236  * End:
237  *
238  * ex: set shiftwidth=4 tabstop=8 expandtab:
239  * :indentSize=4:tabSize=8:noTabs=true:
240  */