Exported PDU: add support for data length on the wire
[metze/wireshark/wip.git] / ui / tap_export_pdu.c
1 /* tap_export_pdu.c
2  * Routines for exporting PDU:s 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 #include <glib.h>
26
27 #include "globals.h"
28 #include "wtap.h"
29 #include "pcap-encap.h"
30 #include "version_info.h"
31 #include "wsutil/tempfile.h"
32
33 #include <epan/tap.h>
34 #include <epan/exported_pdu.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 int
42 export_pdu_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_, 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     int buffer_len;
49     guint8 *packet_buf;
50
51     buffer_len = exp_pdu_data->tvb_captured_length + exp_pdu_data->tlv_buffer_len;
52     packet_buf = (guint8 *)g_malloc(buffer_len);
53
54     if(exp_pdu_data->tlv_buffer_len > 0){
55         memcpy(packet_buf, exp_pdu_data->tlv_buffer, exp_pdu_data->tlv_buffer_len);
56         g_free(exp_pdu_data->tlv_buffer);
57     }
58     if(exp_pdu_data->tvb_length > 0){
59         tvb_memcpy(exp_pdu_data->pdu_tvb, packet_buf+exp_pdu_data->tlv_buffer_len, 0, exp_pdu_data->tvb_length);
60     }
61     pkthdr.ts.secs   = pinfo->fd->abs_ts.secs;
62     pkthdr.ts.nsecs  = pinfo->fd->abs_ts.nsecs;
63     pkthdr.caplen    = buffer_len;
64     pkthdr.len       = exp_pdu_data->tvb_reported_length + exp_pdu_data->tlv_buffer_len;
65
66     pkthdr.pkt_encap = exp_pdu_tap_data->pkt_encap;
67     pkthdr.interface_id = 0;
68     pkthdr.presence_flags = 0;
69     pkthdr.opt_comment = g_strdup(pinfo->pkt_comment);
70     pkthdr.drop_count = 0;
71     pkthdr.pack_flags = 0;
72     pkthdr.presence_flags = WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID|WTAP_HAS_TS|WTAP_HAS_PACK_FLAGS;
73
74     wtap_dump(exp_pdu_tap_data->wdh, &pkthdr, packet_buf, &err);
75
76     g_free(packet_buf);
77     g_free(pkthdr.opt_comment);
78
79     return FALSE; /* Do not redraw */
80 }
81
82 void
83 exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data)
84 {
85     int   import_file_fd;
86     char *tmpname, *capfile_name;
87     int   err;
88
89     /* pcapng defs */
90     wtapng_section_t            *shb_hdr;
91     wtapng_iface_descriptions_t *idb_inf;
92     wtapng_if_descr_t            int_data;
93     GString                     *os_info_str;
94     char                         appname[100];
95
96     /* Choose a random name for the temporary import buffer */
97     import_file_fd = create_tempfile(&tmpname, "Wireshark_PDU_");
98     capfile_name = g_strdup(tmpname);
99
100     /* Create data for SHB  */
101     os_info_str = g_string_new("");
102     get_os_version_info(os_info_str);
103
104     g_snprintf(appname, sizeof(appname), "Wireshark " VERSION "%s", wireshark_gitversion);
105
106     shb_hdr = g_new(wtapng_section_t,1);
107     shb_hdr->section_length = -1;
108     /* options */
109     shb_hdr->opt_comment    = g_strdup_printf("Dump of PDU:s from %s", cfile.filename);
110     shb_hdr->shb_hardware   = NULL;                    /* UTF-8 string containing the
111                                                        * description of the hardware used to create this section.
112                                                        */
113     shb_hdr->shb_os         = os_info_str->str;        /* UTF-8 string containing the name
114                                                        * of the operating system used to create this section.
115                                                        */
116     g_string_free(os_info_str, FALSE);                /* The actual string is not freed */
117     shb_hdr->shb_user_appl  = appname;                /* UTF-8 string containing the name
118                                                        *  of the application used to create this section.
119                                                        */
120
121
122     /* Create fake IDB info */
123     idb_inf = g_new(wtapng_iface_descriptions_t,1);
124     idb_inf->number_of_interfaces = 1;
125     idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t));
126
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;
144
145     g_array_append_val(idb_inf->interface_data, int_data);
146
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);
150         goto end;
151     }
152
153
154     /* Run the tap */
155     cf_retap_packets(&cfile);
156
157
158     if (!wtap_dump_close(exp_pdu_tap_data->wdh, &err)) {
159         write_failure_alert_box(capfile_name, err);
160     }
161
162     remove_tap_listener(exp_pdu_tap_data);
163
164     if (cf_open(&cfile, capfile_name, WTAP_TYPE_AUTO, TRUE /* temporary file */, &err) != CF_OK) {
165         open_failure_alert_box(capfile_name, err, FALSE);
166         goto end;
167     }
168
169     switch (cf_read(&cfile, FALSE)) {
170     case CF_READ_OK:
171     case CF_READ_ERROR:
172     /* Just because we got an error, that doesn't mean we were unable
173        to read any of the file; we handle what we could get from the
174        file. */
175     break;
176
177     case CF_READ_ABORTED:
178     /* The user bailed out of re-reading the capture file; the
179        capture file has been closed - just free the capture file name
180        string and return (without changing the last containing
181        directory). */
182     break;
183     }
184
185 end:
186     g_free(capfile_name);
187 }
188
189 gboolean
190 do_export_pdu(const char *filter, gchar *tap_name, gpointer data)
191 {
192     GString        *error_string;
193     exp_pdu_t  *exp_pdu_tap_data = (exp_pdu_t *)data;
194
195     /* Register this tap listener now */
196     error_string = register_tap_listener(tap_name,             /* The name of the tap we want to listen to */
197                                          exp_pdu_tap_data,     /* instance identifier/pointer to a struct holding
198                                                                 * all state variables */
199                                          filter,               /* pointer to a filter string */
200                                          TL_REQUIRES_NOTHING,  /* flags for the tap listener */
201                                          NULL,
202                                          export_pdu_packet,
203                                          NULL);
204     if (error_string){
205         /* Error.  We failed to attach to the tap. Clean up */
206         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
207         g_string_free(error_string, TRUE);
208         return FALSE;
209     }
210
211     exp_pdu_file_open(exp_pdu_tap_data);
212     return TRUE;
213 }
214
215 /*
216  * Editor modelines
217  *
218  * Local Variables:
219  * c-basic-offset: 4
220  * tab-width: 8
221  * indent-tabs-mode: nil
222  * End:
223  *
224  * ex: set shiftwidth=4 tabstop=8 expandtab:
225  * :indentSize=4:tabSize=8:noTabs=true:
226  */