-
Notifications
You must be signed in to change notification settings - Fork 6
/
common.c
300 lines (248 loc) · 5.8 KB
/
common.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
298
299
300
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <net/ethernet.h>
#include <sys/select.h>
#include <net/if_arp.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <netpacket/packet.h>
#include "common.h"
#define PATH_SEP '/'
extern int getexedir(char *exedir)
{
int cnt = readlink("/proc/self/exe", exedir, PATH_MAX);
if (cnt < 0 || cnt > PATH_MAX)
return -1;
_D("exedir: %s\n", exedir);
char *end = strrchr(exedir, PATH_SEP);
if (!end) return -1;
*(end+1) = '\0';
_D("exedir: %s\n", exedir);
return 0;
}
static int is_filtered(char const *ifname)
{
/* 过滤掉无线,虚拟机接口等 */
char const *filtered_ifs[] = {
/* windows */
"Wireless", "Microsoft",
"Virtual",
/* linux */
"lo", "wlan", "vboxnet",
"ifb", "gre", "teql",
"br", "imq", "ra",
"wds", "sit", "apcli",
NULL,
};
for (char const **if_ = filtered_ifs; *if_; if_++) {
if (strstr(ifname, *if_))
return 1;
}
return 0;
}
static char *get_ifname_from_buff(char *buff)
{
char *s;
while (isspace(*buff))
++buff;
s = buff;
while (':' != *buff && '\0' != *buff)
++buff;
*buff = '\0';
return s;
}
/**
* 获取所有网络接口
* ifnames 实际获取的接口
* cnt 两个作用,1:传入表示ifnames最多可以存储的接口个数
* 2:返回表示实际获取了的接口个数
* 返回接口个数在cnt里
* @return: >=0 成功,实际获取的接口个数
* -1 获取失败
* -2 cnt过小
*/
extern int getall_ifs(ifname_t *ifs, int *cnt)
{
int i = 0;
if (!ifs || *cnt <= 0) return -2;
#define NETDEV_PATH "/proc/net/dev"
#define BUFF_LINE_MAX 1024
char buff[BUFF_LINE_MAX];
FILE *fd = fopen(NETDEV_PATH, "r");
char *name;
if (NULL == fd) {
perror(NETDEV_PATH);
return -1;
}
/* NETDEV_PATH 文件格式如下,...表示后面我们不关心
* Inter-| Receive ...
* face |bytes packets ...
* eth0: 147125283 119599 ...
* wlan0: 229230 2635 ...
* lo: 10285509 38254 ...
*/
/* 略过开始两行 */
fgets(buff, BUFF_LINE_MAX, fd);
fgets(buff, BUFF_LINE_MAX, fd);
while (NULL != fgets(buff, BUFF_LINE_MAX, fd)) {
name = get_ifname_from_buff(buff);
_D("%s\n", name);
/* 过滤无关网络接口 */
if (is_filtered(name)) {
_D("filtered %s.\n", name);
continue;
}
strncpy(ifs[i].name, name, IF_NAMESIZE);
_D("ifs[%d].name: %s\n", i, ifs[i].name);
++i;
if (i >= *cnt) {
fclose(fd);
return -2;
}
}
fclose(fd);
*cnt = i;
return i;
}
extern char const *format_time(void)
{
static char buff[FORMAT_TIME_MAX];
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
if (NULL == timeinfo) return NULL;
strftime(buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", timeinfo);
return buff;
}
extern int copy_file(char const *f1, char const *f2)
{
if (NULL == f1 || NULL == f2) return -1;
FILE *src, *dst;
src = fopen(f1, "r");
dst = fopen(f2, "w");
if (NULL == src || NULL == dst) return -1;
char buff[1024];
int n;
while (0 < (n = fread(buff, 1, 1024, src)))
fwrite(buff, 1, n, dst);
fclose(src);
fclose(dst);
return 0;
}
/**
* 本地是否是小端序
* @return: !0: 是
* 0: 不是(大端序)
*/
static int islsb()
{
static u16 a = 0x0001;
return (int)(*(u8*)&a);
}
static u16 exorders(u16 n)
{
return ((n>>8)|(n<<8));
}
static u32 exorderl(u32 n)
{
return (n>>24)|((n&0x00ff0000)>>8)|((n&0x0000ff00)<<8)|(n<<24);
}
extern u16 htols(u16 n)
{
return islsb()? n: exorders(n);
}
extern u16 htoms(u16 n)
{
return islsb()? exorders(n): n;
}
extern u16 ltohs(u16 n)
{
return islsb()? n: exorders(n);
}
extern u16 mtohs(u16 n)
{
return islsb()? exorders(n): n;
}
extern u32 htoll(u32 n)
{
return islsb()? n: exorderl(n);
}
extern u32 htoml(u32 n)
{
return islsb()? exorderl(n): n;
}
extern u32 ltohl(u32 n)
{
return islsb()? n: exorderl(n);
}
extern u32 mtohl(u32 n)
{
return islsb()? exorderl(n): n;
}
extern u8 const *format_mac(u8 const *macarr)
{
static u8 formatmac[] = "XX:XX:XX:XX:XX:XX";
if (NULL == macarr)
return NULL;
sprintf((char*)formatmac, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
macarr[0], macarr[1], macarr[2],
macarr[3], macarr[4], macarr[5]);
return formatmac;
}
/*
* 以16进制打印数据
*/
extern void format_data(u8 const *d, size_t len)
{
int i;
for (i = 0; i < (long)len; ++i) {
if (i != 0 && i%16 == 0)
_M("\n");
_M("%02x ", d[i]);
}
_M("\n");
}
/**
* 返回t1-t0的时间差
* 由于这里精度没必要达到ns,故返回相差微秒ms
* @return: 时间差,单位微秒(1s == 1000ms)
*/
extern long difftimespec(struct timespec t1, struct timespec t0)
{
long d = t1.tv_sec-t0.tv_sec;
d *= 1000;
d += (t1.tv_nsec-t0.tv_nsec)/(long)(1e6);
return d;
}
/**
* 判断网络是否连通
* 最长延时3s,也就是说如果3s内没有检测到数据回应,那么认为网络不通
* TODO 使用icmp协议判断
* @return: !0: 连通
* 0: 没有连通
*/
extern int isnetok(char const *ifname)
{
static char baidu[] = "baidu.com";
(void)ifname;
(void)baidu; /* repress warn */
sleep(100);
return 1;
}
/**
* 休眠ms微秒
*/
extern void msleep(long ms)
{
struct timeval tv;
tv.tv_sec = ms/1000;
tv.tv_usec = ms%1000*1000;
select(0, 0, 0, 0, &tv);
}