summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorepochqwert <epoch@hacking.allowed.org>2015-05-06 00:44:11 -0500
committerepochqwert <epoch@hacking.allowed.org>2015-05-06 00:44:11 -0500
commit6af32e50e9e00989d4f9dbd7a4069540576952c6 (patch)
tree44c8177d3f3630ff6c8306c50488d9b145c8c4ed /src
parent2b4697d313c0dfae862ee42d4bbc608e50c5eb22 (diff)
downloadmisc-6af32e50e9e00989d4f9dbd7a4069540576952c6.tar.gz
misc-6af32e50e9e00989d4f9dbd7a4069540576952c6.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/bin/cidr.c25
-rw-r--r--src/bin/ipgen.c43
2 files changed, 68 insertions, 0 deletions
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 <stdio.h>
+
+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 <stdio.h>
+
+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;
+}