Example #1
0
/* Start a secure connection with other peer who has public_key and ip_port
   returns -1 if failure
   returns crypt_connection_id of the initialized connection if everything went well. */
int crypto_connect(uint8_t *public_key, IP_Port ip_port)
{
    uint32_t i;
    int id = getcryptconnection_id(public_key);
    if (id != -1) {
        IP_Port c_ip = connection_ip(crypto_connections[id].number);
        if(c_ip.ip.i == ip_port.ip.i && c_ip.port == ip_port.port)
            return -1;
    }
    for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
        if (crypto_connections[i].status == CONN_NO_CONNECTION) {
            int id = new_connection(ip_port);
            if (id == -1)
                return -1;
            crypto_connections[i].number = id;
            crypto_connections[i].status = CONN_HANDSHAKE_SENT;
            random_nonce(crypto_connections[i].recv_nonce);
            memcpy(crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES);
            crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key);

            if (send_cryptohandshake(id, public_key, crypto_connections[i].recv_nonce,
                                     crypto_connections[i].sessionpublic_key) == 1) {
                increment_nonce(crypto_connections[i].recv_nonce);
                return i;
            }
            return -1; /* this should never happen. */
        }
    }
    return -1;
}
Example #2
0
/* Start a secure connection with other peer who has public_key and ip_port.
 *
 *  return -1 if failure.
 *  return crypt_connection_id of the initialized connection if everything went well.
 */
int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port)
{
    uint32_t i;
    int id_existing = getcryptconnection_id(c, public_key);

    if (id_existing != -1) {
        IP_Port c_ip = connection_ip(c->lossless_udp, c->crypto_connections[id_existing].number);

        if (ipport_equal(&c_ip, &ip_port))
            return -1;
    }

    if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == -1
            || c->crypto_connections == NULL)
        return -1;

    memset(&(c->crypto_connections[c->crypto_connections_length]), 0, sizeof(Crypto_Connection));
    c->crypto_connections[c->crypto_connections_length].number = ~0;

    for (i = 0; i <= c->crypto_connections_length; ++i) {
        if (c->crypto_connections[i].status == CRYPTO_CONN_NO_CONNECTION) {
            int id_new = new_connection(c->lossless_udp, ip_port);

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

            c->crypto_connections[i].number = id_new;
            c->crypto_connections[i].status = CRYPTO_CONN_HANDSHAKE_SENT;
            random_nonce(c->crypto_connections[i].recv_nonce);
            memcpy(c->crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES);
            crypto_box_keypair(c->crypto_connections[i].sessionpublic_key, c->crypto_connections[i].sessionsecret_key);
            c->crypto_connections[i].timeout = unix_time() + CRYPTO_HANDSHAKE_TIMEOUT;

            if (c->crypto_connections_length == i)
                ++c->crypto_connections_length;

            if (send_cryptohandshake(c, id_new, public_key,  c->crypto_connections[i].recv_nonce,
                                     c->crypto_connections[i].sessionpublic_key) == 1) {
                increment_nonce(c->crypto_connections[i].recv_nonce);
                return i;
            }

            return -1; /* This should never happen. */
        }
    }

    return -1;
}
Example #3
0
/* Start a secure connection with other peer who has public_key and ip_port.
 *
 *  return -1 if failure.
 *  return crypt_connection_id of the initialized connection if everything went well.
 */
int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port)
{
    uint32_t i;
    int id = getcryptconnection_id(c, public_key);

    if (id != -1) {
        IP_Port c_ip = connection_ip(c->lossless_udp, c->crypto_connections[id].number);

        if (c_ip.ip.uint32 == ip_port.ip.uint32 && c_ip.port == ip_port.port)
            return -1;
    }

    if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == -1)
        return -1;

    memset(&(c->crypto_connections[c->crypto_connections_length]), 0, sizeof(Crypto_Connection));
    c->crypto_connections[c->crypto_connections_length].number = ~0;

    for (i = 0; i <= c->crypto_connections_length; ++i) {
        if (c->crypto_connections[i].status == CONN_NO_CONNECTION) {
            int id = new_connection(c->lossless_udp, ip_port);

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

            c->crypto_connections[i].number = id;
            c->crypto_connections[i].status = CONN_HANDSHAKE_SENT;
            random_nonce(c->crypto_connections[i].recv_nonce);
            memcpy(c->crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES);
            crypto_box_keypair(c->crypto_connections[i].sessionpublic_key, c->crypto_connections[i].sessionsecret_key);

            if (c->crypto_connections_length == i)
                ++c->crypto_connections_length;

            if (send_cryptohandshake(c, id, public_key,  c->crypto_connections[i].recv_nonce,
                                     c->crypto_connections[i].sessionpublic_key) == 1) {
                increment_nonce(c->crypto_connections[i].recv_nonce);
                return i;
            }

            return -1; /* This should never happen. */
        }
    }

    return -1;
}
Example #4
0
int main(int argc, char *argv[])
{
    //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32);
    
    if (argc < 6) {
        printf("usage %s ip port client_id(of friend to find ip_port of) filename(of file to send) client_id(ours)\n", argv[0]);
        exit(0);
    }
    addfriend(argv[3]);
    IP_Port friend_ip;
    int connection = -1;
    int inconnection = -1;
    
    //initialize networking
    //bind to ip 0.0.0.0:PORT
    IP ip;
    ip.i = 0;
    init_networking(ip, PORT);
    
    memcpy(self_client_id, argv[5], 32);
    

    perror("Initialization");
    IP_Port bootstrap_ip_port;
    bootstrap_ip_port.port = htons(atoi(argv[2]));
    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;
    
    char buffer1[128];
    int read1 = 0;
    char buffer2[128];
    int read2 = 0;
    FILE *file1 = fopen(argv[4], "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(recievepacket(&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 = getfriendip(argv[3]);
        if(friend_ip.ip.i != 0)
        {
            if(connection == -1)
            {
                printf("Started connecting to friend:");
                printip(friend_ip);
                connection = new_connection(friend_ip);
            }
        }
        if(inconnection == -1)
        {
            inconnection = incoming_connection();
            if(inconnection != -1)
            {
                printf("Someone connected to us:");
                printip(connection_ip(inconnection));
            }
        }
        //if someone connected to us write what he sends to a file
        //also send him our file.
        if(inconnection != -1)
        {
            if(write_packet(inconnection, buffer1, read1))
            {
                printf("Wrote data.\n");
                read1 = fread(buffer1, 1, 128, file1);
            }
            read2 = read_packet(inconnection, buffer2);
            if(read2 != 0)
            {
                printf("Received data.\n");
                if(!fwrite(buffer2, read2, 1, file2))
                {
                        printf("file write error\n");
                }
                if(read2 < 128)
                {
                    fclose(file2);
                }
            } 
        }
        //if we are connected to a friend send him data from the file.
        //also put what he sends us in a file.
        if(is_connected(connection) == 3)
        {
            if(write_packet(0, buffer1, read1))
            {
                printf("Wrote data.\n");
                read1 = fread(buffer1, 1, 128, file1);
            }
            read2 = read_packet(0, buffer2);
            if(read2 != 0)
            {
                printf("Received data.\n");
                if(!fwrite(buffer2, read2, 1, file2))
                {
                        printf("file write error\n");
                }
                if(read2 < 128)
                {
                    fclose(file2);
                }
            } 
        }
        doDHT();
        doLossless_UDP();
        //print_clientlist();
        //print_friendlist();
        //c_sleep(300);
        c_sleep(1);
    }
    
    shutdown_networking();
    return 0;   
}