-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.cpp
313 lines (231 loc) · 6.28 KB
/
utils.cpp
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// (C) 2020-2023 by folkert van heusden <[email protected]>, CC0 license
#include <algorithm>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <vector>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
#include <sys/syscall.h>
#define gettid() pid_t(syscall(SYS_gettid))
#endif
#include "utils.h"
// replace by std::format when c++20 is more common
std::string myformat(const char *const fmt, ...)
{
char *buffer = nullptr;
va_list ap;
va_start(ap, fmt);
(void)vasprintf(&buffer, fmt, ap);
va_end(ap);
std::string result = buffer;
free(buffer);
return result;
}
uint64_t get_us()
{
timespec ts { 0, 0 };
if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
error_exit(true, "clock_gettime failed");
return uint64_t(ts.tv_sec) * uint64_t(1000000l) + uint64_t(ts.tv_nsec / 1000);
}
uint64_t get_ms()
{
timespec ts { 0, 0 };
if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
error_exit(true, "clock_gettime failed");
return uint64_t(ts.tv_sec) * uint64_t(1000) + uint64_t(ts.tv_nsec / 1000000);
}
void get_random(uint8_t *tgt, size_t n)
{
while(n > 0) {
size_t cur_n = std::min(n, size_t(256));
if (getentropy(tgt, cur_n) == -1)
error_exit(true, "getentropy() failed");
tgt += cur_n;
n -= cur_n;
}
}
std::string random_hex(const size_t n)
{
uint8_t *temp = new uint8_t[n];
get_random(temp, n);
std::string out;
for(size_t i=0; i<n; i++)
out += myformat("%02x", temp[i]);
delete [] temp;
return out;
}
std::vector<std::string> split(std::string in, std::string splitter)
{
std::vector<std::string> out;
size_t splitter_size = splitter.size();
for(;;)
{
size_t pos = in.find(splitter);
if (pos == std::string::npos)
break;
std::string before = in.substr(0, pos);
out.push_back(before);
size_t bytes_left = in.size() - (pos + splitter_size);
if (bytes_left == 0)
{
out.push_back("");
return out;
}
in = in.substr(pos + splitter_size);
}
if (in.size() > 0)
out.push_back(in);
return out;
}
static const char *logfile = strdup("/tmp/myip.log");
log_level_t log_level_file = warning;
log_level_t log_level_screen = warning;
static FILE *lfh = nullptr;
static int lf_uid = -1;
static int lf_gid = -1;
void setlog(const char *lf, const log_level_t ll_file, const log_level_t ll_screen)
{
if (lfh)
fclose(lfh);
free((void *)logfile);
logfile = strdup(lf);
log_level_file = ll_file;
log_level_screen = ll_screen;
}
void setloguid(const int uid, const int gid)
{
lf_uid = uid;
lf_gid = gid;
}
void closelog()
{
fclose(lfh);
lfh = nullptr;
}
void dolog(const log_level_t ll, const char *fmt, ...)
{
if (ll < log_level_file && ll < log_level_screen)
return;
if (!lfh) {
lfh = fopen(logfile, "a+");
if (!lfh)
error_exit(true, "Cannot access log-file %s", logfile);
if (lf_uid != -1 && fchown(fileno(lfh), lf_uid, lf_gid) == -1)
error_exit(true, "Cannot change logfile (%s) ownership", logfile);
if (fcntl(fileno(lfh), F_SETFD, FD_CLOEXEC) == -1)
error_exit(true, "fcntl(FD_CLOEXEC) failed");
}
uint64_t now = get_us();
time_t t_now = now / 1000000;
tm tm { 0 };
if (!localtime_r(&t_now, &tm))
error_exit(true, "localtime_r failed");
char *ts_str = nullptr;
const char *const ll_names[] = { "debug ", "info ", "warning", "error " };
asprintf(&ts_str, "%04d-%02d-%02d %02d:%02d:%02d.%06d %.6f|%d] %s ",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, int(now % 1000000),
get_us() / 1000000.0, gettid(), ll_names[ll]);
char *str = nullptr;
va_list ap;
va_start(ap, fmt);
(void)vasprintf(&str, fmt, ap);
va_end(ap);
if (ll >= log_level_file)
fprintf(lfh, "%s%s", ts_str, str);
if (ll >= log_level_screen)
printf("%s%s", ts_str, str);
free(str);
free(ts_str);
}
void set_thread_name(std::string name)
{
if (name.length() > 15)
name = name.substr(0, 15);
DOLOG(debug, "Set name of thread %d to \"%s\"\n", gettid(), name.c_str());
pthread_setname_np(pthread_self(), name.c_str());
}
void myusleep(uint64_t us)
{
timespec req { 0 };
req.tv_sec = us / 1000000l;
req.tv_nsec = (us % 1000000l) * 1000l;
for(;;) {
timespec rem { 0, 0 };
int rc = nanosleep(&req, &rem);
if (rc == 0 || (rc == -1 && errno != EINTR)) {
if (rc == -1)
error_exit(true, "nanosleep failed");
break;
}
memcpy(&req, &rem, sizeof(timespec));
}
}
std::string str_tolower(std::string s)
{
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c){ return std::tolower(c); });
return s;
}
std::optional<std::string> find_header(const std::vector<std::string> *const lines, const std::string & key, const std::string & seperator)
{
const std::string lkey = str_tolower(key);
std::optional<std::string> value;
for(auto line : *lines) {
auto parts = split(line, seperator);
if (parts.size() >= 2 && str_tolower(parts.at(0)) == lkey) {
value = line.substr(key.size() + 1);
while(value.value().empty() == false && value.value().at(0) == ' ')
value = value.value().substr(1);
}
}
return value;
}
std::string merge(const std::vector<std::string> & in, const std::string & seperator)
{
std::string out;
for(auto l : in)
out += l + seperator;
return out;
}
std::string replace(std::string target, const std::string & what, const std::string & by_what)
{
for(;;) {
std::size_t found = target.find(what);
if (found == std::string::npos)
break;
std::string before = target.substr(0, found);
std::size_t after_offset = found + what.size();
std::string after = target.substr(after_offset);
target = before + by_what + after;
}
return target;
}
void error_exit(const bool se, const char *format, ...)
{
int e = errno;
va_list ap;
va_start(ap, format);
char *temp = nullptr;
if (vasprintf(&temp, format, ap) == -1)
puts(format); // last resort
va_end(ap);
fprintf(stderr, "%s\n", temp);
DOLOG(ll_error, "%s\n", temp);
if (se && e) {
fprintf(stderr, "errno: %d (%s)\n", e, strerror(e));
DOLOG(ll_error, "errno: %d (%s)\n", e, strerror(e));
}
free(temp);
exit(EXIT_FAILURE);
}