Example #1
0
int parent_friend_request(void)
{
    char *message = "Watson, come here, I need you.";
    int len = strlen(message);
    int i = 0;

    fputs("Sending child request.", stdout);
    fflush(stdout);

    m_addfriend(child_id, (uint8_t *)message, len);

    /* wait on the status change */
    for(i = 0; i < WAIT_COUNT; i++) {
        do_tox();
        if(request_flags & FIRST_FLAG)
            break;
        fputs(".", stdout);
        fflush(stdout);
        c_sleep(WAIT_TIME);
    }

    if(!(request_flags & FIRST_FLAG)) {
        fputs("\nfriends_test: The child took to long to respond!\n"
              "Friend requests may be broken, failing build!\n", stderr);
        kill(child_pid, SIGKILL);
        return -1;
    }

    return 0;
}
Example #2
0
int parent_wait_for_message(void)
{
    int i = 0;

    fputs("Parent waiting for message.", stdout);
    fflush(stdout);

    for(i = 0; i < WAIT_COUNT; i++) {
        do_tox();
        if(request_flags & SECOND_FLAG)
            break;
        fputs(".", stdout);
        fflush(stdout);
        c_sleep(WAIT_TIME);
    }

    if(!(request_flags & SECOND_FLAG)) {
        fputs("\nParent hasn't recieved the message yet!\n"
              "Messaging may be broken, failing the build!\n", stderr);
        kill(child_pid, SIGKILL);
        return -1;
    }

    return 0;
}
Example #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;
}
Example #4
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;   
}
Example #5
0
/**
 * Iterate helper
 */
static int iterate_tox(Tox *bootstrap, Tox *Alice, Tox *Bob)
{
    c_sleep(100);
    tox_iterate(bootstrap, NULL);
    tox_iterate(Alice, NULL);
    tox_iterate(Bob, NULL);

    return MIN(tox_iteration_interval(Alice), tox_iteration_interval(Bob));
}
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;
}
Example #7
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 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;
}
    TERMINATE_SCOPE()



    /*************************************************************************************************
     * Other flows
     */

    /*
     * Call and reject
     */
    {
        int step = 0;
        int running = 1;

        while (running) {
            tox_do(bootstrap_node);
            tox_do(Alice);
            tox_do(Bob);

            switch ( step ) {
                case 0: /* Alice */
                    printf("Alice is calling...\n");
                    toxav_call(status_control.Alice.av, 0, TypeAudio, 10);
                    step++;
                    break;
                    \

                case 1: /* Bob */
                    if (status_control.Bob.status == Ringing) {
                        printf("Bob rejects...\n");
                        toxav_reject(status_control.Bob.av, "Who likes D's anyway?");
                        step++;
                    }

                    break;

                case 2:  /* Wait for Both to have status ended */
                    if (status_control.Alice.status == Rejected && status_control.Bob.status == Ended) running = 0;

                    break;
            }

            c_sleep(20);
        }

        printf("\n");
    }
