2 * Routines for rsync dissection
3 * [ very rough, but mininally functional ]
4 * Copyright 2003, Brad Hards <bradh@frogmouth.net>
6 * $Id: packet-rsync.c,v 1.3 2003/03/01 14:12:38 deniel Exp $
8 * Ethereal - Network traffic analyzer
9 * By Gerald Combs <gerald@ethereal.com>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 #include <epan/packet.h>
39 #include <epan/strutil.h>
40 #include <epan/conversation.h>
45 /* what states make sense here ? */
46 typedef enum _rsync_state {
49 RSYNC_CLIENT_QUERY = 2,
50 RSYNC_SERV_RESPONSE = 4,
56 static gboolean rsync_desegment = TRUE;
58 /* this is a guide to the current conversation state */
59 struct rsync_conversation_data {
63 struct rsync_frame_data {
67 static int proto_rsync = -1;
69 static int hf_rsync_hdr_magic = -1;
70 static int hf_rsync_hdr_version = -1;
71 static int hf_rsync_query_string = -1;
72 static int hf_rsync_motd_string = -1;
73 static int hf_rsync_response_string = -1;
74 static int hf_rsync_rsyncdok_string = -1;
75 static int hf_rsync_command_string = -1;
76 static int hf_rsync_data = -1;
78 static gint ett_rsync = -1;
80 dissector_handle_t rsync_handle;
83 #define TCP_PORT_RSYNC 873
85 static unsigned int glb_rsync_tcp_port = TCP_PORT_RSYNC;
87 /* Packet dissection routine called by tcp (& udp) when port 873 detected */
89 dissect_rsync_encap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
92 conversation_t *conversation;
93 struct rsync_conversation_data *conversation_data;
94 struct rsync_frame_data *frame_data;
96 proto_tree *rsync_tree;
99 gchar auth_string[10];
101 gchar magic_string[14];
103 if (check_col(pinfo->cinfo, COL_PROTOCOL))
104 col_set_str(pinfo->cinfo, COL_PROTOCOL, "RSYNC");
106 if (check_col(pinfo->cinfo, COL_INFO))
107 col_clear(pinfo->cinfo, COL_INFO);
109 conversation = find_conversation(&pinfo->src, &pinfo->dst, pinfo->ptype,
110 pinfo->srcport, pinfo->destport, 0);
111 if (conversation == NULL) {
112 conversation = conversation_new(&pinfo->src, &pinfo->dst,
113 pinfo->ptype, pinfo->srcport,
117 conversation_data = conversation_get_proto_data(conversation, proto_rsync);
119 if (conversation_data == NULL) {
120 conversation_data = malloc(sizeof(struct rsync_conversation_data));
121 conversation_data->state = RSYNC_INIT;
122 conversation_add_proto_data(conversation, proto_rsync, conversation_data);
125 conversation_set_dissector(conversation, rsync_handle);
127 ti = proto_tree_add_item(tree, proto_rsync, tvb, 0, -1, FALSE);
129 rsync_tree = proto_item_add_subtree(ti, ett_rsync);
131 frame_data = p_get_proto_data(pinfo->fd, proto_rsync);
133 /* then we haven't seen this frame before */
134 frame_data = malloc(sizeof(struct rsync_frame_data));
135 frame_data->state = conversation_data->state;
136 p_add_proto_data(pinfo->fd, proto_rsync, frame_data);
139 switch (frame_data->state) {
141 proto_tree_add_item(rsync_tree, hf_rsync_hdr_magic, tvb, offset, 8, TRUE);
143 proto_tree_add_item(rsync_tree, hf_rsync_hdr_version, tvb, offset, 4, TRUE);
144 tvb_get_nstringz0(tvb, offset, 3, version);
147 if (check_col(pinfo->cinfo, COL_INFO)) {
148 col_append_fstr(pinfo->cinfo, COL_INFO,
149 "Client Initialisation (Version %s)",
153 conversation_data->state = RSYNC_SERV_INIT;
154 conversation_add_proto_data(conversation, proto_rsync, conversation_data);
157 case RSYNC_SERV_INIT:
158 proto_tree_add_item(rsync_tree, hf_rsync_hdr_magic, tvb, offset, 8, TRUE);
160 proto_tree_add_item(rsync_tree, hf_rsync_hdr_version, tvb, offset, 4, TRUE);
161 tvb_get_nstringz0(tvb, offset, 3, version);
164 if (check_col(pinfo->cinfo, COL_INFO)) {
165 col_append_fstr(pinfo->cinfo, COL_INFO,
166 "Server Initialisation (Version %s)",
170 conversation_data->state = RSYNC_CLIENT_QUERY;
171 conversation_add_proto_data(conversation, proto_rsync, conversation_data);
174 case RSYNC_CLIENT_QUERY:
175 proto_tree_add_item(rsync_tree, hf_rsync_query_string, tvb, offset, -1, TRUE);
177 if (check_col(pinfo->cinfo, COL_INFO)) {
178 col_append_str(pinfo->cinfo, COL_INFO, "Client Query");
181 conversation_data->state = RSYNC_SERV_MOTD;
182 conversation_add_proto_data(conversation, proto_rsync, conversation_data);
185 case RSYNC_SERV_MOTD:
186 proto_tree_add_item(rsync_tree, hf_rsync_motd_string, tvb, offset, -1, TRUE);
188 if (check_col(pinfo->cinfo, COL_INFO)) {
189 col_append_fstr(pinfo->cinfo, COL_INFO, "Server MOTD");
192 conversation_data->state = RSYNC_SERV_RESPONSE;
193 conversation_add_proto_data(conversation, proto_rsync, conversation_data);
196 case RSYNC_SERV_RESPONSE:
197 /* there are two cases - file list, or authentication */
198 tvb_get_nstringz0(tvb, offset, 8, auth_string);
199 if (0 == strncmp("@RSYNCD:", auth_string, 8)) {
200 /* matches, so we assume its an authentication message */
201 /* needs to handle the AUTHREQD case, but doesn't - FIXME */
202 proto_tree_add_item(rsync_tree, hf_rsync_rsyncdok_string, tvb, offset, -1, TRUE);
204 if (check_col(pinfo->cinfo, COL_INFO)) {
205 col_append_str(pinfo->cinfo, COL_INFO, "Authenication");
207 conversation_data->state = RSYNC_COMMAND;
209 } else { /* it didn't match, so it is probably a module list */
211 proto_tree_add_item(rsync_tree, hf_rsync_response_string, tvb, offset, -1, TRUE);
213 if (check_col(pinfo->cinfo, COL_INFO)) {
214 col_append_fstr(pinfo->cinfo, COL_INFO, "Module list");
217 /* we need to check the end of the buffer for magic string */
218 buff_length = tvb_length_remaining(tvb, offset);
219 tvb_get_nstringz0(tvb, buff_length-14, 14, magic_string);
220 if (0 == strncmp("@RSYNCD: EXIT", magic_string, 14)) {
221 /* that's all, folks */
222 conversation_data->state = RSYNC_COMMAND;
223 } else { /* there must be more data */
224 conversation_data->state = RSYNC_SERV_RESPONSE;
228 conversation_add_proto_data(conversation, proto_rsync, conversation_data);
233 if (pinfo->destport == glb_rsync_tcp_port) {
234 /* then we are still sending commands */
235 proto_tree_add_item(rsync_tree, hf_rsync_command_string, tvb, offset, -1, TRUE);
237 if (check_col(pinfo->cinfo, COL_INFO)) {
238 col_append_str(pinfo->cinfo, COL_INFO, "Command");
241 conversation_data->state = RSYNC_COMMAND;
242 conversation_add_proto_data(conversation, proto_rsync, conversation_data);
245 } /* else we fall through to the data phase */
248 /* then we are still sending commands */
249 proto_tree_add_item(rsync_tree, hf_rsync_data, tvb, offset, -1, TRUE);
251 if (check_col(pinfo->cinfo, COL_INFO)) {
252 col_append_str(pinfo->cinfo, COL_INFO, "Data");
255 conversation_data->state = RSYNC_DATA;
256 conversation_add_proto_data(conversation, proto_rsync, conversation_data);
264 /* Packet dissection routine called by tcp (& udp) when port 873 detected */
266 dissect_rsync(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
268 dissect_rsync_encap(tvb, pinfo, tree, rsync_desegment);
271 /* Register protocol with Ethereal. */
273 proto_register_rsync(void)
275 static hf_register_info hf[] = {
276 {&hf_rsync_hdr_magic,
277 {"Magic Header", "rsync.hdr_magic",
278 FT_STRING, BASE_NONE, NULL, 0x0, "", HFILL }
280 {&hf_rsync_hdr_version,
281 {"Header Version", "rsync.hdr_version",
282 FT_STRING, BASE_NONE, NULL, 0x0, "", HFILL }
284 {&hf_rsync_query_string,
285 {"Client Query String", "rsync.query",
286 FT_STRING, BASE_NONE, NULL, 0x0, "", HFILL }
288 {&hf_rsync_response_string,
289 {"Server Response String", "rsync.response",
290 FT_STRING, BASE_NONE, NULL, 0x0, "", HFILL }
292 {&hf_rsync_motd_string,
293 {"Server MOTD String", "rsync.motd",
294 FT_STRING, BASE_NONE, NULL, 0x0, "", HFILL }
296 {&hf_rsync_rsyncdok_string,
297 {"RSYNCD Response String", "rsync.response",
298 FT_STRING, BASE_NONE, NULL, 0x0, "", HFILL }
300 {&hf_rsync_command_string,
301 {"Command String", "rsync.command",
302 FT_STRING, BASE_NONE, NULL, 0x0, "", HFILL }
305 {"rsync data", "rsync.data",
306 FT_BYTES, BASE_NONE, NULL, 0x0, "", HFILL }
311 static gint *ett[] = {
315 module_t *rsync_module;
317 proto_rsync = proto_register_protocol("RSYNC File Synchroniser",
319 proto_register_field_array(proto_rsync, hf, array_length(hf));
320 proto_register_subtree_array(ett, array_length(ett));
322 rsync_module = prefs_register_protocol(proto_rsync, NULL);
323 prefs_register_uint_preference(rsync_module, "tcp_port",
325 "Set the TCP port for RSYNC messages",
327 &glb_rsync_tcp_port);
328 prefs_register_bool_preference(rsync_module, "desegment",
329 "Desegment all RSYNC messages spanning multiple TCP segments",
330 "Whether the RSYNC dissector should desegment all messages spanning multiple TCP segments",
334 proto_reg_handoff_rsync(void)
336 rsync_handle = create_dissector_handle(dissect_rsync, proto_rsync);
337 dissector_add("tcp.port", glb_rsync_tcp_port, rsync_handle);