GNUnet  0.11.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 {
43 
47  void *path_cb_cls;
48 
53 
58 
63 
68 
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;
189  struct GNUNET_MQ_MessageHandler handlers[] = {
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 
228 struct GNUNET_CADET_GetPath *
230  const struct GNUNET_PeerIdentity *id,
231  GNUNET_CADET_PathCB callback,
232  void *callback_cls)
233 {
234  struct GNUNET_CADET_GetPath *gp;
235 
236  if (NULL == callback)
237  {
238  GNUNET_break (0);
239  return NULL;
240  }
241  gp = GNUNET_new (struct GNUNET_CADET_GetPath);
242  gp->path_cb = callback;
243  gp->path_cb_cls = callback_cls;
244  gp->cfg = cfg;
245  gp->id = *id;
246  reconnect (gp);
247  if (NULL == gp->mq)
248  {
249  GNUNET_free (gp);
250  return NULL;
251  }
252  return gp;
253 }
254 
255 
262 void *
264 {
265  void *ret = gp->path_cb_cls;
266 
267  if (NULL != gp->mq)
268  GNUNET_MQ_destroy (gp->mq);
269  if (NULL != gp->reconnect_task)
271  GNUNET_free (gp);
272  return ret;
273 }
274 
275 
276 /* 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:1057
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)
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#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: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
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:1253
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: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: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:760
#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:966