Ejemplo n.º 1
0
/* Use these functions to bootstrap the client.
 * Sends a get nodes request to the given node with ip port and public_key.
 */
void tox_bootstrap_from_ip(Tox *tox, tox_IP_Port _ip_port, uint8_t *public_key)
{
    Messenger *m = tox;
    IP_Port ip_port;
    memcpy(&ip_port, &_ip_port, sizeof(IP_Port));
    DHT_bootstrap(m->dht, ip_port, public_key);
}
Ejemplo n.º 2
0
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;   
}
Ejemplo n.º 3
0
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;
}
Ejemplo n.º 4
0
int handle_LANdiscovery(uint8_t *packet, uint32_t length, IP_Port source)
{
    if (LAN_ip(source.ip) == -1)
        return 1;
    if (length != crypto_box_PUBLICKEYBYTES + 1)
        return 1;
    DHT_bootstrap(source, packet + 1);
    return 0;
}
Ejemplo n.º 5
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;
}
Ejemplo n.º 6
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;
}
Ejemplo n.º 7
0
static int handle_LANdiscovery(void *object, IP_Port source, uint8_t *packet, uint32_t length)
{
    DHT *dht = object;

    if (LAN_ip(source.ip) == -1)
        return 1;

    if (length != crypto_box_PUBLICKEYBYTES + 1)
        return 1;

    DHT_bootstrap(dht, source, packet + 1);
    return 0;
}
Ejemplo n.º 8
0
/* 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;
}
Ejemplo n.º 9
0
static int handle_LANdiscovery(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata)
{
    DHT *dht = (DHT *)object;

    if (LAN_ip(source.ip) == -1) {
        return 1;
    }

    if (length != CRYPTO_PUBLIC_KEY_SIZE + 1) {
        return 1;
    }

    DHT_bootstrap(dht, source, packet + 1);
    return 0;
}
Ejemplo n.º 10
0
static int handle_LANdiscovery(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata)
{
    DHT *dht = (DHT *)object;

    char ip_str[IP_NTOA_LEN] = { 0 };
    ip_ntoa(&source.ip, ip_str, sizeof(ip_str));

    if (ip_is_lan(source.ip) == -1) {
        return 1;
    }

    if (length != CRYPTO_PUBLIC_KEY_SIZE + 1) {
        return 1;
    }

    DHT_bootstrap(dht, source, packet + 1);
    return 0;
}
Ejemplo n.º 11
0
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);
}
/* This unction connects to all specified servers
and connect to them.
returns 1 if the connection to the DHT is up
returns -1 if all attempts failed
*/
int connect_to_servers(DHT *dht, struct server_info_s *info)
{
    int i;
    int c;

    for (i = 0; i < 32; ++i) {
        if (info[i].valid) {
            /* Actual bootstrapping code goes here */
            //puts("Calling DHT_bootstrap");
            DHT_bootstrap(dht, info[i].conn, info[i].bs_pk);
        }
    }

    /* Check if we're connected to the DHT */
    for (c = 0; c != 100; ++c) {
        usleep(10000);

        if (DHT_isconnected(dht)) {
            //puts("Connected");
            return 1;
            break;
        }

        if (DHT_isconnected(dht) == 0 && c == 99) {
            //puts("Not connected");
            return -1;
            break;
        }

        do_DHT(dht);

        networking_poll(dht->c->lossless_udp->net);
    }

    /* This probably never happens */
    return 0;
}
Ejemplo n.º 13
0
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;
}
Ejemplo n.º 14
0
static void execute(ToxWindow* self, char* cmd) {

  // quit/exit: Exit program.
  if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit")) {
    endwin();
    exit(0);
  }
  else if(!strncmp(cmd, "connect ", strlen("connect "))) {
    char* ip;
    char* port;
    char* key;
    IP_Port dht;

    ip = strchr(cmd, ' ');
    if(ip == NULL) {
      return;
    }

    ip++;

    port = strchr(ip, ' ');
    if(port == NULL) {
      return;
    }

    port[0] = 0;
    port++;

    key = strchr(port, ' ');
    if(key == NULL) {
      return;
    }

    key[0] = 0;
    key++;

    if(atoi(port) == 0) {
      return;
    }

    wprintw(self->window, "ip=%s, port=%s, key=%s\n", ip, port, key);

    dht.port = htons(atoi(port));

    int resolved_address = resolve_addr(ip);
    if (resolved_address == -1) {
      return;
    }

    dht.ip.i = resolved_address;
    DHT_bootstrap(dht, hex_string_to_bin(key));
  }
  else if(!strncmp(cmd, "add ", strlen("add "))) {
    char* id;
    char* msg;
    int num;

    id = strchr(cmd, ' ');

    if(id == NULL) {
      return;
    }

    id++;

    msg = strchr(id, ' ');
    if(msg == NULL) {
      return;
    }

    msg[0] = 0;
    msg++;

    num = m_addfriend((uint8_t*) id, (uint8_t*) msg, strlen(msg)+1);
    wprintw(self->window, "Friend added as %d.\n", num);
  }
  else if(!strncmp(cmd, "status ", strlen("status "))) {
    char* msg;

    msg = strchr(cmd, ' ');
    if(msg == NULL) {
      return;
    }

    msg++;
    m_set_userstatus((uint8_t*) msg, strlen(msg)+1);
    wprintw(self->window, "Status set to: %s.\n", msg);
  }
  else if(!strncmp(cmd, "nick ", strlen("nick "))) {
    char* nick;

    nick = strchr(cmd, ' ');
    if(nick == NULL) {
      return;
    }

    nick++;
    setname((uint8_t*) nick, strlen(nick)+1);
    wprintw(self->window, "Nickname set to: %s.\n", nick);
  }
  else if(!strcmp(cmd, "myid")) {
    // XXX: Clean this up
    char idstring0[200];
    char idstring1[32][5];
    char idstring2[32][5];
    uint32_t i;

    for(i = 0; i < 32; i++) {
      if(self_public_key[i] < 16)
	strcpy(idstring1[i], "0");
      else 
	strcpy(idstring1[i], "");

      sprintf(idstring2[i], "%hhX", self_public_key[i]);
    }
    
    for (i=0; i<32; i++) {
      strcat(idstring0, idstring1[i]);
      strcat(idstring0, idstring2[i]);
    }

    wprintw(self->window, "%s\n", idstring0);
  }
  else if(!strncmp(cmd, "accept ", strlen("accept "))) {
    char* id;
    int num;

    id = strchr(cmd, ' ');
    if(id == NULL) {
      return;
    }
    id++;
   
    num = atoi(id);
    if(num >= num_requests) {
      return;
    }

    num = m_addfriend_norequest(pending_requests[num]);
    wprintw(self->window, "Friend accepted as: %d.\n", num);
  }
  else if(!strncmp(cmd, "msg ", strlen("msg "))) {
    char* id;
    char* msg;

    id = strchr(cmd, ' ');

    if(id == NULL) {
      return;
    }

    id++;

    msg = strchr(id, ' ');
    if(msg == NULL) {
      return;
    }

    msg[0] = 0;
    msg++;

    if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) != 1) {
      wprintw(self->window, "Error occurred while sending message.\n");
    }
    else {
      wprintw(self->window, "Message successfully sent.\n");
    }
  }
}
Ejemplo n.º 15
0
int main(int argc, char *argv[])
{
    if (argc < 4) {
        printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]);
        exit(0);
    }
    int c;
    int on = 0;
    initMessenger();
    //if keyfiles exist
    if(argc > 4){
        if(strncmp(argv[4], "nokey", 6) < 0){
        //load_key();
        }
    } else {
        load_key();
    }
    m_callback_friendrequest(print_request);
    m_callback_friendmessage(print_message);
    m_callback_namechange(print_nickchange);
    m_callback_userstatus(print_statuschange);
    char idstring0[200];
    char idstring1[32][5];
    char idstring2[32][5];
    uint32_t i;
    for(i = 0; i < 32; i++)
    {
        if(self_public_key[i] < 16) {
            strcpy(idstring1[i],"0");
        } else {
            strcpy(idstring1[i], "");
        }
        sprintf(idstring2[i], "%hhX",self_public_key[i]);
    }
    strcpy(idstring0,"[i] your ID: ");
    for(i=0; i<32; i++) {
        strcat(idstring0,idstring1[i]);
        strcat(idstring0,idstring2[i]);
    }
    initscr();
    noecho();
    raw();
    getmaxyx(stdscr,y,x);
    new_lines(idstring0);
    new_lines("[i] commands: /f ID (to add friend), /m friendnumber message  (to send message), /s status (to change status), /n nick (to change nickname), /q (to quit)");
    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 != -1) {
        bootstrap_ip_port.ip.i = resolved_address;
    } else {
        exit(1);
    }
    DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
    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(lines, line);
            strcpy(line, "");
        } else if (c == 127) {
            line[strlen(line) - 1] = '\0';
        } else if (isalnum(c) || ispunct(c) || c == ' ') {
            strcpy(line, appender(line, (char) c));
        }
    }
    endwin();
    return 0;
}
Ejemplo n.º 16
0
int main(int argc, char *argv[])
{
    if (argc < 4) {
        printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]);
        exit(0);
    }
    if (initMessenger() == -1) {
        printf("initMessenger failed");
        exit(0);
    }
    if (argc > 4) {
        if(strncmp(argv[4], "nokey", 6) < 0) {
        }
    } else {
        load_key();
    }

    int nameloaded = 0;
    int statusloaded = 0;

    FILE* name_file = NULL;
    name_file = fopen("namefile.txt", "r");
    if(name_file) {
        uint8_t name[MAX_NAME_LENGTH];
        while (fgets(line, MAX_NAME_LENGTH, name_file) != NULL) {
            sscanf(line, "%s", (char*)name);
        }
        setname(name, strlen((char*)name)+1);
        nameloaded = 1;
        printf("%s\n", name);
    }
    fclose(name_file);

    FILE* status_file = NULL;
    status_file = fopen("statusfile.txt", "r");
    if(status_file) {
        uint8_t status[MAX_USERSTATUS_LENGTH];
        while (fgets(line, MAX_USERSTATUS_LENGTH, status_file) != NULL) {
            sscanf(line, "%s", (char*)status);
        }
        m_set_userstatus(status, strlen((char*)status)+1);
        statusloaded = 1;
        printf("%s\n", status);
    }
    fclose(status_file);

    m_callback_friendrequest(print_request);
    m_callback_friendmessage(print_message);
    m_callback_namechange(print_nickchange);
    m_callback_userstatus(print_statuschange);
    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(users_id,"[i] your ID: ");
    int j;
    for (j = 0; j < PUB_KEY_BYTES; j++) {
        strcat(users_id,idstring1[j]);
        strcat(users_id,idstring2[j]);
    }

    do_header();
    
    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);
    
    DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));

    int c;
    int on = 0;

    _beginthread(get_input, 0, NULL);

    if (nameloaded == 1) {
        printf("\nNickname automatically loaded");
        printf("\n---------------------------------");
    }

    if (statusloaded == 1) {
        printf("\nStatus automatically loaded");
        printf("\n---------------------------------");
    }

    while(1) {
        if (on == 1 && DHT_isconnected() == -1) {
            printf("\n---------------------------------");
            printf("\n[i] Disconnected from the DHT");
            printf("\n---------------------------------\n\n");
            on = 0;
        }
        if (on == 0 && DHT_isconnected()) {
            printf("\n[i] Connected to DHT");
            printf("\n---------------------------------\n\n");
            on = 1;
        }
        doMessenger();
        Sleep(1);
    }
    return 0;
}
Ejemplo n.º 17
0
int main(int argc, char *argv[])
{
    if (argc < 4) {
        printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]);
        exit(0);
    }

    if (initMessenger() == -1) {
        printf("initMessenger failed");
        exit(0);
    }

    
    if (argc > 4) {
        if(strncmp(argv[4], "nokey", 6) < 0) {
            //nothing
        } else {
            load_key();
        }
    }
    
    m_callback_friendrequest(print_request);
    m_callback_friendmessage(print_message);
    m_callback_namechange(print_nickchange);
    m_callback_userstatus(print_statuschange);

    system("cls");

    char idstring0[200];
    char idstring1[32][5];
    char idstring2[32][5];
    uint32_t i;
    for(i = 0; i < 32; i++)
    {
        if(self_public_key[i] < 16)
            strcpy(idstring1[i],"0");
        else 
            strcpy(idstring1[i], "");
        sprintf(idstring2[i], "%hhX",self_public_key[i]);
    }
    strcpy(idstring0,"\n[i] your ID: ");
    for (i=0; i<32; i++) {
        strcat(idstring0,idstring1[i]);
        strcat(idstring0,idstring2[i]);
    }

    printf(idstring0);
    do_header();
    

    IP_Port bootstrap_ip_port;
    bootstrap_ip_port.port = htons(atoi(argv[2]));
    int resolved_address = resolve_addr(argv[1]);
    if (resolved_address != -1)
        bootstrap_ip_port.ip.i = resolved_address;
    else 
        exit(1);
    
    DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));


    int c;
    int on = 0;

    _beginthread(get_input, 0, NULL);

    while(1) {
        if (on == 0 && DHT_isconnected()) {
            printf("\n[i] connected to DHT\n\n");
            on = 1;
        }

        doMessenger();
    }

    return 0;
}
Ejemplo n.º 18
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);
    }

    m = initMessenger();

    if ( !m ) {
        fputs("Failed to allocate messenger datastructure\n", stderr);
        exit(0);
    }

    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(m->dht, 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", Messenger_load(m, buffer, read));
        fclose(file);

    }

    m_callback_friendrequest(m, print_request, NULL);
    m_callback_friendmessage(m, print_message, NULL);

    printf("OUR ID: ");
    uint32_t i;
    uint8_t address[FRIEND_ADDRESS_SIZE];
    getaddress(m, address);

    for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) {
        if (address[i] < 16)
            printf("0");

        printf("%hhX", address[i]);
    }

    setname(m, (uint8_t *)"Anon", 5);

    char temp_id[128];
    printf("\nEnter the address of the friend you wish to add (38 bytes HEX format):\n");

    if (scanf("%s", temp_id) != 1) {
        return 1;
    }

    int num = m_addfriend(m, hex_string_to_bin(temp_id), (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"));

    perror("Initialization");

    while (1) {
        uint8_t name[128];
        getname(m, num, name);
        printf("%s\n", name);

        m_sendmessage(m, num, (uint8_t *)"Test", 5);
        doMessenger(m);
        c_sleep(30);
        FILE *file = fopen("Save.bak", "wb");

        if ( file == NULL ) {
            return 1;
        }

        uint8_t *buffer = malloc(Messenger_size(m));
        Messenger_save(m, buffer);
        size_t write_result = fwrite(buffer, 1, Messenger_size(m), file);

        if (write_result < Messenger_size(m)) {
            return 1;
        }

        free(buffer);
        fclose(file);
    }

    cleanupMessenger(m);
}
Ejemplo n.º 19
0
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;   
}
Ejemplo n.º 20
0
/* Use these functions to bootstrap the client.
 * Sends a get nodes request to the given node with ip port and public_key.
 */
