Add data structures necessary to support multiple Section Header blocks.
[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 "wiretap/pcap-encap.h"
28 #include "wsutil/os_version_info.h"
29 #include "ws_version_info.h"
30
31 #include <epan/tap.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>
37
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->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;
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 int
99 exp_pdu_open(exp_pdu_t *exp_pdu_tap_data, int fd, char *comment)
100 {
101
102     int   err;
103
104     /* pcapng defs */
105     wtap_optionblock_t           shb_hdr;
106     GArray                      *shb_hdrs = g_array_new(FALSE, FALSE, sizeof(wtap_optionblock_t));
107     wtapng_iface_descriptions_t *idb_inf;
108     wtap_optionblock_t           int_data;
109     wtapng_if_descr_mandatory_t *int_data_mand;
110     GString                     *os_info_str;
111     gsize                        opt_len;
112
113     /* Create data for SHB  */
114     os_info_str = g_string_new("");
115     get_os_version_info(os_info_str);
116
117     shb_hdr = wtap_optionblock_create(WTAP_OPTION_BLOCK_NG_SECTION);
118
119     /* options */
120     wtap_optionblock_set_option_string(shb_hdr, OPT_COMMENT, comment, strlen(comment));
121     g_free(comment);
122
123     /*
124      * UTF-8 string containing the name of the operating system used to create
125      * this section.
126      */
127     opt_len = os_info_str->len;
128     wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_OS, g_string_free(os_info_str, TRUE), opt_len);
129     /*
130      * UTF-8 string containing the name of the application used to create
131      * this section.
132      */
133     wtap_optionblock_set_option_string_format(shb_hdr, OPT_SHB_USERAPPL, "Wireshark %s", get_ws_vcs_version_info());
134
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_optionblock_t));
138
139     /* create the fake interface data */
140     int_data = wtap_optionblock_create(WTAP_OPTION_BLOCK_IF_DESCR);
141     int_data_mand = (wtapng_if_descr_mandatory_t*)wtap_optionblock_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;
146
147     wtap_optionblock_set_option_string(int_data, OPT_IDB_NAME, "Fake IF, PDU->Export", strlen("Fake IF, PDU->Export"));
148     wtap_optionblock_set_option_uint8(int_data, OPT_IDB_TSRESOL, 9);
149
150     g_array_append_val(idb_inf->interface_data, int_data);
151
152     g_array_append_val(shb_hdrs, shb_hdr);
153
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) {
158         g_assert(err != 0);
159         return err;
160     }
161
162     return 0;
163 }
164
165 int
166 exp_pdu_close(exp_pdu_t *exp_pdu_tap_data)
167 {
168     int err = 0;
169     if (!wtap_dump_close(exp_pdu_tap_data->wdh, &err))
170         g_assert(err != 0);
171
172     remove_tap_listener(exp_pdu_tap_data);
173     return err;
174 }
175
176
177 char *
178 exp_pdu_pre_open(const char *tap_name, const char *filter, exp_pdu_t *exp_pdu_tap_data)
179 {
180     GString        *error_string;
181
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);
184
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 */
191                                          NULL,
192                                          export_pdu_packet,
193                                          NULL);
194     if (error_string != NULL)
195         return g_string_free(error_string, FALSE);
196
197     return NULL;
198 }
199
200
201 /*
202  * Editor modelines
203  *
204  * Local Variables:
205  * c-basic-offset: 4
206  * tab-width: 8
207  * indent-tabs-mode: nil
208  * End:
209  *
210  * ex: set shiftwidth=4 tabstop=8 expandtab:
211  * :indentSize=4:tabSize=8:noTabs=true:
212  */