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.
105
ip2net/ip2net.c
105
ip2net/ip2net.c
@ -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,41 +31,68 @@ 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))
|
||||||
{
|
{
|
||||||
ip = u1<<24 | u2<<16 | u3<<8 | u4;
|
if (i==5 && zct!=32)
|
||||||
if (ipct>=iplist_size)
|
|
||||||
{
|
{
|
||||||
iplist_size += ALLOC_STEP;
|
// we have subnet x.x.x.x/y
|
||||||
iplist_new = (uint*)(iplist ? realloc(iplist,sizeof(*iplist)*iplist_size) : malloc(sizeof(*iplist)*iplist_size));
|
// output it as is if valid, ignore otherwise
|
||||||
if (!iplist_new)
|
if (zct<32)
|
||||||
{
|
printf("%u.%u.%u.%u/%u\n",u1,u2,u3,u4,zct);
|
||||||
free(iplist);
|
|
||||||
fprintf(stderr,"out of memory\n");
|
|
||||||
return 100;
|
|
||||||
}
|
|
||||||
iplist = iplist_new;
|
|
||||||
}
|
}
|
||||||
iplist[ipct++]= ip;
|
else
|
||||||
}
|
|
||||||
|
|
||||||
gnu_quicksort(iplist,ipct,sizeof(*iplist),ucmp,NULL);
|
|
||||||
|
|
||||||
while(pos<ipct)
|
|
||||||
{
|
|
||||||
uchar subnet_ok=0;
|
|
||||||
uint mask,ip_start,ip_end,ip_ct,subnet_ct,pos_end;
|
|
||||||
|
|
||||||
for(zct=10, pos_end=pos+1 ; zct>=2 ; zct--)
|
|
||||||
{
|
{
|
||||||
|
ip = u1<<24 | u2<<16 | u3<<8 | u4;
|
||||||
|
if (ipct>=iplist_size)
|
||||||
|
{
|
||||||
|
iplist_size += ALLOC_STEP;
|
||||||
|
iplist_new = (uint*)(iplist ? realloc(iplist,sizeof(*iplist)*iplist_size) : malloc(sizeof(*iplist)*iplist_size));
|
||||||
|
if (!iplist_new)
|
||||||
|
{
|
||||||
|
free(iplist);
|
||||||
|
fprintf(stderr,"out of memory\n");
|
||||||
|
return 100;
|
||||||
|
}
|
||||||
|
iplist = iplist_new;
|
||||||
|
}
|
||||||
|
iplist[ipct++]= ip;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gnu_quicksort(iplist,ipct,sizeof(*iplist),ucmp,NULL);
|
||||||
|
ipct = unique_uint(iplist,ipct);
|
||||||
|
|
||||||
|
while(pos<ipct)
|
||||||
|
{
|
||||||
|
uchar subnet_ok=0;
|
||||||
|
uint mask,ip_start,ip_end,ip_ct,subnet_ct,pos_end;
|
||||||
|
for(zct=10, pos_end=pos+1 ; zct>=2 ; zct--)
|
||||||
|
{
|
||||||
mask = mask_from_bitcount(zct);
|
mask = mask_from_bitcount(zct);
|
||||||
ip_start = iplist[pos] & mask;
|
ip_start = iplist[pos] & mask;
|
||||||
subnet_ct = ~mask+1;
|
subnet_ct = ~mask+1;
|
||||||
@ -76,21 +105,21 @@ int main()
|
|||||||
pos_end = p;
|
pos_end = p;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!subnet_ok) zct=0,ip_start=iplist[pos];
|
if (!subnet_ok) zct=0,ip_start=iplist[pos];
|
||||||
|
|
||||||
u1 = ip_start>>24;
|
u1 = ip_start>>24;
|
||||||
u2 = (ip_start>>16) & 0xFF;
|
u2 = (ip_start>>16) & 0xFF;
|
||||||
u3 = (ip_start>>8) & 0xFF;
|
u3 = (ip_start>>8) & 0xFF;
|
||||||
u4 = ip_start & 0xFF;
|
u4 = ip_start & 0xFF;
|
||||||
if (zct)
|
if (zct)
|
||||||
printf("%u.%u.%u.%u/%u\n",u1,u2,u3,u4,32-zct);
|
printf("%u.%u.%u.%u/%u\n",u1,u2,u3,u4,32-zct);
|
||||||
else
|
else
|
||||||
printf("%u.%u.%u.%u\n",u1,u2,u3,u4);
|
printf("%u.%u.%u.%u\n",u1,u2,u3,u4);
|
||||||
|
|
||||||
pos = pos_end;
|
pos = pos_end;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(iplist);
|
free(iplist);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -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