17#include <radcli/radcli.h>
21#define HOSTBUF_SIZE 1024
29struct addrinfo *rc_getaddrinfo (
char const *host,
unsigned flags)
31 struct addrinfo hints, *res;
33 const char *service = NULL;
35 memset(&hints, 0,
sizeof(hints));
36 hints.ai_socktype = SOCK_DGRAM;
37 if (flags & PW_AI_PASSIVE)
38 hints.ai_flags = AI_PASSIVE;
40 if (flags & PW_AI_AUTH)
42 else if (flags & PW_AI_ACCT)
43 service =
"radius-acct";
45 err = getaddrinfo(host, service, &hints, &res);
47 rc_log(LOG_ERR,
"rc_getaddrinfo: %s: %s", host ? host :
"(null)", gai_strerror(err));
61int radcli2_priv_get_srcaddr(
struct sockaddr *lia,
const struct sockaddr *ria)
66 temp_sock = socket(ria->sa_family, SOCK_DGRAM, 0);
67 if (temp_sock == -1) {
68 rc_log(LOG_ERR,
"radcli2_priv_get_srcaddr: socket: %s", strerror(errno));
72 if (connect(temp_sock, ria, SA_LEN(ria)) != 0) {
73 int rc = errno == ENETUNREACH ? NETUNREACH_RC : ERROR_RC;
74 rc_log(LOG_ERR,
"radcli2_priv_get_srcaddr: connect: %s",
80 namelen = SA_LEN(ria);
81 if (getsockname(temp_sock, lia, &namelen) != 0) {
82 rc_log(LOG_ERR,
"radcli2_priv_get_srcaddr: getsockname: %s",
97void rc_own_bind_addr(rc_handle *rh,
struct sockaddr_storage *lia)
99 char *txtaddr = rc_conf_str_id(rh, OPT_BINDADDR);
100 struct addrinfo *info;
102 if (rh->own_bind_addr_set) {
103 memcpy(lia, &rh->own_bind_addr, SS_LEN(&rh->own_bind_addr));
107 memset(lia, 0,
sizeof(*lia));
108 if (txtaddr == NULL || txtaddr[0] ==
'*') {
109 ((
struct sockaddr_in*)lia)->sin_family = AF_INET;
110 ((
struct sockaddr_in*)lia)->sin_addr.s_addr = INADDR_ANY;
112 info = rc_getaddrinfo (txtaddr, PW_AI_PASSIVE);
114 rc_log(LOG_ERR,
"rc_own_ipaddress: couldn't get IP address from bindaddr");
115 ((
struct sockaddr_in*)lia)->sin_family = AF_INET;
116 ((
struct sockaddr_in*)lia)->sin_addr.s_addr = INADDR_ANY;
120 memcpy(lia, info->ai_addr, info->ai_addrlen);