Add some GCC warnings to the standard set, and add some others to the
[obnox/wireshark/wip.git] / epan / dissectors / packet-user_encap.c
1 /* packet-user_encap.c
2  * Allow users to specify the dissectors for DLTs
3  * Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998
10  *
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  *  of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <string.h>
32 #include <glib.h>
33 #include <epan/packet.h>
34 #include <epan/expert.h>
35 #include <epan/prefs.h>
36 #include <epan/uat.h>
37 #include <epan/emem.h>
38
39 #ifdef _MSC_VER
40 /* disable: warning C4090: 'XY' : different 'const' qualifiers */
41 #pragma warning(disable:4090)
42 #endif
43
44 typedef struct _user_encap_t {
45         guint encap;
46         dissector_handle_t payload_proto;
47         dissector_handle_t header_proto;
48         dissector_handle_t trailer_proto;
49         guint header_size;
50         guint trailer_size;
51 } user_encap_t;
52
53 #define ENCAP0_STR "User 0 (DLT=147)"
54 static const value_string user_dlts[] = {
55         { WTAP_ENCAP_USER0, ENCAP0_STR},
56         { WTAP_ENCAP_USER1, "User 1 (DLT=148)"},
57         { WTAP_ENCAP_USER2, "User 2 (DLT=149)"},
58         { WTAP_ENCAP_USER3, "User 3 (DLT=150)"},
59         { WTAP_ENCAP_USER4, "User 4 (DLT=151)"},
60         { WTAP_ENCAP_USER5, "User 5 (DLT=152)"},
61         { WTAP_ENCAP_USER6, "User 6 (DLT=153)"},
62         { WTAP_ENCAP_USER7, "User 7 (DLT=154)"},
63         { WTAP_ENCAP_USER8, "User 8 (DLT=155)"},
64         { WTAP_ENCAP_USER9, "User 9 (DLT=156)"},
65         { WTAP_ENCAP_USER10, "User 10 (DLT=157)"},
66         { WTAP_ENCAP_USER11, "User 11 (DLT=158)"},
67         { WTAP_ENCAP_USER12, "User 12 (DLT=159)"},
68         { WTAP_ENCAP_USER13, "User 13 (DLT=160)"},
69         { WTAP_ENCAP_USER14, "User 14 (DLT=161)"},
70         { WTAP_ENCAP_USER15, "User 15 (DLT=162)"},
71         { 0, NULL }
72 };
73 static int proto_user_encap = -1;
74
75 static user_encap_t* encaps = NULL;
76 static guint num_encaps = 0;
77 static uat_t* encaps_uat;
78 static dissector_handle_t data_handle;
79
80 static void dissect_user(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree) {
81         user_encap_t* encap = NULL;
82         tvbuff_t* payload_tvb;
83         int offset = 0;
84         int len;
85         guint i;
86         
87         for (i = 0; i < num_encaps; i++) {
88                 if (encaps[i].encap == pinfo->match_port) {
89                         encap = &(encaps[i]);
90                         break;
91                 }
92         }
93         
94         if (!encap) {
95                 char* msg = ep_strdup_printf("user encap not hanlded: DLT=%d, check you Preferences->Protocols->DLT_USER",
96                                                                          pinfo->match_port + 147 - WTAP_ENCAP_USER0);
97                 proto_item* i = proto_tree_add_text(tree, tvb, 0, 0, msg);
98                 
99                 expert_add_info_format(pinfo, i, PI_UNDECODED, PI_WARN, msg);
100                 
101                 call_dissector(data_handle, tvb, pinfo, tree);
102                 return;
103         } else {
104                 proto_item* i = proto_tree_add_item(tree,proto_user_encap,tvb,0,0,FALSE);
105                 proto_item_set_text(i,"DLT: %d",pinfo->match_port + 147 - WTAP_ENCAP_USER0);
106         }
107
108         len = tvb_reported_length(tvb) - (encap->header_size + encap->trailer_size);
109         
110         if (encap->header_size) {
111                 tvbuff_t* hdr_tvb = tvb_new_subset(tvb, 0, encap->header_size, encap->header_size);
112                 call_dissector(encap->header_proto, hdr_tvb, pinfo, tree);
113                 offset = encap->header_size;
114         }
115         
116         payload_tvb = tvb_new_subset(tvb, encap->header_size, len, len);
117         call_dissector(encap->payload_proto, payload_tvb, pinfo, tree);
118
119         if (encap->trailer_size) {
120                 tvbuff_t* trailer_tvb = tvb_new_subset(tvb, encap->header_size + len, encap->trailer_size, encap->trailer_size);
121                 call_dissector(encap->trailer_proto, trailer_tvb, pinfo, tree);
122                 offset = encap->trailer_size;
123         }
124 }
125
126 static void user_update_cb(void* r _U_, const char** err _U_) {
127 }
128
129 UAT_VS_DEF(user_encap, encap, user_encap_t, WTAP_ENCAP_USER0, ENCAP0_STR)
130 UAT_PROTO_DEF(user_encap, payload_proto, user_encap_t)
131 UAT_DEC_CB_DEF(user_encap, header_size, user_encap_t)
132 UAT_DEC_CB_DEF(user_encap, trailer_size, user_encap_t)
133 UAT_PROTO_DEF(user_encap, header_proto, user_encap_t)
134 UAT_PROTO_DEF(user_encap, trailer_proto, user_encap_t)
135
136 static dissector_handle_t user_encap_handle;
137
138 void proto_reg_handoff_user_encap(void)
139 {
140         guint i;
141         
142         user_encap_handle = find_dissector("user_dlt");
143         data_handle = find_dissector("data");
144         
145         for (i = WTAP_ENCAP_USER0 ; i <= WTAP_ENCAP_USER15; i++)
146                 dissector_add("wtap_encap", i, user_encap_handle);
147
148 }
149
150
151 void proto_register_user_encap(void)
152 {
153         module_t *module;
154
155         static uat_field_t user_flds[] = {
156                 UAT_FLD_VS(user_encap,encap,user_dlts,"The DLT"),
157                 UAT_FLD_PROTO(user_encap,payload_proto,"Protocol to be used for the payload of this DLT"),
158                 UAT_FLD_DEC(user_encap,header_size,"Size of an eventual header that precedes the actual payload, 0 means none"),
159                 UAT_FLD_PROTO(user_encap,header_proto,"Protocol to be used for the header (empty = data)"),
160                 UAT_FLD_DEC(user_encap,trailer_size,"Size of an eventual trailer that follows the actual payload, 0 means none"),
161                 UAT_FLD_PROTO(user_encap,trailer_proto,"Protocol to be used for the trailer (empty = data)"),
162                 UAT_END_FIELDS
163         };
164         
165         
166         proto_user_encap = proto_register_protocol("DLT User","DLT_USER","user_dlt");
167         
168         module = prefs_register_protocol(proto_user_encap, NULL);
169         
170         encaps_uat = uat_new("User DLTs Table",
171                                                  sizeof(user_encap_t),
172                                                  "user_dlts",
173                                                  (void**) &encaps,
174                                                  &num_encaps,
175                                                  UAT_CAT_FFMT,
176                                                  "ChUserDLTsSection",
177                                                  NULL,
178                                                  user_update_cb,
179                                                  NULL,
180                                                  user_flds );
181         
182         prefs_register_uat_preference(module,
183                                                                   "encaps_table",
184                                                                   "Encapsulations Table",
185                                                                   "A table that enumerates the various protocols to be used against a cartain user DLT",
186                                                                   encaps_uat);
187         
188         
189         register_dissector("user_dlt",dissect_user,proto_user_encap);
190
191         /*
192         prefs_register_protocol_obsolete(proto_register_protocol("DLT User A","DLT_USER_A","user_dlt_a"));
193         prefs_register_protocol_obsolete(proto_register_protocol("DLT User B","DLT_USER_B","user_dlt_b"));
194         prefs_register_protocol_obsolete(proto_register_protocol("DLT User C","DLT_USER_C","user_dlt_c"));
195         prefs_register_protocol_obsolete(proto_register_protocol("DLT User D","DLT_USER_D","user_dlt_d"));
196         */
197 }