Blame gio/tests/gdbus-example-peer.c

Packit ae235b
/*
Packit ae235b
Packit ae235b
Usage examples (modulo addresses / credentials).
Packit ae235b
Packit ae235b
UNIX domain socket transport:
Packit ae235b
Packit ae235b
 Server:
Packit ae235b
   $ ./gdbus-example-peer --server --address unix:abstract=myaddr
Packit ae235b
   Server is listening at: unix:abstract=myaddr
Packit ae235b
   Client connected.
Packit ae235b
   Peer credentials: GCredentials:unix-user=500,unix-group=500,unix-process=13378
Packit ae235b
   Negotiated capabilities: unix-fd-passing=1
Packit ae235b
   Client said: Hey, it's 1273093080 already!
Packit ae235b
Packit ae235b
 Client:
Packit ae235b
   $ ./gdbus-example-peer --address unix:abstract=myaddr
Packit ae235b
   Connected.
Packit ae235b
   Negotiated capabilities: unix-fd-passing=1
Packit ae235b
   Server said: You said 'Hey, it's 1273093080 already!'. KTHXBYE!
Packit ae235b
Packit ae235b
Nonce-secured TCP transport on the same host:
Packit ae235b
Packit ae235b
 Server:
Packit ae235b
   $ ./gdbus-example-peer --server --address nonce-tcp:
Packit ae235b
   Server is listening at: nonce-tcp:host=localhost,port=43077,noncefile=/tmp/gdbus-nonce-file-X1ZNCV
Packit ae235b
   Client connected.
Packit ae235b
   Peer credentials: (no credentials received)
Packit ae235b
   Negotiated capabilities: unix-fd-passing=0
Packit ae235b
   Client said: Hey, it's 1273093206 already!
Packit ae235b
Packit ae235b
 Client:
Packit ae235b
   $ ./gdbus-example-peer -address nonce-tcp:host=localhost,port=43077,noncefile=/tmp/gdbus-nonce-file-X1ZNCV
Packit ae235b
   Connected.
Packit ae235b
   Negotiated capabilities: unix-fd-passing=0
Packit ae235b
   Server said: You said 'Hey, it's 1273093206 already!'. KTHXBYE!
Packit ae235b
Packit ae235b
TCP transport on two different hosts with a shared home directory:
Packit ae235b
Packit ae235b
 Server:
Packit ae235b
   host1 $ ./gdbus-example-peer --server --address tcp:host=0.0.0.0
Packit ae235b
   Server is listening at: tcp:host=0.0.0.0,port=46314
Packit ae235b
   Client connected.
Packit ae235b
   Peer credentials: (no credentials received)
Packit ae235b
   Negotiated capabilities: unix-fd-passing=0
Packit ae235b
   Client said: Hey, it's 1273093337 already!
Packit ae235b
Packit ae235b
 Client:
Packit ae235b
   host2 $ ./gdbus-example-peer -a tcp:host=host1,port=46314
Packit ae235b
   Connected.
Packit ae235b
   Negotiated capabilities: unix-fd-passing=0
Packit ae235b
   Server said: You said 'Hey, it's 1273093337 already!'. KTHXBYE!
Packit ae235b
Packit ae235b
TCP transport on two different hosts without authentication:
Packit ae235b
Packit ae235b
 Server:
Packit ae235b
   host1 $ ./gdbus-example-peer --server --address tcp:host=0.0.0.0 --allow-anonymous
Packit ae235b
   Server is listening at: tcp:host=0.0.0.0,port=59556
Packit ae235b
   Client connected.
Packit ae235b
   Peer credentials: (no credentials received)
Packit ae235b
   Negotiated capabilities: unix-fd-passing=0
Packit ae235b
   Client said: Hey, it's 1273093652 already!
Packit ae235b
Packit ae235b
 Client:
Packit ae235b
   host2 $ ./gdbus-example-peer -a tcp:host=host1,port=59556
Packit ae235b
   Connected.
Packit ae235b
   Negotiated capabilities: unix-fd-passing=0
Packit ae235b
   Server said: You said 'Hey, it's 1273093652 already!'. KTHXBYE!
Packit ae235b
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include <gio/gio.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static GDBusNodeInfo *introspection_data = NULL;
Packit ae235b
Packit ae235b
/* Introspection data for the service we are exporting */
Packit ae235b
static const gchar introspection_xml[] =
Packit ae235b
  "<node>"
Packit ae235b
  "  <interface name='org.gtk.GDBus.TestPeerInterface'>"
Packit ae235b
  "    <method name='HelloWorld'>"
Packit ae235b
  "      <arg type='s' name='greeting' direction='in'/>"
Packit ae235b
  "      <arg type='s' name='response' direction='out'/>"
Packit ae235b
  "    </method>"
