2 * Routines for XDMCP message dissection
3 * Copyright 2002, Pasi Eronen <pasi.eronen@nixu.com>
5 * $Id: packet-xdmcp.c,v 1.1 2002/03/16 02:25:48 guy Exp $
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@ethereal.com>
9 * Copyright 1998 Gerald Combs
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.
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.
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.
36 #include <epan/packet.h>
38 #define UDP_PORT_XDMCP 177
40 #define XDMCP_PROTOCOL_VERSION 1
42 #define XDMCP_BROADCAST_QUERY 1
44 #define XDMCP_INDIRECT_QUERY 3
45 #define XDMCP_FORWARD_QUERY 4
46 #define XDMCP_WILLING 5
47 #define XDMCP_UNWILLING 6
48 #define XDMCP_REQUEST 7
49 #define XDMCP_ACCEPT 8
50 #define XDMCP_DECLINE 9
51 #define XDMCP_MANAGE 10
52 #define XDMCP_REFUSE 11
53 #define XDMCP_FAILED 12
54 #define XDMCP_KEEPALIVE 13
55 #define XDMCP_ALIVE 14
57 static const value_string opcode_vals[] = {
58 { XDMCP_BROADCAST_QUERY, "Broadcast_query" },
59 { XDMCP_QUERY, "Query" },
60 { XDMCP_INDIRECT_QUERY, "Indirect_query" },
61 { XDMCP_FORWARD_QUERY, "Forward_query" },
62 { XDMCP_WILLING, "Willing" },
63 { XDMCP_UNWILLING, "Unwilling" },
64 { XDMCP_REQUEST, "Request" },
65 { XDMCP_ACCEPT, "Accept "},
66 { XDMCP_DECLINE, "Decline" },
67 { XDMCP_MANAGE, "Manage" },
68 { XDMCP_REFUSE, "Refuse" },
69 { XDMCP_FAILED, "Failed" },
70 { XDMCP_KEEPALIVE, "Keepalive" },
71 { XDMCP_ALIVE, "Alive" },
75 /* Copied from packet-x11.c */
76 static const value_string family_vals[] = {
83 static gint proto_xdmcp = -1;
84 static gint hf_xdmcp_version = -1;
85 static gint hf_xdmcp_opcode = -1;
86 static gint hf_xdmcp_length = -1;
87 static gint hf_xdmcp_authentication_name = -1;
88 static gint hf_xdmcp_authorization_name = -1;
89 static gint hf_xdmcp_hostname = -1;
90 static gint hf_xdmcp_status = -1;
91 static gint hf_xdmcp_session_id = -1;
92 static gint hf_xdmcp_display_number = -1;
94 static gint ett_xdmcp = -1;
95 static gint ett_xdmcp_authorization_names = -1;
96 static gint ett_xdmcp_connections = -1;
97 static gint ett_xdmcp_connection = -1;
99 /* Copied from packet-x11.c */
100 static void stringCopy(char *dest, const char *source, int length)
105 if (!isgraph(c) && c != ' ') c = '.';
111 static gint xdmcp_add_string(proto_tree *tree, gint hf,
112 tvbuff_t *tvb, gint offset)
117 len = tvb_get_ntohs(tvb, offset);
118 str = g_malloc(len+1);
119 stringCopy(str, tvb_get_ptr(tvb, offset+2, len), len);
120 proto_tree_add_string(tree, hf, tvb, offset, len+2, str);
126 static gint xdmcp_add_text(proto_tree *tree, const char *text,
127 tvbuff_t *tvb, gint offset)
132 len = tvb_get_ntohs(tvb, offset);
133 str = g_malloc(len+1);
134 stringCopy(str, tvb_get_ptr(tvb, offset+2, len), len);
135 proto_tree_add_text(tree, tvb, offset, len+2, "%s: %s", text, str);
141 static gint xdmcp_add_bytes(proto_tree *tree, const char *text,
142 tvbuff_t *tvb, gint offset)
145 len = tvb_get_ntohs(tvb, offset);
146 proto_tree_add_text(tree, tvb, offset, len+2,
147 "%s (%u byte%s)", text, len, plurality(len, "", "s"));
151 static gint xdmcp_add_authorization_names(proto_tree *tree,
152 tvbuff_t *tvb, gint offset)
154 proto_tree *anames_tree;
155 proto_item *anames_ti;
156 gint anames_len, anames_start_offset;
158 anames_start_offset = offset;
159 anames_len = tvb_get_guint8(tvb, offset);
160 anames_ti = proto_tree_add_text(tree, tvb,
161 anames_start_offset, -1,
162 "Authorization names (%d)",
164 anames_tree = proto_item_add_subtree(anames_ti,
165 ett_xdmcp_authorization_names);
167 anames_len = tvb_get_guint8(tvb, offset);
169 while (anames_len > 0) {
170 offset += xdmcp_add_string(anames_tree, hf_xdmcp_authorization_name,
174 proto_item_set_len(anames_ti, offset - anames_start_offset);
175 return offset - anames_start_offset;
179 * I didn't find any documentation for the XDMCP protocol, so
180 * this is reverse-engineered from XFree86 source files
181 * xc/programs/xdm/xdmcp.c and xc/programs/Xserver/os/xdmcp.c.
184 static void dissect_xdmcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
186 gint version = -1, opcode = -1;
189 proto_tree *xdmcp_tree = 0;
191 version = tvb_get_ntohs(tvb, offset);
192 if (version != XDMCP_PROTOCOL_VERSION) {
193 /* Only version 1 exists, so this probably is not XDMCP at all... */
197 if (check_col(pinfo->cinfo, COL_PROTOCOL))
198 col_set_str(pinfo->cinfo, COL_PROTOCOL, "XDMCP");
199 if (check_col(pinfo->cinfo, COL_INFO))
200 col_clear(pinfo->cinfo, COL_INFO);
203 ti = proto_tree_add_item(tree, proto_xdmcp, tvb, offset, -1, FALSE);
204 xdmcp_tree = proto_item_add_subtree(ti, ett_xdmcp);
206 proto_tree_add_uint(xdmcp_tree, hf_xdmcp_version, tvb,
211 opcode = tvb_get_ntohs(tvb, offset);
213 proto_tree_add_uint(xdmcp_tree, hf_xdmcp_opcode, tvb,
217 if (check_col(pinfo->cinfo, COL_INFO)) {
218 col_add_fstr(pinfo->cinfo, COL_INFO, "%s",
219 val_to_str(opcode, opcode_vals, "Unknown (0x%04x)"));
224 proto_tree_add_item(xdmcp_tree, hf_xdmcp_length, tvb,
229 case XDMCP_FORWARD_QUERY:
232 alen = tvb_get_ntohs(tvb, offset);
233 /* I have never seen anything except IPv4 addresses here,
234 * but in theory the protocol should support other address
237 proto_tree_add_text(xdmcp_tree, tvb, offset, alen+2,
238 "Client address: %s",
239 ip_to_str(tvb_get_ptr(tvb, offset+2, 4)));
242 offset += xdmcp_add_bytes(xdmcp_tree, "Client address", tvb, offset);
245 plen = tvb_get_ntohs(tvb, offset);
247 proto_tree_add_text(xdmcp_tree, tvb, offset, plen+2,
249 tvb_get_ntohs(tvb, offset+2));
252 offset += xdmcp_add_bytes(xdmcp_tree, "Client port", tvb, offset);
257 case XDMCP_BROADCAST_QUERY:
259 case XDMCP_INDIRECT_QUERY:
260 offset += xdmcp_add_authorization_names(xdmcp_tree, tvb, offset);
264 offset += xdmcp_add_string(xdmcp_tree, hf_xdmcp_authentication_name,
266 offset += xdmcp_add_string(xdmcp_tree, hf_xdmcp_hostname,
268 offset += xdmcp_add_string(xdmcp_tree, hf_xdmcp_status,
272 case XDMCP_UNWILLING:
273 offset += xdmcp_add_string(xdmcp_tree, hf_xdmcp_hostname,
275 offset += xdmcp_add_string(xdmcp_tree, hf_xdmcp_status,
281 proto_tree *clist_tree;
282 proto_item *clist_ti;
283 gint ctypes_len, caddrs_len, n;
284 gint ctypes_start_offset, caddrs_offset;
286 proto_tree_add_item(xdmcp_tree, hf_xdmcp_display_number, tvb,
290 ctypes_len = tvb_get_guint8(tvb, offset);
291 ctypes_start_offset = offset;
292 caddrs_offset = offset + 1 + 2*ctypes_len;
293 caddrs_len = tvb_get_guint8(tvb, caddrs_offset);
294 if (ctypes_len != caddrs_len) {
295 proto_tree_add_text(xdmcp_tree, NULL, 0, 0,
296 "Error: Connection type/address arrays don't match");
300 clist_ti = proto_tree_add_text(xdmcp_tree,
301 tvb, ctypes_start_offset, -1,
304 clist_tree = proto_item_add_subtree(clist_ti, ett_xdmcp_connections);
310 while (ctypes_len > 0) {
311 proto_item *connection_ti;
312 proto_tree *connection_tree;
313 const char *ip_string;
316 gint ctype = tvb_get_ntohs(tvb, offset);
318 alen = tvb_get_ntohs(tvb, caddrs_offset);
321 if ((ctype == 0) && (alen == 4)) {
322 ip_string = ip_to_str(tvb_get_ptr(tvb, caddrs_offset, 4));
327 connection_ti = proto_tree_add_text(clist_tree, NULL, 0, 0,
328 "Connection %d%s%s", n,
329 (ip_string ? ": " : ""),
330 (ip_string ? ip_string : ""));
331 connection_tree = proto_item_add_subtree(connection_ti,
332 ett_xdmcp_connection);
334 proto_tree_add_text(connection_tree, tvb, offset-2, 2,
336 val_to_str(ctype, family_vals,
337 "Unknown (0x%04x)"));
338 if ((ctype == 0) && (alen == 4)) {
339 proto_tree_add_text(connection_tree, tvb, caddrs_offset-2, alen+2,
340 "Address: %s", ip_string);
342 proto_tree_add_text(connection_tree, tvb, caddrs_offset-2, alen+2,
343 "Address: (%u byte%s)", alen,
344 plurality(alen, "", "s"));
346 caddrs_offset += alen;
350 offset = caddrs_offset;
351 proto_item_set_len(clist_ti, offset - ctypes_start_offset);
353 offset += xdmcp_add_string(xdmcp_tree, hf_xdmcp_authentication_name,
355 offset += xdmcp_add_bytes(xdmcp_tree, "Authentication data",
358 offset += xdmcp_add_authorization_names(xdmcp_tree, tvb, offset);
360 offset += xdmcp_add_text(xdmcp_tree, "Manufacturer display ID",
366 proto_tree_add_item(xdmcp_tree, hf_xdmcp_session_id, tvb,
369 offset += xdmcp_add_string(xdmcp_tree, hf_xdmcp_authentication_name,
371 offset += xdmcp_add_bytes(xdmcp_tree, "Authentication data",
373 offset += xdmcp_add_string(xdmcp_tree, hf_xdmcp_authorization_name,
375 offset += xdmcp_add_bytes(xdmcp_tree, "Authorization data",
380 offset += xdmcp_add_string(xdmcp_tree, hf_xdmcp_status,
382 offset += xdmcp_add_string(xdmcp_tree, hf_xdmcp_authentication_name,
384 offset += xdmcp_add_bytes(xdmcp_tree, "Authentication data",
389 proto_tree_add_item(xdmcp_tree, hf_xdmcp_session_id, tvb,
393 proto_tree_add_item(xdmcp_tree, hf_xdmcp_display_number, tvb,
397 offset += xdmcp_add_text(xdmcp_tree, "Display class",
402 proto_tree_add_item(xdmcp_tree, hf_xdmcp_session_id, tvb,
408 proto_tree_add_item(xdmcp_tree, hf_xdmcp_session_id, tvb,
412 offset += xdmcp_add_string(xdmcp_tree, hf_xdmcp_status,
416 case XDMCP_KEEPALIVE:
417 proto_tree_add_item(xdmcp_tree, hf_xdmcp_display_number, tvb,
421 proto_tree_add_item(xdmcp_tree, hf_xdmcp_session_id, tvb,
427 proto_tree_add_text(xdmcp_tree, tvb, offset, 1,
428 "Session running: %s",
429 (tvb_get_guint8(tvb, offset) ? "Yes" : "No"));
432 proto_tree_add_item(xdmcp_tree, hf_xdmcp_session_id, tvb,
440 /* Register the protocol with Ethereal */
441 void proto_register_xdmcp(void)
443 /* Setup list of header fields */
444 static hf_register_info hf[] = {
446 { "Version", "xdmcp.version",
447 FT_UINT16, BASE_DEC, NULL, 0,
448 "Protocol version", HFILL }
451 { "Opcode", "xdmcp.opcode",
452 FT_UINT16, BASE_HEX, VALS(opcode_vals), 0,
456 { "Message length", "xdmcp.length",
457 FT_UINT16, BASE_DEC, NULL, 0,
458 "Length of the remaining message", HFILL }
460 { &hf_xdmcp_authentication_name,
461 { "Authentication name", "xdmcp.authentication_name",
462 FT_STRING, BASE_DEC, NULL, 0,
463 "Authentication name", HFILL }
465 { &hf_xdmcp_authorization_name,
466 { "Authorization name", "xdmcp.authorization_name",
467 FT_STRING, BASE_DEC, NULL, 0,
468 "Authorization name", HFILL }
470 { &hf_xdmcp_hostname,
471 { "Hostname", "xdmcp.hostname",
472 FT_STRING, BASE_DEC, NULL, 0,
476 { "Status", "xdmcp.status",
477 FT_STRING, BASE_DEC, NULL, 0,
480 { &hf_xdmcp_session_id,
481 { "Session ID", "xdmcp.session_id",
482 FT_UINT32, BASE_HEX, NULL, 0,
483 "Session identifier", HFILL }
485 { &hf_xdmcp_display_number,
486 { "Display number", "xdmcp.display_number",
487 FT_UINT16, BASE_DEC, NULL, 0,
488 "Display number", HFILL }
492 /* Setup protocol subtree array */
493 static gint *ett[] = {
495 &ett_xdmcp_authorization_names,
496 &ett_xdmcp_connections,
497 &ett_xdmcp_connection
500 /* Register the protocol name and description */
501 proto_xdmcp = proto_register_protocol("X Display Manager Control Protocol",
504 /* Required function calls to register the header fields and subtrees used */
505 proto_register_field_array(proto_xdmcp, hf, array_length(hf));
506 proto_register_subtree_array(ett, array_length(ett));
510 proto_reg_handoff_xdmcp(void)
512 dissector_handle_t xdmcp_handle;
514 xdmcp_handle = create_dissector_handle(dissect_xdmcp, proto_xdmcp);
515 dissector_add("udp.port", UDP_PORT_XDMCP, xdmcp_handle);