[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

ip validity - source code

I want a C/linux(unix) source code to check the validity of an ip address for any device.
0
nglvinod
Asked:
nglvinod
1 Solution
 
BlackDiamondCommented:
What are your validation rules?  Do you just want to look at a string and know if it is an ip?

The inet functions are very useful for manipulating ip addresses, and will make this job fairly simple. Take a look at following functions:

inet_aton
inet_ntoa
inet_addr
inet_ntof

If you give a little more information as to what you want, I can provide you with some sample code to get you started.
0
 
nglvinodAuthor Commented:
I enter an/range_of ip address as a string. i just want to know whether the entered ip address(es) is a valid ip address; for a device in particular say a router, switch, hub, bridge, etc....eventhough 255.255.255.255 is a valid ip, it cannot be an valid ip address for a device...

i want a source code for this in C.
0
 
BlackDiamondCommented:
Hmmm.  That would seem to apply to all broadcast and network addresses as well.  I don't see an easy way without (at a minimum) seeding the network addresses in the program (or getting them from a config/database/whatever :->).

To validate if a string is actually an ip address is simple.  just do the following...

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

void main()
{
   struct in_addr theAddrStruct;
   char theString[16];

   strncpy(theString, "12.13.14.15", 16);

   if (inet_aton(theString, &theAddrStruct))
      printf("The address %s is valid\n", inet_ntoa(theAddrStruct));
   else
      printf("The value %s is not a valid ip\n", theString);


}

This is just the first validation however.  If it is ok to have a config with all of the network ip's and masks, I can whip off a couple of functions that will do the validation based on that info( i.e not network, not broadcast, and in your specified network range).  Let me know if that is close enough to what you are looking for.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BlackDiamondCommented:
oops, you'll also need to "#include <string.h>"
0
 
nglvinodAuthor Commented:
yes i am looking for the same - not network, not broadcast and in the network range.
0
 
BlackDiamondCommented:
ok.  Here is a test program that implements all of the logic you will need.  I'll let you put all the nice wrappers on it, but this should be enough to get you started. Cheers, BD.

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>


struct networkinfo {
   struct in_addr network;
   struct in_addr mask;
};

void checkifvalid(char * ipaddress, struct networkinfo * mynetworks, long numberofnetworks) {
   struct in_addr s_ipaddress;
   long count;
   unsigned long networknum, broadcastnum;

   /* Convert the ip string to an in_addr struct */
   if (!inet_aton(ipaddress, &s_ipaddress)) {
      printf("The value %s is not a valid ip string\n", ipaddress);
      return;
   }
   /* Check all of your defined networks to see if the ip is valid */
   for(count = 0; count < numberofnetworks; count++) {
      /* Get the network number as a ulong for comparison */
      networknum = ntohl(mynetworks[count].network.s_addr) & ntohl(mynetworks[count].mask.s_addr);
      /* Get the broadcast number as a ulong for comparison */
      broadcastnum = networknum + ~(ntohl(mynetworks[count].mask.s_addr));
     
      /* Check if the address is in this network range */
      if ((ntohl(s_ipaddress.s_addr) > networknum) &&
          (ntohl(s_ipaddress.s_addr) < broadcastnum)) {
          printf("%s is a valid ip address\n", ipaddress);
          return;
      }

      /* Check if the address is the network or broadcast address */
      if ((ntohl(s_ipaddress.s_addr) == networknum) ||
          (ntohl(s_ipaddress.s_addr) == broadcastnum)) {
         printf("%s is a network or broadcast ip\n", ipaddress);
         return;
      }
   }
   
   /* If it got this far, it was not within any network range */
   printf("%s is not in your network range\n", ipaddress);
}

int main(int argc, char * argv[]) {
   struct networkinfo * mynetworks;
   long numberofnetworks;

   /* You would probably read in a config file
       and populate and resize "mynetworks" as
       appropriate */

   /* Allocate space for 2 networks */
   numberofnetworks = 2;
   mynetworks = malloc(sizeof(mynetworks)*numberofnetworks);
   /* Create a couple of networks */
   inet_aton("22.33.22.0", &mynetworks[0].network);
   inet_aton("255.255.255.0", &mynetworks[0].mask);
   inet_aton("33.33.33.0", &mynetworks[1].network);
   inet_aton("255.255.255.224", &mynetworks[1].mask);

   checkifvalid("1.2.3.4", mynetworks, numberofnetworks);
   checkifvalid("22.33.22.0", mynetworks, numberofnetworks);
   checkifvalid("22.33.22.10", mynetworks, numberofnetworks);
   checkifvalid("22.33.22.255", mynetworks, numberofnetworks);
   checkifvalid("22.33.23.0", mynetworks, numberofnetworks);
}
0
 
BlackDiamondCommented:
oh, and I forgot one line at the end of main()...  hehe

free(mynetworks);
0
 
BlackDiamondCommented:
sheesh....  Sorry, one more error.  the line:

mynetworks = malloc(sizeof(mynetworks)*numberofnetworks);

should read:
mynetworks = malloc(sizeof(struct networkinfo)*numberofnetworks);
0
 
BlackDiamondCommented:
nglvinod,
Did this help?
0
 
CleanupPingCommented:
nglvinod:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
troopernCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Accept BlackDiamond's comment as answer.
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

troopern
EE Cleanup Volunteer
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now