├── LICENSE ├── README.md └── cnips.c /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 greensea 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | cnips 2 | ===== 3 | 4 | 获取中国的 IP 地址范围 5 | 6 | 用法 7 | === 8 | 9 | ``` 10 | gcc cnips.c -O2 -o cnips 11 | 12 | wget http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest 13 | cat delegated-apnic-latest | ./cnips 14 | 15 | ``` 16 | 17 | 配合 iproute2 来设置路由表 18 | === 19 | 20 | ``` 21 | for net in `cat delegated-apnic-latest | ./cnips` 22 | do 23 | ip route add $net via table 24 | done 25 | ``` 26 | -------------------------------------------------------------------------------- /cnips.c: -------------------------------------------------------------------------------- 1 | /** 2 | * License: MIT License 3 | * 4 | * greensea 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | 13 | unsigned int hostnum2subnet(uint32_t num) { 14 | unsigned int ret = 32; 15 | 16 | while (num != 1) { 17 | num >>= 1; 18 | ret--; 19 | } 20 | 21 | 22 | return ret; 23 | } 24 | 25 | void parse_line(char* line) { 26 | char* ip; 27 | char *numstr; 28 | int num; 29 | 30 | /// 过滤出中国的地址 31 | if (strstr(line, "CN") == NULL) { 32 | return; 33 | } 34 | if (strstr(line, "ipv4") == NULL) { 35 | return; 36 | } 37 | 38 | /// 提取 IP 地址和主机数量 39 | strtok(line, "|"); 40 | strtok(NULL, "|"); 41 | strtok(NULL, "|"); 42 | 43 | ip = strtok(NULL, "|"); 44 | numstr = strtok(NULL, "|"); 45 | 46 | /// 输出网段 47 | num = atoi(numstr); 48 | printf("%s/%d\n", ip, hostnum2subnet(num)); 49 | } 50 | 51 | int main(int argc, char* argv[]) { 52 | char* line; 53 | size_t linesize; 54 | 55 | 56 | while (getline(&line, &linesize, stdin) != -1) { 57 | parse_line(line); 58 | } 59 | 60 | return 0; 61 | } 62 | --------------------------------------------------------------------------------