strerror.c 28 KB

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