Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > IP Address and Subnet Comparison in C

Reply
Thread Tools

IP Address and Subnet Comparison in C

 
 
wright.brandt@gmail.com
Guest
Posts: n/a
 
      09-18-2006
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) ){
...
...
...
}

-- http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Ancient_Hacker
Guest
Posts: n/a
 
      09-18-2006

(E-Mail Removed) 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) ){
> ...
> ...
> ...
> }
>
> -- (E-Mail 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.

 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      09-19-2006
On 18 Sep 2006 14:43:38 -0700, "Ancient_Hacker" <(E-Mail Removed)>
wrote in comp.lang.c:

>
> (E-Mail Removed) 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) ){
> > ...
> > ...
> > ...
> > }
> >
> > -- (E-Mail 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.

--
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
 
Reply With Quote
 
wright.brandt@gmail.com
Guest
Posts: n/a
 
      09-19-2006

Ancient_Hacker wrote:
> (E-Mail Removed) 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) ){
> > ...
> > ...
> > ...
> > }
> >
> > -- (E-Mail 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.

 
Reply With Quote
 
wright.brandt@gmail.com
Guest
Posts: n/a
 
      09-19-2006

Jack Klein wrote:
> On 18 Sep 2006 14:43:38 -0700, "Ancient_Hacker" <(E-Mail Removed)>
> wrote in comp.lang.c:
>
> >
> > (E-Mail Removed) 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) ){
> > > ...
> > > ...
> > > ...
> > > }
> > >
> > > -- (E-Mail 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.
>
> --
> 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?

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      09-19-2006
[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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting the subnet subnet of an IP address in C++ yagru_alvarez C++ 7 06-06-2011 09:39 AM
Cisco 1812 subnet to subnet NAT Amadej Cisco 1 09-04-2007 06:53 PM
Add a private subnet to existing real class C subnet bubbleserver@gmail.com Cisco 5 01-18-2007 04:59 PM
cannot ping from subnet A to subnet B for a specific host soup_or_power@yahoo.com Cisco 16 08-04-2006 02:30 PM
Subnet a subnet mask? Vass Computer Support 1 08-26-2005 01:02 PM



Advertisments