#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

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

static char const rcsid[] = "$Id: rv_iptoint.c,v 1.2 2004/03/10 18:04:33 wilhelm Exp $";

unsigned int rv_iptoint (char * ipstring) {
/*       ==============

   Converts an IP number (e.g. 1.2.3.4) into an 32 unsigned int ("0x01020304").

   Input : char * ipstring    String containing the ip number 
   Output: unsigned int       IP number as a 32 bit unsigned int.

   Non-numbers in the input string will be converted to FF.
  

   $Author: wilhelm $
   $Date: 2004/03/10 18:04:33 $
   $Revision: 1.2 $

*/

   unsigned int temp;
   char buffer[10];
   char *point= &buffer[0];
   int i;

   point = strtok ( ipstring, "." );
   if (point == NULL) { return 0; }

   if (strcmp(point, "??") == 0) {
      temp = 0xFF;
   }
   else {
      temp = (int) strtol (point, (char**)NULL, 10);
   }

   for (i=0 ; i<3 ; i++) {
       point = strtok ( NULL, "." );
       if ( (point == NULL) ) {
	  return 0;
       }
       else if ((strcmp(point, "??") == 0) ) {
          temp = temp*0x100 + 0xFF;
       }
       else {
          temp = temp*0x100 + (int) strtol (point, (char**)NULL, 10);
       }
   }
   
   return temp;
}
