49f9a358cb652da8e1b2222a23136ec2f60792b9
[radsecproxy.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 buf[1024];
36 };
37
38 struct rs_peer {                /* Config object for a connection.  */
39     struct rs_connection *conn;
40     struct rs_realm *realm;
41     struct evutil_addrinfo *addr;
42     char *secret;
43     struct rs_peer *next;
44 };
45
46 struct rs_realm {             /* Config object for a RADIUS realm.  */
47     char *name;
48     enum rs_conn_type type;
49     int timeout;
50     int retries;
51     char *cacertfile;
52     char *cacertpath;
53     char *certfile;
54     char *certkeyfile;
55     struct rs_peer *peers;
56     struct rs_realm *next;
57 };
58
59 struct rs_context {
60     struct rs_realm *realms;
61     struct rs_alloc_scheme alloc_scheme;
62     struct rs_error *err;
63     fr_randctx fr_randctx;
64     cfg_t *cfg;
65 };
66
67 struct rs_connection {
68     struct rs_context *ctx;
69     struct rs_realm *realm;     /* Owned by ctx.  */
70     struct event_base *evb;     /* Event base.  */
71     struct event *tev;          /* Timeout event.  */
72     struct rs_credentials transport_credentials;
73     struct rs_conn_callbacks callbacks;
74     void *user_data;
75     struct rs_peer *peers;
76     struct rs_peer *active_peer;
77     struct rs_error *err;
78     struct timeval timeout;
79     char is_connecting;         /* FIXME: replace with a single state member */
80     char is_connected;          /* FIXME: replace with a single state member */
81     int fd;                     /* Socket.  */
82     int tryagain;               /* For server failover.  */
83     int nextid;                 /* Next RADIUS packet identifier.  */
84     /* TCP transport specifics.  */
85     struct bufferevent *bev;    /* Buffer event.  */
86     /* UDP transport specifics.  */
87     struct event *wev;          /* Write event (for UDP).  */
88     struct event *rev;          /* Read event (for UDP).  */
89     struct rs_packet *out_queue; /* Queue for outgoing UDP packets.  */
90 #if defined(RS_ENABLE_TLS)
91     /* TLS specifics.  */
92     SSL_CTX *tls_ctx;
93     SSL *tls_ssl;
94 #endif
95 };
96
97 enum rs_packet_flags {
98     rs_packet_hdr_read_flag,
99     rs_packet_received_flag,
100     rs_packet_sent_flag,
101 };
102
103 struct rs_packet {
104     struct rs_connection *conn;
105     unsigned int flags;
106     uint8_t hdr[RS_HEADER_LEN];
107     RADIUS_PACKET *rpkt;
108     struct rs_packet *next;     /* Used for UDP output queue.  */
109 };
110
111 struct rs_attr {
112     struct rs_packet *pkt;
113     VALUE_PAIR *vp;
114 };
115
116 /* Nonpublic functions.  */
117 struct rs_error *_rs_resolv(struct evutil_addrinfo **addr,
118                             rs_conn_type_t type, const char *hostname,
119                             const char *service);
120 struct rs_peer *_rs_peer_create(struct rs_context *ctx,
121                                 struct rs_peer **rootp);
122 struct rs_error *_rs_err_create(unsigned int code, const char *file,
123                                 int line, const char *fmt, ...);
124 int _rs_err_conn_push_err(struct rs_connection *conn,
125                           struct rs_error *err);
126
127
128 #if defined (__cplusplus)
129 }
130 #endif
131
132 /* Convenience macros.  */
133 #define rs_calloc(h, nmemb, size) \
134     (h->alloc_scheme.calloc ? h->alloc_scheme.calloc : calloc)(nmemb, size)
135 #define rs_malloc(h, size) \
136     (h->alloc_scheme.malloc ? h->alloc_scheme.malloc : malloc)(size)
137 #define rs_free(h, ptr) \
138     (h->alloc_scheme.free ? h->alloc_scheme.free : free)(ptr)
139 #define rs_realloc(h, realloc, ptr, size) \
140     (h->alloc_scheme.realloc ? h->alloc_scheme.realloc : realloc)(ptr, size)
141 #define min(a, b) ((a) < (b) ? (a) : (b))
142 #define max(a, b) ((a) > (b) ? (a) : (b))
143
144 /* Local Variables: */
145 /* c-file-style: "stroustrup" */
146 /* End: */