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 * Public key identifying the caller.
132 struct GNUNET_CRYPTO_EcdsaPublicKey caller_id;
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 *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)
291 _("Call from `%s' terminated\n"),
292 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
293 GNUNET_CONTAINER_DLL_remove (cl_head,
299 phone_state = PS_LISTEN;
308 * Function called with an event emitted by a caller.
310 * @param cls closure with the `struct CallList` of the caller
311 * @param code type of the event issued by the caller
314 caller_event_handler (void *cls,
315 enum GNUNET_CONVERSATION_CallerEventCode code)
317 struct CallList *cl = cls;
321 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
323 _("Call from `%s' suspended by other user\n"),
324 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
326 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
328 _("Call from `%s' resumed by other user\n"),
329 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
341 struct GNUNET_GNSRECORD_Data rd;
343 if (NULL == my_caller_id)
346 _("Ego `%s' no longer available, phone is now down.\n"),
348 phone_state = PS_LOOKUP_EGO;
351 GNUNET_assert (NULL == phone);
352 phone = GNUNET_CONVERSATION_phone_create (cfg,
354 &phone_event_handler, NULL);
355 /* FIXME: get record and print full GNS record info later here... */
360 _("Failed to setup phone (internal error)\n"));
361 phone_state = PS_ERROR;
365 GNUNET_CONVERSATION_phone_get_record (phone,
367 GNUNET_free_non_null (address);
368 address = GNUNET_GNSRECORD_value_to_string (rd.record_type,
372 _("Phone active on line %u. Type `/help' for a list of available commands\n"),
373 (unsigned int) line);
374 phone_state = PS_LISTEN;
380 * Function called with an event emitted by a call.
382 * @param cls closure, NULL
383 * @param code type of the event on the call
386 call_event_handler (void *cls,
387 enum GNUNET_CONVERSATION_CallEventCode code)
391 case GNUNET_CONVERSATION_EC_CALL_RINGING:
392 GNUNET_break (CS_RESOLVING == call_state);
394 _("Resolved address of `%s'. Now ringing other party.\n"),
396 call_state = CS_RINGING;
398 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
399 GNUNET_break (CS_RINGING == call_state);
401 _("Connection established to `%s'\n"),
403 call_state = CS_CONNECTED;
405 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
406 GNUNET_break (CS_RESOLVING == call_state);
408 _("Failed to resolve `%s'\n"),
410 GNUNET_free (peer_name);
414 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
416 _("Call to `%s' terminated\n"),
418 GNUNET_free (peer_name);
422 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
423 GNUNET_break (CS_CONNECTED == call_state);
425 _("Connection to `%s' suspended (by other user)\n"),
428 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
429 GNUNET_break (CS_CONNECTED == call_state);
431 _("Connection to `%s' resumed (by other user)\n"),
434 case GNUNET_CONVERSATION_EC_CALL_ERROR:
436 _("Error with the call, restarting it\n"));
437 call_state = CS_RESOLVING;
438 // FIXME: is this correct?
445 * Function declareation for executing a action
447 * @param arguments arguments given to the function
449 typedef void (*ActionFunction) (const char *arguments);
453 * Structure which defines a command
458 * Command the user needs to enter.
463 * Function to call on command.
465 ActionFunction Action;
468 * Help text for the command.
470 const char *helptext;
475 * Action function to print help for the command shell.
477 * @param args arguments given to the command
480 do_help (const char *args);
484 * Terminate the client
486 * @param args arguments given to the command
489 do_quit (const char *args)
491 GNUNET_SCHEDULER_shutdown ();
496 * Handler for unknown command.
498 * @param msg arguments given to the command
501 do_unknown (const char *msg)
504 _("Unknown command `%s'\n"),
510 * Initiating a new call
512 * @param arg arguments given to the command
515 do_call (const char *arg)
517 if (NULL == my_caller_id)
520 _("Ego `%s' not available\n"),
527 _("You are calling someone else already, hang up first!\n"));
534 _("Ego `%s' not available\n"),
542 _("You are answering call from `%s', hang up or suspend that call first!\n"),
543 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
552 _("Call recipient missing.\n"));
556 peer_name = GNUNET_strdup (arg);
557 call_state = CS_RESOLVING;
558 GNUNET_assert (NULL == call);
559 call = GNUNET_CONVERSATION_call_start (cfg,
565 &call_event_handler, NULL);
570 * Accepting an incoming call
572 * @param args arguments given to the command
575 do_accept (const char *args)
580 if ( (NULL != call) &&
581 (CS_SUSPENDED != call_state) )
584 _("You are calling someone else already, hang up first!\n"));
593 /* this is the expected state */
597 _("You are answering call from `%s', hang up or suspend that call first!\n"),
598 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
608 _("There is no incoming call to accept here!\n"));
611 if ( (NULL != cl->next) || (NULL != args) )
613 for (cl = cl_head; NULL != cl; cl = cl->next)
615 GNUNET_snprintf (buf, sizeof (buf),
618 if (0 == strcmp (buf, args))
625 _("There is no incoming call `%s' to accept right now!\n"),
629 GNUNET_CONTAINER_DLL_remove (cl_head,
633 peer_key = cl->caller_id;
634 phone_state = PS_ACCEPTED;
635 GNUNET_CONVERSATION_caller_pick_up (cl->caller,
636 &caller_event_handler,
644 * Print address information for this phone.
646 * @param args arguments given to the command
649 do_address (const char *args)
655 _("We currently do not have an address.\n"));
665 * Accepting an incoming call
667 * @param args arguments given to the command
670 do_status (const char *args)
678 _("We are currently trying to locate the private key for the ego `%s'.\n"),
683 _("We are listening for incoming calls for ego `%s' on line %u.\n"),
689 _("You are having a conversation with `%s'.\n"),
690 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));;
694 _("We had an internal error setting up our phone line. You can still make calls.\n"));
703 _("We are trying to find the network address to call `%s'.\n"),
708 _("We are calling `%s', his phone should be ringing.\n"),
713 _("You are having a conversation with `%s'.\n"),
717 /* ok to accept incoming call right now */
721 if ( (NULL != cl_head) &&
722 ( (cl_head != cl_active) ||
723 (cl_head != cl_tail) ) )
727 _("Calls waiting:\n"));
728 for (cl = cl_head; NULL != cl; cl = cl->next)
735 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
747 * @param args arguments given to the command
750 do_suspend (const char *args)
761 _("There is no call that could be suspended right now.\n"));
764 call_state = CS_SUSPENDED;
765 GNUNET_CONVERSATION_call_suspend (call);
776 _("There is no call that could be suspended right now.\n"));
779 /* expected state, do rejection logic */
782 GNUNET_assert (NULL != cl_active);
783 GNUNET_CONVERSATION_caller_suspend (cl_active->caller);
785 phone_state = PS_LISTEN;
792 * @param args arguments given to the command
795 do_resume (const char *args)
809 _("There is no call that could be resumed right now.\n"));
812 call_state = CS_CONNECTED;
813 GNUNET_CONVERSATION_call_resume (call,
825 _("There is no call that could be resumed right now.\n"));
828 /* expected state, do resume logic */
832 _("Already talking with `%s', cannot resume a call right now.\n"),
833 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
836 GNUNET_assert (NULL == cl_active);
841 _("There is no incoming call to resume here!\n"));
844 if ( (NULL != cl->next) || (NULL != args) )
846 for (cl = cl_head; NULL != cl; cl = cl->next)
848 GNUNET_snprintf (buf, sizeof (buf),
851 if (0 == strcmp (buf, args))
858 _("There is no incoming call `%s' to resume right now!\n"),
863 GNUNET_CONVERSATION_caller_resume (cl_active->caller,
866 phone_state = PS_ACCEPTED;
873 * @param args arguments given to the command
876 do_reject (const char *args)
883 GNUNET_CONVERSATION_call_stop (call);
893 _("There is no call that could be cancelled right now.\n"));
896 /* look for active incoming calls to refuse */
901 _("There is no incoming call to refuse here!\n"));
904 if ( (NULL != cl->next) || (NULL != args) )
906 for (cl = cl_head; NULL != cl; cl = cl->next)
908 GNUNET_snprintf (buf, sizeof (buf),
911 if (0 == strcmp (buf, args))
918 _("There is no incoming call `%s' to refuse right now!\n"),
922 GNUNET_CONVERSATION_caller_hang_up (cl->caller);
923 GNUNET_CONTAINER_DLL_remove (cl_head,
929 /* expected state, do rejection logic */
930 GNUNET_assert (NULL != cl_active);
931 GNUNET_CONVERSATION_caller_hang_up (cl_active->caller);
933 phone_state = PS_LISTEN;
940 * List of supported commands.
942 static struct VoipCommand commands[] = {
943 {"/address", &do_address,
944 gettext_noop ("Use `/address' to find out which address this phone should have in GNS")},
946 gettext_noop ("Use `/call USER.gnu' to call USER")},
947 {"/accept", &do_accept,
948 gettext_noop ("Use `/accept #NUM' to accept incoming call #NUM")},
949 {"/suspend", &do_suspend,
950 gettext_noop ("Use `/suspend' to suspend the active call")},
951 {"/resume", &do_resume,
952 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.")},
953 {"/cancel", &do_reject,
954 gettext_noop ("Use `/cancel' to reject or terminate a call")},
955 {"/status", &do_status,
956 gettext_noop ("Use `/status' to print status information")},
958 gettext_noop ("Use `/quit' to terminate gnunet-conversation")},
960 gettext_noop ("Use `/help command' to get help for a specific command")},
968 * Action function to print help for the command shell.
970 * @param args arguments given to the command
973 do_help (const char *args)
978 while ( (NULL != args) &&
979 (0 != strlen (args)) &&
980 (commands[i].Action != &do_help))
983 strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
987 gettext (commands[i].helptext));
995 "Available commands:\n");
996 while (commands[i].Action != &do_help)
1000 gettext (commands[i].command));
1008 gettext (commands[i].helptext));
1013 * Task run during shutdown.
1019 do_stop_task (void *cls,
1020 const struct GNUNET_SCHEDULER_TaskContext *tc)
1023 if (NULL != stdin_hlp)
1025 GNUNET_HELPER_stop (stdin_hlp, GNUNET_NO);
1031 GNUNET_CONVERSATION_call_stop (call);
1036 GNUNET_CONVERSATION_phone_destroy (phone);
1039 if (GNUNET_SCHEDULER_NO_TASK != handle_cmd_task)
1041 GNUNET_SCHEDULER_cancel (handle_cmd_task);
1042 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
1046 GNUNET_IDENTITY_disconnect (id);
1049 GNUNET_SPEAKER_destroy (speaker);
1051 GNUNET_MICROPHONE_destroy (mic);
1053 GNUNET_free (ego_name);
1055 GNUNET_free_non_null (peer_name);
1057 phone_state = PS_ERROR;
1062 * Handle user command.
1064 * @param message command the user typed in
1065 * @param str_len number of bytes to process in @a message
1068 handle_command_string (char *message,
1076 if (message[str_len - 1] == '\n')
1077 message[str_len - 1] = '\0';
1078 if (message[str_len - 2] == '\r')
1079 message[str_len - 2] = '\0';
1080 if (0 == strlen (message))
1083 while ((NULL != commands[i].command) &&
1084 (0 != strncasecmp (commands[i].command, message,
1085 strlen (commands[i].command))))
1087 ptr = &message[strlen (commands[i].command)];
1088 while (isspace ((int) *ptr))
1092 commands[i].Action (ptr);
1098 console_reader_chars (void *cls,
1100 const struct GNUNET_MessageHeader *message)
1104 switch (ntohs (message->type))
1106 case GNUNET_MESSAGE_TYPE_W32_CONSOLE_HELPER_CHARS:
1107 chars = (char *) &message[1];
1108 str_size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
1109 if (chars[str_size - 1] != '\0')
1110 return GNUNET_SYSERR;
1111 /* FIXME: is it ok that we pass part of a const struct to
1112 * this function that may mangle the contents?
1114 handle_command_string (chars, str_size - 1);
1125 * Task to handle commands from the terminal.
1128 * @param tc scheduler context
1131 handle_command (void *cls,
1132 const struct GNUNET_SCHEDULER_TaskContext *tc)
1134 char message[MAX_MESSAGE_LENGTH + 1];
1137 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1139 &handle_command, NULL);
1140 /* read message from command line and handle it */
1141 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
1142 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
1144 handle_command_string (message, strlen (message));
1149 * Function called by identity service with information about egos.
1152 * @param ego ego handle
1154 * @param name name of the ego
1157 identity_cb (void *cls,
1158 struct GNUNET_IDENTITY_Ego *ego,
1164 if (ego == my_caller_id)
1168 _("Name of our ego changed to `%s'\n"),
1170 GNUNET_free (ego_name);
1171 ego_name = GNUNET_strdup (name);
1174 if (0 != strcmp (name,
1181 _("Our ego `%s' was deleted!\n"),
1183 my_caller_id = NULL;
1187 GNUNET_CONFIGURATION_set_value_number (cfg,
1196 * Main function that will be run by the scheduler.
1198 * @param cls closure
1199 * @param args remaining command-line arguments
1200 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1201 * @param c configuration
1206 const char *cfgfile,
1207 const struct GNUNET_CONFIGURATION_Handle *c)
1209 cfg = GNUNET_CONFIGURATION_dup (c);
1210 speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
1211 mic = GNUNET_MICROPHONE_create_from_hardware (cfg);
1212 if (NULL == ego_name)
1216 _("You must specify the NAME of an ego to use\n"));
1219 id = GNUNET_IDENTITY_connect (cfg,
1223 if (stdin_fh == NULL)
1225 static char cpid[64];
1226 static char *args[] = {"gnunet-helper-w32-console.exe", "chars",
1227 XSTRINGIFY (MAX_MESSAGE_LENGTH), cpid, NULL};
1228 snprintf (cpid, 64, "%d", GetCurrentProcessId ());
1229 stdin_hlp = GNUNET_HELPER_start (
1231 "gnunet-helper-w32-console",
1233 console_reader_chars,
1236 if (NULL == stdin_hlp)
1240 _("Failed to start gnunet-helper-w32-console\n"));
1247 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
1248 &handle_command, NULL);
1249 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
1255 * The main function to conversation.
1257 * @param argc number of arguments from the command line
1258 * @param argv command line arguments
1259 * @return 0 ok, 1 on error
1262 main (int argc, char *const *argv)
1264 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1265 {'e', "ego", "NAME",
1266 gettext_noop ("sets the NAME of the ego to use for the phone (and name resolution)"),
1267 1, &GNUNET_GETOPT_set_string, &ego_name},
1268 {'p', "phone", "LINE",
1269 gettext_noop ("sets the LINE to use for the phone"),
1270 1, &GNUNET_GETOPT_set_uint, &line},
1271 GNUNET_GETOPT_OPTION_END
1276 flags = fcntl (0, F_GETFL, 0);
1277 flags |= O_NONBLOCK;
1278 fcntl (0, F_SETFL, flags);
1279 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1281 if (FILE_TYPE_CHAR == GetFileType ((HANDLE) _get_osfhandle (0)))
1286 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1289 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1291 ret = GNUNET_PROGRAM_run (argc, argv,
1292 "gnunet-conversation",
1293 gettext_noop ("Enables having a conversation with other GNUnet users."),
1294 options, &run, NULL);
1295 GNUNET_free ((void *) argv);
1298 GNUNET_CONFIGURATION_destroy (cfg);
1301 return (GNUNET_OK == ret) ? 0 : 1;
1304 /* end of gnunet-conversation.c */