addr.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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(in)
  74. struct in_addr in;
  75. {
  76. static char buf[16]; /* max 12 digits + 3 '.'s + 1 nul */
  77. unsigned long addr = ntohl(in.s_addr);
  78. int i;
  79. char *p, *q;
  80. q = 0;
  81. p = buf + sizeof(buf) - 1;
  82. for (i=0 ; i < 4 ; i++ ) {
  83. p = _int10tostr(p, addr & 0xff) - 1;
  84. addr >>= 8;
  85. if (q) {
  86. *q = '.';
  87. }
  88. q = p;
  89. }
  90. return p+1;
  91. }
  92. #endif
  93. #ifdef L_inet_makeaddr
  94. /*
  95. * Formulate an Internet address from network + host. Used in
  96. * building addresses stored in the ifnet structure.
  97. */
  98. struct in_addr inet_makeaddr(net, host)
  99. unsigned long net, host;
  100. {
  101. unsigned long addr;
  102. if (net < 128)
  103. addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
  104. else if (net < 65536)
  105. addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST);
  106. else if (net < 16777216L)
  107. addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST);
  108. else
  109. addr = net | host;
  110. addr = htonl(addr);
  111. return (*(struct in_addr *)&addr);
  112. }
  113. #endif
  114. #ifdef L_inet_lnaof
  115. /*
  116. * Return the local network address portion of an
  117. * internet address; handles class a/b/c network
  118. * number formats.
  119. */
  120. unsigned long inet_lnaof(in)
  121. struct in_addr in;
  122. {
  123. unsigned long i = ntohl(in.s_addr);
  124. if (IN_CLASSA(i))
  125. return ((i)&IN_CLASSA_HOST);
  126. else if (IN_CLASSB(i))
  127. return ((i)&IN_CLASSB_HOST);
  128. else
  129. return ((i)&IN_CLASSC_HOST);
  130. }
  131. #endif
  132. #ifdef L_inet_netof
  133. /*
  134. * Return the network number from an internet
  135. * address; handles class a/b/c network #'s.
  136. */
  137. u_int32_t
  138. inet_netof(in)
  139. struct in_addr in;
  140. {
  141. u_int32_t i = ntohl(in.s_addr);
  142. if (IN_CLASSA(i))
  143. return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
  144. else if (IN_CLASSB(i))
  145. return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
  146. else
  147. return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
  148. }
  149. #endif