ip_util.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * $Id: ip_util.c,v 1.1 2004/11/14 07:26:26 paulus Exp $
  3. *
  4. * Copyright (C) 1995,1996,1997 Lars Fenneberg
  5. *
  6. * Copyright 1992 Livingston Enterprises, Inc.
  7. *
  8. * Copyright 1992,1993, 1994,1995 The Regents of the University of Michigan
  9. * and Merit Network, Inc. All Rights Reserved
  10. *
  11. * See the file COPYRIGHT for the respective terms and conditions.
  12. * If the file is missing contact me at lf@elemental.net
  13. * and I'll send you a copy.
  14. *
  15. */
  16. #include <includes.h>
  17. #include <radiusclient.h>
  18. /*
  19. * Function: rc_get_ipaddr
  20. *
  21. * Purpose: return an IP address in host long notation from a host
  22. * name or address in dot notation.
  23. *
  24. * Returns: 0 on failure
  25. */
  26. UINT4 rc_get_ipaddr (char *host)
  27. {
  28. struct hostent *hp;
  29. if (rc_good_ipaddr (host) == 0)
  30. {
  31. return ntohl(inet_addr (host));
  32. }
  33. else if ((hp = gethostbyname (host)) == (struct hostent *) NULL)
  34. {
  35. error("rc_get_ipaddr: couldn't resolve hostname: %s", host);
  36. return ((UINT4) 0);
  37. }
  38. return ntohl((*(UINT4 *) hp->h_addr));
  39. }
  40. /*
  41. * Function: rc_good_ipaddr
  42. *
  43. * Purpose: check for valid IP address in standard dot notation.
  44. *
  45. * Returns: 0 on success, -1 when failure
  46. *
  47. */
  48. int rc_good_ipaddr (char *addr)
  49. {
  50. int dot_count;
  51. int digit_count;
  52. if (addr == NULL)
  53. return (-1);
  54. dot_count = 0;
  55. digit_count = 0;
  56. while (*addr != '\0' && *addr != ' ')
  57. {
  58. if (*addr == '.')
  59. {
  60. dot_count++;
  61. digit_count = 0;
  62. }
  63. else if (!isdigit (*addr))
  64. {
  65. dot_count = 5;
  66. }
  67. else
  68. {
  69. digit_count++;
  70. if (digit_count > 3)
  71. {
  72. dot_count = 5;
  73. }
  74. }
  75. addr++;
  76. }
  77. if (dot_count != 3)
  78. {
  79. return (-1);
  80. }
  81. else
  82. {
  83. return (0);
  84. }
  85. }
  86. /*
  87. * Function: rc_ip_hostname
  88. *
  89. * Purpose: Return a printable host name (or IP address in dot notation)
  90. * for the supplied IP address.
  91. *
  92. */
  93. const char *rc_ip_hostname (UINT4 h_ipaddr)
  94. {
  95. struct hostent *hp;
  96. UINT4 n_ipaddr = htonl (h_ipaddr);
  97. if ((hp = gethostbyaddr ((char *) &n_ipaddr, sizeof (struct in_addr),
  98. AF_INET)) == NULL) {
  99. error("rc_ip_hostname: couldn't look up host by addr: %08lX", h_ipaddr);
  100. }
  101. return ((hp==NULL)?"unknown":hp->h_name);
  102. }
  103. /*
  104. * Function: rc_own_ipaddress
  105. *
  106. * Purpose: get the IP address of this host in host order
  107. *
  108. * Returns: IP address on success, 0 on failure
  109. *
  110. */
  111. UINT4 rc_own_ipaddress(void)
  112. {
  113. static UINT4 this_host_ipaddr = 0;
  114. if (!this_host_ipaddr) {
  115. if ((this_host_ipaddr = rc_get_ipaddr (hostname)) == 0) {
  116. error("rc_own_ipaddress: couldn't get own IP address");
  117. return 0;
  118. }
  119. }
  120. return this_host_ipaddr;
  121. }
  122. /*
  123. * Function: rc_own_bind_ipaddress
  124. *
  125. * Purpose: get the IP address to be used as a source address
  126. * for sending requests in host order
  127. *
  128. * Returns: IP address
  129. *
  130. */
  131. UINT4 rc_own_bind_ipaddress(void)
  132. {
  133. char *bindaddr;
  134. UINT4 rval = 0;
  135. if ((bindaddr = rc_conf_str("bindaddr")) == NULL ||
  136. strcmp(rc_conf_str("bindaddr"), "*") == 0) {
  137. rval = INADDR_ANY;
  138. } else {
  139. if ((rval = rc_get_ipaddr(bindaddr)) == 0) {
  140. error("rc_own_bind_ipaddress: couldn't get IP address from bindaddr");
  141. rval = INADDR_ANY;
  142. }
  143. }
  144. return rval;
  145. }