WIP -- resolving, crafting packet.
[radsecproxy.git] / lib / err.c
1 #include <stdio.h>
2 #include <assert.h>
3 #include "libradsec.h"
4 #include "libradsec-impl.h"
5
6 const char *_errtxt[] = {
7   "SUCCESS",                    /* 0 RSE_OK */
8   "NOMEM",                      /* 1 RSE_NOMEM */
9   "NYI -- not yet implemented", /* 2 RSE_NOSYS */
10   "invalid handle"              /* 3 RSE_INVALID_CTX */
11   "invalid connection"          /* 4 RSE_INVALID_CONN */
12   "connection type mismatch"    /* 5 RSE_CONN_TYPE_MISMATCH */
13   "FreeRadius error"            /* 6 RSE_FR */
14   "bad hostname or port"        /* 7 RSE_BADADDR */
15   "no peer configured"          /* 8 RSE_NOPEER */
16   "ERR 9"                       /*  RSE_ */
17   "ERR 10"                      /*  RSE_ */
18   "ERR 11"                      /*  RSE_ */
19   "ERR 12"                      /*  RSE_ */
20   "ERR 13"                      /*  RSE_ */
21   "ERR "                        /*  RSE_ */
22   "ERR "                        /*  RSE_ */
23   "ERR "                        /*  RSE_ */
24   "ERR "                        /*  RSE_ */
25   "ERR "                        /*  RSE_ */
26   "ERR "                        /*  RSE_ */
27   "ERR "                        /*  RSE_ */
28   "some error"                  /* 21 RSE_SOME_ERROR */
29 };
30
31 static struct rs_error *
32 _err_new (unsigned int code, const char *file, int line, const char *fmt, va_list args)
33 {
34   struct rs_error *err;
35
36   err = malloc (sizeof(struct rs_error));
37   if (err)
38     {
39       int n;
40       memset (err, 0, sizeof(struct rs_error));
41       err->code = code;
42       n = vsnprintf (err->buf, sizeof(err->buf), fmt, args);
43       if (n > 0)
44         {
45           char *sep = strrchr (file, '/');
46           if (sep)
47             file = sep + 1;
48           snprintf (err->buf + n, sizeof(err->buf) - n, " (%s: %d)", file,
49                     line);
50         }
51     }
52   return err;
53 }
54
55 static int
56 _ctx_err_vpush_fl (struct rs_handle *ctx, int code, const char *file, int line, const char *fmt, va_list args)
57 {
58   struct rs_error *err = _err_new (code, file, line, fmt, args);
59
60   if (err)
61     ctx->err = err;
62   return code;
63 }
64
65 int
66 rs_ctx_err_push (struct rs_handle *ctx, int code, const char *fmt, ...)
67 {
68   va_list args;
69   va_start (args, fmt);
70   _ctx_err_vpush_fl (ctx, code, NULL, 0, fmt, args);
71   va_end (args);
72   return code;
73 }
74
75 int
76 rs_ctx_err_push_fl (struct rs_handle *ctx, int code, const char *file, int line, const char *fmt, ...)
77 {
78   va_list args;
79   va_start (args, fmt);
80   _ctx_err_vpush_fl (ctx, code, file, line, fmt, args);
81   va_end (args);
82   return code;
83 }
84
85 static int
86 _conn_err_vpush_fl (struct rs_connection *conn, int code, const char *file, int line, const char *fmt, va_list args)
87 {
88   struct rs_error *err = _err_new (code, file, line, fmt, args);
89
90   if (err)
91     conn->err = err;
92   return code;
93 }
94
95 int
96 rs_conn_err_push (struct rs_connection *conn, int code, const char *fmt, ...)
97 {
98   va_list args;
99   va_start (args, fmt);
100   _conn_err_vpush_fl (conn, code, NULL, 0, fmt, args);
101   va_end (args);
102   return code;
103 }
104
105 int
106 rs_conn_err_push_fl (struct rs_connection *conn, int code, const char *file, int line, const char *fmt, ...)
107 {
108   va_list args;
109   va_start (args, fmt);
110   _conn_err_vpush_fl (conn, code, file, line, fmt, args);
111   va_end (args);
112   return code;
113 }
114
115 struct rs_error *
116 rs_ctx_err_pop (struct rs_handle *ctx)
117 {
118   struct rs_error *err;
119
120   if (!ctx)
121     return NULL;                /* FIXME: RSE_INVALID_CTX.  */
122   err = ctx->err;
123   ctx->err = NULL;
124   return err;
125 }
126
127 struct rs_error *
128 rs_conn_err_pop (struct rs_connection *conn)
129 {
130   struct rs_error *err;
131
132   if (!conn)
133     return NULL;                /* FIXME: RSE_INVALID_CONN */
134   err = conn->err;
135   conn->err = NULL;
136   return err;
137 }
138
139 void
140 rs_err_free (struct rs_error *err)
141 {
142   assert (err);
143   if (err->msg)
144     free (err->msg);
145   free (err);
146 }
147
148 char *
149 rs_err_msg (struct rs_error *err, int dofree_flag)
150 {
151   char *msg;
152
153   if (!err)
154     return NULL;
155   if (err->msg)
156     msg = err->msg;
157   else
158     msg = strdup (err->buf);
159
160   if (dofree_flag)
161     rs_err_free (err);
162   return msg;
163 }
164
165 int
166 rs_err_code (struct rs_error *err, int dofree_flag)
167 {
168   int code;
169
170   if (!err)
171     return -1;
172   code = err->code;
173
174   if (dofree_flag)
175     rs_err_free(err);
176   return code;
177 }