strerror.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2004, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "setup.h"
  23. #include <curl/curl.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #include "strerror.h"
  28. #define _MPRINTF_REPLACE /* use our functions only */
  29. #include <curl/mprintf.h>
  30. #ifdef HAVE_NO_STRERROR_R_DECL
  31. #ifdef HAVE_POSIX_STRERROR_R
  32. /* seen on AIX 5100-02 gcc 2.9 */
  33. extern int strerror_r(int errnum, char *strerrbuf, size_t buflen);
  34. #else
  35. extern char *strerror_r(int errnum, char *buf, size_t buflen);
  36. #endif
  37. #endif
  38. const char *
  39. curl_easy_strerror(CURLcode error)
  40. {
  41. switch (error) {
  42. case CURLE_OK:
  43. return "no error";
  44. case CURLE_UNSUPPORTED_PROTOCOL:
  45. return "unsupported protocol";
  46. case CURLE_FAILED_INIT:
  47. return "failed init";
  48. case CURLE_URL_MALFORMAT:
  49. return "URL using bad/illegal format or missing URL";
  50. case CURLE_COULDNT_RESOLVE_PROXY:
  51. return "couldnt resolve proxy";
  52. case CURLE_COULDNT_RESOLVE_HOST:
  53. return "couldnt resolve host";
  54. case CURLE_COULDNT_CONNECT:
  55. return "couldn't connect";
  56. case CURLE_FTP_WEIRD_SERVER_REPLY:
  57. return "FTP: weird server reply";
  58. case CURLE_FTP_ACCESS_DENIED:
  59. return "FTP: access denied";
  60. case CURLE_FTP_USER_PASSWORD_INCORRECT:
  61. return "FTP: user and/or password incorrect";
  62. case CURLE_FTP_WEIRD_PASS_REPLY:
  63. return "FTP: unknown PASS reply";
  64. case CURLE_FTP_WEIRD_USER_REPLY:
  65. return "FTP: unknown USER reply";
  66. case CURLE_FTP_WEIRD_PASV_REPLY:
  67. return "FTP: unknown PASV reply";
  68. case CURLE_FTP_WEIRD_227_FORMAT:
  69. return "FTP: unknown 227 response format";
  70. case CURLE_FTP_CANT_GET_HOST:
  71. return "FTP: can't figure out the host in the PASV response";
  72. case CURLE_FTP_CANT_RECONNECT:
  73. return "FTP: can't connect to server the response code is unknown";
  74. case CURLE_FTP_COULDNT_SET_BINARY:
  75. return "FTP: couldn't set binary mode";
  76. case CURLE_PARTIAL_FILE:
  77. return "Transferred a partial file";
  78. case CURLE_FTP_COULDNT_RETR_FILE:
  79. return "FTP: couldn't retrieve (RETR failed) the specified file";
  80. case CURLE_FTP_WRITE_ERROR:
  81. return "FTP: the post-transfer acknowledge response was not OK";
  82. case CURLE_FTP_QUOTE_ERROR:
  83. return "FTP: a quote command returned error";
  84. case CURLE_HTTP_RETURNED_ERROR:
  85. return "HTTP response code said error";
  86. case CURLE_WRITE_ERROR:
  87. return "failed writing received data to disk/application";
  88. case CURLE_FTP_COULDNT_STOR_FILE:
  89. return "failed FTP upload (the STOR command)";
  90. case CURLE_READ_ERROR:
  91. return "failed to open/read local data from file/application";
  92. case CURLE_OUT_OF_MEMORY:
  93. return "out of memory";
  94. case CURLE_OPERATION_TIMEOUTED:
  95. return "a timeout was reached";
  96. case CURLE_FTP_COULDNT_SET_ASCII:
  97. return "FTP could not set ASCII mode (TYPE A)";
  98. case CURLE_FTP_PORT_FAILED:
  99. return "FTP command PORT failed";
  100. case CURLE_FTP_COULDNT_USE_REST:
  101. return "FTP command REST failed";
  102. case CURLE_FTP_COULDNT_GET_SIZE:
  103. return "FTP command SIZE failed";
  104. case CURLE_HTTP_RANGE_ERROR:
  105. return "a range was requested but the server did not deliver it";
  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_FTP_BAD_DOWNLOAD_RESUME:
  111. return "couldn't resume FTP 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_LIBRARY_NOT_FOUND:
  119. return "a required shared library was not found";
  120. case CURLE_FUNCTION_NOT_FOUND:
  121. return "a required function in the shared library was not found";
  122. case CURLE_ABORTED_BY_CALLBACK:
  123. return "the operation was aborted by an application callback";
  124. case CURLE_BAD_FUNCTION_ARGUMENT:
  125. return "a libcurl function was given a bad argument";
  126. case CURLE_INTERFACE_FAILED:
  127. return "failed binding local connection end";
  128. case CURLE_TOO_MANY_REDIRECTS :
  129. return "number of redirects hit maximum amount";
  130. case CURLE_UNKNOWN_TELNET_OPTION:
  131. return "User specified an unknown option";
  132. case CURLE_TELNET_OPTION_SYNTAX :
  133. return "Malformed telnet option";
  134. case CURLE_SSL_PEER_CERTIFICATE:
  135. return "SSL peer certificate was not ok";
  136. case CURLE_GOT_NOTHING:
  137. return "server returned nothing (no headers, no data)";
  138. case CURLE_SSL_ENGINE_NOTFOUND:
  139. return "SSL crypto engine not found";
  140. case CURLE_SSL_ENGINE_SETFAILED:
  141. return "can not set SSL crypto engine as default";
  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_SHARE_IN_USE:
  147. return "share is already in use";
  148. case CURLE_SSL_CERTPROBLEM:
  149. return "problem with the local SSL certificate";
  150. case CURLE_SSL_CIPHER:
  151. return "couldn't use specified SSL cipher";
  152. case CURLE_SSL_CACERT:
  153. return "problem with the SSL CA cert (path? access rights?)";
  154. case CURLE_BAD_CONTENT_ENCODING:
  155. return "Unrecognized HTTP Content-Encoding";
  156. case CURLE_LDAP_INVALID_URL:
  157. return "Invalid LDAP URL";
  158. case CURLE_FILESIZE_EXCEEDED:
  159. return "Maximum file size exceeded";
  160. case CURLE_FTP_SSL_FAILED:
  161. return "Requested FTP SSL level failed";
  162. case CURLE_URL_MALFORMAT_USER: /* not used by current libcurl */
  163. case CURLE_MALFORMAT_USER: /* not used by current libcurl */
  164. case CURLE_BAD_CALLING_ORDER: /* not used by current libcurl */
  165. case CURLE_BAD_PASSWORD_ENTERED:/* not used by current libcurl */
  166. case CURLE_OBSOLETE: /* not used by current libcurl */
  167. case CURL_LAST:
  168. break;
  169. }
  170. /*
  171. * By using a switch, gcc -Wall will complain about enum values
  172. * which do not appear, helping keep this function up-to-date.
  173. * By using gcc -Wall -Werror, you can't forget.
  174. *
  175. * A table would not have the same benefit. Most compilers will
  176. * generate code very similar to a table in any case, so there
  177. * is little performance gain from a table. And something is broken
  178. * for the user's application, anyways, so does it matter how fast
  179. * it _doesn't_ work?
  180. *
  181. * The line number for the error will be near this comment, which
  182. * is why it is here, and not at the start of the switch.
  183. */
  184. return "unknown error";
  185. }
  186. const char *
  187. curl_multi_strerror(CURLMcode error)
  188. {
  189. switch (error) {
  190. case CURLM_CALL_MULTI_PERFORM:
  191. return "please call curl_multi_perform() soon";
  192. case CURLM_OK:
  193. return "no error";
  194. case CURLM_BAD_HANDLE:
  195. return "invalid multi handle";
  196. case CURLM_BAD_EASY_HANDLE:
  197. return "invalid easy handle";
  198. case CURLM_OUT_OF_MEMORY:
  199. return "out of memory";
  200. case CURLM_INTERNAL_ERROR:
  201. return "internal error";
  202. case CURLM_LAST:
  203. break;
  204. }
  205. return "unknown error";
  206. }
  207. const char *
  208. curl_share_strerror(CURLSHcode error)
  209. {
  210. switch (error) {
  211. case CURLSHE_OK:
  212. return "no error";
  213. case CURLSHE_BAD_OPTION:
  214. return "unknown share option";
  215. case CURLSHE_IN_USE:
  216. return "share currently in use";
  217. case CURLSHE_INVALID:
  218. return "invalid share handle";
  219. case CURLSHE_NOMEM:
  220. return "out of memory";
  221. case CURLSHE_LAST:
  222. break;
  223. }
  224. return "CURLSH unknown";
  225. }
  226. #if defined(WIN32) && !defined(__CYGWIN__)
  227. /* This function handles most / all (?) Winsock errors cURL is able to produce.
  228. */
  229. static const char *
  230. get_winsock_error (int err, char *buf, size_t len)
  231. {
  232. char *p;
  233. switch (err) {
  234. case WSAEINTR:
  235. p = "Call interrupted.";
  236. break;
  237. case WSAEBADF:
  238. p = "Bad file";
  239. break;
  240. case WSAEACCES:
  241. p = "Bad access";
  242. break;
  243. case WSAEFAULT:
  244. p = "Bad argument";
  245. break;
  246. case WSAEINVAL:
  247. p = "Invalid arguments";
  248. break;
  249. case WSAEMFILE:
  250. p = "Out of file descriptors";
  251. break;
  252. case WSAEWOULDBLOCK:
  253. p = "Call would block";
  254. break;
  255. case WSAEINPROGRESS:
  256. case WSAEALREADY:
  257. p = "Blocking call in progress";
  258. break;
  259. case WSAENOTSOCK:
  260. p = "Descriptor is not a socket.";
  261. break;
  262. case WSAEDESTADDRREQ:
  263. p = "Need destination address";
  264. break;
  265. case WSAEMSGSIZE:
  266. p = "Bad message size";
  267. break;
  268. case WSAEPROTOTYPE:
  269. p = "Bad protocol";
  270. break;
  271. case WSAENOPROTOOPT:
  272. p = "Protocol option is unsupported";
  273. break;
  274. case WSAEPROTONOSUPPORT:
  275. p = "Protocol is unsupported";
  276. break;
  277. case WSAESOCKTNOSUPPORT:
  278. p = "Socket is unsupported";
  279. break;
  280. case WSAEOPNOTSUPP:
  281. p = "Operation not supported";
  282. break;
  283. case WSAEAFNOSUPPORT:
  284. p = "Address family not supported";
  285. break;
  286. case WSAEPFNOSUPPORT:
  287. p = "Protocol family not supported";
  288. break;
  289. case WSAEADDRINUSE:
  290. p = "Address already in use";
  291. break;
  292. case WSAEADDRNOTAVAIL:
  293. p = "Address not available";
  294. break;
  295. case WSAENETDOWN:
  296. p = "Network down";
  297. break;
  298. case WSAENETUNREACH:
  299. p = "Network unreachable";
  300. break;
  301. case WSAENETRESET:
  302. p = "Network has been reset";
  303. break;
  304. case WSAECONNABORTED:
  305. p = "Connection was aborted";
  306. break;
  307. case WSAECONNRESET:
  308. p = "Connection was reset";
  309. break;
  310. case WSAENOBUFS:
  311. p = "No buffer space";
  312. break;
  313. case WSAEISCONN:
  314. p = "Socket is already connected";
  315. break;
  316. case WSAENOTCONN:
  317. p = "Socket is not connected";
  318. break;
  319. case WSAESHUTDOWN:
  320. p = "Socket has been shut down";
  321. break;
  322. case WSAETOOMANYREFS:
  323. p = "Too many references";
  324. break;
  325. case WSAETIMEDOUT:
  326. p = "Timed out";
  327. break;
  328. case WSAECONNREFUSED:
  329. p = "Connection refused";
  330. break;
  331. case WSAELOOP:
  332. p = "Loop??";
  333. break;
  334. case WSAENAMETOOLONG:
  335. p = "Name too long";
  336. break;
  337. case WSAEHOSTDOWN:
  338. p = "Host down";
  339. break;
  340. case WSAEHOSTUNREACH:
  341. p = "Host unreachable";
  342. break;
  343. case WSAENOTEMPTY:
  344. p = "Not empty";
  345. break;
  346. case WSAEPROCLIM:
  347. p = "Process limit reached";
  348. break;
  349. case WSAEUSERS:
  350. p = "Too many users";
  351. break;
  352. case WSAEDQUOT:
  353. p = "Bad quota";
  354. break;
  355. case WSAESTALE:
  356. p = "Something is stale";
  357. break;
  358. case WSAEREMOTE:
  359. p = "Remote error";
  360. break;
  361. case WSAEDISCON:
  362. p = "Disconnected";
  363. break;
  364. /* Extended Winsock errors */
  365. case WSASYSNOTREADY:
  366. p = "Winsock library is not ready";
  367. break;
  368. case WSANOTINITIALISED:
  369. p = "Winsock library not initalised";
  370. break;
  371. case WSAVERNOTSUPPORTED:
  372. p = "Winsock version not supported.";
  373. break;
  374. /* getXbyY() errors (already handled in herrmsg):
  375. * Authoritative Answer: Host not found */
  376. case WSAHOST_NOT_FOUND:
  377. p = "Host not found";
  378. break;
  379. /* Non-Authoritative: Host not found, or SERVERFAIL */
  380. case WSATRY_AGAIN:
  381. p = "Host not found, try again";
  382. break;
  383. /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
  384. case WSANO_RECOVERY:
  385. p = "Unrecoverable error in call to nameserver";
  386. break;
  387. /* Valid name, no data record of requested type */
  388. case WSANO_DATA:
  389. p = "No data record of requested type";
  390. break;
  391. default:
  392. return NULL;
  393. }
  394. strncpy (buf, p, len);
  395. buf [len-1] = '\0';
  396. return buf;
  397. }
  398. #endif /* WIN32 && !__CYGWIN__ */
  399. /*
  400. * Our thread-safe and smart strerror() replacement.
  401. *
  402. * The 'err' argument passed in to this function MUST be a true errno number
  403. * as reported on this system. We do no range checking on the number before
  404. * we pass it to the "number-to-message" convertion function and there might
  405. * be systems that don't do proper range checking in there themselves.
  406. *
  407. * We don't do range checking (on systems other than Windows) since there is
  408. * no good reliable and portable way to do it.
  409. */
  410. const char *Curl_strerror(struct connectdata *conn, int err)
  411. {
  412. char *buf, *p;
  413. size_t max;
  414. curlassert(conn);
  415. curlassert(err >= 0);
  416. buf = conn->syserr_buf;
  417. max = sizeof(conn->syserr_buf)-1;
  418. *buf = '\0';
  419. #if defined(WIN32) && !defined(__CYGWIN__)
  420. /* 'sys_nerr' is the maximum errno number, it is not widely portable */
  421. if (err >= 0 && err < sys_nerr)
  422. strncpy(buf, strerror(err), max);
  423. else {
  424. if (!get_winsock_error (err, buf, max) &&
  425. !FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
  426. LANG_NEUTRAL, buf, max, NULL))
  427. snprintf(buf, max, "Unknown error %d (%#x)", err, err);
  428. }
  429. #else /* not native Windows coming up */
  430. /* These should be atomic and hopefully thread-safe */
  431. #ifdef HAVE_STRERROR_R
  432. /* There are two different APIs for strerror_r(). The POSIX and the GLIBC
  433. versions. */
  434. #ifdef HAVE_POSIX_STRERROR_R
  435. strerror_r(err, buf, max);
  436. /* this may set errno to ERANGE if insufficient storage was supplied via
  437. 'strerrbuf' and 'buflen' to contain the generated message string, or
  438. EINVAL if the value of 'errnum' is not a valid error number.*/
  439. #else
  440. {
  441. /* HAVE_GLIBC_STRERROR_R */
  442. char buffer[256];
  443. char *msg = strerror_r(err, buffer, sizeof(buffer));
  444. /* this version of strerror_r() only *might* use the buffer we pass to
  445. the function, but it always returns the error message as a pointer,
  446. so we must copy that string unconditionally */
  447. if ( !msg )
  448. {
  449. msg = "Unknown System Error";
  450. }
  451. strncpy(buf, msg, max);
  452. }
  453. #endif /* end of HAVE_GLIBC_STRERROR_R */
  454. #else /* HAVE_STRERROR_R */
  455. {
  456. char *msg = strerror(err);
  457. if ( !msg )
  458. {
  459. msg = "Unknown System Error";
  460. }
  461. strncpy(buf, msg, max);
  462. }
  463. #endif /* end of HAVE_STRERROR_R */
  464. #endif /* end of ! Windows */
  465. buf[max] = '\0'; /* make sure the string is zero terminated */
  466. /* strip trailing '\r\n' or '\n'. */
  467. if ((p = strrchr(buf,'\n')) != NULL && (p - buf) >= 2)
  468. *p = '\0';
  469. if ((p = strrchr(buf,'\r')) != NULL && (p - buf) >= 1)
  470. *p = '\0';
  471. return buf;
  472. }