2 * Routines for exporting PDU:s to file
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
31 #include "pcap-encap.h"
32 #include "version_info.h"
33 #include "wsutil/tempfile.h"
36 #include <epan/exported_pdu.h>
38 #include "ui/alert_box.h"
39 #include "ui/simple_dialog.h"
40 #include "tap_export_pdu.h"
42 /* Main entry point to the tap */
44 export_pdu_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_, const void *data)
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;
53 buffer_len = exp_pdu_data->tvb_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.ts.secs = pinfo->fd->abs_ts.secs;
64 pkthdr.ts.nsecs = pinfo->fd->abs_ts.nsecs;
65 pkthdr.caplen = pkthdr.len = buffer_len;
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;
75 wtap_dump(exp_pdu_tap_data->wdh, &pkthdr, packet_buf, &err);
78 g_free(pkthdr.opt_comment);
80 return FALSE; /* Do not redraw */
84 exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data)
87 char *tmpname, *capfile_name;
91 wtapng_section_t *shb_hdr;
92 wtapng_iface_descriptions_t *idb_inf;
93 wtapng_if_descr_t int_data;
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);
101 /* Create data for SHB */
102 os_info_str = g_string_new("");
103 get_os_version_info(os_info_str);
105 g_snprintf(appname, sizeof(appname), "Wireshark " VERSION "%s", wireshark_svnversion);
107 shb_hdr = g_new(wtapng_section_t,1);
108 shb_hdr->section_length = -1;
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.
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.
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.
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));
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;
146 g_array_append_val(idb_inf->interface_data, int_data);
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);
156 cf_retap_packets(&cfile);
159 if (!wtap_dump_close(exp_pdu_tap_data->wdh, &err)) {
160 write_failure_alert_box(capfile_name, err);
163 remove_tap_listener(exp_pdu_tap_data);
165 if (cf_open(&cfile, capfile_name, TRUE /* temporary file */, &err) != CF_OK) {
166 open_failure_alert_box(capfile_name, err, FALSE);
170 switch (cf_read(&cfile, FALSE)) {
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
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
187 g_free(capfile_name);
191 do_export_pdu(const char *filter, gchar *tap_name, gpointer data)
193 GString *error_string;
194 exp_pdu_t *exp_pdu_tap_data = (exp_pdu_t *)data;
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 */
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);
212 exp_pdu_file_open(exp_pdu_tap_data);
222 * indent-tabs-mode: nil
225 * ex: set shiftwidth=4 tabstop=8 expandtab:
226 * :indentSize=4:tabSize=8:noTabs=true: