Add fr_abin2hex
[freeradius.git] / src / include / libradius.h
index 78971ca..747e846 100644 (file)
 RCSIDH(libradius_h, "$Id$")
 
 /*
- *  Preprocessor hacks.
+ *  Compiler hinting macros.  Included here for 3rd party consumers
+ *  of libradius.h.
  */
-#ifndef STRINGIFY
-#  define XSTRINGIFY(x) #x
-#  define STRINGIFY(x) XSTRINGIFY(x)
-#endif
-
-#ifndef HEXIFY
-#  define XHEXIFY4(b1,b2,b3,b4)        (0x ## b1 ## b2 ## b3 ## b4)
-#  define HEXIFY4(b1,b2,b3,b4) XHEXIFY4(b1, b2, b3, b4)
-
-#  define XHEXIFY3(b1,b2,b3)   (0x ## b1 ## b2 ## b3)
-#  define HEXIFY3(b1,b2,b3)    XHEXIFY3(b1, b2, b3)
-
-#  define XHEXIFY2(b1,b2)      (0x ## b1 ## b2)
-#  define HEXIFY2(b1,b2)       XHEXIFY2(b1, b2)
-
-#  define XHEXIFY(b1)          (0x ## b1)
-#  define HEXIFY(b1)           XHEXIFY(b1)
-#endif
+#include <freeradius-devel/build.h>
 
 /*
  *  Let any external program building against the library know what
@@ -89,15 +73,19 @@ RCSIDH(libradius_h, "$Id$")
 #include <stdbool.h>
 #include <signal.h>
 
+#ifdef HAVE_LIMITS_H
+#  include <limits.h>
+#endif
+
 #include <freeradius-devel/threads.h>
 #include <freeradius-devel/radius.h>
 #include <freeradius-devel/token.h>
 #include <freeradius-devel/hash.h>
 
 #ifdef SIZEOF_UNSIGNED_INT
-#if SIZEOF_UNSIGNED_INT != 4
-#error FATAL: sizeof(unsigned int) != 4
-#endif
+#  if SIZEOF_UNSIGNED_INT != 4
+#    error FATAL: sizeof(unsigned int) != 4
+#  endif
 #endif
 
 /*
@@ -111,24 +99,25 @@ extern "C" {
 #endif
 
 #if defined(WITH_VERIFY_PTR)
-/*
- *     Requires typeof(), which is in most modern C compilers.
- */
+#  define FREE_MAGIC (0xF4EEF4EE)
 
 /*
-#define VERIFY_VP(_x) do { (void) talloc_get_type_abort(_x, VALUE_PAIR); \
-                       if (_x->da) { \
-                               (void) talloc_get_type_abort(_x->da, DICT_ATTR); \
-                       } \
-                     } while (0)
-*/
-#define FREE_MAGIC (0xF4EEF4EE)
-
-#define VERIFY_VP(_x) (void) talloc_get_type_abort(_x, VALUE_PAIR)
-#define VERIFY_PACKET(_x) (void) talloc_get_type_abort(_x, RADIUS_PACKET)
+ * @FIXME
+ *  Add if (_x->da) (void) talloc_get_type_abort(_x->da, DICT_ATTR);
+ *  to the macro below when dictionaries are talloced.
+ */
+#  define VERIFY_VP(_x)                fr_verify_vp(__FILE__,  __LINE__, _x)
+#  define VERIFY_LIST(_x)      fr_verify_list(__FILE__,  __LINE__, NULL, _x)
+#  define VERIFY_PACKET(_x)    (void) talloc_get_type_abort(_x, RADIUS_PACKET)
 #else
-#define VERIFY_VP(_x)
-#define VERIFY_PACKET(_x)
+/*
+ *  Even if were building without WITH_VERIFY_PTR
+ *  the pointer must not be NULL when these various macros are used
+ *  so we can add some sneaky soft asserts.
+ */
+#  define VERIFY_VP(_x)                fr_assert(_x)
+#  define VERIFY_LIST(_x)      fr_assert(_x)
+#  define VERIFY_PACKET(_x)    fr_assert(_x)
 #endif
 
 #define AUTH_VECTOR_LEN                16
