1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
25 #include <curl/curl.h>
36 #define _MPRINTF_REPLACE /* use our functions only */
37 #include <curl/mprintf.h>
39 #ifdef HAVE_NO_STRERROR_R_DECL
40 #ifdef HAVE_POSIX_STRERROR_R
41 /* seen on AIX 5100-02 gcc 2.9 */
42 extern int strerror_r(int errnum, char *strerrbuf, size_t buflen);
44 extern char *strerror_r(int errnum, char *buf, size_t buflen);
49 curl_easy_strerror(CURLcode error)
55 case CURLE_UNSUPPORTED_PROTOCOL:
56 return "unsupported protocol";
58 case CURLE_FAILED_INIT:
61 case CURLE_URL_MALFORMAT:
62 return "URL using bad/illegal format or missing URL";
64 case CURLE_COULDNT_RESOLVE_PROXY:
65 return "couldnt resolve proxy name";
67 case CURLE_COULDNT_RESOLVE_HOST:
68 return "couldnt resolve host name";
70 case CURLE_COULDNT_CONNECT:
71 return "couldn't connect to server";
73 case CURLE_FTP_WEIRD_SERVER_REPLY:
74 return "FTP: weird server reply";
76 case CURLE_FTP_ACCESS_DENIED:
77 return "FTP: access denied";
79 case CURLE_FTP_USER_PASSWORD_INCORRECT:
80 return "FTP: user and/or password incorrect";
82 case CURLE_FTP_WEIRD_PASS_REPLY:
83 return "FTP: unknown PASS reply";
85 case CURLE_FTP_WEIRD_USER_REPLY:
86 return "FTP: unknown USER reply";
88 case CURLE_FTP_WEIRD_PASV_REPLY:
89 return "FTP: unknown PASV reply";
91 case CURLE_FTP_WEIRD_227_FORMAT:
92 return "FTP: unknown 227 response format";
94 case CURLE_FTP_CANT_GET_HOST:
95 return "FTP: can't figure out the host in the PASV response";
97 case CURLE_FTP_CANT_RECONNECT:
98 return "FTP: can't connect to server the response code is unknown";
100 case CURLE_FTP_COULDNT_SET_BINARY:
101 return "FTP: couldn't set binary mode";
103 case CURLE_PARTIAL_FILE:
104 return "Transferred a partial file";
106 case CURLE_FTP_COULDNT_RETR_FILE:
107 return "FTP: couldn't retrieve (RETR failed) the specified file";
109 case CURLE_FTP_WRITE_ERROR:
110 return "FTP: the post-transfer acknowledge response was not OK";
112 case CURLE_FTP_QUOTE_ERROR:
113 return "FTP: a quote command returned error";
115 case CURLE_HTTP_RETURNED_ERROR:
116 return "HTTP response code said error";
118 case CURLE_WRITE_ERROR:
119 return "failed writing received data to disk/application";
121 case CURLE_FTP_COULDNT_STOR_FILE:
122 return "failed FTP upload (the STOR command)";
124 case CURLE_READ_ERROR:
125 return "failed to open/read local data from file/application";
127 case CURLE_OUT_OF_MEMORY:
128 return "out of memory";
130 case CURLE_OPERATION_TIMEOUTED:
131 return "a timeout was reached";
133 case CURLE_FTP_COULDNT_SET_ASCII:
134 return "FTP could not set ASCII mode (TYPE A)";
136 case CURLE_FTP_PORT_FAILED:
137 return "FTP command PORT failed";
139 case CURLE_FTP_COULDNT_USE_REST:
140 return "FTP command REST failed";
142 case CURLE_FTP_COULDNT_GET_SIZE:
143 return "FTP command SIZE failed";
145 case CURLE_HTTP_RANGE_ERROR:
146 return "a range was requested but the server did not deliver it";
148 case CURLE_HTTP_POST_ERROR:
149 return "internal problem setting up the POST";
151 case CURLE_SSL_CONNECT_ERROR:
152 return "SSL connect error";
154 case CURLE_FTP_BAD_DOWNLOAD_RESUME:
155 return "couldn't resume FTP download";
157 case CURLE_FILE_COULDNT_READ_FILE:
158 return "couldn't read a file:// file";
160 case CURLE_LDAP_CANNOT_BIND:
161 return "LDAP: cannot bind";
163 case CURLE_LDAP_SEARCH_FAILED:
164 return "LDAP: search failed";
166 case CURLE_LIBRARY_NOT_FOUND:
167 return "a required shared library was not found";
169 case CURLE_FUNCTION_NOT_FOUND:
170 return "a required function in the shared library was not found";
172 case CURLE_ABORTED_BY_CALLBACK:
173 return "the operation was aborted by an application callback";
175 case CURLE_BAD_FUNCTION_ARGUMENT:
176 return "a libcurl function was given a bad argument";
178 case CURLE_INTERFACE_FAILED:
179 return "failed binding local connection end";
181 case CURLE_TOO_MANY_REDIRECTS :
182 return "number of redirects hit maximum amount";
184 case CURLE_UNKNOWN_TELNET_OPTION:
185 return "User specified an unknown option";
187 case CURLE_TELNET_OPTION_SYNTAX :
188 return "Malformed telnet option";
190 case CURLE_SSL_PEER_CERTIFICATE:
191 return "SSL peer certificate was not ok";
193 case CURLE_GOT_NOTHING:
194 return "server returned nothing (no headers, no data)";
196 case CURLE_SSL_ENGINE_NOTFOUND:
197 return "SSL crypto engine not found";
199 case CURLE_SSL_ENGINE_SETFAILED:
200 return "can not set SSL crypto engine as default";
202 case CURLE_SEND_ERROR:
203 return "failed sending data to the peer";
205 case CURLE_RECV_ERROR:
206 return "failure when receiving data from the peer";
208 case CURLE_SHARE_IN_USE:
209 return "share is already in use";
211 case CURLE_SSL_CERTPROBLEM:
212 return "problem with the local SSL certificate";
214 case CURLE_SSL_CIPHER:
215 return "couldn't use specified SSL cipher";
217 case CURLE_SSL_CACERT:
218 return "problem with the SSL CA cert (path? access rights?)";
220 case CURLE_BAD_CONTENT_ENCODING:
221 return "Unrecognized HTTP Content-Encoding";
223 case CURLE_LDAP_INVALID_URL:
224 return "Invalid LDAP URL";
226 case CURLE_FILESIZE_EXCEEDED:
227 return "Maximum file size exceeded";
229 case CURLE_FTP_SSL_FAILED:
230 return "Requested FTP SSL level failed";
232 case CURLE_URL_MALFORMAT_USER: /* not used by current libcurl */
233 case CURLE_MALFORMAT_USER: /* not used by current libcurl */
234 case CURLE_BAD_CALLING_ORDER: /* not used by current libcurl */
235 case CURLE_BAD_PASSWORD_ENTERED:/* not used by current libcurl */
236 case CURLE_OBSOLETE: /* not used by current libcurl */
241 * By using a switch, gcc -Wall will complain about enum values
242 * which do not appear, helping keep this function up-to-date.
243 * By using gcc -Wall -Werror, you can't forget.
245 * A table would not have the same benefit. Most compilers will
246 * generate code very similar to a table in any case, so there
247 * is little performance gain from a table. And something is broken
248 * for the user's application, anyways, so does it matter how fast
251 * The line number for the error will be near this comment, which
252 * is why it is here, and not at the start of the switch.
254 return "unknown error";
258 curl_multi_strerror(CURLMcode error)
261 case CURLM_CALL_MULTI_PERFORM:
262 return "please call curl_multi_perform() soon";
267 case CURLM_BAD_HANDLE:
268 return "invalid multi handle";
270 case CURLM_BAD_EASY_HANDLE:
271 return "invalid easy handle";
273 case CURLM_OUT_OF_MEMORY:
274 return "out of memory";
276 case CURLM_INTERNAL_ERROR:
277 return "internal error";
283 return "unknown error";
287 curl_share_strerror(CURLSHcode error)
293 case CURLSHE_BAD_OPTION:
294 return "unknown share option";
297 return "share currently in use";
299 case CURLSHE_INVALID:
300 return "invalid share handle";
303 return "out of memory";
309 return "CURLSH unknown";
312 #if defined(WIN32) && !defined(__CYGWIN__)
314 /* This function handles most / all (?) Winsock errors cURL is able to produce.
317 get_winsock_error (int err, char *buf, size_t len)
323 p = "Call interrupted.";
335 p = "Invalid arguments";
338 p = "Out of file descriptors";
341 p = "Call would block";
345 p = "Blocking call in progress";
348 p = "Descriptor is not a socket.";
350 case WSAEDESTADDRREQ:
351 p = "Need destination address";
354 p = "Bad message size";
360 p = "Protocol option is unsupported";
362 case WSAEPROTONOSUPPORT:
363 p = "Protocol is unsupported";
365 case WSAESOCKTNOSUPPORT:
366 p = "Socket is unsupported";
369 p = "Operation not supported";
371 case WSAEAFNOSUPPORT:
372 p = "Address family not supported";
374 case WSAEPFNOSUPPORT:
375 p = "Protocol family not supported";
378 p = "Address already in use";
380 case WSAEADDRNOTAVAIL:
381 p = "Address not available";
387 p = "Network unreachable";
390 p = "Network has been reset";
392 case WSAECONNABORTED:
393 p = "Connection was aborted";
396 p = "Connection was reset";
399 p = "No buffer space";
402 p = "Socket is already connected";
405 p = "Socket is not connected";
408 p = "Socket has been shut down";
410 case WSAETOOMANYREFS:
411 p = "Too many references";
416 case WSAECONNREFUSED:
417 p = "Connection refused";
422 case WSAENAMETOOLONG:
428 case WSAEHOSTUNREACH:
429 p = "Host unreachable";
435 p = "Process limit reached";
438 p = "Too many users";
444 p = "Something is stale";
453 /* Extended Winsock errors */
455 p = "Winsock library is not ready";
457 case WSANOTINITIALISED:
458 p = "Winsock library not initalised";
460 case WSAVERNOTSUPPORTED:
461 p = "Winsock version not supported.";
464 /* getXbyY() errors (already handled in herrmsg):
465 * Authoritative Answer: Host not found */
466 case WSAHOST_NOT_FOUND:
467 p = "Host not found";
470 /* Non-Authoritative: Host not found, or SERVERFAIL */
472 p = "Host not found, try again";
475 /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
477 p = "Unrecoverable error in call to nameserver";
480 /* Valid name, no data record of requested type */
482 p = "No data record of requested type";
488 strncpy (buf, p, len);
492 #endif /* WIN32 && !__CYGWIN__ */
495 * Our thread-safe and smart strerror() replacement.
497 * The 'err' argument passed in to this function MUST be a true errno number
498 * as reported on this system. We do no range checking on the number before
499 * we pass it to the "number-to-message" convertion function and there might
500 * be systems that don't do proper range checking in there themselves.
502 * We don't do range checking (on systems other than Windows) since there is
503 * no good reliable and portable way to do it.
505 const char *Curl_strerror(struct connectdata *conn, int err)
511 curlassert(err >= 0);
513 buf = conn->syserr_buf;
514 max = sizeof(conn->syserr_buf)-1;
517 #if defined(WIN32) && !defined(__CYGWIN__)
524 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
525 LANG_NEUTRAL, wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL);
526 wcstombs(buf,wbuf,max);
531 /* 'sys_nerr' is the maximum errno number, it is not widely portable */
532 if (err >= 0 && err < sys_nerr)
533 strncpy(buf, strerror(err), max);
535 if (!get_winsock_error(err, buf, max) &&
536 !FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
537 LANG_NEUTRAL, buf, max, NULL))
538 snprintf(buf, max, "Unknown error %d (%#x)", err, err);
541 #else /* not native Windows coming up */
543 /* These should be atomic and hopefully thread-safe */
544 #ifdef HAVE_STRERROR_R
545 /* There are two different APIs for strerror_r(). The POSIX and the GLIBC
547 #ifdef HAVE_POSIX_STRERROR_R
548 strerror_r(err, buf, max);
549 /* this may set errno to ERANGE if insufficient storage was supplied via
550 'strerrbuf' and 'buflen' to contain the generated message string, or
551 EINVAL if the value of 'errnum' is not a valid error number.*/
554 /* HAVE_GLIBC_STRERROR_R */
556 char *msg = strerror_r(err, buffer, sizeof(buffer));
557 /* this version of strerror_r() only *might* use the buffer we pass to
558 the function, but it always returns the error message as a pointer,
559 so we must copy that string unconditionally */
560 strncpy(buf, msg, max);
562 #endif /* end of HAVE_GLIBC_STRERROR_R */
563 #else /* HAVE_STRERROR_R */
564 strncpy(buf, strerror(err), max);
565 #endif /* end of HAVE_STRERROR_R */
566 #endif /* end of ! Windows */
568 buf[max] = '\0'; /* make sure the string is zero terminated */
570 /* strip trailing '\r\n' or '\n'. */
571 if ((p = strrchr(buf,'\n')) != NULL && (p - buf) >= 2)
573 if ((p = strrchr(buf,'\r')) != NULL && (p - buf) >= 1)
580 * Return error-string for libidn status as returned from idna_to_ascii_lz().
582 const char *Curl_idn_strerror (struct connectdata *conn, int err)
584 #ifdef HAVE_IDNA_STRERROR
586 return idna_strerror((Idna_rc) err);
594 buf = conn->syserr_buf;
595 max = sizeof(conn->syserr_buf)-1;
597 switch ((Idna_rc)err) {
601 case IDNA_STRINGPREP_ERROR:
602 str = "Error in string preparation";
604 case IDNA_PUNYCODE_ERROR:
605 str = "Error in Punycode operation";
607 case IDNA_CONTAINS_NON_LDH:
608 str = "Illegal ASCII characters";
610 case IDNA_CONTAINS_MINUS:
611 str = "Contains minus";
613 case IDNA_INVALID_LENGTH:
614 str = "Invalid output length";
616 case IDNA_NO_ACE_PREFIX:
617 str = "No ACE prefix (\"xn--\")";
619 case IDNA_ROUNDTRIP_VERIFY_ERROR:
620 str = "Roundtrip verify error";
622 case IDNA_CONTAINS_ACE_PREFIX:
623 str = "Already have ACE prefix (\"xn--\")";
625 case IDNA_ICONV_ERROR:
626 str = "Locale conversion failed";
628 case IDNA_MALLOC_ERROR:
629 str = "Allocation failed";
631 case IDNA_DLOPEN_ERROR:
632 str = "dlopen() error";
635 snprintf(buf, max, "error %d", (int)err);
640 strncpy(buf, str, max);
645 #endif /* USE_LIBIDN */