1f2cbc099c475e345307004fd94117d81ed9388d
[radsecproxy.git] / lib / err.c
1 /* Copyright 2010, 2011 NORDUnet A/S. All rights reserved.
2    See the file COPYING for licensing information.  */
3
4 #if defined HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <assert.h>
11 #include <radsec/radsec.h>
12 #include <radsec/radsec-impl.h>
13
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 };
36 #define ERRTXT_SIZE (sizeof(_errtxt) / sizeof(*_errtxt))
37
38 static struct rs_error *
39 _err_vcreate (unsigned int code, const char *file, int line, const char *fmt,
40               va_list args)
41 {
42   struct rs_error *err;
43
44   err = malloc (sizeof(struct rs_error));
45   if (err)
46     {
47       int n;
48       memset (err, 0, sizeof(struct rs_error));
49       err->code = code;
50       if (fmt)
51         n = vsnprintf (err->buf, sizeof(err->buf), fmt, args);
52       else
53         {
54           strncpy (err->buf,
55                    err->code < ERRTXT_SIZE ? _errtxt[err->code] : "",
56                    sizeof(err->buf));
57           n = strlen (err->buf);
58         }
59       if (n >= 0 && file)
60         {
61           char *sep = strrchr (file, '/');
62           if (sep)
63             file = sep + 1;
64           snprintf (err->buf + n, sizeof(err->buf) - n, " (%s:%d)", file,
65                     line);
66         }
67     }
68   return err;
69 }
70
71 struct rs_error *
72 _rs_err_create (unsigned int code, const char *file, int line, const char *fmt,
73                 ...)
74 {
75   struct rs_error *err;
76
77   va_list args;
78   va_start (args, fmt);
79   err = _err_vcreate (code, file, line, fmt, args);
80   va_end (args);
81   return err;
82 }
83
84 static int
85 _ctx_err_vpush_fl (struct rs_context *ctx, int code, const char *file,
86                    int line, const char *fmt, va_list args)
87 {
88   struct rs_error *err = _err_vcreate (code, file, line, fmt, args);
89
90   if (err)
91     ctx->err = err;
92   return code;
93 }
94
95 int
96 rs_err_ctx_push (struct rs_context *ctx, int code, const char *fmt, ...)
97 {
98   va_list args;
99   va_start (args, fmt);
100   _ctx_err_vpush_fl (ctx, code, NULL, 0, fmt, args);
101   va_end (args);
102   return code;
103 }
104
105 int
106 rs_err_ctx_push_fl (struct rs_context *ctx, int code, const char *file,
107                     int line, const char *fmt, ...)
108 {
109   va_list args;
110   va_start (args, fmt);
111   _ctx_err_vpush_fl (ctx, code, file, line, fmt, args);
112   va_end (args);
113   return code;
114 }
115
116 int
117 _rs_err_conn_push_err (struct rs_connection *conn, struct rs_error *err)
118 {
119   conn->err = err;              /* FIXME: use a stack */
120   return err->code;
121 }
122
123 static int
124 _conn_err_vpush_fl (struct rs_connection *conn, int code, const char *file,
125                     int line, const char *fmt, va_list args)
126 {
127   struct rs_error *err = _err_vcreate (code, file, line, fmt, args);
128
129   if (err)
130     _rs_err_conn_push_err (conn, err);
131   return code;
132 }
133
134 int
135 rs_err_conn_push (struct rs_connection *conn, int code, const char *fmt, ...)
136 {
137   va_list args;
138   va_start (args, fmt);
139   _conn_err_vpush_fl (conn, code, NULL, 0, fmt, args);
140   va_end (args);
141   return code;
142 }
143
144 int
145 rs_err_conn_push_fl (struct rs_connection *conn, int code, const char *file,
146                      int line, const char *fmt, ...)
147 {
148   va_list args;
149   va_start (args, fmt);
150   _conn_err_vpush_fl (conn, code, file, line, fmt, args);
151   va_end (args);
152   return code;
153 }
154
155 struct rs_error *
156 rs_err_ctx_pop (struct rs_context *ctx)
157 {
158   struct rs_error *err;
159
160   if (!ctx)
161     return NULL;                /* FIXME: RSE_INVALID_CTX.  */
162   err = ctx->err;
163   ctx->err = NULL;
164   return err;
165 }
166
167 struct rs_error *
168 rs_err_conn_pop (struct rs_connection *conn)
169 {
170   struct rs_error *err;
171
172   if (!conn)
173     return NULL;                /* FIXME: RSE_INVALID_CONN */
174   err = conn->err;
175   conn->err = NULL;
176   return err;
177 }
178
179 int
180 rs_err_conn_peek_code (struct rs_connection *conn)
181 {
182   if (!conn)
183     return -1;                  /* FIXME: RSE_INVALID_CONN */
184   if (conn->err)
185     return conn->err->code;
186   else
187     return RSE_OK;
188 }
189
190 void
191 rs_err_free (struct rs_error *err)
192 {
193   assert (err);
194   free (err);
195 }
196
197 char *
198 rs_err_msg (struct rs_error *err)
199 {
200   if (!err)
201     return NULL;
202   return err->buf;
203 }
204
205 int
206 rs_err_code (struct rs_error *err, int dofree_flag)
207 {
208   int code;
209
210   if (!err)
211     return -1;
212   code = err->code;
213
214   if (dofree_flag)
215     rs_err_free(err);
216   return code;
217 }