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 * String identifying the caller.
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 *caller_id;
199 * Handle to identity service.
201 static struct GNUNET_IDENTITY_Handle *id;
206 static char *ego_name;
209 * Name of 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 name 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 char *caller_id)
262 case GNUNET_CONVERSATION_EC_PHONE_RING:
264 _("Incoming call from `%s'. Please /accept #%u or /cancel %u the call.\n"),
268 cl = GNUNET_new (struct CallList);
270 cl->caller_id = GNUNET_strdup (caller_id);
271 cl->caller_num = caller_num_gen++;
272 GNUNET_CONTAINER_DLL_insert (cl_head,
276 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
277 for (cl = cl_head; NULL != cl; cl = cl->next)
278 if (caller == cl->caller)
286 _("Call from `%s' terminated\n"),
288 GNUNET_CONTAINER_DLL_remove (cl_head,
291 GNUNET_free (cl->caller_id);
295 phone_state = PS_LISTEN;
304 * Function called with an event emitted by a caller.
306 * @param cls closure with the `struct CallList` of the caller
307 * @param code type of the event issued by the caller
310 caller_event_handler (void *cls,
311 enum GNUNET_CONVERSATION_CallerEventCode code)
313 struct CallList *cl = cls;
317 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
319 _("Call from `%s' suspended by other user\n"),
322 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
324 _("Call from `%s' resumed by other user\n"),
337 struct GNUNET_GNSRECORD_Data rd;
339 if (NULL == caller_id)
342 _("Ego `%s' no longer available, phone is now down.\n"),
344 phone_state = PS_LOOKUP_EGO;
347 GNUNET_assert (NULL == phone);
348 phone = GNUNET_CONVERSATION_phone_create (cfg,
350 &phone_event_handler, NULL);
351 /* FIXME: get record and print full GNS record info later here... */
356 _("Failed to setup phone (internal error)\n"));
357 phone_state = PS_ERROR;
361 GNUNET_CONVERSATION_phone_get_record (phone,
363 GNUNET_free_non_null (address);
364 address = GNUNET_GNSRECORD_value_to_string (rd.record_type,
368 _("Phone active on line %u. Type `/help' for a list of available commands\n"),
369 (unsigned int) line);
370 phone_state = PS_LISTEN;
376 * Function called with an event emitted by a call.
378 * @param cls closure, NULL
379 * @param code type of the event on the call
382 call_event_handler (void *cls,
383 enum GNUNET_CONVERSATION_CallEventCode code)
387 case GNUNET_CONVERSATION_EC_CALL_RINGING:
388 GNUNET_break (CS_RESOLVING == call_state);
390 _("Resolved address of `%s'. Now ringing other party.\n"),
392 call_state = CS_RINGING;
394 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
395 GNUNET_break (CS_RINGING == call_state);
397 _("Connection established to `%s'\n"),
399 call_state = CS_CONNECTED;
401 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
402 GNUNET_break (CS_RESOLVING == call_state);
404 _("Failed to resolve `%s'\n"),
408 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
411 _("Call terminated\n"));
414 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
415 GNUNET_break (CS_CONNECTED == call_state);
417 _("Connection to `%s' suspended (by other user)\n"),
420 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
421 GNUNET_break (CS_CONNECTED == call_state);
423 _("Connection to `%s' resumed (by other user)\n"),
426 case GNUNET_CONVERSATION_EC_CALL_ERROR:
428 _("Error with the call, restarting it\n"));
429 call_state = CS_RESOLVING;
436 * Function declareation for executing a action
438 * @param arguments arguments given to the function
440 typedef void (*ActionFunction) (const char *arguments);
444 * Structure which defines a command
449 * Command the user needs to enter.
454 * Function to call on command.
456 ActionFunction Action;
459 * Help text for the command.
461 const char *helptext;
466 * Action function to print help for the command shell.
468 * @param args arguments given to the command
471 do_help (const char *args);
475 * Terminate the client
477 * @param args arguments given to the command
480 do_quit (const char *args)
482 GNUNET_SCHEDULER_shutdown ();
487 * Handler for unknown command.
489 * @param msg arguments given to the command
492 do_unknown (const char *msg)
495 _("Unknown command `%s'\n"),
501 * Initiating a new call
503 * @param arg arguments given to the command
506 do_call (const char *arg)
508 if (NULL == caller_id)
511 _("Ego `%s' not available\n"),
518 _("You are calling someone else already, hang up first!\n"));
525 _("Ego `%s' not available\n"),
533 _("You are answering call from `%s', hang up or suspend that call first!\n"),
540 GNUNET_free_non_null (peer_name);
544 _("Call recipient missing.\n"));
548 peer_name = GNUNET_strdup (arg);
549 call_state = CS_RESOLVING;
550 GNUNET_assert (NULL == call);
551 call = GNUNET_CONVERSATION_call_start (cfg,
556 &call_event_handler, NULL);
561 * Accepting an incoming call
563 * @param args arguments given to the command
566 do_accept (const char *args)
571 if ( (NULL != call) &&
572 (CS_SUSPENDED != call_state) )
575 _("You are calling someone else already, hang up first!\n"));
584 /* this is the expected state */
588 _("You are answering call from `%s', hang up or suspend that call first!\n"),
599 _("There is no incoming call to accept here!\n"));
602 if ( (NULL != cl->next) || (NULL != args) )
604 for (cl = cl_head; NULL != cl; cl = cl->next)
606 GNUNET_snprintf (buf, sizeof (buf),
609 if (0 == strcmp (buf, args))
616 _("There is no incoming call `%s' to accept right now!\n"),
621 GNUNET_free_non_null (peer_name);
622 peer_name = GNUNET_strdup (cl->caller_id);
623 phone_state = PS_ACCEPTED;
624 GNUNET_CONVERSATION_caller_pick_up (cl->caller,
625 &caller_event_handler,
633 * Print address information for this phone.
635 * @param args arguments given to the command
638 do_address (const char *args)
644 _("We currently do not have an address.\n"));
654 * Accepting an incoming call
656 * @param args arguments given to the command
659 do_status (const char *args)
667 _("We are currently trying to locate the private key for the ego `%s'.\n"),
672 _("We are listening for incoming calls for ego `%s' on line %u.\n"),
678 _("You are having a conversation with `%s'.\n"),
683 _("We had an internal error setting up our phone line. You can still make calls.\n"));
692 _("We are trying to find the network address to call `%s'.\n"),
697 _("We are calling `%s', his phone should be ringing.\n"),
702 _("You are having a conversation with `%s'.\n"),
706 /* ok to accept incoming call right now */
710 if ( (NULL != cl_head) &&
711 ( (cl_head != cl_active) ||
712 (cl_head != cl_tail) ) )
716 _("Calls waiting:\n"));
717 for (cl = cl_head; NULL != cl; cl = cl->next)
736 * @param args arguments given to the command
739 do_suspend (const char *args)
750 _("There is no call that could be suspended right now.\n"));
753 call_state = CS_SUSPENDED;
754 GNUNET_CONVERSATION_call_suspend (call);
765 _("There is no call that could be suspended right now.\n"));
768 /* expected state, do rejection logic */
771 GNUNET_assert (NULL != cl_active);
772 GNUNET_CONVERSATION_caller_suspend (cl_active->caller);
774 phone_state = PS_LISTEN;
781 * @param args arguments given to the command
784 do_resume (const char *args)
798 _("There is no call that could be resumed right now.\n"));
801 call_state = CS_CONNECTED;
802 GNUNET_CONVERSATION_call_resume (call,
814 _("There is no call that could be resumed right now.\n"));
817 /* expected state, do resume logic */
821 _("Already talking with `%s', cannot resume a call right now.\n"),
825 GNUNET_assert (NULL == cl_active);
830 _("There is no incoming call to resume here!\n"));
833 if ( (NULL != cl->next) || (NULL != args) )
835 for (cl = cl_head; NULL != cl; cl = cl->next)
837 GNUNET_snprintf (buf, sizeof (buf),
840 if (0 == strcmp (buf, args))
847 _("There is no incoming call `%s' to resume right now!\n"),
852 GNUNET_CONVERSATION_caller_resume (cl_active->caller,
855 phone_state = PS_ACCEPTED;
862 * @param args arguments given to the command
865 do_reject (const char *args)
872 GNUNET_CONVERSATION_call_stop (call);
882 _("There is no call that could be cancelled right now.\n"));
885 /* look for active incoming calls to refuse */
890 _("There is no incoming call to refuse here!\n"));
893 if ( (NULL != cl->next) || (NULL != args) )
895 for (cl = cl_head; NULL != cl; cl = cl->next)
897 GNUNET_snprintf (buf, sizeof (buf),
900 if (0 == strcmp (buf, args))
907 _("There is no incoming call `%s' to refuse right now!\n"),
911 GNUNET_CONVERSATION_caller_hang_up (cl->caller);
912 GNUNET_CONTAINER_DLL_remove (cl_head,
915 GNUNET_free (cl->caller_id);
919 /* expected state, do rejection logic */
920 GNUNET_assert (NULL != cl_active);
921 GNUNET_CONVERSATION_caller_hang_up (cl_active->caller);
923 phone_state = PS_LISTEN;
930 * List of supported commands.
932 static struct VoipCommand commands[] = {
933 {"/address", &do_address,
934 gettext_noop ("Use `/address' to find out which address this phone should have in GNS")},
936 gettext_noop ("Use `/call USER.gnu' to call USER")},
937 {"/accept", &do_accept,
938 gettext_noop ("Use `/accept #NUM' to accept incoming call #NUM")},
939 {"/suspend", &do_suspend,
940 gettext_noop ("Use `/suspend' to suspend the active call")},
941 {"/resume", &do_resume,
942 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.")},
943 {"/cancel", &do_reject,
944 gettext_noop ("Use `/cancel' to reject or terminate a call")},
945 {"/status", &do_status,
946 gettext_noop ("Use `/status' to print status information")},
948 gettext_noop ("Use `/quit' to terminate gnunet-conversation")},
950 gettext_noop ("Use `/help command' to get help for a specific command")},
958 * Action function to print help for the command shell.
960 * @param args arguments given to the command
963 do_help (const char *args)
968 while ( (NULL != args) &&
969 (0 != strlen (args)) &&
970 (commands[i].Action != &do_help))
973 strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
977 gettext (commands[i].helptext));
985 "Available commands:\n");
986 while (commands[i].Action != &do_help)
990 gettext (commands[i].command));
998 gettext (commands[i].helptext));
1003 * Task run during shutdown.
1009 do_stop_task (void *cls,
1010 const struct GNUNET_SCHEDULER_TaskContext *tc)
1013 if (NULL != stdin_hlp)
1015 GNUNET_HELPER_stop (stdin_hlp, GNUNET_NO);
1021 GNUNET_CONVERSATION_call_stop (call);
1026 GNUNET_CONVERSATION_phone_destroy (phone);
1029 if (GNUNET_SCHEDULER_NO_TASK != handle_cmd_task)
1031 GNUNET_SCHEDULER_cancel (handle_cmd_task);
1032 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
1036 GNUNET_IDENTITY_disconnect (id);
1039 GNUNET_SPEAKER_destroy (speaker);
1041 GNUNET_MICROPHONE_destroy (mic);
1043 GNUNET_free (ego_name);
1045 GNUNET_free_non_null (peer_name);
1046 phone_state = PS_ERROR;
1050 handle_command_string (char *message, size_t str_len)
1057 if (message[str_len - 1] == '\n')
1058 message[str_len - 1] = '\0';
1059 if (message[str_len - 2] == '\r')
1060 message[str_len - 2] = '\0';
1061 if (0 == strlen (message))
1064 while ((NULL != commands[i].command) &&
1065 (0 != strncasecmp (commands[i].command, message,
1066 strlen (commands[i].command))))
1068 ptr = &message[strlen (commands[i].command)];
1069 while (isspace ((int) *ptr))
1073 commands[i].Action (ptr);
1079 console_reader_chars (void *cls, void *client,
1080 const struct GNUNET_MessageHeader *message)
1084 switch (ntohs (message->type))
1086 case GNUNET_MESSAGE_TYPE_W32_CONSOLE_HELPER_CHARS:
1087 chars = (char *) &message[1];
1088 str_size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
1089 if (chars[str_size - 1] != '\0')
1090 return GNUNET_SYSERR;
1091 /* FIXME: is it ok that we pass part of a const struct to
1092 * this function that may mangle the contents?
1094 handle_command_string (chars, str_size - 1);
1105 * Task to handle commands from the terminal.
1108 * @param tc scheduler context
1111 handle_command (void *cls,
1112 const struct GNUNET_SCHEDULER_TaskContext *tc)
1114 char message[MAX_MESSAGE_LENGTH + 1];
1117 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1119 &handle_command, NULL);
1120 /* read message from command line and handle it */
1121 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
1122 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
1124 handle_command_string (message, strlen (message));
1129 * Function called by identity service with information about egos.
1132 * @param ego ego handle
1134 * @param name name of the ego
1137 identity_cb (void *cls,
1138 struct GNUNET_IDENTITY_Ego *ego,
1144 if (ego == caller_id)
1148 _("Name of our ego changed to `%s'\n"),
1150 GNUNET_free (ego_name);
1151 ego_name = GNUNET_strdup (name);
1154 if (0 != strcmp (name,
1161 _("Our ego `%s' was deleted!\n"),
1167 GNUNET_CONFIGURATION_set_value_number (cfg,
1176 * Main function that will be run by the scheduler.
1178 * @param cls closure
1179 * @param args remaining command-line arguments
1180 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1181 * @param c configuration
1186 const char *cfgfile,
1187 const struct GNUNET_CONFIGURATION_Handle *c)
1189 cfg = GNUNET_CONFIGURATION_dup (c);
1190 speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
1191 mic = GNUNET_MICROPHONE_create_from_hardware (cfg);
1192 if (NULL == ego_name)
1196 _("You must specify the NAME of an ego to use\n"));
1199 id = GNUNET_IDENTITY_connect (cfg,
1203 if (stdin_fh == NULL)
1205 static char cpid[64];
1206 static char *args[] = {"gnunet-helper-w32-console.exe", "chars",
1207 XSTRINGIFY (MAX_MESSAGE_LENGTH), cpid, NULL};
1208 snprintf (cpid, 64, "%d", GetCurrentProcessId ());
1209 stdin_hlp = GNUNET_HELPER_start (
1211 "gnunet-helper-w32-console",
1213 console_reader_chars,
1216 if (NULL == stdin_hlp)
1220 _("Failed to start gnunet-helper-w32-console\n"));
1227 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
1228 &handle_command, NULL);
1229 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
1235 * The main function to conversation.
1237 * @param argc number of arguments from the command line
1238 * @param argv command line arguments
1239 * @return 0 ok, 1 on error
1242 main (int argc, char *const *argv)
1244 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1245 {'e', "ego", "NAME",
1246 gettext_noop ("sets the NAME of the ego to use for the phone (and name resolution)"),
1247 1, &GNUNET_GETOPT_set_string, &ego_name},
1248 {'p', "phone", "LINE",
1249 gettext_noop ("sets the LINE to use for the phone"),
1250 1, &GNUNET_GETOPT_set_uint, &line},
1251 GNUNET_GETOPT_OPTION_END
1256 flags = fcntl (0, F_GETFL, 0);
1257 flags |= O_NONBLOCK;
1258 fcntl (0, F_SETFL, flags);
1259 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1261 if (FILE_TYPE_CHAR == GetFileType ((HANDLE) _get_osfhandle (0)))
1266 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1269 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1271 ret = GNUNET_PROGRAM_run (argc, argv,
1272 "gnunet-conversation",
1273 gettext_noop ("Enables having a conversation with other GNUnet users."),
1274 options, &run, NULL);
1275 GNUNET_free ((void *) argv);
1278 GNUNET_CONFIGURATION_destroy (cfg);
1281 return (GNUNET_OK == ret) ? 0 : 1;
1284 /* end of gnunet-conversation.c */