2
0

strerror.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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 "Could not resolve proxy name";
  65. case CURLE_COULDNT_RESOLVE_HOST:
  66. return "Could not resolve hostname";
  67. case CURLE_COULDNT_CONNECT:
  68. return "Could not 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: cannot 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: could not set file type";
  91. case CURLE_PARTIAL_FILE:
  92. return "Transferred a partial file";
  93. case CURLE_FTP_COULDNT_RETR_FILE:
  94. return "FTP: could not 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 "Could not resume download";
  121. case CURLE_FILE_COULDNT_READ_FILE:
  122. return "Could not 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 "Could not 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. case CURLE_TOO_LARGE:
  228. return "A value or data field grew larger than allowed";
  229. case CURLE_ECH_REQUIRED:
  230. return "ECH attempted but failed";
  231. /* error codes not used by current libcurl */
  232. case CURLE_OBSOLETE20:
  233. case CURLE_OBSOLETE24:
  234. case CURLE_OBSOLETE29:
  235. case CURLE_OBSOLETE32:
  236. case CURLE_OBSOLETE40:
  237. case CURLE_OBSOLETE44:
  238. case CURLE_OBSOLETE46:
  239. case CURLE_OBSOLETE50:
  240. case CURLE_OBSOLETE51:
  241. case CURLE_OBSOLETE57:
  242. case CURLE_OBSOLETE62:
  243. case CURLE_OBSOLETE75:
  244. case CURLE_OBSOLETE76:
  245. case CURL_LAST:
  246. break;
  247. }
  248. /*
  249. * By using a switch, gcc -Wall will complain about enum values
  250. * which do not appear, helping keep this function up-to-date.
  251. * By using gcc -Wall -Werror, you cannot forget.
  252. *
  253. * A table would not have the same benefit. Most compilers will generate
  254. * code very similar to a table in any case, so there is little performance
  255. * gain from a table. Something is broken for the user's application,
  256. * anyways, so does it matter how fast it _does not_ work?
  257. *
  258. * The line number for the error will be near this comment, which is why it
  259. * is here, and not at the start of the switch.
  260. */
  261. return "Unknown error";
  262. #else
  263. if(!error)
  264. return "No error";
  265. else
  266. return "Error";
  267. #endif
  268. }
  269. const char *
  270. curl_multi_strerror(CURLMcode error)
  271. {
  272. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  273. switch(error) {
  274. case CURLM_CALL_MULTI_PERFORM:
  275. return "Please call curl_multi_perform() soon";
  276. case CURLM_OK:
  277. return "No error";
  278. case CURLM_BAD_HANDLE:
  279. return "Invalid multi handle";
  280. case CURLM_BAD_EASY_HANDLE:
  281. return "Invalid easy handle";
  282. case CURLM_OUT_OF_MEMORY:
  283. return "Out of memory";
  284. case CURLM_INTERNAL_ERROR:
  285. return "Internal error";
  286. case CURLM_BAD_SOCKET:
  287. return "Invalid socket argument";
  288. case CURLM_UNKNOWN_OPTION:
  289. return "Unknown option";
  290. case CURLM_ADDED_ALREADY:
  291. return "The easy handle is already added to a multi handle";
  292. case CURLM_RECURSIVE_API_CALL:
  293. return "API function called from within callback";
  294. case CURLM_WAKEUP_FAILURE:
  295. return "Wakeup is unavailable or failed";
  296. case CURLM_BAD_FUNCTION_ARGUMENT:
  297. return "A libcurl function was given a bad argument";
  298. case CURLM_ABORTED_BY_CALLBACK:
  299. return "Operation was aborted by an application callback";
  300. case CURLM_UNRECOVERABLE_POLL:
  301. return "Unrecoverable error in select/poll";
  302. case CURLM_LAST:
  303. break;
  304. }
  305. return "Unknown error";
  306. #else
  307. if(error == CURLM_OK)
  308. return "No error";
  309. else
  310. return "Error";
  311. #endif
  312. }
  313. const char *
  314. curl_share_strerror(CURLSHcode error)
  315. {
  316. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  317. switch(error) {
  318. case CURLSHE_OK:
  319. return "No error";
  320. case CURLSHE_BAD_OPTION:
  321. return "Unknown share option";
  322. case CURLSHE_IN_USE:
  323. return "Share currently in use";
  324. case CURLSHE_INVALID:
  325. return "Invalid share handle";
  326. case CURLSHE_NOMEM:
  327. return "Out of memory";
  328. case CURLSHE_NOT_BUILT_IN:
  329. return "Feature not enabled in this library";
  330. case CURLSHE_LAST:
  331. break;
  332. }
  333. return "CURLSHcode unknown";
  334. #else
  335. if(error == CURLSHE_OK)
  336. return "No error";
  337. else
  338. return "Error";
  339. #endif
  340. }
  341. const char *
  342. curl_url_strerror(CURLUcode error)
  343. {
  344. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  345. switch(error) {
  346. case CURLUE_OK:
  347. return "No error";
  348. case CURLUE_BAD_HANDLE:
  349. return "An invalid CURLU pointer was passed as argument";
  350. case CURLUE_BAD_PARTPOINTER:
  351. return "An invalid 'part' argument was passed as argument";
  352. case CURLUE_MALFORMED_INPUT:
  353. return "Malformed input to a URL function";
  354. case CURLUE_BAD_PORT_NUMBER:
  355. return "Port number was not a decimal number between 0 and 65535";
  356. case CURLUE_UNSUPPORTED_SCHEME:
  357. return "Unsupported URL scheme";
  358. case CURLUE_URLDECODE:
  359. return "URL decode error, most likely because of rubbish in the input";
  360. case CURLUE_OUT_OF_MEMORY:
  361. return "A memory function failed";
  362. case CURLUE_USER_NOT_ALLOWED:
  363. return "Credentials was passed in the URL when prohibited";
  364. case CURLUE_UNKNOWN_PART:
  365. return "An unknown part ID was passed to a URL API function";
  366. case CURLUE_NO_SCHEME:
  367. return "No scheme part in the URL";
  368. case CURLUE_NO_USER:
  369. return "No user part in the URL";
  370. case CURLUE_NO_PASSWORD:
  371. return "No password part in the URL";
  372. case CURLUE_NO_OPTIONS:
  373. return "No options part in the URL";
  374. case CURLUE_NO_HOST:
  375. return "No host part in the URL";
  376. case CURLUE_NO_PORT:
  377. return "No port part in the URL";
  378. case CURLUE_NO_QUERY:
  379. return "No query part in the URL";
  380. case CURLUE_NO_FRAGMENT:
  381. return "No fragment part in the URL";
  382. case CURLUE_NO_ZONEID:
  383. return "No zoneid part in the URL";
  384. case CURLUE_BAD_LOGIN:
  385. return "Bad login part";
  386. case CURLUE_BAD_IPV6:
  387. return "Bad IPv6 address";
  388. case CURLUE_BAD_HOSTNAME:
  389. return "Bad hostname";
  390. case CURLUE_BAD_FILE_URL:
  391. return "Bad file:// URL";
  392. case CURLUE_BAD_SLASHES:
  393. return "Unsupported number of slashes following scheme";
  394. case CURLUE_BAD_SCHEME:
  395. return "Bad scheme";
  396. case CURLUE_BAD_PATH:
  397. return "Bad path";
  398. case CURLUE_BAD_FRAGMENT:
  399. return "Bad fragment";
  400. case CURLUE_BAD_QUERY:
  401. return "Bad query";
  402. case CURLUE_BAD_PASSWORD:
  403. return "Bad password";
  404. case CURLUE_BAD_USER:
  405. return "Bad user";
  406. case CURLUE_LACKS_IDN:
  407. return "libcurl lacks IDN support";
  408. case CURLUE_TOO_LARGE:
  409. return "A value or data field is larger than allowed";
  410. case CURLUE_LAST:
  411. break;
  412. }
  413. return "CURLUcode unknown";
  414. #else
  415. if(error == CURLUE_OK)
  416. return "No error";
  417. else
  418. return "Error";
  419. #endif
  420. }
  421. #ifdef USE_WINSOCK
  422. /* This is a helper function for Curl_strerror that converts Winsock error
  423. * codes (WSAGetLastError) to error messages.
  424. * Returns NULL if no error message was found for error code.
  425. */
  426. static const char *
  427. get_winsock_error(int err, char *buf, size_t len)
  428. {
  429. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  430. const char *p;
  431. size_t alen;
  432. #endif
  433. if(!len)
  434. return NULL;
  435. *buf = '\0';
  436. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  437. (void)err;
  438. return NULL;
  439. #else
  440. switch(err) {
  441. case WSAEINTR:
  442. p = "Call interrupted";
  443. break;
  444. case WSAEBADF:
  445. p = "Bad file";
  446. break;
  447. case WSAEACCES:
  448. p = "Bad access";
  449. break;
  450. case WSAEFAULT:
  451. p = "Bad argument";
  452. break;
  453. case WSAEINVAL:
  454. p = "Invalid arguments";
  455. break;
  456. case WSAEMFILE:
  457. p = "Out of file descriptors";
  458. break;
  459. case WSAEWOULDBLOCK:
  460. p = "Call would block";
  461. break;
  462. case WSAEINPROGRESS:
  463. case WSAEALREADY:
  464. p = "Blocking call in progress";
  465. break;
  466. case WSAENOTSOCK:
  467. p = "Descriptor is not a socket";
  468. break;
  469. case WSAEDESTADDRREQ:
  470. p = "Need destination address";
  471. break;
  472. case WSAEMSGSIZE:
  473. p = "Bad message size";
  474. break;
  475. case WSAEPROTOTYPE:
  476. p = "Bad protocol";
  477. break;
  478. case WSAENOPROTOOPT:
  479. p = "Protocol option is unsupported";
  480. break;
  481. case WSAEPROTONOSUPPORT:
  482. p = "Protocol is unsupported";
  483. break;
  484. case WSAESOCKTNOSUPPORT:
  485. p = "Socket is unsupported";
  486. break;
  487. case WSAEOPNOTSUPP:
  488. p = "Operation not supported";
  489. break;
  490. case WSAEAFNOSUPPORT:
  491. p = "Address family not supported";
  492. break;
  493. case WSAEPFNOSUPPORT:
  494. p = "Protocol family not supported";
  495. break;
  496. case WSAEADDRINUSE:
  497. p = "Address already in use";
  498. break;
  499. case WSAEADDRNOTAVAIL:
  500. p = "Address not available";
  501. break;
  502. case WSAENETDOWN:
  503. p = "Network down";
  504. break;
  505. case WSAENETUNREACH:
  506. p = "Network unreachable";
  507. break;
  508. case WSAENETRESET:
  509. p = "Network has been reset";
  510. break;
  511. case WSAECONNABORTED:
  512. p = "Connection was aborted";
  513. break;
  514. case WSAECONNRESET:
  515. p = "Connection was reset";
  516. break;
  517. case WSAENOBUFS:
  518. p = "No buffer space";
  519. break;
  520. case WSAEISCONN:
  521. p = "Socket is already connected";
  522. break;
  523. case WSAENOTCONN:
  524. p = "Socket is not connected";
  525. break;
  526. case WSAESHUTDOWN:
  527. p = "Socket has been shut down";
  528. break;
  529. case WSAETOOMANYREFS:
  530. p = "Too many references";
  531. break;
  532. case WSAETIMEDOUT:
  533. p = "Timed out";
  534. break;
  535. case WSAECONNREFUSED:
  536. p = "Connection refused";
  537. break;
  538. case WSAELOOP:
  539. p = "Loop??";
  540. break;
  541. case WSAENAMETOOLONG:
  542. p = "Name too long";
  543. break;
  544. case WSAEHOSTDOWN:
  545. p = "Host down";
  546. break;
  547. case WSAEHOSTUNREACH:
  548. p = "Host unreachable";
  549. break;
  550. case WSAENOTEMPTY:
  551. p = "Not empty";
  552. break;
  553. case WSAEPROCLIM:
  554. p = "Process limit reached";
  555. break;
  556. case WSAEUSERS:
  557. p = "Too many users";
  558. break;
  559. case WSAEDQUOT:
  560. p = "Bad quota";
  561. break;
  562. case WSAESTALE:
  563. p = "Something is stale";
  564. break;
  565. case WSAEREMOTE:
  566. p = "Remote error";
  567. break;
  568. #ifdef WSAEDISCON /* missing in SalfordC! */
  569. case WSAEDISCON:
  570. p = "Disconnected";
  571. break;
  572. #endif
  573. /* Extended Winsock errors */
  574. case WSASYSNOTREADY:
  575. p = "Winsock library is not ready";
  576. break;
  577. case WSANOTINITIALISED:
  578. p = "Winsock library not initialised";
  579. break;
  580. case WSAVERNOTSUPPORTED:
  581. p = "Winsock version not supported";
  582. break;
  583. /* getXbyY() errors (already handled in herrmsg):
  584. * Authoritative Answer: Host not found */
  585. case WSAHOST_NOT_FOUND:
  586. p = "Host not found";
  587. break;
  588. /* Non-Authoritative: Host not found, or SERVERFAIL */
  589. case WSATRY_AGAIN:
  590. p = "Host not found, try again";
  591. break;
  592. /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
  593. case WSANO_RECOVERY:
  594. p = "Unrecoverable error in call to nameserver";
  595. break;
  596. /* Valid name, no data record of requested type */
  597. case WSANO_DATA:
  598. p = "No data record of requested type";
  599. break;
  600. default:
  601. return NULL;
  602. }
  603. alen = strlen(p);
  604. if(alen < len)
  605. strcpy(buf, p);
  606. return buf;
  607. #endif
  608. }
  609. #endif /* USE_WINSOCK */
  610. #if defined(_WIN32) || defined(_WIN32_WCE)
  611. /* This is a helper function for Curl_strerror that converts Windows API error
  612. * codes (GetLastError) to error messages.
  613. * Returns NULL if no error message was found for error code.
  614. */
  615. static const char *
  616. get_winapi_error(int err, char *buf, size_t buflen)
  617. {
  618. char *p;
  619. wchar_t wbuf[256];
  620. if(!buflen)
  621. return NULL;
  622. *buf = '\0';
  623. *wbuf = L'\0';
  624. /* We return the local codepage version of the error string because if it is
  625. output to the user's terminal it will likely be with functions which
  626. expect the local codepage (eg fprintf, failf, infof).
  627. FormatMessageW -> wcstombs is used for Windows CE compatibility. */
  628. if(FormatMessageW((FORMAT_MESSAGE_FROM_SYSTEM |
  629. FORMAT_MESSAGE_IGNORE_INSERTS), NULL, (DWORD)err,
  630. LANG_NEUTRAL, wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL)) {
  631. size_t written = wcstombs(buf, wbuf, buflen - 1);
  632. if(written != (size_t)-1)
  633. buf[written] = '\0';
  634. else
  635. *buf = '\0';
  636. }
  637. /* Truncate multiple lines */
  638. p = strchr(buf, '\n');
  639. if(p) {
  640. if(p > buf && *(p-1) == '\r')
  641. *(p-1) = '\0';
  642. else
  643. *p = '\0';
  644. }
  645. return (*buf ? buf : NULL);
  646. }
  647. #endif /* _WIN32 || _WIN32_WCE */
  648. /*
  649. * Our thread-safe and smart strerror() replacement.
  650. *
  651. * The 'err' argument passed in to this function MUST be a true errno number
  652. * as reported on this system. We do no range checking on the number before
  653. * we pass it to the "number-to-message" conversion function and there might
  654. * be systems that do not do proper range checking in there themselves.
  655. *
  656. * We do not do range checking (on systems other than Windows) since there is
  657. * no good reliable and portable way to do it.
  658. *
  659. * On Windows different types of error codes overlap. This function has an
  660. * order of preference when trying to match error codes:
  661. * CRT (errno), Winsock (WSAGetLastError), Windows API (GetLastError).
  662. *
  663. * It may be more correct to call one of the variant functions instead:
  664. * Call Curl_sspi_strerror if the error code is definitely Windows SSPI.
  665. * Call Curl_winapi_strerror if the error code is definitely Windows API.
  666. */
  667. const char *Curl_strerror(int err, char *buf, size_t buflen)
  668. {
  669. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  670. DWORD old_win_err = GetLastError();
  671. #endif
  672. int old_errno = errno;
  673. char *p;
  674. if(!buflen)
  675. return NULL;
  676. #ifndef _WIN32
  677. DEBUGASSERT(err >= 0);
  678. #endif
  679. *buf = '\0';
  680. #if defined(_WIN32) || defined(_WIN32_WCE)
  681. #if defined(_WIN32)
  682. /* 'sys_nerr' is the maximum errno number, it is not widely portable */
  683. if(err >= 0 && err < sys_nerr)
  684. msnprintf(buf, buflen, "%s", sys_errlist[err]);
  685. else
  686. #endif
  687. {
  688. if(
  689. #ifdef USE_WINSOCK
  690. !get_winsock_error(err, buf, buflen) &&
  691. #endif
  692. !get_winapi_error(err, buf, buflen))
  693. msnprintf(buf, buflen, "Unknown error %d (%#x)", err, err);
  694. }
  695. #else /* not Windows coming up */
  696. #if defined(HAVE_STRERROR_R) && defined(HAVE_POSIX_STRERROR_R)
  697. /*
  698. * The POSIX-style strerror_r() may set errno to ERANGE if insufficient
  699. * storage is supplied via 'strerrbuf' and 'buflen' to hold the generated
  700. * message string, or EINVAL if 'errnum' is not a valid error number.
  701. */
  702. if(0 != strerror_r(err, buf, buflen)) {
  703. if('\0' == buf[0])
  704. msnprintf(buf, buflen, "Unknown error %d", err);
  705. }
  706. #elif defined(HAVE_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R)
  707. /*
  708. * The glibc-style strerror_r() only *might* use the buffer we pass to
  709. * the function, but it always returns the error message as a pointer,
  710. * so we must copy that string unconditionally (if non-NULL).
  711. */
  712. {
  713. char buffer[256];
  714. char *msg = strerror_r(err, buffer, sizeof(buffer));
  715. if(msg)
  716. msnprintf(buf, buflen, "%s", msg);
  717. else
  718. msnprintf(buf, buflen, "Unknown error %d", err);
  719. }
  720. #else
  721. {
  722. /* !checksrc! disable STRERROR 1 */
  723. const char *msg = strerror(err);
  724. if(msg)
  725. msnprintf(buf, buflen, "%s", msg);
  726. else
  727. msnprintf(buf, buflen, "Unknown error %d", err);
  728. }
  729. #endif
  730. #endif /* end of not Windows */
  731. /* strip trailing '\r\n' or '\n'. */
  732. p = strrchr(buf, '\n');
  733. if(p && (p - buf) >= 2)
  734. *p = '\0';
  735. p = strrchr(buf, '\r');
  736. if(p && (p - buf) >= 1)
  737. *p = '\0';
  738. if(errno != old_errno)
  739. errno = old_errno;
  740. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  741. if(old_win_err != GetLastError())
  742. SetLastError(old_win_err);
  743. #endif
  744. return buf;
  745. }
  746. /*
  747. * Curl_winapi_strerror:
  748. * Variant of Curl_strerror if the error code is definitely Windows API.
  749. */
  750. #if defined(_WIN32) || defined(_WIN32_WCE)
  751. const char *Curl_winapi_strerror(DWORD err, char *buf, size_t buflen)
  752. {
  753. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  754. DWORD old_win_err = GetLastError();
  755. #endif
  756. int old_errno = errno;
  757. if(!buflen)
  758. return NULL;
  759. *buf = '\0';
  760. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  761. if(!get_winapi_error((int)err, buf, buflen)) {
  762. msnprintf(buf, buflen, "Unknown error %lu (0x%08lX)", err, err);
  763. }
  764. #else
  765. {
  766. const char *txt = (err == ERROR_SUCCESS) ? "No error" : "Error";
  767. if(strlen(txt) < buflen)
  768. strcpy(buf, txt);
  769. }
  770. #endif
  771. if(errno != old_errno)
  772. errno = old_errno;
  773. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  774. if(old_win_err != GetLastError())
  775. SetLastError(old_win_err);
  776. #endif
  777. return buf;
  778. }
  779. #endif /* _WIN32 || _WIN32_WCE */
  780. #ifdef USE_WINDOWS_SSPI
  781. /*
  782. * Curl_sspi_strerror:
  783. * Variant of Curl_strerror if the error code is definitely Windows SSPI.
  784. */
  785. const char *Curl_sspi_strerror(int err, char *buf, size_t buflen)
  786. {
  787. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  788. DWORD old_win_err = GetLastError();
  789. #endif
  790. int old_errno = errno;
  791. const char *txt;
  792. if(!buflen)
  793. return NULL;
  794. *buf = '\0';
  795. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  796. switch(err) {
  797. case SEC_E_OK:
  798. txt = "No error";
  799. break;
  800. #define SEC2TXT(sec) case sec: txt = #sec; break
  801. SEC2TXT(CRYPT_E_REVOKED);
  802. SEC2TXT(CRYPT_E_NO_REVOCATION_DLL);
  803. SEC2TXT(CRYPT_E_NO_REVOCATION_CHECK);
  804. SEC2TXT(CRYPT_E_REVOCATION_OFFLINE);
  805. SEC2TXT(CRYPT_E_NOT_IN_REVOCATION_DATABASE);
  806. SEC2TXT(SEC_E_ALGORITHM_MISMATCH);
  807. SEC2TXT(SEC_E_BAD_BINDINGS);
  808. SEC2TXT(SEC_E_BAD_PKGID);
  809. SEC2TXT(SEC_E_BUFFER_TOO_SMALL);
  810. SEC2TXT(SEC_E_CANNOT_INSTALL);
  811. SEC2TXT(SEC_E_CANNOT_PACK);
  812. SEC2TXT(SEC_E_CERT_EXPIRED);
  813. SEC2TXT(SEC_E_CERT_UNKNOWN);
  814. SEC2TXT(SEC_E_CERT_WRONG_USAGE);
  815. SEC2TXT(SEC_E_CONTEXT_EXPIRED);
  816. SEC2TXT(SEC_E_CROSSREALM_DELEGATION_FAILURE);
  817. SEC2TXT(SEC_E_CRYPTO_SYSTEM_INVALID);
  818. SEC2TXT(SEC_E_DECRYPT_FAILURE);
  819. SEC2TXT(SEC_E_DELEGATION_POLICY);
  820. SEC2TXT(SEC_E_DELEGATION_REQUIRED);
  821. SEC2TXT(SEC_E_DOWNGRADE_DETECTED);
  822. SEC2TXT(SEC_E_ENCRYPT_FAILURE);
  823. SEC2TXT(SEC_E_ILLEGAL_MESSAGE);
  824. SEC2TXT(SEC_E_INCOMPLETE_CREDENTIALS);
  825. SEC2TXT(SEC_E_INCOMPLETE_MESSAGE);
  826. SEC2TXT(SEC_E_INSUFFICIENT_MEMORY);
  827. SEC2TXT(SEC_E_INTERNAL_ERROR);
  828. SEC2TXT(SEC_E_INVALID_HANDLE);
  829. SEC2TXT(SEC_E_INVALID_PARAMETER);
  830. SEC2TXT(SEC_E_INVALID_TOKEN);
  831. SEC2TXT(SEC_E_ISSUING_CA_UNTRUSTED);
  832. SEC2TXT(SEC_E_ISSUING_CA_UNTRUSTED_KDC);
  833. SEC2TXT(SEC_E_KDC_CERT_EXPIRED);
  834. SEC2TXT(SEC_E_KDC_CERT_REVOKED);
  835. SEC2TXT(SEC_E_KDC_INVALID_REQUEST);
  836. SEC2TXT(SEC_E_KDC_UNABLE_TO_REFER);
  837. SEC2TXT(SEC_E_KDC_UNKNOWN_ETYPE);
  838. SEC2TXT(SEC_E_LOGON_DENIED);
  839. SEC2TXT(SEC_E_MAX_REFERRALS_EXCEEDED);
  840. SEC2TXT(SEC_E_MESSAGE_ALTERED);
  841. SEC2TXT(SEC_E_MULTIPLE_ACCOUNTS);
  842. SEC2TXT(SEC_E_MUST_BE_KDC);
  843. SEC2TXT(SEC_E_NOT_OWNER);
  844. SEC2TXT(SEC_E_NO_AUTHENTICATING_AUTHORITY);
  845. SEC2TXT(SEC_E_NO_CREDENTIALS);
  846. SEC2TXT(SEC_E_NO_IMPERSONATION);
  847. SEC2TXT(SEC_E_NO_IP_ADDRESSES);
  848. SEC2TXT(SEC_E_NO_KERB_KEY);
  849. SEC2TXT(SEC_E_NO_PA_DATA);
  850. SEC2TXT(SEC_E_NO_S4U_PROT_SUPPORT);
  851. SEC2TXT(SEC_E_NO_TGT_REPLY);
  852. SEC2TXT(SEC_E_OUT_OF_SEQUENCE);
  853. SEC2TXT(SEC_E_PKINIT_CLIENT_FAILURE);
  854. SEC2TXT(SEC_E_PKINIT_NAME_MISMATCH);
  855. SEC2TXT(SEC_E_POLICY_NLTM_ONLY);
  856. SEC2TXT(SEC_E_QOP_NOT_SUPPORTED);
  857. SEC2TXT(SEC_E_REVOCATION_OFFLINE_C);
  858. SEC2TXT(SEC_E_REVOCATION_OFFLINE_KDC);
  859. SEC2TXT(SEC_E_SECPKG_NOT_FOUND);
  860. SEC2TXT(SEC_E_SECURITY_QOS_FAILED);
  861. SEC2TXT(SEC_E_SHUTDOWN_IN_PROGRESS);
  862. SEC2TXT(SEC_E_SMARTCARD_CERT_EXPIRED);
  863. SEC2TXT(SEC_E_SMARTCARD_CERT_REVOKED);
  864. SEC2TXT(SEC_E_SMARTCARD_LOGON_REQUIRED);
  865. SEC2TXT(SEC_E_STRONG_CRYPTO_NOT_SUPPORTED);
  866. SEC2TXT(SEC_E_TARGET_UNKNOWN);
  867. SEC2TXT(SEC_E_TIME_SKEW);
  868. SEC2TXT(SEC_E_TOO_MANY_PRINCIPALS);
  869. SEC2TXT(SEC_E_UNFINISHED_CONTEXT_DELETED);
  870. SEC2TXT(SEC_E_UNKNOWN_CREDENTIALS);
  871. SEC2TXT(SEC_E_UNSUPPORTED_FUNCTION);
  872. SEC2TXT(SEC_E_UNSUPPORTED_PREAUTH);
  873. SEC2TXT(SEC_E_UNTRUSTED_ROOT);
  874. SEC2TXT(SEC_E_WRONG_CREDENTIAL_HANDLE);
  875. SEC2TXT(SEC_E_WRONG_PRINCIPAL);
  876. SEC2TXT(SEC_I_COMPLETE_AND_CONTINUE);
  877. SEC2TXT(SEC_I_COMPLETE_NEEDED);
  878. SEC2TXT(SEC_I_CONTEXT_EXPIRED);
  879. SEC2TXT(SEC_I_CONTINUE_NEEDED);
  880. SEC2TXT(SEC_I_INCOMPLETE_CREDENTIALS);
  881. SEC2TXT(SEC_I_LOCAL_LOGON);
  882. SEC2TXT(SEC_I_NO_LSA_CONTEXT);
  883. SEC2TXT(SEC_I_RENEGOTIATE);
  884. SEC2TXT(SEC_I_SIGNATURE_NEEDED);
  885. default:
  886. txt = "Unknown error";
  887. }
  888. if(err == SEC_E_ILLEGAL_MESSAGE) {
  889. msnprintf(buf, buflen,
  890. "SEC_E_ILLEGAL_MESSAGE (0x%08X) - This error usually occurs "
  891. "when a fatal SSL/TLS alert is received (e.g. handshake failed)."
  892. " More detail may be available in the Windows System event log.",
  893. err);
  894. }
  895. else {
  896. char msgbuf[256];
  897. if(get_winapi_error(err, msgbuf, sizeof(msgbuf)))
  898. msnprintf(buf, buflen, "%s (0x%08X) - %s", txt, err, msgbuf);
  899. else
  900. msnprintf(buf, buflen, "%s (0x%08X)", txt, err);
  901. }
  902. #else
  903. if(err == SEC_E_OK)
  904. txt = "No error";
  905. else
  906. txt = "Error";
  907. if(buflen > strlen(txt))
  908. strcpy(buf, txt);
  909. #endif
  910. if(errno != old_errno)
  911. errno = old_errno;
  912. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  913. if(old_win_err != GetLastError())
  914. SetLastError(old_win_err);
  915. #endif
  916. return buf;
  917. }
  918. #endif /* USE_WINDOWS_SSPI */