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
25 #include <freeradius-devel/libradius.h>
31 int fr_dns_lookups = 0;
32 int fr_debug_flag = 0;
35 * Return an IP address in standard dot notation
39 char const *ip_ntoa(char *buffer, uint32_t ipaddr)
41 ipaddr = ntohl(ipaddr);
43 sprintf(buffer, "%d.%d.%d.%d",
44 (ipaddr >> 24) & 0xff,
45 (ipaddr >> 16) & 0xff,
52 * Internal wrapper for locking, to minimize the number of ifdef's
56 int rad_lockfd(int fd, int lock_len)
65 fl.l_whence = SEEK_CUR;
67 return fcntl(fd, F_SETLKW, (void *)&fl);
69 #error "missing definition for F_WRLCK, all file locks will fail"
76 * Internal wrapper for locking, to minimize the number of ifdef's
78 * Lock an fd, prefer lockf() over flock()
79 * Nonblocking version.
81 int rad_lockfd_nonblock(int fd, int lock_len)
90 fl.l_whence = SEEK_CUR;
92 return fcntl(fd, F_SETLK, (void *)&fl);
94 #error "missing definition for F_WRLCK, all file locks will fail"
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)
115 fl.l_whence = SEEK_CUR;
117 return fcntl(fd, F_UNLCK, (void *)&fl);
119 #error "missing definition for F_WRLCK, all file locks will fail"
126 * Return an interface-id in standard colon notation
128 char *ifid_ntoa(char *buffer, size_t size, uint8_t const *ifid)
130 snprintf(buffer, size, "%x:%x:%x:%x",
131 (ifid[0] << 8) + ifid[1], (ifid[2] << 8) + ifid[3],
132 (ifid[4] << 8) + ifid[5], (ifid[6] << 8) + ifid[7]);
138 * Return an interface-id from
139 * one supplied in standard colon notation.
141 uint8_t *ifid_aton(char const *ifid_str, uint8_t *ifid)
143 static char const xdigits[] = "0123456789abcdef";
145 int num_id = 0, val = 0, idx = 0;
147 for (p = ifid_str; ; ++p) {
148 if (*p == ':' || *p == '\0') {
153 * Drop 'val' into the array.
155 ifid[idx] = (val >> 8) & 0xff;
156 ifid[idx + 1] = val & 0xff;
159 * Must have all entries before
170 } else if ((pch = strchr(xdigits, tolower(*p))) != NULL) {
174 * Dumb version of 'scanf'
177 val |= (pch - xdigits);
185 #ifndef HAVE_INET_PTON
186 static int inet_pton4(char const *src, struct in_addr *dst)
192 static char const digits[] = "0123456789";
198 while (*p && ((off = strchr(digits, *p)) != NULL)) {
200 num += (off - digits);
202 if (num > 255) return 0;
209 * Not a digit, MUST be a dot, else we
221 * End of the string. At the fourth
222 * octet is OK, anything else is an
230 memcpy(dst, &tmp, sizeof(tmp));
235 #ifdef HAVE_STRUCT_SOCKADDR_IN6
237 * inet_pton6(src, dst)
238 * convert presentation level address to network order binary form.
240 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
242 * (1) does not touch `dst' unless it's returning 1.
243 * (2) :: in a full address is silently ignored.
245 * inspired by Mark Andrews.
250 inet_pton6(char const *src, unsigned char *dst)
252 static char const xdigits_l[] = "0123456789abcdef",
253 xdigits_u[] = "0123456789ABCDEF";
254 u_char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
255 char const *xdigits, *curtok;
259 memset((tp = tmp), 0, IN6ADDRSZ);
260 endp = tp + IN6ADDRSZ;
262 /* Leading :: requires some special handling. */
269 while ((ch = *src++) != '\0') {
272 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
273 pch = strchr((xdigits = xdigits_u), ch);
276 val |= (pch - xdigits);
290 if (tp + INT16SZ > endp)
292 *tp++ = (u_char) (val >> 8) & 0xff;
293 *tp++ = (u_char) val & 0xff;
298 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
299 inet_pton4(curtok, (struct in_addr *) tp) > 0) {
302 break; /* '\0' was seen by inet_pton4(). */
307 if (tp + INT16SZ > endp)
309 *tp++ = (u_char) (val >> 8) & 0xff;
310 *tp++ = (u_char) val & 0xff;
312 if (colonp != NULL) {
314 * Since some memmove()'s erroneously fail to handle
315 * overlapping regions, we'll do the shift by hand.
317 const int n = tp - colonp;
320 for (i = 1; i <= n; i++) {
321 endp[- i] = colonp[n - i];
328 /* bcopy(tmp, dst, IN6ADDRSZ); */
329 memcpy(dst, tmp, IN6ADDRSZ);
335 * Utility function, so that the rest of the server doesn't
336 * have ifdef's around IPv6 support
338 int inet_pton(int af, char const *src, void *dst)
341 return inet_pton4(src, dst);
343 #ifdef HAVE_STRUCT_SOCKADDR_IN6
345 if (af == AF_INET6) {
346 return inet_pton6(src, dst);
355 #ifndef HAVE_INET_NTOP
357 * Utility function, so that the rest of the server doesn't
358 * have ifdef's around IPv6 support
360 char const *inet_ntop(int af, void const *src, char *dst, size_t cnt)
363 const uint8_t *ipaddr = src;
365 if (cnt <= INET_ADDRSTRLEN) return NULL;
367 snprintf(dst, cnt, "%d.%d.%d.%d",
368 ipaddr[0], ipaddr[1],
369 ipaddr[2], ipaddr[3]);
374 * If the system doesn't define this, we define it
377 if (af == AF_INET6) {
378 const struct in6_addr *ipaddr = src;
380 if (cnt <= INET6_ADDRSTRLEN) return NULL;
382 snprintf(dst, cnt, "%x:%x:%x:%x:%x:%x:%x:%x",
383 (ipaddr->s6_addr[0] << 8) | ipaddr->s6_addr[1],
384 (ipaddr->s6_addr[2] << 8) | ipaddr->s6_addr[3],
385 (ipaddr->s6_addr[4] << 8) | ipaddr->s6_addr[5],
386 (ipaddr->s6_addr[6] << 8) | ipaddr->s6_addr[7],
387 (ipaddr->s6_addr[8] << 8) | ipaddr->s6_addr[9],
388 (ipaddr->s6_addr[10] << 8) | ipaddr->s6_addr[11],
389 (ipaddr->s6_addr[12] << 8) | ipaddr->s6_addr[13],
390 (ipaddr->s6_addr[14] << 8) | ipaddr->s6_addr[15]);
394 return NULL; /* don't support IPv6 */
400 * Wrappers for IPv4/IPv6 host to IP address lookup.
401 * This API returns only one IP address, of the specified
402 * address family, or the first address (of whatever family),
403 * if AF_UNSPEC is used.
405 int ip_hton(char const *src, int af, fr_ipaddr_t *dst)
408 struct addrinfo hints, *ai = NULL, *res = NULL;
410 memset(&hints, 0, sizeof(hints));
411 hints.ai_family = af;
413 if ((rcode = getaddrinfo(src, NULL, &hints, &res)) != 0) {
414 fr_strerror_printf("ip_hton: %s", gai_strerror(rcode));
418 for (ai = res; ai; ai = ai->ai_next) {
419 if ((af == ai->ai_family) || (af == AF_UNSPEC))
424 fr_strerror_printf("ip_hton failed to find requested information for host %.100s", src);
429 rcode = fr_sockaddr2ipaddr((struct sockaddr_storage *)ai->ai_addr,
430 ai->ai_addrlen, dst, NULL);
432 if (!rcode) return -1;
438 * Look IP addresses up, and print names (depending on DNS config)
440 char const *ip_ntoh(fr_ipaddr_t const *src, char *dst, size_t cnt)
442 struct sockaddr_storage ss;
449 if (!fr_dns_lookups) {
450 return inet_ntop(src->af, &(src->ipaddr), dst, cnt);
453 if (!fr_ipaddr2sockaddr(src, 0, &ss, &salen)) {
457 if ((error = getnameinfo((struct sockaddr *)&ss, salen, dst, cnt, NULL, 0,
458 NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
459 fr_strerror_printf("ip_ntoh: %s", gai_strerror(error));
466 static char const *hextab = "0123456789abcdef";
468 /** Convert hex strings to binary data
470 * @param hex input string.
471 * @param bin Buffer to write output to.
472 * @param len length of input string.
473 * @return length of data written to buffer.
475 size_t fr_hex2bin(char const *hex, uint8_t *bin, size_t len)
480 for (i = 0; i < len; i++) {
481 if(!(c1 = memchr(hextab, tolower((int) hex[i << 1]), 16)) ||
482 !(c2 = memchr(hextab, tolower((int) hex[(i << 1) + 1]), 16)))
484 bin[i] = ((c1-hextab)<<4) + (c2-hextab);
491 /** Convert binary data to a hex string
493 * Ascii encoded hex string will not be prefixed with '0x'
495 * @warning If the output buffer isn't long enough, we have a buffer overflow.
497 * @param[in] bin input.
498 * @param[out] hex Buffer to write hex output.
499 * @param[in] len of bin input.
500 * @return length of data written to buffer.
502 size_t fr_bin2hex(uint8_t const *bin, char *hex, size_t len)
506 for (i = 0; i < len; i++) {
507 hex[0] = hextab[((*bin) >> 4) & 0x0f];
508 hex[1] = hextab[*bin & 0x0f];
519 * So we don't have ifdef's in the rest of the code
521 #ifndef HAVE_CLOSEFROM
522 int closefrom(int fd)
528 maxfd = sysconf(_SC_OPEN_MAX);
534 if (fd > maxfd) return 0;
537 * FIXME: return EINTR?
541 for (i = fd; i < maxfd; i++) {
549 int fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b)
551 if (a->af < b->af) return -1;
552 if (a->af > b->af) return +1;
556 return memcmp(&a->ipaddr.ip4addr,
558 sizeof(a->ipaddr.ip4addr));
561 #ifdef HAVE_STRUCT_SOCKADDR_IN6
563 if (a->scope < b->scope) return -1;
564 if (a->scope > b->scope) return +1;
566 return memcmp(&a->ipaddr.ip6addr,
568 sizeof(a->ipaddr.ip6addr));
579 int fr_ipaddr2sockaddr(fr_ipaddr_t const *ipaddr, int port,
580 struct sockaddr_storage *sa, socklen_t *salen)
582 if (ipaddr->af == AF_INET) {
583 struct sockaddr_in s4;
587 memset(&s4, 0, sizeof(s4));
588 s4.sin_family = AF_INET;
589 s4.sin_addr = ipaddr->ipaddr.ip4addr;
590 s4.sin_port = htons(port);
591 memset(sa, 0, sizeof(*sa));
592 memcpy(sa, &s4, sizeof(s4));
594 #ifdef HAVE_STRUCT_SOCKADDR_IN6
595 } else if (ipaddr->af == AF_INET6) {
596 struct sockaddr_in6 s6;
600 memset(&s6, 0, sizeof(s6));
601 s6.sin6_family = AF_INET6;
602 s6.sin6_addr = ipaddr->ipaddr.ip6addr;
603 s6.sin6_port = htons(port);
604 s6.sin6_scope_id = ipaddr->scope;
605 memset(sa, 0, sizeof(*sa));
606 memcpy(sa, &s6, sizeof(s6));
616 int fr_sockaddr2ipaddr(struct sockaddr_storage const *sa, socklen_t salen,
617 fr_ipaddr_t *ipaddr, int *port)
619 if (sa->ss_family == AF_INET) {
620 struct sockaddr_in s4;
622 if (salen < sizeof(s4)) {
623 fr_strerror_printf("IPv4 address is too small");
627 memcpy(&s4, sa, sizeof(s4));
628 ipaddr->af = AF_INET;
629 ipaddr->ipaddr.ip4addr = s4.sin_addr;
630 if (port) *port = ntohs(s4.sin_port);
632 #ifdef HAVE_STRUCT_SOCKADDR_IN6
633 } else if (sa->ss_family == AF_INET6) {
634 struct sockaddr_in6 s6;
636 if (salen < sizeof(s6)) {
637 fr_strerror_printf("IPv6 address is too small");
641 memcpy(&s6, sa, sizeof(s6));
642 ipaddr->af = AF_INET6;
643 ipaddr->ipaddr.ip6addr = s6.sin6_addr;
644 if (port) *port = ntohs(s6.sin6_port);
645 ipaddr->scope = s6.sin6_scope_id;
649 fr_strerror_printf("Unsupported address famility %d",