blob: 80a4eb239fb029755e0ca456ed2e3596ed681aa7 [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#include <pj/errno.h>
20#include <pj/assert.h>
21#include <pj/compat/stdarg.h>
Benny Prijono37e8d332006-01-20 21:03:36 +000022#include <pj/unicode.h>
Benny Prijono9033e312005-11-21 02:08:39 +000023#include <pj/string.h>
24
25
26#if defined(PJ_HAS_WINSOCK2_H) && PJ_HAS_WINSOCK2_H != 0
27# include <winsock2.h>
28#elif defined(PJ_HAS_WINSOCK_H) && PJ_HAS_WINSOCK_H != 0
29# include <winsock.h>
30#endif
31
32
33/*
34 * From Apache's APR:
35 */
Benny Prijono3ba816e2006-03-18 12:26:55 +000036#if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING!=0)
37
Benny Prijono9033e312005-11-21 02:08:39 +000038static const struct {
39 pj_os_err_type code;
40 const char *msg;
41} gaErrorList[] = {
42 {WSAEINTR, "Interrupted system call"},
43 {WSAEBADF, "Bad file number"},
44 {WSAEACCES, "Permission denied"},
45 {WSAEFAULT, "Bad address"},
46 {WSAEINVAL, "Invalid argument"},
47 {WSAEMFILE, "Too many open sockets"},
48 {WSAEWOULDBLOCK, "Operation would block"},
49 {WSAEINPROGRESS, "Operation now in progress"},
50 {WSAEALREADY, "Operation already in progress"},
51 {WSAENOTSOCK, "Socket operation on non-socket"},
52 {WSAEDESTADDRREQ, "Destination address required"},
53 {WSAEMSGSIZE, "Message too long"},
54 {WSAEPROTOTYPE, "Protocol wrong type for socket"},
55 {WSAENOPROTOOPT, "Bad protocol option"},
56 {WSAEPROTONOSUPPORT, "Protocol not supported"},
57 {WSAESOCKTNOSUPPORT, "Socket type not supported"},
58 {WSAEOPNOTSUPP, "Operation not supported on socket"},
59 {WSAEPFNOSUPPORT, "Protocol family not supported"},
60 {WSAEAFNOSUPPORT, "Address family not supported"},
61 {WSAEADDRINUSE, "Address already in use"},
62 {WSAEADDRNOTAVAIL, "Can't assign requested address"},
63 {WSAENETDOWN, "Network is down"},
64 {WSAENETUNREACH, "Network is unreachable"},
65 {WSAENETRESET, "Net connection reset"},
66 {WSAECONNABORTED, "Software caused connection abort"},
67 {WSAECONNRESET, "Connection reset by peer"},
68 {WSAENOBUFS, "No buffer space available"},
69 {WSAEISCONN, "Socket is already connected"},
70 {WSAENOTCONN, "Socket is not connected"},
71 {WSAESHUTDOWN, "Can't send after socket shutdown"},
72 {WSAETOOMANYREFS, "Too many references, can't splice"},
73 {WSAETIMEDOUT, "Connection timed out"},
74 {WSAECONNREFUSED, "Connection refused"},
75 {WSAELOOP, "Too many levels of symbolic links"},
76 {WSAENAMETOOLONG, "File name too long"},
77 {WSAEHOSTDOWN, "Host is down"},
78 {WSAEHOSTUNREACH, "No route to host"},
79 {WSAENOTEMPTY, "Directory not empty"},
80 {WSAEPROCLIM, "Too many processes"},
81 {WSAEUSERS, "Too many users"},
82 {WSAEDQUOT, "Disc quota exceeded"},
83 {WSAESTALE, "Stale NFS file handle"},
84 {WSAEREMOTE, "Too many levels of remote in path"},
85 {WSASYSNOTREADY, "Network system is unavailable"},
86 {WSAVERNOTSUPPORTED, "Winsock version out of range"},
87 {WSANOTINITIALISED, "WSAStartup not yet called"},
88 {WSAEDISCON, "Graceful shutdown in progress"},
89 {WSAHOST_NOT_FOUND, "Host not found"},
90 {WSANO_DATA, "No host data of that type was found"},
91 {0, NULL}
92};
93
Benny Prijono3ba816e2006-03-18 12:26:55 +000094#endif /* PJ_HAS_ERROR_STRING */
95
96
Benny Prijono9033e312005-11-21 02:08:39 +000097
98PJ_DEF(pj_status_t) pj_get_os_error(void)
99{
100 return PJ_STATUS_FROM_OS(GetLastError());
101}
102
103PJ_DEF(void) pj_set_os_error(pj_status_t code)
104{
105 SetLastError(PJ_STATUS_TO_OS(code));
106}
107
108PJ_DEF(pj_status_t) pj_get_netos_error(void)
109{
110 return PJ_STATUS_FROM_OS(WSAGetLastError());
111}
112
113PJ_DEF(void) pj_set_netos_error(pj_status_t code)
114{
115 WSASetLastError(PJ_STATUS_TO_OS(code));
116}
117
118/*
119 * platform_strerror()
120 *
121 * Platform specific error message. This file is called by pj_strerror()
122 * in errno.c
123 */
124int platform_strerror( pj_os_err_type os_errcode,
125 char *buf, pj_size_t bufsize)
126{
127 int len;
Benny Prijono37e8d332006-01-20 21:03:36 +0000128 PJ_DECL_UNICODE_TEMP_BUF(wbuf,128);
Benny Prijono9033e312005-11-21 02:08:39 +0000129
130 pj_assert(buf != NULL);
131 pj_assert(bufsize >= 0);
132
133 /*
134 * MUST NOT check stack here.
135 * This function might be called from PJ_CHECK_STACK() itself!
136 //PJ_CHECK_STACK();
137 */
138
Benny Prijono37e8d332006-01-20 21:03:36 +0000139#if PJ_NATIVE_STRING_IS_UNICODE
Benny Prijono9033e312005-11-21 02:08:39 +0000140 len = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM
141 | FORMAT_MESSAGE_IGNORE_INSERTS,
142 NULL,
143 os_errcode,
144 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
Benny Prijono37e8d332006-01-20 21:03:36 +0000145 wbuf,
146 sizeof(wbuf),
Benny Prijono9033e312005-11-21 02:08:39 +0000147 NULL);
Benny Prijono37e8d332006-01-20 21:03:36 +0000148 if (len) {
149 pj_unicode_to_ansi(wbuf, len, buf, bufsize);
150 }
151#else
152 len = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM
153 | FORMAT_MESSAGE_IGNORE_INSERTS,
154 NULL,
155 os_errcode,
156 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
157 buf,
158 bufsize,
159 NULL);
160#endif
Benny Prijono9033e312005-11-21 02:08:39 +0000161
162 if (!len) {
Benny Prijono3ba816e2006-03-18 12:26:55 +0000163
164#if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING!=0)
Benny Prijono9033e312005-11-21 02:08:39 +0000165 int i;
166 for (i = 0; gaErrorList[i].msg; ++i) {
167 if (gaErrorList[i].code == os_errcode) {
168 len = strlen(gaErrorList[i].msg);
169 if ((pj_size_t)len >= bufsize) {
170 len = bufsize-1;
171 }
172 pj_memcpy(buf, gaErrorList[i].msg, len);
173 buf[len] = '\0';
174 break;
175 }
176 }
Benny Prijono3ba816e2006-03-18 12:26:55 +0000177#endif /* PJ_HAS_ERROR_STRING */
Benny Prijonoc81dfef2006-01-07 18:41:22 +0000178
179 } else {
180 /* Remove trailing newlines. */
181 while (len && (buf[len-1] == '\n' || buf[len-1] == '\r')) {
182 buf[len-1] = '\0';
183 --len;
184 }
Benny Prijono9033e312005-11-21 02:08:39 +0000185 }
186
187 if (!len) {
Benny Prijonoed811d72006-03-10 12:57:12 +0000188 len = pj_ansi_snprintf( buf, bufsize, "Unknown native error %u",
189 (unsigned)os_errcode);
Benny Prijono9033e312005-11-21 02:08:39 +0000190 buf[len] = '\0';
191 }
192
193 return len;
194}
195