tcpprep_api.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2024 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "tcpprep_api.h"
  20. #include "config.h"
  21. #include "common.h"
  22. #include "tcpprep_opts.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. extern void print_comment(const char *);
  27. extern void print_info(const char *);
  28. extern void print_stats(const char *);
  29. /**
  30. * \brief Initialize a new tcpprep context
  31. *
  32. * Allocates memory and stuff like that. Always returns a buffer or completely
  33. * fails by calling exit() on malloc failure.
  34. */
  35. tcpprep_t *
  36. tcpprep_init()
  37. {
  38. tcpprep_t *ctx;
  39. int i;
  40. ctx = safe_malloc(sizeof(tcpprep_t));
  41. ctx->options = safe_malloc(sizeof(tcpprep_opt_t));
  42. ctx->options->bpf.optimize = BPF_OPTIMIZE;
  43. for (i = DEFAULT_LOW_SERVER_PORT; i <= DEFAULT_HIGH_SERVER_PORT; i++) {
  44. ctx->options->services.tcp[i] = 1;
  45. ctx->options->services.udp[i] = 1;
  46. }
  47. return ctx;
  48. }
  49. /**
  50. * Closes & free's all memory related to a tcpprep context
  51. */
  52. void
  53. tcpprep_close(tcpprep_t *ctx)
  54. {
  55. tcpr_cache_t *cache, *cache_nxt;
  56. tcpr_cidr_t *cidr, *cidr_nxt;
  57. tcpprep_opt_t *options;
  58. assert(ctx);
  59. options = ctx->options;
  60. if (options->pcap != NULL)
  61. pcap_close(options->pcap);
  62. #ifdef ENABLE_VERBOSE
  63. safe_free(options->tcpdump_args);
  64. #endif
  65. safe_free(options->comment);
  66. safe_free(options->maclist);
  67. cache = options->cachedata;
  68. while (cache != NULL) {
  69. cache_nxt = cache->next;
  70. safe_free(cache);
  71. cache = cache_nxt;
  72. }
  73. cidr = options->cidrdata;
  74. while (cidr != NULL) {
  75. cidr_nxt = cidr->next;
  76. safe_free(cidr);
  77. cidr = cidr_nxt;
  78. }
  79. if (options->xX.list)
  80. free_list(options->xX.list);
  81. if (options->xX.cidr)
  82. safe_free(options->xX.cidr);
  83. regfree(&options->preg);
  84. safe_free(options);
  85. safe_free(ctx->outfile);
  86. safe_free(ctx->pcapfile);
  87. safe_free(ctx);
  88. }
  89. /**
  90. * \brief When using AutoOpts, call to do post argument processing
  91. * Used to process the autoopts arguments
  92. */
  93. int
  94. tcpprep_post_args(tcpprep_t *ctx, int argc, char *argv[])
  95. {
  96. char myargs[MYARGS_LEN];
  97. size_t bufsize;
  98. char *endptr;
  99. char *tempstr;
  100. memset(myargs, 0, MYARGS_LEN);
  101. /* print_comment and print_info don't return */
  102. if (HAVE_OPT(PRINT_COMMENT))
  103. print_comment(OPT_ARG(PRINT_COMMENT));
  104. if (HAVE_OPT(PRINT_INFO))
  105. print_info(OPT_ARG(PRINT_INFO));
  106. if (HAVE_OPT(PRINT_STATS))
  107. print_stats(OPT_ARG(PRINT_STATS));
  108. if (!HAVE_OPT(CACHEFILE) && !HAVE_OPT(PCAP))
  109. err(-1, "Must specify an output cachefile (-o) and input pcap (-i)");
  110. if (!ctx->options->mode)
  111. err(-1, "Must specify a processing mode: -a, -c, -r, -p");
  112. #ifdef DEBUG
  113. if (HAVE_OPT(DBUG))
  114. debug = OPT_VALUE_DBUG;
  115. #endif
  116. if (HAVE_OPT(SUPPRESS_WARNINGS))
  117. print_warnings = 0;
  118. #ifdef ENABLE_VERBOSE
  119. if (HAVE_OPT(VERBOSE)) {
  120. ctx->options->verbose = 1;
  121. }
  122. if (HAVE_OPT(DECODE))
  123. ctx->tcpdump.args = safe_strdup(OPT_ARG(DECODE));
  124. #endif
  125. /*
  126. * if we are to include the cli args, then prep it for the
  127. * cache file header
  128. */
  129. if (!ctx->options->nocomment) {
  130. int i;
  131. /* copy all of our args to myargs */
  132. for (i = 1; i < argc; i++) {
  133. /* skip the -C <comment> */
  134. if (strcmp(argv[i], "-C") == 0) {
  135. i += 2;
  136. continue;
  137. }
  138. strlcat(myargs, argv[i], MYARGS_LEN);
  139. strlcat(myargs, " ", MYARGS_LEN);
  140. }
  141. /* remove trailing space */
  142. myargs[strlen(myargs) - 1] = 0;
  143. dbgx(1, "Comment args length: %zu", strlen(myargs));
  144. }
  145. /* setup or options.comment buffer so that we get args\ncomment */
  146. if (ctx->options->comment != NULL) {
  147. strlcat(myargs, "\n", MYARGS_LEN);
  148. bufsize = strlen(ctx->options->comment) + strlen(myargs) + 1;
  149. ctx->options->comment = (char *)safe_realloc(ctx->options->comment, bufsize);
  150. tempstr = strdup(ctx->options->comment);
  151. strlcpy(ctx->options->comment, myargs, bufsize);
  152. strlcat(ctx->options->comment, tempstr, bufsize);
  153. safe_free(tempstr);
  154. } else {
  155. bufsize = strlen(myargs) + 1;
  156. ctx->options->comment = (char *)safe_malloc(bufsize);
  157. strlcpy(ctx->options->comment, myargs, bufsize);
  158. }
  159. dbgx(1, "Final comment length: %zu", strlen(ctx->options->comment));
  160. /* copy over our min/max mask */
  161. ctx->options->min_mask = OPT_VALUE_MINMASK;
  162. ctx->options->max_mask = OPT_VALUE_MAXMASK;
  163. if (ctx->options->min_mask <= ctx->options->max_mask)
  164. errx(-1,
  165. "Min network mask len (%d) must be less then max network mask len (%d)",
  166. ctx->options->min_mask,
  167. ctx->options->max_mask);
  168. ctx->options->ratio = strtod(OPT_ARG(RATIO), &endptr);
  169. if (endptr == OPT_ARG(RATIO))
  170. err(-1, "Ratio supplied is not a number.");
  171. if (ctx->options->ratio < 0)
  172. err(-1, "Ratio must be a non-negative number.");
  173. return 0;
  174. }