2 This file is part of GNUnet.
3 Copyright (C) 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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).
108 * List of incoming calls
116 struct CallList *prev;
121 struct CallList *next;
124 * Handle to hang up or activate.
126 struct GNUNET_CONVERSATION_Caller *caller;
129 * Public key identifying the caller.
131 struct GNUNET_CRYPTO_EcdsaPublicKey caller_id;
134 * Unique number of the call.
136 unsigned int caller_num;
145 static struct GNUNET_CONVERSATION_Phone *phone;
148 * Call handle (for active outgoing call).
150 static struct GNUNET_CONVERSATION_Call *call;
153 * Caller handle (for active incoming call).
154 * This call handler is NOT in the #cl_head / #cl_tail list.
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 (string to be converted to a hash).
174 * Task which handles the commands
176 static struct GNUNET_SCHEDULER_Task * 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 *my_caller_id;
199 * Handle to identity service.
201 static struct GNUNET_IDENTITY_Handle *id;
206 static char *ego_name;
209 * Public key of active conversation partner (if any).
211 static struct GNUNET_CRYPTO_EcdsaPublicKey peer_key;
214 * Name of active conversation partner (if any).
216 static char *peer_name;
219 * File handle for stdin.
221 static struct GNUNET_DISK_FileHandle *stdin_fh;
224 * Our phone's current state.
226 static enum PhoneState phone_state;
229 * Our call's current state.
231 static enum CallState call_state;
234 * Counts the number of incoming calls we have had so far.
236 static unsigned int caller_num_gen;
239 * GNS address for this phone.
241 static char *address;
250 * Function called with an event emitted by a phone.
253 * @param code type of the event
254 * @param caller handle for the caller
255 * @param caller_id public key of the caller (in GNS)
258 phone_event_handler (void *cls,
259 enum GNUNET_CONVERSATION_PhoneEventCode code,
260 struct GNUNET_CONVERSATION_Caller *caller,
261 const struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
268 case GNUNET_CONVERSATION_EC_PHONE_RING:
270 _("Incoming call from `%s'. Please /accept %u or /cancel %u the call.\n"),
271 GNUNET_GNSRECORD_pkey_to_zkey (caller_id),
274 cl = GNUNET_new (struct CallList);
276 cl->caller_id = *caller_id;
277 cl->caller_num = caller_num_gen++;
278 GNUNET_CONTAINER_DLL_insert (cl_head,
282 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
283 for (cl = cl_head; NULL != cl; cl = cl->next)
284 if (caller == cl->caller)
287 (caller == cl_active->caller) )
295 _("Call from `%s' terminated\n"),
296 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
300 phone_state = PS_LISTEN;
304 GNUNET_CONTAINER_DLL_remove (cl_head,
315 * Function called with an event emitted by a caller.
317 * @param cls closure with the `struct CallList` of the caller
318 * @param code type of the event issued by the caller
321 caller_event_handler (void *cls,
322 enum GNUNET_CONVERSATION_CallerEventCode code)
324 struct CallList *cl = cls;
328 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
330 _("Call from `%s' suspended by other user\n"),
331 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
333 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
335 _("Call from `%s' resumed by other user\n"),
336 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
348 struct GNUNET_GNSRECORD_Data rd;
350 if (NULL == my_caller_id)
353 _("Ego `%s' no longer available, phone is now down.\n"),
355 phone_state = PS_LOOKUP_EGO;
358 GNUNET_assert (NULL == phone);
359 phone = GNUNET_CONVERSATION_phone_create (cfg,
361 &phone_event_handler,
363 /* FIXME: get record and print full GNS record info later here... */
368 _("Failed to setup phone (internal error)\n"));
369 phone_state = PS_ERROR;
373 GNUNET_CONVERSATION_phone_get_record (phone,
375 GNUNET_free_non_null (address);
376 address = GNUNET_GNSRECORD_value_to_string (rd.record_type,
380 _("Phone active at `%s'. Type `/help' for a list of available commands\n"),
382 phone_state = PS_LISTEN;
388 * Function called with an event emitted by a call.
390 * @param cls closure, NULL
391 * @param code type of the event on the call
394 call_event_handler (void *cls,
395 enum GNUNET_CONVERSATION_CallEventCode code)
401 case GNUNET_CONVERSATION_EC_CALL_RINGING:
402 GNUNET_break (CS_RESOLVING == call_state);
404 _("Resolved address of `%s'. Now ringing other party.\n"),
406 call_state = CS_RINGING;
408 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
409 GNUNET_break (CS_RINGING == call_state);
411 _("Connection established to `%s'\n"),
413 call_state = CS_CONNECTED;
415 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
416 GNUNET_break (CS_RESOLVING == call_state);
418 _("Failed to resolve `%s'\n"),
420 GNUNET_free (peer_name);
424 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
426 _("Call to `%s' terminated\n"),
428 GNUNET_free (peer_name);
432 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
433 GNUNET_break (CS_CONNECTED == call_state);
435 _("Connection to `%s' suspended (by other user)\n"),
438 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
439 GNUNET_break (CS_CONNECTED == call_state);
441 _("Connection to `%s' resumed (by other user)\n"),
444 case GNUNET_CONVERSATION_EC_CALL_ERROR:
446 _("Error with the call, restarting it\n"));
447 GNUNET_free (peer_name);
456 * Function declareation for executing a action
458 * @param arguments arguments given to the function
461 (*ActionFunction) (const char *arguments);
465 * Structure which defines a command
470 * Command the user needs to enter.
475 * Function to call on command.
477 ActionFunction Action;
480 * Help text for the command.
482 const char *helptext;
487 * Action function to print help for the command shell.
489 * @param args arguments given to the command
492 do_help (const char *args);
496 * Terminate the client
498 * @param args arguments given to the command
501 do_quit (const char *args)
504 GNUNET_SCHEDULER_shutdown ();
509 * Handler for unknown command.
511 * @param msg arguments given to the command
514 do_unknown (const char *msg)
517 _("Unknown command `%s'\n"),
523 * Initiating a new call
525 * @param arg arguments given to the command
528 do_call (const char *arg)
530 if (NULL == my_caller_id)
533 _("Ego `%s' not available\n"),
540 _("You are calling someone else already, hang up first!\n"));
547 _("Ego `%s' not available\n"),
555 _("You are answering call from `%s', hang up or suspend that call first!\n"),
556 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
565 _("Call recipient missing.\n"));
569 peer_name = GNUNET_strdup (arg);
570 call_state = CS_RESOLVING;
571 GNUNET_assert (NULL == call);
572 call = GNUNET_CONVERSATION_call_start (cfg,
577 &call_event_handler, NULL);
582 * Accepting an incoming call
584 * @param args arguments given to the command
587 do_accept (const char *args)
592 if ( (NULL != call) &&
593 (CS_SUSPENDED != call_state) )
596 _("You are calling someone else already, hang up first!\n"));
605 /* this is the expected state */
609 _("You are answering call from `%s', hang up or suspend that call first!\n"),
610 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
620 _("There is no incoming call to accept here!\n"));
623 if ( (NULL != cl->next) || (NULL != args) )
625 for (cl = cl_head; NULL != cl; cl = cl->next)
627 GNUNET_snprintf (buf, sizeof (buf),
630 if (0 == strcmp (buf, args))
637 _("There is no incoming call `%s' to accept right now!\n"),
641 GNUNET_CONTAINER_DLL_remove (cl_head,
645 peer_key = cl->caller_id;
646 phone_state = PS_ACCEPTED;
647 GNUNET_CONVERSATION_caller_pick_up (cl->caller,
648 &caller_event_handler,
656 * Print address information for this phone.
658 * @param args arguments given to the command
661 do_address (const char *args)
668 _("We currently do not have an address.\n"));
678 * Accepting an incoming call
680 * @param args arguments given to the command
683 do_status (const char *args)
692 _("We are currently trying to locate the private key for the ego `%s'.\n"),
697 _("We are listening for incoming calls for ego `%s' on line `%s'.\n"),
703 _("You are having a conversation with `%s'.\n"),
704 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));;
708 _("We had an internal error setting up our phone line. You can still make calls.\n"));
717 _("We are trying to find the network address to call `%s'.\n"),
722 _("We are calling `%s', his phone should be ringing.\n"),
727 _("You are having a conversation with `%s'.\n"),
731 /* ok to accept incoming call right now */
735 if ( (NULL != cl_head) &&
736 ( (cl_head != cl_active) ||
737 (cl_head != cl_tail) ) )
741 _("Calls waiting:\n"));
742 for (cl = cl_head; NULL != cl; cl = cl->next)
749 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
761 * @param args arguments given to the command
764 do_suspend (const char *args)
776 _("There is no call that could be suspended right now.\n"));
779 call_state = CS_SUSPENDED;
780 GNUNET_CONVERSATION_call_suspend (call);
791 _("There is no call that could be suspended right now.\n"));
794 /* expected state, do rejection logic */
797 GNUNET_assert (NULL != cl_active);
798 GNUNET_CONVERSATION_caller_suspend (cl_active->caller);
800 phone_state = PS_LISTEN;
807 * @param args arguments given to the command
810 do_resume (const char *args)
824 _("There is no call that could be resumed right now.\n"));
827 call_state = CS_CONNECTED;
828 GNUNET_CONVERSATION_call_resume (call,
840 _("There is no call that could be resumed right now.\n"));
843 /* expected state, do resume logic */
847 _("Already talking with `%s', cannot resume a call right now.\n"),
848 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
851 GNUNET_assert (NULL == cl_active);
856 _("There is no incoming call to resume here!\n"));
859 if ( (NULL != cl->next) || (NULL != args) )
861 for (cl = cl_head; NULL != cl; cl = cl->next)
863 GNUNET_snprintf (buf, sizeof (buf),
866 if (0 == strcmp (buf, args))
873 _("There is no incoming call `%s' to resume right now!\n"),
878 GNUNET_CONVERSATION_caller_resume (cl_active->caller,
881 phone_state = PS_ACCEPTED;
888 * @param args arguments given to the command
891 do_reject (const char *args)
898 GNUNET_CONVERSATION_call_stop (call);
908 _("There is no call that could be cancelled right now.\n"));
911 /* look for active incoming calls to refuse */
916 _("There is no incoming call to refuse here!\n"));
919 if ( (NULL != cl->next) || (NULL != args) )
921 for (cl = cl_head; NULL != cl; cl = cl->next)
923 GNUNET_snprintf (buf, sizeof (buf),
926 if (0 == strcmp (buf, args))
933 _("There is no incoming call `%s' to refuse right now!\n"),
937 GNUNET_CONVERSATION_caller_hang_up (cl->caller);
938 GNUNET_CONTAINER_DLL_remove (cl_head,
944 /* expected state, do rejection logic */
945 GNUNET_assert (NULL != cl_active);
946 GNUNET_CONVERSATION_caller_hang_up (cl_active->caller);
948 phone_state = PS_LISTEN;
955 * List of supported commands.
957 static struct VoipCommand commands[] = {
958 {"/address", &do_address,
959 gettext_noop ("Use `/address' to find out which address this phone should have in GNS")},
961 gettext_noop ("Use `/call USER.gnu' to call USER")},
962 {"/accept", &do_accept,
963 gettext_noop ("Use `/accept #NUM' to accept incoming call #NUM")},
964 {"/suspend", &do_suspend,
965 gettext_noop ("Use `/suspend' to suspend the active call")},
966 {"/resume", &do_resume,
967 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.")},
968 {"/cancel", &do_reject,
969 gettext_noop ("Use `/cancel' to reject or terminate a call")},
970 {"/status", &do_status,
971 gettext_noop ("Use `/status' to print status information")},
973 gettext_noop ("Use `/quit' to terminate gnunet-conversation")},
975 gettext_noop ("Use `/help command' to get help for a specific command")},
983 * Action function to print help for the command shell.
985 * @param args arguments given to the command
988 do_help (const char *args)
993 while ( (NULL != args) &&
994 (0 != strlen (args)) &&
995 (commands[i].Action != &do_help))
998 strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
1002 gettext (commands[i].helptext));
1010 "Available commands:\n");
1011 while (commands[i].Action != &do_help)
1015 gettext (commands[i].command));
1023 gettext (commands[i].helptext));
1028 * Task run during shutdown.
1033 do_stop_task (void *cls)
1037 if (NULL != stdin_hlp)
1039 GNUNET_HELPER_stop (stdin_hlp, GNUNET_NO);
1045 GNUNET_CONVERSATION_call_stop (call);
1050 GNUNET_CONVERSATION_phone_destroy (phone);
1053 if (NULL != handle_cmd_task)
1055 GNUNET_SCHEDULER_cancel (handle_cmd_task);
1056 handle_cmd_task = NULL;
1060 GNUNET_IDENTITY_disconnect (id);
1063 GNUNET_SPEAKER_destroy (speaker);
1065 GNUNET_MICROPHONE_destroy (mic);
1067 GNUNET_free (ego_name);
1069 GNUNET_free_non_null (peer_name);
1071 phone_state = PS_ERROR;
1076 * Handle user command.
1078 * @param message command the user typed in
1079 * @param str_len number of bytes to process in @a message
1082 handle_command_string (char *message,
1090 if (message[str_len - 1] == '\n')
1091 message[str_len - 1] = '\0';
1092 if (message[str_len - 2] == '\r')
1093 message[str_len - 2] = '\0';
1094 if (0 == strlen (message))
1097 while ((NULL != commands[i].command) &&
1098 (0 != strncasecmp (commands[i].command, message,
1099 strlen (commands[i].command))))
1101 ptr = &message[strlen (commands[i].command)];
1102 while (isspace ((unsigned char) *ptr))
1106 commands[i].Action (ptr);
1112 console_reader_chars (void *cls,
1114 const struct GNUNET_MessageHeader *message)
1120 switch (ntohs (message->type))
1122 case GNUNET_MESSAGE_TYPE_W32_CONSOLE_HELPER_CHARS:
1123 chars = (char *) &message[1];
1124 str_size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
1125 if (chars[str_size - 1] != '\0')
1126 return GNUNET_SYSERR;
1127 /* FIXME: is it ok that we pass part of a const struct to
1128 * this function that may mangle the contents?
1130 handle_command_string (chars, str_size - 1);
1142 * Task to handle commands from the terminal.
1147 handle_command (void *cls)
1149 char message[MAX_MESSAGE_LENGTH + 1];
1153 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1155 &handle_command, NULL);
1156 /* read message from command line and handle it */
1159 MAX_MESSAGE_LENGTH + 1);
1160 if (NULL == fgets (message,
1164 handle_command_string (message,
1170 * Function called by identity service with information about egos.
1173 * @param ego ego handle
1175 * @param name name of the ego
1178 identity_cb (void *cls,
1179 struct GNUNET_IDENTITY_Ego *ego,
1187 if (ego == my_caller_id)
1191 _("Name of our ego changed to `%s'\n"),
1193 GNUNET_free (ego_name);
1194 ego_name = GNUNET_strdup (name);
1197 if (0 != strcmp (name,
1204 _("Our ego `%s' was deleted!\n"),
1206 my_caller_id = NULL;
1210 GNUNET_CONFIGURATION_set_value_string (cfg,
1219 * Main function that will be run by the scheduler.
1221 * @param cls closure
1222 * @param args remaining command-line arguments
1223 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1224 * @param c configuration
1229 const char *cfgfile,
1230 const struct GNUNET_CONFIGURATION_Handle *c)
1235 cfg = GNUNET_CONFIGURATION_dup (c);
1236 speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
1237 mic = GNUNET_MICROPHONE_create_from_hardware (cfg);
1238 if (NULL == ego_name)
1242 _("You must specify the NAME of an ego to use\n"));
1245 id = GNUNET_IDENTITY_connect (cfg,
1249 if (stdin_fh == NULL)
1251 static char cpid[64];
1252 static char *args[] = {"gnunet-helper-w32-console.exe", "chars",
1253 XSTRINGIFY (MAX_MESSAGE_LENGTH), cpid, NULL};
1254 snprintf (cpid, 64, "%d", GetCurrentProcessId ());
1255 stdin_hlp = GNUNET_HELPER_start (
1257 "gnunet-helper-w32-console",
1259 console_reader_chars,
1262 if (NULL == stdin_hlp)
1266 _("Failed to start gnunet-helper-w32-console\n"));
1273 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
1274 &handle_command, NULL);
1275 GNUNET_SCHEDULER_add_shutdown (&do_stop_task,
1281 * The main function to conversation.
1283 * @param argc number of arguments from the command line
1284 * @param argv command line arguments
1285 * @return 0 ok, 1 on error
1291 struct GNUNET_GETOPT_CommandLineOption options[] = {
1292 GNUNET_GETOPT_option_string ('e',
1295 gettext_noop ("sets the NAME of the ego to use for the caller ID"),
1297 GNUNET_GETOPT_option_string ('p',
1300 gettext_noop ("sets the LINE to use for the phone"),
1302 GNUNET_GETOPT_OPTION_END
1307 flags = fcntl (0, F_GETFL, 0);
1308 flags |= O_NONBLOCK;
1309 if (0 != fcntl (0, F_SETFL, flags))
1310 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1312 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1314 if (FILE_TYPE_CHAR == GetFileType ((HANDLE) _get_osfhandle (0)))
1319 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1323 GNUNET_STRINGS_get_utf8_args (argc, argv,
1326 ret = GNUNET_PROGRAM_run (argc,
1328 "gnunet-conversation",
1329 gettext_noop ("Enables having a conversation with other GNUnet users."),
1332 GNUNET_free ((void *) argv);
1335 GNUNET_CONFIGURATION_destroy (cfg);
1338 return (GNUNET_OK == ret) ? 0 : 1;
1341 /* end of gnunet-conversation.c */