2 * Routines for exporting PDU:s 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.
29 #include "pcap-encap.h"
30 #include "version_info.h"
31 #include "wsutil/tempfile.h"
34 #include <epan/exported_pdu.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 _U_, 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;
51 buffer_len = exp_pdu_data->tvb_captured_length + exp_pdu_data->tlv_buffer_len;
52 packet_buf = (guint8 *)g_malloc(buffer_len);
54 if(exp_pdu_data->tlv_buffer_len > 0){
55 memcpy(packet_buf, exp_pdu_data->tlv_buffer, exp_pdu_data->tlv_buffer_len);
56 g_free(exp_pdu_data->tlv_buffer);
58 if(exp_pdu_data->tvb_length > 0){
59 tvb_memcpy(exp_pdu_data->pdu_tvb, packet_buf+exp_pdu_data->tlv_buffer_len, 0, exp_pdu_data->tvb_length);
61 pkthdr.ts.secs = pinfo->fd->abs_ts.secs;
62 pkthdr.ts.nsecs = pinfo->fd->abs_ts.nsecs;
63 pkthdr.caplen = buffer_len;
64 pkthdr.len = exp_pdu_data->tvb_reported_length + exp_pdu_data->tlv_buffer_len;
66 pkthdr.pkt_encap = exp_pdu_tap_data->pkt_encap;
67 pkthdr.interface_id = 0;
68 pkthdr.presence_flags = 0;
69 pkthdr.opt_comment = g_strdup(pinfo->pkt_comment);
70 pkthdr.drop_count = 0;
71 pkthdr.pack_flags = 0;
72 pkthdr.presence_flags = WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID|WTAP_HAS_TS|WTAP_HAS_PACK_FLAGS;
74 wtap_dump(exp_pdu_tap_data->wdh, &pkthdr, packet_buf, &err);
77 g_free(pkthdr.opt_comment);
79 return FALSE; /* Do not redraw */
83 exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data)
86 char *tmpname, *capfile_name;
90 wtapng_section_t *shb_hdr;
91 wtapng_iface_descriptions_t *idb_inf;
92 wtapng_if_descr_t int_data;
96 /* Choose a random name for the temporary import buffer */
97 import_file_fd = create_tempfile(&tmpname, "Wireshark_PDU_");
98 capfile_name = g_strdup(tmpname);
100 /* Create data for SHB */
101 os_info_str = g_string_new("");
102 get_os_version_info(os_info_str);
104 g_snprintf(appname, sizeof(appname), "Wireshark " VERSION "%s", wireshark_gitversion);
106 shb_hdr = g_new(wtapng_section_t,1);
107 shb_hdr->section_length = -1;
109 shb_hdr->opt_comment = g_strdup_printf("Dump of PDU:s from %s", cfile.filename);
110 shb_hdr->shb_hardware = NULL; /* UTF-8 string containing the
111 * description of the hardware used to create this section.
113 shb_hdr->shb_os = os_info_str->str; /* UTF-8 string containing the name
114 * of the operating system used to create this section.
116 g_string_free(os_info_str, FALSE); /* The actual string is not freed */
117 shb_hdr->shb_user_appl = appname; /* UTF-8 string containing the name
118 * of the application used to create this section.
122 /* Create fake IDB info */
123 idb_inf = g_new(wtapng_iface_descriptions_t,1);
124 idb_inf->number_of_interfaces = 1;
125 idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t));
127 /* create the fake interface data */
128 int_data.wtap_encap = WTAP_ENCAP_WIRESHARK_UPPER_PDU;
129 int_data.time_units_per_second = 1000000; /* default microsecond resolution */
130 int_data.link_type = wtap_wtap_encap_to_pcap_encap(WTAP_ENCAP_WIRESHARK_UPPER_PDU);
131 int_data.snap_len = WTAP_MAX_PACKET_SIZE;
132 int_data.if_name = g_strdup("Fake IF, PDU->Export");
133 int_data.opt_comment = NULL;
134 int_data.if_description = NULL;
135 int_data.if_speed = 0;
136 int_data.if_tsresol = 6;
137 int_data.if_filter_str = NULL;
138 int_data.bpf_filter_len = 0;
139 int_data.if_filter_bpf_bytes = NULL;
140 int_data.if_os = NULL;
141 int_data.if_fcslen = -1;
142 int_data.num_stat_entries = 0; /* Number of ISB:s */
143 int_data.interface_statistics = NULL;
145 g_array_append_val(idb_inf->interface_data, int_data);
147 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);
148 if (exp_pdu_tap_data->wdh == NULL) {
149 open_failure_alert_box(capfile_name, err, TRUE);
155 cf_retap_packets(&cfile);
158 if (!wtap_dump_close(exp_pdu_tap_data->wdh, &err)) {
159 write_failure_alert_box(capfile_name, err);
162 remove_tap_listener(exp_pdu_tap_data);
164 if (cf_open(&cfile, capfile_name, WTAP_TYPE_AUTO, TRUE /* temporary file */, &err) != CF_OK) {
165 open_failure_alert_box(capfile_name, err, FALSE);
169 switch (cf_read(&cfile, FALSE)) {
172 /* Just because we got an error, that doesn't mean we were unable
173 to read any of the file; we handle what we could get from the
177 case CF_READ_ABORTED:
178 /* The user bailed out of re-reading the capture file; the
179 capture file has been closed - just free the capture file name
180 string and return (without changing the last containing
186 g_free(capfile_name);
190 do_export_pdu(const char *filter, gchar *tap_name, gpointer data)
192 GString *error_string;
193 exp_pdu_t *exp_pdu_tap_data = (exp_pdu_t *)data;
195 /* Register this tap listener now */
196 error_string = register_tap_listener(tap_name, /* The name of the tap we want to listen to */
197 exp_pdu_tap_data, /* instance identifier/pointer to a struct holding
198 * all state variables */
199 filter, /* pointer to a filter string */
200 TL_REQUIRES_NOTHING, /* flags for the tap listener */
205 /* Error. We failed to attach to the tap. Clean up */
206 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
207 g_string_free(error_string, TRUE);
211 exp_pdu_file_open(exp_pdu_tap_data);
221 * indent-tabs-mode: nil
224 * ex: set shiftwidth=4 tabstop=8 expandtab:
225 * :indentSize=4:tabSize=8:noTabs=true: