As with "file_write_error_message()", so with
[obnox/wireshark/wip.git] / packet-xyplex.c
1 /* packet-xyplex.c
2  * Routines for xyplex packet dissection
3  *
4  * Copyright 2002 Randy McEoin <rmceoin@pe.com>
5  *
6  * $Id: packet-xyplex.c,v 1.2 2002/08/28 21:00:40 jmayer Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  *
12  * Copied from packet-tftp.c
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <glib.h>
34 #include <epan/packet.h>
35 #include <epan/conversation.h>
36
37 static int proto_xyplex = -1;
38 static int hf_xyplex_type = -1;
39 static int hf_xyplex_pad = -1;
40 static int hf_xyplex_server_port = -1;
41 static int hf_xyplex_return_port = -1;
42 static int hf_xyplex_reserved = -1;
43 static int hf_xyplex_reply = -1;
44
45 static gint ett_xyplex = -1;
46
47 static dissector_handle_t xyplex_handle;
48
49 #define UDP_PORT_XYPLEX    173
50
51 #define XYPLEX_REG_OK           0x00
52 #define XYPLEX_REG_QUEFULL      0x05
53
54 static const value_string xyplex_reg_vals[] = {
55   { XYPLEX_REG_OK,      "OK" },
56   { XYPLEX_REG_QUEFULL, "Queue Full" },
57   { 0,          NULL }
58 };
59
60 static void
61 dissect_xyplex(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
62 {
63         proto_tree      *xyplex_tree = NULL;
64         proto_item      *ti;
65         conversation_t  *conversation;
66         gint            offset = 0;
67
68         guint8          prototype;
69         guint8          padding;
70         guint16         server_port;
71         guint16         return_port;
72         guint16         reserved;
73         guint16         reply;
74
75         if (check_col(pinfo->cinfo, COL_PROTOCOL))
76                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "XYPLEX");
77
78         if (tree) {
79           ti = proto_tree_add_item(tree, proto_xyplex, tvb, offset, -1, FALSE);
80           xyplex_tree = proto_item_add_subtree(ti, ett_xyplex);
81         }
82
83         if (pinfo->destport == UDP_PORT_XYPLEX) {
84                 /* This is a registration request from a Unix server
85                  * to the Xyplex server.  The server_port indicates
86                  * which Xyplex serial port is desired.  The
87                  * return_port tells the Xyplex server what TCP port
88                  * to open to the Unix server.
89                  */
90                 prototype = tvb_get_guint8(tvb, offset);
91                 padding = tvb_get_guint8(tvb, offset+1);
92                 server_port = tvb_get_ntohs(tvb, offset+2);
93                 return_port = tvb_get_ntohs(tvb, offset+4);
94                 reserved = tvb_get_ntohs(tvb, offset+6);
95                 if (check_col(pinfo->cinfo, COL_INFO)) {
96                   col_add_fstr(pinfo->cinfo, COL_INFO,
97                           "Registration Request: %d Return: %d",
98                           server_port, return_port);
99                 }
100                 if (tree) {
101                   proto_tree_add_uint(xyplex_tree, hf_xyplex_type, tvb,
102                                     offset, 1, prototype);
103                   proto_tree_add_uint(xyplex_tree, hf_xyplex_pad, tvb,
104                                     offset+1, 1, padding);
105                   proto_tree_add_uint(xyplex_tree, hf_xyplex_server_port, tvb,
106                                     offset+2, 2, server_port);
107                   proto_tree_add_uint(xyplex_tree, hf_xyplex_return_port, tvb,
108                                     offset+4, 2, return_port);
109                   proto_tree_add_uint(xyplex_tree, hf_xyplex_reserved, tvb,
110                                     offset+6, 2, reserved);
111                 }
112                 offset += 8;
113
114                 /* Look for all future TCP conversations between the
115                  * requestiong server and the Xyplex host using the
116                  * return_port.
117                  */
118                 conversation = find_conversation(&pinfo->src, &pinfo->dst,
119                                   PT_TCP, return_port, 0, NO_PORT_B);
120                 if (conversation == NULL) {
121                     conversation = conversation_new(&pinfo->src, &pinfo->dst,
122                                     PT_TCP, return_port, 0, NO_PORT2);
123                     conversation_set_dissector(conversation, xyplex_handle);
124                 }
125                 return;
126         }
127
128         if (pinfo->srcport == UDP_PORT_XYPLEX) {
129                 prototype = tvb_get_guint8(tvb, offset);
130                 padding = tvb_get_guint8(tvb, offset+1);
131                 reply = tvb_get_ntohs(tvb, offset+2);
132                 if (check_col(pinfo->cinfo, COL_INFO)) {
133                   col_add_fstr(pinfo->cinfo, COL_INFO, "Registration Reply: %s",
134                         val_to_str(reply, xyplex_reg_vals, "Unknown (0x%02x)"));
135                 }
136                 if (tree) {
137                   proto_tree_add_uint(xyplex_tree, hf_xyplex_type, tvb,
138                                     offset, 1, prototype);
139                   proto_tree_add_uint(xyplex_tree, hf_xyplex_pad, tvb,
140                                     offset+1, 1, padding);
141                   proto_tree_add_uint(xyplex_tree, hf_xyplex_reply, tvb,
142                                     offset+2, 2, reply);
143                 }
144                 offset += 4;
145                 return;
146         }
147
148         /*
149          * This must be the TCP data stream.  This will just be
150          * the raw data being transfered from the remote server
151          * and the Xyplex serial port.
152          */
153         if (check_col(pinfo->cinfo, COL_INFO)) {
154           col_add_fstr(pinfo->cinfo, COL_INFO, "%d > %d Data",
155                   pinfo->srcport, pinfo->destport);
156         }
157         if (tree) {
158           proto_tree_add_text(xyplex_tree, tvb, offset, -1,
159                 "Data (%d bytes)", tvb_reported_length_remaining(tvb, offset));
160         }
161 }
162
163
164 void
165 proto_register_xyplex(void)
166 {
167   static hf_register_info hf[] = {
168     { &hf_xyplex_type,
169       { "Type",       "xyplex.type",
170         FT_UINT8, BASE_DEC, NULL, 0x0,
171         "Protocol type", HFILL }},
172
173     { &hf_xyplex_pad,
174       { "Pad",        "xyplex.pad",
175         FT_UINT8, BASE_DEC, NULL, 0x0,
176         "Padding", HFILL }},
177
178     { &hf_xyplex_server_port,
179       { "Server Port",        "xyplex.server_port",
180         FT_UINT16, BASE_DEC, NULL, 0x0,
181         "Server port", HFILL }},
182
183     { &hf_xyplex_return_port,
184       { "Return Port",   "xyplex.return_port",
185         FT_UINT16, BASE_DEC, NULL, 0x0,
186         "Return port", HFILL }},
187
188     { &hf_xyplex_reserved,
189       { "Reserved field",  "xyplex.reserved",
190         FT_UINT16, BASE_DEC, NULL, 0x0,
191         "Reserved field", HFILL }},
192
193     { &hf_xyplex_reply,
194       { "Registration Reply",  "xyplex.reply",
195         FT_UINT16, BASE_DEC, VALS(xyplex_reg_vals), 0x0,
196         "Registration reply", HFILL }},
197
198   };
199   static gint *ett[] = {
200     &ett_xyplex,
201   };
202
203   proto_xyplex = proto_register_protocol("Xyplex", "XYPLEX", "xyplex");
204   proto_register_field_array(proto_xyplex, hf, array_length(hf));
205   proto_register_subtree_array(ett, array_length(ett));
206
207   xyplex_handle = create_dissector_handle(dissect_xyplex, proto_xyplex);
208 }
209
210 void
211 proto_reg_handoff_xyplex(void)
212 {
213   dissector_add("udp.port", UDP_PORT_XYPLEX, xyplex_handle);
214 }
215