66c5d94fb13153d8aec65b9b2d0e3af83b84d25f
[radsecproxy.git] / lib / err.c
1 #include <assert.h>
2 #include "libradsec.h"
3 #include "libradsec-impl.h"
4
5 const char *_errtxt[] = {
6   "SUCCESS",                    /* 0 RSE_OK */
7   "NOMEM",                      /* 1 RSE_NOMEM */
8   "NYI -- not yet implemented", /* 2 RSE_NOSYS */
9   "invalid handle"              /* 3 RSE_INVALID_CTX */
10   "invalid connection"          /* 4 RSE_INVALID_CONN */
11   "ERR 5"                       /*  RSE_ */
12   "ERR 6"                       /*  RSE_ */
13   "ERR 7"                       /*  RSE_ */
14   "ERR 8"                       /*  RSE_ */
15   "ERR 9"                       /*  RSE_ */
16   "ERR 10"                      /*  RSE_ */
17   "ERR 11"                      /*  RSE_ */
18   "ERR 12"                      /*  RSE_ */
19   "ERR 13"                      /*  RSE_ */
20   "ERR "                        /*  RSE_ */
21   "ERR "                        /*  RSE_ */
22   "ERR "                        /*  RSE_ */
23   "ERR "                        /*  RSE_ */
24   "ERR "                        /*  RSE_ */
25   "ERR "                        /*  RSE_ */
26   "ERR "                        /*  RSE_ */
27   "some error"                  /* 21 RSE_SOME_ERROR */
28 };
29
30 static struct rs_error *
31 _err_new (unsigned int code, const char *msg)
32 {
33   struct rs_error *err;
34
35   err = malloc (sizeof (struct rs_error));
36   if (err)
37     {
38       memset (err, 0, sizeof (struct rs_error));
39       err->code = code;
40       snprintf (err->buf, sizeof (err->buf), "%s: %s",
41                 code < sizeof (_errtxt) / sizeof (*_errtxt) ?
42                 _errtxt[code] : "invalid error index",
43                 msg);
44     }
45   return err;
46 }
47
48 int
49 rs_ctx_err_push (struct rs_handle *ctx, int code, const char *msg)
50 {
51   struct rs_error *err = _err_new (code, msg);
52
53   if (err)
54     ctx->err = err;
55   return code;
56 }
57
58 int
59 rs_conn_err_push (struct rs_connection *conn, int code, const char *msg)
60 {
61   struct rs_error *err = _err_new (code, msg);
62
63   if (err)
64     conn->err = err;
65   return code;
66 }
67
68 struct rs_error *
69 rs_ctx_err_pop (struct rs_handle *ctx)
70 {
71   struct rs_error *err;
72
73   if (!ctx)
74     return NULL;                /* FIXME: RSE_INVALID_CTX.  */
75   err = ctx->err;
76   ctx->err = NULL;
77   return err;
78 }
79
80 struct rs_error *
81 rs_conn_err_pop (struct rs_connection *conn)
82 {
83   struct rs_error *err;
84
85   if (!conn)
86     return NULL;                /* FIXME: RSE_INVALID_CONN */
87   err = conn->err;
88   conn->err = NULL;
89   return err;
90 }
91
92 void
93 rs_err_free (struct rs_error *err)
94 {
95   assert (err);
96   if (err->msg)
97     free (err->msg);
98   free (err);
99 }
100
101 char *
102 rs_err_msg (struct rs_error *err)
103 {
104   char *msg;
105
106   if (err->msg)
107     msg = err->msg;
108   else
109     msg = strdup (err->buf);
110
111   rs_err_free (err);
112   return msg;
113 }
114
115 int
116 rs_err_code (struct rs_error *err)
117 {
118   int code = err->code;
119   rs_err_free(err);
120   return code;
121 }