strerror.c 28 KB

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