IP Address and Subnet Comparison in C

W

wright.brandt

I am new to programming in C. If my question is not appropriate for
this newsgroup I apologize. I Have been tasked with writing a small
application to do some back end processing in C and have had little
trouble getting most of the app written. What seems to be holding me up
is a tricky (at least for me) piece of network address logic. I need to
be able to take an ip address and compare it to a subnet represented
in the doted and slashed notation ( 192.168.0.0/24 ) and determine if
that ip address is within the subnet. I have played around with some of
the functions defined in netinet/in.b and arpa/inet.h but have not been
able to get anything to work. The code below (kind of) demonstrates
what I need to do but not how to do it (ofcourse, or I wouldnt need
help right ;0 ). I have to believe that this would be a no-brainer for
someone who is familiar with C and basic networking. I am sure there is
a built in function or common snippet that will do it. I just do not
know about it. Any help anyone could give me would be greatly
appreciated! Thanks in advance.

char * sAddress = "192.168.0.1";
char * sSubnet = "192.168.0.0/24";

if ( address_is_in_subnet(sAddress, sSubnet) ){
...
...
...
}

-- (e-mail address removed)
 
A

Ancient_Hacker

I am new to programming in C. If my question is not appropriate for
this newsgroup I apologize. I Have been tasked with writing a small
application to do some back end processing in C and have had little
trouble getting most of the app written. What seems to be holding me up
is a tricky (at least for me) piece of network address logic. I need to
be able to take an ip address and compare it to a subnet represented
in the doted and slashed notation ( 192.168.0.0/24 ) and determine if
that ip address is within the subnet. I have played around with some of
the functions defined in netinet/in.b and arpa/inet.h but have not been
able to get anything to work. The code below (kind of) demonstrates
what I need to do but not how to do it (ofcourse, or I wouldnt need
help right ;0 ). I have to believe that this would be a no-brainer for
someone who is familiar with C and basic networking. I am sure there is
a built in function or common snippet that will do it. I just do not
know about it. Any help anyone could give me would be greatly
appreciated! Thanks in advance.

char * sAddress = "192.168.0.1";
char * sSubnet = "192.168.0.0/24";

if ( address_is_in_subnet(sAddress, sSubnet) ){
...
...
...
}

-- (e-mail address removed)

This isnt strictly a C question, but close enuf I guess.

I would first split the strings into integers, with something like:

unsigned char b[4];

