IP Address and Subnet Comparison in C

Discussion in 'C Programming' started by wright.brandt@gmail.com, Sep 18, 2006.

  1. Guest

    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) ){
    ...
    ...
    ...
    }

    --
    , Sep 18, 2006
    #1
    1. Advertising

  2. 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) ){
    > ...
    > ...
    > ...
    > }
    >
    > --


    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.
    Ancient_Hacker, Sep 18, 2006
    #2
    1. Advertising

  3. Jack Klein Guest

    On 18 Sep 2006 14:43:38 -0700, "Ancient_Hacker" <>
    wrote in comp.lang.c:

    >
    > 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) ){
    > > ...
    > > ...
    > > ...
    > > }
    > >
    > > --

    >
    > 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/~ajo/docs/FAQ-acllc.html
    Jack Klein, Sep 19, 2006
    #3
  4. Guest

    Ancient_Hacker wrote:
    > 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) ){
    > > ...
    > > ...
    > > ...
    > > }
    > >
    > > --

    >
    > 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, 2006
    #4
  5. Guest

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

    > >
    > > 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/~ajo/docs/FAQ-acllc.html



    Should it have been %s? How should this have worked?
    , Sep 19, 2006
    #5
  6. Ben Pfaff Guest

    [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
    Ben Pfaff, Sep 19, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vijesh
    Replies:
    0
    Views:
    437
    Vijesh
    Nov 27, 2004
  2. Glenn M

    Get Requesting Clients Subnet Mask

    Glenn M, Oct 12, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    430
    John Saunders
    Oct 12, 2004
  3. Sascha Teifke
    Replies:
    2
    Views:
    2,794
    Sascha Teifke
    Jun 28, 2003
  4. yagru_alvarez
    Replies:
    7
    Views:
    625
    Michael Doubez
    Jun 6, 2011
  5. kielhd
    Replies:
    3
    Views:
    341
    John W. Krahn
    Nov 9, 2003
Loading...

Share This Page