blob: b838f0690464f0e84bf5bcbc2c36eb2c0f9c87aa [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 Prijonoa0c8b5c2007-05-12 15:03:23 +0000114typedef pj_str_t (*pjsip_error_callback)(pj_status_t, char*, pj_size_t);
Benny Prijonodcc29522006-02-02 19:15:03 +0000115/**
116 * Register strerror message handler for the specified error space.
117 * Application can register its own handler to supply the error message
118 * for the specified error code range. This handler will be called
119 * by #pj_strerror().
120 *
121 * @param start_code The starting error code where the handler should
122 * be called to retrieve the error message.
123 * @param err_space The size of error space. The error code range then
124 * will fall in start_code to start_code+err_space-1
125 * range.
126 * @param f The handler to be called when #pj_strerror() is
127 * supplied with error code that falls into this range.
128 *
129 * @return PJ_SUCCESS or the specified error code. The
130 * registration may fail when the error space has been
131 * occupied by other handler, or when there are too many
132 * handlers registered to PJLIB.
133 */
134PJ_DECL(pj_status_t) pj_register_strerror(pj_status_t start_code,
135 pj_status_t err_space,
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000136 pjsip_error_callback f);
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 */
Benny Prijono5d542642007-05-02 18:54:19 +0000170#if PJ_NATIVE_ERR_POSITIVE
171# define PJ_STATUS_FROM_OS(e) (e == 0 ? PJ_SUCCESS : e + PJ_ERRNO_START_SYS)
172#else
173# define PJ_STATUS_FROM_OS(e) (e == 0 ? PJ_SUCCESS : PJ_ERRNO_START_SYS - e)
174#endif
Benny Prijono9033e312005-11-21 02:08:39 +0000175
176/**
177 * @hideinitializer
178 * Fold an pj_status_t code back to the native platform defined error.
179 *
180 * @param e The pj_status_t folded platform os error code.
181 * @return pj_os_err_type
182 * @warning macro implementation; the statcode argument may be evaluated
183 * multiple times. If the statcode was not created by
184 * pj_get_os_error or PJ_STATUS_FROM_OS, the results are undefined.
185 */
Benny Prijono5d542642007-05-02 18:54:19 +0000186#if PJ_NATIVE_ERR_POSITIVE
187# define PJ_STATUS_TO_OS(e) (e == 0 ? PJ_SUCCESS : e - PJ_ERRNO_START_SYS)
188#else
189# define PJ_STATUS_TO_OS(e) (e == 0 ? PJ_SUCCESS : PJ_ERRNO_START_SYS - e)
190#endif
Benny Prijono9033e312005-11-21 02:08:39 +0000191
192
193/**
194 * @defgroup pj_errnum PJLIB's Own Error Codes
195 * @ingroup pj_errno
196 * @{
197 */
198
199/**
Benny Prijono7eaa0fd2006-06-22 18:30:13 +0000200 * Use this macro to generate error message text for your error code,
201 * so that they look uniformly as the rest of the libraries.
202 *
203 * @param code The error code
204 * @param msg The error test.
205 */
206#ifndef PJ_BUILD_ERR
207# define PJ_BUILD_ERR(code,msg) { code, msg " (" #code ")" }
208#endif
209
210
211/**
Benny Prijono9033e312005-11-21 02:08:39 +0000212 * @hideinitializer
213 * Unknown error has been reported.
214 */
215#define PJ_EUNKNOWN (PJ_ERRNO_START_STATUS + 1) /* 70001 */
216/**
217 * @hideinitializer
218 * The operation is pending and will be completed later.
219 */
220#define PJ_EPENDING (PJ_ERRNO_START_STATUS + 2) /* 70002 */
221/**
222 * @hideinitializer
223 * Too many connecting sockets.
224 */
225#define PJ_ETOOMANYCONN (PJ_ERRNO_START_STATUS + 3) /* 70003 */
226/**
227 * @hideinitializer
228 * Invalid argument.
229 */
230#define PJ_EINVAL (PJ_ERRNO_START_STATUS + 4) /* 70004 */
231/**
232 * @hideinitializer
233 * Name too long (eg. hostname too long).
234 */
235#define PJ_ENAMETOOLONG (PJ_ERRNO_START_STATUS + 5) /* 70005 */
236/**
237 * @hideinitializer
238 * Not found.
239 */
240#define PJ_ENOTFOUND (PJ_ERRNO_START_STATUS + 6) /* 70006 */
241/**
242 * @hideinitializer
243 * Not enough memory.
244 */
245#define PJ_ENOMEM (PJ_ERRNO_START_STATUS + 7) /* 70007 */
246/**
247 * @hideinitializer
248 * Bug detected!
249 */
250#define PJ_EBUG (PJ_ERRNO_START_STATUS + 8) /* 70008 */
251/**
252 * @hideinitializer
253 * Operation timed out.
254 */
255#define PJ_ETIMEDOUT (PJ_ERRNO_START_STATUS + 9) /* 70009 */
256/**
257 * @hideinitializer
258 * Too many objects.
259 */
260#define PJ_ETOOMANY (PJ_ERRNO_START_STATUS + 10)/* 70010 */
261/**
262 * @hideinitializer
263 * Object is busy.
264 */
265#define PJ_EBUSY (PJ_ERRNO_START_STATUS + 11)/* 70011 */
266/**
267 * @hideinitializer
268 * The specified option is not supported.
269 */
270#define PJ_ENOTSUP (PJ_ERRNO_START_STATUS + 12)/* 70012 */
271/**
272 * @hideinitializer
273 * Invalid operation.
274 */
275#define PJ_EINVALIDOP (PJ_ERRNO_START_STATUS + 13)/* 70013 */
276/**
277 * @hideinitializer
278 * Operation is cancelled.
279 */
280#define PJ_ECANCELLED (PJ_ERRNO_START_STATUS + 14)/* 70014 */
281/**
282 * @hideinitializer
283 * Object already exists.
284 */
285#define PJ_EEXISTS (PJ_ERRNO_START_STATUS + 15)/* 70015 */
Benny Prijono6aa5c2a2006-03-05 13:37:41 +0000286/**
287 * @hideinitializer
288 * End of file.
289 */
290#define PJ_EEOF (PJ_ERRNO_START_STATUS + 16)/* 70016 */
Benny Prijono7eaa0fd2006-06-22 18:30:13 +0000291/**
292 * @hideinitializer
293 * Size is too big.
294 */
295#define PJ_ETOOBIG (PJ_ERRNO_START_STATUS + 17)/* 70017 */
Benny Prijonocca32812006-09-14 21:16:11 +0000296/**
297 * @hideinitializer
Benny Prijonod424f5b2006-09-30 11:39:17 +0000298 * Error in gethostbyname(). This is a generic error returned when
299 * gethostbyname() has returned an error.
Benny Prijonocca32812006-09-14 21:16:11 +0000300 */
301#define PJ_ERESOLVE (PJ_ERRNO_START_STATUS + 18)/* 70018 */
Benny Prijonod424f5b2006-09-30 11:39:17 +0000302/**
303 * @hideinitializer
304 * Size is too small.
305 */
306#define PJ_ETOOSMALL (PJ_ERRNO_START_STATUS + 19)/* 70019 */
Benny Prijono1f7767b2007-10-03 18:28:49 +0000307/**
308 * @hideinitializer
309 * Ignored
310 */
311#define PJ_EIGNORED (PJ_ERRNO_START_STATUS + 20)/* 70020 */
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000312/**
313 * @hideinitializer
314 * IPv6 is not supported
315 */
316#define PJ_EIPV6NOTSUP (PJ_ERRNO_START_STATUS + 21)/* 70021 */
Benny Prijono62b86eb2007-12-01 08:52:57 +0000317/**
318 * @hideinitializer
319 * Unsupported address family
320 */
321#define PJ_EAFNOTSUP (PJ_ERRNO_START_STATUS + 22)/* 70022 */
Benny Prijono9033e312005-11-21 02:08:39 +0000322
323/** @} */ /* pj_errnum */
324
325/** @} */ /* pj_errno */
326
327
328/**
329 * PJ_ERRNO_START is where PJLIB specific error values start.
330 */
331#define PJ_ERRNO_START 20000
332
333/**
334 * PJ_ERRNO_SPACE_SIZE is the maximum number of errors in one of
335 * the error/status range below.
336 */
337#define PJ_ERRNO_SPACE_SIZE 50000
338
339/**
340 * PJ_ERRNO_START_STATUS is where PJLIB specific status codes start.
341 * Effectively the error in this class would be 70000 - 119000.
342 */
343#define PJ_ERRNO_START_STATUS (PJ_ERRNO_START + PJ_ERRNO_SPACE_SIZE)
344
345/**
346 * PJ_ERRNO_START_SYS converts platform specific error codes into
347 * pj_status_t values.
348 * Effectively the error in this class would be 120000 - 169000.
349 */
350#define PJ_ERRNO_START_SYS (PJ_ERRNO_START_STATUS + PJ_ERRNO_SPACE_SIZE)
351
352/**
353 * PJ_ERRNO_START_USER are reserved for applications that use error
354 * codes along with PJLIB codes.
355 * Effectively the error in this class would be 170000 - 219000.
356 */
357#define PJ_ERRNO_START_USER (PJ_ERRNO_START_SYS + PJ_ERRNO_SPACE_SIZE)
358
359
Benny Prijonoc5784c12006-02-21 23:40:16 +0000360/*
361 * Below are list of error spaces that have been taken so far:
362 * - PJSIP_ERRNO_START (PJ_ERRNO_START_USER)
363 * - PJMEDIA_ERRNO_START (PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE)
364 * - PJSIP_SIMPLE_ERRNO_START (PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE*2)
Benny Prijonob2343c72006-02-22 22:12:16 +0000365 * - PJLIB_UTIL_ERRNO_START (PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE*3)
Benny Prijonob01897b2007-03-18 17:39:27 +0000366 * - PJNATH_ERRNO_START (PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE*4)
Benny Prijonoc5784c12006-02-21 23:40:16 +0000367 */
368
Benny Prijonof762ee72006-12-01 11:14:37 +0000369/* Internal */
370void pj_errno_clear_handlers(void);
371
Benny Prijono8ab968f2007-07-20 08:08:30 +0000372PJ_END_DECL
373
Benny Prijono9033e312005-11-21 02:08:39 +0000374#endif /* __PJ_ERRNO_H__ */
375