GNUnet  0.19.2
cadet_api_get_path.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2011, 2017 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 
38 {
43 
47  void *path_cb_cls;
48 
53 
58 
63 
68 
72  struct GNUNET_PeerIdentity id;
73 };
74 
75 
84 static int
85 check_get_path (void *cls,
86  const struct GNUNET_CADET_LocalInfoPath *message)
87 {
88  size_t msize = sizeof(struct GNUNET_CADET_LocalInfoPath);
89  size_t esize;
90 
91  (void) cls;
92  esize = ntohs (message->header.size);
93  if (esize < msize)
94  {
95  GNUNET_break (0);
96  return GNUNET_SYSERR;
97  }
98  if (0 != ((esize - msize) % sizeof(struct GNUNET_PeerIdentity)))
99  {
100  GNUNET_break (0);
101  return GNUNET_SYSERR;
102  }
103  return GNUNET_OK;
104 }
105 
106 
113 static void
114 handle_get_path (void *cls,
115  const struct GNUNET_CADET_LocalInfoPath *message)
116 {
117  struct GNUNET_CADET_GetPath *gp = cls;
118  struct GNUNET_CADET_PeerPathDetail ppd;
119 
120  ppd.peer = gp->id;
121  ppd.path = (const struct GNUNET_PeerIdentity *) &message[1];
122  ppd.target_offset = ntohl (message->off);
123  ppd.path_length = (ntohs (message->header.size) - sizeof(*message))
124  / sizeof(struct GNUNET_PeerIdentity);
125  gp->path_cb (gp->path_cb_cls,
126  &ppd);
127 }
128 
129 
136 static void
138  const struct GNUNET_MessageHeader *message)
139 {
140  struct GNUNET_CADET_GetPath *gp = cls;
141 
142  (void) message;
143  gp->path_cb (gp->path_cb_cls,
144  NULL);
146 }
147 
148 
154 static void
155 reconnect (void *cls);
156 
157 
164 static void
165 error_handler (void *cls,
166  enum GNUNET_MQ_Error error)
167 {
168  struct GNUNET_CADET_GetPath *gp = cls;
169 
170  GNUNET_MQ_destroy (gp->mq);
171  gp->mq = NULL;
175  &reconnect,
176  gp);
177 }
178 
179 
185 static void
186 reconnect (void *cls)
187 {
188  struct GNUNET_CADET_GetPath *gp = cls;
190  GNUNET_MQ_hd_var_size (get_path,
193  gp),
194  GNUNET_MQ_hd_fixed_size (get_path_end,
196  struct GNUNET_MessageHeader,
197  gp),
199  };
201  struct GNUNET_MQ_Envelope *env;
202 
203  gp->reconnect_task = NULL;
204  gp->mq = GNUNET_CLIENT_connect (gp->cfg,
205  "cadet",
206  handlers,
207  &error_handler,
208  gp);
209  if (NULL == gp->mq)
210  return;
211  env = GNUNET_MQ_msg (msg,
213  msg->peer = gp->id;
214  GNUNET_MQ_send (gp->mq,
215  env);
216 }
217 
218 
219 struct GNUNET_CADET_GetPath *
221  const struct GNUNET_PeerIdentity *id,
222  GNUNET_CADET_PathCB callback,
223  void *callback_cls)
224 {
225  struct GNUNET_CADET_GetPath *gp;
226 
227  if (NULL == callback)
228  {
229  GNUNET_break (0);
230  return NULL;
231  }
232  gp = GNUNET_new (struct GNUNET_CADET_GetPath);
233  gp->path_cb = callback;
234  gp->path_cb_cls = callback_cls;
235  gp->cfg = cfg;
236  gp->id = *id;
237  reconnect (gp);
238  if (NULL == gp->mq)
239  {
240  GNUNET_free (gp);
241  return NULL;
242  }
243  return gp;
244 }
245 
246 
253 void *
255 {
256  void *ret = gp->path_cb_cls;
257 
258  if (NULL != gp->mq)
259  GNUNET_MQ_destroy (gp->mq);
260  if (NULL != gp->reconnect_task)
262  GNUNET_free (gp);
263  return ret;
264 }
265 
266 
267 /* end of cadet_api_get_path.c */
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static void error_handler(void *cls, enum GNUNET_MQ_Error error)
Function called on connection trouble.
static void reconnect(void *cls)
Reconnect to the service and try again.
static void handle_get_path(void *cls, const struct GNUNET_CADET_LocalInfoPath *message)
Process a local peer info reply, pass info to the user.
static int check_get_path(void *cls, const struct GNUNET_CADET_LocalInfoPath *message)
Check that message received from CADET service is well-formed.
static void handle_get_path_end(void *cls, const struct GNUNET_MessageHeader *message)
Process a local peer info reply, pass info to the user.
P2P messages used by CADET.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
static struct GNUNET_IDENTITY_Handle * id
Handle to identity service.
CADET service; establish channels to distant peers.
struct GNUNET_CADET_GetPath * GNUNET_CADET_get_path(const struct GNUNET_CONFIGURATION_Handle *cfg, const struct GNUNET_PeerIdentity *id, GNUNET_CADET_PathCB callback, void *callback_cls)
Request information about a peer known to the running cadet peer.
void(* GNUNET_CADET_PathCB)(void *cls, const struct GNUNET_CADET_PeerPathDetail *ppd)
Method called to retrieve information about a specific path known to the service.
void * GNUNET_CADET_get_path_cancel(struct GNUNET_CADET_GetPath *gp)
Cancel gp operation.
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:1057
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
GNUNET_MQ_Error
Error codes for the queue.
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:304
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:77
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:683
#define GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH_END
End of local information of service about a specific path.
#define GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_PATH
Request local information of service about paths to specific peer.
#define GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH
Local information of service about a specific path.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:944
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:1241
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,...
Definition: time.c:830
#define GNUNET_TIME_UNIT_MINUTES
One minute.
Operation handle.
struct GNUNET_TIME_Relative backoff
Backoff for reconnect attempts.
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we use.
struct GNUNET_SCHEDULER_Task * reconnect_task
Task to reconnect.
GNUNET_CADET_PathCB path_cb
Monitor callback.
struct GNUNET_MQ_Handle * mq
Message queue to talk to CADET service.
void * path_cb_cls
Closure for path_cb.
struct GNUNET_PeerIdentity id
Peer we want information about.
Message to inform the client about one of the paths known to the service.
Definition: cadet.h:407
uint32_t off
Offset of the peer that was requested.
Definition: cadet.h:416
struct GNUNET_MessageHeader header
Type: GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH.
Definition: cadet.h:411
Detailed information we return per peer.
unsigned int path_length
Number of entries on the path.
const struct GNUNET_PeerIdentity * path
Array of PEER_IDs representing all paths to reach the peer.
unsigned int target_offset
Offset of the target peer on the path.
struct GNUNET_PeerIdentity peer
Peer this is about.
Message to inform the client about channels in the service.
Definition: cadet.h:344
Handle to a message queue.
Definition: mq.c:87
Message handler for a specific message type.
Header for all communications.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
The identity of the host (wraps the signing key of the peer).
Entry in list of pending tasks.
Definition: scheduler.c:136
Time for relative time used by GNUnet, in microseconds.