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)
51 #ifndef CURL_DISABLE_VERBOSE_STRINGS
56 case CURLE_UNSUPPORTED_PROTOCOL:
57 return "unsupported protocol";
59 case CURLE_FAILED_INIT:
62 case CURLE_URL_MALFORMAT:
63 return "URL using bad/illegal format or missing URL";
65 case CURLE_COULDNT_RESOLVE_PROXY:
66 return "couldnt resolve proxy name";
68 case CURLE_COULDNT_RESOLVE_HOST:
69 return "couldnt resolve host name";
71 case CURLE_COULDNT_CONNECT:
72 return "couldn't connect to server";
74 case CURLE_FTP_WEIRD_SERVER_REPLY:
75 return "FTP: weird server reply";
77 case CURLE_FTP_ACCESS_DENIED:
78 return "FTP: access denied";
80 case CURLE_FTP_USER_PASSWORD_INCORRECT:
81 return "FTP: user and/or password incorrect";
83 case CURLE_FTP_WEIRD_PASS_REPLY:
84 return "FTP: unknown PASS reply";
86 case CURLE_FTP_WEIRD_USER_REPLY:
87 return "FTP: unknown USER reply";
89 case CURLE_FTP_WEIRD_PASV_REPLY:
90 return "FTP: unknown PASV reply";
92 case CURLE_FTP_WEIRD_227_FORMAT:
93 return "FTP: unknown 227 response format";
95 case CURLE_FTP_CANT_GET_HOST:
96 return "FTP: can't figure out the host in the PASV response";
98 case CURLE_FTP_CANT_RECONNECT:
99 return "FTP: can't connect to server the response code is unknown";
101 case CURLE_FTP_COULDNT_SET_BINARY:
102 return "FTP: couldn't set binary mode";
104 case CURLE_PARTIAL_FILE:
105 return "Transferred a partial file";
107 case CURLE_FTP_COULDNT_RETR_FILE:
108 return "FTP: couldn't retrieve (RETR failed) the specified file";
110 case CURLE_FTP_WRITE_ERROR:
111 return "FTP: the post-transfer acknowledge response was not OK";
113 case CURLE_FTP_QUOTE_ERROR:
114 return "FTP: a quote command returned error";
116 case CURLE_HTTP_RETURNED_ERROR:
117 return "HTTP response code said error";
119 case CURLE_WRITE_ERROR:
120 return "failed writing received data to disk/application";
122 case CURLE_FTP_COULDNT_STOR_FILE:
123 return "failed FTP upload (the STOR command)";
125 case CURLE_READ_ERROR:
126 return "failed to open/read local data from file/application";
128 case CURLE_OUT_OF_MEMORY:
129 return "out of memory";
131 case CURLE_OPERATION_TIMEOUTED:
132 return "a timeout was reached";
134 case CURLE_FTP_COULDNT_SET_ASCII:
135 return "FTP could not set ASCII mode (TYPE A)";
137 case CURLE_FTP_PORT_FAILED:
138 return "FTP command PORT failed";
140 case CURLE_FTP_COULDNT_USE_REST:
141 return "FTP command REST failed";
143 case CURLE_FTP_COULDNT_GET_SIZE:
144 return "FTP command SIZE failed";
146 case CURLE_HTTP_RANGE_ERROR:
147 return "a range was requested but the server did not deliver it";
149 case CURLE_HTTP_POST_ERROR:
150 return "internal problem setting up the POST";
152 case CURLE_SSL_CONNECT_ERROR:
153 return "SSL connect error";
155 case CURLE_BAD_DOWNLOAD_RESUME:
156 return "couldn't resume download";
158 case CURLE_FILE_COULDNT_READ_FILE:
159 return "couldn't read a file:// file";
161 case CURLE_LDAP_CANNOT_BIND:
162 return "LDAP: cannot bind";
164 case CURLE_LDAP_SEARCH_FAILED:
165 return "LDAP: search failed";
167 case CURLE_LIBRARY_NOT_FOUND:
168 return "a required shared library was not found";
170 case CURLE_FUNCTION_NOT_FOUND:
171 return "a required function in the shared library was not found";
173 case CURLE_ABORTED_BY_CALLBACK:
174 return "the operation was aborted by an application callback";
176 case CURLE_BAD_FUNCTION_ARGUMENT:
177 return "a libcurl function was given a bad argument";
179 case CURLE_INTERFACE_FAILED:
180 return "failed binding local connection end";
182 case CURLE_TOO_MANY_REDIRECTS :
183 return "number of redirects hit maximum amount";
185 case CURLE_UNKNOWN_TELNET_OPTION:
186 return "User specified an unknown option";
188 case CURLE_TELNET_OPTION_SYNTAX :
189 return "Malformed telnet option";
191 case CURLE_SSL_PEER_CERTIFICATE:
192 return "SSL peer certificate was not ok";
194 case CURLE_GOT_NOTHING:
195 return "server returned nothing (no headers, no data)";
197 case CURLE_SSL_ENGINE_NOTFOUND:
198 return "SSL crypto engine not found";
200 case CURLE_SSL_ENGINE_SETFAILED:
201 return "can not set SSL crypto engine as default";
203 case CURLE_SSL_ENGINE_INITFAILED:
204 return "failed to initialise SSL crypto engine";
206 case CURLE_SEND_ERROR:
207 return "failed sending data to the peer";
209 case CURLE_RECV_ERROR:
210 return "failure when receiving data from the peer";
212 case CURLE_SHARE_IN_USE:
213 return "share is already in use";
215 case CURLE_SSL_CERTPROBLEM:
216 return "problem with the local SSL certificate";
218 case CURLE_SSL_CIPHER:
219 return "couldn't use specified SSL cipher";
221 case CURLE_SSL_CACERT:
222 return "problem with the SSL CA cert (path? access rights?)";
224 case CURLE_BAD_CONTENT_ENCODING:
225 return "Unrecognized HTTP Content-Encoding";
227 case CURLE_LDAP_INVALID_URL:
228 return "Invalid LDAP URL";
230 case CURLE_FILESIZE_EXCEEDED:
231 return "Maximum file size exceeded";
233 case CURLE_FTP_SSL_FAILED:
234 return "Requested FTP SSL level failed";
236 case CURLE_SEND_FAIL_REWIND:
237 return "Send failed since rewinding of the data stream failed";
239 case CURLE_LOGIN_DENIED:
240 return "FTP: login denied";;
242 case CURLE_URL_MALFORMAT_USER: /* not used by current libcurl */
243 case CURLE_MALFORMAT_USER: /* not used by current libcurl */
244 case CURLE_BAD_CALLING_ORDER: /* not used by current libcurl */
245 case CURLE_BAD_PASSWORD_ENTERED:/* not used by current libcurl */
246 case CURLE_OBSOLETE: /* not used by current libcurl */
251 * By using a switch, gcc -Wall will complain about enum values
252 * which do not appear, helping keep this function up-to-date.
253 * By using gcc -Wall -Werror, you can't forget.
255 * A table would not have the same benefit. Most compilers will
256 * generate code very similar to a table in any case, so there
257 * is little performance gain from a table. And something is broken
258 * for the user's application, anyways, so does it matter how fast
261 * The line number for the error will be near this comment, which
262 * is why it is here, and not at the start of the switch.
264 return "unknown error";
266 if (error == CURLE_OK)
274 curl_multi_strerror(CURLMcode error)
276 #ifndef CURL_DISABLE_VERBOSE_STRINGS
278 case CURLM_CALL_MULTI_PERFORM:
279 return "please call curl_multi_perform() soon";
284 case CURLM_BAD_HANDLE:
285 return "invalid multi handle";
287 case CURLM_BAD_EASY_HANDLE:
288 return "invalid easy handle";
290 case CURLM_OUT_OF_MEMORY:
291 return "out of memory";
293 case CURLM_INTERNAL_ERROR:
294 return "internal error";
300 return "unknown error";
302 if (error == CURLM_OK)
310 curl_share_strerror(CURLSHcode error)
312 #ifndef CURL_DISABLE_VERBOSE_STRINGS
317 case CURLSHE_BAD_OPTION:
318 return "unknown share option";
321 return "share currently in use";
323 case CURLSHE_INVALID:
324 return "invalid share handle";
327 return "out of memory";
333 return "CURLSH unknown";
335 if (error == CURLSHE_OK)
342 #if defined(WIN32) && !defined(__CYGWIN__)
344 /* This function handles most / all (?) Winsock errors cURL is able to produce.
347 get_winsock_error (int err, char *buf, size_t len)
351 #ifndef CURL_DISABLE_VERBOSE_STRINGS
354 p = "Call interrupted.";
366 p = "Invalid arguments";
369 p = "Out of file descriptors";
372 p = "Call would block";
376 p = "Blocking call in progress";
379 p = "Descriptor is not a socket.";
381 case WSAEDESTADDRREQ:
382 p = "Need destination address";
385 p = "Bad message size";
391 p = "Protocol option is unsupported";
393 case WSAEPROTONOSUPPORT:
394 p = "Protocol is unsupported";
396 case WSAESOCKTNOSUPPORT:
397 p = "Socket is unsupported";
400 p = "Operation not supported";
402 case WSAEAFNOSUPPORT:
403 p = "Address family not supported";
405 case WSAEPFNOSUPPORT:
406 p = "Protocol family not supported";
409 p = "Address already in use";
411 case WSAEADDRNOTAVAIL:
412 p = "Address not available";
418 p = "Network unreachable";
421 p = "Network has been reset";
423 case WSAECONNABORTED:
424 p = "Connection was aborted";
427 p = "Connection was reset";
430 p = "No buffer space";
433 p = "Socket is already connected";
436 p = "Socket is not connected";
439 p = "Socket has been shut down";
441 case WSAETOOMANYREFS:
442 p = "Too many references";
447 case WSAECONNREFUSED:
448 p = "Connection refused";
453 case WSAENAMETOOLONG:
459 case WSAEHOSTUNREACH:
460 p = "Host unreachable";
466 p = "Process limit reached";
469 p = "Too many users";
475 p = "Something is stale";
484 /* Extended Winsock errors */
486 p = "Winsock library is not ready";
488 case WSANOTINITIALISED:
489 p = "Winsock library not initalised";
491 case WSAVERNOTSUPPORTED:
492 p = "Winsock version not supported.";
495 /* getXbyY() errors (already handled in herrmsg):
496 * Authoritative Answer: Host not found */
497 case WSAHOST_NOT_FOUND:
498 p = "Host not found";
501 /* Non-Authoritative: Host not found, or SERVERFAIL */
503 p = "Host not found, try again";
506 /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
508 p = "Unrecoverable error in call to nameserver";
511 /* Valid name, no data record of requested type */
513 p = "No data record of requested type";
520 if (error == CURLE_OK)
525 strncpy (buf, p, len);
529 #endif /* WIN32 && !__CYGWIN__ */
532 * Our thread-safe and smart strerror() replacement.
534 * The 'err' argument passed in to this function MUST be a true errno number
535 * as reported on this system. We do no range checking on the number before
536 * we pass it to the "number-to-message" convertion function and there might
537 * be systems that don't do proper range checking in there themselves.
539 * We don't do range checking (on systems other than Windows) since there is
540 * no good reliable and portable way to do it.
542 const char *Curl_strerror(struct connectdata *conn, int err)
548 curlassert(err >= 0);
550 buf = conn->syserr_buf;
551 max = sizeof(conn->syserr_buf)-1;
554 #if defined(WIN32) && !defined(__CYGWIN__)
561 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
562 LANG_NEUTRAL, wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL);
563 wcstombs(buf,wbuf,max);
568 /* 'sys_nerr' is the maximum errno number, it is not widely portable */
569 if (err >= 0 && err < sys_nerr)
570 strncpy(buf, strerror(err), max);
572 if (!get_winsock_error(err, buf, max) &&
573 !FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
574 LANG_NEUTRAL, buf, max, NULL))
575 snprintf(buf, max, "Unknown error %d (%#x)", err, err);
578 #else /* not native Windows coming up */
580 /* These should be atomic and hopefully thread-safe */
581 #ifdef HAVE_STRERROR_R
582 /* There are two different APIs for strerror_r(). The POSIX and the GLIBC
584 #ifdef HAVE_POSIX_STRERROR_R
585 strerror_r(err, buf, max);
586 /* this may set errno to ERANGE if insufficient storage was supplied via
587 'strerrbuf' and 'buflen' to contain the generated message string, or
588 EINVAL if the value of 'errnum' is not a valid error number.*/
591 /* HAVE_GLIBC_STRERROR_R */
593 char *msg = strerror_r(err, buffer, sizeof(buffer));
594 /* this version of strerror_r() only *might* use the buffer we pass to
595 the function, but it always returns the error message as a pointer,
596 so we must copy that string unconditionally */
597 strncpy(buf, msg, max);
599 #endif /* end of HAVE_GLIBC_STRERROR_R */
600 #else /* HAVE_STRERROR_R */
601 strncpy(buf, strerror(err), max);
602 #endif /* end of HAVE_STRERROR_R */
603 #endif /* end of ! Windows */
605 buf[max] = '\0'; /* make sure the string is zero terminated */
607 /* strip trailing '\r\n' or '\n'. */
608 if ((p = strrchr(buf,'\n')) != NULL && (p - buf) >= 2)
610 if ((p = strrchr(buf,'\r')) != NULL && (p - buf) >= 1)
617 * Return error-string for libidn status as returned from idna_to_ascii_lz().
619 const char *Curl_idn_strerror (struct connectdata *conn, int err)
621 #ifdef HAVE_IDNA_STRERROR
623 return idna_strerror((Idna_rc) err);
631 buf = conn->syserr_buf;
632 max = sizeof(conn->syserr_buf)-1;
634 #ifndef CURL_DISABLE_VERBOSE_STRINGS
635 switch ((Idna_rc)err) {
639 case IDNA_STRINGPREP_ERROR:
640 str = "Error in string preparation";
642 case IDNA_PUNYCODE_ERROR:
643 str = "Error in Punycode operation";
645 case IDNA_CONTAINS_NON_LDH:
646 str = "Illegal ASCII characters";
648 case IDNA_CONTAINS_MINUS:
649 str = "Contains minus";
651 case IDNA_INVALID_LENGTH:
652 str = "Invalid output length";
654 case IDNA_NO_ACE_PREFIX:
655 str = "No ACE prefix (\"xn--\")";
657 case IDNA_ROUNDTRIP_VERIFY_ERROR:
658 str = "Roundtrip verify error";
660 case IDNA_CONTAINS_ACE_PREFIX:
661 str = "Already have ACE prefix (\"xn--\")";
663 case IDNA_ICONV_ERROR:
664 str = "Locale conversion failed";
666 case IDNA_MALLOC_ERROR:
667 str = "Allocation failed";
669 case IDNA_DLOPEN_ERROR:
670 str = "dlopen() error";
673 snprintf(buf, max, "error %d", (int)err);
678 if ((Idna_rc)err == IDNA_SUCCESS)
684 strncpy(buf, str, max);
689 #endif /* USE_LIBIDN */