Back to index

lightdm  1.3.2
vnc-client.c
Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <errno.h>
00005 #include <glib.h>
00006 #include <gio/gio.h>
00007 
00008 #include "status.h"
00009 
00010 static GKeyFile *config;
00011 
00012 static void
00013 request_cb (const gchar *message)
00014 {
00015 }
00016 
00017 int
00018 main (int argc, char **argv)
00019 {
00020     gchar *server_address;
00021     gchar *hostname, *c;
00022     gint port;
00023     GError *error = NULL;
00024     GSocket *socket;
00025     GSocketConnectable *address;
00026     GSocketAddress *socket_address;
00027     gboolean result;
00028     gchar buffer[1024];
00029     gssize n_read, n_sent;
00030 
00031     g_type_init ();
00032 
00033     status_connect (request_cb);
00034 
00035     status_notify ("VNC-CLIENT START");
00036 
00037     config = g_key_file_new ();
00038     g_key_file_load_from_file (config, g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL), G_KEY_FILE_NONE, NULL);
00039   
00040     if (argc > 1)
00041         server_address = g_strdup (argv[1]);
00042     else
00043         server_address = g_strdup (":0");
00044 
00045     status_notify ("VNC-CLIENT CONNECT SERVER=%s", server_address);
00046 
00047     socket = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, &error);
00048     if (error)
00049         g_warning ("Unable to make VNC socket: %s", error->message);
00050     g_clear_error (&error);
00051     if (!socket)
00052         return EXIT_FAILURE;
00053 
00054     hostname = g_strdup (server_address);
00055     c = strchr (hostname, ':');
00056     if (c)
00057     {
00058         *c = '\0';
00059         gchar *port_string = c + 1;
00060         if (g_str_has_prefix (port_string, ":"))
00061             port = atoi (port_string + 1);
00062         else
00063             port = 5900 + atoi (port_string);        
00064     }
00065     else
00066         port = 5900;
00067     if (strcmp (hostname, "") == 0)
00068     {
00069         g_free (hostname);
00070         hostname = g_strdup ("localhost");
00071     }
00072   
00073     address = g_network_address_new (hostname, port);
00074     socket_address = g_socket_address_enumerator_next (g_socket_connectable_enumerate (address), NULL, NULL);
00075 
00076     result = g_socket_connect (socket, socket_address, NULL, &error);
00077     if (error)
00078         g_warning ("Unable to connect VNC socket: %s", error->message);
00079     g_clear_error (&error);
00080     if (!result)
00081         return EXIT_FAILURE;
00082 
00083     n_read = g_socket_receive (socket, buffer, 1023, NULL, &error);
00084     if (error)
00085         g_warning ("Unable to receive on VNC socket: %s", error->message);
00086     g_clear_error (&error);
00087     if (n_read < 0)
00088         return EXIT_FAILURE;
00089 
00090     buffer[n_read] = '\0';
00091     if (g_str_has_suffix (buffer, "\n"))
00092         buffer[n_read-1] = '\0';
00093     status_notify ("VNC-CLIENT CONNECTED VERSION=\"%s\"", buffer);
00094 
00095     snprintf (buffer, 1024, "RFB 003.003\n");
00096     n_sent = g_socket_send (socket, buffer, strlen (buffer), NULL, &error);
00097     if (error)
00098         g_warning ("Unable to send on VNC socket: %s", error->message);
00099     g_clear_error (&error);
00100     if (n_sent != strlen (buffer))
00101         return EXIT_FAILURE;
00102 
00103     return EXIT_SUCCESS;
00104 }