Update copyright to JANET(UK)
[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 <stdlib.h>
10 #include <string.h>
11 #include <assert.h>
12 #include <radsec/radsec.h>
13 #include <radsec/radsec-impl.h>
14
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 */
59 };
60 #define ERRTXT_SIZE (sizeof(_errtxt) / sizeof(*_errtxt))
61
62 static struct rs_error *
63 _err_vcreate (unsigned int code, const char *file, int line, const char *fmt,
64               va_list args)
65 {
66   struct rs_error *err = NULL;
67
68   err = malloc (sizeof(struct rs_error));
69   if (err)
70     {
71       int n;
72       memset (err, 0, sizeof(struct rs_error));
73       err->code = code;
74       if (fmt)
75         n = vsnprintf (err->buf, sizeof(err->buf), fmt, args);
76       else
77         {
78           strncpy (err->buf,
79                    err->code < ERRTXT_SIZE ? _errtxt[err->code] : "",
80                    sizeof(err->buf));
81           n = strlen (err->buf);
82         }
83       if (n >= 0 && file)
84         {
85           char *sep = strrchr (file, '/');
86           if (sep)
87             file = sep + 1;
88           snprintf (err->buf + n, sizeof(err->buf) - n, " (%s:%d)", file,
89                     line);
90         }
91     }
92   return err;
93 }
94
95 struct rs_error *
96 err_create (unsigned int code,
97             const char *file,
98             int line,
99             const char *fmt,
100             ...)
101 {
102   struct rs_error *err = NULL;
103
104   va_list args;
105   va_start (args, fmt);
106   err = _err_vcreate (code, file, line, fmt, args);
107   va_end (args);
108
109   return err;
110 }
111
112 static int
113 _ctx_err_vpush_fl (struct rs_context *ctx, int code, const char *file,
114                    int line, const char *fmt, va_list args)
115 {
116   struct rs_error *err = _err_vcreate (code, file, line, fmt, args);
117
118   if (!err)
119     return RSE_NOMEM;
120
121   /* TODO: Implement a stack.  */
122   if (ctx->err)
123     rs_err_free (ctx->err);
124   ctx->err = err;
125
126   return err->code;
127 }
128
129 int
130 rs_err_ctx_push (struct rs_context *ctx, int code, const char *fmt, ...)
131 {
132   int r = 0;
133   va_list args;
134
135   va_start (args, fmt);
136   r = _ctx_err_vpush_fl (ctx, code, NULL, 0, fmt, args);
137   va_end (args);
138
139   return r;
140 }
141
142 int
143 rs_err_ctx_push_fl (struct rs_context *ctx, int code, const char *file,
144                     int line, const char *fmt, ...)
145 {
146   int r = 0;
147   va_list args;
148
149   va_start (args, fmt);
150   r = _ctx_err_vpush_fl (ctx, code, file, line, fmt, args);
151   va_end (args);
152
153   return r;
154 }
155
156 int
157 err_conn_push_err (struct rs_connection *conn, struct rs_error *err)
158 {
159
160   if (conn->err)
161     rs_err_free (conn->err);
162   conn->err = err;              /* FIXME: use a stack */
163
164   return err->code;
165 }
166
167 static int
168 _conn_err_vpush_fl (struct rs_connection *conn, int code, const char *file,
169                     int line, const char *fmt, va_list args)
170 {
171   struct rs_error *err = _err_vcreate (code, file, line, fmt, args);
172
173   if (!err)
174     return RSE_NOMEM;
175
176   return err_conn_push_err (conn, err);
177 }
178
179 int
180 rs_err_conn_push (struct rs_connection *conn, int code, const char *fmt, ...)
181 {
182   int r = 0;
183
184   va_list args;
185   va_start (args, fmt);
186   r = _conn_err_vpush_fl (conn, code, NULL, 0, fmt, args);
187   va_end (args);
188
189   return r;
190 }
191
192 int
193 rs_err_conn_push_fl (struct rs_connection *conn, int code, const char *file,
194                      int line, const char *fmt, ...)
195 {
196   int r = 0;
197
198   va_list args;
199   va_start (args, fmt);
200   r = _conn_err_vpush_fl (conn, code, file, line, fmt, args);
201   va_end (args);
202
203   return r;
204 }
205
206 struct rs_error *
207 rs_err_ctx_pop (struct rs_context *ctx)
208 {
209   struct rs_error *err;
210
211   if (!ctx)
212     return NULL;                /* FIXME: RSE_INVALID_CTX.  */
213   err = ctx->err;
214   ctx->err = NULL;
215
216   return err;
217 }
218
219 struct rs_error *
220 rs_err_conn_pop (struct rs_connection *conn)
221 {
222   struct rs_error *err;
223
224   if (!conn)
225     return NULL;                /* FIXME: RSE_INVALID_CONN */
226   err = conn->err;
227   conn->err = NULL;
228
229   return err;
230 }
231
232 int
233 rs_err_conn_peek_code (struct rs_connection *conn)
234 {
235   if (!conn)
236     return -1;                  /* FIXME: RSE_INVALID_CONN */
237   if (conn->err)
238     return conn->err->code;
239
240   return RSE_OK;
241 }
242
243 void
244 rs_err_free (struct rs_error *err)
245 {
246   assert (err);
247   free (err);
248 }
249
250 char *
251 rs_err_msg (struct rs_error *err)
252 {
253   if (!err)
254     return NULL;
255
256   return err->buf;
257 }
258
259 int
260 rs_err_code (struct rs_error *err, int dofree_flag)
261 {
262   int code;
263
264   if (!err)
265     return -1;
266   code = err->code;
267
268   if (dofree_flag)
269     rs_err_free (err);
270
271   return code;
272 }