addr.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. /*
  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. int inet_aton(cp, addrptr)
  44. const char *cp;
  45. struct in_addr *addrptr;
  46. {
  47. in_addr_t addr;
  48. int value;
  49. int part;
  50. addr = 0;
  51. for (part = 1; part <= 4; part++) {
  52. if (!isdigit(*cp))
  53. return 0;
  54. value = 0;
  55. while (isdigit(*cp)) {
  56. value *= 10;
  57. value += *cp++ - '0';
  58. if (value > 255)
  59. return 0;
  60. }
  61. if (part < 4) {
  62. if (*cp++ != '.')
  63. return 0;
  64. } else {
  65. char c = *cp++;
  66. if (c != '\0' && !isspace(c))
  67. return 0;
  68. }
  69. addr <<= 8;
  70. addr |= value;
  71. }
  72. /* W. Richard Stevens in his book UNIX Network Programming,
  73. * Volume 1, second edition, on page 71 says:
  74. *
  75. * An undocumented feature of inet_aton is that if addrptr is
  76. * a null pointer, the function still performs it validation
  77. * of the input string, but does not store the result.
  78. */
  79. if (addrptr) {
  80. addrptr->s_addr = htonl(addr);
  81. }
  82. return 1;
  83. }
  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. #endif
  95. #ifdef L_inet_ntoa
  96. #define INET_NTOA_MAX_LEN 16 /* max 12 digits + 3 '.'s + 1 nul */
  97. char *inet_ntoa_r(struct in_addr in, char buf[INET_NTOA_MAX_LEN])
  98. {
  99. in_addr_t addr = ntohl(in.s_addr);
  100. int i;
  101. char *p, *q;
  102. q = 0;
  103. p = buf + INET_NTOA_MAX_LEN - 1; /* cannot use sizeof(buf) here */
  104. for (i=0 ; i < 4 ; i++ ) {
  105. p = _int10tostr(p, addr & 0xff) - 1;
  106. addr >>= 8;
  107. if (q) {
  108. *q = '.';
  109. }
  110. q = p;
  111. }
  112. return p+1;
  113. }
  114. char *inet_ntoa(struct in_addr in)
  115. {
  116. static char buf[INET_NTOA_MAX_LEN];
  117. return(inet_ntoa_r(in, buf));
  118. }
  119. #endif
  120. #ifdef L_inet_makeaddr
  121. /*
  122. * Formulate an Internet address from network + host. Used in
  123. * building addresses stored in the ifnet structure.
  124. */
  125. struct in_addr inet_makeaddr(in_addr_t net, in_addr_t host)
  126. {
  127. in_addr_t addr;
  128. if (net < 128)
  129. addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
  130. else if (net < 65536)
  131. addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST);
  132. else if (net < 16777216UL)
  133. addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST);
  134. else
  135. addr = net | host;
  136. addr = htonl(addr);
  137. return (*(struct in_addr *)&addr);
  138. }
  139. #endif
  140. #ifdef L_inet_lnaof
  141. /*
  142. * Return the local network address portion of an
  143. * internet address; handles class a/b/c network
  144. * number formats.
  145. */
  146. in_addr_t inet_lnaof(struct in_addr in)
  147. {
  148. in_addr_t i = ntohl(in.s_addr);
  149. if (IN_CLASSA(i))
  150. return ((i)&IN_CLASSA_HOST);
  151. else if (IN_CLASSB(i))
  152. return ((i)&IN_CLASSB_HOST);
  153. else
  154. return ((i)&IN_CLASSC_HOST);
  155. }
  156. #endif
  157. #ifdef L_inet_netof
  158. /*
  159. * Return the network number from an internet
  160. * address; handles class a/b/c network #'s.
  161. */
  162. in_addr_t
  163. inet_netof(struct in_addr in)
  164. {
  165. in_addr_t i = ntohl(in.s_addr);
  166. if (IN_CLASSA(i))
  167. return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
  168. else if (IN_CLASSB(i))
  169. return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
  170. else
  171. return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
  172. }
  173. #endif