1 /* $Id$ */
2 
3 /*
4  *   Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
5  *   Copyright (c) 2013-2018 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
6  *
7  *   The Tcpreplay Suite of tools is free software: you can redistribute it
8  *   and/or modify it under the terms of the GNU General Public License as
9  *   published by the Free Software Foundation, either version 3 of the
10  *   License, or with the authors permission any later version.
11  *
12  *   The Tcpreplay Suite is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with the Tcpreplay Suite.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 
22 #ifndef _UTILS_H_
23 #define _UTILS_H_
24 
25 #include "config.h"
26 #include "defines.h"
27 #include "common.h"
28 
29 typedef struct {
30     char *active_pcap;
31     COUNTER bytes_sent;
32     COUNTER pkts_sent;
33     COUNTER failed;
34     struct timeval start_time;
35     struct timeval time_delta;
36     struct timeval end_time;
37     struct timeval pkt_ts_delta;
38     struct timeval last_print;
39     struct timeval first_packet_sent_wall_time;
40     struct timeval first_packet_pcap_timestamp;
41     COUNTER flow_non_flow_packets;
42     COUNTER flows;
43     COUNTER flows_unique;
44     COUNTER flow_packets;
45     COUNTER flows_expired;
46     COUNTER flows_invalid_packets;
47 } tcpreplay_stats_t;
48 
49 
50 int read_hexstring(const char *l2string, u_char *hex, const int hexlen);
51 void packet_stats(const tcpreplay_stats_t *stats);
52 int format_date_time(struct timeval *when, char *buf, size_t len);
53 uint32_t tcpr_random(uint32_t *seed);
54 void restore_stdin(void);
55 
56 /* our "safe" implimentations of functions which allocate memory */
57 #define safe_malloc(x) _our_safe_malloc(x, __FUNCTION__, __LINE__, __FILE__)
58 void *_our_safe_malloc(size_t len, const char *, const int, const char *);
59 
60 #define safe_realloc(x, y) _our_safe_realloc(x, y, __FUNCTION__, __LINE__, __FILE__)
61 void *_our_safe_realloc(void *ptr, size_t len, const char *, const int, const char *);
62 
63 #define safe_strdup(x) _our_safe_strdup(x, __FUNCTION__, __LINE__, __FILE__)
64 char *_our_safe_strdup(const char *str, const char *, const int, const char *);
65 
66 #define safe_free(x) _our_safe_free(x, __FUNCTION__, __LINE__, __FILE__)
67 void _our_safe_free(void *ptr, const char *, const int, const char *);
68 
69 #define safe_pcap_next(x, y) _our_safe_pcap_next(x, y, __FUNCTION__, __LINE__, __FILE__)
70 u_char *_our_safe_pcap_next(pcap_t *pcap,  struct pcap_pkthdr *pkthdr,
71         const char *funcname, const int line, const char *file);
72 
73 #define safe_pcap_next_ex(x, y, z) _our_safe_pcap_next_ex(x, y, z, __FUNCTION__, __LINE__, __FILE__)
74 int _our_safe_pcap_next_ex(pcap_t *pcap, struct pcap_pkthdr **pkthdr,
75         const u_char **pktdata, const char *funcname,
76         const int line, const char *file);
77 
78 #define MAX_ARGS 128
79 
80 #ifndef HAVE_INET_ATON
81 #define HAVE_INET_ATON
82 #define USE_CUSTOM_INET_ATON
83 int inet_aton(const char *name, struct in_addr *addr);
84 #endif
85 
86 #if SIZEOF_LONG  == 8
87 # define do_div(n,base) ({          \
88     uint32_t __base = (base);       \
89     uint32_t __rem;           \
90     __rem = ((uint64_t)(n)) % __base;     \
91     (n) = ((uint64_t)(n)) / __base;       \
92     __rem;              \
93    })
94 #elif SIZEOF_LONG  == 4
95 extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
96 # define do_div(n,base) ({        \
97     uint32_t __base = (base);     \
98     uint32_t __rem;         \
99     if (((n) >> 32) == 0) {     \
100         __rem = (uint32_t)(n) % __base;   \
101         (n) = (uint32_t)(n) / __base;   \
102     } else            \
103         __rem = __div64_32(&(n), __base);  \
104     __rem;            \
105    })
106 #else /* SIZEOF_LONG == ?? */
107 # error do_div() does not yet support the C64
108 #endif /* SIZEOF_LONG  */
109 
110 #endif /* _UTILS_H_ */
111 
112