Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 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> |
| 23 | #include <pj/unicode.h> |
| 24 | #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 | */ |
| 37 | #if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING!=0) |
| 38 | |
| 39 | static const struct { |
| 40 | pj_os_err_type code; |
| 41 | const char *msg; |
| 42 | } gaErrorList[] = { |
| 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"), |
| 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} |
| 110 | }; |
| 111 | |
| 112 | #endif /* PJ_HAS_ERROR_STRING */ |
| 113 | |
| 114 | |
| 115 | |
| 116 | PJ_DEF(pj_status_t) pj_get_os_error(void) |
| 117 | { |
| 118 | return PJ_STATUS_FROM_OS(GetLastError()); |
| 119 | } |
| 120 | |
| 121 | PJ_DEF(void) pj_set_os_error(pj_status_t code) |
| 122 | { |
| 123 | SetLastError(PJ_STATUS_TO_OS(code)); |
| 124 | } |
| 125 | |
| 126 | PJ_DEF(pj_status_t) pj_get_netos_error(void) |
| 127 | { |
| 128 | return PJ_STATUS_FROM_OS(WSAGetLastError()); |
| 129 | } |
| 130 | |
| 131 | PJ_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 | */ |
| 142 | int platform_strerror( pj_os_err_type os_errcode, |
| 143 | char *buf, pj_size_t bufsize) |
| 144 | { |
| 145 | pj_size_t len = 0; |
| 146 | PJ_DECL_UNICODE_TEMP_BUF(wbuf,128); |
| 147 | |
| 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 | |
| 157 | if (!len) { |
| 158 | #if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING!=0) |
| 159 | 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 | } |
| 171 | #endif /* PJ_HAS_ERROR_STRING */ |
| 172 | |
| 173 | } |
| 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 | (int)bufsize, |
| 197 | NULL); |
| 198 | buf[bufsize-1] = '\0'; |
| 199 | #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 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if (!len) { |
| 211 | len = pj_ansi_snprintf( buf, bufsize, "Win32 error code %u", |
| 212 | (unsigned)os_errcode); |
| 213 | if (len < 0 || len >= (int)bufsize) |
| 214 | len = bufsize-1; |
| 215 | buf[len] = '\0'; |
| 216 | } |
| 217 | |
| 218 | return (int)len; |
| 219 | } |
| 220 | |