blob: 6aa46172b639108a2796ffd706354c9363a52ce3 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C)2003-2008 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#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[] = {
Benny Prijono522cd292006-06-26 10:02:28 +000042 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}
Benny Prijono9033e312005-11-21 02:08:39 +0000109};
110
Benny Prijono3ba816e2006-03-18 12:26:55 +0000111#endif /* PJ_HAS_ERROR_STRING */
112
113
Benny Prijono9033e312005-11-21 02:08:39 +0000114
115PJ_DEF(pj_status_t) pj_get_os_error(void)
116{
117 return PJ_STATUS_FROM_OS(GetLastError());
118}
119
120PJ_DEF(void) pj_set_os_error(pj_status_t code)
121{
122 SetLastError(PJ_STATUS_TO_OS(code));
123}
124
125PJ_DEF(pj_status_t) pj_get_netos_error(void)
126{
127 return PJ_STATUS_FROM_OS(WSAGetLastError());
128}
129
130PJ_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 */
141int platform_strerror( pj_os_err_type os_errcode,
142 char *buf, pj_size_t bufsize)
143{
Benny Prijono64df0eb2007-01-26 17:09:14 +0000144 int len = 0;
Benny Prijono37e8d332006-01-20 21:03:36 +0000145 PJ_DECL_UNICODE_TEMP_BUF(wbuf,128);
Benny Prijono9033e312005-11-21 02:08:39 +0000146
147 pj_assert(buf != NULL);
148 pj_assert(bufsize >= 0);
149
150 /*
151 * MUST NOT check stack here.
152 * This function might be called from PJ_CHECK_STACK() itself!
153 //PJ_CHECK_STACK();
154 */
155
Benny Prijono9033e312005-11-21 02:08:39 +0000156 if (!len) {
Benny Prijono3ba816e2006-03-18 12:26:55 +0000157#if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING!=0)
Benny Prijono9033e312005-11-21 02:08:39 +0000158 int i;
159 for (i = 0; gaErrorList[i].msg; ++i) {
160 if (gaErrorList[i].code == os_errcode) {
161 len = strlen(gaErrorList[i].msg);
162 if ((pj_size_t)len >= bufsize) {
163 len = bufsize-1;
164 }
165 pj_memcpy(buf, gaErrorList[i].msg, len);
166 buf[len] = '\0';
167 break;
168 }
169 }
Benny Prijono3ba816e2006-03-18 12:26:55 +0000170#endif /* PJ_HAS_ERROR_STRING */
Benny Prijonoc81dfef2006-01-07 18:41:22 +0000171
Benny Prijono64df0eb2007-01-26 17:09:14 +0000172 }
173
174
175 if (!len) {
176#if PJ_NATIVE_STRING_IS_UNICODE
177 len = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM
178 | FORMAT_MESSAGE_IGNORE_INSERTS,
179 NULL,
180 os_errcode,
181 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
182 wbuf,
183 sizeof(wbuf),
184 NULL);
185 if (len) {
186 pj_unicode_to_ansi(wbuf, len, buf, bufsize);
187 }
188#else
189 len = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM
190 | FORMAT_MESSAGE_IGNORE_INSERTS,
191 NULL,
192 os_errcode,
193 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
194 buf,
195 bufsize,
196 NULL);
Benny Prijono2e5516e2007-02-16 23:56:37 +0000197 buf[bufsize-1] = '\0';
Benny Prijono64df0eb2007-01-26 17:09:14 +0000198#endif
199
200 if (len) {
201 /* Remove trailing newlines. */
202 while (len && (buf[len-1] == '\n' || buf[len-1] == '\r')) {
203 buf[len-1] = '\0';
204 --len;
205 }
Benny Prijonoc81dfef2006-01-07 18:41:22 +0000206 }
Benny Prijono9033e312005-11-21 02:08:39 +0000207 }
208
209 if (!len) {
Benny Prijono522cd292006-06-26 10:02:28 +0000210 len = pj_ansi_snprintf( buf, bufsize, "Win32 error code %u",
Benny Prijonoed811d72006-03-10 12:57:12 +0000211 (unsigned)os_errcode);
Benny Prijono2e5516e2007-02-16 23:56:37 +0000212 if (len < 0 || len >= (int)bufsize)
213 len = bufsize-1;
Benny Prijono9033e312005-11-21 02:08:39 +0000214 buf[len] = '\0';
215 }
216
217 return len;
218}
219