@@ -150,22 +139,26 @@ extern "C" {
 
 #define TAG_VALID(x)           ((x) > 0 && (x) < 0x20)
 #define TAG_VALID_ZERO(x)      ((x) < 0x20)
-#define TAG_ANY                        -128    /* minimum signed char */
-#define TAG_UNUSED             0
+#define TAG_ANY                        INT8_MIN
+#define TAG_NONE               0
+/** Check if tags are equal
+ *
+ * @param _x tag were matching on.
+ * @param _y tag belonging to the attribute were checking.
+ */
+#define TAG_EQ(_x, _y) ((_x == _y) || (_x == TAG_ANY) || ((_x == TAG_NONE) && (_y == TAG_ANY)))
+#define ATTRIBUTE_EQ(_x, _y) ((_x && _y) && (_x->da == _y->da) && (!_x->da->flags.has_tag || TAG_EQ(_x->tag, _y->tag)))
+
+#define NUM_ANY                        INT_MIN
+#define NUM_JOIN               (INT_MIN + 1)
+#define NUM_COUNT              (INT_MIN + 2)
 
 #define PAD(_x, _y)            (_y - ((_x) % _y))
 
-#if defined(__GNUC__)
-# define PRINTF_LIKE(n) __attribute__ ((format(printf, n, n+1)))
-# define NEVER_RETURNS __attribute__ ((noreturn))
-# define UNUSED __attribute__ ((unused))
-# define BLANK_FORMAT " "      /* GCC_LINT whines about empty formats */
-#else
-# define PRINTF_LIKE(n)        /* ignore */
-# define NEVER_RETURNS /* ignore */
-# define UNUSED /* ignore */
-# define BLANK_FORMAT ""
-#endif
+#define PRINTF_LIKE(n)         CC_HINT(format(printf, n, n+1))
+#define NEVER_RETURNS          CC_HINT(noreturn)
+#define UNUSED                 CC_HINT(unused)
+#define BLANK_FORMAT           " "     /* GCC_LINT whines about empty formats */
 
 typedef struct attr_flags {
        unsigned int    is_unknown : 1;                         //!< Attribute number or vendor is unknown.
@@ -235,7 +228,7 @@ typedef struct dict_vendor {
 
 /** Union containing all data types supported by the server
  *
- * This union contains all data types that can be represented with VALUE_PAIRs. It may also be used in other parts
+ * This union contains all data types that can be represented by VALUE_PAIRs. It may also be used in other parts
  * of the server where values of different types need to be stored.
  *
  * PW_TYPE should be an enumeration of the values in this union.
@@ -243,17 +236,26 @@ typedef struct dict_vendor {
 typedef union value_data {
        char const              *strvalue;                      //!< Pointer to UTF-8 string.
        uint8_t const           *octets;                        //!< Pointer to binary string.
+       uint32_t                integer;                        //!< 32bit unsigned integer.
        struct in_addr          ipaddr;                         //!< IPv4 Address.
-       struct in6_addr         ipv6addr;                       //!< IPv6 Address.
        uint32_t                date;                           //!< Date (32bit Unix timestamp).
-       uint32_t                integer;                        //!< 32bit unsigned integer.
+       size_t                  filter[32/sizeof(size_t)];      //!< Ascend binary format a packed data
+                                                               //!< structure.
+
+       uint8_t                 ifid[8];                        //!< IPv6 interface ID (should be struct?).
+       struct in6_addr         ipv6addr;                       //!< IPv6 Address.
+       uint8_t                 ipv6prefix[18];                 //!< IPv6 prefix (should be struct?).
+
+       uint8_t                 byte;                           //!< 8bit unsigned integer.
+       uint16_t                ushort;                         //!< 16bit unsigned integer.
+
+       uint8_t                 ether[6];                       //!< Ethernet (MAC) address.
+
        int32_t                 sinteger;                       //!< 32bit signed integer.
        uint64_t                integer64;                      //!< 64bit unsigned integer.
-       size_t                  filter[32/sizeof(size_t)];      //!< Ascend binary filter.
-       uint8_t                 ifid[8]; /* struct? */          //!< IPv6 interface ID.
-       uint8_t                 ipv6prefix[18]; /* struct? */   //!< IPv6 prefix.
-       uint8_t                 ipv4prefix[6]; /* struct? */    //!< IPv4 prefix.
-       uint8_t                 ether[6];                       //!< Ethernet (MAC) address.
+
+       uint8_t                 ipv4prefix[6];                  //!< IPv4 prefix (should be struct?).
+
        uint8_t                 *tlv;                           //!< Nested TLV (should go away).
        void const              *ptr;                           //!< generic pointer.
 } value_data_t;
@@ -275,8 +277,7 @@ typedef enum value_type {
 
 /** Stores an attribute, a value and various bits of other data
  *
- * VALUE_PAIRs are the main data structure used in the server, they specify an attribute, it's children and
- * it's siblings.
+ * VALUE_PAIRs are the main data structure used in the server
  *
  * They also specify what behaviour should be used when the attribute is merged into a new list/tree.
  */
@@ -317,7 +318,7 @@ typedef struct value_pair {
 typedef struct vp_cursor {
        VALUE_PAIR      **first;
        VALUE_PAIR      *found;                                 //!< pairfind marker.
-       VALUE_PAIR      *last;                                  //!< Temporary only used for pairinsert
+       VALUE_PAIR      *last;                                  //!< Temporary only used for fr_cursor_insert
        VALUE_PAIR      *current;                               //!< The current attribute.
        VALUE_PAIR      *next;                                  //!< Next attribute to process.
 } vp_cursor_t;
@@ -335,20 +336,22 @@ typedef struct value_pair_raw {
        FR_TOKEN op;                                            //!< Operator.
 } VALUE_PAIR_RAW;
 
-#define vp_strvalue   data.strvalue
-#define vp_octets     data.octets
-#define vp_ipv6addr   data.ipv6addr
-#define vp_ifid       data.ifid
-#define vp_ipv6prefix data.ipv6prefix
-#define vp_ipv4prefix data.ipv4prefix
-#define vp_filter     data.filter
-#define vp_ether      data.ether
-#define vp_signed     data.sinteger
-#define vp_tlv       data.tlv
-#define vp_integer64  data.integer64
-#define vp_ipaddr     data.ipaddr.s_addr
-#define vp_date       data.date
-#define vp_integer    data.integer
+#define vp_strvalue    data.strvalue
+#define vp_integer     data.integer
+#define vp_ipaddr      data.ipaddr.s_addr
+#define vp_date                data.date
+#define vp_filter      data.filter
+#define vp_octets      data.octets
+#define vp_ifid                data.ifid
+#define vp_ipv6addr    data.ipv6addr
+#define vp_ipv6prefix  data.ipv6prefix
+#define vp_byte                data.byte
+#define vp_short       data.ushort
+#define vp_ether       data.ether
+#define vp_signed      data.sinteger
+#define vp_integer64   data.integer64
+#define vp_ipv4prefix  data.ipv4prefix
+#define vp_tlv         data.tlv
 
 typedef struct fr_ipaddr_t {
        int             af;     /* address family */
@@ -356,6 +359,7 @@ typedef struct fr_ipaddr_t {
                struct in_addr  ip4addr;
                struct in6_addr ip6addr; /* maybe defined in missing.h */
        } ipaddr;
+       uint8_t         prefix;
        uint32_t        scope;  /* for IPv6 */
 } fr_ipaddr_t;
 
@@ -388,6 +392,23 @@ typedef struct radius_packet {
 #endif
 } RADIUS_PACKET;
 
+typedef enum {
+       DECODE_FAIL_NONE = 0,
+       DECODE_FAIL_MIN_LENGTH_PACKET,
+       DECODE_FAIL_MIN_LENGTH_FIELD,
+       DECODE_FAIL_MIN_LENGTH_MISMATCH,
+       DECODE_FAIL_HEADER_OVERFLOW,
+       DECODE_FAIL_UNKNOWN_PACKET_CODE,
+       DECODE_FAIL_INVALID_ATTRIBUTE,
+       DECODE_FAIL_ATTRIBUTE_TOO_SHORT,
+       DECODE_FAIL_ATTRIBUTE_OVERFLOW,
+       DECODE_FAIL_MA_INVALID_LENGTH,
+       DECODE_FAIL_ATTRIBUTE_UNDERFLOW,
+       DECODE_FAIL_TOO_MANY_ATTRIBUTES,
+       DECODE_FAIL_MA_MISSING,
+       DECODE_FAIL_MAX
+} decode_fail_t;
+
 /*
  *     Version check.
  */
@@ -399,14 +420,20 @@ int               fr_check_lib_magic(uint64_t magic);
 int            fr_utf8_char(uint8_t const *str);
 size_t         fr_print_string(char const *in, size_t inlen,
                                 char *out, size_t outlen);
+size_t         fr_print_string_len(char const *in, size_t inlen);
+
+#define                is_truncated(_ret, _max) ((_ret) >= (_max))
+#define                truncate_len(_ret, _max) (((_ret) >= (_max)) ? ((_max) - 1) : _ret)
+size_t         vp_data_prints_value(char *out, size_t outlen,
+                                    DICT_ATTR const *da, value_data_t const *data, size_t data_len, int8_t quote);
 size_t         vp_prints_value(char *out, size_t outlen, VALUE_PAIR const *vp, int8_t quote);
-char           *vp_aprinttype(TALLOC_CTX *ctx, PW_TYPE type);
-char           *vp_aprint(TALLOC_CTX *ctx, VALUE_PAIR const *vp);
 size_t         vp_prints_value_json(char *out, size_t outlen, VALUE_PAIR const *vp);
-size_t         vp_print_name(char *out, size_t outlen, unsigned int attr, unsigned int vendor);
 size_t         vp_prints(char *out, size_t outlen, VALUE_PAIR const *vp);
 void           vp_print(FILE *, VALUE_PAIR const *);
 void           vp_printlist(FILE *, VALUE_PAIR const *);
+char           *vp_aprint_type(TALLOC_CTX *ctx, PW_TYPE type);
+char           *vp_aprint_value(TALLOC_CTX *ctx, VALUE_PAIR const *vp);
+char           *vp_aprint(TALLOC_CTX *ctx, VALUE_PAIR const *vp);
 #define                fprint_attr_val vp_print
 
 /*
@@ -428,6 +455,7 @@ DICT_ATTR const     *dict_attrunknown(unsigned int attr, unsigned int vendor, int vp
 DICT_ATTR const        *dict_attrunknownbyname(char const *attribute, int vp_free);
 DICT_ATTR const        *dict_attrbyvalue(unsigned int attr, unsigned int vendor);
 DICT_ATTR const        *dict_attrbyname(char const *attr);
+DICT_ATTR const *dict_attrbytagged_name(char const *name);
 DICT_ATTR const        *dict_attrbytype(unsigned int attr, unsigned int vendor,
                                 PW_TYPE type);
 DICT_ATTR const        *dict_attrbyparent(DICT_ATTR const *parent, unsigned int attr,
@@ -448,23 +476,13 @@ DICT_VENDOR       *dict_vendorbyvalue(int vendor);
 #endif
 
 /* md5.c */
-
 void           fr_md5_calc(uint8_t *, uint8_t const *, unsigned int);
 
-/* hmac.c */
-
-void fr_hmac_md5(uint8_t const *text, size_t text_len, uint8_t const *key, size_t key_len, unsigned char *digest);
-
-/* hmacsha1.c */
-
-void fr_hmac_sha1(uint8_t const *text, size_t text_len, uint8_t const *key, size_t key_len, uint8_t *digest);
-
 /* radius.c */
 int            rad_send(RADIUS_PACKET *, RADIUS_PACKET const *, char const *secret);
-int            rad_packet_ok(RADIUS_PACKET *packet, int flags);
+bool           rad_packet_ok(RADIUS_PACKET *packet, int flags, decode_fail_t *reason);
 RADIUS_PACKET  *rad_recv(int fd, int flags);
-ssize_t rad_recv_header(int sockfd, fr_ipaddr_t *src_ipaddr, int *src_port,
-                       int *code);
+ssize_t rad_recv_header(int sockfd, fr_ipaddr_t *src_ipaddr, uint16_t *src_port, int *code);
 void           rad_recv_discard(int sockfd);
 int            rad_verify(RADIUS_PACKET *packet, RADIUS_PACKET *original,
                           char const *secret);
@@ -475,8 +493,10 @@ int                rad_sign(RADIUS_PACKET *packet, RADIUS_PACKET const *original,
                         char const *secret);
 
 int rad_digest_cmp(uint8_t const *a, uint8_t const *b, size_t length);
-RADIUS_PACKET  *rad_alloc(TALLOC_CTX *ctx, int newvector);
+RADIUS_PACKET  *rad_alloc(TALLOC_CTX *ctx, bool new_vector);
 RADIUS_PACKET  *rad_alloc_reply(TALLOC_CTX *ctx, RADIUS_PACKET *);
+RADIUS_PACKET *rad_copy_packet(TALLOC_CTX *ctx, RADIUS_PACKET const *in);
+
 void           rad_free(RADIUS_PACKET **);
 int            rad_pwencode(char *encpw, size_t *len, char const *secret,
                             uint8_t const *vector);
@@ -491,42 +511,47 @@ int               rad_tunnel_pwdecode(uint8_t *encpw, size_t *len,
 int            rad_chap_encode(RADIUS_PACKET *packet, uint8_t *output,
                                int id, VALUE_PAIR *password);
 
-int rad_attr_ok(RADIUS_PACKET const *packet, RADIUS_PACKET const *original,
-               DICT_ATTR *da,
-               uint8_t const *data, size_t length);
-int rad_tlv_ok(uint8_t const *data, size_t length,
-              size_t dv_type, size_t dv_length);
-
-ssize_t        rad_attr2vp(RADIUS_PACKET *packet, RADIUS_PACKET const *original,
-                   char const *secret,
-                   uint8_t const *data, size_t length,
-                   VALUE_PAIR **pvp);
-
-ssize_t  rad_data2vp(unsigned int attribute, unsigned int vendor,
-                    uint8_t const *data, size_t length,
-                    VALUE_PAIR **pvp);
-
-ssize_t rad_vp2data(uint8_t const **out, VALUE_PAIR const *vp);
-
-int rad_vp2extended(RADIUS_PACKET const *packet,
-                   RADIUS_PACKET const *original,
-                   char const *secret, VALUE_PAIR const **pvp,
-                   uint8_t *ptr, size_t room);
-int rad_vp2wimax(RADIUS_PACKET const *packet,
-                RADIUS_PACKET const *original,
-                char const *secret, VALUE_PAIR const **pvp,
-                uint8_t *ptr, size_t room);
-int rad_vp2vsa(RADIUS_PACKET const *packet, RADIUS_PACKET const *original,
-              char const *secret, VALUE_PAIR const **pvp, uint8_t *start,
-              size_t room);
-int rad_vp2rfc(RADIUS_PACKET const *packet,
-              RADIUS_PACKET const *original,
-              char const *secret, VALUE_PAIR const **pvp,
-              uint8_t *ptr, size_t room);
-
-int rad_vp2attr(RADIUS_PACKET const *packet,
-               RADIUS_PACKET const *original, char const *secret,
-               VALUE_PAIR const **pvp, uint8_t *ptr, size_t room);
+int            rad_attr_ok(RADIUS_PACKET const *packet, RADIUS_PACKET const *original,
+                           DICT_ATTR *da, uint8_t const *data, size_t length);
+int            rad_tlv_ok(uint8_t const *data, size_t length,
+                          size_t dv_type, size_t dv_length);
+
+ssize_t                data2vp(TALLOC_CTX *ctx,
+                       RADIUS_PACKET *packet, RADIUS_PACKET const *original,
+                       char const *secret,
+                       DICT_ATTR const *da, uint8_t const *start,
+                       size_t const attrlen, size_t const packetlen,
+                       VALUE_PAIR **pvp);
+
+ssize_t                rad_attr2vp(TALLOC_CTX *ctx,
+                           RADIUS_PACKET *packet, RADIUS_PACKET const *original,
+                           char const *secret,
+                           uint8_t const *data, size_t length,
+                           VALUE_PAIR **pvp);
+
+ssize_t                rad_vp2data(uint8_t const **out, VALUE_PAIR const *vp);
+
+int            rad_vp2extended(RADIUS_PACKET const *packet,
+                               RADIUS_PACKET const *original,
+                               char const *secret, VALUE_PAIR const **pvp,
+                               uint8_t *ptr, size_t room);
+int            rad_vp2wimax(RADIUS_PACKET const *packet,
+                            RADIUS_PACKET const *original,
+                            char const *secret, VALUE_PAIR const **pvp,
+                            uint8_t *ptr, size_t room);
+
+int            rad_vp2vsa(RADIUS_PACKET const *packet, RADIUS_PACKET const *original,
+                          char const *secret, VALUE_PAIR const **pvp, uint8_t *start,
+                          size_t room);
+
+int            rad_vp2rfc(RADIUS_PACKET const *packet,
+                          RADIUS_PACKET const *original,
+                          char const *secret, VALUE_PAIR const **pvp,
+                          uint8_t *ptr, size_t room);
+
+int            rad_vp2attr(RADIUS_PACKET const *packet,
+                           RADIUS_PACKET const *original, char const *secret,
+                           VALUE_PAIR const **pvp, uint8_t *ptr, size_t room);
 
 /* valuepair.c */
 VALUE_PAIR     *pairalloc(TALLOC_CTX *ctx, DICT_ATTR const *da);
@@ -534,126 +559,133 @@ VALUE_PAIR      *paircreate(TALLOC_CTX *ctx, unsigned int attr, unsigned int vendor);
 int            pair2unknown(VALUE_PAIR *vp);
 void           pairfree(VALUE_PAIR **);
 VALUE_PAIR     *pairfind(VALUE_PAIR *, unsigned int attr, unsigned int vendor, int8_t tag);
-
-#define                paircursor(_x, _y)      paircursorc(_x,(VALUE_PAIR const * const *) _y)
-VALUE_PAIR     *paircursorc(vp_cursor_t *cursor, VALUE_PAIR const * const *node);
-VALUE_PAIR     *pairfirst(vp_cursor_t *cursor);
-VALUE_PAIR     *pairfindnext(vp_cursor_t *cursor, unsigned int attr, unsigned int vendor, int8_t tag);
-VALUE_PAIR     *pairnext(vp_cursor_t *cursor);
-VALUE_PAIR     *pairlast(vp_cursor_t *cursor);
-VALUE_PAIR     *paircurrent(vp_cursor_t *cursor);
-void           pairinsert(vp_cursor_t *cursor, VALUE_PAIR *vp);
-VALUE_PAIR     *pairremove(vp_cursor_t *cursor);
+VALUE_PAIR     *pairfind_da(VALUE_PAIR *, DICT_ATTR const *da, int8_t tag);
+
+#define                fr_cursor_init(_x, _y)  _fr_cursor_init(_x,(VALUE_PAIR const * const *) _y)
+VALUE_PAIR     *_fr_cursor_init(vp_cursor_t *cursor, VALUE_PAIR const * const *node);
+void           fr_cursor_copy(vp_cursor_t *out, vp_cursor_t *in);
+VALUE_PAIR     *fr_cursor_first(vp_cursor_t *cursor);
+VALUE_PAIR     *fr_cursor_last(vp_cursor_t *cursor);
+VALUE_PAIR     *fr_cursor_next_by_num(vp_cursor_t *cursor, unsigned int attr, unsigned int vendor, int8_t tag);
+
+VALUE_PAIR     *fr_cursor_next_by_da(vp_cursor_t *cursor, DICT_ATTR const *da, int8_t tag)
+               CC_HINT(nonnull);
+
+VALUE_PAIR     *fr_cursor_next(vp_cursor_t *cursor);
+VALUE_PAIR     *fr_cursor_next_peek(vp_cursor_t *cursor);
+VALUE_PAIR     *fr_cursor_current(vp_cursor_t *cursor);
+void           fr_cursor_insert(vp_cursor_t *cursor, VALUE_PAIR *vp);
+VALUE_PAIR     *fr_cursor_remove(vp_cursor_t *cursor);
+VALUE_PAIR     *fr_cursor_replace(vp_cursor_t *cursor, VALUE_PAIR *new);
 void           pairdelete(VALUE_PAIR **, unsigned int attr, unsigned int vendor, int8_t tag);
 void           pairadd(VALUE_PAIR **, VALUE_PAIR *);
 void           pairreplace(VALUE_PAIR **first, VALUE_PAIR *add);
-int            paircmp(VALUE_PAIR *check, VALUE_PAIR *data);
-int            paircmp_op(VALUE_PAIR const *one, FR_TOKEN op, VALUE_PAIR const *two);
-void           pairsort(VALUE_PAIR **vps, bool with_tag);
-bool           pairvalidate(VALUE_PAIR *filter, VALUE_PAIR *list);
+int8_t         paircmp_value(VALUE_PAIR const *a, VALUE_PAIR const *b);
+int8_t         paircmp_op(VALUE_PAIR const *a, FR_TOKEN op, VALUE_PAIR const *b);
+int8_t         paircmp(VALUE_PAIR *a, VALUE_PAIR *b);
+int8_t         pairlistcmp(VALUE_PAIR *a, VALUE_PAIR *b);
+
+typedef int8_t (*fr_cmp_t)(void const *a, void const *b);
+int8_t         attrcmp(void const *a, void const *b);
+int8_t         attrtagcmp(void const *a, void const *b);
+void           pairsort(VALUE_PAIR **vps, fr_cmp_t cmp);
+void           pairvalidate_debug(TALLOC_CTX *ctx, VALUE_PAIR const *failed[2]);
+bool           pairvalidate(VALUE_PAIR const *failed[2], VALUE_PAIR *filter, VALUE_PAIR *list);
+bool           pairvalidate_relaxed(VALUE_PAIR const *failed[2], VALUE_PAIR *filter, VALUE_PAIR *list);
 VALUE_PAIR     *paircopyvp(TALLOC_CTX *ctx, VALUE_PAIR const *vp);
-VALUE_PAIR     *paircopyvpdata(TALLOC_CTX *ctx, DICT_ATTR const *da, VALUE_PAIR const *vp);
 VALUE_PAIR     *paircopy(TALLOC_CTX *ctx, VALUE_PAIR *from);
 VALUE_PAIR     *paircopy2(TALLOC_CTX *ctx, VALUE_PAIR *from, unsigned int attr, unsigned int vendor, int8_t tag);
+VALUE_PAIR     *pairsteal(TALLOC_CTX *ctx, VALUE_PAIR *from);
 void           pairmemcpy(VALUE_PAIR *vp, uint8_t const * src, size_t len);
 void           pairmemsteal(VALUE_PAIR *vp, uint8_t const *src);
 void           pairstrsteal(VALUE_PAIR *vp, char const *src);
 void           pairstrcpy(VALUE_PAIR *vp, char const * src);
-void           pairsprintf(VALUE_PAIR *vp, char const * fmt, ...)
-#ifdef __GNUC__
-               __attribute__ ((format (printf, 2, 3)))
-#endif
-;
+void           pairstrncpy(VALUE_PAIR *vp, char const * src, size_t len);
+int            pairdatacpy(VALUE_PAIR *vp, DICT_ATTR const *da, value_data_t const *data, size_t len);
+void           pairsprintf(VALUE_PAIR *vp, char const * fmt, ...) CC_HINT(format (printf, 2, 3));
 void           pairmove(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from);
 void           pairfilter(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from,
-                                          unsigned int attr, unsigned int vendor, int8_t tag);
-bool           pairparsevalue(VALUE_PAIR *vp, char const *value);
+                          unsigned int attr, unsigned int vendor, int8_t tag);
+VALUE_PAIR     *pairmake_ip(TALLOC_CTX *ctx, char const *value,
+                            DICT_ATTR *ipv4, DICT_ATTR *ipv6, DICT_ATTR *ipv4_prefix, DICT_ATTR *ipv6_prefix);
+int            pairparsevalue(VALUE_PAIR *vp, char const *value, size_t len);
 VALUE_PAIR     *pairmake(TALLOC_CTX *ctx, VALUE_PAIR **vps, char const *attribute, char const *value, FR_TOKEN op);
 int            pairmark_xlat(VALUE_PAIR *vp, char const *value);
 FR_TOKEN       pairread(char const **ptr, VALUE_PAIR_RAW *raw);
 FR_TOKEN       userparse(TALLOC_CTX *ctx, char const *buffer, VALUE_PAIR **head);
-VALUE_PAIR     *readvp2(TALLOC_CTX *ctx, FILE *fp, int *pfiledone, char const *errprefix);
+int            readvp2(VALUE_PAIR **out, TALLOC_CTX *ctx, FILE *fp, bool *pfiledone);
 
 /*
  *     Error functions.
  */
-#ifdef _LIBRADIUS
-void           fr_strerror_printf(char const *, ...)
-#ifdef __GNUC__
-               __attribute__ ((format (printf, 1, 2)))
-#endif
-;
-#endif
-void           fr_perror(char const *, ...)
-#ifdef __GNUC__
-               __attribute__ ((format (printf, 1, 2)))
-#endif
-;
-char const *fr_syserror(int num);
-extern bool fr_assert_cond(char const *file, int line, char const *expr, bool cond);
-#define fr_assert(_x) fr_assert_cond(__FILE__,  __LINE__, #_x, (_x))
-
-extern void NEVER_RETURNS _fr_exit(char const *file, int line, int status);
-#define fr_exit(_x) _fr_exit(__FILE__,  __LINE__, (_x))
+void           fr_strerror_printf(char const *, ...) CC_HINT(format (printf, 1, 2));
+void           fr_perror(char const *, ...) CC_HINT(format (printf, 1, 2));
 
-extern void NEVER_RETURNS _fr_exit_now(char const *file, int line, int status);
-#define fr_exit_now(_x) _fr_exit_now(__FILE__,  __LINE__, (_x))
 
 extern char const *fr_strerror(void);
+extern char const *fr_syserror(int num);
 extern bool    fr_dns_lookups; /* do IP -> hostname lookups? */
 extern bool    fr_hostname_lookups; /* do hostname -> IP lookups? */
 extern int     fr_debug_flag;  /* 0 = no debugging information */
-extern int     fr_max_attributes; /* per incoming packet */
+extern uint32_t        fr_max_attributes; /* per incoming packet */
 #define        FR_MAX_PACKET_CODE (52)
 extern char const *fr_packet_codes[FR_MAX_PACKET_CODE];
+#define is_radius_code(_x) ((_x > 0) && (_x < FR_MAX_PACKET_CODE))
 extern FILE    *fr_log_fp;
 extern void rad_print_hex(RADIUS_PACKET *packet);
-void           fr_printf_log(char const *, ...)
-#ifdef __GNUC__
-               __attribute__ ((format (printf, 1, 2)))
-#endif
-;
+void           fr_printf_log(char const *, ...) CC_HINT(format (printf, 1, 2));
 
 /*
  *     Several handy miscellaneous functions.
  */
 int            fr_set_signal(int sig, sig_t func);
-TALLOC_CTX     *fr_autofree_ctx(void);
+int            fr_link_talloc_ctx_free(TALLOC_CTX *parent, TALLOC_CTX *child);
 char const     *fr_inet_ntop(int af, void const *src);
 char const     *ip_ntoa(char *, uint32_t);
+int            fr_pton4(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve, bool fallback);
+int            fr_pton6(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve, bool fallback);
+int            fr_pton(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve);
+bool           is_wildcard(fr_ipaddr_t *addr);
+int            fr_ntop(char *out, size_t outlen, fr_ipaddr_t *addr);
 char           *ifid_ntoa(char *buffer, size_t size, uint8_t const *ifid);
 uint8_t                *ifid_aton(char const *ifid_str, uint8_t *ifid);
 int            rad_lockfd(int fd, int lock_len);
 int            rad_lockfd_nonblock(int fd, int lock_len);
 int            rad_unlockfd(int fd, int lock_len);
+char           *fr_abin2hex(TALLOC_CTX *ctx, uint8_t const *bin, size_t inlen);
 size_t         fr_bin2hex(char *hex, uint8_t const *bin, size_t inlen);
-size_t         fr_hex2bin(uint8_t *bin, char const *hex, size_t outlen);
+size_t         fr_hex2bin(uint8_t *bin, size_t outlen, char const *hex, size_t inlen);
 uint32_t       fr_strtoul(char const *value, char **end);
-bool           fr_whitespace_check(char const *value);
-bool           fr_integer_check(char const *value);
+bool           is_whitespace(char const *value);
+bool           is_integer(char const *value);
+bool           is_zero(char const *value);
 
 int            fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b);
 
-int            ip_ptonx(char const *src, fr_ipaddr_t *dst);
-int            ip_hton(char const *src, int af, fr_ipaddr_t *dst);
+int            ip_hton(fr_ipaddr_t *out, int af, char const *hostname, bool fallback);
 char const     *ip_ntoh(fr_ipaddr_t const *src, char *dst, size_t cnt);
-int fr_ipaddr2sockaddr(fr_ipaddr_t const *ipaddr, int port,
-                      struct sockaddr_storage *sa, socklen_t *salen);
-int fr_sockaddr2ipaddr(struct sockaddr_storage const *sa, socklen_t salen,
-                      fr_ipaddr_t *ipaddr, int * port);
+struct in_addr fr_inaddr_mask(struct in_addr const *ipaddr, uint8_t prefix);
+struct in6_addr        fr_in6addr_mask(struct in6_addr const *ipaddr, uint8_t prefix);
+void           fr_ipaddr_mask(fr_ipaddr_t *addr, uint8_t prefix);
+int            fr_ipaddr2sockaddr(fr_ipaddr_t const *ipaddr, uint16_t port,
+                                  struct sockaddr_storage *sa, socklen_t *salen);
+int            fr_sockaddr2ipaddr(struct sockaddr_storage const *sa, socklen_t salen,
+                                  fr_ipaddr_t *ipaddr, uint16_t *port);
 ssize_t                fr_utf8_to_ucs2(uint8_t *out, size_t outlen, char const *in, size_t inlen);
+size_t         fr_prints_uint128(char *out, size_t outlen, uint128_t const num);
 int64_t                fr_pow(int32_t base, uint8_t exp);
 int            fr_get_time(char const *date_str, time_t *date);
-
+int8_t         fr_pointer_cmp(void const *a, void const *b);
+void           fr_quick_sort(void const *to_sort[], int min_idx, int max_idx, fr_cmp_t cmp);
 /*
  *     Define TALLOC_DEBUG to check overflows with talloc.
  *     we can't use valgrind, because the memory used by
  *     talloc is valid memory... just not for us.
  */
 #ifdef TALLOC_DEBUG
-void fr_talloc_verify_cb(const void *ptr, int depth,
-                        int max_depth, int is_ref,
-                        void *private_data);
+void           fr_talloc_verify_cb(const void *ptr, int depth,
+                                   int max_depth, int is_ref,
+                                   void *private_data);
 #define VERIFY_ALL_TALLOC talloc_report_depth_cb(NULL, 0, -1, fr_talloc_verify_cb, NULL)
 #else
 #define VERIFY_ALL_TALLOC
@@ -661,8 +693,8 @@ void fr_talloc_verify_cb(const void *ptr, int depth,
 
 #ifdef WITH_ASCEND_BINARY
 /* filters.c */
-int            ascend_parse_filter(VALUE_PAIR *vp, char const *value);
-void           print_abinary(VALUE_PAIR const *vp, char *buffer, size_t len, int8_t quote);
+int            ascend_parse_filter(VALUE_PAIR *vp, char const *value, size_t len);
+void           print_abinary(char *out, size_t outlen, uint8_t const *data, size_t len, int8_t quote);
 #endif /*WITH_ASCEND_BINARY*/
 
 /* random numbers in isaac.c */
@@ -670,22 +702,23 @@ void              print_abinary(VALUE_PAIR const *vp, char *buffer, size_t len, int8_t quote
 typedef struct fr_randctx {
        uint32_t randcnt;
        uint32_t randrsl[256];
-       uint32_t randmem[256];
+       uint32_t randmem[256];
        uint32_t randa;
        uint32_t randb;
        uint32_t randc;
 } fr_randctx;
 
-void fr_isaac(fr_randctx *ctx);
-void fr_randinit(fr_randctx *ctx, int flag);
-uint32_t fr_rand(void);        /* like rand(), but better. */
-void fr_rand_seed(void const *, size_t ); /* seed the random pool */
+void           fr_isaac(fr_randctx *ctx);
+void           fr_randinit(fr_randctx *ctx, int flag);
+uint32_t       fr_rand(void);  /* like rand(), but better. */
+void           fr_rand_seed(void const *, size_t ); /* seed the random pool */
 
 
 /* crypt wrapper from crypt.c */
-int fr_crypt_check(char const *key, char const *salt);
+int            fr_crypt_check(char const *key, char const *salt);
 
 /* cbuff.c */
+
 typedef struct fr_cbuff fr_cbuff_t;
 
 fr_cbuff_t     *fr_cbuff_alloc(TALLOC_CTX *ctx, uint32_t size, bool lock);
@@ -693,35 +726,80 @@ void              fr_cbuff_rp_insert(fr_cbuff_t *cbuff, void *obj);
 void           *fr_cbuff_rp_next(fr_cbuff_t *cbuff, TALLOC_CTX *ctx);
 
 /* debug.c */
+
+/** Optional callback passed to fr_fault_setup
+ *
+ * Allows optional logic to be run before calling the main fault handler.
+ *
+ * If the callback returns < 0, the main fault handler will not be called.
+ *
+ * @param signum signal raised.
+ * @return 0 on success < 0 on failure.
+ */
+typedef int (*fr_fault_cb_t)(int signum);
 typedef struct fr_bt_marker fr_bt_marker_t;
 
 void           fr_debug_break(void);
 void           backtrace_print(fr_cbuff_t *cbuff, void *obj);
+int            fr_backtrace_do(fr_bt_marker_t *marker);
 fr_bt_marker_t *fr_backtrace_attach(fr_cbuff_t **cbuff, TALLOC_CTX *obj);
+
+typedef void (*fr_fault_log_t)(char const *msg, ...) CC_HINT(format (printf, 1, 2));
+
+void           fr_panic_on_free(TALLOC_CTX *ctx);
+int            fr_set_dumpable_init(void);
+int            fr_set_dumpable(bool allow_core_dumps);
+int            fr_log_talloc_report(TALLOC_CTX *ctx);
+void           fr_fault(int sig);
 int            fr_fault_setup(char const *cmd, char const *program);
+void           fr_fault_set_cb(fr_fault_cb_t func);
+void           fr_fault_set_log_fn(fr_fault_log_t func);
+void           fr_fault_set_log_fd(int fd);
+
+#  ifdef WITH_VERIFY_PTR
+void           fr_verify_vp(char const *file, int line, VALUE_PAIR const *vp);
+void           fr_verify_list(char const *file, int line, TALLOC_CTX *expected, VALUE_PAIR *vps);
+#  endif
+
+bool           fr_assert_cond(char const *file, int line, char const *expr, bool cond);
+#  define      fr_assert(_x) fr_assert_cond(__FILE__,  __LINE__, #_x, (_x))
+
+void           NEVER_RETURNS _fr_exit(char const *file, int line, int status);
+#  define      fr_exit(_x) _fr_exit(__FILE__,  __LINE__, (_x))
+
+void           NEVER_RETURNS _fr_exit_now(char const *file, int line, int status);
+#  define      fr_exit_now(_x) _fr_exit_now(__FILE__,  __LINE__, (_x))
 
 /* rbtree.c */
 typedef struct rbtree_t rbtree_t;
 typedef struct rbnode_t rbnode_t;
 
+/* callback order for walking  */
+typedef enum {
+       RBTREE_PRE_ORDER,
+       RBTREE_IN_ORDER,
+       RBTREE_POST_ORDER,
+       RBTREE_DELETE_ORDER
+} rb_order_t;
+
 #define RBTREE_FLAG_NONE    (0)
 #define RBTREE_FLAG_REPLACE (1 << 0)
 #define RBTREE_FLAG_LOCK    (1 << 1)
-rbtree_t       *rbtree_create(int (*Compare)(void const *, void const *),
-                             void (*freeNode)(void *),
-                             int flags);
+
+typedef int (*rb_comparator_t)(void const *ctx, void const *data);
+typedef int (*rb_walker_t)(void *ctx, void *data);
+typedef void (*rb_free_t)(void *data);
+
+rbtree_t       *rbtree_create(TALLOC_CTX *ctx, rb_comparator_t compare, rb_free_t node_free, int flags);
 void           rbtree_free(rbtree_t *tree);
-bool           rbtree_insert(rbtree_t *tree, void *Data);
-void           rbtree_delete(rbtree_t *tree, rbnode_t *Z);
+bool           rbtree_insert(rbtree_t *tree, void *data);
+rbnode_t       *rbtree_insert_node(rbtree_t *tree, void *data);
+void           rbtree_delete(rbtree_t *tree, rbnode_t *z);
 bool           rbtree_deletebydata(rbtree_t *tree, void const *data);
-rbnode_t       *rbtree_find(rbtree_t *tree, void const *Data);
-void          *rbtree_finddata(rbtree_t *tree, void const *Data);
-int            rbtree_num_elements(rbtree_t *tree);
-void          *rbtree_min(rbtree_t *tree);
-void          *rbtree_node2data(rbtree_t *tree, rbnode_t *node);
-
-/* callback order for walking  */
-typedef enum { PreOrder, InOrder, PostOrder, DeleteOrder } RBTREE_ORDER;
+rbnode_t       *rbtree_find(rbtree_t *tree, void const *data);
+void           *rbtree_finddata(rbtree_t *tree, void const *data);
+uint32_t       rbtree_num_elements(rbtree_t *tree);
+void           *rbtree_node2data(rbtree_t *tree, rbnode_t *node);
 
 /*
  *     The callback should be declared as:
@@ -729,49 +807,29 @@ typedef enum { PreOrder, InOrder, PostOrder, DeleteOrder } RBTREE_ORDER;
  *
  *     The "context" is some user-defined context.
  *     The "data" is the pointer to the user data in the node,
- *       NOT the node itself.
+ *     NOT the node itself.
  *
  *     It should return 0 if all is OK, and !0 for any error.
  *     The walking will stop on any error.
  *
- *     Except with DeleteOrder, where the callback should return <0 for
+ *     Except with RBTREE_DELETE_ORDER, where the callback should return <0 for
  *     errors, and may return 1 to delete the current node and halt,
  *     or 2 to delete the current node and continue.  This may be
  *     used to batch-delete select nodes from a locked rbtree.
  */
-int rbtree_walk(rbtree_t *tree, RBTREE_ORDER order, int (*callback)(void *, void *), void *context);
-
-/*
- *     Find a matching data item in an rbtree and, if one is found,
- *     perform a callback on it.
- *
- *     The callback is similar to rbtree_walk above, except that a
- *     positive return code from the callback will cause the found node
- *     to be deleted from the tree.  If the tree was created with
- *     RBTREE_FLAG_LOCK, then the entire find/callback/delete/rebalance
- *     sequence happens while the lock is held.
- *
- *     Note that the callback MUST NOT alter any of the data which
- *     is used as the rbtree key, nor attempt to alter the rest of
- *     the rbtree in any way.
- *
- *     Returns a pointer to the user data in the found node, or NULL if the
- *     item was not found, or NULL if the item was deleted and the tree was
- *     created with a freeNode garbage collection routine.
- */
-void *rbtree_callbydata(rbtree_t *tree, void const *Data, int (*callback)(void *, void *), void *context);
+int            rbtree_walk(rbtree_t *tree, rb_order_t order, rb_walker_t compare, void *context);
 
 /*
  *     FIFOs
  */
-typedef struct fr_fifo_t fr_fifo_t;
+typedef struct fr_fifo_t fr_fifo_t;
 typedef void (*fr_fifo_free_t)(void *);
-fr_fifo_t *fr_fifo_create(int max_entries, fr_fifo_free_t freeNode);
-void fr_fifo_free(fr_fifo_t *fi);
-int fr_fifo_push(fr_fifo_t *fi, void *data);
-void *fr_fifo_pop(fr_fifo_t *fi);
-void *fr_fifo_peek(fr_fifo_t *fi);
-int fr_fifo_num_elements(fr_fifo_t *fi);
+fr_fifo_t      *fr_fifo_create(int max_entries, fr_fifo_free_t freeNode);
+void           fr_fifo_free(fr_fifo_t *fi);
+int            fr_fifo_push(fr_fifo_t *fi, void *data);
+void           *fr_fifo_pop(fr_fifo_t *fi);
+void           *fr_fifo_peek(fr_fifo_t *fi);
+int            fr_fifo_num_elements(fr_fifo_t *fi);
 
 #ifdef __cplusplus
 }
@@ -780,7 +838,7 @@ int fr_fifo_num_elements(fr_fifo_t *fi);
 #include <freeradius-devel/packet.h>
 
 #ifdef WITH_TCP
-#include <freeradius-devel/tcp.h>
+#  include <freeradius-devel/tcp.h>
 #endif
 
 #endif /*LIBRADIUS_H*/