2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public Liceidentity as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public Liceidentity for more details.
15 You should have received a copy of the GNU General Public Liceidentity
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file identity/identity_api.c
23 * @brief api to interact with the identity service
24 * @author Christian Grothoff
27 #include "gnunet_client_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_server_lib.h"
34 #include "gnunet_time_lib.h"
35 #include "gnunet_identity_service.h"
38 #define LOG(kind,...) GNUNET_log_from (kind, "identity-api",__VA_ARGS__)
43 struct GNUNET_IDENTITY_Ego
49 * Handle for the service.
51 struct GNUNET_IDENTITY_Handle
54 * Configuration to use.
56 const struct GNUNET_CONFIGURATION_Handle *cfg;
59 * Socket (if available).
61 struct GNUNET_CLIENT_Connection *client;
64 * Currently pending transmission request.
66 struct GNUNET_CLIENT_TransmitHandle *th;
69 * Task doing exponential back-off trying to reconnect.
71 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
74 * Time for next connect retry.
76 struct GNUNET_TIME_Relative reconnect_delay;
82 * Try again to connect to network size estimation service.
84 * @param cls the handle to the transport service
85 * @param tc scheduler context
89 const struct GNUNET_SCHEDULER_TaskContext *tc);
93 * Type of a function to call when we receive a message
97 * @param msg message received, NULL on timeout or fatal error
100 message_handler (void *cls,
101 const struct GNUNET_MessageHeader *msg)
103 struct GNUNET_IDENTITY_Handle *h = cls;
104 const struct GNUNET_IDENTITY_ClientMessage *client_msg;
108 /* Error, timeout, death */
109 GNUNET_CLIENT_disconnect (h->client);
112 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
116 GNUNET_CLIENT_receive (h->client, &message_handler, h,
117 GNUNET_TIME_UNIT_FOREVER_REL);
123 * Reschedule a connect attempt to the service.
125 * @param h transport service to reconnect
128 reschedule_connect (struct GNUNET_IDENTITY_Handle *h)
130 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
134 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
137 if (NULL != h->client)
139 GNUNET_CLIENT_disconnect (h->client);
143 LOG (GNUNET_ERROR_TYPE_DEBUG,
144 "Scheduling task to reconnect to identity service in %llu ms.\n",
145 h->reconnect_delay.rel_value);
147 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
148 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
153 * Transmit START message to service.
156 * @param size number of bytes available in buf
157 * @param buf where to copy the message
158 * @return number of bytes copied to buf
161 send_start (void *cls,
170 * Try again to connect to network size estimation service.
172 * @param cls the handle to the transport service
173 * @param tc scheduler context
176 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
178 struct GNUNET_IDENTITY_Handle *h = cls;
180 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
181 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
183 /* shutdown, just give up */
186 LOG (GNUNET_ERROR_TYPE_DEBUG,
187 "Connecting to network size estimation service.\n");
188 GNUNET_assert (h->client == NULL);
189 h->client = GNUNET_CLIENT_connect ("identity", h->cfg);
190 GNUNET_assert (h->client != NULL);
193 GNUNET_CLIENT_notify_transmit_ready (h->client,
194 sizeof (struct GNUNET_MessageHeader),
195 GNUNET_TIME_UNIT_FOREVER_REL,
196 GNUNET_NO, &send_start, h);
197 GNUNET_assert (h->th != NULL);
202 * Connect to the identity service.
204 * @param cfg the configuration to use
205 * @param cb function to call on all identity events, can be NULL
206 * @param cb_cls closure for 'cb'
207 * @return handle to use
209 struct GNUNET_IDENTITY_Handle *
210 GNUNET_IDENTITY_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
211 GNUNET_IDENTITY_Callback cb,
214 struct GNUNET_IDENTITY_Handle *ret;
216 ret = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Handle));
218 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
219 ret->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, ret);
225 * Obtain the ECC key associated with a ego.
228 * @return associated ECC key, valid as long as the ego is valid
230 const struct GNUNET_CRYPTO_EccPrivateKey *
231 GNUNET_IDENTITY_ego_get_key (struct GNUNET_IDENTITY_Ego *ego)
238 * Obtain the identity that is currently preferred/default
241 * @param id identity service to query
242 * @param service_name for which service is an identity wanted
243 * @param cb function to call with the result (will only be called once)
244 * @param cb_cls closure for cb
245 * @return handle to abort the operation
247 struct GNUNET_IDENTITY_Operation *
248 GNUNET_IDENTITY_get (struct GNUNET_IDENTITY_Handle *id,
249 const char *service_name,
250 GNUNET_IDENTITY_Callback cb,
258 * Set the preferred/default identity for a service.
260 * @param id identity service to inform
261 * @param service_name for which service is an identity set
262 * @param ego new default identity to be set for this service
263 * @param cont function to call once the operation finished
264 * @param cont_cls closure for cont
265 * @return handle to abort the operation
267 struct GNUNET_IDENTITY_Operation *
268 GNUNET_IDENTITY_set (struct GNUNET_IDENTITY_Handle *id,
269 const char *service_name,
270 struct GNUNET_IDENTITY_Ego *ego,
271 GNUNET_IDENTITY_Continuation cont,
279 * Create a new identity with the given identifier.
281 * @param id identity service to use
282 * @param identifier desired identifier
283 * @param cb function to call with the result (will only be called once)
284 * @param cb_cls closure for cb
285 * @return handle to abort the operation
287 struct GNUNET_IDENTITY_Operation *
288 GNUNET_IDENTITY_create (struct GNUNET_IDENTITY_Handle *id,
289 const char *identifier,
290 GNUNET_IDENTITY_Callback cb,
298 * Renames an existing identity.
300 * @param id identity service to use
301 * @param old_identifier old identifier
302 * @param new_identifier desired new identifier
303 * @param cb function to call with the result (will only be called once)
304 * @param cb_cls closure for cb
305 * @return handle to abort the operation
307 struct GNUNET_IDENTITY_Operation *
308 GNUNET_IDENTITY_rename (struct GNUNET_IDENTITY_Handle *id,
309 const char *old_identifier,
310 const char *new_identifier,
311 GNUNET_IDENTITY_Continuation cb,
319 * Delete an existing identity.
321 * @param id identity service to use
322 * @param identifier identifier of the identity to delete
323 * @param cb function to call with the result (will only be called once)
324 * @param cb_cls closure for cb
325 * @return handle to abort the operation
327 struct GNUNET_IDENTITY_Operation *
328 GNUNET_IDENTITY_delete (struct GNUNET_IDENTITY_Handle *id,
329 const char *identifier,
330 GNUNET_IDENTITY_Continuation cb,
338 * Cancel an identity operation. Note that the operation MAY still
339 * be executed; this merely cancels the continuation; if the request
340 * was already transmitted, the service may still choose to complete
343 * @param op operation to cancel
346 GNUNET_IDENITY_cancel (struct GNUNET_IDENTITY_Operation *op)
352 * Disconnect from identity service
354 * @param h handle to destroy
357 GNUNET_IDENTITY_disconnect (struct GNUNET_IDENTITY_Handle *h)
359 GNUNET_assert (NULL != h);
360 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
362 GNUNET_SCHEDULER_cancel (h->reconnect_task);
363 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
367 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
370 if (h->client != NULL)
372 GNUNET_CLIENT_disconnect (h->client);
378 /* end of identity_api.c */