strerror.c 27 KB

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