utils.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2017 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 end_time;
  31. struct timeval last_time;
  32. struct timeval 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, const int hexlen);
  41. void packet_stats(const tcpreplay_stats_t *stats);
  42. int format_date_time(struct timeval *when, char *buf, size_t len);
  43. int tcpr_random(uint32_t *seed);
  44. /* our "safe" implimentations of functions which allocate memory */
  45. #define safe_malloc(x) _our_safe_malloc(x, __FUNCTION__, __LINE__, __FILE__)
  46. void *_our_safe_malloc(size_t len, const char *, const int, const char *);
  47. #define safe_realloc(x, y) _our_safe_realloc(x, y, __FUNCTION__, __LINE__, __FILE__)
  48. void *_our_safe_realloc(void *ptr, size_t len, const char *, const int, const char *);
  49. #define safe_strdup(x) _our_safe_strdup(x, __FUNCTION__, __LINE__, __FILE__)
  50. char *_our_safe_strdup(const char *str, const char *, const int, const char *);
  51. #define safe_free(x) _our_safe_free(x, __FUNCTION__, __LINE__, __FILE__)
  52. void _our_safe_free(void *ptr, const char *, const int, const char *);
  53. #define MAX_ARGS 128
  54. #ifndef HAVE_INET_ATON
  55. #define HAVE_INET_ATON
  56. #define USE_CUSTOM_INET_ATON
  57. int inet_aton(const char *name, struct in_addr *addr);
  58. #endif
  59. #if SIZEOF_CHARP == 8
  60. # define do_div(n,base) ({ \
  61. uint32_t __base = (base); \
  62. uint32_t __rem; \
  63. __rem = ((uint64_t)(n)) % __base; \
  64. (n) = ((uint64_t)(n)) / __base; \
  65. __rem; \
  66. })
  67. #elif SIZEOF_CHARP == 4
  68. extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
  69. # define do_div(n,base) ({ \
  70. uint32_t __base = (base); \
  71. uint32_t __rem; \
  72. if (((n) >> 32) == 0) { \
  73. __rem = (uint32_t)(n) % __base; \
  74. (n) = (uint32_t)(n) / __base; \
  75. } else \
  76. __rem = __div64_32(&(n), __base); \
  77. __rem; \
  78. })
  79. #else /* SIZEOF_CHARP == ?? */
  80. # error do_div() does not yet support the C64
  81. #endif /* SIZEOF_CHARP */
  82. #endif /* _UTILS_H_ */