View Full Version : [Solved] Compare IP addresses
March 25th, 2011, 16:14
I want to compare two IP addresses, so that I may compare which is more/less "specific" or "restricted" than the other.
So is there any function/library that may help in doing this comparison in C?
March 25th, 2011, 16:42
Don't know how IP #1 may be more specific than IP#2...
Are you talking about comparing subnets? If yes, you can compare CIDR for them since /32 is more specific than /24.
March 26th, 2011, 02:18
I'm not sure if you wan't to compare two IP addresses. If yes, they are just numbers; you can compare them as such.
Somehow I've got a feeling you want to compare routes to a specific destination or subnets.
Can you give us more details what are you trying to achieve?
As Alt suggested you can compare MASKs (or CIDRs depending what you are working with).
It can get interesting if you are trying to program auto-summary of a routing protocols and you are trying to find a supernet.
March 27th, 2011, 12:52
int inaddr_cmp( const char * a, const char * b, const char * netmask, size_t nmemb )
for( i=0; i!=nmemb; ++i )
if( a[i] & netmask[i] > b[i] & netmask[i] )
if( a[i] & netmask[i] < b[i] & netmask[i] )
if it's just normal host addresses (as opposed to net addresses) just use C's `!=` or `==` operators on uint32_t for ipv4 and uint128_t for ipv6 (if availible)
March 28th, 2011, 13:25
Thank you guys, you have been very helpful.
vBulletin® v3.8.7, Copyright ©2000-2013, vBulletin Solutions, Inc.