2 * misc.c Various miscellaneous functions.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2000,2006 The FreeRADIUS server project
23 #include <freeradius-devel/ident.h>
26 #include <freeradius-devel/libradius.h>
37 * Return an IP address in standard dot notation
41 const char *ip_ntoa(char *buffer, uint32_t ipaddr)
43 ipaddr = ntohl(ipaddr);
45 sprintf(buffer, "%d.%d.%d.%d",
46 (ipaddr >> 24) & 0xff,
47 (ipaddr >> 16) & 0xff,
56 * Internal wrapper for locking, to minimize the number of ifdef's
58 * Lock an fd, prefer lockf() over flock()
60 int rad_lockfd(int fd, int lock_len)
62 #if defined(F_LOCK) && !defined(BSD)
63 return lockf(fd, F_LOCK, lock_len);
64 #elif defined(LOCK_EX)
65 return flock(fd, LOCK_EX);
72 fl.l_whence = SEEK_CUR;
73 return fcntl(fd, F_SETLKW, (void *)&fl);
78 * Internal wrapper for locking, to minimize the number of ifdef's
80 * Lock an fd, prefer lockf() over flock()
81 * Nonblocking version.
83 int rad_lockfd_nonblock(int fd, int lock_len)
85 #if defined(F_LOCK) && !defined(BSD)
86 return lockf(fd, F_TLOCK, lock_len);
87 #elif defined(LOCK_EX)
88 return flock(fd, LOCK_EX | LOCK_NB);
95 fl.l_whence = SEEK_CUR;
96 return fcntl(fd, F_SETLK, (void *)&fl);
101 * Internal wrapper for unlocking, to minimize the number of ifdef's
104 * Unlock an fd, prefer lockf() over flock()
106 int rad_unlockfd(int fd, int lock_len)
108 #if defined(F_LOCK) && !defined(BSD)
109 return lockf(fd, F_ULOCK, lock_len);
110 #elif defined(LOCK_EX)
111 return flock(fd, LOCK_UN);
118 fl.l_whence = SEEK_CUR;
119 return fcntl(fd, F_UNLCK, (void *)&fl);
124 * Return an interface-id in standard colon notation
126 char *ifid_ntoa(char *buffer, size_t size, uint8_t *ifid)
128 snprintf(buffer, size, "%x:%x:%x:%x",
129 (ifid[0] << 8) + ifid[1], (ifid[2] << 8) + ifid[3],
130 (ifid[4] << 8) + ifid[5], (ifid[6] << 8) + ifid[7]);
136 * Return an interface-id from
137 * one supplied in standard colon notation.
139 uint8_t *ifid_aton(const char *ifid_str, uint8_t *ifid)
141 static const char xdigits[] = "0123456789abcdef";
143 int num_id = 0, val = 0, idx = 0;
145 for (p = ifid_str; ; ++p) {
146 if (*p == ':' || *p == '\0') {
151 * Drop 'val' into the array.
153 ifid[idx] = (val >> 8) & 0xff;
154 ifid[idx + 1] = val & 0xff;
157 * Must have all entries before
168 } else if ((pch = strchr(xdigits, tolower(*p))) != NULL) {
172 * Dumb version of 'scanf'
175 val |= (pch - xdigits);
183 #ifndef HAVE_INET_PTON
184 static int inet_pton4(const char *src, struct in_addr *dst)
190 static const char digits[] = "0123456789";
196 while (*p && ((off = strchr(digits, *p)) != NULL)) {
198 num += (off - digits);
200 if (num > 255) return 0;
207 * Not a digit, MUST be a dot, else we
219 * End of the string. At the fourth
220 * octet is OK, anything else is an
228 memcpy(dst, &tmp, sizeof(tmp));
234 * inet_pton6(src, dst)
235 * convert presentation level address to network order binary form.
237 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
239 * (1) does not touch `dst' unless it's returning 1.
240 * (2) :: in a full address is silently ignored.
242 * inspired by Mark Andrews.
247 inet_pton6(const char *src, unsigned char *dst)
249 static const char xdigits_l[] = "0123456789abcdef",
250 xdigits_u[] = "0123456789ABCDEF";
251 u_char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
252 const char *xdigits, *curtok;
256 memset((tp = tmp), 0, IN6ADDRSZ);
257 endp = tp + IN6ADDRSZ;
259 /* Leading :: requires some special handling. */
266 while ((ch = *src++) != '\0') {
269 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
270 pch = strchr((xdigits = xdigits_u), ch);
273 val |= (pch - xdigits);
287 if (tp + INT16SZ > endp)
289 *tp++ = (u_char) (val >> 8) & 0xff;
290 *tp++ = (u_char) val & 0xff;
295 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
296 inet_pton4(curtok, tp) > 0) {
299 break; /* '\0' was seen by inet_pton4(). */
304 if (tp + INT16SZ > endp)
306 *tp++ = (u_char) (val >> 8) & 0xff;
307 *tp++ = (u_char) val & 0xff;
309 if (colonp != NULL) {
311 * Since some memmove()'s erroneously fail to handle
312 * overlapping regions, we'll do the shift by hand.
314 const int n = tp - colonp;
317 for (i = 1; i <= n; i++) {
318 endp[- i] = colonp[n - i];
325 /* bcopy(tmp, dst, IN6ADDRSZ); */
326 memcpy(dst, tmp, IN6ADDRSZ);
331 * Utility function, so that the rest of the server doesn't
332 * have ifdef's around IPv6 support
334 int inet_pton(int af, const char *src, void *dst)
337 return inet_pton4(src, dst);
340 if (af == AF_INET6) {
341 return inet_pton6(src, dst);
349 #ifndef HAVE_INET_NTOP
351 * Utility function, so that the rest of the server doesn't
352 * have ifdef's around IPv6 support
354 const char *inet_ntop(int af, const void *src, char *dst, size_t cnt)
357 const uint8_t *ipaddr = src;
359 if (cnt <= INET_ADDRSTRLEN) return NULL;
361 snprintf(dst, cnt, "%d.%d.%d.%d",
362 ipaddr[0], ipaddr[1],
363 ipaddr[2], ipaddr[3]);
368 * If the system doesn't define this, we define it
371 if (af == AF_INET6) {
372 const struct in6_addr *ipaddr = src;
374 if (cnt <= INET6_ADDRSTRLEN) return NULL;
376 snprintf(dst, cnt, "%x:%x:%x:%x:%x:%x:%x:%x",
377 (ipaddr->s6_addr[0] << 8) | ipaddr->s6_addr[1],
378 (ipaddr->s6_addr[2] << 8) | ipaddr->s6_addr[3],
379 (ipaddr->s6_addr[4] << 8) | ipaddr->s6_addr[5],
380 (ipaddr->s6_addr[6] << 8) | ipaddr->s6_addr[7],
381 (ipaddr->s6_addr[8] << 8) | ipaddr->s6_addr[9],
382 (ipaddr->s6_addr[10] << 8) | ipaddr->s6_addr[11],
383 (ipaddr->s6_addr[12] << 8) | ipaddr->s6_addr[13],
384 (ipaddr->s6_addr[14] << 8) | ipaddr->s6_addr[15]);
388 return NULL; /* don't support IPv6 */
394 * Wrappers for IPv4/IPv6 host to IP address lookup.
395 * This API returns only one IP address, of the specified
396 * address family, or the first address (of whatever family),
397 * if AF_UNSPEC is used.
399 int ip_hton(const char *src, int af, lrad_ipaddr_t *dst)
402 struct addrinfo hints, *ai = NULL, *res = NULL;
404 memset(&hints, 0, sizeof(hints));
405 hints.ai_family = af;
407 if ((error = getaddrinfo(src, NULL, &hints, &res)) != 0) {
408 librad_log("ip_nton: %s", gai_strerror(error));
412 for (ai = res; ai; ai = ai->ai_next) {
413 if ((af == ai->ai_family) || (af == AF_UNSPEC))
418 librad_log("ip_hton failed to find requested information for host %.100s", src);
423 switch (ai->ai_family) {
427 &((struct sockaddr_in*)ai->ai_addr)->sin_addr,
428 sizeof(struct in_addr));
434 &((struct sockaddr_in6*)ai->ai_addr)->sin6_addr,
435 sizeof(struct in6_addr));
438 /* Flow should never reach here */
441 librad_log("ip_hton found unusable information for host %.100s", src);
451 * Look IP addreses up, and print names (depending on DNS config)
453 const char *ip_ntoh(const lrad_ipaddr_t *src, char *dst, size_t cnt)
455 struct sockaddr_storage ss;
456 struct sockaddr_in *s4;
463 return inet_ntop(src->af, &(src->ipaddr), dst, cnt);
467 memset(&ss, 0, sizeof(ss));
470 s4 = (struct sockaddr_in *)&ss;
471 len = sizeof(struct sockaddr_in);
472 s4->sin_family = AF_INET;
474 memcpy(&s4->sin_addr, &src->ipaddr.ip4addr, 4);
477 #ifdef HAVE_STRUCT_SOCKADDR_IN6
480 struct sockaddr_in6 *s6;
482 s6 = (struct sockaddr_in6 *)&ss;
483 len = sizeof(struct sockaddr_in6);
484 s6->sin6_family = AF_INET6;
485 s6->sin6_flowinfo = 0;
487 memcpy(&s6->sin6_addr, &src->ipaddr.ip6addr, 16);
496 if ((error = getnameinfo((struct sockaddr *)&ss, len, dst, cnt, NULL, 0,
497 NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
498 librad_log("ip_ntoh: %s", gai_strerror(error));
505 static const char *hextab = "0123456789abcdef";
510 * We allow: hex == bin
512 int lrad_hex2bin(const char *hex, uint8_t *bin, int len)
517 for (i = 0; i < len; i++) {
518 if(!(c1 = memchr(hextab, tolower((int) hex[i << 1]), 16)) ||
519 !(c2 = memchr(hextab, tolower((int) hex[(i << 1) + 1]), 16)))
521 bin[i] = ((c1-hextab)<<4) + (c2-hextab);
531 * If the output buffer isn't long enough, we have a buffer overflow.
533 void lrad_bin2hex(const uint8_t *bin, char *hex, int len)
537 for (i = 0; i < len; i++) {
538 hex[0] = hextab[((*bin) >> 4) & 0x0f];
539 hex[1] = hextab[*bin & 0x0f];
549 * So we don't have ifdef's in the rest of the code
551 #ifndef HAVE_CLOSEFROM
552 int closefrom(int fd)
558 maxfd = sysconf(_SC_OPEN_MAX);
564 if (fd > maxfd) return 0;
567 * FIXME: return EINTR?
571 for (i = fd; i < maxfd; i++) {