GNUnet  0.10.x
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 {
39 
44 
48  void *path_cb_cls;
49 
54 
59 
64 
69 
74 
75 };
76 
77 
86 static int
87 check_get_path (void *cls,
88  const struct GNUNET_CADET_LocalInfoPath *message)
89 {
90  size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPath);
91  size_t esize;
92 
93  (void) cls;
94  esize = ntohs (message->header.size);
95  if (esize < msize)
96  {
97  GNUNET_break (0);
98  return GNUNET_SYSERR;
99  }
100  if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
101  {
102  GNUNET_break (0);
103  return GNUNET_SYSERR;
104  }
105  return GNUNET_OK;
106 }
107 
108 
115 static void
116 handle_get_path (void *cls,
117  const struct GNUNET_CADET_LocalInfoPath *message)
118 {
119  struct GNUNET_CADET_GetPath *gp = cls;
120  struct GNUNET_CADET_PeerPathDetail ppd;
121 
122  ppd.peer = gp->id;
123  ppd.path = (const struct GNUNET_PeerIdentity *) &message[1];
124  ppd.target_offset = ntohl (message->off);
125  ppd.path_length = (ntohs (message->header.size) - sizeof (*message))
126  / sizeof (struct GNUNET_PeerIdentity);
127  gp->path_cb (gp->path_cb_cls,
128  &ppd);
129 }
130 
131 
138 static void
140  const struct GNUNET_MessageHeader *message)
141 {
142  struct GNUNET_CADET_GetPath *gp = cls;
143 
144  (void) message;
145  gp->path_cb (gp->path_cb_cls,
146  NULL);
148 }
149 
150 
156 static void
157 reconnect (void *cls);
158 
159 
166 static void
167 error_handler (void *cls,
168  enum GNUNET_MQ_Error error)
169 {
170  struct GNUNET_CADET_GetPath *gp = cls;
171 
172  GNUNET_MQ_destroy (gp->mq);
173  gp->mq = NULL;
177  &reconnect,
178  gp);
179 }
180 
181 
187 static void
188 reconnect (void *cls)
189 {
190  struct GNUNET_CADET_GetPath *gp = cls;
191  struct GNUNET_MQ_MessageHandler handlers[] = {
192  GNUNET_MQ_hd_var_size (get_path,
195  gp),
196  GNUNET_MQ_hd_fixed_size (get_path_end,
198  struct GNUNET_MessageHeader,
199  gp),
201  };
203  struct GNUNET_MQ_Envelope *env;
204 
205  gp->reconnect_task = NULL;
206  gp->mq = GNUNET_CLIENT_connect (gp->cfg,
207  "cadet",
208  handlers,
209  &error_handler,
210  gp);
211  if (NULL == gp->mq)
212  return;
213  env = GNUNET_MQ_msg (msg,
215  msg->peer = gp->id;
216  GNUNET_MQ_send (gp->mq,
217  env);
218 }
219 
220 
230 struct GNUNET_CADET_GetPath *
232  const struct GNUNET_PeerIdentity *id,
233  GNUNET_CADET_PathCB callback,
234  void *callback_cls)
235 {
236  struct GNUNET_CADET_GetPath *gp;
237 
238  if (NULL == callback)
239  {
240  GNUNET_break (0);
241  return NULL;
242  }
243  gp = GNUNET_new (struct GNUNET_CADET_GetPath);
244  gp->path_cb = callback;
245  gp->path_cb_cls = callback_cls;
246  gp->cfg = cfg;
247  gp->id = *id;
248  reconnect (gp);
249  if (NULL == gp->mq)
250  {
251  GNUNET_free (gp);
252  return NULL;
253  }
254  return gp;
255 }
256 
257 
264 void *
266 {
267  void *ret = gp->path_cb_cls;
268 
269  if (NULL != gp->mq)
270  GNUNET_MQ_destroy (gp->mq);
271  if (NULL != gp->reconnect_task)
273  GNUNET_free (gp);
274  return ret;
275 }
276 
277 
278 /* end of cadet_api_get_path.c */
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. ...
struct GNUNET_TIME_Relative backoff
Backoff for reconnect attempts.
#define GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH
Local information of service about a specific path.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
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.
#define GNUNET_TIME_UNIT_MINUTES
One minute.
#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_OK
Named constants for return values.
Definition: gnunet_common.h:78
Message to inform the client about channels in the service.
Definition: cadet.h:259
#define GNUNET_new(type)
Allocate a struct or union of the given type.
unsigned int path_length
Number of entries on the path.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
struct GNUNET_PeerIdentity peer
ID of the destination of the channel (can be local peer).
Definition: cadet.h:274
static int ret
Final status code.
Definition: gnunet-arm.c:89
struct GNUNET_SCHEDULER_Task * reconnect_task
Task to reconnect.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_MessageHeader header
Type: GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH.
Definition: cadet.h:327
Message to inform the client about one of the paths known to the service.
Definition: cadet.h:322
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:1246
void * path_cb_cls
Closure for path_cb.
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 paths known to the running cadet peer.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we use.
GNUNET_CADET_PathCB path_cb
Monitor callback.
uint32_t off
Offset of the peer that was requested.
Definition: cadet.h:332
#define GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_PATH
Request local information of service about paths to specific peer.
void * GNUNET_CADET_get_path_cancel(struct GNUNET_CADET_GetPath *gp)
Cancel gp operation.
struct GNUNET_MQ_Handle * mq
Message queue to talk to CADET service.
unsigned int target_offset
Offset of the target peer on the path.
Message handler for a specific message type.
Operation handle.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static void reconnect(void *cls)
Reconnect to the service and try again.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
struct GNUNET_PeerIdentity peer
Peer this is about.
struct GNUNET_PeerIdentity id
Peer we want information about.
P2P messages used by CADET.
const struct GNUNET_PeerIdentity * path
Array of PEER_IDs representing all paths to reach the peer.
Handle to a message queue.
Definition: mq.c:85
The identity of the host (wraps the signing key of the peer).
Detailed information we return per peer.
configuration data
Definition: configuration.c:85
#define GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH_END
End of local information of service about a specific path.
Entry in list of pending tasks.
Definition: scheduler.c:134
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 void error_handler(void *cls, enum GNUNET_MQ_Error error)
Function called on connection trouble.
Header for all communications.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
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
static void handle_get_path_end(void *cls, const struct GNUNET_MessageHeader *message)
Process a local peer info reply, pass info to the user.
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:759
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_free(ptr)
Wrapper around free.
static int check_get_path(void *cls, const struct GNUNET_CADET_LocalInfoPath *message)
Check that message received from CADET service is well-formed.
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:965