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)
267 case GNUNET_CONVERSATION_EC_PHONE_RING:
269 _("Incoming call from `%s'. Please /accept %u or /cancel %u the call.\n"),
270 GNUNET_GNSRECORD_pkey_to_zkey (caller_id),
273 cl = GNUNET_new (struct CallList);
275 cl->caller_id = *caller_id;
276 cl->caller_num = caller_num_gen++;
277 GNUNET_CONTAINER_DLL_insert (cl_head,
281 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
282 for (cl = cl_head; NULL != cl; cl = cl->next)
283 if (caller == cl->caller)
286 (caller == cl_active->caller) )
294 _("Call from `%s' terminated\n"),
295 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
299 phone_state = PS_LISTEN;
303 GNUNET_CONTAINER_DLL_remove (cl_head,
314 * Function called with an event emitted by a caller.
316 * @param cls closure with the `struct CallList` of the caller
317 * @param code type of the event issued by the caller
320 caller_event_handler (void *cls,
321 enum GNUNET_CONVERSATION_CallerEventCode code)
323 struct CallList *cl = cls;
327 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
329 _("Call from `%s' suspended by other user\n"),
330 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
332 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
334 _("Call from `%s' resumed by other user\n"),
335 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
347 struct GNUNET_GNSRECORD_Data rd;
349 if (NULL == my_caller_id)
352 _("Ego `%s' no longer available, phone is now down.\n"),
354 phone_state = PS_LOOKUP_EGO;
357 GNUNET_assert (NULL == phone);
358 phone = GNUNET_CONVERSATION_phone_create (cfg,
360 &phone_event_handler,
362 /* FIXME: get record and print full GNS record info later here... */
367 _("Failed to setup phone (internal error)\n"));
368 phone_state = PS_ERROR;
372 GNUNET_CONVERSATION_phone_get_record (phone,
374 GNUNET_free_non_null (address);
375 address = GNUNET_GNSRECORD_value_to_string (rd.record_type,
379 _("Phone active at `%s'. Type `/help' for a list of available commands\n"),
381 phone_state = PS_LISTEN;
387 * Function called with an event emitted by a call.
389 * @param cls closure, NULL
390 * @param code type of the event on the call
393 call_event_handler (void *cls,
394 enum GNUNET_CONVERSATION_CallEventCode code)
398 case GNUNET_CONVERSATION_EC_CALL_RINGING:
399 GNUNET_break (CS_RESOLVING == call_state);
401 _("Resolved address of `%s'. Now ringing other party.\n"),
403 call_state = CS_RINGING;
405 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
406 GNUNET_break (CS_RINGING == call_state);
408 _("Connection established to `%s'\n"),
410 call_state = CS_CONNECTED;
412 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
413 GNUNET_break (CS_RESOLVING == call_state);
415 _("Failed to resolve `%s'\n"),
417 GNUNET_free (peer_name);
421 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
423 _("Call to `%s' terminated\n"),
425 GNUNET_free (peer_name);
429 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
430 GNUNET_break (CS_CONNECTED == call_state);
432 _("Connection to `%s' suspended (by other user)\n"),
435 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
436 GNUNET_break (CS_CONNECTED == call_state);
438 _("Connection to `%s' resumed (by other user)\n"),
441 case GNUNET_CONVERSATION_EC_CALL_ERROR:
443 _("Error with the call, restarting it\n"));
444 GNUNET_free (peer_name);
453 * Function declareation for executing a action
455 * @param arguments arguments given to the function
457 typedef void (*ActionFunction) (const char *arguments);
461 * Structure which defines a command
466 * Command the user needs to enter.
471 * Function to call on command.
473 ActionFunction Action;
476 * Help text for the command.
478 const char *helptext;
483 * Action function to print help for the command shell.
485 * @param args arguments given to the command
488 do_help (const char *args);
492 * Terminate the client
494 * @param args arguments given to the command
497 do_quit (const char *args)
499 GNUNET_SCHEDULER_shutdown ();
504 * Handler for unknown command.
506 * @param msg arguments given to the command
509 do_unknown (const char *msg)
512 _("Unknown command `%s'\n"),
518 * Initiating a new call
520 * @param arg arguments given to the command
523 do_call (const char *arg)
525 if (NULL == my_caller_id)
528 _("Ego `%s' not available\n"),
535 _("You are calling someone else already, hang up first!\n"));
542 _("Ego `%s' not available\n"),
550 _("You are answering call from `%s', hang up or suspend that call first!\n"),
551 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
560 _("Call recipient missing.\n"));
564 peer_name = GNUNET_strdup (arg);
565 call_state = CS_RESOLVING;
566 GNUNET_assert (NULL == call);
567 call = GNUNET_CONVERSATION_call_start (cfg,
573 &call_event_handler, NULL);
578 * Accepting an incoming call
580 * @param args arguments given to the command
583 do_accept (const char *args)
588 if ( (NULL != call) &&
589 (CS_SUSPENDED != call_state) )
592 _("You are calling someone else already, hang up first!\n"));
601 /* this is the expected state */
605 _("You are answering call from `%s', hang up or suspend that call first!\n"),
606 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
616 _("There is no incoming call to accept here!\n"));
619 if ( (NULL != cl->next) || (NULL != args) )
621 for (cl = cl_head; NULL != cl; cl = cl->next)
623 GNUNET_snprintf (buf, sizeof (buf),
626 if (0 == strcmp (buf, args))
633 _("There is no incoming call `%s' to accept right now!\n"),
637 GNUNET_CONTAINER_DLL_remove (cl_head,
641 peer_key = cl->caller_id;
642 phone_state = PS_ACCEPTED;
643 GNUNET_CONVERSATION_caller_pick_up (cl->caller,
644 &caller_event_handler,
652 * Print address information for this phone.
654 * @param args arguments given to the command
657 do_address (const char *args)
663 _("We currently do not have an address.\n"));
673 * Accepting an incoming call
675 * @param args arguments given to the command
678 do_status (const char *args)
686 _("We are currently trying to locate the private key for the ego `%s'.\n"),
691 _("We are listening for incoming calls for ego `%s' on line `%s'.\n"),
697 _("You are having a conversation with `%s'.\n"),
698 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));;
702 _("We had an internal error setting up our phone line. You can still make calls.\n"));
711 _("We are trying to find the network address to call `%s'.\n"),
716 _("We are calling `%s', his phone should be ringing.\n"),
721 _("You are having a conversation with `%s'.\n"),
725 /* ok to accept incoming call right now */
729 if ( (NULL != cl_head) &&
730 ( (cl_head != cl_active) ||
731 (cl_head != cl_tail) ) )
735 _("Calls waiting:\n"));
736 for (cl = cl_head; NULL != cl; cl = cl->next)
743 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
755 * @param args arguments given to the command
758 do_suspend (const char *args)
769 _("There is no call that could be suspended right now.\n"));
772 call_state = CS_SUSPENDED;
773 GNUNET_CONVERSATION_call_suspend (call);
784 _("There is no call that could be suspended right now.\n"));
787 /* expected state, do rejection logic */
790 GNUNET_assert (NULL != cl_active);
791 GNUNET_CONVERSATION_caller_suspend (cl_active->caller);
793 phone_state = PS_LISTEN;
800 * @param args arguments given to the command
803 do_resume (const char *args)
817 _("There is no call that could be resumed right now.\n"));
820 call_state = CS_CONNECTED;
821 GNUNET_CONVERSATION_call_resume (call,
833 _("There is no call that could be resumed right now.\n"));
836 /* expected state, do resume logic */
840 _("Already talking with `%s', cannot resume a call right now.\n"),
841 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
844 GNUNET_assert (NULL == cl_active);
849 _("There is no incoming call to resume here!\n"));
852 if ( (NULL != cl->next) || (NULL != args) )
854 for (cl = cl_head; NULL != cl; cl = cl->next)
856 GNUNET_snprintf (buf, sizeof (buf),
859 if (0 == strcmp (buf, args))
866 _("There is no incoming call `%s' to resume right now!\n"),
871 GNUNET_CONVERSATION_caller_resume (cl_active->caller,
874 phone_state = PS_ACCEPTED;
881 * @param args arguments given to the command
884 do_reject (const char *args)
891 GNUNET_CONVERSATION_call_stop (call);
901 _("There is no call that could be cancelled right now.\n"));
904 /* look for active incoming calls to refuse */
909 _("There is no incoming call to refuse here!\n"));
912 if ( (NULL != cl->next) || (NULL != args) )
914 for (cl = cl_head; NULL != cl; cl = cl->next)
916 GNUNET_snprintf (buf, sizeof (buf),
919 if (0 == strcmp (buf, args))
926 _("There is no incoming call `%s' to refuse right now!\n"),
930 GNUNET_CONVERSATION_caller_hang_up (cl->caller);
931 GNUNET_CONTAINER_DLL_remove (cl_head,
937 /* expected state, do rejection logic */
938 GNUNET_assert (NULL != cl_active);
939 GNUNET_CONVERSATION_caller_hang_up (cl_active->caller);
941 phone_state = PS_LISTEN;
948 * List of supported commands.
950 static struct VoipCommand commands[] = {
951 {"/address", &do_address,
952 gettext_noop ("Use `/address' to find out which address this phone should have in GNS")},
954 gettext_noop ("Use `/call USER.gnu' to call USER")},
955 {"/accept", &do_accept,
956 gettext_noop ("Use `/accept #NUM' to accept incoming call #NUM")},
957 {"/suspend", &do_suspend,
958 gettext_noop ("Use `/suspend' to suspend the active call")},
959 {"/resume", &do_resume,
960 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.")},
961 {"/cancel", &do_reject,
962 gettext_noop ("Use `/cancel' to reject or terminate a call")},
963 {"/status", &do_status,
964 gettext_noop ("Use `/status' to print status information")},
966 gettext_noop ("Use `/quit' to terminate gnunet-conversation")},
968 gettext_noop ("Use `/help command' to get help for a specific command")},
976 * Action function to print help for the command shell.
978 * @param args arguments given to the command
981 do_help (const char *args)
986 while ( (NULL != args) &&
987 (0 != strlen (args)) &&
988 (commands[i].Action != &do_help))
991 strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
995 gettext (commands[i].helptext));
1003 "Available commands:\n");
1004 while (commands[i].Action != &do_help)
1008 gettext (commands[i].command));
1016 gettext (commands[i].helptext));
1021 * Task run during shutdown.
1026 do_stop_task (void *cls)
1029 if (NULL != stdin_hlp)
1031 GNUNET_HELPER_stop (stdin_hlp, GNUNET_NO);
1037 GNUNET_CONVERSATION_call_stop (call);
1042 GNUNET_CONVERSATION_phone_destroy (phone);
1045 if (NULL != handle_cmd_task)
1047 GNUNET_SCHEDULER_cancel (handle_cmd_task);
1048 handle_cmd_task = NULL;
1052 GNUNET_IDENTITY_disconnect (id);
1055 GNUNET_SPEAKER_destroy (speaker);
1057 GNUNET_MICROPHONE_destroy (mic);
1059 GNUNET_free (ego_name);
1061 GNUNET_free_non_null (peer_name);
1063 phone_state = PS_ERROR;
1068 * Handle user command.
1070 * @param message command the user typed in
1071 * @param str_len number of bytes to process in @a message
1074 handle_command_string (char *message,
1082 if (message[str_len - 1] == '\n')
1083 message[str_len - 1] = '\0';
1084 if (message[str_len - 2] == '\r')
1085 message[str_len - 2] = '\0';
1086 if (0 == strlen (message))
1089 while ((NULL != commands[i].command) &&
1090 (0 != strncasecmp (commands[i].command, message,
1091 strlen (commands[i].command))))
1093 ptr = &message[strlen (commands[i].command)];
1094 while (isspace ((int) *ptr))
1098 commands[i].Action (ptr);
1104 console_reader_chars (void *cls,
1106 const struct GNUNET_MessageHeader *message)
1110 switch (ntohs (message->type))
1112 case GNUNET_MESSAGE_TYPE_W32_CONSOLE_HELPER_CHARS:
1113 chars = (char *) &message[1];
1114 str_size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
1115 if (chars[str_size - 1] != '\0')
1116 return GNUNET_SYSERR;
1117 /* FIXME: is it ok that we pass part of a const struct to
1118 * this function that may mangle the contents?
1120 handle_command_string (chars, str_size - 1);
1131 * Task to handle commands from the terminal.
1136 handle_command (void *cls)
1138 char message[MAX_MESSAGE_LENGTH + 1];
1141 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1143 &handle_command, NULL);
1144 /* read message from command line and handle it */
1145 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
1146 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
1148 handle_command_string (message, strlen (message));
1153 * Function called by identity service with information about egos.
1156 * @param ego ego handle
1158 * @param name name of the ego
1161 identity_cb (void *cls,
1162 struct GNUNET_IDENTITY_Ego *ego,
1168 if (ego == my_caller_id)
1172 _("Name of our ego changed to `%s'\n"),
1174 GNUNET_free (ego_name);
1175 ego_name = GNUNET_strdup (name);
1178 if (0 != strcmp (name,
1185 _("Our ego `%s' was deleted!\n"),
1187 my_caller_id = NULL;
1191 GNUNET_CONFIGURATION_set_value_string (cfg,
1200 * Main function that will be run by the scheduler.
1202 * @param cls closure
1203 * @param args remaining command-line arguments
1204 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1205 * @param c configuration
1210 const char *cfgfile,
1211 const struct GNUNET_CONFIGURATION_Handle *c)
1213 cfg = GNUNET_CONFIGURATION_dup (c);
1214 speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
1215 mic = GNUNET_MICROPHONE_create_from_hardware (cfg);
1216 if (NULL == ego_name)
1220 _("You must specify the NAME of an ego to use\n"));
1223 id = GNUNET_IDENTITY_connect (cfg,
1227 if (stdin_fh == NULL)
1229 static char cpid[64];
1230 static char *args[] = {"gnunet-helper-w32-console.exe", "chars",
1231 XSTRINGIFY (MAX_MESSAGE_LENGTH), cpid, NULL};
1232 snprintf (cpid, 64, "%d", GetCurrentProcessId ());
1233 stdin_hlp = GNUNET_HELPER_start (
1235 "gnunet-helper-w32-console",
1237 console_reader_chars,
1240 if (NULL == stdin_hlp)
1244 _("Failed to start gnunet-helper-w32-console\n"));
1251 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
1252 &handle_command, NULL);
1253 GNUNET_SCHEDULER_add_shutdown (&do_stop_task,
1259 * The main function to conversation.
1261 * @param argc number of arguments from the command line
1262 * @param argv command line arguments
1263 * @return 0 ok, 1 on error
1266 main (int argc, char *const *argv)
1268 struct GNUNET_GETOPT_CommandLineOption options[] = {
1270 GNUNET_GETOPT_OPTION_STRING ('e',
1273 gettext_noop ("sets the NAME of the ego to use for the phone (and name resolution)"),
1276 GNUNET_GETOPT_OPTION_STRING ('p',
1279 gettext_noop ("sets the LINE to use for the phone"),
1282 GNUNET_GETOPT_OPTION_END
1287 flags = fcntl (0, F_GETFL, 0);
1288 flags |= O_NONBLOCK;
1289 if (0 != fcntl (0, F_SETFL, flags))
1290 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1292 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1294 if (FILE_TYPE_CHAR == GetFileType ((HANDLE) _get_osfhandle (0)))
1299 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1302 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1304 ret = GNUNET_PROGRAM_run (argc, argv,
1305 "gnunet-conversation",
1306 gettext_noop ("Enables having a conversation with other GNUnet users."),
1307 options, &run, NULL);
1308 GNUNET_free ((void *) argv);
1311 GNUNET_CONFIGURATION_destroy (cfg);
1314 return (GNUNET_OK == ret) ? 0 : 1;
1317 /* end of gnunet-conversation.c */