-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathutil.h
More file actions
306 lines (257 loc) · 8.74 KB
/
util.h
File metadata and controls
306 lines (257 loc) · 8.74 KB
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
/**
* Utility for common operation(debug, log, numeric conversion)
*/
#ifndef HARDCODER_UTIL_H_H
#define HARDCODER_UTIL_H_H
#include <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
#include <fcntl.h>
#include <limits.h>
#include <list>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sstream>
#include <sys/errno.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/select.h>
#include <unistd.h>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <semaphore.h>
char const *TAG = "HARDCODER"; // default log tag
static void setTag(char const *tag) {
TAG = tag;
}
static bool DEBUG = true; // global debug flag for native code default(false)
#define UNUSED(x) (void)(x)
#define TOINT(x) ((int)( ((int64_t)x) & 0xffffffffL ) )
#define MIN(a,b) (((a)<(b))?(a):(b))
#if defined(ANDROID_NDK)
#include <android/log.h>
#include <fstream>
int (*logFunc)(int prio, const char *tag, const char *fmt, ...) = &__android_log_print;
#define __FILENAME__ (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1): \
(strrchr(__FILE__, '\\') ? (strrchr(__FILE__, '\\') + 1):__FILE__ ) )
#define perr(format, ...) (*logFunc)(ANDROID_LOG_ERROR , TAG, "[%s,%s:%d]"#format, __FILENAME__, __FUNCTION__, __LINE__ , ##__VA_ARGS__);
#define pwrn(format, ...) (*logFunc)(ANDROID_LOG_WARN , TAG, "[%s,%s:%d]"#format, __FILENAME__, __FUNCTION__, __LINE__ , ##__VA_ARGS__);
#define pdbg(format, ...) if (DEBUG)((*logFunc)(ANDROID_LOG_DEBUG , TAG, "[%s,%s:%d]"#format, __FILENAME__ , __FUNCTION__, __LINE__ , ##__VA_ARGS__));
#else
#define perr(format, ...) do{struct timeval now; gettimeofday(&now, NULL);tm tm = *localtime((const time_t*)&(now.tv_sec));\
fprintf(stderr ,"ERR:[%02d:%02d:%02d.%.3ld][%s:%d]"#format"\n", tm.tm_hour, tm.tm_min, tm.tm_sec, now.tv_usec / 1000, \
__FUNCTION__, __LINE__ , ##__VA_ARGS__);}while(0);
#define pwrn(format, ...) do{struct timeval now; gettimeofday(&now, NULL);tm tm = *localtime((const time_t*)&(now.tv_sec));\
fprintf(stderr ,"WRN:[%02d:%02d:%02d.%.3ld][%s:%d]"#format"\n", tm.tm_hour, tm.tm_min, tm.tm_sec, now.tv_usec / 1000, \
__FUNCTION__, __LINE__ , ##__VA_ARGS__);}while(0);
#define pdbg(format, ...) if (DEBUG) do{struct timeval now; gettimeofday(&now, NULL);tm tm = *localtime((const time_t*)&(now.tv_sec));\
fprintf(stderr ,"DBG:[%02d:%02d:%02d.%.3ld][%s:%d]"#format"\n", tm.tm_hour, tm.tm_min, tm.tm_sec, now.tv_usec / 1000, \
__FUNCTION__, __LINE__ , ##__VA_ARGS__);}while(0);
#endif
template<typename T, int queuelen>
class rqueue {
private:
std::deque<T> _q;
pthread_mutex_t _mutex;
sem_t _semcnt;
sem_t _semmax;
public:
rqueue() {
sem_init(&_semmax, 0, queuelen ? queuelen : 65535);
sem_init(&_semcnt, 0, 0);
pthread_mutex_init(&_mutex, NULL);
}
~rqueue() {
sem_destroy(&_semmax);
sem_destroy(&_semcnt);
pthread_mutex_destroy(&_mutex);
}
void push(T obj) {
sem_wait(&_semmax);
pthread_mutex_lock(&_mutex);
_q.push_back(obj);
pthread_mutex_unlock(&_mutex);
sem_post(&_semcnt);
}
T pop() {
sem_wait(&_semcnt);
pthread_mutex_lock(&_mutex);
T obj = _q.front();
_q.pop_front();
pthread_mutex_unlock(&_mutex);
sem_post(&_semmax);
return obj;
}
T front() {
pthread_mutex_lock(&_mutex);
T obj = _q.size() ? _q.front() : NULL;
pthread_mutex_unlock(&_mutex);
return obj;
}
unsigned int size() {
pthread_mutex_lock(&_mutex);
unsigned int s = _q.size();
pthread_mutex_unlock(&_mutex);
return s;
}
};
static int64_t getMillisecond() {
struct timeval now;
gettimeofday(&now, NULL);
return (((int64_t) now.tv_sec) * 1000) + now.tv_usec / 1000;
}
static std::string sockaddrToString(struct sockaddr_in addr) {
char a[32] = {0};
unsigned char *bytes = (unsigned char *) &addr.sin_addr;
snprintf(a, sizeof(a), "%d.%d.%d.%d:%d", bytes[0], bytes[1], bytes[2], bytes[3], ntohs(addr.sin_port));
return (std::string) a;
}
static sockaddr_in toSockaddr(const char *ip, uint16_t port) {
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
inet_pton(AF_INET, ip, &(addr.sin_addr));
return addr;
}
static int sockaddrToIpPort(struct sockaddr_in addr, std::string *ip, int *pPort) {
char a[32] = {0};
unsigned char *bytes = (unsigned char *) &addr.sin_addr;
snprintf(a, sizeof(a), "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]);
*ip = a;
*pPort = ntohs(addr.sin_port);
return 0;
}
static int64_t sockaddrToint64(struct sockaddr_in addr) {
int64_t s = addr.sin_port;
return (s << 32) + addr.sin_addr.s_addr;
}
static const uint8_t crc4_table[16] = {0, 3, 6, 5, 12, 15, 10, 9, 11, 8, 13, 14, 7, 4, 1, 2};
static uint32_t getcrc4(void *data, uint32_t len) { // RETURN 4 bit crc
uint8_t *p = (uint8_t *) data;
uint8_t crc = 0;
for (uint32_t k = 0; k < len; k++) {
crc ^= p[k] >> 4;
crc = crc4_table[crc];
crc ^= p[k] & 0xF;
crc = crc4_table[crc];
}
return crc;
}
static unsigned int Crc32Table[256] = {0};
static unsigned int getCrc32(uint8_t *data, uint32_t len) {
int i, j;
unsigned int Crc;
if (Crc32Table[0] == 0) { //create CRC32 table
for (i = 0; i < 256; i++) {
Crc = i;
for (j = 0; j < 8; j++) {
if (Crc & 1)
Crc = (Crc >> 1) ^ 0xEDB88320;
else
Crc >>= 1;
}
Crc32Table[i] = Crc;
}
}
Crc = 0xffffffff;
for (uint32_t i = 0; i < len; i++) {
Crc = (Crc >> 8) ^ Crc32Table[(Crc & 0xFF) ^ (data[i])];
}
Crc ^= 0xFFFFFFFF;
return Crc;
}
static char* getVersion(const char* str, const char* delims) {
if (str == NULL || delims == NULL) {
return NULL;
}
size_t str_len = strlen(str);
char* result = new char[str_len + 1];
if (result == NULL) {
return NULL;
}
const char* delim_ptr = strstr(str, delims);
if (delim_ptr != NULL) {
size_t delims_len = strlen(delims);
size_t prefix_len = ((unsigned long) delim_ptr - (unsigned long) str) / sizeof(char) + 1;
size_t suffix_len = str_len - prefix_len - delims_len + 1;
strncpy(result, delim_ptr + delims_len, suffix_len);
result[suffix_len] = '\0';
} else {
strncpy(result, str, str_len);
result[str_len] = '\0';
}
return result;
}
static char* getSocketName(const char* str, const char* delims) {
if (str == NULL || delims == NULL) {
return NULL;
}
size_t str_len = strlen(str);
char* result = new char[str_len + 1];
if (result == NULL) {
return NULL;
}
const char* delim_ptr = strstr(str, delims);
if (delim_ptr != NULL) {
size_t prefix_len = ((unsigned long) delim_ptr - (unsigned long) str) / sizeof(char);
strncpy(result, str, prefix_len);
result[prefix_len] = '\0';
} else {
strncpy(result, str, str_len);
result[str_len] = '\0';
}
return result;
}
class LocalPortCheck {
public:
const static int TCP_PORT = 1;
const static int UDP_PORT = 2;
static int getUidByPort(const int type, const int port) {
char szPort[8];
snprintf(szPort, sizeof(szPort), "%X", port);
perr("port type:%d port:%x hex:%s", type, port, szPort);
char const *path = NULL;
if (type == TCP_PORT) path = "/proc/net/tcp";
else if (type == UDP_PORT) path = "/proc/net/udp";
else return -2;
FILE *fp = fopen(path, "r");
if (!fp) {
return -3;
}
char line[4096];
while (fgets(line, sizeof(line), fp)) {
char *saveptr = line;
char *token = NULL;
int portHitRow = 0;
int colIndex = 0;
while ((token = strtok_r(saveptr, " ", &saveptr)) != NULL) {
if (colIndex == 1) {
char *saveptr2 = token;
char *token2 = NULL;
while ((token2 = strtok_r(saveptr2, ":", &saveptr2)) != NULL) {
if (!strcmp(szPort, token2)) portHitRow = 1;
}
}
if (portHitRow == 1 && colIndex == 7) {
fclose(fp);
return atoi(token);
}
colIndex++;
}
}
fclose(fp);
return -1;
}
};
#endif //HARDCODER_UTIL_H_H