addr.c 4.9 KB

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