Solved

ip validity - source code

Posted on 2001-07-17
12
321 Views
Last Modified: 2008-02-01
I want a C/linux(unix) source code to check the validity of an ip address for any device.
0
Comment
Question by:nglvinod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6291145
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
 

Author Comment

by:nglvinod
ID: 6292364
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
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6292470
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
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6292491
oops, you'll also need to "#include <string.h>"
0
 

Author Comment

by:nglvinod
ID: 6292605
yes i am looking for the same - not network, not broadcast and in the network range.
0
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6296015
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
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6296039
oh, and I forgot one line at the end of main()...  hehe

free(mynetworks);
0
 
LVL 5

Accepted Solution

by:
BlackDiamond earned 50 total points
ID: 6296042
sheesh....  Sorry, one more error.  the line:

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

should read:
mynetworks = malloc(sizeof(struct networkinfo)*numberofnetworks);
0
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6360761
nglvinod,
Did this help?
0
 

Expert Comment

by:CleanupPing
ID: 9078741
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
 
LVL 7

Expert Comment

by:troopern
ID: 9978217
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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have seen several blogs and forum entries elsewhere state that because NTFS volumes do not support linux ownership or permissions, they cannot be used for anonymous ftp upload through the vsftpd program.   IT can be done and here's how to get i…
Note: for this to work properly you need to use a Cross-Over network cable. 1. Connect both servers S1 and S2 on the second network slots respectively. Note that you can use the 1st slots but usually these would be occupied by the Service Provide…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question