Example #10
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;
}
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;   
}
Example #12
0
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);
}
Example #14
0
void *in_thread_call (void *arg)
{
#define call_print(call, what, args...) printf("[%d] " what "\n", call, ##args)

    ACall *this_call = arg;
    uint64_t start = 0;
    int step = 0;
    int call_idx;

    const int frame_size = (av_DefaultSettings.audio_sample_rate * av_DefaultSettings.audio_frame_duration / 1000);
    int16_t sample_payload[frame_size];
    randombytes((uint8_t *)sample_payload, sizeof(int16_t) * frame_size);

    uint8_t prepared_payload[RTP_PAYLOAD_SIZE];

    register_callbacks(this_call->Caller.av, &status_control);
    register_callbacks(this_call->Callee.av, arg);

    /* NOTE: CALLEE WILL ALWAHYS NEED CALL_IDX == 0 */
    pthread_mutex_lock(&muhmutex);

    while (call_running[this_call->idx]) {

        pthread_mutex_unlock(&muhmutex);

        switch ( step ) {
            case 0: /* CALLER */
                toxav_call(this_call->Caller.av, &call_idx, this_call->Callee.id, &av_DefaultSettings, 10);
                call_print(call_idx, "Calling ...");
                step++;
                break;

            case 1: /* CALLEE */
                pthread_mutex_lock(&muhmutex);

                if (this_call->Caller.status == Ringing) {
                    call_print(call_idx, "Callee answers ...");
                    pthread_mutex_unlock(&muhmutex);
                    toxav_answer(this_call->Callee.av, 0, &av_DefaultSettings);
                    step++;
                    start = time(NULL);
                    pthread_mutex_lock(&muhmutex);
                }

                pthread_mutex_unlock(&muhmutex);
                break;

            case 2: /* Rtp transmission */
                pthread_mutex_lock(&muhmutex);

                if (this_call->Caller.status == InCall) { /* I think this is okay */
                    call_print(call_idx, "Sending rtp ...");
                    pthread_mutex_unlock(&muhmutex);

                    c_sleep(1000); /* We have race condition here */
                    toxav_prepare_transmission(this_call->Callee.av, 0, 1);
                    toxav_prepare_transmission(this_call->Caller.av, call_idx, 1);

                    int payload_size = toxav_prepare_audio_frame(this_call->Caller.av, call_idx, prepared_payload, RTP_PAYLOAD_SIZE,
                                       sample_payload, frame_size);

                    if ( payload_size < 0 ) {
                        ck_assert_msg ( 0, "Failed to encode payload" );
                    }


                    while (time(NULL) - start < 10) { /* 10 seconds */
                        /* Both send */
                        toxav_send_audio(this_call->Caller.av, call_idx, prepared_payload, payload_size);

                        toxav_send_audio(this_call->Callee.av, 0, prepared_payload, payload_size);

                        /* Both receive */
                        int16_t storage[RTP_PAYLOAD_SIZE];
                        int recved;

                        c_sleep(20);
                    }

                    step++; /* This terminates the loop */

                    pthread_mutex_lock(&muhmutex);
                    toxav_kill_transmission(this_call->Callee.av, 0);
                    toxav_kill_transmission(this_call->Caller.av, call_idx);
                    pthread_mutex_unlock(&muhmutex);

                    /* Call over CALLER hangs up */
                    toxav_hangup(this_call->Caller.av, call_idx);
                    call_print(call_idx, "Hanging up ...");

                    pthread_mutex_lock(&muhmutex);
                }

                pthread_mutex_unlock(&muhmutex);
                break;

            case 3: /* Wait for Both to have status ended */
                pthread_mutex_lock(&muhmutex);

                if (this_call->Caller.status == Ended) {
                    pthread_mutex_unlock(&muhmutex);
                    c_sleep(1000); /* race condition */
                    pthread_mutex_lock(&muhmutex);
                    this_call->Callee.status = Ended;
                    call_running[this_call->idx] = 0;
                }

                pthread_mutex_unlock(&muhmutex);

                break;

        }

        c_sleep(20);

        pthread_mutex_lock(&muhmutex);
    }

    pthread_mutex_unlock(&muhmutex);
    call_print(call_idx, "Call ended successfully!");
    pthread_exit(NULL);
}
Example #15
0
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;
}
Example #16
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;   
}
Example #17
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);
    }  
}
Example #18
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;
}
int main(int argc, char *argv[])
{
    if (argc < 2) 
    {
        printf("usage: %s filename\n", argv[0]);
        exit(0);
    }
    
    char buffer[128];
    int read;
    
    FILE *file = fopen(argv[1], "wb");
    if ( file==NULL ){return 1;}
    
    
    //initialize networking
    //bind to ip 0.0.0.0:PORT
    IP ip;
    ip.i = 0;
    init_networking(ip, PORT);
    perror("Initialization");
    
    int connection;
    uint64_t timer = current_time();
    
    
    while(1)
    {
        Lossless_UDP();
        connection = incoming_connection();
        if(connection != -1)
        {
            if(is_connected(connection) == 2)
            {
                printf("Recieved the connection.\n");
                
            }
            break;
        }
        c_sleep(1);
    }
    
    timer = current_time();
    
    while(1)
    {
        //printconnection(0);
        Lossless_UDP();
        if(is_connected(connection) >= 2)
        {
            read = read_packet(connection, buffer);
            if(read != 0)
            {
               // printf("Recieved data.\n");
                if(!fwrite(buffer, read, 1, file))
                {
                        printf("file write error\n");
                }
            }
        }
        else
        {
            printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer));
            fclose(file);
            return 1;
        }
        c_sleep(1);
    }
        
    return 0;
}
Example #20
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;   
}
int main(int argc, char *argv[])
{
    /* 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);

    if (argc < argvoffset + 4) {
        printf("Usage: %s [--ipv4|--ipv6] ip port filename\n", argv[0]);
        exit(0);
    }

    uint8_t buffer[512];
    int read;

    FILE *file = fopen(argv[argvoffset + 3], "rb");

    if (file == NULL) {
        printf("Failed to open file \"%s\".\n", argv[argvoffset + 3]);
        return 1;
    }


    /* initialize networking */
    /* bind to ip 0.0.0.0:PORT */
    IP ip;
    ip_init(&ip, ipv6enabled);

    Lossless_UDP *ludp = new_lossless_udp(new_networking(ip, PORT));
    perror("Initialization");

    IP_Port serverip;
    ip_init(&serverip.ip, ipv6enabled);

    if (!addr_resolve(argv[argvoffset + 1], &serverip.ip, NULL)) {
        printf("Failed to convert \"%s\" into an IP address.\n", argv[argvoffset + 1]);
        return 1;
    }

    serverip.port = htons(atoi(argv[argvoffset + 2]));
    printip(serverip);

    int connection = new_connection(ludp, serverip);
    uint64_t timer = current_time();

    while (1) {
        /* printconnection(connection); */
        networking_poll(ludp->net);
        do_lossless_udp(ludp);

        if (is_connected(ludp, connection) == 3) {
            printf("Connecting took: %llu us\n", (unsigned long long)(current_time() - timer));
            break;
        }

        if (is_connected(ludp, connection) == 0) {
            printf("Connection timeout after: %llu us\n", (unsigned long long)(current_time() - timer));
            return 1;
        }

        c_sleep(1);
    }

    timer = current_time();


    /*read first part of file */
    read = fread(buffer, 1, 512, file);

    while (1) {
        /* printconnection(connection); */
        networking_poll(ludp->net);
        do_lossless_udp(ludp);

        if (is_connected(ludp, connection) == 3) {

            if (write_packet(ludp, connection, buffer, read)) {
                /* printf("Wrote data.\n"); */
                read = fread(buffer, 1, 512, file);

            }

            /* printf("%u\n", sendqueue(connection)); */
            if (sendqueue(ludp, connection) == 0) {
                if (read == 0) {
                    printf("Sent file successfully in: %llu us\n", (unsigned long long)(current_time() - timer));
                    break;
                }
            }
        } else {
            printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer));
            return 0;
        }

        /* c_sleep(1); */
    }

    return 0;
}
Example #22
0
int main(int argc, char *argv[])
{
    puts("=========== FRIENDS_TEST ===========");

    /* set up the global memory */
    parent_id = mmap(NULL, crypto_box_PUBLICKEYBYTES, PROT_READ | PROT_WRITE,
                        MAP_SHARED | MAP_ANONYMOUS, -1, 0);
    child_id = mmap(NULL, crypto_box_PUBLICKEYBYTES, PROT_READ | PROT_WRITE,
                        MAP_SHARED | MAP_ANONYMOUS, -1, 0);

    fputs("friends_test: Starting test...\n", stdout);
    if((child_pid = fork()) == 0) {
        /* child */
        int i = 0;
        char *message = "Y-yes Mr. Watson?";

        initMessenger();
        Messenger_save(child_id);
        msync(child_id, crypto_box_PUBLICKEYBYTES, MS_SYNC);

        m_callback_friendrequest(child_got_request);
        m_callback_userstatus(child_got_statuschange);

        /* wait on the friend request */
       while(!(request_flags & FIRST_FLAG))
            do_tox();

        /* wait for the status change */
        while(!(request_flags & SECOND_FLAG))
            do_tox();

        for(i = 0; i < 6; i++) {
            /* send the message six times, just to be sure */
            m_sendmessage(0, (uint8_t *)message, strlen(message));
            do_tox();
        }

        return 0;
    }

    /* parent */
    if(atexit(cleanup) != 0) {
        fputs("friends_test: atexit() failed!\nFailing build...\n", stderr);
        kill(child_pid, SIGKILL);
        return -1;
    }

    msync(parent_id, crypto_box_PUBLICKEYBYTES, MS_SYNC);
    m_callback_userstatus(parent_confirm_status);
    m_callback_friendmessage(parent_confirm_message);

    /* hacky way to give the child time to set up */
    c_sleep(50);

    initMessenger();
    Messenger_save(parent_id);

    if(parent_friend_request() == -1)
        return -1;

    if(parent_wait_for_message() == -1)
        return -1;

    wait(NULL);
    fputs("friends_test: Build passed!\n", stdout);
    return 0;
}
Example #23
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);
}
Example #24
0
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]);
    }
}
Example #25
0
int main_udp(int argc, char **argv)
{
    UINT32 this_tcid;
    UINT32 this_comm;
    UINT32 this_rank;

    task_brd_default_init(argc, argv);
    if(EC_FALSE == task_brd_default_check_validity())
    {
        sys_log(LOGSTDOUT, "error:main_trans: validity checking failed\n");
        task_brd_default_abort();
        return (-1);
    }

    this_tcid = task_brd_default_get_tcid();
    this_comm = task_brd_default_get_comm();
    this_rank = task_brd_default_get_rank();

    if (EC_TRUE == task_brd_check_is_dbg_tcid(this_tcid) && CMPI_DBG_RANK == this_rank)
    {
        do_cmd_default();
    }
    else if (EC_TRUE == task_brd_check_is_monitor_tcid(this_tcid) && CMPI_MON_RANK == this_rank)
    {
        void * mod_mgr_def;

        mod_mgr_def = mod_mgr_new(CMPI_ERROR_MODI, LOAD_BALANCING_LOOP);
        mod_mgr_default_init(mod_mgr_def, CMPI_ANY_TCID, CMPI_ANY_RANK);

        //mod_mgr_excl(this_tcid, CMPI_ANY_COMM, this_rank, CMPI_ANY_MODI, mod_mgr_def);

        sys_log(LOGSTDOUT, "======================================================================\n");
        sys_log(LOGSTDOUT, "                       mod_mgr_default_init finished                  \n");
        sys_log(LOGSTDOUT, "======================================================================\n");
        mod_mgr_print(LOGSTDOUT, mod_mgr_def);

        mod_mgr_free(mod_mgr_def);

        do_slave_wait_default();
    }

    /*fwd rank entrance*/
    else if (c_ipv4_to_word("10.10.10.1") == this_tcid && CMPI_FWD_RANK == this_rank)
    {
        int sockfd;
        const char *mcast_ipaddr_str = "239.2.11.71";/*239.0.0.0бл239.255.255.255*/
        UINT32 mcast_ipaddr;
        UINT32 mcast_port = 8888;

        mcast_ipaddr = c_ipv4_to_word(mcast_ipaddr_str);

        if(EC_FALSE == csocket_start_udp_mcast_recver(mcast_ipaddr, mcast_port, &sockfd))
        {
            sys_log(LOGCONSOLE, "error:start udp server %s:%ld failed\n", mcast_ipaddr_str, mcast_port);
        }
        else
        {
            UINT8 data[256];
            UINT32 dlen;

            sys_log(LOGCONSOLE, "start udp server %s:%ld\n", mcast_ipaddr_str, mcast_port);
            for(;EC_TRUE == csocket_udp_mcast_recvfrom(sockfd, mcast_ipaddr, mcast_port, data, sizeof(data)/sizeof(data[0]), &dlen);)
            {
                sys_log(LOGCONSOLE, "[DEBUG] recv udp data: %.*s\n", dlen, (char *)data);
            }
            csocket_stop_udp_mcast_recver(sockfd, mcast_ipaddr);
        }
        do_slave_wait_default();
    }

    else if (c_ipv4_to_word("10.10.10.7") == this_tcid && CMPI_FWD_RANK == this_rank)
    {
        int sockfd;
        const char *mcast_ipaddr_str = "239.2.11.71";
        UINT32 mcast_ipaddr;
        UINT32 mcast_port = 8888;

        UINT32 loop;

        mcast_ipaddr = c_ipv4_to_word(mcast_ipaddr_str);

        csocket_start_udp_mcast_sender(mcast_ipaddr, mcast_port, &sockfd);
        for(loop = 0; loop < 5; loop ++)
        {
            UINT8  data[256];
            UINT32 dlen;

            snprintf((char *)data, sizeof(data)/sizeof(data[0]), "[loop %ld] hello world!", loop);
            dlen = strlen((char *)data);
            //dlen = sizeof(data)/sizeof(data[0]);
            if(EC_FALSE == csocket_udp_mcast_sendto(sockfd, mcast_ipaddr, mcast_port, data, dlen))
            {
                sys_log(LOGSTDOUT, "error:send udp data to %s:%ld failed\n", mcast_ipaddr_str, mcast_port);
                break;
            }
            sys_log(LOGCONSOLE, "send udp data: %.*s\n", dlen, (char *)data);
            c_sleep(5);
        }
        csocket_stop_udp_mcast_sender(sockfd, c_ipv4_to_word(mcast_ipaddr_str));

        do_slave_wait_default();
    }

    else if (CMPI_FWD_RANK == this_rank)
    {
        sys_log(LOGSTDOUT,"======================================================================\n");
        sys_log(LOGSTDOUT,"                taskc_mgr in (tcid %s, rank %ld)                     \n", c_word_to_ipv4(this_tcid), this_rank);
        super_show_work_client(task_brd_default_get_super(), LOGSTDOUT);/*debug only*/
        sys_log(LOGSTDOUT,"======================================================================\n");

        do_slave_wait_default();
    }

    /*work process*/
    else
    {
        do_slave_wait_default();
    }

    return (0);
}
Example #26
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;
}
Example #27
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;
}
int main(int argc, char *argv[])
{
    if (argc < 4) {
        printf("usage: %s ip port filename\n", argv[0]);
        exit(0);
    }
    
    uint8_t buffer[512];
    int read;
    
    FILE *file = fopen(argv[3], "rb");
    if (file == NULL)
      return 1;
    
    
    /* initialize networking */
    /* bind to ip 0.0.0.0:PORT */
    IP ip;
    ip.i = 0;
    init_networking(ip, PORT);
    perror("Initialization");
    IP_Port serverip;
    serverip.ip.i = inet_addr(argv[1]);
    serverip.port = htons(atoi(argv[2]));
    printip(serverip);
    int connection = new_connection(serverip);
    uint64_t timer = current_time();
    while (1) {
       /* printconnection(connection); */
        Lossless_UDP();
        if (is_connected(connection) == 3) {
            printf("Connecting took: %llu us\n", (unsigned long long)(current_time() - timer));
            break;
        }
        if (is_connected(connection) == 0) {
            printf("Connection timeout after: %llu us\n", (unsigned long long)(current_time() - timer));
            return 1;
        }
        c_sleep(1);
    }
    timer = current_time();
    
    
    /*read first part of file */
    read = fread(buffer, 1, 512, file);
    
    while (1) {
        /* printconnection(connection); */
        Lossless_UDP();
        if (is_connected(connection) == 3) {
            
            if (write_packet(connection, buffer, read)) {
               /* printf("Wrote data.\n"); */
                read = fread(buffer, 1, 512, file);

            }
            /* printf("%u\n", sendqueue(connection)); */
            if (sendqueue(connection) == 0) {
                if (read == 0) {
                    printf("Sent file successfully in: %llu us\n", (unsigned long long)(current_time() - timer));
                    break;
                }
            }
        }
        else {
            printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer));
            return 0;
        }
        /* c_sleep(1); */
    }
        
    return 0;
}
Example #29
0
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[])
{
    /* 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);

    /* with optional --ipvx, now it can be 1-4 arguments... */
    if ((argc != argvoffset + 2) && (argc != argvoffset + 4)) {
        printf("Usage: %s [--ipv4|--ipv6] ip port public_key (of the DHT bootstrap node)\n", argv[0]);
        printf("or\n");
        printf("       %s [--ipv4|--ipv6] Save.bak (to read Save.bak as state file)\n", argv[0]);
        exit(0);
    }

    m = initMessenger(ipv6enabled);

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

    if (argc == argvoffset + 4) {
        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(m->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);
        }
    } else {
        FILE *file = fopen(argv[argvoffset + 1], "rb");

        if ( file == NULL ) {
            printf("Failed to open \"%s\" - does it exist?\n", argv[argvoffset + 1]);
            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);
}