1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
/*
* @file tfe_utils.h
* This file provides common usage marcos and helper functions.
*/
#pragma once
#include <MESA/MESA_handle_logger.h>
#include <MESA/MESA_htable.h>
#include <time.h>
#include <dirent.h> //scan_dir
#include <stdbool.h>
#define LOG_TAG_POLICY "POLICY"
#define LOG_TAG_UTILS "UTILS"
#define LOG_TAG_RAWPKT "RAW_PACKET"
#define LOG_TAG_CTRLPKT "CTRL_PACKET"
#define LOG_TAG_STABLE "SESSION_TABLE"
#define LOG_TAG_PKTIO "PACKET_IO"
#define LOG_TAG_METRICS "G_METRICS"
#define LOG_TAG_SF_METRICS "SF_METRICS"
#define LOG_TAG_SF_STATUS "SF_STATUS"
#define LOG_TAG_SCE "SCE"
#define LOG_TAG_TIMESTAMP "TIMESTAMP"
#define TFE_STRING_MAX 2048
#define TFE_PATH_MAX 256
#define TFE_SYMBOL_MAX 64
#define TFE_THREAD_MAX 128
#define TFE_FAKE_C_DEFAULT_TTL 60
#define TFE_FAKE_S_DEFAULT_TTL 65
#ifndef TFE_CONFIG_BACKLOG_DEFAULT
#define TFE_CONFIG_BACKLOG_DEFAULT 20
#endif
#define ALLOC(type, number) ((type *)calloc(sizeof(type), number))
#define FREE(p) {free(*p);*p=NULL;}
#define TFE_STRUCT_ALLOC(struct_type) __extension__ \
({ \
((struct_type) * ) __struct_ptr; \
__struct_ptr = ((struct_type) *)malloc(sizeof((struct_type))); \
__struct_ptr; \
})
#define likely(expr) __builtin_expect((expr), 1)
#define unlikely(expr) __builtin_expect((expr), 0)
#define UNUSED __attribute__((unused))
extern void * g_default_logger;
extern bool g_print_to_stderr;
#define TFE_LOG_ERROR(handler, fmt, ...) \
do \
{ \
if (g_print_to_stderr) \
{ \
fprintf(stderr, fmt "\n", ##__VA_ARGS__); \
} \
if (MESA_handle_runtime_log_level_enabled(handler, RLOG_LV_FATAL)) \
{ \
MESA_handle_runtime_log(handler, RLOG_LV_FATAL, __FUNCTION__, fmt, ##__VA_ARGS__); \
} \
} while (0)
#define TFE_LOG_INFO(handler, fmt, ...) \
do \
{ \
if (g_print_to_stderr) \
{ \
fprintf(stderr, fmt "\n", ##__VA_ARGS__); \
} \
if (MESA_handle_runtime_log_level_enabled(handler, RLOG_LV_INFO)) \
{ \
MESA_handle_runtime_log(handler, RLOG_LV_INFO, __FUNCTION__, fmt, ##__VA_ARGS__); \
} \
} while (0)
#define TFE_LOG_DEBUG(handler, fmt, ...) \
do \
{ \
if (g_print_to_stderr) \
{ \
fprintf(stderr, fmt "\n", ##__VA_ARGS__); \
} \
if (MESA_handle_runtime_log_level_enabled(handler, RLOG_LV_DEBUG)) \
{ \
MESA_handle_runtime_log(handler, RLOG_LV_DEBUG, __FUNCTION__, fmt, ##__VA_ARGS__); \
} \
} while (0)
#define CHECK_OR_EXIT(condition, fmt, ...) \
do { if(!(condition)) { TFE_LOG_ERROR(g_default_logger, fmt, ##__VA_ARGS__); exit(EXIT_FAILURE); } } while(0) \
#define DIE(fmt, ...) \
do { TFE_LOG_ERROR(g_default_logger, "DIE: " fmt, ##__VA_ARGS__); abort(); } while(0) \
#define CHECK_OR_DIE(condition, fmt, ...) \
do { if(!(condition)) { TFE_LOG_ERROR(g_default_logger, fmt, ##__VA_ARGS__); abort(); } } while(0) \
#define TFE_STREAM_LOG_DEBUG(stream, fmt, ...)
#define TFE_STREAM_LOG_INFO(stream, fmt, ...)
#define TFE_STREAM_LOG_ERROR(stream, fmt, ...) do { fprintf(stderr, fmt "\n", ##__VA_ARGS__); } while(0)
#define TFE_STREAM_TRACE_TAG_INFO(stream, tag, fmt, ...)
#define TFE_STREAM_TRACE_TAG_VERBOSE(stream, tag, fmt, ...)
#ifndef offsetof
/** Return the offset of a field in a structure. */
#define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
#endif
/**
* Return pointer to the wrapping struct instance.
*
* Example:
*
* struct wrapper {
* ...
* struct child c;
* ...
* };
*
* struct child *x = obtain(...);
* struct wrapper *w = container_of(x, struct wrapper, c);
*/
#ifndef container_of
#define container_of(ptr, type, member) __extension__ ({ \
const typeof(((type *)0)->member) *_ptr = (ptr); \
__attribute__((unused)) type *_target_ptr = (type *)(ptr); \
(type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
})
#endif
#define ATOMIC_INC(x) __atomic_fetch_add(x,1,__ATOMIC_RELAXED)
#define ATOMIC_DEC(x) __atomic_fetch_sub(x,1,__ATOMIC_RELAXED)
#define ATOMIC_READ(x) __atomic_load_n(x, __ATOMIC_RELAXED)
//#define ATOMIC_READ(x) __atomic_fetch_add(x,0,__ATOMIC_RELAXED)
#define ATOMIC_ADD(x, y) __atomic_fetch_add(x,y,__ATOMIC_RELAXED)
#define ATOMIC_ZERO(x) __atomic_fetch_and(x,0,__ATOMIC_RELAXED)
#define ATOMIC_SET(x,y) __atomic_store_n(x,y,__ATOMIC_RELAXED)
#ifndef MAX
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
char* tfe_strdup(const char* s);
char *tfe_thread_safe_ctime(const time_t *tp, char *buf, int len);
#define TFE_SET_USED(x) (void)(x)
#define TFE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
#define TFE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
#define TFE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
#define TFE_DIM(x) (sizeof (x) / sizeof ((x)[0]))
#ifndef TAILQ_FOREACH_SAFE
#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
for ((var) = TAILQ_FIRST((head)); \
(var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
(var) = (tvar))
#endif
#include <stdio.h>
#include <stdint.h>
static inline void tfe_hexdump2file(FILE *f, const char * title, const void * buf, unsigned int len)
{
unsigned int i, out, ofs;
const unsigned char *data = (const unsigned char *)buf;
#define LINE_LEN 80
char line[LINE_LEN]; /* space needed 8+16*3+3+16 == 75 */
fprintf(f, "%s at [%p], len=%u\n", (title)? title : " Dump data", data, len);
ofs = 0;
while (ofs < len) {
/* format the line in the buffer, then use printf to output to screen */
out = snprintf(line, LINE_LEN, "%08X:", ofs);
for (i = 0; ((ofs + i) < len) && (i < 16); i++)
out += snprintf(line+out, LINE_LEN - out, " %02X", (data[ofs+i] & 0xff));
for(; i <= 16; i++)
out += snprintf(line+out, LINE_LEN - out, " | ");
for(i = 0; (ofs < len) && (i < 16); i++, ofs++) {
unsigned char c = data[ofs];
if ( (c < ' ') || (c > '~'))
c = '.';
out += snprintf(line+out, LINE_LEN - out, "%c", c);
}
fprintf(f, "%s\n", line);
}
fflush(f);
#undef LINE_LEN
}
static inline unsigned char* tfe_hexdump(unsigned char *dst, unsigned char *src, size_t len)
{
static unsigned char hex[] = "0123456789abcdef";
while (len--) {
*dst++ = hex[*src >> 4];
*dst++ = hex[*src++ & 0xf];
}
return dst;
}
int tfe_scandir(const char *dir, struct dirent ***namelist,
int(*filter)(const struct dirent *),
int(*compar)(const void *, const void *));
char *tfe_read_file(const char *filename, size_t *filelen);
const char * tfe_version();
int tfe_decode_base64url(u_char *dst, u_char *src);
/******************************************************************************
* sids
******************************************************************************/
typedef uint16_t sid_t;
#define MR_SID_LIST_MAXLEN 8
struct sids
{
int num;
sid_t elems[MR_SID_LIST_MAXLEN];
};
void sids_write_once(struct sids *dst, struct sids *src);
void sids_copy(struct sids *dst, struct sids *src);
/******************************************************************************
* route_ctx
******************************************************************************/
struct route_ctx
{
char data[64];
int len;
};
int route_ctx_is_empty(struct route_ctx *ctx);
void route_ctx_copy(struct route_ctx *dst, struct route_ctx *src);
/******************************************************************************
* protocol
******************************************************************************/
struct udp_hdr
{
u_int16_t uh_sport; /* source port */
u_int16_t uh_dport; /* destination port */
u_int16_t uh_ulen; /* udp length */
u_int16_t uh_sum; /* udp checksum */
} __attribute__((__packed__));
int str_to_mac(const char *str, char *mac_buff);
int get_mac_by_device_name(const char *dev_name, char *mac_buff);
|