2 This file is part of GNUnet.
3 Copyright (C) 2011, 2017 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file cadet/cadet_api.c
22 * @brief cadet api: client implementation of cadet service
23 * @author Bartlomiej Polot
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_cadet_service.h"
31 #include "cadet_protocol.h"
38 struct GNUNET_CADET_PeersLister
44 GNUNET_CADET_PeersCB peers_cb;
47 * Info callback closure for @c info_cb.
54 * Send message of @a type to CADET service of @a h
56 * @param h handle to CADET service
57 * @param type message type of trivial information request to send
60 send_info_request (struct GNUNET_CADET_Handle *h,
63 struct GNUNET_MessageHeader *msg;
64 struct GNUNET_MQ_Envelope *env;
66 env = GNUNET_MQ_msg (msg,
68 GNUNET_MQ_send (h->mq,
74 * Request information about peers known to the running cadet service.
75 * The callback will be called for every peer known to the service.
76 * Only one info request (of any kind) can be active at once.
78 * WARNING: unstable API, likely to change in the future!
80 * @param h Handle to the cadet peer.
81 * @param callback Function to call with the requested data.
82 * @param callback_cls Closure for @c callback.
83 * @return #GNUNET_OK / #GNUNET_SYSERR
86 GNUNET_CADET_list_peers (struct GNUNET_CADET_Handle *h,
87 GNUNET_CADET_PeersCB callback,
90 if (NULL != h->info_cb.peers_cb)
96 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
97 h->info_cb.peers_cb = callback;
98 h->info_cls = callback_cls;
104 * Cancel a peer info request. The callback will not be called (anymore).
106 * WARNING: unstable API, likely to change in the future!
108 * @param h Cadet handle.
109 * @return Closure given to GNUNET_CADET_get_peers().
112 GNUNET_CADET_list_peers_cancel (struct GNUNET_CADET_Handle *h)
114 void *cls = h->info_cls;
116 h->info_cb.peers_cb = NULL;
123 * Check that message received from CADET service is well-formed.
125 * @param cls the `struct GNUNET_CADET_Handle`
126 * @param message the message we got
127 * @return #GNUNET_OK if the message is well-formed,
128 * #GNUNET_SYSERR otherwise
131 check_get_peers (void *cls,
132 const struct GNUNET_MessageHeader *message)
137 esize = ntohs (message->size);
138 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) == esize)
140 if (sizeof (struct GNUNET_MessageHeader) == esize)
142 return GNUNET_SYSERR;
147 * Process a local reply about info on all tunnels, pass info to the user.
149 * @param cls Closure (Cadet handle).
150 * @param msg Message itself.
153 handle_get_peers (void *cls,
154 const struct GNUNET_MessageHeader *msg)
156 struct GNUNET_CADET_Handle *h = cls;
157 const struct GNUNET_CADET_LocalInfoPeer *info =
158 (const struct GNUNET_CADET_LocalInfoPeer *) msg;
160 if (NULL == h->info_cb.peers_cb)
162 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) == ntohs (msg->size))
163 h->info_cb.peers_cb (h->info_cls,
165 (int) ntohs (info->tunnel),
166 (unsigned int) ntohs (info->paths),
169 h->info_cb.peers_cb (h->info_cls,