+
+/**
+ * Obtain the ECC key associated with a ego.
+ *
+ * @param ego the ego
+ * @return associated ECC key, valid as long as the ego is valid
+ */
+const struct GNUNET_CRYPTO_EccPrivateKey *
+GNUNET_IDENTITY_ego_get_key (struct GNUNET_IDENTITY_Ego *ego)
+{
+ return ego->pk;
+}
+
+
+/**
+ * Obtain the identity that is currently preferred/default
+ * for a service.
+ *
+ * @param id identity service to query
+ * @param service_name for which service is an identity wanted
+ * @param cb function to call with the result (will only be called once)
+ * @param cb_cls closure for cb
+ * @return handle to abort the operation
+ */
+struct GNUNET_IDENTITY_Operation *
+GNUNET_IDENTITY_get (struct GNUNET_IDENTITY_Handle *id,
+ const char *service_name,
+ GNUNET_IDENTITY_Callback cb,
+ void *cb_cls)
+{
+ struct GNUNET_IDENTITY_Operation *op;
+ struct GNUNET_IDENTITY_GetDefaultMessage *gdm;
+ size_t slen;
+
+ slen = strlen (service_name) + 1;
+ if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_GetDefaultMessage))
+ {
+ GNUNET_break (0);
+ return NULL;
+ }
+ op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
+ sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) +
+ slen);
+ op->h = id;
+ op->cb = cb;
+ op->cls = cb_cls;
+ gdm = (struct GNUNET_IDENTITY_GetDefaultMessage *) &op[1];
+ gdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT);
+ gdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) +
+ slen);
+ gdm->name_len = htons (slen);
+ gdm->reserved = htons (0);
+ memcpy (&gdm[1], service_name, slen);
+ op->msg = &gdm->header;
+ GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
+ id->op_tail,
+ op);
+ if (NULL == id->th)
+ transmit_next (id);
+ return op;
+}
+
+
+/**
+ * Set the preferred/default identity for a service.
+ *
+ * @param id identity service to inform
+ * @param service_name for which service is an identity set
+ * @param ego new default identity to be set for this service
+ * @param cont function to call once the operation finished
+ * @param cont_cls closure for cont
+ * @return handle to abort the operation
+ */
+struct GNUNET_IDENTITY_Operation *
+GNUNET_IDENTITY_set (struct GNUNET_IDENTITY_Handle *id,
+ const char *service_name,
+ struct GNUNET_IDENTITY_Ego *ego,
+ GNUNET_IDENTITY_Continuation cont,
+ void *cont_cls)
+{
+ struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
+ struct GNUNET_IDENTITY_Operation *op;
+ struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
+ char *str;
+ uint16_t enc_len;
+ size_t slen;
+
+ slen = strlen (service_name) + 1;
+ enc = GNUNET_CRYPTO_ecc_encode_key (ego->pk);
+ enc_len = ntohs (enc->size);
+
+ if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) - enc_len)
+ {
+ GNUNET_break (0);
+ GNUNET_free (enc);
+ return NULL;
+ }
+ op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
+ sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) +
+ enc_len + slen);
+ op->h = id;
+ op->cont = cont;
+ op->cls = cont_cls;
+ sdm = (struct GNUNET_IDENTITY_SetDefaultMessage *) &op[1];
+ sdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT);
+ sdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) +
+ slen + enc_len);
+ sdm->name_len = htons (slen);
+ sdm->pk_len = htons (enc_len);
+ str = (char *) &sdm[1];
+ memcpy (str, enc, enc_len);
+ memcpy (&str[enc_len], service_name, slen);
+ op->msg = &sdm->header;
+ GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
+ id->op_tail,
+ op);
+ if (NULL == id->th)
+ transmit_next (id);
+ return op;
+}
+
+
+/**
+ * Create a new identity with the given identifier.
+ *
+ * @param id identity service to use
+ * @param identifier desired identifier
+ * @param cb function to call with the result (will only be called once)
+ * @param cb_cls closure for cb
+ * @return handle to abort the operation
+ */
+struct GNUNET_IDENTITY_Operation *
+GNUNET_IDENTITY_create (struct GNUNET_IDENTITY_Handle *id,
+ const char *identifier,
+ GNUNET_IDENTITY_Callback cb,
+ void *cb_cls)
+{
+ struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
+ struct GNUNET_IDENTITY_Operation *op;
+ struct GNUNET_IDENTITY_CreateRequestMessage *crm;
+ struct GNUNET_CRYPTO_EccPrivateKey *pk;
+ char *str;
+ uint16_t enc_len;
+ size_t slen;
+
+ slen = strlen (identifier) + 1;
+ pk = GNUNET_CRYPTO_ecc_key_create ();
+ enc = GNUNET_CRYPTO_ecc_encode_key (pk);
+ GNUNET_CRYPTO_ecc_key_free (pk);
+ enc_len = ntohs (enc->size);
+
+ if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) - enc_len)
+ {
+ GNUNET_break (0);
+ GNUNET_free (enc);
+ return NULL;
+ }
+ op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
+ sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) +
+ enc_len + slen);
+ op->h = id;
+ op->cb = cb;
+ op->cls = cb_cls;
+ crm = (struct GNUNET_IDENTITY_CreateRequestMessage *) &op[1];
+ crm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_CREATE);
+ crm->header.size = htons (sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) +
+ slen + enc_len);
+ crm->name_len = htons (slen);
+ crm->pk_len = htons (enc_len);
+ str = (char *) &crm[1];
+ memcpy (str, enc, enc_len);
+ memcpy (&str[enc_len], identifier, slen);
+ op->msg = &crm->header;
+ GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
+ id->op_tail,
+ op);
+ if (NULL == id->th)
+ transmit_next (id);
+ return op;
+}
+
+
+/**
+ * Renames an existing identity.
+ *
+ * @param id identity service to use
+ * @param old_identifier old identifier
+ * @param new_identifier desired new identifier
+ * @param cb function to call with the result (will only be called once)
+ * @param cb_cls closure for cb
+ * @return handle to abort the operation
+ */
+struct GNUNET_IDENTITY_Operation *
+GNUNET_IDENTITY_rename (struct GNUNET_IDENTITY_Handle *id,
+ const char *old_identifier,
+ const char *new_identifier,
+ GNUNET_IDENTITY_Continuation cb,
+ void *cb_cls)
+{
+ struct GNUNET_IDENTITY_Operation *op;
+ struct GNUNET_IDENTITY_RenameMessage *grm;
+ size_t slen_old;
+ size_t slen_new;
+ char *dst;
+
+ slen_old = strlen (old_identifier) + 1;
+ slen_new = strlen (new_identifier) + 1;
+ if ( (slen_old >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
+ (slen_new >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
+ (slen_old + slen_new >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_RenameMessage)) )
+ {
+ GNUNET_break (0);
+ return NULL;
+ }
+ op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
+ sizeof (struct GNUNET_IDENTITY_RenameMessage) +
+ slen_old + slen_new);
+ op->h = id;
+ op->cont = cb;
+ op->cls = cb_cls;
+ grm = (struct GNUNET_IDENTITY_RenameMessage *) &op[1];
+ grm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_RENAME);
+ grm->header.size = htons (sizeof (struct GNUNET_IDENTITY_RenameMessage) +
+ slen_old + slen_new);
+ grm->old_name_len = htons (slen_old);
+ grm->new_name_len = htons (slen_new);
+ dst = (char *) &grm[1];
+ memcpy (dst, old_identifier, slen_old);
+ memcpy (&dst[slen_old], new_identifier, slen_new);
+ op->msg = &grm->header;
+ GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
+ id->op_tail,
+ op);
+ if (NULL == id->th)
+ transmit_next (id);
+ return op;
+}
+
+
+/**
+ * Delete an existing identity.
+ *
+ * @param id identity service to use
+ * @param identifier identifier of the identity to delete
+ * @param cb function to call with the result (will only be called once)
+ * @param cb_cls closure for cb
+ * @return handle to abort the operation
+ */
+struct GNUNET_IDENTITY_Operation *
+GNUNET_IDENTITY_delete (struct GNUNET_IDENTITY_Handle *id,
+ const char *identifier,
+ GNUNET_IDENTITY_Continuation cb,
+ void *cb_cls)
+{
+ struct GNUNET_IDENTITY_Operation *op;
+ struct GNUNET_IDENTITY_DeleteMessage *gdm;
+ size_t slen;
+
+ slen = strlen (identifier) + 1;
+ if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_DeleteMessage))
+ {
+ GNUNET_break (0);
+ return NULL;
+ }
+ op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
+ sizeof (struct GNUNET_IDENTITY_DeleteMessage) +
+ slen);
+ op->h = id;
+ op->cont = cb;
+ op->cls = cb_cls;
+ gdm = (struct GNUNET_IDENTITY_DeleteMessage *) &op[1];
+ gdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_DELETE);
+ gdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_DeleteMessage) +
+ slen);
+ gdm->name_len = htons (slen);
+ gdm->reserved = htons (0);
+ memcpy (&gdm[1], identifier, slen);
+ op->msg = &gdm->header;
+ GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
+ id->op_tail,
+ op);
+ if (NULL == id->th)
+ transmit_next (id);
+ return op;
+}
+
+
+/**
+ * Cancel an identity operation. Note that the operation MAY still
+ * be executed; this merely cancels the continuation; if the request
+ * was already transmitted, the service may still choose to complete
+ * the operation.
+ *
+ * @param op operation to cancel
+ */
+void
+GNUNET_IDENTITY_cancel (struct GNUNET_IDENTITY_Operation *op)
+{
+ struct GNUNET_IDENTITY_Handle *h = op->h;
+
+ if ( (h->op_head != op) ||
+ (NULL == h->client) )
+ {
+ /* request not active, can simply remove */
+ GNUNET_CONTAINER_DLL_remove (h->op_head,
+ h->op_tail,
+ op);
+ GNUNET_free (op);
+ return;
+ }
+ if (NULL != h->th)
+ {
+ /* request active but not yet with service, can still abort */
+ GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
+ h->th = NULL;
+ GNUNET_CONTAINER_DLL_remove (h->op_head,
+ h->op_tail,
+ op);
+ GNUNET_free (op);
+ transmit_next (h);
+ return;
+ }
+ /* request active with service, simply ensure continuations are not called */
+ op->cont = NULL;
+ op->cb = NULL;
+}
+
+
+/**
+ * Free ego from hash map.
+ *
+ * @param cls identity service handle
+ * @param key unused
+ * @param value ego to free
+ * @return GNUNET_OK (continue to iterate)
+ */
+static int
+free_ego (void *cls,
+ const struct GNUNET_HashCode *key,
+ void *value)
+{
+ struct GNUNET_IDENTITY_Handle *h = cls;
+ struct GNUNET_IDENTITY_Ego *ego = value;
+
+ h->cb (h->cb_cls,
+ ego,
+ &ego->ctx,
+ NULL);
+ GNUNET_CRYPTO_ecc_key_free (ego->pk);
+ GNUNET_free (ego->identifier);
+ GNUNET_free (ego);
+ return GNUNET_OK;