2
0

strerror.c 28 KB

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