static void ss_rq_test8(void) { ssa a; ss_aopen(&a, &ss_stda); ssrq q; t( ss_rqinit(&q, &a, 10, 100) == 0 ); ssrqnode *an = ss_malloc(&a, sizeof(ssrqnode)); ssrqnode *bn = ss_malloc(&a, sizeof(ssrqnode)); ssrqnode *cn = ss_malloc(&a, sizeof(ssrqnode)); ssrqnode *dn = ss_malloc(&a, sizeof(ssrqnode)); t( an != NULL ); t( bn != NULL ); t( cn != NULL ); t( dn != NULL ); ss_rqinitnode(an); ss_rqinitnode(bn); ss_rqinitnode(cn); ss_rqinitnode(dn); ss_rqadd(&q, an, 0); ss_rqadd(&q, bn, 10); ss_rqadd(&q, cn, 20); ss_rqadd(&q, dn, 30); int i = 30; ssrqnode *n = NULL; while ((n = ss_rqprev(&q, n))) i -= 10; ss_rqfree(&q, &a); ss_free(&a, an); ss_free(&a, bn); ss_free(&a, cn); ss_free(&a, dn); }
static void ss_rq_test7(void) { ssa a; ss_aopen(&a, &ss_stda); ssrq q; t( ss_rqinit(&q, &a, 1, 10) == 0 ); ssrqnode *an = ss_malloc(&a, sizeof(ssrqnode)); ssrqnode *bn = ss_malloc(&a, sizeof(ssrqnode)); ssrqnode *cn = ss_malloc(&a, sizeof(ssrqnode)); t( an != NULL ); t( bn != NULL ); t( cn != NULL ); ss_rqinitnode(an); ss_rqinitnode(bn); ss_rqinitnode(cn); ss_rqadd(&q, an, 1); ss_rqadd(&q, bn, 2); ss_rqadd(&q, cn, 3); ss_rqdelete(&q, bn); ss_rqdelete(&q, cn); ss_rqdelete(&q, an); int i = 0; ssrqnode *n = NULL; while ((n = ss_rqprev(&q, n))) { i++; } t( i == 0 ); ss_rqfree(&q, &a); ss_free(&a, an); ss_free(&a, bn); ss_free(&a, cn); }
static remote_t *new_remote(int fd, int timeout) { remote_t *remote; remote = ss_malloc(sizeof(remote_t)); memset(remote, 0, sizeof(remote_t)); remote->buf = ss_malloc(sizeof(buffer_t)); remote->recv_ctx = ss_malloc(sizeof(remote_ctx_t)); remote->send_ctx = ss_malloc(sizeof(remote_ctx_t)); remote->fd = fd; remote->recv_ctx->remote = remote; remote->recv_ctx->connected = 0; remote->send_ctx->remote = remote; remote->send_ctx->connected = 0; ev_io_init(&remote->recv_ctx->io, remote_recv_cb, fd, EV_READ); ev_io_init(&remote->send_ctx->io, remote_send_cb, fd, EV_WRITE); ev_timer_init(&remote->send_ctx->watcher, remote_timeout_cb, min(MAX_CONNECT_TIMEOUT, timeout), 0); balloc(remote->buf, BUF_SIZE); return remote; }
struct query_ctx *new_query_ctx(char *buf, size_t len) { struct query_ctx *ctx = ss_malloc(sizeof(struct query_ctx)); memset(ctx, 0, sizeof(struct query_ctx)); ctx->buf = ss_malloc(sizeof(buffer_t)); balloc(ctx->buf, len); memcpy(ctx->buf->array, buf, len); ctx->buf->len = len; return ctx; }
cipher_t * aead_key_init(int method, const char *pass, const char *key) { if (method < AES128GCM || method >= AEAD_CIPHER_NUM) { LOGE("aead_key_init(): Illegal method"); return NULL; } cipher_t *cipher = (cipher_t *)ss_malloc(sizeof(cipher_t)); memset(cipher, 0, sizeof(cipher_t)); // Initialize sodium for random generator if (sodium_init() == -1) { FATAL("Failed to initialize sodium"); } if (method >= CHACHA20POLY1305IETF) { cipher_kt_t *cipher_info = (cipher_kt_t *)ss_malloc(sizeof(cipher_kt_t)); cipher->info = cipher_info; cipher->info->base = NULL; cipher->info->key_bitlen = supported_aead_ciphers_key_size[method] * 8; cipher->info->iv_size = supported_aead_ciphers_nonce_size[method]; } else { cipher->info = (cipher_kt_t *)aead_get_cipher_type(method); } if (cipher->info == NULL && cipher->key_len == 0) { LOGE("Cipher %s not found in crypto library", supported_aead_ciphers[method]); FATAL("Cannot initialize cipher"); } if (key != NULL) cipher->key_len = crypto_parse_key(key, cipher->key, supported_aead_ciphers_key_size[method]); else cipher->key_len = crypto_derive_key(pass, cipher->key, supported_aead_ciphers_key_size[method]); if (cipher->key_len == 0) { FATAL("Cannot generate key and nonce"); } cipher->nonce_len = supported_aead_ciphers_nonce_size[method]; cipher->tag_len = supported_aead_ciphers_tag_size[method]; cipher->method = method; return cipher; }
void resolv_start(const char *hostname, uint16_t port, void (*client_cb)(struct sockaddr *, void *), void (*free_cb)(void *), void *data) { /* * Wrap c-ares's call back in our own */ struct resolv_query *query = ss_malloc(sizeof(struct resolv_query)); memset(query, 0, sizeof(struct resolv_query)); query->port = port; query->client_cb = client_cb; query->response_count = 0; query->responses = NULL; query->data = data; query->free_cb = free_cb; query->requests[0] = AF_INET; query->requests[1] = AF_INET6; ares_gethostbyname(default_ctx.channel, hostname, AF_INET, dns_query_v4_cb, query); ares_gethostbyname(default_ctx.channel, hostname, AF_INET6, dns_query_v6_cb, query); reset_timer(); }
static void aead_cipher_ctx_init(cipher_ctx_t *cipher_ctx, int method, int enc) { if (method < AES128GCM || method >= AEAD_CIPHER_NUM) { LOGE("cipher_context_init(): Illegal method"); return; } if (method >= CHACHA20POLY1305IETF) { return; } const char *ciphername = supported_aead_ciphers[method]; const cipher_kt_t *cipher = aead_get_cipher_type(method); cipher_ctx->evp = ss_malloc(sizeof(cipher_evp_t)); memset(cipher_ctx->evp, 0, sizeof(cipher_evp_t)); cipher_evp_t *evp = cipher_ctx->evp; if (cipher == NULL) { LOGE("Cipher %s not found in mbed TLS library", ciphername); FATAL("Cannot initialize mbed TLS cipher"); } mbedtls_cipher_init(evp); if (mbedtls_cipher_setup(evp, cipher) != 0) { FATAL("Cannot initialize mbed TLS cipher context"); } #ifdef SS_DEBUG dump("KEY", (char *)cipher_ctx->cipher->key, cipher_ctx->cipher->key_len); #endif }
sinode *si_nodenew(sr *r) { sinode *n = (sinode*)ss_malloc(r->a, sizeof(sinode)); if (ssunlikely(n == NULL)) { sr_oom_malfunction(r->e); return NULL; } n->recover = 0; n->backup = 0; n->lru = 0; n->ac = 0; n->flags = 0; n->update_time = 0; n->used = 0; n->in_memory = 0; si_branchinit(&n->self, r); n->branch = NULL; n->branch_count = 0; n->temperature = 0; n->temperature_reads = 0; ss_fileinit(&n->file, r->vfs); ss_mmapinit(&n->map); ss_mmapinit(&n->map_swap); sv_indexinit(&n->i0); sv_indexinit(&n->i1); ss_rbinitnode(&n->node); ss_rqinitnode(&n->nodecompact); ss_rqinitnode(&n->nodebranch); ss_rqinitnode(&n->nodetemp); ss_listinit(&n->commit); return n; }
int update_block_list(char *addr, int err_level) { size_t addr_len = strlen(addr); if (cache_key_exist(block_list, addr, addr_len)) { int *count = NULL; cache_lookup(block_list, addr, addr_len, &count); if (count != NULL) { if (*count > MAX_TRIES) return 1; (*count) += err_level; } } else if (err_level > 0) { int *count = (int *)ss_malloc(sizeof(int)); *count = 1; cache_insert(block_list, addr, addr_len, count); #ifdef __linux__ if (mode != NO_FIREWALL_MODE) set_firewall_rule(addr, 1); #endif } return 0; }
/** C = alpha*A + beta*B */ cs* X(ss_add) (const cs *A, const cs *B, SS_ENTRY alpha, SS_ENTRY beta) { SS_INT p, j, nz = 0, anz, *Cp, *Ci, *Bp, m, n, bnz, *w, values ; SS_ENTRY *x, *Bx, *Cx ; cs *C ; if (!SS_CSC (A) || !SS_CSC (B)) return (NULL) ; /* check inputs */ if (A->m != B->m || A->n != B->n) return (NULL) ; m = A->m ; anz = A->p [A->n] ; n = B->n ; Bp = B->p ; Bx = B->x ; bnz = Bp [n] ; w = ss_calloc (m, sizeof (SS_INT)) ; /* get workspace */ values = (A->x != NULL) && (Bx != NULL) ; x = values ? ss_malloc (m, sizeof (SS_ENTRY)) : NULL ; /* get workspace */ C = ss_spalloc (m, n, anz + bnz, values, 0) ; /* allocate result*/ if (!C || !w || (values && !x)) return (ss_done (C, w, x, 0)) ; Cp = C->p ; Ci = C->i ; Cx = C->x ; for (j = 0 ; j < n ; j++) { Cp [j] = nz ; /* column j of C starts here */ nz = ss_scatter (A, j, alpha, w, x, j+1, C, nz) ; /* alpha*A(:,j)*/ nz = ss_scatter (B, j, beta, w, x, j+1, C, nz) ; /* beta*B(:,j) */ if (values) for (p = Cp [j] ; p < nz ; p++) Cx [p] = x [Ci [p]] ; } Cp [n] = nz ; /* finalize the last column of C */ ss_sprealloc (C, 0) ; /* remove extra space from C */ return (ss_done (C, w, x, 1)) ; /* success; free workspace, return C */ }
ss_INLINE ss* ss_m_cell(ss v) { ss *c = ss_malloc(sizeof(*c)); *c = v; return c; }
/** C = A*B */ cs* X(ss_multiply) (const cs *A, const cs *B) { SS_INT p, j, nz = 0, anz, *Cp, *Ci, *Bp, m, n, bnz, *w, values, *Bi ; SS_ENTRY *x, *Bx, *Cx ; cs *C ; if (!SS_CSC (A) || !SS_CSC (B)) return (NULL) ; /* check inputs */ if (A->n != B->m) return (NULL) ; m = A->m ; anz = A->p [A->n] ; n = B->n ; Bp = B->p ; Bi = B->i ; Bx = B->x ; bnz = Bp [n] ; w = ss_calloc (m, sizeof (SS_INT)) ; /* get workspace */ values = (A->x != NULL) && (Bx != NULL) ; x = values ? ss_malloc (m, sizeof (SS_ENTRY)) : NULL ; /* get workspace */ C = ss_spalloc (m, n, anz + bnz, values, 0) ; /* allocate result */ if (!C || !w || (values && !x)) return (ss_done (C, w, x, 0)) ; Cp = C->p ; for (j = 0 ; j < n ; j++) { if (nz + m > C->nzmax && !ss_sprealloc (C, 2*(C->nzmax)+m)) { return (ss_done (C, w, x, 0)) ; /* out of memory */ } Ci = C->i ; Cx = C->x ; /* C->i and C->x may be reallocated */ Cp [j] = nz ; /* column j of C starts here */ for (p = Bp [j] ; p < Bp [j+1] ; p++) { nz = ss_scatter (A, Bi [p], Bx ? Bx [p] : 1, w, x, j+1, C, nz) ; } if (values) for (p = Cp [j] ; p < nz ; p++) Cx [p] = x [Ci [p]] ; } Cp [n] = nz ; /* finalize the last column of C */ ss_sprealloc (C, 0) ; /* remove extra space from C */ return (ss_done (C, w, x, 1)) ; /* success; free workspace, return C */ }
/** allocate a sparse matrix (triplet form or compressed-column form) */ static cs *ss_spalloc (SS_INT m, SS_INT n, SS_INT nzmax, SS_INT values, SS_INT triplet) { cs *A = ss_calloc (1, sizeof (cs)) ; /* allocate the cs struct */ if (!A) return (NULL) ; /* out of memory */ A->id = M_SPT; A->m = m ; /* define dimensions and nzmax */ A->n = n ; A->nzmax = nzmax = SS_MAX (nzmax, 1) ; A->nz = triplet ? 0 : -1 ; /* allocate triplet or comp.col */ A->p = ss_malloc (triplet ? nzmax : n+1, sizeof (SS_INT)) ; A->i = ss_malloc (nzmax, sizeof (SS_INT)) ; A->x = values ? ss_malloc (nzmax, sizeof (SS_ENTRY)) : NULL ; A->nref=calloc(1,sizeof(long)); A->nref[0]=1; return ((!A->p || !A->i || (values && !A->x)) ? ss_spfree (A) : A) ; }
int balloc(buffer_t *ptr, size_t capacity) { sodium_memzero(ptr, sizeof(buffer_t)); ptr->array = ss_malloc(capacity); ptr->capacity = capacity; return capacity; }
int balloc(buffer_t *ptr, size_t capacity) { memset(ptr, 0, sizeof(buffer_t)); ptr->array = ss_malloc(capacity); ptr->capacity = capacity; return capacity; }
void stream_cipher_ctx_init(cipher_ctx_t *ctx, int method, int enc) { if (method <= TABLE || method >= STREAM_CIPHER_NUM) { LOGE("stream_ctx_init(): Illegal method"); return; } if (method >= SALSA20) { return; } const char *ciphername = supported_stream_ciphers[method]; const cipher_kt_t *cipher = stream_get_cipher_type(method); ctx->evp = ss_malloc(sizeof(cipher_evp_t)); memset(ctx->evp, 0, sizeof(cipher_evp_t)); cipher_evp_t *evp = ctx->evp; if (cipher == NULL) { LOGE("Cipher %s not found in mbed TLS library", ciphername); FATAL("Cannot initialize mbed TLS cipher"); } mbedtls_cipher_init(evp); if (mbedtls_cipher_setup(evp, cipher) != 0) { FATAL("Cannot initialize mbed TLS cipher context"); } }
JNIEXPORT void JNICALL Java_me_smartproxy_crypto_CryptoUtils_initEncryptor(JNIEnv *env, jclass thiz, jstring jpassword, jstring jmethod, jlong id) { enc_connection *connection = enc_ctx_map[id]; if(connection == NULL) { const char *password = env->GetStringUTFChars(jpassword, 0); const char *method = env->GetStringUTFChars(jmethod, 0); connection = (enc_connection *)ss_malloc(sizeof(struct enc_connection)); connection->text_e_ctx = (enc_ctx *)ss_malloc(sizeof(struct enc_ctx)); connection->text_d_ctx = (enc_ctx *)ss_malloc(sizeof(struct enc_ctx)); int enc_method = enc_init(password, method); enc_ctx_init(enc_method, connection->text_e_ctx, 1); enc_ctx_init(enc_method, connection->text_d_ctx, 0); enc_ctx_map[id] = connection; env->ReleaseStringUTFChars(jpassword, password); env->ReleaseStringUTFChars(jmethod, method); } }
int sr_meta_write(srmeta *m, srmetastmt *s) { if (m->flags & SR_RO) { sr_error(s->r->e, "%s is read-only", s->path); return -1; } switch (m->type) { case SS_U32: if (s->valuetype == SS_I64) { *((uint32_t*)m->value) = *(int64_t*)s->value; } else if (s->valuetype == SS_U32) { *((uint32_t*)m->value) = *(uint32_t*)s->value; } else if (s->valuetype == SS_U64) { *((uint32_t*)m->value) = *(uint64_t*)s->value; } else { goto bad_type; } break; case SS_U64: if (s->valuetype == SS_I64) { *((uint64_t*)m->value) = *(int64_t*)s->value; } else if (s->valuetype == SS_U32) { *((uint64_t*)m->value) = *(uint32_t*)s->value; } else if (s->valuetype == SS_U64) { *((uint64_t*)m->value) = *(uint64_t*)s->value; } else { goto bad_type; } break; case SS_STRINGPTR: { char **string = m->value; if (s->valuetype == SS_STRING) { char *sz = s->value; if (s->valuesize > 0) { sz = ss_malloc(s->r->a, s->valuesize); if (ssunlikely(sz == NULL)) return sr_oom(s->r->e); memcpy(sz, s->value, s->valuesize); } if (*string) ss_free(s->r->a, *string); *string = sz; } else { goto bad_type; } break; } default: assert(0); return -1; } return 0; bad_type: return sr_error(s->r->e, "bad meta write type (%s) for (%s) %s", ss_typeof(s->valuetype), ss_typeof(m->type), s->path); }
ss_INLINE ss ss_alloc(ss type, size_t size) { ss *ptr = ss_malloc(sizeof(ss) + size); *(ptr ++) = type; if ( type && ! ss_fixnumQ(((ss_s_type*) type)->instance_size) ) ((ss_s_type*) type)->instance_size = ss_i(size); return ptr; }
static int deobfs_tls_response(buffer_t *buf, size_t cap, obfs_t *obfs) { if (obfs == NULL || obfs->deobfs_stage < 0) return 0; if (obfs->extra == NULL) { obfs->extra = ss_malloc(sizeof(frame_t)); memset(obfs->extra, 0, sizeof(frame_t)); } if (obfs->deobfs_stage == 0) { size_t hello_len = sizeof(struct tls_server_hello); char *data = buf->data; int len = buf->len; len -= hello_len; if (len <= 0) return OBFS_NEED_MORE; struct tls_server_hello *hello = (struct tls_server_hello*) data; if (hello->content_type != tls_server_hello_template.content_type) return OBFS_ERROR; size_t change_cipher_spec_len = sizeof(struct tls_change_cipher_spec); size_t encrypted_handshake_len = sizeof(struct tls_encrypted_handshake); len -= change_cipher_spec_len + encrypted_handshake_len; if (len <= 0) return OBFS_NEED_MORE; size_t tls_len = hello_len + change_cipher_spec_len + encrypted_handshake_len; struct tls_encrypted_handshake *encrypted_handshake = (struct tls_encrypted_handshake *)(buf->data + hello_len + change_cipher_spec_len); size_t msg_len = CT_NTOHS(encrypted_handshake->len); memmove(buf->data, buf->data + tls_len, buf->len - tls_len); buf->len = buf->len - tls_len; obfs->deobfs_stage++; if (buf->len > msg_len) { return deobfs_app_data(buf, msg_len, obfs); } else { ((frame_t*)obfs->extra)->idx = buf->len - msg_len; } } else if (obfs->deobfs_stage == 1) { return deobfs_app_data(buf, 0, obfs); } return 0; }
static void ssa_malloc(void) { ssa a; ss_aopen(&a, &ss_stda); void *buf = ss_malloc(&a, 123); t( buf != NULL ); ss_free(&a, buf); ss_aclose(&a); }
cipher_t * stream_key_init(int method, const char *pass, const char *key) { if (method <= TABLE || method >= STREAM_CIPHER_NUM) { LOGE("cipher->key_init(): Illegal method"); return NULL; } cipher_t *cipher = (cipher_t *)ss_malloc(sizeof(cipher_t)); memset(cipher, 0, sizeof(cipher_t)); if (method == SALSA20 || method == CHACHA20 || method == CHACHA20IETF) { cipher_kt_t *cipher_info = (cipher_kt_t *)ss_malloc(sizeof(cipher_kt_t)); cipher->info = cipher_info; cipher->info->base = NULL; cipher->info->key_bitlen = supported_stream_ciphers_key_size[method] * 8; cipher->info->iv_size = supported_stream_ciphers_nonce_size[method]; } else { cipher->info = (cipher_kt_t *)stream_get_cipher_type(method); } if (cipher->info == NULL && cipher->key_len == 0) { LOGE("Cipher %s not found in crypto library", supported_stream_ciphers[method]); FATAL("Cannot initialize cipher"); } if (key != NULL) cipher->key_len = crypto_parse_key(key, cipher->key, cipher_key_size(cipher)); else cipher->key_len = crypto_derive_key(pass, cipher->key, cipher_key_size(cipher)); if (cipher->key_len == 0) { FATAL("Cannot generate key and NONCE"); } if (method == RC4_MD5) { cipher->nonce_len = 16; } else { cipher->nonce_len = cipher_nonce_size(cipher); } cipher->method = method; return cipher; }
struct ResolvQuery * resolv_query(const char *hostname, void (*client_cb)(struct sockaddr *, void *), void (*client_free_cb)(void *), void *client_cb_data, uint16_t port) { struct dns_ctx *ctx = (struct dns_ctx *)resolv_io_watcher.data; /* * Wrap udns's call back in our own */ struct ResolvQuery *cb_data = ss_malloc(sizeof(struct ResolvQuery)); if (cb_data == NULL) { LOGE("Failed to allocate memory for DNS query callback data."); return NULL; } memset(cb_data, 0, sizeof(struct ResolvQuery)); cb_data->client_cb = client_cb; cb_data->client_free_cb = client_free_cb; cb_data->client_cb_data = client_cb_data; memset(cb_data->queries, 0, sizeof(cb_data->queries)); cb_data->response_count = 0; cb_data->responses = NULL; cb_data->port = port; /* Submit A and AAAA queries */ if (resolv_mode != MODE_IPV6_ONLY) { cb_data->queries[0] = dns_submit_a4(ctx, hostname, 0, dns_query_v4_cb, cb_data); if (cb_data->queries[0] == NULL) { LOGE("Failed to submit DNS query: %s", dns_strerror(dns_status(ctx))); } } if (resolv_mode != MODE_IPV4_ONLY) { cb_data->queries[1] = dns_submit_a6(ctx, hostname, 0, dns_query_v6_cb, cb_data); if (cb_data->queries[1] == NULL) { LOGE("Failed to submit DNS query: %s", dns_strerror(dns_status(ctx))); } } if (all_queries_are_null(cb_data)) { if (cb_data->client_free_cb != NULL) { cb_data->client_free_cb(cb_data->client_cb_data); } ss_free(cb_data); } return cb_data; }
server_ctx_t *new_server_ctx(int fd) { server_ctx_t *ctx = ss_malloc(sizeof(server_ctx_t)); memset(ctx, 0, sizeof(server_ctx_t)); ctx->fd = fd; ev_io_init(&ctx->io, server_recv_cb, fd, EV_READ); return ctx; }
static int se_dbscheme_init(sedb *db, char *name, int size) { se *e = se_of(&db->o); /* database id */ uint32_t id = sr_seq(&e->seq, SR_DSN); sr_seq(&e->seq, SR_DSNNEXT); /* prepare index scheme */ sischeme *scheme = db->scheme; if (size == 0) size = strlen(name); scheme->name = ss_malloc(&e->a, size + 1); if (ssunlikely(scheme->name == NULL)) goto error; memcpy(scheme->name, name, size); scheme->name[size] = 0; scheme->id = id; scheme->sync = 2; scheme->mmap = 0; scheme->storage = SI_SCACHE; scheme->node_size = 64 * 1024 * 1024; scheme->node_compact_load = 0; scheme->node_page_size = 128 * 1024; scheme->node_page_checksum = 1; scheme->compression_copy = 0; scheme->compression_cold = 0; scheme->compression_cold_if = &ss_nonefilter; scheme->compression_hot = 0; scheme->compression_hot_if = &ss_nonefilter; scheme->temperature = 0; scheme->expire = 0; scheme->amqf = 0; scheme->fmt_storage = SF_RAW; scheme->lru = 0; scheme->lru_step = 128 * 1024; scheme->buf_gc_wm = 1024 * 1024; scheme->storage_sz = ss_strdup(&e->a, "cache"); if (ssunlikely(scheme->storage_sz == NULL)) goto error; scheme->compression_cold_sz = ss_strdup(&e->a, scheme->compression_cold_if->name); if (ssunlikely(scheme->compression_cold_sz == NULL)) goto error; scheme->compression_hot_sz = ss_strdup(&e->a, scheme->compression_hot_if->name); if (ssunlikely(scheme->compression_hot_sz == NULL)) goto error; sf_upsertinit(&scheme->fmt_upsert); sf_schemeinit(&scheme->scheme); return 0; error: sr_oom(&e->error); return -1; }
static server_t * new_server(int fd, int method) { server_t *server; server = ss_malloc(sizeof(server_t)); server->recv_ctx = ss_malloc(sizeof(server_ctx_t)); server->send_ctx = ss_malloc(sizeof(server_ctx_t)); server->buf = ss_malloc(sizeof(buffer_t)); server->fd = fd; server->recv_ctx->server = server; server->recv_ctx->connected = 0; server->send_ctx->server = server; server->send_ctx->connected = 0; server->hostname = NULL; server->hostname_len = 0; if (method) { server->e_ctx = ss_malloc(sizeof(enc_ctx_t)); server->d_ctx = ss_malloc(sizeof(enc_ctx_t)); enc_ctx_init(method, server->e_ctx, 1); enc_ctx_init(method, server->d_ctx, 0); } else { server->e_ctx = NULL; server->d_ctx = NULL; } ev_io_init(&server->recv_ctx->io, server_recv_cb, fd, EV_READ); ev_io_init(&server->send_ctx->io, server_send_cb, fd, EV_WRITE); balloc(server->buf, BUF_SIZE); return server; }
void enc_table_init(const char *pass) { uint32_t i; uint64_t key = 0; uint8_t *digest; enc_table = ss_malloc(256); dec_table = ss_malloc(256); digest = enc_md5((const uint8_t *)pass, strlen(pass), NULL); for (i = 0; i < 8; i++) key += OFFSET_ROL(digest, i); for (i = 0; i < 256; ++i) enc_table[i] = i; for (i = 1; i < 1024; ++i) merge_sort(enc_table, 256, i, key); for (i = 0; i < 256; ++i) // gen decrypt table from encrypt table dec_table[enc_table[i]] = i; }
static void ssaslab_reuse(stc *cx) { sspager p; ss_pagerinit(&p, 3, 1024); ssa slab; t( ss_aopen(&slab, &ss_slaba, &p, 32) == 0 ); void *alloc0[1000]; void *alloc1[1000]; memset(alloc0, 0, sizeof(alloc0)); memset(alloc1, 0, sizeof(alloc1)); int i = 0; while (i < 1000) { alloc0[i] = ss_malloc(&slab, 0); t( alloc0[i] != NULL ); i++; } i--; int pools = p.pools; while (i >= 0) { ss_free(&slab, alloc0[i]); i--; } t( p.pools == pools ); i++; while (i < 1000) { alloc1[i] = ss_malloc(&slab, 0); t( alloc0[i] == alloc1[i] ); i++; } t( p.pools == pools ); ss_aclose(&slab); ss_pagerfree(&p); }
so *sc_readpool_new(screadpool *p, so *o, int add) { scread *r = (scread*)o; scread *n = ss_malloc(p->r->a, sizeof(scread)); if (ssunlikely(r == NULL)) { sr_oom(p->r->e); return NULL; } memcpy(n, r, sizeof(*r)); if (add) { sc_readpool_add(p, n); } return &r->o; }
remote_ctx_t *new_remote(int fd, server_ctx_t *server_ctx) { remote_ctx_t *ctx = ss_malloc(sizeof(remote_ctx_t)); memset(ctx, 0, sizeof(remote_ctx_t)); ctx->fd = fd; ctx->server_ctx = server_ctx; ev_io_init(&ctx->io, remote_recv_cb, fd, EV_READ); ev_timer_init(&ctx->watcher, remote_timeout_cb, server_ctx->timeout, server_ctx->timeout); return ctx; }