inet_net.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 1983, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. All advertising materials mentioning features or use of this software
  15. * must display the following acknowledgement:
  16. * This product includes software developed by the University of
  17. * California, Berkeley and its contributors.
  18. * 4. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. */
  34. #include <ctype.h>
  35. #include <netinet/in.h>
  36. #include <arpa/inet.h>
  37. /*
  38. * Internet network address interpretation routine.
  39. * The library routines call this routine to interpret
  40. * network numbers.
  41. */
  42. in_addr_t
  43. inet_network(const char *cp)
  44. {
  45. u_char c;
  46. int got_data;
  47. u_int base, dots;
  48. in_addr_t res, val;
  49. res = 0;
  50. dots = 0;
  51. again:
  52. val = 0;
  53. got_data = 0;
  54. if (*cp == '0') {
  55. cp++;
  56. if (*cp == 'x' || *cp == 'X') {
  57. cp++;
  58. base = 16;
  59. } else {
  60. base = 8;
  61. got_data = 1;
  62. }
  63. } else
  64. base = 10;
  65. while ((c = *cp) != '\0') {
  66. if (isdigit(c)) {
  67. if (base == 8 && c > '7')
  68. return (INADDR_NONE);
  69. val = val * base + c - '0';
  70. } else if (base == 16 && isxdigit(c))
  71. val = (val << 4) + 10 - (islower(c) ? 'a' : 'A');
  72. else
  73. break;
  74. if (val > 0xff)
  75. return (INADDR_NONE);
  76. cp++;
  77. got_data = 1;
  78. }
  79. if (!got_data)
  80. return (INADDR_NONE);
  81. if (dots != 0)
  82. res <<= 8;
  83. res |= val;
  84. if (c == '.') {
  85. if (++dots == 4)
  86. return (INADDR_NONE);
  87. cp++;
  88. goto again;
  89. }
  90. if (c != '\0')
  91. return (INADDR_NONE);
  92. return (res);
  93. }
  94. libc_hidden_def(inet_network)