send_packets.c 20 KB

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