GNUnet  0.10.x
cadet_api_list_peers.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2011, 2017, 2019 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  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_cadet_service.h"
30 #include "cadet.h"
31 #include "cadet_protocol.h"
32 
33 
42 
46  void *peers_cb_cls;
47 
52 
57 
62 
67 };
68 
69 
76 static void
77 handle_get_peers(void *cls,
78  const struct GNUNET_CADET_LocalInfoPeers *info)
79 {
80  struct GNUNET_CADET_PeersLister *pl = cls;
81  struct GNUNET_CADET_PeerListEntry ple;
82 
83  ple.peer = info->destination;
84  ple.have_tunnel = (int)ntohs(info->tunnel);
85  ple.n_paths = (unsigned int)ntohs(info->paths);
86  ple.best_path_length = (unsigned int)ntohl(info->best_path_length);
87  pl->peers_cb(pl->peers_cb_cls,
88  &ple);
89 }
90 
91 
98 static void
100  const struct GNUNET_MessageHeader *msg)
101 {
102  struct GNUNET_CADET_PeersLister *pl = cls;
103 
104  (void)msg;
105 
106  pl->peers_cb(pl->peers_cb_cls,
107  NULL);
109 }
110 
111 
117 static void
118 reconnect(void *cls);
119 
120 
127 static void
128 error_handler(void *cls,
129  enum GNUNET_MQ_Error error)
130 {
131  struct GNUNET_CADET_PeersLister *pl = cls;
132 
133  GNUNET_MQ_destroy(pl->mq);
134  pl->mq = NULL;
138  &reconnect,
139  pl);
140 }
141 
142 
148 static void
149 reconnect(void *cls)
150 {
151  struct GNUNET_CADET_PeersLister *pl = cls;
152  struct GNUNET_MQ_MessageHandler handlers[] = {
156  pl),
157  GNUNET_MQ_hd_fixed_size(get_peers_end,
159  struct GNUNET_MessageHeader,
160  pl),
162  };
163  struct GNUNET_MessageHeader *msg;
164  struct GNUNET_MQ_Envelope *env;
165 
166  pl->reconnect_task = NULL;
167  pl->mq = GNUNET_CLIENT_connect(pl->cfg,
168  "cadet",
169  handlers,
170  &error_handler,
171  pl);
172  if (NULL == pl->mq)
173  return;
174  env = GNUNET_MQ_msg(msg,
176  GNUNET_MQ_send(pl->mq,
177  env);
178 }
179 
180 
193  GNUNET_CADET_PeersCB callback,
194  void *callback_cls)
195 {
196  struct GNUNET_CADET_PeersLister *pl;
197 
198  if (NULL == callback)
199  {
200  GNUNET_break(0);
201  return NULL;
202  }
204  pl->peers_cb = callback;
205  pl->peers_cb_cls = callback_cls;
206  pl->cfg = cfg;
207  reconnect(pl);
208  if (NULL == pl->mq)
209  {
210  GNUNET_free(pl);
211  return NULL;
212  }
213  return pl;
214 }
215 
216 
223 void *
225 {
226  void *ret = pl->peers_cb_cls;
227 
228  if (NULL != pl->mq)
229  GNUNET_MQ_destroy(pl->mq);
230  if (NULL != pl->reconnect_task)
232  GNUNET_free(pl);
233  return ret;
234 }
235 
236 /* end of cadet_api_list_peers.c */
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we use.
static void error_handler(void *cls, enum GNUNET_MQ_Error error)
Function called on connection trouble.
struct GNUNET_SCHEDULER_Task * reconnect_task
Task to reconnect.
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
static void handle_get_peers(void *cls, const struct GNUNET_CADET_LocalInfoPeers *info)
Process a local reply about info on all tunnels, pass info to the user.
void * peers_cb_cls
Info callback closure for info_cb.
GNUNET_MQ_Error
Error codes for the queue.
#define GNUNET_TIME_UNIT_MINUTES
One minute.
static void handle_get_peers_end(void *cls, const struct GNUNET_MessageHeader *msg)
Process a end of list reply about info on all peers.
uint32_t best_path_length
Shortest known path.
Definition: cadet.h:347
struct GNUNET_MQ_Handle * mq
Message queue to talk to CADET service.
#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_new(type)
Allocate a struct or union of the given type.
uint16_t paths
Number of paths.
Definition: cadet.h:337
struct GNUNET_CADET_PeersLister * GNUNET_CADET_list_peers(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_CADET_PeersCB callback, void *callback_cls)
Request information about peers known to the running cadet service.
static int ret
Final status code.
Definition: gnunet-arm.c:89
void * GNUNET_CADET_list_peers_cancel(struct GNUNET_CADET_PeersLister *pl)
Cancel a peer info request.
int16_t tunnel
Do we have a tunnel toward this peer?
Definition: cadet.h:342
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
unsigned int best_path_length
Length of the shortest path (0 = unknown, 1 = ourselves, 2 = direct neighbour).
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1237
struct GNUNET_TIME_Relative backoff
Backoff for reconnect attempts.
#define GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS_END
End of local information about all peers known to the service.
Message to inform the client about one of the peers in the service.
Definition: cadet.h:328
static void get_peers(void *cls)
Call CADET&#39;s meta API, get all peers known to a peer.
Definition: gnunet-cadet.c:583
struct GNUNET_PeerIdentity peer
Which peer is the information about?
Message handler for a specific message type.
#define GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_PEERS
Request local information about all peers known to the service.
int have_tunnel
Do we have a tunnel to this peer?
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
P2P messages used by CADET.
Handle to a message queue.
Definition: mq.c:84
static void reconnect(void *cls)
Reconnect to the service and try again.
configuration data
Definition: configuration.c:83
void(* GNUNET_CADET_PeersCB)(void *cls, const struct GNUNET_CADET_PeerListEntry *ple)
Method called to retrieve information about all peers in CADET, called once per peer.
#define GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS
Local information about all peers known to the service.
Information we return per peer.
Entry in list of pending tasks.
Definition: scheduler.c:131
unsigned int n_paths
Number of disjoint known paths to peer.
struct GNUNET_PeerIdentity destination
ID of the peer (can be local peer).
Definition: cadet.h:352
Header for all communications.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:821
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
GNUNET_CADET_PeersCB peers_cb
Monitor callback.
struct GNUNET_TIME_Relative GNUNET_TIME_randomized_backoff(struct GNUNET_TIME_Relative rt, struct GNUNET_TIME_Relative threshold)
Randomized exponential back-off, starting at 1 ms and going up by a factor of 2+r, where 0 <= r <= 0.5, up to a maximum of the given threshold.
Definition: time.c:758
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956