addr.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #define INET_NTOA_MAX_LEN 16 /* max 12 digits + 3 '.'s + 1 nul */
  73. char *inet_ntoa_r(struct in_addr in, char buf[INET_NTOA_MAX_LEN])
  74. {
  75. unsigned long addr = ntohl(in.s_addr);
  76. int i;
  77. char *p, *q;
  78. q = 0;
  79. p = buf + INET_NTOA_MAX_LEN - 1; /* cannot use sizeof(buf) here */
  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[INET_NTOA_MAX_LEN];
  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(unsigned long net, unsigned long host)
  102. {
  103. unsigned long addr;
  104. if (net < 128)
  105. addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
  106. else if (net < 65536)
  107. addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST);
  108. else if (net < 16777216L)
  109. addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST);
  110. else
  111. addr = net | host;
  112. addr = htonl(addr);
  113. return (*(struct in_addr *)&addr);
  114. }
  115. #endif
  116. #ifdef L_inet_lnaof
  117. /*
  118. * Return the local network address portion of an
  119. * internet address; handles class a/b/c network
  120. * number formats.
  121. */
  122. unsigned long inet_lnaof(struct in_addr in)
  123. {
  124. unsigned long i = ntohl(in.s_addr);
  125. if (IN_CLASSA(i))
  126. return ((i)&IN_CLASSA_HOST);
  127. else if (IN_CLASSB(i))
  128. return ((i)&IN_CLASSB_HOST);
  129. else
  130. return ((i)&IN_CLASSC_HOST);
  131. }
  132. #endif
  133. #ifdef L_inet_netof
  134. /*
  135. * Return the network number from an internet
  136. * address; handles class a/b/c network #'s.
  137. */
  138. u_int32_t
  139. inet_netof(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