By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,109 Members | 932 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,109 IT Pros & Developers. It's quick & easy.

IP Address and Subnet Comparison in C

P: n/a
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) ){
...
...
...
}

-- br***********@yahoo.com

Sep 18 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

wr***********@gmail.com wrote:
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) ){
...
...
...
}

-- br***********@yahoo.com
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.

Sep 18 '06 #2

P: n/a
On 18 Sep 2006 14:43:38 -0700, "Ancient_Hacker" <gr**@comcast.net>
wrote in comp.lang.c:
>
wr***********@gmail.com wrote:
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) ){
...
...
...
}

-- br***********@yahoo.com

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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Sep 19 '06 #3

P: n/a

Ancient_Hacker wrote:
wr***********@gmail.com wrote:
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) ){
...
...
...
}

-- br***********@yahoo.com

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.

Sep 19 '06 #4

P: n/a

Jack Klein wrote:
On 18 Sep 2006 14:43:38 -0700, "Ancient_Hacker" <gr**@comcast.net>
wrote in comp.lang.c:

wr***********@gmail.com wrote:
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) ){
...
...
...
}
>
-- br***********@yahoo.com
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

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

Sep 19 '06 #5

P: n/a
[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.
--
"If I've told you once, I've told you LLONG_MAX times not to
exaggerate."
--Jack Klein
Sep 19 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.