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 */
35 #define ERRTXT_SIZE (sizeof(_errtxt) / sizeof(*_errtxt))
37 static struct rs_error *
38 _err_vcreate (unsigned int code, const char *file, int line, const char *fmt,
43 err = malloc (sizeof(struct rs_error));
47 memset (err, 0, sizeof(struct rs_error));
50 n = vsnprintf (err->buf, sizeof(err->buf), fmt, args);
54 err->code < ERRTXT_SIZE ? _errtxt[err->code] : "",
56 n = strlen (err->buf);
60 char *sep = strrchr (file, '/');
63 snprintf (err->buf + n, sizeof(err->buf) - n, " (%s:%d)", file,
71 _rs_err_create (unsigned int code, const char *file, int line, const char *fmt,
78 err = _err_vcreate (code, file, line, fmt, args);
84 _ctx_err_vpush_fl (struct rs_context *ctx, int code, const char *file,
85 int line, const char *fmt, va_list args)
87 struct rs_error *err = _err_vcreate (code, file, line, fmt, args);
95 rs_err_ctx_push (struct rs_context *ctx, int code, const char *fmt, ...)
99 _ctx_err_vpush_fl (ctx, code, NULL, 0, fmt, args);
105 rs_err_ctx_push_fl (struct rs_context *ctx, int code, const char *file,
106 int line, const char *fmt, ...)
109 va_start (args, fmt);
110 _ctx_err_vpush_fl (ctx, code, file, line, fmt, args);
116 _rs_err_conn_push_err (struct rs_connection *conn, struct rs_error *err)
118 conn->err = err; /* FIXME: use a stack */
123 _conn_err_vpush_fl (struct rs_connection *conn, int code, const char *file,
124 int line, const char *fmt, va_list args)
126 struct rs_error *err = _err_vcreate (code, file, line, fmt, args);
129 _rs_err_conn_push_err (conn, err);
134 rs_err_conn_push (struct rs_connection *conn, int code, const char *fmt, ...)
137 va_start (args, fmt);
138 _conn_err_vpush_fl (conn, code, NULL, 0, fmt, args);
144 rs_err_conn_push_fl (struct rs_connection *conn, int code, const char *file,
145 int line, const char *fmt, ...)
148 va_start (args, fmt);
149 _conn_err_vpush_fl (conn, code, file, line, fmt, args);
155 rs_err_ctx_pop (struct rs_context *ctx)
157 struct rs_error *err;
160 return NULL; /* FIXME: RSE_INVALID_CTX. */
167 rs_err_conn_pop (struct rs_connection *conn)
169 struct rs_error *err;
172 return NULL; /* FIXME: RSE_INVALID_CONN */
179 rs_err_conn_peek_code (struct rs_connection *conn)
181 if (conn && conn->err)
182 return conn->err->code;
188 rs_err_free (struct rs_error *err)
197 rs_err_msg (struct rs_error *err, int dofree_flag)
206 msg = strdup (err->buf);
214 rs_err_code (struct rs_error *err, int dofree_flag)