]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/radius/radiusclient/lib/ip_util.c
Another patch from Frank Cusack to fix install: target.
[ppp.git] / pppd / plugins / radius / radiusclient / lib / ip_util.c
1 /*
2  * $Id: ip_util.c,v 1.1 2002/01/22 16:03:02 dfs 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 <config.h>
18 #include <includes.h>
19 #include <radiusclient.h>
20
21 /*
22  * Function: rc_get_ipaddr
23  *
24  * Purpose: return an IP address in host long notation from a host
25  *          name or address in dot notation.
26  *
27  * Returns: 0 on failure
28  */
29
30 UINT4 rc_get_ipaddr (char *host)
31 {
32         struct hostent *hp;
33
34         if (rc_good_ipaddr (host) == 0)
35         {
36                 return ntohl(inet_addr (host));
37         }
38         else if ((hp = gethostbyname (host)) == (struct hostent *) NULL)
39         {
40                 rc_log(LOG_ERR,"rc_get_ipaddr: couldn't resolve hostname: %s", host);
41                 return ((UINT4) 0);
42         }
43         return ntohl((*(UINT4 *) hp->h_addr));
44
45
46 /*
47  * Function: rc_good_ipaddr
48  *
49  * Purpose: check for valid IP address in standard dot notation.
50  *
51  * Returns: 0 on success, -1 when failure
52  *
53  */
54
55 int rc_good_ipaddr (char *addr)
56 {
57         int             dot_count;
58         int             digit_count;
59
60         if (addr == NULL)
61                 return (-1);
62
63         dot_count = 0;
64         digit_count = 0;
65         while (*addr != '\0' && *addr != ' ')
66         {
67                 if (*addr == '.')
68                 {
69                         dot_count++;
70                         digit_count = 0;
71                 }
72                 else if (!isdigit (*addr))
73                 {
74                         dot_count = 5;
75                 }
76                 else
77                 {
78                         digit_count++;
79                         if (digit_count > 3)
80                         {
81                                 dot_count = 5;
82                         }
83                 }
84                 addr++;
85         }
86         if (dot_count != 3)
87         {
88                 return (-1);
89         }
90         else
91         {
92                 return (0);
93         }
94 }
95
96 /*
97  * Function: rc_ip_hostname
98  *
99  * Purpose: Return a printable host name (or IP address in dot notation)
100  *          for the supplied IP address.
101  *
102  */
103  
104 const char *rc_ip_hostname (UINT4 h_ipaddr)
105 {
106         struct hostent  *hp;
107         UINT4           n_ipaddr = htonl (h_ipaddr);
108
109         if ((hp = gethostbyaddr ((char *) &n_ipaddr, sizeof (struct in_addr),
110                             AF_INET)) == NULL) {
111                 rc_log(LOG_ERR,"rc_ip_hostname: couldn't look up host by addr: %08lX", h_ipaddr);
112         }
113                                   
114         return ((hp==NULL)?"unknown":hp->h_name);
115
116
117 /*
118  * Function: rc_getport
119  *
120  * Purpose: get the port number for the supplied request type
121  *
122  */
123
124 unsigned short rc_getport(int type)
125 {
126         struct servent *svp;
127
128         if ((svp = getservbyname ((type==AUTH)?"radius":"radacct", "udp")) == NULL)
129         {
130                 return ((type==AUTH)?PW_AUTH_UDP_PORT:PW_ACCT_UDP_PORT);
131         } else {
132                 return ntohs ((unsigned short) svp->s_port);
133         }
134 }
135
136 /*
137  * Function: rc_own_hostname
138  *
139  * Purpose: get the hostname of this machine
140  *
141  * Returns  -1 on failure, 0 on success
142  *
143  */
144
145 int
146 rc_own_hostname(char *hostname, int len)
147 {
148 #ifdef HAVE_UNAME
149         struct  utsname uts;
150 #endif
151
152 #if defined(HAVE_UNAME)
153         if (uname(&uts) < 0)
154         {
155                 rc_log(LOG_ERR,"rc_own_hostname: couldn't get own hostname");
156                 return -1;
157         }
158         strncpy(hostname, uts.nodename, len);
159 #elif defined(HAVE_GETHOSTNAME)
160         if (gethostname(hostname, len) < 0)
161         {
162                 rc_log(LOG_ERR,"rc_own_hostname: couldn't get own hostname");
163                 return -1;
164         }
165 #elif defined(HAVE_SYSINFO)
166         if (sysinfo(SI_HOSTNAME, hostname, len) < 0)
167         {
168                 rc_log(LOG_ERR,"rc_own_hostname: couldn't get own hostname");
169                 return -1;
170         }
171 #else
172         return -1;
173 #endif  
174
175         return 0;
176 }
177
178 /*
179  * Function: rc_own_ipaddress
180  *
181  * Purpose: get the IP address of this host in host order
182  *
183  * Returns: IP address on success, 0 on failure
184  *
185  */
186
187 UINT4 rc_own_ipaddress(void)
188 {
189         static UINT4 this_host_ipaddr = 0;
190         char hostname[256];
191
192         if (!this_host_ipaddr) {
193                 if (rc_own_hostname(hostname, sizeof(hostname)) < 0)
194                         return 0;
195                 if ((this_host_ipaddr = rc_get_ipaddr (hostname)) == 0) {
196                         rc_log(LOG_ERR, "rc_own_ipaddress: couldn't get own IP address");
197                         return 0;
198                 }
199         } 
200         
201         return this_host_ipaddr;
202 }