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>
38 * Some versions of Linux don't have closefrom(), but they will
41 * BSD systems will generally have closefrom(), but not proc.
43 * OSX doesn't have closefrom() or /proc/self/fd, but it does
47 #define CLOSEFROM_DIR "/proc/self/fd"
48 #elif defined(__APPLE__)
49 #define CLOSEFROM_DIR "/dev/fd"
56 #define FR_PUT_LE16(a, val)\
58 a[1] = ((uint16_t) (val)) >> 8;\
59 a[0] = ((uint16_t) (val)) & 0xff;\
62 bool fr_dns_lookups = false; /* IP -> hostname lookups? */
63 bool fr_hostname_lookups = true; /* hostname -> IP lookups? */
66 static char const *months[] = {
67 "jan", "feb", "mar", "apr", "may", "jun",
68 "jul", "aug", "sep", "oct", "nov", "dec" };
70 fr_thread_local_setup(char *, fr_inet_ntop_buffer) /* macro */
72 typedef struct fr_talloc_link {
77 /** Sets a signal handler using sigaction if available, else signal
79 * @param sig to set handler for.
80 * @param func handler to set.
82 int fr_set_signal(int sig, sig_t func)
87 memset(&act, 0, sizeof(act));
89 sigemptyset(&act.sa_mask);
90 act.sa_handler = func;
92 if (sigaction(sig, &act, NULL) < 0) {
93 fr_strerror_printf("Failed setting signal %i handler via sigaction(): %s", sig, fr_syserror(errno));
97 if (signal(sig, func) < 0) {
98 fr_strerror_printf("Failed setting signal %i handler via signal(): %s", sig, fr_syserror(errno));
105 /** Uninstall a signal for a specific handler
107 * man sigaction says these are fine to call from a signal handler.
111 int fr_unset_signal(int sig)
113 #ifdef HAVE_SIGACTION
114 struct sigaction act;
116 memset(&act, 0, sizeof(act));
118 sigemptyset(&act.sa_mask);
119 act.sa_handler = SIG_DFL;
121 return sigaction(sig, &act, NULL);
123 return signal(sig, SIG_DFL);
127 static int _fr_trigger_talloc_ctx_free(fr_talloc_link_t *trigger)
129 if (trigger->armed) talloc_free(trigger->child);
134 static int _fr_disarm_talloc_ctx_free(bool **armed)
140 /** Link a parent and a child context, so the child is freed before the parent
142 * @note This is not thread safe. Do not free parent before threads are joined, do not call from a child thread.
143 * @note It's OK to free the child before threads are joined, but this will leak memory until the parent is freed.
145 * @param parent who's fate the child should share.
146 * @param child bound to parent's lifecycle.
147 * @return 0 on success -1 on failure.
149 int fr_link_talloc_ctx_free(TALLOC_CTX *parent, TALLOC_CTX *child)
151 fr_talloc_link_t *trigger;
154 trigger = talloc(parent, fr_talloc_link_t);
155 if (!trigger) return -1;
157 disarm = talloc(child, bool *);
159 talloc_free(trigger);
163 trigger->child = child;
164 trigger->armed = true;
165 *disarm = &trigger->armed;
167 talloc_set_destructor(trigger, _fr_trigger_talloc_ctx_free);
168 talloc_set_destructor(disarm, _fr_disarm_talloc_ctx_free);
174 * Explicitly cleanup the memory allocated to the error inet_ntop
177 static void _fr_inet_ntop_free(void *arg)
182 /** Wrapper around inet_ntop, prints IPv4/IPv6 addresses
184 * inet_ntop requires the caller pass in a buffer for the address.
185 * This would be annoying and cumbersome, seeing as quite often the ASCII
186 * address is only used for logging output.
188 * So as with lib/log.c use TLS to allocate thread specific buffers, and
189 * write the IP address there instead.
191 * @param af address family, either AF_INET or AF_INET6.
192 * @param src pointer to network address structure.
193 * @return NULL on error, else pointer to ASCII buffer containing text version of address.
195 char const *fr_inet_ntop(int af, void const *src)
203 buffer = fr_thread_local_init(fr_inet_ntop_buffer, _fr_inet_ntop_free);
208 * malloc is thread safe, talloc is not
210 buffer = malloc(sizeof(char) * INET6_ADDRSTRLEN);
212 fr_perror("Failed allocating memory for inet_ntop buffer");
216 ret = fr_thread_local_set(fr_inet_ntop_buffer, buffer);
218 fr_perror("Failed setting up TLS for inet_ntop buffer: %s", fr_syserror(ret));
225 return inet_ntop(af, src, buffer, INET6_ADDRSTRLEN);
229 * Return an IP address in standard dot notation
233 char const *ip_ntoa(char *buffer, uint32_t ipaddr)
235 ipaddr = ntohl(ipaddr);
237 sprintf(buffer, "%d.%d.%d.%d",
238 (ipaddr >> 24) & 0xff,
239 (ipaddr >> 16) & 0xff,
240 (ipaddr >> 8) & 0xff,
246 * Parse decimal digits until we run out of decimal digits.
248 static int ip_octet_from_str(char const *str, uint32_t *poctet)
253 if ((*p < '0') || (*p > '9')) {
259 while ((*p >= '0') && (*p <= '9')) {
264 if (octet > 255) return -1;
272 static int ip_prefix_from_str(char const *str, uint32_t *paddr)
281 for (shift = 24; shift >= 0; shift -= 8) {
282 length = ip_octet_from_str(p, &octet);
283 if (length <= 0) return -1;
285 addr |= octet << shift;
289 * EOS or / means we're done.
291 if (!*p || (*p == '/')) break;
294 * We require dots between octets.
296 if (*p != '.') return -1;
300 *paddr = htonl(addr);
306 * Parse an IPv4 address, IPv4 prefix in presentation format (and others), or
309 * @param out Where to write the ip address value.
310 * @param value to parse, may be dotted quad [+ prefix], or integer, or octal number, or '*' (INADDR_ANY), or a hostname.
311 * @param inlen Length of value, if value is \0 terminated inlen may be -1.
312 * @param resolve If true and value doesn't look like an IP address, try and resolve value as a hostname.
313 * @param fallback to IPv6 resolution if no A records can be found.
314 * @return 0 if ip address was parsed successfully, else -1 on error.
316 int fr_pton4(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback)
322 /* Dotted quad + / + [0-9]{1,2} or a hostname (RFC1035 2.3.4 Size limits) */
326 * Copy to intermediary buffer if we were given a length
329 if (inlen >= (ssize_t)sizeof(buffer)) {
330 fr_strerror_printf("Invalid IPv4 address string \"%s\"", value);
333 memcpy(buffer, value, inlen);
334 buffer[inlen] = '\0';
338 p = strchr(value, '/');
341 * 192.0.2.2 is parsed as if it was /32
348 * Allow '*' as the wildcard address usually 0.0.0.0
350 if ((value[0] == '*') && (value[1] == '\0')) {
351 out->ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
354 * Convert things which are obviously integers to IP addresses
356 * We assume the number is the bigendian representation of the
359 } else if (is_integer(value) || ((value[0] == '0') && (value[1] == 'x'))) {
360 out->ipaddr.ip4addr.s_addr = htonl(strtoul(value, NULL, 0));
362 } else if (!resolve) {
363 if (inet_pton(AF_INET, value, &out->ipaddr.ip4addr.s_addr) <= 0) {
364 fr_strerror_printf("Failed to parse IPv4 addreess string \"%s\"", value);
367 } else if (ip_hton(out, AF_INET, value, fallback) < 0) return -1;
373 * Copy the IP portion into a temporary buffer if we haven't already.
375 if (inlen < 0) memcpy(buffer, value, p - value);
376 buffer[p - value] = '\0';
378 if (ip_prefix_from_str(buffer, &out->ipaddr.ip4addr.s_addr) <= 0) {
379 fr_strerror_printf("Failed to parse IPv4 address string \"%s\"", value);
383 mask = strtoul(p + 1, &eptr, 10);
385 fr_strerror_printf("Invalid IPv4 mask length \"%s\". Should be between 0-32", p);
389 if (eptr[0] != '\0') {
390 fr_strerror_printf("Failed to parse IPv4 address string \"%s\", "
391 "got garbage after mask length \"%s\"", value, eptr);
396 out->ipaddr.ip4addr = fr_inaddr_mask(&out->ipaddr.ip4addr, mask);
399 out->prefix = (uint8_t) mask;
406 * Parse an IPv6 address or IPv6 prefix in presentation format (and others),
409 * @param out Where to write the ip address value.
410 * @param value to parse.
411 * @param inlen Length of value, if value is \0 terminated inlen may be -1.
412 * @param resolve If true and value doesn't look like an IP address, try and resolve value as a hostname.
413 * @param fallback to IPv4 resolution if no AAAA records can be found.
414 * @return 0 if ip address was parsed successfully, else -1 on error.
416 int fr_pton6(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback)
422 /* IPv6 + / + [0-9]{1,3} or a hostname (RFC1035 2.3.4 Size limits) */
426 * Copy to intermediary buffer if we were given a length
429 if (inlen >= (ssize_t)sizeof(buffer)) {
430 fr_strerror_printf("Invalid IPv6 address string \"%s\"", value);
433 memcpy(buffer, value, inlen);
434 buffer[inlen] = '\0';
438 p = strchr(value, '/');
444 * Allow '*' as the wildcard address
446 if ((value[0] == '*') && (value[1] == '\0')) {
447 memset(out->ipaddr.ip6addr.s6_addr, 0, sizeof(out->ipaddr.ip6addr.s6_addr));
448 } else if (!resolve) {
449 if (inet_pton(AF_INET6, value, out->ipaddr.ip6addr.s6_addr) <= 0) {
450 fr_strerror_printf("Failed to parse IPv6 address string \"%s\"", value);
453 } else if (ip_hton(out, AF_INET6, value, fallback) < 0) return -1;
458 if ((p - value) >= INET6_ADDRSTRLEN) {
459 fr_strerror_printf("Invalid IPv6 address string \"%s\"", value);
464 * Copy string to temporary buffer if we didn't do it earlier
466 if (inlen < 0) memcpy(buffer, value, p - value);
467 buffer[p - value] = '\0';
470 if (inet_pton(AF_INET6, buffer, out->ipaddr.ip6addr.s6_addr) <= 0) {
471 fr_strerror_printf("Failed to parse IPv6 address string \"%s\"", value);
474 } else if (ip_hton(out, AF_INET6, buffer, fallback) < 0) return -1;
476 prefix = strtoul(p + 1, &eptr, 10);
478 fr_strerror_printf("Invalid IPv6 mask length \"%s\". Should be between 0-128", p);
481 if (eptr[0] != '\0') {
482 fr_strerror_printf("Failed to parse IPv6 address string \"%s\", "
483 "got garbage after mask length \"%s\"", value, eptr);
488 struct in6_addr addr;
490 addr = fr_in6addr_mask(&out->ipaddr.ip6addr, prefix);
491 memcpy(out->ipaddr.ip6addr.s6_addr, addr.s6_addr, sizeof(out->ipaddr.ip6addr.s6_addr));
494 out->prefix = (uint8_t) prefix;
500 /** Simple wrapper to decide whether an IP value is v4 or v6 and call the appropriate parser.
502 * @param[out] out Where to write the ip address value.
503 * @param[in] value to parse.
504 * @param[in] inlen Length of value, if value is \0 terminated inlen may be -1.
505 * @param[in] resolve If true and value doesn't look like an IP address, try and resolve value as a
507 * @param[in] af If the address type is not obvious from the format, and resolve is true, the DNS
508 * record (A or AAAA) we require. Also controls which parser we pass the address to if
509 * we have no idea what it is.
511 * - 0 if ip address was parsed successfully.
514 int fr_pton(fr_ipaddr_t *out, char const *value, ssize_t inlen, int af, bool resolve)
518 len = (inlen >= 0) ? (size_t)inlen : strlen(value);
519 for (i = 0; i < len; i++) switch (value[i]) {
521 * ':' is illegal in domain names and IPv4 addresses.
522 * Must be v6 and cannot be a domain.
525 return fr_pton6(out, value, inlen, false, false);
528 * Chars which don't really tell us anything
536 * Outside the range of IPv4 chars, must be a domain
537 * Use A record in preference to AAAA record.
539 if ((value[i] < '0') || (value[i] > '9')) {
541 fr_strerror_printf("Not IPv4/6 address, and asked not to resolve");
546 return fr_pton4(out, value, inlen, resolve, true);
549 return fr_pton4(out, value, inlen, resolve, false);
552 return fr_pton6(out, value, inlen, resolve, false);
555 fr_strerror_printf("Invalid address family %i", af);
563 * All chars were in the IPv4 set [0-9/.], must be an IPv4
566 return fr_pton4(out, value, inlen, false, false);
569 /** Parses IPv4/6 address + port, to fr_ipaddr_t and integer
571 * @param[out] out Where to write the ip address value.
572 * @param[out] port_out Where to write the port (0 if no port found).
573 * @param[in] value to parse.
574 * @param[in] inlen Length of value, if value is \0 terminated inlen may be -1.
575 * @param[in] af If the address type is not obvious from the format, and resolve is true, the DNS
576 * record (A or AAAA) we require. Also controls which parser we pass the address to if
577 * we have no idea what it is.
578 * @param[in] resolve If true and value doesn't look like an IP address, try and resolve value as a
581 int fr_pton_port(fr_ipaddr_t *out, uint16_t *port_out, char const *value, ssize_t inlen, int af, bool resolve)
583 char const *p = value, *q;
591 len = (inlen >= 0) ? (size_t)inlen : strlen(value);
594 if (!(q = memchr(p + 1, ']', len - 1))) {
595 fr_strerror_printf("Missing closing ']' for IPv6 address");
600 * inet_pton doesn't like the address being wrapped in []
602 if (fr_pton6(out, p + 1, (q - p) - 1, false, false) < 0) return -1;
613 * Host, IPv4 or IPv6 with no port
615 q = memchr(p, ':', len);
616 if (!q) return fr_pton(out, p, len, af, resolve);
619 * IPv4 or host, with port
621 if (fr_pton(out, p, (q - p), af, resolve) < 0) return -1;
625 * Valid ports are a maximum of 5 digits, so if the
626 * input length indicates there are more than 5 chars
627 * after the ':' then there's an issue.
629 if (len > (size_t) ((q + sizeof(buffer)) - value)) {
631 fr_strerror_printf("IP string contains trailing garbage after port delimiter");
635 p = q + 1; /* Move to first digit */
637 strlcpy(buffer, p, (len - (p - value)) + 1);
638 port = strtoul(buffer, &end, 10);
639 if (*end != '\0') goto error; /* Trailing garbage after integer */
641 if ((port > UINT16_MAX) || (port == 0)) {
642 fr_strerror_printf("Port %lu outside valid port range 1-" STRINGIFY(UINT16_MAX), port);
650 int fr_ntop(char *out, size_t outlen, fr_ipaddr_t *addr)
652 char buffer[INET6_ADDRSTRLEN];
654 if (inet_ntop(addr->af, &(addr->ipaddr), buffer, sizeof(buffer)) == NULL) return -1;
656 return snprintf(out, outlen, "%s/%i", buffer, addr->prefix);
660 * cppcheck apparently can't pick this up from the system headers.
667 * Internal wrapper for locking, to minimize the number of ifdef's
671 int rad_lockfd(int fd, int lock_len)
680 fl.l_whence = SEEK_CUR;
682 return fcntl(fd, F_SETLKW, (void *)&fl);
684 #error "missing definition for F_WRLCK, all file locks will fail"
691 * Internal wrapper for locking, to minimize the number of ifdef's
693 * Lock an fd, prefer lockf() over flock()
694 * Nonblocking version.
696 int rad_lockfd_nonblock(int fd, int lock_len)
705 fl.l_whence = SEEK_CUR;
707 return fcntl(fd, F_SETLK, (void *)&fl);
709 #error "missing definition for F_WRLCK, all file locks will fail"
716 * Internal wrapper for unlocking, to minimize the number of ifdef's
719 * Unlock an fd, prefer lockf() over flock()
721 int rad_unlockfd(int fd, int lock_len)
730 fl.l_whence = SEEK_CUR;
732 return fcntl(fd, F_UNLCK, (void *)&fl);
734 #error "missing definition for F_WRLCK, all file locks will fail"
741 * Return an interface-id in standard colon notation
743 char *ifid_ntoa(char *buffer, size_t size, uint8_t const *ifid)
745 snprintf(buffer, size, "%x:%x:%x:%x",
746 (ifid[0] << 8) + ifid[1], (ifid[2] << 8) + ifid[3],
747 (ifid[4] << 8) + ifid[5], (ifid[6] << 8) + ifid[7]);
753 * Return an interface-id from
754 * one supplied in standard colon notation.
756 uint8_t *ifid_aton(char const *ifid_str, uint8_t *ifid)
758 static char const xdigits[] = "0123456789abcdef";
760 int num_id = 0, val = 0, idx = 0;
762 for (p = ifid_str; ; ++p) {
763 if (*p == ':' || *p == '\0') {
768 * Drop 'val' into the array.
770 ifid[idx] = (val >> 8) & 0xff;
771 ifid[idx + 1] = val & 0xff;
774 * Must have all entries before
785 } else if ((pch = strchr(xdigits, tolower(*p))) != NULL) {
789 * Dumb version of 'scanf'
792 val |= (pch - xdigits);
800 #ifndef HAVE_INET_PTON
801 static int inet_pton4(char const *src, struct in_addr *dst)
807 static char const digits[] = "0123456789";
813 while (*p && ((off = strchr(digits, *p)) != NULL)) {
815 num += (off - digits);
817 if (num > 255) return 0;
824 * Not a digit, MUST be a dot, else we
836 * End of the string. At the fourth
837 * octet is OK, anything else is an
845 memcpy(dst, &tmp, sizeof(tmp));
850 #ifdef HAVE_STRUCT_SOCKADDR_IN6
851 /** Convert presentation level address to network order binary form
853 * @note Does not touch dst unless it's returning 1.
854 * @note :: in a full address is silently ignored.
855 * @note Inspired by Mark Andrews.
856 * @author Paul Vixie, 1996.
858 * @param src presentation level address.
859 * @param dst where to write output address.
860 * @return 1 if `src' is a valid [RFC1884 2.2] address, else 0.
862 static int inet_pton6(char const *src, unsigned char *dst)
864 static char const xdigits_l[] = "0123456789abcdef",
865 xdigits_u[] = "0123456789ABCDEF";
866 u_char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
867 char const *xdigits, *curtok;
871 memset((tp = tmp), 0, IN6ADDRSZ);
872 endp = tp + IN6ADDRSZ;
874 /* Leading :: requires some special handling. */
881 while ((ch = *src++) != '\0') {
884 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
885 pch = strchr((xdigits = xdigits_u), ch);
888 val |= (pch - xdigits);
902 if (tp + INT16SZ > endp)
904 *tp++ = (u_char) (val >> 8) & 0xff;
905 *tp++ = (u_char) val & 0xff;
910 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
911 inet_pton4(curtok, (struct in_addr *) tp) > 0) {
914 break; /* '\0' was seen by inet_pton4(). */
919 if (tp + INT16SZ > endp)
921 *tp++ = (u_char) (val >> 8) & 0xff;
922 *tp++ = (u_char) val & 0xff;
924 if (colonp != NULL) {
926 * Since some memmove()'s erroneously fail to handle
927 * overlapping regions, we'll do the shift by hand.
929 int const n = tp - colonp;
932 for (i = 1; i <= n; i++) {
933 endp[- i] = colonp[n - i];
940 /* bcopy(tmp, dst, IN6ADDRSZ); */
941 memcpy(dst, tmp, IN6ADDRSZ);
947 * Utility function, so that the rest of the server doesn't
948 * have ifdef's around IPv6 support
950 int inet_pton(int af, char const *src, void *dst)
953 return inet_pton4(src, dst);
955 #ifdef HAVE_STRUCT_SOCKADDR_IN6
957 if (af == AF_INET6) {
958 return inet_pton6(src, dst);
966 #ifndef HAVE_INET_NTOP
968 * Utility function, so that the rest of the server doesn't
969 * have ifdef's around IPv6 support
971 char const *inet_ntop(int af, void const *src, char *dst, size_t cnt)
974 uint8_t const *ipaddr = src;
976 if (cnt <= INET_ADDRSTRLEN) return NULL;
978 snprintf(dst, cnt, "%d.%d.%d.%d",
979 ipaddr[0], ipaddr[1],
980 ipaddr[2], ipaddr[3]);
985 * If the system doesn't define this, we define it
988 if (af == AF_INET6) {
989 struct in6_addr const *ipaddr = src;
991 if (cnt <= INET6_ADDRSTRLEN) return NULL;
993 snprintf(dst, cnt, "%x:%x:%x:%x:%x:%x:%x:%x",
994 (ipaddr->s6_addr[0] << 8) | ipaddr->s6_addr[1],
995 (ipaddr->s6_addr[2] << 8) | ipaddr->s6_addr[3],
996 (ipaddr->s6_addr[4] << 8) | ipaddr->s6_addr[5],
997 (ipaddr->s6_addr[6] << 8) | ipaddr->s6_addr[7],
998 (ipaddr->s6_addr[8] << 8) | ipaddr->s6_addr[9],
999 (ipaddr->s6_addr[10] << 8) | ipaddr->s6_addr[11],
1000 (ipaddr->s6_addr[12] << 8) | ipaddr->s6_addr[13],
1001 (ipaddr->s6_addr[14] << 8) | ipaddr->s6_addr[15]);
1005 return NULL; /* don't support IPv6 */
1009 /** Wrappers for IPv4/IPv6 host to IP address lookup
1011 * This function returns only one IP address, of the specified address family,
1012 * or the first address (of whatever family), if AF_UNSPEC is used.
1014 * If fallback is specified and af is AF_INET, but no AF_INET records were
1015 * found and a record for AF_INET6 exists that record will be returned.
1017 * If fallback is specified and af is AF_INET6, and a record with AF_INET4 exists
1018 * that record will be returned instead.
1020 * @param out Where to write result.
1021 * @param af To search for in preference.
1022 * @param hostname to search for.
1023 * @param fallback to the other adress family, if no records matching af, found.
1024 * @return 0 on success, else -1 on failure.
1026 int ip_hton(fr_ipaddr_t *out, int af, char const *hostname, bool fallback)
1029 struct addrinfo hints, *ai = NULL, *alt = NULL, *res = NULL;
1032 * Avoid malloc for IP addresses. This helps us debug
1033 * memory errors when using talloc.
1038 if (!fr_hostname_lookups) {
1040 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1041 if (af == AF_UNSPEC) {
1044 for (p = hostname; *p != '\0'; p++) {
1055 if (af == AF_UNSPEC) af = AF_INET;
1057 if (!inet_pton(af, hostname, &(out->ipaddr))) return -1;
1063 memset(&hints, 0, sizeof(hints));
1066 * If we're falling back we need both IPv4 and IPv6 records
1069 hints.ai_family = AF_UNSPEC;
1071 hints.ai_family = af;
1074 if ((rcode = getaddrinfo(hostname, NULL, &hints, &res)) != 0) {
1078 fr_strerror_printf("Failed resolving \"%s\" to IP address: %s",
1079 hostname, gai_strerror(rcode));
1083 fr_strerror_printf("Failed resolving \"%s\" to IPv4 address: %s",
1084 hostname, gai_strerror(rcode));
1088 fr_strerror_printf("Failed resolving \"%s\" to IPv6 address: %s",
1089 hostname, gai_strerror(rcode));
1094 for (ai = res; ai; ai = ai->ai_next) {
1095 if ((af == ai->ai_family) || (af == AF_UNSPEC)) break;
1096 if (!alt && fallback && ((ai->ai_family == AF_INET) || (ai->ai_family == AF_INET6))) alt = ai;
1101 fr_strerror_printf("ip_hton failed to find requested information for host %.100s", hostname);
1106 rcode = fr_sockaddr2ipaddr((struct sockaddr_storage *)ai->ai_addr,
1107 ai->ai_addrlen, out, NULL);
1110 fr_strerror_printf("Failed converting sockaddr to ipaddr");
1118 * Look IP addresses up, and print names (depending on DNS config)
1120 char const *ip_ntoh(fr_ipaddr_t const *src, char *dst, size_t cnt)
1122 struct sockaddr_storage ss;
1129 if (!fr_dns_lookups) {
1130 return inet_ntop(src->af, &(src->ipaddr), dst, cnt);
1133 if (!fr_ipaddr2sockaddr(src, 0, &ss, &salen)) {
1137 if ((error = getnameinfo((struct sockaddr *)&ss, salen, dst, cnt, NULL, 0,
1138 NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
1139 fr_strerror_printf("ip_ntoh: %s", gai_strerror(error));
1145 /** Mask off a portion of an IPv4 address
1147 * @param ipaddr to mask.
1148 * @param prefix Number of contiguous bits to mask.
1149 * @return an ipv4 address with the host portion zeroed out.
1151 struct in_addr fr_inaddr_mask(struct in_addr const *ipaddr, uint8_t prefix)
1155 if (prefix > 32) prefix = 32;
1158 if (prefix == 32) return *ipaddr;
1160 if (prefix == 0) ret = 0;
1161 else ret = htonl(~((0x00000001UL << (32 - prefix)) - 1)) & ipaddr->s_addr;
1163 return (*(struct in_addr *)&ret);
1166 /** Mask off a portion of an IPv6 address
1168 * @param ipaddr to mask.
1169 * @param prefix Number of contiguous bits to mask.
1170 * @return an ipv6 address with the host portion zeroed out.
1172 struct in6_addr fr_in6addr_mask(struct in6_addr const *ipaddr, uint8_t prefix)
1174 uint64_t const *p = (uint64_t const *) ipaddr;
1175 uint64_t ret[2], *o = ret;
1177 if (prefix > 128) prefix = 128;
1180 if (prefix == 128) return *ipaddr;
1184 *o++ = 0xffffffffffffffffULL & *p++; /* lhs portion masked */
1186 ret[1] = 0; /* rhs portion zeroed */
1189 /* Max left shift is 63 else we get overflow */
1191 *o = htonll(~((uint64_t)(0x0000000000000001ULL << (64 - prefix)) - 1)) & *p;
1196 return *(struct in6_addr *) &ret;
1199 /** Zeroes out the host portion of an fr_ipaddr_t
1201 * @param[in,out] addr to mask
1202 * @param[in] prefix Length of the network portion.
1204 void fr_ipaddr_mask(fr_ipaddr_t *addr, uint8_t prefix)
1209 addr->ipaddr.ip4addr = fr_inaddr_mask(&addr->ipaddr.ip4addr, prefix);
1213 addr->ipaddr.ip6addr = fr_in6addr_mask(&addr->ipaddr.ip6addr, prefix);
1219 addr->prefix = prefix;
1222 static char const hextab[] = "0123456789abcdef";
1224 /** Convert hex strings to binary data
1226 * @param bin Buffer to write output to.
1227 * @param outlen length of output buffer (or length of input string / 2).
1228 * @param hex input string.
1229 * @param inlen length of the input string
1230 * @return length of data written to buffer.
1232 size_t fr_hex2bin(uint8_t *bin, size_t outlen, char const *hex, size_t inlen)
1239 * Smartly truncate output, caller should check number of bytes
1243 if (len > outlen) len = outlen;
1245 for (i = 0; i < len; i++) {
1246 if(!(c1 = memchr(hextab, tolower((int) hex[i << 1]), sizeof(hextab))) ||
1247 !(c2 = memchr(hextab, tolower((int) hex[(i << 1) + 1]), sizeof(hextab))))
1249 bin[i] = ((c1-hextab)<<4) + (c2-hextab);
1255 /** Convert binary data to a hex string
1257 * Ascii encoded hex string will not be prefixed with '0x'
1259 * @warning If the output buffer isn't long enough, we have a buffer overflow.
1261 * @param[out] hex Buffer to write hex output.
1262 * @param[in] bin input.
1263 * @param[in] inlen of bin input.
1264 * @return length of data written to buffer.
1266 size_t fr_bin2hex(char *hex, uint8_t const *bin, size_t inlen)
1270 for (i = 0; i < inlen; i++) {
1271 hex[0] = hextab[((*bin) >> 4) & 0x0f];
1272 hex[1] = hextab[*bin & 0x0f];
1281 /** Convert binary data to a hex string
1283 * Ascii encoded hex string will not be prefixed with '0x'
1285 * @param[in] ctx to alloc buffer in.
1286 * @param[in] bin input.
1287 * @param[in] inlen of bin input.
1288 * @return length of data written to buffer.
1290 char *fr_abin2hex(TALLOC_CTX *ctx, uint8_t const *bin, size_t inlen)
1294 buff = talloc_array(ctx, char, (inlen << 2));
1295 if (!buff) return NULL;
1297 fr_bin2hex(buff, bin, inlen);
1302 /** Consume the integer (or hex) portion of a value string
1304 * @param value string to parse.
1305 * @param end pointer to the first non numeric char.
1306 * @return integer value.
1308 uint32_t fr_strtoul(char const *value, char **end)
1310 if ((value[0] == '0') && (value[1] == 'x')) {
1311 return strtoul(value, end, 16);
1314 return strtoul(value, end, 10);
1317 /** Check whether the string is all whitespace
1319 * @return true if the entirety of the string is whitespace, else false.
1321 bool is_whitespace(char const *value)
1324 if (!isspace(*value)) return false;
1330 /** Check whether the string is made up of printable UTF8 chars
1332 * @param value to check.
1333 * @param len of value.
1336 * - true if the string is printable.
1337 * - false if the string contains non printable chars
1339 bool is_printable(void const *value, size_t len)
1341 uint8_t const *p = value;
1345 for (i = 0; i < len; i++) {
1346 clen = fr_utf8_char(p, len - i);
1347 if (clen == 0) return false;
1354 /** Check whether the string is all numbers
1356 * @return true if the entirety of the string is all numbers, else false.
1358 bool is_integer(char const *value)
1361 if (!isdigit(*value)) return false;
1367 /** Check whether the string is allzeros
1369 * @return true if the entirety of the string is all zeros, else false.
1371 bool is_zero(char const *value)
1374 if (*value != '0') return false;
1381 * So we don't have ifdef's in the rest of the code
1383 #ifndef HAVE_CLOSEFROM
1384 int closefrom(int fd)
1388 #ifdef HAVE_DIRENT_H
1393 if (fcntl(fd, F_CLOSEM) == 0) {
1399 maxfd = fcntl(fd, F_F_MAXFD);
1400 if (maxfd >= 0) goto do_close;
1404 maxfd = sysconf(_SC_OPEN_MAX);
1410 #ifdef HAVE_DIRENT_H
1412 * Use /proc/self/fd directory if it exists.
1414 dir = opendir(CLOSEFROM_DIR);
1420 while ((dp = readdir(dir)) != NULL) {
1421 my_fd = strtol(dp->d_name, &endp, 10);
1422 if (my_fd <= 0) continue;
1424 if (*endp) continue;
1426 if (my_fd == dirfd(dir)) continue;
1428 if ((my_fd >= fd) && (my_fd <= maxfd)) {
1429 (void) close((int) my_fd);
1432 (void) closedir(dir);
1441 if (fd > maxfd) return 0;
1444 * FIXME: return EINTR?
1446 for (i = fd; i < maxfd; i++) {
1454 int fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b)
1456 if (a->af < b->af) return -1;
1457 if (a->af > b->af) return +1;
1459 if (a->prefix < b->prefix) return -1;
1460 if (a->prefix > b->prefix) return +1;
1464 return memcmp(&a->ipaddr.ip4addr,
1466 sizeof(a->ipaddr.ip4addr));
1468 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1470 if (a->scope < b->scope) return -1;
1471 if (a->scope > b->scope) return +1;
1473 return memcmp(&a->ipaddr.ip6addr,
1475 sizeof(a->ipaddr.ip6addr));
1485 int fr_ipaddr2sockaddr(fr_ipaddr_t const *ipaddr, uint16_t port,
1486 struct sockaddr_storage *sa, socklen_t *salen)
1488 memset(sa, 0, sizeof(*sa));
1490 if (ipaddr->af == AF_INET) {
1491 struct sockaddr_in s4;
1493 *salen = sizeof(s4);
1495 memset(&s4, 0, sizeof(s4));
1496 s4.sin_family = AF_INET;
1497 s4.sin_addr = ipaddr->ipaddr.ip4addr;
1498 s4.sin_port = htons(port);
1499 memset(sa, 0, sizeof(*sa));
1500 memcpy(sa, &s4, sizeof(s4));
1502 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1503 } else if (ipaddr->af == AF_INET6) {
1504 struct sockaddr_in6 s6;
1506 *salen = sizeof(s6);
1508 memset(&s6, 0, sizeof(s6));
1509 s6.sin6_family = AF_INET6;
1510 s6.sin6_addr = ipaddr->ipaddr.ip6addr;
1511 s6.sin6_port = htons(port);
1512 s6.sin6_scope_id = ipaddr->scope;
1513 memset(sa, 0, sizeof(*sa));
1514 memcpy(sa, &s6, sizeof(s6));
1524 int fr_sockaddr2ipaddr(struct sockaddr_storage const *sa, socklen_t salen,
1525 fr_ipaddr_t *ipaddr, uint16_t *port)
1527 memset(ipaddr, 0, sizeof(*ipaddr));
1529 if (sa->ss_family == AF_INET) {
1530 struct sockaddr_in s4;
1532 if (salen < sizeof(s4)) {
1533 fr_strerror_printf("IPv4 address is too small");
1537 memcpy(&s4, sa, sizeof(s4));
1538 ipaddr->af = AF_INET;
1539 ipaddr->prefix = 32;
1540 ipaddr->ipaddr.ip4addr = s4.sin_addr;
1541 if (port) *port = ntohs(s4.sin_port);
1543 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1544 } else if (sa->ss_family == AF_INET6) {
1545 struct sockaddr_in6 s6;
1547 if (salen < sizeof(s6)) {
1548 fr_strerror_printf("IPv6 address is too small");
1552 memcpy(&s6, sa, sizeof(s6));
1553 ipaddr->af = AF_INET6;
1554 ipaddr->prefix = 128;
1555 ipaddr->ipaddr.ip6addr = s6.sin6_addr;
1556 if (port) *port = ntohs(s6.sin6_port);
1557 ipaddr->scope = s6.sin6_scope_id;
1561 fr_strerror_printf("Unsupported address famility %d",
1570 /** Set O_NONBLOCK on a socket
1572 * @note O_NONBLOCK is POSIX.
1574 * @param fd to set nonblocking flag on.
1575 * @return flags set on the socket, or -1 on error.
1577 int fr_nonblock(int fd)
1581 flags = fcntl(fd, F_GETFL, NULL);
1583 fr_strerror_printf("Failure getting socket flags: %s", fr_syserror(errno));
1587 flags |= O_NONBLOCK;
1588 if (fcntl(fd, F_SETFL, flags) < 0) {
1589 fr_strerror_printf("Failure setting socket flags: %s", fr_syserror(errno));
1596 /** Unset O_NONBLOCK on a socket
1598 * @note O_NONBLOCK is POSIX.
1600 * @param fd to set nonblocking flag on.
1601 * @return flags set on the socket, or -1 on error.
1603 int fr_blocking(int fd)
1607 flags = fcntl(fd, F_GETFL, NULL);
1609 fr_strerror_printf("Failure getting socket flags: %s", fr_syserror(errno));
1613 flags ^= O_NONBLOCK;
1614 if (fcntl(fd, F_SETFL, flags) < 0) {
1615 fr_strerror_printf("Failure setting socket flags: %s", fr_syserror(errno));
1622 int fr_nonblock(UNUSED int fd)
1624 fr_strerror_printf("Non blocking sockets are not supported");
1627 int fr_blocking(UNUSED int fd)
1629 fr_strerror_printf("Non blocking sockets are not supported");
1634 /** Write out a vector to a file descriptor
1636 * Wraps writev, calling it as necessary. If timeout is not NULL,
1637 * timeout is applied to each call that returns EAGAIN or EWOULDBLOCK
1639 * @note Should only be used on nonblocking file descriptors.
1640 * @note Socket should likely be closed on timeout.
1641 * @note iovec may be modified in such a way that it's not re-usable.
1642 * @note Leaves errno set to the last error that ocurred.
1644 * @param fd to write to.
1645 * @param vector to write.
1646 * @param iovcnt number of elements in iovec.
1647 * @param timeout how long to wait for fd to become writeable before timing out.
1648 * @return number of bytes written, -1 on error.
1650 ssize_t fr_writev(int fd, struct iovec vector[], int iovcnt, struct timeval *timeout)
1652 struct iovec *vector_p = vector;
1655 while (iovcnt > 0) {
1658 wrote = writev(fd, vector_p, iovcnt);
1663 * An entire vector element was written
1665 if (wrote >= (ssize_t)vector_p->iov_len) {
1667 wrote -= vector_p->iov_len;
1673 * Partial vector element was written
1675 vector_p->iov_len -= wrote;
1676 vector_p->iov_base = ((char *)vector_p->iov_base) + wrote;
1680 } else if (wrote == 0) return total;
1683 /* Write operation would block, use select() to implement a timeout */
1684 #if EWOULDBLOCK != EAGAIN
1694 FD_ZERO(&write_set);
1695 FD_SET(fd, &write_set);
1697 /* Don't let signals mess up the select */
1699 ret = select(fd + 1, NULL, &write_set, NULL, timeout);
1700 } while ((ret == -1) && (errno == EINTR));
1702 /* Select returned 0 which means it reached the timeout */
1704 fr_strerror_printf("Write timed out");
1708 /* Other select error */
1710 fr_strerror_printf("Failed waiting on socket: %s", fr_syserror(errno));
1714 /* select said a file descriptor was ready for writing */
1715 if (!fr_assert(FD_ISSET(fd, &write_set))) return -1;
1728 /** Convert UTF8 string to UCS2 encoding
1730 * @note Borrowed from src/crypto/ms_funcs.c of wpa_supplicant project (http://hostap.epitest.fi/wpa_supplicant/)
1732 * @param[out] out Where to write the ucs2 string.
1733 * @param[in] outlen Size of output buffer.
1734 * @param[in] in UTF8 string to convert.
1735 * @param[in] inlen length of UTF8 string.
1736 * @return the size of the UCS2 string written to the output buffer (in bytes).
1738 ssize_t fr_utf8_to_ucs2(uint8_t *out, size_t outlen, char const *in, size_t inlen)
1741 uint8_t *start = out;
1743 for (i = 0; i < inlen; i++) {
1747 if ((size_t)(out - start) >= outlen) {
1748 /* input too long */
1752 /* One-byte encoding */
1754 FR_PUT_LE16(out, c);
1757 } else if ((i == (inlen - 1)) || ((size_t)(out - start) >= (outlen - 1))) {
1758 /* Incomplete surrogate */
1763 /* Two-byte encoding */
1764 if ((c & 0xe0) == 0xc0) {
1765 FR_PUT_LE16(out, ((c & 0x1f) << 6) | (c2 & 0x3f));
1769 if ((i == inlen) || ((size_t)(out - start) >= (outlen - 1))) {
1770 /* Incomplete surrogate */
1774 /* Three-byte encoding */
1776 FR_PUT_LE16(out, ((c & 0xf) << 12) | ((c2 & 0x3f) << 6) | (c3 & 0x3f));
1783 /** Write 128bit unsigned integer to buffer
1785 * @author Alexey Frunze
1787 * @param out where to write result to.
1788 * @param outlen size of out.
1789 * @param num 128 bit integer.
1791 size_t fr_prints_uint128(char *out, size_t outlen, uint128_t const num)
1793 char buff[128 / 3 + 1 + 1];
1797 #ifdef FR_LITTLE_ENDIAN
1805 memset(buff, '0', sizeof(buff) - 1);
1806 buff[sizeof(buff) - 1] = '\0';
1808 memcpy(n, &num, sizeof(n));
1810 for (i = 0; i < 128; i++) {
1814 carry = (n[h] >= 0x8000000000000000);
1816 // Shift n[] left, doubling it
1817 n[h] = ((n[h] << 1) & 0xffffffffffffffff) + (n[l] >= 0x8000000000000000);
1818 n[l] = ((n[l] << 1) & 0xffffffffffffffff);
1820 // Add s[] to itself in decimal, doubling it
1821 for (j = sizeof(buff) - 2; j >= 0; j--) {
1822 buff[j] += buff[j] - '0' + carry;
1823 carry = (buff[j] > '9');
1830 while ((*p == '0') && (p < &buff[sizeof(buff) - 2])) {
1834 return strlcpy(out, p, outlen);
1838 * Sort of strtok/strsep function.
1840 static char *mystrtok(char **ptr, char const *sep)
1848 while (**ptr && strchr(sep, **ptr)) {
1856 while (**ptr && strchr(sep, **ptr) == NULL) {
1866 /** Convert string in various formats to a time_t
1868 * @param date_str input date string.
1869 * @param date time_t to write result to.
1870 * @return 0 on success or -1 on error.
1872 int fr_get_time(char const *date_str, time_t *date)
1876 struct tm *tm, s_tm;
1883 * Test for unix timestamp date
1885 *date = strtoul(date_str, &tail, 10);
1886 if (*tail == '\0') {
1891 memset(tm, 0, sizeof(*tm));
1892 tm->tm_isdst = -1; /* don't know, and don't care about DST */
1894 strlcpy(buf, date_str, sizeof(buf));
1897 f[0] = mystrtok(&p, " \t");
1898 f[1] = mystrtok(&p, " \t");
1899 f[2] = mystrtok(&p, " \t");
1900 f[3] = mystrtok(&p, " \t"); /* may, or may not, be present */
1901 if (!f[0] || !f[1] || !f[2]) return -1;
1904 * The time has a colon, where nothing else does.
1905 * So if we find it, bubble it to the back of the list.
1908 for (i = 0; i < 3; i++) {
1909 if (strchr(f[i], ':')) {
1919 * The month is text, which allows us to find it easily.
1922 for (i = 0; i < 3; i++) {
1923 if (isalpha( (int) *f[i])) {
1925 * Bubble the month to the front of the list
1931 for (j = 0; j < 12; j++) {
1932 if (strncasecmp(months[j], f[0], 3) == 0) {
1940 /* month not found? */
1941 if (tm->tm_mon == 12) return -1;
1944 * The year may be in f[1], or in f[2]
1946 tm->tm_year = atoi(f[1]);
1947 tm->tm_mday = atoi(f[2]);
1949 if (tm->tm_year >= 1900) {
1950 tm->tm_year -= 1900;
1954 * We can't use 2-digit years any more, they make it
1955 * impossible to tell what's the day, and what's the year.
1957 if (tm->tm_mday < 1900) return -1;
1960 * Swap the year and the day.
1963 tm->tm_year = tm->tm_mday - 1900;
1968 * If the day is out of range, die.
1970 if ((tm->tm_mday < 1) || (tm->tm_mday > 31)) {
1975 * There may be %H:%M:%S. Parse it in a hacky way.
1978 f[0] = f[3]; /* HH */
1979 f[1] = strchr(f[0], ':'); /* find : separator */
1980 if (!f[1]) return -1;
1982 *(f[1]++) = '\0'; /* nuke it, and point to MM:SS */
1984 f[2] = strchr(f[1], ':'); /* find : separator */
1986 *(f[2]++) = '\0'; /* nuke it, and point to SS */
1987 tm->tm_sec = atoi(f[2]);
1988 } /* else leave it as zero */
1990 tm->tm_hour = atoi(f[0]);
1991 tm->tm_min = atoi(f[1]);
1995 * Returns -1 on error.
1998 if (t == (time_t) -1) return -1;
2005 /** Compares two pointers
2007 * @param a first pointer to compare.
2008 * @param b second pointer to compare.
2009 * @return -1 if a < b, +1 if b > a, or 0 if both equal.
2011 int8_t fr_pointer_cmp(void const *a, void const *b)
2013 if (a < b) return -1;
2014 if (a == b) return 0;
2019 static int _quick_partition(void const *to_sort[], int min, int max, fr_cmp_t cmp) {
2020 void const *pivot = to_sort[min];
2026 do ++i; while((cmp(to_sort[i], pivot) <= 0) && i <= max);
2027 do --j; while(cmp(to_sort[j], pivot) > 0);
2032 to_sort[i] = to_sort[j];
2037 to_sort[min] = to_sort[j];
2043 /** Quick sort an array of pointers using a comparator
2045 * @param to_sort array of pointers to sort.
2046 * @param min_idx the lowest index (usually 0).
2047 * @param max_idx the highest index (usually length of array - 1).
2048 * @param cmp the comparison function to use to sort the array elements.
2050 void fr_quick_sort(void const *to_sort[], int min_idx, int max_idx, fr_cmp_t cmp)
2054 if (min_idx >= max_idx) return;
2056 part = _quick_partition(to_sort, min_idx, max_idx, cmp);
2057 fr_quick_sort(to_sort, min_idx, part - 1, cmp);
2058 fr_quick_sort(to_sort, part + 1, max_idx, cmp);
2062 void fr_talloc_verify_cb(UNUSED const void *ptr, UNUSED int depth,
2063 UNUSED int max_depth, UNUSED int is_ref,
2064 UNUSED void *private_data)