GNUnet  0.10.x
identity_api_suffix_lookup.c
Go to the documentation of this file.
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  SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
29 #include "identity.h"
30 
31 #define LOG(kind, ...) GNUNET_log_from (kind, "identity-api", __VA_ARGS__)
32 
33 
38 {
39 
44 
48  char *suffix;
49 
54 
58  void *cb_cls;
59 };
60 
61 
70 static int
71 check_identity_result_code (void *cls, const struct ResultCodeMessage *rcm)
72 {
73  (void) cls;
74  if (sizeof (*rcm) != htons (rcm->header.size))
76  return GNUNET_OK;
77 }
78 
79 
86 static void
87 handle_identity_result_code (void *cls, const struct ResultCodeMessage *rcm)
88 {
89  struct GNUNET_IDENTITY_EgoSuffixLookup *el = cls;
90 
91  (void) rcm;
92  el->cb (el->cb_cls, NULL, NULL);
94 }
95 
96 
104 static int
105 check_identity_update (void *cls, const struct UpdateMessage *um)
106 {
107  uint16_t size = ntohs (um->header.size);
108  uint16_t name_len = ntohs (um->name_len);
109  const char *str = (const char *) &um[1];
110 
111  (void) cls;
112  if ((size != name_len + sizeof (struct UpdateMessage)) ||
113  ((0 != name_len) && ('\0' != str[name_len - 1])))
114  {
115  GNUNET_break (0);
116  return GNUNET_SYSERR;
117  }
118  return GNUNET_OK;
119 }
120 
121 
128 static void
129 handle_identity_update (void *cls, const struct UpdateMessage *um)
130 {
131  struct GNUNET_IDENTITY_EgoSuffixLookup *el = cls;
132  uint16_t name_len = ntohs (um->name_len);
133  const char *str = (0 == name_len) ? NULL : (const char *) &um[1];
134 
135  el->cb (el->cb_cls, &um->private_key, str);
137 }
138 
139 
148 static void
149 mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
150 {
151  struct GNUNET_IDENTITY_EgoSuffixLookup *el = cls;
152 
153  (void) error;
154  el->cb (el->cb_cls, NULL, NULL);
156 }
157 
158 
170  const char *suffix,
172  void *cb_cls)
173 {
175  struct GNUNET_MQ_Envelope *env;
176  struct GNUNET_MessageHeader *req;
177  size_t nlen;
178 
179  GNUNET_assert (NULL != cb);
181  el->cb = cb;
182  el->cb_cls = cb_cls;
183  {
184  struct GNUNET_MQ_MessageHandler handlers[] =
185  {GNUNET_MQ_hd_var_size (identity_result_code,
187  struct ResultCodeMessage,
188  el),
189  GNUNET_MQ_hd_var_size (identity_update,
191  struct UpdateMessage,
192  el),
194 
195  el->mq =
196  GNUNET_CLIENT_connect (cfg, "identity", handlers, &mq_error_handler, el);
197  }
198  if (NULL == el->mq)
199  {
200  GNUNET_break (0);
201  GNUNET_free (el);
202  return NULL;
203  }
204  el->suffix = GNUNET_strdup (suffix);
205  nlen = strlen (suffix) + 1;
207  memcpy (&req[1], suffix, nlen);
208  GNUNET_MQ_send (el->mq, env);
209  return el;
210 }
211 
212 
218 void
220 {
221  GNUNET_MQ_destroy (el->mq);
222  GNUNET_free (el->suffix);
223  GNUNET_free (el);
224 }
225 
226 
227 /* end of identity_api_suffix_lookup.c */
static void handle_identity_update(void *cls, const struct UpdateMessage *um)
Handle identity update message.
Common type definitions for the identity service and API.
static struct GNUNET_IDENTITY_EgoLookup * el
EgoLookup.
struct GNUNET_MQ_Handle * GNUNET_CLIENT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *error_handler_cls)
Create a message queue to connect to a GNUnet service.
Definition: client.c:901
GNUNET_MQ_Error
Error codes for the queue.
struct GNUNET_MessageHeader header
Type: GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE.
Definition: identity.h:82
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_MessageHeader header
Type: GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE.
Definition: identity.h:47
struct GNUNET_CRYPTO_EcdsaPrivateKey private_key
The private key.
Definition: identity.h:98
static void mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
Generic error handler, called with the appropriate error code and the same closure specified at the c...
#define GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE
Update about identity status from service to clients.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static void handle_identity_result_code(void *cls, const struct ResultCodeMessage *rcm)
We received a result code from the service.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
struct GNUNET_MQ_Handle * mq
Connection to service.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static int check_identity_result_code(void *cls, const struct ResultCodeMessage *rcm)
We received a result code from the service.
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct...
Definition: gnunet_mq_lib.h:52
#define GNUNET_MQ_check_zero_termination(m)
Insert code for a "check_" function that verifies that a given variable-length message received over ...
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
struct GNUNET_IDENTITY_EgoSuffixLookup * GNUNET_IDENTITY_ego_lookup_by_suffix(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *suffix, GNUNET_IDENTITY_EgoSuffixCallback cb, void *cb_cls)
Lookup an ego by name.
void GNUNET_IDENTITY_ego_lookup_by_suffix_cancel(struct GNUNET_IDENTITY_EgoSuffixLookup *el)
Abort ego lookup attempt.
Message handler for a specific message type.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
#define GNUNET_MESSAGE_TYPE_IDENTITY_LOOKUP_BY_SUFFIX
First message send from identity client to service to lookup a single ego matching the given suffix (...
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static unsigned int size
Size of the "table".
Definition: peer.c:67
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
GNUNET_IDENTITY_EgoSuffixCallback cb
Function to call with the result.
uint16_t name_len
Number of bytes in ego name string including 0-termination, in NBO; 0 if the ego was deleted...
Definition: identity.h:88
Handle to a message queue.
Definition: mq.c:85
configuration data
Definition: configuration.c:85
static int check_identity_update(void *cls, const struct UpdateMessage *um)
Check validity of identity update message.
Header for all communications.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
#define GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE
Generic response from identity service with success and/or error message.
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:353
char * suffix
Suffix we are looking up.
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
void(* GNUNET_IDENTITY_EgoSuffixCallback)(void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, const char *ego_name)
Function called with the result.
Answer from service to client about last operation; GET_DEFAULT maybe answered with this message on f...
Definition: identity.h:42
Service informs client about status of a pseudonym.
Definition: identity.h:77
#define GNUNET_free(ptr)
Wrapper around free.