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/os_version_info.h"
29 #include "ws_version_info.h"
32 #include <epan/exported_pdu.h>
33 #include <epan/epan_dissect.h>
34 #include <wiretap/wtap.h>
35 #include <wiretap/wtap_opttypes.h>
36 #include <wiretap/pcapng.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_captured_length > 0){
61 tvb_memcpy(exp_pdu_data->pdu_tvb, packet_buf+exp_pdu_data->tlv_buffer_len, 0, exp_pdu_data->tvb_captured_length);
63 pkthdr.rec_type = REC_TYPE_PACKET;
64 pkthdr.ts.secs = pinfo->abs_ts.secs;
65 pkthdr.ts.nsecs = pinfo->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_open(exp_pdu_t *exp_pdu_tap_data, int fd, char *comment)
105 wtap_block_t shb_hdr;
106 GArray *shb_hdrs = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
107 wtapng_iface_descriptions_t *idb_inf;
108 wtap_block_t int_data;
109 wtapng_if_descr_mandatory_t *int_data_mand;
110 GString *os_info_str;
113 /* Create data for SHB */
114 os_info_str = g_string_new("");
115 get_os_version_info(os_info_str);
117 shb_hdr = wtap_block_create(WTAP_BLOCK_NG_SECTION);
120 wtap_block_add_string_option(shb_hdr, OPT_COMMENT, comment, strlen(comment));
124 * UTF-8 string containing the name of the operating system used to create
127 opt_len = os_info_str->len;
128 wtap_block_add_string_option(shb_hdr, OPT_SHB_OS, g_string_free(os_info_str, TRUE), opt_len);
130 * UTF-8 string containing the name of the application used to create
133 wtap_block_add_string_option_format(shb_hdr, OPT_SHB_USERAPPL, "Wireshark %s", get_ws_vcs_version_info());
135 /* Create fake IDB info */
136 idb_inf = g_new(wtapng_iface_descriptions_t,1);
137 idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
139 /* create the fake interface data */
140 int_data = wtap_block_create(WTAP_BLOCK_IF_DESCR);
141 int_data_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(int_data);
142 int_data_mand->wtap_encap = WTAP_ENCAP_WIRESHARK_UPPER_PDU;
143 int_data_mand->time_units_per_second = 1000000000; /* default nanosecond resolution */
144 int_data_mand->link_type = wtap_wtap_encap_to_pcap_encap(WTAP_ENCAP_WIRESHARK_UPPER_PDU);
145 int_data_mand->snap_len = WTAP_MAX_PACKET_SIZE;
147 wtap_block_add_string_option(int_data, OPT_IDB_NAME, "Fake IF, PDU->Export", strlen("Fake IF, PDU->Export"));
148 wtap_block_add_uint8_option(int_data, OPT_IDB_TSRESOL, 9);
150 g_array_append_val(idb_inf->interface_data, int_data);
152 g_array_append_val(shb_hdrs, shb_hdr);
154 /* Use a random name for the temporary import buffer */
155 exp_pdu_tap_data->wdh = wtap_dump_fdopen_ng(fd, WTAP_FILE_TYPE_SUBTYPE_PCAPNG, WTAP_ENCAP_WIRESHARK_UPPER_PDU, WTAP_MAX_PACKET_SIZE, FALSE,
156 shb_hdrs, idb_inf, NULL, &err);
157 if (exp_pdu_tap_data->wdh == NULL) {
166 exp_pdu_close(exp_pdu_t *exp_pdu_tap_data)
169 if (!wtap_dump_close(exp_pdu_tap_data->wdh, &err))
172 remove_tap_listener(exp_pdu_tap_data);
178 exp_pdu_pre_open(const char *tap_name, const char *filter, exp_pdu_t *exp_pdu_tap_data)
180 GString *error_string;
182 /* XXX: can we always assume WTAP_ENCAP_WIRESHARK_UPPER_PDU? */
183 exp_pdu_tap_data->pkt_encap = wtap_wtap_encap_to_pcap_encap(WTAP_ENCAP_WIRESHARK_UPPER_PDU);
185 /* Register this tap listener now */
186 error_string = register_tap_listener(tap_name, /* The name of the tap we want to listen to */
187 exp_pdu_tap_data, /* instance identifier/pointer to a struct holding
188 * all state variables */
189 filter, /* pointer to a filter string */
190 TL_REQUIRES_PROTO_TREE, /* flags for the tap listener */
194 if (error_string != NULL)
195 return g_string_free(error_string, FALSE);
207 * indent-tabs-mode: nil
210 * ex: set shiftwidth=4 tabstop=8 expandtab:
211 * :indentSize=4:tabSize=8:noTabs=true: