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 "wsutil/tempfile.h"
31 #include "wsutil/os_version_info.h"
32 #include "wsutil/ws_version_info.h"
35 #include <epan/exported_pdu.h>
37 #include "ui/alert_box.h"
38 #include "ui/simple_dialog.h"
39 #include "tap_export_pdu.h"
41 /* Main entry point to the tap */
43 export_pdu_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_, const void *data)
45 const exp_pdu_data_t *exp_pdu_data = (const exp_pdu_data_t *)data;
46 exp_pdu_t *exp_pdu_tap_data = (exp_pdu_t *)tapdata;
47 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;
70 pkthdr.opt_comment = g_strdup(pinfo->pkt_comment);
71 pkthdr.presence_flags = WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID|WTAP_HAS_TS|WTAP_HAS_PACK_FLAGS;
73 /* XXX: should the pkthdr.pseudo_header be set to the pinfo's pseudo-header? */
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 %s", get_ws_vcs_version_info());
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->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 /* XXX: should this use the open_routine type in the cfile instead of WTAP_TYPE_AUTO? */
165 if (cf_open(&cfile, capfile_name, WTAP_TYPE_AUTO, 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: