Remmina - The GTK+ Remote Desktop Client  v1.4.33
Remmina is a remote desktop client written in GTK+, aiming to be useful for system administrators and travellers, who need to work with lots of remote computers in front of either large monitors or tiny netbooks. Remmina supports multiple network protocols in an integrated and consistent user interface. Currently RDP, VNC, NX, XDMCP and SSH are supported.
remmina_monitor.c
Go to the documentation of this file.
1 /*
2  * Remmina - The GTK+ Remote Desktop Client
3  * Copyright (C) 2016-2023 Antenore Gatta
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  * In addition, as a special exception, the copyright holders give
21  * permission to link the code of portions of this program with the
22  * OpenSSL library under certain conditions as described in each
23  * individual source file, and distribute linked combinations
24  * including the two.
25  * You must obey the GNU General Public License in all respects
26  * for all of the code used other than OpenSSL. * If you modify
27  * file(s) with this exception, you may extend this exception to your
28  * version of the file(s), but you are not obligated to do so. * If you
29  * do not wish to do so, delete this exception statement from your
30  * version. * If you delete this exception statement from all source
31  * files in the program, then also delete it here.
32  *
33  */
34 
35 #include "config.h"
36 #include "remmina_monitor.h"
37 #include "remmina_log.h"
38 #include "remmina_public.h"
40 
42 
43 static void remmina_monitor_can_reach_cb (GNetworkMonitor *netmonitor, GAsyncResult *result, RemminaMonitor *monitor)
44 {
45  g_autoptr (GError) error = NULL;
46 
47  gchar *status = NULL;
48 
49  gboolean is_reachable = g_network_monitor_can_reach_finish (netmonitor, result, &error);
50 
51  const gchar *addr_tostr = g_strdup(g_socket_connectable_to_string (monitor->addr));
52  //gchar *value = (gchar *)g_hash_table_lookup (monitor->server_status, addr_tostr);
53 
54  if (is_reachable) {
55 
56  REMMINA_DEBUG ("Network object %s is reachable", g_socket_connectable_to_string (monitor->addr));
57  status = g_strdup ("online");
58 
59  } else {
60 
61  REMMINA_DEBUG ("Network object %s is not reachable", g_socket_connectable_to_string (monitor->addr));
62  status = g_strdup ("offline");
63  }
64 
65  if (g_hash_table_replace (monitor->server_status, g_strdup(addr_tostr), g_strdup(status))) {
66  REMMINA_DEBUG ("Inserting %s -> %s", addr_tostr, status);
67  } else {
68  REMMINA_DEBUG ("Replacing %s -> %s", addr_tostr, status);
69  }
70 
71  /* Cannot use remminafile here because is freed by remmina_file_manager_iterate */
72  //if (remminafile)
73  //remmina_file_set_state_int (remminafile, "reachable", reachable);
74  g_free (status);
75 }
76 
78 {
79  TRACE_CALL(__func__);
80 
81  const gchar *server;
82  const gchar *ssh_tunnel_server;
83  const gchar *addr_tostr = NULL;
84  gchar *status = NULL;
85  gchar *ssh_tunnel_host, *srv_host;
86  gint netmonit, srv_port, ssh_tunnel_port;
87  const gchar *protocol;
88  gint default_port = 0;
89 
90 
91  if (!remminafile) {
92  status = g_strdup ("I/O Error");
93  REMMINA_DEBUG (status);
94  return NULL;
95  }
96 
97  netmonit = remmina_file_get_int(remminafile, "enable-netmonit", FALSE);
98 
99  if (!netmonit) {
100  status = g_strdup ("Monitoring disabled");
101  REMMINA_DEBUG (status);
102  return NULL;
103  }
104 
105  protocol = remmina_file_get_string (remminafile, "protocol");
106 
107  if (protocol && protocol[0] != '\0') {
108  REMMINA_DEBUG ("Evaluating protocol %s for monitoring", protocol);
109  if (g_strcmp0("RDP", protocol) == 0)
110  default_port = 3389;
111  if (g_strcmp0("VNC", protocol) == 0)
112  default_port = 5900;
113  if (g_strcmp0("GVNC", protocol) == 0)
114  default_port = 5900;
115  if (g_strcmp0("SPICE", protocol) == 0)
116  default_port = 5900;
117  if (g_strcmp0("WWW", protocol) == 0)
118  default_port = 443;
119  if (g_strcmp0("X2GO", protocol) == 0)
120  default_port = 22;
121  if (g_strcmp0("SSH", protocol) == 0)
122  default_port = 22;
123  if (g_strcmp0("SFTP", protocol) == 0)
124  default_port = 22;
125  if (g_strcmp0("EXEC", protocol) == 0)
126  default_port = -1;
127 
128  if (default_port == 0) {
129  status = g_strdup ("Unknown protocol");
130  REMMINA_DEBUG (status);
131  return NULL;
132  }
133  if (default_port < 0) {
134  status = g_strdup ("Cannot monitor");
135  REMMINA_DEBUG (status);
136  return NULL;
137  }
138 
139  ssh_tunnel_server = remmina_file_get_string(remminafile, "ssh_tunnel_server");
140  if (remmina_file_get_int(remminafile, "ssh_tunnel_enabled", FALSE)) {
141  remmina_public_get_server_port(ssh_tunnel_server, 22, &ssh_tunnel_host, &ssh_tunnel_port);
142  monitor->addr = g_network_address_new (ssh_tunnel_host, ssh_tunnel_port);
143  g_free(ssh_tunnel_host), ssh_tunnel_host = NULL;
144  } else {
145  server = remmina_file_get_string(remminafile, "server");
146  remmina_public_get_server_port(server, default_port, &srv_host, &srv_port);
147  monitor->addr = g_network_address_new (srv_host, srv_port);
148  g_free(srv_host), srv_host = NULL;
149  }
150  addr_tostr = g_strdup(g_socket_connectable_to_string (monitor->addr));
151 
152  REMMINA_DEBUG ("addr is %s", addr_tostr);
153  if (monitor->connected && netmonit) {
154  REMMINA_DEBUG ("Testing for %s", addr_tostr);
155  g_network_monitor_can_reach_async (
156  monitor->netmonitor,
157  monitor->addr,
158  NULL,
159  (GAsyncReadyCallback) remmina_monitor_can_reach_cb,
160  monitor);
161  }
162 
163 
164  status = (gchar *)g_hash_table_lookup (monitor->server_status, addr_tostr);
165  //if (!status)
166  //g_hash_table_insert (monitor->server_status, g_strdup(addr_tostr), "offline");
167 
168  }
169 
170  if (!status) {
171  return g_strdup(addr_tostr);
172  } else
173  return status;
174 
175  //g_free(ssh_tunnel_host), ssh_tunnel_host = NULL;
176  //g_free(srv_host), srv_host = NULL;
177  //g_free(dest), dest = NULL;
178 
179 }
180 
182 {
183  TRACE_CALL(__func__);
184 
185  gboolean status = g_network_monitor_get_connectivity (rm_monitor->netmonitor);
186 
187  rm_monitor->server_status = g_hash_table_new_full(
188  g_str_hash,
189  g_str_equal,
190  (GDestroyNotify)g_free,
191  (GDestroyNotify)g_free);
192 
193  switch (status)
194  {
195  case G_NETWORK_CONNECTIVITY_LOCAL:
196  REMMINA_DEBUG ("G_NETWORK_CONNECTIVITY_LOCAL");
197  rm_monitor->connected = FALSE;
198  break;
199 
200  case G_NETWORK_CONNECTIVITY_LIMITED:
201  REMMINA_DEBUG ("G_NETWORK_CONNECTIVITY_LIMITED");
202  rm_monitor->connected = FALSE;
203  break;
204 
205  case G_NETWORK_CONNECTIVITY_PORTAL:
206  REMMINA_DEBUG ("G_NETWORK_CONNECTIVITY_PORTAL");
207  rm_monitor->connected = FALSE;
208  break;
209 
210  case G_NETWORK_CONNECTIVITY_FULL:
211  REMMINA_DEBUG ("G_NETWORK_CONNECTIVITY_FULL");
212  rm_monitor->connected = TRUE;
213  break;
214  }
215 
216  return status;
217 }
218 
219 
221 {
222  TRACE_CALL(__func__);
223 
224  rm_monitor = g_new0(RemminaMonitor, 1);
225 
226  rm_monitor->netmonitor = g_network_monitor_get_default ();
227 
228  return rm_monitor;
229 }
const gchar * remmina_file_get_string(RemminaFile *remminafile, const gchar *setting)
Definition: remmina_file.c:516
typedefG_BEGIN_DECLS struct _RemminaFile RemminaFile
Definition: types.h:44
GSocketConnectable * addr
gboolean remmina_network_monitor_status(RemminaMonitor *rm_monitor)
RemminaMonitor * remmina_network_monitor_new()
gchar * remmina_monitor_can_reach(RemminaFile *remminafile, RemminaMonitor *monitor)
static void remmina_monitor_can_reach_cb(GNetworkMonitor *netmonitor, GAsyncResult *result, RemminaMonitor *monitor)
gint remmina_file_get_int(RemminaFile *remminafile, const gchar *setting, gint default_value)
Definition: remmina_file.c:603
RemminaMonitor * rm_monitor
GHashTable * server_status
GNetworkMonitor * netmonitor
void remmina_public_get_server_port(const gchar *server, gint defaultport, gchar **host, gint *port)