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;
}
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;
}