Fix up a bunch of arguments to "dissect_ber_identifier()" to match its
[obnox/wireshark/wip.git] / epan / dissectors / packet-user_encap.c
1 /* packet-user_encap.c
2  * Allow users to specify the dissectors for USERn DLTs
3  * Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
4  *
5  * $Id$
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
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 <glib.h>
32 #include <epan/packet.h>
33 #include <epan/prefs.h>
34 #include <epan/report_err.h>
35
36 #include <epan/dissectors/packet-sscop.h>
37
38 typedef void (*encap_dissector_t)(tvbuff_t*,packet_info*,proto_tree*,dissector_handle_t);
39
40 typedef struct _user_encap_t {
41         gchar* name;
42         gchar* abbr;
43         gchar* long_name;
44         
45         gchar* payload;
46         gchar* header;
47         gchar* trailer;
48         guint header_size;
49         guint trailer_size;
50         
51         int hfid;
52         
53         gint encap;
54         
55         encap_dissector_t encap_dissector;
56         dissector_t dissector;
57         
58         module_t* module;
59         
60         dissector_handle_t handle;
61         dissector_handle_t payload_handle;
62         dissector_handle_t header_handle;
63         dissector_handle_t trailer_handle;
64 } user_encap_t;
65
66 static const enum_val_t encap_types[] = {
67         { "None", "No encpsulation", 0 },
68         { "SSCOP" , "SSCOP", 1 },
69         { NULL, NULL, 0 }
70 };
71
72 static const encap_dissector_t encap_dissectors[] = {
73         NULL,
74         dissect_sscop_and_payload
75 };
76
77 static void dissect_user(tvbuff_t* tvb,packet_info* pinfo,proto_tree* tree,guint id);
78
79 static void dissect_user0(tvbuff_t* tvb,packet_info* pinfo,proto_tree* tree) { dissect_user(tvb,pinfo,tree,0); } 
80 static void dissect_user1(tvbuff_t* tvb,packet_info* pinfo,proto_tree* tree) { dissect_user(tvb,pinfo,tree,1); } 
81 static void dissect_user2(tvbuff_t* tvb,packet_info* pinfo,proto_tree* tree) { dissect_user(tvb,pinfo,tree,2); } 
82 static void dissect_user3(tvbuff_t* tvb,packet_info* pinfo,proto_tree* tree) { dissect_user(tvb,pinfo,tree,3); } 
83 static void dissect_user4(tvbuff_t* tvb,packet_info* pinfo,proto_tree* tree) { dissect_user(tvb,pinfo,tree,4); } 
84 static void dissect_user5(tvbuff_t* tvb,packet_info* pinfo,proto_tree* tree) { dissect_user(tvb,pinfo,tree,5); } 
85 static void dissect_user6(tvbuff_t* tvb,packet_info* pinfo,proto_tree* tree) { dissect_user(tvb,pinfo,tree,6); } 
86 static void dissect_user7(tvbuff_t* tvb,packet_info* pinfo,proto_tree* tree) { dissect_user(tvb,pinfo,tree,7); } 
87 static void dissect_user8(tvbuff_t* tvb,packet_info* pinfo,proto_tree* tree) { dissect_user(tvb,pinfo,tree,8); } 
88 static void dissect_user9(tvbuff_t* tvb,packet_info* pinfo,proto_tree* tree) { dissect_user(tvb,pinfo,tree,9); } 
89 static void dissect_user10(tvbuff_t* tvb,packet_info* pinfo,proto_tree* tree) { dissect_user(tvb,pinfo,tree,10); } 
90 static void dissect_user11(tvbuff_t* tvb,packet_info* pinfo,proto_tree* tree) { dissect_user(tvb,pinfo,tree,11); } 
91 static void dissect_user12(tvbuff_t* tvb,packet_info* pinfo,proto_tree* tree) { dissect_user(tvb,pinfo,tree,12); } 
92 static void dissect_user13(tvbuff_t* tvb,packet_info* pinfo,proto_tree* tree) { dissect_user(tvb,pinfo,tree,13); } 
93 static void dissect_user14(tvbuff_t* tvb,packet_info* pinfo,proto_tree* tree) { dissect_user(tvb,pinfo,tree,14); } 
94 static void dissect_user15(tvbuff_t* tvb,packet_info* pinfo,proto_tree* tree) { dissect_user(tvb,pinfo,tree,15); } 
95
96 user_encap_t encaps[] = {
97         {"USER0","user0","DLT_USER00","","","",0,0,-1,0,NULL,dissect_user0,NULL,NULL,NULL,NULL,NULL},
98         {"USER1","user1","DLT_USER01","","","",0,0,-1,0,NULL,dissect_user1,NULL,NULL,NULL,NULL,NULL},
99         {"USER2","user2","DLT_USER02","","","",0,0,-1,0,NULL,dissect_user2,NULL,NULL,NULL,NULL,NULL},
100         {"USER3","user3","DLT_USER03","","","",0,0,-1,0,NULL,dissect_user3,NULL,NULL,NULL,NULL,NULL},
101         {"USER4","user4","DLT_USER04","","","",0,0,-1,0,NULL,dissect_user4,NULL,NULL,NULL,NULL,NULL},
102         {"USER5","user5","DLT_USER05","","","",0,0,-1,0,NULL,dissect_user5,NULL,NULL,NULL,NULL,NULL},
103         {"USER6","user6","DLT_USER06","","","",0,0,-1,0,NULL,dissect_user6,NULL,NULL,NULL,NULL,NULL},
104         {"USER7","user7","DLT_USER07","","","",0,0,-1,0,NULL,dissect_user7,NULL,NULL,NULL,NULL,NULL},
105         {"USER8","user8","DLT_USER08","","","",0,0,-1,0,NULL,dissect_user8,NULL,NULL,NULL,NULL,NULL},
106         {"USER9","user9","DLT_USER09","","","",0,0,-1,0,NULL,dissect_user9,NULL,NULL,NULL,NULL,NULL},
107         {"USER10","user10","DLT_USER10","","","",0,0,-1,0,NULL,dissect_user10,NULL,NULL,NULL,NULL,NULL},
108         {"USER11","user11","DLT_USER11","","","",0,0,-1,0,NULL,dissect_user11,NULL,NULL,NULL,NULL,NULL},
109         {"USER12","user12","DLT_USER12","","","",0,0,-1,0,NULL,dissect_user12,NULL,NULL,NULL,NULL,NULL},
110         {"USER13","user13","DLT_USER13","","","",0,0,-1,0,NULL,dissect_user13,NULL,NULL,NULL,NULL,NULL},
111         {"USER14","user14","DLT_USER14","","","",0,0,-1,0,NULL,dissect_user14,NULL,NULL,NULL,NULL,NULL},
112         {"USER15","user15","DLT_USER15","","","",0,0,-1,0,NULL,dissect_user15,NULL,NULL,NULL,NULL,NULL},
113 };
114
115 static void dissect_user(tvbuff_t* tvb,packet_info* pinfo,proto_tree* tree,guint id) {
116         user_encap_t* encap = &(encaps[id]);
117         tvbuff_t* payload_tvb;
118         int offset = 0;
119         int len = tvb_reported_length(tvb) - (encap->header_size + encap->trailer_size);
120         
121         if (encap->encap_dissector) {
122                 encap->encap_dissector(tvb,pinfo,tree,encap->payload_handle);
123                 return;
124         }
125         
126         if (encap->header_size) {
127                 tvbuff_t* hdr_tvb = tvb_new_subset(tvb, 0, encap->header_size, encap->header_size);
128                 call_dissector(encap->header_handle, hdr_tvb, pinfo, tree);
129                 offset = encap->header_size;
130         }
131         
132         payload_tvb = tvb_new_subset(tvb, encap->header_size, len, len);
133         call_dissector(encap->payload_handle, payload_tvb, pinfo, tree);
134
135         if (encap->trailer_size) {
136                 tvbuff_t* trailer_tvb = tvb_new_subset(tvb, encap->header_size + len, encap->trailer_size, encap->trailer_size);
137                 call_dissector(encap->trailer_handle, trailer_tvb, pinfo, tree);
138                 offset = encap->trailer_size;
139         }
140 }
141
142 void proto_reg_handoff_user_encap(void)
143 {
144         int i;
145         static dissector_handle_t data_handle;
146         
147         data_handle = find_dissector("data");
148
149         for (i = 0; i < 16; i++) {
150                 encaps[i].handle =  find_dissector(encaps[i].abbr);
151                 dissector_add("wtap_encap", WTAP_ENCAP_USER0 + i, encaps[i].handle);
152
153                 if(*(encaps[i].payload) != '\0') {
154                         encaps[i].payload_handle = find_dissector(encaps[i].payload);
155                         
156                         if (encaps[i].payload_handle == NULL) {
157                                 encaps[i].payload_handle = data_handle;
158                                 report_failure("%s: No such proto: %s",encaps[i].long_name,encaps[i].payload);
159                         }
160                 } else {
161                         encaps[i].payload_handle = data_handle;                 
162                 }
163
164                 if(*(encaps[i].header) != '\0') {
165                         encaps[i].header_handle = find_dissector(encaps[i].header);
166                         
167                         if (encaps[i].header_handle == NULL) {
168                                 encaps[i].header_handle = data_handle;
169                                 report_failure("%s: No such proto: %s",encaps[i].long_name,encaps[i].header);
170                         }
171                 } else {
172                         encaps[i].header_handle = data_handle;                  
173                 }
174                 
175                 if(*(encaps[i].trailer) != '\0') {
176                         encaps[i].trailer_handle = find_dissector(encaps[i].trailer);
177                         
178                         if (encaps[i].trailer_handle == NULL) {
179                                 encaps[i].trailer_handle = data_handle;
180                                 report_failure("%s: No such proto: %s",encaps[i].long_name,encaps[i].trailer);
181                         }
182                 } else {
183                         encaps[i].trailer_handle = data_handle;                 
184                 }
185                 
186                 encaps[i].encap_dissector = encap_dissectors[encaps[i].encap];
187         }
188 }
189
190 void proto_register_user_encap(void)
191 {
192         int i;
193         
194         for (i = 0; i < 16; i++) {
195                 encaps[i].hfid = proto_register_protocol(encaps[i].name, encaps[i].long_name, encaps[i].abbr);
196                 encaps[i].module = prefs_register_protocol(encaps[i].hfid, proto_reg_handoff_user_encap);
197                 
198                 prefs_register_string_preference(encaps[i].module, "payload","Payload", "Payload", &encaps[i].payload);
199                 prefs_register_enum_preference(encaps[i].module, "encap","Encapsulation", "The encapsulation used", &encaps[i].encap, encap_types, FALSE);
200                 prefs_register_uint_preference(encaps[i].module, "header_size","Header Size", "The size (in bytes) of the Header (0 if none)", 10, &encaps[i].header_size);
201                 prefs_register_uint_preference(encaps[i].module, "trailer_size","Trailer Size", "The size (in bytes) of the Trailer (0 if none)", 10, &encaps[i].trailer_size);
202                 prefs_register_string_preference(encaps[i].module, "header_proto","Header Protocol", "Header Protocol (used only when ecapsulation is not given)", &encaps[i].header);
203                 prefs_register_string_preference(encaps[i].module, "trailer_proto","Trailer Protocol", "Trailer Protocol (used only when ecapsulation is not given)", &encaps[i].trailer);
204                 
205                 register_dissector(encaps[i].abbr, encaps[i].dissector, encaps[i].hfid);
206         }
207 }