Given a CIDR notation, print the range of the possible IP addresses.

andrewll2

This snippet converts a CIDR notated IP address to a range of IP addresses in decimal, dotted notation.
Eg.: 192.168.1.255/31 => 192.168.1.254 - 192.168.1.255

3,139 Views
About the Author
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void) {
	unsigned int ip[4],ip_min[4],ip_max[4],netmask,tmp1,tmp2;
	char ip_string[19];
	for (;gets(ip_string) == NULL ;perror("Please enter an IP and a subnetwork mask!\nEg.:192.168.1.254/32"));
	sscanf(ip_string,"%d.%u.%u.%u/%u",&ip[0],&ip[1],&ip[2],&ip[3],&netmask);
	/*Set the bytes which won't be changed*/
	for (tmp1=tmp2=netmask/8;tmp1>0;tmp1--){
		ip_min[tmp1-1] = ip[tmp1-1];
		ip_max[tmp1-1] = ip[tmp1-1];
	}
	/*Set the bytes which should be 0ed or 255ed.*/
	for(tmp1 = tmp2,++tmp2;tmp2< 4;tmp2++){
		ip_min[tmp2]=0;
		ip_max[tmp2]=255;
	}
	/* Finally set the one which has to be shifted.*/
	if( tmp1 < 4){
		tmp2 = 8-netmask%8;
		ip_min[tmp1]=ip[tmp1]>>tmp2;
		ip_min[tmp1]<<=tmp2;
		ip_max[tmp1]=ip_min[tmp1]+pow(2,tmp2)-1;
	}
	printf("Valid addresses range from %u.%u.%u.%u to %u.%u.%u.%u\n",ip_min[0],ip_min[1],ip_min[2],ip_min[3],ip_max[0],ip_max[1],ip_max[2],ip_max[3]);
	return 0;
}
Dale_7 0 Newbie Poster

Hello, I have use of your code in a project. I want to know what license you prefer for this code. I'd like to put it in a library on GitHub.

Would you prefer to do this? I'll help.

JamesCherrill 4,334 Most Valuable Poster Moderator Featured Poster

Dani owns the copyright to everything on DaniWeb, but I'm sure she will be happy to permit you what you want for this piece of code. Use DaniWeb messaging to contact her directly.
JC

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.