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"
33 #include "../util/gnunet-helper-w32-console.h"
37 * Maximum length allowed for the command line input.
39 #define MAX_MESSAGE_LENGTH 1024
41 #define XSTRINGIFY(x) STRINGIFY(x)
43 #define STRINGIFY(x) (#x)
47 * Helper that reads the console for us.
49 struct GNUNET_HELPER_Handle *stdin_hlp;
53 * Possible states of the phone.
58 * We're waiting for our own idenitty.
63 * We're listening for calls
68 * We accepted an incoming phone call.
80 * States for current outgoing call.
85 * We are looking up some other participant.
90 * We are now ringing the other participant.
95 * The other party accepted our call and we are now connected.
100 * The call is currently suspended (by us).
109 * List of incoming calls
117 struct CallList *prev;
122 struct CallList *next;
125 * Handle to hang up or activate.
127 struct GNUNET_CONVERSATION_Caller *caller;
130 * String identifying the caller.
135 * Unique number of the call.
137 unsigned int caller_num;
146 static struct GNUNET_CONVERSATION_Phone *phone;
149 * Call handle (for active outgoing call).
151 static struct GNUNET_CONVERSATION_Call *call;
154 * Caller handle (for active incoming call).
156 static struct CallList *cl_active;
159 * Head of calls waiting to be accepted.
161 static struct CallList *cl_head;
164 * Tail of calls waiting to be accepted.
166 static struct CallList *cl_tail;
169 * Desired phone line.
171 static unsigned int line;
174 * Task which handles the commands
176 static GNUNET_SCHEDULER_TaskIdentifier handle_cmd_task;
181 static struct GNUNET_SPEAKER_Handle *speaker;
186 static struct GNUNET_MICROPHONE_Handle *mic;
191 static struct GNUNET_CONFIGURATION_Handle *cfg;
196 static struct GNUNET_IDENTITY_Ego *caller_id;
199 * Handle to identity service.
201 static struct GNUNET_IDENTITY_Handle *id;
206 static char *ego_name;
209 * Name of conversation partner (if any).
211 static char *peer_name;
214 * File handle for stdin.
216 static struct GNUNET_DISK_FileHandle *stdin_fh;
219 * Our phone's current state.
221 static enum PhoneState phone_state;
224 * Our call's current state.
226 static enum CallState call_state;
229 * Counts the number of incoming calls we have had so far.
231 static unsigned int caller_num_gen;
234 * GNS address for this phone.
236 static char *address;
245 * Function called with an event emitted by a phone.
248 * @param code type of the event
249 * @param caller handle for the caller
250 * @param caller_id name of the caller in GNS
253 phone_event_handler (void *cls,
254 enum GNUNET_CONVERSATION_PhoneEventCode code,
255 struct GNUNET_CONVERSATION_Caller *caller,
256 const char *caller_id)
262 case GNUNET_CONVERSATION_EC_PHONE_RING:
264 _("Incoming call from `%s'. Please /accept #%u or /cancel %u the call.\n"),
268 cl = GNUNET_new (struct CallList);
270 cl->caller_id = GNUNET_strdup (caller_id);
271 cl->caller_num = caller_num_gen++;
272 GNUNET_CONTAINER_DLL_insert (cl_head,
276 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
277 for (cl = cl_head; NULL != cl; cl = cl->next)
278 if (caller == cl->caller)
286 _("Call from `%s' terminated\n"),
288 GNUNET_CONTAINER_DLL_remove (cl_head,
291 GNUNET_free (cl->caller_id);
295 phone_state = PS_LISTEN;
304 * Function called with an event emitted by a caller.
306 * @param cls closure with the `struct CallList` of the caller
307 * @param code type of the event issued by the caller
310 caller_event_handler (void *cls,
311 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"),
322 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
324 _("Call from `%s' resumed by other user\n"),
337 struct GNUNET_GNSRECORD_Data rd;
339 if (NULL == caller_id)
342 _("Ego `%s' no longer available, phone is now down.\n"),
344 phone_state = PS_LOOKUP_EGO;
347 GNUNET_assert (NULL == phone);
348 phone = GNUNET_CONVERSATION_phone_create (cfg,
350 &phone_event_handler, NULL);
351 /* FIXME: get record and print full GNS record info later here... */
356 _("Failed to setup phone (internal error)\n"));
357 phone_state = PS_ERROR;
361 GNUNET_CONVERSATION_phone_get_record (phone,
363 GNUNET_free_non_null (address);
364 address = GNUNET_GNSRECORD_value_to_string (rd.record_type,
368 _("Phone active on line %u. Type `/help' for a list of available commands\n"),
369 (unsigned int) line);
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,
383 enum GNUNET_CONVERSATION_CallEventCode code)
387 case GNUNET_CONVERSATION_EC_CALL_RINGING:
388 GNUNET_break (CS_RESOLVING == call_state);
390 _("Resolved address of `%s'. Now ringing other party.\n"),
392 call_state = CS_RINGING;
394 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
395 GNUNET_break (CS_RINGING == call_state);
397 _("Connection established to `%s'\n"),
399 call_state = CS_CONNECTED;
401 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
402 GNUNET_break (CS_RESOLVING == call_state);
404 _("Failed to resolve `%s'\n"),
408 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
411 _("Call terminated\n"));
414 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
415 GNUNET_break (CS_CONNECTED == call_state);
417 _("Connection to `%s' suspended (by other user)\n"),
420 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
421 GNUNET_break (CS_CONNECTED == call_state);
423 _("Connection to `%s' resumed (by other user)\n"),
426 case GNUNET_CONVERSATION_EC_CALL_ERROR:
428 _("Error with the call, restarting it\n"));
429 call_state = CS_RESOLVING;
436 * Function declareation for executing a action
438 * @param arguments arguments given to the function
440 typedef void (*ActionFunction) (const char *arguments);
444 * Structure which defines a command
449 * Command the user needs to enter.
454 * Function to call on command.
456 ActionFunction Action;
459 * Help text for the command.
461 const char *helptext;
466 * Action function to print help for the command shell.
468 * @param args arguments given to the command
471 do_help (const char *args);
475 * Terminate the client
477 * @param args arguments given to the command
480 do_quit (const char *args)
482 GNUNET_SCHEDULER_shutdown ();
487 * Handler for unknown command.
489 * @param msg arguments given to the command
492 do_unknown (const char *msg)
495 _("Unknown command `%s'\n"),
501 * Initiating a new call
503 * @param arg arguments given to the command
506 do_call (const char *arg)
508 if (NULL == caller_id)
511 _("Ego `%s' not available\n"),
518 _("You are calling someone else already, hang up first!\n"));
525 _("Ego `%s' not available\n"),
533 _("You are answering call from `%s', hang up or suspend that call first!\n"),
540 GNUNET_free_non_null (peer_name);
544 _("Call recipient missing.\n"));
548 peer_name = GNUNET_strdup (arg);
549 call_state = CS_RESOLVING;
550 GNUNET_assert (NULL == call);
551 call = GNUNET_CONVERSATION_call_start (cfg,
556 &call_event_handler, NULL);
561 * Accepting an incoming call
563 * @param args arguments given to the command
566 do_accept (const char *args)
571 if ( (NULL != call) &&
572 (CS_SUSPENDED != call_state) )
575 _("You are calling someone else already, hang up first!\n"));
584 /* this is the expected state */
588 _("You are answering call from `%s', hang up or suspend that call first!\n"),
599 _("There is no incoming call to accept here!\n"));
602 if ( (NULL != cl->next) || (NULL != args) )
604 for (cl = cl_head; NULL != cl; cl = cl->next)
606 GNUNET_snprintf (buf, sizeof (buf),
609 if (0 == strcmp (buf, args))
616 _("There is no incoming call `%s' to accept right now!\n"),
620 GNUNET_CONTAINER_DLL_remove (cl_head,
624 GNUNET_free_non_null (peer_name);
625 peer_name = GNUNET_strdup (cl->caller_id);
626 phone_state = PS_ACCEPTED;
627 GNUNET_CONVERSATION_caller_pick_up (cl->caller,
628 &caller_event_handler,
636 * Print address information for this phone.
638 * @param args arguments given to the command
641 do_address (const char *args)
647 _("We currently do not have an address.\n"));
657 * Accepting an incoming call
659 * @param args arguments given to the command
662 do_status (const char *args)
670 _("We are currently trying to locate the private key for the ego `%s'.\n"),
675 _("We are listening for incoming calls for ego `%s' on line %u.\n"),
681 _("You are having a conversation with `%s'.\n"),
686 _("We had an internal error setting up our phone line. You can still make calls.\n"));
695 _("We are trying to find the network address to call `%s'.\n"),
700 _("We are calling `%s', his phone should be ringing.\n"),
705 _("You are having a conversation with `%s'.\n"),
709 /* ok to accept incoming call right now */
713 if ( (NULL != cl_head) &&
714 ( (cl_head != cl_active) ||
715 (cl_head != cl_tail) ) )
719 _("Calls waiting:\n"));
720 for (cl = cl_head; NULL != cl; cl = cl->next)
739 * @param args arguments given to the command
742 do_suspend (const char *args)
753 _("There is no call that could be suspended right now.\n"));
756 call_state = CS_SUSPENDED;
757 GNUNET_CONVERSATION_call_suspend (call);
768 _("There is no call that could be suspended right now.\n"));
771 /* expected state, do rejection logic */
774 GNUNET_assert (NULL != cl_active);
775 GNUNET_CONVERSATION_caller_suspend (cl_active->caller);
777 phone_state = PS_LISTEN;
784 * @param args arguments given to the command
787 do_resume (const char *args)
801 _("There is no call that could be resumed right now.\n"));
804 call_state = CS_CONNECTED;
805 GNUNET_CONVERSATION_call_resume (call,
817 _("There is no call that could be resumed right now.\n"));
820 /* expected state, do resume logic */
824 _("Already talking with `%s', cannot resume a call right now.\n"),
828 GNUNET_assert (NULL == cl_active);
833 _("There is no incoming call to resume here!\n"));
836 if ( (NULL != cl->next) || (NULL != args) )
838 for (cl = cl_head; NULL != cl; cl = cl->next)
840 GNUNET_snprintf (buf, sizeof (buf),
843 if (0 == strcmp (buf, args))
850 _("There is no incoming call `%s' to resume right now!\n"),
855 GNUNET_CONVERSATION_caller_resume (cl_active->caller,
858 phone_state = PS_ACCEPTED;
865 * @param args arguments given to the command
868 do_reject (const char *args)
875 GNUNET_CONVERSATION_call_stop (call);
885 _("There is no call that could be cancelled right now.\n"));
888 /* look for active incoming calls to refuse */
893 _("There is no incoming call to refuse here!\n"));
896 if ( (NULL != cl->next) || (NULL != args) )
898 for (cl = cl_head; NULL != cl; cl = cl->next)
900 GNUNET_snprintf (buf, sizeof (buf),
903 if (0 == strcmp (buf, args))
910 _("There is no incoming call `%s' to refuse right now!\n"),
914 GNUNET_CONVERSATION_caller_hang_up (cl->caller);
915 GNUNET_CONTAINER_DLL_remove (cl_head,
918 GNUNET_free (cl->caller_id);
922 /* expected state, do rejection logic */
923 GNUNET_assert (NULL != cl_active);
924 GNUNET_CONVERSATION_caller_hang_up (cl_active->caller);
926 phone_state = PS_LISTEN;
933 * List of supported commands.
935 static struct VoipCommand commands[] = {
936 {"/address", &do_address,
937 gettext_noop ("Use `/address' to find out which address this phone should have in GNS")},
939 gettext_noop ("Use `/call USER.gnu' to call USER")},
940 {"/accept", &do_accept,
941 gettext_noop ("Use `/accept #NUM' to accept incoming call #NUM")},
942 {"/suspend", &do_suspend,
943 gettext_noop ("Use `/suspend' to suspend the active call")},
944 {"/resume", &do_resume,
945 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.")},
946 {"/cancel", &do_reject,
947 gettext_noop ("Use `/cancel' to reject or terminate a call")},
948 {"/status", &do_status,
949 gettext_noop ("Use `/status' to print status information")},
951 gettext_noop ("Use `/quit' to terminate gnunet-conversation")},
953 gettext_noop ("Use `/help command' to get help for a specific command")},
961 * Action function to print help for the command shell.
963 * @param args arguments given to the command
966 do_help (const char *args)
971 while ( (NULL != args) &&
972 (0 != strlen (args)) &&
973 (commands[i].Action != &do_help))
976 strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
980 gettext (commands[i].helptext));
988 "Available commands:\n");
989 while (commands[i].Action != &do_help)
993 gettext (commands[i].command));
1001 gettext (commands[i].helptext));
1006 * Task run during shutdown.
1012 do_stop_task (void *cls,
1013 const struct GNUNET_SCHEDULER_TaskContext *tc)
1016 if (NULL != stdin_hlp)
1018 GNUNET_HELPER_stop (stdin_hlp, GNUNET_NO);
1024 GNUNET_CONVERSATION_call_stop (call);
1029 GNUNET_CONVERSATION_phone_destroy (phone);
1032 if (GNUNET_SCHEDULER_NO_TASK != handle_cmd_task)
1034 GNUNET_SCHEDULER_cancel (handle_cmd_task);
1035 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
1039 GNUNET_IDENTITY_disconnect (id);
1042 GNUNET_SPEAKER_destroy (speaker);
1044 GNUNET_MICROPHONE_destroy (mic);
1046 GNUNET_free (ego_name);
1048 GNUNET_free_non_null (peer_name);
1049 phone_state = PS_ERROR;
1053 handle_command_string (char *message, size_t str_len)
1060 if (message[str_len - 1] == '\n')
1061 message[str_len - 1] = '\0';
1062 if (message[str_len - 2] == '\r')
1063 message[str_len - 2] = '\0';
1064 if (0 == strlen (message))
1067 while ((NULL != commands[i].command) &&
1068 (0 != strncasecmp (commands[i].command, message,
1069 strlen (commands[i].command))))
1071 ptr = &message[strlen (commands[i].command)];
1072 while (isspace ((int) *ptr))
1076 commands[i].Action (ptr);
1082 console_reader_chars (void *cls, void *client,
1083 const struct GNUNET_MessageHeader *message)
1087 switch (ntohs (message->type))
1089 case GNUNET_MESSAGE_TYPE_W32_CONSOLE_HELPER_CHARS:
1090 chars = (char *) &message[1];
1091 str_size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
1092 if (chars[str_size - 1] != '\0')
1093 return GNUNET_SYSERR;
1094 /* FIXME: is it ok that we pass part of a const struct to
1095 * this function that may mangle the contents?
1097 handle_command_string (chars, str_size - 1);
1108 * Task to handle commands from the terminal.
1111 * @param tc scheduler context
1114 handle_command (void *cls,
1115 const struct GNUNET_SCHEDULER_TaskContext *tc)
1117 char message[MAX_MESSAGE_LENGTH + 1];
1120 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1122 &handle_command, NULL);
1123 /* read message from command line and handle it */
1124 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
1125 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
1127 handle_command_string (message, strlen (message));
1132 * Function called by identity service with information about egos.
1135 * @param ego ego handle
1137 * @param name name of the ego
1140 identity_cb (void *cls,
1141 struct GNUNET_IDENTITY_Ego *ego,
1147 if (ego == caller_id)
1151 _("Name of our ego changed to `%s'\n"),
1153 GNUNET_free (ego_name);
1154 ego_name = GNUNET_strdup (name);
1157 if (0 != strcmp (name,
1164 _("Our ego `%s' was deleted!\n"),
1170 GNUNET_CONFIGURATION_set_value_number (cfg,
1179 * Main function that will be run by the scheduler.
1181 * @param cls closure
1182 * @param args remaining command-line arguments
1183 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1184 * @param c configuration
1189 const char *cfgfile,
1190 const struct GNUNET_CONFIGURATION_Handle *c)
1192 cfg = GNUNET_CONFIGURATION_dup (c);
1193 speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
1194 mic = GNUNET_MICROPHONE_create_from_hardware (cfg);
1195 if (NULL == ego_name)
1199 _("You must specify the NAME of an ego to use\n"));
1202 id = GNUNET_IDENTITY_connect (cfg,
1206 if (stdin_fh == NULL)
1208 static char cpid[64];
1209 static char *args[] = {"gnunet-helper-w32-console.exe", "chars",
1210 XSTRINGIFY (MAX_MESSAGE_LENGTH), cpid, NULL};
1211 snprintf (cpid, 64, "%d", GetCurrentProcessId ());
1212 stdin_hlp = GNUNET_HELPER_start (
1214 "gnunet-helper-w32-console",
1216 console_reader_chars,
1219 if (NULL == stdin_hlp)
1223 _("Failed to start gnunet-helper-w32-console\n"));
1230 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
1231 &handle_command, NULL);
1232 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
1238 * The main function to conversation.
1240 * @param argc number of arguments from the command line
1241 * @param argv command line arguments
1242 * @return 0 ok, 1 on error
1245 main (int argc, char *const *argv)
1247 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1248 {'e', "ego", "NAME",
1249 gettext_noop ("sets the NAME of the ego to use for the phone (and name resolution)"),
1250 1, &GNUNET_GETOPT_set_string, &ego_name},
1251 {'p', "phone", "LINE",
1252 gettext_noop ("sets the LINE to use for the phone"),
1253 1, &GNUNET_GETOPT_set_uint, &line},
1254 GNUNET_GETOPT_OPTION_END
1259 flags = fcntl (0, F_GETFL, 0);
1260 flags |= O_NONBLOCK;
1261 fcntl (0, F_SETFL, flags);
1262 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1264 if (FILE_TYPE_CHAR == GetFileType ((HANDLE) _get_osfhandle (0)))
1269 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1272 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1274 ret = GNUNET_PROGRAM_run (argc, argv,
1275 "gnunet-conversation",
1276 gettext_noop ("Enables having a conversation with other GNUnet users."),
1277 options, &run, NULL);
1278 GNUNET_free ((void *) argv);
1281 GNUNET_CONFIGURATION_destroy (cfg);
1284 return (GNUNET_OK == ret) ? 0 : 1;
1287 /* end of gnunet-conversation.c */