Minor proto_reg_handoff cleanup: use find_dissector when appropriate.
[obnox/wireshark/wip.git] / epan / dissectors / packet-epmd.c
1 /* packet-epmd.c
2  * dissector for EPMD (Erlang Port Mapper Daemon) messages;
3  * this are the messages sent between Erlang nodes and
4  * the empd process.
5  * The message formats are derived from the
6  * lib/kernel/src/erl_epmd.* files as part of the Erlang
7  * distribution available from http://www.erlang.org/
8  *
9  * (c) 2007 Joost Yervante Damad <joost[AT]teluna.org>
10  *
11  * $Id$
12  *
13  * Wireshark - Network traffic analyzer
14  * By Gerald Combs <gerald[AT]wireshark.org>
15  * Copyright 1998 Gerald Combs
16  *
17  * Copied from packet-time.c
18  *
19  * This program is free software; you can redistribute it and/or
20  * modify it under the terms of the GNU General Public License
21  * as published by the Free Software Foundation; either version 2
22  * of the License, or (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program; if not, write to the Free Software
31  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
32  */
33
34 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include <epan/packet.h>
39 #include <epan/ptvcursor.h>
40
41 static int proto_epmd = -1;
42 static int hf_epmd_len = -1;
43 static int hf_epmd_type = -1;
44 static int hf_epmd_tcp_port = -1;
45 static int hf_epmd_dist_high = -1;
46 static int hf_epmd_dist_low = -1;
47 static int hf_epmd_name_len = -1;
48 static int hf_epmd_name = -1;
49 static int hf_epmd_elen = -1;
50 static int hf_epmd_edata = -1;
51 static int hf_epmd_names = -1;
52 static int hf_epmd_result = -1;
53 static int hf_epmd_creation = -1;
54
55 static gint ett_epmd = -1;
56
57 #define EPMD_PORT 4369
58
59 /* requests */
60 #define EPMD_ALIVE2 'x'
61 #define EPMD_PORT_PLEASE 'p'
62 #define EPMD_PORT_PLEASE2 'z'
63 #define EPMD_NAMES 'n'
64 #define EPMD_ALIVE 'a'
65
66 /* responses */
67 #define EPMD_ALIVE_OK 'Y'
68 #define EPMD_ALIVE2_OK 'y'
69 #define EPMD_PORT_PLEASE2_OK 'w'
70
71 /* unknown; currently not implemented */
72 #define EPMD_DUMP 'd'
73 #define EPMD_KILL 'k'
74 #define EPMD_STOP 's'
75
76 static const value_string message_types[] =
77 {
78         {  EPMD_ALIVE,          "Alive" },
79         {  EPMD_PORT_PLEASE,    "Port Please" },
80         {  EPMD_NAMES,          "Names" },
81         {  EPMD_DUMP,           "Dump" },
82         {  EPMD_KILL,           "Kill" },
83         {  EPMD_STOP,           "Stop" },
84         {  EPMD_ALIVE_OK,       "Alive Ok" },
85         {  EPMD_ALIVE2,         "Alive 2" },
86         {  EPMD_PORT_PLEASE2,   "Port Please 2" },
87         {  EPMD_ALIVE2_OK,      "Alive 2 Ok" },
88         {  EPMD_PORT_PLEASE2_OK, "Port Please 2 Ok" },
89         {  0, NULL }
90 };
91
92 static void
93 dissect_epmd_request(ptvcursor_t *cursor)
94 {
95     tvbuff_t *tvb;
96     guint8 type;
97
98     tvb = ptvcursor_tvbuff(cursor);
99     ptvcursor_add(cursor, hf_epmd_len, 2, FALSE);
100
101     type = tvb_get_guint8(tvb, ptvcursor_current_offset(cursor));
102     ptvcursor_add(cursor, hf_epmd_type, 1, FALSE);
103     switch (type) {
104         case EPMD_ALIVE2: {
105             guint16 name_length, elen;
106             ptvcursor_add(cursor, hf_epmd_tcp_port, 2, FALSE);
107             ptvcursor_advance(cursor,2); /* 'M', 0 */
108             ptvcursor_add(cursor, hf_epmd_dist_high, 2, FALSE);
109             ptvcursor_add(cursor, hf_epmd_dist_low, 2, FALSE);
110             name_length = tvb_get_ntohs(tvb, ptvcursor_current_offset(cursor));
111             ptvcursor_add(cursor, hf_epmd_name_len, 2, FALSE);
112             ptvcursor_add(cursor, hf_epmd_name, name_length, FALSE);
113             elen = tvb_get_ntohs(tvb, ptvcursor_current_offset(cursor));
114             ptvcursor_add(cursor, hf_epmd_elen, 2, FALSE);
115             if (elen > 0)
116                 ptvcursor_add(cursor, hf_epmd_edata, elen, FALSE);
117             break;
118         }
119         case EPMD_PORT_PLEASE:
120         case EPMD_PORT_PLEASE2:
121             /*ptvcursor_add(cursor, hf_epmd_name, tvb_length(tvb)-3, FALSE);*/
122             ptvcursor_add(cursor, hf_epmd_name, -1, FALSE);
123             break;
124         case EPMD_ALIVE: {
125             ptvcursor_add(cursor, hf_epmd_tcp_port, 2, FALSE);
126             /*ptvcursor_add(cursor, hf_epmd_name, tvb_length(tvb)-3, FALSE);*/
127             ptvcursor_add(cursor, hf_epmd_name, -1, FALSE);
128             break;
129         }
130         case EPMD_NAMES:
131         default:
132             break;
133     }
134 }
135
136 static void
137 dissect_epmd_response_names(ptvcursor_t *cursor)
138 {
139     ptvcursor_add(cursor, hf_epmd_tcp_port, 2, FALSE);
140     ptvcursor_add(cursor, hf_epmd_names, -1, FALSE);
141     /* TODO: parse names */
142 }
143
144 static void
145 dissect_epmd_response(ptvcursor_t *cursor)
146 {
147     tvbuff_t *tvb;
148     guint32 port;
149     guint8 type;
150
151     tvb = ptvcursor_tvbuff(cursor);
152     port = tvb_get_ntohl(tvb, 0);
153     if (port == EPMD_PORT) {
154         dissect_epmd_response_names(cursor);
155         return;
156     }
157
158     type = tvb_get_guint8(tvb, 0);
159     ptvcursor_add(cursor, hf_epmd_type, 1, FALSE);
160     switch (type) {
161         case EPMD_PORT_PLEASE2_OK: {
162             ptvcursor_advance(cursor, 1);
163 /* 'w', 0, Port(16), Type(8), Proto(8), High(16), Low(16), NLen(16), Name(x) */
164             ptvcursor_add(cursor, hf_epmd_tcp_port, 2, FALSE);
165             ptvcursor_advance(cursor, 2); /* 'M', 0 */
166             ptvcursor_add(cursor, hf_epmd_dist_high, 2, FALSE);
167             ptvcursor_add(cursor, hf_epmd_dist_low, 2, FALSE);
168             ptvcursor_add(cursor, hf_epmd_name_len, 2, FALSE);
169             ptvcursor_add(cursor, hf_epmd_name, -1, FALSE);
170         }
171         case EPMD_ALIVE_OK:
172         case EPMD_ALIVE2_OK: {
173             ptvcursor_add(cursor, hf_epmd_result, 1, FALSE);
174             ptvcursor_add(cursor, hf_epmd_creation, 2, FALSE);
175         }
176         default:
177             break;
178     }
179 }
180
181 static gboolean
182 check_epmd(tvbuff_t *tvb)
183 {
184     guint8 type;
185
186     /* simple heuristic:
187      *
188      * just check if the type is one of the EPMD
189      * command types
190      *
191      * It's possible to start checking lengths but imho that
192      * doesn't bring very much.
193      */
194     if (tvb_length(tvb) < 3)
195         return(FALSE);
196
197     type = tvb_get_guint8(tvb, 0);
198     switch (type) {
199         case EPMD_ALIVE_OK:
200         case EPMD_ALIVE2_OK:
201         case EPMD_PORT_PLEASE2_OK:
202             return(TRUE);
203         default:
204             break;
205     }
206
207     type = tvb_get_guint8(tvb, 2);
208     switch (type) {
209         case EPMD_ALIVE2:
210         case EPMD_PORT_PLEASE:
211         case EPMD_PORT_PLEASE2:
212         case EPMD_NAMES:
213         case EPMD_ALIVE:
214             return( TRUE);
215         default:
216             break;
217     }
218
219     return(FALSE);
220 }
221
222 static int
223 dissect_epmd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
224 {
225     proto_tree *epmd_tree;
226     proto_item *ti;
227     ptvcursor_t *cursor;
228
229     if (!check_epmd(tvb))
230         return(0);
231
232     if (check_col(pinfo->cinfo, COL_PROTOCOL))
233         col_set_str(pinfo->cinfo, COL_PROTOCOL, "EPMD");
234
235     if (tree) {
236         ti = proto_tree_add_item(tree, proto_epmd, tvb, 0, -1, FALSE);
237         epmd_tree = proto_item_add_subtree(ti, ett_epmd);
238         cursor = ptvcursor_new(epmd_tree, tvb, 0);
239
240         if (pinfo->srcport==EPMD_PORT) {
241             dissect_epmd_response(cursor);
242         } else {
243             dissect_epmd_request(cursor);
244         }
245
246         ptvcursor_free(cursor);
247     }
248
249     return(tvb_length(tvb));
250 }
251
252 void
253 proto_register_epmd(void)
254 {
255     static hf_register_info hf[] = {
256         { &hf_epmd_len,
257             {   "Length", "epmd.len",
258                 FT_UINT16, BASE_DEC, NULL, 0x0,
259                 "Message Length", HFILL }},
260         { &hf_epmd_type,
261             { "Type", "epmd.type",
262                 FT_UINT8, BASE_DEC, VALS(message_types), 0x0,
263                 "Message Type", HFILL }},
264         { &hf_epmd_result,
265             { "Result", "epmd.result",
266                 FT_UINT8, BASE_DEC, NULL, 0x0,
267                 "Result", HFILL }},
268         { &hf_epmd_tcp_port,
269             { "TCP Port", "epmd.tcp_port",
270                 FT_UINT16, BASE_DEC, NULL, 0x0,
271                 "TCP Port", HFILL }},
272         { &hf_epmd_creation,
273             { "Creation", "epmd.creation",
274                 FT_UINT16, BASE_DEC, NULL, 0x0,
275                 "Creation", HFILL }},
276         { &hf_epmd_dist_high,
277             { "Dist High", "epmd.dist_high",
278                 FT_UINT16, BASE_DEC, NULL, 0x0,
279                 "Dist High", HFILL }},
280         { &hf_epmd_dist_low,
281             { "Dist Low", "epmd.dist_low",
282                 FT_UINT16, BASE_DEC, NULL, 0x0,
283                 "Dist Low", HFILL }},
284         { &hf_epmd_name_len,
285             { "Name Length", "epmd.name_len",
286                 FT_UINT16, BASE_DEC, NULL, 0x0,
287                 "Name Length", HFILL }},
288         { &hf_epmd_name,
289             { "Name", "epmd.name",
290                 FT_STRING, BASE_DEC, NULL, 0x0,
291                 "Name", HFILL }},
292         { &hf_epmd_elen,
293             { "Elen", "epmd.elen",
294                 FT_UINT16, BASE_DEC, NULL, 0x0,
295                 "Extra Length", HFILL }},
296         { &hf_epmd_edata,
297             { "Edata", "epmd.edata",
298                 FT_BYTES, BASE_DEC, NULL, 0x0,
299                 "Extra Data", HFILL }},
300         { &hf_epmd_names,
301             { "Names", "epmd.names",
302                 FT_BYTES, BASE_DEC, NULL, 0x0,
303                 "List of names", HFILL }}
304     };
305     static gint *ett[] = {
306         &ett_epmd,
307     };
308
309     proto_epmd = proto_register_protocol("EPMD Protocol", "EPMD", "epmd");
310     proto_register_field_array(proto_epmd, hf, array_length(hf));
311     proto_register_subtree_array(ett, array_length(ett));
312     new_register_dissector("epmd", dissect_epmd, proto_epmd);
313 }
314
315 void
316 proto_reg_handoff_epmd(void)
317 {
318     dissector_handle_t epmd_handle;
319     epmd_handle = find_dissector("epmd");
320     dissector_add("tcp.port", EPMD_PORT, epmd_handle);
321 }