GNUnet  0.10.x
peerinfo_api.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2001-2014 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"
28 #include "gnunet_protocols.h"
29 #include "peerinfo.h"
30 
31 #define LOG(kind, ...) GNUNET_log_from(kind, "peerinfo-api", __VA_ARGS__)
32 
33 
42 
47 
52 
57 
61  void *callback_cls;
62 
67 
71  int have_peer;
72 
77 };
78 
79 
88 
93 
98 
103 
108 };
109 
110 
116 static void
118 
119 
127 struct GNUNET_PEERINFO_Handle *
129 {
130  struct GNUNET_PEERINFO_Handle *h;
131 
133  h->cfg = cfg;
134  reconnect(h);
135  if (NULL == h->mq)
136  {
137  GNUNET_free(h);
138  return NULL;
139  }
140  return h;
141 }
142 
143 
153 void
155 {
157 
158  while (NULL != (ic = h->ic_head))
159  {
161  h->ic_tail,
162  ic);
163  GNUNET_free(ic);
164  }
165  if (NULL != h->mq)
166  {
167  GNUNET_MQ_destroy(h->mq);
168  h->mq = NULL;
169  }
170  if (NULL != h->r_task)
171  {
173  h->r_task = NULL;
174  }
175  GNUNET_free(h);
176 }
177 
178 
184 static void
185 reconnect_task(void *cls)
186 {
187  struct GNUNET_PEERINFO_Handle *h = cls;
188 
189  h->r_task = NULL;
190  reconnect(h);
191 }
192 
193 
199 static void
201 {
203 
204  GNUNET_MQ_destroy(h->mq);
205  h->mq = NULL;
206  if (NULL != ic)
207  {
209  h->ic_tail,
210  ic);
211  if (NULL != ic->callback)
212  ic->callback(ic->callback_cls,
213  NULL,
214  NULL,
215  _("Failed to receive response from `PEERINFO' service."));
216  GNUNET_free(ic);
217  }
219  h);
220 }
221 
222 
230 static void
232  enum GNUNET_MQ_Error error)
233 {
234  struct GNUNET_PEERINFO_Handle *h = cls;
235 
236  do_reconnect(h);
237 }
238 
239 
240 
249 static int
250 check_info(void *cls,
251  const struct InfoMessage *im)
252 {
253  struct GNUNET_PEERINFO_Handle *h = cls;
255  uint16_t ms = ntohs(im->header.size) - sizeof(*im);
256 
257  if (0 != ntohl(im->reserved))
258  {
259  GNUNET_break(0);
260  return GNUNET_SYSERR;
261  }
262  if (NULL == ic)
263  {
264  /* didn't expect a response, bad */
265  GNUNET_break(0);
266  return GNUNET_SYSERR;
267  }
268  if ((GNUNET_YES == ic->have_peer) &&
269  (0 != GNUNET_memcmp(&ic->peer,
270  &im->peer)))
271  {
272  /* bogus message (from a different iteration call?); out of sequence! */
274  "Received HELLO for peer `%s', expected peer `%s'\n",
275  GNUNET_i2s(&im->peer),
276  GNUNET_i2s(&ic->peer));
277  GNUNET_break(0);
278  return GNUNET_SYSERR;
279  }
280  if (ms > sizeof(struct GNUNET_MessageHeader))
281  {
282  const struct GNUNET_HELLO_Message *hello;
283  struct GNUNET_PeerIdentity id;
284 
285  hello = (const struct GNUNET_HELLO_Message *)&im[1];
286  if (ms != GNUNET_HELLO_size(hello))
287  {
288  /* malformed message */
289  GNUNET_break(0);
290  return GNUNET_SYSERR;
291  }
292  if (GNUNET_OK !=
293  GNUNET_HELLO_get_id(hello,
294  &id))
295  {
296  /* malformed message */
297  GNUNET_break(0);
298  return GNUNET_SYSERR;
299  }
300  if (0 != GNUNET_memcmp(&im->peer,
301  &id))
302  {
303  /* malformed message */
304  GNUNET_break(0);
305  return GNUNET_SYSERR;
306  }
307  }
308  else if (0 != ms)
309  {
310  /* malformed message */
311  GNUNET_break(0);
312  return GNUNET_SYSERR;
313  }
314  return GNUNET_OK;
315 }
316 
317 
324 static void
325 handle_info(void *cls,
326  const struct InfoMessage *im)
327 {
328  struct GNUNET_PEERINFO_Handle *h = cls;
330  const struct GNUNET_HELLO_Message *hello = NULL;
331  uint16_t ms;
332 
333  ms = ntohs(im->header.size);
334  if (ms > sizeof(struct InfoMessage))
335  hello = (const struct GNUNET_HELLO_Message *)&im[1];
336  if (NULL != ic->callback)
337  ic->callback(ic->callback_cls,
338  &im->peer,
339  hello,
340  NULL);
341 }
342 
343 
349 static void
351 {
353  struct GNUNET_MQ_Envelope *env;
354  struct ListAllPeersMessage *lapm;
355  struct ListPeerMessage *lpm;
356 
357  if (NULL == ic)
358  {
359  GNUNET_break(0);
360  return;
361  }
362  if (NULL == h->mq)
363  {
364  GNUNET_break(0);
365  return;
366  }
367  if (GNUNET_NO == ic->have_peer)
368  {
370  "Requesting list of peers from PEERINFO service\n");
371  env = GNUNET_MQ_msg(lapm,
373  lapm->include_friend_only = htonl(ic->include_friend_only);
374  }
375  else
376  {
378  "Requesting information on peer `%s' from PEERINFO service\n",
379  GNUNET_i2s(&ic->peer));
380  env = GNUNET_MQ_msg(lpm,
382  lpm->include_friend_only = htonl(ic->include_friend_only);
383  lpm->peer = ic->peer;
384  }
385  GNUNET_MQ_send(h->mq,
386  env);
387 }
388 
389 
399 static void
401  const struct GNUNET_MessageHeader *msg)
402 {
403  struct GNUNET_PEERINFO_Handle *h = cls;
405 
406  if (NULL == ic)
407  {
408  /* didn't expect a response, reconnect */
409  GNUNET_break(0);
410  reconnect(h);
411  return;
412  }
414  "Received end of list of peers from PEERINFO service\n");
416  h->ic_tail,
417  ic);
418  if (NULL != h->ic_head)
419  send_ic_request(h);
420  if (NULL != ic->callback)
421  ic->callback(ic->callback_cls,
422  NULL,
423  NULL,
424  NULL);
425  GNUNET_free(ic);
426 }
427 
428 
434 static void
436 {
437  struct GNUNET_MQ_MessageHandler handlers[] = {
440  struct InfoMessage,
441  h),
442  GNUNET_MQ_hd_fixed_size(end_iteration,
444  struct GNUNET_MessageHeader,
445  h),
447  };
448 
449  if (NULL != h->r_task)
450  {
452  h->r_task = NULL;
453  }
454  if (NULL != h->mq)
455  {
456  GNUNET_MQ_destroy(h->mq);
457  h->mq = NULL;
458  }
459  h->mq = GNUNET_CLIENT_connect(h->cfg,
460  "peerinfo",
461  handlers,
463  h);
464  if (NULL != h->ic_head)
465  send_ic_request(h);
466 }
467 
468 
488  const struct GNUNET_PeerIdentity *peer,
490  void *callback_cls)
491 {
493 
495  ic->h = h;
497  ic->callback = callback;
499  if (NULL != peer)
500  {
501  ic->have_peer = GNUNET_YES;
502  ic->peer = *peer;
503  }
505  h->ic_tail,
506  ic);
507  if (h->ic_head == ic)
508  send_ic_request(h);
509  return ic;
510 }
511 
512 
518 void
520 {
521  struct GNUNET_PEERINFO_Handle *h = ic->h;
522 
523  ic->callback = NULL;
524  if (ic == h->ic_head)
525  return;
527  h->ic_tail,
528  ic);
529  GNUNET_free(ic);
530 }
531 
532 
551 struct GNUNET_MQ_Envelope *
553  const struct GNUNET_HELLO_Message *hello,
555  void *cont_cls)
556 {
557  struct GNUNET_MQ_Envelope *env;
558  struct GNUNET_PeerIdentity peer;
559 
560  if (NULL == h->mq)
561  return NULL;
563  GNUNET_HELLO_get_id(hello,
564  &peer));
566  "Adding peer `%s' to PEERINFO database\n",
567  GNUNET_i2s(&peer));
568  env = GNUNET_MQ_msg_copy((const struct GNUNET_MessageHeader *)hello);
569  if (NULL != cont)
571  cont,
572  cont_cls);
573  GNUNET_MQ_send(h->mq,
574  env);
575  return env;
576 }
577 
578 
579 /* end of peerinfo_api.c */
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
uint32_t include_friend_only
Include friend only HELLOs and peers in callbacks.
Definition: peerinfo.h:73
#define GNUNET_MESSAGE_TYPE_PEERINFO_GET
Request update and listing of a peer.
struct GNUNET_MQ_Handle * mq
Connection to the service.
Definition: peerinfo_api.c:92
uint32_t include_friend_only
Include friend only HELLOs and peers in callbacks.
Definition: peerinfo.h:51
void(* GNUNET_PEERINFO_Processor)(void *cls, const struct GNUNET_PeerIdentity *peer, const struct GNUNET_HELLO_Message *hello, const char *err_msg)
Type of an iterator over the hosts.
int GNUNET_HELLO_get_id(const struct GNUNET_HELLO_Message *hello, struct GNUNET_PeerIdentity *peer)
Get the peer identity from a HELLO message.
Definition: hello.c:662
struct GNUNET_MQ_Envelope * GNUNET_MQ_msg_copy(const struct GNUNET_MessageHeader *hdr)
Create a new envelope by copying an existing message.
Definition: mq.c:651
Handle to the peerinfo service.
Definition: peerinfo_api.c:83
A HELLO message is used to exchange information about transports with other peers.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
static void reconnect(struct GNUNET_PEERINFO_Handle *h)
Close the existing connection to PEERINFO and reconnect.
Definition: peerinfo_api.c:435
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:900
struct GNUNET_PEERINFO_Handle * GNUNET_PEERINFO_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the peerinfo service.
Definition: peerinfo_api.c:128
struct GNUNET_PeerIdentity peer
Restrict to peers with this identity (optional field, check header.size!).
Definition: peerinfo.h:57
GNUNET_MQ_Error
Error codes for the queue.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static void send_ic_request(struct GNUNET_PEERINFO_Handle *h)
Send the next IC request at the head of the queue.
Definition: peerinfo_api.c:350
Message used to inform the client about a particular peer; this message is optionally followed by a H...
Definition: peerinfo.h:100
struct GNUNET_PEERINFO_IteratorContext * ic_tail
Tail of iterator DLL.
Definition: peerinfo_api.c:102
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: peerinfo_api.c:87
struct GNUNET_PEERINFO_IteratorContext * ic_head
Head of iterator DLL.
Definition: peerinfo_api.c:97
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
#define LOG(kind,...)
Definition: peerinfo_api.c:31
Context for an iteration request.
Definition: peerinfo_api.c:37
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
void(* GNUNET_SCHEDULER_TaskCallback)(void *cls)
Signature of the main function of a task.
static void do_reconnect(struct GNUNET_PEERINFO_Handle *h)
We encountered an error, reconnect to the PEERINFO service.
Definition: peerinfo_api.c:200
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
struct GNUNET_PeerIdentity peer
About which peer are we talking here?
Definition: peerinfo.h:114
struct GNUNET_PEERINFO_IteratorContext * GNUNET_PEERINFO_iterate(struct GNUNET_PEERINFO_Handle *h, int include_friend_only, const struct GNUNET_PeerIdentity *peer, GNUNET_PEERINFO_Processor callback, void *callback_cls)
Call a method for each known matching host.
Definition: peerinfo_api.c:486
void GNUNET_PEERINFO_iterate_cancel(struct GNUNET_PEERINFO_IteratorContext *ic)
Cancel an iteration over peer information.
Definition: peerinfo_api.c:519
void GNUNET_MQ_notify_sent(struct GNUNET_MQ_Envelope *ev, GNUNET_SCHEDULER_TaskCallback cb, void *cb_cls)
Call a callback once the envelope has been sent, that is, sending it can not be canceled anymore...
Definition: mq.c:772
struct GNUNET_PEERINFO_IteratorContext * next
Kept in a DLL.
Definition: peerinfo_api.c:41
uint32_t reserved
Always zero.
Definition: peerinfo.h:109
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1264
static void mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
We got a disconnect after asking regex to do the announcement.
Definition: peerinfo_api.c:231
void * callback_cls
Closure for callback.
Definition: peerinfo_api.c:61
static void handle_end_iteration(void *cls, const struct GNUNET_MessageHeader *msg)
Type of a function to call when we receive a message from the service.
Definition: peerinfo_api.c:400
struct GNUNET_MessageHeader header
Type will be GNUNET_MESSAGE_TYPE_PEERINFO_INFO.
Definition: peerinfo.h:104
struct GNUNET_MQ_Envelope * GNUNET_PEERINFO_add_peer(struct GNUNET_PEERINFO_Handle *h, const struct GNUNET_HELLO_Message *hello, GNUNET_SCHEDULER_TaskCallback cont, void *cont_cls)
Add a host to the persistent list.
Definition: peerinfo_api.c:552
static void reconnect_task(void *cls)
Task scheduled to re-try connecting to the peerinfo service.
Definition: peerinfo_api.c:185
Message handler for a specific message type.
struct GNUNET_SCHEDULER_Task * r_task
ID for a reconnect task.
Definition: peerinfo_api.c:107
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
common internal definitions for peerinfo service
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
#define GNUNET_MESSAGE_TYPE_PEERINFO_INFO
Information about one of the peers.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
Handle to a message queue.
Definition: mq.c:84
The identity of the host (wraps the signing key of the peer).
configuration data
Definition: configuration.c:83
GNUNET_PEERINFO_Processor callback
Function to call with the results.
Definition: peerinfo_api.c:56
static int check_info(void *cls, const struct InfoMessage *im)
Function called when we receive an info message.
Definition: peerinfo_api.c:250
static void handle_info(void *cls, const struct InfoMessage *im)
Handle info message.
Definition: peerinfo_api.c:325
Entry in list of pending tasks.
Definition: scheduler.c:131
Message requesting a listing of all peers, restricted to the specified peer identity.
Definition: peerinfo.h:64
int include_friend_only
Only include friends in reply?
Definition: peerinfo_api.c:76
uint16_t GNUNET_HELLO_size(const struct GNUNET_HELLO_Message *hello)
Return the size of the given HELLO message.
Definition: hello.c:643
Message requesting a listing of peers, restricted to the specified peer identity. ...
Definition: peerinfo.h:42
void GNUNET_PEERINFO_disconnect(struct GNUNET_PEERINFO_Handle *h)
Disconnect from the peerinfo service.
Definition: peerinfo_api.c:154
#define GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL
Request update and listing of all peers.
Header for all communications.
struct GNUNET_PeerIdentity peer
Peer we are interested in (only valid if iteration was restricted to one peer).
Definition: peerinfo_api.c:66
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:821
#define GNUNET_YES
Definition: gnunet_common.h:77
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:351
#define GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END
End of information about other peers.
struct GNUNET_PEERINFO_Handle * h
Handle to the PEERINFO service.
Definition: peerinfo_api.c:51
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
struct GNUNET_PEERINFO_IteratorContext * prev
Kept in a DLL.
Definition: peerinfo_api.c:46
#define GNUNET_free(ptr)
Wrapper around free.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956