static void print_proxy_address (GSocketAddress *sockaddr) { GProxyAddress *proxy = NULL; if (sockaddr == NULL) { printf ("\tdirect://\n"); return; } if (G_IS_PROXY_ADDRESS (sockaddr)) { proxy = G_PROXY_ADDRESS (sockaddr); printf ("\t%s://", g_proxy_address_get_protocol(proxy)); } else { printf ("\tdirect://"); } if (G_IS_INET_SOCKET_ADDRESS (sockaddr)) { GInetAddress *inetaddr; guint port; gchar *addr; g_object_get (sockaddr, "address", &inetaddr, "port", &port, NULL); addr = g_inet_address_to_string (inetaddr); printf ("%s:%u", addr, port); g_free (addr); } if (proxy) { if (g_proxy_address_get_username(proxy)) printf (" (Username: %s Password: %s)", g_proxy_address_get_username(proxy), g_proxy_address_get_password(proxy)); printf (" (Hostname: %s, Port: %i)", g_proxy_address_get_destination_hostname (proxy), g_proxy_address_get_destination_port (proxy)); } printf ("\n"); }
static GIOStream * g_socks4a_proxy_connect (GProxy *proxy, GIOStream *io_stream, GProxyAddress *proxy_address, GCancellable *cancellable, GError **error) { GInputStream *in; GOutputStream *out; const gchar *hostname; guint16 port; const gchar *username; hostname = g_proxy_address_get_destination_hostname (proxy_address); port = g_proxy_address_get_destination_port (proxy_address); username = g_proxy_address_get_username (proxy_address); in = g_io_stream_get_input_stream (io_stream); out = g_io_stream_get_output_stream (io_stream); /* Send SOCKS4 connection request */ { guint8 msg[SOCKS4_CONN_MSG_LEN]; gint len; len = set_connect_msg (msg, hostname, port, username, error); if (len < 0) goto error; if (!g_output_stream_write_all (out, msg, len, NULL, cancellable, error)) goto error; } /* Read SOCKS4 response */ { guint8 data[SOCKS4_CONN_REP_LEN]; if (!g_input_stream_read_all (in, data, SOCKS4_CONN_REP_LEN, NULL, cancellable, error)) goto error; if (!parse_connect_reply (data, error)) goto error; } return g_object_ref (io_stream); error: return NULL; }
static void g_socks4a_proxy_connect_async (GProxy *proxy, GIOStream *io_stream, GProxyAddress *proxy_address, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) { GError *error = NULL; GTask *task; ConnectAsyncData *data; const gchar *hostname; guint16 port; const gchar *username; data = g_slice_new0 (ConnectAsyncData); data->io_stream = g_object_ref (io_stream); task = g_task_new (proxy, cancellable, callback, user_data); g_task_set_task_data (task, data, (GDestroyNotify) free_connect_data); hostname = g_proxy_address_get_destination_hostname (proxy_address); port = g_proxy_address_get_destination_port (proxy_address); username = g_proxy_address_get_username (proxy_address); data->buffer = g_malloc0 (SOCKS4_CONN_MSG_LEN); data->length = set_connect_msg (data->buffer, hostname, port, username, &error); data->offset = 0; if (data->length < 0) { g_task_return_error (task, error); g_object_unref (task); } else { do_write (connect_msg_write_cb, task, data); } }