-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.hpp
166 lines (147 loc) · 3.97 KB
/
common.hpp
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
#ifndef __common_hpp__
#define __common_hpp__
#include <stdio.h>
#include <string.h>
#include <vector>
#include <pthread.h>
#include <arpa/inet.h>
bool debug = true;
pthread_mutex_t debug_mutex;
#ifdef DEBUG
#define MESG(format, ...) do { \
if (debug) { \
fprintf(stderr, "%s:%s(%d): " format "\n", \
__FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
} \
} while (false)
#else
#define MESG(format, ...) do {} while (false)
#endif //DEBUG
#ifdef DEBUG
#define PERROR(func) do { \
if (debug) { \
char s[BUFSIZ]; \
memset(s, 0, BUFSIZ); \
snprintf(s, BUFSIZ, "%s:%s(%d): %s", \
__FILE__, __FUNCTION__, __LINE__, func); \
perror(s); \
} \
} while (false)
#else
#define PERROR(func) do {} while (false)
#endif //DEBUG
static void pktdump(uint8_t* buf, int len)
{
#define P(x) ((c >= ' ' && c < 0x7f) ? c : '.')
int i;
char t[128];
char hex[] = "0123456789abcdef";
fprintf(stderr, "--- %d bytes at %p\n", len, buf);
if (len > 160) {
len = 160;
}
for (i = 0; i < len; i++) {
uint8_t c = (uint8_t)buf[i];
int o = i % 16;
if (o == 0) {
if (i > 0) {
fprintf(stderr, "%s\n", t);
}
memset(t, ' ', 79);
t[80] = '\0';
t[0] = hex[(i>>12) & 0xf];
t[1] = hex[(i>>8) & 0xf];
t[2] = hex[(i>>4) & 0xf];
t[3] = hex[(i>>0) & 0xf];
t[4] = ':';
}
t[6 + 3*o + (o >> 3)] = hex[c >> 4];
t[7 + 3*o + (o >> 3)] = hex[c & 0xf];
t[56 + o + (o >> 3)] = P(c);
}
if (i % 16 != 0) {
fprintf(stderr, "%s\n", t);
}
return;
}
void memdump(void* buffer, int length)
{
uint32_t* addr32 = (uint32_t*)buffer;
int i;
int j;
int k;
int lines = length/16 + (length%16?1:0);
if (lines > 1 || length == 16) {
for (i=0; i<lines; i++) {
printf("%p : %08x %08x %08x %08x\n",
addr32,
htonl(*(addr32)),
htonl(*(addr32+1)),
htonl(*(addr32+2)),
htonl(*(addr32+3))
);
addr32 += 4;
}
} else {
}
j = length%16;
if (j == 0) {
return;
}
k = 0;
uint8_t* addr8 = (uint8_t*)addr32;
printf("%p : ", addr8);
for (i=0; i<16; i++) {
if (k%4 == 0 && i != 0) printf(" ");
if (j > i) {
printf("%02x", *addr8);
addr8++;
} else {
printf("XX");
}
k++;
}
printf("\n");
return;
}
inline void trim_space(std::string& buf)
{
size_t pos;
while ((pos = buf.find_first_of(" \t")) != std::string::npos) {
buf.erase(pos, 1);
}
}
std::vector<std::string>
split(const std::string& src, const char *c)
{
std::vector<std::string> retval;
std::string::size_type i = 0;
std::string::size_type j = src.find(c);
std::string tmp = src.substr(i,j-i);
if (tmp.size() == 0) return retval;
retval.push_back(tmp);
while (j != std::string::npos) {
i = j++;
j = src.find(c,j);
if (j == std::string::npos) {
retval.push_back(src.substr(i+1, src.size()));
break;
}
tmp = src.substr(i,j-i);
retval.push_back(tmp.substr(1, tmp.size()));
}
return retval;
}
/*
uint64_t dwcas(uint64_t* adr, uint64_t nval, uint64_t cmp)
{
uint64_t old;
__asm__ __volatile__(
"lock cmpxchg8b %0\n\t"
: "=a" (old) ,"+m" (*adr)
: "d" ((uint32_t)(cmp >> 32)), "a" ((uint32_t)(cmp & 0xffffffff)),
"c" ((uint32_t)(nval >> 32)), "b" ((uint32_t)(nval & 0xffffffff))
: "cc");
}
*/
#endif //__common_hpp__