PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/extcap/randpktdump.c

https://gitlab.com/jvelando/wireshark
C | 363 lines | 268 code | 58 blank | 37 comment | 33 complexity | 8866f1cdc28c60b0b3a301d621e0e9ac MD5 | raw file
  1. /* randpktdump.c
  2. * randpktdump is an extcap tool used to generate random data for testing/educational purpose
  3. *
  4. * Copyright 2015, Dario Lombardo
  5. *
  6. * Wireshark - Network traffic analyzer
  7. * By Gerald Combs <gerald@wireshark.org>
  8. * Copyright 1998 Gerald Combs
  9. *
  10. * SPDX-License-Identifier: GPL-2.0-or-later
  11. */
  12. #include "config.h"
  13. #define WS_LOG_DOMAIN "randpktdump"
  14. #include "extcap-base.h"
  15. #include "randpkt_core/randpkt_core.h"
  16. #include <wsutil/strtoi.h>
  17. #include <wsutil/filesystem.h>
  18. #include <wsutil/privileges.h>
  19. #include <wsutil/socket.h>
  20. #include <wsutil/please_report_bug.h>
  21. #include <wsutil/wslog.h>
  22. #include <cli_main.h>
  23. #include <ui/cmdarg_err.h>
  24. #define RANDPKT_EXTCAP_INTERFACE "randpkt"
  25. #define RANDPKTDUMP_VERSION_MAJOR "0"
  26. #define RANDPKTDUMP_VERSION_MINOR "1"
  27. #define RANDPKTDUMP_VERSION_RELEASE "0"
  28. enum {
  29. EXTCAP_BASE_OPTIONS_ENUM,
  30. OPT_HELP,
  31. OPT_VERSION,
  32. OPT_MAXBYTES,
  33. OPT_COUNT,
  34. OPT_DELAY,
  35. OPT_RANDOM_TYPE,
  36. OPT_ALL_RANDOM,
  37. OPT_TYPE
  38. };
  39. static struct ws_option longopts[] = {
  40. EXTCAP_BASE_OPTIONS,
  41. { "help", ws_no_argument, NULL, OPT_HELP},
  42. { "version", ws_no_argument, NULL, OPT_VERSION},
  43. { "maxbytes", ws_required_argument, NULL, OPT_MAXBYTES},
  44. { "count", ws_required_argument, NULL, OPT_COUNT},
  45. { "delay", ws_required_argument, NULL, OPT_DELAY},
  46. { "random-type", ws_no_argument, NULL, OPT_RANDOM_TYPE},
  47. { "all-random", ws_no_argument, NULL, OPT_ALL_RANDOM},
  48. { "type", ws_required_argument, NULL, OPT_TYPE},
  49. { 0, 0, 0, 0 }
  50. };
  51. static void help(extcap_parameters* extcap_conf)
  52. {
  53. unsigned i = 0;
  54. char** abbrev_list;
  55. char** longname_list;
  56. extcap_help_print(extcap_conf);
  57. printf("\nPacket types:\n");
  58. randpkt_example_list(&abbrev_list, &longname_list);
  59. while (abbrev_list[i] && longname_list[i]) {
  60. printf("\t%-16s%s\n", abbrev_list[i], longname_list[i]);
  61. i++;
  62. }
  63. printf("\n");
  64. g_strfreev(abbrev_list);
  65. g_strfreev(longname_list);
  66. }
  67. static int list_config(char *interface)
  68. {
  69. unsigned inc = 0;
  70. unsigned i = 0;
  71. char** abbrev_list;
  72. char** longname_list;
  73. if (!interface) {
  74. ws_warning("No interface specified.");
  75. return EXIT_FAILURE;
  76. }
  77. if (g_strcmp0(interface, RANDPKT_EXTCAP_INTERFACE)) {
  78. ws_warning("Interface must be %s", RANDPKT_EXTCAP_INTERFACE);
  79. return EXIT_FAILURE;
  80. }
  81. printf("arg {number=%u}{call=--maxbytes}{display=Max bytes in a packet}"
  82. "{type=unsigned}{range=1,5000}{default=5000}{tooltip=The max number of bytes in a packet}\n",
  83. inc++);
  84. printf("arg {number=%u}{call=--count}{display=Number of packets}"
  85. "{type=long}{default=1000}{tooltip=Number of packets to generate}\n",
  86. inc++);
  87. printf("arg {number=%u}{call=--delay}{display=Packet delay (ms)}"
  88. "{type=long}{default=0}{tooltip=Milliseconds to wait after writing each packet}\n",
  89. inc++);
  90. printf("arg {number=%u}{call=--random-type}{display=Random type}"
  91. "{type=boolflag}{default=false}{tooltip=The packets type is randomly chosen}\n",
  92. inc++);
  93. printf("arg {number=%u}{call=--all-random}{display=All random packets}"
  94. "{type=boolflag}{default=false}{tooltip=Packet type for each packet is randomly chosen}\n",
  95. inc++);
  96. /* Now the types */
  97. printf("arg {number=%u}{call=--type}{display=Type of packet}"
  98. "{type=selector}{tooltip=Type of packet to generate}\n",
  99. inc);
  100. randpkt_example_list(&abbrev_list, &longname_list);
  101. while (abbrev_list[i] && longname_list[i]) {
  102. printf("value {arg=%u}{value=%s}{display=%s}\n", inc, abbrev_list[i], longname_list[i]);
  103. i++;
  104. }
  105. g_strfreev(abbrev_list);
  106. g_strfreev(longname_list);
  107. inc++;
  108. extcap_config_debug(&inc);
  109. return EXIT_SUCCESS;
  110. }
  111. static void randpktdump_cmdarg_err(const char *msg_format, va_list ap)
  112. {
  113. ws_logv(LOG_DOMAIN_CAPCHILD, LOG_LEVEL_WARNING, msg_format, ap);
  114. }
  115. int main(int argc, char *argv[])
  116. {
  117. char* err_msg;
  118. int option_idx = 0;
  119. int result;
  120. guint16 maxbytes = 5000;
  121. guint64 count = 1000;
  122. guint64 packet_delay_ms = 0;
  123. int random_type = FALSE;
  124. int all_random = FALSE;
  125. char* type = NULL;
  126. int produce_type = -1;
  127. randpkt_example *example;
  128. wtap_dumper* savedump;
  129. int ret = EXIT_FAILURE;
  130. extcap_parameters * extcap_conf = g_new0(extcap_parameters, 1);
  131. char* help_url;
  132. char* help_header = NULL;
  133. cmdarg_err_init(randpktdump_cmdarg_err, randpktdump_cmdarg_err);
  134. /* Initialize log handler early so we can have proper logging during startup. */
  135. extcap_log_init("randpktdump");
  136. /*
  137. * Get credential information for later use.
  138. */
  139. init_process_policies();
  140. /*
  141. * Attempt to get the pathname of the directory containing the
  142. * executable file.
  143. */
  144. err_msg = init_progfile_dir(argv[0]);
  145. if (err_msg != NULL) {
  146. ws_warning("Can't get pathname of directory containing the extcap program: %s.",
  147. err_msg);
  148. g_free(err_msg);
  149. }
  150. help_url = data_file_url("randpktdump.html");
  151. extcap_base_set_util_info(extcap_conf, argv[0], RANDPKTDUMP_VERSION_MAJOR, RANDPKTDUMP_VERSION_MINOR,
  152. RANDPKTDUMP_VERSION_RELEASE, help_url);
  153. g_free(help_url);
  154. extcap_base_register_interface(extcap_conf, RANDPKT_EXTCAP_INTERFACE, "Random packet generator", 147, "Generator dependent DLT");
  155. help_header = ws_strdup_printf(
  156. " %s --extcap-interfaces\n"
  157. " %s --extcap-interface=%s --extcap-dlts\n"
  158. " %s --extcap-interface=%s --extcap-config\n"
  159. " %s --extcap-interface=%s --type dns --count 10 "
  160. "--fifo=FILENAME --capture\n", argv[0], argv[0], RANDPKT_EXTCAP_INTERFACE, argv[0], RANDPKT_EXTCAP_INTERFACE,
  161. argv[0], RANDPKT_EXTCAP_INTERFACE);
  162. extcap_help_add_header(extcap_conf, help_header);
  163. g_free(help_header);
  164. extcap_help_add_option(extcap_conf, "--help", "print this help");
  165. extcap_help_add_option(extcap_conf, "--version", "print the version");
  166. extcap_help_add_option(extcap_conf, "--maxbytes <bytes>", "max bytes per pack");
  167. extcap_help_add_option(extcap_conf, "--count <num>", "number of packets to generate");
  168. extcap_help_add_option(extcap_conf, "--delay <ms>", "milliseconds to wait after writing each packet");
  169. extcap_help_add_option(extcap_conf, "--random-type", "one random type is chosen for all packets");
  170. extcap_help_add_option(extcap_conf, "--all-random", "a random type is chosen for each packet");
  171. extcap_help_add_option(extcap_conf, "--type <type>", "the packet type");
  172. if (argc == 1) {
  173. help(extcap_conf);
  174. goto end;
  175. }
  176. while ((result = ws_getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) {
  177. switch (result) {
  178. case OPT_VERSION:
  179. extcap_version_print(extcap_conf);
  180. ret = EXIT_SUCCESS;
  181. goto end;
  182. case OPT_HELP:
  183. help(extcap_conf);
  184. ret = EXIT_SUCCESS;
  185. goto end;
  186. case OPT_MAXBYTES:
  187. if (!ws_strtou16(ws_optarg, NULL, &maxbytes)) {
  188. ws_warning("Invalid parameter maxbytes: %s (max value is %u)",
  189. ws_optarg, G_MAXUINT16);
  190. goto end;
  191. }
  192. break;
  193. case OPT_COUNT:
  194. if (!ws_strtou64(ws_optarg, NULL, &count)) {
  195. ws_warning("Invalid packet count: %s", ws_optarg);
  196. goto end;
  197. }
  198. break;
  199. case OPT_DELAY:
  200. if (!ws_strtou64(ws_optarg, NULL, &packet_delay_ms)) {
  201. ws_warning("Invalid packet delay: %s", ws_optarg);
  202. goto end;
  203. }
  204. break;
  205. case OPT_RANDOM_TYPE:
  206. random_type = TRUE;
  207. break;
  208. case OPT_ALL_RANDOM:
  209. all_random = TRUE;
  210. break;
  211. case OPT_TYPE:
  212. g_free(type);
  213. type = g_strdup(ws_optarg);
  214. break;
  215. case ':':
  216. /* missing option argument */
  217. ws_warning("Option '%s' requires an argument", argv[ws_optind - 1]);
  218. break;
  219. default:
  220. /* Handle extcap specific options */
  221. if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, ws_optarg))
  222. {
  223. ws_warning("Invalid option: %s", argv[ws_optind - 1]);
  224. goto end;
  225. }
  226. }
  227. }
  228. extcap_cmdline_debug(argv, argc);
  229. if (extcap_base_handle_interface(extcap_conf)) {
  230. ret = EXIT_SUCCESS;
  231. goto end;
  232. }
  233. if (extcap_conf->show_config) {
  234. ret = list_config(extcap_conf->interface);
  235. goto end;
  236. }
  237. /* Some sanity checks */
  238. if ((random_type) && (all_random)) {
  239. ws_warning("You can specify only one between: --random-type, --all-random");
  240. goto end;
  241. }
  242. /* Wireshark sets the type, even when random options are selected. We don't want it */
  243. if (random_type || all_random) {
  244. g_free(type);
  245. type = NULL;
  246. }
  247. err_msg = ws_init_sockets();
  248. if (err_msg != NULL) {
  249. ws_warning("ERROR: %s", err_msg);
  250. g_free(err_msg);
  251. ws_warning("%s", please_report_bug());
  252. goto end;
  253. }
  254. if (extcap_conf->capture) {
  255. if (g_strcmp0(extcap_conf->interface, RANDPKT_EXTCAP_INTERFACE)) {
  256. ws_warning("ERROR: invalid interface");
  257. goto end;
  258. }
  259. wtap_init(FALSE);
  260. if (!all_random) {
  261. produce_type = randpkt_parse_type(type);
  262. example = randpkt_find_example(produce_type);
  263. if (!example)
  264. goto end;
  265. ws_debug("Generating packets: %s", example->abbrev);
  266. randpkt_example_init(example, extcap_conf->fifo, maxbytes);
  267. randpkt_loop(example, count, packet_delay_ms);
  268. randpkt_example_close(example);
  269. } else {
  270. produce_type = randpkt_parse_type(NULL);
  271. example = randpkt_find_example(produce_type);
  272. if (!example)
  273. goto end;
  274. randpkt_example_init(example, extcap_conf->fifo, maxbytes);
  275. while (count-- > 0) {
  276. randpkt_loop(example, 1, packet_delay_ms);
  277. produce_type = randpkt_parse_type(NULL);
  278. savedump = example->dump;
  279. example = randpkt_find_example(produce_type);
  280. if (!example)
  281. goto end;
  282. example->dump = savedump;
  283. }
  284. randpkt_example_close(example);
  285. }
  286. ret = EXIT_SUCCESS;
  287. }
  288. end:
  289. /* clean up stuff */
  290. g_free(type);
  291. extcap_base_cleanup(&extcap_conf);
  292. return ret;
  293. }
  294. /*
  295. * Editor modelines - https://www.wireshark.org/tools/modelines.html
  296. *
  297. * Local variables:
  298. * c-basic-offset: 8
  299. * tab-width: 8
  300. * indent-tabs-mode: t
  301. * End:
  302. *
  303. * vi: set shiftwidth=8 tabstop=8 noexpandtab:
  304. * :indentSize=8:tabSize=8:noTabs=false:
  305. */