GNUnet  0.11.x
Macros | Functions | Variables
gnunet-messenger.c File Reference

Print information about messenger groups. More...

#include <stdio.h>
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_messenger_service.h"
Include dependency graph for gnunet-messenger.c:

Go to the source code of this file.

Macros

#define MAX_BUFFER_SIZE   60000
 

Functions

void on_message (void *cls, const struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
 Function called whenever a message is received or sent. More...
 
static void shutdown_hook (void *cls)
 Task to shut down this application. More...
 
static void listen_stdio (void *cls)
 Wait for input on STDIO and send it out over the ch. More...
 
static void read_stdio (void *cls)
 Task run in stdio mode, after some data is available at stdin. More...
 
static void idle (void *cls)
 Initial task to startup application. More...
 
static void on_identity (void *cls, struct GNUNET_MESSENGER_Handle *handle)
 Function called when an identity is retrieved. More...
 
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. More...
 
int main (int argc, char **argv)
 The main function to obtain messenger information. More...
 

Variables

struct GNUNET_MESSENGER_Handlemessenger
 
struct GNUNET_SCHEDULER_Taskread_task
 
char * door_id
 
char * ego_name
 
char * room_key
 
struct GNUNET_SCHEDULER_Taskshutdown_task
 

Detailed Description

Print information about messenger groups.

Author
Tobias Frisch

Definition in file gnunet-messenger.c.

Macro Definition Documentation

◆ MAX_BUFFER_SIZE

#define MAX_BUFFER_SIZE   60000

Definition at line 109 of file gnunet-messenger.c.

Referenced by read_stdio().

Function Documentation

◆ on_message()

void on_message ( void *  cls,
const struct GNUNET_MESSENGER_Room room,
const struct GNUNET_MESSENGER_Message message,
const struct GNUNET_HashCode hash 
)

Function called whenever a message is received or sent.

Parameters
clsClosure
roomRoom
messageMessage
hashHash of message

Definition at line 43 of file gnunet-messenger.c.

References GNUNET_MESSENGER_Message::body, GNUNET_IDENTITY_PublicKey::ecdsa_key, GNUNET_i2s_full(), GNUNET_MESSENGER_contact_get_name(), GNUNET_MESSENGER_get_member(), GNUNET_MESSENGER_KIND_JOIN, GNUNET_MESSENGER_KIND_LEAVE, GNUNET_MESSENGER_KIND_PEER, GNUNET_MESSENGER_KIND_TEXT, GNUNET_MESSENGER_Message::header, GNUNET_MESSENGER_MessageBody::join, GNUNET_MESSENGER_MessageJoin::key, GNUNET_MESSENGER_MessageHeader::kind, GNUNET_MESSENGER_MessagePeer::peer, GNUNET_MESSENGER_MessageBody::peer, GNUNET_CRYPTO_EcdsaPublicKey::q_y, GNUNET_MESSENGER_MessageHeader::sender_id, GNUNET_MESSENGER_MessageText::text, and GNUNET_MESSENGER_MessageBody::text.

Referenced by run().

45 {
46  struct GNUNET_MESSENGER_Contact *sender = GNUNET_MESSENGER_get_member (room, &(message->header.sender_id));
47 
48  const char *sender_name = GNUNET_MESSENGER_contact_get_name (sender);
49 
50  if (!sender_name)
51  sender_name = "anonymous";
52 
53  switch (message->header.kind)
54  {
56  {
57  printf ("* '%s' joined the room! [ %u %u %u %u ]\n", sender_name, message->body.join.key.ecdsa_key.q_y[0],
58  message->body.join.key.ecdsa_key.q_y[1], message->body.join.key.ecdsa_key.q_y[2],
59  message->body.join.key.ecdsa_key.q_y[3]);
60  break;
61  }
63  {
64  printf ("* '%s' leaves the room!\n", sender_name);
65  break;
66  }
68  {
69  printf ("* '%s' opened the room on: %s\n", sender_name, GNUNET_i2s_full (&(message->body.peer.peer)));
70  break;
71  }
73  {
74  printf ("* '%s' says: \"%s\"\n", sender_name, message->body.text.text);
75  break;
76  }
77  default:
78  {
79  break;
80  }
81  }
82 }
struct GNUNET_PeerIdentity peer
The peer identity of the sender opening a room.
struct GNUNET_MESSENGER_MessageHeader header
Header.
struct GNUNET_ShortHashCode sender_id
The senders id inside of the room the message was sent in.
const char * GNUNET_MESSENGER_contact_get_name(const struct GNUNET_MESSENGER_Contact *contact)
Get the name used by the contact.
struct GNUNET_MESSENGER_MessageText text
unsigned char q_y[256/8]
Q consists of an x- and a y-value, each mod p (256 bits), given here in affine coordinates and Ed2551...
const char * GNUNET_i2s_full(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
char * text
The containing text.
struct GNUNET_IDENTITY_PublicKey key
The senders public key to verify its signatures.
struct GNUNET_CRYPTO_EcdsaPublicKey ecdsa_key
An ECDSA identity key.
struct GNUNET_MESSENGER_MessageBody body
Body.
struct GNUNET_MESSENGER_MessagePeer peer
struct GNUNET_MESSENGER_MessageJoin join
struct GNUNET_MESSENGER_Contact * GNUNET_MESSENGER_get_member(const struct GNUNET_MESSENGER_Room *room, const struct GNUNET_ShortHashCode *id)
Get the contact of a member in a room identified by their id.
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ shutdown_hook()

static void shutdown_hook ( void *  cls)
static

Task to shut down this application.

Parameters
clsClosure

Definition at line 92 of file gnunet-messenger.c.

References GNUNET_MESSENGER_close_room(), GNUNET_MESSENGER_disconnect(), GNUNET_SCHEDULER_cancel(), and listen_stdio().

Referenced by on_identity(), and run().

93 {
94  struct GNUNET_MESSENGER_Room *room = cls;
95 
96  if (read_task)
98 
99  if (room)
101 
102  if (messenger)
104 }
void GNUNET_MESSENGER_close_room(struct GNUNET_MESSENGER_Room *room)
Close a room which was entered, opened or both in various order and variety.
struct GNUNET_MESSENGER_Handle * messenger
void GNUNET_MESSENGER_disconnect(struct GNUNET_MESSENGER_Handle *handle)
Disconnect all of the messengers used services and clears up its used memory.
struct GNUNET_SCHEDULER_Task * read_task
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972
Here is the call graph for this function:
Here is the caller graph for this function:

◆ listen_stdio()

static void listen_stdio ( void *  cls)
static

Wait for input on STDIO and send it out over the ch.

Parameters
clsClosure

Definition at line 154 of file gnunet-messenger.c.

References GNUNET_NETWORK_fdset_create(), GNUNET_NETWORK_fdset_destroy(), GNUNET_NETWORK_fdset_set_native(), GNUNET_SCHEDULER_add_select(), GNUNET_SCHEDULER_PRIORITY_DEFAULT, GNUNET_TIME_UNIT_FOREVER_REL, and read_stdio().

Referenced by idle(), read_stdio(), and shutdown_hook().

155 {
156  read_task = NULL;
157 
159 
161 
164  rs,
165  NULL,
166  &read_stdio, cls);
167 
169 }
void GNUNET_NETWORK_fdset_set_native(struct GNUNET_NETWORK_FDSet *to, int nfd)
Set a native fd in a set.
Definition: network.c:1137
void GNUNET_NETWORK_fdset_destroy(struct GNUNET_NETWORK_FDSet *fds)
Releases the associated memory of an fd set.
Definition: network.c:1266
struct GNUNET_NETWORK_FDSet * GNUNET_NETWORK_fdset_create(void)
Creates an fd set.
Definition: network.c:1250
collection of IO descriptors
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:1841
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
static void read_stdio(void *cls)
Task run in stdio mode, after some data is available at stdin.
struct GNUNET_SCHEDULER_Task * read_task
Run with the default priority (normal P2P operations).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_stdio()

static void read_stdio ( void *  cls)
static

Task run in stdio mode, after some data is available at stdin.

Parameters
clsClosure

Definition at line 117 of file gnunet-messenger.c.

References GNUNET_MESSENGER_Message::body, GNUNET_MESSENGER_KIND_TEXT, GNUNET_MESSENGER_send_message(), GNUNET_SCHEDULER_add_now(), GNUNET_SCHEDULER_shutdown(), GNUNET_MESSENGER_Message::header, GNUNET_MESSENGER_MessageHeader::kind, listen_stdio(), MAX_BUFFER_SIZE, GNUNET_MESSENGER_MessageText::text, and GNUNET_MESSENGER_MessageBody::text.

Referenced by listen_stdio().

118 {
119  read_task = NULL;
120 
121  char buffer[MAX_BUFFER_SIZE];
122  ssize_t length;
123 
124  length = read (0, buffer, MAX_BUFFER_SIZE);
125 
126  if ((length <= 0) || (length >= MAX_BUFFER_SIZE))
127  {
129  return;
130  }
131 
132  if (buffer[length - 1] == '\n')
133  buffer[length - 1] = '\0';
134  else
135  buffer[length] = '\0';
136 
137  struct GNUNET_MESSENGER_Room *room = cls;
138 
139  struct GNUNET_MESSENGER_Message message;
141  message.body.text.text = buffer;
142 
143  GNUNET_MESSENGER_send_message (room, &message);
144 
146 }
void GNUNET_MESSENGER_send_message(struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Message *message)
Send a message into a room.
struct GNUNET_MESSENGER_MessageHeader header
Header.
#define MAX_BUFFER_SIZE
static void listen_stdio(void *cls)
Wait for input on STDIO and send it out over the ch.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:531
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:1296
struct GNUNET_SCHEDULER_Task * read_task
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ idle()

static void idle ( void *  cls)
static

Initial task to startup application.

Parameters
clsClosure

Definition at line 177 of file gnunet-messenger.c.

References GNUNET_SCHEDULER_add_now(), and listen_stdio().

Referenced by on_identity(), and updateUsage().

178 {
179  struct GNUNET_MESSENGER_Room *room = cls;
180 
181  printf ("* You joined the room.\n");
182 
184 }
static void listen_stdio(void *cls)
Wait for input on STDIO and send it out over the ch.
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:1296
struct GNUNET_SCHEDULER_Task * read_task
Here is the call graph for this function:
Here is the caller graph for this function:

◆ on_identity()

static void on_identity ( void *  cls,
struct GNUNET_MESSENGER_Handle handle 
)
static

Function called when an identity is retrieved.

Parameters
clsClosure
handleHandle of messenger service

Definition at line 199 of file gnunet-messenger.c.

References door_id, GNUNET_CRYPTO_eddsa_public_key_from_string(), GNUNET_CRYPTO_hash(), GNUNET_free, GNUNET_MESSENGER_entry_room(), GNUNET_MESSENGER_get_name(), GNUNET_MESSENGER_open_room(), GNUNET_new, GNUNET_OK, GNUNET_SCHEDULER_add_delayed_with_priority(), GNUNET_SCHEDULER_add_shutdown(), GNUNET_SCHEDULER_cancel(), GNUNET_SCHEDULER_PRIORITY_IDLE, GNUNET_SCHEDULER_shutdown(), GNUNET_TIME_relative_get_zero_(), idle(), name, GNUNET_PeerIdentity::public_key, room_key, and shutdown_hook().

Referenced by run().

200 {
201  struct GNUNET_HashCode key;
202  memset (&key, 0, sizeof(key));
203 
204  if (room_key)
205  GNUNET_CRYPTO_hash (room_key, strlen (room_key), &key);
206 
207  struct GNUNET_PeerIdentity *door = NULL;
208 
209  if (door_id)
210  {
211  door = GNUNET_new(struct GNUNET_PeerIdentity);
212 
214  {
215  GNUNET_free(door);
216  door = NULL;
217  }
218  }
219 
220  const char *name = GNUNET_MESSENGER_get_name (handle);
221 
222  if (!name)
223  name = "anonymous";
224 
225  printf ("* Welcome to the messenger, '%s'!\n", name);
226 
227  struct GNUNET_MESSENGER_Room *room;
228 
229  if (door)
230  {
231  printf ("* You try to entry a room...\n");
232 
233  room = GNUNET_MESSENGER_entry_room (messenger, door, &key);
234  }
235  else
236  {
237  printf ("* You try to open a room...\n");
238 
240  }
241 
243 
245 
246  if (!room)
248  else
250  room);
251 }
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.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed_with_priority(struct GNUNET_TIME_Relative delay, enum GNUNET_SCHEDULER_Priority priority, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1199
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, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1331
static void idle(void *cls)
Initial task to startup application.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_SCHEDULER_Task * shutdown_task
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:531
char * door_id
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:48
struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_zero_(void)
Return relative time of 0ms.
Definition: time.c:135
struct GNUNET_MESSENGER_Room * GNUNET_MESSENGER_entry_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(...).
A 512-bit hashcode.
Run when otherwise idle.
struct GNUNET_HashCode key
The key used in the DHT.
static void shutdown_hook(void *cls)
Task to shut down this application.
int 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:399
struct GNUNET_MESSENGER_Handle * messenger
The identity of the host (wraps the signing key of the peer).
const char * name
const char * GNUNET_MESSENGER_get_name(const struct GNUNET_MESSENGER_Handle *handle)
Get the name (if specified, otherwise NULL) used by the messenger.
char * room_key
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_CRYPTO_EddsaPublicKey public_key
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972
Here is the call graph for this function:
Here is the caller graph for this function:

◆ run()

static void run ( void *  cls,
char *const *  args,
const char *  cfgfile,
const struct GNUNET_CONFIGURATION_Handle cfg 
)
static

Main function that will be run by the scheduler.

Parameters
clsclosure
argsremaining command-line arguments
cfgfilename of the configuration file used (for saving, can be NULL!)
cfgconfiguration

Definition at line 262 of file gnunet-messenger.c.

References ego_name, GNUNET_MESSENGER_connect(), GNUNET_SCHEDULER_add_shutdown(), on_identity(), on_message(), and shutdown_hook().

Referenced by main().

263 {
265 
267 }
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, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1331
void on_message(void *cls, const struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
Function called whenever a message is received or sent.
struct GNUNET_SCHEDULER_Task * shutdown_task
char * ego_name
static void shutdown_hook(void *cls)
Task to shut down this application.
struct GNUNET_MESSENGER_Handle * messenger
struct GNUNET_MESSENGER_Handle * GNUNET_MESSENGER_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *name, GNUNET_MESSENGER_IdentityCallback identity_callback, void *identity_cls, GNUNET_MESSENGER_MessageCallback msg_callback, void *msg_cls)
Set up a handle for the messenger related functions and connects to all necessary services...
static void on_identity(void *cls, struct GNUNET_MESSENGER_Handle *handle)
Function called when an identity is retrieved.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

The main function to obtain messenger information.

Parameters
argcnumber of arguments from the command line
argvcommand line arguments
Returns
0 ok, 1 on error

Definition at line 277 of file gnunet-messenger.c.

References door_id, ego_name, gettext_noop, GNUNET_GETOPT_OPTION_END, GNUNET_GETOPT_option_string(), GNUNET_OK, GNUNET_PROGRAM_run(), room_key, and run().

278 {
279  const char *description = "Open and connect to rooms using the MESSENGER to chat.";
280 
283  "door",
284  "PEERIDENTITY",
285  "peer identity to entry into the room",
286  &door_id),
288  "ego",
289  "IDENTITY",
290  "identity to use for messaging",
291  &ego_name),
293  "room",
294  "ROOMKEY",
295  "key of the room to connect to",
296  &room_key),
298 
299  return (GNUNET_OK == GNUNET_PROGRAM_run (argc,
300  argv,
301  "gnunet-messenger\0",
302  gettext_noop(description),
303  options,
304  &run,
305  NULL) ? EXIT_SUCCESS : EXIT_FAILURE);
306 }
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
Definition of a command line option.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
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.
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.
char * door_id
char * ego_name
int 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, parse options).
Definition: program.c:372
char * room_key
#define gettext_noop(String)
Definition: gettext.h:69
Here is the call graph for this function:

Variable Documentation

◆ messenger

struct GNUNET_MESSENGER_Handle* messenger

Definition at line 32 of file gnunet-messenger.c.

◆ read_task

struct GNUNET_SCHEDULER_Task* read_task

Definition at line 84 of file gnunet-messenger.c.

◆ door_id

char* door_id

Definition at line 186 of file gnunet-messenger.c.

Referenced by main(), and on_identity().

◆ ego_name

char* ego_name

Definition at line 187 of file gnunet-messenger.c.

Referenced by main(), and run().

◆ room_key

char* room_key

Definition at line 188 of file gnunet-messenger.c.

Referenced by main(), and on_identity().

◆ shutdown_task

struct GNUNET_SCHEDULER_Task* shutdown_task

Definition at line 190 of file gnunet-messenger.c.