static Tox *init_tox(void) { struct Tox_Options tox_opts; memset(&tox_opts, 0, sizeof(struct Tox_Options)); tox_options_default(&tox_opts); Tox *m = load_tox(&tox_opts, DATA_FILE); if (!m) return NULL; tox_callback_self_connection_status(m, cb_self_connection_change, NULL); tox_callback_friend_connection_status(m, cb_friend_connection_change, NULL); tox_callback_friend_request(m, cb_friend_request, NULL); tox_callback_friend_message(m, cb_friend_message, NULL); tox_callback_group_invite(m, cb_group_invite, NULL); tox_callback_group_title(m, cb_group_titlechange, NULL); size_t s_len = tox_self_get_status_message_size(m); if (s_len == 0) { const char *statusmsg = "Send me the command 'help' for more info"; tox_self_set_status_message(m, (uint8_t *) statusmsg, strlen(statusmsg), NULL); } size_t n_len = tox_self_get_name_size(m); if (n_len == 0) tox_self_set_name(m, (uint8_t *) "[LUGNSK]", strlen("[LUGNSK]"), NULL); return m; }
static void init_tox_callbacks(Tox *m) { tox_callback_self_connection_status(m, prompt_onSelfConnectionChange); tox_callback_friend_connection_status(m, on_connectionchange); tox_callback_friend_typing(m, on_typing_change); tox_callback_friend_request(m, on_request); tox_callback_friend_message(m, on_message); tox_callback_friend_name(m, on_nickchange); tox_callback_friend_status(m, on_statuschange); tox_callback_friend_status_message(m, on_statusmessagechange); tox_callback_friend_read_receipt(m, on_read_receipt); tox_callback_conference_invite(m, on_groupinvite); tox_callback_conference_message(m, on_groupmessage); tox_callback_conference_namelist_change(m, on_group_namelistchange); tox_callback_conference_title(m, on_group_titlechange); tox_callback_file_recv(m, on_file_recv); tox_callback_file_chunk_request(m, on_file_chunk_request); tox_callback_file_recv_control(m, on_file_control); tox_callback_file_recv_chunk(m, on_file_recv_chunk); }
void Tox_Dispatcher::init_callbacks() { // self callbacks tox_callback_self_connection_status(tox, &Tox_Dispatcher::self_connection_status, this); // friend callbacks tox_callback_friend_connection_status(tox, &Tox_Dispatcher::friend_connection_status, this); tox_callback_friend_lossless_packet(tox, &Tox_Dispatcher::friend_lossless_packet, this); tox_callback_friend_lossy_packet(tox, &Tox_Dispatcher::friend_lossy_packet, this); tox_callback_friend_message(tox, &Tox_Dispatcher::friend_message, this); tox_callback_friend_name(tox, &Tox_Dispatcher::friend_name, this); tox_callback_friend_read_receipt(tox, &Tox_Dispatcher::friend_read_receipt, this); tox_callback_friend_request(tox, &Tox_Dispatcher::friend_request, this); tox_callback_file_recv(tox, Tox_Dispatcher::file_recv, this); }
static void init_tox_callbacks(Tox *m) { tox_callback_self_connection_status(m, prompt_onSelfConnectionChange, NULL); tox_callback_friend_connection_status(m, on_connectionchange, NULL); tox_callback_friend_typing(m, on_typing_change, NULL); tox_callback_friend_request(m, on_request, NULL); tox_callback_friend_message(m, on_message, NULL); tox_callback_friend_name(m, on_nickchange, NULL); tox_callback_friend_status(m, on_statuschange, NULL); tox_callback_friend_status_message(m, on_statusmessagechange, NULL); tox_callback_friend_read_receipt(m, on_read_receipt, NULL); tox_callback_group_invite(m, on_groupinvite, NULL); tox_callback_group_message(m, on_groupmessage, NULL); tox_callback_group_action(m, on_groupaction, NULL); tox_callback_group_namelist_change(m, on_group_namelistchange, NULL); tox_callback_group_title(m, on_group_titlechange, NULL); tox_callback_file_recv(m, on_file_recv, NULL); tox_callback_file_chunk_request(m, on_file_chunk_request, NULL); tox_callback_file_recv_control(m, on_file_control, NULL); tox_callback_file_recv_chunk(m, on_file_recv_chunk, NULL); }
static int r2tox_connect() { if (tox) { printf ("Status: Online\n"); print_tox_my_address (tox); return -1; } Tox *t = NULL; struct Tox_Options *options = tox_options_new(NULL); FILE *fd = fopen("tox.data", "rb"); if (fd) { eprintf ("Using tox.data\n"); size_t sz = fread (&data, 1, 4096, fd); fclose (fd); tox_options_set_savedata_length (options, sz); tox_options_set_savedata_type (options, TOX_SAVEDATA_TYPE_TOX_SAVE); tox_options_set_savedata_data (options, data, sz); t = tox_new (options, NULL); if (!t) { printf("cannot new\n"); return 1; } } else { t = tox_new (NULL, NULL); if (!t) { eprintf ("cannot new\n"); return 1; } // r2tox_save(); } const char *username = "******"; const char *status = "Available"; tox_self_set_name (t, username, strlen(username), NULL); tox_self_set_status_message (t, status, strlen(status), NULL); tox_callback_friend_name(t, handle_friend_name); tox_callback_friend_request (t, handle_friend_request); tox_callback_friend_message (t, handle_friend_message); tox_callback_friend_lossy_packet (t, handle_friend_lossy_packet); tox_callback_friend_lossless_packet (t, handle_friend_lossless_packet); tox_callback_friend_read_receipt (t, handle_friend_read_receipt); tox_callback_conference_invite(t, handle_conference_invite); tox_callback_conference_message(t, handle_conference_message); tox_callback_conference_title(t, handle_conference_title); // bootstrap size_t i; for (i = 0; i < sizeof(nodes)/sizeof(DHT_node); i ++) { sodium_hex2bin(nodes[i].key_bin, sizeof(nodes[i].key_bin), nodes[i].key_hex, sizeof(nodes[i].key_hex)-1, NULL, NULL, NULL); tox_bootstrap(t, nodes[i].ip, nodes[i].port, nodes[i].key_bin, NULL); } print_tox_my_address (t); tox_callback_self_connection_status (t, self_connection_status_cb); tox = t; // thread here if (!thread) { thread = r_th_new (r2tox_mainloop, NULL, 1); r_th_start (thread, true); } return 0; }
int main(void) { setvbuf(stdout, nullptr, _IONBF, 0); State state1 = {1}; State state2 = {2}; State state3 = {3}; // Create toxes. Tox *tox1 = tox_new_log(nullptr, nullptr, &state1.id); Tox *tox2 = tox_new_log(nullptr, nullptr, &state2.id); Tox *tox3 = tox_new_log(nullptr, nullptr, &state3.id); // tox1 <-> tox2, tox2 <-> tox3 uint8_t key[TOX_PUBLIC_KEY_SIZE]; tox_self_get_public_key(tox2, key); tox_friend_add_norequest(tox1, key, nullptr); // tox1 -> tox2 tox_self_get_public_key(tox1, key); tox_friend_add_norequest(tox2, key, nullptr); // tox2 -> tox1 tox_self_get_public_key(tox3, key); tox_friend_add_norequest(tox2, key, nullptr); // tox2 -> tox3 tox_self_get_public_key(tox2, key); tox_friend_add_norequest(tox3, key, nullptr); // tox3 -> tox2 printf("bootstrapping tox2 and tox3 off tox1\n"); uint8_t dht_key[TOX_PUBLIC_KEY_SIZE]; tox_self_get_dht_id(tox1, dht_key); const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr); tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr); tox_bootstrap(tox3, "localhost", dht_port, dht_key, nullptr); // Connection callbacks. tox_callback_self_connection_status(tox1, handle_self_connection_status); tox_callback_self_connection_status(tox2, handle_self_connection_status); tox_callback_self_connection_status(tox3, handle_self_connection_status); tox_callback_friend_connection_status(tox1, handle_friend_connection_status); tox_callback_friend_connection_status(tox2, handle_friend_connection_status); tox_callback_friend_connection_status(tox3, handle_friend_connection_status); // Conference callbacks. tox_callback_conference_invite(tox1, handle_conference_invite); tox_callback_conference_invite(tox2, handle_conference_invite); tox_callback_conference_invite(tox3, handle_conference_invite); tox_callback_conference_connected(tox1, handle_conference_connected); tox_callback_conference_connected(tox2, handle_conference_connected); tox_callback_conference_connected(tox3, handle_conference_connected); tox_callback_conference_message(tox1, handle_conference_message); tox_callback_conference_message(tox2, handle_conference_message); tox_callback_conference_message(tox3, handle_conference_message); tox_callback_conference_peer_list_changed(tox1, handle_conference_peer_list_changed); tox_callback_conference_peer_list_changed(tox2, handle_conference_peer_list_changed); tox_callback_conference_peer_list_changed(tox3, handle_conference_peer_list_changed); // Wait for self connection. fprintf(stderr, "Waiting for toxes to come online\n"); do { tox_iterate(tox1, &state1); tox_iterate(tox2, &state2); tox_iterate(tox3, &state3); c_sleep(100); } while (!state1.self_online || !state2.self_online || !state3.self_online); fprintf(stderr, "Toxes are online\n"); // Wait for friend connection. fprintf(stderr, "Waiting for friends to connect\n"); do { tox_iterate(tox1, &state1); tox_iterate(tox2, &state2); tox_iterate(tox3, &state3); c_sleep(100); } while (!state1.friend_online || !state2.friend_online || !state3.friend_online); fprintf(stderr, "Friends are connected\n"); { // Create new conference, tox1 is the founder. Tox_Err_Conference_New err; state1.conference = tox_conference_new(tox1, &err); state1.joined = true; ck_assert_msg(err == TOX_ERR_CONFERENCE_NEW_OK, "failed to create a conference: err = %d", err); fprintf(stderr, "Created conference: id = %u\n", state1.conference); } { // Invite friend. Tox_Err_Conference_Invite err; tox_conference_invite(tox1, 0, state1.conference, &err); ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK, "failed to invite a friend: err = %d", err); state1.invited_next = true; fprintf(stderr, "tox1 invited tox2\n"); } fprintf(stderr, "Waiting for invitation to arrive\n"); do { tox_iterate(tox1, &state1); tox_iterate(tox2, &state2); tox_iterate(tox3, &state3); c_sleep(100); } while (!state1.joined || !state2.joined || !state3.joined); fprintf(stderr, "Invitations accepted\n"); fprintf(stderr, "Waiting for peers to come online\n"); do { tox_iterate(tox1, &state1); tox_iterate(tox2, &state2); tox_iterate(tox3, &state3); c_sleep(100); } while (state1.peers == 0 || state2.peers == 0 || state3.peers == 0); fprintf(stderr, "All peers are online\n"); { fprintf(stderr, "tox1 sends a message to the group: \"hello!\"\n"); Tox_Err_Conference_Send_Message err; tox_conference_send_message(tox1, state1.conference, TOX_MESSAGE_TYPE_NORMAL, (const uint8_t *)"hello!", 7, &err); if (err != TOX_ERR_CONFERENCE_SEND_MESSAGE_OK) { fprintf(stderr, "ERROR: %d\n", err); exit(EXIT_FAILURE); } } fprintf(stderr, "Waiting for messages to arrive\n"); do { tox_iterate(tox1, &state1); tox_iterate(tox2, &state2); tox_iterate(tox3, &state3); c_sleep(100); } while (!state2.received || !state3.received); fprintf(stderr, "Messages received. Test complete.\n"); tox_kill(tox3); tox_kill(tox2); tox_kill(tox1); return 0; }
int main(int argc, char *argv[]) { unsigned char tox_id[TOX_ADDRESS_SIZE]; unsigned char tox_printable_id[TOX_ADDRESS_SIZE * 2 + 1]; TOX_ERR_NEW tox_new_err; int oc; size_t save_size = 0; uint8_t *save_data = NULL; allowed_toxid *allowed_toxid_obj = NULL; log_init(); while ((oc = getopt(argc, argv, "L:pi:C:s:P:dqhSF:DU:")) != -1) { switch(oc) { case 'L': /* Local port forwarding */ client_mode = 1; client_local_port_mode = 1; if(parse_local_port_forward(optarg, &local_port, &remote_host, &remote_port) < 0) { log_printf(L_ERROR, "Invalid value for -L option - use something like -L 22:127.0.0.1:22\n"); exit(1); } if(min_log_level == L_UNSET) { min_log_level = L_INFO; } log_printf(L_DEBUG, "Forwarding remote port %d to local port %d\n", remote_port, local_port); break; case 'P': /* Pipe forwarding */ client_mode = 1; client_pipe_mode = 1; if(parse_pipe_port_forward(optarg, &remote_host, &remote_port) < 0) { log_printf(L_ERROR, "Invalid value for -P option - use something like -P 127.0.0.1:22\n"); exit(1); } if(min_log_level == L_UNSET) { min_log_level = L_ERROR; } log_printf(L_INFO, "Forwarding remote port %d to stdin/out\n", remote_port); break; case 'p': /* Ping */ client_mode = 1; ping_mode = 1; if(min_log_level == L_UNSET) { min_log_level = L_INFO; } break; case 'i': /* Tox ID */ server_whitelist_mode = 1; log_printf(L_DEBUG, "Server whitelist mode enabled"); allowed_toxid_obj = (allowed_toxid *)calloc(sizeof(allowed_toxid), 1); if(!allowed_toxid_obj) { log_printf(L_ERROR, "Could not allocate memory for allowed_toxid"); exit(1); } remote_tox_id = optarg; if(!string_to_id(allowed_toxid_obj->toxid, optarg)) { log_printf(L_ERROR, "Invalid Tox ID"); exit(1); } LL_APPEND(allowed_toxids, allowed_toxid_obj); break; case 'C': /* Config directory */ strncpy(config_path, optarg, sizeof(config_path) - 1); if(optarg[strlen(optarg) - 1] != '/') { int optarg_len = strlen(optarg); config_path[optarg_len] = '/'; config_path[optarg_len + 1] = '\0'; } load_saved_toxid_in_client_mode = 1; break; case 's': /* Shared secret */ use_shared_secret = 1; memset(shared_secret, 0, TOX_MAX_FRIEND_REQUEST_LENGTH); strncpy(shared_secret, optarg, TOX_MAX_FRIEND_REQUEST_LENGTH-1); break; case 'd': min_log_level = L_DEBUG; break; case 'q': min_log_level = L_ERROR; break; case 'S': use_syslog = 1; break; case 'D': daemonize = 1; use_syslog = 1; break; case 'F': pidfile = optarg; break; case 'U': daemon_username = optarg; break; case '?': case 'h': default: print_version(); help(); exit(1); } } if(!client_mode && min_log_level == L_UNSET) { min_log_level = L_INFO; } if(!client_mode && server_whitelist_mode) { log_printf(L_INFO, "Server in ToxID whitelisting mode - only clients listed with -i can connect"); } if(daemonize) { do_daemonize(); } atexit(cleanup); print_version(); /* Bootstrap tox */ tox_options_default(&tox_options); if((!client_mode) || load_saved_toxid_in_client_mode) { uint8_t *save_data = NULL; save_size = load_save(&save_data); if(save_data && save_size) { tox_options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE; tox_options.savedata_data = save_data; tox_options.savedata_length = save_size; } } tox = tox_new(&tox_options, &tox_new_err); if(tox == NULL) { log_printf(L_DEBUG, "tox_new() failed (%u) - trying without proxy\n", tox_new_err); if((tox_options.proxy_type != TOX_PROXY_TYPE_NONE) || (tox_options.proxy_type = TOX_PROXY_TYPE_NONE, (tox = tox_new(&tox_options, &tox_new_err)) == NULL)) { log_printf(L_DEBUG, "tox_new() failed (%u) - trying without IPv6\n", tox_new_err); if(!tox_options.ipv6_enabled || (tox_options.ipv6_enabled = 0, (tox = tox_new(&tox_options, &tox_new_err)) == NULL)) { log_printf(L_DEBUG, "tox_new() failed (%u) - trying with Tor\n", tox_new_err); if((tox_options.proxy_type = TOX_PROXY_TYPE_SOCKS5, tox_options.proxy_host="127.0.0.1", tox_options.proxy_port=9050, (tox = tox_new(&tox_options, &tox_new_err)) == NULL)) { log_printf(L_ERROR, "tox_new() failed (%u) - exiting\n", tox_new_err); exit(1); } } } } if(save_size && save_data) { free(save_data); } set_tox_username(tox); tox_callback_self_connection_status(tox, handle_connection_status_change, NULL); do_bootstrap(tox); if(client_mode) { tox_self_get_address(tox, tox_id); id_to_string(tox_printable_id, tox_id); tox_printable_id[TOX_ADDRESS_SIZE * 2] = '\0'; log_printf(L_DEBUG, "Generated Tox ID: %s\n", tox_printable_id); if(!remote_tox_id) { log_printf(L_ERROR, "Tox id is required in client mode. Use -i 58435984ABCDEF475...\n"); exit(1); } do_client_loop(remote_tox_id); } else { write_save(tox); if(!use_shared_secret) { log_printf(L_WARNING, "Shared secret authentication is not used - skilled attackers may connect to your tuntox server"); } tox_self_get_address(tox, tox_id); memset(tox_printable_id, '\0', sizeof(tox_printable_id)); id_to_string(tox_printable_id, tox_id); tox_printable_id[TOX_ADDRESS_SIZE * 2] = '\0'; log_printf(L_INFO, "Using Tox ID: %s\n", tox_printable_id); tox_callback_friend_request(tox, accept_friend_request, NULL); do_server_loop(); } return 0; }