addr.c 3.5 KB

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