2 This file is part of GNUnet.
3 Copyright (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., 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.
171 static unsigned int line;
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, NULL);
361 /* FIXME: get record and print full GNS record info later here... */
366 _("Failed to setup phone (internal error)\n"));
367 phone_state = PS_ERROR;
371 GNUNET_CONVERSATION_phone_get_record (phone,
373 GNUNET_free_non_null (address);
374 address = GNUNET_GNSRECORD_value_to_string (rd.record_type,
378 _("Phone active on line %u. Type `/help' for a list of available commands\n"),
379 (unsigned int) line);
380 phone_state = PS_LISTEN;
386 * Function called with an event emitted by a call.
388 * @param cls closure, NULL
389 * @param code type of the event on the call
392 call_event_handler (void *cls,
393 enum GNUNET_CONVERSATION_CallEventCode code)
397 case GNUNET_CONVERSATION_EC_CALL_RINGING:
398 GNUNET_break (CS_RESOLVING == call_state);
400 _("Resolved address of `%s'. Now ringing other party.\n"),
402 call_state = CS_RINGING;
404 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
405 GNUNET_break (CS_RINGING == call_state);
407 _("Connection established to `%s'\n"),
409 call_state = CS_CONNECTED;
411 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
412 GNUNET_break (CS_RESOLVING == call_state);
414 _("Failed to resolve `%s'\n"),
416 GNUNET_free (peer_name);
420 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
422 _("Call to `%s' terminated\n"),
424 GNUNET_free (peer_name);
428 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
429 GNUNET_break (CS_CONNECTED == call_state);
431 _("Connection to `%s' suspended (by other user)\n"),
434 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
435 GNUNET_break (CS_CONNECTED == call_state);
437 _("Connection to `%s' resumed (by other user)\n"),
440 case GNUNET_CONVERSATION_EC_CALL_ERROR:
442 _("Error with the call, restarting it\n"));
443 GNUNET_free (peer_name);
452 * Function declareation for executing a action
454 * @param arguments arguments given to the function
456 typedef void (*ActionFunction) (const char *arguments);
460 * Structure which defines a command
465 * Command the user needs to enter.
470 * Function to call on command.
472 ActionFunction Action;
475 * Help text for the command.
477 const char *helptext;
482 * Action function to print help for the command shell.
484 * @param args arguments given to the command
487 do_help (const char *args);
491 * Terminate the client
493 * @param args arguments given to the command
496 do_quit (const char *args)
498 GNUNET_SCHEDULER_shutdown ();
503 * Handler for unknown command.
505 * @param msg arguments given to the command
508 do_unknown (const char *msg)
511 _("Unknown command `%s'\n"),
517 * Initiating a new call
519 * @param arg arguments given to the command
522 do_call (const char *arg)
524 if (NULL == my_caller_id)
527 _("Ego `%s' not available\n"),
534 _("You are calling someone else already, hang up first!\n"));
541 _("Ego `%s' not available\n"),
549 _("You are answering call from `%s', hang up or suspend that call first!\n"),
550 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
559 _("Call recipient missing.\n"));
563 peer_name = GNUNET_strdup (arg);
564 call_state = CS_RESOLVING;
565 GNUNET_assert (NULL == call);
566 call = GNUNET_CONVERSATION_call_start (cfg,
572 &call_event_handler, NULL);
577 * Accepting an incoming call
579 * @param args arguments given to the command
582 do_accept (const char *args)
587 if ( (NULL != call) &&
588 (CS_SUSPENDED != call_state) )
591 _("You are calling someone else already, hang up first!\n"));
600 /* this is the expected state */
604 _("You are answering call from `%s', hang up or suspend that call first!\n"),
605 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
615 _("There is no incoming call to accept here!\n"));
618 if ( (NULL != cl->next) || (NULL != args) )
620 for (cl = cl_head; NULL != cl; cl = cl->next)
622 GNUNET_snprintf (buf, sizeof (buf),
625 if (0 == strcmp (buf, args))
632 _("There is no incoming call `%s' to accept right now!\n"),
636 GNUNET_CONTAINER_DLL_remove (cl_head,
640 peer_key = cl->caller_id;
641 phone_state = PS_ACCEPTED;
642 GNUNET_CONVERSATION_caller_pick_up (cl->caller,
643 &caller_event_handler,
651 * Print address information for this phone.
653 * @param args arguments given to the command
656 do_address (const char *args)
662 _("We currently do not have an address.\n"));
672 * Accepting an incoming call
674 * @param args arguments given to the command
677 do_status (const char *args)
685 _("We are currently trying to locate the private key for the ego `%s'.\n"),
690 _("We are listening for incoming calls for ego `%s' on line %u.\n"),
696 _("You are having a conversation with `%s'.\n"),
697 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));;
701 _("We had an internal error setting up our phone line. You can still make calls.\n"));
710 _("We are trying to find the network address to call `%s'.\n"),
715 _("We are calling `%s', his phone should be ringing.\n"),
720 _("You are having a conversation with `%s'.\n"),
724 /* ok to accept incoming call right now */
728 if ( (NULL != cl_head) &&
729 ( (cl_head != cl_active) ||
730 (cl_head != cl_tail) ) )
734 _("Calls waiting:\n"));
735 for (cl = cl_head; NULL != cl; cl = cl->next)
742 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
754 * @param args arguments given to the command
757 do_suspend (const char *args)
768 _("There is no call that could be suspended right now.\n"));
771 call_state = CS_SUSPENDED;
772 GNUNET_CONVERSATION_call_suspend (call);
783 _("There is no call that could be suspended right now.\n"));
786 /* expected state, do rejection logic */
789 GNUNET_assert (NULL != cl_active);
790 GNUNET_CONVERSATION_caller_suspend (cl_active->caller);
792 phone_state = PS_LISTEN;
799 * @param args arguments given to the command
802 do_resume (const char *args)
816 _("There is no call that could be resumed right now.\n"));
819 call_state = CS_CONNECTED;
820 GNUNET_CONVERSATION_call_resume (call,
832 _("There is no call that could be resumed right now.\n"));
835 /* expected state, do resume logic */
839 _("Already talking with `%s', cannot resume a call right now.\n"),
840 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
843 GNUNET_assert (NULL == cl_active);
848 _("There is no incoming call to resume here!\n"));
851 if ( (NULL != cl->next) || (NULL != args) )
853 for (cl = cl_head; NULL != cl; cl = cl->next)
855 GNUNET_snprintf (buf, sizeof (buf),
858 if (0 == strcmp (buf, args))
865 _("There is no incoming call `%s' to resume right now!\n"),
870 GNUNET_CONVERSATION_caller_resume (cl_active->caller,
873 phone_state = PS_ACCEPTED;
880 * @param args arguments given to the command
883 do_reject (const char *args)
890 GNUNET_CONVERSATION_call_stop (call);
900 _("There is no call that could be cancelled right now.\n"));
903 /* look for active incoming calls to refuse */
908 _("There is no incoming call to refuse here!\n"));
911 if ( (NULL != cl->next) || (NULL != args) )
913 for (cl = cl_head; NULL != cl; cl = cl->next)
915 GNUNET_snprintf (buf, sizeof (buf),
918 if (0 == strcmp (buf, args))
925 _("There is no incoming call `%s' to refuse right now!\n"),
929 GNUNET_CONVERSATION_caller_hang_up (cl->caller);
930 GNUNET_CONTAINER_DLL_remove (cl_head,
936 /* expected state, do rejection logic */
937 GNUNET_assert (NULL != cl_active);
938 GNUNET_CONVERSATION_caller_hang_up (cl_active->caller);
940 phone_state = PS_LISTEN;
947 * List of supported commands.
949 static struct VoipCommand commands[] = {
950 {"/address", &do_address,
951 gettext_noop ("Use `/address' to find out which address this phone should have in GNS")},
953 gettext_noop ("Use `/call USER.gnu' to call USER")},
954 {"/accept", &do_accept,
955 gettext_noop ("Use `/accept #NUM' to accept incoming call #NUM")},
956 {"/suspend", &do_suspend,
957 gettext_noop ("Use `/suspend' to suspend the active call")},
958 {"/resume", &do_resume,
959 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.")},
960 {"/cancel", &do_reject,
961 gettext_noop ("Use `/cancel' to reject or terminate a call")},
962 {"/status", &do_status,
963 gettext_noop ("Use `/status' to print status information")},
965 gettext_noop ("Use `/quit' to terminate gnunet-conversation")},
967 gettext_noop ("Use `/help command' to get help for a specific command")},
975 * Action function to print help for the command shell.
977 * @param args arguments given to the command
980 do_help (const char *args)
985 while ( (NULL != args) &&
986 (0 != strlen (args)) &&
987 (commands[i].Action != &do_help))
990 strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
994 gettext (commands[i].helptext));
1002 "Available commands:\n");
1003 while (commands[i].Action != &do_help)
1007 gettext (commands[i].command));
1015 gettext (commands[i].helptext));
1020 * Task run during shutdown.
1026 do_stop_task (void *cls,
1027 const struct GNUNET_SCHEDULER_TaskContext *tc)
1030 if (NULL != stdin_hlp)
1032 GNUNET_HELPER_stop (stdin_hlp, GNUNET_NO);
1038 GNUNET_CONVERSATION_call_stop (call);
1043 GNUNET_CONVERSATION_phone_destroy (phone);
1046 if (NULL != handle_cmd_task)
1048 GNUNET_SCHEDULER_cancel (handle_cmd_task);
1049 handle_cmd_task = NULL;
1053 GNUNET_IDENTITY_disconnect (id);
1056 GNUNET_SPEAKER_destroy (speaker);
1058 GNUNET_MICROPHONE_destroy (mic);
1060 GNUNET_free (ego_name);
1062 GNUNET_free_non_null (peer_name);
1064 phone_state = PS_ERROR;
1069 * Handle user command.
1071 * @param message command the user typed in
1072 * @param str_len number of bytes to process in @a message
1075 handle_command_string (char *message,
1083 if (message[str_len - 1] == '\n')
1084 message[str_len - 1] = '\0';
1085 if (message[str_len - 2] == '\r')
1086 message[str_len - 2] = '\0';
1087 if (0 == strlen (message))
1090 while ((NULL != commands[i].command) &&
1091 (0 != strncasecmp (commands[i].command, message,
1092 strlen (commands[i].command))))
1094 ptr = &message[strlen (commands[i].command)];
1095 while (isspace ((int) *ptr))
1099 commands[i].Action (ptr);
1105 console_reader_chars (void *cls,
1107 const struct GNUNET_MessageHeader *message)
1111 switch (ntohs (message->type))
1113 case GNUNET_MESSAGE_TYPE_W32_CONSOLE_HELPER_CHARS:
1114 chars = (char *) &message[1];
1115 str_size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
1116 if (chars[str_size - 1] != '\0')
1117 return GNUNET_SYSERR;
1118 /* FIXME: is it ok that we pass part of a const struct to
1119 * this function that may mangle the contents?
1121 handle_command_string (chars, str_size - 1);
1132 * Task to handle commands from the terminal.
1135 * @param tc scheduler context
1138 handle_command (void *cls,
1139 const struct GNUNET_SCHEDULER_TaskContext *tc)
1141 char message[MAX_MESSAGE_LENGTH + 1];
1144 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1146 &handle_command, NULL);
1147 /* read message from command line and handle it */
1148 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
1149 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
1151 handle_command_string (message, strlen (message));
1156 * Function called by identity service with information about egos.
1159 * @param ego ego handle
1161 * @param name name of the ego
1164 identity_cb (void *cls,
1165 struct GNUNET_IDENTITY_Ego *ego,
1171 if (ego == my_caller_id)
1175 _("Name of our ego changed to `%s'\n"),
1177 GNUNET_free (ego_name);
1178 ego_name = GNUNET_strdup (name);
1181 if (0 != strcmp (name,
1188 _("Our ego `%s' was deleted!\n"),
1190 my_caller_id = NULL;
1194 GNUNET_CONFIGURATION_set_value_number (cfg,
1203 * Main function that will be run by the scheduler.
1205 * @param cls closure
1206 * @param args remaining command-line arguments
1207 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1208 * @param c configuration
1213 const char *cfgfile,
1214 const struct GNUNET_CONFIGURATION_Handle *c)
1216 cfg = GNUNET_CONFIGURATION_dup (c);
1217 speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
1218 mic = GNUNET_MICROPHONE_create_from_hardware (cfg);
1219 if (NULL == ego_name)
1223 _("You must specify the NAME of an ego to use\n"));
1226 id = GNUNET_IDENTITY_connect (cfg,
1230 if (stdin_fh == NULL)
1232 static char cpid[64];
1233 static char *args[] = {"gnunet-helper-w32-console.exe", "chars",
1234 XSTRINGIFY (MAX_MESSAGE_LENGTH), cpid, NULL};
1235 snprintf (cpid, 64, "%d", GetCurrentProcessId ());
1236 stdin_hlp = GNUNET_HELPER_start (
1238 "gnunet-helper-w32-console",
1240 console_reader_chars,
1243 if (NULL == stdin_hlp)
1247 _("Failed to start gnunet-helper-w32-console\n"));
1254 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
1255 &handle_command, NULL);
1256 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
1262 * The main function to conversation.
1264 * @param argc number of arguments from the command line
1265 * @param argv command line arguments
1266 * @return 0 ok, 1 on error
1269 main (int argc, char *const *argv)
1271 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1272 {'e', "ego", "NAME",
1273 gettext_noop ("sets the NAME of the ego to use for the phone (and name resolution)"),
1274 1, &GNUNET_GETOPT_set_string, &ego_name},
1275 {'p', "phone", "LINE",
1276 gettext_noop ("sets the LINE to use for the phone"),
1277 1, &GNUNET_GETOPT_set_uint, &line},
1278 GNUNET_GETOPT_OPTION_END
1283 flags = fcntl (0, F_GETFL, 0);
1284 flags |= O_NONBLOCK;
1285 if (0 != fcntl (0, F_SETFL, flags))
1286 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1288 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1290 if (FILE_TYPE_CHAR == GetFileType ((HANDLE) _get_osfhandle (0)))
1295 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1298 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1300 ret = GNUNET_PROGRAM_run (argc, argv,
1301 "gnunet-conversation",
1302 gettext_noop ("Enables having a conversation with other GNUnet users."),
1303 options, &run, NULL);
1304 GNUNET_free ((void *) argv);
1307 GNUNET_CONFIGURATION_destroy (cfg);
1310 return (GNUNET_OK == ret) ? 0 : 1;
1313 /* end of gnunet-conversation.c */