f620fed5027cd3546711925e5a956b07aa08ca5f
[radsecproxy.git] / lib / include / radsec / radsec.h
1 /** @file libradsec.h
2     @brief Header file for libradsec.  */
3 /* See the file COPYING for licensing information.  */
4
5 #include <unistd.h>
6 #include <sys/time.h>
7
8 enum rs_error_code {
9     RSE_OK = 0,
10     RSE_NOMEM = 1,
11     RSE_NOSYS = 2,
12     RSE_INVALID_CTX = 3,
13     RSE_INVALID_CONN = 4,
14     RSE_CONN_TYPE_MISMATCH = 5,
15     RSE_FR = 6,                 /* FreeRADIUS error.  */
16     RSE_BADADDR = 7,
17     RSE_NOPEER = 8,
18     RSE_EVENT = 9,              /* libevent error.  */
19     RSE_SOCKERR = 10,
20     RSE_CONFIG = 11,
21     RSE_BADAUTH = 12,
22     RSE_INTERNAL = 13,
23     RSE_SSLERR = 14,            /* OpenSSL error.  */
24     RSE_INVALID_PKT = 15,
25     RSE_TIMEOUT_CONN = 16,      /* Connection timeout.  */
26     RSE_INVAL = 17,             /* Invalid argument.  */
27     RSE_TIMEOUT_IO = 18,        /* I/O timeout.  */
28     RSE_TIMEOUT = 19,           /* High level timeout.  */
29 };
30
31 enum rs_conn_type {
32     RS_CONN_TYPE_NONE = 0,
33     RS_CONN_TYPE_UDP,
34     RS_CONN_TYPE_TCP,
35     RS_CONN_TYPE_TLS,
36     RS_CONN_TYPE_DTLS,
37 };
38 typedef unsigned int rs_conn_type_t;
39
40
41 #if defined (__cplusplus)
42 extern "C" {
43 #endif
44
45 /* Data types.  */
46 struct rs_context;              /* radsec-impl.h */
47 struct rs_connection;           /* radsec-impl.h */
48 struct rs_packet;               /* radsec-impl.h */
49 struct rs_conn;                 /* radsec-impl.h */
50 struct rs_attr;                 /* radsec-impl.h */
51 struct rs_error;                /* radsec-impl.h */
52 struct rs_peer;                 /* radsec-impl.h */
53 struct radius_packet;           /* <freeradius/libradius.h> */
54 struct event_base;              /* <event2/event-internal.h> */
55
56 typedef void *(*rs_calloc_fp) (size_t nmemb, size_t size);
57 typedef void *(*rs_malloc_fp) (size_t size);
58 typedef void (*rs_free_fp) (void *ptr);
59 typedef void *(*rs_realloc_fp) (void *ptr, size_t size);
60 struct rs_alloc_scheme {
61     rs_calloc_fp calloc;
62     rs_malloc_fp malloc;
63     rs_free_fp free;
64     rs_realloc_fp realloc;
65 };
66
67 typedef void (*rs_conn_connected_cb) (void *user_data /* FIXME: peer? */ );
68 typedef void (*rs_conn_disconnected_cb) (void *user_data /* FIXME: reason? */ );
69 typedef void (*rs_conn_packet_received_cb) (struct rs_packet *packet,
70                                             void *user_data);
71 typedef void (*rs_conn_packet_sent_cb) (void *user_data);
72 struct rs_conn_callbacks {
73     /** Callback invoked when the connection has been established.  */
74     rs_conn_connected_cb connected_cb;
75     /** Callback invoked when the connection has been torn down.  */
76     rs_conn_disconnected_cb disconnected_cb;
77     /** Callback invoked when a packet was received.  */
78     rs_conn_packet_received_cb received_cb;
79     /** Callback invoked when a packet was successfully sent.  */
80     rs_conn_packet_sent_cb sent_cb;
81 };
82
83
84 /* Function prototypes.  */
85 /* Context.  */
86 int rs_context_create(struct rs_context **ctx, const char *dict);
87 void rs_context_destroy(struct rs_context *ctx);
88 int rs_context_set_alloc_scheme(struct rs_context *ctx,
89                                 struct rs_alloc_scheme *scheme);
90 int rs_context_read_config(struct rs_context *ctx, const char *config_file);
91
92 /* Connection.  */
93 int rs_conn_create(struct rs_context *ctx,
94                    struct rs_connection **conn,
95                    const char *config);
96 void rs_conn_set_type(struct rs_connection *conn, rs_conn_type_t type);
97 int rs_conn_add_listener(struct rs_connection *conn,
98                          rs_conn_type_t type,
99                          const char *hostname,
100                          int port);
101 int rs_conn_disconnect (struct rs_connection *conn);
102 int rs_conn_destroy(struct rs_connection *conn);
103 int rs_conn_set_eventbase(struct rs_connection *conn, struct event_base *eb);
104 void rs_conn_set_callbacks(struct rs_connection *conn,
105                            struct rs_conn_callbacks *cb);
106 void rs_conn_del_callbacks(struct rs_connection *conn);
107 struct rs_conn_callbacks *rs_conn_get_callbacks(struct rs_connection *conn);
108 int rs_conn_select_peer(struct rs_connection *conn, const char *name);
109 int rs_conn_get_current_peer(struct rs_connection *conn,
110                              const char *name,
111                              size_t buflen);
112 int rs_conn_receive_packet(struct rs_connection *conn,
113                            struct rs_packet *request,
114                            struct rs_packet **pkt_out);
115 int rs_conn_fd(struct rs_connection *conn);
116 void rs_conn_set_timeout(struct rs_connection *conn, struct timeval *tv);
117
118 /* Peer -- client and server.  */
119 int rs_peer_create(struct rs_connection *conn, struct rs_peer **peer_out);
120 int rs_peer_set_address(struct rs_peer *peer, const char *hostname,
121                         const char *service);
122 int rs_peer_set_secret(struct rs_peer *peer, const char *secret);
123 void rs_peer_set_timeout(struct rs_peer *peer, int timeout);
124 void rs_peer_set_retries(struct rs_peer *peer, int retries);
125
126 /* Packet.  */
127 int rs_packet_create(struct rs_connection *conn, struct rs_packet **pkt_out);
128 void rs_packet_destroy(struct rs_packet *pkt);
129 void rs_packet_add_attr(struct rs_packet *pkt, struct rs_attr *attr);
130 int rs_packet_send(struct rs_packet *pkt, void *user_data);
131 struct radius_packet *rs_packet_frpkt(struct rs_packet *pkt);
132 int rs_packet_create_authn_request(struct rs_connection *conn,
133                                    struct rs_packet **pkt,
134                                    const char *user_name,
135                                    const char *user_pw);
136
137 /* Attribute.  */
138 /* FIXME: Replace (or complement) with a wrapper for paircreate().  */
139 int rs_attr_create(struct rs_connection *conn,
140                    struct rs_attr **attr,
141                    const char *type,
142                    const char *val);
143 void rs_attr_destroy(struct rs_attr *attr);
144
145 /* Config.  */
146 struct rs_realm *rs_conf_find_realm(struct rs_context *ctx, const char *name);
147
148 /* Error.  */
149 int rs_err_ctx_push(struct rs_context *ctx, int code, const char *fmt, ...);
150 int rs_err_ctx_push_fl(struct rs_context *ctx,
151                        int code,
152                        const char *file,
153                        int line,
154                        const char *fmt,
155                        ...);
156 struct rs_error *rs_err_ctx_pop(struct rs_context *ctx);
157 int rs_err_conn_push(struct rs_connection *conn,
158                      int code,
159                      const char *fmt,
160                      ...);
161 int rs_err_conn_push_fl(struct rs_connection *conn,
162                         int code,
163                         const char *file,
164                         int line,
165                         const char *fmt,
166                         ...);
167 struct rs_error *rs_err_conn_pop(struct rs_connection *conn);
168 int rs_err_conn_peek_code (struct rs_connection *conn);
169 void rs_err_free(struct rs_error *err);
170 char *rs_err_msg(struct rs_error *err);
171 int rs_err_code(struct rs_error *err, int dofree_flag);
172
173 #if defined (__cplusplus)
174 }
175 #endif
176
177 /* Local Variables: */
178 /* c-file-style: "stroustrup" */
179 /* End: */