-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip_util.cpp
391 lines (347 loc) · 8.27 KB
/
ip_util.cpp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
* $Id: ip_util.c,v 1.13 2007/07/11 17:29:29 cparker Exp $
*
* 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 [email protected]
* and I'll send you a copy.
*
*/
#include "config.h"
#include "includes.h"
#include "freeradius-client.h"
#if !defined(SA_LEN)
#define SA_LEN(sa) \
(((sa)->sa_family == AF_INET) ? \
sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
#endif
/*
* Function: rc_gethostbyname
*
* Purpose: threadsafe replacement for gethostbyname.
*
* Returns: NULL on failure, hostent pointer on success
*/
struct hostent *rc_gethostbyname(const char *hostname)
{
struct hostent *hp;
#ifdef GETHOSTBYNAME_R
#if defined (GETHOSTBYNAMERSTYLE_SYSV) || defined (GETHOSTBYNAMERSTYLE_GNU)
struct hostent hostbuf;
size_t hostbuflen;
char *tmphostbuf;
int res;
int herr;
hostbuflen = 1024;
tmphostbuf = (char*)malloc(hostbuflen);
#endif
#endif
#ifdef GETHOSTBYNAME_R
#if defined (GETHOSTBYNAMERSTYLE_GNU)
while ((res = gethostbyname_r(hostname, &hostbuf, tmphostbuf, hostbuflen, &hp, &herr)) == ERANGE)
{
/* Enlarge the buffer */
hostbuflen *= 2;
tmphostbuf = (char*)realloc(tmphostbuf, hostbuflen);
}
free(tmphostbuf);
#elif defined (GETHOSTBYNAMERSTYLE_SYSV)
hp = gethostbyname_r(hostname, &hostbuf, tmphostbuf, hostbuflen, &herr);
free(tmphostbuf);
#else
hp = gethostbyname(hostname);
#endif
#else
hp = gethostbyname(hostname);
#endif
if (hp == NULL) {
return NULL;
}
return hp;
}
/*
* Function: rc_gethostbyname
*
* Purpose: threadsafe replacement for gethostbyname.
*
* Returns: NULL on failure, hostent pointer on success
*/
struct hostent *rc_gethostbyaddr(const char *addr, size_t length, int format)
{
struct hostent *hp;
#ifdef GETHOSTBYADDR_R
#if defined (GETHOSTBYADDRRSTYLE_SYSV) || defined (GETHOSTBYADDRRSTYLE_GNU)
struct hostent hostbuf;
size_t hostbuflen;
char *tmphostbuf;
int res;
int herr;
hostbuflen = 1024;
tmphostbuf = (char*)malloc(hostbuflen);
#endif
#endif
#ifdef GETHOSTBYADDR_R
#if defined (GETHOSTBYADDRRSTYLE_GNU)
while ((res = gethostbyaddr_r(addr, length, format, &hostbuf, tmphostbuf, hostbuflen,
&hp, &herr)) == ERANGE)
{
/* Enlarge the buffer */
hostbuflen *= 2;
tmphostbuf = (char*)realloc(tmphostbuf, hostbuflen);
}
free(tmphostbuf);
#elif GETHOSTBYADDRSTYLE_SYSV
hp = gethostbyaddr_r(addr, length, format, &hostbuf, tmphostbuf, hostbuflen, &herr);
free(tmphostbuf);
#else
hp = gethostbyaddr((char *)&addr, sizeof(struct in_addr), AF_INET);
#endif
#else
hp = gethostbyaddr((char *)&addr, sizeof(struct in_addr), AF_INET);
#endif
if (hp == NULL) {
return NULL;
}
return hp;
}
/*
* Function: rc_get_ipaddr
*
* Purpose: return an IP address in host long notation from a host
* name or address in dot notation.
*
* Returns: 0 on failure
*/
//uint32_t rc_get_ipaddr (char *host)
uint32_t rc_get_ipaddr (string host)
{
struct hostent *hp;
if (rc_good_ipaddr (host.c_str()) == 0)
{
return ntohl(inet_addr (host.c_str()));
}
else if ((hp = rc_gethostbyname(host.c_str())) == NULL)
{
// rc_log(LOG_ERR,"rc_get_ipaddr: couldn't resolve hostname: %s", host);
return (uint32_t)0;
}
return ntohl((*(uint32_t *) hp->h_addr));
}
/*
* Function: rc_good_ipaddr
*
* Purpose: check for valid IP address in standard dot notation.
*
* Returns: 0 on success, -1 when failure
*
*/
int rc_good_ipaddr (const char *addr)
{
int dot_count;
int digit_count;
if (addr == NULL)
return -1;
dot_count = 0;
digit_count = 0;
while (*addr != '\0' && *addr != ' ')
{
if (*addr == '.')
{
dot_count++;
digit_count = 0;
}
else if (!isdigit (*addr))
{
dot_count = 5;
}
else
{
digit_count++;
if (digit_count > 3)
{
dot_count = 5;
}
}
addr++;
}
if (dot_count != 3)
{
return -1;
}
else
{
return 0;
}
}
/*
* Function: rc_ip_hostname
*
* Purpose: Return a printable host name (or IP address in dot notation)
* for the supplied IP address.
*
*/
const char *rc_ip_hostname (uint32_t h_ipaddr)
{
struct hostent *hp;
uint32_t n_ipaddr = htonl (h_ipaddr);
if ((hp = rc_gethostbyaddr ((char *) &n_ipaddr, sizeof (struct in_addr),
AF_INET)) == NULL) {
// rc_log(LOG_ERR,"rc_ip_hostname: couldn't look up host by addr: %08lX", h_ipaddr);
}
return (hp == NULL) ? "unknown" : hp->h_name;
}
/*
* Function: rc_getport
*
* Purpose: get the port number for the supplied request type
*
*/
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);
}
}
/*
* Function: rc_own_hostname
*
* Purpose: get the hostname of this machine
*
* Returns -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;
}
strncpy(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;
}
/*
* Function: rc_own_ipaddress
*
* Purpose: get the IP address of this host in host order
*
* Returns: IP address on success, 0 on failure
*
*/
uint32_t rc_own_ipaddress(rc_handle *rh)
{
char hostname[256];
if (!rh->this_host_ipaddr) {
if (rc_conf_str(rh, "bindaddr") == NULL ||
strcmp(rc_conf_str(rh, "bindaddr"), "*") == 0) {
if (rc_own_hostname(hostname, sizeof(hostname)) < 0)
return 0;
} else {
strncpy(hostname, rc_conf_str(rh, "bindaddr"), sizeof(hostname));
hostname[sizeof(hostname) - 1] = '\0';
}
if ((rh->this_host_ipaddr = rc_get_ipaddr (hostname)) == 0) {
// rc_log(LOG_ERR, "rc_own_ipaddress: couldn't get own IP address");
return 0;
}
}
return rh->this_host_ipaddr;
}
/*
* Function: rc_own_bind_ipaddress
*
* Purpose: get the IP address to be used as a source address
* for sending requests in host order
*
* Returns: IP address
*
*/
uint32_t rc_own_bind_ipaddress(rc_handle *rh)
{
char hostname[256];
uint32_t rval;
if (rh->this_host_bind_ipaddr != NULL)
return *rh->this_host_bind_ipaddr;
rh->this_host_bind_ipaddr = (uint32_t*)malloc(sizeof(*rh->this_host_bind_ipaddr));
if (rh->this_host_bind_ipaddr == NULL)
// rc_log(LOG_CRIT, "rc_own_bind_ipaddress: out of memory");
if (rc_conf_str(rh, "bindaddr") == NULL ||
strcmp(rc_conf_str(rh, "bindaddr"), "*") == 0) {
rval = INADDR_ANY;
} else {
strncpy(hostname, rc_conf_str(rh, "bindaddr"), sizeof(hostname));
hostname[sizeof(hostname) - 1] = '\0';
if ((rval = rc_get_ipaddr (hostname)) == 0) {
// rc_log(LOG_ERR, "rc_own_ipaddress: couldn't get IP address from bindaddr");
rval = INADDR_ANY;
}
}
if (rh->this_host_bind_ipaddr != NULL)
*rh->this_host_bind_ipaddr = rval;
return rval;
}
/*
* Function: rc_get_srcaddr
*
* Purpose: given remote address find local address which the
* system will use as a source address for sending
* datagrams to that remote address
*
* Returns: 0 in success, -1 on failure, address is filled into
* the first argument.
*
*/
int
rc_get_srcaddr(struct sockaddr *lia, 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;
}