inet_aton.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * inet_aton.c,v 1.3 1993/05/19 03:39:32 jch Exp
  3. */
  4. /* Gated Release 3.5 */
  5. /* Copyright (c) 1990,1991,1992,1993,1994,1995 by Cornell University. All */
  6. /* rights reserved. Refer to Particulars and other Copyright notices at */
  7. /* the end of this file. */
  8. /* */
  9. #include <sys/types.h>
  10. #include <netinet/in.h>
  11. /*
  12. * Check whether "cp" is a valid ascii representation
  13. * of an Internet address and convert to a binary address.
  14. * Returns 1 if the address is valid, 0 if not.
  15. * This replaces inet_addr, the return value from which
  16. * cannot distinguish between failure and a local broadcast address.
  17. */
  18. int
  19. inet_aton(const char *cp, struct in_addr *ap)
  20. {
  21. int dots = 0;
  22. register u_long acc = 0, addr = 0;
  23. do {
  24. register char cc = *cp;
  25. switch (cc) {
  26. case '0':
  27. case '1':
  28. case '2':
  29. case '3':
  30. case '4':
  31. case '5':
  32. case '6':
  33. case '7':
  34. case '8':
  35. case '9':
  36. acc = acc * 10 + (cc - '0');
  37. break;
  38. case '.':
  39. if (++dots > 3) {
  40. return 0;
  41. }
  42. /* Fall through */
  43. case '\0':
  44. if (acc > 255) {
  45. return 0;
  46. }
  47. addr = addr << 8 | acc;
  48. acc = 0;
  49. break;
  50. default:
  51. return 0;
  52. }
  53. } while (*cp++) ;
  54. /* Normalize the address */
  55. if (dots < 3) {
  56. addr <<= 8 * (3 - dots) ;
  57. }
  58. /* Store it if requested */
  59. if (ap) {
  60. ap->s_addr = htonl(addr);
  61. }
  62. return 1;
  63. }
  64. /*
  65. * ------------------------------------------------------------------------
  66. *
  67. * GateD, Release 3.5
  68. *
  69. * Copyright (c) 1990,1991,1992,1993,1994,1995 by Cornell University.
  70. * All rights reserved.
  71. *
  72. * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY
  73. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
  74. * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  75. * AND FITNESS FOR A PARTICULAR PURPOSE.
  76. *
  77. * Royalty-free licenses to redistribute GateD Release
  78. * 3 in whole or in part may be obtained by writing to:
  79. *
  80. * GateDaemon Project
  81. * Information Technologies/Network Resources
  82. * 200 CCC
  83. * Cornell University
  84. * Ithaca, NY 14853-2601 USA
  85. *
  86. * GateD is based on Kirton's EGP, UC Berkeley's routing
  87. * daemon (routed), and DCN's HELLO routing Protocol.
  88. * Development of GateD has been supported in part by the
  89. * National Science Foundation.
  90. *
  91. * Please forward bug fixes, enhancements and questions to the
  92. * gated mailing list: gated-people@gated.cornell.edu.
  93. *
  94. * ------------------------------------------------------------------------
  95. *
  96. * Portions of this software may fall under the following
  97. * copyrights:
  98. *
  99. * Copyright (c) 1988 Regents of the University of California.
  100. * All rights reserved.
  101. *
  102. * Redistribution and use in source and binary forms are
  103. * permitted provided that the above copyright notice and
  104. * this paragraph are duplicated in all such forms and that
  105. * any documentation, advertising materials, and other
  106. * materials related to such distribution and use
  107. * acknowledge that the software was developed by the
  108. * University of California, Berkeley. The name of the
  109. * University may not be used to endorse or promote
  110. * products derived from this software without specific
  111. * prior written permission. THIS SOFTWARE IS PROVIDED
  112. * ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
  113. * INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  114. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  115. */