some compilers dont like unnamed unions and structs
[obnox/wireshark/wip.git] / epan / dissectors / packet-idp.c
1 /* packet-idp.c
2  * Routines for XNS IDP
3  * Based on the Netware IPX dissector by Gilbert Ramirez <gram@alumni.rice.edu>
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 "packet-idp.h"
33 #include <epan/etypes.h>
34
35 static int proto_idp = -1;
36 static int hf_idp_checksum = -1;
37 static int hf_idp_len = -1;
38 static int hf_idp_src = -1;
39 static int hf_idp_dst = -1;
40 static int hf_idp_hops = -1;
41 static int hf_idp_packet_type = -1;
42 static int hf_idp_dnet = -1;
43 static int hf_idp_dnode = -1;
44 static int hf_idp_dsocket = -1;
45 static int hf_idp_snet = -1;
46 static int hf_idp_snode = -1;
47 static int hf_idp_ssocket = -1;
48
49 static gint ett_idp = -1;
50
51 static dissector_handle_t data_handle;
52
53 static dissector_table_t idp_type_dissector_table;
54
55 /*
56  * See
57  *
58  *      "Internet Transport Protocols", XSIS 028112, December 1981
59  *
60  * if you can find it; this is based on the headers in the BSD XNS
61  * implementation.
62  */
63
64 #define IDP_HEADER_LEN  30              /* It's *always* 30 bytes */
65
66 static const value_string idp_packet_type_vals[] = {
67         { IDP_PACKET_TYPE_RIP,          "RIP" },
68         { IDP_PACKET_TYPE_ECHO,         "Echo" },
69         { IDP_PACKET_TYPE_ERROR,        "Error" },
70         { IDP_PACKET_TYPE_PEP,          "PEP" },
71         { IDP_PACKET_TYPE_SPP,          "SPP" },
72         { 0,                            NULL }
73 };
74
75 static const value_string idp_socket_vals[] = {
76         { IDP_SOCKET_SMB,               "SMB" },
77         { 0,                            NULL }
78 };
79
80 static void
81 dissect_idp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
82 {
83         proto_tree      *idp_tree = NULL;
84         proto_item      *ti = NULL;
85         guint16         length;
86         guint8          type;
87         tvbuff_t        *next_tvb;
88         
89         if (check_col(pinfo->cinfo, COL_PROTOCOL))
90                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "IDP");
91         if (check_col(pinfo->cinfo, COL_INFO))
92                 col_clear(pinfo->cinfo, COL_INFO);
93
94         if (check_col(pinfo->cinfo, COL_INFO))
95                 col_clear(pinfo->cinfo, COL_INFO);
96
97         if (tree) {
98                 ti = proto_tree_add_item(tree, proto_idp, tvb, 0, IDP_HEADER_LEN, FALSE);
99                 idp_tree = proto_item_add_subtree(ti, ett_idp);
100         }
101
102         proto_tree_add_item(idp_tree, hf_idp_checksum, tvb, 0, 2, FALSE);
103         length = tvb_get_ntohs(tvb, 2);
104         proto_tree_add_uint_format(idp_tree, hf_idp_len, tvb, 2, 2, length,
105                 "Length: %u bytes", length);
106         /* Adjust the tvbuff length to include only the IDP datagram. */
107         set_actual_length(tvb, length);
108         proto_tree_add_item(idp_tree, hf_idp_hops, tvb, 4, 1, FALSE);
109         type = tvb_get_guint8(tvb, 5);
110         proto_tree_add_uint(idp_tree, hf_idp_packet_type, tvb, 5, 1, type);
111
112         pinfo->ptype = PT_IDP;
113
114         /* Destination */
115         proto_tree_add_item(idp_tree, hf_idp_dnet, tvb, 6, 4, FALSE);
116         proto_tree_add_item(idp_tree, hf_idp_dnode, tvb, 10, 6, FALSE);
117         pinfo->destport = tvb_get_ntohs(tvb, 16);
118         proto_tree_add_uint(idp_tree, hf_idp_dsocket, tvb, 16, 2,
119             pinfo->destport);
120
121         /* Source */
122         proto_tree_add_item(idp_tree, hf_idp_snet, tvb, 18, 4, FALSE);
123         proto_tree_add_item(idp_tree, hf_idp_snode, tvb, 22, 6, FALSE);
124         pinfo->srcport = tvb_get_ntohs(tvb, 28);
125         proto_tree_add_uint(idp_tree, hf_idp_ssocket, tvb, 28, 2,
126             pinfo->srcport);
127
128         /* Make the next tvbuff */
129         next_tvb = tvb_new_subset(tvb, IDP_HEADER_LEN, -1, -1);
130
131         /*
132          * Hand off to the dissector for the packet type.
133          */
134         if (dissector_try_port(idp_type_dissector_table, type, next_tvb,
135             pinfo, tree))
136                 return;
137
138         call_dissector(data_handle, next_tvb, pinfo, tree);
139 }
140
141 void
142 proto_register_idp(void)
143 {
144         static hf_register_info hf_idp[] = {
145                 { &hf_idp_checksum,
146                     { "Checksum",       "idp.checksum", FT_UINT16, BASE_HEX,
147                         NULL, 0x0, "", HFILL }},
148
149                 { &hf_idp_src,
150                     { "Source Address", "idp.src", FT_STRING, BASE_DEC,
151                         NULL, 0x0, "Source Address", HFILL }},
152
153                 { &hf_idp_dst,
154                     { "Destination Address",    "idp.dst", FT_STRING, BASE_DEC,
155                         NULL, 0x0,  "Destination Address", HFILL }},
156
157                 { &hf_idp_len,
158                     { "Length",         "idp.len", FT_UINT16, BASE_DEC,
159                         NULL, 0x0, "", HFILL }},
160
161                 /* XXX - does this have separate hop count and time subfields? */
162                 { &hf_idp_hops,
163                     { "Transport Control (Hops)", "idp.hops", FT_UINT8, BASE_DEC,
164                         NULL, 0x0, "", HFILL }},
165
166                 { &hf_idp_packet_type,
167                     { "Packet Type",    "idp.packet_type", FT_UINT8, BASE_DEC,
168                         VALS(idp_packet_type_vals), 0x0, "", HFILL }},
169
170                 { &hf_idp_dnet,
171                     { "Destination Network","idp.dst.net", FT_UINT32, BASE_HEX,
172                         NULL, 0x0, "", HFILL }},
173
174                 { &hf_idp_dnode,
175                     { "Destination Node",       "idp.dst.node", FT_ETHER, BASE_NONE,
176                         NULL, 0x0, "", HFILL }},
177
178                 { &hf_idp_dsocket,
179                     { "Destination Socket",     "idp.dst.socket", FT_UINT16, BASE_HEX,
180                         VALS(idp_socket_vals), 0x0, "", HFILL }},
181
182                 { &hf_idp_snet,
183                     { "Source Network","idp.src.net", FT_UINT32, BASE_HEX,
184                         NULL, 0x0, "", HFILL }},
185
186                 { &hf_idp_snode,
187                     { "Source Node",    "idp.src.node", FT_ETHER, BASE_NONE,
188                         NULL, 0x0, "", HFILL }},
189
190                 { &hf_idp_ssocket,
191                     { "Source Socket",  "idp.src.socket", FT_UINT16, BASE_HEX,
192                         VALS(idp_socket_vals), 0x0, "", HFILL }},
193         };
194
195         static gint *ett[] = {
196                 &ett_idp,
197         };
198
199         proto_idp = proto_register_protocol("Internetwork Datagram Protocol",
200             "IDP", "idp");
201         proto_register_field_array(proto_idp, hf_idp, array_length(hf_idp));
202         proto_register_subtree_array(ett, array_length(ett));
203
204         idp_type_dissector_table = register_dissector_table("idp.packet_type",
205             "IDP packet type", FT_UINT8, BASE_DEC);
206 }
207
208 void
209 proto_reg_handoff_idp(void)
210 {
211         dissector_handle_t idp_handle;
212
213         idp_handle = create_dissector_handle(dissect_idp, proto_idp);
214         dissector_add("ethertype", ETHERTYPE_XNS_IDP, idp_handle);
215         dissector_add("chdlctype", ETHERTYPE_XNS_IDP, idp_handle);
216
217         data_handle = find_dissector("data");
218 }