Merge branch 'doc-types' into 'master'
[glib.git] / gio / gsocks5proxy.c
blobc58be83695e4cb6a1f1fb72efaf8902b8c027882
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2008, 2010 Collabora, Ltd.
4 * Copyright (C) 2008 Nokia Corporation. All rights reserved.
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.1 of the License, or (at your option) any later version.
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.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Author: Youness Alaoui <youness.alaoui@collabora.co.uk
21 * Contributors:
22 * Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
25 #include "config.h"
27 #include "gsocks5proxy.h"
29 #include <string.h>
31 #include "giomodule.h"
32 #include "giomodule-priv.h"
33 #include "giostream.h"
34 #include "ginetaddress.h"
35 #include "ginputstream.h"
36 #include "glibintl.h"
37 #include "goutputstream.h"
38 #include "gproxy.h"
39 #include "gproxyaddress.h"
40 #include "gtask.h"
42 #define SOCKS5_VERSION 0x05
44 #define SOCKS5_CMD_CONNECT 0x01
45 #define SOCKS5_CMD_BIND 0x02
46 #define SOCKS5_CMD_UDP_ASSOCIATE 0x03
48 #define SOCKS5_ATYP_IPV4 0x01
49 #define SOCKS5_ATYP_DOMAINNAME 0x03
50 #define SOCKS5_ATYP_IPV6 0x04
52 #define SOCKS5_AUTH_VERSION 0x01
54 #define SOCKS5_AUTH_NONE 0x00
55 #define SOCKS5_AUTH_GSSAPI 0x01
56 #define SOCKS5_AUTH_USR_PASS 0x02
57 #define SOCKS5_AUTH_NO_ACCEPT 0xff
59 #define SOCKS5_MAX_LEN 255
60 #define SOCKS5_RESERVED 0x00
62 #define SOCKS5_REP_SUCCEEDED 0x00
63 #define SOCKS5_REP_SRV_FAILURE 0x01
64 #define SOCKS5_REP_NOT_ALLOWED 0x02
65 #define SOCKS5_REP_NET_UNREACH 0x03
66 #define SOCKS5_REP_HOST_UNREACH 0x04
67 #define SOCKS5_REP_REFUSED 0x05
68 #define SOCKS5_REP_TTL_EXPIRED 0x06
69 #define SOCKS5_REP_CMD_NOT_SUP 0x07
70 #define SOCKS5_REP_ATYPE_NOT_SUP 0x08
73 struct _GSocks5Proxy
75 GObject parent;
78 struct _GSocks5ProxyClass
80 GObjectClass parent_class;
83 static void g_socks5_proxy_iface_init (GProxyInterface *proxy_iface);
85 #define g_socks5_proxy_get_type _g_socks5_proxy_get_type
86 G_DEFINE_TYPE_WITH_CODE (GSocks5Proxy, g_socks5_proxy, G_TYPE_OBJECT,
87 G_IMPLEMENT_INTERFACE (G_TYPE_PROXY,
88 g_socks5_proxy_iface_init)
89 _g_io_modules_ensure_extension_points_registered ();
90 g_io_extension_point_implement (G_PROXY_EXTENSION_POINT_NAME,
91 g_define_type_id,
92 "socks5",
93 0))
95 static void
96 g_socks5_proxy_finalize (GObject *object)
98 /* must chain up */
99 G_OBJECT_CLASS (g_socks5_proxy_parent_class)->finalize (object);
102 static void
103 g_socks5_proxy_init (GSocks5Proxy *proxy)
108 * +----+----------+----------+
109 * |VER | NMETHODS | METHODS |
110 * +----+----------+----------+
111 * | 1 | 1 | 1 to 255 |
112 * +----+----------+----------+
114 #define SOCKS5_NEGO_MSG_LEN 4
115 static gint
116 set_nego_msg (guint8 *msg, gboolean has_auth)
118 gint len = 3;
120 msg[0] = SOCKS5_VERSION;
121 msg[1] = 0x01; /* number of methods supported */
122 msg[2] = SOCKS5_AUTH_NONE;
124 /* add support for authentication method */
125 if (has_auth)
127 msg[1] = 0x02; /* number of methods supported */
128 msg[3] = SOCKS5_AUTH_USR_PASS;
129 len++;
132 return len;
137 * +----+--------+
138 * |VER | METHOD |
139 * +----+--------+
140 * | 1 | 1 |
141 * +----+--------+
143 #define SOCKS5_NEGO_REP_LEN 2
144 static gboolean
145 parse_nego_reply (const guint8 *data,
146 gboolean has_auth,
147 gboolean *must_auth,
148 GError **error)
150 if (data[0] != SOCKS5_VERSION)
152 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
153 _("The server is not a SOCKSv5 proxy server."));
154 return FALSE;
157 switch (data[1])
159 case SOCKS5_AUTH_NONE:
160 *must_auth = FALSE;
161 break;
163 case SOCKS5_AUTH_USR_PASS:
164 if (!has_auth)
166 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_NEED_AUTH,
167 _("The SOCKSv5 proxy requires authentication."));
168 return FALSE;
170 *must_auth = TRUE;
171 break;
173 case SOCKS5_AUTH_GSSAPI:
174 case SOCKS5_AUTH_NO_ACCEPT:
175 default:
176 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_AUTH_FAILED,
177 _("The SOCKSv5 proxy requires an authentication "
178 "method that is not supported by GLib."));
179 return FALSE;
180 break;
183 return TRUE;
186 #define SOCKS5_AUTH_MSG_LEN 515
187 static gint
188 set_auth_msg (guint8 *msg,
189 const gchar *username,
190 const gchar *password,
191 GError **error)
193 gint len = 0;
194 gint ulen = 0; /* username length */
195 gint plen = 0; /* Password length */
197 if (username)
198 ulen = strlen (username);
200 if (password)
201 plen = strlen (password);
203 if (ulen > SOCKS5_MAX_LEN || plen > SOCKS5_MAX_LEN)
205 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
206 _("Username or password is too long for SOCKSv5 "
207 "protocol."));
208 return -1;
211 msg[len++] = SOCKS5_AUTH_VERSION;
212 msg[len++] = ulen;
214 if (ulen > 0)
215 memcpy (msg + len, username, ulen);
217 len += ulen;
218 msg[len++] = plen;
220 if (plen > 0)
221 memcpy (msg + len, password, plen);
223 len += plen;
225 return len;
229 static gboolean
230 check_auth_status (const guint8 *data, GError **error)
232 if (data[0] != SOCKS5_VERSION
233 || data[1] != SOCKS5_REP_SUCCEEDED)
235 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_AUTH_FAILED,
236 _("SOCKSv5 authentication failed due to wrong "
237 "username or password."));
238 return FALSE;
240 return TRUE;
244 * +----+-----+-------+------+----------+----------+
245 * |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
246 * +----+-----+-------+------+----------+----------+
247 * | 1 | 1 | X'00' | 1 | Variable | 2 |
248 * +----+-----+-------+------+----------+----------+
249 * DST.ADDR is a string with first byte being the size. So DST.ADDR may not be
250 * longer then 256 bytes.
252 #define SOCKS5_CONN_MSG_LEN 262
253 static gint
254 set_connect_msg (guint8 *msg,
255 const gchar *hostname,
256 guint16 port,
257 GError **error)
259 guint len = 0;
261 msg[len++] = SOCKS5_VERSION;
262 msg[len++] = SOCKS5_CMD_CONNECT;
263 msg[len++] = SOCKS5_RESERVED;
265 if (g_hostname_is_ip_address (hostname))
267 GInetAddress *addr = g_inet_address_new_from_string (hostname);
268 gsize addr_len = g_inet_address_get_native_size (addr);
270 /* We are cheating for simplicity, here's the logic:
271 * 1 = IPV4 = 4 bytes / 4
272 * 4 = IPV6 = 16 bytes / 4 */
273 msg[len++] = addr_len / 4;
274 memcpy (msg + len, g_inet_address_to_bytes (addr), addr_len);
275 len += addr_len;
277 g_object_unref (addr);
279 else
281 gsize host_len = strlen (hostname);
283 if (host_len > SOCKS5_MAX_LEN)
285 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
286 _("Hostname “%s” is too long for SOCKSv5 protocol"),
287 hostname);
288 return -1;
291 msg[len++] = SOCKS5_ATYP_DOMAINNAME;
292 msg[len++] = (guint8) host_len;
293 memcpy (msg + len, hostname, host_len);
294 len += host_len;
298 guint16 hp = g_htons (port);
299 memcpy (msg + len, &hp, 2);
300 len += 2;
303 return len;
307 * +----+-----+-------+------+----------+----------+
308 * |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
309 * +----+-----+-------+------+----------+----------+
310 * | 1 | 1 | X'00' | 1 | Variable | 2 |
311 * +----+-----+-------+------+----------+----------+
312 * This reply need to be read by small part to determin size. Buffer
313 * size is determined in function of the biggest part to read.
315 * The parser only requires 4 bytes.
317 #define SOCKS5_CONN_REP_LEN 255
318 static gboolean
319 parse_connect_reply (const guint8 *data, gint *atype, GError **error)
321 if (data[0] != SOCKS5_VERSION)
323 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
324 _("The server is not a SOCKSv5 proxy server."));
325 return FALSE;
328 switch (data[1])
330 case SOCKS5_REP_SUCCEEDED:
331 if (data[2] != SOCKS5_RESERVED)
333 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
334 _("The server is not a SOCKSv5 proxy server."));
335 return FALSE;
338 switch (data[3])
340 case SOCKS5_ATYP_IPV4:
341 case SOCKS5_ATYP_IPV6:
342 case SOCKS5_ATYP_DOMAINNAME:
343 *atype = data[3];
344 break;
346 default:
347 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
348 _("The SOCKSv5 proxy server uses unknown address type."));
349 return FALSE;
351 break;
353 case SOCKS5_REP_SRV_FAILURE:
354 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
355 _("Internal SOCKSv5 proxy server error."));
356 return FALSE;
357 break;
359 case SOCKS5_REP_NOT_ALLOWED:
360 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_NOT_ALLOWED,
361 _("SOCKSv5 connection not allowed by ruleset."));
362 return FALSE;
363 break;
365 case SOCKS5_REP_TTL_EXPIRED:
366 case SOCKS5_REP_HOST_UNREACH:
367 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_HOST_UNREACHABLE,
368 _("Host unreachable through SOCKSv5 server."));
369 return FALSE;
370 break;
372 case SOCKS5_REP_NET_UNREACH:
373 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NETWORK_UNREACHABLE,
374 _("Network unreachable through SOCKSv5 proxy."));
375 return FALSE;
376 break;
378 case SOCKS5_REP_REFUSED:
379 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_REFUSED,
380 _("Connection refused through SOCKSv5 proxy."));
381 return FALSE;
382 break;
384 case SOCKS5_REP_CMD_NOT_SUP:
385 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
386 _("SOCKSv5 proxy does not support “connect” command."));
387 return FALSE;
388 break;
390 case SOCKS5_REP_ATYPE_NOT_SUP:
391 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
392 _("SOCKSv5 proxy does not support provided address type."));
393 return FALSE;
394 break;
396 default: /* Unknown error */
397 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
398 _("Unknown SOCKSv5 proxy error."));
399 return FALSE;
400 break;
403 return TRUE;
406 static GIOStream *
407 g_socks5_proxy_connect (GProxy *proxy,
408 GIOStream *io_stream,
409 GProxyAddress *proxy_address,
410 GCancellable *cancellable,
411 GError **error)
413 gboolean has_auth;
414 GInputStream *in;
415 GOutputStream *out;
416 const gchar *hostname;
417 guint16 port;
418 const gchar *username;
419 const gchar *password;
421 hostname = g_proxy_address_get_destination_hostname (proxy_address);
422 port = g_proxy_address_get_destination_port (proxy_address);
423 username = g_proxy_address_get_username (proxy_address);
424 password = g_proxy_address_get_password (proxy_address);
426 has_auth = username || password;
428 in = g_io_stream_get_input_stream (io_stream);
429 out = g_io_stream_get_output_stream (io_stream);
431 /* Send SOCKS5 handshake */
433 guint8 msg[SOCKS5_NEGO_MSG_LEN];
434 gint len;
436 len = set_nego_msg (msg, has_auth);
438 if (!g_output_stream_write_all (out, msg, len, NULL,
439 cancellable, error))
440 goto error;
443 /* Receive SOCKS5 response and reply with authentication if required */
445 guint8 data[SOCKS5_NEGO_REP_LEN];
446 gboolean must_auth = FALSE;
448 if (!g_input_stream_read_all (in, data, sizeof (data), NULL,
449 cancellable, error))
450 goto error;
452 if (!parse_nego_reply (data, has_auth, &must_auth, error))
453 goto error;
455 if (must_auth)
457 guint8 msg[SOCKS5_AUTH_MSG_LEN];
458 gint len;
460 len = set_auth_msg (msg, username, password, error);
462 if (len < 0)
463 goto error;
465 if (!g_output_stream_write_all (out, msg, len, NULL,
466 cancellable, error))
467 goto error;
469 if (!g_input_stream_read_all (in, data, sizeof (data), NULL,
470 cancellable, error))
471 goto error;
473 if (!check_auth_status (data, error))
474 goto error;
478 /* Send SOCKS5 connection request */
480 guint8 msg[SOCKS5_CONN_MSG_LEN];
481 gint len;
483 len = set_connect_msg (msg, hostname, port, error);
485 if (len < 0)
486 goto error;
488 if (!g_output_stream_write_all (out, msg, len, NULL,
489 cancellable, error))
490 goto error;
493 /* Read SOCKS5 response */
495 guint8 data[SOCKS5_CONN_REP_LEN];
496 gint atype;
498 if (!g_input_stream_read_all (in, data, 4, NULL,
499 cancellable, error))
500 goto error;
502 if (!parse_connect_reply (data, &atype, error))
503 goto error;
505 switch (atype)
507 case SOCKS5_ATYP_IPV4:
508 if (!g_input_stream_read_all (in, data, 6, NULL,
509 cancellable, error))
510 goto error;
511 break;
513 case SOCKS5_ATYP_IPV6:
514 if (!g_input_stream_read_all (in, data, 18, NULL,
515 cancellable, error))
516 goto error;
517 break;
519 case SOCKS5_ATYP_DOMAINNAME:
520 if (!g_input_stream_read_all (in, data, 1, NULL,
521 cancellable, error))
522 goto error;
523 if (!g_input_stream_read_all (in, data, data[0] + 2, NULL,
524 cancellable, error))
525 goto error;
526 break;
530 return g_object_ref (io_stream);
532 error:
533 return NULL;
537 typedef struct
539 GIOStream *io_stream;
540 gchar *hostname;
541 guint16 port;
542 gchar *username;
543 gchar *password;
544 guint8 *buffer;
545 gssize length;
546 gssize offset;
547 } ConnectAsyncData;
549 static void nego_msg_write_cb (GObject *source,
550 GAsyncResult *res,
551 gpointer user_data);
552 static void nego_reply_read_cb (GObject *source,
553 GAsyncResult *res,
554 gpointer user_data);
555 static void auth_msg_write_cb (GObject *source,
556 GAsyncResult *res,
557 gpointer user_data);
558 static void auth_reply_read_cb (GObject *source,
559 GAsyncResult *result,
560 gpointer user_data);
561 static void send_connect_msg (GTask *task);
562 static void connect_msg_write_cb (GObject *source,
563 GAsyncResult *result,
564 gpointer user_data);
565 static void connect_reply_read_cb (GObject *source,
566 GAsyncResult *result,
567 gpointer user_data);
568 static void connect_addr_len_read_cb (GObject *source,
569 GAsyncResult *result,
570 gpointer user_data);
571 static void connect_addr_read_cb (GObject *source,
572 GAsyncResult *result,
573 gpointer user_data);
575 static void
576 free_connect_data (ConnectAsyncData *data)
578 g_object_unref (data->io_stream);
580 g_free (data->hostname);
581 g_free (data->username);
582 g_free (data->password);
583 g_free (data->buffer);
585 g_slice_free (ConnectAsyncData, data);
588 static void
589 do_read (GAsyncReadyCallback callback, GTask *task, ConnectAsyncData *data)
591 GInputStream *in;
592 in = g_io_stream_get_input_stream (data->io_stream);
593 g_input_stream_read_async (in,
594 data->buffer + data->offset,
595 data->length - data->offset,
596 g_task_get_priority (task),
597 g_task_get_cancellable (task),
598 callback, task);
601 static void
602 do_write (GAsyncReadyCallback callback, GTask *task, ConnectAsyncData *data)
604 GOutputStream *out;
605 out = g_io_stream_get_output_stream (data->io_stream);
606 g_output_stream_write_async (out,
607 data->buffer + data->offset,
608 data->length - data->offset,
609 g_task_get_priority (task),
610 g_task_get_cancellable (task),
611 callback, task);
614 static void
615 g_socks5_proxy_connect_async (GProxy *proxy,
616 GIOStream *io_stream,
617 GProxyAddress *proxy_address,
618 GCancellable *cancellable,
619 GAsyncReadyCallback callback,
620 gpointer user_data)
622 GTask *task;
623 ConnectAsyncData *data;
625 data = g_slice_new0 (ConnectAsyncData);
626 data->io_stream = g_object_ref (io_stream);
628 task = g_task_new (proxy, cancellable, callback, user_data);
629 g_task_set_source_tag (task, g_socks5_proxy_connect_async);
630 g_task_set_task_data (task, data, (GDestroyNotify) free_connect_data);
632 g_object_get (G_OBJECT (proxy_address),
633 "destination-hostname", &data->hostname,
634 "destination-port", &data->port,
635 "username", &data->username,
636 "password", &data->password,
637 NULL);
639 data->buffer = g_malloc0 (SOCKS5_NEGO_MSG_LEN);
640 data->length = set_nego_msg (data->buffer,
641 data->username || data->password);
642 data->offset = 0;
644 do_write (nego_msg_write_cb, task, data);
648 static void
649 nego_msg_write_cb (GObject *source,
650 GAsyncResult *res,
651 gpointer user_data)
653 GTask *task = user_data;
654 ConnectAsyncData *data = g_task_get_task_data (task);
655 GError *error = NULL;
656 gssize written;
658 written = g_output_stream_write_finish (G_OUTPUT_STREAM (source),
659 res, &error);
661 if (written < 0)
663 g_task_return_error (task, error);
664 g_object_unref (task);
665 return;
668 data->offset += written;
670 if (data->offset == data->length)
672 g_free (data->buffer);
674 data->buffer = g_malloc0 (SOCKS5_NEGO_REP_LEN);
675 data->length = SOCKS5_NEGO_REP_LEN;
676 data->offset = 0;
678 do_read (nego_reply_read_cb, task, data);
680 else
682 do_write (nego_msg_write_cb, task, data);
686 static void
687 nego_reply_read_cb (GObject *source,
688 GAsyncResult *res,
689 gpointer user_data)
691 GTask *task = user_data;
692 ConnectAsyncData *data = g_task_get_task_data (task);
693 GError *error = NULL;
694 gssize read;
696 read = g_input_stream_read_finish (G_INPUT_STREAM (source),
697 res, &error);
699 if (read < 0)
701 g_task_return_error (task, error);
702 g_object_unref (task);
703 return;
706 data->offset += read;
708 if (data->offset == data->length)
710 GError *error = NULL;
711 gboolean must_auth = FALSE;
712 gboolean has_auth = data->username || data->password;
714 if (!parse_nego_reply (data->buffer, has_auth, &must_auth, &error))
716 g_task_return_error (task, error);
717 g_object_unref (task);
718 return;
721 if (must_auth)
723 g_free (data->buffer);
725 data->buffer = g_malloc0 (SOCKS5_AUTH_MSG_LEN);
726 data->length = set_auth_msg (data->buffer,
727 data->username,
728 data->password,
729 &error);
730 data->offset = 0;
732 if (data->length < 0)
734 g_task_return_error (task, error);
735 g_object_unref (task);
736 return;
739 do_write (auth_msg_write_cb, task, data);
741 else
743 send_connect_msg (task);
746 else
748 do_read (nego_reply_read_cb, task, data);
752 static void
753 auth_msg_write_cb (GObject *source,
754 GAsyncResult *result,
755 gpointer user_data)
757 GTask *task = user_data;
758 ConnectAsyncData *data = g_task_get_task_data (task);
759 GError *error = NULL;
760 gssize written;
762 written = g_output_stream_write_finish (G_OUTPUT_STREAM (source),
763 result, &error);
765 if (written < 0)
767 g_task_return_error (task, error);
768 g_object_unref (task);
769 return;
772 data->offset += written;
774 if (data->offset == data->length)
776 g_free (data->buffer);
778 data->buffer = g_malloc0 (SOCKS5_NEGO_REP_LEN);
779 data->length = SOCKS5_NEGO_REP_LEN;
780 data->offset = 0;
782 do_read (auth_reply_read_cb, task, data);
784 else
786 do_write (auth_msg_write_cb, task, data);
790 static void
791 auth_reply_read_cb (GObject *source,
792 GAsyncResult *result,
793 gpointer user_data)
795 GTask *task = user_data;
796 ConnectAsyncData *data = g_task_get_task_data (task);
797 GError *error = NULL;
798 gssize read;
800 read = g_input_stream_read_finish (G_INPUT_STREAM (source),
801 result, &error);
803 if (read < 0)
805 g_task_return_error (task, error);
806 g_object_unref (task);
807 return;
810 data->offset += read;
812 if (data->offset == data->length)
814 if (!check_auth_status (data->buffer, &error))
816 g_task_return_error (task, error);
817 g_object_unref (task);
818 return;
821 send_connect_msg (task);
823 else
825 do_read (auth_reply_read_cb, task, data);
829 static void
830 send_connect_msg (GTask *task)
832 ConnectAsyncData *data = g_task_get_task_data (task);
833 GError *error = NULL;
835 g_free (data->buffer);
837 data->buffer = g_malloc0 (SOCKS5_CONN_MSG_LEN);
838 data->length = set_connect_msg (data->buffer,
839 data->hostname,
840 data->port,
841 &error);
842 data->offset = 0;
844 if (data->length < 0)
846 g_task_return_error (task, error);
847 g_object_unref (task);
848 return;
851 do_write (connect_msg_write_cb, task, data);
854 static void
855 connect_msg_write_cb (GObject *source,
856 GAsyncResult *result,
857 gpointer user_data)
859 GTask *task = user_data;
860 ConnectAsyncData *data = g_task_get_task_data (task);
861 GError *error = NULL;
862 gssize written;
864 written = g_output_stream_write_finish (G_OUTPUT_STREAM (source),
865 result, &error);
867 if (written < 0)
869 g_task_return_error (task, error);
870 g_object_unref (task);
871 return;
874 data->offset += written;
876 if (data->offset == data->length)
878 g_free (data->buffer);
880 data->buffer = g_malloc0 (SOCKS5_CONN_REP_LEN);
881 data->length = 4;
882 data->offset = 0;
884 do_read (connect_reply_read_cb, task, data);
886 else
888 do_write (connect_msg_write_cb, task, data);
892 static void
893 connect_reply_read_cb (GObject *source,
894 GAsyncResult *result,
895 gpointer user_data)
897 GTask *task = user_data;
898 ConnectAsyncData *data = g_task_get_task_data (task);
899 GError *error = NULL;
900 gssize read;
902 read = g_input_stream_read_finish (G_INPUT_STREAM (source),
903 result, &error);
905 if (read < 0)
907 g_task_return_error (task, error);
908 g_object_unref (task);
909 return;
912 data->offset += read;
914 if (data->offset == data->length)
916 gint atype;
918 if (!parse_connect_reply (data->buffer, &atype, &error))
920 g_task_return_error (task, error);
921 g_object_unref (task);
922 return;
925 switch (atype)
927 case SOCKS5_ATYP_IPV4:
928 data->length = 6;
929 data->offset = 0;
930 do_read (connect_addr_read_cb, task, data);
931 break;
933 case SOCKS5_ATYP_IPV6:
934 data->length = 18;
935 data->offset = 0;
936 do_read (connect_addr_read_cb, task, data);
937 break;
939 case SOCKS5_ATYP_DOMAINNAME:
940 data->length = 1;
941 data->offset = 0;
942 do_read (connect_addr_len_read_cb, task, data);
943 break;
946 else
948 do_read (connect_reply_read_cb, task, data);
952 static void
953 connect_addr_len_read_cb (GObject *source,
954 GAsyncResult *result,
955 gpointer user_data)
957 GTask *task = user_data;
958 ConnectAsyncData *data = g_task_get_task_data (task);
959 GError *error = NULL;
960 gssize read;
962 read = g_input_stream_read_finish (G_INPUT_STREAM (source),
963 result, &error);
965 if (read < 0)
967 g_task_return_error (task, error);
968 g_object_unref (task);
969 return;
972 data->length = data->buffer[0] + 2;
973 data->offset = 0;
975 do_read (connect_addr_read_cb, task, data);
978 static void
979 connect_addr_read_cb (GObject *source,
980 GAsyncResult *result,
981 gpointer user_data)
983 GTask *task = user_data;
984 ConnectAsyncData *data = g_task_get_task_data (task);
985 GError *error = NULL;
986 gssize read;
988 read = g_input_stream_read_finish (G_INPUT_STREAM (source),
989 result, &error);
991 if (read < 0)
993 g_task_return_error (task, error);
994 g_object_unref (task);
995 return;
998 data->offset += read;
1000 if (data->offset == data->length)
1002 g_task_return_pointer (task, g_object_ref (data->io_stream), g_object_unref);
1003 g_object_unref (task);
1004 return;
1006 else
1008 do_read (connect_reply_read_cb, task, data);
1012 static GIOStream *
1013 g_socks5_proxy_connect_finish (GProxy *proxy,
1014 GAsyncResult *result,
1015 GError **error)
1017 return g_task_propagate_pointer (G_TASK (result), error);
1020 static gboolean
1021 g_socks5_proxy_supports_hostname (GProxy *proxy)
1023 return TRUE;
1026 static void
1027 g_socks5_proxy_class_init (GSocks5ProxyClass *class)
1029 GObjectClass *object_class;
1031 object_class = (GObjectClass *) class;
1032 object_class->finalize = g_socks5_proxy_finalize;
1035 static void
1036 g_socks5_proxy_iface_init (GProxyInterface *proxy_iface)
1038 proxy_iface->connect = g_socks5_proxy_connect;
1039 proxy_iface->connect_async = g_socks5_proxy_connect_async;
1040 proxy_iface->connect_finish = g_socks5_proxy_connect_finish;
1041 proxy_iface->supports_hostname = g_socks5_proxy_supports_hostname;