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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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.
57 * We're waiting for our own idenitty.
62 * We're listening for calls
67 * We accepted an incoming phone call.
79 * States for current outgoing call.
83 * We are looking up some other participant.
88 * We are now ringing the other participant.
93 * The other party accepted our call and we are now connected.
98 * The call is currently suspended (by us).
105 * 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;
138 static struct GNUNET_CONVERSATION_Phone *phone;
141 * Call handle (for active outgoing call).
143 static struct GNUNET_CONVERSATION_Call *call;
146 * Caller handle (for active incoming call).
147 * This call handler is NOT in the #cl_head / #cl_tail list.
149 static struct CallList *cl_active;
152 * Head of calls waiting to be accepted.
154 static struct CallList *cl_head;
157 * Tail of calls waiting to be accepted.
159 static struct CallList *cl_tail;
162 * Desired phone line (string to be converted to a hash).
167 * Task which handles the commands
169 static struct GNUNET_SCHEDULER_Task *handle_cmd_task;
174 static struct GNUNET_SPEAKER_Handle *speaker;
179 static struct GNUNET_MICROPHONE_Handle *mic;
184 static struct GNUNET_CONFIGURATION_Handle *cfg;
189 static struct GNUNET_IDENTITY_Ego *my_caller_id;
192 * Handle to identity service.
194 static struct GNUNET_IDENTITY_Handle *id;
199 static char *ego_name;
202 * Public key of active conversation partner (if any).
204 static struct GNUNET_CRYPTO_EcdsaPublicKey peer_key;
207 * Name of active conversation partner (if any).
209 static char *peer_name;
212 * File handle for stdin.
214 static struct GNUNET_DISK_FileHandle *stdin_fh;
217 * Our phone's current state.
219 static enum PhoneState phone_state;
222 * Our call's current state.
224 static enum CallState call_state;
227 * Counts the number of incoming calls we have had so far.
229 static unsigned int caller_num_gen;
232 * GNS address for this phone.
234 static char *address;
243 * Function called with an event emitted by a phone.
246 * @param code type of the event
247 * @param caller handle for the caller
248 * @param caller_id public key of the caller (in GNS)
251 phone_event_handler(void *cls,
252 enum GNUNET_CONVERSATION_PhoneEventCode code,
253 struct GNUNET_CONVERSATION_Caller *caller,
254 const struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
261 case GNUNET_CONVERSATION_EC_PHONE_RING:
263 * FIXME: we should be playing our ringtones from contrib/sounds now!
267 * see https://gstreamer.freedesktop.org/documentation/application-development/highlevel/playback-components.html on how to play a wav using the gst framework being used here
272 "Incoming call from `%s'. Please /accept %u or /cancel %u the call.\n"),
273 GNUNET_GNSRECORD_pkey_to_zkey(caller_id),
276 cl = GNUNET_new(struct CallList);
278 cl->caller_id = *caller_id;
279 cl->caller_num = caller_num_gen++;
280 GNUNET_CONTAINER_DLL_insert(cl_head, cl_tail, cl);
283 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
284 for (cl = cl_head; NULL != cl; cl = cl->next)
285 if (caller == cl->caller)
287 if ((NULL == cl) && (caller == cl_active->caller))
295 _("Call from `%s' terminated\n"),
296 GNUNET_GNSRECORD_pkey_to_zkey(&cl->caller_id));
300 phone_state = PS_LISTEN;
304 GNUNET_CONTAINER_DLL_remove(cl_head, cl_tail, cl);
313 * Function called with an event emitted by a caller.
315 * @param cls closure with the `struct CallList` of the caller
316 * @param code type of the event issued by the caller
319 caller_event_handler(void *cls, enum GNUNET_CONVERSATION_CallerEventCode code)
321 struct CallList *cl = cls;
325 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
327 _("Call from `%s' suspended by other user\n"),
328 GNUNET_GNSRECORD_pkey_to_zkey(&cl->caller_id));
331 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
333 _("Call from `%s' resumed by other user\n"),
334 GNUNET_GNSRECORD_pkey_to_zkey(&cl->caller_id));
346 struct GNUNET_GNSRECORD_Data rd;
348 if (NULL == my_caller_id)
351 _("Ego `%s' no longer available, phone is now down.\n"),
353 phone_state = PS_LOOKUP_EGO;
356 GNUNET_assert(NULL == phone);
357 phone = GNUNET_CONVERSATION_phone_create(cfg,
359 &phone_event_handler,
361 /* FIXME: get record and print full GNS record info later here... */
364 fprintf(stderr, "%s", _("Failed to setup phone (internal error)\n"));
365 phone_state = PS_ERROR;
369 GNUNET_CONVERSATION_phone_get_record(phone, &rd);
370 GNUNET_free_non_null(address);
372 GNUNET_GNSRECORD_value_to_string(rd.record_type, rd.data, rd.data_size);
376 "Phone active at `%s'. Type `/help' for a list of available commands\n"),
378 phone_state = PS_LISTEN;
384 * Function called with an event emitted by a call.
386 * @param cls closure, NULL
387 * @param code type of the event on the call
390 call_event_handler(void *cls, 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;
404 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
405 GNUNET_break(CS_RINGING == call_state);
406 fprintf(stdout, _("Connection established to `%s'\n"), peer_name);
407 call_state = CS_CONNECTED;
410 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
411 GNUNET_break(CS_RESOLVING == call_state);
412 fprintf(stdout, _("Failed to resolve `%s'\n"), peer_name);
413 GNUNET_free(peer_name);
418 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
419 fprintf(stdout, _("Call to `%s' terminated\n"), peer_name);
420 GNUNET_free(peer_name);
425 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
426 GNUNET_break(CS_CONNECTED == call_state);
428 _("Connection to `%s' suspended (by other user)\n"),
432 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
433 GNUNET_break(CS_CONNECTED == call_state);
435 _("Connection to `%s' resumed (by other user)\n"),
439 case GNUNET_CONVERSATION_EC_CALL_ERROR:
440 fprintf(stdout, _("Error with the call, restarting it\n"));
441 GNUNET_free(peer_name);
450 * Function declareation for executing a action
452 * @param arguments arguments given to the function
454 typedef void (*ActionFunction) (const char *arguments);
458 * Structure which defines a command
462 * Command the user needs to enter.
467 * Function to call on command.
469 ActionFunction Action;
472 * Help text for the command.
474 const char *helptext;
479 * Action function to print help for the command shell.
481 * @param args arguments given to the command
484 do_help(const char *args);
488 * Terminate the client
490 * @param args arguments given to the command
493 do_quit(const char *args)
496 GNUNET_SCHEDULER_shutdown();
501 * Handler for unknown command.
503 * @param msg arguments given to the command
506 do_unknown(const char *msg)
508 fprintf(stderr, _("Unknown command `%s'\n"), msg);
513 * Initiating a new call
515 * @param arg arguments given to the command
518 do_call(const char *arg)
520 if (NULL == my_caller_id)
522 fprintf(stderr, _("Ego `%s' not available\n"), ego_name);
528 _("You are calling someone else already, hang up first!\n"));
534 fprintf(stderr, _("Ego `%s' not available\n"), ego_name);
545 "You are answering call from `%s', hang up or suspend that call first!\n"),
546 GNUNET_GNSRECORD_pkey_to_zkey(&peer_key));
555 fprintf(stderr, _("Call recipient missing.\n"));
559 peer_name = GNUNET_strdup(arg);
560 call_state = CS_RESOLVING;
561 GNUNET_assert(NULL == call);
562 call = GNUNET_CONVERSATION_call_start(cfg,
573 * Accepting an incoming call
575 * @param args arguments given to the command
578 do_accept(const char *args)
583 if ((NULL != call) && (CS_SUSPENDED != call_state))
586 _("You are calling someone else already, hang up first!\n"));
596 /* this is the expected state */
603 "You are answering call from `%s', hang up or suspend that call first!\n"),
604 GNUNET_GNSRECORD_pkey_to_zkey(&peer_key));
614 fprintf(stderr, _("There is no incoming call to accept here!\n"));
617 if ((NULL != cl->next) || (NULL != args))
619 for (cl = cl_head; NULL != cl; cl = cl->next)
621 GNUNET_snprintf(buf, sizeof(buf), "%u", cl->caller_num);
622 if (0 == strcmp(buf, args))
629 _("There is no incoming call `%s' to accept right now!\n"),
633 GNUNET_CONTAINER_DLL_remove(cl_head, cl_tail, cl);
635 peer_key = cl->caller_id;
636 phone_state = PS_ACCEPTED;
637 GNUNET_CONVERSATION_caller_pick_up(cl->caller,
638 &caller_event_handler,
646 * Print address information for this phone.
648 * @param args arguments given to the command
651 do_address(const char *args)
656 fprintf(stdout, "%s", _("We currently do not have an address.\n"));
659 fprintf(stdout, "%s\n", address);
664 * Accepting an incoming call
666 * @param args arguments given to the command
669 do_status(const char *args)
680 "We are currently trying to locate the private key for the ego `%s'.\n"),
687 "We are listening for incoming calls for ego `%s' on line `%s'.\n"),
694 _("You are having a conversation with `%s'.\n"),
695 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"),
718 _("We are calling `%s', their phone should be ringing.\n"),
724 _("You are having a conversation with `%s'.\n"),
729 /* ok to accept incoming call right now */
733 if ((NULL != cl_head) && ((cl_head != cl_active) || (cl_head != cl_tail)))
735 fprintf(stdout, "%s", _("Calls waiting:\n"));
736 for (cl = cl_head; NULL != cl; cl = cl->next)
743 GNUNET_GNSRECORD_pkey_to_zkey(&cl->caller_id));
745 fprintf(stdout, "%s", "\n");
753 * @param args arguments given to the command
756 do_suspend(const char *args)
768 _("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"));
788 /* expected state, do rejection logic */
791 GNUNET_assert(NULL != cl_active);
792 GNUNET_CONVERSATION_caller_suspend(cl_active->caller);
794 phone_state = PS_LISTEN;
801 * @param args arguments given to the command
804 do_resume(const char *args)
818 _("There is no call that could be resumed right now.\n"));
822 call_state = CS_CONNECTED;
823 GNUNET_CONVERSATION_call_resume(call, speaker, mic);
833 _("There is no call that could be resumed right now.\n"));
837 /* 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);
850 fprintf(stderr, _("There is no incoming call to resume here!\n"));
853 if ((NULL != cl->next) || (NULL != args))
855 for (cl = cl_head; NULL != cl; cl = cl->next)
857 GNUNET_snprintf(buf, sizeof(buf), "%u", cl->caller_num);
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, speaker, mic);
871 phone_state = PS_ACCEPTED;
878 * @param args arguments given to the command
881 do_reject(const char *args)
888 GNUNET_CONVERSATION_call_stop(call);
898 _("There is no call that could be cancelled right now.\n"));
902 /* look for active incoming calls to refuse */
906 fprintf(stderr, _("There is no incoming call to refuse here!\n"));
909 if ((NULL != cl->next) || (NULL != args))
911 for (cl = cl_head; NULL != cl; cl = cl->next)
913 GNUNET_snprintf(buf, sizeof(buf), "%u", cl->caller_num);
914 if (0 == strcmp(buf, args))
921 _("There is no incoming call `%s' to refuse right now!\n"),
925 GNUNET_CONVERSATION_caller_hang_up(cl->caller);
926 GNUNET_CONTAINER_DLL_remove(cl_head, cl_tail, cl);
931 /* expected state, do rejection logic */
932 GNUNET_assert(NULL != cl_active);
933 GNUNET_CONVERSATION_caller_hang_up(cl_active->caller);
935 phone_state = PS_LISTEN;
942 * List of supported commands.
944 static struct VoipCommand commands[] = {
948 "Use `/address' to find out which address this phone should have in GNS") },
949 { "/call", &do_call, gettext_noop("Use `/call USER.gnu' to call USER") },
952 gettext_noop("Use `/accept #NUM' to accept incoming call #NUM") },
955 gettext_noop("Use `/suspend' to suspend the active call") },
959 "Use `/resume [#NUM]' to resume a call, #NUM is needed to resume incoming calls, no argument is needed to resume the current outgoing call.") },
962 gettext_noop("Use `/cancel' to reject or terminate a call") },
965 gettext_noop("Use `/status' to print status information") },
968 gettext_noop("Use `/quit' to terminate gnunet-conversation") },
971 gettext_noop("Use `/help command' to get help for a specific command") },
972 { "", &do_unknown, NULL },
973 { NULL, NULL, NULL },
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) && (0 != strlen(args)) &&
989 (commands[i].Action != &do_help))
991 if (0 == strncasecmp(&args[1], &commands[i].command[1], strlen(args) - 1))
993 fprintf(stdout, "%s\n", gettext(commands[i].helptext));
999 fprintf(stdout, "%s", "Available commands:\n");
1000 while (commands[i].Action != &do_help)
1002 fprintf(stdout, "%s\n", gettext(commands[i].command));
1005 fprintf(stdout, "%s", "\n");
1006 fprintf(stdout, "%s\n", gettext(commands[i].helptext));
1011 * Task run during shutdown.
1016 do_stop_task(void *cls)
1020 if (NULL != stdin_hlp)
1022 GNUNET_HELPER_stop(stdin_hlp, GNUNET_NO);
1028 GNUNET_CONVERSATION_call_stop(call);
1033 GNUNET_CONVERSATION_phone_destroy(phone);
1036 if (NULL != handle_cmd_task)
1038 GNUNET_SCHEDULER_cancel(handle_cmd_task);
1039 handle_cmd_task = NULL;
1043 GNUNET_IDENTITY_disconnect(id);
1046 GNUNET_SPEAKER_destroy(speaker);
1048 GNUNET_MICROPHONE_destroy(mic);
1050 GNUNET_free(ego_name);
1052 GNUNET_free_non_null(peer_name);
1054 phone_state = PS_ERROR;
1059 * Handle user command.
1061 * @param message command the user typed in
1062 * @param str_len number of bytes to process in @a message
1065 handle_command_string(char *message, size_t str_len)
1072 if (message[str_len - 1] == '\n')
1073 message[str_len - 1] = '\0';
1074 if (message[str_len - 2] == '\r')
1075 message[str_len - 2] = '\0';
1076 if (0 == strlen(message))
1080 (NULL != commands[i].command) &&
1082 strncasecmp(commands[i].command, message, strlen(commands[i].command))))
1084 ptr = &message[strlen(commands[i].command)];
1085 while (isspace((unsigned char)*ptr))
1089 commands[i].Action(ptr);
1095 console_reader_chars(void *cls,
1097 const struct GNUNET_MessageHeader *message)
1103 switch (ntohs(message->type))
1105 case GNUNET_MESSAGE_TYPE_W32_CONSOLE_HELPER_CHARS:
1106 chars = (char *)&message[1];
1107 str_size = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader);
1108 if (chars[str_size - 1] != '\0')
1109 return GNUNET_SYSERR;
1110 /* FIXME: is it ok that we pass part of a const struct to
1111 * this function that may mangle the contents?
1113 handle_command_string(chars, str_size - 1);
1126 * Task to handle commands from the terminal.
1131 handle_command(void *cls)
1133 char message[MAX_MESSAGE_LENGTH + 1];
1137 GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL,
1141 /* read message from command line and handle it */
1142 memset(message, 0, MAX_MESSAGE_LENGTH + 1);
1143 if (NULL == fgets(message, MAX_MESSAGE_LENGTH, stdin))
1145 handle_command_string(message, strlen(message));
1150 * Function called by identity service with information about egos.
1153 * @param ego ego handle
1155 * @param name name of the ego
1158 identity_cb(void *cls,
1159 struct GNUNET_IDENTITY_Ego *ego,
1167 if (ego == my_caller_id)
1170 fprintf(stdout, _("Name of our ego changed to `%s'\n"), name);
1171 GNUNET_free(ego_name);
1172 ego_name = GNUNET_strdup(name);
1175 if (0 != strcmp(name, ego_name))
1180 fprintf(stdout, _("Our ego `%s' was deleted!\n"), ego_name);
1181 my_caller_id = NULL;
1185 GNUNET_CONFIGURATION_set_value_string(cfg, "CONVERSATION", "LINE", line);
1191 * Main function that will be run by the scheduler.
1193 * @param cls closure
1194 * @param args remaining command-line arguments
1195 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1196 * @param c configuration
1201 const char *cfgfile,
1202 const struct GNUNET_CONFIGURATION_Handle *c)
1207 cfg = GNUNET_CONFIGURATION_dup(c);
1208 speaker = GNUNET_SPEAKER_create_from_hardware(cfg);
1209 mic = GNUNET_MICROPHONE_create_from_hardware(cfg);
1210 if (NULL == ego_name)
1212 fprintf(stderr, "%s", _("You must specify the NAME of an ego to use\n"));
1215 id = GNUNET_IDENTITY_connect(cfg, &identity_cb, NULL);
1217 if (stdin_fh == NULL)
1219 static char cpid[64];
1220 static char *args[] = { "gnunet-helper-w32-console.exe",
1222 XSTRINGIFY(MAX_MESSAGE_LENGTH),
1225 snprintf(cpid, 64, "%d", GetCurrentProcessId());
1226 stdin_hlp = GNUNET_HELPER_start(GNUNET_NO,
1227 "gnunet-helper-w32-console",
1229 console_reader_chars,
1232 if (NULL == stdin_hlp)
1234 fprintf(stderr, "%s", _("Failed to start gnunet-helper-w32-console\n"));
1241 GNUNET_SCHEDULER_add_with_priority(GNUNET_SCHEDULER_PRIORITY_UI,
1244 GNUNET_SCHEDULER_add_shutdown(&do_stop_task, NULL);
1249 * The main function to conversation.
1251 * @param argc number of arguments from the command line
1252 * @param argv command line arguments
1253 * @return 0 ok, 1 on error
1256 main(int argc, char *const *argv)
1258 struct GNUNET_GETOPT_CommandLineOption options[] =
1259 { GNUNET_GETOPT_option_string(
1263 gettext_noop("sets the NAME of the ego to use for the caller ID"),
1265 GNUNET_GETOPT_option_string('p',
1269 "sets the LINE to use for the phone"),
1271 GNUNET_GETOPT_OPTION_END };
1276 flags = fcntl(0, F_GETFL, 0);
1277 flags |= O_NONBLOCK;
1278 if (0 != fcntl(0, F_SETFL, flags))
1279 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "fcntl");
1280 stdin_fh = GNUNET_DISK_get_handle_from_int_fd(0);
1282 if (FILE_TYPE_CHAR == GetFileType((HANDLE)_get_osfhandle(0)))
1287 stdin_fh = GNUNET_DISK_get_handle_from_int_fd(0);
1290 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
1293 GNUNET_PROGRAM_run(argc,
1295 "gnunet-conversation",
1297 "Enables having a conversation with other GNUnet users."),
1301 GNUNET_free((void *)argv);
1304 GNUNET_CONFIGURATION_destroy(cfg);
1307 return (GNUNET_OK == ret) ? 0 : 1;
1310 /* end of gnunet-conversation.c */