Commit fcb526e1 authored by Mihai Moldovan's avatar Mihai Moldovan
Browse files

src/x2goclient-network-ssh.c: split out appending host (and, if applicable,...

src/x2goclient-network-ssh.c: split out appending host (and, if applicable, port) to an GPtrArray to a new function called x2goclient_network_ssh_sshcmd_add_host_port ().

Just a code move, really, but we'll need that later on.
parent c57d50c7
......@@ -136,6 +136,7 @@ static gboolean x2goclient_network_ssh_fetch_openssh_version (X2GoClientNetworkS
static void x2goclient_network_ssh_log_std_str (const gchar * const str, const gsize str_size, const _Bool select_stderr);
static gboolean x2goclient_network_ssh_gptrarray_to_string (GPtrArray * const arr, const gchar * const prelude, gchar ** const ret_str);
static void x2goclient_network_ssh_gptrarray_print_debug (GPtrArray * const arr, const gchar * const prelude, const gchar * const desc);
static gboolean x2goclient_network_ssh_sshcmd_add_host_port (X2GoClientNetworkSSH * const self, GPtrArray * const ssh_cmd, GError ** const gerr);
static void x2goclient_network_ssh_class_init (X2GoClientNetworkSSHClass * const klass) {
......@@ -783,90 +784,8 @@ static gboolean x2goclient_network_ssh_parent_connect (X2GoClientNetwork * const
GPtrArray *ssh_cmd = g_ptr_array_new_with_free_func (&x2goclient_clear_strings);
g_ptr_array_add (ssh_cmd, g_strdup ("ssh"));
GSocketAddress *sock_addr = NULL;
if (ret) {
g_object_get (G_OBJECT (self), "socket", &sock_addr, NULL);
if (!(sock_addr)) {
g_set_error_literal (gerr, X2GOCLIENT_NETWORK_SSH_ERROR, X2GOCLIENT_NETWORK_SSH_ERROR_CONNECT_SOCK_ADDR_NULL, "No end point to connect to.");
ret = FALSE;
}
}
if (ret) {
if (G_IS_UNIX_SOCKET_ADDRESS (sock_addr)) {
GUnixSocketAddress *unix_sock_addr = G_UNIX_SOCKET_ADDRESS (sock_addr);
/*
* Tiny problem here: abstract sockets won't work.
* Since program arguments can't contain NULL bytes due to being C
* strings and every abstract socket already has to start with a NULL
* byte, we'll be just adding an empty argument here.
*
* Maybe there will be a way to do that correctly, some day.
*/
g_ptr_array_add (ssh_cmd, g_strdup (g_unix_socket_address_get_path (unix_sock_addr)));
}
else if (G_IS_INET_SOCKET_ADDRESS (sock_addr)) {
GInetSocketAddress *inet_sock_addr = G_INET_SOCKET_ADDRESS (sock_addr);
GInetAddress *inet_addr = g_inet_socket_address_get_address (inet_sock_addr);
guint16 port = g_inet_socket_address_get_port (inet_sock_addr);
if (!(inet_addr)) {
g_set_error_literal (gerr, X2GOCLIENT_NETWORK_SSH_ERROR, X2GOCLIENT_NETWORK_SSH_ERROR_CONNECT_INET_ADDR_NULL, "Internet socket address is undefined, cannot connect to an unknown remote IP host.");
ret = FALSE;
}
else {
g_ptr_array_add (ssh_cmd, g_inet_address_to_string (inet_addr));
/*
* Handle port parameter, unless it's 0, which means to use the default
* value.
*/
if (port) {
g_ptr_array_add (ssh_cmd, g_strdup ("-p"));
g_ptr_array_add (ssh_cmd, g_strdup_printf ("%d", port));
}
}
}
else if (G_IS_NATIVE_SOCKET_ADDRESS (sock_addr)) {
GNativeSocketAddress *native_sock_addr = G_NATIVE_SOCKET_ADDRESS (sock_addr);
g_assert (sizeof (sockaddr_ho) == g_socket_address_get_native_size (sock_addr));
sockaddr_ho tmp_sockaddr = { 0 };
/* Fetch data. */
GError *tmp_err = NULL;
g_socket_address_to_native (sock_addr, &tmp_sockaddr, sizeof (sockaddr_ho), &tmp_err);
if (tmp_err) {
g_set_error (gerr, X2GOCLIENT_NETWORK_SSH_ERROR, X2GOCLIENT_NETWORK_SSH_ERROR_CONNECT_NATIVE_FETCH, "Unable to convert native socket address object to sockaddr struct: %s", tmp_err->message);
g_clear_error (&tmp_err);
ret = FALSE;
}
if (ret) {
g_ptr_array_add (ssh_cmd, g_strdup (tmp_sockaddr.sho_addr));
/*
* Handle port parameter, unless it's 0, which means to use the default
* value.
*/
if (tmp_sockaddr.sho_port) {
g_ptr_array_add (ssh_cmd, g_strdup ("-p"));
g_ptr_array_add (ssh_cmd, g_strdup_printf ("%d", tmp_sockaddr.sho_port));
}
}
}
else {
g_set_error_literal (gerr, X2GOCLIENT_NETWORK_SSH_ERROR, X2GOCLIENT_NETWORK_SSH_ERROR_CONNECT_SOCK_ADDR_UNKNOWN, "Unknown data in socket property, can't connect.");
ret = FALSE;
}
g_clear_object (&sock_addr);
sock_addr = NULL;
ret = x2goclient_network_ssh_sshcmd_add_host_port (self, ssh_cmd, gerr);
}
if (ret) {
......@@ -1268,3 +1187,99 @@ static void x2goclient_network_ssh_gptrarray_print_debug (GPtrArray * const arr,
g_log (NULL, G_LOG_LEVEL_DEBUG, "Unable to print out %s, internal error (the string likely was too big).", real_desc);
}
}
static gboolean x2goclient_network_ssh_sshcmd_add_host_port (X2GoClientNetworkSSH * const self, GPtrArray * const ssh_cmd, GError ** const gerr) {
gboolean ret = FALSE;
g_return_val_if_fail (X2GOCLIENT_IS_NETWORK_SSH (self), ret);
g_return_val_if_fail (ssh_cmd, ret);
g_return_val_if_fail (((NULL == gerr) || (NULL == *gerr)), ret);
ret = TRUE;
GSocketAddress *sock_addr = NULL;
g_object_get (G_OBJECT (self), "socket", &sock_addr, NULL);
if (!(sock_addr)) {
g_set_error_literal (gerr, X2GOCLIENT_NETWORK_SSH_ERROR, X2GOCLIENT_NETWORK_SSH_ERROR_CONNECT_SOCK_ADDR_NULL, "No end point to connect to.");
ret = FALSE;
}
if (ret) {
if (G_IS_UNIX_SOCKET_ADDRESS (sock_addr)) {
GUnixSocketAddress *unix_sock_addr = G_UNIX_SOCKET_ADDRESS (sock_addr);
/*
* Tiny problem here: abstract sockets won't work.
* Since program arguments can't contain NULL bytes due to being C
* strings and every abstract socket already has to start with a NULL
* byte, we'll be just adding an empty argument here.
*
* Maybe there will be a way to do that correctly, some day.
*/
g_ptr_array_add (ssh_cmd, g_strdup (g_unix_socket_address_get_path (unix_sock_addr)));
}
else if (G_IS_INET_SOCKET_ADDRESS (sock_addr)) {
GInetSocketAddress *inet_sock_addr = G_INET_SOCKET_ADDRESS (sock_addr);
GInetAddress *inet_addr = g_inet_socket_address_get_address (inet_sock_addr);
guint16 port = g_inet_socket_address_get_port (inet_sock_addr);
if (!(inet_addr)) {
g_set_error_literal (gerr, X2GOCLIENT_NETWORK_SSH_ERROR, X2GOCLIENT_NETWORK_SSH_ERROR_CONNECT_INET_ADDR_NULL, "Internet socket address is undefined, cannot connect to an unknown remote IP host.");
ret = FALSE;
}
else {
g_ptr_array_add (ssh_cmd, g_inet_address_to_string (inet_addr));
/*
* Handle port parameter, unless it's 0, which means to use the default
* value.
*/
if (port) {
g_ptr_array_add (ssh_cmd, g_strdup ("-p"));
g_ptr_array_add (ssh_cmd, g_strdup_printf ("%d", port));
}
}
}
else if (G_IS_NATIVE_SOCKET_ADDRESS (sock_addr)) {
GNativeSocketAddress *native_sock_addr = G_NATIVE_SOCKET_ADDRESS (sock_addr);
g_assert (sizeof (sockaddr_ho) == g_socket_address_get_native_size (sock_addr));
sockaddr_ho tmp_sockaddr = { 0 };
/* Fetch data. */
GError *tmp_err = NULL;
g_socket_address_to_native (sock_addr, &tmp_sockaddr, sizeof (sockaddr_ho), &tmp_err);
if (tmp_err) {
g_set_error (gerr, X2GOCLIENT_NETWORK_SSH_ERROR, X2GOCLIENT_NETWORK_SSH_ERROR_CONNECT_NATIVE_FETCH, "Unable to convert native socket address object to sockaddr struct: %s", tmp_err->message);
g_clear_error (&tmp_err);
ret = FALSE;
}
if (ret) {
g_ptr_array_add (ssh_cmd, g_strdup (tmp_sockaddr.sho_addr));
/*
* Handle port parameter, unless it's 0, which means to use the default
* value.
*/
if (tmp_sockaddr.sho_port) {
g_ptr_array_add (ssh_cmd, g_strdup ("-p"));
g_ptr_array_add (ssh_cmd, g_strdup_printf ("%d", tmp_sockaddr.sho_port));
}
}
}
else {
g_set_error_literal (gerr, X2GOCLIENT_NETWORK_SSH_ERROR, X2GOCLIENT_NETWORK_SSH_ERROR_CONNECT_SOCK_ADDR_UNKNOWN, "Unknown data in socket property, can't connect.");
ret = FALSE;
}
g_clear_object (&sock_addr);
sock_addr = NULL;
}
return (ret);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment