PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wireshark-1.8.0/epan/dissectors/packet-distcc.c

#
C | 419 lines | 274 code | 99 blank | 46 comment | 35 complexity | 31489b0b1cdd4595bc31def4e1c3545b MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. /* packet-distcc.c
  2. * Routines for distcc dissection
  3. * Copyright 2003, Brad Hards <bradh@frogmouth.net>
  4. * Copyright 2003, Ronnie Sahlberg, added TCP desegmentation.
  5. *
  6. * $Id: packet-distcc.c 42271 2012-04-26 16:47:37Z wmeier $
  7. *
  8. * Wireshark - Network traffic analyzer
  9. * By Gerald Combs <gerald@wireshark.org>
  10. * Copyright 1998 Gerald Combs
  11. *
  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.
  16. *
  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.
  21. *
  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.
  25. */
  26. /* This dissector supports version 1 of the DISTCC protocol */
  27. #ifdef HAVE_CONFIG_H
  28. # include "config.h"
  29. #endif
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <time.h>
  33. #include <glib.h>
  34. #include <epan/packet.h>
  35. #include <epan/strutil.h>
  36. #include <epan/prefs.h>
  37. static int proto_distcc = -1;
  38. static int hf_distcc_version = -1;
  39. static int hf_distcc_argc = -1;
  40. static int hf_distcc_argv = -1;
  41. static int hf_distcc_doti_source = -1;
  42. static int hf_distcc_stat = -1;
  43. static int hf_distcc_serr = -1;
  44. static int hf_distcc_sout = -1;
  45. static int hf_distcc_doto_object = -1;
  46. static gint ett_distcc = -1;
  47. static dissector_handle_t data_handle;
  48. static gboolean distcc_desegment = TRUE;
  49. #define TCP_PORT_DISTCC 3632
  50. static guint glb_distcc_tcp_port = TCP_PORT_DISTCC;
  51. extern void proto_reg_handoff_distcc(void);
  52. #define CHECK_PDU_LEN(x) \
  53. if(parameter>tvb_length_remaining(tvb, offset) || parameter < 1){\
  54. len=tvb_length_remaining(tvb, offset);\
  55. col_append_str(pinfo->cinfo, COL_INFO, "[Short" x " PDU]");\
  56. } \
  57. tvb_ensure_bytes_exist(tvb, offset, len);
  58. #define DESEGMENT_TCP(x) \
  59. if(distcc_desegment && pinfo->can_desegment){\
  60. /* only attempt reassembly if whe have the full segment */\
  61. if(tvb_length_remaining(tvb, offset)==tvb_reported_length_remaining(tvb, offset)){\
  62. if(parameter>tvb_length_remaining(tvb, offset)){\
  63. proto_tree_add_text(tree, tvb, offset-12, -1, "[Short " x " PDU]");\
  64. pinfo->desegment_offset=offset-12;\
  65. pinfo->desegment_len=parameter-tvb_length_remaining(tvb, offset);\
  66. return offset+len;\
  67. }\
  68. }\
  69. }
  70. static int
  71. dissect_distcc_dist(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int offset, guint32 parameter)
  72. {
  73. proto_tree_add_uint_format(tree, hf_distcc_version, tvb, offset-12, 12, parameter, "DIST: %d", parameter);
  74. col_append_fstr(pinfo->cinfo, COL_INFO, "DIST:%d ", parameter);
  75. return offset;
  76. }
  77. static int
  78. dissect_distcc_done(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int offset, guint32 parameter)
  79. {
  80. proto_tree_add_uint_format(tree, hf_distcc_version, tvb, offset-12, 12, parameter, "DONE: %d", parameter);
  81. col_append_fstr(pinfo->cinfo, COL_INFO, "DONE:%d ", parameter);
  82. return offset;
  83. }
  84. static int
  85. dissect_distcc_stat(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int offset, guint32 parameter)
  86. {
  87. proto_tree_add_uint_format(tree, hf_distcc_stat, tvb, offset-12, 12, parameter, "STAT: %d", parameter);
  88. col_append_fstr(pinfo->cinfo, COL_INFO, "STAT:%d ", parameter);
  89. return offset;
  90. }
  91. static int
  92. dissect_distcc_argc(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, int offset, guint32 parameter)
  93. {
  94. proto_tree_add_uint_format(tree, hf_distcc_argc, tvb, offset-12, 12, parameter, "ARGC: %d", parameter);
  95. col_append_fstr(pinfo->cinfo, COL_INFO, "ARGC:%d ", parameter);
  96. return offset;
  97. }
  98. static int
  99. dissect_distcc_argv(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, int offset, gint parameter)
  100. {
  101. char argv[256];
  102. int argv_len;
  103. gint len=parameter;
  104. CHECK_PDU_LEN("ARGV");
  105. /* see if we need to desegment the PDU */
  106. DESEGMENT_TCP("ARGV");
  107. argv_len=len>255?255:len;
  108. tvb_memcpy(tvb, argv, offset, argv_len);
  109. argv[argv_len]=0;
  110. proto_tree_add_item(tree, hf_distcc_argv, tvb, offset, len, ENC_ASCII|ENC_NA);
  111. col_append_fstr(pinfo->cinfo, COL_INFO, "%s ", argv);
  112. if(len!=parameter){
  113. proto_tree_add_text(tree, tvb, 0, 0, "[Short ARGV PDU]");
  114. }
  115. return offset+len;
  116. }
  117. static int
  118. dissect_distcc_serr(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, int offset, gint parameter)
  119. {
  120. char argv[256];
  121. int argv_len;
  122. gint len=parameter;
  123. CHECK_PDU_LEN("SERR");
  124. /* see if we need to desegment the PDU */
  125. DESEGMENT_TCP("SERR");
  126. argv_len=len>255?255:len;
  127. tvb_memcpy(tvb, argv, offset, argv_len);
  128. argv[argv_len]=0;
  129. proto_tree_add_item(tree, hf_distcc_serr, tvb, offset, len, ENC_ASCII|ENC_NA);
  130. col_append_fstr(pinfo->cinfo, COL_INFO, "SERR:%s ", argv);
  131. if(len!=parameter){
  132. proto_tree_add_text(tree, tvb, 0, 0, "[Short SERR PDU]");
  133. }
  134. return offset+len;
  135. }
  136. static int
  137. dissect_distcc_sout(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, int offset, gint parameter)
  138. {
  139. char argv[256];
  140. int argv_len;
  141. gint len=parameter;
  142. CHECK_PDU_LEN("SOUT");
  143. /* see if we need to desegment the PDU */
  144. DESEGMENT_TCP("SOUT");
  145. argv_len=len>255?255:len;
  146. tvb_memcpy(tvb, argv, offset, argv_len);
  147. argv[argv_len]=0;
  148. proto_tree_add_item(tree, hf_distcc_sout, tvb, offset, len, ENC_ASCII|ENC_NA);
  149. col_append_fstr(pinfo->cinfo, COL_INFO, "SOUT:%s ", argv);
  150. if(len!=parameter){
  151. proto_tree_add_text(tree, tvb, 0, 0, "[Short SOUT PDU]");
  152. }
  153. return offset+len;
  154. }
  155. static int
  156. dissect_distcc_doti(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int offset, gint parameter)
  157. {
  158. gint len=parameter;
  159. CHECK_PDU_LEN("DOTI");
  160. /* see if we need to desegment the PDU */
  161. DESEGMENT_TCP("DOTI");
  162. col_append_str(pinfo->cinfo, COL_INFO, "DOTI source ");
  163. proto_tree_add_item(tree, hf_distcc_doti_source, tvb, offset, len, ENC_ASCII|ENC_NA);
  164. if(len!=parameter){
  165. proto_tree_add_text(tree, tvb, 0, 0, "[Short DOTI PDU]");
  166. }
  167. return offset+len;
  168. }
  169. static int
  170. dissect_distcc_doto(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int offset, gint parameter)
  171. {
  172. gint len=parameter;
  173. CHECK_PDU_LEN("DOTO");
  174. /* see if we need to desegment the PDU */
  175. DESEGMENT_TCP("DOTO");
  176. col_append_str(pinfo->cinfo, COL_INFO, "DOTO object ");
  177. proto_tree_add_item(tree, hf_distcc_doto_object, tvb, offset, len, ENC_NA);
  178. if(len!=parameter){
  179. proto_tree_add_text(tree, tvb, 0, 0, "[Short DOTO PDU]");
  180. }
  181. return offset+len;
  182. }
  183. /* Packet dissection routine called by tcp (& udp) when port 3632 detected */
  184. static void
  185. dissect_distcc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
  186. {
  187. int offset=0;
  188. proto_tree *tree=NULL;
  189. proto_item *item=NULL;
  190. char token[4];
  191. guint32 parameter;
  192. col_set_str(pinfo->cinfo, COL_PROTOCOL, "DISTCC ");
  193. col_clear(pinfo->cinfo, COL_INFO);
  194. if (parent_tree) {
  195. item = proto_tree_add_item(parent_tree, proto_distcc, tvb, offset,
  196. -1, ENC_NA);
  197. tree = proto_item_add_subtree(item, ett_distcc);
  198. }
  199. while(1){
  200. /* we must have at least 12 bytes so we can read the
  201. token and the parameter */
  202. if(tvb_length_remaining(tvb, offset)<12){
  203. return;
  204. }
  205. /* read the token */
  206. tvb_memcpy(tvb, token, offset, 4);
  207. offset+=4;
  208. /* read the parameter */
  209. if (sscanf(tvb_get_ptr(tvb, offset, 8), "%08x", &parameter) != 1)
  210. return;
  211. offset+=8;
  212. if(!strncmp(token, "DIST", 4)){
  213. offset=dissect_distcc_dist(tvb, pinfo, tree, offset, parameter);
  214. } else if(!strncmp(token, "ARGC", 4)){
  215. offset=dissect_distcc_argc(tvb, pinfo, tree, offset, parameter);
  216. } else if(!strncmp(token, "ARGV", 4)){
  217. offset=dissect_distcc_argv(tvb, pinfo, tree, offset, parameter);
  218. } else if(!strncmp(token, "DOTI", 4)){
  219. offset=dissect_distcc_doti(tvb, pinfo, tree, offset, parameter);
  220. } else if(!strncmp(token, "DONE", 4)){
  221. offset=dissect_distcc_done(tvb, pinfo, tree, offset, parameter);
  222. } else if(!strncmp(token, "STAT", 4)){
  223. offset=dissect_distcc_stat(tvb, pinfo, tree, offset, parameter);
  224. } else if(!strncmp(token, "SERR", 4)){
  225. offset=dissect_distcc_serr(tvb, pinfo, tree, offset, parameter);
  226. } else if(!strncmp(token, "SOUT", 4)){
  227. offset=dissect_distcc_sout(tvb, pinfo, tree, offset, parameter);
  228. } else if(!strncmp(token, "DOTO", 4)){
  229. offset=dissect_distcc_doto(tvb, pinfo, tree, offset, parameter);
  230. } else {
  231. call_dissector(data_handle, tvb, pinfo, tree);
  232. return;
  233. }
  234. }
  235. }
  236. /* Register protocol with Wireshark. */
  237. void
  238. proto_register_distcc(void)
  239. {
  240. static hf_register_info hf[] = {
  241. {&hf_distcc_version,
  242. {"DISTCC Version", "distcc.version",
  243. FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }
  244. },
  245. {&hf_distcc_argc,
  246. {"ARGC", "distcc.argc",
  247. FT_UINT32, BASE_DEC, NULL, 0x0, "Number of arguments", HFILL }
  248. },
  249. {&hf_distcc_argv,
  250. {"ARGV", "distcc.argv",
  251. FT_STRING, BASE_NONE, NULL, 0x0, "ARGV argument", HFILL }
  252. },
  253. {&hf_distcc_doti_source,
  254. {"Source", "distcc.doti_source",
  255. FT_STRING, BASE_NONE, NULL, 0x0, "DOTI Preprocessed Source File (.i)", HFILL }
  256. },
  257. {&hf_distcc_stat,
  258. {"Status", "distcc.status",
  259. FT_UINT32, BASE_DEC, NULL, 0x0, "Unix wait status for command completion", HFILL }
  260. },
  261. {&hf_distcc_serr,
  262. {"SERR", "distcc.serr",
  263. FT_STRING, BASE_NONE, NULL, 0x0, "STDERR output", HFILL }
  264. },
  265. {&hf_distcc_sout,
  266. {"SOUT", "distcc.sout",
  267. FT_STRING, BASE_NONE, NULL, 0x0, "STDOUT output", HFILL }
  268. },
  269. {&hf_distcc_doto_object,
  270. {"Object", "distcc.doto_object",
  271. FT_BYTES, BASE_NONE, NULL, 0x0, "DOTO Compiled object file (.o)", HFILL }
  272. }
  273. };
  274. static gint *ett[] = {
  275. &ett_distcc,
  276. };
  277. module_t *distcc_module;
  278. proto_distcc = proto_register_protocol("Distcc Distributed Compiler",
  279. "DISTCC", "distcc");
  280. proto_register_field_array(proto_distcc, hf, array_length(hf));
  281. proto_register_subtree_array(ett, array_length(ett));
  282. distcc_module = prefs_register_protocol(proto_distcc,
  283. proto_reg_handoff_distcc);
  284. prefs_register_uint_preference(distcc_module, "tcp.port",
  285. "DISTCC TCP Port",
  286. "Set the TCP port for DISTCC messages",
  287. 10,
  288. &glb_distcc_tcp_port);
  289. prefs_register_bool_preference(distcc_module, "desegment_distcc_over_tcp",
  290. "Reassemble DISTCC-over-TCP messages\nspanning multiple TCP segments",
  291. "Whether the DISTCC dissector should reassemble messages spanning multiple TCP segments."
  292. " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
  293. &distcc_desegment);
  294. }
  295. void
  296. proto_reg_handoff_distcc(void)
  297. {
  298. static gboolean registered_dissector = FALSE;
  299. static int distcc_tcp_port;
  300. static dissector_handle_t distcc_handle;
  301. if (!registered_dissector) {
  302. /*
  303. * We haven't registered the dissector yet; get a handle
  304. * for it.
  305. */
  306. distcc_handle = create_dissector_handle(dissect_distcc,
  307. proto_distcc);
  308. data_handle = find_dissector("data");
  309. registered_dissector = TRUE;
  310. } else {
  311. /*
  312. * We've registered the dissector with a TCP port number
  313. * of "distcc_tcp_port"; we might be changing the TCP port
  314. * number, so remove that registration.
  315. */
  316. dissector_delete_uint("tcp.port", distcc_tcp_port, distcc_handle);
  317. }
  318. distcc_tcp_port = glb_distcc_tcp_port;
  319. dissector_add_uint("tcp.port", distcc_tcp_port, distcc_handle);
  320. }