Don't free config object until we destroy the context.
[libradsec.git] / lib / include / radsec / radsec-impl.h
1 /** @file libradsec-impl.h
2     @brief Libraray internal header file for libradsec.  */
3
4 /* See the file COPYING for licensing information.  */
5
6 #include <freeradius/libradius.h>
7 #include <event2/util.h>
8 #include <confuse.h>
9 #if defined(RS_ENABLE_TLS)
10 #include <openssl/ssl.h>
11 #endif
12
13 /* Constants.  */
14 #define RS_HEADER_LEN 4
15
16 /* Data types.  */
17 enum rs_cred_type {
18     RS_CRED_NONE = 0,
19     RS_CRED_TLS_PSK_RSA,        /* RFC 4279.  */
20 };
21 typedef unsigned int rs_cred_type_t;
22
23 #if defined (__cplusplus)
24 extern "C" {
25 #endif
26
27 struct rs_credentials {
28     enum rs_cred_type type;
29     char *identity;
30     char *secret;
31 };
32
33 struct rs_error {
34     int code;
35     char *msg;
36     char buf[1024];
37 };
38
39 struct rs_peer {                /* Config object for a connection.  */
40     struct rs_connection *conn;
41     struct rs_realm *realm;
42     struct evutil_addrinfo *addr;
43     char *secret;
44     struct rs_peer *next;
45 };
46
47 struct rs_realm {             /* Config object for a RADIUS realm.  */
48     char *name;
49     enum rs_conn_type type;
50     int timeout;
51     int retries;
52     char *cacertfile;
53     char *cacertpath;
54     char *certfile;
55     char *certkeyfile;
56     struct rs_peer *peers;
57     struct rs_realm *next;
58 };
59
60 struct rs_context {
61     struct rs_realm *realms;
62     struct rs_alloc_scheme alloc_scheme;
63     struct rs_error *err;
64     fr_randctx fr_randctx;
65     cfg_t *cfg;
66 };
67
68 struct rs_connection {
69     struct rs_context *ctx;
70     struct rs_realm *realm;     /* Owned by ctx.  */
71     struct event_base *evb;     /* Event base.  */
72     struct bufferevent *bev;    /* Buffer event.  */
73     struct event *tev;          /* Timeout event.  */
74     struct rs_credentials transport_credentials;
75     struct rs_conn_callbacks callbacks;
76     void *user_data;
77     struct rs_peer *peers;
78     struct rs_peer *active_peer;
79     struct rs_error *err;
80     char is_connecting;         /* FIXME: replace with a single state member */
81     char is_connected;          /* FIXME: replace with a single state member */
82     int fd;                     /* Socket.  */
83     int tryagain;
84     int nextid;
85     int user_dispatch_flag : 1; /* User does the dispatching.  */
86 #if defined(RS_ENABLE_TLS)
87     SSL_CTX *tls_ctx;
88     SSL *tls_ssl;
89 #endif
90 };
91
92 struct rs_packet {
93     struct rs_connection *conn;
94     char hdr_read_flag;
95     uint8_t hdr[4];
96     RADIUS_PACKET *rpkt;
97     struct rs_packet *original;
98     char valid_flag;
99     char written_flag;
100 };
101
102 struct rs_attr {
103     struct rs_packet *pkt;
104     VALUE_PAIR *vp;
105 };
106
107 /* Nonpublic functions.  */
108 struct rs_error *_rs_resolv(struct evutil_addrinfo **addr,
109                             rs_conn_type_t type, const char *hostname,
110                             const char *service);
111 struct rs_peer *_rs_peer_create(struct rs_context *ctx,
112                                 struct rs_peer **rootp);
113 struct rs_error *_rs_err_create(unsigned int code, const char *file,
114                                 int line, const char *fmt, ...);
115 int _rs_err_conn_push_err(struct rs_connection *conn,
116                           struct rs_error *err);
117
118
119 #if defined (__cplusplus)
120 }
121 #endif
122
123 /* Convenience macros.  */
124 #define rs_calloc(h, nmemb, size) \
125     (h->alloc_scheme.calloc ? h->alloc_scheme.calloc : calloc)(nmemb, size)
126 #define rs_malloc(h, size) \
127     (h->alloc_scheme.malloc ? h->alloc_scheme.malloc : malloc)(size)
128 #define rs_free(h, ptr) \
129     (h->alloc_scheme.free ? h->alloc_scheme.free : free)(ptr)
130 #define rs_realloc(h, realloc, ptr, size) \
131     (h->alloc_scheme.realloc ? h->alloc_scheme.realloc : realloc)(ptr, size)
132 #define min(a, b) ((a) < (b) ? (a) : (b))
133 #define max(a, b) ((a) > (b) ? (a) : (b))
134
135 /* Local Variables: */
136 /* c-file-style: "stroustrup" */
137 /* End: */