addr.c 4.4 KB

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