示例#1
0
/* Handle an incoming connection.
 *
 *  return -1 if no crypto inbound connection.
 *  return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection.
 *
 * Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce
 * and the session public key for the connection in session_key.
 * to accept it see: accept_crypto_inbound(...).
 * to refuse it just call kill_connection(...) on the connection id.
 */
int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key)
{
    while (1) {
        int incoming_con = incoming_connection(c->lossless_udp, 1);

        if (incoming_con != -1) {
            if (is_connected(c->lossless_udp, incoming_con) == LUDP_TIMED_OUT) {
                kill_connection(c->lossless_udp, incoming_con);
                continue;
            }

            if (id_packet(c->lossless_udp, incoming_con) == 2) {
                uint8_t temp_data[MAX_DATA_SIZE];
                uint16_t len = read_packet_silent(c->lossless_udp, incoming_con, temp_data);

                if (handle_cryptohandshake(c, public_key, secret_nonce, session_key, temp_data, len)) {
                    return incoming_con;
                }
            }
        } else {
            break;
        }
    }

    return -1;
}
示例#2
0
void		      loop_server(t_server server)
{
	int		      addrlen;
    fd_set        readfds; //set of socket descriptors
    int		      max_sd;

	while(TRUE) 
    {
        //clear the socket set
        FD_ZERO(&readfds);
        //add master socket to set
        FD_SET(server.master_socket, &readfds);
        //add every other sockets to set
        max_sd = sockets_add_child(server, &readfds);
        //wait for an activity on one of the sockets , timeout is NULL , so wait indefinitely
        if ((select( max_sd + 1 , &readfds , NULL , NULL , NULL) < 0) && (errno!=EINTR)) 
        	printf("select error"); 
        //If something happened on the master socket , then its an incoming connection
        incoming_connection(&server, &readfds, &addrlen);
        //else its some IO operation on some other socket :)
        update_producers(&server, &readfds);
        update_consumers(&server, &readfds);
        update_pendings(&server, &readfds);
    }
}
示例#3
0
/* TODO: optimize this
   handle all new incoming connections. */
static void handle_incomings(void)
{
    int income;
    while (1) {
        income = incoming_connection();
        if(income == -1 || new_incoming(income) )
            break;
    }
}
示例#4
0
/* Handle all new incoming connections.
 * TODO: Optimize this.
 */
static void handle_incomings(Net_Crypto *c)
{
    int income;

    while (1) {
        income = incoming_connection(c->lossless_udp);

        if (income == -1 || new_incoming(c, income) )
            break;
    }
}
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;
}
示例#6
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;   
}