GNUnet  0.11.x
cadet_api_get_channel.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 
38 {
43 
48 
53 
58 
63 
68 
73 };
74 
75 
84 static int
85 check_channel_info (void *cls,
86  const struct GNUNET_CADET_ChannelInfoMessage *message)
87 {
88  (void) cls;
89 
90  return GNUNET_OK;
91 }
92 
93 
100 static void
102  const struct GNUNET_CADET_ChannelInfoMessage *message)
103 {
104  struct GNUNET_CADET_ChannelMonitor *cm = cls;
106 
107  ci.root = message->root;
108  ci.dest = message->dest;
109  cm->channel_cb (cm->channel_cb_cls,
110  &ci);
112 }
113 
114 
121 static void
123  const struct GNUNET_MessageHeader *message)
124 {
125  struct GNUNET_CADET_ChannelMonitor *cm = cls;
126 
127  cm->channel_cb (cm->channel_cb_cls,
128  NULL);
130 }
131 
132 
138 static void
139 reconnect (void *cls);
140 
141 
148 static void
149 error_handler (void *cls,
150  enum GNUNET_MQ_Error error)
151 {
152  struct GNUNET_CADET_ChannelMonitor *cm = cls;
153 
154  GNUNET_MQ_destroy (cm->mq);
155  cm->mq = NULL;
159  &reconnect,
160  cm);
161 }
162 
163 
169 static void
170 reconnect (void *cls)
171 {
172  struct GNUNET_CADET_ChannelMonitor *cm = cls;
173  struct GNUNET_MQ_MessageHandler handlers[] = {
174  GNUNET_MQ_hd_fixed_size (channel_info_end,
176  struct GNUNET_MessageHeader,
177  cm),
178  GNUNET_MQ_hd_var_size (channel_info,
181  cm),
183  };
185  struct GNUNET_MQ_Envelope *env;
186 
187  cm->reconnect_task = NULL;
188  cm->mq = GNUNET_CLIENT_connect (cm->cfg,
189  "cadet",
190  handlers,
191  &error_handler,
192  cm);
193  if (NULL == cm->mq)
194  return;
195  env = GNUNET_MQ_msg (msg,
197  msg->target = cm->peer;
198  GNUNET_MQ_send (cm->mq,
199  env);
200 }
201 
202 
214  struct GNUNET_PeerIdentity *peer,
215  GNUNET_CADET_ChannelCB callback,
216  void *callback_cls)
217 {
218  struct GNUNET_CADET_ChannelMonitor *cm;
219 
220  if (NULL == callback)
221  {
222  GNUNET_break (0);
223  return NULL;
224  }
226  cm->channel_cb = callback;
227  cm->channel_cb_cls = callback_cls;
228  cm->cfg = cfg;
229  cm->peer = *peer;
230  reconnect (cm);
231  if (NULL == cm->mq)
232  {
233  GNUNET_free (cm);
234  return NULL;
235  }
236  return cm;
237 }
238 
239 
246 void *
248 {
249  void *ret = cm->channel_cb_cls;
250 
251  if (NULL != cm->mq)
252  GNUNET_MQ_destroy (cm->mq);
253  if (NULL != cm->reconnect_task)
255  GNUNET_free (cm);
256  return ret;
257 }
258 
259 
260 /* end of cadet_api_get_channel.c */
static void reconnect(void *cls)
Reconnect to the service and try again.
#define GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL
Local information of service about a specific channel.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_PeerIdentity root
Root of the channel.
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.
static int check_channel_info(void *cls, const struct GNUNET_CADET_ChannelInfoMessage *message)
Check that message received from CADET service is well-formed.
#define GNUNET_TIME_UNIT_MINUTES
One minute.
Internal details about a channel.
#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
struct GNUNET_CADET_ChannelMonitor * GNUNET_CADET_get_channel(const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_PeerIdentity *peer, GNUNET_CADET_ChannelCB callback, void *callback_cls)
Request information about a specific channel of the running cadet peer.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
void * channel_cb_cls
Info callback closure for channel_cb.
void(* GNUNET_CADET_ChannelCB)(void *cls, const struct GNUNET_CADET_ChannelInternals *info)
Method called to retrieve information about a specific channel the cadet peer is aware of...
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
Message to inform the client about channels in the service.
Definition: cadet.h:281
struct GNUNET_PeerIdentity dest
Destination of the channel.
Definition: cadet.h:296
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we use.
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
struct GNUNET_PeerIdentity peer
Peer we want information about.
static void handle_channel_info_end(void *cls, const struct GNUNET_MessageHeader *message)
Process a local peer info reply, pass info to the user.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
struct GNUNET_TIME_Relative backoff
Backoff for reconnect attempts.
GNUNET_CADET_ChannelCB channel_cb
Channel callback.
Message to as the service about information on a channel.
Definition: cadet.h:305
struct GNUNET_MQ_Handle * mq
Message queue to talk to CADET service.
#define GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_CHANNEL
Local information about all channels of service.
Message handler for a specific message type.
struct GNUNET_PeerIdentity target
Target of the channel.
Definition: cadet.h:315
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
P2P messages used by CADET.
void * GNUNET_CADET_get_channel_cancel(struct GNUNET_CADET_ChannelMonitor *cm)
Cancel a channel monitor request.
static void error_handler(void *cls, enum GNUNET_MQ_Error error)
Function called on connection trouble.
Handle to a message queue.
Definition: mq.c:85
The identity of the host (wraps the signing key of the peer).
configuration data
Definition: configuration.c:85
struct GNUNET_PeerIdentity root
Root of the channel.
Definition: cadet.h:291
Entry in list of pending tasks.
Definition: scheduler.c:134
Header for all communications.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
static void handle_channel_info(void *cls, const struct GNUNET_CADET_ChannelInfoMessage *message)
Process a local peer info reply, pass info to the user.
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
#define GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL_END
End of local information of service about channels.
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.
struct GNUNET_SCHEDULER_Task * reconnect_task
Task to reconnect.
struct GNUNET_PeerIdentity dest
Destination of the channel.
#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:966