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"
34 * Maximum length allowed for the command line input.
36 #define MAX_MESSAGE_LENGTH 1024
38 #define XSTRINGIFY(x) STRINGIFY (x)
40 #define STRINGIFY(x) (#x)
43 * Possible states of the phone.
48 * We're waiting for our own idenitty.
53 * We're listening for calls
58 * We accepted an incoming phone call.
70 * States for current outgoing call.
75 * We are looking up some other participant.
80 * We are now ringing the other participant.
85 * The other party accepted our call and we are now connected.
90 * The call is currently suspended (by us).
97 * List of incoming calls
104 struct CallList *prev;
109 struct CallList *next;
112 * Handle to hang up or activate.
114 struct GNUNET_CONVERSATION_Caller *caller;
117 * Public key identifying the caller.
119 struct GNUNET_CRYPTO_EcdsaPublicKey caller_id;
122 * Unique number of the call.
124 unsigned int caller_num;
131 static struct GNUNET_CONVERSATION_Phone *phone;
134 * Call handle (for active outgoing call).
136 static struct GNUNET_CONVERSATION_Call *call;
139 * Caller handle (for active incoming call).
140 * This call handler is NOT in the #cl_head / #cl_tail list.
142 static struct CallList *cl_active;
145 * Head of calls waiting to be accepted.
147 static struct CallList *cl_head;
150 * Tail of calls waiting to be accepted.
152 static struct CallList *cl_tail;
155 * Desired phone line (string to be converted to a hash).
160 * Task which handles the commands
162 static struct GNUNET_SCHEDULER_Task *handle_cmd_task;
167 static struct GNUNET_SPEAKER_Handle *speaker;
172 static struct GNUNET_MICROPHONE_Handle *mic;
177 static struct GNUNET_CONFIGURATION_Handle *cfg;
182 static struct GNUNET_IDENTITY_Ego *my_caller_id;
185 * Handle to identity service.
187 static struct GNUNET_IDENTITY_Handle *id;
192 static char *ego_name;
195 * Public key of active conversation partner (if any).
197 static struct GNUNET_CRYPTO_EcdsaPublicKey peer_key;
200 * Name of active conversation partner (if any).
202 static char *peer_name;
205 * File handle for stdin.
207 static struct GNUNET_DISK_FileHandle *stdin_fh;
210 * Our phone's current state.
212 static enum PhoneState phone_state;
215 * Our call's current state.
217 static enum CallState call_state;
220 * Counts the number of incoming calls we have had so far.
222 static unsigned int caller_num_gen;
225 * GNS address for this phone.
227 static char *address;
236 * Function called with an event emitted by a phone.
239 * @param code type of the event
240 * @param caller handle for the caller
241 * @param caller_id public key of the caller (in GNS)
244 phone_event_handler (void *cls,
245 enum GNUNET_CONVERSATION_PhoneEventCode code,
246 struct GNUNET_CONVERSATION_Caller *caller,
247 const struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
254 case GNUNET_CONVERSATION_EC_PHONE_RING:
256 * FIXME: we should be playing our ringtones from contrib/sounds now!
260 * 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
264 "Incoming call from `%s'. Please /accept %u or /cancel %u the call.\n"),
265 GNUNET_GNSRECORD_pkey_to_zkey (caller_id),
268 cl = GNUNET_new (struct CallList);
270 cl->caller_id = *caller_id;
271 cl->caller_num = caller_num_gen++;
272 GNUNET_CONTAINER_DLL_insert (cl_head, cl_tail, cl);
275 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
276 for (cl = cl_head; NULL != cl; cl = cl->next)
277 if (caller == cl->caller)
279 if ((NULL == cl) && (caller == cl_active->caller))
287 _ ("Call from `%s' terminated\n"),
288 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
292 phone_state = PS_LISTEN;
296 GNUNET_CONTAINER_DLL_remove (cl_head, cl_tail, cl);
305 * Function called with an event emitted by a caller.
307 * @param cls closure with the `struct CallList` of the caller
308 * @param code type of the event issued by the caller
311 caller_event_handler (void *cls, enum GNUNET_CONVERSATION_CallerEventCode code)
313 struct CallList *cl = cls;
317 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
319 _ ("Call from `%s' suspended by other user\n"),
320 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
323 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
325 _ ("Call from `%s' resumed by other user\n"),
326 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
338 struct GNUNET_GNSRECORD_Data rd;
340 if (NULL == my_caller_id)
343 _ ("Ego `%s' no longer available, phone is now down.\n"),
345 phone_state = PS_LOOKUP_EGO;
348 GNUNET_assert (NULL == phone);
349 phone = GNUNET_CONVERSATION_phone_create (cfg,
351 &phone_event_handler,
353 /* FIXME: get record and print full GNS record info later here... */
356 fprintf (stderr, "%s", _ ("Failed to setup phone (internal error)\n"));
357 phone_state = PS_ERROR;
361 GNUNET_CONVERSATION_phone_get_record (phone, &rd);
362 GNUNET_free_non_null (address);
364 GNUNET_GNSRECORD_value_to_string (rd.record_type, rd.data, rd.data_size);
368 "Phone active at `%s'. Type `/help' for a list of available commands\n"),
370 phone_state = PS_LISTEN;
376 * Function called with an event emitted by a call.
378 * @param cls closure, NULL
379 * @param code type of the event on the call
382 call_event_handler (void *cls, enum GNUNET_CONVERSATION_CallEventCode code)
388 case GNUNET_CONVERSATION_EC_CALL_RINGING:
389 GNUNET_break (CS_RESOLVING == call_state);
391 _ ("Resolved address of `%s'. Now ringing other party.\n"),
393 call_state = CS_RINGING;
396 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
397 GNUNET_break (CS_RINGING == call_state);
398 fprintf (stdout, _ ("Connection established to `%s'\n"), peer_name);
399 call_state = CS_CONNECTED;
402 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
403 GNUNET_break (CS_RESOLVING == call_state);
404 fprintf (stdout, _ ("Failed to resolve `%s'\n"), peer_name);
405 GNUNET_free (peer_name);
410 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
411 fprintf (stdout, _ ("Call to `%s' terminated\n"), peer_name);
412 GNUNET_free (peer_name);
417 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
418 GNUNET_break (CS_CONNECTED == call_state);
420 _ ("Connection to `%s' suspended (by other user)\n"),
424 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
425 GNUNET_break (CS_CONNECTED == call_state);
427 _ ("Connection to `%s' resumed (by other user)\n"),
431 case GNUNET_CONVERSATION_EC_CALL_ERROR:
432 fprintf (stdout, _ ("Error with the call, restarting it\n"));
433 GNUNET_free (peer_name);
442 * Function declareation for executing a action
444 * @param arguments arguments given to the function
446 typedef void (*ActionFunction) (const char *arguments);
450 * Structure which defines a command
455 * Command the user needs to enter.
460 * Function to call on command.
462 ActionFunction Action;
465 * Help text for the command.
467 const char *helptext;
472 * Action function to print help for the command shell.
474 * @param args arguments given to the command
477 do_help (const char *args);
481 * Terminate the client
483 * @param args arguments given to the command
486 do_quit (const char *args)
489 GNUNET_SCHEDULER_shutdown ();
494 * Handler for unknown command.
496 * @param msg arguments given to the command
499 do_unknown (const char *msg)
501 fprintf (stderr, _ ("Unknown command `%s'\n"), msg);
506 * Initiating a new call
508 * @param arg arguments given to the command
511 do_call (const char *arg)
513 if (NULL == my_caller_id)
515 fprintf (stderr, _ ("Ego `%s' not available\n"), ego_name);
521 _ ("You are calling someone else already, hang up first!\n"));
527 fprintf (stderr, _ ("Ego `%s' not available\n"), ego_name);
538 "You are answering call from `%s', hang up or suspend that call first!\n"),
539 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
548 fprintf (stderr, _ ("Call recipient missing.\n"));
552 peer_name = GNUNET_strdup (arg);
553 call_state = CS_RESOLVING;
554 GNUNET_assert (NULL == call);
555 call = GNUNET_CONVERSATION_call_start (cfg,
566 * Accepting an incoming call
568 * @param args arguments given to the command
571 do_accept (const char *args)
576 if ((NULL != call) && (CS_SUSPENDED != call_state))
579 _ ("You are calling someone else already, hang up first!\n"));
589 /* this is the expected state */
596 "You are answering call from `%s', hang up or suspend that call first!\n"),
597 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
607 fprintf (stderr, _ ("There is no incoming call to accept here!\n"));
610 if ((NULL != cl->next) || (NULL != args))
612 for (cl = cl_head; NULL != cl; cl = cl->next)
614 GNUNET_snprintf (buf, sizeof(buf), "%u", cl->caller_num);
615 if (0 == strcmp (buf, args))
622 _ ("There is no incoming call `%s' to accept right now!\n"),
626 GNUNET_CONTAINER_DLL_remove (cl_head, cl_tail, cl);
628 peer_key = cl->caller_id;
629 phone_state = PS_ACCEPTED;
630 GNUNET_CONVERSATION_caller_pick_up (cl->caller,
631 &caller_event_handler,
639 * Print address information for this phone.
641 * @param args arguments given to the command
644 do_address (const char *args)
649 fprintf (stdout, "%s", _ ("We currently do not have an address.\n"));
652 fprintf (stdout, "%s\n", address);
657 * Accepting an incoming call
659 * @param args arguments given to the command
662 do_status (const char *args)
673 "We are currently trying to locate the private key for the ego `%s'.\n"),
680 "We are listening for incoming calls for ego `%s' on line `%s'.\n"),
687 _ ("You are having a conversation with `%s'.\n"),
688 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
696 "We had an internal error setting up our phone line. You can still make calls.\n"));
705 _ ("We are trying to find the network address to call `%s'.\n"),
711 _ ("We are calling `%s', their phone should be ringing.\n"),
717 _ ("You are having a conversation with `%s'.\n"),
722 /* ok to accept incoming call right now */
726 if ((NULL != cl_head) && ((cl_head != cl_active) || (cl_head != cl_tail)))
728 fprintf (stdout, "%s", _ ("Calls waiting:\n"));
729 for (cl = cl_head; NULL != cl; cl = cl->next)
736 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
738 fprintf (stdout, "%s", "\n");
746 * @param args arguments given to the command
749 do_suspend (const char *args)
761 _ ("There is no call that could be suspended right now.\n"));
765 call_state = CS_SUSPENDED;
766 GNUNET_CONVERSATION_call_suspend (call);
777 _ ("There is no call that could be suspended right now.\n"));
781 /* expected state, do rejection logic */
784 GNUNET_assert (NULL != cl_active);
785 GNUNET_CONVERSATION_caller_suspend (cl_active->caller);
787 phone_state = PS_LISTEN;
794 * @param args arguments given to the command
797 do_resume (const char *args)
811 _ ("There is no call that could be resumed right now.\n"));
815 call_state = CS_CONNECTED;
816 GNUNET_CONVERSATION_call_resume (call, speaker, mic);
826 _ ("There is no call that could be resumed right now.\n"));
830 /* expected state, do resume logic */
835 _ ("Already talking with `%s', cannot resume a call right now.\n"),
836 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
839 GNUNET_assert (NULL == cl_active);
843 fprintf (stderr, _ ("There is no incoming call to resume here!\n"));
846 if ((NULL != cl->next) || (NULL != args))
848 for (cl = cl_head; NULL != cl; cl = cl->next)
850 GNUNET_snprintf (buf, sizeof(buf), "%u", cl->caller_num);
851 if (0 == strcmp (buf, args))
858 _ ("There is no incoming call `%s' to resume right now!\n"),
863 GNUNET_CONVERSATION_caller_resume (cl_active->caller, speaker, mic);
864 phone_state = PS_ACCEPTED;
871 * @param args arguments given to the command
874 do_reject (const char *args)
881 GNUNET_CONVERSATION_call_stop (call);
891 _ ("There is no call that could be cancelled right now.\n"));
895 /* look for active incoming calls to refuse */
899 fprintf (stderr, _ ("There is no incoming call to refuse here!\n"));
902 if ((NULL != cl->next) || (NULL != args))
904 for (cl = cl_head; NULL != cl; cl = cl->next)
906 GNUNET_snprintf (buf, sizeof(buf), "%u", cl->caller_num);
907 if (0 == strcmp (buf, args))
914 _ ("There is no incoming call `%s' to refuse right now!\n"),
918 GNUNET_CONVERSATION_caller_hang_up (cl->caller);
919 GNUNET_CONTAINER_DLL_remove (cl_head, cl_tail, cl);
924 /* expected state, do rejection logic */
925 GNUNET_assert (NULL != cl_active);
926 GNUNET_CONVERSATION_caller_hang_up (cl_active->caller);
928 phone_state = PS_LISTEN;
935 * List of supported commands.
937 static struct VoipCommand commands[] = {
941 "Use `/address' to find out which address this phone should have in GNS") },
942 { "/call", &do_call, gettext_noop ("Use `/call USER.gnu' to call USER") },
945 gettext_noop ("Use `/accept #NUM' to accept incoming call #NUM") },
948 gettext_noop ("Use `/suspend' to suspend the active call") },
952 "Use `/resume [#NUM]' to resume a call, #NUM is needed to resume incoming calls, no argument is needed to resume the current outgoing call.") },
955 gettext_noop ("Use `/cancel' to reject or terminate a call") },
958 gettext_noop ("Use `/status' to print status information") },
961 gettext_noop ("Use `/quit' to terminate gnunet-conversation") },
964 gettext_noop ("Use `/help command' to get help for a specific command") },
965 { "", &do_unknown, NULL },
966 { NULL, NULL, NULL },
971 * Action function to print help for the command shell.
973 * @param args arguments given to the command
976 do_help (const char *args)
981 while ((NULL != args) && (0 != strlen (args)) &&
982 (commands[i].Action != &do_help))
984 if (0 == strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
986 fprintf (stdout, "%s\n", gettext (commands[i].helptext));
992 fprintf (stdout, "%s", "Available commands:\n");
993 while (commands[i].Action != &do_help)
995 fprintf (stdout, "%s\n", gettext (commands[i].command));
998 fprintf (stdout, "%s", "\n");
999 fprintf (stdout, "%s\n", gettext (commands[i].helptext));
1004 * Task run during shutdown.
1009 do_stop_task (void *cls)
1014 GNUNET_CONVERSATION_call_stop (call);
1019 GNUNET_CONVERSATION_phone_destroy (phone);
1022 if (NULL != handle_cmd_task)
1024 GNUNET_SCHEDULER_cancel (handle_cmd_task);
1025 handle_cmd_task = NULL;
1029 GNUNET_IDENTITY_disconnect (id);
1032 GNUNET_SPEAKER_destroy (speaker);
1034 GNUNET_MICROPHONE_destroy (mic);
1036 GNUNET_free (ego_name);
1038 GNUNET_free_non_null (peer_name);
1040 phone_state = PS_ERROR;
1045 * Handle user command.
1047 * @param message command the user typed in
1048 * @param str_len number of bytes to process in @a message
1051 handle_command_string (char *message, size_t str_len)
1058 if (message[str_len - 1] == '\n')
1059 message[str_len - 1] = '\0';
1060 if (message[str_len - 2] == '\r')
1061 message[str_len - 2] = '\0';
1062 if (0 == strlen (message))
1066 (NULL != commands[i].command) &&
1068 strncasecmp (commands[i].command, message, strlen (commands[i].command))))
1070 ptr = &message[strlen (commands[i].command)];
1071 while (isspace ((unsigned char) *ptr))
1075 commands[i].Action (ptr);
1080 * Task to handle commands from the terminal.
1085 handle_command (void *cls)
1087 char message[MAX_MESSAGE_LENGTH + 1];
1091 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1095 /* read message from command line and handle it */
1096 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
1097 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
1099 handle_command_string (message, strlen (message));
1104 * Function called by identity service with information about egos.
1107 * @param ego ego handle
1109 * @param name name of the ego
1112 identity_cb (void *cls,
1113 struct GNUNET_IDENTITY_Ego *ego,
1121 if (ego == my_caller_id)
1124 fprintf (stdout, _ ("Name of our ego changed to `%s'\n"), name);
1125 GNUNET_free (ego_name);
1126 ego_name = GNUNET_strdup (name);
1129 if (0 != strcmp (name, ego_name))
1134 fprintf (stdout, _ ("Our ego `%s' was deleted!\n"), ego_name);
1135 my_caller_id = NULL;
1139 GNUNET_CONFIGURATION_set_value_string (cfg, "CONVERSATION", "LINE", line);
1145 * Main function that will be run by the scheduler.
1147 * @param cls closure
1148 * @param args remaining command-line arguments
1149 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1150 * @param c configuration
1155 const char *cfgfile,
1156 const struct GNUNET_CONFIGURATION_Handle *c)
1161 cfg = GNUNET_CONFIGURATION_dup (c);
1162 speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
1163 mic = GNUNET_MICROPHONE_create_from_hardware (cfg);
1164 if (NULL == ego_name)
1166 fprintf (stderr, "%s", _ ("You must specify the NAME of an ego to use\n"));
1169 id = GNUNET_IDENTITY_connect (cfg, &identity_cb, NULL);
1171 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
1174 GNUNET_SCHEDULER_add_shutdown (&do_stop_task, NULL);
1179 * The main function to conversation.
1181 * @param argc number of arguments from the command line
1182 * @param argv command line arguments
1183 * @return 0 ok, 1 on error
1186 main (int argc, char *const *argv)
1188 struct GNUNET_GETOPT_CommandLineOption options[] =
1189 { GNUNET_GETOPT_option_string (
1193 gettext_noop ("sets the NAME of the ego to use for the caller ID"),
1195 GNUNET_GETOPT_option_string ('p',
1199 "sets the LINE to use for the phone"),
1201 GNUNET_GETOPT_OPTION_END };
1205 flags = fcntl (0, F_GETFL, 0);
1206 flags |= O_NONBLOCK;
1207 if (0 != fcntl (0, F_SETFL, flags))
1208 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
1209 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1211 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1214 GNUNET_PROGRAM_run (argc,
1216 "gnunet-conversation",
1218 "Enables having a conversation with other GNUnet users."),
1222 GNUNET_free ((void *) argv);
1225 GNUNET_CONFIGURATION_destroy (cfg);
1228 return (GNUNET_OK == ret) ? 0 : 1;
1232 /* end of gnunet-conversation.c */