starting with #5385 in earnest
[oweals/gnunet.git] / src / cadet / cadet_api_list_peers.c
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 */
20 /**
21  * @file cadet/cadet_api.c
22  * @brief cadet api: client implementation of cadet service
23  * @author Bartlomiej Polot
24  * @author Christian Grothoff
25  */
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
34
35 /**
36  * Ugly legacy hack.
37  */
38 struct GNUNET_CADET_PeersLister
39 {
40
41   /**
42    * Monitor callback
43    */
44   GNUNET_CADET_PeersCB peers_cb;
45
46   /**
47    * Info callback closure for @c info_cb.
48    */
49   void *peers_cb_cls;
50 };
51
52
53 /**
54  * Send message of @a type to CADET service of @a h
55  *
56  * @param h handle to CADET service
57  * @param type message type of trivial information request to send
58  */
59 static void
60 send_info_request (struct GNUNET_CADET_Handle *h,
61                    uint16_t type)
62 {
63   struct GNUNET_MessageHeader *msg;
64   struct GNUNET_MQ_Envelope *env;
65
66   env = GNUNET_MQ_msg (msg,
67                        type);
68   GNUNET_MQ_send (h->mq,
69                   env);
70 }
71
72
73 /**
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.
77  *
78  * WARNING: unstable API, likely to change in the future!
79  *
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
84  */
85 int
86 GNUNET_CADET_list_peers (struct GNUNET_CADET_Handle *h,
87                          GNUNET_CADET_PeersCB callback,
88                          void *callback_cls)
89 {
90   if (NULL != h->info_cb.peers_cb)
91   {
92     GNUNET_break (0);
93     return GNUNET_SYSERR;
94   }
95   send_info_request (h,
96                      GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
97   h->info_cb.peers_cb = callback;
98   h->info_cls = callback_cls;
99   return GNUNET_OK;
100 }
101
102
103 /**
104  * Cancel a peer info request. The callback will not be called (anymore).
105  *
106  * WARNING: unstable API, likely to change in the future!
107  *
108  * @param h Cadet handle.
109  * @return Closure given to GNUNET_CADET_get_peers().
110  */
111 void *
112 GNUNET_CADET_list_peers_cancel (struct GNUNET_CADET_Handle *h)
113 {
114   void *cls = h->info_cls;
115
116   h->info_cb.peers_cb = NULL;
117   h->info_cls = NULL;
118   return cls;
119 }
120
121
122 /**
123  * Check that message received from CADET service is well-formed.
124  *
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
129  */
130 static int
131 check_get_peers (void *cls,
132                  const struct GNUNET_MessageHeader *message)
133 {
134   size_t esize;
135
136   (void) cls;
137   esize = ntohs (message->size);
138   if (sizeof (struct GNUNET_CADET_LocalInfoPeer) == esize)
139     return GNUNET_OK;
140   if (sizeof (struct GNUNET_MessageHeader) == esize)
141     return GNUNET_OK;
142   return GNUNET_SYSERR;
143 }
144
145
146 /**
147  * Process a local reply about info on all tunnels, pass info to the user.
148  *
149  * @param cls Closure (Cadet handle).
150  * @param msg Message itself.
151  */
152 static void
153 handle_get_peers (void *cls,
154                   const struct GNUNET_MessageHeader *msg)
155 {
156   struct GNUNET_CADET_Handle *h = cls;
157   const struct GNUNET_CADET_LocalInfoPeer *info =
158     (const struct GNUNET_CADET_LocalInfoPeer *) msg;
159
160   if (NULL == h->info_cb.peers_cb)
161     return;
162   if (sizeof (struct GNUNET_CADET_LocalInfoPeer) == ntohs (msg->size))
163     h->info_cb.peers_cb (h->info_cls,
164                          &info->destination,
165                          (int) ntohs (info->tunnel),
166                          (unsigned int) ntohs (info->paths),
167                          0);
168   else
169     h->info_cb.peers_cb (h->info_cls,
170                          NULL,
171                          0,
172                          0,
173                          0);
174 }
175
176
177
178
179