addr.c 4.3 KB

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