2
0

strerror.c 28 KB

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