~ubuntu-branches/ubuntu/lucid/curl/lucid-security

« back to all changes in this revision

Viewing changes to lib/sendf.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2005-03-23 18:41:29 UTC
  • mto: (3.1.1 lenny) (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050323184129-9hgq7luenq51umpu
Tags: upstream-7.12.3
ImportĀ upstreamĀ versionĀ 7.12.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/***************************************************************************
2
 
 *                                  _   _ ____  _     
3
 
 *  Project                     ___| | | |  _ \| |    
4
 
 *                             / __| | | | |_) | |    
5
 
 *                            | (__| |_| |  _ <| |___ 
 
2
 *                                  _   _ ____  _
 
3
 *  Project                     ___| | | |  _ \| |
 
4
 *                             / __| | | | |_) | |
 
5
 *                            | (__| |_| |  _ <| |___
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
8
 * Copyright (C) 1998 - 2004, 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
12
12
 * are also available at http://curl.haxx.se/docs/copyright.html.
13
 
 * 
 
13
 *
14
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
15
 * copies of the Software, and permit persons to whom the Software is
16
16
 * furnished to do so, under the terms of the COPYING file.
18
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
 * KIND, either express or implied.
20
20
 *
21
 
 * $Id: sendf.c,v 1.81 2004/04/21 08:49:14 bagder Exp $
 
21
 * $Id: sendf.c,v 1.93 2004/12/06 23:04:31 bagder Exp $
22
22
 ***************************************************************************/
23
23
 
24
24
#include "setup.h"
33
33
#endif
34
34
 
35
35
#ifdef HAVE_SYS_SOCKET_H
36
 
#include <sys/socket.h> /* required for send() & recv() prototypes */
 
36
#include <sys/socket.h> /* required for send() & recv() prototypes */
37
37
#endif
38
38
 
39
39
#ifdef HAVE_UNISTD_H
52
52
#include "security.h"
53
53
#endif
54
54
#include <string.h>
 
55
#include "memory.h"
 
56
#include "strerror.h"
55
57
/* The last #include file should be: */
56
 
#ifdef CURLDEBUG
57
58
#include "memdebug.h"
58
 
#endif
59
59
 
60
60
/* returns last node in linked list */
61
61
static struct curl_slist *slist_get_last(struct curl_slist *list)
62
62
{
63
 
  struct curl_slist     *item;
 
63
  struct curl_slist     *item;
64
64
 
65
65
  /* if caller passed us a NULL, return now */
66
66
  if (!list)
74
74
  return item;
75
75
}
76
76
 
77
 
/* append a struct to the linked list. It always retunrs the address of the
78
 
 * first record, so that you can sure this function as an initialization
79
 
 * function as well as an append function. If you find this bothersome,
80
 
 * then simply create a separate _init function and call it appropriately from
81
 
 * within the proram. */
 
77
/*
 
78
 * curl_slist_append() appends a string to the linked list. It always retunrs
 
79
 * the address of the first record, so that you can sure this function as an
 
80
 * initialization function as well as an append function. If you find this
 
81
 * bothersome, then simply create a separate _init function and call it
 
82
 * appropriately from within the proram.
 
83
 */
82
84
struct curl_slist *curl_slist_append(struct curl_slist *list,
83
85
                                     const char *data)
84
86
{
85
 
  struct curl_slist     *last;
86
 
  struct curl_slist     *new_item;
 
87
  struct curl_slist     *last;
 
88
  struct curl_slist     *new_item;
87
89
 
88
90
  new_item = (struct curl_slist *) malloc(sizeof(struct curl_slist));
89
91
  if (new_item) {
90
 
    new_item->next = NULL;
91
 
    new_item->data = strdup(data);
 
92
    char *dup = strdup(data);
 
93
    if(dup) {
 
94
      new_item->next = NULL;
 
95
      new_item->data = dup;
 
96
    }
 
97
    else {
 
98
      free(new_item);
 
99
      return NULL;
 
100
    }
92
101
  }
93
 
  if (new_item == NULL || new_item->data == NULL) {
 
102
  else
94
103
    return NULL;
95
 
  }
96
104
 
97
105
  if (list) {
98
106
    last = slist_get_last(list);
107
115
/* be nice and clean up resources */
108
116
void curl_slist_free_all(struct curl_slist *list)
109
117
{
110
 
  struct curl_slist     *next;
111
 
  struct curl_slist     *item;
 
118
  struct curl_slist     *next;
 
119
  struct curl_slist     *item;
112
120
 
113
121
  if (!list)
114
122
    return;
116
124
  item = list;
117
125
  do {
118
126
    next = item->next;
119
 
                
 
127
 
120
128
    if (item->data) {
121
129
      free(item->data);
122
130
    }
135
143
    va_start(ap, fmt);
136
144
    vsnprintf(print_buffer, 1024, fmt, ap);
137
145
    va_end(ap);
138
 
    Curl_debug(data, CURLINFO_TEXT, print_buffer, strlen(print_buffer));
 
146
    Curl_debug(data, CURLINFO_TEXT, print_buffer, strlen(print_buffer), NULL);
139
147
  }
140
148
}
141
149
 
150
158
  if(data->set.errorbuffer && !data->state.errorbuf) {
151
159
    vsnprintf(data->set.errorbuffer, CURL_ERROR_SIZE, fmt, ap);
152
160
    data->state.errorbuf = TRUE; /* wrote error string */
153
 
 
154
 
    if(data->set.verbose) {
155
 
      size_t len = strlen(data->set.errorbuffer);
156
 
      bool doneit=FALSE;
157
 
      if(len < CURL_ERROR_SIZE - 1) {
158
 
        doneit = TRUE;
159
 
        data->set.errorbuffer[len] = '\n';
160
 
        data->set.errorbuffer[++len] = '\0';
 
161
  }
 
162
  if(data->set.verbose) {
 
163
      size_t len;
 
164
 
 
165
      vsnprintf(data->state.buffer, BUFSIZE, fmt, ap);
 
166
      len = strlen(data->state.buffer);
 
167
 
 
168
      if(len < BUFSIZE - 1) {
 
169
        data->state.buffer[len] = '\n';
 
170
        data->state.buffer[++len] = '\0';
161
171
      }
162
 
      Curl_debug(data, CURLINFO_TEXT, data->set.errorbuffer, len);
163
 
      if(doneit)
164
 
        /* cut off the newline again */
165
 
        data->set.errorbuffer[--len]=0;
166
 
    }
 
172
      Curl_debug(data, CURLINFO_TEXT, data->state.buffer, len, NULL);
167
173
  }
 
174
 
168
175
  va_end(ap);
169
176
}
170
177
 
197
204
      break;
198
205
 
199
206
    if(data->set.verbose)
200
 
      Curl_debug(data, CURLINFO_DATA_OUT, sptr, bytes_written);
 
207
      Curl_debug(data, CURLINFO_DATA_OUT, sptr, bytes_written,
 
208
                 conn->host.dispname);
201
209
 
202
210
    if((size_t)bytes_written != write_len) {
203
211
      /* if not all was written at once, we must advance the pointer, decrease
238
246
    int err;
239
247
    char error_buffer[120]; /* OpenSSL documents that this must be at least
240
248
                               120 bytes long. */
241
 
    int sslerror;
242
 
    int rc = SSL_write(conn->ssl[num].handle, mem, len);
 
249
    unsigned long sslerror;
 
250
    int rc = SSL_write(conn->ssl[num].handle, mem, (int)len);
243
251
 
244
252
    if(rc < 0) {
245
253
      err = SSL_get_error(conn->ssl[num].handle, rc);
246
 
    
 
254
 
247
255
      switch(err) {
248
256
      case SSL_ERROR_WANT_READ:
249
257
      case SSL_ERROR_WANT_WRITE:
300
308
        )
301
309
        /* this is just a case of EWOULDBLOCK */
302
310
        bytes_written=0;
 
311
      else
 
312
        failf(conn->data, "Send failure: %s",
 
313
              Curl_strerror(conn, err));
303
314
    }
304
315
#ifdef USE_SSLEAY
305
316
  }
348
359
      return CURLE_WRITE_ERROR;
349
360
    }
350
361
  }
351
 
  
 
362
 
352
363
  return CURLE_OK;
353
364
}
354
365
 
