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