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,
357 _("Phone active on line %u\n"),
358 (unsigned int) line);
359 phone_state = PS_LISTEN;
365 * Function called with an event emitted by a call.
367 * @param cls closure, NULL
368 * @param code type of the event on the call
371 call_event_handler (void *cls,
372 enum GNUNET_CONVERSATION_CallEventCode code)
376 case GNUNET_CONVERSATION_EC_CALL_RINGING:
377 GNUNET_break (CS_RESOLVING == call_state);
379 _("Resolved address of `%s'. 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 GNUNET_assert (NULL == call);
528 call = GNUNET_CONVERSATION_call_start (cfg,
533 &call_event_handler, NULL);
538 * Accepting an incoming call
540 * @param args arguments given to the command
543 do_accept (const char *args)
548 if ( (NULL != call) &&
549 (CS_SUSPENDED != call_state) )
552 _("You are calling someone else already, hang up first!\n"));
561 /* this is the expected state */
565 _("You are answering call from `%s', hang up or suspend that call first!\n"),
576 _("There is no incoming call to accept here!\n"));
579 if ( (NULL != cl->next) || (NULL != args) )
581 for (cl = cl_head; NULL != cl; cl = cl->next)
583 GNUNET_snprintf (buf, sizeof (buf),
586 if (0 == strcmp (buf, args))
593 _("There is no incoming call `%s' to accept right now!\n"),
598 GNUNET_free_non_null (peer_name);
599 peer_name = GNUNET_strdup (cl->caller_id);
600 phone_state = PS_ACCEPTED;
601 GNUNET_CONVERSATION_caller_pick_up (cl->caller,
602 &caller_event_handler,
610 * Print address information for this phone.
612 * @param args arguments given to the command
615 do_address (const char *args)
621 _("We currently do not have an address.\n"));
631 * Accepting an incoming call
633 * @param args arguments given to the command
636 do_status (const char *args)
644 _("We are currently trying to locate the private key for the ego `%s'.\n"),
649 _("We are listening for incoming calls for ego `%s' on line %u.\n"),
655 _("You are having a conversation with `%s'.\n"),
660 _("We had an internal error setting up our phone line. You can still make calls.\n"));
669 _("We are trying to find the network address to call `%s'.\n"),
674 _("We are calling `%s', his phone should be ringing.\n"),
679 _("You are having a conversation with `%s'.\n"),
683 /* ok to accept incoming call right now */
687 if ( (NULL != cl_head) &&
688 ( (cl_head != cl_active) ||
689 (cl_head != cl_tail) ) )
693 _("Calls waiting:\n"));
694 for (cl = cl_head; NULL != cl; cl = cl->next)
713 * @param args arguments given to the command
716 do_suspend (const char *args)
727 _("There is no call that could be suspended right now.\n"));
730 call_state = CS_SUSPENDED;
731 GNUNET_CONVERSATION_call_suspend (call);
742 _("There is no call that could be suspended right now.\n"));
745 /* expected state, do rejection logic */
748 GNUNET_assert (NULL != cl_active);
749 GNUNET_CONVERSATION_caller_suspend (cl_active->caller);
751 phone_state = PS_LISTEN;
758 * @param args arguments given to the command
761 do_resume (const char *args)
775 _("There is no call that could be resumed right now.\n"));
778 call_state = CS_CONNECTED;
779 GNUNET_CONVERSATION_call_resume (call,
791 _("There is no call that could be resumed right now.\n"));
794 /* expected state, do resume logic */
798 _("Already talking with `%s', cannot resume a call right now.\n"),
802 GNUNET_assert (NULL == cl_active);
807 _("There is no incoming call to resume here!\n"));
810 if ( (NULL != cl->next) || (NULL != args) )
812 for (cl = cl_head; NULL != cl; cl = cl->next)
814 GNUNET_snprintf (buf, sizeof (buf),
817 if (0 == strcmp (buf, args))
824 _("There is no incoming call `%s' to resume right now!\n"),
829 GNUNET_CONVERSATION_caller_resume (cl_active->caller,
832 phone_state = PS_ACCEPTED;
839 * @param args arguments given to the command
842 do_reject (const char *args)
849 GNUNET_CONVERSATION_call_stop (call);
859 _("There is no call that could be cancelled right now.\n"));
862 /* look for active incoming calls to refuse */
867 _("There is no incoming call to refuse here!\n"));
870 if ( (NULL != cl->next) || (NULL != args) )
872 for (cl = cl_head; NULL != cl; cl = cl->next)
874 GNUNET_snprintf (buf, sizeof (buf),
877 if (0 == strcmp (buf, args))
884 _("There is no incoming call `%s' to refuse right now!\n"),
888 GNUNET_CONVERSATION_caller_hang_up (cl->caller);
889 GNUNET_CONTAINER_DLL_remove (cl_head,
892 GNUNET_free (cl->caller_id);
896 /* expected state, do rejection logic */
897 GNUNET_assert (NULL != cl_active);
898 GNUNET_CONVERSATION_caller_hang_up (cl_active->caller);
900 phone_state = PS_LISTEN;
907 * List of supported commands.
909 static struct VoipCommand commands[] = {
910 {"/address", &do_address,
911 gettext_noop ("Use `/address' to find out which address this phone should have in GNS")},
913 gettext_noop ("Use `/call USER.gnu' to call USER")},
914 {"/accept", &do_accept,
915 gettext_noop ("Use `/accept #NUM' to accept incoming call #NUM")},
916 {"/suspend", &do_suspend,
917 gettext_noop ("Use `/suspend' to suspend the active call")},
918 {"/resume", &do_resume,
919 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.")},
920 {"/cancel", &do_reject,
921 gettext_noop ("Use `/cancel' to reject or terminate a call")},
922 {"/status", &do_status,
923 gettext_noop ("Use `/status' to print status information")},
925 gettext_noop ("Use `/quit' to terminate gnunet-conversation")},
927 gettext_noop ("Use `/help command' to get help for a specific command")},
935 * Action function to print help for the command shell.
937 * @param args arguments given to the command
940 do_help (const char *args)
945 while ( (NULL != args) &&
946 (0 != strlen (args)) &&
947 (commands[i].Action != &do_help))
950 strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
954 gettext (commands[i].helptext));
962 "Available commands:\n");
963 while (commands[i].Action != &do_help)
967 gettext (commands[i].command));
975 gettext (commands[i].helptext));
980 * Task run during shutdown.
986 do_stop_task (void *cls,
987 const struct GNUNET_SCHEDULER_TaskContext *tc)
991 GNUNET_CONVERSATION_call_stop (call);
996 GNUNET_CONVERSATION_phone_destroy (phone);
999 if (GNUNET_SCHEDULER_NO_TASK != handle_cmd_task)
1001 GNUNET_SCHEDULER_cancel (handle_cmd_task);
1002 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
1006 GNUNET_IDENTITY_disconnect (id);
1009 GNUNET_SPEAKER_destroy (speaker);
1011 GNUNET_MICROPHONE_destroy (mic);
1013 GNUNET_free (ego_name);
1015 GNUNET_free_non_null (peer_name);
1016 phone_state = PS_ERROR;
1021 * Task to handle commands from the terminal.
1024 * @param tc scheduler context
1027 handle_command (void *cls,
1028 const struct GNUNET_SCHEDULER_TaskContext *tc)
1030 char message[MAX_MESSAGE_LENGTH + 1];
1035 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1037 &handle_command, NULL);
1038 /* read message from command line and handle it */
1039 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
1040 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
1042 if (0 == strlen (message))
1044 if (message[strlen (message) - 1] == '\n')
1045 message[strlen (message) - 1] = '\0';
1046 if (0 == strlen (message))
1049 while ((NULL != commands[i].command) &&
1050 (0 != strncasecmp (commands[i].command, message,
1051 strlen (commands[i].command))))
1053 ptr = &message[strlen (commands[i].command)];
1054 while (isspace ((int) *ptr))
1058 commands[i].Action (ptr);
1063 * Function called by identity service with information about egos.
1066 * @param ego ego handle
1068 * @param name name of the ego
1071 identity_cb (void *cls,
1072 struct GNUNET_IDENTITY_Ego *ego,
1078 if (ego == caller_id)
1082 _("Name of our ego changed to `%s'\n"),
1084 GNUNET_free (ego_name);
1085 ego_name = GNUNET_strdup (name);
1088 if (0 != strcmp (name,
1095 _("Our ego `%s' was deleted!\n"),
1101 GNUNET_CONFIGURATION_set_value_number (cfg,
1110 * Main function that will be run by the scheduler.
1112 * @param cls closure
1113 * @param args remaining command-line arguments
1114 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1115 * @param c configuration
1120 const char *cfgfile,
1121 const struct GNUNET_CONFIGURATION_Handle *c)
1123 cfg = GNUNET_CONFIGURATION_dup (c);
1124 speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
1125 mic = GNUNET_MICROPHONE_create_from_hardware (cfg);
1126 if (NULL == ego_name)
1130 _("You must specify the NAME of an ego to use\n"));
1133 id = GNUNET_IDENTITY_connect (cfg,
1137 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
1138 &handle_command, NULL);
1139 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
1145 * The main function to conversation.
1147 * @param argc number of arguments from the command line
1148 * @param argv command line arguments
1149 * @return 0 ok, 1 on error
1152 main (int argc, char *const *argv)
1154 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1155 {'e', "ego", "NAME",
1156 gettext_noop ("sets the NAME of the ego to use for the phone (and name resolution)"),
1157 1, &GNUNET_GETOPT_set_string, &ego_name},
1158 {'p', "phone", "LINE",
1159 gettext_noop ("sets the LINE to use for the phone"),
1160 1, &GNUNET_GETOPT_set_uint, &line},
1161 GNUNET_GETOPT_OPTION_END
1166 flags = fcntl (0, F_GETFL, 0);
1167 flags |= O_NONBLOCK;
1168 fcntl (0, F_SETFL, flags);
1169 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1170 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1172 ret = GNUNET_PROGRAM_run (argc, argv,
1173 "gnunet-conversation",
1174 gettext_noop ("Enables having a conversation with other GNUnet users."),
1175 options, &run, NULL);
1176 GNUNET_free ((void *) argv);
1179 GNUNET_CONFIGURATION_destroy (cfg);
1182 return (GNUNET_OK == ret) ? 0 : 1;
1185 /* end of gnunet-conversation.c */