Solved

ip validity - source code

Posted on 2001-07-17
12
298 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
12 Comments
 
LVL 5

Expert Comment

by:BlackDiamond
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 5

Expert Comment

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

Author Comment

by:nglvinod
Comment Utility
yes i am looking for the same - not network, not broadcast and in the network range.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 5

Expert Comment

by:BlackDiamond
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
nglvinod,
Did this help?
0
 

Expert Comment

by:CleanupPing
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now