1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
/*
* Copyright (C) 1995,1996,1997 Lars Fenneberg
*
* Copyright 1992 Livingston Enterprises, Inc.
*
* Copyright 1992,1993, 1994,1995 The Regents of the University of Michigan
* and Merit Network, Inc. All Rights Reserved
*
* See the file COPYRIGHT for the respective terms and conditions.
* If the file is missing contact me at lf@elemental.net
* and I'll send you a copy.
*
*/
#include <config.h>
#include <includes.h>
#include <radcli/radcli.h>
#include "util.h"
#define HOSTBUF_SIZE 1024
/*- Returns a struct addrinfo from a host name or address in textual notation.
*
* @param host the name of the host
* @param flags should be a combinations of PW_AI flags
* @return address which should be deallocated using freeaddrinfo() or NULL on failure
-*/
struct addrinfo *rc_getaddrinfo (char const *host, unsigned flags)
{
struct addrinfo hints, *res;
int err;
const char *service = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_DGRAM;
if (flags & PW_AI_PASSIVE)
hints.ai_flags = AI_PASSIVE;
if (flags & PW_AI_AUTH)
service = "radius";
else if (flags & PW_AI_ACCT)
service = "radacct";
err = getaddrinfo(host, service, &hints, &res);
if (err != 0) {
return NULL;
}
return res;
}
/**
* @defgroup misc-api Miscellaneous API
*
* @{
*/
/** Get the port number for the supplied request type
*
* @param type %AUTH or %ACCT.
* @return the port number.
*/
unsigned short rc_getport(int type)
{
struct servent *svp;
if ((svp = getservbyname ((type==AUTH)?"radius" : "radacct", "udp")) == NULL)
{
return (type==AUTH) ? PW_AUTH_UDP_PORT : PW_ACCT_UDP_PORT;
} else {
return ntohs ((unsigned short) svp->s_port);
}
}
/** Get the hostname of this machine
*
* @param hostname will hold the name of the host.
* @param len the size of hostname.
* @return -1 on failure, 0 on success.
*/
int rc_own_hostname(char *hostname, int len)
{
#ifdef HAVE_UNAME
struct utsname uts;
#endif
#if defined(HAVE_UNAME)
if (uname(&uts) < 0)
{
rc_log(LOG_ERR,"rc_own_hostname: couldn't get own hostname");
return -1;
}
strlcpy(hostname, uts.nodename, len);
#elif defined(HAVE_GETHOSTNAME)
if (gethostname(hostname, len) < 0)
{
rc_log(LOG_ERR,"rc_own_hostname: couldn't get own hostname");
return -1;
}
#elif defined(HAVE_SYSINFO)
if (sysinfo(SI_HOSTNAME, hostname, len) < 0)
{
rc_log(LOG_ERR,"rc_own_hostname: couldn't get own hostname");
return -1;
}
#else
return -1;
#endif
return 0;
}
/** Find outbound interface address for a given destination
*
* Given remote address find local address which the system will use as a source address for sending
* datagrams to that remote address.
*
* @param lia local address.
* @param ria the remove address.
* @return 0 in success, -1 on failure, address is filled into the first argument.
*/
int rc_get_srcaddr(struct sockaddr *lia, const struct sockaddr *ria)
{
int temp_sock;
socklen_t namelen;
temp_sock = socket(ria->sa_family, SOCK_DGRAM, 0);
if (temp_sock == -1) {
rc_log(LOG_ERR, "rc_get_srcaddr: socket: %s", strerror(errno));
return -1;
}
if (connect(temp_sock, ria, SA_LEN(ria)) != 0) {
rc_log(LOG_ERR, "rc_get_srcaddr: connect: %s",
strerror(errno));
close(temp_sock);
return -1;
}
namelen = SA_LEN(ria);
if (getsockname(temp_sock, lia, &namelen) != 0) {
rc_log(LOG_ERR, "rc_get_srcaddr: getsockname: %s",
strerror(errno));
close(temp_sock);
return -1;
}
close(temp_sock);
return 0;
}
/** Find our source address
*
* Get the IP address to be used as a source address
* for sending requests in host order.
*
* @param rh a handle to parsed configuration
* @param lia the local address to listen to
*
**/
void rc_own_bind_addr(rc_handle *rh, struct sockaddr_storage *lia)
{
char *txtaddr = rc_conf_str(rh, "bindaddr");
struct addrinfo *info;
if (rh->own_bind_addr_set) {
memcpy(lia, &rh->own_bind_addr, SS_LEN(&rh->own_bind_addr));
return;
}
memset(lia, 0, sizeof(*lia));
if (txtaddr == NULL || txtaddr[0] == '*') {
((struct sockaddr_in*)lia)->sin_family = AF_INET;
((struct sockaddr_in*)lia)->sin_addr.s_addr = INADDR_ANY;
} else {
info = rc_getaddrinfo (txtaddr, PW_AI_PASSIVE);
if (info == NULL) {
rc_log(LOG_ERR, "rc_own_ipaddress: couldn't get IP address from bindaddr");
((struct sockaddr_in*)lia)->sin_family = AF_INET;
((struct sockaddr_in*)lia)->sin_addr.s_addr = INADDR_ANY;
return;
}
memcpy(lia, info->ai_addr, info->ai_addrlen);
}
return;
}
/** @} */
|