2
0

strerror.c 28 KB

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