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 */
36 "invalid credentials", /* 21 RSE_CRED */
37 "certificate validation error", /* 22 RSE_CERT */
39 #define ERRTXT_SIZE (sizeof(_errtxt) / sizeof(*_errtxt))
41 static struct rs_error *
42 _err_vcreate (unsigned int code, const char *file, int line, const char *fmt,
45 struct rs_error *err = NULL;
47 err = malloc (sizeof(struct rs_error));
51 memset (err, 0, sizeof(struct rs_error));
54 n = vsnprintf (err->buf, sizeof(err->buf), fmt, args);
58 err->code < ERRTXT_SIZE ? _errtxt[err->code] : "",
60 n = strlen (err->buf);
64 char *sep = strrchr (file, '/');
67 snprintf (err->buf + n, sizeof(err->buf) - n, " (%s:%d)", file,
75 err_create (unsigned int code,
81 struct rs_error *err = NULL;
85 err = _err_vcreate (code, file, line, fmt, args);
92 _ctx_err_vpush_fl (struct rs_context *ctx, int code, const char *file,
93 int line, const char *fmt, va_list args)
95 struct rs_error *err = _err_vcreate (code, file, line, fmt, args);
100 /* TODO: Implement a stack. */
102 rs_err_free (ctx->err);
109 rs_err_ctx_push (struct rs_context *ctx, int code, const char *fmt, ...)
114 va_start (args, fmt);
115 r = _ctx_err_vpush_fl (ctx, code, NULL, 0, fmt, args);
122 rs_err_ctx_push_fl (struct rs_context *ctx, int code, const char *file,
123 int line, const char *fmt, ...)
128 va_start (args, fmt);
129 r = _ctx_err_vpush_fl (ctx, code, file, line, fmt, args);
136 err_conn_push_err (struct rs_connection *conn, struct rs_error *err)
140 rs_err_free (conn->err);
141 conn->err = err; /* FIXME: use a stack */
147 _conn_err_vpush_fl (struct rs_connection *conn, int code, const char *file,
148 int line, const char *fmt, va_list args)
150 struct rs_error *err = _err_vcreate (code, file, line, fmt, args);
155 return err_conn_push_err (conn, err);
159 rs_err_conn_push (struct rs_connection *conn, int code, const char *fmt, ...)
164 va_start (args, fmt);
165 r = _conn_err_vpush_fl (conn, code, NULL, 0, fmt, args);
172 rs_err_conn_push_fl (struct rs_connection *conn, int code, const char *file,
173 int line, const char *fmt, ...)
178 va_start (args, fmt);
179 r = _conn_err_vpush_fl (conn, code, file, line, fmt, args);
186 rs_err_ctx_pop (struct rs_context *ctx)
188 struct rs_error *err;
191 return NULL; /* FIXME: RSE_INVALID_CTX. */
199 rs_err_conn_pop (struct rs_connection *conn)
201 struct rs_error *err;
204 return NULL; /* FIXME: RSE_INVALID_CONN */
212 rs_err_conn_peek_code (struct rs_connection *conn)
215 return -1; /* FIXME: RSE_INVALID_CONN */
217 return conn->err->code;
223 rs_err_free (struct rs_error *err)
230 rs_err_msg (struct rs_error *err)
239 rs_err_code (struct rs_error *err, int dofree_flag)