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 =
122 /* ---------------------------------------------------------------------------------------------------- */
125 on_new_connection (GDBusServer *server,
126 GDBusConnection *connection,
129 guint registration_id;
130 GCredentials *credentials;
133 credentials = g_dbus_connection_get_peer_credentials (connection);
134 if (credentials == NULL)
135 s = g_strdup ("(no credentials received)");
137 s = g_credentials_to_string (credentials);
140 g_print ("Client connected.\n"
141 "Peer credentials: %s\n"
142 "Negotiated capabilities: unix-fd-passing=%d\n",
144 g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
146 g_object_ref (connection);
147 registration_id = g_dbus_connection_register_object (connection,
148 "/org/gtk/GDBus/TestObject",
149 introspection_data->interfaces[0],
151 NULL, /* user_data */
152 NULL, /* user_data_free_func */
153 NULL); /* GError** */
154 g_assert (registration_id > 0);
157 /* ---------------------------------------------------------------------------------------------------- */
160 main (int argc, char *argv[])
165 GOptionContext *opt_context;
166 gboolean opt_allow_anonymous;
168 GOptionEntry opt_entries[] =
170 { "server", 's', 0, G_OPTION_ARG_NONE, &opt_server, "Start a server instead of a client", NULL },
171 { "address", 'a', 0, G_OPTION_ARG_STRING, &opt_address, "D-Bus address to use", NULL },
172 { "allow-anonymous", 'n', 0, G_OPTION_ARG_NONE, &opt_allow_anonymous, "Allow anonymous authentication", NULL },
182 opt_allow_anonymous = FALSE;
184 opt_context = g_option_context_new ("peer-to-peer example");
186 g_option_context_add_main_entries (opt_context, opt_entries, NULL);
187 if (!g_option_context_parse (opt_context, &argc, &argv, &error))
189 g_printerr ("Error parsing options: %s\n", error->message);
190 g_error_free (error);
193 if (opt_address == NULL)
195 g_printerr ("Incorrect usage, try --help.\n");
198 if (!opt_server && opt_allow_anonymous)
200 g_printerr ("The --allow-anonymous option only makes sense when used with --server.\n");
204 /* We are lazy here - we don't want to manually provide
205 * the introspection data structures - so we just build
208 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
209 g_assert (introspection_data != NULL);
216 GDBusServerFlags server_flags;
218 guid = g_dbus_generate_guid ();
220 server_flags = G_DBUS_SERVER_FLAGS_NONE;
221 if (opt_allow_anonymous)
222 server_flags |= G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;
225 server = g_dbus_server_new_sync (opt_address,
228 NULL, /* GDBusAuthObserver */
229 NULL, /* GCancellable */
231 g_dbus_server_start (server);
236 g_printerr ("Error creating server at address %s: %s\n", opt_address, error->message);
237 g_error_free (error);
240 g_print ("Server is listening at: %s\n", g_dbus_server_get_client_address (server));
241 g_signal_connect (server,
243 G_CALLBACK (on_new_connection),
246 loop = g_main_loop_new (NULL, FALSE);
247 g_main_loop_run (loop);
249 g_object_unref (server);
250 g_main_loop_unref (loop);
254 GDBusConnection *connection;
255 const gchar *greeting_response;
260 connection = g_dbus_connection_new_for_address_sync (opt_address,
261 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
262 NULL, /* GDBusAuthObserver */
263 NULL, /* GCancellable */
265 if (connection == NULL)
267 g_printerr ("Error connecting to D-Bus address %s: %s\n", opt_address, error->message);
268 g_error_free (error);
272 g_print ("Connected.\n"
273 "Negotiated capabilities: unix-fd-passing=%d\n",
274 g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
276 greeting = g_strdup_printf ("Hey, it's %" G_GUINT64_FORMAT " already!", (guint64) time (NULL));
277 value = g_dbus_connection_call_sync (connection,
279 "/org/gtk/GDBus/TestObject",
280 "org.gtk.GDBus.TestPeerInterface",
282 g_variant_new ("(s)", greeting),
283 G_DBUS_CALL_FLAGS_NONE,
289 g_printerr ("Error invoking HelloWorld(): %s\n", error->message);
290 g_error_free (error);
293 g_variant_get (value, "(&s)", &greeting_response);
294 g_print ("Server said: %s\n", greeting_response);
295 g_variant_unref (value);
297 g_object_unref (connection);
299 g_dbus_node_info_unref (introspection_data);