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 "wiretap/pcap-encap.h"
28 #include "wsutil/tempfile.h"
29 #include "wsutil/os_version_info.h"
30 #include "ws_version_info.h"
33 #include <epan/exported_pdu.h>
34 #include <epan/epan_dissect.h>
35 #include <wiretap/wtap_opttypes.h>
36 #include <wiretap/pcapng.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, 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;
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);
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);
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);
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;
71 pkthdr.pkt_encap = exp_pdu_tap_data->pkt_encap;
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);
78 pkthdr.presence_flags = WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID|WTAP_HAS_TS|WTAP_HAS_PACK_FLAGS;
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)) {
85 case WTAP_ERR_UNWRITABLE_REC_DATA:
95 g_free(pkthdr.opt_comment);
97 return FALSE; /* Do not redraw */
101 exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data)
103 char *tmpname, *capfile_name;
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;
114 /* Create data for SHB */
115 os_info_str = g_string_new("");
116 get_os_version_info(os_info_str);
118 shb_hdr = wtap_optionblock_create(WTAP_OPTION_BLOCK_NG_SECTION);
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);
126 * UTF-8 string containing the name of the operating system used to create
129 wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_OS, g_string_free(os_info_str, TRUE));
131 * UTF-8 string containing the name of the application used to create
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);
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));
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;
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);
153 g_array_append_val(idb_inf->interface_data, int_data);
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);
167 cf_retap_packets(&cfile);
170 if (!wtap_dump_close(exp_pdu_tap_data->wdh, &err)) {
171 write_failure_alert_box(capfile_name, err);
174 remove_tap_listener(exp_pdu_tap_data);
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);
182 switch (cf_read(&cfile, FALSE)) {
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
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
199 g_free(capfile_name);
200 wtap_optionblock_free(shb_hdr);
201 wtap_free_idb_info(idb_inf);
205 do_export_pdu(const char *filter, gchar *tap_name, exp_pdu_t *exp_pdu_tap_data)
207 GString *error_string;
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 */
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);
225 exp_pdu_file_open(exp_pdu_tap_data);
235 * indent-tabs-mode: nil
238 * ex: set shiftwidth=4 tabstop=8 expandtab:
239 * :indentSize=4:tabSize=8:noTabs=true: