summaryrefslogtreecommitdiff
path: root/Utilities/cmcurl/lib/cfilters.c
blob: bcb33da7715ef58216c6801d447088faf1c48a8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at https://curl.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 * SPDX-License-Identifier: curl
 *
 ***************************************************************************/

#include "curl_setup.h"

#include "urldata.h"
#include "strerror.h"
#include "cfilters.h"
#include "connect.h"
#include "url.h" /* for Curl_safefree() */
#include "sendf.h"
#include "sockaddr.h" /* required for Curl_sockaddr_storage */
#include "multiif.h"
#include "progress.h"
#include "warnless.h"
#include "http_proxy.h"
#include "socks.h"
#include "vtls/vtls.h"

/* The last 3 #include files should be in this order */
#include "curl_printf.h"
#include "curl_memory.h"
#include "memdebug.h"

#ifndef ARRAYSIZE
#define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
#endif


void Curl_cf_def_destroy_this(struct Curl_cfilter *cf, struct Curl_easy *data)
{
  (void)cf;
  (void)data;
}

CURLcode Curl_cf_def_setup(struct Curl_cfilter *cf,
                           struct Curl_easy *data,
                           const struct Curl_dns_entry *remotehost)
{
  DEBUGASSERT(cf->next);
  return cf->next->cft->setup(cf->next, data, remotehost);
}

void     Curl_cf_def_attach_data(struct Curl_cfilter *cf,
                                 struct Curl_easy *data)
{
  (void)cf;
  (void)data;
}

void     Curl_cf_def_detach_data(struct Curl_cfilter *cf,
                                 struct Curl_easy *data)
{
  (void)cf;
  (void)data;
}

void Curl_cf_def_close(struct Curl_cfilter *cf, struct Curl_easy *data)
{
  DEBUGASSERT(cf->next);
  cf->connected = FALSE;
  cf->next->cft->close(cf->next, data);
}

CURLcode Curl_cf_def_connect(struct Curl_cfilter *cf,
                             struct Curl_easy *data,
                             bool blocking, bool *done)
{
  DEBUGASSERT(cf->next);
  return cf->next->cft->connect(cf->next, data, blocking, done);
}

void Curl_cf_def_get_host(struct Curl_cfilter *cf, struct Curl_easy *data,
                          const char **phost, const char **pdisplay_host,
                          int *pport)
{
  DEBUGASSERT(cf->next);
  cf->next->cft->get_host(cf->next, data, phost, pdisplay_host, pport);
}

int Curl_cf_def_get_select_socks(struct Curl_cfilter *cf,
                                 struct Curl_easy *data,
                                 curl_socket_t *socks)
{
  DEBUGASSERT(cf->next);
  return cf->next->cft->get_select_socks(cf->next, data, socks);
}

bool Curl_cf_def_data_pending(struct Curl_cfilter *cf,
                              const struct Curl_easy *data)
{
  DEBUGASSERT(cf->next);
  return cf->next->cft->has_data_pending(cf->next, data);
}

ssize_t  Curl_cf_def_send(struct Curl_cfilter *cf, struct Curl_easy *data,
                          const void *buf, size_t len, CURLcode *err)
{
  DEBUGASSERT(cf->next);
  return cf->next->cft->do_send(cf->next, data, buf, len, err);
}

ssize_t  Curl_cf_def_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
                          char *buf, size_t len, CURLcode *err)
{
  DEBUGASSERT(cf->next);
  return cf->next->cft->do_recv(cf->next, data, buf, len, err);
}

void Curl_conn_cf_discard_all(struct Curl_easy *data,
                              struct connectdata *conn, int index)
{
  struct Curl_cfilter *cfn, *cf = conn->cfilter[index];

  if(cf) {
    conn->cfilter[index] = NULL;
    while(cf) {
      cfn = cf->next;
      cf->cft->destroy(cf, data);
      free(cf);
      cf = cfn;
    }
  }
}

void Curl_conn_close(struct Curl_easy *data, int index)
{
  struct Curl_cfilter *cf;

  DEBUGASSERT(data->conn);
  /* it is valid to call that without filters being present */
  cf = data->conn->cfilter[index];
  if(cf) {
    cf->cft->close(cf, data);
  }
}

ssize_t Curl_conn_recv(struct Curl_easy *data, int num, char *buf,
                       size_t len, CURLcode *code)
{
  struct Curl_cfilter *cf;
  ssize_t nread;

  DEBUGASSERT(data);
  DEBUGASSERT(data->conn);
  cf = data->conn->cfilter[num];
  while(cf && !cf->connected) {
    cf = cf->next;
  }
  if(cf) {
    nread = cf->cft->do_recv(cf, data, buf, len, code);
    /* DEBUGF(infof(data, "Curl_conn_recv(handle=%p, index=%d)"
           "-> %ld, err=%d", data, num, nread, *code));*/
    return nread;
  }
  failf(data, "no filter connected, conn=%ld, sockindex=%d",
        data->conn->connection_id, num);
  *code = CURLE_FAILED_INIT;
  return -1;
}

