addr.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
  2. * This file is part of the Linux-8086 C library and is distributed
  3. * under the GNU Library General Public License.
  4. */
  5. /*
  6. * Manuel Novoa III Dec 2000
  7. *
  8. * Converted to use my new (un)signed long (long) to string routines, which
  9. * are smaller than the previous functions and don't require static buffers.
  10. * In the process, removed the reference to strcat and cut object size of
  11. * inet_ntoa in half (from 190 bytes down to 94).
  12. *
  13. * Manuel Novoa III Feb 2002
  14. *
  15. * Changed to use _int10tostr.
  16. */
  17. #define _GNU_SOURCE
  18. #define __FORCE_GLIBC
  19. #include <features.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23. #include <netinet/in.h>
  24. #include <bits/uClibc_uintmaxtostr.h>
  25. int inet_aton(const char *cp, struct in_addr *addrptr);
  26. #ifdef L_inet_aton
  27. int inet_aton(cp, addrptr)
  28. const char *cp;
  29. struct in_addr *addrptr;
  30. {
  31. in_addr_t addr;
  32. int value;
  33. int part;
  34. addr = 0;
  35. for (part = 1; part <= 4; part++) {
  36. if (!isdigit(*cp))
  37. return 0;
  38. value = 0;
  39. while (isdigit(*cp)) {
  40. value *= 10;
  41. value += *cp++ - '0';
  42. if (value > 255)
  43. return 0;
  44. }
  45. if (part < 4) {
  46. if (*cp++ != '.')
  47. return 0;
  48. } else {
  49. char c = *cp++;
  50. if (c != '\0' && !isspace(c))
  51. return 0;
  52. }
  53. addr <<= 8;
  54. addr |= value;
  55. }
  56. /* W. Richard Stevens in his book UNIX Network Programming,
  57. * Volume 1, second edition, on page 71 says:
  58. *
  59. * An undocumented feature of inet_aton is that if addrptr is
  60. * a null pointer, the function still performs it validation
  61. * of the input string, but does not store the result.
  62. */
  63. if (addrptr) {
  64. addrptr->s_addr = htonl(addr);
  65. }
  66. return 1;
  67. }
  68. #endif
  69. #ifdef L_inet_addr
  70. in_addr_t inet_addr(const char *cp)
  71. {
  72. struct in_addr a;
  73. if (!inet_aton(cp, &a))
  74. return INADDR_NONE;
  75. else
  76. return a.s_addr;
  77. }
  78. #endif
  79. #ifdef L_inet_ntoa
  80. #define INET_NTOA_MAX_LEN 16 /* max 12 digits + 3 '.'s + 1 nul */
  81. char *inet_ntoa_r(struct in_addr in, char buf[INET_NTOA_MAX_LEN])
  82. {
  83. in_addr_t addr = ntohl(in.s_addr);
  84. int i;
  85. char *p, *q;
  86. q = 0;
  87. p = buf + INET_NTOA_MAX_LEN - 1; /* cannot use sizeof(buf) here */
  88. for (i=0 ; i < 4 ; i++ ) {
  89. p = _int10tostr(p, addr & 0xff) - 1;
  90. addr >>= 8;
  91. if (q) {
  92. *q = '.';
  93. }
  94. q = p;
  95. }
  96. return p+1;
  97. }
  98. char *inet_ntoa(struct in_addr in)
  99. {
  100. static char buf[INET_NTOA_MAX_LEN];
  101. return(inet_ntoa_r(in, buf));
  102. }
  103. #endif
  104. #ifdef L_inet_makeaddr
  105. /*
  106. * Formulate an Internet address from network + host. Used in
  107. * building addresses stored in the ifnet structure.
  108. */
  109. struct in_addr inet_makeaddr(in_addr_t net, in_addr_t host)
  110. {
  111. in_addr_t addr;
  112. if (net < 128)
  113. addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
  114. else if (net < 65536)
  115. addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST);
  116. else if (net < 16777216UL)
  117. addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST);
  118. else
  119. addr = net | host;
  120. addr = htonl(addr);
  121. return (*(struct in_addr *)&addr);
  122. }
  123. #endif
  124. #ifdef L_inet_lnaof
  125. /*
  126. * Return the local network address portion of an
  127. * internet address; handles class a/b/c network
  128. * number formats.
  129. */
  130. in_addr_t inet_lnaof(struct in_addr in)
  131. {
  132. in_addr_t i = ntohl(in.s_addr);
  133. if (IN_CLASSA(i))
  134. return ((i)&IN_CLASSA_HOST);
  135. else if (IN_CLASSB(i))
  136. return ((i)&IN_CLASSB_HOST);
  137. else
  138. return ((i)&IN_CLASSC_HOST);
  139. }
  140. #endif
  141. #ifdef L_inet_netof
  142. /*
  143. * Return the network number from an internet
  144. * address; handles class a/b/c network #'s.
  145. */
  146. in_addr_t
  147. inet_netof(struct in_addr in)
  148. {
  149. in_addr_t i = ntohl(in.s_addr);
  150. if (IN_CLASSA(i))
  151. return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
  152. else if (IN_CLASSB(i))
  153. return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
  154. else
  155. return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
  156. }
  157. #endif