~ubuntu-branches/ubuntu/gutsy/curl/gutsy

« back to all changes in this revision

Viewing changes to lib/socks.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-05-16 15:16:54 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20070516151654-jo48r81zempo1qav
Tags: 7.16.2-3ubuntu1
* Merge with Debian; remaining changes:
  - Drop the stunnel build dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *                                  _   _ ____  _
 
3
 *  Project                     ___| | | |  _ \| |
 
4
 *                             / __| | | | |_) | |
 
5
 *                            | (__| |_| |  _ <| |___
 
6
 *                             \___|\___/|_| \_\_____|
 
7
 *
 
8
 * Copyright (C) 1998 - 2007, 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 http://curl.haxx.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
 * $Id: socks.c,v 1.12 2007-03-30 19:59:15 bagder Exp $
 
22
 ***************************************************************************/
 
23
 
 
24
#include "setup.h"
 
25
 
 
26
#include <string.h>
 
27
 
 
28
#ifdef NEED_MALLOC_H
 
29
#include <malloc.h>
 
30
#endif
 
31
#ifdef HAVE_STDLIB_H
 
32
#include <stdlib.h>
 
33
#endif
 
34
#ifdef HAVE_SYS_SOCKET_H
 
35
#include <sys/socket.h>
 
36
#endif
 
37
#ifdef HAVE_NETINET_IN_H
 
38
#include <netinet/in.h>
 
39
#endif
 
40
#ifdef HAVE_ARPA_INET_H
 
41
#include <arpa/inet.h>
 
42
#endif
 
43
 
 
44
#include "urldata.h"
 
45
#include "sendf.h"
 
46
#include "strequal.h"
 
47
#include "select.h"
 
48
#include "connect.h"
 
49
#include "timeval.h"
 
50
#include "socks.h"
 
51
 
 
52
/* The last #include file should be: */
 
53
#include "memdebug.h"
 
54
 
 
55
/*
 
56
 * Helper read-from-socket functions. Does the same as Curl_read() but it
 
57
 * blocks until all bytes amount of buffersize will be read. No more, no less.
 
58
 *
 
59
 * This is STUPID BLOCKING behaviour which we frown upon, but right now this
 
60
 * is what we have...
 
61
 */
 
62
static int blockread_all(struct connectdata *conn, /* connection data */
 
63
                         curl_socket_t sockfd,     /* read from this socket */
 
64
                         char *buf,                /* store read data here */
 
65
                         ssize_t buffersize,       /* max amount to read */
 
66
                         ssize_t *n,               /* amount bytes read */
 
67
                         long conn_timeout)        /* timeout for data wait
 
68
                                                      relative to
 
69
                                                      conn->created */
 
70
{
 
71
  ssize_t nread;
 
72
  ssize_t allread = 0;
 
73
  int result;
 
74
  struct timeval tvnow;
 
75
  long conntime;
 
76
  *n = 0;
 
77
  do {
 
78
    tvnow = Curl_tvnow();
 
79
    /* calculating how long connection is establishing */
 
80
    conntime = Curl_tvdiff(tvnow, conn->created);
 
81
    if(conntime > conn_timeout) {
 
82
      /* we already got the timeout */
 
83
      result = ~CURLE_OK;
 
84
      break;
 
85
    }
 
86
    if(Curl_socket_ready(sockfd, CURL_SOCKET_BAD,
 
87
                   (int)(conn_timeout - conntime)) <= 0) {
 
88
      result = ~CURLE_OK;
 
89
      break;
 
90
    }
 
91
    result = Curl_read(conn, sockfd, buf, buffersize, &nread);
 
92
    if(result)
 
93
      break;
 
94
 
 
95
    if(buffersize == nread) {
 
96
      allread += nread;
 
97
      *n = allread;
 
98
      result = CURLE_OK;
 
99
      break;
 
100
    }
 
101
    buffersize -= nread;
 
102
    buf += nread;
 
103
    allread += nread;
 
104
  } while(1);
 
105
  return result;
 
106
}
 
107
 
 
108
/*
 
109
* This function logs in to a SOCKS4 proxy and sends the specifics to the final
 
110
* destination server.
 
111
*
 
112
* Reference :
 
113
*   http://socks.permeo.com/protocol/socks4.protocol
 
114
*
 
115
* Note :
 
116
*   Nonsupport "SOCKS 4A (Simple Extension to SOCKS 4 Protocol)"
 
117
*   Nonsupport "Identification Protocol (RFC1413)"
 
118
*/
 
119
CURLcode Curl_SOCKS4(const char *proxy_name,
 
120
                     char *hostname,
 
121
                     int remote_port,
 
122
                     int sockindex,
 
123
                     struct connectdata *conn)
 
124
{
 
125
  unsigned char socksreq[262]; /* room for SOCKS4 request incl. user id */
 
126
  int result;
 
127
  CURLcode code;
 
128
  curl_socket_t sock = conn->sock[sockindex];
 
129
  long timeout;
 
130
  struct SessionHandle *data = conn->data;
 
131
 
 
132
  /* get timeout */
 
133
  if(data->set.timeout && data->set.connecttimeout) {
 
134
    if (data->set.timeout < data->set.connecttimeout)
 
135
      timeout = data->set.timeout;
 
136
    else
 
137
      timeout = data->set.connecttimeout;
 
138
  }
 
139
  else if(data->set.timeout)
 
140
    timeout = data->set.timeout;
 
141
  else if(data->set.connecttimeout)
 
142
    timeout = data->set.connecttimeout;
 
143
  else
 
144
    timeout = DEFAULT_CONNECT_TIMEOUT;
 
145
 
 
146
  Curl_nonblock(sock, FALSE);
 
147
 
 
148
  /*
 
149
   * Compose socks4 request
 
150
   *
 
151
   * Request format
 
152
   *
 
153
   *     +----+----+----+----+----+----+----+----+----+----+....+----+
 
154
   *     | VN | CD | DSTPORT |      DSTIP        | USERID       |NULL|
 
155
   *     +----+----+----+----+----+----+----+----+----+----+....+----+
 
156
   * # of bytes:  1    1      2              4           variable       1
 
157
   */
 
158
 
 
159
  socksreq[0] = 4; /* version (SOCKS4) */
 
160
  socksreq[1] = 1; /* connect */
 
161
  *((unsigned short*)&socksreq[2]) = htons((unsigned short)remote_port);
 
162
 
 
163
  /* DNS resolve */
 
164
  {
 
165
    struct Curl_dns_entry *dns;
 
166
    Curl_addrinfo *hp=NULL;
 
167
    int rc;
 
168
 
 
169
    rc = Curl_resolv(conn, hostname, remote_port, &dns);
 
170
 
 
171
    if(rc == CURLRESOLV_ERROR)
 
172
      return CURLE_COULDNT_RESOLVE_PROXY;
 
173
 
 
174
    if(rc == CURLRESOLV_PENDING)
 
175
      /* this requires that we're in "wait for resolve" state */
 
176
      rc = Curl_wait_for_resolv(conn, &dns);
 
177
 
 
178
    /*
 
179
     * We cannot use 'hostent' as a struct that Curl_resolv() returns.  It
 
180
     * returns a Curl_addrinfo pointer that may not always look the same.
 
181
     */
 
182
    if(dns)
 
183
      hp=dns->addr;
 
184
    if (hp) {
 
185
      char buf[64];
 
186
      unsigned short ip[4];
 
187
      Curl_printable_address(hp, buf, sizeof(buf));
 
188
 
 
189
      if(4 == sscanf( buf, "%hu.%hu.%hu.%hu",
 
190
                      &ip[0], &ip[1], &ip[2], &ip[3])) {
 
191
        /* Set DSTIP */
 
192
        socksreq[4] = (unsigned char)ip[0];
 
193
        socksreq[5] = (unsigned char)ip[1];
 
194
        socksreq[6] = (unsigned char)ip[2];
 
195
        socksreq[7] = (unsigned char)ip[3];
 
196
      }
 
197
      else
 
198
        hp = NULL; /* fail! */
 
199
 
 
200
      Curl_resolv_unlock(data, dns); /* not used anymore from now on */
 
201
 
 
202
    }
 
203
    if(!hp) {
 
204
      failf(data, "Failed to resolve \"%s\" for SOCKS4 connect.",
 
205
            hostname);
 
206
      return CURLE_COULDNT_RESOLVE_HOST;
 
207
    }
 
208
  }
 
209
 
 
210
  /*
 
211
   * This is currently not supporting "Identification Protocol (RFC1413)".
 
212
   */
 
213
  socksreq[8] = 0; /* ensure empty userid is NUL-terminated */
 
214
  if (proxy_name)
 
215
    strlcat((char*)socksreq + 8, proxy_name, sizeof(socksreq) - 8);
 
216
 
 
217
  /*
 
218
   * Make connection
 
219
   */
 
220
  {
 
221
    ssize_t actualread;
 
222
    ssize_t written;
 
223
    int packetsize = 9 +
 
224
      (int)strlen((char*)socksreq + 8); /* size including NUL */
 
225
 
 
226
    /* Send request */
 
227
    code = Curl_write(conn, sock, (char *)socksreq, packetsize, &written);
 
228
    if ((code != CURLE_OK) || (written != packetsize)) {
 
229
      failf(data, "Failed to send SOCKS4 connect request.");
 
230
      return CURLE_COULDNT_CONNECT;
 
231
    }
 
232
 
 
233
    packetsize = 8; /* receive data size */
 
234
 
 
235
    /* Receive response */
 
236
    result = blockread_all(conn, sock, (char *)socksreq, packetsize,
 
237
                           &actualread, timeout);
 
238
    if ((result != CURLE_OK) || (actualread != packetsize)) {
 
239
      failf(data, "Failed to receive SOCKS4 connect request ack.");
 
240
      return CURLE_COULDNT_CONNECT;
 
241
    }
 
242
 
 
243
    /*
 
244
     * Response format
 
245
     *
 
246
     *     +----+----+----+----+----+----+----+----+
 
247
     *     | VN | CD | DSTPORT |      DSTIP        |
 
248
     *     +----+----+----+----+----+----+----+----+
 
249
     * # of bytes:  1    1      2              4
 
250
     *
 
251
     * VN is the version of the reply code and should be 0. CD is the result
 
252
     * code with one of the following values:
 
253
     *
 
254
     * 90: request granted
 
255
     * 91: request rejected or failed
 
256
     * 92: request rejected because SOCKS server cannot connect to
 
257
     *     identd on the client
 
258
     * 93: request rejected because the client program and identd
 
259
     *     report different user-ids
 
260
     */
 
261
 
 
262
    /* wrong version ? */
 
263
    if (socksreq[0] != 0) {
 
264
      failf(data,
 
265
            "SOCKS4 reply has wrong version, version should be 4.");
 
266
      return CURLE_COULDNT_CONNECT;
 
267
    }
 
268
 
 
269
    /* Result */
 
270
    switch(socksreq[1])
 
271
    {
 
272
    case 90:
 
273
      infof(data, "SOCKS4 request granted.\n");
 
274
      break;
 
275
    case 91:
 
276
      failf(data,
 
277
            "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
 
278
            ", request rejected or failed.",
 
279
            (unsigned char)socksreq[4], (unsigned char)socksreq[5],
 
280
            (unsigned char)socksreq[6], (unsigned char)socksreq[7],
 
281
            (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
 
282
            socksreq[1]);
 
283
      return CURLE_COULDNT_CONNECT;
 
284
    case 92:
 
285
      failf(data,
 
286
            "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
 
287
            ", request rejected because SOCKS server cannot connect to "
 
288
            "identd on the client.",
 
289
            (unsigned char)socksreq[4], (unsigned char)socksreq[5],
 
290
            (unsigned char)socksreq[6], (unsigned char)socksreq[7],
 
291
            (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
 
292
            socksreq[1]);
 
293
      return CURLE_COULDNT_CONNECT;
 
294
    case 93:
 
295
      failf(data,
 
296
            "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
 
297
            ", request rejected because the client program and identd "
 
298
            "report different user-ids.",
 
299
            (unsigned char)socksreq[4], (unsigned char)socksreq[5],
 
300
            (unsigned char)socksreq[6], (unsigned char)socksreq[7],
 
301
            (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
 
302
            socksreq[1]);
 
303
      return CURLE_COULDNT_CONNECT;
 
304
    default:
 
305
      failf(data,
 
306
            "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
 
307
            ", Unknown.",
 
308
            (unsigned char)socksreq[4], (unsigned char)socksreq[5],
 
309
            (unsigned char)socksreq[6], (unsigned char)socksreq[7],
 
310
            (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
 
311
            socksreq[1]);
 
312
      return CURLE_COULDNT_CONNECT;
 
313
    }
 
314
  }
 
315
 
 
316
  Curl_nonblock(sock, TRUE);
 
317
 
 
318
  return CURLE_OK; /* Proxy was successful! */
 
319
}
 
320
 
 
321
/*
 
322
 * This function logs in to a SOCKS5 proxy and sends the specifics to the final
 
323
 * destination server.
 
324
 */
 
325
CURLcode Curl_SOCKS5(const char *proxy_name,
 
326
                     const char *proxy_password,
 
327
                     char *hostname,
 
328
                     int remote_port,
 
329
                     int sockindex,
 
330
                     struct connectdata *conn)
 
331
{
 
332
  /*
 
333
    According to the RFC1928, section "6.  Replies". This is what a SOCK5
 
334
    replies:
 
335
 
 
336
        +----+-----+-------+------+----------+----------+
 
337
        |VER | REP |  RSV  | ATYP | BND.ADDR | BND.PORT |
 
338
        +----+-----+-------+------+----------+----------+
 
339
        | 1  |  1  | X'00' |  1   | Variable |    2     |
 
340
        +----+-----+-------+------+----------+----------+
 
341
 
 
342
    Where:
 
343
 
 
344
    o  VER    protocol version: X'05'
 
345
    o  REP    Reply field:
 
346
    o  X'00' succeeded
 
347
  */
 
348
 
 
349
  unsigned char socksreq[600]; /* room for large user/pw (255 max each) */
 
350
  ssize_t actualread;
 
351
  ssize_t written;
 
352
  int result;
 
353
  CURLcode code;
 
354
  curl_socket_t sock = conn->sock[sockindex];
 
355
  struct SessionHandle *data = conn->data;
 
356
  long timeout;
 
357
 
 
358
  /* get timeout */
 
359
  if(data->set.timeout && data->set.connecttimeout) {
 
360
    if (data->set.timeout < data->set.connecttimeout)
 
361
      timeout = data->set.timeout;
 
362
    else
 
363
      timeout = data->set.connecttimeout;
 
364
  }
 
365
  else if(data->set.timeout)
 
366
    timeout = data->set.timeout;
 
367
  else if(data->set.connecttimeout)
 
368
    timeout = data->set.connecttimeout;
 
369
  else
 
370
    timeout = DEFAULT_CONNECT_TIMEOUT;
 
371
 
 
372
  Curl_nonblock(sock, TRUE);
 
373
 
 
374
  /* wait until socket gets connected */
 
375
  result = Curl_socket_ready(CURL_SOCKET_BAD, sock, (int)timeout);
 
376
 
 
377
  if(-1 == result) {
 
378
    failf(conn->data, "SOCKS5: no connection here");
 
379
    return CURLE_COULDNT_CONNECT;
 
380
  }
 
381
  else if(0 == result) {
 
382
    failf(conn->data, "SOCKS5: connection timeout");
 
383
    return CURLE_OPERATION_TIMEDOUT;
 
384
  }
 
385
 
 
386
  if(result & CSELECT_ERR) {
 
387
    failf(conn->data, "SOCKS5: error occured during connection");
 
388
    return CURLE_COULDNT_CONNECT;
 
389
  }
 
390
 
 
391
  socksreq[0] = 5; /* version */
 
392
  socksreq[1] = (char)(proxy_name ? 2 : 1); /* number of methods (below) */
 
393
  socksreq[2] = 0; /* no authentication */
 
394
  socksreq[3] = 2; /* username/password */
 
395
 
 
396
  Curl_nonblock(sock, FALSE);
 
397
 
 
398
  code = Curl_write(conn, sock, (char *)socksreq, (2 + (int)socksreq[1]),
 
399
                      &written);
 
400
  if ((code != CURLE_OK) || (written != (2 + (int)socksreq[1]))) {
 
401
    failf(data, "Unable to send initial SOCKS5 request.");
 
402
    return CURLE_COULDNT_CONNECT;
 
403
  }
 
404
 
 
405
  Curl_nonblock(sock, TRUE);
 
406
 
 
407
  result = Curl_socket_ready(sock, CURL_SOCKET_BAD, (int)timeout);
 
408
 
 
409
  if(-1 == result) {
 
410
    failf(conn->data, "SOCKS5 nothing to read");
 
411
    return CURLE_COULDNT_CONNECT;
 
412
  }
 
413
  else if(0 == result) {
 
414
    failf(conn->data, "SOCKS5 read timeout");
 
415
    return CURLE_OPERATION_TIMEDOUT;
 
416
  }
 
417
 
 
418
  if(result & CSELECT_ERR) {
 
419
    failf(conn->data, "SOCKS5 read error occured");
 
420
    return CURLE_RECV_ERROR;
 
421
  }
 
422
 
 
423
  Curl_nonblock(sock, FALSE);
 
424
 
 
425
  result=blockread_all(conn, sock, (char *)socksreq, 2, &actualread, timeout);
 
426
  if ((result != CURLE_OK) || (actualread != 2)) {
 
427
    failf(data, "Unable to receive initial SOCKS5 response.");
 
428
    return CURLE_COULDNT_CONNECT;
 
429
  }
 
430
 
 
431
  if (socksreq[0] != 5) {
 
432
    failf(data, "Received invalid version in initial SOCKS5 response.");
 
433
    return CURLE_COULDNT_CONNECT;
 
434
  }
 
435
  if (socksreq[1] == 0) {
 
436
    /* Nothing to do, no authentication needed */
 
437
    ;
 
438
  }
 
439
  else if (socksreq[1] == 2) {
 
440
    /* Needs user name and password */
 
441
    size_t userlen, pwlen;
 
442
    int len;
 
443
    if(proxy_name && proxy_password) {
 
444
      userlen = strlen(proxy_name);
 
445
      pwlen = strlen(proxy_password);
 
446
    }
 
447
    else {
 
448
      userlen = 0;
 
449
      pwlen = 0;
 
450
    }
 
451
 
 
452
    /*   username/password request looks like
 
453
     * +----+------+----------+------+----------+
 
454
     * |VER | ULEN |  UNAME   | PLEN |  PASSWD  |
 
455
     * +----+------+----------+------+----------+
 
456
     * | 1  |  1   | 1 to 255 |  1   | 1 to 255 |
 
457
     * +----+------+----------+------+----------+
 
458
     */
 
459
    len = 0;
 
460
    socksreq[len++] = 1;    /* username/pw subnegotiation version */
 
461
    socksreq[len++] = (char) userlen;
 
462
    memcpy(socksreq + len, proxy_name, (int) userlen);
 
463
    len += userlen;
 
464
    socksreq[len++] = (char) pwlen;
 
465
    memcpy(socksreq + len, proxy_password, (int) pwlen);
 
466
    len += pwlen;
 
467
 
 
468
    code = Curl_write(conn, sock, (char *)socksreq, len, &written);
 
469
    if ((code != CURLE_OK) || (len != written)) {
 
470
      failf(data, "Failed to send SOCKS5 sub-negotiation request.");
 
471
      return CURLE_COULDNT_CONNECT;
 
472
    }
 
473
 
 
474
    result=blockread_all(conn, sock, (char *)socksreq, 2, &actualread,
 
475
                         timeout);
 
476
    if ((result != CURLE_OK) || (actualread != 2)) {
 
477
      failf(data, "Unable to receive SOCKS5 sub-negotiation response.");
 
478
      return CURLE_COULDNT_CONNECT;
 
479
    }
 
480
 
 
481
    /* ignore the first (VER) byte */
 
482
    if (socksreq[1] != 0) { /* status */
 
483
      failf(data, "User was rejected by the SOCKS5 server (%d %d).",
 
484
            socksreq[0], socksreq[1]);
 
485
      return CURLE_COULDNT_CONNECT;
 
486
    }
 
487
 
 
488
    /* Everything is good so far, user was authenticated! */
 
489
  }
 
490
  else {
 
491
    /* error */
 
492
    if (socksreq[1] == 1) {
 
493
      failf(data,
 
494
            "SOCKS5 GSSAPI per-message authentication is not supported.");
 
495
      return CURLE_COULDNT_CONNECT;
 
496
    }
 
497
    else if (socksreq[1] == 255) {
 
498
      if (!proxy_name || !*proxy_name) {
 
499
        failf(data,
 
500
              "No authentication method was acceptable. (It is quite likely"
 
501
              " that the SOCKS5 server wanted a username/password, since none"
 
502
              " was supplied to the server on this connection.)");
 
503
      }
 
504
      else {
 
505
        failf(data, "No authentication method was acceptable.");
 
506
      }
 
507
      return CURLE_COULDNT_CONNECT;
 
508
    }
 
509
    else {
 
510
      failf(data,
 
511
            "Undocumented SOCKS5 mode attempted to be used by server.");
 
512
      return CURLE_COULDNT_CONNECT;
 
513
    }
 
514
  }
 
515
 
 
516
  /* Authentication is complete, now specify destination to the proxy */
 
517
  socksreq[0] = 5; /* version (SOCKS5) */
 
518
  socksreq[1] = 1; /* connect */
 
519
  socksreq[2] = 0; /* must be zero */
 
520
  socksreq[3] = 1; /* IPv4 = 1 */
 
521
 
 
522
  {
 
523
    struct Curl_dns_entry *dns;
 
524
    Curl_addrinfo *hp=NULL;
 
525
    int rc = Curl_resolv(conn, hostname, remote_port, &dns);
 
526
 
 
527
    if(rc == CURLRESOLV_ERROR)
 
528
      return CURLE_COULDNT_RESOLVE_HOST;
 
529
 
 
530
    if(rc == CURLRESOLV_PENDING)
 
531
      /* this requires that we're in "wait for resolve" state */
 
532
      rc = Curl_wait_for_resolv(conn, &dns);
 
533
 
 
534
    /*
 
535
     * We cannot use 'hostent' as a struct that Curl_resolv() returns.  It
 
536
     * returns a Curl_addrinfo pointer that may not always look the same.
 
537
     */
 
538
    if(dns)
 
539
      hp=dns->addr;
 
540
    if (hp) {
 
541
      char buf[64];
 
542
      unsigned short ip[4];
 
543
      Curl_printable_address(hp, buf, sizeof(buf));
 
544
 
 
545
      if(4 == sscanf( buf, "%hu.%hu.%hu.%hu",
 
546
                      &ip[0], &ip[1], &ip[2], &ip[3])) {
 
547
        socksreq[4] = (unsigned char)ip[0];
 
548
        socksreq[5] = (unsigned char)ip[1];
 
549
        socksreq[6] = (unsigned char)ip[2];
 
550
        socksreq[7] = (unsigned char)ip[3];
 
551
      }
 
552
      else
 
553
        hp = NULL; /* fail! */
 
554
 
 
555
      Curl_resolv_unlock(data, dns); /* not used anymore from now on */
 
556
    }
 
557
    if(!hp) {
 
558
      failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.",
 
559
            hostname);
 
560
      return CURLE_COULDNT_RESOLVE_HOST;
 
561
    }
 
562
  }
 
563
 
 
564
  *((unsigned short*)&socksreq[8]) = htons((unsigned short)remote_port);
 
565
 
 
566
  {
 
567
    const int packetsize = 10;
 
568
 
 
569
    code = Curl_write(conn, sock, (char *)socksreq, packetsize, &written);
 
570
    if ((code != CURLE_OK) || (written != packetsize)) {
 
571
      failf(data, "Failed to send SOCKS5 connect request.");
 
572
      return CURLE_COULDNT_CONNECT;
 
573
    }
 
574
 
 
575
    result = blockread_all(conn, sock, (char *)socksreq, packetsize,
 
576
                           &actualread, timeout);
 
577
    if ((result != CURLE_OK) || (actualread != packetsize)) {
 
578
      failf(data, "Failed to receive SOCKS5 connect request ack.");
 
579
      return CURLE_COULDNT_CONNECT;
 
580
    }
 
581
 
 
582
    if (socksreq[0] != 5) { /* version */
 
583
      failf(data,
 
584
            "SOCKS5 reply has wrong version, version should be 5.");
 
585
      return CURLE_COULDNT_CONNECT;
 
586
    }
 
587
    if (socksreq[1] != 0) { /* Anything besides 0 is an error */
 
588
        failf(data,
 
589
              "Can't complete SOCKS5 connection to %d.%d.%d.%d:%d. (%d)",
 
590
              (unsigned char)socksreq[4], (unsigned char)socksreq[5],
 
591
              (unsigned char)socksreq[6], (unsigned char)socksreq[7],
 
592
              (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
 
593
              socksreq[1]);
 
594
        return CURLE_COULDNT_CONNECT;
 
595
    }
 
596
  }
 
597
 
 
598
  Curl_nonblock(sock, TRUE);
 
599
  return CURLE_OK; /* Proxy was successful! */
 
600
}