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 
42 
46  void *path_cb_cls;
47 
52 
57 
62 
67 
72 };
73 
74 
83 static int
84 check_get_path(void *cls,
85  const struct GNUNET_CADET_LocalInfoPath *message)
86 {
87  size_t msize = sizeof(struct GNUNET_CADET_LocalInfoPath);
88  size_t esize;
89 
90  (void)cls;
91  esize = ntohs(message->header.size);
92  if (esize < msize)
93  {
94  GNUNET_break(0);
95  return GNUNET_SYSERR;
96  }
97  if (0 != ((esize - msize) % sizeof(struct GNUNET_PeerIdentity)))
98  {
99  GNUNET_break(0);
100  return GNUNET_SYSERR;
101  }
102  return GNUNET_OK;
103 }
104 
105 
112 static void
113 handle_get_path(void *cls,
114  const struct GNUNET_CADET_LocalInfoPath *message)
115 {
116  struct GNUNET_CADET_GetPath *gp = cls;
117  struct GNUNET_CADET_PeerPathDetail ppd;
118 
119  ppd.peer = gp->id;
120  ppd.path = (const struct GNUNET_PeerIdentity *)&message[1];
121  ppd.target_offset = ntohl(message->off);
122  ppd.path_length = (ntohs(message->header.size) - sizeof(*message))
123  / sizeof(struct GNUNET_PeerIdentity);
124  gp->path_cb(gp->path_cb_cls,
125  &ppd);
126 }
127 
128 
135 static void
137  const struct GNUNET_MessageHeader *message)
138 {
139  struct GNUNET_CADET_GetPath *gp = cls;
140 
141  (void)message;
142  gp->path_cb(gp->path_cb_cls,
143  NULL);
145 }
146 
147 
153 static void
154 reconnect(void *cls);
155 
156 
163 static void
164 error_handler(void *cls,
165  enum GNUNET_MQ_Error error)
166 {
167  struct GNUNET_CADET_GetPath *gp = cls;
168 
169  GNUNET_MQ_destroy(gp->mq);
170  gp->mq = NULL;
174  &reconnect,
175  gp);
176 }
177 
178 
184 static void
185 reconnect(void *cls)
186 {
187  struct GNUNET_CADET_GetPath *gp = cls;
188  struct GNUNET_MQ_MessageHandler handlers[] = {
189  GNUNET_MQ_hd_var_size(get_path,
192  gp),
193  GNUNET_MQ_hd_fixed_size(get_path_end,
195  struct GNUNET_MessageHeader,
196  gp),
198  };
200  struct GNUNET_MQ_Envelope *env;
201 
202  gp->reconnect_task = NULL;
203  gp->mq = GNUNET_CLIENT_connect(gp->cfg,
204  "cadet",
205  handlers,
206  &error_handler,
207  gp);
208  if (NULL == gp->mq)
209  return;
210  env = GNUNET_MQ_msg(msg,
212  msg->peer = gp->id;
213  GNUNET_MQ_send(gp->mq,
214  env);
215 }
216 
217 
227 struct GNUNET_CADET_GetPath *
229  const struct GNUNET_PeerIdentity *id,
230  GNUNET_CADET_PathCB callback,
231  void *callback_cls)
232 {
233  struct GNUNET_CADET_GetPath *gp;
234 
235  if (NULL == callback)
236  {
237  GNUNET_break(0);
238  return NULL;
239  }
240  gp = GNUNET_new(struct GNUNET_CADET_GetPath);
241  gp->path_cb = callback;
242  gp->path_cb_cls = callback_cls;
243  gp->cfg = cfg;
244  gp->id = *id;
245  reconnect(gp);
246  if (NULL == gp->mq)
247  {
248  GNUNET_free(gp);
249  return NULL;
250  }
251  return gp;
252 }
253 
254 
261 void *
263 {
264  void *ret = gp->path_cb_cls;
265 
266  if (NULL != gp->mq)
267  GNUNET_MQ_destroy(gp->mq);
268  if (NULL != gp->reconnect_task)
270  GNUNET_free(gp);
271  return ret;
272 }
273 
274 
275 /* 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:900
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:75
Message to inform the client about channels in the service.
Definition: cadet.h:252
#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:266
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:316
Message to inform the client about one of the paths known to the service.
Definition: cadet.h:312
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
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:321
#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:76
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:84
The identity of the host (wraps the signing key of the peer).
Detailed information we return per peer.
configuration data
Definition: configuration.c:83
#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:131
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: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
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:758
#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:956