GNUnet  0.11.x
gnunet-service-messenger_tunnel.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2020--2021 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  */
27 
33 #include "messenger_api_util.h"
34 
37 {
38  GNUNET_assert((room) && (door));
39 
41 
42  tunnel->room = room;
43  tunnel->channel = NULL;
44 
45  tunnel->peer = GNUNET_PEER_intern (door);
46 
47  tunnel->messenger_version = 0;
48 
49  tunnel->peer_message = NULL;
50 
51  init_message_state(&(tunnel->state));
52 
53  return tunnel;
54 }
55 
56 void
58 {
59  GNUNET_assert(tunnel);
60 
61  if (tunnel->channel)
63 
64  GNUNET_PEER_change_rc (tunnel->peer, -1);
65 
66  if (tunnel->peer_message)
67  GNUNET_free(tunnel->peer_message);
68 
69  clear_message_state(&(tunnel->state));
70 
71  GNUNET_free(tunnel);
72 }
73 
74 void
76 {
77  GNUNET_assert(tunnel);
78 
79  if (tunnel->channel)
81 
82  tunnel->channel = channel;
83 }
84 
85 extern void
87 
88 void
90 {
91  struct GNUNET_MESSENGER_SrvTunnel *tunnel = cls;
92 
93  if (tunnel)
94  {
95  tunnel->channel = NULL;
96 
97  callback_room_disconnect (tunnel->room, cls);
98  }
99 }
100 
101 extern int
103  struct GNUNET_MESSENGER_Message *message, struct GNUNET_HashCode *hash);
104 
105 int
106 check_tunnel_message (void *cls, const struct GNUNET_MessageHeader *header)
107 {
108  struct GNUNET_MESSENGER_SrvTunnel *tunnel = cls;
109 
110  if (!tunnel)
111  return GNUNET_SYSERR;
112 
113  const uint16_t length = ntohs (header->size) - sizeof(*header);
114  const char *buffer = (const char*) &header[1];
115 
116  struct GNUNET_MESSENGER_Message message;
117 
119  {
120  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Tunnel error: Message too short! (%d)\n", length);
121  return GNUNET_SYSERR;
122  }
123 
124  uint16_t padding = 0;
125 
126  if (GNUNET_YES != decode_message (&message, length, buffer, GNUNET_YES, &padding))
127  {
128  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Tunnel error: Decoding failed!\n");
129  return GNUNET_SYSERR;
130  }
131 
132  struct GNUNET_HashCode hash;
133  hash_message (&message, length - padding, buffer, &hash);
134 
135  return callback_verify_room_message (tunnel->room, cls, &message, &hash);
136 }
137 
138 extern int
140  struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash);
141 
142 extern void
144  const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash);
145 
146 static void
148 {
149  struct GNUNET_MESSENGER_OperationStore *operation_store = get_room_operation_store(tunnel->room);
150 
151  const int requested = (GNUNET_MESSENGER_OP_REQUEST == get_store_operation_type(operation_store, hash)?
153  );
154 
155  struct GNUNET_MESSENGER_MessageStore *message_store = get_room_message_store(tunnel->room);
156 
157  const struct GNUNET_MESSENGER_Message *message = get_store_message(message_store, hash);
158 
159  if (message)
160  update_message_state(&(tunnel->state), requested, message, hash);
161 }
162 
163 void
165 {
166  struct GNUNET_MESSENGER_SrvTunnel *tunnel = cls;
167 
168  const uint16_t length = ntohs (header->size) - sizeof(*header);
169  const char *buffer = (const char*) &header[1];
170 
171  struct GNUNET_MESSENGER_Message message;
172  struct GNUNET_HashCode hash;
173 
174  uint16_t padding = 0;
175 
176  decode_message (&message, length, buffer, GNUNET_YES, &padding);
177  hash_message (&message, length - padding, buffer, &hash);
178 
179  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got message of kind: %s!\n",
181 
182  if (!tunnel)
183  return;
184 
185  const int new_message = update_room_message (
186  tunnel->room, copy_message (&message), &hash
187  );
188 
189  if (GNUNET_YES != new_message)
190  goto receive_done;
191 
192  update_tunnel_last_message (tunnel, &hash);
193 
194  int forward_message = GNUNET_YES;
195 
196  switch (message.header.kind)
197  {
199  forward_message = recv_message_info (tunnel->room, tunnel, &message, &hash);
200  break;
202  forward_message = recv_message_peer (tunnel->room, tunnel, &message, &hash);
203  break;
205  forward_message = recv_message_request (tunnel->room, tunnel, &message, &hash);
206  break;
207  default:
208  break;
209  }
210 
211  if (GNUNET_YES == forward_message)
212  {
213  forward_room_message (tunnel->room, tunnel, &message, &hash);
214  callback_room_handle_message (tunnel->room, NULL, &message, &hash);
215  }
216 
217 receive_done:
219 }
220 
221 int
223 {
224  GNUNET_assert(tunnel);
225 
226  if (tunnel->channel)
227  return GNUNET_NO;
228 
229  const struct GNUNET_PeerIdentity *door = GNUNET_PEER_resolve2 (tunnel->peer);
230 
231  struct GNUNET_CADET_Handle *cadet = get_room_cadet (tunnel->room);
232  const struct GNUNET_HashCode *key = get_room_key (tunnel->room);
233 
235  struct GNUNET_MessageHeader, NULL),
237 
238  tunnel->channel = GNUNET_CADET_channel_create (cadet, tunnel, door, key, NULL, callback_tunnel_disconnect, handlers);
239 
240  return GNUNET_YES;
241 }
242 
243 void
245 {
246  GNUNET_assert(tunnel);
247 
248  if (tunnel->channel)
249  {
251 
252  tunnel->channel = NULL;
253  }
254 }
255 
256 int
258 {
259  GNUNET_assert(tunnel);
260 
261  return (tunnel->channel ? GNUNET_YES : GNUNET_NO);
262 }
263 
265 {
267  struct GNUNET_HashCode hash;
268 };
269 
270 static void
272 {
273  struct GNUNET_MESSENGER_MessageSent *sent = cls;
274 
275  if (sent->tunnel)
276  update_tunnel_last_message (sent->tunnel, &(sent->hash));
277 
278  GNUNET_free(sent);
279 }
280 
281 void
283  const struct GNUNET_HashCode *hash)
284 {
285  GNUNET_assert((tunnel) && (env) && (hash));
286 
287  struct GNUNET_MQ_Handle *mq = GNUNET_CADET_get_mq (tunnel->channel);
288 
290 
291  GNUNET_memcpy(&(sent->hash), hash, sizeof(struct GNUNET_HashCode));
292 
293  sent->tunnel = tunnel;
294 
296  GNUNET_MQ_send (mq, env);
297 }
298 
299 int
301 {
302  GNUNET_assert((tunnel) && (handle));
303 
304  if (!message)
305  return GNUNET_NO;
306 
307  struct GNUNET_HashCode hash;
309  tunnel->room, (struct GNUNET_MESSENGER_SrvHandle*) handle,
311  );
312 
313  destroy_message(message);
314 
315  if (!env)
316  return GNUNET_NO;
317 
318  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending tunnel message: %s\n",
319  GNUNET_h2s(&hash));
320 
321  send_tunnel_envelope (tunnel, env, &hash);
322  return GNUNET_YES;
323 }
324 
325 void
327  const struct GNUNET_HashCode *hash)
328 {
329  GNUNET_assert((tunnel) && (message) && (hash));
330 
331  struct GNUNET_MESSENGER_Message *copy = copy_message(message);
333 
334  destroy_message(copy);
335 
336  if (!env)
337  return;
338 
339  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Forwarding tunnel message: %s\n",
340  GNUNET_h2s(hash));
341 
342  send_tunnel_envelope (tunnel, env, hash);
343 }
344 
345 const struct GNUNET_HashCode*
347 {
348  GNUNET_assert(tunnel);
349 
350  return tunnel->peer_message;
351 }
352 
353 void
355 {
356  GNUNET_assert(tunnel);
357 
358  GNUNET_PEER_resolve(tunnel->peer, peer);
359 }
360 
361 uint32_t
363 {
364  GNUNET_assert(tunnel);
365 
366  return tunnel->messenger_version;
367 }
368 
369 int
371 {
372  GNUNET_assert(tunnel);
373 
374  if (version != GNUNET_MESSENGER_VERSION)
375  return GNUNET_SYSERR;
376 
377  if (version > tunnel->messenger_version)
378  tunnel->messenger_version = version;
379 
380  return GNUNET_OK;
381 }
const struct GNUNET_PeerIdentity * GNUNET_PEER_resolve2(GNUNET_PEER_Id id)
Convert an interned PID to a normal peer identity.
Definition: peer.c:245
static struct GNUNET_CADET_Handle * cadet
Handle for cadet.
struct GNUNET_MESSENGER_MessageHeader header
Header.
struct GNUNET_MESSENGER_MessageStore * get_room_message_store(struct GNUNET_MESSENGER_SrvRoom *room)
Returns the used message store of a given room.
void destroy_message(struct GNUNET_MESSENGER_Message *message)
Destroys a message and frees its memory fully.
void delayed_disconnect_channel(struct GNUNET_CADET_Channel *channel)
Starts an urgent task to close a CADET channel asynchronously.
void init_message_state(struct GNUNET_MESSENGER_MessageState *state)
Opaque handle to the service.
Definition: cadet_api.c:38
void handle_tunnel_message(void *cls, const struct GNUNET_MessageHeader *header)
GNUnet MESSENGER service.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
int decode_message(struct GNUNET_MESSENGER_Message *message, uint16_t length, const char *buffer, int include_signature, uint16_t *padding)
Decodes a message from a given buffer of a maximal length in bytes.
void callback_tunnel_disconnect(void *cls, const struct GNUNET_CADET_Channel *channel)
int check_tunnel_message(void *cls, const struct GNUNET_MessageHeader *header)
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
int connect_tunnel(struct GNUNET_MESSENGER_SrvTunnel *tunnel)
Tries to connect a tunnel by creating a new CADET channel and binding it.
Opaque handle to a channel.
Definition: cadet.h:116
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_MESSAGE_TYPE_CADET_CLI
Traffic (net-cat style) used by the Command Line Interface.
#define GNUNET_MESSENGER_VERSION
Version number of GNUnet Messenger API.
GNUnet MESSENGER service.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
void GNUNET_PEER_resolve(GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
Convert an interned PID to a normal peer identity.
Definition: peer.c:225
int recv_message_info(struct GNUNET_MESSENGER_SrvRoom *room, struct GNUNET_MESSENGER_SrvTunnel *tunnel, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
Handles a received info message to change the current member id to the one generated by the host conn...
struct GNUNET_CADET_Handle * get_room_cadet(struct GNUNET_MESSENGER_SrvRoom *room)
Returns the CADET handle from a rooms service.
uint32_t get_tunnel_messenger_version(const struct GNUNET_MESSENGER_SrvTunnel *tunnel)
Returns the current messenger version the peer connected via a given tunnel has reported to be using ...
const struct GNUNET_HashCode * get_tunnel_peer_message(const struct GNUNET_MESSENGER_SrvTunnel *tunnel)
Returns the hash of the latest peer message published through a given tunnel and matching the tunnels...
int is_tunnel_connected(const struct GNUNET_MESSENGER_SrvTunnel *tunnel)
Returns the status of a currently bound channel of a tunnel.
struct GNUNET_MQ_Envelope * pack_message(struct GNUNET_MESSENGER_Message *message, struct GNUNET_HashCode *hash, const struct GNUNET_MESSENGER_Ego *ego, int mode)
Encodes the message to pack it into a newly allocated envelope if mode is equal to GNUNET_MESSENGER_P...
struct GNUNET_MESSENGER_OperationStore * get_room_operation_store(struct GNUNET_MESSENGER_SrvRoom *room)
Returns the used operation store of a given room.
void GNUNET_MQ_notify_sent(struct GNUNET_MQ_Envelope *ev, GNUNET_SCHEDULER_TaskCallback cb, void *cb_cls)
Call a callback once the envelope has been sent, that is, sending it can not be canceled anymore...
Definition: mq.c:787
uint16_t get_message_kind_size(enum GNUNET_MESSENGER_MessageKind kind)
Returns the minimal size in bytes to encode a message of a specific kind.
int callback_verify_room_message(struct GNUNET_MESSENGER_SrvRoom *room, void *cls, struct GNUNET_MESSENGER_Message *message, struct GNUNET_HashCode *hash)
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
void get_tunnel_peer_identity(const struct GNUNET_MESSENGER_SrvTunnel *tunnel, struct GNUNET_PeerIdentity *peer)
Writes the peer identity of the peer connected via tunnel to this peer into the peer parameter...
int update_room_message(struct GNUNET_MESSENGER_SrvRoom *room, struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
struct GNUNET_MQ_Envelope * pack_room_message(const struct GNUNET_MESSENGER_SrvRoom *room, const struct GNUNET_MESSENGER_SrvHandle *handle, struct GNUNET_MESSENGER_Message *message, struct GNUNET_HashCode *hash, int mode)
Packs a message depending on the selected mode into a newly allocated envelope.
const char * GNUNET_MESSENGER_name_of_kind(enum GNUNET_MESSENGER_MessageKind kind)
Get the name of a message kind.
Definition: messenger_api.c:35
struct GNUNET_MESSENGER_SrvRoom * room
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
struct GNUNET_MESSENGER_SrvTunnel * create_tunnel(struct GNUNET_MESSENGER_SrvRoom *room, const struct GNUNET_PeerIdentity *door)
Creates and allocates a tunnel of a room to a specific peer identity (called door).
void destroy_tunnel(struct GNUNET_MESSENGER_SrvTunnel *tunnel)
Destroys a tunnel and frees its memory fully.
A 512-bit hashcode.
int recv_message_request(struct GNUNET_MESSENGER_SrvRoom *room, struct GNUNET_MESSENGER_SrvTunnel *tunnel, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
Handles a received request message by checking for the requested message and forwarding it back if th...
const struct GNUNET_MESSENGER_Message * get_store_message(struct GNUNET_MESSENGER_MessageStore *store, const struct GNUNET_HashCode *hash)
Returns the message from a message store matching a given hash.
Message handler for a specific message type.
struct GNUNET_MESSENGER_SrvTunnel * tunnel
enum GNUNET_MESSENGER_OperationType get_store_operation_type(const struct GNUNET_MESSENGER_OperationStore *store, const struct GNUNET_HashCode *hash)
Returns the type of the active operation under a given hash in a specific operation store...
messenger api: client implementation of GNUnet MESSENGER service
void callback_room_handle_message(struct GNUNET_MESSENGER_SrvRoom *room, struct GNUNET_MESSENGER_SrvHandle *handle, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
void clear_message_state(struct GNUNET_MESSENGER_MessageState *state)
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
struct GNUNET_HashCode key
The key used in the DHT.
GNUnet MESSENGER service.
GNUnet MESSENGER service.
struct GNUNET_MESSENGER_Message * copy_message(const struct GNUNET_MESSENGER_Message *message)
Creates and allocates a copy of a given message.
#define GNUNET_MESSENGER_PACK_MODE_ENVELOPE
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
void callback_room_disconnect(struct GNUNET_MESSENGER_SrvRoom *room, void *cls)
void hash_message(const struct GNUNET_MESSENGER_Message *message, uint16_t length, const char *buffer, struct GNUNET_HashCode *hash)
Calculates a hash of a given buffer with a length in bytes from a message.
struct GNUNET_CADET_Channel * channel
int recv_message_peer(struct GNUNET_MESSENGER_SrvRoom *room, struct GNUNET_MESSENGER_SrvTunnel *tunnel, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
Handles a received peer message to link it to its origin tunnel if the peer identity matches...
Handle to a message queue.
Definition: mq.c:85
void forward_tunnel_message(struct GNUNET_MESSENGER_SrvTunnel *tunnel, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
Forwards a given message with a known hash through a tunnel.
struct GNUNET_MESSENGER_MessageState state
The identity of the host (wraps the signing key of the peer).
void GNUNET_CADET_receive_done(struct GNUNET_CADET_Channel *channel)
Send an ack on the channel to confirm the processing of a message.
Definition: cadet_api.c:888
int update_tunnel_messenger_version(struct GNUNET_MESSENGER_SrvTunnel *tunnel, uint32_t version)
Updates the messenger version of the tunnel to a given version if it is compatible to the running pee...
struct GNUNET_CADET_Channel * GNUNET_CADET_channel_create(struct GNUNET_CADET_Handle *h, void *channel_cls, const struct GNUNET_PeerIdentity *destination, const struct GNUNET_HashCode *port, GNUNET_CADET_WindowSizeEventHandler window_changes, GNUNET_CADET_DisconnectEventHandler disconnects, const struct GNUNET_MQ_MessageHandler *handlers)
Create a new channel towards a remote peer.
Definition: cadet_api.c:1031
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
void GNUNET_PEER_change_rc(GNUNET_PEER_Id id, int delta)
Change the reference counter of an interned PID.
Definition: peer.c:197
#define GNUNET_log(kind,...)
void update_message_state(struct GNUNET_MESSENGER_MessageState *state, int requested, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
int send_tunnel_message(struct GNUNET_MESSENGER_SrvTunnel *tunnel, void *handle, struct GNUNET_MESSENGER_Message *message)
Sends a message by packing it automatically into an envelope and passing it through the tunnel...
void bind_tunnel(struct GNUNET_MESSENGER_SrvTunnel *tunnel, struct GNUNET_CADET_Channel *channel)
Binds a CADET channel to a tunnel and replaces its channel the tunnel is currently bound to if necess...
const struct GNUNET_HashCode * get_room_key(const struct GNUNET_MESSENGER_SrvRoom *room)
Returns the shared secret you need to access a room.
Header for all communications.
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:355
void disconnect_tunnel(struct GNUNET_MESSENGER_SrvTunnel *tunnel)
Disconnects and unbinds a channel from a tunnel.
void send_tunnel_envelope(struct GNUNET_MESSENGER_SrvTunnel *tunnel, struct GNUNET_MQ_Envelope *env, const struct GNUNET_HashCode *hash)
Sends an envelope containing a message with a given hash through a tunnel.
static void callback_tunnel_sent(void *cls)
void GNUNET_CADET_channel_destroy(struct GNUNET_CADET_Channel *channel)
Destroy an existing channel.
Definition: cadet_api.c:837
static void update_tunnel_last_message(struct GNUNET_MESSENGER_SrvTunnel *tunnel, const struct GNUNET_HashCode *hash)
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
void forward_room_message(struct GNUNET_MESSENGER_SrvRoom *room, struct GNUNET_MESSENGER_SrvTunnel *tunnel, struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
Forwards a message with a given hash to a specific tunnel inside of a room.
GNUNET_PEER_Id GNUNET_PEER_intern(const struct GNUNET_PeerIdentity *pid)
Intern an peer identity.
Definition: peer.c:107
#define GNUNET_free(ptr)
Wrapper around free.
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
struct GNUNET_MQ_Handle * GNUNET_CADET_get_mq(const struct GNUNET_CADET_Channel *channel)
Obtain the message queue for a connected peer.
Definition: cadet_api.c:1082