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>
35 * Some versions of Linux don't have closefrom(), but they will
38 * BSD systems will generally have closefrom(), but not proc.
40 * If a system doesn't have closefrom() and isn't Linux, it
41 * doesn't have /proc, either. So don't waste time trying
52 #define FR_PUT_LE16(a, val)\
54 a[1] = ((uint16_t) (val)) >> 8;\
55 a[0] = ((uint16_t) (val)) & 0xff;\
58 bool fr_dns_lookups = false; /* IP -> hostname lookups? */
59 bool fr_hostname_lookups = true; /* hostname -> IP lookups? */
62 static char const *months[] = {
63 "jan", "feb", "mar", "apr", "may", "jun",
64 "jul", "aug", "sep", "oct", "nov", "dec" };
66 fr_thread_local_setup(char *, fr_inet_ntop_buffer) /* macro */
68 typedef struct fr_talloc_link {
73 /** Sets a signal handler using sigaction if available, else signal
75 * @param sig to set handler for.
76 * @param func handler to set.
78 int fr_set_signal(int sig, sig_t func)
83 memset(&act, 0, sizeof(act));
85 sigemptyset(&act.sa_mask);
86 act.sa_handler = func;
88 if (sigaction(sig, &act, NULL) < 0) {
89 fr_strerror_printf("Failed setting signal %i handler via sigaction(): %s", sig, fr_syserror(errno));
93 if (signal(sig, func) < 0) {
94 fr_strerror_printf("Failed setting signal %i handler via signal(): %s", sig, fr_syserror(errno));
101 /** Uninstall a signal for a specific handler
103 * man sigaction says these are fine to call from a signal handler.
107 int fr_unset_signal(int sig)
109 #ifdef HAVE_SIGACTION
110 struct sigaction act;
112 memset(&act, 0, sizeof(act));
114 sigemptyset(&act.sa_mask);
115 act.sa_handler = SIG_DFL;
117 return sigaction(sig, &act, NULL);
119 return signal(sig, SIG_DFL);
123 static int _fr_trigger_talloc_ctx_free(fr_talloc_link_t *trigger)
125 if (trigger->armed) talloc_free(trigger->child);
130 static int _fr_disarm_talloc_ctx_free(bool **armed)
136 /** Link a parent and a child context, so the child is freed before the parent
138 * @note This is not thread safe. Do not free parent before threads are joined, do not call from a child thread.
139 * @note It's OK to free the child before threads are joined, but this will leak memory until the parent is freed.
141 * @param parent who's fate the child should share.
142 * @param child bound to parent's lifecycle.
143 * @return 0 on success -1 on failure.
145 int fr_link_talloc_ctx_free(TALLOC_CTX *parent, TALLOC_CTX *child)
147 fr_talloc_link_t *trigger;
150 trigger = talloc(parent, fr_talloc_link_t);
151 if (!trigger) return -1;
153 disarm = talloc(child, bool *);
155 talloc_free(trigger);
159 trigger->child = child;
160 trigger->armed = true;
161 *disarm = &trigger->armed;
163 talloc_set_destructor(trigger, _fr_trigger_talloc_ctx_free);
164 talloc_set_destructor(disarm, _fr_disarm_talloc_ctx_free);
170 * Explicitly cleanup the memory allocated to the error inet_ntop
173 static void _fr_inet_ntop_free(void *arg)
178 /** Wrapper around inet_ntop, prints IPv4/IPv6 addresses
180 * inet_ntop requires the caller pass in a buffer for the address.
181 * This would be annoying and cumbersome, seeing as quite often the ASCII
182 * address is only used for logging output.
184 * So as with lib/log.c use TLS to allocate thread specific buffers, and
185 * write the IP address there instead.
187 * @param af address family, either AF_INET or AF_INET6.
188 * @param src pointer to network address structure.
189 * @return NULL on error, else pointer to ASCII buffer containing text version of address.
191 char const *fr_inet_ntop(int af, void const *src)
199 buffer = fr_thread_local_init(fr_inet_ntop_buffer, _fr_inet_ntop_free);
204 * malloc is thread safe, talloc is not
206 buffer = malloc(sizeof(char) * INET6_ADDRSTRLEN);
208 fr_perror("Failed allocating memory for inet_ntop buffer");
212 ret = fr_thread_local_set(fr_inet_ntop_buffer, buffer);
214 fr_perror("Failed setting up TLS for inet_ntop buffer: %s", fr_syserror(ret));
221 return inet_ntop(af, src, buffer, INET6_ADDRSTRLEN);
225 * Return an IP address in standard dot notation
229 char const *ip_ntoa(char *buffer, uint32_t ipaddr)
231 ipaddr = ntohl(ipaddr);
233 sprintf(buffer, "%d.%d.%d.%d",
234 (ipaddr >> 24) & 0xff,
235 (ipaddr >> 16) & 0xff,
236 (ipaddr >> 8) & 0xff,
242 * Parse decimal digits until we run out of decimal digits.
244 static int ip_octet_from_str(char const *str, uint32_t *poctet)
249 if ((*p < '0') || (*p > '9')) {
255 while ((*p >= '0') && (*p <= '9')) {
260 if (octet > 255) return -1;
268 static int ip_prefix_from_str(char const *str, uint32_t *paddr)
277 for (shift = 24; shift >= 0; shift -= 8) {
278 length = ip_octet_from_str(p, &octet);
279 if (length <= 0) return -1;
281 addr |= octet << shift;
285 * EOS or / means we're done.
287 if (!*p || (*p == '/')) break;
290 * We require dots between octets.
292 if (*p != '.') return -1;
296 *paddr = htonl(addr);
301 /** Parse an IPv4 address or IPv4 prefix in presentation format (and others)
303 * @param out Where to write the ip address value.
304 * @param value to parse, may be dotted quad [+ prefix], or integer, or octal number, or '*' (INADDR_ANY).
305 * @param inlen Length of value, if value is \0 terminated inlen may be -1.
306 * @param resolve If true and value doesn't look like an IP address, try and resolve value as a hostname.
307 * @param fallback to IPv6 resolution if no A records can be found.
308 * @return 0 if ip address was parsed successfully, else -1 on error.
310 int fr_pton4(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback)
316 /* Dotted quad + / + [0-9]{1,2} */
317 char buffer[INET_ADDRSTRLEN + 3];
320 * Copy to intermediary buffer if we were given a length
323 if (inlen >= (ssize_t)sizeof(buffer)) {
324 fr_strerror_printf("Invalid IPv4 address string \"%s\"", value);
327 memcpy(buffer, value, inlen);
328 buffer[inlen] = '\0';
332 p = strchr(value, '/');
335 * 192.0.2.2 is parsed as if it was /32
342 * Allow '*' as the wildcard address usually 0.0.0.0
344 if ((value[0] == '*') && (value[1] == '\0')) {
345 out->ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
348 * Convert things which are obviously integers to IP addresses
350 * We assume the number is the bigendian representation of the
353 } else if (is_integer(value) || ((value[0] == '0') && (value[1] == 'x'))) {
354 out->ipaddr.ip4addr.s_addr = htonl(strtoul(value, NULL, 0));
356 } else if (!resolve) {
357 if (inet_pton(AF_INET, value, &out->ipaddr.ip4addr.s_addr) <= 0) {
358 fr_strerror_printf("Failed to parse IPv4 addreess string \"%s\"", value);
361 } else if (ip_hton(out, AF_INET, value, fallback) < 0) return -1;
367 * Copy the IP portion into a temporary buffer if we haven't already.
369 if (inlen < 0) memcpy(buffer, value, p - value);
370 buffer[p - value] = '\0';
372 if (ip_prefix_from_str(buffer, &out->ipaddr.ip4addr.s_addr) <= 0) {
373 fr_strerror_printf("Failed to parse IPv4 address string \"%s\"", value);
377 mask = strtoul(p + 1, &eptr, 10);
379 fr_strerror_printf("Invalid IPv4 mask length \"%s\". Should be between 0-32", p);
383 if (eptr[0] != '\0') {
384 fr_strerror_printf("Failed to parse IPv4 address string \"%s\", "
385 "got garbage after mask length \"%s\"", value, eptr);
390 out->ipaddr.ip4addr = fr_inaddr_mask(&out->ipaddr.ip4addr, mask);
393 out->prefix = (uint8_t) mask;
399 /** Parse an IPv6 address or IPv6 prefix in presentation format (and others)
401 * @param out Where to write the ip address value.
402 * @param value to parse.
403 * @param inlen Length of value, if value is \0 terminated inlen may be -1.
404 * @param resolve If true and value doesn't look like an IP address, try and resolve value as a hostname.
405 * @param fallback to IPv4 resolution if no AAAA records can be found.
406 * @return 0 if ip address was parsed successfully, else -1 on error.
408 int fr_pton6(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback)
414 /* IPv6 + / + [0-9]{1,3} */
415 char buffer[INET6_ADDRSTRLEN + 4];
418 * Copy to intermediary buffer if we were given a length
421 if (inlen >= (ssize_t)sizeof(buffer)) {
422 fr_strerror_printf("Invalid IPv6 address string \"%s\"", value);
425 memcpy(buffer, value, inlen);
426 buffer[inlen] = '\0';
430 p = strchr(value, '/');
436 * Allow '*' as the wildcard address
438 if ((value[0] == '*') && (value[1] == '\0')) {
439 memset(out->ipaddr.ip6addr.s6_addr, 0, sizeof(out->ipaddr.ip6addr.s6_addr));
440 } else if (!resolve) {
441 if (inet_pton(AF_INET6, value, out->ipaddr.ip6addr.s6_addr) <= 0) {
442 fr_strerror_printf("Failed to parse IPv6 address string \"%s\"", value);
445 } else if (ip_hton(out, AF_INET6, value, fallback) < 0) return -1;
450 if ((p - value) >= INET6_ADDRSTRLEN) {
451 fr_strerror_printf("Invalid IPv6 address string \"%s\"", value);
456 * Copy string to temporary buffer if we didn't do it earlier
458 if (inlen < 0) memcpy(buffer, value, p - value);
459 buffer[p - value] = '\0';
462 if (inet_pton(AF_INET6, buffer, out->ipaddr.ip6addr.s6_addr) <= 0) {
463 fr_strerror_printf("Failed to parse IPv6 address string \"%s\"", value);
466 } else if (ip_hton(out, AF_INET6, buffer, fallback) < 0) return -1;
468 prefix = strtoul(p + 1, &eptr, 10);
470 fr_strerror_printf("Invalid IPv6 mask length \"%s\". Should be between 0-128", p);
473 if (eptr[0] != '\0') {
474 fr_strerror_printf("Failed to parse IPv6 address string \"%s\", "
475 "got garbage after mask length \"%s\"", value, eptr);
480 struct in6_addr addr;
482 addr = fr_in6addr_mask(&out->ipaddr.ip6addr, prefix);
483 memcpy(out->ipaddr.ip6addr.s6_addr, addr.s6_addr, sizeof(out->ipaddr.ip6addr.s6_addr));
486 out->prefix = (uint8_t) prefix;
492 /** Simple wrapper to decide whether an IP value is v4 or v6 and call the appropriate parser.
494 * @param[out] out Where to write the ip address value.
495 * @param[in] value to parse.
496 * @param[in] inlen Length of value, if value is \0 terminated inlen may be -1.
497 * @param[in] resolve If true and value doesn't look like an IP address, try and resolve value as a
499 * @param[in] af If the address type is not obvious from the format, and resolve is true, the DNS
500 * record (A or AAAA) we require. Also controls which parser we pass the address to if
501 * we have no idea what it is.
503 * - 0 if ip address was parsed successfully.
506 int fr_pton(fr_ipaddr_t *out, char const *value, ssize_t inlen, int af, bool resolve)
510 len = (inlen >= 0) ? (size_t)inlen : strlen(value);
511 for (i = 0; i < len; i++) switch (value[i]) {
513 * ':' is illegal in domain names and IPv4 addresses.
514 * Must be v6 and cannot be a domain.
517 return fr_pton6(out, value, inlen, false, false);
520 * Chars which don't really tell us anything
528 * Outside the range of IPv4 chars, must be a domain
529 * Use A record in preference to AAAA record.
531 if ((value[i] < '0') || (value[i] > '9')) {
533 fr_strerror_printf("Not IPv4/6 address, and asked not to resolve");
538 return fr_pton4(out, value, inlen, resolve, true);
541 return fr_pton4(out, value, inlen, resolve, false);
544 return fr_pton6(out, value, inlen, resolve, false);
547 fr_strerror_printf("Invalid address family %i", af);
555 * All chars were in the IPv4 set [0-9/.], must be an IPv4
558 return fr_pton4(out, value, inlen, false, false);
561 /** Parses IPv4/6 address + port, to fr_ipaddr_t and integer
563 * @param[out] out Where to write the ip address value.
564 * @param[out] port_out Where to write the port (0 if no port found).
565 * @param[in] value to parse.
566 * @param[in] inlen Length of value, if value is \0 terminated inlen may be -1.
567 * @param[in] af If the address type is not obvious from the format, and resolve is true, the DNS
568 * record (A or AAAA) we require. Also controls which parser we pass the address to if
569 * we have no idea what it is.
570 * @param[in] resolve If true and value doesn't look like an IP address, try and resolve value as a
573 int fr_pton_port(fr_ipaddr_t *out, uint16_t *port_out, char const *value, ssize_t inlen, int af, bool resolve)
575 char const *p = value, *q;
583 len = (inlen >= 0) ? (size_t)inlen : strlen(value);
586 if (!(q = memchr(p + 1, ']', len - 1))) {
587 fr_strerror_printf("Missing closing ']' for IPv6 address");
592 * inet_pton doesn't like the address being wrapped in []
594 if (fr_pton6(out, p + 1, (q - p) - 1, false, false) < 0) return -1;
605 * Host, IPv4 or IPv6 with no port
607 q = memchr(p, ':', len);
608 if (!q) return fr_pton(out, p, len, af, resolve);
611 * IPv4 or host, with port
613 if (fr_pton(out, p, (q - p), af, resolve) < 0) return -1;
617 * Valid ports are a maximum of 5 digits, so if the
618 * input length indicates there are more than 5 chars
619 * after the ':' then there's an issue.
621 if (inlen > ((q + sizeof(buffer)) - value)) {
623 fr_strerror_printf("IP string contains trailing garbage after port delimiter");
627 p = q + 1; /* Move to first digit */
629 strlcpy(buffer, p, (len - (p - value)) + 1);
630 port = strtoul(buffer, &end, 10);
631 if (*end != '\0') goto error; /* Trailing garbage after integer */
633 if ((port > UINT16_MAX) || (port == 0)) {
634 fr_strerror_printf("Port %lu outside valid port range 1-" STRINGIFY(UINT16_MAX), port);
642 int fr_ntop(char *out, size_t outlen, fr_ipaddr_t *addr)
644 char buffer[INET6_ADDRSTRLEN];
646 if (inet_ntop(addr->af, &(addr->ipaddr), buffer, sizeof(buffer)) == NULL) return -1;
648 return snprintf(out, outlen, "%s/%i", buffer, addr->prefix);
652 * cppcheck apparently can't pick this up from the system headers.
659 * Internal wrapper for locking, to minimize the number of ifdef's
663 int rad_lockfd(int fd, int lock_len)
672 fl.l_whence = SEEK_CUR;
674 return fcntl(fd, F_SETLKW, (void *)&fl);
676 #error "missing definition for F_WRLCK, all file locks will fail"
683 * Internal wrapper for locking, to minimize the number of ifdef's
685 * Lock an fd, prefer lockf() over flock()
686 * Nonblocking version.
688 int rad_lockfd_nonblock(int fd, int lock_len)
697 fl.l_whence = SEEK_CUR;
699 return fcntl(fd, F_SETLK, (void *)&fl);
701 #error "missing definition for F_WRLCK, all file locks will fail"
708 * Internal wrapper for unlocking, to minimize the number of ifdef's
711 * Unlock an fd, prefer lockf() over flock()
713 int rad_unlockfd(int fd, int lock_len)
722 fl.l_whence = SEEK_CUR;
724 return fcntl(fd, F_UNLCK, (void *)&fl);
726 #error "missing definition for F_WRLCK, all file locks will fail"
733 * Return an interface-id in standard colon notation
735 char *ifid_ntoa(char *buffer, size_t size, uint8_t const *ifid)
737 snprintf(buffer, size, "%x:%x:%x:%x",
738 (ifid[0] << 8) + ifid[1], (ifid[2] << 8) + ifid[3],
739 (ifid[4] << 8) + ifid[5], (ifid[6] << 8) + ifid[7]);
745 * Return an interface-id from
746 * one supplied in standard colon notation.
748 uint8_t *ifid_aton(char const *ifid_str, uint8_t *ifid)
750 static char const xdigits[] = "0123456789abcdef";
752 int num_id = 0, val = 0, idx = 0;
754 for (p = ifid_str; ; ++p) {
755 if (*p == ':' || *p == '\0') {
760 * Drop 'val' into the array.
762 ifid[idx] = (val >> 8) & 0xff;
763 ifid[idx + 1] = val & 0xff;
766 * Must have all entries before
777 } else if ((pch = strchr(xdigits, tolower(*p))) != NULL) {
781 * Dumb version of 'scanf'
784 val |= (pch - xdigits);
792 #ifndef HAVE_INET_PTON
793 static int inet_pton4(char const *src, struct in_addr *dst)
799 static char const digits[] = "0123456789";
805 while (*p && ((off = strchr(digits, *p)) != NULL)) {
807 num += (off - digits);
809 if (num > 255) return 0;
816 * Not a digit, MUST be a dot, else we
828 * End of the string. At the fourth
829 * octet is OK, anything else is an
837 memcpy(dst, &tmp, sizeof(tmp));
842 #ifdef HAVE_STRUCT_SOCKADDR_IN6
843 /** Convert presentation level address to network order binary form
845 * @note Does not touch dst unless it's returning 1.
846 * @note :: in a full address is silently ignored.
847 * @note Inspired by Mark Andrews.
848 * @author Paul Vixie, 1996.
850 * @param src presentation level address.
851 * @param dst where to write output address.
852 * @return 1 if `src' is a valid [RFC1884 2.2] address, else 0.
854 static int inet_pton6(char const *src, unsigned char *dst)
856 static char const xdigits_l[] = "0123456789abcdef",
857 xdigits_u[] = "0123456789ABCDEF";
858 u_char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
859 char const *xdigits, *curtok;
863 memset((tp = tmp), 0, IN6ADDRSZ);
864 endp = tp + IN6ADDRSZ;
866 /* Leading :: requires some special handling. */
873 while ((ch = *src++) != '\0') {
876 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
877 pch = strchr((xdigits = xdigits_u), ch);
880 val |= (pch - xdigits);
894 if (tp + INT16SZ > endp)
896 *tp++ = (u_char) (val >> 8) & 0xff;
897 *tp++ = (u_char) val & 0xff;
902 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
903 inet_pton4(curtok, (struct in_addr *) tp) > 0) {
906 break; /* '\0' was seen by inet_pton4(). */
911 if (tp + INT16SZ > endp)
913 *tp++ = (u_char) (val >> 8) & 0xff;
914 *tp++ = (u_char) val & 0xff;
916 if (colonp != NULL) {
918 * Since some memmove()'s erroneously fail to handle
919 * overlapping regions, we'll do the shift by hand.
921 int const n = tp - colonp;
924 for (i = 1; i <= n; i++) {
925 endp[- i] = colonp[n - i];
932 /* bcopy(tmp, dst, IN6ADDRSZ); */
933 memcpy(dst, tmp, IN6ADDRSZ);
939 * Utility function, so that the rest of the server doesn't
940 * have ifdef's around IPv6 support
942 int inet_pton(int af, char const *src, void *dst)
945 return inet_pton4(src, dst);
947 #ifdef HAVE_STRUCT_SOCKADDR_IN6
949 if (af == AF_INET6) {
950 return inet_pton6(src, dst);
958 #ifndef HAVE_INET_NTOP
960 * Utility function, so that the rest of the server doesn't
961 * have ifdef's around IPv6 support
963 char const *inet_ntop(int af, void const *src, char *dst, size_t cnt)
966 uint8_t const *ipaddr = src;
968 if (cnt <= INET_ADDRSTRLEN) return NULL;
970 snprintf(dst, cnt, "%d.%d.%d.%d",
971 ipaddr[0], ipaddr[1],
972 ipaddr[2], ipaddr[3]);
977 * If the system doesn't define this, we define it
980 if (af == AF_INET6) {
981 struct in6_addr const *ipaddr = src;
983 if (cnt <= INET6_ADDRSTRLEN) return NULL;
985 snprintf(dst, cnt, "%x:%x:%x:%x:%x:%x:%x:%x",
986 (ipaddr->s6_addr[0] << 8) | ipaddr->s6_addr[1],
987 (ipaddr->s6_addr[2] << 8) | ipaddr->s6_addr[3],
988 (ipaddr->s6_addr[4] << 8) | ipaddr->s6_addr[5],
989 (ipaddr->s6_addr[6] << 8) | ipaddr->s6_addr[7],
990 (ipaddr->s6_addr[8] << 8) | ipaddr->s6_addr[9],
991 (ipaddr->s6_addr[10] << 8) | ipaddr->s6_addr[11],
992 (ipaddr->s6_addr[12] << 8) | ipaddr->s6_addr[13],
993 (ipaddr->s6_addr[14] << 8) | ipaddr->s6_addr[15]);
997 return NULL; /* don't support IPv6 */
1001 /** Wrappers for IPv4/IPv6 host to IP address lookup
1003 * This function returns only one IP address, of the specified address family,
1004 * or the first address (of whatever family), if AF_UNSPEC is used.
1006 * If fallback is specified and af is AF_INET, but no AF_INET records were
1007 * found and a record for AF_INET6 exists that record will be returned.
1009 * If fallback is specified and af is AF_INET6, and a record with AF_INET4 exists
1010 * that record will be returned instead.
1012 * @param out Where to write result.
1013 * @param af To search for in preference.
1014 * @param hostname to search for.
1015 * @param fallback to the other adress family, if no records matching af, found.
1016 * @return 0 on success, else -1 on failure.
1018 int ip_hton(fr_ipaddr_t *out, int af, char const *hostname, bool fallback)
1021 struct addrinfo hints, *ai = NULL, *alt = NULL, *res = NULL;
1024 * Avoid malloc for IP addresses. This helps us debug
1025 * memory errors when using talloc.
1030 if (!fr_hostname_lookups) {
1032 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1033 if (af == AF_UNSPEC) {
1036 for (p = hostname; *p != '\0'; p++) {
1047 if (af == AF_UNSPEC) af = AF_INET;
1049 if (!inet_pton(af, hostname, &(out->ipaddr))) return -1;
1055 memset(&hints, 0, sizeof(hints));
1058 * If we're falling back we need both IPv4 and IPv6 records
1061 hints.ai_family = AF_UNSPEC;
1063 hints.ai_family = af;
1066 if ((rcode = getaddrinfo(hostname, NULL, &hints, &res)) != 0) {
1070 fr_strerror_printf("Failed resolving \"%s\" to IP address: %s",
1071 hostname, gai_strerror(rcode));
1075 fr_strerror_printf("Failed resolving \"%s\" to IPv4 address: %s",
1076 hostname, gai_strerror(rcode));
1080 fr_strerror_printf("Failed resolving \"%s\" to IPv6 address: %s",
1081 hostname, gai_strerror(rcode));
1086 for (ai = res; ai; ai = ai->ai_next) {
1087 if ((af == ai->ai_family) || (af == AF_UNSPEC)) break;
1088 if (!alt && fallback && ((ai->ai_family == AF_INET) || (ai->ai_family == AF_INET6))) alt = ai;
1093 fr_strerror_printf("ip_hton failed to find requested information for host %.100s", hostname);
1098 rcode = fr_sockaddr2ipaddr((struct sockaddr_storage *)ai->ai_addr,
1099 ai->ai_addrlen, out, NULL);
1102 fr_strerror_printf("Failed converting sockaddr to ipaddr");
1110 * Look IP addresses up, and print names (depending on DNS config)
1112 char const *ip_ntoh(fr_ipaddr_t const *src, char *dst, size_t cnt)
1114 struct sockaddr_storage ss;
1121 if (!fr_dns_lookups) {
1122 return inet_ntop(src->af, &(src->ipaddr), dst, cnt);
1125 if (!fr_ipaddr2sockaddr(src, 0, &ss, &salen)) {
1129 if ((error = getnameinfo((struct sockaddr *)&ss, salen, dst, cnt, NULL, 0,
1130 NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
1131 fr_strerror_printf("ip_ntoh: %s", gai_strerror(error));
1137 /** Mask off a portion of an IPv4 address
1139 * @param ipaddr to mask.
1140 * @param prefix Number of contiguous bits to mask.
1141 * @return an ipv4 address with the host portion zeroed out.
1143 struct in_addr fr_inaddr_mask(struct in_addr const *ipaddr, uint8_t prefix)
1147 if (prefix > 32) prefix = 32;
1150 if (prefix == 32) return *ipaddr;
1152 if (prefix == 0) ret = 0;
1153 else ret = htonl(~((0x00000001UL << (32 - prefix)) - 1)) & ipaddr->s_addr;
1155 return (*(struct in_addr *)&ret);
1158 /** Mask off a portion of an IPv6 address
1160 * @param ipaddr to mask.
1161 * @param prefix Number of contiguous bits to mask.
1162 * @return an ipv6 address with the host portion zeroed out.
1164 struct in6_addr fr_in6addr_mask(struct in6_addr const *ipaddr, uint8_t prefix)
1166 uint64_t const *p = (uint64_t const *) ipaddr;
1167 uint64_t ret[2], *o = ret;
1169 if (prefix > 128) prefix = 128;
1172 if (prefix == 128) return *ipaddr;
1176 *o++ = 0xffffffffffffffffULL & *p++; /* lhs portion masked */
1178 ret[1] = 0; /* rhs portion zeroed */
1181 /* Max left shift is 63 else we get overflow */
1183 *o = htonll(~((uint64_t)(0x0000000000000001ULL << (64 - prefix)) - 1)) & *p;
1188 return *(struct in6_addr *) &ret;
1191 /** Zeroes out the host portion of an fr_ipaddr_t
1193 * @param[in,out] addr to mask
1194 * @param[in] prefix Length of the network portion.
1196 void fr_ipaddr_mask(fr_ipaddr_t *addr, uint8_t prefix)
1201 addr->ipaddr.ip4addr = fr_inaddr_mask(&addr->ipaddr.ip4addr, prefix);
1205 addr->ipaddr.ip6addr = fr_in6addr_mask(&addr->ipaddr.ip6addr, prefix);
1211 addr->prefix = prefix;
1214 static char const hextab[] = "0123456789abcdef";
1216 /** Convert hex strings to binary data
1218 * @param bin Buffer to write output to.
1219 * @param outlen length of output buffer (or length of input string / 2).
1220 * @param hex input string.
1221 * @param inlen length of the input string
1222 * @return length of data written to buffer.
1224 size_t fr_hex2bin(uint8_t *bin, size_t outlen, char const *hex, size_t inlen)
1231 * Smartly truncate output, caller should check number of bytes
1235 if (len > outlen) len = outlen;
1237 for (i = 0; i < len; i++) {
1238 if(!(c1 = memchr(hextab, tolower((int) hex[i << 1]), sizeof(hextab))) ||
1239 !(c2 = memchr(hextab, tolower((int) hex[(i << 1) + 1]), sizeof(hextab))))
1241 bin[i] = ((c1-hextab)<<4) + (c2-hextab);
1247 /** Convert binary data to a hex string
1249 * Ascii encoded hex string will not be prefixed with '0x'
1251 * @warning If the output buffer isn't long enough, we have a buffer overflow.
1253 * @param[out] hex Buffer to write hex output.
1254 * @param[in] bin input.
1255 * @param[in] inlen of bin input.
1256 * @return length of data written to buffer.
1258 size_t fr_bin2hex(char *hex, uint8_t const *bin, size_t inlen)
1262 for (i = 0; i < inlen; i++) {
1263 hex[0] = hextab[((*bin) >> 4) & 0x0f];
1264 hex[1] = hextab[*bin & 0x0f];
1273 /** Convert binary data to a hex string
1275 * Ascii encoded hex string will not be prefixed with '0x'
1277 * @param[in] ctx to alloc buffer in.
1278 * @param[in] bin input.
1279 * @param[in] inlen of bin input.
1280 * @return length of data written to buffer.
1282 char *fr_abin2hex(TALLOC_CTX *ctx, uint8_t const *bin, size_t inlen)
1286 buff = talloc_array(ctx, char, (inlen << 2));
1287 if (!buff) return NULL;
1289 fr_bin2hex(buff, bin, inlen);
1294 /** Consume the integer (or hex) portion of a value string
1296 * @param value string to parse.
1297 * @param end pointer to the first non numeric char.
1298 * @return integer value.
1300 uint32_t fr_strtoul(char const *value, char **end)
1302 if ((value[0] == '0') && (value[1] == 'x')) {
1303 return strtoul(value, end, 16);
1306 return strtoul(value, end, 10);
1309 /** Check whether the string is all whitespace
1311 * @return true if the entirety of the string is whitespace, else false.
1313 bool is_whitespace(char const *value)
1316 if (!isspace(*value)) return false;
1322 /** Check whether the string is made up of printable UTF8 chars
1324 * @param value to check.
1325 * @param len of value.
1328 * - true if the string is printable.
1329 * - false if the string contains non printable chars
1331 bool is_printable(void const *value, size_t len)
1333 uint8_t const *p = value;
1337 for (i = 0; i < len; i++) {
1338 clen = fr_utf8_char(p, len - i);
1339 if (clen == 0) return false;
1346 /** Check whether the string is all numbers
1348 * @return true if the entirety of the string is all numbers, else false.
1350 bool is_integer(char const *value)
1353 if (!isdigit(*value)) return false;
1359 /** Check whether the string is allzeros
1361 * @return true if the entirety of the string is all zeros, else false.
1363 bool is_zero(char const *value)
1366 if (*value != '0') return false;
1373 * So we don't have ifdef's in the rest of the code
1375 #ifndef HAVE_CLOSEFROM
1376 int closefrom(int fd)
1380 #ifdef HAVE_DIRENT_H
1385 if (fcntl(fd, F_CLOSEM) == 0) {
1391 maxfd = fcntl(fd, F_F_MAXFD);
1392 if (maxfd >= 0) goto do_close;
1396 maxfd = sysconf(_SC_OPEN_MAX);
1402 #ifdef HAVE_DIRENT_H
1404 * Use /proc/self/fd directory if it exists.
1406 dir = opendir("/proc/self/fd");
1412 while ((dp = readdir(dir)) != NULL) {
1413 my_fd = strtol(dp->d_name, &endp, 10);
1414 if (my_fd <= 0) continue;
1416 if ((endp > dp->d_name) && *endp) continue;
1418 if (my_fd == dirfd(dir)) continue;
1420 if ((my_fd >= fd) && (my_fd <= maxfd)) {
1421 (void) close((int) my_fd);
1424 (void) closedir(dir);
1433 if (fd > maxfd) return 0;
1436 * FIXME: return EINTR?
1438 for (i = fd; i < maxfd; i++) {
1446 int fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b)
1448 if (a->af < b->af) return -1;
1449 if (a->af > b->af) return +1;
1451 if (a->prefix < b->prefix) return -1;
1452 if (a->prefix > b->prefix) return +1;
1456 return memcmp(&a->ipaddr.ip4addr,
1458 sizeof(a->ipaddr.ip4addr));
1460 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1462 if (a->scope < b->scope) return -1;
1463 if (a->scope > b->scope) return +1;
1465 return memcmp(&a->ipaddr.ip6addr,
1467 sizeof(a->ipaddr.ip6addr));
1477 int fr_ipaddr2sockaddr(fr_ipaddr_t const *ipaddr, uint16_t port,
1478 struct sockaddr_storage *sa, socklen_t *salen)
1480 memset(sa, 0, sizeof(*sa));
1482 if (ipaddr->af == AF_INET) {
1483 struct sockaddr_in s4;
1485 *salen = sizeof(s4);
1487 memset(&s4, 0, sizeof(s4));
1488 s4.sin_family = AF_INET;
1489 s4.sin_addr = ipaddr->ipaddr.ip4addr;
1490 s4.sin_port = htons(port);
1491 memset(sa, 0, sizeof(*sa));
1492 memcpy(sa, &s4, sizeof(s4));
1494 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1495 } else if (ipaddr->af == AF_INET6) {
1496 struct sockaddr_in6 s6;
1498 *salen = sizeof(s6);
1500 memset(&s6, 0, sizeof(s6));
1501 s6.sin6_family = AF_INET6;
1502 s6.sin6_addr = ipaddr->ipaddr.ip6addr;
1503 s6.sin6_port = htons(port);
1504 s6.sin6_scope_id = ipaddr->scope;
1505 memset(sa, 0, sizeof(*sa));
1506 memcpy(sa, &s6, sizeof(s6));
1516 int fr_sockaddr2ipaddr(struct sockaddr_storage const *sa, socklen_t salen,
1517 fr_ipaddr_t *ipaddr, uint16_t *port)
1519 memset(ipaddr, 0, sizeof(*ipaddr));
1521 if (sa->ss_family == AF_INET) {
1522 struct sockaddr_in s4;
1524 if (salen < sizeof(s4)) {
1525 fr_strerror_printf("IPv4 address is too small");
1529 memcpy(&s4, sa, sizeof(s4));
1530 ipaddr->af = AF_INET;
1531 ipaddr->prefix = 32;
1532 ipaddr->ipaddr.ip4addr = s4.sin_addr;
1533 if (port) *port = ntohs(s4.sin_port);
1535 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1536 } else if (sa->ss_family == AF_INET6) {
1537 struct sockaddr_in6 s6;
1539 if (salen < sizeof(s6)) {
1540 fr_strerror_printf("IPv6 address is too small");
1544 memcpy(&s6, sa, sizeof(s6));
1545 ipaddr->af = AF_INET6;
1546 ipaddr->prefix = 128;
1547 ipaddr->ipaddr.ip6addr = s6.sin6_addr;
1548 if (port) *port = ntohs(s6.sin6_port);
1549 ipaddr->scope = s6.sin6_scope_id;
1553 fr_strerror_printf("Unsupported address famility %d",
1562 /** Set O_NONBLOCK on a socket
1564 * @note O_NONBLOCK is POSIX.
1566 * @param fd to set nonblocking flag on.
1567 * @return flags set on the socket, or -1 on error.
1569 int fr_nonblock(int fd)
1573 flags = fcntl(fd, F_GETFL, NULL);
1575 fr_strerror_printf("Failure getting socket flags: %s", fr_syserror(errno));
1579 flags |= O_NONBLOCK;
1580 if (fcntl(fd, F_SETFL, flags) < 0) {
1581 fr_strerror_printf("Failure setting socket flags: %s", fr_syserror(errno));
1588 /** Unset O_NONBLOCK on a socket
1590 * @note O_NONBLOCK is POSIX.
1592 * @param fd to set nonblocking flag on.
1593 * @return flags set on the socket, or -1 on error.
1595 int fr_blocking(int fd)
1599 flags = fcntl(fd, F_GETFL, NULL);
1601 fr_strerror_printf("Failure getting socket flags: %s", fr_syserror(errno));
1605 flags ^= O_NONBLOCK;
1606 if (fcntl(fd, F_SETFL, flags) < 0) {
1607 fr_strerror_printf("Failure setting socket flags: %s", fr_syserror(errno));
1614 int fr_nonblock(UNUSED int fd)
1616 fr_strerror_printf("Non blocking sockets are not supported");
1619 int fr_blocking(UNUSED int fd)
1621 fr_strerror_printf("Non blocking sockets are not supported");
1626 /** Write out a vector to a file descriptor
1628 * Wraps writev, calling it as necessary. If timeout is not NULL,
1629 * timeout is applied to each call that returns EAGAIN or EWOULDBLOCK
1631 * @note Should only be used on nonblocking file descriptors.
1632 * @note Socket should likely be closed on timeout.
1633 * @note iovec may be modified in such a way that it's not re-usable.
1634 * @note Leaves errno set to the last error that ocurred.
1636 * @param fd to write to.
1637 * @param vector to write.
1638 * @param iovcnt number of elements in iovec.
1639 * @param timeout how long to wait for fd to become writeable before timing out.
1640 * @return number of bytes written, -1 on error.
1642 ssize_t fr_writev(int fd, struct iovec vector[], int iovcnt, struct timeval *timeout)
1644 struct iovec *vector_p = vector;
1647 while (iovcnt > 0) {
1650 wrote = writev(fd, vector_p, iovcnt);
1655 * An entire vector element was written
1657 if (wrote >= (ssize_t)vector_p->iov_len) {
1659 wrote -= vector_p->iov_len;
1665 * Partial vector element was written
1667 vector_p->iov_len -= wrote;
1668 vector_p->iov_base = ((char *)vector_p->iov_base) + wrote;
1672 } else if (wrote == 0) return total;
1675 /* Write operation would block, use select() to implement a timeout */
1676 #if EWOULDBLOCK != EAGAIN
1686 FD_ZERO(&write_set);
1687 FD_SET(fd, &write_set);
1689 /* Don't let signals mess up the select */
1691 ret = select(fd + 1, NULL, &write_set, NULL, timeout);
1692 } while ((ret == -1) && (errno == EINTR));
1694 /* Select returned 0 which means it reached the timeout */
1696 fr_strerror_printf("Write timed out");
1700 /* Other select error */
1702 fr_strerror_printf("Failed waiting on socket: %s", fr_syserror(errno));
1706 /* select said a file descriptor was ready for writing */
1707 if (!fr_assert(FD_ISSET(fd, &write_set))) return -1;
1720 /** Convert UTF8 string to UCS2 encoding
1722 * @note Borrowed from src/crypto/ms_funcs.c of wpa_supplicant project (http://hostap.epitest.fi/wpa_supplicant/)
1724 * @param[out] out Where to write the ucs2 string.
1725 * @param[in] outlen Size of output buffer.
1726 * @param[in] in UTF8 string to convert.
1727 * @param[in] inlen length of UTF8 string.
1728 * @return the size of the UCS2 string written to the output buffer (in bytes).
1730 ssize_t fr_utf8_to_ucs2(uint8_t *out, size_t outlen, char const *in, size_t inlen)
1733 uint8_t *start = out;
1735 for (i = 0; i < inlen; i++) {
1739 if ((size_t)(out - start) >= outlen) {
1740 /* input too long */
1744 /* One-byte encoding */
1746 FR_PUT_LE16(out, c);
1749 } else if ((i == (inlen - 1)) || ((size_t)(out - start) >= (outlen - 1))) {
1750 /* Incomplete surrogate */
1755 /* Two-byte encoding */
1756 if ((c & 0xe0) == 0xc0) {
1757 FR_PUT_LE16(out, ((c & 0x1f) << 6) | (c2 & 0x3f));
1761 if ((i == inlen) || ((size_t)(out - start) >= (outlen - 1))) {
1762 /* Incomplete surrogate */
1766 /* Three-byte encoding */
1768 FR_PUT_LE16(out, ((c & 0xf) << 12) | ((c2 & 0x3f) << 6) | (c3 & 0x3f));
1775 /** Write 128bit unsigned integer to buffer
1777 * @author Alexey Frunze
1779 * @param out where to write result to.
1780 * @param outlen size of out.
1781 * @param num 128 bit integer.
1783 size_t fr_prints_uint128(char *out, size_t outlen, uint128_t const num)
1785 char buff[128 / 3 + 1 + 1];
1789 #ifdef FR_LITTLE_ENDIAN
1797 memset(buff, '0', sizeof(buff) - 1);
1798 buff[sizeof(buff) - 1] = '\0';
1800 memcpy(n, &num, sizeof(n));
1802 for (i = 0; i < 128; i++) {
1806 carry = (n[h] >= 0x8000000000000000);
1808 // Shift n[] left, doubling it
1809 n[h] = ((n[h] << 1) & 0xffffffffffffffff) + (n[l] >= 0x8000000000000000);
1810 n[l] = ((n[l] << 1) & 0xffffffffffffffff);
1812 // Add s[] to itself in decimal, doubling it
1813 for (j = sizeof(buff) - 2; j >= 0; j--) {
1814 buff[j] += buff[j] - '0' + carry;
1815 carry = (buff[j] > '9');
1822 while ((*p == '0') && (p < &buff[sizeof(buff) - 2])) {
1826 return strlcpy(out, p, outlen);
1830 * Sort of strtok/strsep function.
1832 static char *mystrtok(char **ptr, char const *sep)
1840 while (**ptr && strchr(sep, **ptr)) {
1848 while (**ptr && strchr(sep, **ptr) == NULL) {
1858 /** Convert string in various formats to a time_t
1860 * @param date_str input date string.
1861 * @param date time_t to write result to.
1862 * @return 0 on success or -1 on error.
1864 int fr_get_time(char const *date_str, time_t *date)
1868 struct tm *tm, s_tm;
1875 * Test for unix timestamp date
1877 *date = strtoul(date_str, &tail, 10);
1878 if (*tail == '\0') {
1883 memset(tm, 0, sizeof(*tm));
1884 tm->tm_isdst = -1; /* don't know, and don't care about DST */
1886 strlcpy(buf, date_str, sizeof(buf));
1889 f[0] = mystrtok(&p, " \t");
1890 f[1] = mystrtok(&p, " \t");
1891 f[2] = mystrtok(&p, " \t");
1892 f[3] = mystrtok(&p, " \t"); /* may, or may not, be present */
1893 if (!f[0] || !f[1] || !f[2]) return -1;
1896 * The time has a colon, where nothing else does.
1897 * So if we find it, bubble it to the back of the list.
1900 for (i = 0; i < 3; i++) {
1901 if (strchr(f[i], ':')) {
1911 * The month is text, which allows us to find it easily.
1914 for (i = 0; i < 3; i++) {
1915 if (isalpha( (int) *f[i])) {
1917 * Bubble the month to the front of the list
1923 for (i = 0; i < 12; i++) {
1924 if (strncasecmp(months[i], f[0], 3) == 0) {
1932 /* month not found? */
1933 if (tm->tm_mon == 12) return -1;
1936 * The year may be in f[1], or in f[2]
1938 tm->tm_year = atoi(f[1]);
1939 tm->tm_mday = atoi(f[2]);
1941 if (tm->tm_year >= 1900) {
1942 tm->tm_year -= 1900;
1946 * We can't use 2-digit years any more, they make it
1947 * impossible to tell what's the day, and what's the year.
1949 if (tm->tm_mday < 1900) return -1;
1952 * Swap the year and the day.
1955 tm->tm_year = tm->tm_mday - 1900;
1960 * If the day is out of range, die.
1962 if ((tm->tm_mday < 1) || (tm->tm_mday > 31)) {
1967 * There may be %H:%M:%S. Parse it in a hacky way.
1970 f[0] = f[3]; /* HH */
1971 f[1] = strchr(f[0], ':'); /* find : separator */
1972 if (!f[1]) return -1;
1974 *(f[1]++) = '\0'; /* nuke it, and point to MM:SS */
1976 f[2] = strchr(f[1], ':'); /* find : separator */
1978 *(f[2]++) = '\0'; /* nuke it, and point to SS */
1979 tm->tm_sec = atoi(f[2]);
1980 } /* else leave it as zero */
1982 tm->tm_hour = atoi(f[0]);
1983 tm->tm_min = atoi(f[1]);
1987 * Returns -1 on error.
1990 if (t == (time_t) -1) return -1;
1997 /** Compares two pointers
1999 * @param a first pointer to compare.
2000 * @param b second pointer to compare.
2001 * @return -1 if a < b, +1 if b > a, or 0 if both equal.
2003 int8_t fr_pointer_cmp(void const *a, void const *b)
2005 if (a < b) return -1;
2006 if (a == b) return 0;
2011 static int _quick_partition(void const *to_sort[], int min, int max, fr_cmp_t cmp) {
2012 void const *pivot = to_sort[min];
2018 do ++i; while((cmp(to_sort[i], pivot) <= 0) && i <= max);
2019 do --j; while(cmp(to_sort[j], pivot) > 0);
2024 to_sort[i] = to_sort[j];
2029 to_sort[min] = to_sort[j];
2035 /** Quick sort an array of pointers using a comparator
2037 * @param to_sort array of pointers to sort.
2038 * @param min_idx the lowest index (usually 0).
2039 * @param max_idx the highest index (usually length of array - 1).
2040 * @param cmp the comparison function to use to sort the array elements.
2042 void fr_quick_sort(void const *to_sort[], int min_idx, int max_idx, fr_cmp_t cmp)
2046 if (min_idx >= max_idx) return;
2048 part = _quick_partition(to_sort, min_idx, max_idx, cmp);
2049 fr_quick_sort(to_sort, min_idx, part - 1, cmp);
2050 fr_quick_sort(to_sort, part + 1, max_idx, cmp);
2054 void fr_talloc_verify_cb(UNUSED const void *ptr, UNUSED int depth,
2055 UNUSED int max_depth, UNUSED int is_ref,
2056 UNUSED void *private_data)