addr.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #define _STDIO_UTILITY /* For _int10tostr. */
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23. #include <netinet/in.h>
  24. int inet_aton(const char *cp, struct in_addr *addrptr);
  25. #ifdef L_inet_aton
  26. int inet_aton(cp, addrptr)
  27. const char *cp;
  28. struct in_addr *addrptr;
  29. {
  30. unsigned long addr;
  31. int value;
  32. int part;
  33. addr = 0;
  34. for (part = 1; part <= 4; part++) {
  35. if (!isdigit(*cp))
  36. return 0;
  37. value = 0;
  38. while (isdigit(*cp)) {
  39. value *= 10;
  40. value += *cp++ - '0';
  41. if (value > 255)
  42. return 0;
  43. }
  44. if (part < 4) {
  45. if (*cp++ != '.')
  46. return 0;
  47. } else {
  48. char c = *cp++;
  49. if (c != '\0' && !isspace(c))
  50. return 0;
  51. }
  52. addr <<= 8;
  53. addr |= value;
  54. }
  55. /* W. Richard Stevens in his book UNIX Network Programming,
  56. * Volume 1, second edition, on page 71 says:
  57. *
  58. * An undocumented feature of inet_aton is that if addrptr is
  59. * a null pointer, the function still performs it validation
  60. * of the input string, but does not store the result.
  61. */
  62. if (addrptr) {
  63. addrptr->s_addr = htonl(addr);
  64. }
  65. return 1;
  66. }
  67. #endif
  68. #ifdef L_inet_addr
  69. unsigned long inet_addr(const char *cp)
  70. {
  71. struct in_addr a;
  72. if (!inet_aton(cp, &a))
  73. return -1;
  74. else
  75. return a.s_addr;
  76. }
  77. #endif
  78. #ifdef L_inet_ntoa
  79. #define INET_NTOA_MAX_LEN 16 /* max 12 digits + 3 '.'s + 1 nul */
  80. char *inet_ntoa_r(struct in_addr in, char buf[INET_NTOA_MAX_LEN])
  81. {
  82. unsigned long addr = ntohl(in.s_addr);
  83. int i;
  84. char *p, *q;
  85. q = 0;
  86. p = buf + INET_NTOA_MAX_LEN - 1; /* cannot use sizeof(buf) here */
  87. for (i=0 ; i < 4 ; i++ ) {
  88. p = _int10tostr(p, addr & 0xff) - 1;
  89. addr >>= 8;
  90. if (q) {
  91. *q = '.';
  92. }
  93. q = p;
  94. }
  95. return p+1;
  96. }
  97. char *inet_ntoa(struct in_addr in)
  98. {
  99. static char buf[INET_NTOA_MAX_LEN];
  100. return(inet_ntoa_r(in, buf));
  101. }
  102. #endif
  103. #ifdef L_inet_makeaddr
  104. /*
  105. * Formulate an Internet address from network + host. Used in
  106. * building addresses stored in the ifnet structure.
  107. */
  108. struct in_addr inet_makeaddr(unsigned long net, unsigned long host)
  109. {
  110. unsigned long addr;
  111. if (net < 128)
  112. addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
  113. else if (net < 65536)
  114. addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST);
  115. else if (net < 16777216L)
  116. addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST);
  117. else
  118. addr = net | host;
  119. addr = htonl(addr);
  120. return (*(struct in_addr *)&addr);
  121. }
  122. #endif
  123. #ifdef L_inet_lnaof
  124. /*
  125. * Return the local network address portion of an
  126. * internet address; handles class a/b/c network
  127. * number formats.
  128. */
  129. unsigned long inet_lnaof(struct in_addr in)
  130. {
  131. unsigned long i = ntohl(in.s_addr);
  132. if (IN_CLASSA(i))
  133. return ((i)&IN_CLASSA_HOST);
  134. else if (IN_CLASSB(i))
  135. return ((i)&IN_CLASSB_HOST);
  136. else
  137. return ((i)&IN_CLASSC_HOST);
  138. }
  139. #endif
  140. #ifdef L_inet_netof
  141. /*
  142. * Return the network number from an internet
  143. * address; handles class a/b/c network #'s.
  144. */
  145. u_int32_t
  146. inet_netof(struct in_addr in)
  147. {
  148. u_int32_t i = ntohl(in.s_addr);
  149. if (IN_CLASSA(i))
  150. return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
  151. else if (IN_CLASSB(i))
  152. return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
  153. else
  154. return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
  155. }
  156. #endif