You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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