2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * @brief Structures and prototypes for the radius library.
24 * @copyright 1999-2014 The FreeRADIUS server project
26 RCSIDH(libradius_h, "$Id$")
29 * Compiler hinting macros. Included here for 3rd party consumers
32 #include <freeradius-devel/build.h>
35 * Let any external program building against the library know what
36 * features the library was built with.
38 #include <freeradius-devel/features.h>
40 #ifdef WITHOUT_VERSION_CHECK
41 # define RADIUSD_MAGIC_NUMBER ((uint64_t) (0xf4ee4ad3f4ee4ad3))
42 # define MAGIC_PREFIX(_x) ((uint8_t) 0x00)
43 # define MAGIC_VERSION(_x) ((uint32_t) 0x00000000)
44 # define MAGIC_COMMIT(_x) ((uint32_t) 0x00000000)
46 # ifdef RADIUSD_VERSION_COMMIT
47 # define RADIUSD_MAGIC_NUMBER ((uint64_t) HEXIFY4(f4, RADIUSD_VERSION, RADIUSD_VERSION_COMMIT, 0))
49 # define RADIUSD_MAGIC_NUMBER ((uint64_t) HEXIFY3(f4, RADIUSD_VERSION, 00000000))
51 # define MAGIC_PREFIX(_x) ((uint8_t) (_x >> 56))
52 # define MAGIC_VERSION(_x) ((uint32_t) ((_x >> 32) & 0x00ffffff))
53 # define MAGIC_COMMIT(_x) ((uint32_t) (_x & 0xffffffff))
57 * Talloc memory allocation is used in preference to malloc throughout
58 * the libraries and server.
63 * Defines signatures for any missing functions.
65 #include <freeradius-devel/missing.h>
68 * Include system headers.
80 #include <freeradius-devel/threads.h>
81 #include <freeradius-devel/radius.h>
82 #include <freeradius-devel/token.h>
83 #include <freeradius-devel/hash.h>
85 #ifdef SIZEOF_UNSIGNED_INT
86 # if SIZEOF_UNSIGNED_INT != 4
87 # error FATAL: sizeof(unsigned int) != 4
92 * Include for modules.
94 #include <freeradius-devel/sha1.h>
95 #include <freeradius-devel/md4.h>
101 #if defined(WITH_VERIFY_PTR)
102 # define FREE_MAGIC (0xF4EEF4EE)
106 * Add if (_x->da) (void) talloc_get_type_abort(_x->da, DICT_ATTR);
107 * to the macro below when dictionaries are talloced.
109 # define VERIFY_VP(_x) fr_verify_vp(__FILE__, __LINE__, _x)
110 # define VERIFY_LIST(_x) fr_verify_list(__FILE__, __LINE__, NULL, _x)
111 # define VERIFY_PACKET(_x) (void) talloc_get_type_abort(_x, RADIUS_PACKET)
114 * Even if were building without WITH_VERIFY_PTR
115 * the pointer must not be NULL when these various macros are used
116 * so we can add some sneaky soft asserts.
118 # define VERIFY_VP(_x) fr_assert(_x)
119 # define VERIFY_LIST(_x) fr_assert(_x)
120 # define VERIFY_PACKET(_x) fr_assert(_x)
123 #define AUTH_VECTOR_LEN 16
124 #define CHAP_VALUE_LENGTH 16
125 #define MAX_STRING_LEN 254 /* RFC2138: string 0-253 octets */
126 #define FR_MAX_VENDOR (1 << 24) /* RFC limitations */
129 # define AUTH_HDR_LEN 20
130 # define VENDORPEC_USR 429
131 #define VENDORPEC_LUCENT 4846
132 #define VENDORPEC_STARENT 8164
133 # define DEBUG if (fr_debug_flag && fr_log_fp) fr_printf_log
134 # define debug_pair(vp) do { if (fr_debug_flag && fr_log_fp) { \
135 vp_print(fr_log_fp, vp); \
140 #define TAG_VALID(x) ((x) > 0 && (x) < 0x20)
141 #define TAG_VALID_ZERO(x) ((x) < 0x20)
142 #define TAG_ANY INT8_MIN
144 /** Check if tags are equal
146 * @param _x tag were matching on.
147 * @param _y tag belonging to the attribute were checking.
149 #define TAG_EQ(_x, _y) ((_x == _y) || (_x == TAG_ANY) || ((_x == TAG_NONE) && (_y == TAG_ANY)))
150 #define ATTRIBUTE_EQ(_x, _y) ((_x && _y) && (_x->da == _y->da) && (!_x->da->flags.has_tag || TAG_EQ(_x->tag, _y->tag)))
152 #define NUM_ANY INT_MIN
153 #define NUM_JOIN (INT_MIN + 1)
154 #define NUM_COUNT (INT_MIN + 2)
156 #define PAD(_x, _y) (_y - ((_x) % _y))
158 #define PRINTF_LIKE(n) CC_HINT(format(printf, n, n+1))
159 #define NEVER_RETURNS CC_HINT(noreturn)
160 #define UNUSED CC_HINT(unused)
161 #define BLANK_FORMAT " " /* GCC_LINT whines about empty formats */
163 typedef struct attr_flags {
164 unsigned int is_unknown : 1; //!< Attribute number or vendor is unknown.
165 unsigned int is_tlv : 1; //!< Is a sub attribute.
166 unsigned int vp_free : 1; //!< Should be freed when VALUE_PAIR is freed.
168 unsigned int has_tag : 1; //!< Tagged attribute.
169 unsigned int array : 1; //!< Pack multiples into 1 attr.
170 unsigned int has_value : 1; //!< Has a value.
171 unsigned int has_value_alias : 1; //!< Has a value alias.
172 unsigned int has_tlv : 1; //!< Has sub attributes.
174 unsigned int extended : 1; //!< Extended attribute.
175 unsigned int long_extended : 1; //!< Long format.
176 unsigned int evs : 1; //!< Extended VSA.
177 unsigned int wimax: 1; //!< WiMAX format=1,1,c.
179 unsigned int concat : 1; //!< concatenate multiple instances
180 unsigned int is_pointer : 1; //!< data is a pointer
182 uint8_t encrypt; //!< Ecryption method.
187 * Values of the encryption flags.
189 #define FLAG_ENCRYPT_NONE (0)
190 #define FLAG_ENCRYPT_USER_PASSWORD (1)
191 #define FLAG_ENCRYPT_TUNNEL_PASSWORD (2)
192 #define FLAG_ENCRYPT_ASCEND_SECRET (3)
194 extern const FR_NAME_NUMBER dict_attr_types[];
195 extern const size_t dict_attr_sizes[PW_TYPE_MAX][2];
197 /** dictionary attribute
200 typedef struct dict_attr {
208 /** value of an enumerated attribute
211 typedef struct dict_value {
218 /** dictionary vendor
221 typedef struct dict_vendor {
222 unsigned int vendorpec;
223 size_t type; //!< Length of type data
224 size_t length; //!< Length of length data
229 /** Union containing all data types supported by the server
231 * This union contains all data types that can be represented by VALUE_PAIRs. It may also be used in other parts
232 * of the server where values of different types need to be stored.
234 * PW_TYPE should be an enumeration of the values in this union.
236 typedef union value_data {
237 char const *strvalue; //!< Pointer to UTF-8 string.
238 uint8_t const *octets; //!< Pointer to binary string.
239 uint32_t integer; //!< 32bit unsigned integer.
240 struct in_addr ipaddr; //!< IPv4 Address.
241 uint32_t date; //!< Date (32bit Unix timestamp).
242 size_t filter[32/sizeof(size_t)]; //!< Ascend binary format a packed data
245 uint8_t ifid[8]; //!< IPv6 interface ID (should be struct?).
246 struct in6_addr ipv6addr; //!< IPv6 Address.
247 uint8_t ipv6prefix[18]; //!< IPv6 prefix (should be struct?).
249 uint8_t byte; //!< 8bit unsigned integer.
250 uint16_t ushort; //!< 16bit unsigned integer.
252 uint8_t ether[6]; //!< Ethernet (MAC) address.
254 int32_t sinteger; //!< 32bit signed integer.
255 uint64_t integer64; //!< 64bit unsigned integer.
257 uint8_t ipv4prefix[6]; //!< IPv4 prefix (should be struct?).
259 uint8_t *tlv; //!< Nested TLV (should go away).
260 void const *ptr; //!< generic pointer.
263 /** The type of value a VALUE_PAIR contains
265 * This is used to add structure to nested VALUE_PAIRs and specifies what type of node it is (set, list, data).
267 * xlat is another type of data node which must first be expanded before use.
269 typedef enum value_type {
270 VT_NONE = 0, //!< VALUE_PAIR has no value.
271 VT_SET, //!< VALUE_PAIR has children.
272 VT_LIST, //!< VALUE_PAIR has multiple values.
273 VT_DATA, //!< VALUE_PAIR has a single value.
274 VT_XLAT //!< valuepair value must be xlat expanded when it's
275 //!< added to VALUE_PAIR tree.
278 /** Stores an attribute, a value and various bits of other data
280 * VALUE_PAIRs are the main data structure used in the server
282 * They also specify what behaviour should be used when the attribute is merged into a new list/tree.
284 typedef struct value_pair {
285 DICT_ATTR const *da; //!< Dictionary attribute defines the attribute
286 //!< number, vendor and type of the attribute.
288 struct value_pair *next;
290 FR_TOKEN op; //!< Operator to use when moving or inserting
291 //!< valuepair into a list.
293 int8_t tag; //!< Tag value used to group valuepairs.
296 // VALUE_SET *set; //!< Set of child attributes.
297 // VALUE_LIST *list; //!< List of values for
298 //!< multivalued attribute.
299 // value_data_t *data; //!< Value data for this attribute.
301 char const *xlat; //!< Source string for xlat expansion.
304 value_type_t type; //!< Type of pointer in value union.
306 size_t length; //!< of Data field.
310 /** Abstraction to allow iterating over different configurations of VALUE_PAIRs
312 * This allows functions which do not care about the structure of collections of VALUE_PAIRs
313 * to iterate over all members in a collection.
315 * Field within a vp_cursor should not be accessed directly, and vp_cursors should only be
316 * manipulated with the pair* functions.
318 typedef struct vp_cursor {
320 VALUE_PAIR *found; //!< pairfind marker.
321 VALUE_PAIR *last; //!< Temporary only used for fr_cursor_insert
322 VALUE_PAIR *current; //!< The current attribute.
323 VALUE_PAIR *next; //!< Next attribute to process.
326 /** A VALUE_PAIR in string format.
328 * Used to represent pairs in the legacy 'users' file format.
330 typedef struct value_pair_raw {
331 char l_opand[256]; //!< Left hand side of the pair.
332 char r_opand[1024]; //!< Right hand side of the pair.
334 FR_TOKEN quote; //!< Type of quoting around the r_opand.
336 FR_TOKEN op; //!< Operator.
339 #define vp_strvalue data.strvalue
340 #define vp_integer data.integer
341 #define vp_ipaddr data.ipaddr.s_addr
342 #define vp_date data.date
343 #define vp_filter data.filter
344 #define vp_octets data.octets
345 #define vp_ifid data.ifid
346 #define vp_ipv6addr data.ipv6addr
347 #define vp_ipv6prefix data.ipv6prefix
348 #define vp_byte data.byte
349 #define vp_short data.ushort
350 #define vp_ether data.ether
351 #define vp_signed data.sinteger
352 #define vp_integer64 data.integer64
353 #define vp_ipv4prefix data.ipv4prefix
354 #define vp_tlv data.tlv
356 typedef struct fr_ipaddr_t {
357 int af; /* address family */
359 struct in_addr ip4addr;
360 struct in6_addr ip6addr; /* maybe defined in missing.h */
363 uint32_t scope; /* for IPv6 */
367 * vector: Request authenticator from access-request packet
368 * Put in there by rad_decode, and must be put in the
369 * response RADIUS_PACKET as well before calling rad_send
371 * verified: Filled in by rad_decode for accounting-request packets
373 * data,data_len: Used between rad_recv and rad_decode.
375 typedef struct radius_packet {
377 fr_ipaddr_t src_ipaddr;
378 fr_ipaddr_t dst_ipaddr;
383 uint8_t vector[AUTH_VECTOR_LEN];
384 struct timeval timestamp;
396 DECODE_FAIL_NONE = 0,
397 DECODE_FAIL_MIN_LENGTH_PACKET,
398 DECODE_FAIL_MIN_LENGTH_FIELD,
399 DECODE_FAIL_MIN_LENGTH_MISMATCH,
400 DECODE_FAIL_HEADER_OVERFLOW,
401 DECODE_FAIL_UNKNOWN_PACKET_CODE,
402 DECODE_FAIL_INVALID_ATTRIBUTE,
403 DECODE_FAIL_ATTRIBUTE_TOO_SHORT,
404 DECODE_FAIL_ATTRIBUTE_OVERFLOW,
405 DECODE_FAIL_MA_INVALID_LENGTH,
406 DECODE_FAIL_ATTRIBUTE_UNDERFLOW,
407 DECODE_FAIL_TOO_MANY_ATTRIBUTES,
408 DECODE_FAIL_MA_MISSING,
415 int fr_check_lib_magic(uint64_t magic);
418 * Printing functions.
420 int fr_utf8_char(uint8_t const *str);
421 size_t fr_print_string(char const *in, size_t inlen,
422 char *out, size_t outlen);
423 size_t fr_print_string_len(char const *in, size_t inlen);
425 #define is_truncated(_ret, _max) ((_ret) >= (_max))
426 #define truncate_len(_ret, _max) (((_ret) >= (_max)) ? ((_max) - 1) : _ret)
427 size_t vp_data_prints_value(char *out, size_t outlen,
428 DICT_ATTR const *da, value_data_t const *data, size_t data_len, int8_t quote);
429 size_t vp_prints_value(char *out, size_t outlen, VALUE_PAIR const *vp, int8_t quote);
430 size_t vp_prints_value_json(char *out, size_t outlen, VALUE_PAIR const *vp);
431 size_t vp_prints(char *out, size_t outlen, VALUE_PAIR const *vp);
432 void vp_print(FILE *, VALUE_PAIR const *);
433 void vp_printlist(FILE *, VALUE_PAIR const *);
434 char *vp_aprint_type(TALLOC_CTX *ctx, PW_TYPE type);
435 char *vp_aprint_value(TALLOC_CTX *ctx, VALUE_PAIR const *vp);
436 char *vp_aprint(TALLOC_CTX *ctx, VALUE_PAIR const *vp);
437 #define fprint_attr_val vp_print
440 * Dictionary functions.
442 extern const int dict_attr_allowed_chars[256];
443 int str2argv(char *str, char **argv, int max_argc);
444 int dict_str2oid(char const *ptr, unsigned int *pattr,
445 unsigned int *pvendor, int tlv_depth);
446 int dict_addvendor(char const *name, unsigned int value);
447 int dict_addattr(char const *name, int attr, unsigned int vendor, PW_TYPE type, ATTR_FLAGS flags);
448 int dict_addvalue(char const *namestr, char const *attrstr, int value);
449 int dict_init(char const *dir, char const *fn);
450 void dict_free(void);
451 int dict_read(char const *dir, char const *filename);
452 void dict_attr_free(DICT_ATTR const **da);
453 DICT_ATTR const *dict_attr_copy(DICT_ATTR const *da, int vp_free);
454 DICT_ATTR const *dict_attrunknown(unsigned int attr, unsigned int vendor, int vp_free);
455 DICT_ATTR const *dict_attrunknownbyname(char const *attribute, int vp_free);
456 DICT_ATTR const *dict_attrbyvalue(unsigned int attr, unsigned int vendor);
457 DICT_ATTR const *dict_attrbyname(char const *attr);
458 DICT_ATTR const *dict_attrbytagged_name(char const *name);
459 DICT_ATTR const *dict_attrbytype(unsigned int attr, unsigned int vendor,
461 DICT_ATTR const *dict_attrbyparent(DICT_ATTR const *parent, unsigned int attr,
462 unsigned int vendor);
463 int dict_attr_child(DICT_ATTR const *parent,
464 unsigned int *pattr, unsigned int *pvendor);
465 DICT_VALUE *dict_valbyattr(unsigned int attr, unsigned int vendor, int val);
466 DICT_VALUE *dict_valbyname(unsigned int attr, unsigned int vendor, char const *val);
467 char const *dict_valnamebyattr(unsigned int attr, unsigned int vendor, int value);
468 int dict_vendorbyname(char const *name);
469 DICT_VENDOR *dict_vendorbyvalue(int vendor);
471 #if 1 /* FIXME: compat */
472 #define dict_attrget dict_attrbyvalue
473 #define dict_attrfind dict_attrbyname
474 #define dict_valfind dict_valbyname
475 /*#define dict_valget dict_valbyattr almost but not quite*/
479 void fr_md5_calc(uint8_t *, uint8_t const *, unsigned int);
482 int rad_send(RADIUS_PACKET *, RADIUS_PACKET const *, char const *secret);
483 bool rad_packet_ok(RADIUS_PACKET *packet, int flags, decode_fail_t *reason);
484 RADIUS_PACKET *rad_recv(int fd, int flags);
485 ssize_t rad_recv_header(int sockfd, fr_ipaddr_t *src_ipaddr, uint16_t *src_port, int *code);
486 void rad_recv_discard(int sockfd);
487 int rad_verify(RADIUS_PACKET *packet, RADIUS_PACKET *original,
489 int rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original, char const *secret);
490 int rad_encode(RADIUS_PACKET *packet, RADIUS_PACKET const *original,
492 int rad_sign(RADIUS_PACKET *packet, RADIUS_PACKET const *original,
495 int rad_digest_cmp(uint8_t const *a, uint8_t const *b, size_t length);
496 RADIUS_PACKET *rad_alloc(TALLOC_CTX *ctx, bool new_vector);
497 RADIUS_PACKET *rad_alloc_reply(TALLOC_CTX *ctx, RADIUS_PACKET *);
498 RADIUS_PACKET *rad_copy_packet(TALLOC_CTX *ctx, RADIUS_PACKET const *in);
500 void rad_free(RADIUS_PACKET **);
501 int rad_pwencode(char *encpw, size_t *len, char const *secret,
502 uint8_t const *vector);
503 int rad_pwdecode(char *encpw, size_t len, char const *secret,
504 uint8_t const *vector);
506 #define FR_TUNNEL_PW_ENC_LENGTH(_x) (2 + 1 + _x + PAD(_x + 1, 16))
507 int rad_tunnel_pwencode(char *encpw, size_t *len, char const *secret,
508 uint8_t const *vector);
509 int rad_tunnel_pwdecode(uint8_t *encpw, size_t *len,
510 char const *secret, uint8_t const *vector);
511 int rad_chap_encode(RADIUS_PACKET *packet, uint8_t *output,
512 int id, VALUE_PAIR *password);
514 int rad_attr_ok(RADIUS_PACKET const *packet, RADIUS_PACKET const *original,
515 DICT_ATTR *da, uint8_t const *data, size_t length);
516 int rad_tlv_ok(uint8_t const *data, size_t length,
517 size_t dv_type, size_t dv_length);
519 ssize_t data2vp(TALLOC_CTX *ctx,
520 RADIUS_PACKET *packet, RADIUS_PACKET const *original,
522 DICT_ATTR const *da, uint8_t const *start,
523 size_t const attrlen, size_t const packetlen,
526 ssize_t rad_attr2vp(TALLOC_CTX *ctx,
527 RADIUS_PACKET *packet, RADIUS_PACKET const *original,
529 uint8_t const *data, size_t length,
532 ssize_t rad_vp2data(uint8_t const **out, VALUE_PAIR const *vp);
534 int rad_vp2extended(RADIUS_PACKET const *packet,
535 RADIUS_PACKET const *original,
536 char const *secret, VALUE_PAIR const **pvp,
537 uint8_t *ptr, size_t room);
538 int rad_vp2wimax(RADIUS_PACKET const *packet,
539 RADIUS_PACKET const *original,
540 char const *secret, VALUE_PAIR const **pvp,
541 uint8_t *ptr, size_t room);
543 int rad_vp2vsa(RADIUS_PACKET const *packet, RADIUS_PACKET const *original,
544 char const *secret, VALUE_PAIR const **pvp, uint8_t *start,
547 int rad_vp2rfc(RADIUS_PACKET const *packet,
548 RADIUS_PACKET const *original,
549 char const *secret, VALUE_PAIR const **pvp,
550 uint8_t *ptr, size_t room);
552 int rad_vp2attr(RADIUS_PACKET const *packet,
553 RADIUS_PACKET const *original, char const *secret,
554 VALUE_PAIR const **pvp, uint8_t *ptr, size_t room);
557 VALUE_PAIR *pairalloc(TALLOC_CTX *ctx, DICT_ATTR const *da);
558 VALUE_PAIR *paircreate(TALLOC_CTX *ctx, unsigned int attr, unsigned int vendor);
559 int pair2unknown(VALUE_PAIR *vp);
560 void pairfree(VALUE_PAIR **);
561 VALUE_PAIR *pairfind(VALUE_PAIR *, unsigned int attr, unsigned int vendor, int8_t tag);
562 VALUE_PAIR *pairfind_da(VALUE_PAIR *, DICT_ATTR const *da, int8_t tag);
564 #define fr_cursor_init(_x, _y) _fr_cursor_init(_x,(VALUE_PAIR const * const *) _y)
565 VALUE_PAIR *_fr_cursor_init(vp_cursor_t *cursor, VALUE_PAIR const * const *node);
566 void fr_cursor_copy(vp_cursor_t *out, vp_cursor_t *in);
567 VALUE_PAIR *fr_cursor_first(vp_cursor_t *cursor);
568 VALUE_PAIR *fr_cursor_last(vp_cursor_t *cursor);
569 VALUE_PAIR *fr_cursor_next_by_num(vp_cursor_t *cursor, unsigned int attr, unsigned int vendor, int8_t tag);
571 VALUE_PAIR *fr_cursor_next_by_da(vp_cursor_t *cursor, DICT_ATTR const *da, int8_t tag)
574 VALUE_PAIR *fr_cursor_next(vp_cursor_t *cursor);
575 VALUE_PAIR *fr_cursor_next_peek(vp_cursor_t *cursor);
576 VALUE_PAIR *fr_cursor_current(vp_cursor_t *cursor);
577 void fr_cursor_insert(vp_cursor_t *cursor, VALUE_PAIR *vp);
578 VALUE_PAIR *fr_cursor_remove(vp_cursor_t *cursor);
579 VALUE_PAIR *fr_cursor_replace(vp_cursor_t *cursor, VALUE_PAIR *new);
580 void pairdelete(VALUE_PAIR **, unsigned int attr, unsigned int vendor, int8_t tag);
581 void pairadd(VALUE_PAIR **, VALUE_PAIR *);
582 void pairreplace(VALUE_PAIR **first, VALUE_PAIR *add);
583 int8_t paircmp_value(VALUE_PAIR const *a, VALUE_PAIR const *b);
584 int8_t paircmp_op(VALUE_PAIR const *a, FR_TOKEN op, VALUE_PAIR const *b);
585 int8_t paircmp(VALUE_PAIR *a, VALUE_PAIR *b);
586 int8_t pairlistcmp(VALUE_PAIR *a, VALUE_PAIR *b);
588 typedef int8_t (*fr_cmp_t)(void const *a, void const *b);
589 int8_t attrcmp(void const *a, void const *b);
590 int8_t attrtagcmp(void const *a, void const *b);
591 void pairsort(VALUE_PAIR **vps, fr_cmp_t cmp);
592 void pairvalidate_debug(TALLOC_CTX *ctx, VALUE_PAIR const *failed[2]);
593 bool pairvalidate(VALUE_PAIR const *failed[2], VALUE_PAIR *filter, VALUE_PAIR *list);
594 bool pairvalidate_relaxed(VALUE_PAIR const *failed[2], VALUE_PAIR *filter, VALUE_PAIR *list);
595 VALUE_PAIR *paircopyvp(TALLOC_CTX *ctx, VALUE_PAIR const *vp);
596 VALUE_PAIR *paircopy(TALLOC_CTX *ctx, VALUE_PAIR *from);
597 VALUE_PAIR *paircopy2(TALLOC_CTX *ctx, VALUE_PAIR *from, unsigned int attr, unsigned int vendor, int8_t tag);
598 VALUE_PAIR *pairsteal(TALLOC_CTX *ctx, VALUE_PAIR *from);
599 void pairmemcpy(VALUE_PAIR *vp, uint8_t const * src, size_t len);
600 void pairmemsteal(VALUE_PAIR *vp, uint8_t const *src);
601 void pairstrsteal(VALUE_PAIR *vp, char const *src);
602 void pairstrcpy(VALUE_PAIR *vp, char const * src);
603 void pairstrncpy(VALUE_PAIR *vp, char const * src, size_t len);
604 int pairdatacpy(VALUE_PAIR *vp, DICT_ATTR const *da, value_data_t const *data, size_t len);
605 void pairsprintf(VALUE_PAIR *vp, char const * fmt, ...) CC_HINT(format (printf, 2, 3));
606 void pairmove(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from);
607 void pairfilter(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from,
608 unsigned int attr, unsigned int vendor, int8_t tag);
609 VALUE_PAIR *pairmake_ip(TALLOC_CTX *ctx, char const *value,
610 DICT_ATTR *ipv4, DICT_ATTR *ipv6, DICT_ATTR *ipv4_prefix, DICT_ATTR *ipv6_prefix);
611 int pairparsevalue(VALUE_PAIR *vp, char const *value, size_t len);
612 VALUE_PAIR *pairmake(TALLOC_CTX *ctx, VALUE_PAIR **vps, char const *attribute, char const *value, FR_TOKEN op);
613 int pairmark_xlat(VALUE_PAIR *vp, char const *value);
614 FR_TOKEN pairread(char const **ptr, VALUE_PAIR_RAW *raw);
615 FR_TOKEN userparse(TALLOC_CTX *ctx, char const *buffer, VALUE_PAIR **head);
616 int readvp2(VALUE_PAIR **out, TALLOC_CTX *ctx, FILE *fp, bool *pfiledone);
621 void fr_strerror_printf(char const *, ...) CC_HINT(format (printf, 1, 2));
622 void fr_perror(char const *, ...) CC_HINT(format (printf, 1, 2));
625 extern char const *fr_strerror(void);
626 extern char const *fr_syserror(int num);
627 extern bool fr_dns_lookups; /* do IP -> hostname lookups? */
628 extern bool fr_hostname_lookups; /* do hostname -> IP lookups? */
629 extern int fr_debug_flag; /* 0 = no debugging information */
630 extern uint32_t fr_max_attributes; /* per incoming packet */
631 #define FR_MAX_PACKET_CODE (52)
632 extern char const *fr_packet_codes[FR_MAX_PACKET_CODE];
633 #define is_radius_code(_x) ((_x > 0) && (_x < FR_MAX_PACKET_CODE))
634 extern FILE *fr_log_fp;
635 extern void rad_print_hex(RADIUS_PACKET *packet);
636 void fr_printf_log(char const *, ...) CC_HINT(format (printf, 1, 2));
639 * Several handy miscellaneous functions.
641 int fr_set_signal(int sig, sig_t func);
642 int fr_link_talloc_ctx_free(TALLOC_CTX *parent, TALLOC_CTX *child);
643 char const *fr_inet_ntop(int af, void const *src);
644 char const *ip_ntoa(char *, uint32_t);
645 int fr_pton4(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve, bool fallback);
646 int fr_pton6(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve, bool fallback);
647 int fr_pton(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve);
648 bool is_wildcard(fr_ipaddr_t *addr);
649 int fr_ntop(char *out, size_t outlen, fr_ipaddr_t *addr);
650 char *ifid_ntoa(char *buffer, size_t size, uint8_t const *ifid);
651 uint8_t *ifid_aton(char const *ifid_str, uint8_t *ifid);
652 int rad_lockfd(int fd, int lock_len);
653 int rad_lockfd_nonblock(int fd, int lock_len);
654 int rad_unlockfd(int fd, int lock_len);
655 char *fr_abin2hex(TALLOC_CTX *ctx, uint8_t const *bin, size_t inlen);
656 size_t fr_bin2hex(char *hex, uint8_t const *bin, size_t inlen);
657 size_t fr_hex2bin(uint8_t *bin, size_t outlen, char const *hex, size_t inlen);
658 uint32_t fr_strtoul(char const *value, char **end);
659 bool is_whitespace(char const *value);
660 bool is_integer(char const *value);
661 bool is_zero(char const *value);
663 int fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b);
665 int ip_hton(fr_ipaddr_t *out, int af, char const *hostname, bool fallback);
666 char const *ip_ntoh(fr_ipaddr_t const *src, char *dst, size_t cnt);
667 struct in_addr fr_inaddr_mask(struct in_addr const *ipaddr, uint8_t prefix);
668 struct in6_addr fr_in6addr_mask(struct in6_addr const *ipaddr, uint8_t prefix);
669 void fr_ipaddr_mask(fr_ipaddr_t *addr, uint8_t prefix);
670 int fr_ipaddr2sockaddr(fr_ipaddr_t const *ipaddr, uint16_t port,
671 struct sockaddr_storage *sa, socklen_t *salen);
672 int fr_sockaddr2ipaddr(struct sockaddr_storage const *sa, socklen_t salen,
673 fr_ipaddr_t *ipaddr, uint16_t *port);
674 ssize_t fr_utf8_to_ucs2(uint8_t *out, size_t outlen, char const *in, size_t inlen);
675 size_t fr_prints_uint128(char *out, size_t outlen, uint128_t const num);
676 int64_t fr_pow(int32_t base, uint8_t exp);
677 int fr_get_time(char const *date_str, time_t *date);
678 int8_t fr_pointer_cmp(void const *a, void const *b);
679 void fr_quick_sort(void const *to_sort[], int min_idx, int max_idx, fr_cmp_t cmp);
681 * Define TALLOC_DEBUG to check overflows with talloc.
682 * we can't use valgrind, because the memory used by
683 * talloc is valid memory... just not for us.
686 void fr_talloc_verify_cb(const void *ptr, int depth,
687 int max_depth, int is_ref,
689 #define VERIFY_ALL_TALLOC talloc_report_depth_cb(NULL, 0, -1, fr_talloc_verify_cb, NULL)
691 #define VERIFY_ALL_TALLOC
694 #ifdef WITH_ASCEND_BINARY
696 int ascend_parse_filter(VALUE_PAIR *vp, char const *value, size_t len);
697 void print_abinary(char *out, size_t outlen, uint8_t const *data, size_t len, int8_t quote);
698 #endif /*WITH_ASCEND_BINARY*/
700 /* random numbers in isaac.c */
701 /* context of random number generator */
702 typedef struct fr_randctx {
704 uint32_t randrsl[256];
705 uint32_t randmem[256];
711 void fr_isaac(fr_randctx *ctx);
712 void fr_randinit(fr_randctx *ctx, int flag);
713 uint32_t fr_rand(void); /* like rand(), but better. */
714 void fr_rand_seed(void const *, size_t ); /* seed the random pool */
717 /* crypt wrapper from crypt.c */
718 int fr_crypt_check(char const *key, char const *salt);
722 typedef struct fr_cbuff fr_cbuff_t;
724 fr_cbuff_t *fr_cbuff_alloc(TALLOC_CTX *ctx, uint32_t size, bool lock);
725 void fr_cbuff_rp_insert(fr_cbuff_t *cbuff, void *obj);
726 void *fr_cbuff_rp_next(fr_cbuff_t *cbuff, TALLOC_CTX *ctx);
730 /** Optional callback passed to fr_fault_setup
732 * Allows optional logic to be run before calling the main fault handler.
734 * If the callback returns < 0, the main fault handler will not be called.
736 * @param signum signal raised.
737 * @return 0 on success < 0 on failure.
739 typedef int (*fr_fault_cb_t)(int signum);
740 typedef struct fr_bt_marker fr_bt_marker_t;
742 void fr_debug_break(void);
743 void backtrace_print(fr_cbuff_t *cbuff, void *obj);
744 int fr_backtrace_do(fr_bt_marker_t *marker);
745 fr_bt_marker_t *fr_backtrace_attach(fr_cbuff_t **cbuff, TALLOC_CTX *obj);
747 typedef void (*fr_fault_log_t)(char const *msg, ...) CC_HINT(format (printf, 1, 2));
749 void fr_panic_on_free(TALLOC_CTX *ctx);
750 int fr_set_dumpable_init(void);
751 int fr_set_dumpable(bool allow_core_dumps);
752 int fr_log_talloc_report(TALLOC_CTX *ctx);
753 void fr_fault(int sig);
754 int fr_fault_setup(char const *cmd, char const *program);
755 void fr_fault_set_cb(fr_fault_cb_t func);
756 void fr_fault_set_log_fn(fr_fault_log_t func);
757 void fr_fault_set_log_fd(int fd);
759 # ifdef WITH_VERIFY_PTR
760 void fr_verify_vp(char const *file, int line, VALUE_PAIR const *vp);
761 void fr_verify_list(char const *file, int line, TALLOC_CTX *expected, VALUE_PAIR *vps);
764 bool fr_assert_cond(char const *file, int line, char const *expr, bool cond);
765 # define fr_assert(_x) fr_assert_cond(__FILE__, __LINE__, #_x, (_x))
767 void NEVER_RETURNS _fr_exit(char const *file, int line, int status);
768 # define fr_exit(_x) _fr_exit(__FILE__, __LINE__, (_x))
770 void NEVER_RETURNS _fr_exit_now(char const *file, int line, int status);
771 # define fr_exit_now(_x) _fr_exit_now(__FILE__, __LINE__, (_x))
774 typedef struct rbtree_t rbtree_t;
775 typedef struct rbnode_t rbnode_t;
777 /* callback order for walking */
785 #define RBTREE_FLAG_NONE (0)
786 #define RBTREE_FLAG_REPLACE (1 << 0)
787 #define RBTREE_FLAG_LOCK (1 << 1)
789 typedef int (*rb_comparator_t)(void const *ctx, void const *data);
790 typedef int (*rb_walker_t)(void *ctx, void *data);
791 typedef void (*rb_free_t)(void *data);
793 rbtree_t *rbtree_create(TALLOC_CTX *ctx, rb_comparator_t compare, rb_free_t node_free, int flags);
794 void rbtree_free(rbtree_t *tree);
795 bool rbtree_insert(rbtree_t *tree, void *data);
796 rbnode_t *rbtree_insert_node(rbtree_t *tree, void *data);
797 void rbtree_delete(rbtree_t *tree, rbnode_t *z);
798 bool rbtree_deletebydata(rbtree_t *tree, void const *data);
799 rbnode_t *rbtree_find(rbtree_t *tree, void const *data);
800 void *rbtree_finddata(rbtree_t *tree, void const *data);
801 uint32_t rbtree_num_elements(rbtree_t *tree);
802 void *rbtree_node2data(rbtree_t *tree, rbnode_t *node);
805 * The callback should be declared as:
806 * int callback(void *context, void *data)
808 * The "context" is some user-defined context.
809 * The "data" is the pointer to the user data in the node,
810 * NOT the node itself.
812 * It should return 0 if all is OK, and !0 for any error.
813 * The walking will stop on any error.
815 * Except with RBTREE_DELETE_ORDER, where the callback should return <0 for
816 * errors, and may return 1 to delete the current node and halt,
817 * or 2 to delete the current node and continue. This may be
818 * used to batch-delete select nodes from a locked rbtree.
820 int rbtree_walk(rbtree_t *tree, rb_order_t order, rb_walker_t compare, void *context);
825 typedef struct fr_fifo_t fr_fifo_t;
826 typedef void (*fr_fifo_free_t)(void *);
827 fr_fifo_t *fr_fifo_create(int max_entries, fr_fifo_free_t freeNode);
828 void fr_fifo_free(fr_fifo_t *fi);
829 int fr_fifo_push(fr_fifo_t *fi, void *data);
830 void *fr_fifo_pop(fr_fifo_t *fi);
831 void *fr_fifo_peek(fr_fifo_t *fi);
832 int fr_fifo_num_elements(fr_fifo_t *fi);
838 #include <freeradius-devel/packet.h>
841 # include <freeradius-devel/tcp.h>
844 #endif /*LIBRADIUS_H*/