Home | History | Annotate | Download | only in core
      1 /**************************************************************************
      2 MISC Support Routines
      3 **************************************************************************/
      4 
      5 FILE_LICENCE ( GPL2_OR_LATER );
      6 
      7 #include <stdlib.h>
      8 #include <ctype.h>
      9 #include <byteswap.h>
     10 #include <gpxe/in.h>
     11 #include <gpxe/timer.h>
     12 
     13 /**************************************************************************
     14 INET_ATON - Convert an ascii x.x.x.x to binary form
     15 **************************************************************************/
     16 int inet_aton ( const char *cp, struct in_addr *inp ) {
     17 	const char *p = cp;
     18 	const char *digits_start;
     19 	unsigned long ip = 0;
     20 	unsigned long val;
     21 	int j;
     22 	for(j = 0; j <= 3; j++) {
     23 		digits_start = p;
     24 		val = strtoul(p, ( char ** ) &p, 10);
     25 		if ((p == digits_start) || (val > 255)) return 0;
     26 		if ( ( j < 3 ) && ( *(p++) != '.' ) ) return 0;
     27 		ip = (ip << 8) | val;
     28 	}
     29 	if ( *p == '\0' ) {
     30 		inp->s_addr = htonl(ip);
     31 		return 1;
     32 	}
     33 	return 0;
     34 }
     35 
     36 unsigned long strtoul ( const char *p, char **endp, int base ) {
     37 	unsigned long ret = 0;
     38 	unsigned int charval;
     39 
     40 	while ( isspace ( *p ) )
     41 		p++;
     42 
     43 	if ( base == 0 ) {
     44 		base = 10;
     45 		if ( *p == '0' ) {
     46 			p++;
     47 			base = 8;
     48 			if ( ( *p | 0x20 ) == 'x' ) {
     49 				p++;
     50 				base = 16;
     51 			}
     52 		}
     53 	}
     54 
     55 	while ( 1 ) {
     56 		charval = *p;
     57 		if ( charval >= 'a' ) {
     58 			charval = ( charval - 'a' + 10 );
     59 		} else if ( charval >= 'A' ) {
     60 			charval = ( charval - 'A' + 10 );
     61 		} else if ( charval <= '9' ) {
     62 			charval = ( charval - '0' );
     63 		}
     64 		if ( charval >= ( unsigned int ) base )
     65 			break;
     66 		ret = ( ( ret * base ) + charval );
     67 		p++;
     68 	}
     69 
     70 	if ( endp )
     71 		*endp = ( char * ) p;
     72 
     73 	return ( ret );
     74 }
     75 
     76 /*
     77  * Local variables:
     78  *  c-basic-offset: 8
     79  * End:
     80  */
     81