-add ego creation
[oweals/gnunet.git] / src / identity / identity_api_lookup.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file identity/identity_api_lookup.c
23  * @brief api to lookup an ego
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_identity_service.h"
29
30 #define LOG(kind,...) GNUNET_log_from (kind, "identity-api",__VA_ARGS__)
31
32
33 /**
34  * Handle for ego lookup.
35  */
36 struct GNUNET_IDENTITY_EgoLookup
37 {
38
39   /**
40    * Handle to the identity service.
41    */
42   struct GNUNET_IDENTITY_Handle *identity;
43
44   /**
45    * Name of the ego we are looking up.
46    */
47   char *name;
48
49   /**
50    * Function to call with the result.
51    */
52   GNUNET_IDENTITY_EgoCallback cb;
53
54   /**
55    * Closure for @e cb
56    */
57   void *cb_cls;
58 };
59
60
61 /**
62  * Method called to inform about the egos of this peer.
63  *
64  * When used with #GNUNET_IDENTITY_connect, this function is
65  * initially called for all egos and then again whenever a
66  * ego's name changes or if it is deleted.  At the end of
67  * the initial pass over all egos, the function is once called
68  * with 'NULL' for @a ego. That does NOT mean that the callback won't
69  * be invoked in the future or that there was an error.
70  *
71  * If the @a name matches the name from @a cls, we found the zone
72  * for our computation and will invoke the callback.
73  * If we have iterated over all egos and not found the name, we
74  * invoke the callback with NULL.
75  *
76  * @param cls closure with the `struct GNUNET_IDENTITY_EgoLookup`
77  * @param ego ego handle
78  * @param ctx context for application to store data for this ego
79  *                 (during the lifetime of this process, initially NULL)
80  * @param name name assigned by the user for this ego,
81  *                   NULL if the user just deleted the ego and it
82  *                   must thus no longer be used
83  */
84 static void
85 identity_cb (void *cls,
86              struct GNUNET_IDENTITY_Ego *ego,
87              void **ctx,
88              const char *name)
89 {
90   struct GNUNET_IDENTITY_EgoLookup *el = cls;
91
92   if ( (NULL != name) &&
93        (0 == strcmp (name,
94                      el->name)) )
95   {
96     el->cb (el->cb_cls,
97             ego);
98     GNUNET_IDENTITY_ego_lookup_cancel (el);
99     return;
100   }
101   if (NULL == ego)
102   {
103     /* not found */
104     el->cb (el->cb_cls,
105             NULL);
106     GNUNET_IDENTITY_ego_lookup_cancel (el);
107     return;
108   }
109 }
110
111
112 /**
113  * Lookup an ego by name.
114  *
115  * @param cfg configuration to use
116  * @param name name to look up
117  * @param cb callback to invoke with the result
118  * @param cb_cls closure for @a cb
119  * @return NULL on error
120  */
121 struct GNUNET_IDENTITY_EgoLookup *
122 GNUNET_IDENTITY_ego_lookup (const struct GNUNET_CONFIGURATION_Handle *cfg,
123                             const char *name,
124                             GNUNET_IDENTITY_EgoCallback cb,
125                             void *cb_cls)
126 {
127   struct GNUNET_IDENTITY_EgoLookup *el;
128
129   el = GNUNET_new (struct GNUNET_IDENTITY_EgoLookup);
130   el->name = GNUNET_strdup (name);
131   el->cb = cb;
132   el->cb_cls = cb_cls;
133   el->identity = GNUNET_IDENTITY_connect (cfg,
134                                           &identity_cb,
135                                           el);
136   return el;
137 }
138
139
140 /**
141  * Abort ego lookup attempt.
142  *
143  * @param el handle for lookup to abort
144  */
145 void
146 GNUNET_IDENTITY_ego_lookup_cancel (struct GNUNET_IDENTITY_EgoLookup *el)
147 {
148   GNUNET_IDENTITY_disconnect (el->identity);
149   GNUNET_free (el->name);
150   GNUNET_free (el);
151 }
152
153
154 /* end of identity_api_lookup.c */