Coverage Report

Created: 2025-07-23 09:13

/src/gdal/curl/lib/socks.c
Line
Count
Source (jump to first uncovered line)
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
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 https://curl.se/docs/copyright.html.
13
 *
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.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
25
#include "curl_setup.h"
26
27
#if !defined(CURL_DISABLE_PROXY)
28
29
#ifdef HAVE_NETINET_IN_H
30
#include <netinet/in.h>
31
#endif
32
#ifdef HAVE_ARPA_INET_H
33
#include <arpa/inet.h>
34
#endif
35
36
#include "urldata.h"
37
#include "sendf.h"
38
#include "select.h"
39
#include "cfilters.h"
40
#include "connect.h"
41
#include "curlx/timeval.h"
42
#include "socks.h"
43
#include "multiif.h" /* for getsock macros */
44
#include "curlx/inet_pton.h"
45
#include "url.h"
46
47
/* The last 3 #include files should be in this order */
48
#include "curl_printf.h"
49
#include "curl_memory.h"
50
#include "memdebug.h"
51
52
/* for the (SOCKS) connect state machine */
53
enum connect_t {
54
  CONNECT_INIT,
55
  CONNECT_SOCKS_INIT, /* 1 */
56
  CONNECT_SOCKS_SEND, /* 2 waiting to send more first data */
57
  CONNECT_SOCKS_READ_INIT, /* 3 set up read */
58
  CONNECT_SOCKS_READ, /* 4 read server response */
59
  CONNECT_GSSAPI_INIT, /* 5 */
60
  CONNECT_AUTH_INIT, /* 6 setup outgoing auth buffer */
61
  CONNECT_AUTH_SEND, /* 7 send auth */
62
  CONNECT_AUTH_READ, /* 8 read auth response */
63
  CONNECT_REQ_INIT,  /* 9 init SOCKS "request" */
64
  CONNECT_RESOLVING, /* 10 */
65
  CONNECT_RESOLVED,  /* 11 */
66
  CONNECT_RESOLVE_REMOTE, /* 12 */
67
  CONNECT_REQ_SEND,  /* 13 */
68
  CONNECT_REQ_SENDING, /* 14 */
69
  CONNECT_REQ_READ,  /* 15 */
70
  CONNECT_REQ_READ_MORE, /* 16 */
71
  CONNECT_DONE /* 17 connected fine to the remote or the SOCKS proxy */
72
};
73
74
#define CURL_SOCKS_BUF_SIZE 600
75
76
/* make sure we configure it not too low */
77
#if CURL_SOCKS_BUF_SIZE < 600
78
#error CURL_SOCKS_BUF_SIZE must be at least 600
79
#endif
80
81
82
struct socks_state {
83
  enum connect_t state;
84
  size_t outstanding;  /* send this many bytes more */
85
  unsigned char buffer[CURL_SOCKS_BUF_SIZE];
86
  unsigned char *outp; /* send from this pointer */
87
88
  const char *hostname;
89
  int remote_port;
90
  const char *proxy_user;
91
  const char *proxy_password;
92
};
93
94
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
95
/*
96
 * Helper read-from-socket functions. Does the same as Curl_read() but it
97
 * blocks until all bytes amount of buffersize will be read. No more, no less.
98
 *
99
 * This is STUPID BLOCKING behavior. Only used by the SOCKS GSSAPI functions.
100
 */
101
int Curl_blockread_all(struct Curl_cfilter *cf,
102
                       struct Curl_easy *data,   /* transfer */
103
                       char *buf,                /* store read data here */
104
                       size_t blen,              /* space in buf */
105
                       size_t *pnread)           /* amount bytes read */
106
{
107
  size_t nread = 0;
108
  CURLcode err;
109
110
  *pnread = 0;
111
  for(;;) {
112
    timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
113
    if(timeout_ms < 0) {
114
      /* we already got the timeout */
115
      return CURLE_OPERATION_TIMEDOUT;
116
    }
117
    if(!timeout_ms)
118
      timeout_ms = TIMEDIFF_T_MAX;
119
    if(SOCKET_READABLE(cf->conn->sock[cf->sockindex], timeout_ms) <= 0) {
120
      return ~CURLE_OK;
121
    }
122
    err = Curl_conn_cf_recv(cf->next, data, buf, blen, &nread);
123
    if(CURLE_AGAIN == err)
124
      continue;
125
    else if(err)
126
      return (int)err;
127
128
    if(blen == nread) {
129
      *pnread += nread;
130
      return CURLE_OK;
131
    }
132
    if(!nread) /* EOF */
133
      return ~CURLE_OK;
134
135
    buf += nread;
136
    blen -= nread;
137
    *pnread += nread;
138
  }
139
}
140
#endif
141
142
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
143
#define DEBUG_AND_VERBOSE
144
#define sxstate(x,d,y) socksstate(x,d,y, __LINE__)
145
#else
146
0
#define sxstate(x,d,y) socksstate(x,d,y)
147
#endif
148
149
/* always use this function to change state, to make debugging easier */
150
static void socksstate(struct socks_state *sx, struct Curl_easy *data,
151
                       enum connect_t state
152
#ifdef DEBUG_AND_VERBOSE
153
                       , int lineno
154
#endif
155
)
156
0
{
157
0
  enum connect_t oldstate = sx->state;
158
#ifdef DEBUG_AND_VERBOSE
159
  /* synced with the state list in urldata.h */
160
  static const char * const socks_statename[] = {
161
    "INIT",
162
    "SOCKS_INIT",
163
    "SOCKS_SEND",
164
    "SOCKS_READ_INIT",
165
    "SOCKS_READ",
166
    "GSSAPI_INIT",
167
    "AUTH_INIT",
168
    "AUTH_SEND",
169
    "AUTH_READ",
170
    "REQ_INIT",
171
    "RESOLVING",
172
    "RESOLVED",
173
    "RESOLVE_REMOTE",
174
    "REQ_SEND",
175
    "REQ_SENDING",
176
    "REQ_READ",
177
    "REQ_READ_MORE",
178
    "DONE"
179
  };
180
#endif
181
182
0
  (void)data;
183
0
  if(oldstate == state)
184
    /* do not bother when the new state is the same as the old state */
185
0
    return;
186
187
0
  sx->state = state;
188
189
#ifdef DEBUG_AND_VERBOSE
190
  infof(data,
191
        "SXSTATE: %s => %s; line %d",
192
        socks_statename[oldstate], socks_statename[sx->state],
193
        lineno);
194
#endif
195
0
}
196
197
static CURLproxycode socks_state_send(struct Curl_cfilter *cf,
198
                                      struct socks_state *sx,
199
                                      struct Curl_easy *data,
200
                                      CURLproxycode failcode,
201
                                      const char *description)
202
0
{
203
0
  size_t nwritten;
204
0
  CURLcode result;
205
206
0
  result = Curl_conn_cf_send(cf->next, data, (char *)sx->outp,
207
0
                             sx->outstanding, FALSE, &nwritten);
208
0
  if(result) {
209
0
    if(CURLE_AGAIN == result)
210
0
      return CURLPX_OK;
211
212
0
    failf(data, "Failed to send %s: %s", description,
213
0
          curl_easy_strerror(result));
214
0
    return failcode;
215
0
  }
216
0
  else if(!nwritten) {
217
    /* connection closed */
218
0
    failf(data, "connection to proxy closed");
219
0
    return CURLPX_CLOSED;
220
0
  }
221
222
0
  DEBUGASSERT(sx->outstanding >= nwritten);
223
  /* not done, remain in state */
224
0
  sx->outstanding -= nwritten;
225
0
  sx->outp += nwritten;
226
0
  return CURLPX_OK;
227
0
}
228
229
static CURLproxycode socks_state_recv(struct Curl_cfilter *cf,
230
                                      struct socks_state *sx,
231
                                      struct Curl_easy *data,
232
                                      CURLproxycode failcode,
233
                                      const char *description)
234
0
{
235
0
  size_t nread;
236
0
  CURLcode result;
237
238
0
  result = Curl_conn_cf_recv(cf->next, data, (char *)sx->outp,
239
0
                            sx->outstanding, &nread);
240
0
  if(result) {
241
0
    if(CURLE_AGAIN == result)
242
0
      return CURLPX_OK;
243
244
0
    failf(data, "SOCKS: Failed receiving %s: %s", description,
245
0
          curl_easy_strerror(result));
246
0
    return failcode;
247
0
  }
248
0
  else if(!nread) {
249
    /* connection closed */
250
0
    failf(data, "connection to proxy closed");
251
0
    return CURLPX_CLOSED;
252
0
  }
253
  /* remain in reading state */
254
0
  DEBUGASSERT(sx->outstanding >= nread);
255
0
  sx->outstanding -= nread;
256
0
  sx->outp += nread;
257
0
  return CURLPX_OK;
258
0
}
259
260
/*
261
* This function logs in to a SOCKS4 proxy and sends the specifics to the final
262
* destination server.
263
*
264
* Reference :
265
*   https://www.openssh.com/txt/socks4.protocol
266
*
267
* Note :
268
*   Set protocol4a=true for  "SOCKS 4A (Simple Extension to SOCKS 4 Protocol)"
269
*   Nonsupport "Identification Protocol (RFC1413)"
270
*/
271
static CURLproxycode do_SOCKS4(struct Curl_cfilter *cf,
272
                               struct socks_state *sx,
273
                               struct Curl_easy *data)
274
0
{
275
0
  struct connectdata *conn = cf->conn;
276
0
  const bool protocol4a =
277
0
    (conn->socks_proxy.proxytype == CURLPROXY_SOCKS4A);
278
0
  unsigned char *socksreq = sx->buffer;
279
0
  CURLcode result;
280
0
  CURLproxycode presult;
281
0
  struct Curl_dns_entry *dns = NULL;
282
283
0
  switch(sx->state) {
284
0
  case CONNECT_SOCKS_INIT:
285
    /* SOCKS4 can only do IPv4, insist! */
286
0
    conn->ip_version = CURL_IPRESOLVE_V4;
287
0
    if(conn->bits.httpproxy)
288
0
      infof(data, "SOCKS4%s: connecting to HTTP proxy %s port %d",
289
0
            protocol4a ? "a" : "", sx->hostname, sx->remote_port);
290
291
0
    infof(data, "SOCKS4 communication to %s:%d",
292
0
          sx->hostname, sx->remote_port);
293
294
    /*
295
     * Compose socks4 request
296
     *
297
     * Request format
298
     *
299
     *     +----+----+----+----+----+----+----+----+----+----+....+----+
300
     *     | VN | CD | DSTPORT |      DSTIP        | USERID       |NULL|
301
     *     +----+----+----+----+----+----+----+----+----+----+....+----+
302
     * # of bytes:  1    1      2              4           variable       1
303
     */
304
305
0
    socksreq[0] = 4; /* version (SOCKS4) */
306
0
    socksreq[1] = 1; /* connect */
307
0
    socksreq[2] = (unsigned char)((sx->remote_port >> 8) & 0xff); /* MSB */
308
0
    socksreq[3] = (unsigned char)(sx->remote_port & 0xff);        /* LSB */
309
310
    /* DNS resolve only for SOCKS4, not SOCKS4a */
311
0
    if(!protocol4a) {
312
0
      result = Curl_resolv(data, sx->hostname, sx->remote_port,
313
0
                           cf->conn->ip_version, TRUE, &dns);
314
315
0
      if(result == CURLE_AGAIN) {
316
0
        sxstate(sx, data, CONNECT_RESOLVING);
317
0
        infof(data, "SOCKS4 non-blocking resolve of %s", sx->hostname);
318
0
        return CURLPX_OK;
319
0
      }
320
0
      else if(result)
321
0
        return CURLPX_RESOLVE_HOST;
322
0
      sxstate(sx, data, CONNECT_RESOLVED);
323
0
      goto CONNECT_RESOLVED;
324
0
    }
325
326
    /* socks4a does not resolve anything locally */
327
0
    sxstate(sx, data, CONNECT_REQ_INIT);
328
0
    goto CONNECT_REQ_INIT;
329
330
0
  case CONNECT_RESOLVING:
331
    /* check if we have the name resolved by now */
332
0
    result = Curl_resolv_check(data, &dns);
333
0
    if(!dns) {
334
0
      if(result)
335
0
        return CURLPX_RESOLVE_HOST;
336
0
      return CURLPX_OK;
337
0
    }
338
0
    FALLTHROUGH();
339
0
  case CONNECT_RESOLVED:
340
0
CONNECT_RESOLVED:
341
0
  {
342
0
    struct Curl_addrinfo *hp = NULL;
343
    /*
344
     * We cannot use 'hostent' as a struct that Curl_resolv() returns. It
345
     * returns a Curl_addrinfo pointer that may not always look the same.
346
     */
347
0
    if(dns) {
348
0
      hp = dns->addr;
349
350
      /* scan for the first IPv4 address */
351
0
      while(hp && (hp->ai_family != AF_INET))
352
0
        hp = hp->ai_next;
353
354
0
      if(hp) {
355
0
        struct sockaddr_in *saddr_in;
356
0
        char buf[64];
357
0
        Curl_printable_address(hp, buf, sizeof(buf));
358
359
0
        saddr_in = (struct sockaddr_in *)(void *)hp->ai_addr;
360
0
        socksreq[4] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[0];
361
0
        socksreq[5] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[1];
362
0
        socksreq[6] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[2];
363
0
        socksreq[7] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[3];
364
365
0
        infof(data, "SOCKS4 connect to IPv4 %s (locally resolved)", buf);
366
367
0
        Curl_resolv_unlink(data, &dns); /* not used anymore from now on */
368
0
      }
369
0
      else
370
0
        failf(data, "SOCKS4 connection to %s not supported", sx->hostname);
371
0
    }
372
0
    else
373
0
      failf(data, "Failed to resolve \"%s\" for SOCKS4 connect.",
374
0
            sx->hostname);
375
376
0
    if(!hp)
377
0
      return CURLPX_RESOLVE_HOST;
378
0
  }
379
0
    FALLTHROUGH();
380
0
  case CONNECT_REQ_INIT:
381
0
CONNECT_REQ_INIT:
382
    /*
383
     * This is currently not supporting "Identification Protocol (RFC1413)".
384
     */
385
0
    socksreq[8] = 0; /* ensure empty userid is null-terminated */
386
0
    if(sx->proxy_user) {
387
0
      size_t plen = strlen(sx->proxy_user);
388
0
      if(plen > 255) {
389
        /* there is no real size limit to this field in the protocol, but
390
           SOCKS5 limits the proxy user field to 255 bytes and it seems likely
391
           that a longer field is either a mistake or malicious input */
392
0
        failf(data, "Too long SOCKS proxy username");
393
0
        return CURLPX_LONG_USER;
394
0
      }
395
      /* copy the proxy name WITH trailing zero */
396
0
      memcpy(socksreq + 8, sx->proxy_user, plen + 1);
397
0
    }
398
399
    /*
400
     * Make connection
401
     */
402
0
    {
403
0
      size_t packetsize = 9 +
404
0
        strlen((char *)socksreq + 8); /* size including NUL */
405
406
      /* If SOCKS4a, set special invalid IP address 0.0.0.x */
407
0
      if(protocol4a) {
408
0
        size_t hostnamelen = 0;
409
0
        socksreq[4] = 0;
410
0
        socksreq[5] = 0;
411
0
        socksreq[6] = 0;
412
0
        socksreq[7] = 1;
413
        /* append hostname */
414
0
        hostnamelen = strlen(sx->hostname) + 1; /* length including NUL */
415
0
        if((hostnamelen <= 255) &&
416
0
           (packetsize + hostnamelen < sizeof(sx->buffer)))
417
0
          strcpy((char *)socksreq + packetsize, sx->hostname);
418
0
        else {
419
0
          failf(data, "SOCKS4: too long hostname");
420
0
          return CURLPX_LONG_HOSTNAME;
421
0
        }
422
0
        packetsize += hostnamelen;
423
0
      }
424
0
      sx->outp = socksreq;
425
0
      DEBUGASSERT(packetsize <= sizeof(sx->buffer));
426
0
      sx->outstanding = packetsize;
427
0
      sxstate(sx, data, CONNECT_REQ_SENDING);
428
0
    }
429
0
    FALLTHROUGH();
430
0
  case CONNECT_REQ_SENDING:
431
    /* Send request */
432
0
    presult = socks_state_send(cf, sx, data, CURLPX_SEND_CONNECT,
433
0
                               "SOCKS4 connect request");
434
0
    if(CURLPX_OK != presult)
435
0
      return presult;
436
0
    else if(sx->outstanding) {
437
      /* remain in sending state */
438
0
      return CURLPX_OK;
439
0
    }
440
    /* done sending! */
441
0
    sx->outstanding = 8; /* receive data size */
442
0
    sx->outp = socksreq;
443
0
    sxstate(sx, data, CONNECT_SOCKS_READ);
444
445
0
    FALLTHROUGH();
446
0
  case CONNECT_SOCKS_READ:
447
    /* Receive response */
448
0
    presult = socks_state_recv(cf, sx, data, CURLPX_RECV_CONNECT,
449
0
                               "connect request ack");
450
0
    if(CURLPX_OK != presult)
451
0
      return presult;
452
0
    else if(sx->outstanding) {
453
      /* remain in reading state */
454
0
      return CURLPX_OK;
455
0
    }
456
0
    sxstate(sx, data, CONNECT_DONE);
457
0
    break;
458
0
  default: /* lots of unused states in SOCKS4 */
459
0
    break;
460
0
  }
461
462
  /*
463
   * Response format
464
   *
465
   *     +----+----+----+----+----+----+----+----+
466
   *     | VN | CD | DSTPORT |      DSTIP        |
467
   *     +----+----+----+----+----+----+----+----+
468
   * # of bytes:  1    1      2              4
469
   *
470
   * VN is the version of the reply code and should be 0. CD is the result
471
   * code with one of the following values:
472
   *
473
   * 90: request granted
474
   * 91: request rejected or failed
475
   * 92: request rejected because SOCKS server cannot connect to
476
   *     identd on the client
477
   * 93: request rejected because the client program and identd
478
   *     report different user-ids
479
   */
480
481
  /* wrong version ? */
482
0
  if(socksreq[0]) {
483
0
    failf(data,
484
0
          "SOCKS4 reply has wrong version, version should be 0.");
485
0
    return CURLPX_BAD_VERSION;
486
0
  }
487
488
  /* Result */
489
0
  switch(socksreq[1]) {
490
0
  case 90:
491
0
    infof(data, "SOCKS4%s request granted.", protocol4a ? "a" : "");
492
0
    break;
493
0
  case 91:
494
0
    failf(data,
495
0
          "cannot complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
496
0
          ", request rejected or failed.",
497
0
          socksreq[4], socksreq[5], socksreq[6], socksreq[7],
498
0
          (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
499
0
          (unsigned char)socksreq[1]);
500
0
    return CURLPX_REQUEST_FAILED;
501
0
  case 92:
502
0
    failf(data,
503
0
          "cannot complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
504
0
          ", request rejected because SOCKS server cannot connect to "
505
0
          "identd on the client.",
506
0
          socksreq[4], socksreq[5], socksreq[6], socksreq[7],
507
0
          (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
508
0
          (unsigned char)socksreq[1]);
509
0
    return CURLPX_IDENTD;
510
0
  case 93:
511
0
    failf(data,
512
0
          "cannot complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
513
0
          ", request rejected because the client program and identd "
514
0
          "report different user-ids.",
515
0
          socksreq[4], socksreq[5], socksreq[6], socksreq[7],
516
0
          (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
517
0
          (unsigned char)socksreq[1]);
518
0
    return CURLPX_IDENTD_DIFFER;
519
0
  default:
520
0
    failf(data,
521
0
          "cannot complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
522
0
          ", Unknown.",
523
0
          socksreq[4], socksreq[5], socksreq[6], socksreq[7],
524
0
          (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
525
0
          (unsigned char)socksreq[1]);
526
0
    return CURLPX_UNKNOWN_FAIL;
527
0
  }
528
529
0
  return CURLPX_OK; /* Proxy was successful! */
530
0
}
531
532
/*
533
 * This function logs in to a SOCKS5 proxy and sends the specifics to the final
534
 * destination server.
535
 */
536
static CURLproxycode do_SOCKS5(struct Curl_cfilter *cf,
537
                               struct socks_state *sx,
538
                               struct Curl_easy *data)
539
0
{
540
  /*
541
    According to the RFC1928, section "6. Replies". This is what a SOCK5
542
    replies:
543
544
        +----+-----+-------+------+----------+----------+
545
        |VER | REP |  RSV  | ATYP | BND.ADDR | BND.PORT |
546
        +----+-----+-------+------+----------+----------+
547
        | 1  |  1  | X'00' |  1   | Variable |    2     |
548
        +----+-----+-------+------+----------+----------+
549
550
    Where:
551
552
    o  VER    protocol version: X'05'
553
    o  REP    Reply field:
554
    o  X'00' succeeded
555
  */
556
0
  struct connectdata *conn = cf->conn;
557
0
  unsigned char *socksreq = sx->buffer;
558
0
  size_t idx;
559
0
  CURLcode result;
560
0
  CURLproxycode presult;
561
0
  bool socks5_resolve_local =
562
0
    (conn->socks_proxy.proxytype == CURLPROXY_SOCKS5);
563
0
  const size_t hostname_len = strlen(sx->hostname);
564
0
  size_t len = 0;
565
0
  const unsigned char auth = data->set.socks5auth;
566
0
  bool allow_gssapi = FALSE;
567
0
  struct Curl_dns_entry *dns = NULL;
568
569
0
  switch(sx->state) {
570
0
  case CONNECT_SOCKS_INIT:
571
0
    if(conn->bits.httpproxy)
572
0
      infof(data, "SOCKS5: connecting to HTTP proxy %s port %d",
573
0
            sx->hostname, sx->remote_port);
574
575
    /* RFC1928 chapter 5 specifies max 255 chars for domain name in packet */
576
0
    if(!socks5_resolve_local && hostname_len > 255) {
577
0
      failf(data, "SOCKS5: the destination hostname is too long to be "
578
0
            "resolved remotely by the proxy.");
579
0
      return CURLPX_LONG_HOSTNAME;
580
0
    }
581
582
0
    if(auth & ~(CURLAUTH_BASIC | CURLAUTH_GSSAPI))
583
0
      infof(data,
584
0
            "warning: unsupported value passed to CURLOPT_SOCKS5_AUTH: %u",
585
0
            auth);
586
0
    if(!(auth & CURLAUTH_BASIC))
587
      /* disable username/password auth */
588
0
      sx->proxy_user = NULL;
589
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
590
    if(auth & CURLAUTH_GSSAPI)
591
      allow_gssapi = TRUE;
592
#endif
593
594
0
    if(!sx->outstanding) {
595
0
      idx = 0;
596
0
      socksreq[idx++] = 5;   /* version */
597
0
      idx++;                 /* number of authentication methods */
598
0
      socksreq[idx++] = 0;   /* no authentication */
599
0
      if(allow_gssapi)
600
0
        socksreq[idx++] = 1; /* GSS-API */
601
0
      if(sx->proxy_user)
602
0
        socksreq[idx++] = 2; /* username/password */
603
      /* write the number of authentication methods */
604
0
      socksreq[1] = (unsigned char) (idx - 2);
605
606
0
      sx->outp = socksreq;
607
0
      DEBUGASSERT(idx <= sizeof(sx->buffer));
608
0
      sx->outstanding = idx;
609
0
    }
610
611
0
    presult = socks_state_send(cf, sx, data, CURLPX_SEND_CONNECT,
612
0
                               "initial SOCKS5 request");
613
0
    if(CURLPX_OK != presult)
614
0
      return presult;
615
0
    else if(sx->outstanding) {
616
      /* remain in sending state */
617
0
      return CURLPX_OK;
618
0
    }
619
0
    sxstate(sx, data, CONNECT_SOCKS_READ);
620
0
    goto CONNECT_SOCKS_READ_INIT;
621
0
  case CONNECT_SOCKS_SEND:
622
0
    presult = socks_state_send(cf, sx, data, CURLPX_SEND_CONNECT,
623
0
                               "initial SOCKS5 request");
624
0
    if(CURLPX_OK != presult)
625
0
      return presult;
626
0
    else if(sx->outstanding) {
627
      /* remain in sending state */
628
0
      return CURLPX_OK;
629
0
    }
630
0
    FALLTHROUGH();
631
0
  case CONNECT_SOCKS_READ_INIT:
632
0
CONNECT_SOCKS_READ_INIT:
633
0
    sx->outstanding = 2; /* expect two bytes */
634
0
    sx->outp = socksreq; /* store it here */
635
0
    FALLTHROUGH();
636
0
  case CONNECT_SOCKS_READ:
637
0
    presult = socks_state_recv(cf, sx, data, CURLPX_RECV_CONNECT,
638
0
                               "initial SOCKS5 response");
639
0
    if(CURLPX_OK != presult)
640
0
      return presult;
641
0
    else if(sx->outstanding) {
642
      /* remain in reading state */
643
0
      return CURLPX_OK;
644
0
    }
645
0
    else if(socksreq[0] != 5) {
646
0
      failf(data, "Received invalid version in initial SOCKS5 response.");
647
0
      return CURLPX_BAD_VERSION;
648
0
    }
649
0
    else if(socksreq[1] == 0) {
650
      /* DONE! No authentication needed. Send request. */
651
0
      sxstate(sx, data, CONNECT_REQ_INIT);
652
0
      goto CONNECT_REQ_INIT;
653
0
    }
654
0
    else if(socksreq[1] == 2) {
655
      /* regular name + password authentication */
656
0
      sxstate(sx, data, CONNECT_AUTH_INIT);
657
0
      goto CONNECT_AUTH_INIT;
658
0
    }
659
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
660
    else if(allow_gssapi && (socksreq[1] == 1)) {
661
      sxstate(sx, data, CONNECT_GSSAPI_INIT);
662
      result = Curl_SOCKS5_gssapi_negotiate(cf, data);
663
      if(result) {
664
        failf(data, "Unable to negotiate SOCKS5 GSS-API context.");
665
        return CURLPX_GSSAPI;
666
      }
667
    }
668
#endif
669
0
    else {
670
      /* error */
671
0
      if(!allow_gssapi && (socksreq[1] == 1)) {
672
0
        failf(data,
673
0
              "SOCKS5 GSSAPI per-message authentication is not supported.");
674
0
        return CURLPX_GSSAPI_PERMSG;
675
0
      }
676
0
      else if(socksreq[1] == 255) {
677
0
        failf(data, "No authentication method was acceptable.");
678
0
        return CURLPX_NO_AUTH;
679
0
      }
680
0
    }
681
0
    failf(data,
682
0
          "Undocumented SOCKS5 mode attempted to be used by server.");
683
0
    return CURLPX_UNKNOWN_MODE;
684
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
685
  case CONNECT_GSSAPI_INIT:
686
    /* GSSAPI stuff done non-blocking */
687
    break;
688
#endif
689
690
0
  default: /* do nothing! */
691
0
    break;
692
693
0
CONNECT_AUTH_INIT:
694
0
  case CONNECT_AUTH_INIT: {
695
    /* Needs username and password */
696
0
    size_t proxy_user_len, proxy_password_len;
697
0
    if(sx->proxy_user && sx->proxy_password) {
698
0
      proxy_user_len = strlen(sx->proxy_user);
699
0
      proxy_password_len = strlen(sx->proxy_password);
700
0
    }
701
0
    else {
702
0
      proxy_user_len = 0;
703
0
      proxy_password_len = 0;
704
0
    }
705
706
    /*   username/password request looks like
707
     * +----+------+----------+------+----------+
708
     * |VER | ULEN |  UNAME   | PLEN |  PASSWD  |
709
     * +----+------+----------+------+----------+
710
     * | 1  |  1   | 1 to 255 |  1   | 1 to 255 |
711
     * +----+------+----------+------+----------+
712
     */
713
0
    len = 0;
714
0
    socksreq[len++] = 1;    /* username/pw subnegotiation version */
715
0
    socksreq[len++] = (unsigned char) proxy_user_len;
716
0
    if(sx->proxy_user && proxy_user_len) {
717
      /* the length must fit in a single byte */
718
0
      if(proxy_user_len > 255) {
719
0
        failf(data, "Excessive username length for proxy auth");
720
0
        return CURLPX_LONG_USER;
721
0
      }
722
0
      memcpy(socksreq + len, sx->proxy_user, proxy_user_len);
723
0
    }
724
0
    len += proxy_user_len;
725
0
    socksreq[len++] = (unsigned char) proxy_password_len;
726
0
    if(sx->proxy_password && proxy_password_len) {
727
      /* the length must fit in a single byte */
728
0
      if(proxy_password_len > 255) {
729
0
        failf(data, "Excessive password length for proxy auth");
730
0
        return CURLPX_LONG_PASSWD;
731
0
      }
732
0
      memcpy(socksreq + len, sx->proxy_password, proxy_password_len);
733
0
    }
734
0
    len += proxy_password_len;
735
0
    sxstate(sx, data, CONNECT_AUTH_SEND);
736
0
    DEBUGASSERT(len <= sizeof(sx->buffer));
737
0
    sx->outstanding = len;
738
0
    sx->outp = socksreq;
739
0
  }
740
0
    FALLTHROUGH();
741
0
  case CONNECT_AUTH_SEND:
742
0
    presult = socks_state_send(cf, sx, data, CURLPX_SEND_AUTH,
743
0
                               "SOCKS5 sub-negotiation request");
744
0
    if(CURLPX_OK != presult)
745
0
      return presult;
746
0
    else if(sx->outstanding) {
747
      /* remain in sending state */
748
0
      return CURLPX_OK;
749
0
    }
750
0
    sx->outp = socksreq;
751
0
    sx->outstanding = 2;
752
0
    sxstate(sx, data, CONNECT_AUTH_READ);
753
0
    FALLTHROUGH();
754
0
  case CONNECT_AUTH_READ:
755
0
    presult = socks_state_recv(cf, sx, data, CURLPX_RECV_AUTH,
756
0
                               "SOCKS5 sub-negotiation response");
757
0
    if(CURLPX_OK != presult)
758
0
      return presult;
759
0
    else if(sx->outstanding) {
760
      /* remain in reading state */
761
0
      return CURLPX_OK;
762
0
    }
763
    /* ignore the first (VER) byte */
764
0
    else if(socksreq[1]) { /* status */
765
0
      failf(data, "User was rejected by the SOCKS5 server (%d %d).",
766
0
            socksreq[0], socksreq[1]);
767
0
      return CURLPX_USER_REJECTED;
768
0
    }
769
770
    /* Everything is good so far, user was authenticated! */
771
0
    sxstate(sx, data, CONNECT_REQ_INIT);
772
0
    FALLTHROUGH();
773
0
  case CONNECT_REQ_INIT:
774
0
CONNECT_REQ_INIT:
775
0
    if(socks5_resolve_local) {
776
0
      result = Curl_resolv(data, sx->hostname, sx->remote_port,
777
0
                           cf->conn->ip_version, TRUE, &dns);
778
779
0
      if(result == CURLE_AGAIN) {
780
0
        sxstate(sx, data, CONNECT_RESOLVING);
781
0
        return CURLPX_OK;
782
0
      }
783
0
      else if(result)
784
0
        return CURLPX_RESOLVE_HOST;
785
0
      sxstate(sx, data, CONNECT_RESOLVED);
786
0
      goto CONNECT_RESOLVED;
787
0
    }
788
0
    goto CONNECT_RESOLVE_REMOTE;
789
790
0
  case CONNECT_RESOLVING:
791
    /* check if we have the name resolved by now */
792
0
    result = Curl_resolv_check(data, &dns);
793
0
    if(!dns) {
794
0
      if(result)
795
0
        return CURLPX_RESOLVE_HOST;
796
0
      return CURLPX_OK;
797
0
    }
798
0
    FALLTHROUGH();
799
0
  case CONNECT_RESOLVED:
800
0
CONNECT_RESOLVED:
801
0
  {
802
0
    char dest[MAX_IPADR_LEN];  /* printable address */
803
0
    struct Curl_addrinfo *hp = NULL;
804
0
    if(dns)
805
0
      hp = dns->addr;
806
0
#ifdef USE_IPV6
807
0
    if(data->set.ipver != CURL_IPRESOLVE_WHATEVER) {
808
0
      int wanted_family = data->set.ipver == CURL_IPRESOLVE_V4 ?
809
0
        AF_INET : AF_INET6;
810
      /* scan for the first proper address */
811
0
      while(hp && (hp->ai_family != wanted_family))
812
0
        hp = hp->ai_next;
813
0
    }
814
0
#endif
815
0
    if(!hp) {
816
0
      failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.",
817
0
            sx->hostname);
818
0
      return CURLPX_RESOLVE_HOST;
819
0
    }
820
821
0
    Curl_printable_address(hp, dest, sizeof(dest));
822
823
0
    len = 0;
824
0
    socksreq[len++] = 5; /* version (SOCKS5) */
825
0
    socksreq[len++] = 1; /* connect */
826
0
    socksreq[len++] = 0; /* must be zero */
827
0
    if(hp->ai_family == AF_INET) {
828
0
      int i;
829
0
      struct sockaddr_in *saddr_in;
830
0
      socksreq[len++] = 1; /* ATYP: IPv4 = 1 */
831
832
0
      saddr_in = (struct sockaddr_in *)(void *)hp->ai_addr;
833
0
      for(i = 0; i < 4; i++) {
834
0
        socksreq[len++] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[i];
835
0
      }
836
837
0
      infof(data, "SOCKS5 connect to %s:%d (locally resolved)", dest,
838
0
            sx->remote_port);
839
0
    }
840
0
#ifdef USE_IPV6
841
0
    else if(hp->ai_family == AF_INET6) {
842
0
      int i;
843
0
      struct sockaddr_in6 *saddr_in6;
844
0
      socksreq[len++] = 4; /* ATYP: IPv6 = 4 */
845
846
0
      saddr_in6 = (struct sockaddr_in6 *)(void *)hp->ai_addr;
847
0
      for(i = 0; i < 16; i++) {
848
0
        socksreq[len++] =
849
0
          ((unsigned char *)&saddr_in6->sin6_addr.s6_addr)[i];
850
0
      }
851
852
0
      infof(data, "SOCKS5 connect to [%s]:%d (locally resolved)", dest,
853
0
            sx->remote_port);
854
0
    }
855
0
#endif
856
0
    else {
857
0
      hp = NULL; /* fail! */
858
0
      failf(data, "SOCKS5 connection to %s not supported", dest);
859
0
    }
860
861
0
    Curl_resolv_unlink(data, &dns); /* not used anymore from now on */
862
0
    goto CONNECT_REQ_SEND;
863
0
  }
864
0
CONNECT_RESOLVE_REMOTE:
865
0
  case CONNECT_RESOLVE_REMOTE:
866
    /* Authentication is complete, now specify destination to the proxy */
867
0
    len = 0;
868
0
    socksreq[len++] = 5; /* version (SOCKS5) */
869
0
    socksreq[len++] = 1; /* connect */
870
0
    socksreq[len++] = 0; /* must be zero */
871
872
0
    if(!socks5_resolve_local) {
873
      /* ATYP: domain name = 3,
874
         IPv6 == 4,
875
         IPv4 == 1 */
876
0
      unsigned char ip4[4];
877
0
#ifdef USE_IPV6
878
0
      if(conn->bits.ipv6_ip) {
879
0
        char ip6[16];
880
0
        if(1 != curlx_inet_pton(AF_INET6, sx->hostname, ip6))
881
0
          return CURLPX_BAD_ADDRESS_TYPE;
882
0
        socksreq[len++] = 4;
883
0
        memcpy(&socksreq[len], ip6, sizeof(ip6));
884
0
        len += sizeof(ip6);
885
0
      }
886
0
      else
887
0
#endif
888
0
      if(1 == curlx_inet_pton(AF_INET, sx->hostname, ip4)) {
889
0
        socksreq[len++] = 1;
890
0
        memcpy(&socksreq[len], ip4, sizeof(ip4));
891
0
        len += sizeof(ip4);
892
0
      }
893
0
      else {
894
0
        socksreq[len++] = 3;
895
0
        socksreq[len++] = (unsigned char) hostname_len; /* one byte length */
896
0
        memcpy(&socksreq[len], sx->hostname, hostname_len); /* w/o NULL */
897
0
        len += hostname_len;
898
0
      }
899
0
      infof(data, "SOCKS5 connect to %s:%d (remotely resolved)",
900
0
            sx->hostname, sx->remote_port);
901
0
    }
902
0
    FALLTHROUGH();
903
904
0
  case CONNECT_REQ_SEND:
905
0
CONNECT_REQ_SEND:
906
    /* PORT MSB */
907
0
    socksreq[len++] = (unsigned char)((sx->remote_port >> 8) & 0xff);
908
    /* PORT LSB */
909
0
    socksreq[len++] = (unsigned char)(sx->remote_port & 0xff);
910
911
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
912
    if(conn->socks5_gssapi_enctype) {
913
      failf(data, "SOCKS5 GSS-API protection not yet implemented.");
914
      return CURLPX_GSSAPI_PROTECTION;
915
    }
916
#endif
917
0
    sx->outp = socksreq;
918
0
    DEBUGASSERT(len <= sizeof(sx->buffer));
919
0
    sx->outstanding = len;
920
0
    sxstate(sx, data, CONNECT_REQ_SENDING);
921
0
    FALLTHROUGH();
922
0
  case CONNECT_REQ_SENDING:
923
0
    presult = socks_state_send(cf, sx, data, CURLPX_SEND_REQUEST,
924
0
                               "SOCKS5 connect request");
925
0
    if(CURLPX_OK != presult)
926
0
      return presult;
927
0
    else if(sx->outstanding) {
928
      /* remain in send state */
929
0
      return CURLPX_OK;
930
0
    }
931
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
932
    if(conn->socks5_gssapi_enctype) {
933
      failf(data, "SOCKS5 GSS-API protection not yet implemented.");
934
      return CURLPX_GSSAPI_PROTECTION;
935
    }
936
#endif
937
0
    sx->outstanding = 10; /* minimum packet size is 10 */
938
0
    sx->outp = socksreq;
939
0
    sxstate(sx, data, CONNECT_REQ_READ);
940
0
    FALLTHROUGH();
941
0
  case CONNECT_REQ_READ:
942
0
    presult = socks_state_recv(cf, sx, data, CURLPX_RECV_REQACK,
943
0
                               "SOCKS5 connect request ack");
944
0
    if(CURLPX_OK != presult)
945
0
      return presult;
946
0
    else if(sx->outstanding) {
947
      /* remain in reading state */
948
0
      return CURLPX_OK;
949
0
    }
950
0
    else if(socksreq[0] != 5) { /* version */
951
0
      failf(data,
952
0
            "SOCKS5 reply has wrong version, version should be 5.");
953
0
      return CURLPX_BAD_VERSION;
954
0
    }
955
0
    else if(socksreq[1]) { /* Anything besides 0 is an error */
956
0
      CURLproxycode rc = CURLPX_REPLY_UNASSIGNED;
957
0
      int code = socksreq[1];
958
0
      failf(data, "cannot complete SOCKS5 connection to %s. (%d)",
959
0
            sx->hostname, (unsigned char)socksreq[1]);
960
0
      if(code < 9) {
961
        /* RFC 1928 section 6 lists: */
962
0
        static const CURLproxycode lookup[] = {
963
0
          CURLPX_OK,
964
0
          CURLPX_REPLY_GENERAL_SERVER_FAILURE,
965
0
          CURLPX_REPLY_NOT_ALLOWED,
966
0
          CURLPX_REPLY_NETWORK_UNREACHABLE,
967
0
          CURLPX_REPLY_HOST_UNREACHABLE,
968
0
          CURLPX_REPLY_CONNECTION_REFUSED,
969
0
          CURLPX_REPLY_TTL_EXPIRED,
970
0
          CURLPX_REPLY_COMMAND_NOT_SUPPORTED,
971
0
          CURLPX_REPLY_ADDRESS_TYPE_NOT_SUPPORTED,
972
0
        };
973
0
        rc = lookup[code];
974
0
      }
975
0
      return rc;
976
0
    }
977
978
    /* Fix: in general, returned BND.ADDR is variable length parameter by RFC
979
       1928, so the reply packet should be read until the end to avoid errors
980
       at subsequent protocol level.
981
982
       +----+-----+-------+------+----------+----------+
983
       |VER | REP |  RSV  | ATYP | BND.ADDR | BND.PORT |
984
       +----+-----+-------+------+----------+----------+
985
       | 1  |  1  | X'00' |  1   | Variable |    2     |
986
       +----+-----+-------+------+----------+----------+
987
988
       ATYP:
989
       o  IP v4 address: X'01', BND.ADDR = 4 byte
990
       o  domain name:  X'03', BND.ADDR = [ 1 byte length, string ]
991
       o  IP v6 address: X'04', BND.ADDR = 16 byte
992
    */
993
994
    /* Calculate real packet size */
995
0
    if(socksreq[3] == 3) {
996
      /* domain name */
997
0
      int addrlen = (int) socksreq[4];
998
0
      len = 5 + addrlen + 2;
999
0
    }
1000
0
    else if(socksreq[3] == 4) {
1001
      /* IPv6 */
1002
0
      len = 4 + 16 + 2;
1003
0
    }
1004
0
    else if(socksreq[3] == 1) {
1005
0
      len = 4 + 4 + 2;
1006
0
    }
1007
0
    else {
1008
0
      failf(data, "SOCKS5 reply has wrong address type.");
1009
0
      return CURLPX_BAD_ADDRESS_TYPE;
1010
0
    }
1011
1012
    /* At this point we already read first 10 bytes */
1013
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1014
    if(!conn->socks5_gssapi_enctype) {
1015
      /* decrypt_gssapi_blockread already read the whole packet */
1016
#endif
1017
0
      if(len > 10) {
1018
0
        DEBUGASSERT(len <= sizeof(sx->buffer));
1019
0
        sx->outstanding = len - 10; /* get the rest */
1020
0
        sx->outp = &socksreq[10];
1021
0
        sxstate(sx, data, CONNECT_REQ_READ_MORE);
1022
0
      }
1023
0
      else {
1024
0
        sxstate(sx, data, CONNECT_DONE);
1025
0
        break;
1026
0
      }
1027
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1028
    }
1029
#endif
1030
0
    FALLTHROUGH();
1031
0
  case CONNECT_REQ_READ_MORE:
1032
0
    presult = socks_state_recv(cf, sx, data, CURLPX_RECV_ADDRESS,
1033
0
                               "SOCKS5 connect request address");
1034
0
    if(CURLPX_OK != presult)
1035
0
      return presult;
1036
0
    else if(sx->outstanding) {
1037
      /* remain in reading state */
1038
0
      return CURLPX_OK;
1039
0
    }
1040
0
    sxstate(sx, data, CONNECT_DONE);
1041
0
  }
1042
0
  infof(data, "SOCKS5 request granted.");
1043
1044
0
  return CURLPX_OK; /* Proxy was successful! */
1045
0
}
1046
1047
static CURLcode connect_SOCKS(struct Curl_cfilter *cf,
1048
                              struct socks_state *sxstate,
1049
                              struct Curl_easy *data)
1050
0
{
1051
0
  CURLcode result = CURLE_OK;
1052
0
  CURLproxycode pxresult = CURLPX_OK;
1053
0
  struct connectdata *conn = cf->conn;
1054
1055
0
  switch(conn->socks_proxy.proxytype) {
1056
0
  case CURLPROXY_SOCKS5:
1057
0
  case CURLPROXY_SOCKS5_HOSTNAME:
1058
0
    pxresult = do_SOCKS5(cf, sxstate, data);
1059
0
    break;
1060
1061
0
  case CURLPROXY_SOCKS4:
1062
0
  case CURLPROXY_SOCKS4A:
1063
0
    pxresult = do_SOCKS4(cf, sxstate, data);
1064
0
    break;
1065
1066
0
  default:
1067
0
    failf(data, "unknown proxytype option given");
1068
0
    result = CURLE_COULDNT_CONNECT;
1069
0
  } /* switch proxytype */
1070
0
  if(pxresult) {
1071
0
    result = CURLE_PROXY;
1072
0
    data->info.pxcode = pxresult;
1073
0
  }
1074
1075
0
  return result;
1076
0
}
1077
1078
static void socks_proxy_cf_free(struct Curl_cfilter *cf)
1079
0
{
1080
0
  struct socks_state *sxstate = cf->ctx;
1081
0
  if(sxstate) {
1082
0
    free(sxstate);
1083
0
    cf->ctx = NULL;
1084
0
  }
1085
0
}
1086
1087
/* After a TCP connection to the proxy has been verified, this function does
1088
   the next magic steps. If 'done' is not set TRUE, it is not done yet and
1089
   must be called again.
1090
1091
   Note: this function's sub-functions call failf()
1092
1093
*/
1094
static CURLcode socks_proxy_cf_connect(struct Curl_cfilter *cf,
1095
                                       struct Curl_easy *data,
1096
                                       bool *done)
1097
0
{
1098
0
  CURLcode result;
1099
0
  struct connectdata *conn = cf->conn;
1100
0
  int sockindex = cf->sockindex;
1101
0
  struct socks_state *sx = cf->ctx;
1102
1103
0
  if(cf->connected) {
1104
0
    *done = TRUE;
1105
0
    return CURLE_OK;
1106
0
  }
1107
1108
0
  result = cf->next->cft->do_connect(cf->next, data, done);
1109
0
  if(result || !*done)
1110
0
    return result;
1111
1112
0
  if(!sx) {
1113
0
    sx = calloc(1, sizeof(*sx));
1114
0
    if(!sx)
1115
0
      return CURLE_OUT_OF_MEMORY;
1116
0
    cf->ctx = sx;
1117
0
  }
1118
1119
0
  if(sx->state == CONNECT_INIT) {
1120
    /* for the secondary socket (FTP), use the "connect to host"
1121
     * but ignore the "connect to port" (use the secondary port)
1122
     */
1123
0
    sxstate(sx, data, CONNECT_SOCKS_INIT);
1124
0
    sx->hostname =
1125
0
      conn->bits.httpproxy ?
1126
0
      conn->http_proxy.host.name :
1127
0
      conn->bits.conn_to_host ?
1128
0
      conn->conn_to_host.name :
1129
0
      sockindex == SECONDARYSOCKET ?
1130
0
      conn->secondaryhostname : conn->host.name;
1131
0
    sx->remote_port =
1132
0
      conn->bits.httpproxy ? (int)conn->http_proxy.port :
1133
0
      sockindex == SECONDARYSOCKET ? conn->secondary_port :
1134
0
      conn->bits.conn_to_port ? conn->conn_to_port :
1135
0
      conn->remote_port;
1136
0
    sx->proxy_user = conn->socks_proxy.user;
1137
0
    sx->proxy_password = conn->socks_proxy.passwd;
1138
0
  }
1139
1140
0
  result = connect_SOCKS(cf, sx, data);
1141
0
  if(!result && sx->state == CONNECT_DONE) {
1142
0
    cf->connected = TRUE;
1143
0
    Curl_verboseconnect(data, conn, cf->sockindex);
1144
0
    socks_proxy_cf_free(cf);
1145
0
  }
1146
1147
0
  *done = cf->connected;
1148
0
  return result;
1149
0
}
1150
1151
static void socks_cf_adjust_pollset(struct Curl_cfilter *cf,
1152
                                    struct Curl_easy *data,
1153
                                    struct easy_pollset *ps)
1154
0
{
1155
0
  struct socks_state *sx = cf->ctx;
1156
1157
0
  if(!cf->connected && sx) {
1158
    /* If we are not connected, the filter below is and has nothing
1159
     * to wait on, we determine what to wait for. */
1160
0
    curl_socket_t sock = Curl_conn_cf_get_socket(cf, data);
1161
0
    switch(sx->state) {
1162
0
    case CONNECT_RESOLVING:
1163
0
    case CONNECT_SOCKS_READ:
1164
0
    case CONNECT_AUTH_READ:
1165
0
    case CONNECT_REQ_READ:
1166
0
    case CONNECT_REQ_READ_MORE:
1167
0
      Curl_pollset_set_in_only(data, ps, sock);
1168
0
      break;
1169
0
    default:
1170
0
      Curl_pollset_set_out_only(data, ps, sock);
1171
0
      break;
1172
0
    }
1173
0
  }
1174
0
}
1175
1176
static void socks_proxy_cf_close(struct Curl_cfilter *cf,
1177
                                 struct Curl_easy *data)
1178
0
{
1179
1180
0
  DEBUGASSERT(cf->next);
1181
0
  cf->connected = FALSE;
1182
0
  socks_proxy_cf_free(cf);
1183
0
  cf->next->cft->do_close(cf->next, data);
1184
0
}
1185
1186
static void socks_proxy_cf_destroy(struct Curl_cfilter *cf,
1187
                                   struct Curl_easy *data)
1188
0
{
1189
0
  (void)data;
1190
0
  socks_proxy_cf_free(cf);
1191
0
}
1192
1193
static CURLcode socks_cf_query(struct Curl_cfilter *cf,
1194
                               struct Curl_easy *data,
1195
                               int query, int *pres1, void *pres2)
1196
0
{
1197
0
  struct socks_state *sx = cf->ctx;
1198
1199
0
  if(sx) {
1200
0
    switch(query) {
1201
0
    case CF_QUERY_HOST_PORT:
1202
0
      *pres1 = sx->remote_port;
1203
0
      *((const char **)pres2) = sx->hostname;
1204
0
      return CURLE_OK;
1205
0
    default:
1206
0
      break;
1207
0
    }
1208
0
  }
1209
0
  return cf->next ?
1210
0
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
1211
0
    CURLE_UNKNOWN_OPTION;
1212
0
}
1213
1214
struct Curl_cftype Curl_cft_socks_proxy = {
1215
  "SOCKS-PROXYY",
1216
  CF_TYPE_IP_CONNECT|CF_TYPE_PROXY,
1217
  0,
1218
  socks_proxy_cf_destroy,
1219
  socks_proxy_cf_connect,
1220
  socks_proxy_cf_close,
1221
  Curl_cf_def_shutdown,
1222
  socks_cf_adjust_pollset,
1223
  Curl_cf_def_data_pending,
1224
  Curl_cf_def_send,
1225
  Curl_cf_def_recv,
1226
  Curl_cf_def_cntrl,
1227
  Curl_cf_def_conn_is_alive,
1228
  Curl_cf_def_conn_keep_alive,
1229
  socks_cf_query,
1230
};
1231
1232
CURLcode Curl_cf_socks_proxy_insert_after(struct Curl_cfilter *cf_at,
1233
                                          struct Curl_easy *data)
1234
0
{
1235
0
  struct Curl_cfilter *cf;
1236
0
  CURLcode result;
1237
1238
0
  (void)data;
1239
0
  result = Curl_cf_create(&cf, &Curl_cft_socks_proxy, NULL);
1240
0
  if(!result)
1241
0
    Curl_conn_cf_insert_after(cf_at, cf);
1242
0
  return result;
1243
0
}
1244
1245
#endif /* CURL_DISABLE_PROXY */