Remove unneeded #include <stdio.h>
[obnox/wireshark/wip.git] / epan / dissectors / packet-ayiya.c
1 /* packet-ayiya.c
2  * Anything in Anything protocol
3  * Copyright 2008, Jelmer Vernooij <jelmer@samba.org>
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  * ref: http://unfix.org/~jeroen/archive/drafts/draft-massar-v6ops-ayiya-02.html#anchor4
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <string.h>
33 #include <glib.h>
34 #include <epan/packet.h>
35 #include <epan/ipproto.h>
36 #include <epan/strutil.h>
37
38 static dissector_table_t ip_dissector_table;
39
40 static int proto_ayiya = -1;
41 static int hf_id_len = -1;
42 static int hf_id_type = -1;
43 static int hf_sig_len = -1;
44 static int hf_hash_method = -1;
45 static int hf_auth_method = -1;
46 static int hf_opcode = -1;
47 static int hf_next_header = -1;
48 static int hf_epoch = -1;
49 static int hf_identity = -1;
50 static int hf_signature = -1;
51
52 static gint ett_ayiya = -1;
53
54 static const value_string identity_types[] = {
55         { 0x0, "None" },
56         { 0x1, "Integer" },
57         { 0x2, "ASCII string" },
58         { 0, NULL }
59 };
60
61 static const value_string hash_methods[] = {
62         { 0x0, "No hash" },
63         { 0x1, "MD5" },
64         { 0x2, "SHA1" },
65         { 0, NULL }
66 };
67
68 static const value_string auth_methods[] = {
69         { 0x0, "No authentication" },
70         { 0x1, "Hash using a Shared Secret" },
71         { 0x2, "Hash using a public/private key method" },
72         { 0, NULL }
73 };
74
75 #define OPCODE_FORWARD 1
76
77 static const value_string opcodes[] = {
78         { 0x0, "No Operation / Heartbeat" },
79         { 0x1, "Forward" },
80         { 0x2, "Echo Request" },
81         { 0x3, "Echo Request and Forward" },
82         { 0x4, "Echo Response" },
83         { 0x5, "MOTD" },
84         { 0x6, "Query Request" },
85         { 0x7, "Query Response" },
86         { 0, NULL }
87 };
88
89 #define UDP_PORT_AYIYA                  5072
90
91 static void
92 dissect_ayiya(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
93 {
94         proto_tree *ayiya_tree;
95         int offset = 0;
96         int idlen, siglen, ayiya_len;
97         guint8 next_header, opcode;
98         tvbuff_t *payload;
99
100         idlen = 1 << tvb_get_bits8(tvb, 0, 4);
101         siglen = tvb_get_bits8(tvb, 8, 4) * 4;
102         opcode = tvb_get_bits8(tvb, 20, 4);
103         next_header = tvb_get_guint8(tvb, 3);
104
105         ayiya_len = 8+idlen+siglen;
106
107         col_set_str(pinfo->cinfo, COL_PROTOCOL, "AYIYA");
108
109         if (tree) {
110                 proto_item *ti;
111                 nstime_t tv;
112                 ti = proto_tree_add_protocol_format( tree, proto_ayiya, tvb,
113                                                                                          offset, ayiya_len, "AYIYA" );
114                 ayiya_tree = proto_item_add_subtree(ti, ett_ayiya);
115
116                 proto_tree_add_bits_item(ayiya_tree, hf_id_len, tvb, 0, 4, FALSE);
117                 proto_tree_add_bits_item(ayiya_tree, hf_id_type, tvb, 4, 4, FALSE);
118                 proto_tree_add_bits_item(ayiya_tree, hf_sig_len, tvb, 8, 4, FALSE);
119                 proto_tree_add_bits_item(ayiya_tree, hf_hash_method, tvb, 12, 4, FALSE);
120                 proto_tree_add_bits_item(ayiya_tree, hf_auth_method, tvb, 16, 4, FALSE);
121                 proto_tree_add_bits_item(ayiya_tree, hf_opcode, tvb, 20, 4, FALSE);
122                 proto_tree_add_uint_format(ayiya_tree, hf_next_header, tvb,
123                                                                    3, 1, next_header, 
124                                                                    "Next header: %s (0x%02x)", 
125                                                                    ipprotostr(next_header), next_header);
126                 tv.secs = tvb_get_ntohl(tvb, 4);
127                 tv.nsecs = 0;
128                 proto_tree_add_time(ayiya_tree, hf_epoch, tvb, 4, 4, &tv);
129                 proto_tree_add_item(ayiya_tree, hf_identity, tvb, 8, idlen, FALSE);
130                 proto_tree_add_item(ayiya_tree, hf_signature, tvb, 8+idlen, siglen, FALSE);
131         }
132         offset = ayiya_len;
133         switch (opcode) {
134         case OPCODE_FORWARD:
135                 payload = tvb_new_subset_remaining(tvb, offset);
136                 dissector_try_port(ip_dissector_table, next_header, payload, pinfo, tree);
137                 break;
138         }
139 }
140
141 void
142 proto_register_ayiya(void)
143 {
144   static hf_register_info hf[] = {
145         { &hf_id_len,
146                 { "Identity field length", "ayiya.idlen", FT_UINT8, 
147                         BASE_HEX, NULL, 0x0, NULL, HFILL
148                 }
149         },
150         { &hf_id_type,
151                 { "Identity field type", "ayiya.idtype", FT_UINT8, 
152                         BASE_HEX, VALS(identity_types), 0x0, NULL, HFILL
153                 }
154         },
155         { &hf_sig_len,
156                 { "Signature Length", "ayiya.siglen", FT_UINT8, 
157                         BASE_HEX, NULL, 0x0, NULL, HFILL
158                 }
159         },
160         { &hf_hash_method,
161                 { "Hash method", "ayiya.hashmethod", FT_UINT8, 
162                         BASE_HEX, VALS(hash_methods), 0x0, NULL, HFILL
163                 }
164         },
165         { &hf_auth_method,
166                 { "Authentication method", "ayiya.authmethod", FT_UINT8, 
167                         BASE_HEX, VALS(auth_methods), 0x0, NULL, HFILL
168                 }
169         },
170         { &hf_opcode,
171                 { "Operation Code", "ayiya.opcode", FT_UINT8, 
172                         BASE_HEX, VALS(opcodes), 0x0, NULL, HFILL
173                 }
174         },
175         { &hf_next_header,
176                 { "Next Header", "ayiya.nextheader", FT_UINT8, 
177                         BASE_HEX, NULL, 0x0, NULL, HFILL
178                 }
179         },
180         { &hf_epoch,
181                 { "Epoch", "ayiya.epoch", FT_ABSOLUTE_TIME, 
182                         ABSOLUTE_TIME_LOCAL, NULL, 0x0, NULL, HFILL
183                 }
184         },
185         { &hf_identity,
186                 { "Identity", "ayiya.identity", FT_BYTES, 
187                         BASE_NONE, NULL, 0x0, NULL, HFILL
188                 }
189         },
190         { &hf_signature,
191                 { "Signature", "ayiya.signature", FT_BYTES, 
192                         BASE_NONE, NULL, 0x0, NULL, HFILL
193                 }
194         },
195   };
196   static gint *ett[] = {
197     &ett_ayiya,
198   };
199
200   proto_ayiya = proto_register_protocol("Anything in Anything Protocol",
201                                        "AYIYA", "ayiya");
202   register_dissector("ayiya", dissect_ayiya, proto_ayiya);
203   proto_register_field_array(proto_ayiya, hf, array_length(hf));
204   proto_register_subtree_array(ett, array_length(ett));
205 }
206
207 void
208 proto_reg_handoff_ayiya(void)
209 {
210   dissector_handle_t ayiya_handle;
211
212   ayiya_handle = find_dissector("ayiya");
213   dissector_add("udp.port", UDP_PORT_AYIYA, ayiya_handle);
214   
215   ip_dissector_table = find_dissector_table("ip.proto");
216 }