Add rbtree_callbydata for sane threadsafe/garbage-collected operations
[freeradius.git] / src / include / libradius.h
1 /*
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.
6  *
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.
11  *
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
15  */
16 #ifndef LIBRADIUS_H
17 #define LIBRADIUS_H
18 /*
19  * $Id$
20  *
21  * @file libradius.h
22  * @brief Structures and prototypes for the radius library.
23  *
24  * @copyright 1999-2008 The FreeRADIUS server project
25  */
26 RCSIDH(libradius_h, "$Id$")
27
28 #include <freeradius-devel/missing.h>
29
30 #include <talloc.h>
31
32 /*
33  *  Let any external program building against the library know what
34  *  features the library was built with.
35  */
36 #include <freeradius-devel/features.h>
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <stdarg.h>
41 #include <stdbool.h>
42
43 #include <freeradius-devel/radius.h>
44 #include <freeradius-devel/token.h>
45 #include <freeradius-devel/hash.h>
46
47 #ifdef SIZEOF_UNSIGNED_INT
48 #if SIZEOF_UNSIGNED_INT != 4
49 #error FATAL: sizeof(unsigned int) != 4
50 #endif
51 #endif
52
53 /*
54  *  Include for modules.
55  */
56 #include <freeradius-devel/sha1.h>
57 #include <freeradius-devel/md4.h>
58
59 #ifdef __cplusplus
60 extern "C" {
61 #endif
62
63 #if defined(WITH_VERIFY_PTR)
64 /*
65  *      Requires typeof(), which is in most modern C compilers.
66  */
67
68 /*
69 #define VERIFY_VP(_x) do { (void) talloc_get_type_abort(_x, VALUE_PAIR); \
70                         if (_x->da) { \
71                                 (void) talloc_get_type_abort(_x->da, DICT_ATTR); \
72                         } \
73                       } while (0)
74 */
75
76 #define VERIFY_VP(_x) (void) talloc_get_type_abort(_x, VALUE_PAIR)
77 #define VERIFY_PACKET(_x) (void) talloc_get_type_abort(_x, RADIUS_PACKET)
78 #else
79 #define VERIFY_VP(_x)
80 #define VERIFY_PACKET(_x)
81 #endif
82
83 #define AUTH_VECTOR_LEN         16
84 #define CHAP_VALUE_LENGTH       16
85 #define MAX_STRING_LEN          254     /* RFC2138: string 0-253 octets */
86 #define FR_MAX_VENDOR           (1 << 24) /* RFC limitations */
87
88 #ifdef _LIBRADIUS
89 #  define AUTH_HDR_LEN          20
90 #  define VENDORPEC_USR         429
91 #define VENDORPEC_LUCENT        4846
92 #define VENDORPEC_STARENT       8164
93 #  define DEBUG                 if (fr_debug_flag && fr_log_fp) fr_printf_log
94 #  define debug_pair(vp)        do { if (fr_debug_flag && fr_log_fp) { \
95                                         vp_print(fr_log_fp, vp); \
96                                      } \
97                                 } while(0)
98 #endif
99
100 #define TAG_VALID(x)            ((x) > 0 && (x) < 0x20)
101 #define TAG_VALID_ZERO(x)       ((x) < 0x20)
102 #define TAG_ANY                 -128    /* minimum signed char */
103 #define TAG_UNUSED              0
104
105 #if defined(__GNUC__)
106 # define PRINTF_LIKE(n) __attribute__ ((format(printf, n, n+1)))
107 # define NEVER_RETURNS __attribute__ ((noreturn))
108 # define UNUSED __attribute__ ((unused))
109 # define BLANK_FORMAT " "       /* GCC_LINT whines about empty formats */
110 #else
111 # define PRINTF_LIKE(n) /* ignore */
112 # define NEVER_RETURNS /* ignore */
113 # define UNUSED /* ignore */
114 # define BLANK_FORMAT ""
115 #endif
116
117 typedef struct attr_flags {
118         unsigned int    is_unknown : 1;                         //!< Attribute number or vendor is unknown.
119         unsigned int    is_tlv : 1;                             //!< Is a sub attribute.
120         unsigned int    vp_free : 1;                            //!< Should be freed when VALUE_PAIR is freed.
121
122         unsigned int    has_tag : 1;                            //!< Tagged attribute.
123         unsigned int    array : 1;                              //!< Pack multiples into 1 attr.
124         unsigned int    has_value : 1;                          //!< Has a value.
125         unsigned int    has_value_alias : 1;                    //!< Has a value alias.
126         unsigned int    has_tlv : 1;                            //!< Has sub attributes.
127
128         unsigned int    extended : 1;                           //!< Extended attribute.
129         unsigned int    long_extended : 1;                      //!< Long format.
130         unsigned int    evs : 1;                                //!< Extended VSA.
131         unsigned int    wimax: 1;                               //!< WiMAX format=1,1,c.
132
133         unsigned int    concat : 1;                             //!< concatenate multiple instances
134         unsigned int    is_pointer : 1;                         //!< data is a pointer
135
136         uint8_t         encrypt;                                //!< Ecryption method.
137         uint8_t         length;
138 } ATTR_FLAGS;
139
140 /*
141  *  Values of the encryption flags.
142  */
143 #define FLAG_ENCRYPT_NONE           (0)
144 #define FLAG_ENCRYPT_USER_PASSWORD   (1)
145 #define FLAG_ENCRYPT_TUNNEL_PASSWORD (2)
146 #define FLAG_ENCRYPT_ASCEND_SECRET   (3)
147
148 extern const FR_NAME_NUMBER dict_attr_types[];
149 extern const size_t dict_attr_sizes[PW_TYPE_MAX][2];
150
151 /** dictionary attribute
152  *
153  */
154 typedef struct dict_attr {
155         unsigned int            attr;
156         PW_TYPE                 type;
157         unsigned int            vendor;
158         ATTR_FLAGS              flags;
159         char                    name[1];
160 } DICT_ATTR;
161
162 /** value of an enumerated attribute
163  *
164  */
165 typedef struct dict_value {
166         unsigned int            attr;
167         unsigned int            vendor;
168         int                     value;
169         char                    name[1];
170 } DICT_VALUE;
171
172 /** dictionary vendor
173  *
174  */
175 typedef struct dict_vendor {
176         unsigned int            vendorpec;
177         size_t                  type;                           //!< Length of type data
178         size_t                  length;                         //!< Length of length data
179         size_t                  flags;
180         char                    name[1];
181 } DICT_VENDOR;
182
183 /** Union containing all data types supported by the server
184  *
185  * This union contains all data types that can be represented with VALUE_PAIRs. It may also be used in other parts
186  * of the server where values of different types need to be stored.
187  *
188  * PW_TYPE should be an enumeration of the values in this union.
189  */
190 typedef union value_data {
191         char const              *strvalue;                      //!< Pointer to UTF-8 string.
192         uint8_t const           *octets;                        //!< Pointer to binary string.
193         struct in_addr          ipaddr;                         //!< IPv4 Address.
194         struct in6_addr         ipv6addr;                       //!< IPv6 Address.
195         uint32_t                date;                           //!< Date (32bit Unix timestamp).
196         uint32_t                integer;                        //!< 32bit unsigned integer.
197         int32_t                 sinteger;                       //!< 32bit signed integer.
198         uint64_t                integer64;                      //!< 64bit unsigned integer.
199         size_t                  filter[32/sizeof(size_t)];      //!< 64bit signed integer.
200         uint8_t                 ifid[8]; /* struct? */          //!< IPv6 interface ID.
201         uint8_t                 ipv6prefix[18]; /* struct? */   //!< IPv6 prefix.
202         uint8_t                 ipv4prefix[6]; /* struct? */    //!< IPv4 prefix.
203         uint8_t                 ether[6];                       //!< Ethernet (MAC) address.
204         uint8_t                 *tlv;                           //!< Nested TLV (should go away).
205         void const              *ptr;                           //!< generic pointer.
206 } value_data_t;
207
208 /** The type of value a VALUE_PAIR contains
209  *
210  * This is used to add structure to nested VALUE_PAIRs and specifies what type of node it is (set, list, data).
211  *
212  * xlat is another type of data node which must first be expanded before use.
213  */
214 typedef enum value_type {
215         VT_NONE = 0,                                            //!< VALUE_PAIR has no value.
216         VT_SET,                                                 //!< VALUE_PAIR has children.
217         VT_LIST,                                                //!< VALUE_PAIR has multiple values.
218         VT_DATA,                                                //!< VALUE_PAIR has a single value.
219         VT_XLAT                                                 //!< valuepair value must be xlat expanded when it's
220                                                                 //!< added to VALUE_PAIR tree.
221 } value_type_t;
222
223 /** Stores an attribute, a value and various bits of other data
224  *
225  * VALUE_PAIRs are the main data structure used in the server, they specify an attribute, it's children and
226  * it's siblings.
227  *
228  * They also specify what behaviour should be used when the attribute is merged into a new list/tree.
229  */
230 typedef struct value_pair {
231         DICT_ATTR const         *da;                            //!< Dictionary attribute defines the attribute
232                                                                 //!< number, vendor and type of the attribute.
233
234         struct value_pair       *next;
235
236         FR_TOKEN                op;                             //!< Operator to use when moving or inserting
237                                                                 //!< valuepair into a list.
238
239         int8_t                  tag;                            //!< Tag value used to group valuepairs.
240
241         union {
242         //      VALUE_SET       *set;                           //!< Set of child attributes.
243         //      VALUE_LIST      *list;                          //!< List of values for
244                                                                 //!< multivalued attribute.
245         //      value_data_t    *data;                          //!< Value data for this attribute.
246
247                 char const      *xlat;                          //!< Source string for xlat expansion.
248         } value;
249
250         value_type_t            type;                           //!< Type of pointer in value union.
251
252         size_t                  length;                         //!< of Data field.
253         value_data_t            data;
254 } VALUE_PAIR;
255
256 /** Abstraction to allow iterating over different configurations of VALUE_PAIRs
257  *
258  * This allows functions which do not care about the structure of collections of VALUE_PAIRs
259  * to iterate over all members in a collection.
260  *
261  * Field within a vp_cursor should not be accessed directly, and vp_cursors should only be
262  * manipulated with the pair* functions.
263  */
264 typedef struct vp_cursor {
265         VALUE_PAIR      **first;
266         VALUE_PAIR      *found;                                 //!< pairfind marker.
267         VALUE_PAIR      *last;                                  //!< Temporary only used for pairinsert
268         VALUE_PAIR      *current;                               //!< The current attribute.
269         VALUE_PAIR      *next;                                  //!< Next attribute to process.
270 } vp_cursor_t;
271
272 /** A VALUE_PAIR in string format.
273  *
274  * Used to represent pairs in the legacy 'users' file format.
275  */
276 typedef struct value_pair_raw {
277         char l_opand[64];                                       //!< Left hand side of the pair.
278         char r_opand[1024];                                     //!< Right hand side of the pair.
279
280         FR_TOKEN quote;                                         //!< Type of quoting around the r_opand.
281
282         FR_TOKEN op;                                            //!< Operator.
283 } VALUE_PAIR_RAW;
284
285 #define vp_strvalue   data.strvalue
286 #define vp_octets     data.octets
287 #define vp_ipv6addr   data.ipv6addr
288 #define vp_ifid       data.ifid
289 #define vp_ipv6prefix data.ipv6prefix
290 #define vp_ipv4prefix data.ipv4prefix
291 #define vp_filter     data.filter
292 #define vp_ether      data.ether
293 #define vp_signed     data.sinteger
294 #define vp_tlv        data.tlv
295 #define vp_integer64  data.integer64
296 #define vp_ipaddr     data.ipaddr.s_addr
297 #define vp_date       data.date
298 #define vp_integer    data.integer
299
300 typedef struct fr_ipaddr_t {
301         int             af;     /* address family */
302         union {
303                 struct in_addr  ip4addr;
304                 struct in6_addr ip6addr; /* maybe defined in missing.h */
305         } ipaddr;
306         uint32_t        scope;  /* for IPv6 */
307 } fr_ipaddr_t;
308
309 /*
310  *      vector:         Request authenticator from access-request packet
311  *                      Put in there by rad_decode, and must be put in the
312  *                      response RADIUS_PACKET as well before calling rad_send
313  *
314  *      verified:       Filled in by rad_decode for accounting-request packets
315  *
316  *      data,data_len:  Used between rad_recv and rad_decode.
317  */
318 typedef struct radius_packet {
319         int                     sockfd;
320         fr_ipaddr_t             src_ipaddr;
321         fr_ipaddr_t             dst_ipaddr;
322         uint16_t                src_port;
323         uint16_t                dst_port;
324         int                     id;
325         unsigned int            code;
326         uint8_t                 vector[AUTH_VECTOR_LEN];
327         struct timeval          timestamp;
328         uint8_t                 *data;
329         size_t                  data_len;
330         VALUE_PAIR              *vps;
331         ssize_t                 offset;
332 #ifdef WITH_TCP
333         size_t                  partial;
334 #endif
335 } RADIUS_PACKET;
336
337 /*
338  *      Printing functions.
339  */
340 int             fr_utf8_char(uint8_t const *str);
341 size_t          fr_print_string(char const *in, size_t inlen,
342                                  char *out, size_t outlen);
343 size_t          vp_prints_value(char *out, size_t outlen, VALUE_PAIR const *vp, int8_t quote);
344 char            *vp_aprinttype(TALLOC_CTX *ctx, PW_TYPE type);
345 char            *vp_aprint(TALLOC_CTX *ctx, VALUE_PAIR const *vp);
346 int             vp_prints_value_json(char *out, size_t outlen,
347                                      VALUE_PAIR const *vp);
348 size_t          vp_print_name(char *buffer, size_t bufsize,
349                               unsigned int attr, unsigned int vendor);
350 int             vp_prints(char *out, size_t outlen, VALUE_PAIR const *vp);
351 void            vp_print(FILE *, VALUE_PAIR const *);
352 void            vp_printlist(FILE *, VALUE_PAIR const *);
353 #define         fprint_attr_val vp_print
354
355 /*
356  *      Dictionary functions.
357  */
358 extern const int dict_attr_allowed_chars[256];
359 int             str2argv(char *str, char **argv, int max_argc);
360 int             dict_str2oid(char const *ptr, unsigned int *pattr,
361                              unsigned int *pvendor, int tlv_depth);
362 int             dict_addvendor(char const *name, unsigned int value);
363 int             dict_addattr(char const *name, int attr, unsigned int vendor, int type, ATTR_FLAGS flags);
364 int             dict_addvalue(char const *namestr, char const *attrstr, int value);
365 int             dict_init(char const *dir, char const *fn);
366 void            dict_free(void);
367 int             dict_read(char const *dir, char const *filename);
368 void            dict_attr_free(DICT_ATTR const **da);
369 DICT_ATTR const *dict_attr_copy(DICT_ATTR const *da, int vp_free);
370 DICT_ATTR const *dict_attrunknown(unsigned int attr, unsigned int vendor, int vp_free);
371 DICT_ATTR const *dict_attrunknownbyname(char const *attribute, int vp_free);
372 DICT_ATTR const *dict_attrbyvalue(unsigned int attr, unsigned int vendor);
373 DICT_ATTR const *dict_attrbyname(char const *attr);
374 DICT_ATTR const *dict_attrbytype(unsigned int attr, unsigned int vendor,
375                                  PW_TYPE type);
376 DICT_ATTR const *dict_attrbyparent(DICT_ATTR const *parent, unsigned int attr,
377                                            unsigned int vendor);
378 int             dict_attr_child(DICT_ATTR const *parent,
379                                 unsigned int *pattr, unsigned int *pvendor);
380 DICT_VALUE      *dict_valbyattr(unsigned int attr, unsigned int vendor, int val);
381 DICT_VALUE      *dict_valbyname(unsigned int attr, unsigned int vendor, char const *val);
382 char const      *dict_valnamebyattr(unsigned int attr, unsigned int vendor, int value);
383 int             dict_vendorbyname(char const *name);
384 DICT_VENDOR     *dict_vendorbyvalue(int vendor);
385
386 #if 1 /* FIXME: compat */
387 #define dict_attrget    dict_attrbyvalue
388 #define dict_attrfind   dict_attrbyname
389 #define dict_valfind    dict_valbyname
390 /*#define dict_valget   dict_valbyattr almost but not quite*/
391 #endif
392
393 /* md5.c */
394
395 void            fr_md5_calc(uint8_t *, uint8_t const *, unsigned int);
396
397 /* hmac.c */
398
399 void fr_hmac_md5(uint8_t const *text, size_t text_len, uint8_t const *key, size_t key_len, unsigned char *digest);
400
401 /* hmacsha1.c */
402
403 void fr_hmac_sha1(uint8_t const *text, size_t text_len, uint8_t const *key, size_t key_len, uint8_t *digest);
404
405 /* radius.c */
406 int             rad_send(RADIUS_PACKET *, RADIUS_PACKET const *, char const *secret);
407 int             rad_packet_ok(RADIUS_PACKET *packet, int flags);
408 RADIUS_PACKET   *rad_recv(int fd, int flags);
409 ssize_t rad_recv_header(int sockfd, fr_ipaddr_t *src_ipaddr, int *src_port,
410                         int *code);
411 void            rad_recv_discard(int sockfd);
412 int             rad_verify(RADIUS_PACKET *packet, RADIUS_PACKET *original,
413                            char const *secret);
414 int             rad_decode(RADIUS_PACKET *packet, RADIUS_PACKET *original, char const *secret);
415 int             rad_encode(RADIUS_PACKET *packet, RADIUS_PACKET const *original,
416                            char const *secret);
417 int             rad_sign(RADIUS_PACKET *packet, RADIUS_PACKET const *original,
418                          char const *secret);
419
420 int rad_digest_cmp(uint8_t const *a, uint8_t const *b, size_t length);
421 RADIUS_PACKET   *rad_alloc(TALLOC_CTX *ctx, int newvector);
422 RADIUS_PACKET   *rad_alloc_reply(TALLOC_CTX *ctx, RADIUS_PACKET *);
423 void            rad_free(RADIUS_PACKET **);
424 int             rad_pwencode(char *encpw, size_t *len, char const *secret,
425                              uint8_t const *vector);
426 int             rad_pwdecode(char *encpw, size_t len, char const *secret,
427                              uint8_t const *vector);
428 int             rad_tunnel_pwencode(char *encpw, size_t *len, char const *secret,
429                                     uint8_t const *vector);
430 int             rad_tunnel_pwdecode(uint8_t *encpw, size_t *len,
431                                     char const *secret, uint8_t const *vector);
432 int             rad_chap_encode(RADIUS_PACKET *packet, uint8_t *output,
433                                 int id, VALUE_PAIR *password);
434
435 int rad_attr_ok(RADIUS_PACKET const *packet, RADIUS_PACKET const *original,
436                 DICT_ATTR *da,
437                 uint8_t const *data, size_t length);
438 int rad_tlv_ok(uint8_t const *data, size_t length,
439                size_t dv_type, size_t dv_length);
440
441 ssize_t rad_attr2vp(RADIUS_PACKET *packet, RADIUS_PACKET const *original,
442                     char const *secret,
443                     uint8_t const *data, size_t length,
444                     VALUE_PAIR **pvp);
445
446 ssize_t  rad_data2vp(unsigned int attribute, unsigned int vendor,
447                      uint8_t const *data, size_t length,
448                      VALUE_PAIR **pvp);
449
450 ssize_t rad_vp2data(VALUE_PAIR const *vp, uint8_t *out, size_t outlen);
451
452 int rad_vp2extended(RADIUS_PACKET const *packet,
453                     RADIUS_PACKET const *original,
454                     char const *secret, VALUE_PAIR const **pvp,
455                     uint8_t *ptr, size_t room);
456 int rad_vp2wimax(RADIUS_PACKET const *packet,
457                  RADIUS_PACKET const *original,
458                  char const *secret, VALUE_PAIR const **pvp,
459                  uint8_t *ptr, size_t room);
460 int rad_vp2vsa(RADIUS_PACKET const *packet, RADIUS_PACKET const *original,
461                char const *secret, VALUE_PAIR const **pvp, uint8_t *start,
462                size_t room);
463 int rad_vp2rfc(RADIUS_PACKET const *packet,
464                RADIUS_PACKET const *original,
465                char const *secret, VALUE_PAIR const **pvp,
466                uint8_t *ptr, size_t room);
467
468 int rad_vp2attr(RADIUS_PACKET const *packet,
469                 RADIUS_PACKET const *original, char const *secret,
470                 VALUE_PAIR const **pvp, uint8_t *ptr, size_t room);
471
472 /* valuepair.c */
473 VALUE_PAIR      *pairalloc(TALLOC_CTX *ctx, DICT_ATTR const *da);
474 VALUE_PAIR      *paircreate(TALLOC_CTX *ctx, unsigned int attr, unsigned int vendor);
475 int             pair2unknown(VALUE_PAIR *vp);
476 void            pairfree(VALUE_PAIR **);
477 void            pairbasicfree(VALUE_PAIR *pair);
478 VALUE_PAIR      *pairfind(VALUE_PAIR *, unsigned int attr, unsigned int vendor, int8_t tag);
479
480 #define         paircursor(_x, _y)      paircursorc(_x,(VALUE_PAIR const * const *) _y)
481 VALUE_PAIR      *paircursorc(vp_cursor_t *cursor, VALUE_PAIR const * const *node);
482 VALUE_PAIR      *pairfirst(vp_cursor_t *cursor);
483 VALUE_PAIR      *pairfindnext(vp_cursor_t *cursor, unsigned int attr, unsigned int vendor, int8_t tag);
484 VALUE_PAIR      *pairnext(vp_cursor_t *cursor);
485 VALUE_PAIR      *pairlast(vp_cursor_t *cursor);
486 VALUE_PAIR      *paircurrent(vp_cursor_t *cursor);
487 void            pairinsert(vp_cursor_t *cursor, VALUE_PAIR *vp);
488 VALUE_PAIR      *pairremove(vp_cursor_t *cursor);
489 void            pairdelete(VALUE_PAIR **, unsigned int attr, unsigned int vendor, int8_t tag);
490 void            pairadd(VALUE_PAIR **, VALUE_PAIR *);
491 void            pairreplace(VALUE_PAIR **first, VALUE_PAIR *add);
492 int             paircmp(VALUE_PAIR *check, VALUE_PAIR *data);
493 int             paircmp_op(VALUE_PAIR const *one, FR_TOKEN op, VALUE_PAIR const *two);
494 void            pairsort(VALUE_PAIR **vps, bool with_tag);
495 bool            pairvalidate(VALUE_PAIR *filter, VALUE_PAIR *list);
496 VALUE_PAIR      *paircopyvp(TALLOC_CTX *ctx, VALUE_PAIR const *vp);
497 VALUE_PAIR      *paircopyvpdata(TALLOC_CTX *ctx, DICT_ATTR const *da, VALUE_PAIR const *vp);
498 VALUE_PAIR      *paircopy(TALLOC_CTX *ctx, VALUE_PAIR *from);
499 VALUE_PAIR      *paircopy2(TALLOC_CTX *ctx, VALUE_PAIR *from, unsigned int attr, unsigned int vendor, int8_t tag);
500 void            pairmemcpy(VALUE_PAIR *vp, uint8_t const * src, size_t len);
501 void            pairstrcpy(VALUE_PAIR *vp, char const * src);
502 void            pairsprintf(VALUE_PAIR *vp, char const * fmt, ...)
503 #ifdef __GNUC__
504                 __attribute__ ((format (printf, 2, 3)))
505 #endif
506 ;
507 void            pairmove(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from);
508 void            pairfilter(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from,
509                                            unsigned int attr, unsigned int vendor, int8_t tag);
510 bool            pairparsevalue(VALUE_PAIR *vp, char const *value);
511 VALUE_PAIR      *pairmake(TALLOC_CTX *ctx, VALUE_PAIR **vps, char const *attribute, char const *value, FR_TOKEN op);
512 int             pairmark_xlat(VALUE_PAIR *vp, char const *value);
513 FR_TOKEN        pairread(char const **ptr, VALUE_PAIR_RAW *raw);
514 FR_TOKEN        userparse(TALLOC_CTX *ctx, char const *buffer, VALUE_PAIR **head);
515 VALUE_PAIR      *readvp2(TALLOC_CTX *ctx, FILE *fp, int *pfiledone, char const *errprefix);
516
517 /*
518  *      Error functions.
519  */
520 #ifdef _LIBRADIUS
521 void            fr_strerror_printf(char const *, ...)
522 #ifdef __GNUC__
523                 __attribute__ ((format (printf, 1, 2)))
524 #endif
525 ;
526 #endif
527 void            fr_perror(char const *, ...)
528 #ifdef __GNUC__
529                 __attribute__ ((format (printf, 1, 2)))
530 #endif
531 ;
532 extern char const *fr_strerror(void);
533 extern int      fr_dns_lookups; /* 0 = no dns lookups */
534 extern int      fr_debug_flag;  /* 0 = no debugging information */
535 extern int      fr_max_attributes; /* per incoming packet */
536 #define FR_MAX_PACKET_CODE (52)
537 extern char const *fr_packet_codes[FR_MAX_PACKET_CODE];
538 extern FILE     *fr_log_fp;
539 extern void rad_print_hex(RADIUS_PACKET *packet);
540 void            fr_printf_log(char const *, ...)
541 #ifdef __GNUC__
542                 __attribute__ ((format (printf, 1, 2)))
543 #endif
544 ;
545
546 /*
547  *      Several handy miscellaneous functions.
548  */
549 char const      *ip_ntoa(char *, uint32_t);
550 char            *ifid_ntoa(char *buffer, size_t size, uint8_t const *ifid);
551 uint8_t         *ifid_aton(char const *ifid_str, uint8_t *ifid);
552 int             rad_lockfd(int fd, int lock_len);
553 int             rad_lockfd_nonblock(int fd, int lock_len);
554 int             rad_unlockfd(int fd, int lock_len);
555 size_t          fr_bin2hex(char *hex, uint8_t const *bin, size_t inlen);
556 size_t          fr_hex2bin(uint8_t *bin, char const *hex, size_t outlen);
557 int fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b);
558
559 int             ip_hton(char const *src, int af, fr_ipaddr_t *dst);
560 char const      *ip_ntoh(fr_ipaddr_t const *src, char *dst, size_t cnt);
561 int fr_ipaddr2sockaddr(fr_ipaddr_t const *ipaddr, int port,
562                        struct sockaddr_storage *sa, socklen_t *salen);
563 int fr_sockaddr2ipaddr(struct sockaddr_storage const *sa, socklen_t salen,
564                        fr_ipaddr_t *ipaddr, int * port);
565
566
567 #ifdef WITH_ASCEND_BINARY
568 /* filters.c */
569 int             ascend_parse_filter(VALUE_PAIR *pair);
570 void            print_abinary(VALUE_PAIR const *vp, char *buffer, size_t len, int8_t quote);
571 #endif /*WITH_ASCEND_BINARY*/
572
573 /* random numbers in isaac.c */
574 /* context of random number generator */
575 typedef struct fr_randctx {
576         uint32_t randcnt;
577         uint32_t randrsl[256];
578         uint32_t randmem[256];
579         uint32_t randa;
580         uint32_t randb;
581         uint32_t randc;
582 } fr_randctx;
583
584 void fr_isaac(fr_randctx *ctx);
585 void fr_randinit(fr_randctx *ctx, int flag);
586 uint32_t fr_rand(void); /* like rand(), but better. */
587 void fr_rand_seed(void const *, size_t ); /* seed the random pool */
588
589
590 /* crypt wrapper from crypt.c */
591 int fr_crypt_check(char const *key, char const *salt);
592
593 /* rbtree.c */
594 typedef struct rbtree_t rbtree_t;
595 typedef struct rbnode_t rbnode_t;
596
597 #define RBTREE_FLAG_NONE    (0)
598 #define RBTREE_FLAG_REPLACE (1 << 0)
599 #define RBTREE_FLAG_LOCK    (1 << 1)
600 rbtree_t       *rbtree_create(int (*Compare)(void const *, void const *),
601                               void (*freeNode)(void *),
602                               int flags);
603 void            rbtree_free(rbtree_t *tree);
604 bool            rbtree_insert(rbtree_t *tree, void *Data);
605 rbnode_t        *rbtree_insertnode(rbtree_t *tree, void *Data);
606 void            rbtree_delete(rbtree_t *tree, rbnode_t *Z);
607 bool            rbtree_deletebydata(rbtree_t *tree, void const *data);
608 rbnode_t       *rbtree_find(rbtree_t *tree, void const *Data);
609 void           *rbtree_finddata(rbtree_t *tree, void const *Data);
610 int             rbtree_num_elements(rbtree_t *tree);
611 void           *rbtree_min(rbtree_t *tree);
612 void           *rbtree_node2data(rbtree_t *tree, rbnode_t *node);
613
614 /* callback order for walking  */
615 typedef enum { PreOrder, InOrder, PostOrder } RBTREE_ORDER;
616
617 /*
618  *      The callback should be declared as:
619  *      int callback(void *context, void *data)
620  *
621  *      The "context" is some user-defined context.
622  *      The "data" is the pointer to the user data in the node,
623  *        NOT the node itself.
624  *
625  *      It should return 0 if all is OK, and !0 for any error.
626  *      The walking will stop on any error.
627  */
628 int rbtree_walk(rbtree_t *tree, RBTREE_ORDER order, int (*callback)(void *, void *), void *context);
629
630 /*
631  *      Find a matching data item in an rbtree and, if one is found,
632  *      perform a callback on it.
633  *
634  *      The callback is similar to rbtree_walk above, except that a
635  *      positive return code from the callback will cause the found node
636  *      to be deleted from the tree.  If the tree was created with
637  *      RBTREE_FLAG_LOCK, then the entire find/callback/delete/rebalance
638  *      sequence happens while the lock is held.
639  *
640  *      Note that the callback MUST NOT alter any of the data which
641  *      is used as the rbtree key, nor attempt to alter the rest of
642  *      the rbtree in any way.
643  *
644  *      Returns a pointer to the user data in the found node, or NULL if the
645  *      item was not found, or NULL if the item was deleted and the tree was
646  *      created with a freeNode garbage collection routine.
647  */
648 void *rbtree_callbydata(rbtree_t *tree, void const *Data, int (*callback)(void *, void *), void *context);
649
650 /*
651  *      FIFOs
652  */
653 typedef struct fr_fifo_t fr_fifo_t;
654 typedef void (*fr_fifo_free_t)(void *);
655 fr_fifo_t *fr_fifo_create(int max_entries, fr_fifo_free_t freeNode);
656 void fr_fifo_free(fr_fifo_t *fi);
657 int fr_fifo_push(fr_fifo_t *fi, void *data);
658 void *fr_fifo_pop(fr_fifo_t *fi);
659 void *fr_fifo_peek(fr_fifo_t *fi);
660 int fr_fifo_num_elements(fr_fifo_t *fi);
661
662 #ifdef __cplusplus
663 }
664 #endif
665
666 #include <freeradius-devel/packet.h>
667
668 #ifdef WITH_TCP
669 #include <freeradius-devel/tcp.h>
670 #endif
671
672 #endif /*LIBRADIUS_H*/