IP address and matching it to a network/netmask

J

john

I want to check an IP address against a given network/netmask, but I am
not sure how to do this.

For example:

ip=192.168.1.5 nw/nm=192.168.1.0/24 = match
ip=192.168.1.5 nw/nm=192.168.2.0/24 = no match
ip=192.168.1.5 nw/nm=192.168.0.0/16 = match
ip=10.120.12.5 nw/nm=192.168.2.0/24 = no match
ip=10.120.12.5 nw/nm=10.120.12.5/32 = match

This is what I have so far, but I am now stuck. I think I am missing
one small piece of the puzzle. Where you see inet_addr(), I also tried
inet_aton() (and using s_addr), but this did not work either. On the
final step, I am not sure if I need to AND or need to OR, but neither
seem to work as I expected.

Any help would be greatly appreciated.

Thanks,
-John

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

void
Usage
(char *pgm) {
fprintf( stderr, "\n" );
fprintf( stderr, "Usage : %s [ ip ] [ network/netmask ]\n",
pgm );
fprintf( stderr, "Exmaple: %s 192.168.1.9 192.168.1.0/24\n",
pgm );
fprintf( stderr, "\n" );
}

int
main
(int argc, char *argv[]) {
char *s_ip, *s_nwnm, *s_nw, *s_nm;
char *ptr;
unsigned int i, nm;
unsigned int mask;
in_addr_t ip2, nw2;

if ( 3 != argc ) {
Usage(argv[0]);
return 1;
}

s_ip = argv[1];
s_nwnm = argv[2];
ptr = index(s_nwnm,(int) '/');
if ( NULL != ptr ) {
s_nm = ptr+1;
i = ptr - argv[2];

s_nw = s_nwnm;
s_nw = '\0';
}

printf("\n");
printf("ip: _%s_ nw: _%s_ nm: _%s_\n\n", s_ip, s_nw, s_nm);

ip2 = inet_addr(s_ip);
nw2 = inet_addr(s_nw);
printf("ip2: %u\n", ip2);
printf("nw2: %u\n", nw2);

nm = 32 - atoi(s_nm);
mask = -1;
mask <<= nm;
printf("nm: %u\n\n", mask);

printf(" or: %u :: %u == %u\n", (ip2|mask), (nw2|mask),
(ip2|mask)==(nw2|mask) );

printf("and: %u :: %u == %u\n", (ip2&mask), (nw2&mask),
(ip2&mask)==(nw2&mask) );

printf("\n");
return 0;
}
 
W

Walter Roberson

I want to check an IP address against a given network/netmask, but I am
not sure how to do this.
For example:
ip=192.168.1.5 nw/nm=192.168.1.0/24 = match
ip=192.168.1.5 nw/nm=192.168.2.0/24 = no match

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

Those last three headers are not part of standard C, so your
program is non-portable, and any part of your program that uses
those functions is considered Off Topic in this newsgroup.
Try comp.unix.programmer .

int
main
(int argc, char *argv[]) {
unsigned int mask;
nm = 32 - atoi(s_nm);
mask = -1;
mask <<= nm;
printf("nm: %u\n\n", mask);

If your unsigned int is only 16 bits, then that calculation of
the mask would not work. You should use unsigned long for these
calculations.
printf(" or: %u :: %u == %u\n", (ip2|mask), (nw2|mask),
(ip2|mask)==(nw2|mask) );

That would tell you whether they have the same broadcast address.
printf("and: %u :: %u == %u\n", (ip2&mask), (nw2&mask),
(ip2&mask)==(nw2&mask) );

And that would tell you whether they have the same base address.

In the case of testing to see whether an IP address fits a given
network and range, testing either one of these would work. In
the more general case where the IP address itself is accompanied
by a netmask, the tests get a little more complicated, particularily
if you are attempting to determine whether one range fully encloses
the other or if there is some overlap or no overlap.

When you switch to long, be sure to convert your %u formats to %lu .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top