Pcapng: clean up Section Header Block handling
[metze/wireshark/wip.git] / ui / tap_export_pdu.c
1 /* tap_export_pdu.c
2  * Routines for exporting PDUs to file
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include "config.h"
24
25
26 #include "globals.h"
27 #include "pcap-encap.h"
28 #include "wsutil/tempfile.h"
29 #include "wsutil/os_version_info.h"
30 #include "wsutil/ws_version_info.h"
31
32 #include <epan/tap.h>
33 #include <epan/exported_pdu.h>
34 #include <epan/epan_dissect.h>
35
36 #include "ui/alert_box.h"
37 #include "ui/simple_dialog.h"
38 #include "tap_export_pdu.h"
39
40 /* Main entry point to the tap */
41 static gboolean
42 export_pdu_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt, const void *data)
43 {
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;
47     int err;
48     gchar *err_info;
49     int buffer_len;
50     guint8 *packet_buf;
51
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);
55
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);
59     }
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);
62     }
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;
68
69     pkthdr.pkt_encap = exp_pdu_tap_data->pkt_encap;
70
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);
75
76     pkthdr.presence_flags = WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID|WTAP_HAS_TS|WTAP_HAS_PACK_FLAGS;
77
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)) {
81         switch (err) {
82
83         case WTAP_ERR_UNWRITABLE_REC_DATA:
84             g_free(err_info);
85             break;
86
87         default:
88             break;
89         }
90     }
91
92     g_free(packet_buf);
93     g_free(pkthdr.opt_comment);
94
95     return FALSE; /* Do not redraw */
96 }
97
98 static void
99 exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data)
100 {
101     int   import_file_fd;
102     char *tmpname, *capfile_name;
103     int   err;
104
105     /* pcapng defs */
106     wtapng_section_t            *shb_hdr;
107     wtapng_iface_descriptions_t *idb_inf;
108     wtapng_if_descr_t            int_data;
109     GString                     *os_info_str;
110     char                        *appname;
111
112     /* Choose a random name for the temporary import buffer */
113     import_file_fd = create_tempfile(&tmpname, "Wireshark_PDU_");
114     capfile_name = g_strdup(tmpname);
115
116     /* Create data for SHB  */
117     os_info_str = g_string_new("");
118     get_os_version_info(os_info_str);
119
120     appname = g_strdup_printf("Wireshark %s", get_ws_vcs_version_info());
121
122     shb_hdr = g_new0(wtapng_section_t,1);
123     shb_hdr->section_length = -1;
124     /* options */
125     shb_hdr->opt_comment    = g_strdup_printf("Dump of PDUs from %s", cfile.filename);
126     /*
127      * UTF-8 string containing the description of the hardware used to create
128      * this section.
129      */
130     shb_hdr->shb_hardware   = NULL;
131     /*
132      * UTF-8 string containing the name of the operating system used to create
133      * this section.
134      */
135     shb_hdr->shb_os         = g_string_free(os_info_str, FALSE);
136     /*
137      * UTF-8 string containing the name of the application used to create
138      * this section.
139      */
140     shb_hdr->shb_user_appl  = appname;
141
142     /* Create fake IDB info */
143     idb_inf = g_new(wtapng_iface_descriptions_t,1);
144     idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t));
145
146     /* create the fake interface data */
147     int_data.wtap_encap            = WTAP_ENCAP_WIRESHARK_UPPER_PDU;
148     int_data.time_units_per_second = 1000000000; /* default nanosecond resolution */
149     int_data.link_type             = wtap_wtap_encap_to_pcap_encap(WTAP_ENCAP_WIRESHARK_UPPER_PDU);
150     int_data.snap_len              = WTAP_MAX_PACKET_SIZE;
151     int_data.if_name               = g_strdup("Fake IF, PDU->Export");
152     int_data.opt_comment           = NULL;
153     int_data.if_description        = NULL;
154     int_data.if_speed              = 0;
155     int_data.if_tsresol            = 9;
156     int_data.if_filter_str         = NULL;
157     int_data.bpf_filter_len        = 0;
158     int_data.if_filter_bpf_bytes   = NULL;
159     int_data.if_os                 = NULL;
160     int_data.if_fcslen             = -1;
161     int_data.num_stat_entries      = 0;          /* Number of ISB:s */
162     int_data.interface_statistics  = NULL;
163
164     g_array_append_val(idb_inf->interface_data, int_data);
165
166     exp_pdu_tap_data->wdh = wtap_dump_fdopen_ng(import_file_fd, WTAP_FILE_TYPE_SUBTYPE_PCAPNG,
167                                                 WTAP_ENCAP_WIRESHARK_UPPER_PDU, WTAP_MAX_PACKET_SIZE,
168                                                 FALSE, shb_hdr, idb_inf, NULL, &err);
169     if (exp_pdu_tap_data->wdh == NULL) {
170         open_failure_alert_box(capfile_name, err, TRUE);
171         goto end;
172     }
173
174
175     /* Run the tap */
176     cf_retap_packets(&cfile);
177
178
179     if (!wtap_dump_close(exp_pdu_tap_data->wdh, &err)) {
180         write_failure_alert_box(capfile_name, err);
181     }
182
183     remove_tap_listener(exp_pdu_tap_data);
184
185     /* XXX: should this use the open_routine type in the cfile instead of WTAP_TYPE_AUTO? */
186     if (cf_open(&cfile, capfile_name, WTAP_TYPE_AUTO, TRUE /* temporary file */, &err) != CF_OK) {
187         open_failure_alert_box(capfile_name, err, FALSE);
188         goto end;
189     }
190
191     switch (cf_read(&cfile, FALSE)) {
192     case CF_READ_OK:
193     case CF_READ_ERROR:
194     /* Just because we got an error, that doesn't mean we were unable
195        to read any of the file; we handle what we could get from the
196        file. */
197     break;
198
199     case CF_READ_ABORTED:
200     /* The user bailed out of re-reading the capture file; the
201        capture file has been closed - just free the capture file name
202        string and return (without changing the last containing
203        directory). */
204     break;
205     }
206
207 end:
208     g_free(capfile_name);
209     g_free(shb_hdr);
210     g_free(appname);
211 }
212
213 gboolean
214 do_export_pdu(const char *filter, gchar *tap_name, exp_pdu_t *exp_pdu_tap_data)
215 {
216     GString        *error_string;
217
218     /* Register this tap listener now */
219     error_string = register_tap_listener(tap_name,             /* The name of the tap we want to listen to */
220                                          exp_pdu_tap_data,     /* instance identifier/pointer to a struct holding
221                                                                 * all state variables */
222                                          filter,               /* pointer to a filter string */
223                                          TL_REQUIRES_NOTHING,  /* flags for the tap listener */
224                                          NULL,
225                                          export_pdu_packet,
226                                          NULL);
227     if (error_string){
228         /* Error.  We failed to attach to the tap. Clean up */
229         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
230         g_string_free(error_string, TRUE);
231         return FALSE;
232     }
233
234     exp_pdu_file_open(exp_pdu_tap_data);
235     return TRUE;
236 }
237
238 /*
239  * Editor modelines
240  *
241  * Local Variables:
242  * c-basic-offset: 4
243  * tab-width: 8
244  * indent-tabs-mode: nil
245  * End:
246  *
247  * ex: set shiftwidth=4 tabstop=8 expandtab:
248  * :indentSize=4:tabSize=8:noTabs=true:
249  */