GOOSE Messages don't use the length field to perform the dissection.
[obnox/wireshark/wip.git] / asn1 / goose / packet-goose-template.c
1 /* packet-goose.c
2  * Routines for IEC 61850 GOOSE packet dissection
3  * Martin Lutz 2008
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <epan/packet.h>
32 #include <epan/asn1.h>
33 #include <epan/etypes.h>
34 #include <epan/expert.h>
35 #include <epan/nstime.h>
36
37 #include "packet-ber.h"
38 #include "packet-acse.h"
39
40 #define PNAME  "GOOSE"
41 #define PSNAME "GOOSE"
42 #define PFNAME "goose"
43
44 /* Initialize the protocol and registered fields */
45 static int proto_goose = -1;
46 static int hf_goose_appid = -1;
47 static int hf_goose_length = -1;
48 static int hf_goose_reserve1 = -1;
49 static int hf_goose_reserve2 = -1;
50
51 #include "packet-goose-hf.c"
52
53 /* Initialize the subtree pointers */
54 static int ett_goose = -1;
55
56 #include "packet-goose-ett.c"
57
58 #include "packet-goose-fn.c"
59
60 /*
61 * Dissect GOOSE PDUs inside a PPDU.
62 */
63 static void
64 dissect_goose(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
65 {
66         int offset = 0;
67         int old_offset;
68         guint16 length;
69         proto_item *item = NULL;
70         proto_tree *tree = NULL;
71         asn1_ctx_t asn1_ctx;
72         asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
73
74         col_set_str(pinfo->cinfo, COL_PROTOCOL, PNAME);
75         col_clear(pinfo->cinfo, COL_INFO);
76
77         if (parent_tree){
78                 item = proto_tree_add_item(parent_tree, proto_goose, tvb, 0, -1, ENC_NA);
79                 tree = proto_item_add_subtree(item, ett_goose);
80
81
82                 /* APPID */
83                 proto_tree_add_item(tree, hf_goose_appid, tvb, offset, 2, ENC_BIG_ENDIAN);
84
85                 /* Length */
86                 length = tvb_get_ntohs(tvb, offset + 2);
87                 proto_tree_add_item(tree, hf_goose_length, tvb, offset + 2, 2, ENC_BIG_ENDIAN);
88
89                 /* Reserved 1 */
90                 proto_tree_add_item(tree, hf_goose_reserve1, tvb, offset + 4, 2, ENC_BIG_ENDIAN);
91
92                 /* Reserved 2 */
93                 proto_tree_add_item(tree, hf_goose_reserve2, tvb, offset + 6, 2, ENC_BIG_ENDIAN);
94
95                 offset = 8;
96                 while (offset < length){
97                         old_offset = offset;
98                         offset = dissect_goose_GOOSEpdu(FALSE, tvb, offset, &asn1_ctx , tree, -1);
99                         if (offset == old_offset) {
100                                 proto_tree_add_text(tree, tvb, offset, -1, "Internal error, zero-byte GOOSE PDU");
101                                 return;
102                         }
103                 }
104         }
105 }
106
107
108 /*--- proto_register_goose -------------------------------------------*/
109 void proto_register_goose(void) {
110
111   /* List of fields */
112   static hf_register_info hf[] =
113   {
114         { &hf_goose_appid,
115         { "APPID",      "goose.appid", FT_UINT16, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
116
117         { &hf_goose_length,
118         { "Length",     "goose.length", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
119
120         { &hf_goose_reserve1,
121         { "Reserved 1", "goose.reserve1", FT_UINT16, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
122
123         { &hf_goose_reserve2,
124         { "Reserved 2", "goose.reserve2", FT_UINT16, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
125
126 #include "packet-goose-hfarr.c"
127   };
128
129   /* List of subtrees */
130   static gint *ett[] = {
131           &ett_goose,
132 #include "packet-goose-ettarr.c"
133   };
134
135         /* Register protocol */
136         proto_goose = proto_register_protocol(PNAME, PSNAME, PFNAME);
137         register_dissector("goose", dissect_goose, proto_goose);
138
139         /* Register fields and subtrees */
140         proto_register_field_array(proto_goose, hf, array_length(hf));
141         proto_register_subtree_array(ett, array_length(ett));
142
143 }
144
145 /*--- proto_reg_handoff_goose --- */
146 void proto_reg_handoff_goose(void) {
147
148         dissector_handle_t goose_handle;
149         goose_handle = find_dissector("goose");
150
151         dissector_add_uint("ethertype", ETHERTYPE_IEC61850_GOOSE, goose_handle);
152 }