void exit_toxic_success(Tox *m) { store_data(m, DATA_FILE); memset(&user_password, 0, sizeof(struct user_password)); kill_all_file_transfers(m); kill_all_windows(m); terminate_notify(); #ifdef AUDIO #ifdef VIDEO terminate_video(); #endif /* VIDEO */ terminate_audio(); #endif /* AUDIO */ free_global_data(); tox_kill(m); endwin(); curl_global_cleanup(); #ifdef X11 /* We have to terminate xtra last coz reasons * Please don't call this anywhere else coz trust me */ terminate_xtra(); #endif /* X11 */ exit(EXIT_SUCCESS); }
static void r2tox_disconnect() { r_th_kill (thread, true); //r_th_free (thread); thread = NULL; tox_kill (tox); tox = NULL; }
void Tox_Dispatcher::start() { if (run.load()) { LOG(INFO) << "Tox dispatcher is already running"; } LOG(INFO) << "Starting Tox dispatcher"; try { init_tox(); init_callbacks(); bootstrap(); } catch (const std::exception& e) { LOG(ERROR) << e.what(); exit(-1); } dispatcher_started.Emit(this); LOG(INFO) << "Tox Id: " << get_self_id(); /* run tox main loop */ run.store(true); while (run.load()) { lock.lock(); tox_iterate(tox); uint32_t millis = tox_iteration_interval(tox); lock.unlock(); std::this_thread::sleep_for(std::chrono::milliseconds(millis)); } tox_kill(tox); tox = nullptr; LOG(INFO) << "Tox dispatcher stopped"; }
Core::~Core() { if (tox) { saveConfiguration(); tox_kill(tox); } }
static void select_friend(Tox *m, wint_t key) { if (num_friends < 1) return; int n = num_selected; if (key == KEY_UP) { while (--n != num_selected) { if (n < 0) n = num_friends - 1; if (friends[n].active) { num_selected = n; return; } } } else if (key == KEY_DOWN) { while (++n != num_selected) { n = n % num_friends; if (friends[n].active) { num_selected = n; return; } } } else return; /* Bad key input */ /* If we reach this something is wrong */ endwin(); tox_kill(m); fprintf(stderr, "select_friend() failed. Aborting...\n"); exit(EXIT_FAILURE); }
void CToxProto::UninitToxCore() { if (tox) { for (size_t i = 0; i < transfers.Count(); i++) { FileTransferParam *transfer = transfers.GetAt(i); tox_file_control(tox, transfer->friendNumber, transfer->fileNumber, TOX_FILE_CONTROL_CANCEL, NULL); ProtoBroadcastAck(transfer->pfts.hContact, ACKTYPE_FILE, ACKRESULT_DENIED, (HANDLE)transfer, 0); transfers.Remove(transfer); } //if (IsToxCoreInited()) //{ // ptrA nickname(mir_utf8encodeW(ptrT(getTStringA("Nick")))); // tox_set_name(tox, (uint8_t*)(char*)nickname, mir_strlen(nickname)); // //temporary // ptrA statusmes(mir_utf8encodeW(ptrT(getTStringA("StatusMsg")))); // tox_set_status_message(tox, (uint8_t*)(char*)statusmes, mir_strlen(statusmes)); //} if (toxAv) toxav_kill(toxAv); SaveToxProfile(); if (password != NULL) { mir_free(password); password = NULL; } tox_kill(tox); tox = NULL; } }
void exit_toxic(Tox *m) { store_data(m, DATA_FILE); free(DATA_FILE); free(SRVLIST_FILE); free(prompt->stb); tox_kill(m); endwin(); exit(EXIT_SUCCESS); }
void cleanup() { log_printf(L_DEBUG, "kthxbye\n"); fflush(stdout); tox_kill(tox); if(client_socket) { close(client_socket); } log_close(); }
static void exit_toxbot(Tox *m) { uint32_t numchats = tox_count_chatlist(m); if (numchats) exit_groupchats(m, numchats); save_data(m, DATA_FILE); tox_kill(m); exit(EXIT_SUCCESS); }
void Core::deadifyTox() { if (av) { delete av; av = nullptr; } if (tox) { tox_kill(tox); tox = nullptr; } }
void Core::deadifyTox() { if (toxav) { toxav_kill(toxav); toxav = nullptr; } if (tox) { tox_kill(tox); tox = nullptr; } }
int main(void) { uint32_t conference_number; struct Tox_Options to; Tox *t; TOX_ERR_CONFERENCE_NEW conference_err; TOX_ERR_CONFERENCE_TITLE title_err; tox_options_default(&to); t = tox_new(&to, NULL); tox_callback_conference_title(t, &cbtitlechange); if ((conference_number = tox_conference_new(t, &conference_err)) == UINT32_MAX) { tox_kill(t); fprintf(stderr, "error: could not create new conference, error code %d\n", conference_err); return 2; } tox_iterate(t, NULL); c_sleep(tox_iteration_interval(t)); if (!tox_conference_set_title(t, conference_number, (const uint8_t *)newtitle, strlen(newtitle), &title_err)) { tox_kill(t); fprintf(stderr, "error: could not set conference title, error code %d\n", title_err); return 3; } tox_iterate(t, NULL); c_sleep(tox_iteration_interval(t)); tox_iterate(t, NULL); fprintf(stderr, "error: title was not changed in callback. exiting.\n"); tox_kill(t); return 1; }
void exit_toxic(Tox *m) { store_data(m, DATA_FILE); close_all_file_senders(); kill_all_windows(); log_disable(prompt->promptbuf->log); free(DATA_FILE); free(prompt->stb); free(prompt->promptbuf->log); free(prompt->promptbuf); tox_kill(m); #ifdef _SUPPORT_AUDIO terminate_audio(prompt, av); #endif /* _SUPPORT_AUDIO */ endwin(); exit(EXIT_SUCCESS); }
int av_terminate_session(av_session_t *_phone) { toxav_hangup(_phone->av); free(_phone->_friends); pthread_mutex_destroy ( &_phone->_mutex ); Tox *_p = _phone->_messenger; _phone->_messenger = NULL; usleep(100000); /* Wait for tox_poll to end */ tox_kill(_p); toxav_kill(_phone->av); free(_phone); printf("\r[i] Quit!\n"); return 0; }
void exit_toxic(Tox *m) { store_data(m, DATA_FILE); close_all_file_senders(); kill_all_windows(); log_disable(prompt->chatwin->log); line_info_cleanup(prompt->chatwin->hst); free(DATA_FILE); free(prompt->stb); free(prompt->chatwin->log); free(prompt->chatwin->hst); free(prompt->chatwin); tox_kill(m); #ifdef _SUPPORT_AUDIO terminate_audio(); #endif /* _SUPPORT_AUDIO */ endwin(); exit(EXIT_SUCCESS); }
void exit_toxic_success(Tox *m) { store_data(m, DATA_FILE); close_all_file_senders(m); kill_all_windows(); free(DATA_FILE); free(user_settings_); #ifdef _SOUND_NOTIFY notify(NULL, self_log_out, NT_ALWAYS); #endif /* _SOUND_NOTIFY */ terminate_notify(); #ifdef _AUDIO terminate_audio(); #endif /* _AUDIO */ tox_kill(m); endwin(); exit(EXIT_SUCCESS); }
int upgrade(const char *file, const char *output) { FILE *f = fopen(file, "r"); if (!f) { perror("mio/upgrade"); return -1; } fseek(f, 0, SEEK_END); size_t bufsize = ftell(f); fseek(f, 0, 0); uint8_t *buf = malloc(bufsize); fread(buf, bufsize, 1, f); fclose(f); Tox *temptox = tox_new(1); tox_load(temptox, buf, (uint32_t)bufsize); free(buf); puts("mio/upgrade: letting Tox run for a bit"); for (int n = 0; n < 20; ++n) { tox_do(temptox); printf("."); fflush(stdout); usleep(50000); } puts(" done"); txd_intermediate_t txd = txd_intermediate_from_tox(temptox); tox_kill(temptox); uint8_t *clear; uint64_t clearlen; int eerr = txd_export_to_buf(txd, &clear, &clearlen); if (eerr != TXD_ERR_SUCCESS) { printf("mio/upgrade: error: txd_export_to_buf failed with code %d\n", eerr); return -1; } txd_intermediate_free(txd); char *template = strdup(".si-XXXXXXXX");
int av_terminate_session(av_session_t *_phone) { _phone->running_decaud = 0; usleep(100000); /* Wait for tox_poll to end */ toxav_kill(_phone->av); printf("\r[i] KILLED AV ARGH!\n"); usleep(1000000); /* Wait for cancel request to be sent */ Tox *_p = _phone->_messenger; _phone->_messenger = NULL; tox_kill(_p); free(_phone->_friends); pthread_mutex_destroy ( &_phone->_mutex ); free(_phone); printf("\r[i] Quit!\n"); return 0; }
int main(void) { setvbuf(stdout, nullptr, _IONBF, 0); Tox *tox_udp = tox_new_log(nullptr, nullptr, nullptr); tox_bootstrap(tox_udp, "node.tox.biribiri.org", 33445, key, nullptr); printf("Waiting for connection"); while (tox_self_get_connection_status(tox_udp) == TOX_CONNECTION_NONE) { printf("."); fflush(stdout); tox_iterate(tox_udp, nullptr); c_sleep(ITERATION_INTERVAL); } printf("Connection (UDP): %d\n", tox_self_get_connection_status(tox_udp)); tox_kill(tox_udp); return 0; }
Core::~Core() { if (tox) { saveConfiguration(); toxav_kill(toxav); tox_kill(tox); } if (videobuf) { delete[] videobuf; videobuf=nullptr; } if (alContext) { alcMakeContextCurrent(nullptr); alcDestroyContext(alContext); } if (alOutDev) alcCloseDevice(alOutDev); if (alInDev) alcCaptureCloseDevice(alInDev); }
END_TEST #define NUM_TCP_RELAYS 3 START_TEST(test_many_clients_tcp_b) { long long unsigned int cur_time = time(nullptr); Tox *toxes[NUM_TOXES_TCP]; uint32_t index[NUM_TOXES_TCP]; uint32_t i, j; uint32_t to_comp = 974536; for (i = 0; i < NUM_TOXES_TCP; ++i) { struct Tox_Options *opts = tox_options_new(nullptr); if (i < NUM_TCP_RELAYS) { tox_options_set_tcp_port(opts, TCP_RELAY_PORT + i); } else { tox_options_set_udp_enabled(opts, 0); } index[i] = i + 1; toxes[i] = tox_new_log(opts, nullptr, &index[i]); ck_assert_msg(toxes[i] != nullptr, "Failed to create tox instances %u", i); tox_callback_friend_request(toxes[i], accept_friend_request); uint8_t dpk[TOX_PUBLIC_KEY_SIZE]; tox_self_get_dht_id(toxes[(i % NUM_TCP_RELAYS)], dpk); ck_assert_msg(tox_add_tcp_relay(toxes[i], TOX_LOCALHOST, TCP_RELAY_PORT + (i % NUM_TCP_RELAYS), dpk, nullptr), "add relay error"); tox_self_get_dht_id(toxes[0], dpk); uint16_t first_port = tox_self_get_udp_port(toxes[0], nullptr); ck_assert_msg(tox_bootstrap(toxes[i], TOX_LOCALHOST, first_port, dpk, nullptr), "Bootstrap error"); tox_options_free(opts); } struct { uint16_t tox1; uint16_t tox2; } pairs[NUM_FRIENDS]; uint8_t address[TOX_ADDRESS_SIZE]; for (i = 0; i < NUM_FRIENDS; ++i) { loop_top: pairs[i].tox1 = random_u32() % NUM_TOXES_TCP; pairs[i].tox2 = (pairs[i].tox1 + random_u32() % (NUM_TOXES_TCP - 1) + 1) % NUM_TOXES_TCP; for (j = 0; j < i; ++j) { if (pairs[j].tox2 == pairs[i].tox1 && pairs[j].tox1 == pairs[i].tox2) { goto loop_top; } } tox_self_get_address(toxes[pairs[i].tox1], address); TOX_ERR_FRIEND_ADD test; uint32_t num = tox_friend_add(toxes[pairs[i].tox2], address, (const uint8_t *)"Gentoo", 7, &test); if (test == TOX_ERR_FRIEND_ADD_ALREADY_SENT) { goto loop_top; } ck_assert_msg(num != UINT32_MAX && test == TOX_ERR_FRIEND_ADD_OK, "Failed to add friend error code: %i", test); } uint16_t last_count = 0; while (1) { uint16_t counter = 0; for (i = 0; i < NUM_TOXES_TCP; ++i) { for (j = 0; j < tox_self_get_friend_list_size(toxes[i]); ++j) { if (tox_friend_get_connection_status(toxes[i], j, nullptr) == TOX_CONNECTION_TCP) { ++counter; } } } if (counter != last_count) { printf("many_clients_tcp_b got to %u\n", counter); last_count = counter; } if (counter == NUM_FRIENDS * 2) { break; } for (i = 0; i < NUM_TOXES_TCP; ++i) { tox_iterate(toxes[i], &to_comp); } c_sleep(30); } for (i = 0; i < NUM_TOXES_TCP; ++i) { tox_kill(toxes[i]); } printf("test_many_clients_tcp_b succeeded, took %llu seconds\n", time(nullptr) - cur_time); }
static void test_set_status_message(void) { printf("initialising 2 toxes\n"); uint32_t index[] = { 1, 2 }; const time_t cur_time = time(nullptr); Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]); Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]); ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances"); printf("tox1 adds tox2 as friend, tox2 adds tox1\n"); uint8_t public_key[TOX_PUBLIC_KEY_SIZE]; tox_self_get_public_key(tox2, public_key); tox_friend_add_norequest(tox1, public_key, nullptr); tox_self_get_public_key(tox1, public_key); tox_friend_add_norequest(tox2, public_key, nullptr); printf("bootstrapping tox2 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); while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE || tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE) { tox_iterate(tox1, nullptr); tox_iterate(tox2, nullptr); c_sleep(ITERATION_INTERVAL); } printf("toxes are online, took %ld seconds\n", time(nullptr) - cur_time); const time_t con_time = time(nullptr); while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP || tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP) { tox_iterate(tox1, nullptr); tox_iterate(tox2, nullptr); c_sleep(ITERATION_INTERVAL); } printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time); TOX_ERR_SET_INFO err_n; tox_callback_friend_status_message(tox2, status_callback); bool ret = tox_self_set_status_message(tox1, (const uint8_t *)STATUS_MESSAGE, sizeof(STATUS_MESSAGE), &err_n); ck_assert_msg(ret && err_n == TOX_ERR_SET_INFO_OK, "tox_self_set_status_message failed because %u\n", err_n); bool status_updated = false; while (!status_updated) { tox_iterate(tox1, nullptr); tox_iterate(tox2, &status_updated); c_sleep(ITERATION_INTERVAL); } ck_assert_msg(tox_friend_get_status_message_size(tox2, 0, nullptr) == sizeof(STATUS_MESSAGE), "status message length not correct"); uint8_t cmp_status[sizeof(STATUS_MESSAGE)]; tox_friend_get_status_message(tox2, 0, cmp_status, nullptr); ck_assert_msg(memcmp(cmp_status, STATUS_MESSAGE, sizeof(STATUS_MESSAGE)) == 0, "status message not correct"); printf("test_set_status_message succeeded, took %ld seconds\n", time(nullptr) - cur_time); tox_kill(tox1); tox_kill(tox2); }
int main(void) { setvbuf(stdout, nullptr, _IONBF, 0); Tox *const tox1 = tox_new_log(nullptr, nullptr, nullptr); Tox *const tox2 = tox_new_log(nullptr, nullptr, nullptr); printf("bootstrapping tox2 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); struct test_data to_compare = {{0}}; uint8_t public_key[TOX_PUBLIC_KEY_SIZE]; tox_self_get_public_key(tox1, public_key); tox_friend_add_norequest(tox2, public_key, nullptr); tox_self_get_public_key(tox2, public_key); tox_friend_add_norequest(tox1, public_key, nullptr); uint8_t reference_name[TOX_MAX_NAME_LENGTH] = { 0 }; uint8_t reference_status[TOX_MAX_STATUS_MESSAGE_LENGTH] = { 0 }; set_random(tox1, tox_self_set_name, TOX_MAX_NAME_LENGTH); set_random(tox2, tox_self_set_name, TOX_MAX_NAME_LENGTH); set_random(tox1, tox_self_set_status_message, TOX_MAX_STATUS_MESSAGE_LENGTH); set_random(tox2, tox_self_set_status_message, TOX_MAX_STATUS_MESSAGE_LENGTH); tox_self_get_name(tox2, reference_name); tox_self_get_status_message(tox2, reference_status); tox_callback_friend_name(tox1, namechange_callback); tox_callback_friend_status_message(tox1, statuschange_callback); while (true) { if (tox_self_get_connection_status(tox1) && tox_self_get_connection_status(tox2) && tox_friend_get_connection_status(tox1, 0, nullptr) == TOX_CONNECTION_UDP) { printf("Connected.\n"); break; } tox_iterate(tox1, &to_compare); tox_iterate(tox2, nullptr); c_sleep(tox_iteration_interval(tox1)); } while (true) { if (to_compare.received_name && to_compare.received_status_message) { printf("Exchanged names and status messages.\n"); break; } tox_iterate(tox1, &to_compare); tox_iterate(tox2, nullptr); c_sleep(tox_iteration_interval(tox1)); } size_t save_size = tox_get_savedata_size(tox1); VLA(uint8_t, savedata, save_size); tox_get_savedata(tox1, savedata); struct Tox_Options *const options = tox_options_new(nullptr); tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); tox_options_set_savedata_data(options, savedata, save_size); Tox *const tox_to_compare = tox_new_log(options, nullptr, nullptr); tox_friend_get_name(tox_to_compare, 0, to_compare.name, nullptr); tox_friend_get_status_message(tox_to_compare, 0, to_compare.status_message, nullptr); ck_assert_msg(memcmp(reference_name, to_compare.name, TOX_MAX_NAME_LENGTH) == 0, "incorrect name: should be all zeroes"); ck_assert_msg(memcmp(reference_status, to_compare.status_message, TOX_MAX_STATUS_MESSAGE_LENGTH) == 0, "incorrect status message: should be all zeroes"); tox_options_free(options); tox_kill(tox1); tox_kill(tox2); tox_kill(tox_to_compare); 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; }
Core::~Core() { if (tox) { tox_kill(tox); } }
END_TEST #define NUM_TOXES 66 #define NUM_FRIENDS 20 START_TEST(test_many_clients) { long long unsigned int cur_time = time(NULL); Tox *toxes[NUM_TOXES]; uint32_t i, j; uint32_t to_comp = 974536; for (i = 0; i < NUM_TOXES; ++i) { toxes[i] = tox_new(0); ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); tox_callback_friend_request(toxes[i], accept_friend_request, &to_comp); } struct { uint16_t tox1; uint16_t tox2; } pairs[NUM_FRIENDS]; uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; for (i = 0; i < NUM_FRIENDS; ++i) { loop_top: pairs[i].tox1 = rand() % NUM_TOXES; pairs[i].tox2 = (pairs[i].tox1 + rand() % (NUM_TOXES - 1) + 1) % NUM_TOXES; for (j = 0; j < i; ++j) { if (pairs[j].tox2 == pairs[i].tox1 && pairs[j].tox1 == pairs[i].tox2) goto loop_top; } tox_get_address(toxes[pairs[i].tox1], address); int test = tox_add_friend(toxes[pairs[i].tox2], address, (uint8_t *)"Gentoo", 7); if (test == TOX_FAERR_ALREADYSENT) { goto loop_top; } ck_assert_msg(test >= 0, "Failed to add friend error code: %i", test); } while (1) { uint16_t counter = 0; for (i = 0; i < NUM_TOXES; ++i) { for (j = 0; j < tox_count_friendlist(toxes[i]); ++j) if (tox_get_friend_connection_status(toxes[i], j) == 1) ++counter; } if (counter == NUM_FRIENDS * 2) { break; } for (i = 0; i < NUM_TOXES; ++i) { tox_do(toxes[i]); } c_sleep(50); } printf("test_many_clients succeeded, took %llu seconds\n", time(NULL) - cur_time); for (i = 0; i < NUM_TOXES; ++i) { tox_kill(toxes[i]); } }
static void test_one(void) { uint8_t name[TOX_MAX_NAME_LENGTH]; uint8_t status_message[TOX_MAX_STATUS_MESSAGE_LENGTH]; uint8_t name2[TOX_MAX_NAME_LENGTH]; uint8_t status_message2[TOX_MAX_STATUS_MESSAGE_LENGTH]; uint32_t index[] = { 1, 2 }; Tox *tox1 = tox_new_log(nullptr, nullptr, &index[0]); set_random_name_and_status_message(tox1, name, status_message); Tox *tox2 = tox_new_log(nullptr, nullptr, &index[1]); set_random_name_and_status_message(tox2, name2, status_message2); uint8_t address[TOX_ADDRESS_SIZE]; tox_self_get_address(tox1, address); Tox_Err_Friend_Add error; uint32_t ret = tox_friend_add(tox1, address, (const uint8_t *)"m", 1, &error); ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_OWN_KEY, "Adding own address worked."); tox_self_get_address(tox2, address); uint8_t message[TOX_MAX_FRIEND_REQUEST_LENGTH + 1]; ret = tox_friend_add(tox1, address, nullptr, 0, &error); ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_NULL, "Sending request with no message worked."); ret = tox_friend_add(tox1, address, message, 0, &error); ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_NO_MESSAGE, "Sending request with no message worked."); ret = tox_friend_add(tox1, address, message, sizeof(message), &error); ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_TOO_LONG, "TOX_MAX_FRIEND_REQUEST_LENGTH is too big."); address[0]++; ret = tox_friend_add(tox1, address, (const uint8_t *)"m", 1, &error); ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_BAD_CHECKSUM, "Adding address with bad checksum worked."); tox_self_get_address(tox2, address); ret = tox_friend_add(tox1, address, message, TOX_MAX_FRIEND_REQUEST_LENGTH, &error); ck_assert_msg(ret == 0 && error == TOX_ERR_FRIEND_ADD_OK, "Failed to add friend."); ret = tox_friend_add(tox1, address, message, TOX_MAX_FRIEND_REQUEST_LENGTH, &error); ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_ALREADY_SENT, "Adding friend twice worked."); tox_self_set_name(tox1, name, sizeof(name), nullptr); ck_assert_msg(tox_self_get_name_size(tox1) == sizeof(name), "Can't set name of TOX_MAX_NAME_LENGTH"); tox_self_set_status_message(tox1, status_message, sizeof(status_message), nullptr); ck_assert_msg(tox_self_get_status_message_size(tox1) == sizeof(status_message), "Can't set status message of TOX_MAX_STATUS_MESSAGE_LENGTH"); tox_self_get_address(tox1, address); size_t save_size = tox_get_savedata_size(tox1); VLA(uint8_t, data, save_size); tox_get_savedata(tox1, data); tox_kill(tox2); Tox_Err_New err_n; struct Tox_Options *options = tox_options_new(nullptr); tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); tox_options_set_savedata_data(options, data, save_size); tox2 = tox_new_log(options, &err_n, &index[1]); ck_assert_msg(err_n == TOX_ERR_NEW_OK, "Load failed"); ck_assert_msg(tox_self_get_name_size(tox2) == sizeof name, "Wrong name size."); ck_assert_msg(tox_self_get_status_message_size(tox2) == sizeof status_message, "Wrong status message size"); uint8_t name_loaded[TOX_MAX_NAME_LENGTH] = { 0 }; tox_self_get_name(tox2, name_loaded); ck_assert_msg(!memcmp(name, name_loaded, sizeof name), "Wrong name."); uint8_t status_message_loaded[TOX_MAX_STATUS_MESSAGE_LENGTH] = { 0 }; tox_self_get_status_message(tox2, status_message_loaded); ck_assert_msg(!memcmp(status_message, status_message_loaded, sizeof status_message_loaded), "Wrong status message."); uint8_t address2[TOX_ADDRESS_SIZE] = { 0 }; tox_self_get_address(tox2, address2); ck_assert_msg(memcmp(address2, address, TOX_ADDRESS_SIZE) == 0, "Wrong address."); uint8_t new_name[TOX_MAX_NAME_LENGTH] = { 0 }; tox_self_get_name(tox2, new_name); ck_assert_msg(memcmp(name, new_name, TOX_MAX_NAME_LENGTH) == 0, "Wrong name"); uint8_t sk[TOX_SECRET_KEY_SIZE]; tox_self_get_secret_key(tox2, sk); tox_kill(tox2); tox_options_default(options); tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_SECRET_KEY); tox_options_set_savedata_data(options, sk, sizeof(sk)); tox2 = tox_new_log(options, &err_n, &index[1]); ck_assert_msg(err_n == TOX_ERR_NEW_OK, "Load failed"); uint8_t address3[TOX_ADDRESS_SIZE]; tox_self_get_address(tox2, address3); ck_assert_msg(memcmp(address3, address, TOX_PUBLIC_KEY_SIZE) == 0, "Wrong public key."); uint8_t pk[TOX_PUBLIC_KEY_SIZE]; tox_self_get_public_key(tox2, pk); ck_assert_msg(memcmp(pk, address, TOX_PUBLIC_KEY_SIZE) == 0, "Wrong public key."); tox_options_free(options); tox_kill(tox1); tox_kill(tox2); }
void tox_thread(void *UNUSED(args)) { Tox *tox; ToxAv *av; uint8_t id[TOX_FRIEND_ADDRESS_SIZE]; TOP:; debug("new tox object ipv6: %u no_udp: %u proxy: %u %s %u\n", options.ipv6enabled, options.udp_disabled, options.proxy_enabled, options.proxy_address, options.proxy_port); if((tox = tox_new(&options)) == NULL) { debug("trying without proxy\n"); if(!options.proxy_enabled || (options.proxy_enabled = 0, (tox = tox_new(&options)) == NULL)) { debug("trying without ipv6\n"); if(!options.ipv6enabled || (options.ipv6enabled = 0, (tox = tox_new(&options)) == NULL)) { debug("tox_new() failed\n"); exit(1); } dropdown_ipv6.selected = dropdown_ipv6.over = 1; } dropdown_proxy.selected = dropdown_proxy.over = 0; } if(!load_save(tox)) { debug("No save file, using defaults\n"); load_defaults(tox); } edit_setstr(&edit_name, self.name, self.name_length); edit_setstr(&edit_status, self.statusmsg, self.statusmsg_length); tox_get_address(tox, id); id_to_string(self.id, id); debug("Tox ID: %.*s\n", (int)sizeof(self.id), self.id); set_callbacks(tox); do_bootstrap(tox); av = toxav_new(tox, MAX_CALLS); set_av_callbacks(av); global_av = av; tox_thread_init = 1; thread(audio_thread, av); thread(video_thread, av); thread(toxav_thread, av); _Bool connected = 0, reconfig; uint64_t last_save = get_time(), time; while(1) { tox_do(tox); if(tox_isconnected(tox) != connected) { connected = !connected; postmessage(DHT_CONNECTED, connected, 0, NULL); debug("Connected to DHT: %u\n", connected); } time = get_time(); if(time - last_save >= (uint64_t)10 * 1000 * 1000 * 1000) { last_save = time; if(!connected) { do_bootstrap(tox); } write_save(tox); } if(tox_thread_msg) { TOX_MSG *msg = &tox_msg; if(!msg->msg) { reconfig = msg->param1; tox_thread_msg = 0; break; } tox_thread_message(tox, av, time, msg->msg, msg->param1, msg->param2, msg->data); tox_thread_msg = 0; } utox_thread_work_for_transfers(tox, time); utox_thread_work_for_typing_notifications(tox, time); uint32_t interval = tox_do_interval(tox); yieldcpu((interval > 20) ? 20 : interval); } write_save(tox); while(audio_thread_init || video_thread_init || toxav_thread_init) { yieldcpu(1); } debug("av_thread exit, tox thread ending\n"); toxav_kill(av); tox_kill(tox); if(reconfig) { goto TOP; } tox_thread_init = 0; }
static void crawler_kill(Crawler *cwl) { tox_kill(cwl->tox); free(cwl->nodes_list); free(cwl); }
int main(int argc, char *argv[]) { int on = 0; int c = 0; int i = 0; char *filename = "data"; char idstring[200] = {0}; Tox *m; if (argc < 4) { printf("[!] Usage: %s [IP] [port] [public_key] <keyfile>\n", argv[0]); exit(0); } for (i = 0; i < argc; i++) { if (argv[i] == NULL) { break; } else if (argv[i][0] == '-') { if (argv[i][1] == 'h') { print_help(); exit(0); } else if (argv[i][1] == 'f') { if (argv[i + 1] != NULL) filename = argv[i + 1]; else { fputs("[!] you passed '-f' without giving an argument!\n", stderr); } } } } m = tox_new(); if ( !m ) { fputs("Failed to allocate Messenger datastructure", stderr); exit(0); } load_key(m, filename); tox_callback_friendrequest(m, print_request, NULL); tox_callback_friendmessage(m, print_message, NULL); tox_callback_namechange(m, print_nickchange, NULL); tox_callback_statusmessage(m, print_statuschange, NULL); initscr(); noecho(); raw(); getmaxyx(stdscr, y, x); new_lines("/h for list of commands"); get_id(m, idstring); new_lines(idstring); strcpy(line, ""); tox_IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); int resolved_address = resolve_addr(argv[1]); if (resolved_address != 0) bootstrap_ip_port.ip.i = resolved_address; else exit(1); unsigned char *binary_string = hex_string_to_bin(argv[3]); tox_bootstrap(m, bootstrap_ip_port, binary_string); free(binary_string); nodelay(stdscr, TRUE); while (true) { if (on == 0 && tox_isconnected(m)) { new_lines("[i] connected to DHT\n[i] define username with /n"); on = 1; } tox_do(m); c_sleep(1); do_refresh(); c = getch(); if (c == ERR || c == 27) continue; getmaxyx(stdscr, y, x); if (c == '\n') { line_eval(m, line); strcpy(line, ""); } else if (c == 8 || c == 127) { line[strlen(line) - 1] = '\0'; } else if (isalnum(c) || ispunct(c) || c == ' ') { strcpy(line, appender(line, (char) c)); } } tox_kill(m); endwin(); return 0; }