375
386
  *n=0; /* reset amount to zero */
376
387
 
377
388
  if (conn->ssl[num].use) {
378
 
    nread = SSL_read(conn->ssl[num].handle, buf, buffersize);
 
389
    nread = (ssize_t)SSL_read(conn->ssl[num].handle, buf, (int)buffersize);
379
390
 
380
391
    if(nread < 0) {
381
392
      /* failed SSL_read */
382
 
      int err = SSL_get_error(conn->ssl[num].handle, nread);
 
393
      int err = SSL_get_error(conn->ssl[num].handle, (int)nread);
383
394
 
384
395
      switch(err) {
385
396
      case SSL_ERROR_NONE: /* this is not an error */
394
405
        {
395
406
          char error_buffer[120]; /* OpenSSL documents that this must be at
396
407
                                     least 120 bytes long. */
397
 
          int sslerror = ERR_get_error();
 
408
          unsigned long sslerror = ERR_get_error();
398
409
          failf(conn->data, "SSL read: %s, errno %d",
399
410
                ERR_error_string(sslerror, error_buffer),
400
411
                Curl_ourerrno() );
433
444
}
434
445
 
435
446
/* return 0 on success */
436
 
int Curl_debug(struct SessionHandle *data, curl_infotype type,
437
 
               char *ptr, size_t size)
 
447
static int showit(struct SessionHandle *data, curl_infotype type,
 
448
                  char *ptr, size_t size)
438
449
{
439
450
  static const char * const s_infotype[CURLINFO_END] = {
440
 
    "* ", "< ", "> ", "{ ", "} " };
 
451
    "* ", "< ", "> ", "{ ", "} ", "{ ", "} " };
441
452
 
442
453
  if(data->set.fdebug)
443
454
    return (*data->set.fdebug)(data, type, ptr, size,
455
466
  }
456
467
  return 0;
457
468
}
 
469
 
 
470
int Curl_debug(struct SessionHandle *data, curl_infotype type,
 
471
               char *ptr, size_t size, char *host)
 
472
{
 
473
  int rc;
 
474
  if(data->set.printhost && host) {
 
475
    char buffer[160];
 
476
    const char *t=NULL;
 
477
    switch (type) {
 
478
    case CURLINFO_HEADER_IN:
 
479
    case CURLINFO_DATA_IN:
 
480
      t = "from";
 
481
      break;
 
482
    case CURLINFO_HEADER_OUT:
 
483
    case CURLINFO_DATA_OUT:
 
484
      t = "to";
 
485
      break;
 
486
    default:
 
487
      break;
 
488
    }
 
489
 
 
490
    if(t) {
 
491
      snprintf(buffer, sizeof(buffer), "[Data %s %s]", t, host);
 
492
      rc = showit(data, CURLINFO_TEXT, buffer, strlen(buffer));
 
493
      if(rc)
 
494
        return rc;
 
495
    }
 
496
  }
 
497
  rc = showit(data, type, ptr, size);
 
498
  return rc;
 
499
}