blob: f47bcf41a9bcf0f2d2c001508ed7020b64e96ce5 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono9033e312005-11-21 02:08:39 +00005 *
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.
10 *
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.
15 *
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
19 */
20#include <pj/errno.h>
21#include <pj/assert.h>
22#include <pj/compat/stdarg.h>
Benny Prijono37e8d332006-01-20 21:03:36 +000023#include <pj/unicode.h>
Benny Prijono9033e312005-11-21 02:08:39 +000024#include <pj/string.h>
25
26
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
30# include <winsock.h>
31#endif
32
33
34/*
35 * From Apache's APR:
36 */
Benny Prijono3ba816e2006-03-18 12:26:55 +000037#if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING!=0)
38
Benny Prijono9033e312005-11-21 02:08:39 +000039static const struct {
40 pj_os_err_type code;
41 const char *msg;
42} gaErrorList[] = {
Benny Prijono522cd292006-06-26 10:02:28 +000043 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"),
90/*
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)
102 */
103 PJ_BUILD_ERR( WSAHOST_NOT_FOUND, "Host not found"),
104/*
105#define WSATRY_AGAIN (WSABASEERR+1002)
106#define WSANO_RECOVERY (WSABASEERR+1003)
107 */
108 PJ_BUILD_ERR( WSANO_DATA, "No host data of that type was found"),
109 {0, NULL}
Benny Prijono9033e312005-11-21 02:08:39 +0000110};
111
Benny Prijono3ba816e2006-03-18 12:26:55 +0000112#endif /* PJ_HAS_ERROR_STRING */
113
114
Benny Prijono9033e312005-11-21 02:08:39 +0000115
116PJ_DEF(pj_status_t) pj_get_os_error(void)
117{
118 return PJ_STATUS_FROM_OS(GetLastError());
119}
120
121PJ_DEF(void) pj_set_os_error(pj_status_t code)
122{
123 SetLastError(PJ_STATUS_TO_OS(code));
124}
125
126PJ_DEF(pj_status_t) pj_get_netos_error(void)
127{
128 return PJ_STATUS_FROM_OS(WSAGetLastError());
129}
130
131PJ_DEF(void) pj_set_netos_error(pj_status_t code)
132{
133 WSASetLastError(PJ_STATUS_TO_OS(code));
134}
135
136/*
137 * platform_strerror()
138 *
139 * Platform specific error message. This file is called by pj_strerror()
140 * in errno.c
141 */
142int platform_strerror( pj_os_err_type os_errcode,
143 char *buf, pj_size_t bufsize)
144{
Benny Prijono64df0eb2007-01-26 17:09:14 +0000145 int len = 0;
Benny Prijono37e8d332006-01-20 21:03:36 +0000146 PJ_DECL_UNICODE_TEMP_BUF(wbuf,128);
Benny Prijono9033e312005-11-21 02:08:39 +0000147
148 pj_assert(buf != NULL);
149 pj_assert(bufsize >= 0);
150
151 /*
152 * MUST NOT check stack here.
153 * This function might be called from PJ_CHECK_STACK() itself!
154 //PJ_CHECK_STACK();
155 */
156
Benny Prijono9033e312005-11-21 02:08:39 +0000157 if (!len) {
Benny Prijono3ba816e2006-03-18 12:26:55 +0000158#if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING!=0)
Benny Prijono9033e312005-11-21 02:08:39 +0000159 int i;
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) {
164 len = bufsize-1;
165 }
166 pj_memcpy(buf, gaErrorList[i].msg, len);
167 buf[len] = '\0';
168 break;
169 }
170 }
Benny Prijono3ba816e2006-03-18 12:26:55 +0000171#endif /* PJ_HAS_ERROR_STRING */
Benny Prijonoc81dfef2006-01-07 18:41:22 +0000172
Benny Prijono64df0eb2007-01-26 17:09:14 +0000173 }
174
175
176 if (!len) {
177#if PJ_NATIVE_STRING_IS_UNICODE
178 len = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM
179 | FORMAT_MESSAGE_IGNORE_INSERTS,
180 NULL,
181 os_errcode,
182 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
183 wbuf,
184 sizeof(wbuf),
185 NULL);
186 if (len) {
187 pj_unicode_to_ansi(wbuf, len, buf, bufsize);
188 }
189#else
190 len = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM
191 | FORMAT_MESSAGE_IGNORE_INSERTS,
192 NULL,
193 os_errcode,
194 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
195 buf,
196 bufsize,
197 NULL);
Benny Prijono2e5516e2007-02-16 23:56:37 +0000198 buf[bufsize-1] = '\0';
Benny Prijono64df0eb2007-01-26 17:09:14 +0000199#endif
200
201 if (len) {
202 /* Remove trailing newlines. */
203 while (len && (buf[len-1] == '\n' || buf[len-1] == '\r')) {
204 buf[len-1] = '\0';
205 --len;
206 }
Benny Prijonoc81dfef2006-01-07 18:41:22 +0000207 }
Benny Prijono9033e312005-11-21 02:08:39 +0000208 }
209
210 if (!len) {
Benny Prijono522cd292006-06-26 10:02:28 +0000211 len = pj_ansi_snprintf( buf, bufsize, "Win32 error code %u",
Benny Prijonoed811d72006-03-10 12:57:12 +0000212 (unsigned)os_errcode);
Benny Prijono2e5516e2007-02-16 23:56:37 +0000213 if (len < 0 || len >= (int)bufsize)
214 len = bufsize-1;
Benny Prijono9033e312005-11-21 02:08:39 +0000215 buf[len] = '\0';
216 }
217
218 return len;
219}
220