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 suspended 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)
845 GNUNET_CONVERSATION_call_stop (call);
856 _("There is no call that could be cancelled right now.\n"));
859 /* expected state, do rejection logic */
862 GNUNET_assert (NULL != cl_active);
863 GNUNET_CONVERSATION_caller_hang_up (cl_active->caller);
865 phone_state = PS_LISTEN;
870 * List of supported commands.
872 static struct VoipCommand commands[] = {
873 {"/address", &do_address,
874 gettext_noop ("Use `/address' to find out which address this phone should have in GNS")},
876 gettext_noop ("Use `/call USER.gnu' to call USER")},
877 {"/accept", &do_accept,
878 gettext_noop ("Use `/accept #NUM' to accept incoming call #NUM")},
879 {"/suspend", &do_suspend,
880 gettext_noop ("Use `/suspend' to suspend the active call")},
881 {"/resume", &do_resume,
882 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.")},
883 {"/cancel", &do_reject,
884 gettext_noop ("Use `/cancel' to reject or terminate a call")},
885 {"/status", &do_status,
886 gettext_noop ("Use `/status' to print status information")},
888 gettext_noop ("Use `/quit' to terminate gnunet-conversation")},
890 gettext_noop ("Use `/help command' to get help for a specific command")},
898 * Action function to print help for the command shell.
900 * @param args arguments given to the command
903 do_help (const char *args)
908 while ( (NULL != args) &&
909 (0 != strlen (args)) &&
910 (commands[i].Action != &do_help))
913 strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
917 gettext (commands[i].helptext));
925 "Available commands:\n");
926 while (commands[i].Action != &do_help)
930 gettext (commands[i].command));
938 gettext (commands[i].helptext));
943 * Task run during shutdown.
949 do_stop_task (void *cls,
950 const struct GNUNET_SCHEDULER_TaskContext *tc)
954 GNUNET_CONVERSATION_call_stop (call);
959 GNUNET_CONVERSATION_phone_destroy (phone);
962 if (GNUNET_SCHEDULER_NO_TASK != handle_cmd_task)
964 GNUNET_SCHEDULER_cancel (handle_cmd_task);
965 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
969 GNUNET_IDENTITY_disconnect (id);
972 GNUNET_SPEAKER_destroy (speaker);
974 GNUNET_MICROPHONE_destroy (mic);
976 GNUNET_free (ego_name);
978 GNUNET_CONFIGURATION_destroy (cfg);
980 GNUNET_free_non_null (peer_name);
981 phone_state = PS_ERROR;
986 * Task to handle commands from the terminal.
989 * @param tc scheduler context
992 handle_command (void *cls,
993 const struct GNUNET_SCHEDULER_TaskContext *tc)
995 char message[MAX_MESSAGE_LENGTH + 1];
1000 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1002 &handle_command, NULL);
1003 /* read message from command line and handle it */
1004 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
1005 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
1007 if (0 == strlen (message))
1009 if (message[strlen (message) - 1] == '\n')
1010 message[strlen (message) - 1] = '\0';
1011 if (0 == strlen (message))
1014 while ((NULL != commands[i].command) &&
1015 (0 != strncasecmp (commands[i].command, message,
1016 strlen (commands[i].command))))
1018 ptr = &message[strlen (commands[i].command)];
1019 while (isspace ((int) *ptr))
1021 commands[i].Action (ptr);
1026 * Function called by identity service with information about egos.
1029 * @param ego ego handle
1031 * @param name name of the ego
1034 identity_cb (void *cls,
1035 struct GNUNET_IDENTITY_Ego *ego,
1041 if (ego == caller_id)
1045 _("Name of our ego changed to `%s'\n"),
1047 GNUNET_free (ego_name);
1048 ego_name = GNUNET_strdup (name);
1051 if (0 != strcmp (name,
1058 _("Our ego `%s' was deleted!\n"),
1064 GNUNET_CONFIGURATION_set_value_number (cfg,
1073 * Main function that will be run by the scheduler.
1075 * @param cls closure
1076 * @param args remaining command-line arguments
1077 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1078 * @param c configuration
1083 const char *cfgfile,
1084 const struct GNUNET_CONFIGURATION_Handle *c)
1086 cfg = GNUNET_CONFIGURATION_dup (c);
1087 speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
1088 mic = GNUNET_MICROPHONE_create_from_hardware (cfg);
1089 if (NULL == ego_name)
1093 _("You must specify the NAME of an ego to use\n"));
1096 id = GNUNET_IDENTITY_connect (cfg,
1100 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
1101 &handle_command, NULL);
1102 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
1108 * The main function to conversation.
1110 * @param argc number of arguments from the command line
1111 * @param argv command line arguments
1112 * @return 0 ok, 1 on error
1115 main (int argc, char *const *argv)
1117 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1118 {'e', "ego", "NAME",
1119 gettext_noop ("sets the NAME of the ego to use for the phone (and name resolution)"),
1120 1, &GNUNET_GETOPT_set_string, &ego_name},
1121 {'p', "phone", "LINE",
1122 gettext_noop ("sets the LINE to use for the phone"),
1123 1, &GNUNET_GETOPT_set_uint, &line},
1124 GNUNET_GETOPT_OPTION_END
1129 flags = fcntl (0, F_GETFL, 0);
1130 flags |= O_NONBLOCK;
1131 fcntl (0, F_SETFL, flags);
1132 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1133 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1135 ret = GNUNET_PROGRAM_run (argc, argv,
1136 "gnunet-conversation",
1137 gettext_noop ("Enables having a conversation with other GNUnet users."),
1138 options, &run, NULL);
1139 GNUNET_free ((void *) argv);
1140 return (GNUNET_OK == ret) ? 0 : 1;
1143 /* end of gnunet-conversation.c */