2 * Routines for exporting PDUs to file
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
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.
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.
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.
27 #include "pcap-encap.h"
28 #include "wsutil/tempfile.h"
29 #include "wsutil/os_version_info.h"
30 #include "wsutil/ws_version_info.h"
33 #include <epan/exported_pdu.h>
34 #include <epan/epan_dissect.h>
36 #include "ui/alert_box.h"
37 #include "ui/simple_dialog.h"
38 #include "tap_export_pdu.h"
40 /* Main entry point to the tap */
42 export_pdu_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt, const void *data)
44 const exp_pdu_data_t *exp_pdu_data = (const exp_pdu_data_t *)data;
45 exp_pdu_t *exp_pdu_tap_data = (exp_pdu_t *)tapdata;
46 struct wtap_pkthdr pkthdr;
52 memset(&pkthdr, 0, sizeof(struct wtap_pkthdr));
53 buffer_len = exp_pdu_data->tvb_captured_length + exp_pdu_data->tlv_buffer_len;
54 packet_buf = (guint8 *)g_malloc(buffer_len);
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);
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);
63 pkthdr.rec_type = REC_TYPE_PACKET;
64 pkthdr.ts.secs = pinfo->fd->abs_ts.secs;
65 pkthdr.ts.nsecs = pinfo->fd->abs_ts.nsecs;
66 pkthdr.caplen = buffer_len;
67 pkthdr.len = exp_pdu_data->tvb_reported_length + exp_pdu_data->tlv_buffer_len;
69 pkthdr.pkt_encap = exp_pdu_tap_data->pkt_encap;
71 if (pinfo->fd->flags.has_user_comment)
72 pkthdr.opt_comment = g_strdup(epan_get_user_comment(edt->session, pinfo->fd));
73 else if (pinfo->fd->flags.has_phdr_comment)
74 pkthdr.opt_comment = g_strdup(pinfo->phdr->opt_comment);
76 pkthdr.presence_flags = WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID|WTAP_HAS_TS|WTAP_HAS_PACK_FLAGS;
78 /* XXX: should the pkthdr.pseudo_header be set to the pinfo's pseudo-header? */
79 /* XXX: report errors! */
80 if (!wtap_dump(exp_pdu_tap_data->wdh, &pkthdr, packet_buf, &err, &err_info)) {
83 case WTAP_ERR_UNWRITABLE_REC_DATA:
93 g_free(pkthdr.opt_comment);
95 return FALSE; /* Do not redraw */
99 exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data)
102 char *tmpname, *capfile_name;
106 wtapng_section_t *shb_hdr;
107 wtapng_iface_descriptions_t *idb_inf;
108 wtapng_if_descr_t int_data;
109 GString *os_info_str;
112 /* Choose a random name for the temporary import buffer */
113 import_file_fd = create_tempfile(&tmpname, "Wireshark_PDU_");
114 capfile_name = g_strdup(tmpname);
116 /* Create data for SHB */
117 os_info_str = g_string_new("");
118 get_os_version_info(os_info_str);
120 g_snprintf(appname, sizeof(appname), "Wireshark %s", get_ws_vcs_version_info());
122 shb_hdr = g_new(wtapng_section_t,1);
123 shb_hdr->section_length = -1;
125 shb_hdr->opt_comment = g_strdup_printf("Dump of PDUs from %s", cfile.filename);
126 shb_hdr->shb_hardware = NULL; /* UTF-8 string containing the
127 * description of the hardware used to create this section.
129 shb_hdr->shb_os = os_info_str->str; /* UTF-8 string containing the name
130 * of the operating system used to create this section.
132 g_string_free(os_info_str, FALSE); /* The actual string is not freed */
133 shb_hdr->shb_user_appl = appname; /* UTF-8 string containing the name
134 * of the application used to create this section.
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(wtapng_if_descr_t));
142 /* create the fake interface data */
143 int_data.wtap_encap = WTAP_ENCAP_WIRESHARK_UPPER_PDU;
144 int_data.time_units_per_second = 1000000; /* default microsecond resolution */
145 int_data.link_type = wtap_wtap_encap_to_pcap_encap(WTAP_ENCAP_WIRESHARK_UPPER_PDU);
146 int_data.snap_len = WTAP_MAX_PACKET_SIZE;
147 int_data.if_name = g_strdup("Fake IF, PDU->Export");
148 int_data.opt_comment = NULL;
149 int_data.if_description = NULL;
150 int_data.if_speed = 0;
151 int_data.if_tsresol = 6;
152 int_data.if_filter_str = NULL;
153 int_data.bpf_filter_len = 0;
154 int_data.if_filter_bpf_bytes = NULL;
155 int_data.if_os = NULL;
156 int_data.if_fcslen = -1;
157 int_data.num_stat_entries = 0; /* Number of ISB:s */
158 int_data.interface_statistics = NULL;
160 g_array_append_val(idb_inf->interface_data, int_data);
162 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);
163 if (exp_pdu_tap_data->wdh == NULL) {
164 open_failure_alert_box(capfile_name, err, TRUE);
170 cf_retap_packets(&cfile);
173 if (!wtap_dump_close(exp_pdu_tap_data->wdh, &err)) {
174 write_failure_alert_box(capfile_name, err);
177 remove_tap_listener(exp_pdu_tap_data);
179 /* XXX: should this use the open_routine type in the cfile instead of WTAP_TYPE_AUTO? */
180 if (cf_open(&cfile, capfile_name, WTAP_TYPE_AUTO, TRUE /* temporary file */, &err) != CF_OK) {
181 open_failure_alert_box(capfile_name, err, FALSE);
185 switch (cf_read(&cfile, FALSE)) {
188 /* Just because we got an error, that doesn't mean we were unable
189 to read any of the file; we handle what we could get from the
193 case CF_READ_ABORTED:
194 /* The user bailed out of re-reading the capture file; the
195 capture file has been closed - just free the capture file name
196 string and return (without changing the last containing
202 g_free(capfile_name);
206 do_export_pdu(const char *filter, gchar *tap_name, exp_pdu_t *exp_pdu_tap_data)
208 GString *error_string;
210 /* Register this tap listener now */
211 error_string = register_tap_listener(tap_name, /* The name of the tap we want to listen to */
212 exp_pdu_tap_data, /* instance identifier/pointer to a struct holding
213 * all state variables */
214 filter, /* pointer to a filter string */
215 TL_REQUIRES_NOTHING, /* flags for the tap listener */
220 /* Error. We failed to attach to the tap. Clean up */
221 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
222 g_string_free(error_string, TRUE);
226 exp_pdu_file_open(exp_pdu_tap_data);
236 * indent-tabs-mode: nil
239 * ex: set shiftwidth=4 tabstop=8 expandtab:
240 * :indentSize=4:tabSize=8:noTabs=true: