-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpfutils.c
294 lines (230 loc) · 6.76 KB
/
pfutils.c
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
*
* (C) 2005-12 - Luca Deri <deri@ntop.org>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* VLAN support courtesy of Vincent Magnin <vincent.magnin@ci.unil.ch>
*
*/
//#include "../lib/config.h"
#ifndef __USE_GNU
#define __USE_GNU
#endif
//#define _GNU_SOURCE
#include <pthread.h>
#include <sched.h> /* for CPU_XXXX */
#include <sys/types.h>
#include <pwd.h>
#include <sys/stat.h>
#include <numa.h>
#define TRACE_ERROR 0, __FILE__, __LINE__
#define TRACE_WARNING 1, __FILE__, __LINE__
#define TRACE_NORMAL 2, __FILE__, __LINE__
#define TRACE_INFO 3, __FILE__, __LINE__
typedef u_int64_t ticks;
struct compact_eth_hdr {
unsigned char h_dest[ETH_ALEN];
unsigned char h_source[ETH_ALEN];
u_int16_t h_proto;
};
struct compact_ip_hdr {
u_int8_t ihl:4,
version:4;
u_int8_t tos;
u_int16_t tot_len;
u_int16_t id;
u_int16_t frag_off;
u_int8_t ttl;
u_int8_t protocol;
u_int16_t check;
u_int32_t saddr;
u_int32_t daddr;
};
struct compact_ipv6_hdr {
u_int8_t priority:4,
version:4;
u_int8_t flow_lbl[3];
u_int16_t payload_len;
u_int8_t nexthdr;
u_int8_t hop_limit;
struct in6_addr saddr;
struct in6_addr daddr;
};
struct compact_udp_hdr {
u_int16_t sport;
u_int16_t dport;
u_int16_t len;
u_int16_t check;
};
/* ******************************** */
void daemonize() {
pid_t pid, sid;
pid = fork();
if (pid < 0) exit(EXIT_FAILURE);
if (pid > 0) {
#if 0 /* moved out */
if (pidFile != NULL) {
FILE *fp = fopen(pidFile, "w");
fprintf(fp, "%d", pid);
fclose(fp);
}
#endif
exit(EXIT_SUCCESS);
}
sid = setsid();
if (sid < 0) exit(EXIT_FAILURE);
if ((chdir("/")) < 0) exit(EXIT_FAILURE);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
/* ******************************** */
void drop_privileges(char *username) {
struct passwd *pw = NULL;
if (getgid() && getuid()) {
fprintf(stderr, "privileges are not dropped as we're not superuser\n");
return;
}
pw = getpwnam(username);
if(pw == NULL) {
username = "nobody";
pw = getpwnam(username);
}
if(pw != NULL) {
if(setgid(pw->pw_gid) != 0 || setuid(pw->pw_uid) != 0)
fprintf(stderr, "unable to drop privileges [%s]\n", strerror(errno));
else
fprintf(stderr, "user changed to %s\n", username);
} else {
fprintf(stderr, "unable to locate user %s\n", username);
}
umask(0);
}
/* ******************************** */
void create_pid_file(char *pidFile) {
FILE *fp;
if (pidFile == NULL) return;
fp = fopen(pidFile, "w");
if (fp == NULL) {
fprintf(stderr, "unable to create pid file %s: %s\n", pidFile, strerror(errno));
return;
}
fprintf(fp, "%d\n", getpid());
fclose(fp);
}
/* ******************************** */
void remove_pid_file(char *pidFile) {
if (pidFile == NULL) return;
unlink(pidFile);
}
/* *************************************** */
/*
* The time difference in millisecond
*/
double delta_time (struct timeval * now,
struct timeval * before) {
time_t delta_seconds;
time_t delta_microseconds;
/*
* compute delta in second, 1/10's and 1/1000's second units
*/
delta_seconds = now -> tv_sec - before -> tv_sec;
delta_microseconds = now -> tv_usec - before -> tv_usec;
if(delta_microseconds < 0) {
/* manually carry a one from the seconds field */
delta_microseconds += 1000000; /* 1e6 */
-- delta_seconds;
}
return((double)(delta_seconds * 1000) + (double)delta_microseconds/1000);
}
/* *************************************** */
#define MSEC_IN_DAY (1000 * 60 * 60 * 24)
#define MSEC_IN_HOUR (1000 * 60 * 60)
#define MSEC_IN_MINUTE (1000 * 60)
#define MSEC_IN_SEC (1000)
char *msec2dhmsm(u_int64_t msec, char *buf, u_int buf_len) {
snprintf(buf, buf_len, "%u:%02u:%02u:%02u:%03u",
(unsigned) (msec / MSEC_IN_DAY),
(unsigned) (msec / MSEC_IN_HOUR) % 24,
(unsigned) (msec / MSEC_IN_MINUTE) % 60,
(unsigned) (msec / MSEC_IN_SEC) % 60,
(unsigned) (msec) % 1000
);
return(buf);
}
/* *************************************** */
int bind2node(int core_id) {
char node_str[8];
if (core_id < 0 || numa_available() == -1)
return -1;
snprintf(node_str, sizeof(node_str), "%u", numa_node_of_cpu(core_id));
numa_bind(numa_parse_nodestring(node_str));
return 0;
}
/* *************************************** */
/* Bind this thread to a specific core */
int bindthread2core(pthread_t thread_id, u_int core_id) {
#ifdef HAVE_PTHREAD_SETAFFINITY_NP
cpu_set_t cpuset;
int s;
CPU_ZERO(&cpuset);
CPU_SET(core_id, &cpuset);
if((s = pthread_setaffinity_np(thread_id, sizeof(cpu_set_t), &cpuset)) != 0) {
fprintf(stderr, "Error while binding to core %u: errno=%i\n", core_id, s);
return(-1);
} else {
return(0);
}
#else
fprintf(stderr, "WARNING: your system lacks of pthread_setaffinity_np() (not core binding)\n");
return(0);
#endif
}
/* *************************************** */
/* Bind the current thread to a core */
int bind2core(u_int core_id) {
return(bindthread2core(pthread_self(), core_id));
}
/* *************************************** */
static __inline__ ticks getticks(void)
{
u_int32_t a, d;
// asm("cpuid"); // serialization
asm volatile("rdtsc" : "=a" (a), "=d" (d));
return (((ticks)a) | (((ticks)d) << 32));
}
/* *************************************** */
void trace(int trace_level, char* file, int line, char * format, ...) {
va_list va_ap;
char buf[2048], out_buf[640];
char theDate[32], *extra_msg = "";
time_t theTime = time(NULL);
va_start(va_ap, format);
memset(buf, 0, sizeof(buf));
strftime(theDate, 32, "%d/%b/%Y %H:%M:%S", localtime(&theTime));
vsnprintf(buf, sizeof(buf)-1, format, va_ap);
if(trace_level == 0)
extra_msg = "ERROR: ";
else if(trace_level == 1)
extra_msg = "WARNING: ";
while(buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';
snprintf(out_buf, sizeof(out_buf), "%s [%s:%d] %s%s", theDate, file, line, extra_msg, buf);
fprintf(trace_level ? stdout : stderr, "%s\n", out_buf);
fflush(stdout);
va_end(va_ap);
}
/* *************************************** */