GSocks5Proxy: don't crash if parsing negotiation reply fails
[platform/upstream/glib.git] / gio / gsocks5proxy.c
1  /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2008, 2010 Collabora, Ltd.
4  * Copyright (C) 2008 Nokia Corporation. All rights reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General
17  * Public License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  * Author:  Youness Alaoui <youness.alaoui@collabora.co.uk
22  *
23  * Contributors:
24  *          Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
25  */
26
27 #include "config.h"
28
29 #include "gsocks5proxy.h"
30
31 #include <string.h>
32
33 #include "giomodule.h"
34 #include "giomodule-priv.h"
35 #include "giostream.h"
36 #include "ginetaddress.h"
37 #include "ginputstream.h"
38 #include "glibintl.h"
39 #include "goutputstream.h"
40 #include "gproxy.h"
41 #include "gproxyaddress.h"
42 #include "gtask.h"
43
44 #define SOCKS5_VERSION            0x05
45
46 #define SOCKS5_CMD_CONNECT        0x01
47 #define SOCKS5_CMD_BIND           0x02
48 #define SOCKS5_CMD_UDP_ASSOCIATE  0x03
49
50 #define SOCKS5_ATYP_IPV4          0x01
51 #define SOCKS5_ATYP_DOMAINNAME    0x03
52 #define SOCKS5_ATYP_IPV6          0x04
53
54 #define SOCKS5_AUTH_VERSION       0x01
55
56 #define SOCKS5_AUTH_NONE          0x00
57 #define SOCKS5_AUTH_GSSAPI        0x01
58 #define SOCKS5_AUTH_USR_PASS      0x02
59 #define SOCKS5_AUTH_NO_ACCEPT     0xff
60
61 #define SOCKS5_MAX_LEN            255
62 #define SOCKS5_RESERVED           0x00
63
64 #define SOCKS5_REP_SUCCEEDED      0x00
65 #define SOCKS5_REP_SRV_FAILURE    0x01
66 #define SOCKS5_REP_NOT_ALLOWED    0x02
67 #define SOCKS5_REP_NET_UNREACH    0x03
68 #define SOCKS5_REP_HOST_UNREACH   0x04
69 #define SOCKS5_REP_REFUSED        0x05
70 #define SOCKS5_REP_TTL_EXPIRED    0x06
71 #define SOCKS5_REP_CMD_NOT_SUP    0x07
72 #define SOCKS5_REP_ATYPE_NOT_SUP  0x08
73
74
75 struct _GSocks5Proxy
76 {
77   GObject parent;
78 };
79
80 struct _GSocks5ProxyClass
81 {
82   GObjectClass parent_class;
83 };
84
85 static void g_socks5_proxy_iface_init (GProxyInterface *proxy_iface);
86
87 #define g_socks5_proxy_get_type _g_socks5_proxy_get_type
88 G_DEFINE_TYPE_WITH_CODE (GSocks5Proxy, g_socks5_proxy, G_TYPE_OBJECT,
89                          G_IMPLEMENT_INTERFACE (G_TYPE_PROXY,
90                                                 g_socks5_proxy_iface_init)
91                          _g_io_modules_ensure_extension_points_registered ();
92                          g_io_extension_point_implement (G_PROXY_EXTENSION_POINT_NAME,
93                                                          g_define_type_id,
94                                                          "socks5",
95                                                          0))
96
97 static void
98 g_socks5_proxy_finalize (GObject *object)
99 {
100   /* must chain up */
101   G_OBJECT_CLASS (g_socks5_proxy_parent_class)->finalize (object);
102 }
103
104 static void
105 g_socks5_proxy_init (GSocks5Proxy *proxy)
106 {
107 }
108
109 /*
110  * +----+----------+----------+
111  * |VER | NMETHODS | METHODS  |
112  * +----+----------+----------+
113  * | 1  |    1     | 1 to 255 |
114  * +----+----------+----------+
115  */
116 #define SOCKS5_NEGO_MSG_LEN       4
117 static gint
118 set_nego_msg (guint8 *msg, gboolean has_auth)
119 {
120   gint len = 3;
121
122   msg[0] = SOCKS5_VERSION;
123   msg[1] = 0x01; /* number of methods supported */
124   msg[2] = SOCKS5_AUTH_NONE;
125
126   /* add support for authentication method */
127   if (has_auth)
128     {
129       msg[1] = 0x02; /* number of methods supported */
130       msg[3] = SOCKS5_AUTH_USR_PASS;
131       len++;
132     }
133
134   return len;
135 }
136
137
138 /*
139  * +----+--------+
140  * |VER | METHOD |
141  * +----+--------+
142  * | 1  |   1    |
143  * +----+--------+
144  */
145 #define SOCKS5_NEGO_REP_LEN       2
146 static gboolean
147 parse_nego_reply (const guint8 *data,
148                   gboolean     has_auth,
149                   gboolean    *must_auth,
150                   GError     **error)
151 {
152   if (data[0] != SOCKS5_VERSION)
153     {
154       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
155                            _("The server is not a SOCKSv5 proxy server."));
156       return FALSE;
157     }
158
159   switch (data[1])
160     {
161       case SOCKS5_AUTH_NONE:
162         *must_auth = FALSE;
163         break;
164
165       case SOCKS5_AUTH_USR_PASS:
166         if (!has_auth)
167           {
168             g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_NEED_AUTH,
169                            _("The SOCKSv5 proxy requires authentication."));
170             return FALSE;
171           }
172         *must_auth = TRUE;
173         break;
174
175       case SOCKS5_AUTH_GSSAPI:
176       case SOCKS5_AUTH_NO_ACCEPT:
177       default:
178         g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_AUTH_FAILED,
179                              _("The SOCKSv5 proxy requires an authentication "
180                                "method that is not supported by GLib."));
181         return FALSE;
182         break;
183     }
184
185   return TRUE;
186 }
187
188 #define SOCKS5_AUTH_MSG_LEN       515
189 static gint
190 set_auth_msg (guint8      *msg,
191               const gchar *username,
192               const gchar *password,
193               GError **error)
194 {
195   gint len = 0;
196   gint ulen = 0; /* username length */
197   gint plen = 0; /* Password length */
198
199   if (username)
200     ulen = strlen (username);
201
202   if (password)
203     plen = strlen (password);
204
205   if (ulen > SOCKS5_MAX_LEN || plen > SOCKS5_MAX_LEN)
206     {
207       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
208                            _("Username or password is too long for SOCKSv5 "
209                              "protocol."));
210       return FALSE;
211     }
212
213   msg[len++] = SOCKS5_AUTH_VERSION;
214   msg[len++] = ulen;
215
216   if (ulen > 0)
217     memcpy (msg + len, username, ulen);
218
219   len += ulen;
220   msg[len++] = plen;
221
222   if (plen > 0)
223     memcpy (msg + len, password, plen);
224
225   len += plen;
226
227   return len;
228 }
229
230
231 static gboolean
232 check_auth_status (const guint8 *data, GError **error)
233 {
234   if (data[0] != SOCKS5_VERSION
235       || data[1] != SOCKS5_REP_SUCCEEDED)
236     {
237       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_AUTH_FAILED,
238                            _("SOCKSv5 authentication failed due to wrong "
239                              "username or password."));
240       return FALSE;
241     }
242   return TRUE;
243 }
244
245 /*
246  * +----+-----+-------+------+----------+----------+
247  * |VER | CMD |  RSV  | ATYP | DST.ADDR | DST.PORT |
248  * +----+-----+-------+------+----------+----------+
249  * | 1  |  1  | X'00' |  1   | Variable |    2     |
250  * +----+-----+-------+------+----------+----------+
251  * DST.ADDR is a string with first byte being the size. So DST.ADDR may not be
252  * longer then 256 bytes.
253  */
254 #define SOCKS5_CONN_MSG_LEN       262
255 static gint
256 set_connect_msg (guint8       *msg,
257                  const gchar *hostname,
258                  guint16      port,
259                  GError     **error)
260 {
261   guint len = 0;
262
263   msg[len++] = SOCKS5_VERSION;
264   msg[len++] = SOCKS5_CMD_CONNECT;
265   msg[len++] = SOCKS5_RESERVED;
266
267   if (g_hostname_is_ip_address (hostname))
268     {
269       GInetAddress *addr = g_inet_address_new_from_string (hostname);
270       gsize addr_len = g_inet_address_get_native_size (addr);
271
272       /* We are cheating for simplicity, here's the logic:
273        *   1 = IPV4 = 4 bytes / 4
274        *   4 = IPV6 = 16 bytes / 4 */
275       msg[len++] = addr_len / 4;
276       memcpy (msg + len, g_inet_address_to_bytes (addr), addr_len);
277       len += addr_len;
278
279       g_object_unref (addr);
280     }
281   else
282     {
283       gsize host_len = strlen (hostname);
284
285       if (host_len > SOCKS5_MAX_LEN)
286         {
287           g_set_error (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
288                        _("Hostname '%s' is too long for SOCKSv5 protocol"),
289                        hostname);
290           return -1;
291         }
292
293       msg[len++] = SOCKS5_ATYP_DOMAINNAME;
294       msg[len++] = (guint8) host_len;
295       memcpy (msg + len, hostname, host_len);
296       len += host_len;
297     }
298
299     {
300       guint16 hp = g_htons (port);
301       memcpy (msg + len, &hp, 2);
302       len += 2;
303     }
304
305   return len;
306 }
307
308 /*
309  * +----+-----+-------+------+----------+----------+
310  * |VER | REP |  RSV  | ATYP | BND.ADDR | BND.PORT |
311  * +----+-----+-------+------+----------+----------+
312  * | 1  |  1  | X'00' |  1   | Variable |    2     |
313  * +----+-----+-------+------+----------+----------+
314  * This reply need to be read by small part to determin size. Buffer
315  * size is determined in function of the biggest part to read.
316  *
317  * The parser only requires 4 bytes.
318  */
319 #define SOCKS5_CONN_REP_LEN       255
320 static gboolean
321 parse_connect_reply (const guint8 *data, gint *atype, GError **error)
322 {
323   if (data[0] != SOCKS5_VERSION)
324     {
325       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
326                            _("The server is not a SOCKSv5 proxy server."));
327       return FALSE;
328     }
329
330   switch (data[1])
331     {
332       case SOCKS5_REP_SUCCEEDED:
333         if (data[2] != SOCKS5_RESERVED)
334           {
335             g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
336                            _("The server is not a SOCKSv5 proxy server."));
337             return FALSE;
338           }
339
340         switch (data[3])
341           {
342           case SOCKS5_ATYP_IPV4:
343           case SOCKS5_ATYP_IPV6:
344           case SOCKS5_ATYP_DOMAINNAME:
345             *atype = data[3];
346             break;
347
348           default:
349             g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
350                            _("The SOCKSv5 proxy server uses unknown address type."));
351             return FALSE;
352           }
353         break;
354
355       case SOCKS5_REP_SRV_FAILURE:
356         g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
357                              _("Internal SOCKSv5 proxy server error."));
358         return FALSE;
359         break;
360
361       case SOCKS5_REP_NOT_ALLOWED:
362         g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_NOT_ALLOWED,
363                              _("SOCKSv5 connection not allowed by ruleset."));
364         return FALSE;
365         break;
366
367       case SOCKS5_REP_TTL_EXPIRED:
368       case SOCKS5_REP_HOST_UNREACH:
369         g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_HOST_UNREACHABLE,
370                              _("Host unreachable through SOCKSv5 server."));
371         return FALSE;
372         break;
373
374       case SOCKS5_REP_NET_UNREACH:
375         g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NETWORK_UNREACHABLE,
376                              _("Network unreachable through SOCKSv5 proxy."));
377         return FALSE;
378         break;
379
380       case SOCKS5_REP_REFUSED:
381         g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_REFUSED,
382                              _("Connection refused through SOCKSv5 proxy."));
383         return FALSE;
384         break;
385
386       case SOCKS5_REP_CMD_NOT_SUP:
387         g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
388                              _("SOCKSv5 proxy does not support 'connect' command."));
389         return FALSE;
390         break;
391
392       case SOCKS5_REP_ATYPE_NOT_SUP:
393         g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
394                              _("SOCKSv5 proxy does not support provided address type."));
395         return FALSE;
396         break;
397
398       default: /* Unknown error */
399         g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
400                              _("Unknown SOCKSv5 proxy error."));
401         return FALSE;
402         break;
403     }
404
405   return TRUE;
406 }
407
408 static GIOStream *
409 g_socks5_proxy_connect (GProxy            *proxy,
410                         GIOStream         *io_stream,
411                         GProxyAddress     *proxy_address,
412                         GCancellable      *cancellable,
413                         GError          **error)
414 {
415   gboolean has_auth;
416   GInputStream *in;
417   GOutputStream *out;
418   const gchar *hostname;
419   guint16 port;
420   const gchar *username;
421   const gchar *password;
422
423   hostname = g_proxy_address_get_destination_hostname (proxy_address);
424   port = g_proxy_address_get_destination_port (proxy_address);
425   username = g_proxy_address_get_username (proxy_address);
426   password = g_proxy_address_get_password (proxy_address);
427
428   has_auth = username || password;
429   
430   in = g_io_stream_get_input_stream (io_stream);
431   out = g_io_stream_get_output_stream (io_stream);
432
433   /* Send SOCKS5 handshake */
434     {
435       guint8 msg[SOCKS5_NEGO_MSG_LEN];
436       gint len;
437
438       len = set_nego_msg (msg, has_auth);
439
440       if (!g_output_stream_write_all (out, msg, len, NULL,
441                                       cancellable, error))
442         goto error;
443     }
444
445   /* Receive SOCKS5 response and reply with authentication if required */
446     {
447       guint8 data[SOCKS5_NEGO_REP_LEN];
448       gboolean must_auth = FALSE;
449
450       if (!g_input_stream_read_all (in, data, sizeof (data), NULL,
451                                     cancellable, error))
452         goto error;
453
454       if (!parse_nego_reply (data, has_auth, &must_auth, error))
455           goto error;
456
457       if (must_auth)
458         {
459           guint8 msg[SOCKS5_AUTH_MSG_LEN];
460           gint len;
461
462           len = set_auth_msg (msg, username, password, error);
463
464           if (len < 0)
465             goto error;
466           
467           if (!g_output_stream_write_all (out, msg, len, NULL,
468                                           cancellable, error))
469             goto error;
470
471           if (!g_input_stream_read_all (in, data, sizeof (data), NULL,
472                                         cancellable, error))
473             goto error;
474
475           if (!check_auth_status (data, error))
476             goto error;
477         }
478     }
479
480   /* Send SOCKS5 connection request */
481     {
482       guint8 msg[SOCKS5_CONN_MSG_LEN];
483       gint len;
484       
485       len = set_connect_msg (msg, hostname, port, error);
486
487       if (len < 0)
488         goto error;
489
490       if (!g_output_stream_write_all (out, msg, len, NULL,
491                                       cancellable, error))
492         goto error;
493     }
494
495   /* Read SOCKS5 response */
496     {
497       guint8 data[SOCKS5_CONN_REP_LEN];
498       gint atype;
499
500       if (!g_input_stream_read_all (in, data, 4, NULL,
501                                     cancellable, error))
502         goto error;
503
504       if (!parse_connect_reply (data, &atype, error))
505         goto error;
506
507       switch (atype)
508         {
509           case SOCKS5_ATYP_IPV4:
510             if (!g_input_stream_read_all (in, data, 6, NULL,
511                                           cancellable, error))
512               goto error;
513             break;
514
515           case SOCKS5_ATYP_IPV6:
516             if (!g_input_stream_read_all (in, data, 18, NULL,
517                                           cancellable, error))
518               goto error;
519             break;
520
521           case SOCKS5_ATYP_DOMAINNAME:
522             if (!g_input_stream_read_all (in, data, 1, NULL,
523                                           cancellable, error))
524               goto error;
525             if (!g_input_stream_read_all (in, data, data[0] + 2, NULL,
526                                           cancellable, error))
527               goto error;
528             break;
529         }
530     }
531
532   return g_object_ref (io_stream);
533
534 error:
535   return NULL;
536 }
537
538
539 typedef struct
540 {
541   GIOStream *io_stream;
542   gchar *hostname;
543   guint16 port;
544   gchar *username;
545   gchar *password;
546   guint8 *buffer;
547   gssize length;
548   gssize offset;
549 } ConnectAsyncData;
550
551 static void nego_msg_write_cb         (GObject          *source,
552                                        GAsyncResult     *res,
553                                        gpointer          user_data);
554 static void nego_reply_read_cb        (GObject          *source,
555                                        GAsyncResult     *res,
556                                        gpointer          user_data);
557 static void auth_msg_write_cb         (GObject          *source,
558                                        GAsyncResult     *res,
559                                        gpointer          user_data);
560 static void auth_reply_read_cb        (GObject          *source,
561                                        GAsyncResult     *result,
562                                        gpointer          user_data);
563 static void send_connect_msg          (GTask            *task);
564 static void connect_msg_write_cb      (GObject          *source,
565                                        GAsyncResult     *result,
566                                        gpointer          user_data);
567 static void connect_reply_read_cb     (GObject          *source,
568                                        GAsyncResult     *result,
569                                        gpointer          user_data);
570 static void connect_addr_len_read_cb  (GObject          *source,
571                                        GAsyncResult     *result,
572                                        gpointer          user_data);
573 static void connect_addr_read_cb      (GObject          *source,
574                                        GAsyncResult     *result,
575                                        gpointer          user_data);
576
577 static void
578 free_connect_data (ConnectAsyncData *data)
579 {
580   g_object_unref (data->io_stream);
581
582   g_free (data->hostname);
583   g_free (data->username);
584   g_free (data->password);
585   g_free (data->buffer);
586
587   g_slice_free (ConnectAsyncData, data);
588 }
589
590 static void
591 do_read (GAsyncReadyCallback callback, GTask *task, ConnectAsyncData *data)
592 {
593    GInputStream *in;
594    in = g_io_stream_get_input_stream (data->io_stream);
595    g_input_stream_read_async (in,
596                               data->buffer + data->offset,
597                               data->length - data->offset,
598                               g_task_get_priority (task),
599                               g_task_get_cancellable (task),
600                               callback, task);
601 }
602
603 static void
604 do_write (GAsyncReadyCallback callback, GTask *task, ConnectAsyncData *data)
605 {
606   GOutputStream *out;
607   out = g_io_stream_get_output_stream (data->io_stream);
608   g_output_stream_write_async (out,
609                                data->buffer + data->offset,
610                                data->length - data->offset,
611                                g_task_get_priority (task),
612                                g_task_get_cancellable (task),
613                                callback, task);
614 }
615
616 static void
617 g_socks5_proxy_connect_async (GProxy               *proxy,
618                               GIOStream            *io_stream,
619                               GProxyAddress        *proxy_address,
620                               GCancellable         *cancellable,
621                               GAsyncReadyCallback   callback,
622                               gpointer              user_data)
623 {
624   GTask *task;
625   ConnectAsyncData *data;
626
627   data = g_slice_new0 (ConnectAsyncData);
628   data->io_stream = g_object_ref (io_stream);
629
630   task = g_task_new (proxy, cancellable, callback, user_data);
631   g_task_set_task_data (task, data, (GDestroyNotify) free_connect_data);
632
633   g_object_get (G_OBJECT (proxy_address),
634                 "destination-hostname", &data->hostname,
635                 "destination-port", &data->port,
636                 "username", &data->username,
637                 "password", &data->password,
638                 NULL);
639
640   data->buffer = g_malloc0 (SOCKS5_NEGO_MSG_LEN);
641   data->length = set_nego_msg (data->buffer,
642                                data->username || data->password);
643   data->offset = 0;
644
645   do_write (nego_msg_write_cb, task, data);
646 }
647
648
649 static void
650 nego_msg_write_cb (GObject      *source,
651                    GAsyncResult *res,
652                    gpointer      user_data)
653 {
654   GTask *task = user_data;
655   ConnectAsyncData *data = g_task_get_task_data (task);
656   GError *error = NULL;
657   gssize written;
658
659   written = g_output_stream_write_finish (G_OUTPUT_STREAM (source),
660                                           res, &error);
661
662   if (written < 0)
663     {
664       g_task_return_error (task, error);
665       g_object_unref (task);
666       return;
667     }
668
669   data->offset += written;
670
671   if (data->offset == data->length)
672     {
673       g_free (data->buffer);
674
675       data->buffer = g_malloc0 (SOCKS5_NEGO_REP_LEN);
676       data->length = SOCKS5_NEGO_REP_LEN;
677       data->offset = 0;
678
679       do_read (nego_reply_read_cb, task, data);
680     }
681   else
682     {
683       do_write (nego_msg_write_cb, task, data);
684     }
685 }
686
687 static void
688 nego_reply_read_cb (GObject      *source,
689                     GAsyncResult *res,
690                     gpointer      user_data)
691 {
692   GTask *task = user_data;
693   ConnectAsyncData *data = g_task_get_task_data (task);
694   GError *error = NULL;
695   gssize read;
696
697   read = g_input_stream_read_finish (G_INPUT_STREAM (source),
698                                      res, &error);
699
700   if (read < 0)
701     {
702       g_task_return_error (task, error);
703       g_object_unref (task);
704       return;
705     }
706
707   data->offset += read;
708   
709   if (data->offset == data->length)
710     {
711       GError *error = NULL;
712       gboolean must_auth = FALSE;
713       gboolean has_auth = data->username || data->password;
714
715       if (!parse_nego_reply (data->buffer, has_auth, &must_auth, &error))
716         {
717           g_task_return_error (task, error);
718           g_object_unref (task);
719           return;
720         }
721
722       if (must_auth)
723         {
724           g_free (data->buffer);
725
726           data->buffer = g_malloc0 (SOCKS5_AUTH_MSG_LEN);
727           data->length = set_auth_msg (data->buffer,
728                                        data->username,
729                                        data->password,
730                                        &error);
731           data->offset = 0;
732
733           if (data->length < 0)
734             {
735               g_task_return_error (task, error);
736               g_object_unref (task);
737               return;
738             }
739           
740           do_write (auth_msg_write_cb, task, data);
741         }
742       else
743         {
744           send_connect_msg (task);
745         }
746     }
747   else
748     {
749       do_read (nego_reply_read_cb, task, data);
750     }
751 }
752
753 static void
754 auth_msg_write_cb (GObject      *source,
755                    GAsyncResult *result,
756                    gpointer      user_data)
757 {
758   GTask *task = user_data;
759   ConnectAsyncData *data = g_task_get_task_data (task);
760   GError *error = NULL;
761   gssize written;
762
763   written = g_output_stream_write_finish (G_OUTPUT_STREAM (source),
764                                           result, &error);
765   
766   if (written < 0)
767     {
768       g_task_return_error (task, error);
769       g_object_unref (task);
770       return;
771     }
772
773   data->offset += written;
774
775   if (data->offset == data->length)
776     {
777       g_free (data->buffer);
778
779       data->buffer = g_malloc0 (SOCKS5_NEGO_REP_LEN);
780       data->length = SOCKS5_NEGO_REP_LEN;
781       data->offset = 0;
782
783       do_read (auth_reply_read_cb, task, data);
784     }
785   else
786     {
787       do_write (auth_msg_write_cb, task, data);
788     }
789 }
790
791 static void
792 auth_reply_read_cb (GObject      *source,
793                     GAsyncResult *result,
794                     gpointer      user_data)
795 {
796   GTask *task = user_data;
797   ConnectAsyncData *data = g_task_get_task_data (task);
798   GError *error = NULL;
799   gssize read;
800
801   read = g_input_stream_read_finish (G_INPUT_STREAM (source),
802                                      result, &error);
803
804   if (read < 0)
805     {
806       g_task_return_error (task, error);
807       g_object_unref (task);
808       return;
809     }
810
811   data->offset += read;
812
813   if (data->offset == data->length)
814     {
815       if (!check_auth_status (data->buffer, &error))
816         {
817           g_task_return_error (task, error);
818           g_object_unref (task);
819           return;
820         }
821         
822       send_connect_msg (task);
823     }
824   else
825     {
826       do_read (auth_reply_read_cb, task, data);
827     }
828 }
829
830 static void
831 send_connect_msg (GTask *task)
832 {
833   ConnectAsyncData *data = g_task_get_task_data (task);
834   GError *error = NULL;
835
836   g_free (data->buffer);
837
838   data->buffer = g_malloc0 (SOCKS5_CONN_MSG_LEN);
839   data->length = set_connect_msg (data->buffer,
840                                   data->hostname,
841                                   data->port,
842                                   &error);
843   data->offset = 0;
844   
845   if (data->length < 0)
846     {
847       g_task_return_error (task, error);
848       g_object_unref (task);
849       return;
850     }
851
852   do_write (connect_msg_write_cb, task, data);
853 }
854
855 static void
856 connect_msg_write_cb (GObject      *source,
857                       GAsyncResult *result,
858                       gpointer      user_data)
859 {
860   GTask *task = user_data;
861   ConnectAsyncData *data = g_task_get_task_data (task);
862   GError *error = NULL;
863   gssize written;
864
865   written = g_output_stream_write_finish (G_OUTPUT_STREAM (source),
866                                           result, &error);
867   
868   if (written < 0)
869     {
870       g_task_return_error (task, error);
871       g_object_unref (task);
872       return;
873     }
874
875   data->offset += written;
876
877   if (data->offset == data->length)
878     {
879       g_free (data->buffer);
880
881       data->buffer = g_malloc0 (SOCKS5_CONN_REP_LEN);
882       data->length = 4;
883       data->offset = 0;
884
885       do_read (connect_reply_read_cb, task, data);
886     }
887   else
888     {
889       do_write (connect_msg_write_cb, task, data);
890     }
891 }
892
893 static void
894 connect_reply_read_cb (GObject       *source,
895                        GAsyncResult  *result,
896                        gpointer       user_data)
897 {
898   GTask *task = user_data;
899   ConnectAsyncData *data = g_task_get_task_data (task);
900   GError *error = NULL;
901   gssize read;
902
903   read = g_input_stream_read_finish (G_INPUT_STREAM (source),
904                                      result, &error);
905
906   if (read < 0)
907     {
908       g_task_return_error (task, error);
909       g_object_unref (task);
910       return;
911     }
912
913   data->offset += read;
914
915   if (data->offset == data->length)
916     {
917       gint atype;
918
919       if (!parse_connect_reply (data->buffer, &atype, &error))
920         {
921           g_task_return_error (task, error);
922           g_object_unref (task);
923           return;
924         }
925
926       switch (atype)
927         {
928           case SOCKS5_ATYP_IPV4:
929             data->length = 6;
930             data->offset = 0;
931             do_read (connect_addr_read_cb, task, data);
932             break;
933
934           case SOCKS5_ATYP_IPV6:
935             data->length = 18;
936             data->offset = 0;
937             do_read (connect_addr_read_cb, task, data);
938             break;
939
940           case SOCKS5_ATYP_DOMAINNAME:
941             data->length = 1;
942             data->offset = 0;
943             do_read (connect_addr_len_read_cb, task, data);
944             break;
945         }
946     }
947   else
948     {
949       do_read (connect_reply_read_cb, task, data);
950     }
951 }
952
953 static void
954 connect_addr_len_read_cb (GObject      *source,
955                           GAsyncResult *result,
956                           gpointer      user_data)
957 {
958   GTask *task = user_data;
959   ConnectAsyncData *data = g_task_get_task_data (task);
960   GError *error = NULL;
961   gssize read;
962
963   read = g_input_stream_read_finish (G_INPUT_STREAM (source),
964                                      result, &error);
965
966   if (read < 0)
967     {
968       g_task_return_error (task, error);
969       g_object_unref (task);
970       return;
971     }
972
973   data->length = data->buffer[0] + 2;
974   data->offset = 0;
975
976   do_read (connect_addr_read_cb, task, data);
977 }
978
979 static void
980 connect_addr_read_cb (GObject      *source,
981                       GAsyncResult *result,
982                       gpointer      user_data)
983 {
984   GTask *task = user_data;
985   ConnectAsyncData *data = g_task_get_task_data (task);
986   GError *error = NULL;
987   gssize read;
988
989   read = g_input_stream_read_finish (G_INPUT_STREAM (source),
990                                      result, &error);
991
992   if (read < 0)
993     {
994       g_task_return_error (task, error);
995       g_object_unref (task);
996       return;
997     }
998
999   data->offset += read;
1000
1001   if (data->offset == data->length)
1002     {
1003       g_task_return_pointer (task, g_object_ref (data->io_stream), g_object_unref);
1004       g_object_unref (task);
1005       return;
1006     }
1007   else
1008     {
1009       do_read (connect_reply_read_cb, task, data);
1010     }
1011 }
1012
1013 static GIOStream *
1014 g_socks5_proxy_connect_finish (GProxy       *proxy,
1015                                GAsyncResult *result,
1016                                GError      **error)
1017 {
1018   return g_task_propagate_pointer (G_TASK (result), error);
1019 }
1020
1021 static gboolean
1022 g_socks5_proxy_supports_hostname (GProxy *proxy)
1023 {
1024   return TRUE;
1025 }
1026
1027 static void
1028 g_socks5_proxy_class_init (GSocks5ProxyClass *class)
1029 {
1030   GObjectClass *object_class;
1031
1032   object_class = (GObjectClass *) class;
1033   object_class->finalize = g_socks5_proxy_finalize;
1034 }
1035
1036 static void
1037 g_socks5_proxy_iface_init (GProxyInterface *proxy_iface)
1038 {
1039   proxy_iface->connect  = g_socks5_proxy_connect;
1040   proxy_iface->connect_async = g_socks5_proxy_connect_async;
1041   proxy_iface->connect_finish = g_socks5_proxy_connect_finish;
1042   proxy_iface->supports_hostname = g_socks5_proxy_supports_hostname;
1043 }