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 */
36 #define ERRTXT_SIZE (sizeof(_errtxt) / sizeof(*_errtxt))
38 static struct rs_error *
39 _err_vcreate (unsigned int code, const char *file, int line, const char *fmt,
42 struct rs_error *err = NULL;
44 err = malloc (sizeof(struct rs_error));
48 memset (err, 0, sizeof(struct rs_error));
51 n = vsnprintf (err->buf, sizeof(err->buf), fmt, args);
55 err->code < ERRTXT_SIZE ? _errtxt[err->code] : "",
57 n = strlen (err->buf);
61 char *sep = strrchr (file, '/');
64 snprintf (err->buf + n, sizeof(err->buf) - n, " (%s:%d)", file,
72 err_create (unsigned int code,
78 struct rs_error *err = NULL;
82 err = _err_vcreate (code, file, line, fmt, args);
89 _ctx_err_vpush_fl (struct rs_context *ctx, int code, const char *file,
90 int line, const char *fmt, va_list args)
92 struct rs_error *err = _err_vcreate (code, file, line, fmt, args);
97 /* TODO: Implement a stack. */
99 rs_err_free (ctx->err);
106 rs_err_ctx_push (struct rs_context *ctx, int code, const char *fmt, ...)
111 va_start (args, fmt);
112 r = _ctx_err_vpush_fl (ctx, code, NULL, 0, fmt, args);
119 rs_err_ctx_push_fl (struct rs_context *ctx, int code, const char *file,
120 int line, const char *fmt, ...)
125 va_start (args, fmt);
126 r = _ctx_err_vpush_fl (ctx, code, file, line, fmt, args);
133 err_conn_push_err (struct rs_connection *conn, struct rs_error *err)
137 rs_err_free (conn->err);
138 conn->err = err; /* FIXME: use a stack */
144 _conn_err_vpush_fl (struct rs_connection *conn, int code, const char *file,
145 int line, const char *fmt, va_list args)
147 struct rs_error *err = _err_vcreate (code, file, line, fmt, args);
152 return err_conn_push_err (conn, err);
156 rs_err_conn_push (struct rs_connection *conn, int code, const char *fmt, ...)
161 va_start (args, fmt);
162 r = _conn_err_vpush_fl (conn, code, NULL, 0, fmt, args);
169 rs_err_conn_push_fl (struct rs_connection *conn, int code, const char *file,
170 int line, const char *fmt, ...)
175 va_start (args, fmt);
176 r = _conn_err_vpush_fl (conn, code, file, line, fmt, args);
183 rs_err_ctx_pop (struct rs_context *ctx)
185 struct rs_error *err;
188 return NULL; /* FIXME: RSE_INVALID_CTX. */
196 rs_err_conn_pop (struct rs_connection *conn)
198 struct rs_error *err;
201 return NULL; /* FIXME: RSE_INVALID_CONN */
209 rs_err_conn_peek_code (struct rs_connection *conn)
212 return -1; /* FIXME: RSE_INVALID_CONN */
214 return conn->err->code;
220 rs_err_free (struct rs_error *err)
227 rs_err_msg (struct rs_error *err)
236 rs_err_code (struct rs_error *err, int dofree_flag)