addr.c 3.5 KB

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