1 /* Copyright 2010, 2011 NORDUnet A/S. All rights reserved.
2 See the file COPYING for licensing information. */
4 #if defined HAVE_CONFIG_H
11 #include <radsec/radsec.h>
12 #include <radsec/radsec-impl.h>
14 static const char *_errtxt[] = {
15 "SUCCESS", /* 0 RSE_OK */
16 "out of memory", /* 1 RSE_NOMEM */
17 "not yet implemented", /* 2 RSE_NOSYS */
18 "invalid handle", /* 3 RSE_INVALID_CTX */
19 "invalid connection", /* 4 RSE_INVALID_CONN */
20 "connection type mismatch", /* 5 RSE_CONN_TYPE_MISMATCH */
21 "FreeRadius error", /* 6 RSE_FR */
22 "bad hostname or port", /* 7 RSE_BADADDR */
23 "no peer configured", /* 8 RSE_NOPEER */
24 "libevent error", /* 9 RSE_EVENT */
25 "socket error", /* 10 RSE_SOCKERR */
26 "invalid configuration file", /* 11 RSE_CONFIG */
27 "authentication failed", /* 12 RSE_BADAUTH */
28 "internal error", /* 13 RSE_INTERNAL */
29 "SSL error", /* 14 RSE_SSLERR */
30 "invalid packet", /* 15 RSE_INVALID_PKT */
31 "connect timeout", /* 16 RSE_TIMEOUT_CONN */
32 "invalid argument", /* 17 RSE_INVAL */
33 "I/O timeout", /* 18 RSE_TIMEOUT_IO */
34 "timeout", /* 19 RSE_TIMEOUT */
35 "peer disconnected", /* 20 RSE_DISCO */
37 #define ERRTXT_SIZE (sizeof(_errtxt) / sizeof(*_errtxt))
39 static struct rs_error *
40 _err_vcreate (unsigned int code, const char *file, int line, const char *fmt,
43 struct rs_error *err = NULL;
45 err = malloc (sizeof(struct rs_error));
49 memset (err, 0, sizeof(struct rs_error));
52 n = vsnprintf (err->buf, sizeof(err->buf), fmt, args);
56 err->code < ERRTXT_SIZE ? _errtxt[err->code] : "",
58 n = strlen (err->buf);
62 char *sep = strrchr (file, '/');
65 snprintf (err->buf + n, sizeof(err->buf) - n, " (%s:%d)", file,
73 err_create (unsigned int code,
79 struct rs_error *err = NULL;
83 err = _err_vcreate (code, file, line, fmt, args);
90 _ctx_err_vpush_fl (struct rs_context *ctx, int code, const char *file,
91 int line, const char *fmt, va_list args)
93 struct rs_error *err = _err_vcreate (code, file, line, fmt, args);
98 /* TODO: Implement a stack. */
100 rs_err_free (ctx->err);
107 rs_err_ctx_push (struct rs_context *ctx, int code, const char *fmt, ...)
112 va_start (args, fmt);
113 r = _ctx_err_vpush_fl (ctx, code, NULL, 0, fmt, args);
120 rs_err_ctx_push_fl (struct rs_context *ctx, int code, const char *file,
121 int line, const char *fmt, ...)
126 va_start (args, fmt);
127 r = _ctx_err_vpush_fl (ctx, code, file, line, fmt, args);
134 err_conn_push_err (struct rs_connection *conn, struct rs_error *err)
138 rs_err_free (conn->err);
139 conn->err = err; /* FIXME: use a stack */
145 _conn_err_vpush_fl (struct rs_connection *conn, int code, const char *file,
146 int line, const char *fmt, va_list args)
148 struct rs_error *err = _err_vcreate (code, file, line, fmt, args);
153 return err_conn_push_err (conn, err);
157 rs_err_conn_push (struct rs_connection *conn, int code, const char *fmt, ...)
162 va_start (args, fmt);
163 r = _conn_err_vpush_fl (conn, code, NULL, 0, fmt, args);
170 rs_err_conn_push_fl (struct rs_connection *conn, int code, const char *file,
171 int line, const char *fmt, ...)
176 va_start (args, fmt);
177 r = _conn_err_vpush_fl (conn, code, file, line, fmt, args);
184 rs_err_ctx_pop (struct rs_context *ctx)
186 struct rs_error *err;
189 return NULL; /* FIXME: RSE_INVALID_CTX. */
197 rs_err_conn_pop (struct rs_connection *conn)
199 struct rs_error *err;
202 return NULL; /* FIXME: RSE_INVALID_CONN */
210 rs_err_conn_peek_code (struct rs_connection *conn)
213 return -1; /* FIXME: RSE_INVALID_CONN */
215 return conn->err->code;
221 rs_err_free (struct rs_error *err)
228 rs_err_msg (struct rs_error *err)
237 rs_err_code (struct rs_error *err, int dofree_flag)