Radcli library 2.0.2
A simple radius library -- legacy API reference
Loading...
Searching...
No Matches
ip_util.c
1/*
2 * Copyright (C) 1995,1996,1997 Lars Fenneberg
3 *
4 * Copyright 1992 Livingston Enterprises, Inc.
5 *
6 * Copyright 1992,1993, 1994,1995 The Regents of the University of Michigan
7 * and Merit Network, Inc. All Rights Reserved
8 *
9 * See the file COPYRIGHT for the respective terms and conditions.
10 * If the file is missing contact me at lf@elemental.net
11 * and I'll send you a copy.
12 *
13 */
14
15#include <config.h>
16#include <includes.h>
17#include <radcli/radcli.h>
18#include "options.h"
19#include "util.h"
20
21#if defined(__linux__)
22#include <linux/in6.h>
23#endif
24
25#define HOSTBUF_SIZE 1024
26
27/*- Returns a struct addrinfo from a host name or address in textual notation.
28 *
29 * @param host the name of the host
30 * @param flags should be a combination of PW_AI flags
31 * @return address which should be deallocated using freeaddrinfo() or NULL on failure
32 -*/
33struct addrinfo *rc_getaddrinfo (char const *host, unsigned flags)
34{
35 struct addrinfo hints, *res;
36 int err;
37 const char *service = NULL;
38
39 memset(&hints, 0, sizeof(hints));
40 hints.ai_socktype = SOCK_DGRAM;
41 if (flags & PW_AI_PASSIVE)
42 hints.ai_flags = AI_PASSIVE;
43
44 if (flags & PW_AI_AUTH)
45 service = "radius";
46 else if (flags & PW_AI_ACCT)
47 service = "radius-acct";
48
49 err = getaddrinfo(host, service, &hints, &res);
50 if (err != 0) {
51 rc_log(LOG_ERR, "rc_getaddrinfo: %s: %s", host ? host : "(null)", gai_strerror(err));
52 return NULL;
53 }
54
55 return res;
56}
57
58/*- Find the local source address the system would use to reach ria.
59 *
60 * @param lia set to the local address on success.
61 * @param ria the remote address to probe a route towards.
62 * @return OK_RC on success; NETUNREACH_RC if there is no route to ria;
63 * ERROR_RC for any other failure.
64 -*/
65int radcli2_priv_get_srcaddr(struct sockaddr *lia, const struct sockaddr *ria)
66{
67 int temp_sock;
68 socklen_t namelen;
69
70 temp_sock = socket(ria->sa_family, SOCK_DGRAM, 0);
71 if (temp_sock == -1) {
72 rc_log(LOG_ERR, "radcli2_priv_get_srcaddr: socket: %s", strerror(errno));
73 return ERROR_RC;
74 }
75
76 if (connect(temp_sock, ria, SA_LEN(ria)) != 0) {
77 int rc = errno == ENETUNREACH ? NETUNREACH_RC : ERROR_RC;
78 rc_log(LOG_ERR, "radcli2_priv_get_srcaddr: connect: %s",
79 strerror(errno));
80 close(temp_sock);
81 return rc;
82 }
83
84 namelen = SA_LEN(ria);
85 if (getsockname(temp_sock, lia, &namelen) != 0) {
86 rc_log(LOG_ERR, "radcli2_priv_get_srcaddr: getsockname: %s",
87 strerror(errno));
88 close(temp_sock);
89 return ERROR_RC;
90 }
91
92 close(temp_sock);
93 return OK_RC;
94}
95
96/*- Determine the local address to bind as a source for outgoing requests.
97 *
98 * @param rh a handle to parsed configuration.
99 * @param lia set to the resolved local bind address.
100 -*/
101void rc_own_bind_addr(rc_handle *rh, struct sockaddr_storage *lia)
102{
103 char *txtaddr = rc_conf_str_id(rh, OPT_BINDADDR);
104 struct addrinfo *info;
105
106 if (rh->own_bind_addr_set) {
107 memcpy(lia, &rh->own_bind_addr, SS_LEN(&rh->own_bind_addr));
108 return;
109 }
110
111 memset(lia, 0, sizeof(*lia));
112 if (txtaddr == NULL || txtaddr[0] == '*') {
113 ((struct sockaddr_in*)lia)->sin_family = AF_INET;
114 ((struct sockaddr_in*)lia)->sin_addr.s_addr = INADDR_ANY;
115 } else {
116 info = rc_getaddrinfo (txtaddr, PW_AI_PASSIVE);
117 if (info == NULL) {
118 rc_log(LOG_ERR, "rc_own_ipaddress: couldn't get IP address from bindaddr");
119 ((struct sockaddr_in*)lia)->sin_family = AF_INET;
120 ((struct sockaddr_in*)lia)->sin_addr.s_addr = INADDR_ANY;
121 return;
122 }
123
124 memcpy(lia, info->ai_addr, info->ai_addrlen);
125 }
126
127 return;
128}
129
130/*- Prefer a public over a temporary IPv6 source address when
131 * use-public-addr asks for it.
132 *
133 * @param rh a handle to parsed configuration.
134 * @param fd an AF_INET6 socket, not yet connected.
135 * @return 0 on success, -1 on failure.
136 -*/
137int radcli2_priv_prefer_public_addr(rc_handle *rh, int fd)
138{
139 char *non_temp_addr = rc_conf_str_id(rh, OPT_USE_PUBLIC_ADDR);
140
141 if (non_temp_addr == NULL || strcasecmp(non_temp_addr, "true") != 0)
142 return 0;
143#if defined(__linux__)
144 {
145 int sock_opt = IPV6_PREFER_SRC_PUBLIC;
146 if (setsockopt(fd, IPPROTO_IPV6, IPV6_ADDR_PREFERENCES,
147 &sock_opt, sizeof(sock_opt)) != 0) {
148 rc_log(LOG_ERR, "%s: setsockopt: %s", __func__, strerror(errno));
149 return -1;
150 }
151 }
152#elif defined(BSD) || defined(__APPLE__)
153 {
154 int sock_opt = 0;
155 if (setsockopt(fd, IPPROTO_IPV6, IPV6_PREFER_TEMPADDR,
156 &sock_opt, sizeof(sock_opt)) != 0) {
157 rc_log(LOG_ERR, "%s: setsockopt: %s", __func__, strerror(errno));
158 return -1;
159 }
160 }
161#else
162 rc_log(LOG_INFO, "%s: Usage of non-temporary IPv6 address is not "
163 "supported in this system", __func__);
164#endif
165 return 0;
166}