-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcnips.c
More file actions
61 lines (46 loc) · 1011 Bytes
/
Copy pathcnips.c
File metadata and controls
61 lines (46 loc) · 1011 Bytes
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
/**
* License: MIT License
*
* greensea <gs@bbxy.net>
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
unsigned int hostnum2subnet(uint32_t num) {
unsigned int ret = 32;
while (num != 1) {
num >>= 1;
ret--;
}
return ret;
}
void parse_line(char* line) {
char* ip;
char *numstr;
int num;
/// 过滤出中国的地址
if (strstr(line, "CN") == NULL) {
return;
}
if (strstr(line, "ipv4") == NULL) {
return;
}
/// 提取 IP 地址和主机数量
strtok(line, "|");
strtok(NULL, "|");
strtok(NULL, "|");
ip = strtok(NULL, "|");
numstr = strtok(NULL, "|");
/// 输出网段
num = atoi(numstr);
printf("%s/%d\n", ip, hostnum2subnet(num));
}
int main(int argc, char* argv[]) {
char* line;
size_t linesize;
while (getline(&line, &linesize, stdin) != -1) {
parse_line(line);
}
return 0;
}