2 This file is part of GNUnet.
3 Copyright (C) 2013 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file conversation/gnunet-conversation.c
22 * @brief conversation implementation
23 * @author Simon Dieterle
24 * @author Andreas Fuchs
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_gnsrecord_lib.h"
30 #include "gnunet_conversation_service.h"
31 #include "gnunet_namestore_service.h"
33 #include "../util/gnunet-helper-w32-console.h"
37 * Maximum length allowed for the command line input.
39 #define MAX_MESSAGE_LENGTH 1024
41 #define XSTRINGIFY(x) STRINGIFY(x)
43 #define STRINGIFY(x) (#x)
47 * Helper that reads the console for us.
49 struct GNUNET_HELPER_Handle *stdin_hlp;
53 * Possible states of the phone.
58 * We're waiting for our own idenitty.
63 * We're listening for calls
68 * We accepted an incoming phone call.
80 * States for current outgoing call.
85 * We are looking up some other participant.
90 * We are now ringing the other participant.
95 * The other party accepted our call and we are now connected.
100 * The call is currently suspended (by us).
108 * List of incoming calls
116 struct CallList *prev;
121 struct CallList *next;
124 * Handle to hang up or activate.
126 struct GNUNET_CONVERSATION_Caller *caller;
129 * Public key identifying the caller.
131 struct GNUNET_CRYPTO_EcdsaPublicKey caller_id;
134 * Unique number of the call.
136 unsigned int caller_num;
145 static struct GNUNET_CONVERSATION_Phone *phone;
148 * Call handle (for active outgoing call).
150 static struct GNUNET_CONVERSATION_Call *call;
153 * Caller handle (for active incoming call).
154 * This call handler is NOT in the #cl_head / #cl_tail list.
156 static struct CallList *cl_active;
159 * Head of calls waiting to be accepted.
161 static struct CallList *cl_head;
164 * Tail of calls waiting to be accepted.
166 static struct CallList *cl_tail;
169 * Desired phone line (string to be converted to a hash).
174 * Task which handles the commands
176 static struct GNUNET_SCHEDULER_Task * handle_cmd_task;
181 static struct GNUNET_SPEAKER_Handle *speaker;
186 static struct GNUNET_MICROPHONE_Handle *mic;
191 static struct GNUNET_CONFIGURATION_Handle *cfg;
196 static struct GNUNET_IDENTITY_Ego *my_caller_id;
199 * Handle to identity service.
201 static struct GNUNET_IDENTITY_Handle *id;
206 static char *ego_name;
209 * Public key of active conversation partner (if any).
211 static struct GNUNET_CRYPTO_EcdsaPublicKey peer_key;
214 * Name of active conversation partner (if any).
216 static char *peer_name;
219 * File handle for stdin.
221 static struct GNUNET_DISK_FileHandle *stdin_fh;
224 * Our phone's current state.
226 static enum PhoneState phone_state;
229 * Our call's current state.
231 static enum CallState call_state;
234 * Counts the number of incoming calls we have had so far.
236 static unsigned int caller_num_gen;
239 * GNS address for this phone.
241 static char *address;
250 * Function called with an event emitted by a phone.
253 * @param code type of the event
254 * @param caller handle for the caller
255 * @param caller_id public key of the caller (in GNS)
258 phone_event_handler (void *cls,
259 enum GNUNET_CONVERSATION_PhoneEventCode code,
260 struct GNUNET_CONVERSATION_Caller *caller,
261 const struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
268 case GNUNET_CONVERSATION_EC_PHONE_RING:
270 * FIXME: we should be playing our ringtones from contrib/sounds now!
274 * see https://gstreamer.freedesktop.org/documentation/application-development/highlevel/playback-components.html on how to play a wav using the gst framework being used here
277 _("Incoming call from `%s'. Please /accept %u or /cancel %u the call.\n"),
278 GNUNET_GNSRECORD_pkey_to_zkey (caller_id),
281 cl = GNUNET_new (struct CallList);
283 cl->caller_id = *caller_id;
284 cl->caller_num = caller_num_gen++;
285 GNUNET_CONTAINER_DLL_insert (cl_head,
289 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
290 for (cl = cl_head; NULL != cl; cl = cl->next)
291 if (caller == cl->caller)
294 (caller == cl_active->caller) )
302 _("Call from `%s' terminated\n"),
303 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
307 phone_state = PS_LISTEN;
311 GNUNET_CONTAINER_DLL_remove (cl_head,
322 * Function called with an event emitted by a caller.
324 * @param cls closure with the `struct CallList` of the caller
325 * @param code type of the event issued by the caller
328 caller_event_handler (void *cls,
329 enum GNUNET_CONVERSATION_CallerEventCode code)
331 struct CallList *cl = cls;
335 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
337 _("Call from `%s' suspended by other user\n"),
338 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
340 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
342 _("Call from `%s' resumed by other user\n"),
343 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
355 struct GNUNET_GNSRECORD_Data rd;
357 if (NULL == my_caller_id)
360 _("Ego `%s' no longer available, phone is now down.\n"),
362 phone_state = PS_LOOKUP_EGO;
365 GNUNET_assert (NULL == phone);
366 phone = GNUNET_CONVERSATION_phone_create (cfg,
368 &phone_event_handler,
370 /* FIXME: get record and print full GNS record info later here... */
375 _("Failed to setup phone (internal error)\n"));
376 phone_state = PS_ERROR;
380 GNUNET_CONVERSATION_phone_get_record (phone,
382 GNUNET_free_non_null (address);
383 address = GNUNET_GNSRECORD_value_to_string (rd.record_type,
387 _("Phone active at `%s'. Type `/help' for a list of available commands\n"),
389 phone_state = PS_LISTEN;
395 * Function called with an event emitted by a call.
397 * @param cls closure, NULL
398 * @param code type of the event on the call
401 call_event_handler (void *cls,
402 enum GNUNET_CONVERSATION_CallEventCode code)
408 case GNUNET_CONVERSATION_EC_CALL_RINGING:
409 GNUNET_break (CS_RESOLVING == call_state);
411 _("Resolved address of `%s'. Now ringing other party.\n"),
413 call_state = CS_RINGING;
415 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
416 GNUNET_break (CS_RINGING == call_state);
418 _("Connection established to `%s'\n"),
420 call_state = CS_CONNECTED;
422 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
423 GNUNET_break (CS_RESOLVING == call_state);
425 _("Failed to resolve `%s'\n"),
427 GNUNET_free (peer_name);
431 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
433 _("Call to `%s' terminated\n"),
435 GNUNET_free (peer_name);
439 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
440 GNUNET_break (CS_CONNECTED == call_state);
442 _("Connection to `%s' suspended (by other user)\n"),
445 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
446 GNUNET_break (CS_CONNECTED == call_state);
448 _("Connection to `%s' resumed (by other user)\n"),
451 case GNUNET_CONVERSATION_EC_CALL_ERROR:
453 _("Error with the call, restarting it\n"));
454 GNUNET_free (peer_name);
463 * Function declareation for executing a action
465 * @param arguments arguments given to the function
468 (*ActionFunction) (const char *arguments);
472 * Structure which defines a command
477 * Command the user needs to enter.
482 * Function to call on command.
484 ActionFunction Action;
487 * Help text for the command.
489 const char *helptext;
494 * Action function to print help for the command shell.
496 * @param args arguments given to the command
499 do_help (const char *args);
503 * Terminate the client
505 * @param args arguments given to the command
508 do_quit (const char *args)
511 GNUNET_SCHEDULER_shutdown ();
516 * Handler for unknown command.
518 * @param msg arguments given to the command
521 do_unknown (const char *msg)
524 _("Unknown command `%s'\n"),
530 * Initiating a new call
532 * @param arg arguments given to the command
535 do_call (const char *arg)
537 if (NULL == my_caller_id)
540 _("Ego `%s' not available\n"),
547 _("You are calling someone else already, hang up first!\n"));
554 _("Ego `%s' not available\n"),
562 _("You are answering call from `%s', hang up or suspend that call first!\n"),
563 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
572 _("Call recipient missing.\n"));
576 peer_name = GNUNET_strdup (arg);
577 call_state = CS_RESOLVING;
578 GNUNET_assert (NULL == call);
579 call = GNUNET_CONVERSATION_call_start (cfg,
584 &call_event_handler, NULL);
589 * Accepting an incoming call
591 * @param args arguments given to the command
594 do_accept (const char *args)
599 if ( (NULL != call) &&
600 (CS_SUSPENDED != call_state) )
603 _("You are calling someone else already, hang up first!\n"));
612 /* this is the expected state */
616 _("You are answering call from `%s', hang up or suspend that call first!\n"),
617 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
627 _("There is no incoming call to accept here!\n"));
630 if ( (NULL != cl->next) || (NULL != args) )
632 for (cl = cl_head; NULL != cl; cl = cl->next)
634 GNUNET_snprintf (buf, sizeof (buf),
637 if (0 == strcmp (buf, args))
644 _("There is no incoming call `%s' to accept right now!\n"),
648 GNUNET_CONTAINER_DLL_remove (cl_head,
652 peer_key = cl->caller_id;
653 phone_state = PS_ACCEPTED;
654 GNUNET_CONVERSATION_caller_pick_up (cl->caller,
655 &caller_event_handler,
663 * Print address information for this phone.
665 * @param args arguments given to the command
668 do_address (const char *args)
675 _("We currently do not have an address.\n"));
685 * Accepting an incoming call
687 * @param args arguments given to the command
690 do_status (const char *args)
699 _("We are currently trying to locate the private key for the ego `%s'.\n"),
704 _("We are listening for incoming calls for ego `%s' on line `%s'.\n"),
710 _("You are having a conversation with `%s'.\n"),
711 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));;
715 _("We had an internal error setting up our phone line. You can still make calls.\n"));
724 _("We are trying to find the network address to call `%s'.\n"),
729 _("We are calling `%s', their phone should be ringing.\n"),
734 _("You are having a conversation with `%s'.\n"),
738 /* ok to accept incoming call right now */
742 if ( (NULL != cl_head) &&
743 ( (cl_head != cl_active) ||
744 (cl_head != cl_tail) ) )
748 _("Calls waiting:\n"));
749 for (cl = cl_head; NULL != cl; cl = cl->next)
756 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
768 * @param args arguments given to the command
771 do_suspend (const char *args)
783 _("There is no call that could be suspended right now.\n"));
786 call_state = CS_SUSPENDED;
787 GNUNET_CONVERSATION_call_suspend (call);
798 _("There is no call that could be suspended right now.\n"));
801 /* expected state, do rejection logic */
804 GNUNET_assert (NULL != cl_active);
805 GNUNET_CONVERSATION_caller_suspend (cl_active->caller);
807 phone_state = PS_LISTEN;
814 * @param args arguments given to the command
817 do_resume (const char *args)
831 _("There is no call that could be resumed right now.\n"));
834 call_state = CS_CONNECTED;
835 GNUNET_CONVERSATION_call_resume (call,
847 _("There is no call that could be resumed right now.\n"));
850 /* expected state, do resume logic */
854 _("Already talking with `%s', cannot resume a call right now.\n"),
855 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
858 GNUNET_assert (NULL == cl_active);
863 _("There is no incoming call to resume here!\n"));
866 if ( (NULL != cl->next) || (NULL != args) )
868 for (cl = cl_head; NULL != cl; cl = cl->next)
870 GNUNET_snprintf (buf, sizeof (buf),
873 if (0 == strcmp (buf, args))
880 _("There is no incoming call `%s' to resume right now!\n"),
885 GNUNET_CONVERSATION_caller_resume (cl_active->caller,
888 phone_state = PS_ACCEPTED;
895 * @param args arguments given to the command
898 do_reject (const char *args)
905 GNUNET_CONVERSATION_call_stop (call);
915 _("There is no call that could be cancelled right now.\n"));
918 /* look for active incoming calls to refuse */
923 _("There is no incoming call to refuse here!\n"));
926 if ( (NULL != cl->next) || (NULL != args) )
928 for (cl = cl_head; NULL != cl; cl = cl->next)
930 GNUNET_snprintf (buf, sizeof (buf),
933 if (0 == strcmp (buf, args))
940 _("There is no incoming call `%s' to refuse right now!\n"),
944 GNUNET_CONVERSATION_caller_hang_up (cl->caller);
945 GNUNET_CONTAINER_DLL_remove (cl_head,
951 /* expected state, do rejection logic */
952 GNUNET_assert (NULL != cl_active);
953 GNUNET_CONVERSATION_caller_hang_up (cl_active->caller);
955 phone_state = PS_LISTEN;
962 * List of supported commands.
964 static struct VoipCommand commands[] = {
965 {"/address", &do_address,
966 gettext_noop ("Use `/address' to find out which address this phone should have in GNS")},
968 gettext_noop ("Use `/call USER.gnu' to call USER")},
969 {"/accept", &do_accept,
970 gettext_noop ("Use `/accept #NUM' to accept incoming call #NUM")},
971 {"/suspend", &do_suspend,
972 gettext_noop ("Use `/suspend' to suspend the active call")},
973 {"/resume", &do_resume,
974 gettext_noop ("Use `/resume [#NUM]' to resume a call, #NUM is needed to resume incoming calls, no argument is needed to resume the current outgoing call.")},
975 {"/cancel", &do_reject,
976 gettext_noop ("Use `/cancel' to reject or terminate a call")},
977 {"/status", &do_status,
978 gettext_noop ("Use `/status' to print status information")},
980 gettext_noop ("Use `/quit' to terminate gnunet-conversation")},
982 gettext_noop ("Use `/help command' to get help for a specific command")},
990 * Action function to print help for the command shell.
992 * @param args arguments given to the command
995 do_help (const char *args)
1000 while ( (NULL != args) &&
1001 (0 != strlen (args)) &&
1002 (commands[i].Action != &do_help))
1005 strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
1009 gettext (commands[i].helptext));
1017 "Available commands:\n");
1018 while (commands[i].Action != &do_help)
1022 gettext (commands[i].command));
1030 gettext (commands[i].helptext));
1035 * Task run during shutdown.
1040 do_stop_task (void *cls)
1044 if (NULL != stdin_hlp)
1046 GNUNET_HELPER_stop (stdin_hlp, GNUNET_NO);
1052 GNUNET_CONVERSATION_call_stop (call);
1057 GNUNET_CONVERSATION_phone_destroy (phone);
1060 if (NULL != handle_cmd_task)
1062 GNUNET_SCHEDULER_cancel (handle_cmd_task);
1063 handle_cmd_task = NULL;
1067 GNUNET_IDENTITY_disconnect (id);
1070 GNUNET_SPEAKER_destroy (speaker);
1072 GNUNET_MICROPHONE_destroy (mic);
1074 GNUNET_free (ego_name);
1076 GNUNET_free_non_null (peer_name);
1078 phone_state = PS_ERROR;
1083 * Handle user command.
1085 * @param message command the user typed in
1086 * @param str_len number of bytes to process in @a message
1089 handle_command_string (char *message,
1097 if (message[str_len - 1] == '\n')
1098 message[str_len - 1] = '\0';
1099 if (message[str_len - 2] == '\r')
1100 message[str_len - 2] = '\0';
1101 if (0 == strlen (message))
1104 while ((NULL != commands[i].command) &&
1105 (0 != strncasecmp (commands[i].command, message,
1106 strlen (commands[i].command))))
1108 ptr = &message[strlen (commands[i].command)];
1109 while (isspace ((unsigned char) *ptr))
1113 commands[i].Action (ptr);
1119 console_reader_chars (void *cls,
1121 const struct GNUNET_MessageHeader *message)
1127 switch (ntohs (message->type))
1129 case GNUNET_MESSAGE_TYPE_W32_CONSOLE_HELPER_CHARS:
1130 chars = (char *) &message[1];
1131 str_size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
1132 if (chars[str_size - 1] != '\0')
1133 return GNUNET_SYSERR;
1134 /* FIXME: is it ok that we pass part of a const struct to
1135 * this function that may mangle the contents?
1137 handle_command_string (chars, str_size - 1);
1149 * Task to handle commands from the terminal.
1154 handle_command (void *cls)
1156 char message[MAX_MESSAGE_LENGTH + 1];
1160 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1162 &handle_command, NULL);
1163 /* read message from command line and handle it */
1166 MAX_MESSAGE_LENGTH + 1);
1167 if (NULL == fgets (message,
1171 handle_command_string (message,
1177 * Function called by identity service with information about egos.
1180 * @param ego ego handle
1182 * @param name name of the ego
1185 identity_cb (void *cls,
1186 struct GNUNET_IDENTITY_Ego *ego,
1194 if (ego == my_caller_id)
1198 _("Name of our ego changed to `%s'\n"),
1200 GNUNET_free (ego_name);
1201 ego_name = GNUNET_strdup (name);
1204 if (0 != strcmp (name,
1211 _("Our ego `%s' was deleted!\n"),
1213 my_caller_id = NULL;
1217 GNUNET_CONFIGURATION_set_value_string (cfg,
1226 * Main function that will be run by the scheduler.
1228 * @param cls closure
1229 * @param args remaining command-line arguments
1230 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1231 * @param c configuration
1236 const char *cfgfile,
1237 const struct GNUNET_CONFIGURATION_Handle *c)
1242 cfg = GNUNET_CONFIGURATION_dup (c);
1243 speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
1244 mic = GNUNET_MICROPHONE_create_from_hardware (cfg);
1245 if (NULL == ego_name)
1249 _("You must specify the NAME of an ego to use\n"));
1252 id = GNUNET_IDENTITY_connect (cfg,
1256 if (stdin_fh == NULL)
1258 static char cpid[64];
1259 static char *args[] = {"gnunet-helper-w32-console.exe", "chars",
1260 XSTRINGIFY (MAX_MESSAGE_LENGTH), cpid, NULL};
1261 snprintf (cpid, 64, "%d", GetCurrentProcessId ());
1262 stdin_hlp = GNUNET_HELPER_start (
1264 "gnunet-helper-w32-console",
1266 console_reader_chars,
1269 if (NULL == stdin_hlp)
1273 _("Failed to start gnunet-helper-w32-console\n"));
1280 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
1281 &handle_command, NULL);
1282 GNUNET_SCHEDULER_add_shutdown (&do_stop_task,
1288 * The main function to conversation.
1290 * @param argc number of arguments from the command line
1291 * @param argv command line arguments
1292 * @return 0 ok, 1 on error
1298 struct GNUNET_GETOPT_CommandLineOption options[] = {
1299 GNUNET_GETOPT_option_string ('e',
1302 gettext_noop ("sets the NAME of the ego to use for the caller ID"),
1304 GNUNET_GETOPT_option_string ('p',
1307 gettext_noop ("sets the LINE to use for the phone"),
1309 GNUNET_GETOPT_OPTION_END
1314 flags = fcntl (0, F_GETFL, 0);
1315 flags |= O_NONBLOCK;
1316 if (0 != fcntl (0, F_SETFL, flags))
1317 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1319 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1321 if (FILE_TYPE_CHAR == GetFileType ((HANDLE) _get_osfhandle (0)))
1326 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1330 GNUNET_STRINGS_get_utf8_args (argc, argv,
1333 ret = GNUNET_PROGRAM_run (argc,
1335 "gnunet-conversation",
1336 gettext_noop ("Enables having a conversation with other GNUnet users."),
1339 GNUNET_free ((void *) argv);
1342 GNUNET_CONFIGURATION_destroy (cfg);
1345 return (GNUNET_OK == ret) ? 0 : 1;
1348 /* end of gnunet-conversation.c */