Packit ae235b
  "  </interface>"
Packit ae235b
  "</node>";
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static void
Packit ae235b
handle_method_call (GDBusConnection       *connection,
Packit ae235b
                    const gchar           *sender,
Packit ae235b
                    const gchar           *object_path,
Packit ae235b
                    const gchar           *interface_name,
Packit ae235b
                    const gchar           *method_name,
Packit ae235b
                    GVariant              *parameters,
Packit ae235b
                    GDBusMethodInvocation *invocation,
Packit ae235b
                    gpointer               user_data)
Packit ae235b
{
Packit ae235b
  if (g_strcmp0 (method_name, "HelloWorld") == 0)
Packit ae235b
    {
Packit ae235b
      const gchar *greeting;
Packit ae235b
      gchar *response;
Packit ae235b
Packit ae235b
      g_variant_get (parameters, "(&s)", &greeting);
Packit ae235b
      response = g_strdup_printf ("You said '%s'. KTHXBYE!", greeting);
Packit ae235b
      g_dbus_method_invocation_return_value (invocation,
Packit ae235b
                                             g_variant_new ("(s)", response));
Packit ae235b
      g_free (response);
Packit ae235b
      g_print ("Client said: %s\n", greeting);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static const GDBusInterfaceVTable interface_vtable =
Packit ae235b
{
Packit ae235b
  handle_method_call,
Packit ae235b
  NULL,
Packit ae235b
  NULL,
Packit ae235b
};
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
on_new_connection (GDBusServer *server,
Packit ae235b
                   GDBusConnection *connection,
Packit ae235b
                   gpointer user_data)
Packit ae235b
{
Packit ae235b
  guint registration_id;
Packit ae235b
  GCredentials *credentials;
Packit ae235b
  gchar *s;
Packit ae235b
Packit ae235b
  credentials = g_dbus_connection_get_peer_credentials (connection);
Packit ae235b
  if (credentials == NULL)
Packit ae235b
    s = g_strdup ("(no credentials received)");
Packit ae235b
  else
Packit ae235b
    s = g_credentials_to_string (credentials);
Packit ae235b
Packit ae235b
Packit ae235b
  g_print ("Client connected.\n"
Packit ae235b
           "Peer credentials: %s\n"
Packit ae235b
           "Negotiated capabilities: unix-fd-passing=%d\n",
Packit ae235b
           s,
Packit ae235b
           g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
Packit ae235b
Packit ae235b
  g_object_ref (connection);
Packit ae235b
  registration_id = g_dbus_connection_register_object (connection,
Packit ae235b
                                                       "/org/gtk/GDBus/TestObject",
Packit ae235b
                                                       introspection_data->interfaces[0],
Packit ae235b
                                                       &interface_vtable,
Packit ae235b
                                                       NULL,  /* user_data */
Packit ae235b
                                                       NULL,  /* user_data_free_func */
Packit ae235b
                                                       NULL); /* GError** */
Packit ae235b
  g_assert (registration_id > 0);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int argc, char *argv[])
Packit ae235b
{
Packit ae235b
  gint ret;
Packit ae235b
  gboolean opt_server;
Packit ae235b
  gchar *opt_address;
Packit ae235b
  GOptionContext *opt_context;
Packit ae235b
  gboolean opt_allow_anonymous;
Packit ae235b
  GError *error;
Packit ae235b
  GOptionEntry opt_entries[] =
Packit ae235b
    {
Packit ae235b
      { "server", 's', 0, G_OPTION_ARG_NONE, &opt_server, "Start a server instead of a client", NULL },
Packit ae235b
      { "address", 'a', 0, G_OPTION_ARG_STRING, &opt_address, "D-Bus address to use", NULL },
Packit ae235b
      { "allow-anonymous", 'n', 0, G_OPTION_ARG_NONE, &opt_allow_anonymous, "Allow anonymous authentication", NULL },
Packit ae235b
      { NULL}
Packit ae235b
    };
Packit ae235b
Packit ae235b
  ret = 1;
Packit ae235b
Packit ae235b
  opt_address = NULL;
Packit ae235b
  opt_server = FALSE;
Packit ae235b
  opt_allow_anonymous = FALSE;
Packit ae235b
Packit ae235b
  opt_context = g_option_context_new ("peer-to-peer example");
Packit ae235b
  error = NULL;
Packit ae235b
  g_option_context_add_main_entries (opt_context, opt_entries, NULL);
Packit ae235b
  if (!g_option_context_parse (opt_context, &argc, &argv, &error))
Packit ae235b
    {
Packit ae235b
      g_printerr ("Error parsing options: %s\n", error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
  if (opt_address == NULL)
Packit ae235b
    {
Packit ae235b
      g_printerr ("Incorrect usage, try --help.\n");
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
  if (!opt_server && opt_allow_anonymous)
Packit ae235b
    {
Packit ae235b
      g_printerr ("The --allow-anonymous option only makes sense when used with --server.\n");
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* We are lazy here - we don't want to manually provide
Packit ae235b
   * the introspection data structures - so we just build
Packit ae235b
   * them from XML.
Packit ae235b
   */
Packit ae235b
  introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
Packit ae235b
  g_assert (introspection_data != NULL);
Packit ae235b
Packit ae235b
  if (opt_server)
Packit ae235b
    {
Packit ae235b
      GDBusServer *server;
Packit ae235b
      gchar *guid;
Packit ae235b
      GMainLoop *loop;
Packit ae235b
      GDBusServerFlags server_flags;
Packit ae235b
Packit ae235b
      guid = g_dbus_generate_guid ();
Packit ae235b
Packit ae235b
      server_flags = G_DBUS_SERVER_FLAGS_NONE;
Packit ae235b
      if (opt_allow_anonymous)
Packit ae235b
        server_flags |= G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;
Packit ae235b
Packit ae235b
      error = NULL;
Packit ae235b
      server = g_dbus_server_new_sync (opt_address,
Packit ae235b
                                       server_flags,
Packit ae235b
                                       guid,
Packit ae235b
                                       NULL, /* GDBusAuthObserver */
Packit ae235b
                                       NULL, /* GCancellable */
Packit ae235b
                                       &error);
Packit ae235b
      g_dbus_server_start (server);
Packit ae235b
      g_free (guid);
Packit ae235b
Packit ae235b
      if (server == NULL)
Packit ae235b
        {
Packit ae235b
          g_printerr ("Error creating server at address %s: %s\n", opt_address, error->message);
Packit ae235b
          g_error_free (error);
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
      g_print ("Server is listening at: %s\n", g_dbus_server_get_client_address (server));
Packit ae235b
      g_signal_connect (server,
Packit ae235b
                        "new-connection",
Packit ae235b
                        G_CALLBACK (on_new_connection),
Packit ae235b
                        NULL);
Packit ae235b
Packit ae235b
      loop = g_main_loop_new (NULL, FALSE);
Packit ae235b
      g_main_loop_run (loop);
Packit ae235b
Packit ae235b
      g_object_unref (server);
Packit ae235b
      g_main_loop_unref (loop);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      GDBusConnection *connection;
Packit ae235b
      const gchar *greeting_response;
Packit ae235b
      GVariant *value;
Packit ae235b
      gchar *greeting;
Packit ae235b
Packit ae235b
      error = NULL;
Packit ae235b
      connection = g_dbus_connection_new_for_address_sync (opt_address,
Packit ae235b
                                                           G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
Packit ae235b
                                                           NULL, /* GDBusAuthObserver */
Packit ae235b
                                                           NULL, /* GCancellable */
Packit ae235b
                                                           &error);
Packit ae235b
      if (connection == NULL)
Packit ae235b
        {
Packit ae235b
          g_printerr ("Error connecting to D-Bus address %s: %s\n", opt_address, error->message);
Packit ae235b
          g_error_free (error);
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_print ("Connected.\n"
Packit ae235b
               "Negotiated capabilities: unix-fd-passing=%d\n",
Packit ae235b
               g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
Packit ae235b
Packit ae235b
      greeting = g_strdup_printf ("Hey, it's %" G_GUINT64_FORMAT " already!", (guint64) time (NULL));
Packit ae235b
      value = g_dbus_connection_call_sync (connection,
Packit ae235b
                                           NULL, /* bus_name */
Packit ae235b
                                           "/org/gtk/GDBus/TestObject",
Packit ae235b
                                           "org.gtk.GDBus.TestPeerInterface",
Packit ae235b
                                           "HelloWorld",
Packit ae235b
                                           g_variant_new ("(s)", greeting),
Packit ae235b
                                           G_VARIANT_TYPE ("(s)"),
Packit ae235b
                                           G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                                           -1,
Packit ae235b
                                           NULL,
Packit ae235b
                                           &error);
Packit ae235b
      if (value == NULL)
Packit ae235b
        {
Packit ae235b
          g_printerr ("Error invoking HelloWorld(): %s\n", error->message);
Packit ae235b
          g_error_free (error);
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
      g_variant_get (value, "(&s)", &greeting_response);
Packit ae235b
      g_print ("Server said: %s\n", greeting_response);
Packit ae235b
      g_variant_unref (value);
Packit ae235b
Packit ae235b
      g_object_unref (connection);
Packit ae235b
    }
Packit ae235b
  g_dbus_node_info_unref (introspection_data);
Packit ae235b
Packit ae235b
  ret = 0;
Packit ae235b
Packit ae235b
 out:
Packit ae235b
  return ret;
Packit ae235b
}