utils.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2018 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. #ifndef _UTILS_H_
  20. #define _UTILS_H_
  21. #include "config.h"
  22. #include "defines.h"
  23. #include "common.h"
  24. typedef struct {
  25. char *active_pcap;
  26. COUNTER bytes_sent;
  27. COUNTER pkts_sent;
  28. COUNTER failed;
  29. struct timeval start_time;
  30. struct timeval time_delta;
  31. struct timeval end_time;
  32. struct timeval pkt_ts_delta;
  33. struct timeval last_print;
  34. COUNTER flow_non_flow_packets;
  35. COUNTER flows;
  36. COUNTER flows_unique;
  37. COUNTER flow_packets;
  38. COUNTER flows_expired;
  39. COUNTER flows_invalid_packets;
  40. } tcpreplay_stats_t;
  41. int read_hexstring(const char *l2string, u_char *hex, const int hexlen);
  42. void packet_stats(const tcpreplay_stats_t *stats);
  43. int format_date_time(struct timeval *when, char *buf, size_t len);
  44. uint32_t tcpr_random(uint32_t *seed);
  45. void restore_stdin(void);
  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 *, const 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 *, const 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 *, const int, const char *);
  53. #define safe_free(x) _our_safe_free(x, __FUNCTION__, __LINE__, __FILE__)
  54. void _our_safe_free(void *ptr, const char *, const int, const char *);
  55. #define safe_pcap_next(x, y) _our_safe_pcap_next(x, y, __FUNCTION__, __LINE__, __FILE__)
  56. u_char *_our_safe_pcap_next(pcap_t *pcap, struct pcap_pkthdr *pkthdr,
  57. const char *funcname, const 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, struct pcap_pkthdr **pkthdr,
  60. const u_char **pktdata, const char *funcname,
  61. const int line, const char *file);
  62. #define MAX_ARGS 128
  63. #ifndef HAVE_INET_ATON
  64. #define HAVE_INET_ATON
  65. #define USE_CUSTOM_INET_ATON
  66. int inet_aton(const char *name, struct in_addr *addr);
  67. #endif
  68. #if SIZEOF_LONG == 8
  69. # define do_div(n,base) ({ \
  70. uint32_t __base = (base); \
  71. uint32_t __rem; \
  72. __rem = ((uint64_t)(n)) % __base; \
  73. (n) = ((uint64_t)(n)) / __base; \
  74. __rem; \
  75. })
  76. #elif SIZEOF_LONG == 4
  77. extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
  78. # define do_div(n,base) ({ \
  79. uint32_t __base = (base); \
  80. uint32_t __rem; \
  81. if (((n) >> 32) == 0) { \
  82. __rem = (uint32_t)(n) % __base; \
  83. (n) = (uint32_t)(n) / __base; \
  84. } else \
  85. __rem = __div64_32(&(n), __base); \
  86. __rem; \
  87. })
  88. #else /* SIZEOF_LONG == ?? */
  89. # error do_div() does not yet support the C64
  90. #endif /* SIZEOF_LONG */
  91. #endif /* _UTILS_H_ */