-
Notifications
You must be signed in to change notification settings - Fork 21
/
pfutils.c
298 lines (234 loc) · 7.12 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
294
295
296
297
/*
* (C) 2003-15 - ntop
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "./PF_RING/userland/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>
#define TRACE_ERROR 0, __FILE__, __LINE__
#define TRACE_WARNING 1, __FILE__, __LINE__
#define TRACE_NORMAL 2, __FILE__, __LINE__
#define TRACE_INFO 3, __FILE__, __LINE__
#ifdef HAVE_PF_RING_ZC
#include "pfring_zc.h"
#endif
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_int32_t tot_len:16,
tos:8,
ihl:4,
version:4;
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_int32_t flow_lbl:24,
priority:4,
version:4;
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) {
#ifdef HAVE_PF_RING_ZC
if (core_id < 0)
return -1;
pfring_zc_numa_set_numa_affinity(pfring_zc_numa_get_cpu_node(core_id));
#endif
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));
}
/* *************************************** */
#define TRACE_ERROR 0, __FILE__, __LINE__
#define TRACE_WARNING 1, __FILE__, __LINE__
#define TRACE_NORMAL 2, __FILE__, __LINE__
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 == 0 ? stderr : stdout, "%s\n", out_buf);
fflush(stdout);
va_end(va_ap);
}
/* *************************************** */