strerror.c 28 KB

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