int main(int argc, char *argv[]) { Suite *messenger = messenger_suite(); SRunner *test_runner = srunner_create(messenger); int number_failed = 0; friend_id = hex_string_to_bin(friend_id_str); good_id_a = hex_string_to_bin(good_id_a_str); good_id_b = hex_string_to_bin(good_id_b_str); bad_id = hex_string_to_bin(bad_id_str); /* setup a default friend and friendnum */ if(m_addfriend_norequest((uint8_t *)friend_id) < 0) fputs("m_addfriend_norequest() failed on a valid ID!\n" "this was CRITICAL to the test, and the build WILL fail.\n" "the tests will continue now...\n\n", stderr); if((friend_id_num = getfriend_id((uint8_t *)friend_id)) < 0) fputs("getfriend_id() failed on a valid ID!\n" "this was CRITICAL to the test, and the build WILL fail.\n" "the tests will continue now...\n\n", stderr); srunner_run_all(test_runner, CK_NORMAL); number_failed = srunner_ntests_failed(test_runner); srunner_free(test_runner); free(friend_id); free(good_id_a); free(good_id_b); free(bad_id); return number_failed; }
void add_friend() { int i; char temp_id[128]; for (i = 0; i < 128; i++) temp_id[i] = line[i+3]; int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); if (num >= 0) { char numstring[100]; sprintf(numstring, "\n[i] Friend request sent. Wait to be accepted. Friend id: %d\n\n", num); printf(numstring); ++maxnumfriends; } else if (num == -1) printf("\n[i] Message is too long.\n\n"); else if (num == -2) printf("\n[i] Please add a message to your friend request.\n\n"); else if (num == -3) printf("\n[i] That appears to be your own ID.\n\n"); else if (num == -4) printf("\n[i] Friend request already sent.\n\n"); else if (num == -5) printf("\n[i] Undefined error when adding friend\n\n"); }
void cmd_connect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { char *errmsg; /* check arguments */ if (argc != 3) { errmsg = "Invalid syntax."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); return; } const char *ip = argv[1]; const char *port = argv[2]; const char *key = argv[3]; if (atoi(port) == 0) { errmsg = "Invalid syntax."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); return; } char *binary_string = hex_string_to_bin(key); tox_bootstrap_from_address(m, ip, TOX_ENABLE_IPV6_DEFAULT, htons(atoi(port)), (uint8_t *) binary_string); free(binary_string); }
int main(int argc, char *argv[]) { manage_keys(); printf("Public key: "); uint32_t i; FILE *file; file = fopen("PUBLIC_ID.txt", "w"); for(i = 0; i < 32; i++) { if(self_public_key[i] < 16) printf("0"); printf("%hhX",self_public_key[i]); fprintf(file, "%hhX",self_public_key[i]); } fclose(file); printf("\n"); printf("Port: %u\n", PORT); //initialize networking //bind to ip 0.0.0.0:PORT IP ip; ip.i = 0; init_networking(ip, PORT); perror("Initialization"); if (argc > 3) { printf("Trying to bootstrap into the network...\n"); IP_Port bootstrap_info; bootstrap_info.ip.i = inet_addr(argv[1]); bootstrap_info.port = htons(atoi(argv[2])); uint8_t *bootstrap_key = hex_string_to_bin(argv[3]); DHT_bootstrap(bootstrap_info, bootstrap_key); free(bootstrap_key); } DHT_init(); friendreq_init(); int is_waiting_for_dht_connection = 1; while(1) { if (is_waiting_for_dht_connection && DHT_isconnected()) { printf("Connected to other bootstrap server successfully.\n"); is_waiting_for_dht_connection = 0; } doDHT(); networking_poll(); c_sleep(1); } shutdown_networking(); return 0; }
static int load_nodelist(const char *filename) { if (!filename) return 1; FILE *fp = fopen(filename, "r"); if (fp == NULL) return 1; char line[MAX_NODE_LINE]; while (fgets(line, sizeof(line), fp) && toxNodes.lines < MAXNODES) { size_t line_len = strlen(line); if (line_len >= MIN_NODE_LINE && line_len <= MAX_NODE_LINE) { const char *name = strtok(line, " "); const char *port_str = strtok(NULL, " "); const char *key_ascii = strtok(NULL, " "); if (name == NULL || port_str == NULL || key_ascii == NULL) continue; long int port = strtol(port_str, NULL, 10); if (port <= 0 || port > MAX_PORT_RANGE) continue; size_t key_len = strlen(key_ascii); size_t name_len = strlen(name); if (key_len < TOX_PUBLIC_KEY_SIZE * 2 || name_len >= NODELEN) continue; snprintf(toxNodes.nodes[toxNodes.lines], sizeof(toxNodes.nodes[toxNodes.lines]), "%s", name); toxNodes.nodes[toxNodes.lines][NODELEN - 1] = 0; toxNodes.ports[toxNodes.lines] = port; /* remove possible trailing newline from key string */ char real_ascii_key[TOX_PUBLIC_KEY_SIZE * 2 + 1]; memcpy(real_ascii_key, key_ascii, TOX_PUBLIC_KEY_SIZE * 2); key_len = TOX_PUBLIC_KEY_SIZE * 2; real_ascii_key[key_len] = '\0'; if (hex_string_to_bin(real_ascii_key, key_len, toxNodes.keys[toxNodes.lines], TOX_PUBLIC_KEY_SIZE) == -1) continue; toxNodes.lines++; } } fclose(fp); if (toxNodes.lines < 1) return 1; return 0; }
int main(int argc, char *argv[]) { /* Initialize networking - Bind to ip 0.0.0.0:PORT */ IP ip; ip.uint32 = 0; DHT *dht = new_DHT(new_net_crypto(new_networking(ip, PORT))); manage_keys(dht); printf("Public key: "); uint32_t i; FILE *file; file = fopen("PUBLIC_ID.txt", "w"); for (i = 0; i < 32; i++) { if (dht->c->self_public_key[i] < 16) printf("0"); printf("%hhX", dht->c->self_public_key[i]); fprintf(file, "%hhX", dht->c->self_public_key[i]); } fclose(file); printf("\n"); printf("Port: %u\n", PORT); perror("Initialization."); if (argc > 3) { printf("Trying to bootstrap into the network...\n"); IP_Port bootstrap_info; bootstrap_info.ip.uint32 = inet_addr(argv[1]); bootstrap_info.port = htons(atoi(argv[2])); uint8_t *bootstrap_key = hex_string_to_bin(argv[3]); DHT_bootstrap(dht, bootstrap_info, bootstrap_key); free(bootstrap_key); } int is_waiting_for_dht_connection = 1; while (1) { if (is_waiting_for_dht_connection && DHT_isconnected(dht)) { printf("Connected to other bootstrap server successfully.\n"); is_waiting_for_dht_connection = 0; } do_DHT(dht); networking_poll(dht->c->lossless_udp->net); c_sleep(1); } return 0; }
int init_tox_connection(Tox *m) { uint8_t pub_key[TOX_FRIEND_ADDRESS_SIZE]; hex_string_to_bin(pub_key, BOOTSTRAP_KEY); int res = tox_bootstrap_from_address(m, BOOTSTRAP_ADDRESS, 0, htons(BOOTSTRAP_PORT), pub_key); if (!res) { exit(1); } return 0; }
int main(int argc, char *argv[]) { manage_keys(); printf("Public key: "); uint32_t i; for(i = 0; i < 32; i++) { if(self_public_key[i] < 16) printf("0"); printf("%hhX",self_public_key[i]); } printf("\n"); printf("Port: %u\n", PORT); //initialize networking //bind to ip 0.0.0.0:PORT IP ip; ip.i = 0; init_networking(ip, PORT); perror("Initialization"); if (argc > 3) { printf("Trying to bootstrap into the network...\n"); IP_Port bootstrap_info; bootstrap_info.ip.i = inet_addr(argv[1]); bootstrap_info.port = htons(atoi(argv[2])); uint8_t *bootstrap_key = hex_string_to_bin(argv[3]); DHT_bootstrap(bootstrap_info, bootstrap_key); free(bootstrap_key); } IP_Port ip_port; uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; int is_waiting_for_dht_connection = 1; while(1) { if (is_waiting_for_dht_connection && DHT_isconnected()) { printf("Connected to other bootstrap server successfully.\n"); is_waiting_for_dht_connection = 0; } doDHT(); while(receivepacket(&ip_port, data, &length) != -1) { DHT_handlepacket(data, length, ip_port); friendreq_handlepacket(data, length, ip_port); } c_sleep(1); } shutdown_networking(); return 0; }
int av_connect_to_dht(av_session_t *_phone, char *_dht_key, const char *_dht_addr, unsigned short _dht_port) { unsigned char *_binary_string = hex_string_to_bin(_dht_key); uint16_t _port = htons(_dht_port); int _if = tox_bootstrap_from_address(_phone->_messenger, _dht_addr, 1, _port, _binary_string ); free(_binary_string); return _if ? 0 : -1; }
int main(int argc, char *argv[]) { Suite *messenger = messenger_suite(); SRunner *test_runner = srunner_create(messenger); int number_failed = 0; friend_id = hex_string_to_bin(friend_id_str); good_id_a = hex_string_to_bin(good_id_a_str); good_id_b = hex_string_to_bin(good_id_b_str); bad_id = hex_string_to_bin(bad_id_str); /* IPv6 status from global define */ Messenger_Options options = {0}; options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; m = new_messenger(&options); /* setup a default friend and friendnum */ if (m_addfriend_norequest(m, (uint8_t *)friend_id) < 0) fputs("m_addfriend_norequest() failed on a valid ID!\n" "this was CRITICAL to the test, and the build WILL fail.\n" "the tests will continue now...\n\n", stderr); if ((friend_id_num = getfriend_id(m, (uint8_t *)friend_id)) < 0) fputs("getfriend_id() failed on a valid ID!\n" "this was CRITICAL to the test, and the build WILL fail.\n" "the tests will continue now...\n\n", stderr); srunner_run_all(test_runner, CK_NORMAL); number_failed = srunner_ntests_failed(test_runner); srunner_free(test_runner); free(friend_id); free(good_id_a); free(good_id_b); free(bad_id); kill_messenger(m); return number_failed; }
/* Returns true if friendnumber's Tox ID is in the masterkeys list, false otherwise. Note that it only compares the public key portion of the IDs. */ bool friend_is_master(Tox *m, uint32_t friendnumber) { if (!file_exists(MASTERLIST_FILE)) { FILE *fp = fopen(MASTERLIST_FILE, "w"); if (fp == NULL) { fprintf(stderr, "Warning: failed to create masterkeys file\n"); return false; } fclose(fp); fprintf(stderr, "Warning: creating new masterkeys file. Did you lose the old one?\n"); return false; } FILE *fp = fopen(MASTERLIST_FILE, "r"); if (fp == NULL) { fprintf(stderr, "Warning: failed to read masterkeys file\n"); return false; } char friend_key[TOX_PUBLIC_KEY_SIZE]; if (tox_friend_get_public_key(m, friendnumber, (uint8_t *) friend_key, NULL) == 0) { fclose(fp); return false; } char id[256]; while (fgets(id, sizeof(id), fp)) { int len = strlen(id); if (--len < TOX_PUBLIC_KEY_SIZE) continue; char *key_bin = hex_string_to_bin(id); if (memcmp(key_bin, friend_key, TOX_PUBLIC_KEY_SIZE) == 0) { free(key_bin); fclose(fp); return true; } free(key_bin); } fclose(fp); return false; }
/* Connects to a random DHT server listed in the DHTservers file */ int init_connection(Messenger *m) { FILE *fp = NULL; if (DHT_isconnected(m->dht)) return 0; fp = fopen(SRVLIST_FILE, "r"); if (!fp) return 1; char servers[MAXSERVERS][MAXLINE]; char line[MAXLINE]; int linecnt = 0; while (fgets(line, sizeof(line), fp) && linecnt < MAXSERVERS) { if (strlen(line) > MINLINE) strcpy(servers[linecnt++], line); } if (linecnt < 1) { fclose(fp); return 2; } fclose(fp); char *server = servers[rand() % linecnt]; char *ip = strtok(server, " "); char *port = strtok(NULL, " "); char *key = strtok(NULL, " "); if (!ip || !port || !key) return 3; IP_Port dht; dht.port = htons(atoi(port)); uint32_t resolved_address = resolve_addr(ip); if (resolved_address == 0) return 0; dht.ip.i = resolved_address; unsigned char *binary_string = hex_string_to_bin(key); DHT_bootstrap(m->dht, dht, binary_string); free(binary_string); return 0; }
static void bootstrap_DHT(Tox *m) { int i; for (i = 0; nodes[i].ip; ++i) { char *key = hex_string_to_bin(nodes[i].key); TOX_ERR_BOOTSTRAP err; tox_bootstrap(m, nodes[i].ip, nodes[i].port, (uint8_t *) key, &err); free(key); if (err != TOX_ERR_BOOTSTRAP_OK) fprintf(stderr, "Failed to bootstrap DHT via: %s %d (error %d)\n", nodes[i].ip, nodes[i].port, err); } }
int main(int argc, char *argv[]) { if (argc < 2) { printf("usage: ./cracker public_key(or beginning of one in hex format)\n"); return 0; } long long unsigned int num_tries = 0; uint32_t len = strlen(argv[1]) / 2; unsigned char *key = hex_string_to_bin(argv[1]); uint8_t pub_key[32], priv_key[32], c_key[32]; if (len > 32) { len = 32; } memcpy(c_key, key, len); free(key); randombytes(priv_key, 32); while (1) { crypto_scalarmult_curve25519_base(pub_key, priv_key); uint32_t i; if (memcmp(c_key, pub_key, len) == 0) { break; } for (i = 32; i != 0; --i) { priv_key[i - 1] += 1; if (priv_key[i - 1] != 0) { break; } } ++num_tries; } printf("Public key:\n"); print_key(pub_key); printf("\nPrivate key:\n"); print_key(priv_key); printf("\n %llu keys tried\n", num_tries); return 0; }
int av_add_friend(av_session_t *_phone, char *_friend_hash) { trim_spaces(_friend_hash); unsigned char *_bin_string = hex_string_to_bin(_friend_hash); int _number = tox_add_friend(_phone->_messenger, _bin_string, (uint8_t *)"Tox phone "_USERAGENT, sizeof("Tox phone "_USERAGENT)); free(_bin_string); if ( _number >= 0) { INFO("Added friend as %d", _number ); av_allocate_friend(_phone, _number, 0); } else INFO("Unknown error %i", _number ); return _number; }
/* Bootstraps to NUM_BOOTSTRAPS random nodes in the bootsrap nodes list. */ static void bootstrap_tox(Crawler *cwl) { for (size_t i = 0; i < NUM_BOOTSTRAPS; ++i) { int r = rand() % NUM_BOOTSTRAP_NODES; char bin_key[TOX_PUBLIC_KEY_SIZE]; if (hex_string_to_bin(bs_nodes[r].key, strlen(bs_nodes[r].key), bin_key, sizeof(bin_key)) == -1) { continue; } TOX_ERR_BOOTSTRAP err; tox_bootstrap(cwl->tox, bs_nodes[r].ip, bs_nodes[r].port, (uint8_t *) bin_key, &err); if (err != TOX_ERR_BOOTSTRAP_OK) { fprintf(stderr, "Failed to bootstrap DHT via: %s %d (error %d)\n", bs_nodes[r].ip, bs_nodes[r].port, err); } } }
void cmd_connect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { if (argc != 3) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Require: <ip> <port> <key>"); return; } const char *ip = argv[1]; const char *port_str = argv[2]; const char *ascii_key = argv[3]; long int port = strtol(port_str, NULL, 10); if (port <= 0 || port > MAX_PORT_RANGE) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid port."); return; } char key_binary[TOX_PUBLIC_KEY_SIZE * 2 + 1]; if (hex_string_to_bin(ascii_key, strlen(ascii_key), key_binary, TOX_PUBLIC_KEY_SIZE) == -1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid key."); return; } TOX_ERR_BOOTSTRAP err; tox_bootstrap(m, ip, port, (uint8_t *) key_binary, &err); tox_add_tcp_relay(m, ip, port, (uint8_t *) key_binary, &err); switch (err) { case TOX_ERR_BOOTSTRAP_BAD_HOST: line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Bootstrap failed: Invalid IP."); break; case TOX_ERR_BOOTSTRAP_BAD_PORT: line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Bootstrap failed: Invalid port."); break; case TOX_ERR_BOOTSTRAP_NULL: line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Bootstrap failed."); break; default: break; } }
static int nodelist_load(const char *filename) { if (!filename) return 1; FILE *fp = fopen(filename, "r"); if (fp == NULL) return 1; char line[MAXLINE]; while (fgets(line, sizeof(line), fp) && toxNodes.lines < MAXNODES) { if (strlen(line) > MINLINE) { const char *name = strtok(line, " "); const char *port = strtok(NULL, " "); const char *key_ascii = strtok(NULL, " "); /* invalid line */ if (name == NULL || port == NULL || key_ascii == NULL) continue; snprintf(toxNodes.nodes[toxNodes.lines], sizeof(toxNodes.nodes[toxNodes.lines]), "%s", name); toxNodes.nodes[toxNodes.lines][NODELEN - 1] = 0; toxNodes.ports[toxNodes.lines] = htons(atoi(port)); char *key_binary = hex_string_to_bin(key_ascii); memcpy(toxNodes.keys[toxNodes.lines], key_binary, TOX_CLIENT_ID_SIZE); free(key_binary); toxNodes.lines++; } } if (toxNodes.lines < 1) { fclose(fp); return 2; } fclose(fp); return 0; }
void cmd_connect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { if (argc != 3) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Require: <ip> <port> <key>"); return; } const char *ip = argv[1]; const char *port = argv[2]; const char *key = argv[3]; if (atoi(port) == 0) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid port."); return; } char *binary_string = hex_string_to_bin(key); tox_bootstrap_from_address(m, ip, atoi(port), (uint8_t *) binary_string); free(binary_string); }
static int nodelist_load(char *filename) { if (!filename) return 1; FILE *fp = fopen(filename, "r"); if (fp == NULL) return 1; char line[MAXLINE]; while (fgets(line, sizeof(line), fp) && linecnt < MAXNODES) { if (strlen(line) > MINLINE) { char *name = strtok(line, " "); char *port = strtok(NULL, " "); char *key_ascii = strtok(NULL, " "); /* invalid line */ if (name == NULL || port == NULL || key_ascii == NULL) continue; strncpy(nodes[linecnt], name, NODELEN); nodes[linecnt][NODELEN - 1] = 0; ports[linecnt] = htons(atoi(port)); uint8_t *key_binary = hex_string_to_bin(key_ascii); memcpy(keys[linecnt], key_binary, TOX_CLIENT_ID_SIZE); free(key_binary); linecnt++; } } if (linecnt < 1) { fclose(fp); return 2; } fclose(fp); return 0; }
int serverlist_load() { FILE *fp = NULL; fp = fopen(SRVLIST_FILE, "r"); if (fp == NULL) return 1; char line[MAXLINE]; while (fgets(line, sizeof(line), fp) && linecnt < MAXSERVERS) { if (strlen(line) > MINLINE) { char *name = strtok(line, " "); char *port = strtok(NULL, " "); char *key_ascii = strtok(NULL, " "); /* invalid line */ if (name == NULL || port == NULL || key_ascii == NULL) continue; strncpy(servers[linecnt], name, SERVERLEN); servers[linecnt][SERVERLEN - 1] = 0; ports[linecnt] = htons(atoi(port)); uint8_t *key_binary = hex_string_to_bin(key_ascii); memcpy(keys[linecnt], key_binary, TOX_CLIENT_ID_SIZE); free(key_binary); linecnt++; } } if (linecnt < 1) { fclose(fp); return 2; } fclose(fp); return 0; }
void cmd_connect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { if (argc != 3) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Require: <ip> <port> <key>"); return; } const char *ip = argv[1]; const char *port = argv[2]; const char *key = argv[3]; if (atoi(port) == 0) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid port."); return; } char *binary_string = hex_string_to_bin(key); TOX_ERR_BOOTSTRAP err; tox_bootstrap(m, ip, atoi(port), (uint8_t *) binary_string, &err); tox_add_tcp_relay(m, ip, atoi(port), (uint8_t *) binary_string, &err); free(binary_string); switch (err) { case TOX_ERR_BOOTSTRAP_BAD_HOST: line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Bootstrap failed: Invalid IP."); break; case TOX_ERR_BOOTSTRAP_BAD_PORT: line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Bootstrap failed: Invalid port."); break; case TOX_ERR_BOOTSTRAP_NULL: line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Bootstrap failed."); break; default: break; } }
void cmd_connect(ToxWindow *self, Messenger *m, char **args) { IP_Port dht; char *ip = args[1]; char *port = args[2]; char *key = args[3]; if (atoi(port) == 0) { wprintw(self->window, "Invalid syntax.\n"); return; } dht.port = htons(atoi(port)); uint32_t resolved_address = resolve_addr(ip); if (resolved_address == 0) { return; } dht.ip.i = resolved_address; unsigned char *binary_string = hex_string_to_bin(key); DHT_bootstrap(dht, binary_string); free(binary_string); }
/** * add data to remote message queue * this func may block if current message queue is not available * data format * { * target_id: target to send * uuid: sock uuid * data: raw data <SOCK_BUF_SIZE or less> * cmd: can be null when no cmd is to send * } * */ void write_data_remote(const uint8_t *uuid, const uint8_t* cmd, const uint8_t *data, const uint32_t length){ // pack msg to bin uint8_t *msg_bin = (uint8_t *)malloc(sizeof(uint8_t)*MY_MESSAGE_LENGTH); pack_msg_bin(msg_bin, uuid, cmd, data, length); uint8_t hight_byte = msg_bin[UUID_LENGTH + CMD_LENGTH]; uint8_t low_byte = msg_bin[UUID_LENGTH + CMD_LENGTH + 1]; uint16_t mlength = hight_byte * 256 + low_byte; // print msg content /*printf("uuid:%s\n", uuid); printf("cmd:%s\n", cmd); printf("data:%s\n", data); printf("length:%d\n", length); printf("afterLength:%d\n", mlength);*/ // unpack msg bin //debug_msg_bin(msg_bin); // add msg to message queue MSGTask *newTask = (MSGTask *)malloc(sizeof(MSGTask)); uint8_t *target_addr_bin = (uint8_t *)malloc(sizeof(uint8_t)*TOX_FRIEND_ADDRESS_SIZE); hex_string_to_bin(target_addr_bin, target_id); newTask->target_addr_bin = target_addr_bin; newTask->msg = msg_bin; pthread_mutex_lock(&msg_task_lock); while ((msg_task_queue->size) >= MAX_MSG_CACHE){ pthread_cond_wait(&msg_task_cond, &msg_task_lock); }; // enter queue Enqueue(msg_task_queue, newTask); if((msg_task_queue->size) < MAX_MSG_CACHE) pthread_cond_broadcast(&msg_task_cond); pthread_mutex_unlock(&msg_task_lock); // free space free(msg_bin); free(target_addr_bin); free(newTask); }
void *on_remote_sock_created(void *msockfd){ uint32_t sockfd = *((uint32_t *)msockfd); uint8_t *target_addr_bin = (uint8_t *)malloc(sizeof(uint8_t)*TOX_FRIEND_ADDRESS_SIZE); hex_string_to_bin(target_addr_bin,target_id); uint8_t uuid[UUID_LENGTH+1]; get_local_socks_uuid(msocks_list,sockfd,uuid); printf("uuid: %s\n",uuid); printf("socketfd: %d\n",sockfd); uint8_t buf[SOCK_BUF_SIZE]; int length = 1; while(length > 0){ memset(buf,0,SOCK_BUF_SIZE); length = readCSock(sockfd,buf,SOCK_BUF_SIZE-1); if(length > 0){ on_local_data_received(buf,length,sockfd); } } // read data error // close remote and local sock printf("uuid: %s\n", uuid); printf("socketfd: %d\n", sockfd); if(get_local_socks(msocks_list, uuid) != 0){ close_remote_socket(uuid,target_addr_bin);// 从本地发起的关闭 }else{ // closed by remote before create success send if(uuid[0] == '\0'){ //exit(0);// maybe local socket has been closed printf("closed by local before create socket received\n"); } } close_local_socks(msocks_list, sockfd); free(target_addr_bin); free(msockfd); return NULL; }
int main(int argc, char *argv[]) { if (argc == 2 && !strncasecmp(argv[1], "-h", 3)) { printf("Usage (connected) : %s [--ipv4|--ipv6] IP PORT KEY\n", argv[0]); printf("Usage (unconnected): %s [--ipv4|--ipv6]\n", argv[0]); exit(0); } /* let user override default by cmdline */ uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); if (argvoffset < 0) exit(1); /* Initialize networking - Bind to ip 0.0.0.0 / [::] : PORT */ IP ip; ip_init(&ip, ipv6enabled); DHT *dht = new_DHT(new_networking(ip, PORT)); Onion *onion = new_onion(dht); Onion_Announce *onion_a = new_onion_announce(dht); #ifdef DHT_NODE_EXTRA_PACKETS bootstrap_set_callbacks(dht->net, DHT_VERSION_NUMBER, DHT_MOTD, sizeof(DHT_MOTD)); #endif if (!(onion && onion_a)) { printf("Something failed to initialize.\n"); exit(1); } perror("Initialization"); manage_keys(dht); printf("Public key: "); uint32_t i; #ifdef TCP_RELAY_ENABLED #define NUM_PORTS 3 uint16_t ports[NUM_PORTS] = {443, 3389, PORT}; TCP_Server *tcp_s = new_TCP_server(ipv6enabled, NUM_PORTS, ports, dht->self_public_key, dht->self_secret_key, onion); if (tcp_s == NULL) { printf("TCP server failed to initialize.\n"); exit(1); } #endif FILE *file; file = fopen("PUBLIC_ID.txt", "w"); for (i = 0; i < 32; i++) { printf("%02hhX", dht->self_public_key[i]); fprintf(file, "%02hhX", dht->self_public_key[i]); } fclose(file); printf("\n"); printf("Port: %u\n", ntohs(dht->net->port)); if (argc > argvoffset + 3) { printf("Trying to bootstrap into the network...\n"); uint16_t port = htons(atoi(argv[argvoffset + 2])); uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]); int res = DHT_bootstrap_from_address(dht, argv[argvoffset + 1], ipv6enabled, port, bootstrap_key); free(bootstrap_key); if (!res) { printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); exit(1); } } int is_waiting_for_dht_connection = 1; uint64_t last_LANdiscovery = 0; LANdiscovery_init(dht); while (1) { if (is_waiting_for_dht_connection && DHT_isconnected(dht)) { printf("Connected to other bootstrap node successfully.\n"); is_waiting_for_dht_connection = 0; } do_DHT(dht); if (is_timeout(last_LANdiscovery, is_waiting_for_dht_connection ? 5 : LAN_DISCOVERY_INTERVAL)) { send_LANdiscovery(htons(PORT), dht); last_LANdiscovery = unix_time(); } #ifdef TCP_RELAY_ENABLED do_TCP_server(tcp_s); #endif networking_poll(dht->net); c_sleep(1); } return 0; }
int main(int argc, char *argv[]) { if (argc < 4 && argc != 2) { printf("usage %s ip port public_key (of the DHT bootstrap node)\n or\n %s Save.bak\n", argv[0], argv[0]); exit(0); } init_tox(); if(argc > 3) { IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); bootstrap_ip_port.ip.i = inet_addr(argv[1]); DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); } else { FILE *file = fopen(argv[1], "rb"); if ( file==NULL ){return 1;} int read; uint8_t buffer[128000]; read = fread(buffer, 1, 128000, file); printf("Messenger loaded: %i\n", load_tox_state(buffer, read)); fclose(file); } friend_add_request_callback(print_request); message_receive_callback(print_message); printf("OUR ID: "); uint32_t i; for(i = 0; i < 32; i++) { if(self_public_key[i] < 16) printf("0"); printf("%hhX",self_public_key[i]); } set_self_name((uint8_t *)"Anon", 5); char temp_id[128]; printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n"); if(scanf("%s", temp_id) != 1) { return 1; } int num = add_friend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); perror("Initialization"); while(1) { uint8_t name[128]; get_friend_name(num, name); printf("%s\n", name); send_message(num, (uint8_t*)"Test", 5); process_tox(); c_sleep(30); FILE *file = fopen("Save.bak", "wb"); if ( file==NULL ){return 1;} uint8_t * buffer = malloc(tox_state_size()); save_tox_state(buffer); fwrite(buffer, 1, tox_state_size(), file); free(buffer); fclose(file); } }
int main(int argc, char *argv[]) { unsigned char pk[crypto_sign_ed25519_PUBLICKEYBYTES]; unsigned char sk[crypto_sign_ed25519_SECRETKEYBYTES]; if (argc == 2 && argv[1][0] == 'g') { crypto_sign_ed25519_keypair(pk, sk); printf("Public key:\n"); int i; for (i = 0; i < crypto_sign_ed25519_PUBLICKEYBYTES; i++) { printf("%02hhX", pk[i]); } printf("\nSecret key:\n"); for (i = 0; i < crypto_sign_ed25519_SECRETKEYBYTES; i++) { printf("%02hhX", sk[i]); } printf("\n"); } if (argc == 5 && argv[1][0] == 's') { unsigned char *secret_key = hex_string_to_bin(argv[2]); char *data; int size = load_file(argv[3], &data); if (size < 0) goto fail; unsigned long long smlen; char *sm = malloc(size + crypto_sign_ed25519_BYTES * 2); crypto_sign_ed25519(sm, &smlen, data, size, secret_key); if (smlen - size != crypto_sign_ed25519_BYTES) goto fail; FILE *f = fopen(argv[4], "wb"); if (f == NULL) goto fail; memcpy(sm + smlen, sm, crypto_sign_ed25519_BYTES); // Move signature from beginning to end of file. if (fwrite(sm + (smlen - size), 1, smlen, f) != smlen) goto fail; fclose(f); printf("Signed successfully.\n"); } if (argc == 4 && argv[1][0] == 'c') { unsigned char *public_key = hex_string_to_bin(argv[2]); char *data; int size = load_file(argv[3], &data); if (size < 0) goto fail; char *signe = malloc(size + crypto_sign_ed25519_BYTES); memcpy(signe, data + size - crypto_sign_ed25519_BYTES, crypto_sign_ed25519_BYTES); // Move signature from end to beginning of file. memcpy(signe + crypto_sign_ed25519_BYTES, data, size - crypto_sign_ed25519_BYTES); unsigned long long smlen; char *m = malloc(size); unsigned long long mlen; if (crypto_sign_ed25519_open(m, &mlen, signe, size, public_key) == -1) { printf("Failed checking sig.\n"); goto fail; } printf("Checked successfully.\n"); } return 0; fail: printf("FAIL\n"); return 1; }
int main(int argc, char *argv[]) { int on = 0; int c = 0; int i = 0; char *filename = "data"; char idstring[200] = {0}; 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); } } } } initMessenger(); load_key(filename); m_callback_friendrequest(print_request); m_callback_friendmessage(print_message); m_callback_namechange(print_nickchange); m_callback_statusmessage(print_statuschange); initscr(); noecho(); raw(); getmaxyx(stdscr, y, x); new_lines("/h for list of commands"); get_id(idstring); new_lines(idstring); strcpy(line, ""); 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]); DHT_bootstrap(bootstrap_ip_port, binary_string); free(binary_string); nodelay(stdscr, TRUE); while(true) { if (on == 0 && DHT_isconnected()) { new_lines("[i] connected to DHT\n[i] define username with /n"); on = 1; } doMessenger(); c_sleep(1); do_refresh(); c = getch(); if (c == ERR || c == 27) continue; getmaxyx(stdscr, y, x); if (c == '\n') { line_eval(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)); } } endwin(); return 0; }
void line_eval(char *line) { if (line[0] == '/') { char inpt_command = line[1]; char prompt[STRING_LENGTH+2] = "> "; int prompt_offset = 3; strcat(prompt, line); new_lines(prompt); if (inpt_command == 'f') { // add friend command: /f ID int i; char temp_id[128]; for (i = 0; i < 128; i++) temp_id[i] = line[i+prompt_offset]; unsigned char *bin_string = hex_string_to_bin(temp_id); int num = m_addfriend(bin_string, (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); free(bin_string); char numstring[100]; switch (num) { case FAERR_TOOLONG: sprintf(numstring, "[i] Message is too long."); break; case FAERR_NOMESSAGE: sprintf(numstring, "[i] Please add a message to your request."); break; case FAERR_OWNKEY: sprintf(numstring, "[i] That appears to be your own ID."); break; case FAERR_ALREADYSENT: sprintf(numstring, "[i] Friend request already sent."); break; case FAERR_UNKNOWN: sprintf(numstring, "[i] Undefined error when adding friend."); break; default: sprintf(numstring, "[i] Added friend as %d.", num); break; } new_lines(numstring); do_refresh(); } else if (inpt_command == 'd') { doMessenger(); } else if (inpt_command == 'm') { //message command: /m friendnumber messsage size_t len = strlen(line); if(len < 3) return; char numstring[len-3]; char message[len-3]; int i; for (i = 0; i < len; i++) { if (line[i+3] != ' ') { numstring[i] = line[i+3]; } else { int j; for (j = (i+1); j < (len+1); j++) message[j-i-1] = line[j+3]; break; } } int num = atoi(numstring); if (m_sendmessage(num, (uint8_t*) message, strlen(message) + 1) != 1) { new_lines("[i] could not send message"); } else { new_lines(format_message(message, -1)); } } else if (inpt_command == 'n') { uint8_t name[MAX_NAME_LENGTH]; int i = 0; size_t len = strlen(line); for (i = 3; i < len; i++) { if (line[i] == 0 || line[i] == '\n') break; name[i-3] = line[i]; } name[i-3] = 0; setname(name, i - 2); char numstring[100]; sprintf(numstring, "[i] changed nick to %s", (char*)name); new_lines(numstring); } else if (inpt_command == 'l') { print_friendlist(); } else if (inpt_command == 's') { uint8_t status[MAX_STATUSMESSAGE_LENGTH]; int i = 0; size_t len = strlen(line); for (i = 3; i < len; i++) { if (line[i] == 0 || line[i] == '\n') break; status[i-3] = line[i]; } status[i-3] = 0; m_set_statusmessage(status, strlen((char*)status) + 1); char numstring[100]; sprintf(numstring, "[i] changed status to %s", (char*)status); new_lines(numstring); } else if (inpt_command == 'a') { uint8_t numf = atoi(line + 3); char numchar[100]; if (numf >= num_requests || pending_requests[numf].accepted) { sprintf(numchar,"[i] you either didn't receive that request or you already accepted it"); new_lines(numchar); } else { int num = m_addfriend_norequest(pending_requests[numf].id); if (num != -1) { pending_requests[numf].accepted = 1; sprintf(numchar, "[i] friend request %u accepted", numf); new_lines(numchar); sprintf(numchar, "[i] added friendnumber %d", num); new_lines(numchar); } else { sprintf(numchar, "[i] failed to add friend"); new_lines(numchar); } } do_refresh(); } else if (inpt_command == 'h') { //help new_lines(help); } else if (inpt_command == 'i') { //info char idstring[200]; get_id(idstring); new_lines(idstring); } else if (inpt_command == 'q') { //exit endwin(); exit(EXIT_SUCCESS); } else { new_lines("[i] invalid command"); } } else { new_lines("[i] invalid command"); //new_lines(line); } }