3 Usage examples (modulo addresses / credentials).
5 UNIX domain socket transport:
8 $ ./gdbus-example-peer --server --address unix:abstract=myaddr
9 Server is listening at: unix:abstract=myaddr
11 Peer credentials: GCredentials:unix-user=500,unix-group=500,unix-process=13378
12 Negotiated capabilities: unix-fd-passing=1
13 Client said: Hey, it's 1273093080 already!
16 $ ./gdbus-example-peer --address unix:abstract=myaddr
18 Negotiated capabilities: unix-fd-passing=1
19 Server said: You said 'Hey, it's 1273093080 already!'. KTHXBYE!
21 Nonce-secured TCP transport on the same host:
24 $ ./gdbus-example-peer --server --address nonce-tcp:
25 Server is listening at: nonce-tcp:host=localhost,port=43077,noncefile=/tmp/gdbus-nonce-file-X1ZNCV
27 Peer credentials: (no credentials received)
28 Negotiated capabilities: unix-fd-passing=0
29 Client said: Hey, it's 1273093206 already!
32 $ ./gdbus-example-peer -address nonce-tcp:host=localhost,port=43077,noncefile=/tmp/gdbus-nonce-file-X1ZNCV
34 Negotiated capabilities: unix-fd-passing=0
35 Server said: You said 'Hey, it's 1273093206 already!'. KTHXBYE!
37 TCP transport on two different hosts with a shared home directory:
40 host1 $ ./gdbus-example-peer --server --address tcp:host=0.0.0.0
41 Server is listening at: tcp:host=0.0.0.0,port=46314
43 Peer credentials: (no credentials received)
44 Negotiated capabilities: unix-fd-passing=0
45 Client said: Hey, it's 1273093337 already!
48 host2 $ ./gdbus-example-peer -a tcp:host=host1,port=46314
50 Negotiated capabilities: unix-fd-passing=0
51 Server said: You said 'Hey, it's 1273093337 already!'. KTHXBYE!
53 TCP transport on two different hosts without authentication:
56 host1 $ ./gdbus-example-peer --server --address tcp:host=0.0.0.0 --allow-anonymous
57 Server is listening at: tcp:host=0.0.0.0,port=59556
59 Peer credentials: (no credentials received)
60 Negotiated capabilities: unix-fd-passing=0
61 Client said: Hey, it's 1273093652 already!
64 host2 $ ./gdbus-example-peer -a tcp:host=host1,port=59556
66 Negotiated capabilities: unix-fd-passing=0
67 Server said: You said 'Hey, it's 1273093652 already!'. KTHXBYE!
74 /* ---------------------------------------------------------------------------------------------------- */
76 static GDBusNodeInfo *introspection_data = NULL;
78 /* Introspection data for the service we are exporting */
79 static const gchar introspection_xml[] =
81 " <interface name='org.gtk.GDBus.TestPeerInterface'>"
82 " <method name='HelloWorld'>"
83 " <arg type='s' name='greeting' direction='in'/>"
84 " <arg type='s' name='response' direction='out'/>"
89 /* ---------------------------------------------------------------------------------------------------- */
92 handle_method_call (GDBusConnection *connection,
94 const gchar *object_path,
95 const gchar *interface_name,
96 const gchar *method_name,
98 GDBusMethodInvocation *invocation,
101 if (g_strcmp0 (method_name, "HelloWorld") == 0)
103 const gchar *greeting;
106 g_variant_get (parameters, "(&s)", &greeting);
107 response = g_strdup_printf ("You said '%s'. KTHXBYE!", greeting);
108 g_dbus_method_invocation_return_value (invocation,
109 g_variant_new ("(s)", response));
111 g_print ("Client said: %s\n", greeting);
115 static const GDBusInterfaceVTable interface_vtable =
123 /* ---------------------------------------------------------------------------------------------------- */
126 connection_closed (GDBusConnection *connection,
127 gboolean remote_peer_vanished,
131 g_print ("Client disconnected.\n");
132 g_object_unref (connection);
136 on_new_connection (GDBusServer *server,
137 GDBusConnection *connection,
140 guint registration_id;
141 GCredentials *credentials;
144 credentials = g_dbus_connection_get_peer_credentials (connection);
145 if (credentials == NULL)
146 s = g_strdup ("(no credentials received)");
148 s = g_credentials_to_string (credentials);
150 g_print ("Client connected.\n"
151 "Peer credentials: %s\n"
152 "Negotiated capabilities: unix-fd-passing=%d\n",
154 g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
156 g_object_ref (connection);
157 g_signal_connect (connection, "closed", G_CALLBACK (connection_closed), NULL);
158 registration_id = g_dbus_connection_register_object (connection,
159 "/org/gtk/GDBus/TestObject",
160 introspection_data->interfaces[0],
162 NULL, /* user_data */
163 NULL, /* user_data_free_func */
164 NULL); /* GError** */
165 g_assert (registration_id > 0);
172 /* ---------------------------------------------------------------------------------------------------- */
175 allow_mechanism_cb (GDBusAuthObserver *observer,
176 const gchar *mechanism,
177 G_GNUC_UNUSED gpointer user_data)
180 * In a production GDBusServer that only needs to work on modern Unix
181 * platforms, consider requiring EXTERNAL (credentials-passing),
182 * which is the recommended authentication mechanism for AF_UNIX
185 * if (g_strcmp0 (mechanism, "EXTERNAL") == 0)
190 * For this example we accept everything.
193 g_print ("Considering whether to accept %s authentication...\n", mechanism);
198 authorize_authenticated_peer_cb (GDBusAuthObserver *observer,
199 G_GNUC_UNUSED GIOStream *stream,
200 GCredentials *credentials,
201 G_GNUC_UNUSED gpointer user_data)
203 gboolean authorized = FALSE;
205 g_print ("Considering whether to authorize authenticated peer...\n");
207 if (credentials != NULL)
209 GCredentials *own_credentials;
210 gchar *credentials_string = NULL;
212 credentials_string = g_credentials_to_string (credentials);
213 g_print ("Peer's credentials: %s\n", credentials_string);
214 g_free (credentials_string);
216 own_credentials = g_credentials_new ();
218 credentials_string = g_credentials_to_string (own_credentials);
219 g_print ("Server's credentials: %s\n", credentials_string);
220 g_free (credentials_string);
222 if (g_credentials_is_same_user (credentials, own_credentials, NULL))
225 g_object_unref (own_credentials);
230 /* In most servers you'd want to reject this, but for this example
232 g_print ("A server would often not want to authorize this identity\n");
233 g_print ("Authorizing it anyway for demonstration purposes\n");
240 /* ---------------------------------------------------------------------------------------------------- */
243 main (int argc, char *argv[])
248 GOptionContext *opt_context;
249 gboolean opt_allow_anonymous;
251 GOptionEntry opt_entries[] =
253 { "server", 's', 0, G_OPTION_ARG_NONE, &opt_server, "Start a server instead of a client", NULL },
254 { "address", 'a', 0, G_OPTION_ARG_STRING, &opt_address, "D-Bus address to use", NULL },
255 { "allow-anonymous", 'n', 0, G_OPTION_ARG_NONE, &opt_allow_anonymous, "Allow anonymous authentication", NULL },
263 opt_allow_anonymous = FALSE;
265 opt_context = g_option_context_new ("peer-to-peer example");
267 g_option_context_add_main_entries (opt_context, opt_entries, NULL);
268 if (!g_option_context_parse (opt_context, &argc, &argv, &error))
270 g_option_context_free (opt_context);
271 g_printerr ("Error parsing options: %s\n", error->message);
272 g_error_free (error);
276 g_option_context_free (opt_context);
278 if (opt_address == NULL)
280 g_printerr ("Incorrect usage, try --help.\n");
283 if (!opt_server && opt_allow_anonymous)
285 g_printerr ("The --allow-anonymous option only makes sense when used with --server.\n");
289 /* We are lazy here - we don't want to manually provide
290 * the introspection data structures - so we just build
293 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
294 g_assert (introspection_data != NULL);
298 GDBusAuthObserver *observer;
302 GDBusServerFlags server_flags;
304 guid = g_dbus_generate_guid ();
306 server_flags = G_DBUS_SERVER_FLAGS_NONE;
307 if (opt_allow_anonymous)
308 server_flags |= G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;
310 observer = g_dbus_auth_observer_new ();
311 g_signal_connect (observer, "allow-mechanism", G_CALLBACK (allow_mechanism_cb), NULL);
312 g_signal_connect (observer, "authorize-authenticated-peer", G_CALLBACK (authorize_authenticated_peer_cb), NULL);
315 server = g_dbus_server_new_sync (opt_address,
319 NULL, /* GCancellable */
321 g_dbus_server_start (server);
323 g_object_unref (observer);
328 g_printerr ("Error creating server at address %s: %s\n", opt_address, error->message);
329 g_error_free (error);
332 g_print ("Server is listening at: %s\n", g_dbus_server_get_client_address (server));
333 g_signal_connect (server,
335 G_CALLBACK (on_new_connection),
338 loop = g_main_loop_new (NULL, FALSE);
339 g_main_loop_run (loop);
341 g_object_unref (server);
342 g_main_loop_unref (loop);
346 GDBusConnection *connection;
347 const gchar *greeting_response;
352 connection = g_dbus_connection_new_for_address_sync (opt_address,
353 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
354 NULL, /* GDBusAuthObserver */
355 NULL, /* GCancellable */
357 if (connection == NULL)
359 g_printerr ("Error connecting to D-Bus address %s: %s\n", opt_address, error->message);
360 g_error_free (error);
364 g_print ("Connected.\n"
365 "Negotiated capabilities: unix-fd-passing=%d\n",
366 g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
368 greeting = g_strdup_printf ("Hey, it's %" G_GINT64_FORMAT " already!",
369 g_get_real_time () / G_USEC_PER_SEC);
370 value = g_dbus_connection_call_sync (connection,
372 "/org/gtk/GDBus/TestObject",
373 "org.gtk.GDBus.TestPeerInterface",
375 g_variant_new ("(s)", greeting),
376 G_VARIANT_TYPE ("(s)"),
377 G_DBUS_CALL_FLAGS_NONE,
385 g_printerr ("Error invoking HelloWorld(): %s\n", error->message);
386 g_error_free (error);
389 g_variant_get (value, "(&s)", &greeting_response);
390 g_print ("Server said: %s\n", greeting_response);
391 g_variant_unref (value);
393 g_object_unref (connection);
395 g_dbus_node_info_unref (introspection_data);
400 g_free (opt_address);