1 #ifndef ETTERCAP_H
2 #define ETTERCAP_H
3 
4 #include <config.h>
5 
6 #include <sys/types.h>
7 #include <sys/time.h>
8 
9 #include <stdio.h>
10 #include <stdbool.h>
11 #include <stdlib.h>
12 
13 #ifdef OS_WINDOWS
14    #include <windows.h>
15 #endif
16 
17 #if defined OS_DARWIN || defined OS_BSD
18    #define PCAP_DONT_INCLUDE_PCAP_BPF_H 1
19    #include <net/bpf.h>
20 #endif
21 
22 #ifndef PATH_MAX
23    #define PATH_MAX  1024
24 #endif
25 
26 #if !defined (__USE_GNU)   /* for memmem(), strsignal(), etc etc... */
27    #define __USE_GNU
28 #endif
29 #ifdef OS_SOLARIS
30    #define _REENTRANT      /* for strtok_r() */
31 #endif
32 #include <string.h>
33 #if defined (__USE_GNU)
34    #undef __USE_GNU
35 #endif
36 #include <strings.h>
37 #include <unistd.h>
38 #include <time.h>
39 
40 /*
41  * On Windows (MinGw) we must export all ettercap.exe variables/function
42  * used in plugins and functions in plugins must be declared as 'importable'
43  */
44 #if defined(OS_WINDOWS)
45    #if defined(BUILDING_PLUGIN)
46       #define EC_API_EXTERN __declspec(dllimport)
47    #else
48       #define EC_API_EXTERN __declspec(dllexport)
49    #endif
50 #else
51    #define EC_API_EXTERN extern
52 #endif
53 
54 /* these are often needed... */
55 #include <ec_queue.h>
56 #include <ec_error.h>
57 #include <ec_debug.h>
58 #include <ec_stdint.h>
59 #include <ec_globals.h>
60 #include <ec_strings.h>
61 
62 #ifdef OS_MINGW
63    #include <ec_os_mingw.h>
64 #endif
65 
66 
67 /* wrappers for safe memory allocation */
68 
69 #define SAFE_CALLOC(x, n, s) do { \
70    x = calloc(n, s); \
71    ON_ERROR(x, NULL, "virtual memory exhausted"); \
72 } while(0)
73 
74 #define SAFE_MALLOC(x, s) do { \
75    x = malloc(s); \
76    ON_ERROR(x, NULL, "virtual memory exhausted"); \
77 } while (0)
78 
79 #define SAFE_REALLOC(x, s) do { \
80    x = realloc(x, s); \
81    ON_ERROR(x, NULL, "virtual memory exhausted"); \
82 } while(0)
83 
84 #define SAFE_STRDUP(x, s) do{ \
85    x = strdup(s); \
86    ON_ERROR(x, NULL, "virtual memory exhausted"); \
87 }while(0)
88 
89 #define SAFE_FREE(x) do{ if(x) { free(x); x = NULL; } }while(0)
90 
91 
92 /* convert to string */
93 #define EC_STRINGIFY(in) #in
94 #define EC_TOSTRING(in) EC_STRINGIFY(in)
95 
96 // No need to have a priority in the constructor at this moment
97 /*
98 #if __GNUC_PREREQ(4,3)
99 #define __init __attribute__ ((constructor(101)))
100 #else
101 */
102 #define __init __attribute__ ((constructor))
103 //#endif
104 
105 #ifndef __set_errno
106 #define __set_errno(e) (errno = (e))
107 #endif
108 
109 #define LOOP for(;;)
110 
111 #define EXECUTE(x, ...) do{ if(x != NULL) x( __VA_ARGS__ ); }while(0)
112 
113 /* couple of useful macros */
114 #ifndef offsetof
115 #define offsetof(type, member) ((size_t) &((type*)0)->member)
116 #endif
117 #ifndef containerof
118 #define containerof(ptr, type, member) ((type*)((char*)ptr - offsetof(type,member)))
119 #endif
120 
121 /* min and max */
122 
123 #ifndef MIN
124    #define MIN(a, b)    (((a) < (b)) ? (a) : (b))
125 #endif
126 #ifndef MAX
127    #define MAX(a, b)    (((a) > (b)) ? (a) : (b))
128 #endif
129 
130 /* timeunit conversions */
131 #define SEC2NANO(x)     x * 1000000000
132 #define MILLI2NANO(x)   x *    1000000
133 #define MICRO2NANO(x)   x *       1000
134 #define SEC2MICRO(x)    x *    1000000
135 #define MILLI2MICRO(x)  x *       1000
136 #define MILLI2SEC(x)    x /       1000
137 #define MICRO2SEC(x)    x /    1000000
138 #define NANO2SEC(x)     x / 1000000000
139 
140 /* file operations */
141 #ifndef OS_WINDOWS
142    #define O_BINARY  0
143 #endif
144 
145 /* bit operations */
146 
147 #define BIT_SET(r,b)       ( r[b>>3] |=   1<<(b&7) )
148 #define BIT_RESET(r,b)     ( r[b>>3] &= ~ 1<<(b&7) )
149 #define BIT_TEST(r,b)      ( r[b>>3]  &   1<<(b&7) )
150 #define BIT_NOT(r,b)       ( r[b>>3] ^=   1<<(b&7) )
151 
152 /* Save and restore relative offsets for pointers into a buffer */
153 #define SAVE_OFFSET(o,b)     o=(u_int8 *)((int)o-(int)b)
154 #define RESTORE_OFFSET(o,b)  o=(u_int8 *)((int)o+(int)b)
155 
156 /* ANSI colors */
157 #ifndef OS_WINDOWS
158    #define EC_COLOR_END    "\033[0m"
159    #define EC_COLOR_BOLD   "\033[1m"
160 
161    #define EC_COLOR_RED    "\033[31m"EC_COLOR_BOLD
162    #define EC_COLOR_YELLOW "\033[33m"EC_COLOR_BOLD
163    #define EC_COLOR_GREEN  "\033[32m"EC_COLOR_BOLD
164    #define EC_COLOR_BLUE   "\033[34m"EC_COLOR_BOLD
165    #define EC_COLOR_CYAN   "\033[36m"EC_COLOR_BOLD
166 #else
167    /* Windows console doesn't grok ANSI */
168    #define EC_COLOR_END
169    #define EC_COLOR_BOLD
170 
171    #define EC_COLOR_RED
172    #define EC_COLOR_YELLOW
173    #define EC_COLOR_GREEN
174    #define EC_COLOR_BLUE
175    #define EC_COLOR_CYAN
176 #endif
177 
178 /* magic numbers */
179 
180 #ifndef RANDMAGIC
181    #define EC_MAGIC_8   0xec
182    #define EC_MAGIC_16  0xe77e
183    #define EC_MAGIC_32  0xe77ee77e
184 #else
185    #define EC_MAGIC_8   ((RANDMAGIC & 0x0ff0) >> 4)
186    #define EC_MAGIC_16  (RANDMAGIC & 0xffff)
187    #define EC_MAGIC_32  (((RANDMAGIC & 0xff) << 8)|((RANDMAGIC & 0xff00) >> 8)|(RANDMAGIC & 0xffff0000))
188 #endif
189 
190 /* exported by ec_main */
191 EC_API_EXTERN void clean_exit(int errcode);
192 
193 /* exported by ec_mem */
194 EC_API_EXTERN void safe_free_mem(char **param, int *param_length, char *command);
195 
196 
197 #endif   /*  EC_H */
198 
199 /* EOF */
200 
201 // vim:ts=3:expandtab
202 
203