ssize_t Curl_conn_send(struct Curl_easy *data, int num,
                       const void *mem, size_t len, CURLcode *code)
{
  struct Curl_cfilter *cf;
  ssize_t nwritten;

  DEBUGASSERT(data);
  DEBUGASSERT(data->conn);
  cf = data->conn->cfilter[num];
  while(cf && !cf->connected) {
    cf = cf->next;
  }
  if(cf) {
    nwritten = cf->cft->do_send(cf, data, mem, len, code);
    /* DEBUGF(infof(data, "Curl_conn_send(handle=%p, index=%d, len=%ld)"
           " -> %ld, err=%d", data, num, len, nwritten, *code));*/
    return nwritten;
  }
  failf(data, "no filter connected, conn=%ld, sockindex=%d",
        data->conn->connection_id, num);
  *code = CURLE_FAILED_INIT;
  return -1;
}

CURLcode Curl_cf_create(struct Curl_cfilter **pcf,
                        const struct Curl_cftype *cft,
                        void *ctx)
{
  struct Curl_cfilter *cf;
  CURLcode result = CURLE_OUT_OF_MEMORY;

  DEBUGASSERT(cft);
  cf = calloc(sizeof(*cf), 1);
  if(!cf)
    goto out;

  cf->cft = cft;
  cf->ctx = ctx;
  result = CURLE_OK;
out:
  *pcf = cf;
  return result;
}

void Curl_conn_cf_add(struct Curl_easy *data,
                      struct connectdata *conn,
                      int index,
                      struct Curl_cfilter *cf)
{
  (void)data;
  DEBUGASSERT(conn);
  DEBUGASSERT(!cf->conn);
  DEBUGASSERT(!cf->next);

  DEBUGF(infof(data, CMSGI(conn, index, "cf_add(filter=%s)"),
               cf->cft->name));
  cf->next = conn->cfilter[index];
  cf->conn = conn;
  cf->sockindex = index;
  conn->cfilter[index] = cf;
}

void Curl_conn_cf_discard(struct Curl_cfilter *cf, struct Curl_easy *data)
{
  struct Curl_cfilter **pprev = &cf->conn->cfilter[cf->sockindex];

  /* remove from chain if still in there */
  DEBUGASSERT(cf);
  while (*pprev) {
    if (*pprev == cf) {
      *pprev = cf->next;
      break;
    }
    pprev = &((*pprev)->next);
  }
  cf->cft->destroy(cf, data);
  free(cf);
}

ssize_t Curl_conn_cf_send(struct Curl_cfilter *cf, struct Curl_easy *data,
                          const void *buf, size_t len, CURLcode *err)
{
  return cf->cft->do_send(cf, data, buf, len, err);
}

ssize_t Curl_conn_cf_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
                          char *buf, size_t len, CURLcode *err)
{
  return cf->cft->do_recv(cf, data, buf, len, err);
}

CURLcode Curl_conn_setup(struct Curl_easy *data,
                         struct connectdata *conn,
                         int sockindex,
                         const struct Curl_dns_entry *remotehost,
                         int ssl_mode)
{
  struct Curl_cfilter *cf;
  CURLcode result;

  DEBUGASSERT(data);
  /* If no filter is set, we have the "default" setup of connection filters.
   * The filter chain from botton to top will be:
   * - SOCKET       socket filter for outgoing connection to remotehost
   * if http_proxy tunneling is engaged:
   *    - SSL                 if proxytype is CURLPROXY_HTTPS
   *    - HTTP_PROXY_TUNNEL
   * otherwise, if socks_proxy is engaged:
   *    - SOCKS_PROXY_TUNNEL
   * - SSL          if conn->handler has PROTOPT_SSL
   */
  if(!conn->cfilter[sockindex]) {
    DEBUGF(infof(data, DMSGI(data, sockindex, "setup, init filter chain")));
    result = Curl_conn_socket_set(data, conn, sockindex);
    if(result)
      goto out;

#ifndef CURL_DISABLE_PROXY
    if(conn->bits.socksproxy) {
      result = Curl_conn_socks_proxy_add(data, conn, sockindex);
      if(result)
        goto out;
    }

    if(conn->bits.httpproxy) {
#ifdef USE_SSL
      if(conn->http_proxy.proxytype == CURLPROXY_HTTPS) {
        result = Curl_ssl_cfilter_proxy_add(data, conn, sockindex);
        if(result)
          goto out;
      }
#endif /* USE_SSL */

#if !defined(CURL_DISABLE_HTTP)
      if(conn->bits.tunnel_proxy) {
        result = Curl_conn_http_proxy_add(data, conn, sockindex);
        if(result)
          goto out;
      }
#endif /* !CURL_DISABLE_HTTP */
    }
#endif /* !CURL_DISABLE_PROXY */

#ifdef USE_SSL
    if(ssl_mode == CURL_CF_SSL_ENABLE
      || (ssl_mode != CURL_CF_SSL_DISABLE
           && conn->handler->flags & PROTOPT_SSL)) {
      result = Curl_ssl_cfilter_add(data, conn, sockindex);
      if(result)
        goto out;
    }
#else
    (void)ssl_mode;
#endif /* USE_SSL */

#if !defined(CURL_DISABLE_PROXY) && !defined(CURL_DISABLE_HTTP)
    if(data->set.haproxyprotocol) {
      result = Curl_conn_haproxy_add(data, conn, sockindex);
      if(result)
        goto out;
    }
#endif /* !CURL_DISABLE_PROXY && !CURL_DISABLE_HTTP */

  }
  DEBUGASSERT(conn->cfilter[sockindex]);
  cf = data->conn->cfilter[sockindex];
  result = cf->cft->setup(cf, data, remotehost);

out:
  return result;
}

