Debian Codegraph @

Xpjproject/pjlib/src/pj/os_error_win32.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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>
22 #include <pj/unicode.h>
23 #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 */
36 #if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING!=0)
37
38 static const struct {
39 pj_os_err_type code;
40 const char *msg;
41 } gaErrorList[] = {
42 PJ_BUILD_ERR( WSAEINTR, "Interrupted system call"),
43 PJ_BUILD_ERR( WSAEBADF, "Bad file number"),
44 PJ_BUILD_ERR( WSAEACCES, "Permission denied"),
45 PJ_BUILD_ERR( WSAEFAULT, "Bad address"),
46 PJ_BUILD_ERR( WSAEINVAL, "Invalid argument"),
47 PJ_BUILD_ERR( WSAEMFILE, "Too many open sockets"),
48 PJ_BUILD_ERR( WSAEWOULDBLOCK, "Operation would block"),
49 PJ_BUILD_ERR( WSAEINPROGRESS, "Operation now in progress"),
50 PJ_BUILD_ERR( WSAEALREADY, "Operation already in progress"),
51 PJ_BUILD_ERR( WSAENOTSOCK, "Socket operation on non-socket"),
52 PJ_BUILD_ERR( WSAEDESTADDRREQ, "Destination address required"),
53 PJ_BUILD_ERR( WSAEMSGSIZE, "Message too long"),
54 PJ_BUILD_ERR( WSAEPROTOTYPE, "Protocol wrong type for socket"),
55 PJ_BUILD_ERR( WSAENOPROTOOPT, "Bad protocol option"),
56 PJ_BUILD_ERR( WSAEPROTONOSUPPORT, "Protocol not supported"),
57 PJ_BUILD_ERR( WSAESOCKTNOSUPPORT, "Socket type not supported"),
58 PJ_BUILD_ERR( WSAEOPNOTSUPP, "Operation not supported on socket"),
59 PJ_BUILD_ERR( WSAEPFNOSUPPORT, "Protocol family not supported"),
60 PJ_BUILD_ERR( WSAEAFNOSUPPORT, "Address family not supported"),
61 PJ_BUILD_ERR( WSAEADDRINUSE, "Address already in use"),
62 PJ_BUILD_ERR( WSAEADDRNOTAVAIL, "Can't assign requested address"),
63 PJ_BUILD_ERR( WSAENETDOWN, "Network is down"),
64 PJ_BUILD_ERR( WSAENETUNREACH, "Network is unreachable"),
65 PJ_BUILD_ERR( WSAENETRESET, "Net connection reset"),
66 PJ_BUILD_ERR( WSAECONNABORTED, "Software caused connection abort"),
67 PJ_BUILD_ERR( WSAECONNRESET, "Connection reset by peer"),
68 PJ_BUILD_ERR( WSAENOBUFS, "No buffer space available"),
69 PJ_BUILD_ERR( WSAEISCONN, "Socket is already connected"),
70 PJ_BUILD_ERR( WSAENOTCONN, "Socket is not connected"),
71 PJ_BUILD_ERR( WSAESHUTDOWN, "Can't send after socket shutdown"),
72 PJ_BUILD_ERR( WSAETOOMANYREFS, "Too many references, can't splice"),
73 PJ_BUILD_ERR( WSAETIMEDOUT, "Connection timed out"),
74 PJ_BUILD_ERR( WSAECONNREFUSED, "Connection refused"),
75 PJ_BUILD_ERR( WSAELOOP, "Too many levels of symbolic links"),
76 PJ_BUILD_ERR( WSAENAMETOOLONG, "File name too long"),
77 PJ_BUILD_ERR( WSAEHOSTDOWN, "Host is down"),
78 PJ_BUILD_ERR( WSAEHOSTUNREACH, "No route to host"),
79 PJ_BUILD_ERR( WSAENOTEMPTY, "Directory not empty"),
80 PJ_BUILD_ERR( WSAEPROCLIM, "Too many processes"),
81 PJ_BUILD_ERR( WSAEUSERS, "Too many users"),
82 PJ_BUILD_ERR( WSAEDQUOT, "Disc quota exceeded"),
83 PJ_BUILD_ERR( WSAESTALE, "Stale NFS file handle"),
84 PJ_BUILD_ERR( WSAEREMOTE, "Too many levels of remote in path"),
85 PJ_BUILD_ERR( WSASYSNOTREADY, "Network system is unavailable"),
86 PJ_BUILD_ERR( WSAVERNOTSUPPORTED, "Winsock version out of range"),
87 PJ_BUILD_ERR( WSANOTINITIALISED, "WSAStartup not yet called"),
88 PJ_BUILD_ERR( WSAEDISCON, "Graceful shutdown in progress"),
89 /*
90 #define WSAENOMORE (WSABASEERR+102)
91 #define WSAECANCELLED (WSABASEERR+103)
92 #define WSAEINVALIDPROCTABLE (WSABASEERR+104)
93 #define WSAEINVALIDPROVIDER (WSABASEERR+105)
94 #define WSAEPROVIDERFAILEDINIT (WSABASEERR+106)
95 #define WSASYSCALLFAILURE (WSABASEERR+107)
96 #define WSASERVICE_NOT_FOUND (WSABASEERR+108)
97 #define WSATYPE_NOT_FOUND (WSABASEERR+109)
98 #define WSA_E_NO_MORE (WSABASEERR+110)
99 #define WSA_E_CANCELLED (WSABASEERR+111)
100 #define WSAEREFUSED (WSABASEERR+112)
101 */
102 PJ_BUILD_ERR( WSAHOST_NOT_FOUND, "Host not found"),
103 /*
104 #define WSATRY_AGAIN (WSABASEERR+1002)
105 #define WSANO_RECOVERY (WSABASEERR+1003)
106 */
107 PJ_BUILD_ERR( WSANO_DATA, "No host data of that type was found"),
108 {0, NULL}
109 };
110
111 #endif /* PJ_HAS_ERROR_STRING */
112
113
114
115 PJ_DEF(pj_status_t) pj_get_os_error(void)
116 {
117 return PJ_STATUS_FROM_OS(GetLastError());
118 }
119
120 PJ_DEF(void) pj_set_os_error(pj_status_t code)
121 {
122 SetLastError(PJ_STATUS_TO_OS(code));
123 }
124
125 PJ_DEF(pj_status_t) pj_get_netos_error(void)
126 {
127 return PJ_STATUS_FROM_OS(WSAGetLastError());
128 }
129
130 PJ_DEF(void) pj_set_netos_error(pj_status_t code)
131 {
132 WSASetLastError(PJ_STATUS_TO_OS(code));
133 }
134
135 /*
136 * platform_strerror()
137 *
138 * Platform specific error message. This file is called by pj_strerror()
139 * in errno.c
140 */
141 int platform_strerror( pj_os_err_type os_errcode,
142 char *buf, pj_size_t bufsize)
143 {
144 pj_size_t len = 0;
145 PJ_DECL_UNICODE_TEMP_BUF(wbuf,128);
146
147 pj_assert(buf != NULL);
148
149 if (bufsize == 0)
150 return 0;
151
152 /*
153 * MUST NOT check stack here.
154 * This function might be called from PJ_CHECK_STACK() itself!
155 //PJ_CHECK_STACK();
156 */
157
158 if (!len) {
159 #if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING!=0)
160 int i;
161 for (i = 0; gaErrorList[i].msg; ++i) {
162 if (gaErrorList[i].code == os_errcode) {
163 len = strlen(gaErrorList[i].msg);
164 if ((pj_size_t)len >= bufsize) {
165 len = bufsize-1;
166 }
167 pj_memcpy(buf, gaErrorList[i].msg, len);
168 buf[len] = '\0';
169 break;
170 }
171 }
172 #endif /* PJ_HAS_ERROR_STRING */
173
174 }
175
176
177 if (!len) {
178 DWORD dwLanguageId;
179 #if defined(PJ_STRERROR_USE_WIN_GET_THREAD_LOCALE) && \
180 (PJ_STRERROR_USE_WIN_GET_THREAD_LOCALE==1) && (WINVER >= 0x0500)
181 /* current thread language */
182 dwLanguageId = LANGIDFROMLCID(GetThreadLocale());
183 #else
184 /* LANG_USER_DEFAULT - User default language */
185 dwLanguageId = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
186 #endif
187 #if PJ_NATIVE_STRING_IS_UNICODE
188 len = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM
189 | FORMAT_MESSAGE_IGNORE_INSERTS,
190 NULL,
191 os_errcode,
192 dwLanguageId,
193 wbuf,
194 PJ_ARRAY_SIZE(wbuf),
195 NULL);
196 if (len) {
197 pj_unicode_to_ansi(wbuf, len, buf, bufsize);
198 }
199 #else
200 len = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM
201 | FORMAT_MESSAGE_IGNORE_INSERTS,
202 NULL,
203 os_errcode,
204 dwLanguageId,
205 buf,
206 (int)bufsize,
207 NULL);
208 buf[bufsize-1] = '\0';
209 #endif
210
211 if (len) {
212 /* Remove trailing newlines. */
213 while (len && (buf[len-1] == '\n' || buf[len-1] == '\r')) {
214 buf[len-1] = '\0';
215 --len;
216 }
217 }
218 }
219
220 if (!len) {
221 len = pj_ansi_snprintf( buf, bufsize, "Win32 error code %u",
222 (unsigned)os_errcode);
223 if (len < 0 || len >= (int)bufsize)
224 len = bufsize-1;
225 buf[len] = '\0';
226 }
227
228 return (int)len;
229 }
230