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  const struct GNUNET_PeerIdentity *door)
38 {
39  GNUNET_assert((room) && (door));
40 
42 
43  tunnel->room = room;
44  tunnel->channel = NULL;
45 
46  tunnel->peer = GNUNET_PEER_intern (door);
47 
48  tunnel->messenger_version = 0;
49 
50  tunnel->peer_message = NULL;
51 
52  init_message_state(&(tunnel->state));
53 
54  return tunnel;
55 }
56 
57 void
59 {
60  GNUNET_assert(tunnel);
61 
62  if (tunnel->channel)
64 
65  GNUNET_PEER_change_rc (tunnel->peer, -1);
66 
67  if (tunnel->peer_message)
68  GNUNET_free(tunnel->peer_message);
69 
70  clear_message_state(&(tunnel->state));
71 
72  GNUNET_free(tunnel);
73 }
74 
75 void
78 {
79  GNUNET_assert(tunnel);
80 
81  if (tunnel->channel)
83 
84  tunnel->channel = channel;
85 }
86 
87 extern void
89  void *cls);
90 
91 void
93  const struct GNUNET_CADET_Channel *channel)
94 {
95  struct GNUNET_MESSENGER_SrvTunnel *tunnel = cls;
96 
97  if (tunnel)
98  {
99  tunnel->channel = NULL;
100 
101  callback_room_disconnect (tunnel->room, cls);
102  }
103 }
104 
105 extern int
107  void *cls,
108  struct GNUNET_MESSENGER_Message *message,
109  struct GNUNET_HashCode *hash);
110 
111 int
113  const struct GNUNET_MessageHeader *header)
114 {
115  struct GNUNET_MESSENGER_SrvTunnel *tunnel = cls;
116 
117  if (!tunnel)
118  return GNUNET_SYSERR;
119 
120  const uint16_t length = ntohs (header->size) - sizeof(*header);
121  const char *buffer = (const char*) &header[1];
122 
123  struct GNUNET_MESSENGER_Message message;
124 
126  {
127  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Tunnel error: Message too short! (%d)\n", length);
128  return GNUNET_SYSERR;
129  }
130 
131  uint16_t padding = 0;
132 
133  if (GNUNET_YES != decode_message (&message, length, buffer, GNUNET_YES, &padding))
134  {
135  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Tunnel error: Decoding failed!\n");
136  return GNUNET_SYSERR;
137  }
138 
139  struct GNUNET_HashCode hash;
140  hash_message (&message, length - padding, buffer, &hash);
141 
142  return callback_verify_room_message (tunnel->room, cls, &message, &hash);
143 }
144 
145 extern int
147  struct GNUNET_MESSENGER_Message *message,
148  const struct GNUNET_HashCode *hash);
149 
150 extern void
153  const struct GNUNET_MESSENGER_Message *message,
154  const struct GNUNET_HashCode *hash);
155 
156 static void
158  const struct GNUNET_HashCode *hash)
159 {
160  struct GNUNET_MESSENGER_OperationStore *operation_store = get_room_operation_store(tunnel->room);
161 
162  const int requested = (GNUNET_MESSENGER_OP_REQUEST == get_store_operation_type(operation_store, hash)?
164  );
165 
166  struct GNUNET_MESSENGER_MessageStore *message_store = get_room_message_store(tunnel->room);
167 
168  const struct GNUNET_MESSENGER_Message *message = get_store_message(message_store, hash);
169 
170  if (message)
171  update_message_state(&(tunnel->state), requested, message, hash);
172 }
173 
174 void
176 {
177  struct GNUNET_MESSENGER_SrvTunnel *tunnel = cls;
178 
179  const uint16_t length = ntohs (header->size) - sizeof(*header);
180  const char *buffer = (const char*) &header[1];
181 
182  struct GNUNET_MESSENGER_Message message;
183  struct GNUNET_HashCode hash;
184 
185  uint16_t padding = 0;
186 
187  decode_message (&message, length, buffer, GNUNET_YES, &padding);
188  hash_message (&message, length - padding, buffer, &hash);
189 
190  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got message of kind: %s!\n",
192 
193  if (!tunnel)
194  return;
195 
196  const int new_message = update_room_message (
197  tunnel->room, copy_message (&message), &hash
198  );
199 
200  if (GNUNET_YES != new_message)
201  goto receive_done;
202 
203  update_tunnel_last_message (tunnel, &hash);
204 
205  int forward_message = GNUNET_YES;
206 
207  switch (message.header.kind)
208  {
210  forward_message = recv_message_info (tunnel->room, tunnel, &message, &hash);
211  break;
213  forward_message = recv_message_peer (tunnel->room, tunnel, &message, &hash);
214  break;
216  forward_message = recv_message_request (tunnel->room, tunnel, &message, &hash);
217  break;
218  default:
219  break;
220  }
221 
222  if (GNUNET_YES == forward_message)
223  {
224  forward_room_message (tunnel->room, tunnel, &message, &hash);
225  callback_room_handle_message (tunnel->room, NULL, &message, &hash);
226  }
227 
228 receive_done:
230 }
231 
232 int
234 {
235  GNUNET_assert(tunnel);
236 
237  if (tunnel->channel)
238  return GNUNET_NO;
239 
240  const struct GNUNET_PeerIdentity *door = GNUNET_PEER_resolve2 (tunnel->peer);
241 
242  struct GNUNET_CADET_Handle *cadet = get_room_cadet (tunnel->room);
243  const struct GNUNET_HashCode *key = get_room_key (tunnel->room);
244 
246  struct GNUNET_MessageHeader, NULL),
248 
249  struct GNUNET_HashCode port;
252 
253  return GNUNET_YES;
254 }
255 
256 void
258 {
259  GNUNET_assert(tunnel);
260 
261  if (tunnel->channel)
262  {
264 
265  tunnel->channel = NULL;
266  }
267 }
268 
269 int
271 {
272  GNUNET_assert(tunnel);
273 
274  return (tunnel->channel ? GNUNET_YES : GNUNET_NO);
275 }
276 
278 {
280  struct GNUNET_HashCode hash;
281 };
282 
283 static void
285 {
286  struct GNUNET_MESSENGER_MessageSent *sent = cls;
287 
288  if (sent->tunnel)
289  update_tunnel_last_message (sent->tunnel, &(sent->hash));
290 
291  GNUNET_free(sent);
292 }
293 
294 void
296  struct GNUNET_MQ_Envelope *env,
297  const struct GNUNET_HashCode *hash)
298 {
299  GNUNET_assert((tunnel) && (env) && (hash));
300 
301  struct GNUNET_MQ_Handle *mq = GNUNET_CADET_get_mq (tunnel->channel);
302 
304 
305  GNUNET_memcpy(&(sent->hash), hash, sizeof(struct GNUNET_HashCode));
306 
307  sent->tunnel = tunnel;
308 
310  GNUNET_MQ_send (mq, env);
311 }
312 
313 int
315  void *handle,
316  struct GNUNET_MESSENGER_Message *message)
317 {
318  GNUNET_assert((tunnel) && (handle));
319 
320  if (!message)
321  return GNUNET_NO;
322 
323  struct GNUNET_HashCode hash;
325  tunnel->room, (struct GNUNET_MESSENGER_SrvHandle*) handle,
327  );
328 
329  destroy_message(message);
330 
331  if (!env)
332  return GNUNET_NO;
333 
334  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending tunnel message: %s\n",
335  GNUNET_h2s(&hash));
336 
337  send_tunnel_envelope (tunnel, env, &hash);
338  return GNUNET_YES;
339 }
340 
341 void
343  const struct GNUNET_MESSENGER_Message *message,
344  const struct GNUNET_HashCode *hash)
345 {
346  GNUNET_assert((tunnel) && (message) && (hash));
347 
348  struct GNUNET_MESSENGER_Message *copy = copy_message(message);
350 
351  destroy_message(copy);
352 
353  if (!env)
354  return;
355 
356  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Forwarding tunnel message: %s\n",
357  GNUNET_h2s(hash));
358 
359  send_tunnel_envelope (tunnel, env, hash);
360 }
361 
362 const struct GNUNET_HashCode*
364 {
365  GNUNET_assert(tunnel);
366 
367  return tunnel->peer_message;
368 }
369 
370 void
372  struct GNUNET_PeerIdentity *peer)
373 {
374  GNUNET_assert(tunnel);
375 
376  GNUNET_PEER_resolve(tunnel->peer, peer);
377 }
378 
379 uint32_t
381 {
382  GNUNET_assert(tunnel);
383 
384  return tunnel->messenger_version;
385 }
386 
387 int
389  uint32_t version)
390 {
391  GNUNET_assert(tunnel);
392 
393  if (version != GNUNET_MESSENGER_VERSION)
394  return GNUNET_SYSERR;
395 
396  if (version > tunnel->messenger_version)
397  tunnel->messenger_version = version;
398 
399  return GNUNET_OK;
400 }
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static uint16_t port
Port number.
Definition: gnunet-bcd.c:83
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
static struct GNUNET_CADET_Handle * cadet
Handle for cadet.
GNUnet MESSENGER service.
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.
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...
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...
void update_message_state(struct GNUNET_MESSENGER_MessageState *state, int requested, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
void clear_message_state(struct GNUNET_MESSENGER_MessageState *state)
void init_message_state(struct GNUNET_MESSENGER_MessageState *state)
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.
GNUnet MESSENGER service.
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.
struct GNUNET_MESSENGER_OperationStore * get_room_operation_store(struct GNUNET_MESSENGER_SrvRoom *room)
Returns the used operation store of a given room.
const struct GNUNET_HashCode * get_room_key(const struct GNUNET_MESSENGER_SrvRoom *room)
Returns the shared secret you need to access a room.
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.
struct GNUNET_MESSENGER_MessageStore * get_room_message_store(struct GNUNET_MESSENGER_SrvRoom *room)
Returns the used message store of a given room.
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.
struct GNUNET_CADET_Handle * get_room_cadet(struct GNUNET_MESSENGER_SrvRoom *room)
Returns the CADET handle from a rooms service.
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 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.
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.
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...
int connect_tunnel(struct GNUNET_MESSENGER_SrvTunnel *tunnel)
Tries to connect a tunnel by creating a new CADET channel and binding it.
void handle_tunnel_message(void *cls, const struct GNUNET_MessageHeader *header)
static void callback_tunnel_sent(void *cls)
int is_tunnel_connected(const struct GNUNET_MESSENGER_SrvTunnel *tunnel)
Returns the status of a currently bound channel of a tunnel.
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.
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...
int callback_verify_room_message(struct GNUNET_MESSENGER_SrvRoom *room, void *cls, struct GNUNET_MESSENGER_Message *message, struct GNUNET_HashCode *hash)
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 ...
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.
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)
int check_tunnel_message(void *cls, const struct GNUNET_MessageHeader *header)
void callback_tunnel_disconnect(void *cls, const struct GNUNET_CADET_Channel *channel)
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...
void disconnect_tunnel(struct GNUNET_MESSENGER_SrvTunnel *tunnel)
Disconnects and unbinds a channel from a tunnel.
int update_room_message(struct GNUNET_MESSENGER_SrvRoom *room, struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
static void update_tunnel_last_message(struct GNUNET_MESSENGER_SrvTunnel *tunnel, const struct GNUNET_HashCode *hash)
void callback_room_disconnect(struct GNUNET_MESSENGER_SrvRoom *room, void *cls)
GNUnet MESSENGER service.
#define GNUNET_log(kind,...)
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_YES
Definition: gnunet_common.h:97
@ GNUNET_NO
Definition: gnunet_common.h:94
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
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
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
void GNUNET_CADET_channel_destroy(struct GNUNET_CADET_Channel *channel)
Destroy an existing channel.
Definition: cadet_api.c:837
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
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_MESSENGER_VERSION
Version number of GNUnet Messenger API.
const char * GNUNET_MESSENGER_name_of_kind(enum GNUNET_MESSENGER_MessageKind kind)
Get the name of a message kind.
Definition: messenger_api.c:35
@ GNUNET_MESSENGER_KIND_INFO
The info kind.
@ GNUNET_MESSENGER_KIND_REQUEST
The request kind.
@ GNUNET_MESSENGER_KIND_PEER
The peer kind.
@ GNUNET_MESSENGER_KIND_UNKNOWN
The unknown kind.
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
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
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
void GNUNET_PEER_change_rc(GNUNET_PEER_Id id, int delta)
Change the reference counter of an interned PID.
Definition: peer.c:197
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
const struct GNUNET_PeerIdentity * GNUNET_PEER_resolve2(GNUNET_PEER_Id id)
Convert an interned PID to a normal peer identity.
Definition: peer.c:245
GNUNET_PEER_Id GNUNET_PEER_intern(const struct GNUNET_PeerIdentity *pid)
Intern an peer identity.
Definition: peer.c:107
#define GNUNET_MESSAGE_TYPE_CADET_CLI
Traffic (net-cat style) used by the Command Line Interface.
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...
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.
struct GNUNET_MESSENGER_Message * copy_message(const struct GNUNET_MESSENGER_Message *message)
Creates and allocates a copy of a given message.
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.
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.
void destroy_message(struct GNUNET_MESSENGER_Message *message)
Destroys a message and frees its memory fully.
#define GNUNET_MESSENGER_PACK_MODE_ENVELOPE
void convert_messenger_key_to_port(const struct GNUNET_HashCode *key, struct GNUNET_HashCode *port)
Converts a Messenger service key of a room to the specific port which gets used for the CADET channel...
void delayed_disconnect_channel(struct GNUNET_CADET_Channel *channel)
Starts an urgent task to close a CADET channel asynchronously.
messenger api: client implementation of GNUnet MESSENGER service
Opaque handle to a channel.
Definition: cadet.h:117
Opaque handle to the service.
Definition: cadet_api.c:39
A 512-bit hashcode.
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
struct GNUNET_MESSENGER_SrvTunnel * tunnel
struct GNUNET_MESSENGER_MessageHeader header
Header.
struct GNUNET_CADET_Channel * channel
struct GNUNET_MESSENGER_MessageState state
struct GNUNET_MESSENGER_SrvRoom * room
Handle to a message queue.
Definition: mq.c:86
Message handler for a specific message type.
Header for all communications.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
The identity of the host (wraps the signing key of the peer).
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.