1 /* Copyright 2010, 2011 NORDUnet A/S. All rights reserved.
2 See the file COPYING for licensing information. */
4 #if defined HAVE_CONFIG_H
12 #include <radsec/radsec.h>
13 #include <radsec/radsec-impl.h>
15 static const char *_errtxt[] = {
16 "SUCCESS", /* 0 RSE_OK */
17 "out of memory", /* 1 RSE_NOMEM */
18 "not yet implemented", /* 2 RSE_NOSYS */
19 "invalid handle", /* 3 RSE_INVALID_CTX */
20 "invalid connection", /* 4 RSE_INVALID_CONN */
21 "connection type mismatch", /* 5 RSE_CONN_TYPE_MISMATCH */
22 "FreeRadius error", /* 6 RSE_FR */
23 "bad hostname or port", /* 7 RSE_BADADDR */
24 "no peer configured", /* 8 RSE_NOPEER */
25 "libevent error", /* 9 RSE_EVENT */
26 "socket error", /* 10 RSE_SOCKERR */
27 "invalid configuration file", /* 11 RSE_CONFIG */
28 "authentication failed", /* 12 RSE_BADAUTH */
29 "internal error", /* 13 RSE_INTERNAL */
30 "SSL error", /* 14 RSE_SSLERR */
31 "invalid packet", /* 15 RSE_INVALID_PKT */
32 "connect timeout", /* 16 RSE_TIMEOUT_CONN */
33 "invalid argument", /* 17 RSE_INVAL */
34 "I/O timeout", /* 18 RSE_TIMEOUT_IO */
35 "timeout", /* 19 RSE_TIMEOUT */
36 "peer disconnected", /* 20 RSE_DISCO */
37 "resource is in use", /* 21 RSE_INUSE */
38 "packet is too small", /* 22 RSE_PACKET_TOO_SMALL */
39 "packet is too large", /* 23 RSE_PACKET_TOO_LARGE */
40 "attribute overflows packet", /* 24 RSE_ATTR_OVERFLOW */
41 "attribute is too small", /* 25 RSE_ATTR_TOO_SMALL */
42 "attribute is too large", /* 26 RSE_ATTR_TOO_LARGE */
43 "unknown attribute", /* 27 RSE_ATTR_UNKNOWN */
44 "invalid name for attribute", /* 28 RSE_ATTR_BAD_NAME */
45 "invalid value for attribute", /* 29 RSE_ATTR_VALUE_MALFORMED */
46 "invalid attribute", /* 30 RSE_ATTR_INVALID */
47 "too many attributes in the packet", /* 31 RSE_TOO_MANY_ATTRS */
48 "attribute type unknown", /* 32 RSE_ATTR_TYPE_UNKNOWN */
49 "invalid message authenticator", /* 33 RSE_MSG_AUTH_LEN */
50 "incorrect message authenticator", /* 34 RSE_MSG_AUTH_WRONG */
51 "request is required", /* 35 RSE_REQUEST_REQUIRED */
52 "invalid request code", /* 36 RSE_REQUEST_CODE_INVALID */
53 "incorrect request authenticator", /* 37 RSE_AUTH_VECTOR_WRONG */
54 "response code is unsupported", /* 38 RSE_INVALID_RESPONSE_CODE */
55 "response ID is invalid", /* 39 RSE_INVALID_RESPONSE_ID */
56 "response from the wrong source address", /* 40 RSE_INVALID_RESPONSE_SRC */
57 "no packet data", /* 41 RSE_NO_PACKET_DATA */
58 "vendor is unknown", /* 42 RSE_VENDOR_UNKNOWN */
60 #define ERRTXT_SIZE (sizeof(_errtxt) / sizeof(*_errtxt))
62 static struct rs_error *
63 _err_vcreate (unsigned int code, const char *file, int line, const char *fmt,
66 struct rs_error *err = NULL;
68 err = malloc (sizeof(struct rs_error));
72 memset (err, 0, sizeof(struct rs_error));
75 n = vsnprintf (err->buf, sizeof(err->buf), fmt, args);
79 err->code < ERRTXT_SIZE ? _errtxt[err->code] : "",
81 n = strlen (err->buf);
85 char *sep = strrchr (file, '/');
88 snprintf (err->buf + n, sizeof(err->buf) - n, " (%s:%d)", file,
96 err_create (unsigned int code,
102 struct rs_error *err = NULL;
105 va_start (args, fmt);
106 err = _err_vcreate (code, file, line, fmt, args);
113 _ctx_err_vpush_fl (struct rs_context *ctx, int code, const char *file,
114 int line, const char *fmt, va_list args)
116 struct rs_error *err = _err_vcreate (code, file, line, fmt, args);
121 /* TODO: Implement a stack. */
123 rs_err_free (ctx->err);
130 rs_err_ctx_push (struct rs_context *ctx, int code, const char *fmt, ...)
135 va_start (args, fmt);
136 r = _ctx_err_vpush_fl (ctx, code, NULL, 0, fmt, args);
143 rs_err_ctx_push_fl (struct rs_context *ctx, int code, const char *file,
144 int line, const char *fmt, ...)
149 va_start (args, fmt);
150 r = _ctx_err_vpush_fl (ctx, code, file, line, fmt, args);
157 err_conn_push_err (struct rs_connection *conn, struct rs_error *err)
161 rs_err_free (conn->err);
162 conn->err = err; /* FIXME: use a stack */
168 _conn_err_vpush_fl (struct rs_connection *conn, int code, const char *file,
169 int line, const char *fmt, va_list args)
171 struct rs_error *err = _err_vcreate (code, file, line, fmt, args);
176 return err_conn_push_err (conn, err);
180 rs_err_conn_push (struct rs_connection *conn, int code, const char *fmt, ...)
185 va_start (args, fmt);
186 r = _conn_err_vpush_fl (conn, code, NULL, 0, fmt, args);
193 rs_err_conn_push_fl (struct rs_connection *conn, int code, const char *file,
194 int line, const char *fmt, ...)
199 va_start (args, fmt);
200 r = _conn_err_vpush_fl (conn, code, file, line, fmt, args);
207 rs_err_ctx_pop (struct rs_context *ctx)
209 struct rs_error *err;
212 return NULL; /* FIXME: RSE_INVALID_CTX. */
220 rs_err_conn_pop (struct rs_connection *conn)
222 struct rs_error *err;
225 return NULL; /* FIXME: RSE_INVALID_CONN */
233 rs_err_conn_peek_code (struct rs_connection *conn)
236 return -1; /* FIXME: RSE_INVALID_CONN */
238 return conn->err->code;
244 rs_err_free (struct rs_error *err)
251 rs_err_msg (struct rs_error *err)
260 rs_err_code (struct rs_error *err, int dofree_flag)