Radcli library 1.5.3
A simple radius library
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 "util.h"
19
20#define HOSTBUF_SIZE 1024
21
22/*- Returns a struct addrinfo from a host name or address in textual notation.
23 *
24 * @param host the name of the host
25 * @param flags should be a combination of PW_AI flags
26 * @return address which should be deallocated using freeaddrinfo() or NULL on failure
27 -*/
28struct addrinfo *rc_getaddrinfo (char const *host, unsigned flags)
29{
30 struct addrinfo hints, *res;
31 int err;
32 const char *service = NULL;
33
34 memset(&hints, 0, sizeof(hints));
35 hints.ai_socktype = SOCK_DGRAM;
36 if (flags & PW_AI_PASSIVE)
37 hints.ai_flags = AI_PASSIVE;
38
39 if (flags & PW_AI_AUTH)
40 service = "radius";
41 else if (flags & PW_AI_ACCT)
42 service = "radius-acct";
43
44 err = getaddrinfo(host, service, &hints, &res);
45 if (err != 0) {
46 rc_log(LOG_ERR, "rc_getaddrinfo: %s: %s", host ? host : "(null)", gai_strerror(err));
47 return NULL;
48 }
49
50 return res;
51}
52
58
64unsigned short rc_getport(int type)
65{
66 struct servent *svp;
67
68 if ((svp = getservbyname ((type==AUTH)?"radius" : "radacct", "udp")) == NULL)
69 {
70 return (type==AUTH) ? PW_AUTH_UDP_PORT : PW_ACCT_UDP_PORT;
71 } else {
72 return ntohs ((unsigned short) svp->s_port);
73 }
74}
75
82int rc_own_hostname(char *hostname, int len)
83{
84#ifdef HAVE_UNAME
85 struct utsname uts;
86#endif
87
88#if defined(HAVE_UNAME)
89 if (uname(&uts) < 0)
90 {
91 rc_log(LOG_ERR,"rc_own_hostname: couldn't get own hostname");
92 return -1;
93 }
94 strlcpy(hostname, uts.nodename, len);
95#elif defined(HAVE_GETHOSTNAME)
96 if (gethostname(hostname, len) < 0)
97 {
98 rc_log(LOG_ERR,"rc_own_hostname: couldn't get own hostname");
99 return -1;
100 }
101#elif defined(HAVE_SYSINFO)
102 if (sysinfo(SI_HOSTNAME, hostname, len) < 0)
103 {
104 rc_log(LOG_ERR,"rc_own_hostname: couldn't get own hostname");
105 return -1;
106 }
107#else
108 return -1;
109#endif
110
111 return 0;
112}
113
125int rc_get_srcaddr(struct sockaddr *lia, const struct sockaddr *ria)
126{
127 int temp_sock;
128 socklen_t namelen;
129
130 temp_sock = socket(ria->sa_family, SOCK_DGRAM, 0);
131 if (temp_sock == -1) {
132 rc_log(LOG_ERR, "rc_get_srcaddr: socket: %s", strerror(errno));
133 return ERROR_RC;
134 }
135
136 if (connect(temp_sock, ria, SA_LEN(ria)) != 0) {
137 int rc = errno == ENETUNREACH ? NETUNREACH_RC : ERROR_RC;
138 rc_log(LOG_ERR, "rc_get_srcaddr: connect: %s",
139 strerror(errno));
140 close(temp_sock);
141 return rc;
142 }
143
144 namelen = SA_LEN(ria);
145 if (getsockname(temp_sock, lia, &namelen) != 0) {
146 rc_log(LOG_ERR, "rc_get_srcaddr: getsockname: %s",
147 strerror(errno));
148 close(temp_sock);
149 return ERROR_RC;
150 }
151
152 close(temp_sock);
153 return OK_RC;
154}
155
156/* Find our source address
157 *
158 * Get the IP address to be used as a source address
159 * for sending requests in host order.
160 *
161 * @param rh a handle to parsed configuration
162 * @param lia the local address to listen to
163 *
164 **/
166void rc_own_bind_addr(rc_handle *rh, struct sockaddr_storage *lia)
167{
168 char *txtaddr = rc_conf_str(rh, "bindaddr");
169 struct addrinfo *info;
170
171 if (rh->own_bind_addr_set) {
172 memcpy(lia, &rh->own_bind_addr, SS_LEN(&rh->own_bind_addr));
173 return;
174 }
175
176 memset(lia, 0, sizeof(*lia));
177 if (txtaddr == NULL || txtaddr[0] == '*') {
178 ((struct sockaddr_in*)lia)->sin_family = AF_INET;
179 ((struct sockaddr_in*)lia)->sin_addr.s_addr = INADDR_ANY;
180 } else {
181 info = rc_getaddrinfo (txtaddr, PW_AI_PASSIVE);
182 if (info == NULL) {
183 rc_log(LOG_ERR, "rc_own_ipaddress: couldn't get IP address from bindaddr");
184 ((struct sockaddr_in*)lia)->sin_family = AF_INET;
185 ((struct sockaddr_in*)lia)->sin_addr.s_addr = INADDR_ANY;
186 return;
187 }
188
189 memcpy(lia, info->ai_addr, info->ai_addrlen);
190 }
191
192 return;
193}
195
unsigned short rc_getport(int type)
Get the port number for the supplied request type.
Definition ip_util.c:64
int rc_get_srcaddr(struct sockaddr *lia, const struct sockaddr *ria)
Find outbound interface address for a given destination.
Definition ip_util.c:125
int rc_own_hostname(char *hostname, int len)
Get the hostname of this machine.
Definition ip_util.c:82
char * rc_conf_str(rc_handle const *rh, char const *optname)
Get the value of a config option.
Definition config.c:817
@ AUTH
Request for authentication server.
Definition radcli.h:75
Public API of the radcli library.