strerror.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2004 - 2022, 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_LAST:
  404. break;
  405. }
  406. return "CURLUcode unknown";
  407. #else
  408. if(error == CURLUE_OK)
  409. return "No error";
  410. else
  411. return "Error";
  412. #endif
  413. }
  414. #ifdef USE_WINSOCK
  415. /* This is a helper function for Curl_strerror that converts Winsock error
  416. * codes (WSAGetLastError) to error messages.
  417. * Returns NULL if no error message was found for error code.
  418. */
  419. static const char *
  420. get_winsock_error (int err, char *buf, size_t len)
  421. {
  422. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  423. const char *p;
  424. #endif
  425. if(!len)
  426. return NULL;
  427. *buf = '\0';
  428. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  429. (void)err;
  430. return NULL;
  431. #else
  432. switch(err) {
  433. case WSAEINTR:
  434. p = "Call interrupted";
  435. break;
  436. case WSAEBADF:
  437. p = "Bad file";
  438. break;
  439. case WSAEACCES:
  440. p = "Bad access";
  441. break;
  442. case WSAEFAULT:
  443. p = "Bad argument";
  444. break;
  445. case WSAEINVAL:
  446. p = "Invalid arguments";
  447. break;
  448. case WSAEMFILE:
  449. p = "Out of file descriptors";
  450. break;
  451. case WSAEWOULDBLOCK:
  452. p = "Call would block";
  453. break;
  454. case WSAEINPROGRESS:
  455. case WSAEALREADY:
  456. p = "Blocking call in progress";
  457. break;
  458. case WSAENOTSOCK:
  459. p = "Descriptor is not a socket";
  460. break;
  461. case WSAEDESTADDRREQ:
  462. p = "Need destination address";
  463. break;
  464. case WSAEMSGSIZE:
  465. p = "Bad message size";
  466. break;
  467. case WSAEPROTOTYPE:
  468. p = "Bad protocol";
  469. break;
  470. case WSAENOPROTOOPT:
  471. p = "Protocol option is unsupported";
  472. break;
  473. case WSAEPROTONOSUPPORT:
  474. p = "Protocol is unsupported";
  475. break;
  476. case WSAESOCKTNOSUPPORT:
  477. p = "Socket is unsupported";
  478. break;
  479. case WSAEOPNOTSUPP:
  480. p = "Operation not supported";
  481. break;
  482. case WSAEAFNOSUPPORT:
  483. p = "Address family not supported";
  484. break;
  485. case WSAEPFNOSUPPORT:
  486. p = "Protocol family not supported";
  487. break;
  488. case WSAEADDRINUSE:
  489. p = "Address already in use";
  490. break;
  491. case WSAEADDRNOTAVAIL:
  492. p = "Address not available";
  493. break;
  494. case WSAENETDOWN:
  495. p = "Network down";
  496. break;
  497. case WSAENETUNREACH:
  498. p = "Network unreachable";
  499. break;
  500. case WSAENETRESET:
  501. p = "Network has been reset";
  502. break;
  503. case WSAECONNABORTED:
  504. p = "Connection was aborted";
  505. break;
  506. case WSAECONNRESET:
  507. p = "Connection was reset";
  508. break;
  509. case WSAENOBUFS:
  510. p = "No buffer space";
  511. break;
  512. case WSAEISCONN:
  513. p = "Socket is already connected";
  514. break;
  515. case WSAENOTCONN:
  516. p = "Socket is not connected";
  517. break;
  518. case WSAESHUTDOWN:
  519. p = "Socket has been shut down";
  520. break;
  521. case WSAETOOMANYREFS:
  522. p = "Too many references";
  523. break;
  524. case WSAETIMEDOUT:
  525. p = "Timed out";
  526. break;
  527. case WSAECONNREFUSED:
  528. p = "Connection refused";
  529. break;
  530. case WSAELOOP:
  531. p = "Loop??";
  532. break;
  533. case WSAENAMETOOLONG:
  534. p = "Name too long";
  535. break;
  536. case WSAEHOSTDOWN:
  537. p = "Host down";
  538. break;
  539. case WSAEHOSTUNREACH:
  540. p = "Host unreachable";
  541. break;
  542. case WSAENOTEMPTY:
  543. p = "Not empty";
  544. break;
  545. case WSAEPROCLIM:
  546. p = "Process limit reached";
  547. break;
  548. case WSAEUSERS:
  549. p = "Too many users";
  550. break;
  551. case WSAEDQUOT:
  552. p = "Bad quota";
  553. break;
  554. case WSAESTALE:
  555. p = "Something is stale";
  556. break;
  557. case WSAEREMOTE:
  558. p = "Remote error";
  559. break;
  560. #ifdef WSAEDISCON /* missing in SalfordC! */
  561. case WSAEDISCON:
  562. p = "Disconnected";
  563. break;
  564. #endif
  565. /* Extended Winsock errors */
  566. case WSASYSNOTREADY:
  567. p = "Winsock library is not ready";
  568. break;
  569. case WSANOTINITIALISED:
  570. p = "Winsock library not initialised";
  571. break;
  572. case WSAVERNOTSUPPORTED:
  573. p = "Winsock version not supported";
  574. break;
  575. /* getXbyY() errors (already handled in herrmsg):
  576. * Authoritative Answer: Host not found */
  577. case WSAHOST_NOT_FOUND:
  578. p = "Host not found";
  579. break;
  580. /* Non-Authoritative: Host not found, or SERVERFAIL */
  581. case WSATRY_AGAIN:
  582. p = "Host not found, try again";
  583. break;
  584. /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
  585. case WSANO_RECOVERY:
  586. p = "Unrecoverable error in call to nameserver";
  587. break;
  588. /* Valid name, no data record of requested type */
  589. case WSANO_DATA:
  590. p = "No data record of requested type";
  591. break;
  592. default:
  593. return NULL;
  594. }
  595. strncpy(buf, p, len);
  596. buf [len-1] = '\0';
  597. return buf;
  598. #endif
  599. }
  600. #endif /* USE_WINSOCK */
  601. #if defined(WIN32) || defined(_WIN32_WCE)
  602. /* This is a helper function for Curl_strerror that converts Windows API error
  603. * codes (GetLastError) to error messages.
  604. * Returns NULL if no error message was found for error code.
  605. */
  606. static const char *
  607. get_winapi_error(int err, char *buf, size_t buflen)
  608. {
  609. char *p;
  610. wchar_t wbuf[256];
  611. if(!buflen)
  612. return NULL;
  613. *buf = '\0';
  614. *wbuf = L'\0';
  615. /* We return the local codepage version of the error string because if it is
  616. output to the user's terminal it will likely be with functions which
  617. expect the local codepage (eg fprintf, failf, infof).
  618. FormatMessageW -> wcstombs is used for Windows CE compatibility. */
  619. if(FormatMessageW((FORMAT_MESSAGE_FROM_SYSTEM |
  620. FORMAT_MESSAGE_IGNORE_INSERTS), NULL, err,
  621. LANG_NEUTRAL, wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL)) {
  622. size_t written = wcstombs(buf, wbuf, buflen - 1);
  623. if(written != (size_t)-1)
  624. buf[written] = '\0';
  625. else
  626. *buf = '\0';
  627. }
  628. /* Truncate multiple lines */
  629. p = strchr(buf, '\n');
  630. if(p) {
  631. if(p > buf && *(p-1) == '\r')
  632. *(p-1) = '\0';
  633. else
  634. *p = '\0';
  635. }
  636. return (*buf ? buf : NULL);
  637. }
  638. #endif /* WIN32 || _WIN32_WCE */
  639. /*
  640. * Our thread-safe and smart strerror() replacement.
  641. *
  642. * The 'err' argument passed in to this function MUST be a true errno number
  643. * as reported on this system. We do no range checking on the number before
  644. * we pass it to the "number-to-message" conversion function and there might
  645. * be systems that don't do proper range checking in there themselves.
  646. *
  647. * We don't do range checking (on systems other than Windows) since there is
  648. * no good reliable and portable way to do it.
  649. *
  650. * On Windows different types of error codes overlap. This function has an
  651. * order of preference when trying to match error codes:
  652. * CRT (errno), Winsock (WSAGetLastError), Windows API (GetLastError).
  653. *
  654. * It may be more correct to call one of the variant functions instead:
  655. * Call Curl_sspi_strerror if the error code is definitely Windows SSPI.
  656. * Call Curl_winapi_strerror if the error code is definitely Windows API.
  657. */
  658. const char *Curl_strerror(int err, char *buf, size_t buflen)
  659. {
  660. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  661. DWORD old_win_err = GetLastError();
  662. #endif
  663. int old_errno = errno;
  664. char *p;
  665. size_t max;
  666. if(!buflen)
  667. return NULL;
  668. #ifndef WIN32
  669. DEBUGASSERT(err >= 0);
  670. #endif
  671. max = buflen - 1;
  672. *buf = '\0';
  673. #if defined(WIN32) || defined(_WIN32_WCE)
  674. #if defined(WIN32)
  675. /* 'sys_nerr' is the maximum errno number, it is not widely portable */
  676. if(err >= 0 && err < sys_nerr)
  677. strncpy(buf, sys_errlist[err], max);
  678. else
  679. #endif
  680. {
  681. if(
  682. #ifdef USE_WINSOCK
  683. !get_winsock_error(err, buf, max) &&
  684. #endif
  685. !get_winapi_error((DWORD)err, buf, max))
  686. msnprintf(buf, max, "Unknown error %d (%#x)", err, err);
  687. }
  688. #else /* not Windows coming up */
  689. #if defined(HAVE_STRERROR_R) && defined(HAVE_POSIX_STRERROR_R)
  690. /*
  691. * The POSIX-style strerror_r() may set errno to ERANGE if insufficient
  692. * storage is supplied via 'strerrbuf' and 'buflen' to hold the generated
  693. * message string, or EINVAL if 'errnum' is not a valid error number.
  694. */
  695. if(0 != strerror_r(err, buf, max)) {
  696. if('\0' == buf[0])
  697. msnprintf(buf, max, "Unknown error %d", err);
  698. }
  699. #elif defined(HAVE_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R)
  700. /*
  701. * The glibc-style strerror_r() only *might* use the buffer we pass to
  702. * the function, but it always returns the error message as a pointer,
  703. * so we must copy that string unconditionally (if non-NULL).
  704. */
  705. {
  706. char buffer[256];
  707. char *msg = strerror_r(err, buffer, sizeof(buffer));
  708. if(msg)
  709. strncpy(buf, msg, max);
  710. else
  711. msnprintf(buf, max, "Unknown error %d", err);
  712. }
  713. #else
  714. {
  715. /* !checksrc! disable STRERROR 1 */
  716. const char *msg = strerror(err);
  717. if(msg)
  718. strncpy(buf, msg, max);
  719. else
  720. msnprintf(buf, max, "Unknown error %d", err);
  721. }
  722. #endif
  723. #endif /* end of not Windows */
  724. buf[max] = '\0'; /* make sure the string is null-terminated */
  725. /* strip trailing '\r\n' or '\n'. */
  726. p = strrchr(buf, '\n');
  727. if(p && (p - buf) >= 2)
  728. *p = '\0';
  729. p = strrchr(buf, '\r');
  730. if(p && (p - buf) >= 1)
  731. *p = '\0';
  732. if(errno != old_errno)
  733. errno = old_errno;
  734. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  735. if(old_win_err != GetLastError())
  736. SetLastError(old_win_err);
  737. #endif
  738. return buf;
  739. }
  740. /*
  741. * Curl_winapi_strerror:
  742. * Variant of Curl_strerror if the error code is definitely Windows API.
  743. */
  744. #if defined(WIN32) || defined(_WIN32_WCE)
  745. const char *Curl_winapi_strerror(DWORD err, char *buf, size_t buflen)
  746. {
  747. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  748. DWORD old_win_err = GetLastError();
  749. #endif
  750. int old_errno = errno;
  751. if(!buflen)
  752. return NULL;
  753. *buf = '\0';
  754. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  755. if(!get_winapi_error(err, buf, buflen)) {
  756. msnprintf(buf, buflen, "Unknown error %u (0x%08X)", err, err);
  757. }
  758. #else
  759. {
  760. const char *txt = (err == ERROR_SUCCESS) ? "No error" : "Error";
  761. strncpy(buf, txt, buflen);
  762. buf[buflen - 1] = '\0';
  763. }
  764. #endif
  765. if(errno != old_errno)
  766. errno = old_errno;
  767. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  768. if(old_win_err != GetLastError())
  769. SetLastError(old_win_err);
  770. #endif
  771. return buf;
  772. }
  773. #endif /* WIN32 || _WIN32_WCE */
  774. #ifdef USE_WINDOWS_SSPI
  775. /*
  776. * Curl_sspi_strerror:
  777. * Variant of Curl_strerror if the error code is definitely Windows SSPI.
  778. */
  779. const char *Curl_sspi_strerror(int err, char *buf, size_t buflen)
  780. {
  781. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  782. DWORD old_win_err = GetLastError();
  783. #endif
  784. int old_errno = errno;
  785. const char *txt;
  786. if(!buflen)
  787. return NULL;
  788. *buf = '\0';
  789. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  790. switch(err) {
  791. case SEC_E_OK:
  792. txt = "No error";
  793. break;
  794. #define SEC2TXT(sec) case sec: txt = #sec; break
  795. SEC2TXT(CRYPT_E_REVOKED);
  796. SEC2TXT(SEC_E_ALGORITHM_MISMATCH);
  797. SEC2TXT(SEC_E_BAD_BINDINGS);
  798. SEC2TXT(SEC_E_BAD_PKGID);
  799. SEC2TXT(SEC_E_BUFFER_TOO_SMALL);
  800. SEC2TXT(SEC_E_CANNOT_INSTALL);
  801. SEC2TXT(SEC_E_CANNOT_PACK);
  802. SEC2TXT(SEC_E_CERT_EXPIRED);
  803. SEC2TXT(SEC_E_CERT_UNKNOWN);
  804. SEC2TXT(SEC_E_CERT_WRONG_USAGE);
  805. SEC2TXT(SEC_E_CONTEXT_EXPIRED);
  806. SEC2TXT(SEC_E_CROSSREALM_DELEGATION_FAILURE);
  807. SEC2TXT(SEC_E_CRYPTO_SYSTEM_INVALID);
  808. SEC2TXT(SEC_E_DECRYPT_FAILURE);
  809. SEC2TXT(SEC_E_DELEGATION_POLICY);
  810. SEC2TXT(SEC_E_DELEGATION_REQUIRED);
  811. SEC2TXT(SEC_E_DOWNGRADE_DETECTED);
  812. SEC2TXT(SEC_E_ENCRYPT_FAILURE);
  813. SEC2TXT(SEC_E_ILLEGAL_MESSAGE);
  814. SEC2TXT(SEC_E_INCOMPLETE_CREDENTIALS);
  815. SEC2TXT(SEC_E_INCOMPLETE_MESSAGE);
  816. SEC2TXT(SEC_E_INSUFFICIENT_MEMORY);
  817. SEC2TXT(SEC_E_INTERNAL_ERROR);
  818. SEC2TXT(SEC_E_INVALID_HANDLE);
  819. SEC2TXT(SEC_E_INVALID_PARAMETER);
  820. SEC2TXT(SEC_E_INVALID_TOKEN);
  821. SEC2TXT(SEC_E_ISSUING_CA_UNTRUSTED);
  822. SEC2TXT(SEC_E_ISSUING_CA_UNTRUSTED_KDC);
  823. SEC2TXT(SEC_E_KDC_CERT_EXPIRED);
  824. SEC2TXT(SEC_E_KDC_CERT_REVOKED);
  825. SEC2TXT(SEC_E_KDC_INVALID_REQUEST);
  826. SEC2TXT(SEC_E_KDC_UNABLE_TO_REFER);
  827. SEC2TXT(SEC_E_KDC_UNKNOWN_ETYPE);
  828. SEC2TXT(SEC_E_LOGON_DENIED);
  829. SEC2TXT(SEC_E_MAX_REFERRALS_EXCEEDED);
  830. SEC2TXT(SEC_E_MESSAGE_ALTERED);
  831. SEC2TXT(SEC_E_MULTIPLE_ACCOUNTS);
  832. SEC2TXT(SEC_E_MUST_BE_KDC);
  833. SEC2TXT(SEC_E_NOT_OWNER);
  834. SEC2TXT(SEC_E_NO_AUTHENTICATING_AUTHORITY);
  835. SEC2TXT(SEC_E_NO_CREDENTIALS);
  836. SEC2TXT(SEC_E_NO_IMPERSONATION);
  837. SEC2TXT(SEC_E_NO_IP_ADDRESSES);
  838. SEC2TXT(SEC_E_NO_KERB_KEY);
  839. SEC2TXT(SEC_E_NO_PA_DATA);
  840. SEC2TXT(SEC_E_NO_S4U_PROT_SUPPORT);
  841. SEC2TXT(SEC_E_NO_TGT_REPLY);
  842. SEC2TXT(SEC_E_OUT_OF_SEQUENCE);
  843. SEC2TXT(SEC_E_PKINIT_CLIENT_FAILURE);
  844. SEC2TXT(SEC_E_PKINIT_NAME_MISMATCH);
  845. SEC2TXT(SEC_E_POLICY_NLTM_ONLY);
  846. SEC2TXT(SEC_E_QOP_NOT_SUPPORTED);
  847. SEC2TXT(SEC_E_REVOCATION_OFFLINE_C);
  848. SEC2TXT(SEC_E_REVOCATION_OFFLINE_KDC);
  849. SEC2TXT(SEC_E_SECPKG_NOT_FOUND);
  850. SEC2TXT(SEC_E_SECURITY_QOS_FAILED);
  851. SEC2TXT(SEC_E_SHUTDOWN_IN_PROGRESS);
  852. SEC2TXT(SEC_E_SMARTCARD_CERT_EXPIRED);
  853. SEC2TXT(SEC_E_SMARTCARD_CERT_REVOKED);
  854. SEC2TXT(SEC_E_SMARTCARD_LOGON_REQUIRED);
  855. SEC2TXT(SEC_E_STRONG_CRYPTO_NOT_SUPPORTED);
  856. SEC2TXT(SEC_E_TARGET_UNKNOWN);
  857. SEC2TXT(SEC_E_TIME_SKEW);
  858. SEC2TXT(SEC_E_TOO_MANY_PRINCIPALS);
  859. SEC2TXT(SEC_E_UNFINISHED_CONTEXT_DELETED);
  860. SEC2TXT(SEC_E_UNKNOWN_CREDENTIALS);
  861. SEC2TXT(SEC_E_UNSUPPORTED_FUNCTION);
  862. SEC2TXT(SEC_E_UNSUPPORTED_PREAUTH);
  863. SEC2TXT(SEC_E_UNTRUSTED_ROOT);
  864. SEC2TXT(SEC_E_WRONG_CREDENTIAL_HANDLE);
  865. SEC2TXT(SEC_E_WRONG_PRINCIPAL);
  866. SEC2TXT(SEC_I_COMPLETE_AND_CONTINUE);
  867. SEC2TXT(SEC_I_COMPLETE_NEEDED);
  868. SEC2TXT(SEC_I_CONTEXT_EXPIRED);
  869. SEC2TXT(SEC_I_CONTINUE_NEEDED);
  870. SEC2TXT(SEC_I_INCOMPLETE_CREDENTIALS);
  871. SEC2TXT(SEC_I_LOCAL_LOGON);
  872. SEC2TXT(SEC_I_NO_LSA_CONTEXT);
  873. SEC2TXT(SEC_I_RENEGOTIATE);
  874. SEC2TXT(SEC_I_SIGNATURE_NEEDED);
  875. default:
  876. txt = "Unknown error";
  877. }
  878. if(err == SEC_E_ILLEGAL_MESSAGE) {
  879. msnprintf(buf, buflen,
  880. "SEC_E_ILLEGAL_MESSAGE (0x%08X) - This error usually occurs "
  881. "when a fatal SSL/TLS alert is received (e.g. handshake failed)."
  882. " More detail may be available in the Windows System event log.",
  883. err);
  884. }
  885. else {
  886. char txtbuf[80];
  887. char msgbuf[256];
  888. msnprintf(txtbuf, sizeof(txtbuf), "%s (0x%08X)", txt, err);
  889. if(get_winapi_error(err, msgbuf, sizeof(msgbuf)))
  890. msnprintf(buf, buflen, "%s - %s", txtbuf, msgbuf);
  891. else {
  892. strncpy(buf, txtbuf, buflen);
  893. buf[buflen - 1] = '\0';
  894. }
  895. }
  896. #else
  897. if(err == SEC_E_OK)
  898. txt = "No error";
  899. else
  900. txt = "Error";
  901. strncpy(buf, txt, buflen);
  902. buf[buflen - 1] = '\0';
  903. #endif
  904. if(errno != old_errno)
  905. errno = old_errno;
  906. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  907. if(old_win_err != GetLastError())
  908. SetLastError(old_win_err);
  909. #endif
  910. return buf;
  911. }
  912. #endif /* USE_WINDOWS_SSPI */