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