Created
July 28, 2017 05:16
-
-
Save cachefiles/718607fdb1a6cda21938ecbadaae4e74 to your computer and use it in GitHub Desktop.
exclude route cacalate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
int _net_count = 0; | |
int _net_pref[25600]; | |
int _net_list[25600]; | |
int includeNetwork(int network, int prefix) { | |
int i, j = 0; | |
int net1, msk1; | |
int net0, msk0 = (1<< (32 - prefix)) - 1; | |
for (i = 0; i < _net_count; i++) { | |
int pref = _net_pref[i]; | |
if (pref <= prefix) { | |
msk1 = (1 << (32 - pref)) - 1; | |
net0 = (network & ~msk1); | |
if (_net_list[i] == net0) { | |
for (int n = 0; n < _net_count - i; n++) { | |
_net_list[j + n] = _net_list[i + n]; | |
_net_pref[j + n] = _net_pref[i + n]; | |
} | |
_net_count -= (i - j); | |
return 0; | |
} | |
_net_list[j] = _net_list[i]; | |
_net_pref[j] = _net_pref[i]; | |
j++; | |
continue; | |
} | |
net1 = (_net_list[i] & ~msk0); | |
if (net1 == network) { | |
continue; | |
} | |
_net_list[j] = _net_list[i]; | |
_net_pref[j] = _net_pref[i]; | |
j++; | |
} | |
_net_list[j] = (network & 0xffffffff); | |
_net_pref[j] = prefix; | |
_net_count = ++j; | |
return 0; | |
} | |
int excludeNetwork(int network, int prefix) { | |
int i, j = 0; | |
int net1, msk1; | |
int net0, msk0 = (1<< (32 - prefix)) - 1; | |
for (i = 0; i < _net_count; i++) { | |
int pref = _net_pref[i]; | |
// System.out.println("pref " + Long.toHexString(pref)); | |
if (pref <= prefix) { | |
msk1 = (1 << (32 - pref)) - 1; | |
net0 = (network & ~msk1) & 0xffffffff; | |
// System.out.println("net0 " + Long.toHexString(net0) + " " + Long.toHexString(_net_list[i])); | |
if (_net_list[i] == net0) { | |
net0 = _net_list[i]; | |
for (int n = 0; n < _net_count - i - 1; n++) { | |
_net_list[j + n] = _net_list[i + 1 + n]; | |
_net_pref[j + n] = _net_pref[i + 1 + n]; | |
} | |
_net_count -= (i + 1 - j); | |
for (int k = pref + 1; k <= prefix; k++) { | |
int newnet = network ^ (1 << (32 - k)); | |
msk1 = (1 << (32 - k)) - 1; | |
newnet &= 0xffffffff; | |
// System.out.println("newnet " + Long.toHexString(newnet)); | |
includeNetwork(newnet & ~msk1, (int)k); | |
} | |
return 0; | |
} | |
_net_list[j] = _net_list[i]; | |
_net_pref[j] = _net_pref[i]; | |
j++; | |
continue; | |
} | |
net1 = (_net_list[i] & ~msk0) & 0xffffffff; | |
// System.out.println("net1 " + Long.toHexString(net1)); | |
// System.out.println("network " + Long.toHexString(network)); | |
if (net1 == network) { | |
continue; | |
} | |
_net_list[j] = _net_list[i]; | |
_net_pref[j] = _net_pref[i]; | |
j++; | |
} | |
_net_count = j; | |
return 0; | |
} | |
#define ARRAY_SIZE(array) (sizeof(array)/ sizeof(*array)) | |
int main(int argc, char *argv[]) | |
{ | |
#if 0 | |
String _include[] = {"0.0.0.0/1", "128.0.0.0/2", "192.0.0.0/3"}; | |
String _internal[] = {"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"}; | |
String _exclude[] = {"0.0.0.0/8", "10.0.0.0/8", "127.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "169.254.0.0/16", "100.64.0.0/10", "192.0.0.0/24", "192.0.2.0/24", "192.88.99.0/24", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24"}; | |
String _exclude[] = {"0.0.0.0/8", "127.0.0.0/8", "192.168.0.0/16", "169.254.0.0/16"}; | |
#endif | |
const char * _include[] = {"0.0.0.0/1", "128.0.0.0/2", "192.0.0.0/3"}; | |
const char * _exclude[] = {"0.0.0.0/8", "10.0.0.0/8", "127.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "169.254.0.0/16", "100.64.0.0/10", "192.0.0.0/24", "192.0.2.0/24", "192.88.99.0/24", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24"}; | |
char dummy[256]; | |
for (int i = 0; i < ARRAY_SIZE(_include); i++) { | |
int len = 0; | |
const char *domain = _include[i]; | |
const char *dotp = strchr(domain, '/'); | |
int prefix = atoi(dotp + 1); | |
len = dotp - domain; | |
memcpy(dummy, domain, len); | |
dummy[len] = 0; | |
int network = inet_addr(dummy); | |
// printf("%s/%d %s\n", inet_ntoa(*(in_addr *)&network), prefix, domain); | |
includeNetwork(htonl(network), prefix); | |
} | |
for (int i = 0; i < ARRAY_SIZE(_exclude); i++) { | |
int len = 0; | |
const char *domain = _exclude[i]; | |
const char *dotp = strchr(domain, '/'); | |
int prefix = atoi(dotp + 1); | |
len = dotp - domain; | |
memcpy(dummy, domain, len); | |
dummy[len] = 0; | |
int network = inet_addr(dummy); | |
// printf("%s/%d %s\n", inet_ntoa(*(in_addr *)&network), prefix, domain); | |
excludeNetwork(htonl(network), prefix); | |
} | |
char buf[256]; | |
while (fgets(buf, sizeof(buf), stdin) != NULL) { | |
int len = 0; | |
const char *domain = buf; | |
const char *dotp = strchr(domain, '/'); | |
int prefix = atoi(dotp + 1); | |
len = dotp - domain; | |
memcpy(dummy, domain, len); | |
dummy[len] = 0; | |
int network = inet_addr(dummy); | |
// printf("%s/%d %s\n", inet_ntoa(*(in_addr *)&network), prefix, domain); | |
excludeNetwork(htonl(network), prefix); | |
} | |
for (int i = 0; i < _net_count; i++) { | |
int nw = htonl(_net_list[i]); | |
printf("%s/%d\n", inet_ntoa(*(in_addr *)&nw), _net_pref[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment