5
5
* | (__| |_| | _ <| |___
6
6
* \___|\___/|_| \_\_____|
8
* Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
8
* Copyright (C) 1998 - 2005, Daniel Stenberg, <daniel@haxx.se>, et al.
10
10
* This software is licensed as described in the file COPYING, which
11
11
* you should have received as part of this distribution. The terms
18
18
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
* KIND, either express or implied.
21
* $Id: sendf.c,v 1.93 2004/12/06 23:04:31 bagder Exp $
21
* $Id: sendf.c,v 1.98 2005/04/07 15:27:14 bagder Exp $
22
22
***************************************************************************/
44
44
#include "urldata.h"
46
46
#include "connect.h" /* for the Curl_ourerrno() proto */
48
49
#define _MPRINTF_REPLACE /* use the internal *printf() functions */
49
50
#include <curl/mprintf.h>
55
#define Curl_sec_write(a,b,c,d) -1
56
#define Curl_sec_read(a,b,c,d) -1
54
59
#include <string.h>
55
60
#include "memory.h"
56
61
#include "strerror.h"
154
159
void Curl_failf(struct SessionHandle *data, const char *fmt, ...)
157
163
va_start(ap, fmt);
165
vsnprintf(data->state.buffer, BUFSIZE, fmt, ap);
158
167
if(data->set.errorbuffer && !data->state.errorbuf) {
159
vsnprintf(data->set.errorbuffer, CURL_ERROR_SIZE, fmt, ap);
168
snprintf(data->set.errorbuffer, CURL_ERROR_SIZE, "%s", data->state.buffer);
160
169
data->state.errorbuf = TRUE; /* wrote error string */
162
171
if(data->set.verbose) {
165
vsnprintf(data->state.buffer, BUFSIZE, fmt, ap);
166
len = strlen(data->state.buffer);
168
if(len < BUFSIZE - 1) {
169
data->state.buffer[len] = '\n';
170
data->state.buffer[++len] = '\0';
172
Curl_debug(data, CURLINFO_TEXT, data->state.buffer, len, NULL);
172
len = strlen(data->state.buffer);
173
if(len < BUFSIZE - 1) {
174
data->state.buffer[len] = '\n';
175
data->state.buffer[++len] = '\0';
177
Curl_debug(data, CURLINFO_TEXT, data->state.buffer, len, NULL);
206
211
if(data->set.verbose)
207
Curl_debug(data, CURLINFO_DATA_OUT, sptr, bytes_written,
208
conn->host.dispname);
212
Curl_debug(data, CURLINFO_DATA_OUT, sptr, bytes_written, conn);
210
214
if((size_t)bytes_written != write_len) {
211
215
/* if not all was written at once, we must advance the pointer, decrease
235
239
ssize_t bytes_written;
236
240
CURLcode retcode;
239
/* Set 'num' to 0 or 1, depending on which socket that has been sent here.
240
If it is the second socket, we set num to 1. Otherwise to 0. This lets
241
us use the correct ssl handle. */
242
241
int num = (sockfd == conn->sock[SECONDARYSOCKET]);
243
/* SSL_write() is said to return 'int' while write() and send() returns
245
if (conn->ssl[num].use) {
247
char error_buffer[120]; /* OpenSSL documents that this must be at least
249
unsigned long sslerror;
250
int rc = SSL_write(conn->ssl[num].handle, mem, (int)len);
253
err = SSL_get_error(conn->ssl[num].handle, rc);
256
case SSL_ERROR_WANT_READ:
257
case SSL_ERROR_WANT_WRITE:
258
/* The operation did not complete; the same TLS/SSL I/O function
259
should be called again later. This is basicly an EWOULDBLOCK
263
case SSL_ERROR_SYSCALL:
264
failf(conn->data, "SSL_write() returned SYSCALL, errno = %d\n",
266
return CURLE_SEND_ERROR;
268
/* A failure in the SSL library occurred, usually a protocol error.
269
The OpenSSL error queue contains more information on the error. */
270
sslerror = ERR_get_error();
271
failf(conn->data, "SSL_write() error: %s\n",
272
ERR_error_string(sslerror, error_buffer));
273
return CURLE_SEND_ERROR;
276
failf(conn->data, "SSL_write() return error %d\n", err);
277
return CURLE_SEND_ERROR;
243
if (conn->ssl[num].use)
244
/* only TRUE if SSL enabled */
245
bytes_written = Curl_ssl_send(conn, num, mem, len);
286
if(conn->sec_complete) {
247
if(conn->sec_complete)
248
/* only TRUE if krb4 enabled */
287
249
bytes_written = Curl_sec_write(conn, sockfd, mem, len);
290
#endif /* HAVE_KRB4 */
292
251
bytes_written = (ssize_t)swrite(sockfd, mem, len);
294
253
if(-1 == bytes_written) {
295
254
int err = Curl_ourerrno();
312
271
failf(conn->data, "Send failure: %s",
313
272
Curl_strerror(conn, err));
319
275
*written = bytes_written;
320
276
retcode = (-1 != bytes_written)?CURLE_OK:CURLE_SEND_ERROR;
377
333
ssize_t *n) /* amount bytes read */
381
337
/* Set 'num' to 0 or 1, depending on which socket that has been sent here.
382
338
If it is the second socket, we set num to 1. Otherwise to 0. This lets
383
339
us use the correct ssl handle. */
386
342
*n=0; /* reset amount to zero */
388
if (conn->ssl[num].use) {
389
nread = (ssize_t)SSL_read(conn->ssl[num].handle, buf, (int)buffersize);
392
/* failed SSL_read */
393
int err = SSL_get_error(conn->ssl[num].handle, (int)nread);
396
case SSL_ERROR_NONE: /* this is not an error */
397
case SSL_ERROR_ZERO_RETURN: /* no more data */
399
case SSL_ERROR_WANT_READ:
400
case SSL_ERROR_WANT_WRITE:
401
/* there's data pending, re-invoke SSL_read() */
402
return -1; /* basicly EWOULDBLOCK */
404
/* openssl/ssl.h says "look at error stack/return value/errno" */
406
char error_buffer[120]; /* OpenSSL documents that this must be at
407
least 120 bytes long. */
408
unsigned long sslerror = ERR_get_error();
409
failf(conn->data, "SSL read: %s, errno %d",
410
ERR_error_string(sslerror, error_buffer),
413
return CURLE_RECV_ERROR;
344
if(conn->ssl[num].use) {
345
nread = Curl_ssl_recv(conn, num, buf, buffersize);
348
return -1; /* -1 from Curl_ssl_recv() means EWOULDBLOCK */
421
351
*n=0; /* reset amount to zero */
423
352
if(conn->sec_complete)
424
353
nread = Curl_sec_read(conn, sockfd, buf, buffersize);
427
355
nread = sread(sockfd, buf, buffersize);
429
357
if(-1 == nread) {
470
395
int Curl_debug(struct SessionHandle *data, curl_infotype type,
471
char *ptr, size_t size, char *host)
396
char *ptr, size_t size,
397
struct connectdata *conn)
474
if(data->set.printhost && host) {
400
if(data->set.printhost && conn && conn->host.dispname) {
475
401
char buffer[160];
476
402
const char *t=NULL;
403
const char *w="Data";
478
405
case CURLINFO_HEADER_IN:
479
407
case CURLINFO_DATA_IN:
482
410
case CURLINFO_HEADER_OUT:
483
412
case CURLINFO_DATA_OUT:
491
snprintf(buffer, sizeof(buffer), "[Data %s %s]", t, host);
420
snprintf(buffer, sizeof(buffer), "[%s %s %s%s]", w, t,
421
conn->xfertype==NORMAL?"":
422
(conn->xfertype==SOURCE3RD?"source ":"target "),
423
conn->host.dispname);
492
424
rc = showit(data, CURLINFO_TEXT, buffer, strlen(buffer));