int lxl_dns_core_init(lxl_cycle_t *cycle) { lxl_dns_pool = lxl_create_pool(LXL_DEFAULT_POOL_SIZE); if (lxl_dns_pool == NULL) { return -1; } lxl_dns_root_zone = lxl_palloc(lxl_dns_pool, sizeof(lxl_dns_zone_t)); if (lxl_dns_root_zone == NULL) { return -1; } lxl_log_error(LXL_LOG_INFO, 0, "dns init zone hash"); if (lxl_hash_init(&lxl_dns_hash, lxl_dns_pool, 1024000, lxl_hash_key) == -1) { lxl_log_error(LXL_LOG_ERROR, 0, "dns init hash failed"); return -1; } lxl_list_init(&lxl_dns_root_zone->rrset_list); lxl_dns_root_zone->update_sec = lxl_current_sec; lxl_log_error(LXL_LOG_INFO, 0, "dns load named root path conf/named.root"); lxl_dns_load_named_root("conf/named.root"); lxl_log_error(LXL_LOG_INFO, 0, "dns add root zone"); if (lxl_dns_zone_add(LXL_DNS_ROOT_LABEL, LXL_DNS_ROOT_LEN, lxl_dns_root_zone) == -1) { lxl_log_error(LXL_LOG_ERROR, 0, "dns add root zone failed"); return -1; } return 0; }
int lxl_dns_core_init(lxl_cycle_t *cycle) { lxl_connection_t *c; lxl_dns_pool = lxl_create_pool(LXL_DEFAULT_POOL_SIZE); if (lxl_dns_pool == NULL) { return -1; } lxl_dns_root_zone = lxl_palloc(lxl_dns_pool, sizeof(lxl_dns_zone_t)); if (lxl_dns_root_zone == NULL) { return -1; } lxl_log_error(LXL_LOG_INFO, 0, "dns init zone hash"); if (lxl_hash_init(&lxl_dns_hash, lxl_dns_pool, 1024000, lxl_hash_key) == -1) { lxl_log_error(LXL_LOG_ERROR, 0, "dns init hash failed"); return -1; } lxl_list_init(&lxl_dns_root_zone->rrset_list); lxl_dns_root_zone->update_sec = lxl_current_sec; lxl_log_error(LXL_LOG_INFO, 0, "dns load named root path conf/named.root"); if (lxl_dns_load_named_root("conf/named.root") == -1) { lxl_log_error(LXL_LOG_ERROR, 0, "dns load named root path conf/named.root failed"); return -1; } lxl_log_error(LXL_LOG_INFO, 0, "dns add root zone"); if (lxl_dns_zone_add(LXL_DNS_ROOT_LABEL, LXL_DNS_ROOT_LEN, lxl_dns_root_zone) == -1) { lxl_log_error(LXL_LOG_ERROR, 0, "dns add root zone failed"); return -1; } c = lxl_get_connection(-1); if (c == NULL) { return -1; } memset(&lxl_dns_event, 0x00, sizeof(lxl_event_t)); lxl_dns_event.data = c; lxl_dns_event.handler = lxl_dns_data_rebuild_handler; lxl_add_timer(&lxl_dns_event, 86400*1000); #if 0 char *t; memcpy(t, "lxl coredump test", 1000); #endif return 0; }
lxl_dns_zone_t * lxl_dns_zone_addfind(char *zname, uint16_t zlen) { void **value; lxl_dns_zone_t *zone; value = lxl_hash_addfind(&lxl_dns_hash, zname, zlen); if (value == NULL) { return NULL; } if (*value == NULL) { zone = lxl_palloc(lxl_dns_pool, sizeof(lxl_dns_zone_t)); if (zone == NULL) { return NULL; } lxl_log_debug(LXL_LOG_DEBUG_DNS, 0, "dns data add zone zname:%s zlen:%hu", zname, zlen); lxl_list_init(&zone->rrset_list); *value = zone; } return *value; }
lxl_int_t lxl_dns_rr_add(lxl_pool_t *pool, lxl_dns_zone_t *zone, lxl_dns_rr_t *rr) { lxl_dns_rrset_t *rrset; lxl_dns_rdata_t *rdata; #if 1 char *data, buffer[40]; /* 40 ipv6 16 ipv4 */ if (rr->soa_flags & LXL_DNS_RRSET_NORMAL_TYPE) { switch(rr->type) { case LXL_DNS_A: inet_ntop(AF_INET, rr->rdata, buffer, 40); data = buffer; break; case LXL_DNS_AAAA: inet_ntop(AF_INET6, rr->rdata, buffer, 40); data = buffer; break; case LXL_DNS_MX: data = rr->rdata + 2; default: data = rr->rdata; break; } } else { data = rr->rdata; } lxl_log_debug(LXL_LOG_DEBUG_DNS, 0, "dns add rr: name:%s type:%04x ttl:%u rdata:%s", rr->name, rr->type, rr->ttl, data); #endif rrset = lxl_dns_rrset_find(&zone->rrset_list, rr->name, rr->nlen, rr->type, LXL_DNS_RRSET_NORMAL_FIND); if (rrset == NULL) { lxl_log_debug(LXL_LOG_DEBUG_DNS, 0, "dna add rrset"); rrset = lxl_palloc(pool, sizeof(lxl_dns_rrset_t) + rr->nlen); if (rrset == NULL) { return -1; } rdata = lxl_palloc(pool, sizeof(lxl_dns_rdata_t) + rr->rdlength); if (rdata == NULL) { //lxl_free(rrset); return -1; } rrset->soa_nlen = rr->soa_nlen; rrset->soa_flags = rr->soa_flags; rrset->expires_sec = rr->expires_sec; rrset->update_flags = rr->update_flags; rrset->type = rr->type; rrset->ttl = rr->ttl; rrset->nlen = rr->nlen; memcpy(rrset->name, rr->name, rr->nlen); rdata->update_flags = rr->update_flags; rdata->rdlength = rr->rdlength; memcpy(rdata->rdata, rr->rdata, rr->rdlength); lxl_list_init(&rrset->rdata_list); lxl_list_add_tail(&rrset->rdata_list, &rdata->list); lxl_list_add_tail(&zone->rrset_list, &rrset->list); return 0; } rrset->update_flags = rr->update_flags; rrset->ttl = rr->ttl; rrset->expires_sec = rr->expires_sec; rdata = lxl_dns_rdata_find(&rrset->rdata_list, rr->rdata, rr->rdlength); if (rdata == NULL) { lxl_log_debug(LXL_LOG_DEBUG_DNS, 0, "dns add rdata"); rdata = lxl_palloc(pool, sizeof(lxl_dns_rdata_t) + rr->rdlength); if (rdata == NULL) { return -1; } rdata->update_flags = rr->update_flags; rdata->rdlength = rr->rdlength; memcpy(rdata->rdata, rr->rdata, rr->rdlength); lxl_list_add_tail(&rrset->rdata_list, &rdata->list); } else { rdata->update_flags = rr->update_flags; } return 0; }
void lxl_dns_data_rebuild(void) { lxl_pool_t *new_pool; lxl_list_t *list1, *list2; lxl_dns_rr_t rr; lxl_dns_zone_t *old_zone, *new_zone; lxl_dns_rrset_t *rrset; lxl_dns_rdata_t *rdata; old_zone = lxl_dns_zone_find(LXL_DNS_ROOT_LABEL, LXL_DNS_ROOT_LEN); if (old_zone == NULL) { lxl_log_error(LXL_LOG_ERROR, 0, "dns data rebuild root not find"); return; } lxl_log_error(LXL_LOG_INFO, 0, "dns data rebuild create new pool"); new_pool = lxl_create_pool(LXL_DEFAULT_POOL_SIZE); if (new_pool == NULL) { lxl_log_error(LXL_LOG_ERROR, 0, "dns data rebuild create pool failed"); return; } lxl_log_error(LXL_LOG_INFO, 0, "dns data rebuild init hash"); if (lxl_hash_init(&lxl_dns_hash, new_pool, lxl_dns_hash.nelts, lxl_dns_hash.key) == -1) { lxl_log_error(LXL_LOG_ERROR, 0, "dns init hash failed"); return; } new_zone = lxl_palloc(new_pool, sizeof(lxl_dns_zone_t)); if (new_zone == NULL) { return; } lxl_list_init(&new_zone->rrset_list); new_zone->update_sec = lxl_current_sec; for (list1 = lxl_list_head(&old_zone->rrset_list); list1 != lxl_list_sentinel(&old_zone->rrset_list); list1 = lxl_list_next(list1)) { rrset = lxl_list_data(list1, lxl_dns_rrset_t, list); rr.type = rrset->type; rr.ttl = rrset->ttl; rr.nlen = rrset->nlen; rr.name = rrset->name; rr.soa_nlen = 0; rr.soa_flags = LXL_DNS_RRSET_NORMAL_TYPE; rr.expires_sec = lxl_current_sec + rrset->ttl; rr.update_flags = LXL_DNS_RR_NORMAL_TYPE; for (list2 = lxl_list_head(&rrset->rdata_list); list2 != lxl_list_sentinel(&rrset->rdata_list); list2 = lxl_list_next(list2)) { rdata = lxl_list_data(list2, lxl_dns_rdata_t, list); rr.rdlength = rdata->rdlength; rr.rdata = rdata->rdata; if (lxl_dns_rr_add(new_pool, new_zone, &rr) == -1) { lxl_log_error(LXL_LOG_ERROR, 0, "dns data rebuild add rr failed"); return; } } } lxl_log_error(LXL_LOG_INFO, 0, "dns data rebuild add root zone"); if (lxl_dns_zone_add(LXL_DNS_ROOT_LABEL, LXL_DNS_ROOT_LEN, new_zone) == -1) { lxl_log_error(LXL_LOG_ERROR, 0, "dns data rebuild add root zone failed"); //return; } lxl_log_error(LXL_LOG_INFO, 0, "dns data rebuild destory pool"); lxl_destroy_pool(lxl_dns_pool); lxl_dns_pool = new_pool; lxl_log_error(LXL_LOG_INFO, 0, "dns data rebuild succeed"); }
static int lxl_dfss_core_init(lxl_cycle_t *cycle) { int s; unsigned i, n; char *base_path = LXL_DFSS_DATA_PATH; char dir[256]; struct ifreq ifr; lxl_uint_t nelts; lxl_listening_t *ls; lxl_connection_t *c; lxl_log_debug(LXL_LOG_DEBUG_DFS, 0, "dfss core init"); s = socket(AF_INET, SOCK_DGRAM, 0); if (s == -1) { lxl_log_error(LXL_LOG_ERROR, errno, "socket() failed"); return -1; } strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name)); if (ioctl(s, SIOCGIFHWADDR, &ifr) != 0) { lxl_log_error(LXL_LOG_ERROR, errno, "ioctl() failed"); return -1; } snprintf(lxl_dfss_uid, sizeof(lxl_dfss_uid), "%02x%02x%02x%02x%02x%02x%04x", (unsigned char) ifr.ifr_hwaddr.sa_data[0], (unsigned char) ifr.ifr_hwaddr.sa_data[1], (unsigned char) ifr.ifr_hwaddr.sa_data[2], (unsigned char) ifr.ifr_hwaddr.sa_data[3], (unsigned char) ifr.ifr_hwaddr.sa_data[4], (unsigned char) ifr.ifr_hwaddr.sa_data[5], lxl_dfss_ip_port.port); lxl_log_error(LXL_LOG_INFO, 0, "dfss uid %s", lxl_dfss_uid); if (lxl_hash1_init(&lxl_dfss_fid_hash, 1000000, lxl_hash_key) == -1) { return -1; } lxl_list_init(&lxl_dfss_sync_pull_list); lxl_list_init(&lxl_dfss_sync_push_list); lxl_dfss_temp_pool = lxl_create_pool(LXL_DEFAULT_POOL_SIZE); if (lxl_dfss_temp_pool == NULL) { return -1; } if (lxl_hash_init(&lxl_dfss_fid_phash, lxl_dfss_temp_pool, 10000000, lxl_hash_key) == -1) { goto failed; } if (access(base_path, R_OK|W_OK|X_OK) != 0) { //lxl_log_error(LXL_LOG_INFO, errno, "access(R_OK|W_OK|X_OK) %s failed", base_path); if (errno == ENOENT) { if (mkdir(base_path, 0744) != 0) { //if (mkdir(base_path, 0774) != 0) { lxl_log_error(LXL_LOG_EMERG, errno, "mkdir(%s) failed", base_path); goto failed; } } else { lxl_log_error(LXL_LOG_EMERG, errno, "access(R_OK|W_OK|X_OK) %s failed", base_path); goto failed; } } n = LXL_DFSS_DIR_COUNT; for (i = 0; i < n; ++i) { snprintf(dir, 256, "%s%02X", base_path, i); /* access qu diao panduan cuowu */ if (access(dir, R_OK|W_OK|X_OK) != 0 && errno == ENOENT) { if (errno == ENOENT) { if (mkdir(dir, 0777) != 0) { lxl_log_error(LXL_LOG_EMERG, errno, "mkdir(%s) failed", dir); goto failed; } } else { lxl_log_error(LXL_LOG_EMERG, errno, "access(R_OK|W_OK|X_OK) %s failed", dir); goto failed; } } } lxl_dfss_collect_fid(LXL_DFSS_DATA_PATH); lxl_dfss_collect_sync_push_fid(LXL_DFSS_SYSNLOG_PATH); lxl_dfss_dir_seed = 0; lxl_dfss_file_count = 0; //lxl_dfss_ip_port.ip = 1024; //lxl_dfss_ip_port.port = 1024; nelts = lxl_array_nelts(&cycle->listening); if (nelts == 0) { lxl_log_error(LXL_LOG_ERROR, 0, "listening nelts is 0"); return -1; } ls = lxl_array_data(&cycle->listening, lxl_listening_t, 0); c = lxl_get_connection(-1); if (c == NULL) { return -1; } c->listening = ls; memset(&lxl_dfss_report_state_event, 0x00, sizeof(lxl_event_t)); lxl_dfss_report_state_event.data = c; lxl_dfss_report_state_event.handler = lxl_dfss_report_state_handler; //lxl_add_timer(&lxl_dfss_report_state_event, 100); c = lxl_get_connection(-1); if (c == NULL) { return -1; } c->listening = ls; memset(&lxl_dfss_sync_push_event, 0x00, sizeof(lxl_event_t)); lxl_dfss_sync_push_event.data = c; lxl_dfss_sync_push_event.handler = lxl_dfss_sync_push_handler; //lxl_add_timer(&lxl_dfss_report_fid_event, 60000); c = lxl_get_connection(-1); if (c == NULL) { return -1; } c->listening = ls; memset(&lxl_dfss_sync_pull_event, 0x00, sizeof(lxl_event_t)); lxl_dfss_sync_pull_event.data = c; lxl_dfss_sync_pull_event.handler = lxl_dfss_sync_pull_handler; c = lxl_get_connection(-1); if (c == NULL) { return -1; } c->listening = ls; memset(&lxl_dfss_sync_fid_event, 0x00, sizeof(lxl_event_t)); lxl_dfss_sync_fid_event.data = c; lxl_dfss_sync_fid_event.handler = lxl_dfss_sync_fid_handler; //lxl_dfss_sync_fid_handler(&lxl_dfss_sync_fid_event); lxl_dfss_sync_fid(); return 0; failed: lxl_destroy_pool(lxl_dfss_temp_pool); return -1; }