Radcli library 1.3.1
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 combinations 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 = "radacct";
43
44 err = getaddrinfo(host, service, &hints, &res);
45 if (err != 0) {
46 return NULL;
47 }
48
49 return res;
50}
51
63unsigned short rc_getport(int type)
64{
65 struct servent *svp;
66
67 if ((svp = getservbyname ((type==AUTH)?"radius" : "radacct", "udp")) == NULL)
68 {
69 return (type==AUTH) ? PW_AUTH_UDP_PORT : PW_ACCT_UDP_PORT;
70 } else {
71 return ntohs ((unsigned short) svp->s_port);
72 }
73}
74
81int rc_own_hostname(char *hostname, int len)
82{
83#ifdef HAVE_UNAME
84 struct utsname uts;
85#endif
86
87#if defined(HAVE_UNAME)
88 if (uname(&uts) < 0)
89 {
90 rc_log(LOG_ERR,"rc_own_hostname: couldn't get own hostname");
91 return -1;
92 }
93 strlcpy(hostname, uts.nodename, len);
94#elif defined(HAVE_GETHOSTNAME)
95 if (gethostname(hostname, len) < 0)
96 {
97 rc_log(LOG_ERR,"rc_own_hostname: couldn't get own hostname");
98 return -1;
99 }
100#elif defined(HAVE_SYSINFO)
101 if (sysinfo(SI_HOSTNAME, hostname, len) < 0)
102 {
103 rc_log(LOG_ERR,"rc_own_hostname: couldn't get own hostname");
104 return -1;
105 }
106#else
107 return -1;
108#endif
109
110 return 0;
111}
112
124int rc_get_srcaddr(struct sockaddr *lia, const struct sockaddr *ria)
125{
126 int temp_sock;
127 socklen_t namelen;
128
129 temp_sock = socket(ria->sa_family, SOCK_DGRAM, 0);
130 if (temp_sock == -1) {
131 rc_log(LOG_ERR, "rc_get_srcaddr: socket: %s", strerror(errno));
132 return ERROR_RC;
133 }
134
135 if (connect(temp_sock, ria, SA_LEN(ria)) != 0) {
136 int rc = errno == ENETUNREACH ? NETUNREACH_RC : ERROR_RC;
137 rc_log(LOG_ERR, "rc_get_srcaddr: connect: %s",
138 strerror(errno));
139 close(temp_sock);
140 return rc;
141 }
142
143 namelen = SA_LEN(ria);
144 if (getsockname(temp_sock, lia, &namelen) != 0) {
145 rc_log(LOG_ERR, "rc_get_srcaddr: getsockname: %s",
146 strerror(errno));
147 close(temp_sock);
148 return ERROR_RC;
149 }
150
151 close(temp_sock);
152 return OK_RC;
153}
154
164void rc_own_bind_addr(rc_handle *rh, struct sockaddr_storage *lia)
165{
166 char *txtaddr = rc_conf_str(rh, "bindaddr");
167 struct addrinfo *info;
168
169 if (rh->own_bind_addr_set) {
170 memcpy(lia, &rh->own_bind_addr, SS_LEN(&rh->own_bind_addr));
171 return;
172 }
173
174 memset(lia, 0, sizeof(*lia));
175 if (txtaddr == NULL || txtaddr[0] == '*') {
176 ((struct sockaddr_in*)lia)->sin_family = AF_INET;
177 ((struct sockaddr_in*)lia)->sin_addr.s_addr = INADDR_ANY;
178 } else {
179 info = rc_getaddrinfo (txtaddr, PW_AI_PASSIVE);
180 if (info == NULL) {
181 rc_log(LOG_ERR, "rc_own_ipaddress: couldn't get IP address from bindaddr");
182 ((struct sockaddr_in*)lia)->sin_family = AF_INET;
183 ((struct sockaddr_in*)lia)->sin_addr.s_addr = INADDR_ANY;
184 return;
185 }
186
187 memcpy(lia, info->ai_addr, info->ai_addrlen);
188 }
189
190 return;
191}
unsigned short rc_getport(int type)
Definition: ip_util.c:63
int rc_get_srcaddr(struct sockaddr *lia, const struct sockaddr *ria)
Definition: ip_util.c:124
int rc_own_hostname(char *hostname, int len)
Definition: ip_util.c:81
void rc_own_bind_addr(rc_handle *rh, struct sockaddr_storage *lia)
Definition: ip_util.c:164
char * rc_conf_str(rc_handle const *rh, char const *optname)
Definition: config.c:708
@ AUTH
Request for authentication server.
Definition: radcli.h:67