blob: ffb72c19617925608942a67464f0b251761888ee [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
4 *
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/**
67 * Get the last platform error/status, folded into pj_status_t.
68 * @return OS dependent error code, folded into pj_status_t.
69 * @remark This function gets errno, or calls GetLastError() function and
70 * convert the code into pj_status_t with PJ_STATUS_FROM_OS. Do
71 * not call this for socket functions!
72 * @see pj_get_netos_error()
73 */
74PJ_DECL(pj_status_t) pj_get_os_error(void);
75
76/**
77 * Set last error.
78 * @param code pj_status_t
79 */
80PJ_DECL(void) pj_set_os_error(pj_status_t code);
81
82/**
83 * Get the last error from socket operations.
84 * @return Last socket error, folded into pj_status_t.
85 */
86PJ_DECL(pj_status_t) pj_get_netos_error(void);
87
88/**
89 * Set error code.
90 * @param code pj_status_t.
91 */
92PJ_DECL(void) pj_set_netos_error(pj_status_t code);
93
94
95/**
96 * Get the error message for the specified error code. The message
97 * string will be NULL terminated.
98 *
99 * @param statcode The error code.
100 * @param buf Buffer to hold the error message string.
101 * @param bufsize Size of the buffer.
102 *
103 * @return The error message as NULL terminated string,
104 * wrapped with pj_str_t.
105 */
106PJ_DECL(pj_str_t) pj_strerror( pj_status_t statcode,
107 char *buf, pj_size_t bufsize);
108
109
110/**
111 * @hideinitializer
112 * Return platform os error code folded into pj_status_t code. This is
113 * the macro that is used throughout the library for all PJLIB's functions
114 * that returns error from operating system. Application may override
115 * this macro to reduce size (e.g. by defining it to always return
116 * #PJ_EUNKNOWN).
117 *
118 * Note:
119 * This macro MUST return non-zero value regardless whether zero is
120 * passed as the argument. The reason is to protect logic error when
121 * the operating system doesn't report error codes properly.
122 *
123 * @param os_code Platform OS error code. This value may be evaluated
124 * more than once.
125 * @return The platform os error code folded into pj_status_t.
126 */
127#ifndef PJ_RETURN_OS_ERROR
128# define PJ_RETURN_OS_ERROR(os_code) (os_code ? \
129 PJ_STATUS_FROM_OS(os_code) : -1)
130#endif
131
132
133/**
134 * @hideinitializer
135 * Fold a platform specific error into an pj_status_t code.
136 *
137 * @param e The platform os error code.
138 * @return pj_status_t
139 * @warning Macro implementation; the syserr argument may be evaluated
140 * multiple times.
141 */
142#define PJ_STATUS_FROM_OS(e) (e == 0 ? PJ_SUCCESS : e + PJ_ERRNO_START_SYS)
143
144/**
145 * @hideinitializer
146 * Fold an pj_status_t code back to the native platform defined error.
147 *
148 * @param e The pj_status_t folded platform os error code.
149 * @return pj_os_err_type
150 * @warning macro implementation; the statcode argument may be evaluated
151 * multiple times. If the statcode was not created by
152 * pj_get_os_error or PJ_STATUS_FROM_OS, the results are undefined.
153 */
154#define PJ_STATUS_TO_OS(e) (e == 0 ? PJ_SUCCESS : e - PJ_ERRNO_START_SYS)
155
156
157/**
158 * @defgroup pj_errnum PJLIB's Own Error Codes
159 * @ingroup pj_errno
160 * @{
161 */
162
163/**
164 * @hideinitializer
165 * Unknown error has been reported.
166 */
167#define PJ_EUNKNOWN (PJ_ERRNO_START_STATUS + 1) /* 70001 */
168/**
169 * @hideinitializer
170 * The operation is pending and will be completed later.
171 */
172#define PJ_EPENDING (PJ_ERRNO_START_STATUS + 2) /* 70002 */
173/**
174 * @hideinitializer
175 * Too many connecting sockets.
176 */
177#define PJ_ETOOMANYCONN (PJ_ERRNO_START_STATUS + 3) /* 70003 */
178/**
179 * @hideinitializer
180 * Invalid argument.
181 */
182#define PJ_EINVAL (PJ_ERRNO_START_STATUS + 4) /* 70004 */
183/**
184 * @hideinitializer
185 * Name too long (eg. hostname too long).
186 */
187#define PJ_ENAMETOOLONG (PJ_ERRNO_START_STATUS + 5) /* 70005 */
188/**
189 * @hideinitializer
190 * Not found.
191 */
192#define PJ_ENOTFOUND (PJ_ERRNO_START_STATUS + 6) /* 70006 */
193/**
194 * @hideinitializer
195 * Not enough memory.
196 */
197#define PJ_ENOMEM (PJ_ERRNO_START_STATUS + 7) /* 70007 */
198/**
199 * @hideinitializer
200 * Bug detected!
201 */
202#define PJ_EBUG (PJ_ERRNO_START_STATUS + 8) /* 70008 */
203/**
204 * @hideinitializer
205 * Operation timed out.
206 */
207#define PJ_ETIMEDOUT (PJ_ERRNO_START_STATUS + 9) /* 70009 */
208/**
209 * @hideinitializer
210 * Too many objects.
211 */
212#define PJ_ETOOMANY (PJ_ERRNO_START_STATUS + 10)/* 70010 */
213/**
214 * @hideinitializer
215 * Object is busy.
216 */
217#define PJ_EBUSY (PJ_ERRNO_START_STATUS + 11)/* 70011 */
218/**
219 * @hideinitializer
220 * The specified option is not supported.
221 */
222#define PJ_ENOTSUP (PJ_ERRNO_START_STATUS + 12)/* 70012 */
223/**
224 * @hideinitializer
225 * Invalid operation.
226 */
227#define PJ_EINVALIDOP (PJ_ERRNO_START_STATUS + 13)/* 70013 */
228/**
229 * @hideinitializer
230 * Operation is cancelled.
231 */
232#define PJ_ECANCELLED (PJ_ERRNO_START_STATUS + 14)/* 70014 */
233/**
234 * @hideinitializer
235 * Object already exists.
236 */
237#define PJ_EEXISTS (PJ_ERRNO_START_STATUS + 15)/* 70015 */
238
239/** @} */ /* pj_errnum */
240
241/** @} */ /* pj_errno */
242
243
244/**
245 * PJ_ERRNO_START is where PJLIB specific error values start.
246 */
247#define PJ_ERRNO_START 20000
248
249/**
250 * PJ_ERRNO_SPACE_SIZE is the maximum number of errors in one of
251 * the error/status range below.
252 */
253#define PJ_ERRNO_SPACE_SIZE 50000
254
255/**
256 * PJ_ERRNO_START_STATUS is where PJLIB specific status codes start.
257 * Effectively the error in this class would be 70000 - 119000.
258 */
259#define PJ_ERRNO_START_STATUS (PJ_ERRNO_START + PJ_ERRNO_SPACE_SIZE)
260
261/**
262 * PJ_ERRNO_START_SYS converts platform specific error codes into
263 * pj_status_t values.
264 * Effectively the error in this class would be 120000 - 169000.
265 */
266#define PJ_ERRNO_START_SYS (PJ_ERRNO_START_STATUS + PJ_ERRNO_SPACE_SIZE)
267
268/**
269 * PJ_ERRNO_START_USER are reserved for applications that use error
270 * codes along with PJLIB codes.
271 * Effectively the error in this class would be 170000 - 219000.
272 */
273#define PJ_ERRNO_START_USER (PJ_ERRNO_START_SYS + PJ_ERRNO_SPACE_SIZE)
274
275
276PJ_END_DECL
277
278#endif /* __PJ_ERRNO_H__ */
279