addr.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 __FORCE_GLIBC
  18. #include <features.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include <netinet/in.h>
  23. #include <arpa/inet.h>
  24. #include <bits/uClibc_uintmaxtostr.h>
  25. #ifdef L_inet_aton
  26. /*
  27. * More undocumented inet_aton features.
  28. * (read: uclibc doesnt support but glibc does)
  29. * http://www.mkssoftware.com/docs/man3/inet_aton.3.asp
  30. *
  31. * *cp can take the form of:
  32. * a.b.c.d - {a,b,c,d} -> 1 byte
  33. * a.b.c - {a,b} -> 1 byte {c} -> 2 bytes
  34. * a.b - {a} -> 1 byte {b} -> 3 bytes
  35. * a - {a} -> 4 bytes
  36. *
  37. * Each part may be decimal, octal, or hexadecimal as in ISO C.
  38. * 0x or 0X -> hexadecimal
  39. * leading 0 -> octal
  40. * all else -> decimal
  41. */
  42. #ifdef __UCLIBC_HAS_XLOCALE__
  43. libc_hidden_proto(__ctype_b_loc)
  44. #else
  45. libc_hidden_proto(__ctype_b)
  46. #endif
  47. libc_hidden_proto(inet_aton)
  48. int inet_aton(const char *cp, struct in_addr *addrptr)
  49. {
  50. in_addr_t addr;
  51. int value;
  52. int part;
  53. addr = 0;
  54. for (part = 1; part <= 4; part++) {
  55. if (!isdigit(*cp))
  56. return 0;
  57. value = 0;
  58. while (isdigit(*cp)) {
  59. value *= 10;
  60. value += *cp++ - '0';
  61. if (value > 255)
  62. return 0;
  63. }
  64. if (part < 4) {
  65. if (*cp++ != '.')
  66. return 0;
  67. } else {
  68. char c = *cp++;
  69. if (c != '\0' && !isspace(c))
  70. return 0;
  71. }
  72. addr <<= 8;
  73. addr |= value;
  74. }
  75. /* W. Richard Stevens in his book UNIX Network Programming,
  76. * Volume 1, second edition, on page 71 says:
  77. *
  78. * An undocumented feature of inet_aton is that if addrptr is
  79. * a null pointer, the function still performs it validation
  80. * of the input string, but does not store the result.
  81. */
  82. if (addrptr) {
  83. addrptr->s_addr = htonl(addr);
  84. }
  85. return 1;
  86. }
  87. libc_hidden_def(inet_aton)
  88. #endif
  89. #ifdef L_inet_addr
  90. libc_hidden_proto(inet_aton)
  91. libc_hidden_proto(inet_addr)
  92. in_addr_t inet_addr(const char *cp)
  93. {
  94. struct in_addr a;
  95. if (!inet_aton(cp, &a))
  96. return INADDR_NONE;
  97. else
  98. return a.s_addr;
  99. }
  100. libc_hidden_def(inet_addr)
  101. #endif
  102. #ifdef L_inet_ntoa
  103. #define INET_NTOA_MAX_LEN 16 /* max 12 digits + 3 '.'s + 1 nul */
  104. extern char *inet_ntoa_r(struct in_addr in, char buf[INET_NTOA_MAX_LEN]);
  105. libc_hidden_proto(inet_ntoa_r)
  106. char *inet_ntoa_r(struct in_addr in, char buf[INET_NTOA_MAX_LEN])
  107. {
  108. in_addr_t addr = ntohl(in.s_addr);
  109. int i;
  110. char *p, *q;
  111. q = 0;
  112. p = buf + INET_NTOA_MAX_LEN - 1; /* cannot use sizeof(buf) here */
  113. for (i=0 ; i < 4 ; i++ ) {
  114. p = _int10tostr(p, addr & 0xff) - 1;
  115. addr >>= 8;
  116. if (q) {
  117. *q = '.';
  118. }
  119. q = p;
  120. }
  121. return p+1;
  122. }
  123. libc_hidden_def(inet_ntoa_r)
  124. libc_hidden_proto(inet_ntoa)
  125. char *inet_ntoa(struct in_addr in)
  126. {
  127. static char buf[INET_NTOA_MAX_LEN];
  128. return(inet_ntoa_r(in, buf));
  129. }
  130. libc_hidden_def(inet_ntoa)
  131. #endif
  132. #ifdef L_inet_makeaddr
  133. /* for some reason it does not remove the jump relocation */
  134. libc_hidden_proto(memmove)
  135. /*
  136. * Formulate an Internet address from network + host. Used in
  137. * building addresses stored in the ifnet structure.
  138. */
  139. libc_hidden_proto(inet_makeaddr)
  140. struct in_addr inet_makeaddr(in_addr_t net, in_addr_t host)
  141. {
  142. in_addr_t addr;
  143. if (net < 128)
  144. addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
  145. else if (net < 65536)
  146. addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST);
  147. else if (net < 16777216UL)
  148. addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST);
  149. else
  150. addr = net | host;
  151. addr = htonl(addr);
  152. return (*(struct in_addr *)&addr);
  153. }
  154. libc_hidden_def(inet_makeaddr)
  155. #endif
  156. #ifdef L_inet_lnaof
  157. /*
  158. * Return the local network address portion of an
  159. * internet address; handles class a/b/c network
  160. * number formats.
  161. */
  162. in_addr_t inet_lnaof(struct in_addr in)
  163. {
  164. in_addr_t i = ntohl(in.s_addr);
  165. if (IN_CLASSA(i))
  166. return ((i)&IN_CLASSA_HOST);
  167. else if (IN_CLASSB(i))
  168. return ((i)&IN_CLASSB_HOST);
  169. else
  170. return ((i)&IN_CLASSC_HOST);
  171. }
  172. #endif
  173. #ifdef L_inet_netof
  174. /*
  175. * Return the network number from an internet
  176. * address; handles class a/b/c network #'s.
  177. */
  178. libc_hidden_proto(inet_netof)
  179. in_addr_t
  180. inet_netof(struct in_addr in)
  181. {
  182. in_addr_t i = ntohl(in.s_addr);
  183. if (IN_CLASSA(i))
  184. return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
  185. else if (IN_CLASSB(i))
  186. return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
  187. else
  188. return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
  189. }
  190. libc_hidden_def(inet_netof)
  191. #endif