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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
16 * @file conversation/gnunet-conversation.c
17 * @brief conversation implementation
18 * @author Simon Dieterle
19 * @author Andreas Fuchs
22 #include "gnunet_util_lib.h"
23 #include "gnunet_constants.h"
24 #include "gnunet_gnsrecord_lib.h"
25 #include "gnunet_conversation_service.h"
26 #include "gnunet_namestore_service.h"
28 #include "../util/gnunet-helper-w32-console.h"
32 * Maximum length allowed for the command line input.
34 #define MAX_MESSAGE_LENGTH 1024
36 #define XSTRINGIFY(x) STRINGIFY(x)
38 #define STRINGIFY(x) (#x)
42 * Helper that reads the console for us.
44 struct GNUNET_HELPER_Handle *stdin_hlp;
48 * Possible states of the phone.
53 * We're waiting for our own idenitty.
58 * We're listening for calls
63 * We accepted an incoming phone call.
75 * States for current outgoing call.
80 * We are looking up some other participant.
85 * We are now ringing the other participant.
90 * The other party accepted our call and we are now connected.
95 * The call is currently suspended (by us).
103 * List of incoming calls
111 struct CallList *prev;
116 struct CallList *next;
119 * Handle to hang up or activate.
121 struct GNUNET_CONVERSATION_Caller *caller;
124 * Public key identifying the caller.
126 struct GNUNET_CRYPTO_EcdsaPublicKey caller_id;
129 * Unique number of the call.
131 unsigned int caller_num;
140 static struct GNUNET_CONVERSATION_Phone *phone;
143 * Call handle (for active outgoing call).
145 static struct GNUNET_CONVERSATION_Call *call;
148 * Caller handle (for active incoming call).
149 * This call handler is NOT in the #cl_head / #cl_tail list.
151 static struct CallList *cl_active;
154 * Head of calls waiting to be accepted.
156 static struct CallList *cl_head;
159 * Tail of calls waiting to be accepted.
161 static struct CallList *cl_tail;
164 * Desired phone line (string to be converted to a hash).
169 * Task which handles the commands
171 static struct GNUNET_SCHEDULER_Task * handle_cmd_task;
176 static struct GNUNET_SPEAKER_Handle *speaker;
181 static struct GNUNET_MICROPHONE_Handle *mic;
186 static struct GNUNET_CONFIGURATION_Handle *cfg;
191 static struct GNUNET_IDENTITY_Ego *my_caller_id;
194 * Handle to identity service.
196 static struct GNUNET_IDENTITY_Handle *id;
201 static char *ego_name;
204 * Public key of active conversation partner (if any).
206 static struct GNUNET_CRYPTO_EcdsaPublicKey peer_key;
209 * Name of active 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 public key 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 struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
263 case GNUNET_CONVERSATION_EC_PHONE_RING:
265 _("Incoming call from `%s'. Please /accept %u or /cancel %u the call.\n"),
266 GNUNET_GNSRECORD_pkey_to_zkey (caller_id),
269 cl = GNUNET_new (struct CallList);
271 cl->caller_id = *caller_id;
272 cl->caller_num = caller_num_gen++;
273 GNUNET_CONTAINER_DLL_insert (cl_head,
277 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
278 for (cl = cl_head; NULL != cl; cl = cl->next)
279 if (caller == cl->caller)
282 (caller == cl_active->caller) )
290 _("Call from `%s' terminated\n"),
291 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
295 phone_state = PS_LISTEN;
299 GNUNET_CONTAINER_DLL_remove (cl_head,
310 * Function called with an event emitted by a caller.
312 * @param cls closure with the `struct CallList` of the caller
313 * @param code type of the event issued by the caller
316 caller_event_handler (void *cls,
317 enum GNUNET_CONVERSATION_CallerEventCode code)
319 struct CallList *cl = cls;
323 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
325 _("Call from `%s' suspended by other user\n"),
326 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
328 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
330 _("Call from `%s' resumed by other user\n"),
331 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
343 struct GNUNET_GNSRECORD_Data rd;
345 if (NULL == my_caller_id)
348 _("Ego `%s' no longer available, phone is now down.\n"),
350 phone_state = PS_LOOKUP_EGO;
353 GNUNET_assert (NULL == phone);
354 phone = GNUNET_CONVERSATION_phone_create (cfg,
356 &phone_event_handler,
358 /* FIXME: get record and print full GNS record info later here... */
363 _("Failed to setup phone (internal error)\n"));
364 phone_state = PS_ERROR;
368 GNUNET_CONVERSATION_phone_get_record (phone,
370 GNUNET_free_non_null (address);
371 address = GNUNET_GNSRECORD_value_to_string (rd.record_type,
375 _("Phone active at `%s'. Type `/help' for a list of available commands\n"),
377 phone_state = PS_LISTEN;
383 * Function called with an event emitted by a call.
385 * @param cls closure, NULL
386 * @param code type of the event on the call
389 call_event_handler (void *cls,
390 enum GNUNET_CONVERSATION_CallEventCode code)
396 case GNUNET_CONVERSATION_EC_CALL_RINGING:
397 GNUNET_break (CS_RESOLVING == call_state);
399 _("Resolved address of `%s'. Now ringing other party.\n"),
401 call_state = CS_RINGING;
403 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
404 GNUNET_break (CS_RINGING == call_state);
406 _("Connection established to `%s'\n"),
408 call_state = CS_CONNECTED;
410 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
411 GNUNET_break (CS_RESOLVING == call_state);
413 _("Failed to resolve `%s'\n"),
415 GNUNET_free (peer_name);
419 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
421 _("Call to `%s' terminated\n"),
423 GNUNET_free (peer_name);
427 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
428 GNUNET_break (CS_CONNECTED == call_state);
430 _("Connection to `%s' suspended (by other user)\n"),
433 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
434 GNUNET_break (CS_CONNECTED == call_state);
436 _("Connection to `%s' resumed (by other user)\n"),
439 case GNUNET_CONVERSATION_EC_CALL_ERROR:
441 _("Error with the call, restarting it\n"));
442 GNUNET_free (peer_name);
451 * Function declareation for executing a action
453 * @param arguments arguments given to the function
456 (*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)
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,
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)
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)
687 _("We are currently trying to locate the private key for the ego `%s'.\n"),
692 _("We are listening for incoming calls for ego `%s' on line `%s'.\n"),
698 _("You are having a conversation with `%s'.\n"),
699 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));;
703 _("We had an internal error setting up our phone line. You can still make calls.\n"));
712 _("We are trying to find the network address to call `%s'.\n"),
717 _("We are calling `%s', his phone should be ringing.\n"),
722 _("You are having a conversation with `%s'.\n"),
726 /* ok to accept incoming call right now */
730 if ( (NULL != cl_head) &&
731 ( (cl_head != cl_active) ||
732 (cl_head != cl_tail) ) )
736 _("Calls waiting:\n"));
737 for (cl = cl_head; NULL != cl; cl = cl->next)
744 GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
756 * @param args arguments given to the command
759 do_suspend (const char *args)
771 _("There is no call that could be suspended right now.\n"));
774 call_state = CS_SUSPENDED;
775 GNUNET_CONVERSATION_call_suspend (call);
786 _("There is no call that could be suspended right now.\n"));
789 /* expected state, do rejection logic */
792 GNUNET_assert (NULL != cl_active);
793 GNUNET_CONVERSATION_caller_suspend (cl_active->caller);
795 phone_state = PS_LISTEN;
802 * @param args arguments given to the command
805 do_resume (const char *args)
819 _("There is no call that could be resumed right now.\n"));
822 call_state = CS_CONNECTED;
823 GNUNET_CONVERSATION_call_resume (call,
835 _("There is no call that could be resumed right now.\n"));
838 /* expected state, do resume logic */
842 _("Already talking with `%s', cannot resume a call right now.\n"),
843 GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
846 GNUNET_assert (NULL == cl_active);
851 _("There is no incoming call to resume here!\n"));
854 if ( (NULL != cl->next) || (NULL != args) )
856 for (cl = cl_head; NULL != cl; cl = cl->next)
858 GNUNET_snprintf (buf, sizeof (buf),
861 if (0 == strcmp (buf, args))
868 _("There is no incoming call `%s' to resume right now!\n"),
873 GNUNET_CONVERSATION_caller_resume (cl_active->caller,
876 phone_state = PS_ACCEPTED;
883 * @param args arguments given to the command
886 do_reject (const char *args)
893 GNUNET_CONVERSATION_call_stop (call);
903 _("There is no call that could be cancelled right now.\n"));
906 /* look for active incoming calls to refuse */
911 _("There is no incoming call to refuse here!\n"));
914 if ( (NULL != cl->next) || (NULL != args) )
916 for (cl = cl_head; NULL != cl; cl = cl->next)
918 GNUNET_snprintf (buf, sizeof (buf),
921 if (0 == strcmp (buf, args))
928 _("There is no incoming call `%s' to refuse right now!\n"),
932 GNUNET_CONVERSATION_caller_hang_up (cl->caller);
933 GNUNET_CONTAINER_DLL_remove (cl_head,
939 /* expected state, do rejection logic */
940 GNUNET_assert (NULL != cl_active);
941 GNUNET_CONVERSATION_caller_hang_up (cl_active->caller);
943 phone_state = PS_LISTEN;
950 * List of supported commands.
952 static struct VoipCommand commands[] = {
953 {"/address", &do_address,
954 gettext_noop ("Use `/address' to find out which address this phone should have in GNS")},
956 gettext_noop ("Use `/call USER.gnu' to call USER")},
957 {"/accept", &do_accept,
958 gettext_noop ("Use `/accept #NUM' to accept incoming call #NUM")},
959 {"/suspend", &do_suspend,
960 gettext_noop ("Use `/suspend' to suspend the active call")},
961 {"/resume", &do_resume,
962 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.")},
963 {"/cancel", &do_reject,
964 gettext_noop ("Use `/cancel' to reject or terminate a call")},
965 {"/status", &do_status,
966 gettext_noop ("Use `/status' to print status information")},
968 gettext_noop ("Use `/quit' to terminate gnunet-conversation")},
970 gettext_noop ("Use `/help command' to get help for a specific command")},
978 * Action function to print help for the command shell.
980 * @param args arguments given to the command
983 do_help (const char *args)
988 while ( (NULL != args) &&
989 (0 != strlen (args)) &&
990 (commands[i].Action != &do_help))
993 strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
997 gettext (commands[i].helptext));
1005 "Available commands:\n");
1006 while (commands[i].Action != &do_help)
1010 gettext (commands[i].command));
1018 gettext (commands[i].helptext));
1023 * Task run during shutdown.
1028 do_stop_task (void *cls)
1032 if (NULL != stdin_hlp)
1034 GNUNET_HELPER_stop (stdin_hlp, GNUNET_NO);
1040 GNUNET_CONVERSATION_call_stop (call);
1045 GNUNET_CONVERSATION_phone_destroy (phone);
1048 if (NULL != handle_cmd_task)
1050 GNUNET_SCHEDULER_cancel (handle_cmd_task);
1051 handle_cmd_task = NULL;
1055 GNUNET_IDENTITY_disconnect (id);
1058 GNUNET_SPEAKER_destroy (speaker);
1060 GNUNET_MICROPHONE_destroy (mic);
1062 GNUNET_free (ego_name);
1064 GNUNET_free_non_null (peer_name);
1066 phone_state = PS_ERROR;
1071 * Handle user command.
1073 * @param message command the user typed in
1074 * @param str_len number of bytes to process in @a message
1077 handle_command_string (char *message,
1085 if (message[str_len - 1] == '\n')
1086 message[str_len - 1] = '\0';
1087 if (message[str_len - 2] == '\r')
1088 message[str_len - 2] = '\0';
1089 if (0 == strlen (message))
1092 while ((NULL != commands[i].command) &&
1093 (0 != strncasecmp (commands[i].command, message,
1094 strlen (commands[i].command))))
1096 ptr = &message[strlen (commands[i].command)];
1097 while (isspace ((unsigned char) *ptr))
1101 commands[i].Action (ptr);
1107 console_reader_chars (void *cls,
1109 const struct GNUNET_MessageHeader *message)
1115 switch (ntohs (message->type))
1117 case GNUNET_MESSAGE_TYPE_W32_CONSOLE_HELPER_CHARS:
1118 chars = (char *) &message[1];
1119 str_size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
1120 if (chars[str_size - 1] != '\0')
1121 return GNUNET_SYSERR;
1122 /* FIXME: is it ok that we pass part of a const struct to
1123 * this function that may mangle the contents?
1125 handle_command_string (chars, str_size - 1);
1137 * Task to handle commands from the terminal.
1142 handle_command (void *cls)
1144 char message[MAX_MESSAGE_LENGTH + 1];
1148 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1150 &handle_command, NULL);
1151 /* read message from command line and handle it */
1154 MAX_MESSAGE_LENGTH + 1);
1155 if (NULL == fgets (message,
1159 handle_command_string (message,
1165 * Function called by identity service with information about egos.
1168 * @param ego ego handle
1170 * @param name name of the ego
1173 identity_cb (void *cls,
1174 struct GNUNET_IDENTITY_Ego *ego,
1182 if (ego == my_caller_id)
1186 _("Name of our ego changed to `%s'\n"),
1188 GNUNET_free (ego_name);
1189 ego_name = GNUNET_strdup (name);
1192 if (0 != strcmp (name,
1199 _("Our ego `%s' was deleted!\n"),
1201 my_caller_id = NULL;
1205 GNUNET_CONFIGURATION_set_value_string (cfg,
1214 * Main function that will be run by the scheduler.
1216 * @param cls closure
1217 * @param args remaining command-line arguments
1218 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1219 * @param c configuration
1224 const char *cfgfile,
1225 const struct GNUNET_CONFIGURATION_Handle *c)
1230 cfg = GNUNET_CONFIGURATION_dup (c);
1231 speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
1232 mic = GNUNET_MICROPHONE_create_from_hardware (cfg);
1233 if (NULL == ego_name)
1237 _("You must specify the NAME of an ego to use\n"));
1240 id = GNUNET_IDENTITY_connect (cfg,
1244 if (stdin_fh == NULL)
1246 static char cpid[64];
1247 static char *args[] = {"gnunet-helper-w32-console.exe", "chars",
1248 XSTRINGIFY (MAX_MESSAGE_LENGTH), cpid, NULL};
1249 snprintf (cpid, 64, "%d", GetCurrentProcessId ());
1250 stdin_hlp = GNUNET_HELPER_start (
1252 "gnunet-helper-w32-console",
1254 console_reader_chars,
1257 if (NULL == stdin_hlp)
1261 _("Failed to start gnunet-helper-w32-console\n"));
1268 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
1269 &handle_command, NULL);
1270 GNUNET_SCHEDULER_add_shutdown (&do_stop_task,
1276 * The main function to conversation.
1278 * @param argc number of arguments from the command line
1279 * @param argv command line arguments
1280 * @return 0 ok, 1 on error
1286 struct GNUNET_GETOPT_CommandLineOption options[] = {
1287 GNUNET_GETOPT_option_string ('e',
1290 gettext_noop ("sets the NAME of the ego to use for the caller ID"),
1292 GNUNET_GETOPT_option_string ('p',
1295 gettext_noop ("sets the LINE to use for the phone"),
1297 GNUNET_GETOPT_OPTION_END
1302 flags = fcntl (0, F_GETFL, 0);
1303 flags |= O_NONBLOCK;
1304 if (0 != fcntl (0, F_SETFL, flags))
1305 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1307 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1309 if (FILE_TYPE_CHAR == GetFileType ((HANDLE) _get_osfhandle (0)))
1314 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1318 GNUNET_STRINGS_get_utf8_args (argc, argv,
1321 ret = GNUNET_PROGRAM_run (argc,
1323 "gnunet-conversation",
1324 gettext_noop ("Enables having a conversation with other GNUnet users."),
1327 GNUNET_free ((void *) argv);
1330 GNUNET_CONFIGURATION_destroy (cfg);
1333 return (GNUNET_OK == ret) ? 0 : 1;
1336 /* end of gnunet-conversation.c */