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(_x)
110 # define VERIFY_LIST(_x) fr_verify_list(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_prints_value(char *out, size_t outlen, VALUE_PAIR const *vp, int8_t quote);
428 size_t vp_prints_value_json(char *out, size_t outlen, VALUE_PAIR const *vp);
429 size_t vp_prints(char *out, size_t outlen, VALUE_PAIR const *vp);
430 void vp_print(FILE *, VALUE_PAIR const *);
431 void vp_printlist(FILE *, VALUE_PAIR const *);
432 char *vp_aprint_type(TALLOC_CTX *ctx, PW_TYPE type);
433 char *vp_aprint_value(TALLOC_CTX *ctx, VALUE_PAIR const *vp);
434 char *vp_aprint(TALLOC_CTX *ctx, VALUE_PAIR const *vp);
435 #define fprint_attr_val vp_print
438 * Dictionary functions.
440 extern const int dict_attr_allowed_chars[256];
441 int str2argv(char *str, char **argv, int max_argc);
442 int dict_str2oid(char const *ptr, unsigned int *pattr,
443 unsigned int *pvendor, int tlv_depth);
444 int dict_addvendor(char const *name, unsigned int value);
445 int dict_addattr(char const *name, int attr, unsigned int vendor, PW_TYPE type, ATTR_FLAGS flags);
446 int dict_addvalue(char const *namestr, char const *attrstr, int value);
447 int dict_init(char const *dir, char const *fn);
448 void dict_free(void);
449 int dict_read(char const *dir, char const *filename);
450 void dict_attr_free(DICT_ATTR const **da);
451 DICT_ATTR const *dict_attr_copy(DICT_ATTR const *da, int vp_free);
452 DICT_ATTR const *dict_attrunknown(unsigned int attr, unsigned int vendor, int vp_free);
453 DICT_ATTR const *dict_attrunknownbyname(char const *attribute, int vp_free);
454 DICT_ATTR const *dict_attrbyvalue(unsigned int attr, unsigned int vendor);
455 DICT_ATTR const *dict_attrbyname(char const *attr);
456 DICT_ATTR const *dict_attrbytagged_name(char const *name);
457 DICT_ATTR const *dict_attrbytype(unsigned int attr, unsigned int vendor,
459 DICT_ATTR const *dict_attrbyparent(DICT_ATTR const *parent, unsigned int attr,
460 unsigned int vendor);
461 int dict_attr_child(DICT_ATTR const *parent,
462 unsigned int *pattr, unsigned int *pvendor);
463 DICT_VALUE *dict_valbyattr(unsigned int attr, unsigned int vendor, int val);
464 DICT_VALUE *dict_valbyname(unsigned int attr, unsigned int vendor, char const *val);
465 char const *dict_valnamebyattr(unsigned int attr, unsigned int vendor, int value);
466 int dict_vendorbyname(char const *name);
467 DICT_VENDOR *dict_vendorbyvalue(int vendor);
469 #if 1 /* FIXME: compat */
470 #define dict_attrget dict_attrbyvalue
471 #define dict_attrfind dict_attrbyname
472 #define dict_valfind dict_valbyname
473 /*#define dict_valget dict_valbyattr almost but not quite*/
478 void fr_md5_calc(uint8_t *, uint8_t const *, unsigned int);
481 int rad_send(RADIUS_PACKET *, RADIUS_PACKET const *, char const *secret);
482 bool rad_packet_ok(RADIUS_PACKET *packet, int flags, decode_fail_t *reason);
483 RADIUS_PACKET *rad_recv(int fd, int flags);
484 ssize_t rad_recv_header(int sockfd, fr_ipaddr_t *src_ipaddr, uint16_t *src_port, int *code);
485 void rad_recv_discard(int sockfd);
486 int rad_verify(RADIUS_PACKET *packet, RADIUS_PACKET *original,
488 int rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original, char const *secret);
489 int rad_encode(RADIUS_PACKET *packet, RADIUS_PACKET const *original,
491 int rad_sign(RADIUS_PACKET *packet, RADIUS_PACKET const *original,
494 int rad_digest_cmp(uint8_t const *a, uint8_t const *b, size_t length);
495 RADIUS_PACKET *rad_alloc(TALLOC_CTX *ctx, int newvector);
496 RADIUS_PACKET *rad_alloc_reply(TALLOC_CTX *ctx, RADIUS_PACKET *);
497 RADIUS_PACKET *rad_copy_packet(TALLOC_CTX *ctx, RADIUS_PACKET const *in);
499 void rad_free(RADIUS_PACKET **);
500 int rad_pwencode(char *encpw, size_t *len, char const *secret,
501 uint8_t const *vector);
502 int rad_pwdecode(char *encpw, size_t len, char const *secret,
503 uint8_t const *vector);
505 #define FR_TUNNEL_PW_ENC_LENGTH(_x) (2 + 1 + _x + PAD(_x + 1, 16))
506 int rad_tunnel_pwencode(char *encpw, size_t *len, char const *secret,
507 uint8_t const *vector);
508 int rad_tunnel_pwdecode(uint8_t *encpw, size_t *len,
509 char const *secret, uint8_t const *vector);
510 int rad_chap_encode(RADIUS_PACKET *packet, uint8_t *output,
511 int id, VALUE_PAIR *password);
513 int rad_attr_ok(RADIUS_PACKET const *packet, RADIUS_PACKET const *original,
514 DICT_ATTR *da, uint8_t const *data, size_t length);
515 int rad_tlv_ok(uint8_t const *data, size_t length,
516 size_t dv_type, size_t dv_length);
518 ssize_t data2vp(TALLOC_CTX *ctx,
519 RADIUS_PACKET *packet, RADIUS_PACKET const *original,
521 DICT_ATTR const *da, uint8_t const *start,
522 size_t const attrlen, size_t const packetlen,
525 ssize_t rad_attr2vp(TALLOC_CTX *ctx,
526 RADIUS_PACKET *packet, RADIUS_PACKET const *original,
528 uint8_t const *data, size_t length,
531 ssize_t rad_vp2data(uint8_t const **out, VALUE_PAIR const *vp);
533 int rad_vp2extended(RADIUS_PACKET const *packet,
534 RADIUS_PACKET const *original,
535 char const *secret, VALUE_PAIR const **pvp,
536 uint8_t *ptr, size_t room);
537 int rad_vp2wimax(RADIUS_PACKET const *packet,
538 RADIUS_PACKET const *original,
539 char const *secret, VALUE_PAIR const **pvp,
540 uint8_t *ptr, size_t room);
542 int rad_vp2vsa(RADIUS_PACKET const *packet, RADIUS_PACKET const *original,
543 char const *secret, VALUE_PAIR const **pvp, uint8_t *start,
546 int rad_vp2rfc(RADIUS_PACKET const *packet,
547 RADIUS_PACKET const *original,
548 char const *secret, VALUE_PAIR const **pvp,
549 uint8_t *ptr, size_t room);
551 int rad_vp2attr(RADIUS_PACKET const *packet,
552 RADIUS_PACKET const *original, char const *secret,
553 VALUE_PAIR const **pvp, uint8_t *ptr, size_t room);
556 VALUE_PAIR *pairalloc(TALLOC_CTX *ctx, DICT_ATTR const *da);
557 VALUE_PAIR *paircreate(TALLOC_CTX *ctx, unsigned int attr, unsigned int vendor);
558 int pair2unknown(VALUE_PAIR *vp);
559 void pairfree(VALUE_PAIR **);
560 VALUE_PAIR *pairfind(VALUE_PAIR *, unsigned int attr, unsigned int vendor, int8_t tag);
561 VALUE_PAIR *pairfind_da(VALUE_PAIR *, DICT_ATTR const *da, int8_t tag);
563 #define fr_cursor_init(_x, _y) _fr_cursor_init(_x,(VALUE_PAIR const * const *) _y)
564 VALUE_PAIR *_fr_cursor_init(vp_cursor_t *cursor, VALUE_PAIR const * const *node);
565 void fr_cursor_copy(vp_cursor_t *out, vp_cursor_t *in);
566 VALUE_PAIR *fr_cursor_first(vp_cursor_t *cursor);
567 VALUE_PAIR *fr_cursor_last(vp_cursor_t *cursor);
568 VALUE_PAIR *fr_cursor_next_by_num(vp_cursor_t *cursor, unsigned int attr, unsigned int vendor, int8_t tag);
570 VALUE_PAIR *fr_cursor_next_by_da(vp_cursor_t *cursor, DICT_ATTR const *da, int8_t tag)
573 VALUE_PAIR *fr_cursor_next(vp_cursor_t *cursor);
574 VALUE_PAIR *fr_cursor_current(vp_cursor_t *cursor);
575 void fr_cursor_insert(vp_cursor_t *cursor, VALUE_PAIR *vp);
576 VALUE_PAIR *fr_cursor_remove(vp_cursor_t *cursor);
577 VALUE_PAIR *fr_cursor_replace(vp_cursor_t *cursor, VALUE_PAIR *new);
578 void pairdelete(VALUE_PAIR **, unsigned int attr, unsigned int vendor, int8_t tag);
579 void pairadd(VALUE_PAIR **, VALUE_PAIR *);
580 void pairreplace(VALUE_PAIR **first, VALUE_PAIR *add);
581 int8_t paircmp_value(VALUE_PAIR const *a, VALUE_PAIR const *b);
582 int8_t paircmp_op(VALUE_PAIR const *a, FR_TOKEN op, VALUE_PAIR const *b);
583 int8_t paircmp(VALUE_PAIR *a, VALUE_PAIR *b);
584 int8_t pairlistcmp(VALUE_PAIR *a, VALUE_PAIR *b);
586 typedef int8_t (*fr_cmp_t)(void const *a, void const *b);
587 int8_t attrcmp(void const *a, void const *b);
588 int8_t attrtagcmp(void const *a, void const *b);
589 void pairsort(VALUE_PAIR **vps, fr_cmp_t cmp);
590 void pairvalidate_debug(TALLOC_CTX *ctx, VALUE_PAIR const *failed[2]);
591 bool pairvalidate(VALUE_PAIR const *failed[2], VALUE_PAIR *filter, VALUE_PAIR *list);
592 bool pairvalidate_relaxed(VALUE_PAIR const *failed[2], VALUE_PAIR *filter, VALUE_PAIR *list);
593 VALUE_PAIR *paircopyvp(TALLOC_CTX *ctx, VALUE_PAIR const *vp);
594 VALUE_PAIR *paircopyvpdata(TALLOC_CTX *ctx, DICT_ATTR const *da, VALUE_PAIR const *vp);
595 VALUE_PAIR *paircopy(TALLOC_CTX *ctx, VALUE_PAIR *from);
596 VALUE_PAIR *paircopy2(TALLOC_CTX *ctx, VALUE_PAIR *from, unsigned int attr, unsigned int vendor, int8_t tag);
597 VALUE_PAIR *pairsteal(TALLOC_CTX *ctx, VALUE_PAIR *from);
598 void pairmemcpy(VALUE_PAIR *vp, uint8_t const * src, size_t len);
599 void pairmemsteal(VALUE_PAIR *vp, uint8_t const *src);
600 void pairstrsteal(VALUE_PAIR *vp, char const *src);
601 void pairstrcpy(VALUE_PAIR *vp, char const * src);
602 void pairstrncpy(VALUE_PAIR *vp, char const * src, size_t len);
603 void pairsprintf(VALUE_PAIR *vp, char const * fmt, ...) CC_HINT(format (printf, 2, 3));
604 void pairmove(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from);
605 void pairfilter(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from,
606 unsigned int attr, unsigned int vendor, int8_t tag);
607 VALUE_PAIR *pairmake_ip(TALLOC_CTX *ctx, char const *value,
608 DICT_ATTR *ipv4, DICT_ATTR *ipv6, DICT_ATTR *ipv4_prefix, DICT_ATTR *ipv6_prefix);
609 int pairparsevalue(VALUE_PAIR *vp, char const *value, size_t len);
610 VALUE_PAIR *pairmake(TALLOC_CTX *ctx, VALUE_PAIR **vps, char const *attribute, char const *value, FR_TOKEN op);
611 int pairmark_xlat(VALUE_PAIR *vp, char const *value);
612 FR_TOKEN pairread(char const **ptr, VALUE_PAIR_RAW *raw);
613 FR_TOKEN userparse(TALLOC_CTX *ctx, char const *buffer, VALUE_PAIR **head);
614 int readvp2(VALUE_PAIR **out, TALLOC_CTX *ctx, FILE *fp, bool *pfiledone);
619 void fr_strerror_printf(char const *, ...) CC_HINT(format (printf, 1, 2));
620 void fr_perror(char const *, ...) CC_HINT(format (printf, 1, 2));
622 extern bool fr_assert_cond(char const *file, int line, char const *expr, bool cond);
623 #define fr_assert(_x) fr_assert_cond(__FILE__, __LINE__, #_x, (_x))
625 extern void NEVER_RETURNS _fr_exit(char const *file, int line, int status);
626 #define fr_exit(_x) _fr_exit(__FILE__, __LINE__, (_x))
628 extern void NEVER_RETURNS _fr_exit_now(char const *file, int line, int status);
629 #define fr_exit_now(_x) _fr_exit_now(__FILE__, __LINE__, (_x))
631 extern char const *fr_strerror(void);
632 extern char const *fr_syserror(int num);
633 extern bool fr_dns_lookups; /* do IP -> hostname lookups? */
634 extern bool fr_hostname_lookups; /* do hostname -> IP lookups? */
635 extern int fr_debug_flag; /* 0 = no debugging information */
636 extern uint32_t fr_max_attributes; /* per incoming packet */
637 #define FR_MAX_PACKET_CODE (52)
638 extern char const *fr_packet_codes[FR_MAX_PACKET_CODE];
639 #define is_radius_code(_x) ((_x > 0) && (_x < FR_MAX_PACKET_CODE))
640 extern FILE *fr_log_fp;
641 extern void rad_print_hex(RADIUS_PACKET *packet);
642 void fr_printf_log(char const *, ...) CC_HINT(format (printf, 1, 2));
645 * Several handy miscellaneous functions.
647 int fr_set_signal(int sig, sig_t func);
648 TALLOC_CTX *fr_autofree_ctx(void);
649 char const *fr_inet_ntop(int af, void const *src);
650 char const *ip_ntoa(char *, uint32_t);
651 int fr_pton4(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve, bool fallback);
652 int fr_pton6(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve, bool fallback);
653 int fr_pton(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve);
654 bool is_wildcard(fr_ipaddr_t *addr);
655 int fr_ntop(char *out, size_t outlen, fr_ipaddr_t *addr);
656 char *ifid_ntoa(char *buffer, size_t size, uint8_t const *ifid);
657 uint8_t *ifid_aton(char const *ifid_str, uint8_t *ifid);
658 int rad_lockfd(int fd, int lock_len);
659 int rad_lockfd_nonblock(int fd, int lock_len);
660 int rad_unlockfd(int fd, int lock_len);
661 size_t fr_bin2hex(char *hex, uint8_t const *bin, size_t inlen);
662 size_t fr_hex2bin(uint8_t *bin, size_t outlen, char const *hex, size_t inlen);
663 uint32_t fr_strtoul(char const *value, char **end);
664 bool is_whitespace(char const *value);
665 bool is_integer(char const *value);
666 bool is_zero(char const *value);
668 int fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b);
670 int ip_hton(fr_ipaddr_t *out, int af, char const *hostname, bool fallback);
671 char const *ip_ntoh(fr_ipaddr_t const *src, char *dst, size_t cnt);
672 struct in_addr fr_inaddr_mask(struct in_addr const *ipaddr, uint8_t prefix);
673 struct in6_addr fr_in6addr_mask(struct in6_addr const *ipaddr, uint8_t prefix);
674 void fr_ipaddr_mask(fr_ipaddr_t *addr, uint8_t prefix);
675 int fr_ipaddr2sockaddr(fr_ipaddr_t const *ipaddr, uint16_t port,
676 struct sockaddr_storage *sa, socklen_t *salen);
677 int fr_sockaddr2ipaddr(struct sockaddr_storage const *sa, socklen_t salen,
678 fr_ipaddr_t *ipaddr, uint16_t *port);
679 ssize_t fr_utf8_to_ucs2(uint8_t *out, size_t outlen, char const *in, size_t inlen);
680 size_t fr_prints_uint128(char *out, size_t outlen, uint128_t const num);
681 int64_t fr_pow(int32_t base, uint8_t exp);
682 int fr_get_time(char const *date_str, time_t *date);
683 int8_t fr_pointer_cmp(void const *a, void const *b);
684 void fr_quick_sort(void const *to_sort[], int min_idx, int max_idx, fr_cmp_t cmp);
686 * Define TALLOC_DEBUG to check overflows with talloc.
687 * we can't use valgrind, because the memory used by
688 * talloc is valid memory... just not for us.
691 void fr_talloc_verify_cb(const void *ptr, int depth,
692 int max_depth, int is_ref,
694 #define VERIFY_ALL_TALLOC talloc_report_depth_cb(NULL, 0, -1, fr_talloc_verify_cb, NULL)
696 #define VERIFY_ALL_TALLOC
699 #ifdef WITH_ASCEND_BINARY
701 int ascend_parse_filter(VALUE_PAIR *vp, char const *value, size_t len);
702 void print_abinary(char *out, size_t outlen, VALUE_PAIR const *vp, int8_t quote);
703 #endif /*WITH_ASCEND_BINARY*/
705 /* random numbers in isaac.c */
706 /* context of random number generator */
707 typedef struct fr_randctx {
709 uint32_t randrsl[256];
710 uint32_t randmem[256];
716 void fr_isaac(fr_randctx *ctx);
717 void fr_randinit(fr_randctx *ctx, int flag);
718 uint32_t fr_rand(void); /* like rand(), but better. */
719 void fr_rand_seed(void const *, size_t ); /* seed the random pool */
722 /* crypt wrapper from crypt.c */
723 int fr_crypt_check(char const *key, char const *salt);
726 typedef struct fr_cbuff fr_cbuff_t;
728 fr_cbuff_t *fr_cbuff_alloc(TALLOC_CTX *ctx, uint32_t size, bool lock);
729 void fr_cbuff_rp_insert(fr_cbuff_t *cbuff, void *obj);
730 void *fr_cbuff_rp_next(fr_cbuff_t *cbuff, TALLOC_CTX *ctx);
734 /** Optional callback passed to fr_fault_setup
736 * Allows optional logic to be run before calling the main fault handler.
738 * If the callback returns < 0, the main fault handler will not be called.
740 * @param signum signal raised.
741 * @return 0 on success < 0 on failure.
743 typedef int (*fr_fault_cb_t)(int signum);
744 typedef struct fr_bt_marker fr_bt_marker_t;
746 void fr_debug_break(void);
747 void backtrace_print(fr_cbuff_t *cbuff, void *obj);
748 fr_bt_marker_t *fr_backtrace_attach(fr_cbuff_t **cbuff, TALLOC_CTX *obj);
750 typedef void (*fr_fault_log_t)(char const *msg, ...) CC_HINT(format (printf, 1, 2));
752 void fr_panic_on_free(TALLOC_CTX *ctx);
753 int fr_set_dumpable_init(void);
754 int fr_set_dumpable(bool allow_core_dumps);
755 int fr_log_talloc_report(TALLOC_CTX *ctx);
756 void fr_fault(int sig);
757 int fr_fault_setup(char const *cmd, char const *program);
758 void fr_fault_set_cb(fr_fault_cb_t func);
759 void fr_fault_set_log_fn(fr_fault_log_t func);
760 void fr_fault_set_log_fd(int fd);
762 #ifdef WITH_VERIFY_PTR
763 void fr_verify_vp(VALUE_PAIR const *vp);
764 void fr_verify_list(TALLOC_CTX *expected, VALUE_PAIR *vps);
768 typedef struct rbtree_t rbtree_t;
769 typedef struct rbnode_t rbnode_t;
771 /* callback order for walking */
779 #define RBTREE_FLAG_NONE (0)
780 #define RBTREE_FLAG_REPLACE (1 << 0)
781 #define RBTREE_FLAG_LOCK (1 << 1)
783 typedef int (*rb_comparator_t)(void const *ctx, void const *data);
784 typedef int (*rb_walker_t)(void *ctx, void *data);
785 typedef void (*rb_free_t)(void *data);
787 rbtree_t *rbtree_create(rb_comparator_t compare, rb_free_t node_free, int flags);
788 void rbtree_free(rbtree_t *tree);
789 bool rbtree_insert(rbtree_t *tree, void *data);
790 rbnode_t *rbtree_insert_node(rbtree_t *tree, void *data);
791 void rbtree_delete(rbtree_t *tree, rbnode_t *z);
792 bool rbtree_deletebydata(rbtree_t *tree, void const *data);
793 rbnode_t *rbtree_find(rbtree_t *tree, void const *data);
794 void *rbtree_finddata(rbtree_t *tree, void const *data);
795 uint32_t rbtree_num_elements(rbtree_t *tree);
796 void *rbtree_node2data(rbtree_t *tree, rbnode_t *node);
799 * The callback should be declared as:
800 * int callback(void *context, void *data)
802 * The "context" is some user-defined context.
803 * The "data" is the pointer to the user data in the node,
804 * NOT the node itself.
806 * It should return 0 if all is OK, and !0 for any error.
807 * The walking will stop on any error.
809 * Except with RBTREE_DELETE_ORDER, where the callback should return <0 for
810 * errors, and may return 1 to delete the current node and halt,
811 * or 2 to delete the current node and continue. This may be
812 * used to batch-delete select nodes from a locked rbtree.
814 int rbtree_walk(rbtree_t *tree, rb_order_t order, rb_walker_t compare, void *context);
819 typedef struct fr_fifo_t fr_fifo_t;
820 typedef void (*fr_fifo_free_t)(void *);
821 fr_fifo_t *fr_fifo_create(int max_entries, fr_fifo_free_t freeNode);
822 void fr_fifo_free(fr_fifo_t *fi);
823 int fr_fifo_push(fr_fifo_t *fi, void *data);
824 void *fr_fifo_pop(fr_fifo_t *fi);
825 void *fr_fifo_peek(fr_fifo_t *fi);
826 int fr_fifo_num_elements(fr_fifo_t *fi);
832 #include <freeradius-devel/packet.h>
835 # include <freeradius-devel/tcp.h>
838 #endif /*LIBRADIUS_H*/