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
17 #include <includes.h>
18 #include <radiusclient.h>
19
20 /*
21 * Function: rc_get_ipaddr
22 *
23 * Purpose: return an IP address in host long notation from a host
24 * name or address in dot notation.
25 *
26 * Returns: 0 on failure
27 */
28
rc_get_ipaddr(char * host)29 UINT4 rc_get_ipaddr (char *host)
30 {
31 struct hostent *hp;
32
33 if (rc_good_ipaddr (host) == 0)
34 {
35 return ntohl(inet_addr (host));
36 }
37 else if ((hp = gethostbyname (host)) == (struct hostent *) NULL)
38 {
39 error("rc_get_ipaddr: couldn't resolve hostname: %s", host);
40 return ((UINT4) 0);
41 }
42 return ntohl((*(UINT4 *) hp->h_addr));
43 }
44
45 /*
46 * Function: rc_good_ipaddr
47 *
48 * Purpose: check for valid IP address in standard dot notation.
49 *
50 * Returns: 0 on success, -1 when failure
51 *
52 */
53
rc_good_ipaddr(char * addr)54 int rc_good_ipaddr (char *addr)
55 {
56 int dot_count;
57 int digit_count;
58
59 if (addr == NULL)
60 return (-1);
61
62 dot_count = 0;
63 digit_count = 0;
64 while (*addr != '\0' && *addr != ' ')
65 {
66 if (*addr == '.')
67 {
68 dot_count++;
69 digit_count = 0;
70 }
71 else if (!isdigit (*addr))
72 {
73 dot_count = 5;
74 }
75 else
76 {
77 digit_count++;
78 if (digit_count > 3)
79 {
80 dot_count = 5;
81 }
82 }
83 addr++;
84 }
85 if (dot_count != 3)
86 {
87 return (-1);
88 }
89 else
90 {
91 return (0);
92 }
93 }
94
95 /*
96 * Function: rc_ip_hostname
97 *
98 * Purpose: Return a printable host name (or IP address in dot notation)
99 * for the supplied IP address.
100 *
101 */
102
rc_ip_hostname(UINT4 h_ipaddr)103 const char *rc_ip_hostname (UINT4 h_ipaddr)
104 {
105 struct hostent *hp;
106 UINT4 n_ipaddr = htonl (h_ipaddr);
107
108 if ((hp = gethostbyaddr ((char *) &n_ipaddr, sizeof (struct in_addr),
109 AF_INET)) == NULL) {
110 error("rc_ip_hostname: couldn't look up host by addr: %08lX", h_ipaddr);
111 }
112
113 return ((hp==NULL)?"unknown":hp->h_name);
114 }
115
116 /*
117 * Function: rc_own_ipaddress
118 *
119 * Purpose: get the IP address of this host in host order
120 *
121 * Returns: IP address on success, 0 on failure
122 *
123 */
124
rc_own_ipaddress(void)125 UINT4 rc_own_ipaddress(void)
126 {
127 static UINT4 this_host_ipaddr = 0;
128
129 if (!this_host_ipaddr) {
130 if ((this_host_ipaddr = rc_get_ipaddr (hostname)) == 0) {
131 error("rc_own_ipaddress: couldn't get own IP address");
132 return 0;
133 }
134 }
135
136 return this_host_ipaddr;
137 }
138
139 /*
140 * Function: rc_own_bind_ipaddress
141 *
142 * Purpose: get the IP address to be used as a source address
143 * for sending requests in host order
144 *
145 * Returns: IP address
146 *
147 */
148
rc_own_bind_ipaddress(void)149 UINT4 rc_own_bind_ipaddress(void)
150 {
151 char *bindaddr;
152 UINT4 rval = 0;
153
154 if ((bindaddr = rc_conf_str("bindaddr")) == NULL ||
155 strcmp(rc_conf_str("bindaddr"), "*") == 0) {
156 rval = INADDR_ANY;
157 } else {
158 if ((rval = rc_get_ipaddr(bindaddr)) == 0) {
159 error("rc_own_bind_ipaddress: couldn't get IP address from bindaddr");
160 rval = INADDR_ANY;
161 }
162 }
163
164 return rval;
165 }
166