2 * $Id: ip_util.c,v 1.1 2002/01/22 16:03:02 dfs Exp $
4 * Copyright (C) 1995,1996,1997 Lars Fenneberg
6 * Copyright 1992 Livingston Enterprises, Inc.
8 * Copyright 1992,1993, 1994,1995 The Regents of the University of Michigan
9 * and Merit Network, Inc. All Rights Reserved
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.
19 #include <radiusclient.h>
22 * Function: rc_get_ipaddr
24 * Purpose: return an IP address in host long notation from a host
25 * name or address in dot notation.
27 * Returns: 0 on failure
30 UINT4 rc_get_ipaddr (char *host)
34 if (rc_good_ipaddr (host) == 0)
36 return ntohl(inet_addr (host));
38 else if ((hp = gethostbyname (host)) == (struct hostent *) NULL)
40 rc_log(LOG_ERR,"rc_get_ipaddr: couldn't resolve hostname: %s", host);
43 return ntohl((*(UINT4 *) hp->h_addr));
47 * Function: rc_good_ipaddr
49 * Purpose: check for valid IP address in standard dot notation.
51 * Returns: 0 on success, -1 when failure
55 int rc_good_ipaddr (char *addr)
65 while (*addr != '\0' && *addr != ' ')
72 else if (!isdigit (*addr))
97 * Function: rc_ip_hostname
99 * Purpose: Return a printable host name (or IP address in dot notation)
100 * for the supplied IP address.
104 const char *rc_ip_hostname (UINT4 h_ipaddr)
107 UINT4 n_ipaddr = htonl (h_ipaddr);
109 if ((hp = gethostbyaddr ((char *) &n_ipaddr, sizeof (struct in_addr),
111 rc_log(LOG_ERR,"rc_ip_hostname: couldn't look up host by addr: %08lX", h_ipaddr);
114 return ((hp==NULL)?"unknown":hp->h_name);
118 * Function: rc_getport
120 * Purpose: get the port number for the supplied request type
124 unsigned short rc_getport(int type)
128 if ((svp = getservbyname ((type==AUTH)?"radius":"radacct", "udp")) == NULL)
130 return ((type==AUTH)?PW_AUTH_UDP_PORT:PW_ACCT_UDP_PORT);
132 return ntohs ((unsigned short) svp->s_port);
137 * Function: rc_own_hostname
139 * Purpose: get the hostname of this machine
141 * Returns -1 on failure, 0 on success
146 rc_own_hostname(char *hostname, int len)
152 #if defined(HAVE_UNAME)
155 rc_log(LOG_ERR,"rc_own_hostname: couldn't get own hostname");
158 strncpy(hostname, uts.nodename, len);
159 #elif defined(HAVE_GETHOSTNAME)
160 if (gethostname(hostname, len) < 0)
162 rc_log(LOG_ERR,"rc_own_hostname: couldn't get own hostname");
165 #elif defined(HAVE_SYSINFO)
166 if (sysinfo(SI_HOSTNAME, hostname, len) < 0)
168 rc_log(LOG_ERR,"rc_own_hostname: couldn't get own hostname");
179 * Function: rc_own_ipaddress
181 * Purpose: get the IP address of this host in host order
183 * Returns: IP address on success, 0 on failure
187 UINT4 rc_own_ipaddress(void)
189 static UINT4 this_host_ipaddr = 0;
192 if (!this_host_ipaddr) {
193 if (rc_own_hostname(hostname, sizeof(hostname)) < 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");
201 return this_host_ipaddr;