void tox_bootstrap_from_ip(void *tox, IP_Port ip_port, uint8_t *public_key)
{
    Messenger *m = tox;
    DHT_bootstrap(m->dht, ip_port, public_key);
}
Ejemplo n.º 21
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);
    }  
}
Ejemplo n.º 22
0
int main(int argc, char *argv[])
{
    if (argc < 4) {
        printf("usage %s ip port filename(of file to send)\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]);
    }
    printf("\n");
    
    memcpy(self_client_id, self_public_key, 32);
    
    char temp_id[128];
    printf("Enter the client_id of the friend to connect to (32 bytes HEX format):\n");
    scanf("%s", temp_id);
    
    uint8_t friend_id[32];
    memcpy(friend_id, hex_string_to_bin(temp_id), 32);
    
    /* memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); */
    

    DHT_addfriend(friend_id);
    IP_Port friend_ip;
    int connection = -1;
    int inconnection = -1;
    
    uint8_t acceptedfriend_public_key[crypto_box_PUBLICKEYBYTES];
    int friendrequest = -1;
    uint8_t request_data[512];
    
    /* initialize networking
     * bind to ip 0.0.0.0:PORT */
    IP ip;
    ip.i = 0;
    init_networking(ip, PORT);
    initNetCrypto();
    
    perror("Initialization");
    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);
    
    IP_Port ip_port;
    uint8_t data[MAX_UDP_PACKET_SIZE];
    uint32_t length;
    
    uint8_t buffer1[128];
    int read1 = 0;
    uint8_t buffer2[128];
    int read2 = 0;
    FILE *file1 = fopen(argv[3], "rb");
    if ( file1==NULL ){printf("Error opening file.\n");return 1;}
    FILE *file2 = fopen("received.txt", "wb");
    if ( file2==NULL ){return 1;}
    read1 = fread(buffer1, 1, 128, file1);
    
    while(1) {
        while(receivepacket(&ip_port, data, &length) != -1) {
            if(rand() % 3 != 1) { /* simulate packet loss */
                if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) {
                    /* if packet is not recognized */
                    printf("Received unhandled packet with length: %u\n", length);
                } else {
                    printf("Received handled packet with length: %u\n", length);
                }
            }
        }
        friend_ip = DHT_getfriendip(friend_id);
        if(friend_ip.ip.i != 0) {
            if(connection == -1 && friendrequest == -1) {
                printf("Sending friend request to peer:");
                printip(friend_ip);
                friendrequest = send_friendrequest(friend_id, friend_ip,(uint8_t *) "Hello World", 12);
                /* connection = crypto_connect((uint8_t *)friend_id, friend_ip); */
                /* connection = new_connection(friend_ip); */
            }
            if(check_friendrequest(friendrequest) == 1) {
                printf("Started connecting to friend:");
                connection = crypto_connect(friend_id, friend_ip);
            }
        }
        if(inconnection == -1) {
            uint8_t secret_nonce[crypto_box_NONCEBYTES];
            uint8_t public_key[crypto_box_PUBLICKEYBYTES];
            uint8_t session_key[crypto_box_PUBLICKEYBYTES];
            inconnection = crypto_inbound(public_key, secret_nonce, session_key);
            inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key);
            /* inconnection = incoming_connection(); */
            if(inconnection != -1) {
                printf("Someone connected to us:\n");
               /* printip(connection_ip(inconnection)); */
            }
        }
        if(handle_friendrequest(acceptedfriend_public_key, request_data) > 1) {
            printf("RECIEVED FRIEND REQUEST: %s\n", request_data);
        }

        /* if someone connected to us write what he sends to a file
         * also send him our file. */
        if(inconnection != -1) {
            if(write_cryptpacket(inconnection, buffer1, read1)) {
                printf("Wrote data1.\n");
                read1 = fread(buffer1, 1, 128, file1);
            }
            read2 = read_cryptpacket(inconnection, buffer2);
            if(read2 != 0) {
                printf("Received data1.\n");
                if(!fwrite(buffer2, read2, 1, file2)) {
                        printf("file write error1\n");
                }
                if(read2 < 128) {
                    printf("Closed file1 %u\n", read2);
                    fclose(file2);
                }
            }
            /* if buffer is empty and the connection timed out. */
            else if(is_cryptoconnected(inconnection) == 4) {
                crypto_kill(inconnection);
            }
        }
        /* if we are connected to a friend send him data from the file.
         * also put what he sends us in a file. */
        if(is_cryptoconnected(connection) >= 3)  {
            if(write_cryptpacket(0, buffer1, read1)) {
                printf("Wrote data2.\n");
                read1 = fread(buffer1, 1, 128, file1);
            }
            read2 = read_cryptpacket(0, buffer2);
            if(read2 != 0) {
                printf("Received data2.\n");
                if(!fwrite(buffer2, read2, 1, file2)) {
                        printf("file write error2\n");
                }
                if(read2 < 128) {
                    printf("Closed file2 %u\n", read2);
                    fclose(file2);
                }
            }
            /* if buffer is empty and the connection timed out. */
            else if(is_cryptoconnected(connection) == 4) {
                crypto_kill(connection);
            }
        }
        doDHT();
        doLossless_UDP();
        doNetCrypto();
        /*print_clientlist();
         *print_friendlist();
         *c_sleep(300); */
        c_sleep(1);
    }
    
    shutdown_networking();
    return 0;   
}
Ejemplo n.º 23
0
static void execute(ToxWindow* self, char* cmd) {

  if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit") || !strcmp(cmd, "q")) {
    endwin();
    exit(0);
  }
  else if(!strncmp(cmd, "connect ", strlen("connect "))) {
    char* ip;
    char* port;
    char* key;
    IP_Port dht;

    ip = strchr(cmd, ' ');
    if(ip == NULL) {
      wprintw(self->window, "Invalid syntax.\n");
      return;
    }
    ip++;

    port = strchr(ip, ' ');
    if(port == NULL) {
      wprintw(self->window, "Invalid syntax.\n");
      return;
    }
    port[0] = 0;
    port++;

    key = strchr(port, ' ');
    if(key == NULL) {
      wprintw(self->window, "Invalid syntax.\n");
      return;
    }
    key[0] = 0;
    key++;

    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;
    DHT_bootstrap(dht, hex_string_to_bin(key));
  }
  else if(!strncmp(cmd, "add ", strlen("add "))) {
    uint8_t id_bin[32];
    size_t i;
    char xx[3];
    uint32_t x;

    char* id;
    char* msg;
    int num;

    id = strchr(cmd, ' ');
    if(id == NULL) {
      wprintw(self->window, "Invalid syntax.\n");
      return;
    }
    id++;

    msg = strchr(id, ' ');
    if(msg != NULL) {
      msg[0] = 0;
      msg++;
    }
    else msg = "";

    if(strlen(id) != 2*32) {
      wprintw(self->window, "Invalid ID length.\n");
      return;
    }

    for(i=0; i<32; i++) {
      xx[0] = id[2*i];
      xx[1] = id[2*i+1];
      xx[2] = '\0';

      if(sscanf(xx, "%02x", &x) != 1) {
        wprintw(self->window, "Invalid ID.\n");
        return;
      }

      id_bin[i] = x;
    }

    num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1);
    switch (num) {
    case -1: 
      wprintw(self->window, "Message is too long.\n");
      break;
    case -2:
      wprintw(self->window, "Please add a message to your request.\n");
      break;
    case -3:
      wprintw(self->window, "That appears to be your own ID.\n");
      break;
    case -4:
      wprintw(self->window, "Friend request already sent.\n");
      break;
    case -5:
      wprintw(self->window, "[i] Undefined error when adding friend.\n");
      break; 
    default:
      wprintw(self->window, "Friend added as %d.\n", num);
      on_friendadded(num);
      break;
    }
  }

  else if(!strcmp(cmd, "help")) {
	  print_usage(self);
  }
  else if(!strncmp(cmd, "status ", strlen("status "))) {
    char* msg;

    msg = strchr(cmd, ' ');
    if(msg == NULL) {
      wprintw(self->window, "Invalid syntax.\n");
      return;
    }
    msg++;

    m_set_userstatus((uint8_t*) msg, strlen(msg)+1);
    wprintw(self->window, "Status set to: %s\n", msg);
  }
  else if(!strncmp(cmd, "nick ", strlen("nick "))) {
    char* nick;

    nick = strchr(cmd, ' ');
    if(nick == NULL) {
      return;
    }
    nick++;

    setname((uint8_t*) nick, strlen(nick)+1);
    wprintw(self->window, "Nickname set to: %s.\n", nick);
  }
  else if(!strcmp(cmd, "myid")) {
    char id[32*2 + 1] = {0};
    size_t i;

    for(i=0; i<32; i++) {
      char xx[3];
      snprintf(xx, sizeof(xx), "%02x",  self_public_key[i] & 0xff);
      strcat(id, xx);
    }
    
    wprintw(self->window, "%s\n", id);
  }
  else if(!strncmp(cmd, "accept ", strlen("accept "))) {
    char* id;
    int num;

    id = strchr(cmd, ' ');
    if(id == NULL) {
      wprintw(self->window, "Invalid syntax.\n");
      return;
    }
    id++;

    num = atoi(id);
    if(num >= num_requests) {
      wprintw(self->window, "Invalid syntax.\n");
      return;
    }

    num = m_addfriend_norequest(pending_requests[num]);

    if(num == -1) {
      wprintw(self->window, "Failed to add friend.\n");
    }
    else {
      wprintw(self->window, "Friend accepted as: %d.\n", num);
      on_friendadded(num);
    }
  }
  else if(!strncmp(cmd, "msg ", strlen("msg "))) {
    char* id;
    char* msg;

    id = strchr(cmd, ' ');

    if(id == NULL) {
      wprintw(self->window, "Invalid syntax.\n");
      return;
    }
    id++;

    msg = strchr(id, ' ');
    if(msg == NULL) {
      wprintw(self->window, "Invalid syntax.\n");
      return;
    }
    msg[0] = 0;
    msg++;

    if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) < 0) {
      wprintw(self->window, "Error occurred while sending message.\n");
    }
    else {
      wprintw(self->window, "Message successfully sent.\n");
    }
  }
  else {
    wprintw(self->window, "Invalid syntax.\n");
  }
}