Ejemplo n.º 1
0
Archivo: main.c Proyecto: Elimut/ALM2
int main()
{
    int i;
    unsigned int hash;
    char* key;
    // srand(todo);
    # if 0
    for (i=0; i<PRIHRADEK/2000; i++) {
        key = strrand();
        hash = hash1(key);
        printf("key = %s, hash = %u\n" ,key ,hash);
    }
    # endif
    for (i=0; i<(PRIHRADEK * 0.95); /*i++ az na konci, kvuli strrand() duplikatum */) {
        key = strrand();

        if (NULL==ht1_insert(key)) {//strrand() vygeneroval duplikat
          //fprintf(stderr,"%s[%d] dupl!\n",key,i);
            continue;
        }
      //else {fprintf(stderr,"%s\n",key); }
        ht2_insert(key);
        ht3_insert(key);
//todo: udelat vypisy kazdych 5% a format jako import pro Excel/OpenCalc => namalovat graf
        if ( i == (int) (PRIHRADEK * 0.5)) {
            printf("50%%    %g   %g    %g\n" ,(pocitadlo1/i) ,(pocitadlo2/i) ,(pocitadlo3/i));
            //pocitadlo1=pocitadlo2=pocitadlo3=0;//po vypisu pocitadla vynulovat?
        }
        else if (i == (int) (PRIHRADEK * 0.75)) {
            printf("75%%    %g   %g    %g\n" ,(pocitadlo1/i) ,(pocitadlo2/i) ,(pocitadlo3/i));
            //pocitadlo1=pocitadlo2=pocitadlo3=0;//po vypisu pocitadla vynulovat?
        }
        else if (i == (int) (PRIHRADEK * 0.9)) {
            printf("90%%    %g   %g    %g\n" ,(pocitadlo1/i) ,(pocitadlo2/i) ,(pocitadlo3/i));
            //pocitadlo1=pocitadlo2=pocitadlo3=0;//po vypisu pocitadla vynulovat?
        }
        i++;//zvetsime az ted
    }
     printf("95%%    %g   %g    %g\n" ,(pocitadlo1/i) ,(pocitadlo2/i) ,(pocitadlo3/i));
    return 0;
}
Ejemplo n.º 2
0
gchar *get_peer_token ( peer *p ) {
    char *n;
    int carry = 1;
    int len = sizeof(p->token) - 1;

    g_assert( p != NULL );
    if (! *(p->token))
	strrand(p->token, len);	

    for (n = p->token + len - 1; carry && n >= p->token; n--)
	switch (*n) {
	    case '9': *n = '0'; carry = 1; break;
	    case 'Z': *n = 'A'; carry = 1; break;
	    case 'z': *n = 'a'; carry = 1; break;
	    default : (*n)++; carry = 0;
	}
        
    n = md5_crypt( p->token, p->token + len - 8 );
    strncpy( p->token, n, len );
    p->token[len - 1] = '\0';

    g_free(n);
    return p->token;
}
Ejemplo n.º 3
0
int _engine_authorize_connection(engine *p, network_command cmd) {
    net_client* p_cli;
    network_command cmd_out;
    my_byte reintroduce;
    int socket = cmd.origin_socket;
    
    
    if (cmd.type != NCT_LEAD_INTRODUCE) {
        glog(LOG_FINE, "Authorization of connection %02d failed because command"
                "type was not correct. Expected %d, got %d", socket, NCT_LEAD_INTRODUCE, cmd.type);
        return 1;
    }
    if (cmd.length < 2) {
        glog(LOG_FINE, "Authorization of connection %02d failed because command"
                " was too short", socket);
        return 1;
    }

    reintroduce = read_hex_byte(cmd.data);
    if (reintroduce) {
        p_cli = engine_client_by_secret(p, cmd.data + 2);
        if (p_cli == NULL) {
            reintroduce = 0;
        } else {
            if (p_cli->status != NET_CLIENT_STATUS_DISCONNECTED) {
                glog(LOG_FINE, "Reauthorization of connection %02d failed "
                        "because command client is not disconnected", socket);
                return 1;
            }
        }

    }
    if (!reintroduce) {
        p_cli = engine_first_free_client_offset(p);
    }

    if (p_cli == NULL) {
        glog(LOG_FINE, "Authorization of socket %02d failed because there"
                "was no more room for new client", socket);
        return 1;
    }

    p->resources->con_to_cli[socket] = p_cli - p->resources->clients;
    if (!reintroduce) {
        net_client_init(p_cli, p->resources->connections + socket);
        strrand(p_cli->connection_secret, NET_CLIENT_SECRET_LENGTH);
        p_cli->connection_secret[NET_CLIENT_SECRET_LENGTH] = '\0';
        p_cli->room_id = ITEM_EMPTY;
    } else {
        p_cli->connection = p->resources->connections + socket;
    }
    p_cli->status = NET_CLIENT_STATUS_CONNECTED;

    network_command_prepare(&cmd_out, NCT_LEAD_INTRODUCE);
    write_hex_byte(cmd_out.data, reintroduce);
    memcpy(cmd_out.data + 2, p_cli->connection_secret, NET_CLIENT_SECRET_LENGTH);

    netadapter_send_command(p->p_netadapter, p_cli->connection, &cmd_out);

    if (reintroduce) {
        _engine_authorize_reconnect(p, p_cli);
    } else {
        glog(LOG_INFO, "Connection %02d authorized as client %02d", socket, p->resources->con_to_cli[socket]);
    }


    return 0;
}