int main(int argc, char *argv[]) { //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); if (argc < 4) { printf("usage %s ip port client_id(of friend to find ip_port of)\n", argv[0]); exit(0); } DHT_addfriend((uint8_t *)argv[3]); //initialize networking //bind to ip 0.0.0.0:PORT IP ip; ip.i = 0; init_networking(ip, PORT); int randdomnum = random_int(); memcpy(self_client_id, &randdomnum, 4); perror("Initialization"); IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); //bootstrap_ip_port.ip.c[0] = 127; //bootstrap_ip_port.ip.c[1] = 0; //bootstrap_ip_port.ip.c[2] = 0; //bootstrap_ip_port.ip.c[3] = 1; bootstrap_ip_port.ip.i = inet_addr(argv[1]); DHT_bootstrap(bootstrap_ip_port); IP_Port ip_port; uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; while(1) { doDHT(); while(receivepacket(&ip_port, data, &length) != -1) { if(DHT_handlepacket(data, length, ip_port)) { //unhandled packet printpacket(data, length, ip_port); } else { printf("Received handled packet with length: %u\n", length); } } print_clientlist(); print_friendlist(); c_sleep(300); } shutdown_networking(); 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); } }
int main(int argc, char *argv[]) { //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); if (argc < 4) { printf("usage %s ip port public_key\n", argv[0]); exit(0); } new_keys(); 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]); } 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) exit(0); DHT_addfriend(hex_string_to_bin(temp_id)); /* initialize networking */ /* bind to ip 0.0.0.0:PORT */ IP ip; ip.i = 0; init_networking(ip, PORT); perror("Initialization"); IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); /* bootstrap_ip_port.ip.c[0] = 127; * bootstrap_ip_port.ip.c[1] = 0; * bootstrap_ip_port.ip.c[2] = 0; * bootstrap_ip_port.ip.c[3] = 1; */ bootstrap_ip_port.ip.i = inet_addr(argv[1]); DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); IP_Port ip_port; uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; while(1) { doDHT(); while(receivepacket(&ip_port, data, &length) != -1) { if(DHT_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port)) { //unhandled packet printpacket(data, length, ip_port); } else { printf("Received handled packet with length: %u\n", length); } } print_clientlist(); print_friendlist(); c_sleep(300); } shutdown_networking(); return 0; }
int main(int argc, char *argv[]) { log_init(); srand(time(NULL)); int randdomnum = rand(); memcpy(self_client_id, &randdomnum, 4); //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); if (argc < 4) { printf("usage %s ip port client_id(of friend to find ip_port of)\n", argv[0]); exit(0); } addfriend(argv[3]); //initialize networking //bind to ip 0.0.0.0:PORT IP ip; ip.i = 0; if (init_networking(ip, PORT) == -1) { return 1; } perror("Initialization"); IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); //bootstrap_ip_port.ip.c[0] = 127; //bootstrap_ip_port.ip.c[1] = 0; //bootstrap_ip_port.ip.c[2] = 0; //bootstrap_ip_port.ip.c[3] = 1; bootstrap_ip_port.ip.i = inet_addr(argv[1]); bootstrap(bootstrap_ip_port); IP_Port ip_port; char data[MAX_UDP_PACKET_SIZE]; uint32_t length; uint32_t i; while(1) { doDHT(); while(recievepacket(&ip_port, data, &length) != -1) { if(DHT_handlepacket(data, length, ip_port)) { printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); printf("--------------------BEGIN-----------------------------\n"); for(i = 0; i < length; i++) { if(data[i] < 16) printf("0"); printf("%hhX",data[i]); } printf("\n--------------------END-----------------------------\n\n\n"); } else { printf("Received handled packet with length: %u\n", length); } } print_clientlist(); print_friendlist(); c_sleep(300); } exit_networking(); log_exit(); return 0; }
void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) { if (line[0] == '/') { char inpt_command = line[1]; char prompt[STRING_LENGTH + 2] = "> "; 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+3]; int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); char numstring[100]; sprintf(numstring, "[i] added friend %d", num); 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); 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; j++) message[j-i-1] = line[j+3]; break; } } int num = atoi(numstring); if (m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 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); 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_USERSTATUS_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_userstatus(status, strlen((char*)status)); 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]; int num = m_addfriend_norequest(pending_requests[numf]); if (num != -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("[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status)"); new_lines("[i] /l list (list friends), /h for help, /i for info, /n nick (to change nickname), /q (to quit)"); } else if (inpt_command == 'i') { //info char idstring0[200]; char idstring1[PUB_KEY_BYTES][5]; char idstring2[PUB_KEY_BYTES][5]; int i; for (i = 0; i < PUB_KEY_BYTES; i++) { if (self_public_key[i] < (PUB_KEY_BYTES/2)) strcpy(idstring1[i],"0"); else strcpy(idstring1[i], ""); sprintf(idstring2[i], "%hhX", self_public_key[i]); } // strcpy(idstring0,"[i] ID: "); int j; for (j = 0; j < PUB_KEY_BYTES; j++) { strcat(idstring0,idstring1[j]); strcat(idstring0,idstring2[j]); } new_lines(idstring0); } 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); } }
void line_eval(Tox *m, 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, delta = 0; char temp_id[128]; for (i = 0; i < 128; i++) { temp_id[i - delta] = line[i + prompt_offset]; if ((temp_id[i - delta] == ' ') || (temp_id[i - delta] == '+')) delta++; } unsigned char *bin_string = hex_string_to_bin(temp_id); int num = tox_add_friend(m, bin_string, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); free(bin_string); char numstring[100]; switch (num) { case TOX_FAERR_TOOLONG: sprintf(numstring, "[i] Message is too long."); break; case TOX_FAERR_NOMESSAGE: sprintf(numstring, "[i] Please add a message to your request."); break; case TOX_FAERR_OWNKEY: sprintf(numstring, "[i] That appears to be your own ID."); break; case TOX_FAERR_ALREADYSENT: sprintf(numstring, "[i] Friend request already sent."); break; case TOX_FAERR_UNKNOWN: sprintf(numstring, "[i] Undefined error when adding friend."); break; default: if (num >= 0) { sprintf(numstring, "[i] Added friend as %d.", num); save_data(m); } else sprintf(numstring, "[i] Unknown error %i.", num); break; } new_lines(numstring); } else if (inpt_command == 'd') { tox_do(m); } else if (inpt_command == 'm') { //message command: /m friendnumber messsage char *posi[1]; int num = strtoul(line + prompt_offset, posi, 0); if (**posi != 0) { if (tox_send_message(m, num, (uint8_t *) *posi + 1, strlen(*posi + 1)) < 1) { char sss[256]; sprintf(sss, "[i] could not send message to friend num %u", num); new_lines(sss); } else { print_formatted_message(m, *posi + 1, num, 1); } } else new_lines("Error, bad input."); } else if (inpt_command == 'n') { uint8_t name[TOX_MAX_NAME_LENGTH]; size_t i, 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; tox_set_name(m, 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(m); } else if (inpt_command == 's') { uint8_t status[TOX_MAX_STATUSMESSAGE_LENGTH]; size_t i, 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; tox_set_status_message(m, status, strlen((char *)status)); char numstring[100]; sprintf(numstring, "[i] changed status to %s", (char *)status); new_lines(numstring); } else if (inpt_command == 'a') { // /a #: accept 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 = tox_add_friend_norequest(m, pending_requests[numf].id); if (num != -1) { pending_requests[numf].accepted = 1; sprintf(numchar, "[i] friend request %u accepted as friend no. %d", numf, num); new_lines(numchar); save_data(m); } else { sprintf(numchar, "[i] failed to add friend"); new_lines(numchar); } } } else if (inpt_command == 'r') { // /r #: remove friend uint8_t numf = atoi(line + 3); if (!tox_friend_exists(m, numf)) { char err[64]; sprintf(err, "You don't have a friend %i.", numf); new_lines(err); return; } char msg[128 + TOX_MAX_NAME_LENGTH]; char fname[TOX_MAX_NAME_LENGTH ]; getfriendname_terminated(m, numf, fname); sprintf(msg, "Are you sure you want to delete friend %i: %s? (y/n)", numf, fname); input_line[0] = 0; new_lines(msg); int c; do { c = getchar(); } while ((c != 'y') && (c != 'n') && (c != EOF)); if (c == 'y') { int res = tox_del_friend(m, numf); if (res == 0) sprintf(msg, "[i] [%i: %s] is no longer your friend", numf, fname); else sprintf(msg, "[i] failed to remove friend"); new_lines(msg); } } else if (inpt_command == 'h') { //help if (line[2] == ' ') { if (line[3] == 'f') { new_lines_mark(help_friend1, 1); new_lines_mark(help_friend2, 1); return; } else if (line[3] == 'g') { new_lines_mark(help_group, 1); return; } } new_lines_mark(help_main, 1); } else if (inpt_command == 'x') { //info char idstring[200]; get_id(m, idstring); new_lines(idstring); } else if (inpt_command == 'g') { //create new group chat char msg[256]; sprintf(msg, "[g] Created new group chat with number: %u", tox_add_groupchat(m)); new_lines(msg); } else if (inpt_command == 'i') { //invite friendnum to groupnum char *posi[1]; int friendnumber = strtoul(line + prompt_offset, posi, 0); int groupnumber = strtoul(*posi + 1, NULL, 0); char msg[256]; sprintf(msg, "[g] Invited friend number %u to group number %u, returned: %u (0 means success)", friendnumber, groupnumber, tox_invite_friend(m, friendnumber, groupnumber)); new_lines(msg); } else if (inpt_command == 'z') { //send message to groupnum char *posi[1]; int groupnumber = strtoul(line + prompt_offset, posi, 0); if (**posi != 0) { int res = tox_group_message_send(m, groupnumber, (uint8_t *)*posi + 1, strlen(*posi + 1)); if (res == 0) { char msg[32 + STRING_LENGTH]; sprintf(msg, "[g] #%u: YOU: %s", groupnumber, *posi + 1); new_lines(msg); } else { char msg[128]; sprintf(msg, "[i] could not send message to group no. %u: %i", groupnumber, res); new_lines(msg); } } } else if (inpt_command == 't') { char *posi[1]; int friendnum = strtoul(line + prompt_offset, posi, 0); if (**posi != 0) { char msg[512]; sprintf(msg, "[t] Sending file %s to friendnum %u filenumber is %i (-1 means failure)", *posi + 1, friendnum, add_filesender(m, friendnum, *posi + 1)); new_lines(msg); } } else if (inpt_command == 'q') { //exit save_data(m); endwin(); tox_kill(m); exit(EXIT_SUCCESS); } else if (inpt_command == 'c') { //set conversation partner if (line[2] == 'r') { if (conversation_default != 0) { conversation_default = 0; new_lines("[i] default conversation reset"); } else new_lines("[i] default conversation wasn't set, nothing to do"); } else if (line[3] != ' ') { new_lines("[i] invalid command"); } else { int num = atoi(line + 4); /* zero is also returned for not-a-number */ if (!num && strcmp(line + 4, "0")) num = -1; if (num < 0) new_lines("[i] invalid command parameter"); else if (line[2] == 'f') { conversation_default = num + 1; char buffer[128]; sprintf(buffer, "[i] default conversation is now to friend %i", num); new_lines(buffer); } else if (line[2] == 'g') { char buffer[128]; conversation_default = - (num + 1); sprintf(buffer, "[i] default conversation is now to group %i", num); new_lines(buffer); } else new_lines("[i] invalid command"); } } else if (inpt_command == 'p') { //list peers char *posi = NULL; int group_number = strtoul(line + prompt_offset, &posi, 0); if (posi != NULL) { char msg[64]; int peer_cnt = tox_group_number_peers(m, group_number); if (peer_cnt < 0) { new_lines("[g] Invalid group number."); } else if (peer_cnt == 0) { sprintf(msg, "[g] #%i: No peers in group.", group_number); new_lines(msg); } else { sprintf(msg, "[g] #%i: Group has %i peers. Names:", group_number, peer_cnt); new_lines(msg); print_groupchatpeers(m, group_number); } } } else { new_lines("[i] invalid command"); } } else { if (conversation_default != 0) { if (conversation_default > 0) { int friendnumber = conversation_default - 1; uint32_t res = tox_send_message(m, friendnumber, (uint8_t *)line, strlen(line)); if (res == 0) { char sss[128]; sprintf(sss, "[i] could not send message to friend no. %u", friendnumber); new_lines(sss); } else print_formatted_message(m, line, friendnumber, 1); } else { int groupnumber = - conversation_default - 1; int res = tox_group_message_send(m, groupnumber, (uint8_t *)line, strlen(line)); if (res == 0) { char msg[32 + STRING_LENGTH]; sprintf(msg, "[g] #%u: YOU: %s", groupnumber, line); new_lines(msg); } else { char msg[128]; sprintf(msg, "[i] could not send message to group no. %u: %i", groupnumber, res); new_lines(msg); } } } else new_lines("[i] invalid input: neither command nor in conversation"); } }