2 * Routines for rsync dissection
3 * [ very rough, but mininally functional ]
4 * Copyright 2003, Brad Hards <bradh@frogmouth.net>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #define NEW_PROTO_TREE_API
33 #include <epan/packet.h>
34 #include <epan/strutil.h>
35 #include <epan/conversation.h>
36 #include <epan/wmem/wmem.h>
37 #include <epan/prefs.h>
39 void proto_register_rsync(void);
41 #define RSYNCD_MAGIC_HEADER "@RSYNCD:"
42 #define RSYNCD_MAGIC_HEADER_LEN 8
44 #define RSYNCD_AUTHREQD "@RSYNCD: AUTHREQD "
45 #define RSYNCD_AUTHREQD_LEN 18
47 #define RSYNCD_EXIT "@RSYNCD: EXIT"
48 #define RSYNCD_EXIT_LEN 13
50 #define RSYNC_MODULE_LIST_QUERY "\n"
51 #define RSYNC_MODULE_LIST_QUERY_LEN 1
53 /* what states make sense here ? */
54 typedef enum _rsync_state {
57 RSYNC_CLIENT_QUERY = 2,
58 RSYNC_MODULE_LIST = 4,
69 static gboolean rsync_desegment = TRUE;
71 /* this is a guide to the current conversation state */
72 struct rsync_conversation_data {
73 rsync_state_t client_state;
74 rsync_state_t server_state;
77 struct rsync_frame_data {
81 static header_field_info *hfi_rsync = NULL;
83 #define RSYNC_HF_INIT HFI_INIT(proto_rsync)
85 static header_field_info hfi_rsync_hdr_magic RSYNC_HF_INIT = {
86 "Magic Header", "rsync.hdr_magic",
87 FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL };
89 static header_field_info hfi_rsync_hdr_version RSYNC_HF_INIT = {
90 "Header Version", "rsync.hdr_version",
91 FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL };
93 static header_field_info hfi_rsync_query_string RSYNC_HF_INIT = {
94 "Client Query String", "rsync.query",
95 FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL };
97 static header_field_info hfi_rsync_motd_string RSYNC_HF_INIT = {
98 "Server MOTD String", "rsync.motd",
99 FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL };
101 static header_field_info hfi_rsync_module_list_string RSYNC_HF_INIT = {
102 "Server Module List", "rsync.module_list",
103 FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL };
105 static header_field_info hfi_rsync_rsyncdok_string RSYNC_HF_INIT = {
106 "RSYNCD Response String", "rsync.response",
107 FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL };
109 static header_field_info hfi_rsync_command_string RSYNC_HF_INIT = {
110 "Client Command String", "rsync.command",
111 FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL };
113 static header_field_info hfi_rsync_data RSYNC_HF_INIT = {
114 "rsync data", "rsync.data",
115 FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL };
117 static gint ett_rsync = -1;
119 static dissector_handle_t rsync_handle;
122 #define TCP_PORT_RSYNC 873
124 static guint glb_rsync_tcp_port = TCP_PORT_RSYNC;
126 #define VERSION_LEN 4 /* 2 digits for main version; '.'; 1 digit for sub version */
129 dissect_rsync_version_header(tvbuff_t *tvb, packet_info *pinfo, proto_tree *rsync_tree, enum rsync_who me)
134 proto_tree_add_item(rsync_tree, &hfi_rsync_hdr_magic, tvb, offset, RSYNCD_MAGIC_HEADER_LEN, ENC_ASCII|ENC_NA);
135 offset += RSYNCD_MAGIC_HEADER_LEN;
136 offset += 1; /* skip the space */
137 proto_tree_add_item(rsync_tree, &hfi_rsync_hdr_version, tvb, offset, VERSION_LEN, ENC_ASCII|ENC_NA);
138 version = tvb_get_string_enc(wmem_packet_scope(),tvb, offset, VERSION_LEN, ENC_ASCII|ENC_NA);
140 col_add_fstr(pinfo->cinfo, COL_INFO, "%s Initialisation (Version %s)", (me == SERVER ? "Server" : "Client"), version);
143 /* Packet dissection routine called by tcp (& udp) when port 873 detected */
145 dissect_rsync_encap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
146 gboolean desegment _U_)
148 conversation_t *conversation;
149 struct rsync_conversation_data *conversation_data;
150 struct rsync_frame_data *rsync_frame_data_p;
152 proto_tree *rsync_tree;
157 col_set_str(pinfo->cinfo, COL_PROTOCOL, "RSYNC");
159 col_clear(pinfo->cinfo, COL_INFO);
161 me = pinfo->srcport == glb_rsync_tcp_port ? SERVER : CLIENT;
163 conversation = find_or_create_conversation(pinfo);
165 conversation_data = (struct rsync_conversation_data *)conversation_get_proto_data(conversation, hfi_rsync->id);
167 if (conversation_data == NULL) { /* new conversation */
168 conversation_data = wmem_new(wmem_file_scope(), struct rsync_conversation_data);
169 conversation_data->client_state = RSYNC_INIT;
170 conversation_data->server_state = RSYNC_SERV_INIT;
171 conversation_add_proto_data(conversation, hfi_rsync->id, conversation_data);
174 conversation_set_dissector(conversation, rsync_handle);
176 ti = proto_tree_add_item(tree, hfi_rsync, tvb, 0, -1, ENC_NA);
178 rsync_tree = proto_item_add_subtree(ti, ett_rsync);
180 rsync_frame_data_p = (struct rsync_frame_data *)p_get_proto_data(wmem_file_scope(), pinfo, hfi_rsync->id, 0);
181 if (!rsync_frame_data_p) {
182 /* then we haven't seen this frame before */
183 rsync_frame_data_p = wmem_new(wmem_file_scope(), struct rsync_frame_data);
184 rsync_frame_data_p->state = (me == SERVER) ? conversation_data->server_state : conversation_data->client_state;
185 p_add_proto_data(wmem_file_scope(), pinfo, hfi_rsync->id, 0, rsync_frame_data_p);
189 switch (rsync_frame_data_p->state) {
190 case RSYNC_SERV_INIT:
191 dissect_rsync_version_header(tvb, pinfo, rsync_tree, me);
193 conversation_data->server_state = RSYNC_SERV_MOTD;
197 case RSYNC_SERV_MOTD:
198 proto_tree_add_item(rsync_tree, &hfi_rsync_motd_string, tvb, offset, -1, ENC_ASCII|ENC_NA);
200 col_set_str(pinfo->cinfo, COL_INFO, "Server MOTD");
202 conversation_data->server_state = RSYNC_SERV_MOTD;
206 case RSYNC_MODULE_LIST:
207 /* there are two cases - file list, or authentication */
208 if (0 == tvb_strneql(tvb, offset, RSYNCD_AUTHREQD, RSYNCD_AUTHREQD_LEN)) {
209 /* matches, so we assume its an authentication message */
210 proto_tree_add_item(rsync_tree, &hfi_rsync_rsyncdok_string, tvb, offset, -1, ENC_ASCII|ENC_NA);
212 col_set_str(pinfo->cinfo, COL_INFO, "Authentication");
213 conversation_data->server_state = RSYNC_DATA;
215 } else { /* it didn't match, so it is probably a module list */
217 proto_tree_add_item(rsync_tree, &hfi_rsync_module_list_string, tvb, offset, -1, ENC_ASCII|ENC_NA);
219 /* we need to check the end of the buffer for magic string */
220 buff_length = tvb_length_remaining(tvb, offset);
221 if (buff_length > RSYNCD_EXIT_LEN &&
222 0 == tvb_strneql(tvb, buff_length-RSYNCD_EXIT_LEN-1, RSYNCD_EXIT, RSYNCD_EXIT_LEN)) {
223 /* that's all, folks */
224 col_set_str(pinfo->cinfo, COL_INFO, "Final module list");
225 conversation_data->server_state = RSYNC_DATA;
226 } else { /* there must be more data */
227 col_set_str(pinfo->cinfo, COL_INFO, "Module list");
228 conversation_data->server_state = RSYNC_MODULE_LIST;
235 proto_tree_add_item(rsync_tree, &hfi_rsync_data, tvb, offset, -1, ENC_NA);
237 col_set_str(pinfo->cinfo, COL_INFO, "Data");
239 conversation_data->server_state = RSYNC_DATA;
247 } else { /* me == CLIENT */
248 switch (rsync_frame_data_p->state) {
250 dissect_rsync_version_header(tvb, pinfo, rsync_tree, me);
252 conversation_data->client_state = RSYNC_CLIENT_QUERY;
256 case RSYNC_CLIENT_QUERY:
257 proto_tree_add_item(rsync_tree, &hfi_rsync_query_string, tvb, offset, -1, ENC_ASCII|ENC_NA);
259 col_set_str(pinfo->cinfo, COL_INFO, "Client Query");
261 conversation_data->client_state = RSYNC_COMMAND;
263 if (tvb_length(tvb) == RSYNC_MODULE_LIST_QUERY_LEN &&
264 0 == tvb_strneql(tvb, offset, RSYNC_MODULE_LIST_QUERY, RSYNC_MODULE_LIST_QUERY_LEN)) {
265 conversation_data->server_state = RSYNC_MODULE_LIST;
267 conversation_data->server_state = RSYNC_DATA;
273 /* then we are still sending commands */
274 proto_tree_add_item(rsync_tree, &hfi_rsync_command_string, tvb, offset, -1, ENC_ASCII|ENC_NA);
276 col_set_str(pinfo->cinfo, COL_INFO, "Client Command");
278 conversation_data->client_state = RSYNC_COMMAND;
283 /* then we are still sending commands */
284 proto_tree_add_item(rsync_tree, &hfi_rsync_data, tvb, offset, -1, ENC_NA);
286 col_set_str(pinfo->cinfo, COL_INFO, "Data");
288 conversation_data->client_state = RSYNC_DATA;
297 return tvb_length(tvb);
300 /* Packet dissection routine called by tcp (& udp) when port 873 detected */
302 dissect_rsync(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
304 return dissect_rsync_encap(tvb, pinfo, tree, rsync_desegment);
307 /* Register protocol with Wireshark. */
309 void proto_reg_handoff_rsync(void);
312 proto_register_rsync(void)
314 #ifndef HAVE_HFI_SECTION_INIT
315 static header_field_info *hfi[] = {
316 &hfi_rsync_hdr_magic,
317 &hfi_rsync_hdr_version,
318 &hfi_rsync_query_string,
319 &hfi_rsync_module_list_string,
320 &hfi_rsync_motd_string,
321 &hfi_rsync_rsyncdok_string,
322 &hfi_rsync_command_string,
327 static gint *ett[] = {
331 module_t *rsync_module;
335 proto_rsync = proto_register_protocol("RSYNC File Synchroniser",
337 hfi_rsync = proto_registrar_get_nth(proto_rsync);
339 proto_register_fields(proto_rsync, hfi, array_length(hfi));
340 proto_register_subtree_array(ett, array_length(ett));
342 rsync_module = prefs_register_protocol(proto_rsync, proto_reg_handoff_rsync);
343 prefs_register_uint_preference(rsync_module, "tcp_port",
345 "Set the TCP port for RSYNC messages",
347 &glb_rsync_tcp_port);
348 prefs_register_bool_preference(rsync_module, "desegment",
349 "Reassemble RSYNC messages spanning multiple TCP segments",
350 "Whether the RSYNC dissector should reassemble messages spanning multiple TCP segments."
351 " To use this option, you must also enable"
352 " \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
355 rsync_handle = new_create_dissector_handle(dissect_rsync, proto_rsync);
358 proto_reg_handoff_rsync(void)
360 static gboolean initialized = FALSE;
361 static guint saved_rsync_tcp_port;
366 dissector_delete_uint("tcp.port", saved_rsync_tcp_port, rsync_handle);
369 dissector_add_uint("tcp.port", glb_rsync_tcp_port, rsync_handle);
370 saved_rsync_tcp_port = glb_rsync_tcp_port;
374 * Editor modelines - http://www.wireshark.org/tools/modelines.html
379 * indent-tabs-mode: nil
382 * vi: set shiftwidth=4 tabstop=8 expandtab:
383 * :indentSize=4:tabSize=8:noTabs=true: