blob: 76c23f5c55c45961754e39db9d1bad6ad24a95fe [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C)2003-2008 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
Benny Prijono9033e312005-11-21 02:08:39 +000033 * @{
34 *
35 * In PJLIB, error/status codes from operating system are translated
36 * into PJLIB error namespace, and stored in @a pj_status_t. All functions
37 * that work with @a pj_status_t expect to get PJLIB error code instead
38 * of native codes.
39 *
40 * @section pj_errno_retval Return Values
41 *
42 * All functions that returns @a pj_status_t returns @a PJ_SUCCESS if the
43 * operation was completed successfully, or non-zero value to indicate
44 * error. If the error came from operating system, then the native error
45 * code is translated/folded into PJLIB's error namespace by using
46 * #PJ_STATUS_FROM_OS() macro. The function will do this automatically
47 * before returning the error to caller.
48 *
49 * @section pj_errno_errmsg Error Message
50 *
51 * To get the error message corresponding to a particular code, use function
52 * #pj_strerror(). This function expects error code in PJLIB error namespace,
53 * not the native error code. Application can pass the value from the
54 * following sources to this function:
55 * - #pj_get_os_error()
56 * - #pj_get_netos_error()
57 * - any return value from function returning @a pj_status_t.
58 *
59 * Application MUST NOT pass native error code (such as error code from
60 * functions like GetLastError() or errno) to PJLIB functions expecting
61 * @a pj_status_t.
62 *
63 */
64
65/**
Benny Prijonod47401e2006-02-07 12:32:59 +000066 * Guidelines on error message length.
67 */
Benny Prijono7eaa0fd2006-06-22 18:30:13 +000068#define PJ_ERR_MSG_SIZE 80
Benny Prijonod47401e2006-02-07 12:32:59 +000069
70/**
Benny Prijono9033e312005-11-21 02:08:39 +000071 * Get the last platform error/status, folded into pj_status_t.
72 * @return OS dependent error code, folded into pj_status_t.
73 * @remark This function gets errno, or calls GetLastError() function and
74 * convert the code into pj_status_t with PJ_STATUS_FROM_OS. Do
75 * not call this for socket functions!
76 * @see pj_get_netos_error()
77 */
78PJ_DECL(pj_status_t) pj_get_os_error(void);
79
80/**
81 * Set last error.
82 * @param code pj_status_t
83 */
84PJ_DECL(void) pj_set_os_error(pj_status_t code);
85
86/**
87 * Get the last error from socket operations.
88 * @return Last socket error, folded into pj_status_t.
89 */
90PJ_DECL(pj_status_t) pj_get_netos_error(void);
91
92/**
93 * Set error code.
94 * @param code pj_status_t.
95 */
96PJ_DECL(void) pj_set_netos_error(pj_status_t code);
97
98
99/**
100 * Get the error message for the specified error code. The message
101 * string will be NULL terminated.
102 *
103 * @param statcode The error code.
104 * @param buf Buffer to hold the error message string.
105 * @param bufsize Size of the buffer.
106 *
107 * @return The error message as NULL terminated string,
108 * wrapped with pj_str_t.
109 */
110PJ_DECL(pj_str_t) pj_strerror( pj_status_t statcode,
111 char *buf, pj_size_t bufsize);
112
Benny Prijonoa7b376b2008-01-25 16:06:33 +0000113/**
114 * Type of callback to be specified in #pj_register_strerror()
115 *
116 * @param e The error code to lookup.
117 * @param msg Buffer to store the error message.
118 * @param max Length of the buffer.
119 *
120 * @return The error string.
121 */
122typedef pj_str_t (*pj_error_callback)(pj_status_t e, char *msg, pj_size_t max);
123
124
Benny Prijonodcc29522006-02-02 19:15:03 +0000125/**
126 * Register strerror message handler for the specified error space.
127 * Application can register its own handler to supply the error message
128 * for the specified error code range. This handler will be called
129 * by #pj_strerror().
130 *
131 * @param start_code The starting error code where the handler should
132 * be called to retrieve the error message.
133 * @param err_space The size of error space. The error code range then
134 * will fall in start_code to start_code+err_space-1
135 * range.
136 * @param f The handler to be called when #pj_strerror() is
137 * supplied with error code that falls into this range.
138 *
139 * @return PJ_SUCCESS or the specified error code. The
140 * registration may fail when the error space has been
141 * occupied by other handler, or when there are too many
142 * handlers registered to PJLIB.
143 */
144PJ_DECL(pj_status_t) pj_register_strerror(pj_status_t start_code,
145 pj_status_t err_space,
Benny Prijonoa7b376b2008-01-25 16:06:33 +0000146 pj_error_callback f);
Benny Prijono9033e312005-11-21 02:08:39 +0000147
148/**
149 * @hideinitializer
150 * Return platform os error code folded into pj_status_t code. This is
151 * the macro that is used throughout the library for all PJLIB's functions
152 * that returns error from operating system. Application may override
153 * this macro to reduce size (e.g. by defining it to always return
154 * #PJ_EUNKNOWN).
155 *
156 * Note:
157 * This macro MUST return non-zero value regardless whether zero is
158 * passed as the argument. The reason is to protect logic error when
159 * the operating system doesn't report error codes properly.
160 *
161 * @param os_code Platform OS error code. This value may be evaluated
162 * more than once.
163 * @return The platform os error code folded into pj_status_t.
164 */
165#ifndef PJ_RETURN_OS_ERROR
166# define PJ_RETURN_OS_ERROR(os_code) (os_code ? \
167 PJ_STATUS_FROM_OS(os_code) : -1)
168#endif
169
170
171/**
172 * @hideinitializer
173 * Fold a platform specific error into an pj_status_t code.
174 *
175 * @param e The platform os error code.
176 * @return pj_status_t
177 * @warning Macro implementation; the syserr argument may be evaluated
178 * multiple times.
179 */
Benny Prijono5d542642007-05-02 18:54:19 +0000180#if PJ_NATIVE_ERR_POSITIVE
181# define PJ_STATUS_FROM_OS(e) (e == 0 ? PJ_SUCCESS : e + PJ_ERRNO_START_SYS)
182#else
183# define PJ_STATUS_FROM_OS(e) (e == 0 ? PJ_SUCCESS : PJ_ERRNO_START_SYS - e)
184#endif
Benny Prijono9033e312005-11-21 02:08:39 +0000185
186/**
187 * @hideinitializer
188 * Fold an pj_status_t code back to the native platform defined error.
189 *
190 * @param e The pj_status_t folded platform os error code.
191 * @return pj_os_err_type
192 * @warning macro implementation; the statcode argument may be evaluated
193 * multiple times. If the statcode was not created by
194 * pj_get_os_error or PJ_STATUS_FROM_OS, the results are undefined.
195 */
Benny Prijono5d542642007-05-02 18:54:19 +0000196#if PJ_NATIVE_ERR_POSITIVE
197# define PJ_STATUS_TO_OS(e) (e == 0 ? PJ_SUCCESS : e - PJ_ERRNO_START_SYS)
198#else
199# define PJ_STATUS_TO_OS(e) (e == 0 ? PJ_SUCCESS : PJ_ERRNO_START_SYS - e)
200#endif
Benny Prijono9033e312005-11-21 02:08:39 +0000201
202
203/**
204 * @defgroup pj_errnum PJLIB's Own Error Codes
205 * @ingroup pj_errno
206 * @{
207 */
208
209/**
Benny Prijono7eaa0fd2006-06-22 18:30:13 +0000210 * Use this macro to generate error message text for your error code,
211 * so that they look uniformly as the rest of the libraries.
212 *
213 * @param code The error code
214 * @param msg The error test.
215 */
216#ifndef PJ_BUILD_ERR
217# define PJ_BUILD_ERR(code,msg) { code, msg " (" #code ")" }
218#endif
219
220
221/**
Benny Prijono9033e312005-11-21 02:08:39 +0000222 * @hideinitializer
223 * Unknown error has been reported.
224 */
225#define PJ_EUNKNOWN (PJ_ERRNO_START_STATUS + 1) /* 70001 */
226/**
227 * @hideinitializer
228 * The operation is pending and will be completed later.
229 */
230#define PJ_EPENDING (PJ_ERRNO_START_STATUS + 2) /* 70002 */
231/**
232 * @hideinitializer
233 * Too many connecting sockets.
234 */
235#define PJ_ETOOMANYCONN (PJ_ERRNO_START_STATUS + 3) /* 70003 */
236/**
237 * @hideinitializer
238 * Invalid argument.
239 */
240#define PJ_EINVAL (PJ_ERRNO_START_STATUS + 4) /* 70004 */
241/**
242 * @hideinitializer
243 * Name too long (eg. hostname too long).
244 */
245#define PJ_ENAMETOOLONG (PJ_ERRNO_START_STATUS + 5) /* 70005 */
246/**
247 * @hideinitializer
248 * Not found.
249 */
250#define PJ_ENOTFOUND (PJ_ERRNO_START_STATUS + 6) /* 70006 */
251/**
252 * @hideinitializer
253 * Not enough memory.
254 */
255#define PJ_ENOMEM (PJ_ERRNO_START_STATUS + 7) /* 70007 */
256/**
257 * @hideinitializer
258 * Bug detected!
259 */
260#define PJ_EBUG (PJ_ERRNO_START_STATUS + 8) /* 70008 */
261/**
262 * @hideinitializer
263 * Operation timed out.
264 */
265#define PJ_ETIMEDOUT (PJ_ERRNO_START_STATUS + 9) /* 70009 */
266/**
267 * @hideinitializer
268 * Too many objects.
269 */
270#define PJ_ETOOMANY (PJ_ERRNO_START_STATUS + 10)/* 70010 */
271/**
272 * @hideinitializer
273 * Object is busy.
274 */
275#define PJ_EBUSY (PJ_ERRNO_START_STATUS + 11)/* 70011 */
276/**
277 * @hideinitializer
278 * The specified option is not supported.
279 */
280#define PJ_ENOTSUP (PJ_ERRNO_START_STATUS + 12)/* 70012 */
281/**
282 * @hideinitializer
283 * Invalid operation.
284 */
285#define PJ_EINVALIDOP (PJ_ERRNO_START_STATUS + 13)/* 70013 */
286/**
287 * @hideinitializer
288 * Operation is cancelled.
289 */
290#define PJ_ECANCELLED (PJ_ERRNO_START_STATUS + 14)/* 70014 */
291/**
292 * @hideinitializer
293 * Object already exists.
294 */
295#define PJ_EEXISTS (PJ_ERRNO_START_STATUS + 15)/* 70015 */
Benny Prijono6aa5c2a2006-03-05 13:37:41 +0000296/**
297 * @hideinitializer
298 * End of file.
299 */
300#define PJ_EEOF (PJ_ERRNO_START_STATUS + 16)/* 70016 */
Benny Prijono7eaa0fd2006-06-22 18:30:13 +0000301/**
302 * @hideinitializer
303 * Size is too big.
304 */
305#define PJ_ETOOBIG (PJ_ERRNO_START_STATUS + 17)/* 70017 */
Benny Prijonocca32812006-09-14 21:16:11 +0000306/**
307 * @hideinitializer
Benny Prijonod424f5b2006-09-30 11:39:17 +0000308 * Error in gethostbyname(). This is a generic error returned when
309 * gethostbyname() has returned an error.
Benny Prijonocca32812006-09-14 21:16:11 +0000310 */
311#define PJ_ERESOLVE (PJ_ERRNO_START_STATUS + 18)/* 70018 */
Benny Prijonod424f5b2006-09-30 11:39:17 +0000312/**
313 * @hideinitializer
314 * Size is too small.
315 */
316#define PJ_ETOOSMALL (PJ_ERRNO_START_STATUS + 19)/* 70019 */
Benny Prijono1f7767b2007-10-03 18:28:49 +0000317/**
318 * @hideinitializer
319 * Ignored
320 */
321#define PJ_EIGNORED (PJ_ERRNO_START_STATUS + 20)/* 70020 */
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000322/**
323 * @hideinitializer
324 * IPv6 is not supported
325 */
326#define PJ_EIPV6NOTSUP (PJ_ERRNO_START_STATUS + 21)/* 70021 */
Benny Prijono62b86eb2007-12-01 08:52:57 +0000327/**
328 * @hideinitializer
329 * Unsupported address family
330 */
331#define PJ_EAFNOTSUP (PJ_ERRNO_START_STATUS + 22)/* 70022 */
Benny Prijono9033e312005-11-21 02:08:39 +0000332
333/** @} */ /* pj_errnum */
334
335/** @} */ /* pj_errno */
336
337
338/**
339 * PJ_ERRNO_START is where PJLIB specific error values start.
340 */
341#define PJ_ERRNO_START 20000
342
343/**
344 * PJ_ERRNO_SPACE_SIZE is the maximum number of errors in one of
345 * the error/status range below.
346 */
347#define PJ_ERRNO_SPACE_SIZE 50000
348
349/**
350 * PJ_ERRNO_START_STATUS is where PJLIB specific status codes start.
351 * Effectively the error in this class would be 70000 - 119000.
352 */
353#define PJ_ERRNO_START_STATUS (PJ_ERRNO_START + PJ_ERRNO_SPACE_SIZE)
354
355/**
356 * PJ_ERRNO_START_SYS converts platform specific error codes into
357 * pj_status_t values.
358 * Effectively the error in this class would be 120000 - 169000.
359 */
360#define PJ_ERRNO_START_SYS (PJ_ERRNO_START_STATUS + PJ_ERRNO_SPACE_SIZE)
361
362/**
363 * PJ_ERRNO_START_USER are reserved for applications that use error
364 * codes along with PJLIB codes.
365 * Effectively the error in this class would be 170000 - 219000.
366 */
367#define PJ_ERRNO_START_USER (PJ_ERRNO_START_SYS + PJ_ERRNO_SPACE_SIZE)
368
369
Benny Prijonoc5784c12006-02-21 23:40:16 +0000370/*
371 * Below are list of error spaces that have been taken so far:
372 * - PJSIP_ERRNO_START (PJ_ERRNO_START_USER)
373 * - PJMEDIA_ERRNO_START (PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE)
374 * - PJSIP_SIMPLE_ERRNO_START (PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE*2)
Benny Prijonob2343c72006-02-22 22:12:16 +0000375 * - PJLIB_UTIL_ERRNO_START (PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE*3)
Benny Prijonob01897b2007-03-18 17:39:27 +0000376 * - PJNATH_ERRNO_START (PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE*4)
Benny Prijonoc5784c12006-02-21 23:40:16 +0000377 */
378
Benny Prijonof762ee72006-12-01 11:14:37 +0000379/* Internal */
380void pj_errno_clear_handlers(void);
381
Benny Prijono8ab968f2007-07-20 08:08:30 +0000382PJ_END_DECL
383
Benny Prijono9033e312005-11-21 02:08:39 +0000384#endif /* __PJ_ERRNO_H__ */
385