sscanf( sAddress, "%d.%d.%d.%d", &b[0],&b[1,&b[2],&b[3] );

do the same with the netmask.
sscanf( sAddress, "%d.%d.%d.%d/%d", &m[0],&m[1,&m[2],&m[3], &Bits );

Then take the Bits part and build a mask from it:

unsigned long int Tot = 0;
for( i = 1; i <= Bits; i++ ) Tot = (Tot << 1) | 1;
for(; i <= 32; i++ ) Tot = Tot << 1);

Now you can mask off the candidate ip address and compare it. Better
write a few functions for this:

void MaskEm( ), int IpEqual(), et al...


Hope this gets you started.

BTW there are probably dozens of these routines already written, try
googling for "c code to compare subnets" or somesuch.
 
J

Jack Klein

I am new to programming in C. If my question is not appropriate for
this newsgroup I apologize. I Have been tasked with writing a small
application to do some back end processing in C and have had little
trouble getting most of the app written. What seems to be holding me up
is a tricky (at least for me) piece of network address logic. I need to
be able to take an ip address and compare it to a subnet represented
in the doted and slashed notation ( 192.168.0.0/24 ) and determine if
that ip address is within the subnet. I have played around with some of
the functions defined in netinet/in.b and arpa/inet.h but have not been
able to get anything to work. The code below (kind of) demonstrates
what I need to do but not how to do it (ofcourse, or I wouldnt need
help right ;0 ). I have to believe that this would be a no-brainer for
someone who is familiar with C and basic networking. I am sure there is
a built in function or common snippet that will do it. I just do not
know about it. Any help anyone could give me would be greatly
appreciated! Thanks in advance.

char * sAddress = "192.168.0.1";
char * sSubnet = "192.168.0.0/24";

if ( address_is_in_subnet(sAddress, sSubnet) ){
...
...
...
}

-- (e-mail address removed)

This isnt strictly a C question, but close enuf I guess.

I would first split the strings into integers, with something like:

unsigned char b[4];

sscanf( sAddress, "%d.%d.%d.%d", &b[0],&b[1,&b[2],&b[3] );

Surely you didn't really mean to pass one of the *scanf() families the
addresses of unsigned chars matched with a "%d" conversion specifier.
 
W

wright.brandt

Ancient_Hacker said:
I am new to programming in C. If my question is not appropriate for
this newsgroup I apologize. I Have been tasked with writing a small
application to do some back end processing in C and have had little
trouble getting most of the app written. What seems to be holding me up
is a tricky (at least for me) piece of network address logic. I need to
be able to take an ip address and compare it to a subnet represented
in the doted and slashed notation ( 192.168.0.0/24 ) and determine if
that ip address is within the subnet. I have played around with some of
the functions defined in netinet/in.b and arpa/inet.h but have not been
able to get anything to work. The code below (kind of) demonstrates
what I need to do but not how to do it (ofcourse, or I wouldnt need
help right ;0 ). I have to believe that this would be a no-brainer for
someone who is familiar with C and basic networking. I am sure there is
a built in function or common snippet that will do it. I just do not
know about it. Any help anyone could give me would be greatly
appreciated! Thanks in advance.

char * sAddress = "192.168.0.1";
char * sSubnet = "192.168.0.0/24";

if ( address_is_in_subnet(sAddress, sSubnet) ){
...
...
...
}

-- (e-mail address removed)

This isnt strictly a C question, but close enuf I guess.

I would first split the strings into integers, with something like:

unsigned char b[4];

sscanf( sAddress, "%d.%d.%d.%d", &b[0],&b[1,&b[2],&b[3] );

do the same with the netmask.
sscanf( sAddress, "%d.%d.%d.%d/%d", &m[0],&m[1,&m[2],&m[3], &Bits );

Then take the Bits part and build a mask from it:

unsigned long int Tot = 0;
for( i = 1; i <= Bits; i++ ) Tot = (Tot << 1) | 1;
for(; i <= 32; i++ ) Tot = Tot << 1);

Now you can mask off the candidate ip address and compare it. Better
write a few functions for this:

void MaskEm( ), int IpEqual(), et al...


Hope this gets you started.

BTW there are probably dozens of these routines already written, try
googling for "c code to compare subnets" or somesuch.


Thanks very much for your help. I appreciate it. How would I go about
masking off the candidate ip address and compare? I know this may seem
like a silly question but as I mentioned above I am new to C
programming.
 
W

wright.brandt

Jack said:
I am new to programming in C. If my question is not appropriate for
this newsgroup I apologize. I Have been tasked with writing a small
application to do some back end processing in C and have had little
trouble getting most of the app written. What seems to be holding me up
is a tricky (at least for me) piece of network address logic. I need to
be able to take an ip address and compare it to a subnet represented
in the doted and slashed notation ( 192.168.0.0/24 ) and determine if
that ip address is within the subnet. I have played around with some of
the functions defined in netinet/in.b and arpa/inet.h but have not been
able to get anything to work. The code below (kind of) demonstrates
what I need to do but not how to do it (ofcourse, or I wouldnt need
help right ;0 ). I have to believe that this would be a no-brainer for
someone who is familiar with C and basic networking. I am sure there is
a built in function or common snippet that will do it. I just do not
know about it. Any help anyone could give me would be greatly
appreciated! Thanks in advance.

char * sAddress = "192.168.0.1";
char * sSubnet = "192.168.0.0/24";

if ( address_is_in_subnet(sAddress, sSubnet) ){
...
...
...
}

-- (e-mail address removed)

This isnt strictly a C question, but close enuf I guess.

I would first split the strings into integers, with something like:

unsigned char b[4];

sscanf( sAddress, "%d.%d.%d.%d", &b[0],&b[1,&b[2],&b[3] );

Surely you didn't really mean to pass one of the *scanf() families the
addresses of unsigned chars matched with a "%d" conversion specifier.


Should it have been %s? How should this have worked?
 
B

Ben Pfaff

[I was baffled by the attributions, so I dropped them.]
unsigned char b[4];

sscanf( sAddress, "%d.%d.%d.%d", &b[0],&b[1,&b[2],&b[3] );

Surely you didn't really mean to pass one of the *scanf() families the
addresses of unsigned chars matched with a "%d" conversion specifier.
Should it have been %s? How should this have worked?

%hhu is the proper format specifier for unsigned char data, but
it is only available in C99.
 

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,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top