Ejemplo n.º 1
0
int hammy_put(ErlNifEnv *env, hammy_db *db, unsigned char *key, int key_size, unsigned char *value, int value_size) {
    ham_key_t k;
    ham_record_t rec;
    ham_txn_t *txn;
    int rc;

    setup_key(&k, key, key_size);
    setup_record(&rec, value, value_size);

    ham_txn_begin(&txn, db->databases[0], 0);
    rc = ham_insert(db->databases[0], txn, &k, &rec, HAM_OVERWRITE);
    if (rc == HAM_SUCCESS) {
        ham_txn_commit(txn, 0);
        return HAMMY_TRUE;
    }
    else {
        ham_txn_abort(txn, 0);
        return HAMMY_FALSE;
    }
}
Ejemplo n.º 2
0
int main(int argc, char *argv[]) {
    int i, clients, keysize, error;
    struct addrinfo *result;
#ifdef _WIN32
    static struct WSAData wsa_state;
#endif
    char ip[40], port[6];

    if(argc<5) {
        fprintf(stderr, "Usage:\n\t%s host port keysize numclients\n", argv[0]);
        fprintf(stderr, "Example:\n\t%s localhost 443 1024 100\n", argv[0]);
        fprintf(stderr, "Key size can be retrieved with:\n");
        fprintf(stderr, "\topenssl s_client -connect host:port");
        fprintf(stderr, " </dev/zero 2>&1 |grep '^Server public key is '\n");
        return 1;
    }
#ifdef _WIN32
    WSAStartup(MAKEWORD(2, 2), &wsa_state);
#endif
    error=getaddrinfo(argv[1], argv[2], NULL, &result);
    if(error) {
        fprintf(stderr, "Error in getaddrinfo: %s\n", gai_strerror(error));
        return 1;
    }
    addr_len=result->ai_addrlen;
    addr=malloc(addr_len);
    memcpy(addr, result->ai_addr, addr_len);
    freeaddrinfo(result);

    keysize=atoi(argv[3]);
    if(keysize<1 || keysize>4096) {
        fprintf(stderr, "Keysize is usually either 1024 or 2048\n");
        return 1;
    }
    keysize=(keysize+7)/8; /* convert to number of bytes */

    clients=atoi(argv[4]);
    if(clients<1 || clients>10000) {
        fprintf(stderr, "Number of clients should be a number between 1 and 10000\n");
        return 1;
    }

    error=getnameinfo(addr, addr_len, ip, sizeof ip,
        port, sizeof port, NI_NUMERICHOST|NI_NUMERICSERV);
    if(error) {
        fprintf(stderr, "getnameinfo: %s", gai_strerror(error));
        return 1;
    }
    fprintf(stderr, "Let's Handshake ~ \n");
    fprintf(stderr, "Squeezing %s:%s\n", ip, port);

    setup_record(&r1.rec, 22, sizeof r1.fragment); /* handshake */
    r1.fragment.type=1; /* client_hello */
    r1.fragment.length[0]=0;
    r1.fragment.length[1]=sizeof r1.fragment.client_hello>>8;
    r1.fragment.length[2]=sizeof r1.fragment.client_hello&0xff;
    r1.fragment.client_hello.version[0]=3;
    r1.fragment.client_hello.version[1]=0;
    r1.fragment.client_hello.session_id_length[0]=0;
    r1.fragment.client_hello.cipher_suite_length[0]=0;
    r1.fragment.client_hello.cipher_suite_length[1]=sizeof r1.fragment.client_hello.cipher_suite_list;
    /* Cipher Suite List : https://www.thesprawl.org/research/tls-and-ssl-cipher-suites/ */
    r1.fragment.client_hello.cipher_suite_list[0]=0x00;
    r1.fragment.client_hello.cipher_suite_list[1]=0x0a; /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */
    r1.fragment.client_hello.cipher_suite_list[2]=0x00;
    r1.fragment.client_hello.cipher_suite_list[3]=0x04; /* SSL_RSA_WITH_RC4_128_MD5 */
    r1.fragment.client_hello.cipher_suite_list[4]=0x00;
    r1.fragment.client_hello.cipher_suite_list[5]=0x2f; /* TLS_RSA_WITH_AES_128_CBC_SHA */
    r1.fragment.client_hello.compression_length[0]=sizeof r1.fragment.client_hello.compression_list;
    r1.fragment.client_hello.compression_list[0]=0;

    setup_record(&r2.rec, 22, offsetof(struct client_key_exchange_fragment, client_key_exchange)+keysize); /* handshake */
    r2.fragment.type=16; /* client_key_exchange */
    r2.fragment.length[0]=0;
    r2.fragment.length[1]=keysize>>8;
    r2.fragment.length[2]=keysize&0xff;
#if 0
    for(i=0; i<sizeof r2.fragment.client_key_exchange; ++i)
        r2.fragment.client_key_exchange[i]=random();
#endif

    setup_record(&r3.rec, 20, sizeof r3.fragment); /* change_cipher_spec */
    r3.fragment.type=1;

    setup_record(&r4.rec, 22, sizeof r4.fragment); /* handshake */

    base=event_base_new();
    for(i=0; i<clients; ++i)
        new_connection(malloc(sizeof(state)));
    event_base_dispatch(base);
    return 0;
}