strerror.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef HAVE_STRERROR_R
  26. # if (!defined(HAVE_POSIX_STRERROR_R) && \
  27. !defined(HAVE_GLIBC_STRERROR_R)) || \
  28. (defined(HAVE_POSIX_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R))
  29. # error "strerror_r MUST be either POSIX, glibc style"
  30. # endif
  31. #endif
  32. #include <curl/curl.h>
  33. #ifdef USE_LIBIDN2
  34. #include <idn2.h>
  35. #endif
  36. #ifdef USE_WINDOWS_SSPI
  37. #include "curl_sspi.h"
  38. #endif
  39. #include "strerror.h"
  40. /* The last 3 #include files should be in this order */
  41. #include "curl_printf.h"
  42. #include "curl_memory.h"
  43. #include "memdebug.h"
  44. #if defined(WIN32) || defined(_WIN32_WCE)
  45. #define PRESERVE_WINDOWS_ERROR_CODE
  46. #endif
  47. const char *
  48. curl_easy_strerror(CURLcode error)
  49. {
  50. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  51. switch(error) {
  52. case CURLE_OK:
  53. return "No error";
  54. case CURLE_UNSUPPORTED_PROTOCOL:
  55. return "Unsupported protocol";
  56. case CURLE_FAILED_INIT:
  57. return "Failed initialization";
  58. case CURLE_URL_MALFORMAT:
  59. return "URL using bad/illegal format or missing URL";
  60. case CURLE_NOT_BUILT_IN:
  61. return "A requested feature, protocol or option was not found built-in in"
  62. " this libcurl due to a build-time decision.";
  63. case CURLE_COULDNT_RESOLVE_PROXY:
  64. return "Couldn't resolve proxy name";
  65. case CURLE_COULDNT_RESOLVE_HOST:
  66. return "Couldn't resolve host name";
  67. case CURLE_COULDNT_CONNECT:
  68. return "Couldn't connect to server";
  69. case CURLE_WEIRD_SERVER_REPLY:
  70. return "Weird server reply";
  71. case CURLE_REMOTE_ACCESS_DENIED:
  72. return "Access denied to remote resource";
  73. case CURLE_FTP_ACCEPT_FAILED:
  74. return "FTP: The server failed to connect to data port";
  75. case CURLE_FTP_ACCEPT_TIMEOUT:
  76. return "FTP: Accepting server connect has timed out";
  77. case CURLE_FTP_PRET_FAILED:
  78. return "FTP: The server did not accept the PRET command.";
  79. case CURLE_FTP_WEIRD_PASS_REPLY:
  80. return "FTP: unknown PASS reply";
  81. case CURLE_FTP_WEIRD_PASV_REPLY:
  82. return "FTP: unknown PASV reply";
  83. case CURLE_FTP_WEIRD_227_FORMAT:
  84. return "FTP: unknown 227 response format";
  85. case CURLE_FTP_CANT_GET_HOST:
  86. return "FTP: can't figure out the host in the PASV response";
  87. case CURLE_HTTP2:
  88. return "Error in the HTTP2 framing layer";
  89. case CURLE_FTP_COULDNT_SET_TYPE:
  90. return "FTP: couldn't set file type";
  91. case CURLE_PARTIAL_FILE:
  92. return "Transferred a partial file";
  93. case CURLE_FTP_COULDNT_RETR_FILE:
  94. return "FTP: couldn't retrieve (RETR failed) the specified file";
  95. case CURLE_QUOTE_ERROR:
  96. return "Quote command returned error";
  97. case CURLE_HTTP_RETURNED_ERROR:
  98. return "HTTP response code said error";
  99. case CURLE_WRITE_ERROR:
  100. return "Failed writing received data to disk/application";
  101. case CURLE_UPLOAD_FAILED:
  102. return "Upload failed (at start/before it took off)";
  103. case CURLE_READ_ERROR:
  104. return "Failed to open/read local data from file/application";
  105. case CURLE_OUT_OF_MEMORY:
  106. return "Out of memory";
  107. case CURLE_OPERATION_TIMEDOUT:
  108. return "Timeout was reached";
  109. case CURLE_FTP_PORT_FAILED:
  110. return "FTP: command PORT failed";
  111. case CURLE_FTP_COULDNT_USE_REST:
  112. return "FTP: command REST failed";
  113. case CURLE_RANGE_ERROR:
  114. return "Requested range was not delivered by the server";
  115. case CURLE_HTTP_POST_ERROR:
  116. return "Internal problem setting up the POST";
  117. case CURLE_SSL_CONNECT_ERROR:
  118. return "SSL connect error";
  119. case CURLE_BAD_DOWNLOAD_RESUME:
  120. return "Couldn't resume download";
  121. case CURLE_FILE_COULDNT_READ_FILE:
  122. return "Couldn't read a file:// file";
  123. case CURLE_LDAP_CANNOT_BIND:
  124. return "LDAP: cannot bind";
  125. case CURLE_LDAP_SEARCH_FAILED:
  126. return "LDAP: search failed";
  127. case CURLE_FUNCTION_NOT_FOUND:
  128. return "A required function in the library was not found";
  129. case CURLE_ABORTED_BY_CALLBACK:
  130. return "Operation was aborted by an application callback";
  131. case CURLE_BAD_FUNCTION_ARGUMENT:
  132. return "A libcurl function was given a bad argument";
  133. case CURLE_INTERFACE_FAILED:
  134. return "Failed binding local connection end";
  135. case CURLE_TOO_MANY_REDIRECTS:
  136. return "Number of redirects hit maximum amount";
  137. case CURLE_UNKNOWN_OPTION:
  138. return "An unknown option was passed in to libcurl";
  139. case CURLE_SETOPT_OPTION_SYNTAX:
  140. return "Malformed option provided in a setopt";
  141. case CURLE_GOT_NOTHING:
  142. return "Server returned nothing (no headers, no data)";
  143. case CURLE_SSL_ENGINE_NOTFOUND:
  144. return "SSL crypto engine not found";
  145. case CURLE_SSL_ENGINE_SETFAILED:
  146. return "Can not set SSL crypto engine as default";
  147. case CURLE_SSL_ENGINE_INITFAILED:
  148. return "Failed to initialise SSL crypto engine";
  149. case CURLE_SEND_ERROR:
  150. return "Failed sending data to the peer";
  151. case CURLE_RECV_ERROR:
  152. return "Failure when receiving data from the peer";
  153. case CURLE_SSL_CERTPROBLEM:
  154. return "Problem with the local SSL certificate";
  155. case CURLE_SSL_CIPHER:
  156. return "Couldn't use specified SSL cipher";
  157. case CURLE_PEER_FAILED_VERIFICATION:
  158. return "SSL peer certificate or SSH remote key was not OK";
  159. case CURLE_SSL_CACERT_BADFILE:
  160. return "Problem with the SSL CA cert (path? access rights?)";
  161. case CURLE_BAD_CONTENT_ENCODING:
  162. return "Unrecognized or bad HTTP Content or Transfer-Encoding";
  163. case CURLE_FILESIZE_EXCEEDED:
  164. return "Maximum file size exceeded";
  165. case CURLE_USE_SSL_FAILED:
  166. return "Requested SSL level failed";
  167. case CURLE_SSL_SHUTDOWN_FAILED:
  168. return "Failed to shut down the SSL connection";
  169. case CURLE_SSL_CRL_BADFILE:
  170. return "Failed to load CRL file (path? access rights?, format?)";
  171. case CURLE_SSL_ISSUER_ERROR:
  172. return "Issuer check against peer certificate failed";
  173. case CURLE_SEND_FAIL_REWIND:
  174. return "Send failed since rewinding of the data stream failed";
  175. case CURLE_LOGIN_DENIED:
  176. return "Login denied";
  177. case CURLE_TFTP_NOTFOUND:
  178. return "TFTP: File Not Found";
  179. case CURLE_TFTP_PERM:
  180. return "TFTP: Access Violation";
  181. case CURLE_REMOTE_DISK_FULL:
  182. return "Disk full or allocation exceeded";
  183. case CURLE_TFTP_ILLEGAL:
  184. return "TFTP: Illegal operation";
  185. case CURLE_TFTP_UNKNOWNID:
  186. return "TFTP: Unknown transfer ID";
  187. case CURLE_REMOTE_FILE_EXISTS:
  188. return "Remote file already exists";
  189. case CURLE_TFTP_NOSUCHUSER:
  190. return "TFTP: No such user";
  191. case CURLE_REMOTE_FILE_NOT_FOUND:
  192. return "Remote file not found";
  193. case CURLE_SSH:
  194. return "Error in the SSH layer";
  195. case CURLE_AGAIN:
  196. return "Socket not ready for send/recv";
  197. case CURLE_RTSP_CSEQ_ERROR:
  198. return "RTSP CSeq mismatch or invalid CSeq";
  199. case CURLE_RTSP_SESSION_ERROR:
  200. return "RTSP session error";
  201. case CURLE_FTP_BAD_FILE_LIST:
  202. return "Unable to parse FTP file list";
  203. case CURLE_CHUNK_FAILED:
  204. return "Chunk callback failed";
  205. case CURLE_NO_CONNECTION_AVAILABLE:
  206. return "The max connection limit is reached";
  207. case CURLE_SSL_PINNEDPUBKEYNOTMATCH:
  208. return "SSL public key does not match pinned public key";
  209. case CURLE_SSL_INVALIDCERTSTATUS:
  210. return "SSL server certificate status verification FAILED";
  211. case CURLE_HTTP2_STREAM:
  212. return "Stream error in the HTTP/2 framing layer";
  213. case CURLE_RECURSIVE_API_CALL:
  214. return "API function called from within callback";
  215. case CURLE_AUTH_ERROR:
  216. return "An authentication function returned an error";
  217. case CURLE_HTTP3:
  218. return "HTTP/3 error";
  219. case CURLE_QUIC_CONNECT_ERROR:
  220. return "QUIC connection error";
  221. case CURLE_PROXY:
  222. return "proxy handshake error";
  223. case CURLE_SSL_CLIENTCERT:
  224. return "SSL Client Certificate required";
  225. case CURLE_UNRECOVERABLE_POLL:
  226. return "Unrecoverable error in select/poll";
  227. /* error codes not used by current libcurl */
  228. case CURLE_OBSOLETE20:
  229. case CURLE_OBSOLETE24:
  230. case CURLE_OBSOLETE29:
  231. case CURLE_OBSOLETE32:
  232. case CURLE_OBSOLETE40:
  233. case CURLE_OBSOLETE44:
  234. case CURLE_OBSOLETE46:
  235. case CURLE_OBSOLETE50:
  236. case CURLE_OBSOLETE51:
  237. case CURLE_OBSOLETE57:
  238. case CURLE_OBSOLETE62:
  239. case CURLE_OBSOLETE75:
  240. case CURLE_OBSOLETE76:
  241. case CURL_LAST:
  242. break;
  243. }
  244. /*
  245. * By using a switch, gcc -Wall will complain about enum values
  246. * which do not appear, helping keep this function up-to-date.
  247. * By using gcc -Wall -Werror, you can't forget.
  248. *
  249. * A table would not have the same benefit. Most compilers will
  250. * generate code very similar to a table in any case, so there
  251. * is little performance gain from a table. And something is broken
  252. * for the user's application, anyways, so does it matter how fast
  253. * it _doesn't_ work?
  254. *
  255. * The line number for the error will be near this comment, which
  256. * is why it is here, and not at the start of the switch.
  257. */
  258. return "Unknown error";
  259. #else
  260. if(!error)
  261. return "No error";
  262. else
  263. return "Error";
  264. #endif
  265. }
  266. const char *
  267. curl_multi_strerror(CURLMcode error)
  268. {
  269. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  270. switch(error) {
  271. case CURLM_CALL_MULTI_PERFORM:
  272. return "Please call curl_multi_perform() soon";
  273. case CURLM_OK:
  274. return "No error";
  275. case CURLM_BAD_HANDLE:
  276. return "Invalid multi handle";
  277. case CURLM_BAD_EASY_HANDLE:
  278. return "Invalid easy handle";
  279. case CURLM_OUT_OF_MEMORY:
  280. return "Out of memory";
  281. case CURLM_INTERNAL_ERROR:
  282. return "Internal error";
  283. case CURLM_BAD_SOCKET:
  284. return "Invalid socket argument";
  285. case CURLM_UNKNOWN_OPTION:
  286. return "Unknown option";
  287. case CURLM_ADDED_ALREADY:
  288. return "The easy handle is already added to a multi handle";
  289. case CURLM_RECURSIVE_API_CALL:
  290. return "API function called from within callback";
  291. case CURLM_WAKEUP_FAILURE:
  292. return "Wakeup is unavailable or failed";
  293. case CURLM_BAD_FUNCTION_ARGUMENT:
  294. return "A libcurl function was given a bad argument";
  295. case CURLM_ABORTED_BY_CALLBACK:
  296. return "Operation was aborted by an application callback";
  297. case CURLM_UNRECOVERABLE_POLL:
  298. return "Unrecoverable error in select/poll";
  299. case CURLM_LAST:
  300. break;
  301. }
  302. return "Unknown error";
  303. #else
  304. if(error == CURLM_OK)
  305. return "No error";
  306. else
  307. return "Error";
  308. #endif
  309. }
  310. const char *
  311. curl_share_strerror(CURLSHcode error)
  312. {
  313. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  314. switch(error) {
  315. case CURLSHE_OK:
  316. return "No error";
  317. case CURLSHE_BAD_OPTION:
  318. return "Unknown share option";
  319. case CURLSHE_IN_USE:
  320. return "Share currently in use";
  321. case CURLSHE_INVALID:
  322. return "Invalid share handle";
  323. case CURLSHE_NOMEM:
  324. return "Out of memory";
  325. case CURLSHE_NOT_BUILT_IN:
  326. return "Feature not enabled in this library";
  327. case CURLSHE_LAST:
  328. break;
  329. }
  330. return "CURLSHcode unknown";
  331. #else
  332. if(error == CURLSHE_OK)
  333. return "No error";
  334. else
  335. return "Error";
  336. #endif
  337. }
  338. const char *
  339. curl_url_strerror(CURLUcode error)
  340. {
  341. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  342. switch(error) {
  343. case CURLUE_OK:
  344. return "No error";
  345. case CURLUE_BAD_HANDLE:
  346. return "An invalid CURLU pointer was passed as argument";
  347. case CURLUE_BAD_PARTPOINTER:
  348. return "An invalid 'part' argument was passed as argument";
  349. case CURLUE_MALFORMED_INPUT:
  350. return "Malformed input to a URL function";
  351. case CURLUE_BAD_PORT_NUMBER:
  352. return "Port number was not a decimal number between 0 and 65535";
  353. case CURLUE_UNSUPPORTED_SCHEME:
  354. return "Unsupported URL scheme";
  355. case CURLUE_URLDECODE:
  356. return "URL decode error, most likely because of rubbish in the input";
  357. case CURLUE_OUT_OF_MEMORY:
  358. return "A memory function failed";
  359. case CURLUE_USER_NOT_ALLOWED:
  360. return "Credentials was passed in the URL when prohibited";
  361. case CURLUE_UNKNOWN_PART:
  362. return "An unknown part ID was passed to a URL API function";
  363. case CURLUE_NO_SCHEME:
  364. return "No scheme part in the URL";
  365. case CURLUE_NO_USER:
  366. return "No user part in the URL";
  367. case CURLUE_NO_PASSWORD:
  368. return "No password part in the URL";
  369. case CURLUE_NO_OPTIONS:
  370. return "No options part in the URL";
  371. case CURLUE_NO_HOST:
  372. return "No host part in the URL";
  373. case CURLUE_NO_PORT:
  374. return "No port part in the URL";
  375. case CURLUE_NO_QUERY:
  376. return "No query part in the URL";
  377. case CURLUE_NO_FRAGMENT:
  378. return "No fragment part in the URL";
  379. case CURLUE_NO_ZONEID:
  380. return "No zoneid part in the URL";
  381. case CURLUE_BAD_LOGIN:
  382. return "Bad login part";
  383. case CURLUE_BAD_IPV6:
  384. return "Bad IPv6 address";
  385. case CURLUE_BAD_HOSTNAME:
  386. return "Bad hostname";
  387. case CURLUE_BAD_FILE_URL:
  388. return "Bad file:// URL";
  389. case CURLUE_BAD_SLASHES:
  390. return "Unsupported number of slashes following scheme";
  391. case CURLUE_BAD_SCHEME:
  392. return "Bad scheme";
  393. case CURLUE_BAD_PATH:
  394. return "Bad path";
  395. case CURLUE_BAD_FRAGMENT:
  396. return "Bad fragment";
  397. case CURLUE_BAD_QUERY:
  398. return "Bad query";
  399. case CURLUE_BAD_PASSWORD:
  400. return "Bad password";
  401. case CURLUE_BAD_USER:
  402. return "Bad user";
  403. case CURLUE_LACKS_IDN:
  404. return "libcurl lacks IDN support";
  405. case CURLUE_LAST:
  406. break;
  407. }
  408. return "CURLUcode unknown";
  409. #else
  410. if(error == CURLUE_OK)
  411. return "No error";
  412. else
  413. return "Error";
  414. #endif
  415. }
  416. #ifdef USE_WINSOCK
  417. /* This is a helper function for Curl_strerror that converts Winsock error
  418. * codes (WSAGetLastError) to error messages.
  419. * Returns NULL if no error message was found for error code.
  420. */
  421. static const char *
  422. get_winsock_error (int err, char *buf, size_t len)
  423. {
  424. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  425. const char *p;
  426. #endif
  427. if(!len)
  428. return NULL;
  429. *buf = '\0';
  430. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  431. (void)err;
  432. return NULL;
  433. #else
  434. switch(err) {
  435. case WSAEINTR:
  436. p = "Call interrupted";
  437. break;
  438. case WSAEBADF:
  439. p = "Bad file";
  440. break;
  441. case WSAEACCES:
  442. p = "Bad access";
  443. break;
  444. case WSAEFAULT:
  445. p = "Bad argument";
  446. break;
  447. case WSAEINVAL:
  448. p = "Invalid arguments";
  449. break;
  450. case WSAEMFILE:
  451. p = "Out of file descriptors";
  452. break;
  453. case WSAEWOULDBLOCK:
  454. p = "Call would block";
  455. break;
  456. case WSAEINPROGRESS:
  457. case WSAEALREADY:
  458. p = "Blocking call in progress";
  459. break;
  460. case WSAENOTSOCK:
  461. p = "Descriptor is not a socket";
  462. break;
  463. case WSAEDESTADDRREQ:
  464. p = "Need destination address";
  465. break;
  466. case WSAEMSGSIZE:
  467. p = "Bad message size";
  468. break;
  469. case WSAEPROTOTYPE:
  470. p = "Bad protocol";
  471. break;
  472. case WSAENOPROTOOPT:
  473. p = "Protocol option is unsupported";
  474. break;
  475. case WSAEPROTONOSUPPORT:
  476. p = "Protocol is unsupported";
  477. break;
  478. case WSAESOCKTNOSUPPORT:
  479. p = "Socket is unsupported";
  480. break;
  481. case WSAEOPNOTSUPP:
  482. p = "Operation not supported";
  483. break;
  484. case WSAEAFNOSUPPORT:
  485. p = "Address family not supported";
  486. break;
  487. case WSAEPFNOSUPPORT:
  488. p = "Protocol family not supported";
  489. break;
  490. case WSAEADDRINUSE:
  491. p = "Address already in use";
  492. break;
  493. case WSAEADDRNOTAVAIL:
  494. p = "Address not available";
  495. break;
  496. case WSAENETDOWN:
  497. p = "Network down";
  498. break;
  499. case WSAENETUNREACH:
  500. p = "Network unreachable";
  501. break;
  502. case WSAENETRESET:
  503. p = "Network has been reset";
  504. break;
  505. case WSAECONNABORTED:
  506. p = "Connection was aborted";
  507. break;
  508. case WSAECONNRESET:
  509. p = "Connection was reset";
  510. break;
  511. case WSAENOBUFS:
  512. p = "No buffer space";
  513. break;
  514. case WSAEISCONN:
  515. p = "Socket is already connected";
  516. break;
  517. case WSAENOTCONN:
  518. p = "Socket is not connected";
  519. break;
  520. case WSAESHUTDOWN:
  521. p = "Socket has been shut down";
  522. break;
  523. case WSAETOOMANYREFS:
  524. p = "Too many references";
  525. break;
  526. case WSAETIMEDOUT:
  527. p = "Timed out";
  528. break;
  529. case WSAECONNREFUSED:
  530. p = "Connection refused";
  531. break;
  532. case WSAELOOP:
  533. p = "Loop??";
  534. break;
  535. case WSAENAMETOOLONG:
  536. p = "Name too long";
  537. break;
  538. case WSAEHOSTDOWN:
  539. p = "Host down";
  540. break;
  541. case WSAEHOSTUNREACH:
  542. p = "Host unreachable";
  543. break;
  544. case WSAENOTEMPTY:
  545. p = "Not empty";
  546. break;
  547. case WSAEPROCLIM:
  548. p = "Process limit reached";
  549. break;
  550. case WSAEUSERS:
  551. p = "Too many users";
  552. break;
  553. case WSAEDQUOT:
  554. p = "Bad quota";
  555. break;
  556. case WSAESTALE:
  557. p = "Something is stale";
  558. break;
  559. case WSAEREMOTE:
  560. p = "Remote error";
  561. break;
  562. #ifdef WSAEDISCON /* missing in SalfordC! */
  563. case WSAEDISCON:
  564. p = "Disconnected";
  565. break;
  566. #endif
  567. /* Extended Winsock errors */
  568. case WSASYSNOTREADY:
  569. p = "Winsock library is not ready";
  570. break;
  571. case WSANOTINITIALISED:
  572. p = "Winsock library not initialised";
  573. break;
  574. case WSAVERNOTSUPPORTED:
  575. p = "Winsock version not supported";
  576. break;
  577. /* getXbyY() errors (already handled in herrmsg):
  578. * Authoritative Answer: Host not found */
  579. case WSAHOST_NOT_FOUND:
  580. p = "Host not found";
  581. break;
  582. /* Non-Authoritative: Host not found, or SERVERFAIL */
  583. case WSATRY_AGAIN:
  584. p = "Host not found, try again";
  585. break;
  586. /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
  587. case WSANO_RECOVERY:
  588. p = "Unrecoverable error in call to nameserver";
  589. break;
  590. /* Valid name, no data record of requested type */
  591. case WSANO_DATA:
  592. p = "No data record of requested type";
  593. break;
  594. default:
  595. return NULL;
  596. }
  597. strncpy(buf, p, len);
  598. buf [len-1] = '\0';
  599. return buf;
  600. #endif
  601. }
  602. #endif /* USE_WINSOCK */
  603. #if defined(WIN32) || defined(_WIN32_WCE)
  604. /* This is a helper function for Curl_strerror that converts Windows API error
  605. * codes (GetLastError) to error messages.
  606. * Returns NULL if no error message was found for error code.
  607. */
  608. static const char *
  609. get_winapi_error(int err, char *buf, size_t buflen)
  610. {
  611. char *p;
  612. wchar_t wbuf[256];
  613. if(!buflen)
  614. return NULL;
  615. *buf = '\0';
  616. *wbuf = L'\0';
  617. /* We return the local codepage version of the error string because if it is
  618. output to the user's terminal it will likely be with functions which
  619. expect the local codepage (eg fprintf, failf, infof).
  620. FormatMessageW -> wcstombs is used for Windows CE compatibility. */
  621. if(FormatMessageW((FORMAT_MESSAGE_FROM_SYSTEM |
  622. FORMAT_MESSAGE_IGNORE_INSERTS), NULL, err,
  623. LANG_NEUTRAL, wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL)) {
  624. size_t written = wcstombs(buf, wbuf, buflen - 1);
  625. if(written != (size_t)-1)
  626. buf[written] = '\0';
  627. else
  628. *buf = '\0';
  629. }
  630. /* Truncate multiple lines */
  631. p = strchr(buf, '\n');
  632. if(p) {
  633. if(p > buf && *(p-1) == '\r')
  634. *(p-1) = '\0';
  635. else
  636. *p = '\0';
  637. }
  638. return (*buf ? buf : NULL);
  639. }
  640. #endif /* WIN32 || _WIN32_WCE */
  641. /*
  642. * Our thread-safe and smart strerror() replacement.
  643. *
  644. * The 'err' argument passed in to this function MUST be a true errno number
  645. * as reported on this system. We do no range checking on the number before
  646. * we pass it to the "number-to-message" conversion function and there might
  647. * be systems that don't do proper range checking in there themselves.
  648. *
  649. * We don't do range checking (on systems other than Windows) since there is
  650. * no good reliable and portable way to do it.
  651. *
  652. * On Windows different types of error codes overlap. This function has an
  653. * order of preference when trying to match error codes:
  654. * CRT (errno), Winsock (WSAGetLastError), Windows API (GetLastError).
  655. *
  656. * It may be more correct to call one of the variant functions instead:
  657. * Call Curl_sspi_strerror if the error code is definitely Windows SSPI.
  658. * Call Curl_winapi_strerror if the error code is definitely Windows API.
  659. */
  660. const char *Curl_strerror(int err, char *buf, size_t buflen)
  661. {
  662. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  663. DWORD old_win_err = GetLastError();
  664. #endif
  665. int old_errno = errno;
  666. char *p;
  667. size_t max;
  668. if(!buflen)
  669. return NULL;
  670. #ifndef WIN32
  671. DEBUGASSERT(err >= 0);
  672. #endif
  673. max = buflen - 1;
  674. *buf = '\0';
  675. #if defined(WIN32) || defined(_WIN32_WCE)
  676. #if defined(WIN32)
  677. /* 'sys_nerr' is the maximum errno number, it is not widely portable */
  678. if(err >= 0 && err < sys_nerr)
  679. strncpy(buf, sys_errlist[err], max);
  680. else
  681. #endif
  682. {
  683. if(
  684. #ifdef USE_WINSOCK
  685. !get_winsock_error(err, buf, max) &&
  686. #endif
  687. !get_winapi_error((DWORD)err, buf, max))
  688. msnprintf(buf, max, "Unknown error %d (%#x)", err, err);
  689. }
  690. #else /* not Windows coming up */
  691. #if defined(HAVE_STRERROR_R) && defined(HAVE_POSIX_STRERROR_R)
  692. /*
  693. * The POSIX-style strerror_r() may set errno to ERANGE if insufficient
  694. * storage is supplied via 'strerrbuf' and 'buflen' to hold the generated
  695. * message string, or EINVAL if 'errnum' is not a valid error number.
  696. */
  697. if(0 != strerror_r(err, buf, max)) {
  698. if('\0' == buf[0])
  699. msnprintf(buf, max, "Unknown error %d", err);
  700. }
  701. #elif defined(HAVE_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R)
  702. /*
  703. * The glibc-style strerror_r() only *might* use the buffer we pass to
  704. * the function, but it always returns the error message as a pointer,
  705. * so we must copy that string unconditionally (if non-NULL).
  706. */
  707. {
  708. char buffer[256];
  709. char *msg = strerror_r(err, buffer, sizeof(buffer));
  710. if(msg)
  711. strncpy(buf, msg, max);
  712. else
  713. msnprintf(buf, max, "Unknown error %d", err);
  714. }
  715. #else
  716. {
  717. /* !checksrc! disable STRERROR 1 */
  718. const char *msg = strerror(err);
  719. if(msg)
  720. strncpy(buf, msg, max);
  721. else
  722. msnprintf(buf, max, "Unknown error %d", err);
  723. }
  724. #endif
  725. #endif /* end of not Windows */
  726. buf[max] = '\0'; /* make sure the string is null-terminated */
  727. /* strip trailing '\r\n' or '\n'. */
  728. p = strrchr(buf, '\n');
  729. if(p && (p - buf) >= 2)
  730. *p = '\0';
  731. p = strrchr(buf, '\r');
  732. if(p && (p - buf) >= 1)
  733. *p = '\0';
  734. if(errno != old_errno)
  735. errno = old_errno;
  736. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  737. if(old_win_err != GetLastError())
  738. SetLastError(old_win_err);
  739. #endif
  740. return buf;
  741. }
  742. /*
  743. * Curl_winapi_strerror:
  744. * Variant of Curl_strerror if the error code is definitely Windows API.
  745. */
  746. #if defined(WIN32) || defined(_WIN32_WCE)
  747. const char *Curl_winapi_strerror(DWORD err, char *buf, size_t buflen)
  748. {
  749. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  750. DWORD old_win_err = GetLastError();
  751. #endif
  752. int old_errno = errno;
  753. if(!buflen)
  754. return NULL;
  755. *buf = '\0';
  756. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  757. if(!get_winapi_error(err, buf, buflen)) {
  758. msnprintf(buf, buflen, "Unknown error %u (0x%08X)", err, err);
  759. }
  760. #else
  761. {
  762. const char *txt = (err == ERROR_SUCCESS) ? "No error" : "Error";
  763. strncpy(buf, txt, buflen);
  764. buf[buflen - 1] = '\0';
  765. }
  766. #endif
  767. if(errno != old_errno)
  768. errno = old_errno;
  769. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  770. if(old_win_err != GetLastError())
  771. SetLastError(old_win_err);
  772. #endif
  773. return buf;
  774. }
  775. #endif /* WIN32 || _WIN32_WCE */
  776. #ifdef USE_WINDOWS_SSPI
  777. /*
  778. * Curl_sspi_strerror:
  779. * Variant of Curl_strerror if the error code is definitely Windows SSPI.
  780. */
  781. const char *Curl_sspi_strerror(int err, char *buf, size_t buflen)
  782. {
  783. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  784. DWORD old_win_err = GetLastError();
  785. #endif
  786. int old_errno = errno;
  787. const char *txt;
  788. if(!buflen)
  789. return NULL;
  790. *buf = '\0';
  791. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  792. switch(err) {
  793. case SEC_E_OK:
  794. txt = "No error";
  795. break;
  796. #define SEC2TXT(sec) case sec: txt = #sec; break
  797. SEC2TXT(CRYPT_E_REVOKED);
  798. SEC2TXT(SEC_E_ALGORITHM_MISMATCH);
  799. SEC2TXT(SEC_E_BAD_BINDINGS);
  800. SEC2TXT(SEC_E_BAD_PKGID);
  801. SEC2TXT(SEC_E_BUFFER_TOO_SMALL);
  802. SEC2TXT(SEC_E_CANNOT_INSTALL);
  803. SEC2TXT(SEC_E_CANNOT_PACK);
  804. SEC2TXT(SEC_E_CERT_EXPIRED);
  805. SEC2TXT(SEC_E_CERT_UNKNOWN);
  806. SEC2TXT(SEC_E_CERT_WRONG_USAGE);
  807. SEC2TXT(SEC_E_CONTEXT_EXPIRED);
  808. SEC2TXT(SEC_E_CROSSREALM_DELEGATION_FAILURE);
  809. SEC2TXT(SEC_E_CRYPTO_SYSTEM_INVALID);
  810. SEC2TXT(SEC_E_DECRYPT_FAILURE);
  811. SEC2TXT(SEC_E_DELEGATION_POLICY);
  812. SEC2TXT(SEC_E_DELEGATION_REQUIRED);
  813. SEC2TXT(SEC_E_DOWNGRADE_DETECTED);
  814. SEC2TXT(SEC_E_ENCRYPT_FAILURE);
  815. SEC2TXT(SEC_E_ILLEGAL_MESSAGE);
  816. SEC2TXT(SEC_E_INCOMPLETE_CREDENTIALS);
  817. SEC2TXT(SEC_E_INCOMPLETE_MESSAGE);
  818. SEC2TXT(SEC_E_INSUFFICIENT_MEMORY);
  819. SEC2TXT(SEC_E_INTERNAL_ERROR);
  820. SEC2TXT(SEC_E_INVALID_HANDLE);
  821. SEC2TXT(SEC_E_INVALID_PARAMETER);
  822. SEC2TXT(SEC_E_INVALID_TOKEN);
  823. SEC2TXT(SEC_E_ISSUING_CA_UNTRUSTED);
  824. SEC2TXT(SEC_E_ISSUING_CA_UNTRUSTED_KDC);
  825. SEC2TXT(SEC_E_KDC_CERT_EXPIRED);
  826. SEC2TXT(SEC_E_KDC_CERT_REVOKED);
  827. SEC2TXT(SEC_E_KDC_INVALID_REQUEST);
  828. SEC2TXT(SEC_E_KDC_UNABLE_TO_REFER);
  829. SEC2TXT(SEC_E_KDC_UNKNOWN_ETYPE);
  830. SEC2TXT(SEC_E_LOGON_DENIED);
  831. SEC2TXT(SEC_E_MAX_REFERRALS_EXCEEDED);
  832. SEC2TXT(SEC_E_MESSAGE_ALTERED);
  833. SEC2TXT(SEC_E_MULTIPLE_ACCOUNTS);
  834. SEC2TXT(SEC_E_MUST_BE_KDC);
  835. SEC2TXT(SEC_E_NOT_OWNER);
  836. SEC2TXT(SEC_E_NO_AUTHENTICATING_AUTHORITY);
  837. SEC2TXT(SEC_E_NO_CREDENTIALS);
  838. SEC2TXT(SEC_E_NO_IMPERSONATION);
  839. SEC2TXT(SEC_E_NO_IP_ADDRESSES);
  840. SEC2TXT(SEC_E_NO_KERB_KEY);
  841. SEC2TXT(SEC_E_NO_PA_DATA);
  842. SEC2TXT(SEC_E_NO_S4U_PROT_SUPPORT);
  843. SEC2TXT(SEC_E_NO_TGT_REPLY);
  844. SEC2TXT(SEC_E_OUT_OF_SEQUENCE);
  845. SEC2TXT(SEC_E_PKINIT_CLIENT_FAILURE);
  846. SEC2TXT(SEC_E_PKINIT_NAME_MISMATCH);
  847. SEC2TXT(SEC_E_POLICY_NLTM_ONLY);
  848. SEC2TXT(SEC_E_QOP_NOT_SUPPORTED);
  849. SEC2TXT(SEC_E_REVOCATION_OFFLINE_C);
  850. SEC2TXT(SEC_E_REVOCATION_OFFLINE_KDC);
  851. SEC2TXT(SEC_E_SECPKG_NOT_FOUND);
  852. SEC2TXT(SEC_E_SECURITY_QOS_FAILED);
  853. SEC2TXT(SEC_E_SHUTDOWN_IN_PROGRESS);
  854. SEC2TXT(SEC_E_SMARTCARD_CERT_EXPIRED);
  855. SEC2TXT(SEC_E_SMARTCARD_CERT_REVOKED);
  856. SEC2TXT(SEC_E_SMARTCARD_LOGON_REQUIRED);
  857. SEC2TXT(SEC_E_STRONG_CRYPTO_NOT_SUPPORTED);
  858. SEC2TXT(SEC_E_TARGET_UNKNOWN);
  859. SEC2TXT(SEC_E_TIME_SKEW);
  860. SEC2TXT(SEC_E_TOO_MANY_PRINCIPALS);
  861. SEC2TXT(SEC_E_UNFINISHED_CONTEXT_DELETED);
  862. SEC2TXT(SEC_E_UNKNOWN_CREDENTIALS);
  863. SEC2TXT(SEC_E_UNSUPPORTED_FUNCTION);
  864. SEC2TXT(SEC_E_UNSUPPORTED_PREAUTH);
  865. SEC2TXT(SEC_E_UNTRUSTED_ROOT);
  866. SEC2TXT(SEC_E_WRONG_CREDENTIAL_HANDLE);
  867. SEC2TXT(SEC_E_WRONG_PRINCIPAL);
  868. SEC2TXT(SEC_I_COMPLETE_AND_CONTINUE);
  869. SEC2TXT(SEC_I_COMPLETE_NEEDED);
  870. SEC2TXT(SEC_I_CONTEXT_EXPIRED);
  871. SEC2TXT(SEC_I_CONTINUE_NEEDED);
  872. SEC2TXT(SEC_I_INCOMPLETE_CREDENTIALS);
  873. SEC2TXT(SEC_I_LOCAL_LOGON);
  874. SEC2TXT(SEC_I_NO_LSA_CONTEXT);
  875. SEC2TXT(SEC_I_RENEGOTIATE);
  876. SEC2TXT(SEC_I_SIGNATURE_NEEDED);
  877. default:
  878. txt = "Unknown error";
  879. }
  880. if(err == SEC_E_ILLEGAL_MESSAGE) {
  881. msnprintf(buf, buflen,
  882. "SEC_E_ILLEGAL_MESSAGE (0x%08X) - This error usually occurs "
  883. "when a fatal SSL/TLS alert is received (e.g. handshake failed)."
  884. " More detail may be available in the Windows System event log.",
  885. err);
  886. }
  887. else {
  888. char txtbuf[80];
  889. char msgbuf[256];
  890. msnprintf(txtbuf, sizeof(txtbuf), "%s (0x%08X)", txt, err);
  891. if(get_winapi_error(err, msgbuf, sizeof(msgbuf)))
  892. msnprintf(buf, buflen, "%s - %s", txtbuf, msgbuf);
  893. else {
  894. strncpy(buf, txtbuf, buflen);
  895. buf[buflen - 1] = '\0';
  896. }
  897. }
  898. #else
  899. if(err == SEC_E_OK)
  900. txt = "No error";
  901. else
  902. txt = "Error";
  903. strncpy(buf, txt, buflen);
  904. buf[buflen - 1] = '\0';
  905. #endif
  906. if(errno != old_errno)
  907. errno = old_errno;
  908. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  909. if(old_win_err != GetLastError())
  910. SetLastError(old_win_err);
  911. #endif
  912. return buf;
  913. }
  914. #endif /* USE_WINDOWS_SSPI */