diff options
Diffstat (limited to 'src/services/gnunet-service-gns-dbus.c')
-rw-r--r-- | src/services/gnunet-service-gns-dbus.c | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/src/services/gnunet-service-gns-dbus.c b/src/services/gnunet-service-gns-dbus.c new file mode 100644 index 0000000..29ac2f8 --- /dev/null +++ b/src/services/gnunet-service-gns-dbus.c | |||
@@ -0,0 +1,198 @@ | |||
1 | #include "config.h" | ||
2 | |||
3 | #include <gnunet/platform.h> | ||
4 | #include <gnunet/gnunet_common.h> | ||
5 | #include <gnunet/gnunet_configuration_lib.h> | ||
6 | #include <gnunet/gnunet_getopt_lib.h> | ||
7 | #include <gnunet/gnunet_strings_lib.h> | ||
8 | #include <gnunet/gnunet_program_lib.h> | ||
9 | #include <gnunet/gnunet_gns_service.h> | ||
10 | |||
11 | #include "gnunet_dbus_lib.h" | ||
12 | #include "gnunet_crypto_dbus_lib.h" | ||
13 | #include "gnunet_gnsrecord_dbus_lib.h" | ||
14 | |||
15 | #define LOG(kind, ...) GNUNET_log_from (kind, "gns-dbus", __VA_ARGS__) | ||
16 | |||
17 | static void | ||
18 | lookup_return ( | ||
19 | void *cls, | ||
20 | uint32_t rd_count, | ||
21 | const struct GNUNET_GNSRECORD_Data *rd) | ||
22 | { | ||
23 | struct GNUNET_DBUS_MethodContext *mc = (struct GNUNET_DBUS_MethodContext *)cls; | ||
24 | unsigned msg_serial = dbus_message_get_serial (mc->message); | ||
25 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Received reply from GNS. Method call id %u\n", msg_serial); | ||
26 | DBusMessage *reply = GNUNET_DBUS_method_context_create_reply (mc); | ||
27 | DBusMessageIter reply_iter; | ||
28 | dbus_message_iter_init_append (reply, &reply_iter); | ||
29 | |||
30 | DBusMessageIter reply_iter_sub; | ||
31 | GNUNET_DBUS_push_open_array (reply, &reply_iter, &reply_iter_sub, GNUNET_GNSRECORD_DBUS_SIGNATURE_DATA); | ||
32 | uint32_t i; | ||
33 | for (i = 0; i < rd_count; i++) | ||
34 | GNUNET_GNSRECORD_DBUS_push_data (reply, &reply_iter_sub, rd + i); | ||
35 | GNUNET_DBUS_push_close_array (reply, &reply_iter, &reply_iter_sub); | ||
36 | |||
37 | GNUNET_DBUS_method_context_send_reply (mc, reply); | ||
38 | GNUNET_DBUS_method_context_unref (mc); | ||
39 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Forwarded reply to dbus. Method call id %u\n", msg_serial); | ||
40 | }; | ||
41 | |||
42 | #if 0 | ||
43 | static void | ||
44 | lookup_timeout ( | ||
45 | void *cls, | ||
46 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
47 | { | ||
48 | struct GNUNET_GNS_LookupRequest *lr = (struct GNUNET_GNS_LookupRequest *)cls; | ||
49 | GNUNET_G | ||
50 | } | ||
51 | #endif | ||
52 | |||
53 | static void | ||
54 | lookup ( | ||
55 | struct GNUNET_DBUS_MethodContext *mc) | ||
56 | { | ||
57 | const char *name; | ||
58 | struct GNUNET_CRYPTO_EcdsaPublicKey zone; | ||
59 | uint32_t type; | ||
60 | dbus_bool_t only_cached; | ||
61 | |||
62 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Received lookup request from dbus. Method call id %u\n", dbus_message_get_serial (mc->message)); | ||
63 | |||
64 | DBusMessage *message = mc->message; | ||
65 | DBusMessageIter message_iter; | ||
66 | dbus_message_iter_init (message, &message_iter); | ||
67 | |||
68 | DBusMessage *reply = NULL; | ||
69 | reply = reply ? reply : GNUNET_DBUS_pop_string (message, &message_iter, "name", &name); | ||
70 | reply = reply ? reply : GNUNET_CRYPTO_DBUS_pop_ecdsa_public_key (message, &message_iter, "zone", &zone); | ||
71 | reply = reply ? reply : GNUNET_GNSRECORD_DBUS_pop_type (message, &message_iter, "type", &type); | ||
72 | reply = reply ? reply : GNUNET_DBUS_pop_boolean (message, &message_iter, "only_cached", &only_cached); | ||
73 | if (reply) | ||
74 | { | ||
75 | GNUNET_DBUS_method_context_send_reply (mc, reply); | ||
76 | return; | ||
77 | }; | ||
78 | |||
79 | GNUNET_DBUS_method_context_ref (mc); | ||
80 | struct GNUNET_GNS_Handle *handle = GNUNET_DBUS_client_get_data (mc->client); | ||
81 | struct GNUNET_GNS_LookupRequest *lr = GNUNET_GNS_lookup ( | ||
82 | handle, | ||
83 | name, &zone, (int)type, (int)only_cached, NULL, | ||
84 | lookup_return, mc); | ||
85 | (void)lr; | ||
86 | |||
87 | //GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (500, GNUNET_TIME_UNIT_MILLISECONDS), | ||
88 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Forwarded lookup to GNS. Method call id %u\n", dbus_message_get_serial (mc->message)); | ||
89 | }; | ||
90 | |||
91 | static void | ||
92 | client_connects ( | ||
93 | struct GNUNET_DBUS_Service *service, | ||
94 | struct GNUNET_DBUS_Client *client) | ||
95 | { | ||
96 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating GNS client for %s\n", GNUNET_DBUS_client_get_unique_name (client)); | ||
97 | const struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_DBUS_service_get_config (service); | ||
98 | struct GNUNET_GNS_Handle *handle = GNUNET_GNS_connect (cfg); | ||
99 | GNUNET_DBUS_client_set_data (client, handle); | ||
100 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished creating GNS client for %s\n", GNUNET_DBUS_client_get_unique_name (client)); | ||
101 | }; | ||
102 | |||
103 | static void | ||
104 | client_disconnects ( | ||
105 | struct GNUNET_DBUS_Service *service, | ||
106 | struct GNUNET_DBUS_Client *client) | ||
107 | { | ||
108 | (void)service; | ||
109 | GNUNET_GNS_disconnect (GNUNET_DBUS_client_get_data (client)); | ||
110 | }; | ||
111 | |||
112 | static void | ||
113 | shutdown_task ( | ||
114 | void *cls, | ||
115 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
116 | { | ||
117 | (void)tc; | ||
118 | |||
119 | struct GNUNET_DBUS_Service *gns_service = (struct GNUNET_DBUS_Service *)cls; | ||
120 | GNUNET_DBUS_service_unref (gns_service); | ||
121 | |||
122 | LOG (GNUNET_ERROR_TYPE_INFO, "Exiting.\n"); | ||
123 | }; | ||
124 | |||
125 | static void | ||
126 | run ( | ||
127 | void *cls, | ||
128 | char *const *args, | ||
129 | const char *configfile, | ||
130 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
131 | { | ||
132 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Running.\n"); | ||
133 | |||
134 | struct GNUNET_DBUS_Service *gns_service = GNUNET_DBUS_service_create (cfg, "gns"); | ||
135 | if (! gns_service) | ||
136 | { | ||
137 | LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to create gns service.\n"); | ||
138 | GNUNET_abort_ (); | ||
139 | }; | ||
140 | GNUNET_DBUS_service_set_client_handlers (gns_service, client_connects, client_disconnects); | ||
141 | |||
142 | struct GNUNET_DBUS_Object *gns_object = GNUNET_DBUS_service_get_root_object (gns_service); | ||
143 | |||
144 | struct GNUNET_DBUS_Interface *gns_interface = GNUNET_DBUS_interface_create ("gnu.gnunet.gns"); | ||
145 | GNUNET_DBUS_object_add_interface (gns_object, GNUNET_DBUS_interface_introspectable ()); | ||
146 | GNUNET_DBUS_object_add_interface (gns_object, gns_interface); | ||
147 | |||
148 | struct GNUNET_DBUS_Method *gns_method_lookup = GNUNET_DBUS_method_create ("lookup", lookup); | ||
149 | GNUNET_DBUS_interface_add_method (gns_interface, gns_method_lookup); | ||
150 | GNUNET_DBUS_method_add_arg (gns_method_lookup, "name", GNUNET_DBUS_SIGNATURE_STRING); | ||
151 | GNUNET_DBUS_method_add_arg (gns_method_lookup, "zone", GNUNET_CRYPTO_DBUS_SIGNATURE_ECDSA_PUBLIC_KEY); | ||
152 | GNUNET_DBUS_method_add_arg (gns_method_lookup, "type", GNUNET_GNSRECORD_DBUS_SIGNATURE_TYPE); | ||
153 | GNUNET_DBUS_method_add_arg (gns_method_lookup, "only_cached", GNUNET_DBUS_SIGNATURE_BOOLEAN); | ||
154 | |||
155 | GNUNET_DBUS_method_add_return_arg (gns_method_lookup, "records", GNUNET_DBUS_SIGNATURE_ARRAY (GNUNET_GNSRECORD_DBUS_SIGNATURE_DATA)); | ||
156 | |||
157 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, shutdown_task, gns_service); | ||
158 | }; | ||
159 | |||
160 | int | ||
161 | main ( | ||
162 | int argc, | ||
163 | char *const *argv) | ||
164 | { | ||
165 | int ret; | ||
166 | |||
167 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
168 | GNUNET_GETOPT_OPTION_END | ||
169 | }; | ||
170 | static const char bin_name[] = "gnunet-service-gns-dbus [OPTIONS]"; | ||
171 | static const char bin_help[] = gettext_noop ("DBus proxy for gnunet-service-gns"); | ||
172 | |||
173 | ret = GNUNET_log_setup ("gnunet-service-gns-dbus", "DEBUG", NULL); | ||
174 | if (GNUNET_OK != ret) | ||
175 | { | ||
176 | fprintf (stderr, "ERROR: Failed to setup logging. GNUNET_log_setup returned %d\n", ret); | ||
177 | return 1; | ||
178 | } | ||
179 | |||
180 | ret = GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv); | ||
181 | if (GNUNET_OK != ret) | ||
182 | { | ||
183 | LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to parse command line options. GNUNET_STRINGS_get_utf8_args returned %d\n", ret); | ||
184 | return 1; | ||
185 | }; | ||
186 | |||
187 | ret = GNUNET_PROGRAM_run (argc, argv, bin_name, bin_help, options, run, NULL); | ||
188 | if (GNUNET_OK != ret) | ||
189 | { | ||
190 | LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to run program. GNUNET_PROGRAM_run returned %d\n", ret); | ||
191 | return 1; | ||
192 | }; | ||
193 | |||
194 | GNUNET_free ((void *)argv); | ||
195 | return 0; | ||
196 | }; | ||
197 | |||
198 | |||