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