1
/* $Id: os_error_win32.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
#include <pj/assert.h>
22
#include <pj/compat/stdarg.h>
23
#include <pj/unicode.h>
24
#include <pj/string.h>
27
#if defined(PJ_HAS_WINSOCK2_H) && PJ_HAS_WINSOCK2_H != 0
28
# include <winsock2.h>
29
#elif defined(PJ_HAS_WINSOCK_H) && PJ_HAS_WINSOCK_H != 0
37
#if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING!=0)
43
PJ_BUILD_ERR( WSAEINTR, "Interrupted system call"),
44
PJ_BUILD_ERR( WSAEBADF, "Bad file number"),
45
PJ_BUILD_ERR( WSAEACCES, "Permission denied"),
46
PJ_BUILD_ERR( WSAEFAULT, "Bad address"),
47
PJ_BUILD_ERR( WSAEINVAL, "Invalid argument"),
48
PJ_BUILD_ERR( WSAEMFILE, "Too many open sockets"),
49
PJ_BUILD_ERR( WSAEWOULDBLOCK, "Operation would block"),
50
PJ_BUILD_ERR( WSAEINPROGRESS, "Operation now in progress"),
51
PJ_BUILD_ERR( WSAEALREADY, "Operation already in progress"),
52
PJ_BUILD_ERR( WSAENOTSOCK, "Socket operation on non-socket"),
53
PJ_BUILD_ERR( WSAEDESTADDRREQ, "Destination address required"),
54
PJ_BUILD_ERR( WSAEMSGSIZE, "Message too long"),
55
PJ_BUILD_ERR( WSAEPROTOTYPE, "Protocol wrong type for socket"),
56
PJ_BUILD_ERR( WSAENOPROTOOPT, "Bad protocol option"),
57
PJ_BUILD_ERR( WSAEPROTONOSUPPORT, "Protocol not supported"),
58
PJ_BUILD_ERR( WSAESOCKTNOSUPPORT, "Socket type not supported"),
59
PJ_BUILD_ERR( WSAEOPNOTSUPP, "Operation not supported on socket"),
60
PJ_BUILD_ERR( WSAEPFNOSUPPORT, "Protocol family not supported"),
61
PJ_BUILD_ERR( WSAEAFNOSUPPORT, "Address family not supported"),
62
PJ_BUILD_ERR( WSAEADDRINUSE, "Address already in use"),
63
PJ_BUILD_ERR( WSAEADDRNOTAVAIL, "Can't assign requested address"),
64
PJ_BUILD_ERR( WSAENETDOWN, "Network is down"),
65
PJ_BUILD_ERR( WSAENETUNREACH, "Network is unreachable"),
66
PJ_BUILD_ERR( WSAENETRESET, "Net connection reset"),
67
PJ_BUILD_ERR( WSAECONNABORTED, "Software caused connection abort"),
68
PJ_BUILD_ERR( WSAECONNRESET, "Connection reset by peer"),
69
PJ_BUILD_ERR( WSAENOBUFS, "No buffer space available"),
70
PJ_BUILD_ERR( WSAEISCONN, "Socket is already connected"),
71
PJ_BUILD_ERR( WSAENOTCONN, "Socket is not connected"),
72
PJ_BUILD_ERR( WSAESHUTDOWN, "Can't send after socket shutdown"),
73
PJ_BUILD_ERR( WSAETOOMANYREFS, "Too many references, can't splice"),
74
PJ_BUILD_ERR( WSAETIMEDOUT, "Connection timed out"),
75
PJ_BUILD_ERR( WSAECONNREFUSED, "Connection refused"),
76
PJ_BUILD_ERR( WSAELOOP, "Too many levels of symbolic links"),
77
PJ_BUILD_ERR( WSAENAMETOOLONG, "File name too long"),
78
PJ_BUILD_ERR( WSAEHOSTDOWN, "Host is down"),
79
PJ_BUILD_ERR( WSAEHOSTUNREACH, "No route to host"),
80
PJ_BUILD_ERR( WSAENOTEMPTY, "Directory not empty"),
81
PJ_BUILD_ERR( WSAEPROCLIM, "Too many processes"),
82
PJ_BUILD_ERR( WSAEUSERS, "Too many users"),
83
PJ_BUILD_ERR( WSAEDQUOT, "Disc quota exceeded"),
84
PJ_BUILD_ERR( WSAESTALE, "Stale NFS file handle"),
85
PJ_BUILD_ERR( WSAEREMOTE, "Too many levels of remote in path"),
86
PJ_BUILD_ERR( WSASYSNOTREADY, "Network system is unavailable"),
87
PJ_BUILD_ERR( WSAVERNOTSUPPORTED, "Winsock version out of range"),
88
PJ_BUILD_ERR( WSANOTINITIALISED, "WSAStartup not yet called"),
89
PJ_BUILD_ERR( WSAEDISCON, "Graceful shutdown in progress"),
91
#define WSAENOMORE (WSABASEERR+102)
92
#define WSAECANCELLED (WSABASEERR+103)
93
#define WSAEINVALIDPROCTABLE (WSABASEERR+104)
94
#define WSAEINVALIDPROVIDER (WSABASEERR+105)
95
#define WSAEPROVIDERFAILEDINIT (WSABASEERR+106)
96
#define WSASYSCALLFAILURE (WSABASEERR+107)
97
#define WSASERVICE_NOT_FOUND (WSABASEERR+108)
98
#define WSATYPE_NOT_FOUND (WSABASEERR+109)
99
#define WSA_E_NO_MORE (WSABASEERR+110)
100
#define WSA_E_CANCELLED (WSABASEERR+111)
101
#define WSAEREFUSED (WSABASEERR+112)
103
PJ_BUILD_ERR( WSAHOST_NOT_FOUND, "Host not found"),
105
#define WSATRY_AGAIN (WSABASEERR+1002)
106
#define WSANO_RECOVERY (WSABASEERR+1003)
108
PJ_BUILD_ERR( WSANO_DATA, "No host data of that type was found"),
112
#endif /* PJ_HAS_ERROR_STRING */
116
PJ_DEF(pj_status_t) pj_get_os_error(void)
118
return PJ_STATUS_FROM_OS(GetLastError());
121
PJ_DEF(void) pj_set_os_error(pj_status_t code)
123
SetLastError(PJ_STATUS_TO_OS(code));
126
PJ_DEF(pj_status_t) pj_get_netos_error(void)
128
return PJ_STATUS_FROM_OS(WSAGetLastError());
131
PJ_DEF(void) pj_set_netos_error(pj_status_t code)
133
WSASetLastError(PJ_STATUS_TO_OS(code));
137
* platform_strerror()
139
* Platform specific error message. This file is called by pj_strerror()
142
int platform_strerror( pj_os_err_type os_errcode,
143
char *buf, pj_size_t bufsize)
146
PJ_DECL_UNICODE_TEMP_BUF(wbuf,128);
148
pj_assert(buf != NULL);
149
pj_assert(bufsize >= 0);
152
* MUST NOT check stack here.
153
* This function might be called from PJ_CHECK_STACK() itself!
158
#if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING!=0)
160
for (i = 0; gaErrorList[i].msg; ++i) {
161
if (gaErrorList[i].code == os_errcode) {
162
len = strlen(gaErrorList[i].msg);
163
if ((pj_size_t)len >= bufsize) {
166
pj_memcpy(buf, gaErrorList[i].msg, len);
171
#endif /* PJ_HAS_ERROR_STRING */
177
#if PJ_NATIVE_STRING_IS_UNICODE
178
len = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM
179
| FORMAT_MESSAGE_IGNORE_INSERTS,
182
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
187
pj_unicode_to_ansi(wbuf, len, buf, bufsize);
190
len = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM
191
| FORMAT_MESSAGE_IGNORE_INSERTS,
194
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
198
buf[bufsize-1] = '\0';
202
/* Remove trailing newlines. */
203
while (len && (buf[len-1] == '\n' || buf[len-1] == '\r')) {
211
len = pj_ansi_snprintf( buf, bufsize, "Win32 error code %u",
212
(unsigned)os_errcode);
213
if (len < 0 || len >= (int)bufsize)