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'.\nPlease /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 phone = GNUNET_CONVERSATION_phone_create (cfg,
337 &phone_event_handler, NULL);
338 /* FIXME: get record and print full GNS record info later here... */
343 _("Failed to setup phone (internal error)\n"));
344 phone_state = PS_ERROR;
348 GNUNET_CONVERSATION_phone_get_record (phone,
350 GNUNET_free_non_null (address);
351 address = GNUNET_GNSRECORD_value_to_string (rd.record_type,
356 _("Phone active on line %u\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);
380 _("Resolved address. Now ringing other party.\n"));
381 call_state = CS_RINGING;
383 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
384 GNUNET_break (CS_RINGING == call_state);
386 _("Connection established to `%s'\n"),
388 call_state = CS_CONNECTED;
390 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
391 GNUNET_break (CS_RESOLVING == call_state);
393 _("Failed to resolve `%s'\n"),
397 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
400 _("Call terminated\n"));
403 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
404 GNUNET_break (CS_CONNECTED == call_state);
406 _("Connection to `%s' suspended (by other user)\n"),
409 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
410 GNUNET_break (CS_CONNECTED == call_state);
412 _("Connection to `%s' resumed (by other user)\n"),
420 * Function declareation for executing a action
422 * @param arguments arguments given to the function
424 typedef void (*ActionFunction) (const char *arguments);
428 * Structure which defines a command
433 * Command the user needs to enter.
438 * Function to call on command.
440 ActionFunction Action;
443 * Help text for the command.
445 const char *helptext;
450 * Action function to print help for the command shell.
452 * @param args arguments given to the command
455 do_help (const char *args);
459 * Terminate the client
461 * @param args arguments given to the command
464 do_quit (const char *args)
466 GNUNET_SCHEDULER_shutdown ();
471 * Handler for unknown command.
473 * @param msg arguments given to the command
476 do_unknown (const char *msg)
479 _("Unknown command `%s'\n"),
485 * Initiating a new call
487 * @param arg arguments given to the command
490 do_call (const char *arg)
492 if (NULL == caller_id)
495 _("Ego `%s' not available\n"),
502 _("You are calling someone else already, hang up first!\n"));
509 _("Ego `%s' not available\n"),
517 _("You are answering call from `%s', hang up or suspend that call first!\n"),
524 GNUNET_free_non_null (peer_name);
525 peer_name = GNUNET_strdup (arg);
526 call_state = CS_RESOLVING;
527 call = GNUNET_CONVERSATION_call_start (cfg,
532 &call_event_handler, NULL);
537 * Accepting an incoming call
539 * @param args arguments given to the command
542 do_accept (const char *args)
547 if ( (NULL != call) &&
548 (CS_SUSPENDED != call_state) )
551 _("You are calling someone else already, hang up first!\n"));
560 /* this is the expected state */
564 _("You are answering call from `%s', hang up or suspend that call first!\n"),
575 _("There is no incoming call to accept here!\n"));
578 if ( (NULL != cl->next) || (NULL != args) )
580 for (cl = cl_head; NULL != cl; cl = cl->next)
582 GNUNET_snprintf (buf, sizeof (buf),
585 if (0 == strcmp (buf, args))
592 _("There is no incoming call `%s' to accept right now!\n"),
597 GNUNET_free_non_null (peer_name);
598 peer_name = GNUNET_strdup (cl->caller_id);
599 phone_state = PS_ACCEPTED;
600 GNUNET_CONVERSATION_caller_pick_up (cl->caller,
601 &caller_event_handler,
609 * Print address information for this phone.
611 * @param args arguments given to the command
614 do_address (const char *args)
620 _("We currently do not have an address.\n"));
630 * Accepting an incoming call
632 * @param args arguments given to the command
635 do_status (const char *args)
643 _("We are currently trying to locate the private key for the ego `%s'.\n"),
648 _("We are listening for incoming calls for ego `%s' on line %u.\n"),
654 _("You are having a conversation with `%s'.\n"),
659 _("We had an internal error setting up our phone line. You can still make calls.\n"));
668 _("We are trying to find the network address to call `%s'.\n"),
673 _("We are calling `%s', his phone should be ringing.\n"),
678 _("You are having a conversation with `%s'.\n"),
682 /* ok to accept incoming call right now */
686 if ( (NULL != cl_head) &&
687 ( (cl_head != cl_active) ||
688 (cl_head != cl_tail) ) )
692 _("Calls waiting:\n"));
693 for (cl = cl_head; NULL != cl; cl = cl->next)
712 * @param args arguments given to the command
715 do_suspend (const char *args)
726 _("There is no call that could be suspended right now.\n"));
729 call_state = CS_SUSPENDED;
730 GNUNET_CONVERSATION_call_suspend (call);
741 _("There is no call that could be suspended right now.\n"));
744 /* expected state, do rejection logic */
747 GNUNET_assert (NULL != cl_active);
748 GNUNET_CONVERSATION_caller_suspend (cl_active->caller);
750 phone_state = PS_LISTEN;
757 * @param args arguments given to the command
760 do_resume (const char *args)
774 _("There is no call that could be resumed right now.\n"));
777 call_state = CS_CONNECTED;
778 GNUNET_CONVERSATION_call_resume (call,
790 _("There is no call that could be resumed right now.\n"));
793 /* expected state, do resume logic */
797 _("Already talking with `%s', cannot resume a call right now.\n"),
801 GNUNET_assert (NULL == cl_active);
806 _("There is no incoming call to resume here!\n"));
809 if ( (NULL != cl->next) || (NULL != args) )
811 for (cl = cl_head; NULL != cl; cl = cl->next)
813 GNUNET_snprintf (buf, sizeof (buf),
816 if (0 == strcmp (buf, args))
823 _("There is no incoming call `%s' to resume right now!\n"),
828 GNUNET_CONVERSATION_caller_resume (cl_active->caller,
831 phone_state = PS_ACCEPTED;
838 * @param args arguments given to the command
841 do_reject (const char *args)
848 GNUNET_CONVERSATION_call_stop (call);
858 _("There is no call that could be cancelled right now.\n"));
861 /* look for active incoming calls to refuse */
866 _("There is no incoming call to refuse here!\n"));
869 if ( (NULL != cl->next) || (NULL != args) )
871 for (cl = cl_head; NULL != cl; cl = cl->next)
873 GNUNET_snprintf (buf, sizeof (buf),
876 if (0 == strcmp (buf, args))
883 _("There is no incoming call `%s' to refuse right now!\n"),
887 GNUNET_CONVERSATION_caller_hang_up (cl->caller);
888 GNUNET_CONTAINER_DLL_remove (cl_head,
891 GNUNET_free (cl->caller_id);
895 /* expected state, do rejection logic */
896 GNUNET_assert (NULL != cl_active);
897 GNUNET_CONVERSATION_caller_hang_up (cl_active->caller);
899 phone_state = PS_LISTEN;
906 * List of supported commands.
908 static struct VoipCommand commands[] = {
909 {"/address", &do_address,
910 gettext_noop ("Use `/address' to find out which address this phone should have in GNS")},
912 gettext_noop ("Use `/call USER.gnu' to call USER")},
913 {"/accept", &do_accept,
914 gettext_noop ("Use `/accept #NUM' to accept incoming call #NUM")},
915 {"/suspend", &do_suspend,
916 gettext_noop ("Use `/suspend' to suspend the active call")},
917 {"/resume", &do_resume,
918 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.")},
919 {"/cancel", &do_reject,
920 gettext_noop ("Use `/cancel' to reject or terminate a call")},
921 {"/status", &do_status,
922 gettext_noop ("Use `/status' to print status information")},
924 gettext_noop ("Use `/quit' to terminate gnunet-conversation")},
926 gettext_noop ("Use `/help command' to get help for a specific command")},
934 * Action function to print help for the command shell.
936 * @param args arguments given to the command
939 do_help (const char *args)
944 while ( (NULL != args) &&
945 (0 != strlen (args)) &&
946 (commands[i].Action != &do_help))
949 strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
953 gettext (commands[i].helptext));
961 "Available commands:\n");
962 while (commands[i].Action != &do_help)
966 gettext (commands[i].command));
974 gettext (commands[i].helptext));
979 * Task run during shutdown.
985 do_stop_task (void *cls,
986 const struct GNUNET_SCHEDULER_TaskContext *tc)
990 GNUNET_CONVERSATION_call_stop (call);
995 GNUNET_CONVERSATION_phone_destroy (phone);
998 if (GNUNET_SCHEDULER_NO_TASK != handle_cmd_task)
1000 GNUNET_SCHEDULER_cancel (handle_cmd_task);
1001 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
1005 GNUNET_IDENTITY_disconnect (id);
1008 GNUNET_SPEAKER_destroy (speaker);
1010 GNUNET_MICROPHONE_destroy (mic);
1012 GNUNET_free (ego_name);
1014 GNUNET_CONFIGURATION_destroy (cfg);
1016 GNUNET_free_non_null (peer_name);
1017 phone_state = PS_ERROR;
1022 * Task to handle commands from the terminal.
1025 * @param tc scheduler context
1028 handle_command (void *cls,
1029 const struct GNUNET_SCHEDULER_TaskContext *tc)
1031 char message[MAX_MESSAGE_LENGTH + 1];
1036 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1038 &handle_command, NULL);
1039 /* read message from command line and handle it */
1040 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
1041 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
1043 if (0 == strlen (message))
1045 if (message[strlen (message) - 1] == '\n')
1046 message[strlen (message) - 1] = '\0';
1047 if (0 == strlen (message))
1050 while ((NULL != commands[i].command) &&
1051 (0 != strncasecmp (commands[i].command, message,
1052 strlen (commands[i].command))))
1054 ptr = &message[strlen (commands[i].command)];
1055 while (isspace ((int) *ptr))
1059 commands[i].Action (ptr);
1064 * Function called by identity service with information about egos.
1067 * @param ego ego handle
1069 * @param name name of the ego
1072 identity_cb (void *cls,
1073 struct GNUNET_IDENTITY_Ego *ego,
1079 if (ego == caller_id)
1083 _("Name of our ego changed to `%s'\n"),
1085 GNUNET_free (ego_name);
1086 ego_name = GNUNET_strdup (name);
1089 if (0 != strcmp (name,
1096 _("Our ego `%s' was deleted!\n"),
1102 GNUNET_CONFIGURATION_set_value_number (cfg,
1111 * Main function that will be run by the scheduler.
1113 * @param cls closure
1114 * @param args remaining command-line arguments
1115 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1116 * @param c configuration
1121 const char *cfgfile,
1122 const struct GNUNET_CONFIGURATION_Handle *c)
1124 cfg = GNUNET_CONFIGURATION_dup (c);
1125 speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
1126 mic = GNUNET_MICROPHONE_create_from_hardware (cfg);
1127 if (NULL == ego_name)
1131 _("You must specify the NAME of an ego to use\n"));
1134 id = GNUNET_IDENTITY_connect (cfg,
1138 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
1139 &handle_command, NULL);
1140 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
1146 * The main function to conversation.
1148 * @param argc number of arguments from the command line
1149 * @param argv command line arguments
1150 * @return 0 ok, 1 on error
1153 main (int argc, char *const *argv)
1155 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1156 {'e', "ego", "NAME",
1157 gettext_noop ("sets the NAME of the ego to use for the phone (and name resolution)"),
1158 1, &GNUNET_GETOPT_set_string, &ego_name},
1159 {'p', "phone", "LINE",
1160 gettext_noop ("sets the LINE to use for the phone"),
1161 1, &GNUNET_GETOPT_set_uint, &line},
1162 GNUNET_GETOPT_OPTION_END
1167 flags = fcntl (0, F_GETFL, 0);
1168 flags |= O_NONBLOCK;
1169 fcntl (0, F_SETFL, flags);
1170 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1171 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1173 ret = GNUNET_PROGRAM_run (argc, argv,
1174 "gnunet-conversation",
1175 gettext_noop ("Enables having a conversation with other GNUnet users."),
1176 options, &run, NULL);
1177 GNUNET_free ((void *) argv);
1178 return (GNUNET_OK == ret) ? 0 : 1;
1181 /* end of gnunet-conversation.c */