HandlerParser *HandlerParser_create(size_t max_targets) { HandlerParser *parser = h_calloc(sizeof(HandlerParser), 1); check_mem(parser); parser->target_max = max_targets; parser->targets = h_calloc(sizeof(unsigned long), max_targets); check_mem(parser->targets); hattach(parser->targets, parser); return parser; error: return NULL; }
int connection_http_to_proxy(Connection *conn) { Proxy *proxy = Request_get_action(conn->req, proxy); check(proxy != NULL, "Should have a proxy backend."); int proxy_fd = netdial(1, bdata(proxy->server), proxy->port); check(proxy_fd != -1, "Failed to connect to proxy backend %s:%d", bdata(proxy->server), proxy->port); if(!conn->proxy_iob) { conn->proxy_iob = IOBuf_create(BUFFER_SIZE, proxy_fd, IOBUF_SOCKET); check_mem(conn->proxy_iob); } if(!conn->client) { conn->client = h_calloc(sizeof(httpclient_parser), 1); check_mem(conn->client); hattach(conn->client, conn); } return CONNECT; error: return FAILED; }
Host *Host_create(bstring name, bstring matching) { if(!MAX_URL_PATH || !MAX_HOST_NAME) { MAX_URL_PATH = Setting_get_int("limits.url_path", 256); MAX_HOST_NAME = Setting_get_int("limits.host_name", 256); log_info("MAX limits.url_path=%d, limits.host_name=%d", MAX_URL_PATH, MAX_HOST_NAME); } Host *host = h_calloc(sizeof(Host), 1); check_mem(host); host->name = bstrcpy(name); check(blength(host->name) < MAX_HOST_NAME, "Host name too long (max %d): '%s'\n", MAX_HOST_NAME, bdata(name)); host->matching = bstrcpy(matching); check(blength(host->matching) < MAX_HOST_NAME, "Host matching pattern too long (max %d): '%s'\n", MAX_HOST_NAME, bdata(name)); host->routes = RouteMap_create(backend_destroy_cb); check(host->routes, "Failed to create host route map for %s.", bdata(name)); return host; error: return NULL; }
Connection *Connection_create(Server *srv, int fd, int rport, const char *remote, SSL_CTX *ssl_ctx) { Connection *conn = h_calloc(sizeof(Connection), 1); check_mem(conn); conn->server = srv; conn->rport = rport; memcpy(conn->remote, remote, IPADDR_SIZE); conn->remote[IPADDR_SIZE] = '\0'; conn->type = 0; conn->req = Request_create(); check_mem(conn->req); if(ssl_ctx != NULL) { conn->iob = IOBuf_create(BUFFER_SIZE, fd, IOBUF_SSL); check(conn->iob != NULL, "Failed to create the SSL IOBuf."); conn->iob->ssl = ssl_server_new(ssl_ctx, IOBuf_fd(conn->iob)); check(conn->iob->ssl != NULL, "Failed to create new ssl for connection"); } else { conn->iob = IOBuf_create(BUFFER_SIZE, fd, IOBUF_SOCKET); } return conn; error: Connection_destroy(conn); return NULL; }
csm_service_list * csm_services_init(void) { csm_service_list *services = h_calloc(1, sizeof(csm_service_list)); CHECK_MEM(services); co_obj_t *csm_services = co_list16_create(); CHECK_MEM(csm_services); services->services = csm_services; hattach(services->services, services); co_obj_t *service_fields = co_list16_create(); CHECK_MEM(service_fields); services->service_fields = service_fields; hattach(services->service_fields, services); co_obj_t *update_handlers = co_list16_create(); CHECK_MEM(update_handlers); services->update_handlers = update_handlers; hattach(services->update_handlers, services); return services; error: return NULL; }
Heap *Heap_create(size_t capacity) { Heap *h = (Heap *)h_calloc(sizeof(Heap), 1); check_mem(h); h->capacity = capacity; h->keys = h_calloc(sizeof(int), capacity); check_mem(h->keys); hattach(h, h->keys); return h; error: Heap_destroy(h); return NULL; }
Server *Server_create(bstring uuid, bstring default_host, bstring bind_addr, int port, bstring chroot, bstring access_log, bstring error_log, bstring pid_file, bstring control_port, int use_ssl) { Server *srv = NULL; int rc = 0; srv = h_calloc(sizeof(Server), 1); check_mem(srv); srv->hosts = RouteMap_create(host_destroy_cb); check(srv->hosts != NULL, "Failed to create host RouteMap."); srv->handlers = darray_create(sizeof(Handler), 20); check_mem(srv->handlers); check(port > 0, "Invalid port given, must be > 0: %d", port); srv->port = port; srv->listen_fd = 0; srv->bind_addr = bstrcpy(bind_addr); check_mem(srv->bind_addr); srv->uuid = bstrcpy(uuid); check_mem(srv->uuid); // TODO: once mbedtls supports opening urandom early and keeping it open, // put the rng initialization back here (before chroot) //if(use_ssl) { // rc = Server_init_rng(srv); // check(rc == 0, "Failed to initialize rng for server %s", bdata(uuid)); //} if(blength(chroot) > 0) { srv->chroot = bstrcpy(chroot); check_mem(srv->chroot); } else { srv->chroot = NULL; } srv->access_log = bstrcpy(access_log); check_mem(srv->access_log); srv->error_log = bstrcpy(error_log); check_mem(srv->error_log); srv->pid_file = bstrcpy(pid_file); check_mem(srv->pid_file); if(blength(control_port) > 0) { srv->control_port = bstrcpy(control_port); check_mem(srv->control_port); } else { srv->control_port = NULL; } srv->default_hostname = bstrcpy(default_host); srv->use_ssl = use_ssl; srv->created_on = time(NULL); if(srv->use_ssl) { rc = Server_init_ssl(srv); check(rc == 0, "Failed to initialize ssl for server %s", bdata(uuid)); } return srv; error: Server_destroy(srv); return NULL; }
svl_crypto_ctx * svl_crypto_ctx_new(void) { svl_crypto_ctx *ctx = h_calloc(1,sizeof(svl_crypto_ctx)); CHECK_MEM(ctx); return ctx; error: return NULL; }
IOBuf *IOBuf_create(size_t len, int fd, IOBufType type) { IOBuf *buf = h_calloc(sizeof(IOBuf), 1); check_mem(buf); buf->fd = fd; buf->len = len; buf->buf = h_malloc(len + 1); check_mem(buf->buf); hattach(buf->buf, buf); buf->type = type; if(type == IOBUF_SSL) { buf->use_ssl = 1; buf->handshake_performed = 0; ssl_init(&buf->ssl); ssl_set_endpoint(&buf->ssl, SSL_IS_SERVER); ssl_set_authmode(&buf->ssl, SSL_VERIFY_NONE); havege_init(&buf->hs); ssl_set_rng(&buf->ssl, havege_rand, &buf->hs); ssl_set_dbg(&buf->ssl, ssl_debug, NULL); ssl_set_bio(&buf->ssl, ssl_fdrecv_wrapper, buf, ssl_fdsend_wrapper, buf); ssl_set_session(&buf->ssl, 1, 0, &buf->ssn); memset(&buf->ssn, 0, sizeof(buf->ssn)); buf->send = ssl_send; buf->recv = ssl_recv; buf->stream_file = ssl_stream_file; } else if(type == IOBUF_NULL) { buf->send = null_send; buf->recv = null_recv; buf->stream_file = null_stream_file; } else if(type == IOBUF_FILE) { buf->send = file_send; buf->recv = file_recv; buf->stream_file = plain_stream_file; } else if(type == IOBUF_SOCKET) { buf->send = plaintext_send; buf->recv = plaintext_recv; buf->stream_file = plain_stream_file; } else { sentinel("Invalid IOBufType given: %d", type); } return buf; error: if(buf) h_free(buf); return NULL; }
/* 创建 */ h_rbtree_st *h_rbtree_create(rbtree_data_free_func_t data_free, rbtree_cmp_func_t cmp) { h_rbtree_st *rb; if ((rb= h_calloc(1, sizeof(h_rbtree_st))) == NULL) return NULL; rb->data_free = data_free; rb->cmp_fn = cmp ? cmp : default_cmp; return rb; }
int co_init(void) { CHECK(_pool == NULL, "Commotion API already initialized."); _pool = h_calloc(1, sizeof(_pool)); _sockets = co_list16_create(); hattach(_sockets, _pool); DEBUG("Commotion API initialized."); return 1; error: return 0; }
static inline _treenode_t * _co_tree_insert_r(_treenode_t *root, _treenode_t *current, const char *orig_key, const size_t orig_klen, const char *key, const size_t klen, co_obj_t *value) { if (current == NULL) { current = (_treenode_t *) h_calloc(1, sizeof(_treenode_t)); if(root == NULL) { root = current; } else { hattach(current, root); } current->splitchar = *key; } if (*key < current->splitchar) { current->low = _co_tree_insert_r(root, current->low, orig_key, orig_klen, key, klen, value); } else if (*key == current->splitchar) { if (klen > 1) { // not done yet, keep going but one less current->equal = _co_tree_insert_r(root, current->equal, orig_key, orig_klen, key+1, klen - 1, value); } else { if(current->value != NULL) { co_obj_free(current->value); } if(current->key != NULL) { co_obj_free(current->key); } current->value = value; current->key = co_str8_create(orig_key, orig_klen, 0); hattach(current->key, current); hattach(current->value, current); current->key->_ref++; current->value->_ref++; } } else { current->high = _co_tree_insert_r(root, current->high, orig_key, orig_klen, key, klen, value); } return current; }
/** * update handlers must be idempotent and side effect free, as * handlers are called in the order they were registered with * service list */ int csm_services_register_commit_hook(csm_service_list *services, co_cb_t handler) { co_cbptr_t *callback = h_calloc(1, sizeof(co_cbptr_t)); CHECK_MEM(callback); callback->_header._type = _ext8; callback->_header._ref = 0; callback->_header._flags = 0; callback->_exttype = _cbptr; callback->_len = sizeof(co_cb_t *); callback->cb = handler; CHECK(co_list_append(services->update_handlers, (co_obj_t *)callback), "Failed to register commit hook"); return 1; error: return 0; }
Route *Route_alloc(Route *parent, const char *name) { Route *r = h_calloc(1, sizeof(Route)); assert_mem(r); r->name = bfromcstr(name); assert_mem(r->name); r->members = Heap_create(NULL); r->children = Heap_create(Route_children_compare); // add it to the children mapping if(parent) { hattach(r, parent); Heap_add(parent->children, r); } return r; }
darray_t *darray_create(size_t element_size, size_t initial_max) { darray_t *array = h_malloc(sizeof(darray_t)); check_mem(array); array->contents = h_calloc(sizeof(void *), initial_max); check_mem(array->contents); hattach(array->contents, array); array->max = initial_max; array->end = 0; array->element_size = element_size; array->expand_rate = DEFAULT_EXPAND_RATE; return array; error: if(array) h_free(array); return NULL; }
static int Server_load_ciphers(Server *srv, bstring ssl_ciphers_val) { const struct tagbstring bstr_underscore = bsStatic("_"); const struct tagbstring bstr_dash = bsStatic("-"); struct bstrList *ssl_cipher_list = bsplit(ssl_ciphers_val, ' '); int i = 0, n = 0; int *ciphers = NULL; check(ssl_cipher_list != NULL && ssl_cipher_list->qty > 0, "Invalid cipher list, it must be separated by space ' ' characters " "and you need at least one. Or, just leave it out for defaults."); ciphers = h_calloc(ssl_cipher_list->qty + 1, sizeof(int)); check_mem(ciphers); for(i = 0; i < ssl_cipher_list->qty; i++) { bstring cipher = ssl_cipher_list->entry[i]; int id = -1; // Replace underscores (used in old ciphers) with dashes bfindreplace(cipher, &bstr_underscore, &bstr_dash, 0); // Search legacy cipher list for(n = 0; legacy_cipher_table[n].name != NULL; ++n) { if(biseqcstr(cipher, legacy_cipher_table[n].name)) { id = legacy_cipher_table[n].id; break; } } if(id != -1 && mbedtls_ssl_ciphersuite_from_id(id) != NULL) { ciphers[i] = id; } else { // Search polarssl cipher list const mbedtls_ssl_ciphersuite_t * suite = mbedtls_ssl_ciphersuite_from_string(bdata(cipher)); if (suite != NULL) ciphers[i] = suite->id; else sentinel("Unrecognized cipher: %s", bdata(cipher)); } } bstrListDestroy(ssl_cipher_list); ciphers[i] = 0; srv->ciphers = ciphers; hattach(ciphers, srv); return 0; error: if(ssl_cipher_list) bstrListDestroy(ssl_cipher_list); if(ciphers != NULL) h_free(ciphers); return -1; }
/* 插入key, value */ int h_rbtree_insert(h_rbtree_st *tree, const void *key, uint32_t ksize, void *val) { rbtree_node_st *parent; int is_left; rbtree_node_st *node = do_lookup(tree, key, ksize, &parent, &is_left); if (node) { if ((void *)tree->data_free) tree->data_free(node->cs.data); node->cs.data = val; return 0; } if ((node = h_calloc(1, sizeof(rbtree_node_st) + ksize)) == NULL) return -1; memcpy_s(node->cs.key, key, ksize); node->cs.ksize = ksize; node->cs.data = val; node->left = NULL; node->right = NULL; set_color(RB_RED, node); set_parent(parent, node); if (parent) { if (is_left) { if (parent == tree->first) tree->first = node; } else { if (parent == tree->last) tree->last = node; } set_child(node, parent, is_left); } else { tree->root = node; tree->first = node; tree->last = node; } /* * Fixup the modified tree by recoloring nodes and performing * rotations (2 at most) hence the red-black tree properties are * preserved. */ while ((parent = get_parent(node)) && is_red(parent)) { rbtree_node_st *grandpa = get_parent(parent); if (parent == grandpa->left) { rbtree_node_st *uncle = grandpa->right; if (uncle && is_red(uncle)) { set_color(RB_BLACK, parent); set_color(RB_BLACK, uncle); set_color(RB_RED, grandpa); node = grandpa; } else { if (node == parent->right) { rotate_left(tree, parent); node = parent; parent = get_parent(node); } set_color(RB_BLACK, parent); set_color(RB_RED, grandpa); rotate_right(tree, grandpa); } } else { rbtree_node_st *uncle = grandpa->left; if (uncle && is_red(uncle)) { set_color(RB_BLACK, parent); set_color(RB_BLACK, uncle); set_color(RB_RED, grandpa); node = grandpa; } else { if (node == parent->left) { rotate_right(tree, parent); node = parent; parent = get_parent(node); } set_color(RB_BLACK, parent); set_color(RB_RED, grandpa); rotate_left(tree, grandpa); } } } set_color(RB_BLACK, tree->root); tree->nelem++; return 0; }