CURLcode Curl_conn_connect(struct Curl_easy *data,
                           int sockindex,
                           bool blocking,
                           bool *done)
{
  struct Curl_cfilter *cf;
  CURLcode result;

  DEBUGASSERT(data);

  cf = data->conn->cfilter[sockindex];
  DEBUGASSERT(cf);
  result = cf->cft->connect(cf, data, blocking, done);

  DEBUGF(infof(data, DMSGI(data, sockindex, "connect(block=%d)-> %d, done=%d"),
         blocking, result, *done));
  return result;
}

bool Curl_conn_is_connected(struct connectdata *conn, int sockindex)
{
  struct Curl_cfilter *cf;

  cf = conn->cfilter[sockindex];
  return cf && cf->connected;
}

bool Curl_conn_is_ip_connected(struct Curl_easy *data, int sockindex)
{
  struct Curl_cfilter *cf;

  cf = data->conn->cfilter[sockindex];
  while(cf) {
    if(cf->connected)
      return TRUE;
    if(cf->cft->flags & CF_TYPE_IP_CONNECT)
      return FALSE;
    cf = cf->next;
  }
  return FALSE;
}

bool Curl_conn_is_ssl(struct Curl_easy *data, int sockindex)
{
  struct Curl_cfilter *cf = data->conn? data->conn->cfilter[sockindex] : NULL;

  (void)data;
  for(; cf; cf = cf->next) {
    if(cf->cft->flags & CF_TYPE_SSL)
      return TRUE;
    if(cf->cft->flags & CF_TYPE_IP_CONNECT)
      return FALSE;
  }
  return FALSE;
}


bool Curl_conn_data_pending(struct Curl_easy *data, int sockindex)
{
  struct Curl_cfilter *cf;

  (void)data;
  DEBUGASSERT(data);
  DEBUGASSERT(data->conn);
  if(Curl_recv_has_postponed_data(data->conn, sockindex))
    return TRUE;

  cf = data->conn->cfilter[sockindex];
  while(cf && !cf->connected) {
    cf = cf->next;
  }
  if(cf) {
    return cf->cft->has_data_pending(cf, data);
  }
  return FALSE;
}

int Curl_conn_get_select_socks(struct Curl_easy *data, int sockindex,
                               curl_socket_t *socks)
{
  struct Curl_cfilter *cf;

  DEBUGASSERT(data);
  DEBUGASSERT(data->conn);
  cf = data->conn->cfilter[sockindex];
  if(cf) {
    return cf->cft->get_select_socks(cf, data, socks);
  }
  return GETSOCK_BLANK;
}

void Curl_conn_attach_data(struct connectdata *conn,
                           struct Curl_easy *data)
{
  size_t i;
  struct Curl_cfilter *cf;

  for(i = 0; i < ARRAYSIZE(conn->cfilter); ++i) {
    cf = conn->cfilter[i];
    if(cf) {
      while(cf) {
        cf->cft->attach_data(cf, data);
        cf = cf->next;
      }
    }
  }
}

void Curl_conn_detach_data(struct connectdata *conn,
                           struct Curl_easy *data)
{
  size_t i;
  struct Curl_cfilter *cf;

  for(i = 0; i < ARRAYSIZE(conn->cfilter); ++i) {
    cf = conn->cfilter[i];
    if(cf) {
      while(cf) {
        cf->cft->detach_data(cf, data);
        cf = cf->next;
      }
    }
  }
}

void Curl_conn_get_host(struct Curl_easy *data, int sockindex,
                        const char **phost, const char **pdisplay_host,
                        int *pport)
{
  struct Curl_cfilter *cf;

  DEBUGASSERT(data->conn);
  cf = data->conn->cfilter[sockindex];
  if(cf) {
    cf->cft->get_host(cf, data, phost, pdisplay_host, pport);
  }
  else {
    /* Some filter ask during shutdown for this, mainly for debugging
     * purposes. We hand out the defaults, however this is not always
     * accurate, as the connction might be tunneled, etc. But all that
     * state is already gone here. */
    *phost = data->conn->host.name;
    *pdisplay_host = data->conn->host.dispname;
    *pport = data->conn->remote_port;
  }
}