utils.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #pragma once
  20. #include "defines.h"
  21. #include "config.h"
  22. #include "common.h"
  23. typedef struct {
  24. char *active_pcap;
  25. COUNTER bytes_sent;
  26. COUNTER pkts_sent;
  27. COUNTER failed;
  28. struct timespec start_time;
  29. struct timespec time_delta;
  30. struct timespec end_time;
  31. struct timespec pkt_ts_delta;
  32. struct timespec last_print;
  33. COUNTER flow_non_flow_packets;
  34. COUNTER flows;
  35. COUNTER flows_unique;
  36. COUNTER flow_packets;
  37. COUNTER flows_expired;
  38. COUNTER flows_invalid_packets;
  39. } tcpreplay_stats_t;
  40. int read_hexstring(const char *l2string, u_char *hex, int hexlen);
  41. void packet_stats(const tcpreplay_stats_t *stats);
  42. int format_date_time(struct timespec *when, char *buf, size_t len);
  43. uint32_t tcpr_random(uint32_t *seed);
  44. void restore_stdin(void);
  45. pcap_t* tcpr_pcap_open(const char *path, char *ebuf);
  46. /* our "safe" implimentations of functions which allocate memory */
  47. #define safe_malloc(x) our_safe_malloc(x, __FUNCTION__, __LINE__, __FILE__)
  48. void *our_safe_malloc(size_t len, const char *, int, const char *);
  49. #define safe_realloc(x, y) our_safe_realloc(x, y, __FUNCTION__, __LINE__, __FILE__)
  50. void *our_safe_realloc(void *ptr, size_t len, const char *, int, const char *);
  51. #define safe_strdup(x) our_safe_strdup(x, __FUNCTION__, __LINE__, __FILE__)
  52. char *our_safe_strdup(const char *str, const char *, int, const char *);
  53. #define safe_free(x) our_safe_free(x, __FUNCTION__, __LINE__, __FILE__)
  54. void our_safe_free(void *ptr, const char *, int, const char *);
  55. #define safe_pcap_next(x, y) our_safe_pcap_next(x, y, __FUNCTION__, __LINE__, __FILE__)
  56. u_char *
  57. our_safe_pcap_next(pcap_t *pcap, struct pcap_pkthdr *pkthdr, const char *funcname, int line, const char *file);
  58. #define safe_pcap_next_ex(x, y, z) our_safe_pcap_next_ex(x, y, z, __FUNCTION__, __LINE__, __FILE__)
  59. int our_safe_pcap_next_ex(pcap_t *pcap,
  60. struct pcap_pkthdr **pkthdr,
  61. const u_char **pktdata,
  62. const char *funcname,
  63. int line,
  64. const char *file);
  65. #define MAX_ARGS 128
  66. #ifndef HAVE_INET_ATON
  67. #define HAVE_INET_ATON
  68. #define USE_CUSTOM_INET_ATON
  69. int inet_aton(const char *name, struct in_addr *addr);
  70. #endif
  71. #if SIZEOF_LONG == 8
  72. #define do_div(n, base) \
  73. ({ \
  74. uint32_t __base = (base); \
  75. uint32_t __rem; \
  76. __rem = ((uint64_t)(n)) % __base; \
  77. (n) = ((uint64_t)(n)) / __base; \
  78. __rem; \
  79. })
  80. #elif SIZEOF_LONG == 4
  81. extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
  82. #define do_div(n, base) \
  83. ({ \
  84. uint32_t __base = (base); \
  85. uint32_t __rem; \
  86. if (((n) >> 32) == 0) { \
  87. __rem = (uint32_t)(n) % __base; \
  88. (n) = (uint32_t)(n) / __base; \
  89. } else \
  90. __rem = __div64_32(&(n), __base); \
  91. __rem; \
  92. })
  93. #else /* SIZEOF_LONG == ?? */
  94. #error do_div() does not yet support the C64
  95. #endif /* SIZEOF_LONG */