GNUnet 0.22.0
gnunet-messenger.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2020--2024 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 <stdio.h>
27#include <unistd.h>
28
31#include "gnunet_util_lib.h"
32
35
36uint64_t waiting;
37
41
47static void
48delay_shutdown (void *cls)
49{
50 read_task = NULL;
51
52 if (waiting)
53 return;
54
56}
57
58static void
59idle (void *cls);
60
71static void
72on_message (void *cls,
73 struct GNUNET_MESSENGER_Room *room,
74 const struct GNUNET_MESSENGER_Contact *sender,
75 const struct GNUNET_MESSENGER_Contact *recipient,
76 const struct GNUNET_MESSENGER_Message *message,
77 const struct GNUNET_HashCode *hash,
79{
80 uint64_t waited;
81 const char *sender_name;
82 const char *recipient_name;
83
84 waited = waiting;
85
86 if (GNUNET_YES == talk_mode)
87 {
89 {
91 {
92 waiting = waiting > message->body.talk.length?
93 waiting - message->body.talk.length : 0;
94 }
95 else if ((GNUNET_YES != silence_flag) &&
96 (0 < write (1, message->body.talk.data,
97 message->body.talk.length)))
98 {
99 fflush (stdout);
100 }
101 }
102
103 goto skip_printing;
104 }
105 else if (GNUNET_YES == silence_flag)
106 goto skip_printing;
107
108 sender_name = GNUNET_MESSENGER_contact_get_name (sender);
109 recipient_name = GNUNET_MESSENGER_contact_get_name (recipient);
110
111 if (! sender_name)
112 sender_name = "anonymous";
113
114 if (! recipient_name)
115 recipient_name = "anonymous";
116
117 printf ("[%s ->", GNUNET_h2s (&(message->header.previous)));
118 printf (" %s]", GNUNET_h2s (hash));
119 printf ("[%s] ", GNUNET_sh2s (&(message->header.sender_id)));
120
122 printf ("*( '%s' ) ", recipient_name);
123
124 switch (message->header.kind)
125 {
127 {
128 printf ("* '%s' joined the room!\n", sender_name);
129 break;
130 }
132 {
133 printf ("* '%s' gets renamed to '%s'\n", sender_name,
134 message->body.name.name);
135 break;
136 }
138 {
139 printf ("* '%s' leaves the room!\n", sender_name);
140 break;
141 }
143 {
144 printf ("* '%s' opened the room on: %s\n", sender_name,
145 GNUNET_i2s_full (&(message->body.peer.peer)));
146 break;
147 }
149 {
150 uint16_t len;
151
152 len = strlen (message->body.text.text) + 1;
153
154 if (flags & GNUNET_MESSENGER_FLAG_SENT)
155 {
156 waiting = waiting > len? waiting - len : 0;
157
158 printf (">");
159 }
160 else
161 printf ("<");
162
163 printf (" '%s' says: \"%s\"\n", sender_name,
164 message->body.text.text);
165 break;
166 }
168 {
169 if (flags & GNUNET_MESSENGER_FLAG_SENT)
170 printf (">");
171 else
172 printf ("<");
173
174 printf(" '%s' shares: \"%s\"\n%s\n", sender_name,
175 message->body.file.name, message->body.file.uri);
176 break;
177 }
178 default:
179 {
180 printf ("~ message: %s\n",
182 break;
183 }
184 }
185
186skip_printing:
187 if ((! read_task) && (! waiting) && (waited))
190 delay_shutdown, NULL);
191
192 if ((GNUNET_MESSENGER_KIND_JOIN == message->header.kind) &&
194 {
196 const char *name;
197
198 if (! read_task)
201 idle, room);
202
204
205 if (! name)
206 return;
207
209 response.body.name.name = GNUNET_strdup (name);
210
212
213 GNUNET_free (response.body.name.name);
214
215 if (GNUNET_YES != talk_mode)
216 return;
217
220 response.body.subscribe.time =
222
223 memset(&(response.body.subscribe.discourse), 0,
224 sizeof(response.body.subscribe.discourse));
225
227 }
228}
229
230
232
238static void
239shutdown_hook (void *cls)
240{
241 struct GNUNET_MESSENGER_Room *room;
242
243 GNUNET_assert (cls);
244
245 room = cls;
246
247 if (read_task)
249
250 if (room)
252
253 if (messenger)
255
256 if (ego_lookup)
258}
259
260
261static void
262listen_stdio (void *cls);
263
264#define MAX_BUFFER_SIZE 57345
265
266static int
268 struct GNUNET_MESSENGER_Room *room,
269 const struct GNUNET_MESSENGER_Contact *contact)
270{
271 struct GNUNET_MESSENGER_Message *message;
272
273 GNUNET_assert ((cls) && (room) && (contact));
274
275 message = cls;
276
278 GNUNET_MESSENGER_send_message (room, message, contact);
279
280 return GNUNET_YES;
281}
282
283
285
291static void
292read_stdio (void *cls)
293{
294 struct GNUNET_MESSENGER_Room *room;
295 struct GNUNET_MESSENGER_Message message;
296 char buffer[MAX_BUFFER_SIZE];
297 ssize_t length;
298
299 GNUNET_assert (cls);
300
301 room = cls;
302 read_task = NULL;
303
304 length = read (0, buffer, MAX_BUFFER_SIZE - 1);
305
306 if ((length <= 0) || (length >= MAX_BUFFER_SIZE))
307 {
308 delay_shutdown (NULL);
309 return;
310 }
311
312 waiting += length;
313
314 if (GNUNET_YES == talk_mode)
315 {
317 message.body.talk.length = length;
318 message.body.talk.data = buffer;
319
320 memset(&(message.body.talk.discourse), 0,
321 sizeof(message.body.talk.discourse));
322 }
323 else
324 {
325 if (buffer[length - 1] == '\n')
326 buffer[length - 1] = '\0';
327 else
328 buffer[length] = '\0';
329
331 message.body.text.text = buffer;
332 }
333
336 &message);
337 else
338 GNUNET_MESSENGER_send_message (room, &message, NULL);
339
341}
342
343
349static void
350listen_stdio (void *cls)
351{
352 struct GNUNET_NETWORK_FDSet *rs;
353
354 read_task = NULL;
355
358
361 NULL, &read_stdio, cls);
362
364}
365
366
372static void
373idle (void *cls)
374{
375 struct GNUNET_MESSENGER_Room *room;
376
377 GNUNET_assert (cls);
378
379 room = cls;
380
381 if ((GNUNET_YES != talk_mode) ||
383 printf ("* You joined the room.\n");
384
386}
387
388
392
394
401static void
402on_identity (void *cls,
404{
405 struct GNUNET_HashCode key;
406 struct GNUNET_MESSENGER_Room *room;
407 struct GNUNET_PeerIdentity door_peer;
408 struct GNUNET_PeerIdentity *door;
409 const char *name;
410
411 memset (&key, 0, sizeof(key));
412
413 if (room_key)
415
416 door = NULL;
417
418 if ((door_id) &&
420 strlen (
421 door_id),
422 &(door_peer.
423 public_key))))
424 door = &door_peer;
425
426 if ((GNUNET_YES == talk_mode) ||
428 goto skip_welcome;
429
431
432 if (! name)
433 name = "anonymous";
434
435 printf ("* Welcome to the messenger, '%s'!\n", name);
436
437skip_welcome:
438 if (door)
439 {
440 if ((GNUNET_YES != talk_mode) ||
442 printf ("* You try to entry a room...\n");
443
445 }
446 else
447 {
448 if ((GNUNET_YES != talk_mode) ||
450 printf ("* You try to open a room...\n");
451
453 }
454
456
458
459 waiting = 0;
460
461 if (! room)
463 else
464 read_task = NULL;
465}
466
467
468static void
469on_ego_lookup (void *cls,
470 struct GNUNET_IDENTITY_Ego *ego)
471{
472 const struct GNUNET_CRYPTO_PrivateKey *key;
473
474 ego_lookup = NULL;
475
476 key = ego ? GNUNET_IDENTITY_ego_get_private_key (ego) : NULL;
477
479 NULL);
480
481 on_identity (NULL, messenger);
482}
483
484
493static void
494run (void *cls,
495 char *const *args,
496 const char *cfgfile,
497 const struct GNUNET_CONFIGURATION_Handle *cfg)
498{
499 config = cfg;
500
501 if (ego_name)
502 {
504 NULL);
505 messenger = NULL;
506 }
507 else
508 {
509 ego_lookup = NULL;
510 messenger = GNUNET_MESSENGER_connect (cfg, NULL, NULL, &on_message, NULL);
511 }
512
514
515 if (messenger)
516 on_identity (NULL, messenger);
517}
518
519
527int
528main (int argc,
529 char **argv)
530{
531 const char *description =
532 "Open and connect to rooms using the MESSENGER to chat.";
533
535 GNUNET_GETOPT_option_string ('d', "door", "PEERIDENTITY",
536 "peer identity to entry into the room",
537 &door_id),
538 GNUNET_GETOPT_option_string ('e', "ego", "IDENTITY",
539 "identity to use for messaging",
540 &ego_name),
541 GNUNET_GETOPT_option_string ('r', "room", "ROOMKEY",
542 "key of the room to connect to",
543 &room_key),
544 GNUNET_GETOPT_option_flag ('p', "private", "flag to enable private mode",
545 &private_mode),
546 GNUNET_GETOPT_option_flag ('s', "silence", "flag to silence all others to send only",
547 &silence_flag),
548 GNUNET_GETOPT_option_flag ('t', "talk", "flag to enable talk mode",
549 &talk_mode),
551 };
552
553 return (GNUNET_OK == GNUNET_PROGRAM_run (argc, argv, "gnunet-messenger\0",
555 &run,
556 NULL) ? EXIT_SUCCESS : EXIT_FAILURE);
557}
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
#define gettext_noop(String)
Definition: gettext.h:74
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:108
static struct MHD_Response * response
Our canonical response.
struct GNUNET_HashCode key
The key used in the DHT.
static void on_identity(void *cls, struct GNUNET_MESSENGER_Handle *handle)
Function called when an identity is retrieved.
static void listen_stdio(void *cls)
Wait for input on STDIO and send it out over the ch.
static void delay_shutdown(void *cls)
Delay forced shutdown by input to wait for data processing.
int talk_mode
uint64_t waiting
int main(int argc, char **argv)
The main function to obtain messenger information.
static void idle(void *cls)
Initial task to startup application.
static void read_stdio(void *cls)
Task run in stdio mode, after some data is available at stdin.
struct GNUNET_MESSENGER_Handle * messenger
char * door_id
char * ego_name
struct GNUNET_IDENTITY_EgoLookup * ego_lookup
struct GNUNET_SCHEDULER_Task * read_task
int private_mode
const struct GNUNET_CONFIGURATION_Handle * config
static int iterate_send_private_message(void *cls, struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Contact *contact)
struct GNUNET_SCHEDULER_Task * shutdown_task
char * room_key
int silence_flag
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be run by the scheduler.
#define MAX_BUFFER_SIZE
static void on_ego_lookup(void *cls, struct GNUNET_IDENTITY_Ego *ego)
static void shutdown_hook(void *cls)
Task to shut down this application.
static void on_message(void *cls, struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Contact *sender, const struct GNUNET_MESSENGER_Contact *recipient, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash, enum GNUNET_MESSENGER_MessageFlags flags)
Function called whenever a message is received or sent.
static char * name
Name (label) of the records to list.
static struct GNUNET_VPN_Handle * handle
Handle to vpn service.
Definition: gnunet-vpn.c:35
Identity service; implements identity management for GNUnet.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
const struct GNUNET_CRYPTO_PrivateKey * GNUNET_IDENTITY_ego_get_private_key(const struct GNUNET_IDENTITY_Ego *ego)
Obtain the ECC key associated with a ego.
Definition: identity_api.c:517
struct GNUNET_IDENTITY_EgoLookup * GNUNET_IDENTITY_ego_lookup(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *name, GNUNET_IDENTITY_EgoCallback cb, void *cb_cls)
Lookup an ego by name.
void GNUNET_IDENTITY_ego_lookup_cancel(struct GNUNET_IDENTITY_EgoLookup *el)
Abort ego lookup attempt.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_public_key_from_string(const char *enc, size_t enclen, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Convert a string representing a public key to a public key.
Definition: crypto_ecc.c:361
@ GNUNET_SCHEDULER_PRIORITY_IDLE
Run when otherwise idle.
@ GNUNET_SCHEDULER_PRIORITY_DEFAULT
Run with the default priority (normal P2P operations).
@ GNUNET_OK
@ GNUNET_YES
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
const char * GNUNET_sh2s(const struct GNUNET_ShortHashCode *shc)
Convert a short hash value to a string (for printing debug messages).
const char * GNUNET_i2s_full(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_MESSENGER_Room * GNUNET_MESSENGER_open_room(struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_HashCode *key)
Open a room to send and receive messages.
int GNUNET_MESSENGER_iterate_members(struct GNUNET_MESSENGER_Room *room, GNUNET_MESSENGER_MemberCallback callback, void *cls)
Iterates through all members of a given room and calls a selected callback for each of them with a pr...
const char * GNUNET_MESSENGER_name_of_kind(enum GNUNET_MESSENGER_MessageKind kind)
Get the name of a message kind.
Definition: messenger_api.c:44
GNUNET_MESSENGER_MessageFlags
Enum for the different supported flags used by message handling.
const char * GNUNET_MESSENGER_contact_get_name(const struct GNUNET_MESSENGER_Contact *contact)
Get the name used by the contact.
struct GNUNET_MESSENGER_Handle * GNUNET_MESSENGER_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *name, const struct GNUNET_CRYPTO_PrivateKey *key, GNUNET_MESSENGER_MessageCallback msg_callback, void *msg_cls)
Set up a handle for the messenger related functions and connects to all necessary services.
struct GNUNET_MESSENGER_Room * GNUNET_MESSENGER_enter_room(struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_PeerIdentity *door, const struct GNUNET_HashCode *key)
Enter a room to send and receive messages through a door opened using GNUNET_MESSENGER_open_room.
const struct GNUNET_CRYPTO_PublicKey * GNUNET_MESSENGER_contact_get_key(const struct GNUNET_MESSENGER_Contact *contact)
Get the public key used by the contact or NULL if the anonymous key was used.
void GNUNET_MESSENGER_disconnect(struct GNUNET_MESSENGER_Handle *handle)
Disconnect all of the messengers used services and clears up its used memory.
const char * GNUNET_MESSENGER_get_name(const struct GNUNET_MESSENGER_Handle *handle)
Get the name (if specified, otherwise NULL) used by the messenger.
void GNUNET_MESSENGER_close_room(struct GNUNET_MESSENGER_Room *room)
Close a room which was entered, opened or both in various order and variety.
void GNUNET_MESSENGER_send_message(struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_MESSENGER_Contact *contact)
Send a message into a room.
@ GNUNET_MESSENGER_KIND_FILE
The file kind.
@ GNUNET_MESSENGER_KIND_NAME
The name kind.
@ GNUNET_MESSENGER_KIND_LEAVE
The leave kind.
@ GNUNET_MESSENGER_KIND_SUBSCRIBE
The subscribe kind.
@ GNUNET_MESSENGER_KIND_TALK
The talk kind.
@ GNUNET_MESSENGER_KIND_PEER
The peer kind.
@ GNUNET_MESSENGER_KIND_TEXT
The text kind.
@ GNUNET_MESSENGER_KIND_JOIN
The join kind.
@ GNUNET_MESSENGER_FLAG_PRIVATE
The private flag.
@ GNUNET_MESSENGER_FLAG_SENT
The sent flag.
@ GNUNET_MESSENGER_FLAG_SUBSCRIPTION_KEEP_ALIVE
The keep alive flag.
void GNUNET_NETWORK_fdset_destroy(struct GNUNET_NETWORK_FDSet *fds)
Releases the associated memory of an fd set.
Definition: network.c:1187
struct GNUNET_NETWORK_FDSet * GNUNET_NETWORK_fdset_create(void)
Creates an fd set.
Definition: network.c:1171
void GNUNET_NETWORK_fdset_set_native(struct GNUNET_NETWORK_FDSet *to, int nfd)
Set a native fd in a set.
Definition: network.c:1058
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition: program.c:400
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:566
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_select(enum GNUNET_SCHEDULER_Priority prio, struct GNUNET_TIME_Relative delay, const struct GNUNET_NETWORK_FDSet *rs, const struct GNUNET_NETWORK_FDSet *ws, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when any of the specified file descriptor set...
Definition: scheduler.c:1834
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition: scheduler.c:1338
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:979
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1303
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_with_priority(enum GNUNET_SCHEDULER_Priority prio, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified priority.
Definition: scheduler.c:1230
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_second_(void)
Return relative time of 1s.
Definition: time.c:169
struct GNUNET_TIME_RelativeNBO GNUNET_TIME_relative_hton(struct GNUNET_TIME_Relative a)
Convert relative time to network byte order.
Definition: time.c:620
A private key for an identity as per LSD0001.
Definition of a command line option.
const char * description
Help text for the option (description)
A 512-bit hashcode.
Handle for ego lookup.
Handle for an ego.
Definition: identity.h:37
struct GNUNET_MESSENGER_MessageText text
struct GNUNET_MESSENGER_MessageName name
struct GNUNET_MESSENGER_MessageTalk talk
struct GNUNET_MESSENGER_MessageFile file
struct GNUNET_MESSENGER_MessagePeer peer
char * uri
The uri of the encrypted file.
char name[NAME_MAX]
The name of the original file.
struct GNUNET_HashCode previous
The hash of the previous message from the senders perspective.
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
struct GNUNET_ShortHashCode sender_id
The senders id inside of the room the message was sent in.
char * name
The new name which replaces the current senders name.
struct GNUNET_PeerIdentity peer
The peer identity of the sender opening a room.
uint16_t length
The length of the talk message data.
char * data
The data of the talk message.
struct GNUNET_ShortHashCode discourse
The hash of the discourse to talk.
char * text
The containing text.
struct GNUNET_MESSENGER_MessageHeader header
Header.
struct GNUNET_MESSENGER_MessageBody body
Body.
collection of IO descriptors
The identity of the host (wraps the signing key of the peer).
struct GNUNET_CRYPTO_EddsaPublicKey public_key
Entry in list of pending tasks.
Definition: scheduler.c:135