mirror of
https://github.com/bol-van/zapret.git
synced 2025-04-21 22:42:57 +03:00
ip2net internal unique + ipv4/bitcount passthrough
This commit is contained in:
parent
366b065641
commit
a412fa24d5
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,5 +1,7 @@
|
|||||||
// group ip list from stdout into subnets
|
// group ipv4 list from stdout into subnets
|
||||||
// ip list must be pre-uniqued
|
// each line must contain either ipv4 or ipv4/bitcount
|
||||||
|
// valid ipv4/bitcount are passed through without modification
|
||||||
|
// ipv4 are groupped into subnets
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -29,15 +31,40 @@ uint mask_from_bitcount(uint zct)
|
|||||||
return ~((1<<zct)-1);
|
return ~((1<<zct)-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// make presorted array unique. return number of unique items.
|
||||||
|
// 1,1,2,3,3,0,0,0 (ct=8) => 1,2,3,0 (ct=4)
|
||||||
|
uint unique_uint(uint *pu,uint ct)
|
||||||
|
{
|
||||||
|
uint i,j,u;
|
||||||
|
for(i=j=0 ; j<ct ; i++)
|
||||||
|
{
|
||||||
|
u = pu[j++];
|
||||||
|
for(; j<ct && pu[j]==u ; j++);
|
||||||
|
pu[i] = u;
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
uint u1,u2,u3,u4,ip;
|
uint u1,u2,u3,u4,ip;
|
||||||
uint ipct=0,iplist_size=0,*iplist=NULL,*iplist_new;
|
uint ipct=0,iplist_size=0,*iplist=NULL,*iplist_new;
|
||||||
uint pos=0,p;
|
uint pos=0,p;
|
||||||
uint i,zct,subnet_ct,end_ip;
|
uint i,zct,subnet_ct,end_ip;
|
||||||
|
char str[256];
|
||||||
|
|
||||||
while (!feof(stdin))
|
while (fgets(str,sizeof(str),stdin))
|
||||||
if (scanf("%u.%u.%u.%u",&u1,&u2,&u3,&u4)==4 && !(u1 & 0xFFFFFF00) && !(u2 & 0xFFFFFF00) && !(u3 & 0xFFFFFF00) && !(u4 & 0xFFFFFF00))
|
{
|
||||||
|
if ((i=sscanf(str,"%u.%u.%u.%u/%u",&u1,&u2,&u3,&u4,&zct))>=4 && !(u1 & 0xFFFFFF00) && !(u2 & 0xFFFFFF00) && !(u3 & 0xFFFFFF00) && !(u4 & 0xFFFFFF00))
|
||||||
|
{
|
||||||
|
if (i==5 && zct!=32)
|
||||||
|
{
|
||||||
|
// we have subnet x.x.x.x/y
|
||||||
|
// output it as is if valid, ignore otherwise
|
||||||
|
if (zct<32)
|
||||||
|
printf("%u.%u.%u.%u/%u\n",u1,u2,u3,u4,zct);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
ip = u1<<24 | u2<<16 | u3<<8 | u4;
|
ip = u1<<24 | u2<<16 | u3<<8 | u4;
|
||||||
if (ipct>=iplist_size)
|
if (ipct>=iplist_size)
|
||||||
@ -54,14 +81,16 @@ int main()
|
|||||||
}
|
}
|
||||||
iplist[ipct++]= ip;
|
iplist[ipct++]= ip;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
gnu_quicksort(iplist,ipct,sizeof(*iplist),ucmp,NULL);
|
gnu_quicksort(iplist,ipct,sizeof(*iplist),ucmp,NULL);
|
||||||
|
ipct = unique_uint(iplist,ipct);
|
||||||
|
|
||||||
while(pos<ipct)
|
while(pos<ipct)
|
||||||
{
|
{
|
||||||
uchar subnet_ok=0;
|
uchar subnet_ok=0;
|
||||||
uint mask,ip_start,ip_end,ip_ct,subnet_ct,pos_end;
|
uint mask,ip_start,ip_end,ip_ct,subnet_ct,pos_end;
|
||||||
|
|
||||||
for(zct=10, pos_end=pos+1 ; zct>=2 ; zct--)
|
for(zct=10, pos_end=pos+1 ; zct>=2 ; zct--)
|
||||||
{
|
{
|
||||||
mask = mask_from_bitcount(zct);
|
mask = mask_from_bitcount(zct);
|
||||||
|
@ -23,9 +23,9 @@ do
|
|||||||
if [ -x $IP2NET ]; then
|
if [ -x $IP2NET ]; then
|
||||||
echo Adding to ipset $2 \($IPSTYPE , ip2net\) : $f
|
echo Adding to ipset $2 \($IPSTYPE , ip2net\) : $f
|
||||||
if [ -f "$ZIPLIST_EXCLUDE" ] ; then
|
if [ -f "$ZIPLIST_EXCLUDE" ] ; then
|
||||||
grep -vxFf $ZIPLIST_EXCLUDE "$f" | sort -u | $IP2NET | sed -nre "s/^.+$/add $2 &/p" | ipset -! restore
|
grep -vxFf $ZIPLIST_EXCLUDE "$f" | $IP2NET | sed -nre "s/^.+$/add $2 &/p" | ipset -! restore
|
||||||
else
|
else
|
||||||
sort -u "$f" | $IP2NET | sed -nre "s/^.+$/add $2 &/p" | ipset -! restore
|
$IP2NET <"$f" | sed -nre "s/^.+$/add $2 &/p" | ipset -! restore
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
echo Adding to ipset $2 \($IPSTYPE\) : $f
|
echo Adding to ipset $2 \($IPSTYPE\) : $f
|
||||||
|
Loading…
x
Reference in New Issue
Block a user