1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2010 Collabora, Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
25 #include "gsocks4aproxy.h"
29 #include "giomodule.h"
30 #include "giomodule-priv.h"
31 #include "giostream.h"
32 #include "ginetaddress.h"
33 #include "ginputstream.h"
35 #include "goutputstream.h"
37 #include "gproxyaddress.h"
40 #define SOCKS4_VERSION 4
42 #define SOCKS4_CMD_CONNECT 1
43 #define SOCKS4_CMD_BIND 2
45 #define SOCKS4_MAX_LEN 255
47 #define SOCKS4_REP_VERSION 0
48 #define SOCKS4_REP_GRANTED 90
49 #define SOCKS4_REP_REJECTED 91
50 #define SOCKS4_REP_NO_IDENT 92
51 #define SOCKS4_REP_BAD_IDENT 93
53 static void g_socks4a_proxy_iface_init (GProxyInterface *proxy_iface);
55 #define g_socks4a_proxy_get_type _g_socks4a_proxy_get_type
56 G_DEFINE_TYPE_WITH_CODE (GSocks4aProxy, g_socks4a_proxy, G_TYPE_OBJECT,
57 G_IMPLEMENT_INTERFACE (G_TYPE_PROXY,
58 g_socks4a_proxy_iface_init)
59 _g_io_modules_ensure_extension_points_registered ();
60 g_io_extension_point_implement (G_PROXY_EXTENSION_POINT_NAME,
66 g_socks4a_proxy_finalize (GObject *object)
69 G_OBJECT_CLASS (g_socks4a_proxy_parent_class)->finalize (object);
73 g_socks4a_proxy_init (GSocks4aProxy *proxy)
75 proxy->supports_hostname = TRUE;
79 * +----+----+----+----+----+----+----+----+----+----+....+----+------+....+------+
80 * | VN | CD | DSTPORT | DSTIP | USERID |NULL| HOST | | NULL |
81 * +----+----+----+----+----+----+----+----+----+----+....+----+------+....+------+
82 * 1 1 2 4 variable 1 variable
84 #define SOCKS4_CONN_MSG_LEN (9 + SOCKS4_MAX_LEN * 2)
86 set_connect_msg (guint8 *msg,
87 const gchar *hostname,
98 msg[len++] = SOCKS4_VERSION;
99 msg[len++] = SOCKS4_CMD_CONNECT;
102 guint16 hp = g_htons (port);
103 memcpy (msg + len, &hp, 2);
107 is_ip = g_hostname_is_ip_address (hostname);
114 addr = g_inet_address_new_from_string (ip);
115 addr_len = g_inet_address_get_native_size (addr);
119 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
120 _("SOCKSv4 does not support IPv6 address '%s'"),
122 g_object_unref (addr);
126 memcpy (msg + len, g_inet_address_to_bytes (addr), addr_len);
129 g_object_unref (addr);
133 gsize user_len = strlen (username);
135 if (user_len > SOCKS4_MAX_LEN)
137 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
138 _("Username is too long for SOCKSv4 protocol"));
142 memcpy (msg + len, username, user_len);
150 gsize host_len = strlen (hostname);
152 if (host_len > SOCKS4_MAX_LEN)
154 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
155 _("Hostname '%s' is too long for SOCKSv4 protocol"),
160 memcpy (msg + len, hostname, host_len);
169 * +----+----+----+----+----+----+----+----+
170 * | VN | CD | DSTPORT | DSTIP |
171 * +----+----+----+----+----+----+----+----+
174 #define SOCKS4_CONN_REP_LEN 8
176 parse_connect_reply (const guint8 *data, GError **error)
178 if (data[0] != SOCKS4_REP_VERSION)
180 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
181 _("The server is not a SOCKSv4 proxy server."));
185 if (data[1] != SOCKS4_REP_GRANTED)
187 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
188 _("Connection through SOCKSv4 server was rejected"));
196 g_socks4a_proxy_connect (GProxy *proxy,
197 GIOStream *io_stream,
198 GProxyAddress *proxy_address,
199 GCancellable *cancellable,
204 const gchar *hostname;
206 const gchar *username;
208 hostname = g_proxy_address_get_destination_hostname (proxy_address);
209 port = g_proxy_address_get_destination_port (proxy_address);
210 username = g_proxy_address_get_username (proxy_address);
212 in = g_io_stream_get_input_stream (io_stream);
213 out = g_io_stream_get_output_stream (io_stream);
215 /* Send SOCKS4 connection request */
217 guint8 msg[SOCKS4_CONN_MSG_LEN];
220 len = set_connect_msg (msg, hostname, port, username, error);
225 if (!g_output_stream_write_all (out, msg, len, NULL,
230 /* Read SOCKS4 response */
232 guint8 data[SOCKS4_CONN_REP_LEN];
234 if (!g_input_stream_read_all (in, data, SOCKS4_CONN_REP_LEN, NULL,
238 if (!parse_connect_reply (data, error))
242 return g_object_ref (io_stream);
251 GIOStream *io_stream;
260 static void connect_msg_write_cb (GObject *source,
261 GAsyncResult *result,
263 static void connect_reply_read_cb (GObject *source,
264 GAsyncResult *result,
268 free_connect_data (ConnectAsyncData *data)
270 g_object_unref (data->io_stream);
271 g_slice_free (ConnectAsyncData, data);
275 do_read (GAsyncReadyCallback callback, GTask *task, ConnectAsyncData *data)
278 in = g_io_stream_get_input_stream (data->io_stream);
279 g_input_stream_read_async (in,
280 data->buffer + data->offset,
281 data->length - data->offset,
282 g_task_get_priority (task),
283 g_task_get_cancellable (task),
288 do_write (GAsyncReadyCallback callback, GTask *task, ConnectAsyncData *data)
291 out = g_io_stream_get_output_stream (data->io_stream);
292 g_output_stream_write_async (out,
293 data->buffer + data->offset,
294 data->length - data->offset,
295 g_task_get_priority (task),
296 g_task_get_cancellable (task),
303 g_socks4a_proxy_connect_async (GProxy *proxy,
304 GIOStream *io_stream,
305 GProxyAddress *proxy_address,
306 GCancellable *cancellable,
307 GAsyncReadyCallback callback,
310 GError *error = NULL;
312 ConnectAsyncData *data;
313 const gchar *hostname;
315 const gchar *username;
317 data = g_slice_new0 (ConnectAsyncData);
318 data->io_stream = g_object_ref (io_stream);
320 task = g_task_new (proxy, cancellable, callback, user_data);
321 g_task_set_task_data (task, data, (GDestroyNotify) free_connect_data);
323 hostname = g_proxy_address_get_destination_hostname (proxy_address);
324 port = g_proxy_address_get_destination_port (proxy_address);
325 username = g_proxy_address_get_username (proxy_address);
327 data->buffer = g_malloc0 (SOCKS4_CONN_MSG_LEN);
328 data->length = set_connect_msg (data->buffer,
329 hostname, port, username,
333 if (data->length < 0)
335 g_task_return_error (task, error);
336 g_object_unref (task);
340 do_write (connect_msg_write_cb, task, data);
345 connect_msg_write_cb (GObject *source,
346 GAsyncResult *result,
349 GTask *task = user_data;
350 ConnectAsyncData *data = g_task_get_task_data (task);
351 GError *error = NULL;
354 written = g_output_stream_write_finish (G_OUTPUT_STREAM (source),
359 g_task_return_error (task, error);
360 g_object_unref (task);
364 data->offset += written;
366 if (data->offset == data->length)
368 g_free (data->buffer);
370 data->buffer = g_malloc0 (SOCKS4_CONN_REP_LEN);
371 data->length = SOCKS4_CONN_REP_LEN;
374 do_read (connect_reply_read_cb, task, data);
378 do_write (connect_msg_write_cb, task, data);
383 connect_reply_read_cb (GObject *source,
384 GAsyncResult *result,
387 GTask *task = user_data;
388 ConnectAsyncData *data = g_task_get_task_data (task);
389 GError *error = NULL;
392 read = g_input_stream_read_finish (G_INPUT_STREAM (source),
397 g_task_return_error (task, error);
398 g_object_unref (task);
402 data->offset += read;
404 if (data->offset == data->length)
406 if (!parse_connect_reply (data->buffer, &error))
408 g_task_return_error (task, error);
409 g_object_unref (task);
414 g_task_return_pointer (task, g_object_ref (data->io_stream), g_object_unref);
415 g_object_unref (task);
421 do_read (connect_reply_read_cb, task, data);
425 static GIOStream *g_socks4a_proxy_connect_finish (GProxy *proxy,
426 GAsyncResult *result,
430 g_socks4a_proxy_connect_finish (GProxy *proxy,
431 GAsyncResult *result,
434 return g_task_propagate_pointer (G_TASK (result), error);
438 g_socks4a_proxy_supports_hostname (GProxy *proxy)
440 return G_SOCKS4A_PROXY (proxy)->supports_hostname;
444 g_socks4a_proxy_class_init (GSocks4aProxyClass *class)
446 GObjectClass *object_class;
448 object_class = (GObjectClass *) class;
449 object_class->finalize = g_socks4a_proxy_finalize;
453 g_socks4a_proxy_iface_init (GProxyInterface *proxy_iface)
455 proxy_iface->connect = g_socks4a_proxy_connect;
456 proxy_iface->connect_async = g_socks4a_proxy_connect_async;
457 proxy_iface->connect_finish = g_socks4a_proxy_connect_finish;
458 proxy_iface->supports_hostname = g_socks4a_proxy_supports_hostname;