2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
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"
35 * Maximum length allowed for the command line input.
37 #define MAX_MESSAGE_LENGTH 1024
41 * Possible states of the phone.
46 * We're waiting for our own idenitty.
51 * We're listening for calls
56 * We accepted an incoming phone call.
68 * States for current outgoing call.
73 * We are looking up some other participant.
78 * We are now ringing the other participant.
83 * The other party accepted our call and we are now connected.
88 * The call is currently suspended (by us).
97 * List of incoming calls
105 struct CallList *prev;
110 struct CallList *next;
113 * Handle to hang up or activate.
115 struct GNUNET_CONVERSATION_Caller *caller;
118 * String identifying the caller.
123 * Unique number of the call.
125 unsigned int caller_num;
134 static struct GNUNET_CONVERSATION_Phone *phone;
137 * Call handle (for active outgoing call).
139 static struct GNUNET_CONVERSATION_Call *call;
142 * Caller handle (for active incoming call).
144 static struct CallList *cl_active;
147 * Head of calls waiting to be accepted.
149 static struct CallList *cl_head;
152 * Tail of calls waiting to be accepted.
154 static struct CallList *cl_tail;
157 * Desired phone line.
159 static unsigned int line;
162 * Task which handles the commands
164 static GNUNET_SCHEDULER_TaskIdentifier handle_cmd_task;
169 static struct GNUNET_SPEAKER_Handle *speaker;
174 static struct GNUNET_MICROPHONE_Handle *mic;
179 static struct GNUNET_CONFIGURATION_Handle *cfg;
184 static struct GNUNET_IDENTITY_Ego *caller_id;
187 * Handle to identity service.
189 static struct GNUNET_IDENTITY_Handle *id;
194 static char *ego_name;
197 * Name of conversation partner (if any).
199 static char *peer_name;
202 * File handle for stdin.
204 static struct GNUNET_DISK_FileHandle *stdin_fh;
207 * Our phone's current state.
209 static enum PhoneState phone_state;
212 * Our call's current state.
214 static enum CallState call_state;
217 * Counts the number of incoming calls we have had so far.
219 static unsigned int caller_num_gen;
222 * GNS address for this phone.
224 static char *address;
233 * Function called with an event emitted by a phone.
236 * @param code type of the event
237 * @param caller handle for the caller
238 * @param caller_id name of the caller in GNS
241 phone_event_handler (void *cls,
242 enum GNUNET_CONVERSATION_PhoneEventCode code,
243 struct GNUNET_CONVERSATION_Caller *caller,
244 const char *caller_id)
250 case GNUNET_CONVERSATION_EC_PHONE_RING:
252 _("Incoming call from `%s'. Please /accept #%u or /cancel %u the call.\n"),
256 cl = GNUNET_new (struct CallList);
258 cl->caller_id = GNUNET_strdup (caller_id);
259 cl->caller_num = caller_num_gen++;
260 GNUNET_CONTAINER_DLL_insert (cl_head,
264 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
265 for (cl = cl_head; NULL != cl; cl = cl->next)
266 if (caller == cl->caller)
274 _("Call from `%s' terminated\n"),
276 GNUNET_CONTAINER_DLL_remove (cl_head,
279 GNUNET_free (cl->caller_id);
283 phone_state = PS_LISTEN;
292 * Function called with an event emitted by a caller.
294 * @param cls closure with the `struct CallList` of the caller
295 * @param code type of the event issued by the caller
298 caller_event_handler (void *cls,
299 enum GNUNET_CONVERSATION_CallerEventCode code)
301 struct CallList *cl = cls;
305 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
307 _("Call from `%s' suspended by other user\n"),
310 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
312 _("Call from `%s' resumed by other user\n"),
325 struct GNUNET_GNSRECORD_Data rd;
327 if (NULL == caller_id)
330 _("Ego `%s' no longer available, phone is now down.\n"),
332 phone_state = PS_LOOKUP_EGO;
335 GNUNET_assert (NULL == phone);
336 phone = GNUNET_CONVERSATION_phone_create (cfg,
338 &phone_event_handler, NULL);
339 /* FIXME: get record and print full GNS record info later here... */
344 _("Failed to setup phone (internal error)\n"));
345 phone_state = PS_ERROR;
349 GNUNET_CONVERSATION_phone_get_record (phone,
351 GNUNET_free_non_null (address);
352 address = GNUNET_GNSRECORD_value_to_string (rd.record_type,
356 _("Phone active on line %u. Type `/help' for a list of available commands\n"),
357 (unsigned int) line);
358 phone_state = PS_LISTEN;
364 * Function called with an event emitted by a call.
366 * @param cls closure, NULL
367 * @param code type of the event on the call
370 call_event_handler (void *cls,
371 enum GNUNET_CONVERSATION_CallEventCode code)
375 case GNUNET_CONVERSATION_EC_CALL_RINGING:
376 GNUNET_break (CS_RESOLVING == call_state);
378 _("Resolved address of `%s'. Now ringing other party.\n"),
380 call_state = CS_RINGING;
382 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
383 GNUNET_break (CS_RINGING == call_state);
385 _("Connection established to `%s'\n"),
387 call_state = CS_CONNECTED;
389 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
390 GNUNET_break (CS_RESOLVING == call_state);
392 _("Failed to resolve `%s'\n"),
396 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
399 _("Call terminated\n"));
402 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
403 GNUNET_break (CS_CONNECTED == call_state);
405 _("Connection to `%s' suspended (by other user)\n"),
408 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
409 GNUNET_break (CS_CONNECTED == call_state);
411 _("Connection to `%s' resumed (by other user)\n"),
414 case GNUNET_CONVERSATION_EC_CALL_ERROR:
416 _("Error with the call, restarting it\n"));
417 call_state = CS_RESOLVING;
424 * Function declareation for executing a action
426 * @param arguments arguments given to the function
428 typedef void (*ActionFunction) (const char *arguments);
432 * Structure which defines a command
437 * Command the user needs to enter.
442 * Function to call on command.
444 ActionFunction Action;
447 * Help text for the command.
449 const char *helptext;
454 * Action function to print help for the command shell.
456 * @param args arguments given to the command
459 do_help (const char *args);
463 * Terminate the client
465 * @param args arguments given to the command
468 do_quit (const char *args)
470 GNUNET_SCHEDULER_shutdown ();
475 * Handler for unknown command.
477 * @param msg arguments given to the command
480 do_unknown (const char *msg)
483 _("Unknown command `%s'\n"),
489 * Initiating a new call
491 * @param arg arguments given to the command
494 do_call (const char *arg)
496 if (NULL == caller_id)
499 _("Ego `%s' not available\n"),
506 _("You are calling someone else already, hang up first!\n"));
513 _("Ego `%s' not available\n"),
521 _("You are answering call from `%s', hang up or suspend that call first!\n"),
528 GNUNET_free_non_null (peer_name);
532 _("Call recipient missing.\n"));
536 peer_name = GNUNET_strdup (arg);
537 call_state = CS_RESOLVING;
538 GNUNET_assert (NULL == call);
539 call = GNUNET_CONVERSATION_call_start (cfg,
544 &call_event_handler, NULL);
549 * Accepting an incoming call
551 * @param args arguments given to the command
554 do_accept (const char *args)
559 if ( (NULL != call) &&
560 (CS_SUSPENDED != call_state) )
563 _("You are calling someone else already, hang up first!\n"));
572 /* this is the expected state */
576 _("You are answering call from `%s', hang up or suspend that call first!\n"),
587 _("There is no incoming call to accept here!\n"));
590 if ( (NULL != cl->next) || (NULL != args) )
592 for (cl = cl_head; NULL != cl; cl = cl->next)
594 GNUNET_snprintf (buf, sizeof (buf),
597 if (0 == strcmp (buf, args))
604 _("There is no incoming call `%s' to accept right now!\n"),
609 GNUNET_free_non_null (peer_name);
610 peer_name = GNUNET_strdup (cl->caller_id);
611 phone_state = PS_ACCEPTED;
612 GNUNET_CONVERSATION_caller_pick_up (cl->caller,
613 &caller_event_handler,
621 * Print address information for this phone.
623 * @param args arguments given to the command
626 do_address (const char *args)
632 _("We currently do not have an address.\n"));
642 * Accepting an incoming call
644 * @param args arguments given to the command
647 do_status (const char *args)
655 _("We are currently trying to locate the private key for the ego `%s'.\n"),
660 _("We are listening for incoming calls for ego `%s' on line %u.\n"),
666 _("You are having a conversation with `%s'.\n"),
671 _("We had an internal error setting up our phone line. You can still make calls.\n"));
680 _("We are trying to find the network address to call `%s'.\n"),
685 _("We are calling `%s', his phone should be ringing.\n"),
690 _("You are having a conversation with `%s'.\n"),
694 /* ok to accept incoming call right now */
698 if ( (NULL != cl_head) &&
699 ( (cl_head != cl_active) ||
700 (cl_head != cl_tail) ) )
704 _("Calls waiting:\n"));
705 for (cl = cl_head; NULL != cl; cl = cl->next)
724 * @param args arguments given to the command
727 do_suspend (const char *args)
738 _("There is no call that could be suspended right now.\n"));
741 call_state = CS_SUSPENDED;
742 GNUNET_CONVERSATION_call_suspend (call);
753 _("There is no call that could be suspended right now.\n"));
756 /* expected state, do rejection logic */
759 GNUNET_assert (NULL != cl_active);
760 GNUNET_CONVERSATION_caller_suspend (cl_active->caller);
762 phone_state = PS_LISTEN;
769 * @param args arguments given to the command
772 do_resume (const char *args)
786 _("There is no call that could be resumed right now.\n"));
789 call_state = CS_CONNECTED;
790 GNUNET_CONVERSATION_call_resume (call,
802 _("There is no call that could be resumed right now.\n"));
805 /* expected state, do resume logic */
809 _("Already talking with `%s', cannot resume a call right now.\n"),
813 GNUNET_assert (NULL == cl_active);
818 _("There is no incoming call to resume here!\n"));
821 if ( (NULL != cl->next) || (NULL != args) )
823 for (cl = cl_head; NULL != cl; cl = cl->next)
825 GNUNET_snprintf (buf, sizeof (buf),
828 if (0 == strcmp (buf, args))
835 _("There is no incoming call `%s' to resume right now!\n"),
840 GNUNET_CONVERSATION_caller_resume (cl_active->caller,
843 phone_state = PS_ACCEPTED;
850 * @param args arguments given to the command
853 do_reject (const char *args)
860 GNUNET_CONVERSATION_call_stop (call);
870 _("There is no call that could be cancelled right now.\n"));
873 /* look for active incoming calls to refuse */
878 _("There is no incoming call to refuse here!\n"));
881 if ( (NULL != cl->next) || (NULL != args) )
883 for (cl = cl_head; NULL != cl; cl = cl->next)
885 GNUNET_snprintf (buf, sizeof (buf),
888 if (0 == strcmp (buf, args))
895 _("There is no incoming call `%s' to refuse right now!\n"),
899 GNUNET_CONVERSATION_caller_hang_up (cl->caller);
900 GNUNET_CONTAINER_DLL_remove (cl_head,
903 GNUNET_free (cl->caller_id);
907 /* expected state, do rejection logic */
908 GNUNET_assert (NULL != cl_active);
909 GNUNET_CONVERSATION_caller_hang_up (cl_active->caller);
911 phone_state = PS_LISTEN;
918 * List of supported commands.
920 static struct VoipCommand commands[] = {
921 {"/address", &do_address,
922 gettext_noop ("Use `/address' to find out which address this phone should have in GNS")},
924 gettext_noop ("Use `/call USER.gnu' to call USER")},
925 {"/accept", &do_accept,
926 gettext_noop ("Use `/accept #NUM' to accept incoming call #NUM")},
927 {"/suspend", &do_suspend,
928 gettext_noop ("Use `/suspend' to suspend the active call")},
929 {"/resume", &do_resume,
930 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.")},
931 {"/cancel", &do_reject,
932 gettext_noop ("Use `/cancel' to reject or terminate a call")},
933 {"/status", &do_status,
934 gettext_noop ("Use `/status' to print status information")},
936 gettext_noop ("Use `/quit' to terminate gnunet-conversation")},
938 gettext_noop ("Use `/help command' to get help for a specific command")},
946 * Action function to print help for the command shell.
948 * @param args arguments given to the command
951 do_help (const char *args)
956 while ( (NULL != args) &&
957 (0 != strlen (args)) &&
958 (commands[i].Action != &do_help))
961 strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
965 gettext (commands[i].helptext));
973 "Available commands:\n");
974 while (commands[i].Action != &do_help)
978 gettext (commands[i].command));
986 gettext (commands[i].helptext));
991 * Task run during shutdown.
997 do_stop_task (void *cls,
998 const struct GNUNET_SCHEDULER_TaskContext *tc)
1002 GNUNET_CONVERSATION_call_stop (call);
1007 GNUNET_CONVERSATION_phone_destroy (phone);
1010 if (GNUNET_SCHEDULER_NO_TASK != handle_cmd_task)
1012 GNUNET_SCHEDULER_cancel (handle_cmd_task);
1013 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
1017 GNUNET_IDENTITY_disconnect (id);
1020 GNUNET_SPEAKER_destroy (speaker);
1022 GNUNET_MICROPHONE_destroy (mic);
1024 GNUNET_free (ego_name);
1026 GNUNET_free_non_null (peer_name);
1027 phone_state = PS_ERROR;
1032 * Task to handle commands from the terminal.
1035 * @param tc scheduler context
1038 handle_command (void *cls,
1039 const struct GNUNET_SCHEDULER_TaskContext *tc)
1041 char message[MAX_MESSAGE_LENGTH + 1];
1046 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1048 &handle_command, NULL);
1049 /* read message from command line and handle it */
1050 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
1051 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
1053 if (0 == strlen (message))
1055 if (message[strlen (message) - 1] == '\n')
1056 message[strlen (message) - 1] = '\0';
1057 if (0 == strlen (message))
1060 while ((NULL != commands[i].command) &&
1061 (0 != strncasecmp (commands[i].command, message,
1062 strlen (commands[i].command))))
1064 ptr = &message[strlen (commands[i].command)];
1065 while (isspace ((int) *ptr))
1069 commands[i].Action (ptr);
1074 * Function called by identity service with information about egos.
1077 * @param ego ego handle
1079 * @param name name of the ego
1082 identity_cb (void *cls,
1083 struct GNUNET_IDENTITY_Ego *ego,
1089 if (ego == caller_id)
1093 _("Name of our ego changed to `%s'\n"),
1095 GNUNET_free (ego_name);
1096 ego_name = GNUNET_strdup (name);
1099 if (0 != strcmp (name,
1106 _("Our ego `%s' was deleted!\n"),
1112 GNUNET_CONFIGURATION_set_value_number (cfg,
1121 * Main function that will be run by the scheduler.
1123 * @param cls closure
1124 * @param args remaining command-line arguments
1125 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1126 * @param c configuration
1131 const char *cfgfile,
1132 const struct GNUNET_CONFIGURATION_Handle *c)
1134 cfg = GNUNET_CONFIGURATION_dup (c);
1135 speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
1136 mic = GNUNET_MICROPHONE_create_from_hardware (cfg);
1137 if (NULL == ego_name)
1141 _("You must specify the NAME of an ego to use\n"));
1144 id = GNUNET_IDENTITY_connect (cfg,
1148 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
1149 &handle_command, NULL);
1150 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
1156 * The main function to conversation.
1158 * @param argc number of arguments from the command line
1159 * @param argv command line arguments
1160 * @return 0 ok, 1 on error
1163 main (int argc, char *const *argv)
1165 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1166 {'e', "ego", "NAME",
1167 gettext_noop ("sets the NAME of the ego to use for the phone (and name resolution)"),
1168 1, &GNUNET_GETOPT_set_string, &ego_name},
1169 {'p', "phone", "LINE",
1170 gettext_noop ("sets the LINE to use for the phone"),
1171 1, &GNUNET_GETOPT_set_uint, &line},
1172 GNUNET_GETOPT_OPTION_END
1177 flags = fcntl (0, F_GETFL, 0);
1178 flags |= O_NONBLOCK;
1179 fcntl (0, F_SETFL, flags);
1180 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1181 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1183 ret = GNUNET_PROGRAM_run (argc, argv,
1184 "gnunet-conversation",
1185 gettext_noop ("Enables having a conversation with other GNUnet users."),
1186 options, &run, NULL);
1187 GNUNET_free ((void *) argv);
1190 GNUNET_CONFIGURATION_destroy (cfg);
1193 return (GNUNET_OK == ret) ? 0 : 1;
1196 /* end of gnunet-conversation.c */