blob: 6ff304d611511e621d17e1ebe0c845cc54d1307a [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono9033e312005-11-21 02:08:39 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#ifndef __PJ_ERRNO_H__
20#define __PJ_ERRNO_H__
21
22/**
23 * @file errno.h
24 * @brief PJLIB Error Codes
25 */
26#include <pj/types.h>
27#include <pj/compat/errno.h>
28
29PJ_BEGIN_DECL
30
31/**
32 * @defgroup pj_errno Error Codes
33 * @ingroup PJ
34 * @{
35 *
36 * In PJLIB, error/status codes from operating system are translated
37 * into PJLIB error namespace, and stored in @a pj_status_t. All functions
38 * that work with @a pj_status_t expect to get PJLIB error code instead
39 * of native codes.
40 *
41 * @section pj_errno_retval Return Values
42 *
43 * All functions that returns @a pj_status_t returns @a PJ_SUCCESS if the
44 * operation was completed successfully, or non-zero value to indicate
45 * error. If the error came from operating system, then the native error
46 * code is translated/folded into PJLIB's error namespace by using
47 * #PJ_STATUS_FROM_OS() macro. The function will do this automatically
48 * before returning the error to caller.
49 *
50 * @section pj_errno_errmsg Error Message
51 *
52 * To get the error message corresponding to a particular code, use function
53 * #pj_strerror(). This function expects error code in PJLIB error namespace,
54 * not the native error code. Application can pass the value from the
55 * following sources to this function:
56 * - #pj_get_os_error()
57 * - #pj_get_netos_error()
58 * - any return value from function returning @a pj_status_t.
59 *
60 * Application MUST NOT pass native error code (such as error code from
61 * functions like GetLastError() or errno) to PJLIB functions expecting
62 * @a pj_status_t.
63 *
64 */
65
66/**
Benny Prijonod47401e2006-02-07 12:32:59 +000067 * Guidelines on error message length.
68 */
Benny Prijono7eaa0fd2006-06-22 18:30:13 +000069#define PJ_ERR_MSG_SIZE 80
Benny Prijonod47401e2006-02-07 12:32:59 +000070
71/**
Benny Prijono9033e312005-11-21 02:08:39 +000072 * Get the last platform error/status, folded into pj_status_t.
73 * @return OS dependent error code, folded into pj_status_t.
74 * @remark This function gets errno, or calls GetLastError() function and
75 * convert the code into pj_status_t with PJ_STATUS_FROM_OS. Do
76 * not call this for socket functions!
77 * @see pj_get_netos_error()
78 */
79PJ_DECL(pj_status_t) pj_get_os_error(void);
80
81/**
82 * Set last error.
83 * @param code pj_status_t
84 */
85PJ_DECL(void) pj_set_os_error(pj_status_t code);
86
87/**
88 * Get the last error from socket operations.
89 * @return Last socket error, folded into pj_status_t.
90 */
91PJ_DECL(pj_status_t) pj_get_netos_error(void);
92
93/**
94 * Set error code.
95 * @param code pj_status_t.
96 */
97PJ_DECL(void) pj_set_netos_error(pj_status_t code);
98
99
100/**
101 * Get the error message for the specified error code. The message
102 * string will be NULL terminated.
103 *
104 * @param statcode The error code.
105 * @param buf Buffer to hold the error message string.
106 * @param bufsize Size of the buffer.
107 *
108 * @return The error message as NULL terminated string,
109 * wrapped with pj_str_t.
110 */
111PJ_DECL(pj_str_t) pj_strerror( pj_status_t statcode,
112 char *buf, pj_size_t bufsize);
113
Benny Prijonodcc29522006-02-02 19:15:03 +0000114/**
115 * Register strerror message handler for the specified error space.
116 * Application can register its own handler to supply the error message
117 * for the specified error code range. This handler will be called
118 * by #pj_strerror().
119 *
120 * @param start_code The starting error code where the handler should
121 * be called to retrieve the error message.
122 * @param err_space The size of error space. The error code range then
123 * will fall in start_code to start_code+err_space-1
124 * range.
125 * @param f The handler to be called when #pj_strerror() is
126 * supplied with error code that falls into this range.
127 *
128 * @return PJ_SUCCESS or the specified error code. The
129 * registration may fail when the error space has been
130 * occupied by other handler, or when there are too many
131 * handlers registered to PJLIB.
132 */
133PJ_DECL(pj_status_t) pj_register_strerror(pj_status_t start_code,
134 pj_status_t err_space,
135 pj_str_t (*f)(pj_status_t,char*,
136 pj_size_t));
Benny Prijono9033e312005-11-21 02:08:39 +0000137
138/**
139 * @hideinitializer
140 * Return platform os error code folded into pj_status_t code. This is
141 * the macro that is used throughout the library for all PJLIB's functions
142 * that returns error from operating system. Application may override
143 * this macro to reduce size (e.g. by defining it to always return
144 * #PJ_EUNKNOWN).
145 *
146 * Note:
147 * This macro MUST return non-zero value regardless whether zero is
148 * passed as the argument. The reason is to protect logic error when
149 * the operating system doesn't report error codes properly.
150 *
151 * @param os_code Platform OS error code. This value may be evaluated
152 * more than once.
153 * @return The platform os error code folded into pj_status_t.
154 */
155#ifndef PJ_RETURN_OS_ERROR
156# define PJ_RETURN_OS_ERROR(os_code) (os_code ? \
157 PJ_STATUS_FROM_OS(os_code) : -1)
158#endif
159
160
161/**
162 * @hideinitializer
163 * Fold a platform specific error into an pj_status_t code.
164 *
165 * @param e The platform os error code.
166 * @return pj_status_t
167 * @warning Macro implementation; the syserr argument may be evaluated
168 * multiple times.
169 */
170#define PJ_STATUS_FROM_OS(e) (e == 0 ? PJ_SUCCESS : e + PJ_ERRNO_START_SYS)
171
172/**
173 * @hideinitializer
174 * Fold an pj_status_t code back to the native platform defined error.
175 *
176 * @param e The pj_status_t folded platform os error code.
177 * @return pj_os_err_type
178 * @warning macro implementation; the statcode argument may be evaluated
179 * multiple times. If the statcode was not created by
180 * pj_get_os_error or PJ_STATUS_FROM_OS, the results are undefined.
181 */
182#define PJ_STATUS_TO_OS(e) (e == 0 ? PJ_SUCCESS : e - PJ_ERRNO_START_SYS)
183
184
185/**
186 * @defgroup pj_errnum PJLIB's Own Error Codes
187 * @ingroup pj_errno
188 * @{
189 */
190
191/**
Benny Prijono7eaa0fd2006-06-22 18:30:13 +0000192 * Use this macro to generate error message text for your error code,
193 * so that they look uniformly as the rest of the libraries.
194 *
195 * @param code The error code
196 * @param msg The error test.
197 */
198#ifndef PJ_BUILD_ERR
199# define PJ_BUILD_ERR(code,msg) { code, msg " (" #code ")" }
200#endif
201
202
203/**
Benny Prijono9033e312005-11-21 02:08:39 +0000204 * @hideinitializer
205 * Unknown error has been reported.
206 */
207#define PJ_EUNKNOWN (PJ_ERRNO_START_STATUS + 1) /* 70001 */
208/**
209 * @hideinitializer
210 * The operation is pending and will be completed later.
211 */
212#define PJ_EPENDING (PJ_ERRNO_START_STATUS + 2) /* 70002 */
213/**
214 * @hideinitializer
215 * Too many connecting sockets.
216 */
217#define PJ_ETOOMANYCONN (PJ_ERRNO_START_STATUS + 3) /* 70003 */
218/**
219 * @hideinitializer
220 * Invalid argument.
221 */
222#define PJ_EINVAL (PJ_ERRNO_START_STATUS + 4) /* 70004 */
223/**
224 * @hideinitializer
225 * Name too long (eg. hostname too long).
226 */
227#define PJ_ENAMETOOLONG (PJ_ERRNO_START_STATUS + 5) /* 70005 */
228/**
229 * @hideinitializer
230 * Not found.
231 */
232#define PJ_ENOTFOUND (PJ_ERRNO_START_STATUS + 6) /* 70006 */
233/**
234 * @hideinitializer
235 * Not enough memory.
236 */
237#define PJ_ENOMEM (PJ_ERRNO_START_STATUS + 7) /* 70007 */
238/**
239 * @hideinitializer
240 * Bug detected!
241 */
242#define PJ_EBUG (PJ_ERRNO_START_STATUS + 8) /* 70008 */
243/**
244 * @hideinitializer
245 * Operation timed out.
246 */
247#define PJ_ETIMEDOUT (PJ_ERRNO_START_STATUS + 9) /* 70009 */
248/**
249 * @hideinitializer
250 * Too many objects.
251 */
252#define PJ_ETOOMANY (PJ_ERRNO_START_STATUS + 10)/* 70010 */
253/**
254 * @hideinitializer
255 * Object is busy.
256 */
257#define PJ_EBUSY (PJ_ERRNO_START_STATUS + 11)/* 70011 */
258/**
259 * @hideinitializer
260 * The specified option is not supported.
261 */
262#define PJ_ENOTSUP (PJ_ERRNO_START_STATUS + 12)/* 70012 */
263/**
264 * @hideinitializer
265 * Invalid operation.
266 */
267#define PJ_EINVALIDOP (PJ_ERRNO_START_STATUS + 13)/* 70013 */
268/**
269 * @hideinitializer
270 * Operation is cancelled.
271 */
272#define PJ_ECANCELLED (PJ_ERRNO_START_STATUS + 14)/* 70014 */
273/**
274 * @hideinitializer
275 * Object already exists.
276 */
277#define PJ_EEXISTS (PJ_ERRNO_START_STATUS + 15)/* 70015 */
Benny Prijono6aa5c2a2006-03-05 13:37:41 +0000278/**
279 * @hideinitializer
280 * End of file.
281 */
282#define PJ_EEOF (PJ_ERRNO_START_STATUS + 16)/* 70016 */
Benny Prijono7eaa0fd2006-06-22 18:30:13 +0000283/**
284 * @hideinitializer
285 * Size is too big.
286 */
287#define PJ_ETOOBIG (PJ_ERRNO_START_STATUS + 17)/* 70017 */
Benny Prijonocca32812006-09-14 21:16:11 +0000288/**
289 * @hideinitializer
Benny Prijonod424f5b2006-09-30 11:39:17 +0000290 * Error in gethostbyname(). This is a generic error returned when
291 * gethostbyname() has returned an error.
Benny Prijonocca32812006-09-14 21:16:11 +0000292 */
293#define PJ_ERESOLVE (PJ_ERRNO_START_STATUS + 18)/* 70018 */
Benny Prijonod424f5b2006-09-30 11:39:17 +0000294/**
295 * @hideinitializer
296 * Size is too small.
297 */
298#define PJ_ETOOSMALL (PJ_ERRNO_START_STATUS + 19)/* 70019 */
Benny Prijono7eaa0fd2006-06-22 18:30:13 +0000299
Benny Prijono9033e312005-11-21 02:08:39 +0000300
301/** @} */ /* pj_errnum */
302
303/** @} */ /* pj_errno */
304
305
306/**
307 * PJ_ERRNO_START is where PJLIB specific error values start.
308 */
309#define PJ_ERRNO_START 20000
310
311/**
312 * PJ_ERRNO_SPACE_SIZE is the maximum number of errors in one of
313 * the error/status range below.
314 */
315#define PJ_ERRNO_SPACE_SIZE 50000
316
317/**
318 * PJ_ERRNO_START_STATUS is where PJLIB specific status codes start.
319 * Effectively the error in this class would be 70000 - 119000.
320 */
321#define PJ_ERRNO_START_STATUS (PJ_ERRNO_START + PJ_ERRNO_SPACE_SIZE)
322
323/**
324 * PJ_ERRNO_START_SYS converts platform specific error codes into
325 * pj_status_t values.
326 * Effectively the error in this class would be 120000 - 169000.
327 */
328#define PJ_ERRNO_START_SYS (PJ_ERRNO_START_STATUS + PJ_ERRNO_SPACE_SIZE)
329
330/**
331 * PJ_ERRNO_START_USER are reserved for applications that use error
332 * codes along with PJLIB codes.
333 * Effectively the error in this class would be 170000 - 219000.
334 */
335#define PJ_ERRNO_START_USER (PJ_ERRNO_START_SYS + PJ_ERRNO_SPACE_SIZE)
336
337
Benny Prijonoc5784c12006-02-21 23:40:16 +0000338/*
339 * Below are list of error spaces that have been taken so far:
340 * - PJSIP_ERRNO_START (PJ_ERRNO_START_USER)
341 * - PJMEDIA_ERRNO_START (PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE)
342 * - PJSIP_SIMPLE_ERRNO_START (PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE*2)
Benny Prijonob2343c72006-02-22 22:12:16 +0000343 * - PJLIB_UTIL_ERRNO_START (PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE*3)
Benny Prijonoc5784c12006-02-21 23:40:16 +0000344 */
345
346
Benny Prijono9033e312005-11-21 02:08:39 +0000347PJ_END_DECL
348
Benny Prijonof762ee72006-12-01 11:14:37 +0000349/* Internal */
350void pj_errno_clear_handlers(void);
351
Benny Prijono9033e312005-11-21 02:08:39 +0000352#endif /* __PJ_ERRNO_H__ */
353