strerror.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2004 - 2015, Daniel Stenberg, <[email protected]>, 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 http://curl.haxx.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_VXWORKS_STRERROR_R)) || \
  27. (defined(HAVE_POSIX_STRERROR_R) && defined(HAVE_VXWORKS_STRERROR_R)) || \
  28. (defined(HAVE_GLIBC_STRERROR_R) && defined(HAVE_VXWORKS_STRERROR_R)) || \
  29. (defined(HAVE_POSIX_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R))
  30. # error "strerror_r MUST be either POSIX, glibc or vxworks-style"
  31. # endif
  32. #endif
  33. #include <curl/curl.h>
  34. #ifdef USE_LIBIDN
  35. #include <idna.h>
  36. #endif
  37. #include "strerror.h"
  38. #include "curl_printf.h"
  39. #include "curl_memory.h"
  40. /* The last #include file should be: */
  41. #include "memdebug.h"
  42. const char *
  43. curl_easy_strerror(CURLcode error)
  44. {
  45. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  46. switch (error) {
  47. case CURLE_OK:
  48. return "No error";
  49. case CURLE_UNSUPPORTED_PROTOCOL:
  50. return "Unsupported protocol";
  51. case CURLE_FAILED_INIT:
  52. return "Failed initialization";
  53. case CURLE_URL_MALFORMAT:
  54. return "URL using bad/illegal format or missing URL";
  55. case CURLE_NOT_BUILT_IN:
  56. return "A requested feature, protocol or option was not found built-in in"
  57. " this libcurl due to a build-time decision.";
  58. case CURLE_COULDNT_RESOLVE_PROXY:
  59. return "Couldn't resolve proxy name";
  60. case CURLE_COULDNT_RESOLVE_HOST:
  61. return "Couldn't resolve host name";
  62. case CURLE_COULDNT_CONNECT:
  63. return "Couldn't connect to server";
  64. case CURLE_FTP_WEIRD_SERVER_REPLY:
  65. return "FTP: weird server reply";
  66. case CURLE_REMOTE_ACCESS_DENIED:
  67. return "Access denied to remote resource";
  68. case CURLE_FTP_ACCEPT_FAILED:
  69. return "FTP: The server failed to connect to data port";
  70. case CURLE_FTP_ACCEPT_TIMEOUT:
  71. return "FTP: Accepting server connect has timed out";
  72. case CURLE_FTP_PRET_FAILED:
  73. return "FTP: The server did not accept the PRET command.";
  74. case CURLE_FTP_WEIRD_PASS_REPLY:
  75. return "FTP: unknown PASS reply";
  76. case CURLE_FTP_WEIRD_PASV_REPLY:
  77. return "FTP: unknown PASV reply";
  78. case CURLE_FTP_WEIRD_227_FORMAT:
  79. return "FTP: unknown 227 response format";
  80. case CURLE_FTP_CANT_GET_HOST:
  81. return "FTP: can't figure out the host in the PASV response";
  82. case CURLE_HTTP2:
  83. return "Error in the HTTP2 framing layer";
  84. case CURLE_FTP_COULDNT_SET_TYPE:
  85. return "FTP: couldn't set file type";
  86. case CURLE_PARTIAL_FILE:
  87. return "Transferred a partial file";
  88. case CURLE_FTP_COULDNT_RETR_FILE:
  89. return "FTP: couldn't retrieve (RETR failed) the specified file";
  90. case CURLE_QUOTE_ERROR:
  91. return "Quote command returned error";
  92. case CURLE_HTTP_RETURNED_ERROR:
  93. return "HTTP response code said error";
  94. case CURLE_WRITE_ERROR:
  95. return "Failed writing received data to disk/application";
  96. case CURLE_UPLOAD_FAILED:
  97. return "Upload failed (at start/before it took off)";
  98. case CURLE_READ_ERROR:
  99. return "Failed to open/read local data from file/application";
  100. case CURLE_OUT_OF_MEMORY:
  101. return "Out of memory";
  102. case CURLE_OPERATION_TIMEDOUT:
  103. return "Timeout was reached";
  104. case CURLE_FTP_PORT_FAILED:
  105. return "FTP: command PORT failed";
  106. case CURLE_FTP_COULDNT_USE_REST:
  107. return "FTP: command REST failed";
  108. case CURLE_RANGE_ERROR:
  109. return "Requested range was not delivered by the server";
  110. case CURLE_HTTP_POST_ERROR:
  111. return "Internal problem setting up the POST";
  112. case CURLE_SSL_CONNECT_ERROR:
  113. return "SSL connect error";
  114. case CURLE_BAD_DOWNLOAD_RESUME:
  115. return "Couldn't resume download";
  116. case CURLE_FILE_COULDNT_READ_FILE:
  117. return "Couldn't read a file:// file";
  118. case CURLE_LDAP_CANNOT_BIND:
  119. return "LDAP: cannot bind";
  120. case CURLE_LDAP_SEARCH_FAILED:
  121. return "LDAP: search failed";
  122. case CURLE_FUNCTION_NOT_FOUND:
  123. return "A required function in the library was not found";
  124. case CURLE_ABORTED_BY_CALLBACK:
  125. return "Operation was aborted by an application callback";
  126. case CURLE_BAD_FUNCTION_ARGUMENT:
  127. return "A libcurl function was given a bad argument";
  128. case CURLE_INTERFACE_FAILED:
  129. return "Failed binding local connection end";
  130. case CURLE_TOO_MANY_REDIRECTS :
  131. return "Number of redirects hit maximum amount";
  132. case CURLE_UNKNOWN_OPTION:
  133. return "An unknown option was passed in to libcurl";
  134. case CURLE_TELNET_OPTION_SYNTAX :
  135. return "Malformed telnet option";
  136. case CURLE_PEER_FAILED_VERIFICATION:
  137. return "SSL peer certificate or SSH remote key was not OK";
  138. case CURLE_GOT_NOTHING:
  139. return "Server returned nothing (no headers, no data)";
  140. case CURLE_SSL_ENGINE_NOTFOUND:
  141. return "SSL crypto engine not found";
  142. case CURLE_SSL_ENGINE_SETFAILED:
  143. return "Can not set SSL crypto engine as default";
  144. case CURLE_SSL_ENGINE_INITFAILED:
  145. return "Failed to initialise SSL crypto engine";
  146. case CURLE_SEND_ERROR:
  147. return "Failed sending data to the peer";
  148. case CURLE_RECV_ERROR:
  149. return "Failure when receiving data from the peer";
  150. case CURLE_SSL_CERTPROBLEM:
  151. return "Problem with the local SSL certificate";
  152. case CURLE_SSL_CIPHER:
  153. return "Couldn't use specified SSL cipher";
  154. case CURLE_SSL_CACERT:
  155. return "Peer certificate cannot be authenticated with given CA "
  156. "certificates";
  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_LDAP_INVALID_URL:
  162. return "Invalid LDAP URL";
  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_CONV_FAILED:
  192. return "Conversion failed";
  193. case CURLE_CONV_REQD:
  194. return "Caller must register CURLOPT_CONV_ callback options";
  195. case CURLE_REMOTE_FILE_NOT_FOUND:
  196. return "Remote file not found";
  197. case CURLE_SSH:
  198. return "Error in the SSH layer";
  199. case CURLE_AGAIN:
  200. return "Socket not ready for send/recv";
  201. case CURLE_RTSP_CSEQ_ERROR:
  202. return "RTSP CSeq mismatch or invalid CSeq";
  203. case CURLE_RTSP_SESSION_ERROR:
  204. return "RTSP session error";
  205. case CURLE_FTP_BAD_FILE_LIST:
  206. return "Unable to parse FTP file list";
  207. case CURLE_CHUNK_FAILED:
  208. return "Chunk callback failed";
  209. case CURLE_NO_CONNECTION_AVAILABLE:
  210. return "The max connection limit is reached";
  211. case CURLE_SSL_PINNEDPUBKEYNOTMATCH:
  212. return "SSL public key does not match pinned public key";
  213. case CURLE_SSL_INVALIDCERTSTATUS:
  214. return "SSL server certificate status verification FAILED";
  215. /* error codes not used by current libcurl */
  216. case CURLE_OBSOLETE20:
  217. case CURLE_OBSOLETE24:
  218. case CURLE_OBSOLETE29:
  219. case CURLE_OBSOLETE32:
  220. case CURLE_OBSOLETE40:
  221. case CURLE_OBSOLETE44:
  222. case CURLE_OBSOLETE46:
  223. case CURLE_OBSOLETE50:
  224. case CURLE_OBSOLETE57:
  225. case CURL_LAST:
  226. break;
  227. }
  228. /*
  229. * By using a switch, gcc -Wall will complain about enum values
  230. * which do not appear, helping keep this function up-to-date.
  231. * By using gcc -Wall -Werror, you can't forget.
  232. *
  233. * A table would not have the same benefit. Most compilers will
  234. * generate code very similar to a table in any case, so there
  235. * is little performance gain from a table. And something is broken
  236. * for the user's application, anyways, so does it matter how fast
  237. * it _doesn't_ work?
  238. *
  239. * The line number for the error will be near this comment, which
  240. * is why it is here, and not at the start of the switch.
  241. */
  242. return "Unknown error";
  243. #else
  244. if(!error)
  245. return "No error";
  246. else
  247. return "Error";
  248. #endif
  249. }
  250. const char *
  251. curl_multi_strerror(CURLMcode error)
  252. {
  253. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  254. switch (error) {
  255. case CURLM_CALL_MULTI_PERFORM:
  256. return "Please call curl_multi_perform() soon";
  257. case CURLM_OK:
  258. return "No error";
  259. case CURLM_BAD_HANDLE:
  260. return "Invalid multi handle";
  261. case CURLM_BAD_EASY_HANDLE:
  262. return "Invalid easy handle";
  263. case CURLM_OUT_OF_MEMORY:
  264. return "Out of memory";
  265. case CURLM_INTERNAL_ERROR:
  266. return "Internal error";
  267. case CURLM_BAD_SOCKET:
  268. return "Invalid socket argument";
  269. case CURLM_UNKNOWN_OPTION:
  270. return "Unknown option";
  271. case CURLM_ADDED_ALREADY:
  272. return "The easy handle is already added to a multi handle";
  273. case CURLM_LAST:
  274. break;
  275. }
  276. return "Unknown error";
  277. #else
  278. if(error == CURLM_OK)
  279. return "No error";
  280. else
  281. return "Error";
  282. #endif
  283. }
  284. const char *
  285. curl_share_strerror(CURLSHcode error)
  286. {
  287. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  288. switch (error) {
  289. case CURLSHE_OK:
  290. return "No error";
  291. case CURLSHE_BAD_OPTION:
  292. return "Unknown share option";
  293. case CURLSHE_IN_USE:
  294. return "Share currently in use";
  295. case CURLSHE_INVALID:
  296. return "Invalid share handle";
  297. case CURLSHE_NOMEM:
  298. return "Out of memory";
  299. case CURLSHE_NOT_BUILT_IN:
  300. return "Feature not enabled in this library";
  301. case CURLSHE_LAST:
  302. break;
  303. }
  304. return "CURLSHcode unknown";
  305. #else
  306. if(error == CURLSHE_OK)
  307. return "No error";
  308. else
  309. return "Error";
  310. #endif
  311. }
  312. #ifdef USE_WINSOCK
  313. /* This function handles most / all (?) Winsock errors cURL is able to produce.
  314. */
  315. static const char *
  316. get_winsock_error (int err, char *buf, size_t len)
  317. {
  318. const char *p;
  319. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  320. switch (err) {
  321. case WSAEINTR:
  322. p = "Call interrupted";
  323. break;
  324. case WSAEBADF:
  325. p = "Bad file";
  326. break;
  327. case WSAEACCES:
  328. p = "Bad access";
  329. break;
  330. case WSAEFAULT:
  331. p = "Bad argument";
  332. break;
  333. case WSAEINVAL:
  334. p = "Invalid arguments";
  335. break;
  336. case WSAEMFILE:
  337. p = "Out of file descriptors";
  338. break;
  339. case WSAEWOULDBLOCK:
  340. p = "Call would block";
  341. break;
  342. case WSAEINPROGRESS:
  343. case WSAEALREADY:
  344. p = "Blocking call in progress";
  345. break;
  346. case WSAENOTSOCK:
  347. p = "Descriptor is not a socket";
  348. break;
  349. case WSAEDESTADDRREQ:
  350. p = "Need destination address";
  351. break;
  352. case WSAEMSGSIZE:
  353. p = "Bad message size";
  354. break;
  355. case WSAEPROTOTYPE:
  356. p = "Bad protocol";
  357. break;
  358. case WSAENOPROTOOPT:
  359. p = "Protocol option is unsupported";
  360. break;
  361. case WSAEPROTONOSUPPORT:
  362. p = "Protocol is unsupported";
  363. break;
  364. case WSAESOCKTNOSUPPORT:
  365. p = "Socket is unsupported";
  366. break;
  367. case WSAEOPNOTSUPP:
  368. p = "Operation not supported";
  369. break;
  370. case WSAEAFNOSUPPORT:
  371. p = "Address family not supported";
  372. break;
  373. case WSAEPFNOSUPPORT:
  374. p = "Protocol family not supported";
  375. break;
  376. case WSAEADDRINUSE:
  377. p = "Address already in use";
  378. break;
  379. case WSAEADDRNOTAVAIL:
  380. p = "Address not available";
  381. break;
  382. case WSAENETDOWN:
  383. p = "Network down";
  384. break;
  385. case WSAENETUNREACH:
  386. p = "Network unreachable";
  387. break;
  388. case WSAENETRESET:
  389. p = "Network has been reset";
  390. break;
  391. case WSAECONNABORTED:
  392. p = "Connection was aborted";
  393. break;
  394. case WSAECONNRESET:
  395. p = "Connection was reset";
  396. break;
  397. case WSAENOBUFS:
  398. p = "No buffer space";
  399. break;
  400. case WSAEISCONN:
  401. p = "Socket is already connected";
  402. break;
  403. case WSAENOTCONN:
  404. p = "Socket is not connected";
  405. break;
  406. case WSAESHUTDOWN:
  407. p = "Socket has been shut down";
  408. break;
  409. case WSAETOOMANYREFS:
  410. p = "Too many references";
  411. break;
  412. case WSAETIMEDOUT:
  413. p = "Timed out";
  414. break;
  415. case WSAECONNREFUSED:
  416. p = "Connection refused";
  417. break;
  418. case WSAELOOP:
  419. p = "Loop??";
  420. break;
  421. case WSAENAMETOOLONG:
  422. p = "Name too long";
  423. break;
  424. case WSAEHOSTDOWN:
  425. p = "Host down";
  426. break;
  427. case WSAEHOSTUNREACH:
  428. p = "Host unreachable";
  429. break;
  430. case WSAENOTEMPTY:
  431. p = "Not empty";
  432. break;
  433. case WSAEPROCLIM:
  434. p = "Process limit reached";
  435. break;
  436. case WSAEUSERS:
  437. p = "Too many users";
  438. break;
  439. case WSAEDQUOT:
  440. p = "Bad quota";
  441. break;
  442. case WSAESTALE:
  443. p = "Something is stale";
  444. break;
  445. case WSAEREMOTE:
  446. p = "Remote error";
  447. break;
  448. #ifdef WSAEDISCON /* missing in SalfordC! */
  449. case WSAEDISCON:
  450. p = "Disconnected";
  451. break;
  452. #endif
  453. /* Extended Winsock errors */
  454. case WSASYSNOTREADY:
  455. p = "Winsock library is not ready";
  456. break;
  457. case WSANOTINITIALISED:
  458. p = "Winsock library not initialised";
  459. break;
  460. case WSAVERNOTSUPPORTED:
  461. p = "Winsock version not supported";
  462. break;
  463. /* getXbyY() errors (already handled in herrmsg):
  464. * Authoritative Answer: Host not found */
  465. case WSAHOST_NOT_FOUND:
  466. p = "Host not found";
  467. break;
  468. /* Non-Authoritative: Host not found, or SERVERFAIL */
  469. case WSATRY_AGAIN:
  470. p = "Host not found, try again";
  471. break;
  472. /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
  473. case WSANO_RECOVERY:
  474. p = "Unrecoverable error in call to nameserver";
  475. break;
  476. /* Valid name, no data record of requested type */
  477. case WSANO_DATA:
  478. p = "No data record of requested type";
  479. break;
  480. default:
  481. return NULL;
  482. }
  483. #else
  484. if(!err)
  485. return NULL;
  486. else
  487. p = "error";
  488. #endif
  489. strncpy (buf, p, len);
  490. buf [len-1] = '\0';
  491. return buf;
  492. }
  493. #endif /* USE_WINSOCK */
  494. /*
  495. * Our thread-safe and smart strerror() replacement.
  496. *
  497. * The 'err' argument passed in to this function MUST be a true errno number
  498. * as reported on this system. We do no range checking on the number before
  499. * we pass it to the "number-to-message" conversion function and there might
  500. * be systems that don't do proper range checking in there themselves.
  501. *
  502. * We don't do range checking (on systems other than Windows) since there is
  503. * no good reliable and portable way to do it.
  504. */
  505. const char *Curl_strerror(struct connectdata *conn, int err)
  506. {
  507. char *buf, *p;
  508. size_t max;
  509. int old_errno = ERRNO;
  510. DEBUGASSERT(conn);
  511. DEBUGASSERT(err >= 0);
  512. buf = conn->syserr_buf;
  513. max = sizeof(conn->syserr_buf)-1;
  514. *buf = '\0';
  515. #ifdef USE_WINSOCK
  516. #ifdef _WIN32_WCE
  517. {
  518. wchar_t wbuf[256];
  519. wbuf[0] = L'\0';
  520. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
  521. LANG_NEUTRAL, wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL);
  522. wcstombs(buf, wbuf, max);
  523. }
  524. #else
  525. /* 'sys_nerr' is the maximum errno number, it is not widely portable */
  526. if(err >= 0 && err < sys_nerr)
  527. strncpy(buf, strerror(err), max);
  528. else {
  529. if(!get_winsock_error(err, buf, max) &&
  530. !FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
  531. LANG_NEUTRAL, buf, (DWORD)max, NULL))
  532. snprintf(buf, max, "Unknown error %d (%#x)", err, err);
  533. }
  534. #endif
  535. #else /* not USE_WINSOCK coming up */
  536. #if defined(HAVE_STRERROR_R) && defined(HAVE_POSIX_STRERROR_R)
  537. /*
  538. * The POSIX-style strerror_r() may set errno to ERANGE if insufficient
  539. * storage is supplied via 'strerrbuf' and 'buflen' to hold the generated
  540. * message string, or EINVAL if 'errnum' is not a valid error number.
  541. */
  542. if(0 != strerror_r(err, buf, max)) {
  543. if('\0' == buf[0])
  544. snprintf(buf, max, "Unknown error %d", err);
  545. }
  546. #elif defined(HAVE_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R)
  547. /*
  548. * The glibc-style strerror_r() only *might* use the buffer we pass to
  549. * the function, but it always returns the error message as a pointer,
  550. * so we must copy that string unconditionally (if non-NULL).
  551. */
  552. {
  553. char buffer[256];
  554. char *msg = strerror_r(err, buffer, sizeof(buffer));
  555. if(msg)
  556. strncpy(buf, msg, max);
  557. else
  558. snprintf(buf, max, "Unknown error %d", err);
  559. }
  560. #elif defined(HAVE_STRERROR_R) && defined(HAVE_VXWORKS_STRERROR_R)
  561. /*
  562. * The vxworks-style strerror_r() does use the buffer we pass to the function.
  563. * The buffer size should be at least NAME_MAX (256)
  564. */
  565. {
  566. char buffer[256];
  567. if(OK == strerror_r(err, buffer))
  568. strncpy(buf, buffer, max);
  569. else
  570. snprintf(buf, max, "Unknown error %d", err);
  571. }
  572. #else
  573. {
  574. char *msg = strerror(err);
  575. if(msg)
  576. strncpy(buf, msg, max);
  577. else
  578. snprintf(buf, max, "Unknown error %d", err);
  579. }
  580. #endif
  581. #endif /* end of ! USE_WINSOCK */
  582. buf[max] = '\0'; /* make sure the string is zero terminated */
  583. /* strip trailing '\r\n' or '\n'. */
  584. if((p = strrchr(buf, '\n')) != NULL && (p - buf) >= 2)
  585. *p = '\0';
  586. if((p = strrchr(buf, '\r')) != NULL && (p - buf) >= 1)
  587. *p = '\0';
  588. if(old_errno != ERRNO)
  589. SET_ERRNO(old_errno);
  590. return buf;
  591. }
  592. #ifdef USE_LIBIDN
  593. /*
  594. * Return error-string for libidn status as returned from idna_to_ascii_lz().
  595. */
  596. const char *Curl_idn_strerror (struct connectdata *conn, int err)
  597. {
  598. #ifdef HAVE_IDNA_STRERROR
  599. (void)conn;
  600. return idna_strerror((Idna_rc) err);
  601. #else
  602. const char *str;
  603. char *buf;
  604. size_t max;
  605. DEBUGASSERT(conn);
  606. buf = conn->syserr_buf;
  607. max = sizeof(conn->syserr_buf)-1;
  608. *buf = '\0';
  609. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  610. switch ((Idna_rc)err) {
  611. case IDNA_SUCCESS:
  612. str = "No error";
  613. break;
  614. case IDNA_STRINGPREP_ERROR:
  615. str = "Error in string preparation";
  616. break;
  617. case IDNA_PUNYCODE_ERROR:
  618. str = "Error in Punycode operation";
  619. break;
  620. case IDNA_CONTAINS_NON_LDH:
  621. str = "Illegal ASCII characters";
  622. break;
  623. case IDNA_CONTAINS_MINUS:
  624. str = "Contains minus";
  625. break;
  626. case IDNA_INVALID_LENGTH:
  627. str = "Invalid output length";
  628. break;
  629. case IDNA_NO_ACE_PREFIX:
  630. str = "No ACE prefix (\"xn--\")";
  631. break;
  632. case IDNA_ROUNDTRIP_VERIFY_ERROR:
  633. str = "Round trip verify error";
  634. break;
  635. case IDNA_CONTAINS_ACE_PREFIX:
  636. str = "Already have ACE prefix (\"xn--\")";
  637. break;
  638. case IDNA_ICONV_ERROR:
  639. str = "Locale conversion failed";
  640. break;
  641. case IDNA_MALLOC_ERROR:
  642. str = "Allocation failed";
  643. break;
  644. case IDNA_DLOPEN_ERROR:
  645. str = "dlopen() error";
  646. break;
  647. default:
  648. snprintf(buf, max, "error %d", err);
  649. str = NULL;
  650. break;
  651. }
  652. #else
  653. if((Idna_rc)err == IDNA_SUCCESS)
  654. str = "No error";
  655. else
  656. str = "Error";
  657. #endif
  658. if(str)
  659. strncpy(buf, str, max);
  660. buf[max] = '\0';
  661. return (buf);
  662. #endif
  663. }
  664. #endif /* USE_LIBIDN */
  665. #ifdef USE_WINDOWS_SSPI
  666. const char *Curl_sspi_strerror (struct connectdata *conn, int err)
  667. {
  668. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  669. char txtbuf[80];
  670. char msgbuf[sizeof(conn->syserr_buf)];
  671. char *p, *str, *msg = NULL;
  672. bool msg_formatted = FALSE;
  673. int old_errno;
  674. #endif
  675. const char *txt;
  676. char *outbuf;
  677. size_t outmax;
  678. DEBUGASSERT(conn);
  679. outbuf = conn->syserr_buf;
  680. outmax = sizeof(conn->syserr_buf)-1;
  681. *outbuf = '\0';
  682. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  683. old_errno = ERRNO;
  684. switch (err) {
  685. case SEC_E_OK:
  686. txt = "No error";
  687. break;
  688. case CRYPT_E_REVOKED:
  689. txt = "CRYPT_E_REVOKED";
  690. break;
  691. case SEC_E_ALGORITHM_MISMATCH:
  692. txt = "SEC_E_ALGORITHM_MISMATCH";
  693. break;
  694. case SEC_E_BAD_BINDINGS:
  695. txt = "SEC_E_BAD_BINDINGS";
  696. break;
  697. case SEC_E_BAD_PKGID:
  698. txt = "SEC_E_BAD_PKGID";
  699. break;
  700. case SEC_E_BUFFER_TOO_SMALL:
  701. txt = "SEC_E_BUFFER_TOO_SMALL";
  702. break;
  703. case SEC_E_CANNOT_INSTALL:
  704. txt = "SEC_E_CANNOT_INSTALL";
  705. break;
  706. case SEC_E_CANNOT_PACK:
  707. txt = "SEC_E_CANNOT_PACK";
  708. break;
  709. case SEC_E_CERT_EXPIRED:
  710. txt = "SEC_E_CERT_EXPIRED";
  711. break;
  712. case SEC_E_CERT_UNKNOWN:
  713. txt = "SEC_E_CERT_UNKNOWN";
  714. break;
  715. case SEC_E_CERT_WRONG_USAGE:
  716. txt = "SEC_E_CERT_WRONG_USAGE";
  717. break;
  718. case SEC_E_CONTEXT_EXPIRED:
  719. txt = "SEC_E_CONTEXT_EXPIRED";
  720. break;
  721. case SEC_E_CROSSREALM_DELEGATION_FAILURE:
  722. txt = "SEC_E_CROSSREALM_DELEGATION_FAILURE";
  723. break;
  724. case SEC_E_CRYPTO_SYSTEM_INVALID:
  725. txt = "SEC_E_CRYPTO_SYSTEM_INVALID";
  726. break;
  727. case SEC_E_DECRYPT_FAILURE:
  728. txt = "SEC_E_DECRYPT_FAILURE";
  729. break;
  730. case SEC_E_DELEGATION_POLICY:
  731. txt = "SEC_E_DELEGATION_POLICY";
  732. break;
  733. case SEC_E_DELEGATION_REQUIRED:
  734. txt = "SEC_E_DELEGATION_REQUIRED";
  735. break;
  736. case SEC_E_DOWNGRADE_DETECTED:
  737. txt = "SEC_E_DOWNGRADE_DETECTED";
  738. break;
  739. case SEC_E_ENCRYPT_FAILURE:
  740. txt = "SEC_E_ENCRYPT_FAILURE";
  741. break;
  742. case SEC_E_ILLEGAL_MESSAGE:
  743. txt = "SEC_E_ILLEGAL_MESSAGE";
  744. break;
  745. case SEC_E_INCOMPLETE_CREDENTIALS:
  746. txt = "SEC_E_INCOMPLETE_CREDENTIALS";
  747. break;
  748. case SEC_E_INCOMPLETE_MESSAGE:
  749. txt = "SEC_E_INCOMPLETE_MESSAGE";
  750. break;
  751. case SEC_E_INSUFFICIENT_MEMORY:
  752. txt = "SEC_E_INSUFFICIENT_MEMORY";
  753. break;
  754. case SEC_E_INTERNAL_ERROR:
  755. txt = "SEC_E_INTERNAL_ERROR";
  756. break;
  757. case SEC_E_INVALID_HANDLE:
  758. txt = "SEC_E_INVALID_HANDLE";
  759. break;
  760. case SEC_E_INVALID_PARAMETER:
  761. txt = "SEC_E_INVALID_PARAMETER";
  762. break;
  763. case SEC_E_INVALID_TOKEN:
  764. txt = "SEC_E_INVALID_TOKEN";
  765. break;
  766. case SEC_E_ISSUING_CA_UNTRUSTED:
  767. txt = "SEC_E_ISSUING_CA_UNTRUSTED";
  768. break;
  769. case SEC_E_ISSUING_CA_UNTRUSTED_KDC:
  770. txt = "SEC_E_ISSUING_CA_UNTRUSTED_KDC";
  771. break;
  772. case SEC_E_KDC_CERT_EXPIRED:
  773. txt = "SEC_E_KDC_CERT_EXPIRED";
  774. break;
  775. case SEC_E_KDC_CERT_REVOKED:
  776. txt = "SEC_E_KDC_CERT_REVOKED";
  777. break;
  778. case SEC_E_KDC_INVALID_REQUEST:
  779. txt = "SEC_E_KDC_INVALID_REQUEST";
  780. break;
  781. case SEC_E_KDC_UNABLE_TO_REFER:
  782. txt = "SEC_E_KDC_UNABLE_TO_REFER";
  783. break;
  784. case SEC_E_KDC_UNKNOWN_ETYPE:
  785. txt = "SEC_E_KDC_UNKNOWN_ETYPE";
  786. break;
  787. case SEC_E_LOGON_DENIED:
  788. txt = "SEC_E_LOGON_DENIED";
  789. break;
  790. case SEC_E_MAX_REFERRALS_EXCEEDED:
  791. txt = "SEC_E_MAX_REFERRALS_EXCEEDED";
  792. break;
  793. case SEC_E_MESSAGE_ALTERED:
  794. txt = "SEC_E_MESSAGE_ALTERED";
  795. break;
  796. case SEC_E_MULTIPLE_ACCOUNTS:
  797. txt = "SEC_E_MULTIPLE_ACCOUNTS";
  798. break;
  799. case SEC_E_MUST_BE_KDC:
  800. txt = "SEC_E_MUST_BE_KDC";
  801. break;
  802. case SEC_E_NOT_OWNER:
  803. txt = "SEC_E_NOT_OWNER";
  804. break;
  805. case SEC_E_NO_AUTHENTICATING_AUTHORITY:
  806. txt = "SEC_E_NO_AUTHENTICATING_AUTHORITY";
  807. break;
  808. case SEC_E_NO_CREDENTIALS:
  809. txt = "SEC_E_NO_CREDENTIALS";
  810. break;
  811. case SEC_E_NO_IMPERSONATION:
  812. txt = "SEC_E_NO_IMPERSONATION";
  813. break;
  814. case SEC_E_NO_IP_ADDRESSES:
  815. txt = "SEC_E_NO_IP_ADDRESSES";
  816. break;
  817. case SEC_E_NO_KERB_KEY:
  818. txt = "SEC_E_NO_KERB_KEY";
  819. break;
  820. case SEC_E_NO_PA_DATA:
  821. txt = "SEC_E_NO_PA_DATA";
  822. break;
  823. case SEC_E_NO_S4U_PROT_SUPPORT:
  824. txt = "SEC_E_NO_S4U_PROT_SUPPORT";
  825. break;
  826. case SEC_E_NO_TGT_REPLY:
  827. txt = "SEC_E_NO_TGT_REPLY";
  828. break;
  829. case SEC_E_OUT_OF_SEQUENCE:
  830. txt = "SEC_E_OUT_OF_SEQUENCE";
  831. break;
  832. case SEC_E_PKINIT_CLIENT_FAILURE:
  833. txt = "SEC_E_PKINIT_CLIENT_FAILURE";
  834. break;
  835. case SEC_E_PKINIT_NAME_MISMATCH:
  836. txt = "SEC_E_PKINIT_NAME_MISMATCH";
  837. break;
  838. case SEC_E_POLICY_NLTM_ONLY:
  839. txt = "SEC_E_POLICY_NLTM_ONLY";
  840. break;
  841. case SEC_E_QOP_NOT_SUPPORTED:
  842. txt = "SEC_E_QOP_NOT_SUPPORTED";
  843. break;
  844. case SEC_E_REVOCATION_OFFLINE_C:
  845. txt = "SEC_E_REVOCATION_OFFLINE_C";
  846. break;
  847. case SEC_E_REVOCATION_OFFLINE_KDC:
  848. txt = "SEC_E_REVOCATION_OFFLINE_KDC";
  849. break;
  850. case SEC_E_SECPKG_NOT_FOUND:
  851. txt = "SEC_E_SECPKG_NOT_FOUND";
  852. break;
  853. case SEC_E_SECURITY_QOS_FAILED:
  854. txt = "SEC_E_SECURITY_QOS_FAILED";
  855. break;
  856. case SEC_E_SHUTDOWN_IN_PROGRESS:
  857. txt = "SEC_E_SHUTDOWN_IN_PROGRESS";
  858. break;
  859. case SEC_E_SMARTCARD_CERT_EXPIRED:
  860. txt = "SEC_E_SMARTCARD_CERT_EXPIRED";
  861. break;
  862. case SEC_E_SMARTCARD_CERT_REVOKED:
  863. txt = "SEC_E_SMARTCARD_CERT_REVOKED";
  864. break;
  865. case SEC_E_SMARTCARD_LOGON_REQUIRED:
  866. txt = "SEC_E_SMARTCARD_LOGON_REQUIRED";
  867. break;
  868. case SEC_E_STRONG_CRYPTO_NOT_SUPPORTED:
  869. txt = "SEC_E_STRONG_CRYPTO_NOT_SUPPORTED";
  870. break;
  871. case SEC_E_TARGET_UNKNOWN:
  872. txt = "SEC_E_TARGET_UNKNOWN";
  873. break;
  874. case SEC_E_TIME_SKEW:
  875. txt = "SEC_E_TIME_SKEW";
  876. break;
  877. case SEC_E_TOO_MANY_PRINCIPALS:
  878. txt = "SEC_E_TOO_MANY_PRINCIPALS";
  879. break;
  880. case SEC_E_UNFINISHED_CONTEXT_DELETED:
  881. txt = "SEC_E_UNFINISHED_CONTEXT_DELETED";
  882. break;
  883. case SEC_E_UNKNOWN_CREDENTIALS:
  884. txt = "SEC_E_UNKNOWN_CREDENTIALS";
  885. break;
  886. case SEC_E_UNSUPPORTED_FUNCTION:
  887. txt = "SEC_E_UNSUPPORTED_FUNCTION";
  888. break;
  889. case SEC_E_UNSUPPORTED_PREAUTH:
  890. txt = "SEC_E_UNSUPPORTED_PREAUTH";
  891. break;
  892. case SEC_E_UNTRUSTED_ROOT:
  893. txt = "SEC_E_UNTRUSTED_ROOT";
  894. break;
  895. case SEC_E_WRONG_CREDENTIAL_HANDLE:
  896. txt = "SEC_E_WRONG_CREDENTIAL_HANDLE";
  897. break;
  898. case SEC_E_WRONG_PRINCIPAL:
  899. txt = "SEC_E_WRONG_PRINCIPAL";
  900. break;
  901. case SEC_I_COMPLETE_AND_CONTINUE:
  902. txt = "SEC_I_COMPLETE_AND_CONTINUE";
  903. break;
  904. case SEC_I_COMPLETE_NEEDED:
  905. txt = "SEC_I_COMPLETE_NEEDED";
  906. break;
  907. case SEC_I_CONTEXT_EXPIRED:
  908. txt = "SEC_I_CONTEXT_EXPIRED";
  909. break;
  910. case SEC_I_CONTINUE_NEEDED:
  911. txt = "SEC_I_CONTINUE_NEEDED";
  912. break;
  913. case SEC_I_INCOMPLETE_CREDENTIALS:
  914. txt = "SEC_I_INCOMPLETE_CREDENTIALS";
  915. break;
  916. case SEC_I_LOCAL_LOGON:
  917. txt = "SEC_I_LOCAL_LOGON";
  918. break;
  919. case SEC_I_NO_LSA_CONTEXT:
  920. txt = "SEC_I_NO_LSA_CONTEXT";
  921. break;
  922. case SEC_I_RENEGOTIATE:
  923. txt = "SEC_I_RENEGOTIATE";
  924. break;
  925. case SEC_I_SIGNATURE_NEEDED:
  926. txt = "SEC_I_SIGNATURE_NEEDED";
  927. break;
  928. default:
  929. txt = "Unknown error";
  930. }
  931. if(err == SEC_E_OK)
  932. strncpy(outbuf, txt, outmax);
  933. else if(err == SEC_E_ILLEGAL_MESSAGE)
  934. snprintf(outbuf, outmax,
  935. "SEC_E_ILLEGAL_MESSAGE (0x%04X%04X) - This error usually occurs "
  936. "when a fatal SSL/TLS alert is received (e.g. handshake failed). "
  937. "More detail may be available in the Windows System event log.",
  938. (err >> 16) & 0xffff, err & 0xffff);
  939. else {
  940. str = txtbuf;
  941. snprintf(txtbuf, sizeof(txtbuf), "%s (0x%04X%04X)",
  942. txt, (err >> 16) & 0xffff, err & 0xffff);
  943. txtbuf[sizeof(txtbuf)-1] = '\0';
  944. #ifdef _WIN32_WCE
  945. {
  946. wchar_t wbuf[256];
  947. wbuf[0] = L'\0';
  948. if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
  949. FORMAT_MESSAGE_IGNORE_INSERTS,
  950. NULL, err, LANG_NEUTRAL,
  951. wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL)) {
  952. wcstombs(msgbuf, wbuf, sizeof(msgbuf)-1);
  953. msg_formatted = TRUE;
  954. }
  955. }
  956. #else
  957. if(FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
  958. FORMAT_MESSAGE_IGNORE_INSERTS,
  959. NULL, err, LANG_NEUTRAL,
  960. msgbuf, sizeof(msgbuf)-1, NULL)) {
  961. msg_formatted = TRUE;
  962. }
  963. #endif
  964. if(msg_formatted) {
  965. msgbuf[sizeof(msgbuf)-1] = '\0';
  966. /* strip trailing '\r\n' or '\n' */
  967. if((p = strrchr(msgbuf, '\n')) != NULL && (p - msgbuf) >= 2)
  968. *p = '\0';
  969. if((p = strrchr(msgbuf, '\r')) != NULL && (p - msgbuf) >= 1)
  970. *p = '\0';
  971. msg = msgbuf;
  972. }
  973. if(msg)
  974. snprintf(outbuf, outmax, "%s - %s", str, msg);
  975. else
  976. strncpy(outbuf, str, outmax);
  977. }
  978. if(old_errno != ERRNO)
  979. SET_ERRNO(old_errno);
  980. #else
  981. if(err == SEC_E_OK)
  982. txt = "No error";
  983. else
  984. txt = "Error";
  985. strncpy(outbuf, txt, outmax);
  986. #endif
  987. outbuf[outmax] = '\0';
  988. return outbuf;
  989. }
  990. #endif /* USE_WINDOWS_SSPI */