send_packets.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /* $Id: send_packets.c 2346 2009-05-19 00:14:52Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2008 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "config.h"
  32. #include "defines.h"
  33. #include "common.h"
  34. #include <sys/time.h>
  35. #include <sys/types.h>
  36. #include <signal.h>
  37. #include <string.h>
  38. #include <netinet/in.h>
  39. #include <errno.h>
  40. #include <stdlib.h>
  41. #include <unistd.h>
  42. #include <fcntl.h>
  43. #include "tcpreplay.h"
  44. #ifdef TCPREPLAY
  45. #ifdef TCPREPLAY_EDIT
  46. #include "tcpreplay_edit_opts.h"
  47. #include "tcpedit/tcpedit.h"
  48. extern tcpedit_t *tcpedit;
  49. #else
  50. #include "tcpreplay_opts.h"
  51. #endif
  52. #endif /* TCPREPLAY */
  53. #include "send_packets.h"
  54. #include "sleep.h"
  55. extern tcpreplay_opt_t options;
  56. extern struct timeval begin, end;
  57. extern COUNTER bytes_sent, failed, pkts_sent;
  58. extern volatile int didsig;
  59. #ifdef DEBUG
  60. extern int debug;
  61. #endif
  62. static void do_sleep(struct timeval *time, struct timeval *last, int len,
  63. int accurate, sendpacket_t *sp, COUNTER counter, delta_t *ctx);
  64. static const u_char *get_next_packet(pcap_t *pcap, struct pcap_pkthdr *pkthdr,
  65. int file_idx, packet_cache_t **prev_packet);
  66. static u_int32_t get_user_count(sendpacket_t *sp, COUNTER counter);
  67. /**
  68. * the main loop function for tcpreplay. This is where we figure out
  69. * what to do with each packet
  70. */
  71. void
  72. send_packets(pcap_t *pcap, int cache_file_idx)
  73. {
  74. struct timeval last = { 0, 0 };
  75. COUNTER packetnum = 0;
  76. struct pcap_pkthdr pkthdr;
  77. const u_char *pktdata = NULL;
  78. sendpacket_t *sp = options.intf1;
  79. u_int32_t pktlen;
  80. packet_cache_t *cached_packet = NULL;
  81. packet_cache_t **prev_packet = NULL;
  82. #if defined TCPREPLAY && defined TCPREPLAY_EDIT
  83. struct pcap_pkthdr *pkthdr_ptr;
  84. #endif
  85. delta_t delta_ctx;
  86. init_delta_time(&delta_ctx);
  87. /* register signals */
  88. didsig = 0;
  89. if (options.speed.mode != SPEED_ONEATATIME) {
  90. (void)signal(SIGINT, catcher);
  91. } else {
  92. (void)signal(SIGINT, break_now);
  93. }
  94. if (options.enable_file_cache) {
  95. prev_packet = &cached_packet;
  96. } else {
  97. prev_packet = NULL;
  98. }
  99. /* MAIN LOOP
  100. * Keep sending while we have packets or until
  101. * we've sent enough packets
  102. */
  103. while ((pktdata = get_next_packet(pcap, &pkthdr, cache_file_idx, prev_packet)) != NULL) {
  104. /* die? */
  105. if (didsig)
  106. break_now(0);
  107. /* stop sending based on the limit -L? */
  108. if (options.limit_send > 0 && pkts_sent >= options.limit_send)
  109. return;
  110. packetnum++;
  111. #ifdef TCPREPLAY
  112. /* do we use the snaplen (caplen) or the "actual" packet len? */
  113. pktlen = HAVE_OPT(PKTLEN) ? pkthdr.len : pkthdr.caplen;
  114. #elif TCPBRIDGE
  115. pktlen = pkthdr.caplen;
  116. #else
  117. #error WTF??? We should not be here!
  118. #endif
  119. dbgx(2, "packet " COUNTER_SPEC " caplen %d", packetnum, pktlen);
  120. /* Dual nic processing */
  121. if (options.intf2 != NULL) {
  122. sp = (sendpacket_t *) cache_mode(options.cachedata, packetnum);
  123. /* sometimes we should not send the packet */
  124. if (sp == TCPR_DIR_NOSEND)
  125. continue;
  126. }
  127. /* do we need to print the packet via tcpdump? */
  128. #ifdef ENABLE_VERBOSE
  129. if (options.verbose)
  130. tcpdump_print(options.tcpdump, &pkthdr, pktdata);
  131. #endif
  132. #if defined TCPREPLAY && defined TCPREPLAY_EDIT
  133. pkthdr_ptr = &pkthdr;
  134. if (tcpedit_packet(tcpedit, &pkthdr_ptr, &pktdata, sp->cache_dir) == -1) {
  135. errx(-1, "Error editing packet #" COUNTER_SPEC ": %s", packetnum, tcpedit_geterr(tcpedit));
  136. }
  137. pktlen = HAVE_OPT(PKTLEN) ? pkthdr_ptr->len : pkthdr_ptr->caplen;
  138. #endif
  139. /*
  140. * we have to cast the ts, since OpenBSD sucks
  141. * had to be special and use bpf_timeval.
  142. * Only sleep if we're not in top speed mode (-t)
  143. */
  144. if (options.speed.mode != SPEED_TOPSPEED)
  145. do_sleep((struct timeval *)&pkthdr.ts, &last, pktlen, options.accurate, sp, packetnum, &delta_ctx);
  146. /* mark the time when we send the last packet */
  147. start_delta_time(&delta_ctx);
  148. dbgx(2, "Sending packet #" COUNTER_SPEC, packetnum);
  149. /* write packet out on network */
  150. if (sendpacket(sp, pktdata, pktlen) < (int)pktlen)
  151. warnx("Unable to send packet: %s", sendpacket_geterr(sp));
  152. /*
  153. * track the time of the "last packet sent". Again, because of OpenBSD
  154. * we have to do a mempcy rather then assignment.
  155. *
  156. * A number of 3rd party tools generate bad timestamps which go backwards
  157. * in time. Hence, don't update the "last" unless pkthdr.ts > last
  158. */
  159. if (timercmp(&last, &pkthdr.ts, <))
  160. memcpy(&last, &pkthdr.ts, sizeof(struct timeval));
  161. pkts_sent ++;
  162. bytes_sent += pktlen;
  163. } /* while */
  164. if (options.enable_file_cache) {
  165. options.file_cache[cache_file_idx].cached = TRUE;
  166. }
  167. }
  168. /**
  169. * Gets the next packet to be sent out. This will either read from the pcap file
  170. * or will retrieve the packet from the internal cache.
  171. *
  172. * The parameter prev_packet is used as the parent of the new entry in the cache list.
  173. * This should be NULL on the first call to this function for each file and
  174. * will be updated as new entries are added (or retrieved) from the cache list.
  175. */
  176. static const u_char *
  177. get_next_packet(pcap_t *pcap, struct pcap_pkthdr *pkthdr, int file_idx,
  178. packet_cache_t **prev_packet)
  179. {
  180. u_char *pktdata = NULL;
  181. u_int32_t pktlen;
  182. /* pcap may be null in cache mode! */
  183. /* packet_cache_t may be null in file read mode! */
  184. assert(pkthdr);
  185. /*
  186. * Check if we're caching files
  187. */
  188. if (options.enable_file_cache && (prev_packet != NULL)) {
  189. /*
  190. * Yes we are caching files - has this one been cached?
  191. */
  192. if (options.file_cache[file_idx].cached) {
  193. if (*prev_packet == NULL) {
  194. /*
  195. * Get the first packet in the cache list directly from the file
  196. */
  197. *prev_packet = options.file_cache[file_idx].packet_cache;
  198. } else {
  199. /*
  200. * Get the next packet in the cache list
  201. */
  202. *prev_packet = (*prev_packet)->next;
  203. }
  204. if (*prev_packet != NULL) {
  205. pktdata = (*prev_packet)->pktdata;
  206. memcpy(pkthdr, &((*prev_packet)->pkthdr), sizeof(struct pcap_pkthdr));
  207. }
  208. } else {
  209. /*
  210. * We should read the pcap file, and cache the results
  211. */
  212. pktdata = (u_char *)pcap_next(pcap, pkthdr);
  213. if (pktdata != NULL) {
  214. if (*prev_packet == NULL) {
  215. /*
  216. * Create the first packet in the list
  217. */
  218. *prev_packet = safe_malloc(sizeof(packet_cache_t));
  219. options.file_cache[file_idx].packet_cache = *prev_packet;
  220. } else {
  221. /*
  222. * Add a packet to the end of the list
  223. */
  224. (*prev_packet)->next = safe_malloc(sizeof(packet_cache_t));
  225. *prev_packet = (*prev_packet)->next;
  226. }
  227. if (*prev_packet != NULL) {
  228. (*prev_packet)->next = NULL;
  229. pktlen = pkthdr->len;
  230. (*prev_packet)->pktdata = safe_malloc(pktlen);
  231. memcpy((*prev_packet)->pktdata, pktdata, pktlen);
  232. memcpy(&((*prev_packet)->pkthdr), pkthdr, sizeof(struct pcap_pkthdr));
  233. }
  234. }
  235. }
  236. } else {
  237. /*
  238. * Read pcap file as normal
  239. */
  240. pktdata = (u_char *)pcap_next(pcap, pkthdr);
  241. }
  242. /* this get's casted to a const on the way out */
  243. return pktdata;
  244. }
  245. /**
  246. * determines based upon the cachedata which interface the given packet
  247. * should go out. Also rewrites any layer 2 data we might need to adjust.
  248. * Returns a void cased pointer to the options.intfX of the corresponding
  249. * interface.
  250. */
  251. void *
  252. cache_mode(char *cachedata, COUNTER packet_num)
  253. {
  254. void *sp = NULL;
  255. int result;
  256. if (packet_num > options.cache_packets)
  257. err(-1, "Exceeded number of packets in cache file.");
  258. result = check_cache(cachedata, packet_num);
  259. if (result == TCPR_DIR_NOSEND) {
  260. dbgx(2, "Cache: Not sending packet " COUNTER_SPEC ".", packet_num);
  261. return TCPR_DIR_NOSEND;
  262. }
  263. else if (result == TCPR_DIR_C2S) {
  264. dbgx(2, "Cache: Sending packet " COUNTER_SPEC " out primary interface.", packet_num);
  265. sp = options.intf1;
  266. }
  267. else if (result == TCPR_DIR_S2C) {
  268. dbgx(2, "Cache: Sending packet " COUNTER_SPEC " out secondary interface.", packet_num);
  269. sp = options.intf2;
  270. }
  271. else {
  272. err(-1, "check_cache() returned an error. Aborting...");
  273. }
  274. return sp;
  275. }
  276. /**
  277. * Given the timestamp on the current packet and the last packet sent,
  278. * calculate the appropriate amount of time to sleep and do so.
  279. */
  280. static void
  281. do_sleep(struct timeval *time, struct timeval *last, int len, int accurate,
  282. sendpacket_t *sp, COUNTER counter, delta_t *delta_ctx)
  283. {
  284. static struct timeval didsleep = { 0, 0 };
  285. static struct timeval start = { 0, 0 };
  286. #ifdef DEBUG
  287. static struct timeval totalsleep = { 0, 0 };
  288. #endif
  289. struct timespec adjuster = { 0, 0 };
  290. static struct timespec nap = { 0, 0 }, delta_time = {0, 0};
  291. struct timeval nap_for, now, sleep_until;
  292. struct timespec nap_this_time;
  293. static int32_t nsec_adjuster = -1, nsec_times = -1;
  294. float n;
  295. static u_int32_t send = 0; /* accellerator. # of packets to send w/o sleeping */
  296. u_int64_t ppnsec; /* packets per usec */
  297. static int first_time = 1; /* need to track the first time through for the pps accelerator */
  298. #ifdef TCPREPLAY
  299. adjuster.tv_nsec = options.sleep_accel * 1000;
  300. dbgx(4, "Adjuster: " TIMESPEC_FORMAT, adjuster.tv_sec, adjuster.tv_nsec);
  301. #else
  302. adjuster.tv_nsec = 0;
  303. #endif
  304. /* acclerator time? */
  305. if (send > 0) {
  306. send --;
  307. return;
  308. }
  309. /*
  310. * pps_multi accelerator. This uses the existing send accelerator above
  311. * and hence requires the funky math to get the expected timings.
  312. */
  313. if (options.speed.mode == SPEED_PACKETRATE && options.speed.pps_multi) {
  314. send = options.speed.pps_multi - 1;
  315. if (first_time) {
  316. first_time = 0;
  317. return;
  318. }
  319. }
  320. dbgx(4, "This packet time: " TIMEVAL_FORMAT, time->tv_sec, time->tv_usec);
  321. dbgx(4, "Last packet time: " TIMEVAL_FORMAT, last->tv_sec, last->tv_usec);
  322. if (gettimeofday(&now, NULL) < 0)
  323. errx(-1, "Error gettimeofday: %s", strerror(errno));
  324. dbgx(4, "Now time: " TIMEVAL_FORMAT, now.tv_sec, now.tv_usec);
  325. /* First time through for this file */
  326. if (pkts_sent == 0 || ((options.speed.mode != SPEED_MBPSRATE) && (counter == 0))) {
  327. start = now;
  328. timerclear(&sleep_until);
  329. timerclear(&didsleep);
  330. }
  331. else {
  332. timersub(&now, &start, &sleep_until);
  333. }
  334. /* If top speed, you shouldn't even be here */
  335. assert(options.speed.mode != SPEED_TOPSPEED);
  336. switch(options.speed.mode) {
  337. case SPEED_MULTIPLIER:
  338. /*
  339. * Replay packets a factor of the time they were originally sent.
  340. */
  341. if (timerisset(last)) {
  342. if (timercmp(time, last, <)) {
  343. /* Packet has gone back in time! Don't sleep and warn user */
  344. warnx("Packet #" COUNTER_SPEC " has gone back in time!", counter);
  345. timesclear(&nap);
  346. } else {
  347. /* time has increased or is the same, so handle normally */
  348. timersub(time, last, &nap_for);
  349. dbgx(3, "original packet delta time: " TIMEVAL_FORMAT, nap_for.tv_sec, nap_for.tv_usec);
  350. TIMEVAL_TO_TIMESPEC(&nap_for, &nap);
  351. dbgx(3, "original packet delta timv: " TIMESPEC_FORMAT, nap.tv_sec, nap.tv_nsec);
  352. timesdiv(&nap, options.speed.speed);
  353. dbgx(3, "original packet delta/div: " TIMESPEC_FORMAT, nap.tv_sec, nap.tv_nsec);
  354. }
  355. } else {
  356. /* Don't sleep if this is our first packet */
  357. timesclear(&nap);
  358. }
  359. break;
  360. case SPEED_MBPSRATE:
  361. /*
  362. * Ignore the time supplied by the capture file and send data at
  363. * a constant 'rate' (bytes per second).
  364. */
  365. if (pkts_sent != 0) {
  366. n = (float)len / (options.speed.speed * 1024 * 1024 / 8); /* convert Mbps to bps */
  367. nap.tv_sec = n;
  368. nap.tv_nsec = (n - nap.tv_sec) * 1000000000;
  369. dbgx(3, "packet size %d\t\tequals %f bps\t\tnap " TIMESPEC_FORMAT, len, n,
  370. nap.tv_sec, nap.tv_nsec);
  371. }
  372. else {
  373. /* don't sleep at all for the first packet */
  374. timesclear(&nap);
  375. }
  376. break;
  377. case SPEED_PACKETRATE:
  378. /* only need to calculate this the first time */
  379. if (! timesisset(&nap)) {
  380. /* run in packets/sec */
  381. ppnsec = 1000000000 / options.speed.speed * (options.speed.pps_multi > 0 ? options.speed.pps_multi : 1);
  382. NANOSEC_TO_TIMESPEC(ppnsec, &nap);
  383. dbgx(1, "sending %d packet(s) per %lu nsec", (options.speed.pps_multi > 0 ? options.speed.pps_multi : 1), nap.tv_nsec);
  384. }
  385. break;
  386. case SPEED_ONEATATIME:
  387. /* do we skip prompting for a key press? */
  388. if (send == 0) {
  389. send = get_user_count(sp, counter);
  390. }
  391. /* decrement our send counter */
  392. printf("Sending packet " COUNTER_SPEC " out: %s\n", counter,
  393. sp == options.intf1 ? options.intf1_name : options.intf2_name);
  394. send --;
  395. /* leave do_sleep() */
  396. return;
  397. break;
  398. default:
  399. errx(-1, "Unknown/supported speed mode: %d", options.speed.mode);
  400. break;
  401. }
  402. /*
  403. * since we apply the adjuster to the sleep time, we can't modify nap
  404. */
  405. nap_this_time.tv_sec = nap.tv_sec;
  406. nap_this_time.tv_nsec = nap.tv_nsec;
  407. dbgx(2, "nap_time before rounding: " TIMESPEC_FORMAT, nap_this_time.tv_sec, nap_this_time.tv_nsec);
  408. if (accurate != ACCURATE_ABS_TIME) {
  409. switch (options.speed.mode) {
  410. /* Mbps & Multipler are dynamic timings, so we round to the nearest usec */
  411. case SPEED_MBPSRATE:
  412. case SPEED_MULTIPLIER:
  413. ROUND_TIMESPEC_TO_MICROSEC(&nap_this_time);
  414. break;
  415. /* Packets/sec is static, so we weight packets for .1usec accuracy */
  416. case SPEED_PACKETRATE:
  417. if (nsec_adjuster < 0)
  418. nsec_adjuster = (nap_this_time.tv_nsec % 10000) / 1000;
  419. /* update in the range of 0-9 */
  420. nsec_times = (nsec_times + 1) % 10;
  421. if (nsec_times < nsec_adjuster) {
  422. /* sorta looks like a no-op, but gives us a nice round usec number */
  423. nap_this_time.tv_nsec = (nap_this_time.tv_nsec / 1000 * 1000) + 1000;
  424. } else {
  425. nap_this_time.tv_nsec -= (nap_this_time.tv_nsec % 1000);
  426. }
  427. dbgx(3, "(%d)\tnsec_times = %d\tnap adjust: %lu -> %lu", nsec_adjuster, nsec_times, nap.tv_nsec, nap_this_time.tv_nsec);
  428. break;
  429. default:
  430. errx(-1, "Unknown/supported speed mode: %d", options.speed.mode);
  431. }
  432. }
  433. dbgx(2, "nap_time before delta calc: " TIMESPEC_FORMAT, nap_this_time.tv_sec, nap_this_time.tv_nsec);
  434. get_delta_time(delta_ctx, &delta_time);
  435. dbgx(2, "delta: " TIMESPEC_FORMAT, delta_time.tv_sec, delta_time.tv_nsec);
  436. if (timesisset(&delta_time)) {
  437. if (timescmp(&nap_this_time, &delta_time, >)) {
  438. timessub(&nap_this_time, &delta_time, &nap_this_time);
  439. dbgx(3, "timesub: %lu %lu", delta_time.tv_sec, delta_time.tv_nsec);
  440. } else {
  441. timesclear(&nap_this_time);
  442. dbgx(3, "timesclear: " TIMESPEC_FORMAT, delta_time.tv_sec, delta_time.tv_nsec);
  443. }
  444. }
  445. /* apply the adjuster... */
  446. if (timesisset(&adjuster)) {
  447. if (timescmp(&nap_this_time, &adjuster, >)) {
  448. timessub(&nap_this_time, &adjuster, &nap_this_time);
  449. } else {
  450. timesclear(&nap_this_time);
  451. }
  452. }
  453. dbgx(2, "Sleeping: " TIMESPEC_FORMAT, nap_this_time.tv_sec, nap_this_time.tv_nsec);
  454. /* don't sleep if nap = {0, 0} */
  455. if (!timesisset(&nap_this_time))
  456. return;
  457. /*
  458. * Depending on the accurate method & packet rate computation method
  459. * We have multiple methods of sleeping, pick the right one...
  460. */
  461. switch (accurate) {
  462. #ifdef HAVE_SELECT
  463. case ACCURATE_SELECT:
  464. select_sleep(nap_this_time);
  465. break;
  466. #endif
  467. #ifdef HAVE_IOPERM
  468. case ACCURATE_IOPORT:
  469. ioport_sleep(nap_this_time);
  470. break;
  471. #endif
  472. #ifdef HAVE_RDTSC
  473. case ACCURATE_RDTSC:
  474. rdtsc_sleep(nap_this_time);
  475. break;
  476. #endif
  477. #ifdef HAVE_ABSOLUTE_TIME
  478. case ACCURATE_ABS_TIME:
  479. absolute_time_sleep(nap_this_time);
  480. break;
  481. #endif
  482. case ACCURATE_GTOD:
  483. gettimeofday_sleep(nap_this_time);
  484. break;
  485. case ACCURATE_NANOSLEEP:
  486. nanosleep_sleep(nap_this_time);
  487. break;
  488. /*
  489. timeradd(&didsleep, &nap_this_time, &didsleep);
  490. dbgx(4, "I will sleep " TIMEVAL_FORMAT, nap_this_time.tv_sec, nap_this_time.tv_usec);
  491. if (timercmp(&didsleep, &sleep_until, >)) {
  492. timersub(&didsleep, &sleep_until, &nap_this_time);
  493. TIMEVAL_TO_TIMESPEC(&nap_this_time, &sleep);
  494. dbgx(4, "Sleeping " TIMEVAL_FORMAT, nap_this_time.tv_sec, nap_this_time.tv_usec);
  495. #ifdef DEBUG
  496. timeradd(&totalsleep, &nap_this_time, &totalsleep);
  497. #endif
  498. if (nanosleep(&sleep, &ignore) == -1) {
  499. warnx("nanosleep error: %s", strerror(errno));
  500. }
  501. }
  502. break;
  503. */
  504. default:
  505. errx(-1, "Unknown timer mode %d", accurate);
  506. }
  507. #ifdef DEBUG
  508. dbgx(4, "Total sleep time: " TIMEVAL_FORMAT, totalsleep.tv_sec, totalsleep.tv_usec);
  509. #endif
  510. dbgx(2, "sleep delta: " TIMESPEC_FORMAT, delta_time.tv_sec, delta_time.tv_nsec);
  511. }
  512. /**
  513. * Ask the user how many packets they want to send.
  514. */
  515. static u_int32_t
  516. get_user_count(sendpacket_t *sp, COUNTER counter)
  517. {
  518. struct pollfd poller[1]; /* use poll to read from the keyboard */
  519. char input[EBUF_SIZE];
  520. u_int32_t send = 0;
  521. printf("**** Next packet #" COUNTER_SPEC " out %s. How many packets do you wish to send? ",
  522. counter, (sp == options.intf1 ? options.intf1_name : options.intf2_name));
  523. fflush(NULL);
  524. poller[0].fd = STDIN_FILENO;
  525. poller[0].events = POLLIN | POLLPRI | POLLNVAL;
  526. poller[0].revents = 0;
  527. if (fcntl(0, F_SETFL, fcntl(0, F_GETFL) & ~O_NONBLOCK))
  528. errx(-1, "Unable to clear non-blocking flag on stdin: %s", strerror(errno));
  529. /* wait for the input */
  530. if (poll(poller, 1, -1) < 0)
  531. errx(-1, "Error reading user input from stdin: %s", strerror(errno));
  532. /*
  533. * read to the end of the line or EBUF_SIZE,
  534. * Note, if people are stupid, and type in more text then EBUF_SIZE
  535. * then the next fgets() will pull in that data, which will have poor
  536. * results. fuck them.
  537. */
  538. if (fgets(input, sizeof(input), stdin) == NULL) {
  539. errx(-1, "Unable to process user input for fd %d: %s", fileno(stdin), strerror(errno));
  540. } else if (strlen(input) > 1) {
  541. send = strtoul(input, NULL, 0);
  542. }
  543. /* how many packets should we send? */
  544. if (send == 0) {
  545. dbg(1, "Input was less then 1 or non-numeric, assuming 1");
  546. /* assume send only one packet */
  547. send = 1;
  548. }
  549. return send;
  550. }
  551. /*
  552. Local Variables:
  553. mode:c
  554. indent-tabs-mode:nil
  555. c-basic-offset:4
  556. End:
  557. */