tcpreplay_api.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2022 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 "config.h"
  20. #include "defines.h"
  21. #include "common.h"
  22. #include <ctype.h>
  23. #include <fcntl.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/types.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. #include <stdarg.h>
  31. #include "tcpreplay_api.h"
  32. #include "send_packets.h"
  33. #include "replay.h"
  34. #ifdef TCPREPLAY_EDIT
  35. #include "tcpreplay_edit_opts.h"
  36. #else
  37. #include "tcpreplay_opts.h"
  38. #endif
  39. /**
  40. * \brief Returns a string describing the last error.
  41. *
  42. * Value when the last call does not result in an error is undefined
  43. * (may be NULL, may be garbage)
  44. */
  45. char *
  46. tcpreplay_geterr(tcpreplay_t *ctx)
  47. {
  48. assert(ctx);
  49. return(ctx->errstr);
  50. }
  51. /**
  52. * \brief Returns a string describing the last warning.
  53. *
  54. * Value when the last call does not result in an warning is undefined
  55. * (may be NULL, may be garbage)
  56. */
  57. char *
  58. tcpreplay_getwarn(tcpreplay_t *ctx)
  59. {
  60. assert(ctx);
  61. return(ctx->warnstr);
  62. }
  63. /**
  64. * \brief Initialize a new tcpreplay context
  65. *
  66. * Allocates memory and stuff like that. Always returns a buffer or completely
  67. * fails by calling exit() on malloc failure.
  68. */
  69. tcpreplay_t *
  70. tcpreplay_init()
  71. {
  72. tcpreplay_t *ctx;
  73. /* allocations will reset everything to zeros */
  74. ctx = safe_malloc(sizeof(tcpreplay_t));
  75. ctx->options = safe_malloc(sizeof(tcpreplay_opt_t));
  76. /* replay packets only once */
  77. ctx->options->loop = 1;
  78. /* Default mode is to replay pcap once in real-time */
  79. ctx->options->speed.mode = speed_multiplier;
  80. ctx->options->speed.multiplier = 1.0;
  81. /* Set the default timing method */
  82. ctx->options->accurate = accurate_gtod;
  83. /* set the default MTU size */
  84. ctx->options->mtu = DEFAULT_MTU;
  85. /* disable periodic statistics */
  86. ctx->options->stats = -1;
  87. /* disable limit send */
  88. ctx->options->limit_send = -1;
  89. /* default unique-loops */
  90. ctx->options->unique_loops = 1.0;
  91. #ifdef ENABLE_VERBOSE
  92. /* clear out tcpdump struct */
  93. ctx->options->tcpdump = (tcpdump_t *)safe_malloc(sizeof(tcpdump_t));
  94. #endif
  95. if (fcntl(STDERR_FILENO, F_SETFL, O_NONBLOCK) < 0)
  96. tcpreplay_setwarn(ctx, "Unable to set STDERR to non-blocking: %s", strerror(errno));
  97. #ifdef ENABLE_PCAP_FINDALLDEVS
  98. ctx->intlist = get_interface_list();
  99. #else
  100. ctx->intlist = NULL;
  101. #endif
  102. /* set up flows - on by default*/
  103. ctx->options->flow_stats = 1;
  104. ctx->flow_hash_table = flow_hash_table_init(DEFAULT_FLOW_HASH_BUCKET_SIZE);
  105. ctx->sp_type = SP_TYPE_NONE;
  106. ctx->intf1dlt = -1;
  107. ctx->intf2dlt = -1;
  108. ctx->abort = false;
  109. ctx->first_time = true;
  110. return ctx;
  111. }
  112. /**
  113. * \brief Parses the GNU AutoOpts options for tcpreplay
  114. *
  115. * If you're using AutoOpts with tcpreplay_api, then just call this after
  116. * optionProcess() and it will parse all the options for you. As always,
  117. * returns 0 on success, and -1 on error & -2 on warning.
  118. */
  119. int
  120. tcpreplay_post_args(tcpreplay_t *ctx, int argc)
  121. {
  122. char *temp, *intname;
  123. char *ebuf;
  124. tcpreplay_opt_t *options;
  125. int warn = 0;
  126. float n;
  127. int ret = 0;
  128. options = ctx->options;
  129. dbg(2, "tcpreplay_post_args: parsing command arguments");
  130. ebuf = safe_malloc(SENDPACKET_ERRBUF_SIZE);
  131. #ifdef DEBUG
  132. if (HAVE_OPT(DBUG))
  133. debug = OPT_VALUE_DBUG;
  134. #else
  135. if (HAVE_OPT(DBUG)) {
  136. warn ++;
  137. tcpreplay_setwarn(ctx, "%s", "not configured with --enable-debug. Debugging disabled.");
  138. }
  139. #endif
  140. options->loop = OPT_VALUE_LOOP;
  141. options->loopdelay_ms = OPT_VALUE_LOOPDELAY_MS;
  142. if (HAVE_OPT(LIMIT))
  143. options->limit_send = OPT_VALUE_LIMIT;
  144. if (HAVE_OPT(DURATION))
  145. options->limit_time = OPT_VALUE_DURATION;
  146. if (HAVE_OPT(TOPSPEED)) {
  147. options->speed.mode = speed_topspeed;
  148. options->speed.speed = 0;
  149. } else if (HAVE_OPT(PPS)) {
  150. n = atof(OPT_ARG(PPS));
  151. if (!n) {
  152. tcpreplay_seterr(ctx, "invalid pps value '%s'", OPT_ARG(PPS));
  153. ret = -1;
  154. goto out;
  155. }
  156. options->speed.speed = (COUNTER)(n * 60.0 * 60.0); /* convert to packets per hour */
  157. options->speed.mode = speed_packetrate;
  158. options->speed.pps_multi = OPT_VALUE_PPS_MULTI;
  159. } else if (HAVE_OPT(ONEATATIME)) {
  160. options->speed.mode = speed_oneatatime;
  161. options->speed.speed = 0;
  162. } else if (HAVE_OPT(MBPS)) {
  163. n = atof(OPT_ARG(MBPS));
  164. if (n) {
  165. options->speed.mode = speed_mbpsrate;
  166. options->speed.speed = (COUNTER)(n * 1000000.0); /* convert to bps */
  167. } else {
  168. options->speed.mode = speed_topspeed;
  169. options->speed.speed = 0;
  170. }
  171. } else if (HAVE_OPT(MULTIPLIER)) {
  172. options->speed.mode = speed_multiplier;
  173. options->speed.multiplier = atof(OPT_ARG(MULTIPLIER));
  174. }
  175. if (HAVE_OPT(MAXSLEEP)) {
  176. options->maxsleep.tv_sec = OPT_VALUE_MAXSLEEP / 1000;
  177. options->maxsleep.tv_nsec = (OPT_VALUE_MAXSLEEP % 1000) * 1000 * 1000;
  178. }
  179. #ifdef ENABLE_VERBOSE
  180. if (HAVE_OPT(VERBOSE))
  181. options->verbose = 1;
  182. if (HAVE_OPT(DECODE))
  183. options->tcpdump->args = safe_strdup(OPT_ARG(DECODE));
  184. #endif
  185. if (HAVE_OPT(STATS))
  186. options->stats = OPT_VALUE_STATS;
  187. /*
  188. * preloading the pcap before the first run
  189. */
  190. if (HAVE_OPT(PRELOAD_PCAP)) {
  191. options->preload_pcap = true;
  192. }
  193. #ifdef TCPREPLAY_EDIT
  194. if (HAVE_OPT(PRELOAD_PCAP) && OPT_VALUE_LOOP > 1) {
  195. tcpreplay_seterr(ctx,
  196. "%s",
  197. "tcpreplay_edit --loop (-l) and --preload_pcap (-K) options are mutually exclusive");
  198. ret = -1;
  199. goto out;
  200. }
  201. #endif
  202. /* Dual file mode */
  203. if (HAVE_OPT(DUALFILE)) {
  204. options->dualfile = true;
  205. if (argc < 2) {
  206. tcpreplay_seterr(ctx, "%s", "--dualfile mode requires at least two pcap files");
  207. ret = -1;
  208. goto out;
  209. }
  210. if (argc % 2 != 0) {
  211. tcpreplay_seterr(ctx, "%s", "--dualfile mode requires an even number of pcap files");
  212. ret = -1;
  213. goto out;
  214. }
  215. }
  216. #ifdef HAVE_NETMAP
  217. options->netmap_delay = OPT_VALUE_NM_DELAY;
  218. #endif
  219. if (HAVE_OPT(NETMAP)) {
  220. #ifdef HAVE_NETMAP
  221. options->netmap = 1;
  222. ctx->sp_type = SP_TYPE_NETMAP;
  223. #else
  224. err(-1, "--netmap feature was not compiled in. See INSTALL.");
  225. #endif
  226. }
  227. if (HAVE_OPT(UNIQUE_IP))
  228. options->unique_ip = 1;
  229. if (HAVE_OPT(UNIQUE_IP_LOOPS)) {
  230. options->unique_loops = atof(OPT_ARG(UNIQUE_IP_LOOPS));
  231. if (options->unique_loops < 1.0) {
  232. tcpreplay_seterr(ctx, "%s", "--unique-ip-loops requires loop count >= 1.0");
  233. ret = -1;
  234. goto out;
  235. }
  236. }
  237. /* flow statistics */
  238. if (HAVE_OPT(NO_FLOW_STATS))
  239. options->flow_stats = 0;
  240. if (HAVE_OPT(FLOW_EXPIRY)) {
  241. options->flow_expiry = OPT_VALUE_FLOW_EXPIRY;
  242. }
  243. if (HAVE_OPT(TIMER)) {
  244. if (strcmp(OPT_ARG(TIMER), "select") == 0) {
  245. #ifdef HAVE_SELECT
  246. options->accurate = accurate_select;
  247. #else
  248. tcpreplay_seterr(ctx, "%s", "tcpreplay_api not compiled with select support");
  249. ret = -1;
  250. goto out;
  251. #endif
  252. } else if (strcmp(OPT_ARG(TIMER), "ioport") == 0) {
  253. #if defined HAVE_IOPORT_SLEEP__
  254. options.accurate = ACCURATE_IOPORT;
  255. ioport_sleep_init();
  256. #else
  257. err(-1, "tcpreplay not compiled with IO Port 0x80 support");
  258. #endif
  259. } else if (strcmp(OPT_ARG(TIMER), "gtod") == 0) {
  260. options->accurate = accurate_gtod;
  261. } else if (strcmp(OPT_ARG(TIMER), "nano") == 0) {
  262. options->accurate = accurate_nanosleep;
  263. } else if (strcmp(OPT_ARG(TIMER), "abstime") == 0) {
  264. tcpreplay_seterr(ctx, "%s", "abstime is deprecated");
  265. ret = -1;
  266. goto out;
  267. } else {
  268. tcpreplay_seterr(ctx, "Unsupported timer mode: %s", OPT_ARG(TIMER));
  269. ret = -1;
  270. goto out;
  271. }
  272. }
  273. #ifdef HAVE_RDTSC
  274. if (HAVE_OPT(RDTSC_CLICKS)) {
  275. rdtsc_calibrate(OPT_VALUE_RDTSC_CLICKS);
  276. }
  277. #endif
  278. if (HAVE_OPT(PKTLEN)) {
  279. options->use_pkthdr_len = true;
  280. warn ++;
  281. tcpreplay_setwarn(ctx, "%s", "--pktlen may cause problems. Use with caution.");
  282. }
  283. if ((intname = get_interface(ctx->intlist, OPT_ARG(INTF1))) == NULL) {
  284. if (!strncmp(OPT_ARG(INTF1), "netmap:", 7) || !strncmp(OPT_ARG(INTF1), "vale", 4))
  285. tcpreplay_seterr(ctx, "Unable to connect to netmap interface %s. Ensure netmap module is installed (see INSTALL).",
  286. OPT_ARG(INTF1));
  287. else
  288. tcpreplay_seterr(ctx, "Invalid interface name/alias: %s", OPT_ARG(INTF1));
  289. ret = -1;
  290. goto out;
  291. }
  292. if (!strncmp(intname, "netmap:", 7) || !strncmp(intname, "vale:", 5)) {
  293. #ifdef HAVE_NETMAP
  294. options->netmap = 1;
  295. ctx->sp_type = SP_TYPE_NETMAP;
  296. #else
  297. tcpreplay_seterr(ctx, "%s", "tcpreplay_api not compiled with netmap support");
  298. ret = -1;
  299. goto out;
  300. #endif
  301. }
  302. options->intf1_name = safe_strdup(intname);
  303. /* open interfaces for writing */
  304. if ((ctx->intf1 = sendpacket_open(options->intf1_name, ebuf, TCPR_DIR_C2S, ctx->sp_type, ctx)) == NULL) {
  305. tcpreplay_seterr(ctx, "Can't open %s: %s", options->intf1_name, ebuf);
  306. ret = -1;
  307. goto out;
  308. }
  309. #if defined HAVE_NETMAP
  310. ctx->intf1->netmap_delay = ctx->options->netmap_delay;
  311. #endif
  312. ctx->intf1dlt = sendpacket_get_dlt(ctx->intf1);
  313. if (HAVE_OPT(INTF2)) {
  314. if (!HAVE_OPT(CACHEFILE) && !HAVE_OPT(DUALFILE)) {
  315. tcpreplay_seterr(ctx, "--intf2=%s requires either --cachefile or --dualfile", OPT_ARG(INTF2));
  316. ret = -1;
  317. goto out;
  318. }
  319. if ((intname = get_interface(ctx->intlist, OPT_ARG(INTF2))) == NULL) {
  320. tcpreplay_seterr(ctx, "Invalid interface name/alias: %s", OPT_ARG(INTF2));
  321. ret = -1;
  322. goto out;
  323. }
  324. options->intf2_name = safe_strdup(intname);
  325. /* open interface for writing */
  326. if ((ctx->intf2 = sendpacket_open(options->intf2_name, ebuf, TCPR_DIR_S2C, ctx->sp_type, ctx)) == NULL) {
  327. tcpreplay_seterr(ctx, "Can't open %s: %s", options->intf2_name, ebuf);
  328. }
  329. #if defined HAVE_NETMAP
  330. ctx->intf2->netmap_delay = ctx->options->netmap_delay;
  331. #endif
  332. ctx->intf2dlt = sendpacket_get_dlt(ctx->intf2);
  333. if (ctx->intf2dlt != ctx->intf1dlt) {
  334. tcpreplay_seterr(ctx, "DLT type mismatch for %s (%s) and %s (%s)",
  335. options->intf1_name, pcap_datalink_val_to_name(ctx->intf1dlt),
  336. options->intf2_name, pcap_datalink_val_to_name(ctx->intf2dlt));
  337. ret = -1;
  338. goto out;
  339. }
  340. }
  341. if (HAVE_OPT(CACHEFILE)) {
  342. temp = safe_strdup(OPT_ARG(CACHEFILE));
  343. options->cache_packets = read_cache(&options->cachedata, temp,
  344. &options->comment);
  345. safe_free(temp);
  346. }
  347. /* return -2 on warnings */
  348. if (warn > 0)
  349. ret = -2;
  350. out:
  351. safe_free(ebuf);
  352. return ret;
  353. }
  354. /**
  355. * Closes & free's all memory related to a tcpreplay context
  356. */
  357. void
  358. tcpreplay_close(tcpreplay_t *ctx)
  359. {
  360. tcpreplay_opt_t *options;
  361. interface_list_t *intlist, *intlistnext;
  362. packet_cache_t *packet_cache, *next;
  363. assert(ctx);
  364. assert(ctx->options);
  365. options = ctx->options;
  366. safe_free(options->intf1_name);
  367. safe_free(options->intf2_name);
  368. sendpacket_close(ctx->intf1);
  369. if (ctx->intf2 != NULL)
  370. sendpacket_close(ctx->intf2);
  371. safe_free(options->cachedata);
  372. safe_free(options->comment);
  373. #ifdef ENABLE_VERBOSE
  374. safe_free(options->tcpdump_args);
  375. tcpdump_close(options->tcpdump);
  376. #endif
  377. /* free the flow hash table */
  378. flow_hash_table_release(ctx->flow_hash_table);
  379. /* free the file cache */
  380. packet_cache = options->file_cache->packet_cache;
  381. while (packet_cache != NULL) {
  382. next = packet_cache->next;
  383. safe_free(packet_cache->pktdata);
  384. safe_free(packet_cache);
  385. packet_cache = next;
  386. }
  387. /* free our interface list */
  388. if (ctx->intlist != NULL) {
  389. intlist = ctx->intlist;
  390. while (intlist != NULL) {
  391. intlistnext = intlist->next;
  392. safe_free(intlist);
  393. intlist = intlistnext;
  394. }
  395. }
  396. }
  397. /**
  398. * \brief Specifies an interface to use for sending.
  399. *
  400. * You may call this up to two (2) times with different interfaces
  401. * when using a tcpprep cache file or dualfile mode. Note, both interfaces
  402. * must use the same DLT type
  403. */
  404. int
  405. tcpreplay_set_interface(tcpreplay_t *ctx, tcpreplay_intf intf, char *value)
  406. {
  407. static int int1dlt = -1, int2dlt = -1;
  408. char *intname;
  409. char *ebuf;
  410. int ret = 0;
  411. assert(ctx);
  412. assert(value);
  413. ebuf = safe_malloc(SENDPACKET_ERRBUF_SIZE);
  414. if (intf == intf1) {
  415. if ((intname = get_interface(ctx->intlist, value)) == NULL) {
  416. if (!strncmp(OPT_ARG(INTF1), "netmap:", 7) || !strncmp(OPT_ARG(INTF1), "vale", 4))
  417. tcpreplay_seterr(ctx, "Unable to connect to netmap interface %s. Ensure netmap module is installed (see INSTALL).",
  418. value);
  419. else
  420. tcpreplay_seterr(ctx, "Invalid interface name/alias: %s", value);
  421. ret = -1;
  422. goto out;
  423. }
  424. ctx->options->intf1_name = safe_strdup(intname);
  425. /* open interfaces for writing */
  426. if ((ctx->intf1 = sendpacket_open(ctx->options->intf1_name, ebuf, TCPR_DIR_C2S, ctx->sp_type, ctx)) == NULL) {
  427. tcpreplay_seterr(ctx, "Can't open %s: %s", ctx->options->intf1_name, ebuf);
  428. ret = -1;
  429. goto out;
  430. }
  431. int1dlt = sendpacket_get_dlt(ctx->intf1);
  432. } else if (intf == intf2) {
  433. if ((intname = get_interface(ctx->intlist, value)) == NULL) {
  434. tcpreplay_seterr(ctx, "Invalid interface name/alias: %s", ctx->options->intf2_name);
  435. ret = -1;
  436. goto out;
  437. }
  438. ctx->options->intf2_name = safe_strdup(intname);
  439. /* open interface for writing */
  440. if ((ctx->intf2 = sendpacket_open(ctx->options->intf2_name, ebuf, TCPR_DIR_S2C, ctx->sp_type, ctx)) == NULL) {
  441. tcpreplay_seterr(ctx, "Can't open %s: %s", ctx->options->intf2_name, ebuf);
  442. ret = -1;
  443. goto out;
  444. }
  445. int2dlt = sendpacket_get_dlt(ctx->intf2);
  446. }
  447. /*
  448. * If both interfaces are selected, then make sure both interfaces use
  449. * the same DLT type
  450. */
  451. if (int1dlt != -1 && int2dlt != -1) {
  452. if (int1dlt != int2dlt) {
  453. tcpreplay_seterr(ctx, "DLT type mismatch for %s (%s) and %s (%s)",
  454. ctx->options->intf1_name, pcap_datalink_val_to_name(int1dlt),
  455. ctx->options->intf2_name, pcap_datalink_val_to_name(int2dlt));
  456. ret = -1;
  457. goto out;
  458. }
  459. }
  460. out:
  461. safe_free(ebuf);
  462. return ret;
  463. }
  464. /**
  465. * Set the replay speed mode.
  466. */
  467. int
  468. tcpreplay_set_speed_mode(tcpreplay_t *ctx, tcpreplay_speed_mode value)
  469. {
  470. assert(ctx);
  471. ctx->options->speed.mode = value;
  472. return 0;
  473. }
  474. /**
  475. * Set the approprate speed value. Value is interpreted based on
  476. * how tcpreplay_set_speed_mode() value
  477. */
  478. int
  479. tcpreplay_set_speed_speed(tcpreplay_t *ctx, COUNTER value)
  480. {
  481. assert(ctx);
  482. ctx->options->speed.speed = value;
  483. return 0;
  484. }
  485. /**
  486. * Sending under packets/sec requires an integer value, not float.
  487. * you must first call tcpreplay_set_speed_mode(ctx, speed_packetrate)
  488. */
  489. int
  490. tcpreplay_set_speed_pps_multi(tcpreplay_t *ctx, int value)
  491. {
  492. assert(ctx);
  493. ctx->options->speed.pps_multi = value;
  494. return 0;
  495. }
  496. /**
  497. * How many times should we loop through all the pcap files?
  498. */
  499. int
  500. tcpreplay_set_loop(tcpreplay_t *ctx, u_int32_t value)
  501. {
  502. assert(ctx);
  503. ctx->options->loop = value;
  504. return 0;
  505. }
  506. /**
  507. * Set the unique IP address flag
  508. */
  509. int
  510. tcpreplay_set_unique_ip(tcpreplay_t *ctx, bool value)
  511. {
  512. assert(ctx);
  513. ctx->options->unique_ip = value;
  514. return 0;
  515. }
  516. int
  517. tcpreplay_set_unique_ip_loops(tcpreplay_t *ctx, int value)
  518. {
  519. assert(ctx);
  520. ctx->options->unique_loops = value;
  521. return 0;
  522. }
  523. /**
  524. * Set netmap mode
  525. */
  526. int
  527. tcpreplay_set_netmap(_U_ tcpreplay_t *ctx, _U_ bool value)
  528. {
  529. assert(ctx);
  530. #ifdef HAVE_NETMAP
  531. ctx->options->netmap = value;
  532. return 0;
  533. #else
  534. warn("netmap not compiled in");
  535. return -1;
  536. #endif
  537. }
  538. /**
  539. * Tell tcpreplay to ignore the snaplen (default) and use the "actual"
  540. * packet len instead
  541. */
  542. int
  543. tcpreplay_set_use_pkthdr_len(tcpreplay_t *ctx, bool value)
  544. {
  545. assert(ctx);
  546. ctx->options->use_pkthdr_len = value;
  547. return 0;
  548. }
  549. /**
  550. * Override the outbound MTU
  551. */
  552. int
  553. tcpreplay_set_mtu(tcpreplay_t *ctx, int value)
  554. {
  555. assert(ctx);
  556. ctx->options->mtu = value;
  557. return 0;
  558. }
  559. /**
  560. * Sets the accurate timing mode
  561. */
  562. int
  563. tcpreplay_set_accurate(tcpreplay_t *ctx, tcpreplay_accurate value)
  564. {
  565. assert(ctx);
  566. ctx->options->accurate = value;
  567. return 0;
  568. }
  569. /**
  570. * Sets the number of seconds between printing stats
  571. */
  572. int
  573. tcpreplay_set_stats(tcpreplay_t *ctx, int value)
  574. {
  575. assert(ctx);
  576. ctx->options->stats = value;
  577. return 0;
  578. }
  579. /**
  580. * \brief Enable or disable dual file mode
  581. *
  582. * In dual file mode, we read two files at the same time and use
  583. * one file for each interface.
  584. */
  585. int
  586. tcpreplay_set_dualfile(tcpreplay_t *ctx, bool value)
  587. {
  588. assert(ctx);
  589. ctx->options->dualfile = value;
  590. return 0;
  591. }
  592. /**
  593. * \brief Enable or disable preloading the file cache
  594. *
  595. * Note: This is a global option and forces all pcaps
  596. * to be preloaded for this context. If you turn this
  597. * on, then it forces set_file_cache(true)
  598. */
  599. int
  600. tcpreplay_set_preload_pcap(tcpreplay_t *ctx, bool value)
  601. {
  602. assert(ctx);
  603. ctx->options->preload_pcap = value;
  604. return 0;
  605. }
  606. /**
  607. * \brief Add a pcap file to be sent via tcpreplay
  608. *
  609. * One or more pcap files can be added. Each file will be replayed
  610. * in order
  611. */
  612. int
  613. tcpreplay_add_pcapfile(tcpreplay_t *ctx, char *pcap_file)
  614. {
  615. assert(ctx);
  616. assert(pcap_file);
  617. if (ctx->options->source_cnt < MAX_FILES) {
  618. ctx->options->sources[ctx->options->source_cnt].filename = safe_strdup(pcap_file);
  619. ctx->options->sources[ctx->options->source_cnt].type = source_filename;
  620. /*
  621. * prepare the cache info data struct. This doesn't actually enable
  622. * file caching for this pcap (that is controlled globally via
  623. * tcpreplay_set_file_cache())
  624. */
  625. ctx->options->file_cache[ctx->options->source_cnt].index = ctx->options->source_cnt;
  626. ctx->options->file_cache[ctx->options->source_cnt].cached = false;
  627. ctx->options->file_cache[ctx->options->source_cnt].packet_cache = NULL;
  628. ctx->options->source_cnt += 1;
  629. } else {
  630. tcpreplay_seterr(ctx, "Unable to add more then %u files", MAX_FILES);
  631. return -1;
  632. }
  633. return 0;
  634. }
  635. /**
  636. * Limit the total number of packets to send
  637. */
  638. int
  639. tcpreplay_set_limit_send(tcpreplay_t *ctx, COUNTER value)
  640. {
  641. assert(ctx);
  642. ctx->options->limit_send = value;
  643. return 0;
  644. }
  645. /**
  646. * \brief Specify the tcpprep cache file to use for replaying with two NICs
  647. *
  648. * Note: this only works if you have a single pcap file
  649. * returns -1 on error
  650. */
  651. int
  652. tcpreplay_set_tcpprep_cache(tcpreplay_t *ctx, char *file)
  653. {
  654. assert(ctx);
  655. char *tcpprep_file;
  656. if (ctx->options->source_cnt > 1) {
  657. tcpreplay_seterr(ctx, "%s", "Unable to use tcpprep cache file with a single pcap file");
  658. return -1;
  659. }
  660. tcpprep_file = safe_strdup(file);
  661. ctx->options->cache_packets = read_cache(&ctx->options->cachedata,
  662. tcpprep_file, &ctx->options->comment);
  663. free(tcpprep_file);
  664. return 0;
  665. }
  666. /*
  667. * Verbose mode requires fork() and tcpdump binary, hence won't work
  668. * under Win32 without Cygwin
  669. */
  670. /**
  671. * Enable verbose mode
  672. */
  673. int
  674. tcpreplay_set_verbose(tcpreplay_t *ctx, bool value _U_)
  675. {
  676. assert(ctx);
  677. #ifdef ENABLE_VERBOSE
  678. ctx->options->verbose = value;
  679. return 0;
  680. #else
  681. tcpreplay_seterr(ctx, "%s", "verbose mode not supported");
  682. return -1;
  683. #endif
  684. }
  685. /**
  686. * \brief Set the arguments to be passed to tcpdump
  687. *
  688. * Specify the additional argument to be passed to tcpdump when enabling
  689. * verbose mode. See TCPDUMP_ARGS in tcpdump.h for the default options
  690. */
  691. int
  692. tcpreplay_set_tcpdump_args(tcpreplay_t *ctx, char *value _U_)
  693. {
  694. assert(ctx);
  695. #ifdef ENABLE_VERBOSE
  696. assert(value);
  697. ctx->options->tcpdump_args = safe_strdup(value);
  698. return 0;
  699. #else
  700. tcpreplay_seterr(ctx, "%s", "verbose mode not supported");
  701. return -1;
  702. #endif
  703. }
  704. /**
  705. * \brief Set the path to the tcpdump binary
  706. *
  707. * In order to support the verbose feature, tcpreplay needs to know where
  708. * tcpdump lives
  709. */
  710. int
  711. tcpreplay_set_tcpdump(tcpreplay_t *ctx, tcpdump_t *value _U_)
  712. {
  713. assert(ctx);
  714. #ifdef ENABLE_VERBOSE
  715. assert(value);
  716. ctx->options->verbose = true;
  717. ctx->options->tcpdump = value;
  718. return 0;
  719. #else
  720. tcpreplay_seterr(ctx, "%s", "verbose mode not supported");
  721. return -1;
  722. #endif
  723. }
  724. /**
  725. * \brief Set the callback function for handing manual iteration
  726. *
  727. * Obviously for this to work, you need to first set speed_mode = speed_oneatatime
  728. * returns 0 on success, < 0 on error
  729. */
  730. int
  731. tcpreplay_set_manual_callback(tcpreplay_t *ctx, tcpreplay_manual_callback callback)
  732. {
  733. assert(ctx);
  734. assert(callback);
  735. if (ctx->options->speed.mode != speed_oneatatime) {
  736. tcpreplay_seterr(ctx, "%s",
  737. "Unable to set manual callback because speed mode is not 'speed_oneatatime'");
  738. return -1;
  739. }
  740. ctx->options->speed.manual_callback = callback;
  741. return 0;
  742. }
  743. /**
  744. * \brief return the number of packets sent so far
  745. */
  746. COUNTER
  747. tcpreplay_get_pkts_sent(tcpreplay_t *ctx)
  748. {
  749. assert(ctx);
  750. ctx->static_stats.pkts_sent = ctx->stats.pkts_sent;
  751. return ctx->static_stats.pkts_sent;
  752. }
  753. /**
  754. * \brief return the number of bytes sent so far
  755. */
  756. COUNTER
  757. tcpreplay_get_bytes_sent(tcpreplay_t *ctx)
  758. {
  759. assert(ctx);
  760. ctx->static_stats.bytes_sent = ctx->stats.bytes_sent;
  761. return ctx->static_stats.bytes_sent;
  762. }
  763. /**
  764. * \brief return the number of failed attempts to send a packet
  765. */
  766. COUNTER
  767. tcpreplay_get_failed(tcpreplay_t *ctx)
  768. {
  769. assert(ctx);
  770. ctx->static_stats.failed = ctx->stats.failed;
  771. return ctx->static_stats.failed;
  772. }
  773. /**
  774. * \brief returns a pointer to the timeval structure of when replay first started
  775. */
  776. const struct timeval *
  777. tcpreplay_get_start_time(tcpreplay_t *ctx)
  778. {
  779. assert(ctx);
  780. TIMEVAL_SET(&ctx->static_stats.start_time, &ctx->stats.start_time);
  781. return &ctx->static_stats.start_time;
  782. }
  783. /**
  784. * \brief returns a pointer to the timeval structure of when replay finished
  785. */
  786. const struct timeval *
  787. tcpreplay_get_end_time(tcpreplay_t *ctx)
  788. {
  789. assert(ctx);
  790. TIMEVAL_SET(&ctx->static_stats.end_time, &ctx->stats.end_time);
  791. return &ctx->static_stats.end_time;
  792. }
  793. /**
  794. * \brief Internal function to set the tcpreplay error string
  795. *
  796. * Used to set the error string when there is an error, result is retrieved
  797. * using tcpedit_geterr(). You shouldn't ever actually call this, but use
  798. * tcpreplay_seterr() which is a macro wrapping this instead.
  799. */
  800. void
  801. __tcpreplay_seterr(tcpreplay_t *ctx, const char *func,
  802. const int line, const char *file, const char *fmt, ...)
  803. {
  804. va_list ap;
  805. char errormsg[TCPREPLAY_ERRSTR_LEN - 32];
  806. assert(ctx);
  807. assert(file);
  808. assert(func);
  809. assert(line);
  810. va_start(ap, fmt);
  811. if (fmt != NULL)
  812. (void)vsnprintf(errormsg, sizeof(errormsg), fmt, ap);
  813. va_end(ap);
  814. #ifdef DEBUG
  815. snprintf(ctx->errstr, sizeof(ctx->errstr), "From %s:%s() line %d:\n%s",
  816. file, func, line, errormsg);
  817. #else
  818. snprintf(ctx->errstr, sizeof(ctx->errstr), "%s", errormsg);
  819. #endif
  820. }
  821. /**
  822. * \brief Internal function to set the tcpedit warning string
  823. *
  824. * Used to set the warning string when there is an non-fatal issue, result is retrieved
  825. * using tcpedit_getwarn().
  826. */
  827. void
  828. tcpreplay_setwarn(tcpreplay_t *ctx, const char *fmt, ...)
  829. {
  830. va_list ap;
  831. assert(ctx);
  832. va_start(ap, fmt);
  833. if (fmt != NULL)
  834. (void)vsnprintf(ctx->warnstr, sizeof(ctx->warnstr), fmt, ap);
  835. va_end(ap);
  836. }
  837. /**
  838. * \brief Does all the prep work before calling tcpreplay_replay()
  839. *
  840. * Technically this validates our config options, preloads the tcpprep
  841. * cache file, loads the packet cache and anything else which might
  842. * cause a delay for starting to send packets with tcpreplay_replay()
  843. */
  844. int
  845. tcpreplay_prepare(tcpreplay_t *ctx)
  846. {
  847. char *intname, *ebuf;
  848. int int1dlt, int2dlt, i;
  849. int ret = 0;
  850. assert(ctx);
  851. ebuf = safe_malloc(SENDPACKET_ERRBUF_SIZE);
  852. /*
  853. * First, process the validations, basically the same we do in
  854. * tcpreplay_post_args() and AutoOpts
  855. */
  856. if (ctx->options->intf1_name == NULL) {
  857. tcpreplay_seterr(ctx, "%s", "You must specify at least one network interface");
  858. ret = -1;
  859. goto out;
  860. }
  861. if (ctx->options->source_cnt == 0) {
  862. tcpreplay_seterr(ctx, "%s", "You must specify at least one source pcap");
  863. ret = -1;
  864. goto out;
  865. }
  866. if (ctx->options->dualfile) {
  867. if (!(ctx->options->source_cnt >= 2)) {
  868. tcpreplay_seterr(ctx, "%s", "Dual file mode requires 2 or more pcap files");
  869. ret = -1;
  870. goto out;
  871. }
  872. if (ctx->options->source_cnt % 2 != 0) {
  873. tcpreplay_seterr(ctx, "%s", "Dual file mode requires an even number of pcap files");
  874. ret = -1;
  875. goto out;
  876. }
  877. }
  878. if (ctx->options->dualfile && ctx->options->cachedata != NULL) {
  879. tcpreplay_seterr(ctx, "%s", "Can't use dual file mode and tcpprep cache file together");
  880. ret = -1;
  881. goto out;
  882. }
  883. if ((ctx->options->dualfile || ctx->options->cachedata != NULL) &&
  884. ctx->options->intf2_name == NULL) {
  885. tcpreplay_seterr(ctx, "%s", "dual file mode and tcpprep cache files require two interfaces");
  886. }
  887. #ifndef HAVE_SELECT
  888. if (ctx->options->accurate == accurate_select) {
  889. tcpreplay_seterr(ctx, "%s", "tcpreplay_api not compiled with select support");
  890. ret = -1;
  891. goto out;
  892. }
  893. #endif
  894. if ((intname = get_interface(ctx->intlist, ctx->options->intf1_name)) == NULL) {
  895. if (!strncmp(OPT_ARG(INTF1), "netmap:", 7) || !strncmp(OPT_ARG(INTF1), "vale", 4))
  896. tcpreplay_seterr(ctx, "Unable to connect to netmap interface %s. Ensure netmap module is installed (see INSTALL).",
  897. OPT_ARG(INTF1));
  898. else
  899. tcpreplay_seterr(ctx, "Invalid interface name/alias: %s", OPT_ARG(INTF1));
  900. ret = -1;
  901. goto out;
  902. }
  903. /* open interfaces for writing */
  904. if ((ctx->intf1 = sendpacket_open(ctx->options->intf1_name, ebuf, TCPR_DIR_C2S, ctx->sp_type, ctx)) == NULL) {
  905. tcpreplay_seterr(ctx, "Can't open %s: %s", ctx->options->intf1_name, ebuf);
  906. ret = -1;
  907. goto out;
  908. }
  909. int1dlt = sendpacket_get_dlt(ctx->intf1);
  910. if (ctx->options->intf2_name != NULL) {
  911. if ((intname = get_interface(ctx->intlist, ctx->options->intf2_name)) == NULL) {
  912. tcpreplay_seterr(ctx, "Invalid interface name/alias: %s", OPT_ARG(INTF2));
  913. ret = -1;
  914. goto out;
  915. }
  916. /* open interfaces for writing */
  917. if ((ctx->intf2 = sendpacket_open(ctx->options->intf2_name, ebuf, TCPR_DIR_C2S, ctx->sp_type, ctx)) == NULL) {
  918. tcpreplay_seterr(ctx, "Can't open %s: %s", ctx->options->intf2_name, ebuf);
  919. ret = -1;
  920. goto out;
  921. }
  922. int2dlt = sendpacket_get_dlt(ctx->intf2);
  923. if (int2dlt != int1dlt) {
  924. tcpreplay_seterr(ctx, "DLT type mismatch for %s (%s) and %s (%s)",
  925. ctx->options->intf1_name, pcap_datalink_val_to_name(int1dlt),
  926. ctx->options->intf2_name, pcap_datalink_val_to_name(int2dlt));
  927. ret = -1;
  928. goto out;
  929. }
  930. }
  931. /*
  932. * Setup up the file cache, if required
  933. */
  934. if (ctx->options->preload_pcap) {
  935. /* Initialise each of the file cache structures */
  936. for (i = 0; i < ctx->options->source_cnt; i++) {
  937. ctx->options->file_cache[i].index = i;
  938. ctx->options->file_cache[i].cached = FALSE;
  939. ctx->options->file_cache[i].packet_cache = NULL;
  940. }
  941. }
  942. out:
  943. safe_free(ebuf);
  944. return ret;
  945. }
  946. /**
  947. * \brief sends the traffic out the interfaces
  948. *
  949. * Designed to be called in a separate thread if you need to. Blocks until
  950. * the replay is complete or you call tcpreplay_abort() in another thread.
  951. */
  952. int
  953. tcpreplay_replay(tcpreplay_t *ctx)
  954. {
  955. int rcode;
  956. COUNTER loop, total_loops;
  957. assert(ctx);
  958. if (!ctx->options->source_cnt) {
  959. tcpreplay_seterr(ctx, "invalid source count: %d", ctx->options->source_cnt);
  960. return -1;
  961. }
  962. if (ctx->options->dualfile && ctx->options->source_cnt < 2) {
  963. tcpreplay_seterr(ctx, "invalid dualfile source count: %d", ctx->options->source_cnt);
  964. return -1;
  965. }
  966. init_timestamp(&ctx->stats.start_time);
  967. init_timestamp(&ctx->stats.time_delta);
  968. init_timestamp(&ctx->stats.end_time);
  969. init_timestamp(&ctx->stats.pkt_ts_delta);
  970. init_timestamp(&ctx->stats.last_print);
  971. ctx->running = true;
  972. total_loops = ctx->options->loop;
  973. loop = 0;
  974. /* main loop, when not looping forever (or until abort) */
  975. if (ctx->options->loop > 0) {
  976. while (ctx->options->loop-- && !ctx->abort) { /* limited loop */
  977. ++loop;
  978. if (ctx->options->stats == 0) {
  979. if (!ctx->unique_iteration || loop == ctx->unique_iteration)
  980. printf("Loop " COUNTER_SPEC " of " COUNTER_SPEC "...\n",
  981. loop, total_loops);
  982. else
  983. printf("Loop " COUNTER_SPEC " of " COUNTER_SPEC " (" COUNTER_SPEC " unique)...\n",
  984. loop, total_loops,
  985. ctx->unique_iteration);
  986. }
  987. if ((rcode = tcpr_replay_index(ctx)) < 0)
  988. return rcode;
  989. if (ctx->options->loop > 0) {
  990. if (!ctx->abort && ctx->options->loopdelay_ms > 0) {
  991. usleep(ctx->options->loopdelay_ms * 1000);
  992. gettimeofday(&ctx->stats.end_time, NULL);
  993. }
  994. if (ctx->options->stats == 0)
  995. packet_stats(&ctx->stats);
  996. }
  997. }
  998. } else {
  999. while (!ctx->abort) { /* loop forever unless user aborts */
  1000. ++loop;
  1001. if (ctx->options->stats == 0) {
  1002. if (!ctx->unique_iteration || loop == ctx->unique_iteration)
  1003. printf("Loop " COUNTER_SPEC "...\n", loop);
  1004. else
  1005. printf("Loop " COUNTER_SPEC " (" COUNTER_SPEC " unique)...\n", loop,
  1006. ctx->unique_iteration);
  1007. }
  1008. if ((rcode = tcpr_replay_index(ctx)) < 0)
  1009. return rcode;
  1010. if (!ctx->abort && ctx->options->loopdelay_ms > 0) {
  1011. usleep(ctx->options->loopdelay_ms * 1000);
  1012. gettimeofday(&ctx->stats.end_time, NULL);
  1013. }
  1014. if (ctx->options->stats == 0 && !ctx->abort)
  1015. packet_stats(&ctx->stats);
  1016. }
  1017. }
  1018. ctx->running = false;
  1019. if (ctx->options->stats >= 0) {
  1020. char buf[64];
  1021. if (format_date_time(&ctx->stats.end_time, buf, sizeof(buf)) > 0)
  1022. printf("Test complete: %s\n", buf);
  1023. }
  1024. return 0;
  1025. }
  1026. /**
  1027. * \brief Abort the tcpreplay_replay execution.
  1028. *
  1029. * This might take a little while since tcpreplay_replay() only checks this
  1030. * once per packet (sleeping between packets can cause delays), however,
  1031. * this function returns once the signal has been sent and does not block
  1032. */
  1033. int
  1034. tcpreplay_abort(tcpreplay_t *ctx)
  1035. {
  1036. assert(ctx);
  1037. ctx->abort = true;
  1038. printf("sendpacket_abort\n");
  1039. if (ctx->intf1 != NULL)
  1040. sendpacket_abort(ctx->intf1);
  1041. if (ctx->intf2 != NULL)
  1042. sendpacket_abort(ctx->intf2);
  1043. return 0;
  1044. }
  1045. /**
  1046. * \brief Temporarily suspend tcpreplay_replay()
  1047. *
  1048. * This might take a little while since tcpreplay_replay() only checks this
  1049. * once per packet (sleeping between packets can cause delays), however,
  1050. * this function returns once the signal has been sent and does not block
  1051. *
  1052. * Note that suspending a running context can create odd timing
  1053. */
  1054. int
  1055. tcpreplay_suspend(tcpreplay_t *ctx)
  1056. {
  1057. assert(ctx);
  1058. ctx->suspend = true;
  1059. return 0;
  1060. }
  1061. /**
  1062. * \brief Restart tcpreplay_replay() after suspend
  1063. *
  1064. * Causes the worker thread to restart sending packets
  1065. */
  1066. int
  1067. tcpreplay_restart(tcpreplay_t *ctx)
  1068. {
  1069. assert(ctx);
  1070. ctx->suspend = false;
  1071. return 0;
  1072. }
  1073. /**
  1074. * \brief Tells you if the given tcpreplay context is currently suspended
  1075. *
  1076. * Suspended == running, but not sending packets
  1077. */
  1078. bool
  1079. tcpreplay_is_suspended(tcpreplay_t *ctx)
  1080. {
  1081. assert(ctx);
  1082. return ctx->suspend;
  1083. }
  1084. /**
  1085. * \brief Tells you if the tcpreplay context is running (not yet finished)
  1086. *
  1087. * Returns true even if it is suspended
  1088. */
  1089. bool
  1090. tcpreplay_is_running(tcpreplay_t *ctx)
  1091. {
  1092. assert(ctx);
  1093. return ctx->running;
  1094. }
  1095. /**
  1096. * \brief returns the current statistics during or after a replay
  1097. *
  1098. * For performance reasons, I don't bother to put a mutex around this and you
  1099. * don't need to either. Just realize that your values may be off by one until
  1100. * tcreplay_replay() returns.
  1101. */
  1102. const tcpreplay_stats_t *
  1103. tcpreplay_get_stats(tcpreplay_t *ctx)
  1104. {
  1105. const tcpreplay_stats_t *ptr;
  1106. assert(ctx);
  1107. /* copy stats over so they don't change while caller is using the buffer */
  1108. memcpy(&ctx->static_stats, &ctx->stats, sizeof(tcpreplay_stats_t));
  1109. ptr = &ctx->static_stats;
  1110. return ptr;
  1111. }
  1112. /**
  1113. * \brief returns the current number of sources/files to be sent
  1114. */
  1115. int
  1116. tcpreplay_get_source_count(tcpreplay_t *ctx)
  1117. {
  1118. assert(ctx);
  1119. return ctx->options->source_cnt;
  1120. }
  1121. /**
  1122. * \brief Returns the current source id being replayed
  1123. */
  1124. int
  1125. tcpreplay_get_current_source(tcpreplay_t *ctx)
  1126. {
  1127. assert(ctx);
  1128. return ctx->current_source;
  1129. }
  1130. /* vim: set tabstop=8 expandtab shiftwidth=4 softtabstop=4: */
  1131. /**
  1132. * \brief Sets printing of flow statistics
  1133. */
  1134. int tcpreplay_set_flow_stats(tcpreplay_t *ctx, bool value)
  1135. {
  1136. assert(ctx);
  1137. ctx->options->flow_stats = value;
  1138. return 0;
  1139. }
  1140. /**
  1141. * \brief Sets the flow expiry in seconds
  1142. */
  1143. int tcpreplay_set_flow_expiry(tcpreplay_t *ctx, int value)
  1144. {
  1145. assert(ctx);
  1146. ctx->options->flow_expiry = value;
  1147. return 0;
  1148. }
  1149. /**
  1150. * \brief Get whether to printof flow statistics
  1151. */
  1152. bool tcpreplay_get_flow_stats(tcpreplay_t *ctx)
  1153. {
  1154. assert(ctx);
  1155. return ctx->options->flow_stats;
  1156. }
  1157. /**
  1158. * \brief Gets the flow expiry in seconds
  1159. */
  1160. int tcpreplay_get_flow_expiry(tcpreplay_t *ctx)
  1161. {
  1162. assert(ctx);
  1163. return ctx->options->flow_expiry;
  1164. }