From 6af32e50e9e00989d4f9dbd7a4069540576952c6 Mon Sep 17 00:00:00 2001 From: epochqwert Date: Wed, 6 May 2015 00:44:11 -0500 Subject: added ipgen and cidr programs. They can be used together to spit out all IPs belonging to a CIDR style network or you can use just ipgen to use a netmask. even works with weird netmasks. --- src/bin/cidr.c | 25 +++++++++++++++++++++++++ src/bin/ipgen.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/bin/cidr.c create mode 100644 src/bin/ipgen.c (limited to 'src/bin') diff --git a/src/bin/cidr.c b/src/bin/cidr.c new file mode 100644 index 0000000..fe44668 --- /dev/null +++ b/src/bin/cidr.c @@ -0,0 +1,25 @@ +#include + +int main(int argc,char *argv[]) { + unsigned int cidr; + unsigned int net; + unsigned int mask; + if(argc < 2) { + printf("usage: cidr address/cidr\n"); + printf("example: cidr 192.168.0.1/24\n"); + return 1; + } + if(!strchr(argv[1],'/')) { + cidr=32; + net=argv[1]; + } else { + cidr=atoi(strchr(argv[1],'/')+1); + *strchr(argv[1],'/')=0; + net=htonl(inet_addr(argv[1])); + } + printf("%d.%d.%d.%d ",net>>24&255,net>>16&255,net>>8&255,net&255); + mask=-1; + mask>>=(32-cidr); + mask<<=(32-cidr); + printf("%d.%d.%d.%d\n",mask>>24&255,mask>>16&255,mask>>8&255,mask&255); +} diff --git a/src/bin/ipgen.c b/src/bin/ipgen.c new file mode 100644 index 0000000..7aae99e --- /dev/null +++ b/src/bin/ipgen.c @@ -0,0 +1,43 @@ +#include + +void print_ip(unsigned int i) { + printf("%d.%d.%d.%d\n",(i>>24)&255,(i>>16)&255,(i>>8)&255,i&255); +} + +int main(int argc,char *argv[]) { + if(argc < 3) { + printf("usage: ipgen net mask\n"); + printf("example: ipgen 192.168.0.1 255.255.0.255\n"); + return 1; + } + unsigned int net=htonl(inet_addr(argv[1])); + unsigned int mask=htonl(inet_addr(argv[2])); + unsigned int i; + unsigned int min; + unsigned int max; + unsigned int inc=1; + if(!(~mask)) {//nothing to do. this is a /32 + print_ip(net); + return 0; + } + for(i=0;(mask>>i&1);inc<<=1,i++); + min=net&mask; + max=net&mask|(~mask); + if(argc > 3) { + printf("net: "); + print_ip(net); + printf("mask: "); + print_ip(mask); + printf("min: "); + print_ip(min); + printf("max: "); + print_ip(max); + printf("inc: %d\n",inc); + } + for(i=min;i<=max;i+=inc) { + if((i&mask) == (net&mask)) { + print_ip(i); + } + } + return 0; +} -- cgit v1.2.3