addr.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #define __FORCE_GLIBC
  14. #include <features.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include <netinet/in.h>
  18. int inet_aton(const char *cp, struct in_addr *inp);
  19. #ifdef L_inet_aton
  20. int inet_aton(cp, inp)
  21. const char *cp;
  22. struct in_addr *inp;
  23. {
  24. unsigned long addr;
  25. int value;
  26. int part;
  27. if (!inp)
  28. return 0;
  29. addr = 0;
  30. for (part = 1; part <= 4; part++) {
  31. if (!isdigit(*cp))
  32. return 0;
  33. value = 0;
  34. while (isdigit(*cp)) {
  35. value *= 10;
  36. value += *cp++ - '0';
  37. if (value > 255)
  38. return 0;
  39. }
  40. if (part < 4) {
  41. if (*cp++ != '.')
  42. return 0;
  43. } else {
  44. char c = *cp++;
  45. if (c != '\0' && !isspace(c))
  46. return 0;
  47. }
  48. addr <<= 8;
  49. addr |= value;
  50. }
  51. inp->s_addr = htonl(addr);
  52. return 1;
  53. }
  54. #endif
  55. #ifdef L_inet_addr
  56. unsigned long inet_addr(cp)
  57. const char *cp;
  58. {
  59. struct in_addr a;
  60. if (!inet_aton(cp, &a))
  61. return -1;
  62. else
  63. return a.s_addr;
  64. }
  65. #endif
  66. #ifdef L_inet_ntoa
  67. #include <limits.h>
  68. #if (ULONG_MAX >> 32)
  69. /* We're set up for 32 bit unsigned longs */
  70. #error need to check size allocation for static buffer 'buf'
  71. #endif
  72. extern char *__ultostr(char *buf, unsigned long uval, int base, int uppercase);
  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 = __ultostr(p, addr & 0xff, 10, 0 ) - 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