示例#1
0
char *
lxl_errlog_module_init_conf(lxl_cycle_t *cycle, void *conf)
{
    lxl_log_conf_t *lcf = (lxl_log_conf_t *) conf;

    int fd;

    fd = cycle->log->fd;
    if (!lcf->use_stderr) {
        lxl_log_debug(LXL_LOG_DEBUG_CORE, 0, "open log file: %s", lcf->file.data);
        cycle->log->fd = open(lcf->file.data, O_CREAT|O_WRONLY|O_APPEND, 0666);
        if (cycle->log->fd == -1) {
            lxl_log_error(LXL_LOG_EMERG, errno, "open(%s) failed", lcf->file.data);
            return LXL_CONF_ERROR;
        }
    } else {
        cycle->log->fd = STDERR_FILENO;
    }

    if (close(fd) == -1) {
        lxl_log_error(LXL_LOG_ALERT, errno, "close() build-in log failed");
    }

    cycle->log->use_flush = lcf->use_flush;
    cycle->log->use_stderr = lcf->use_stderr;
    cycle->log->error_level = lcf->error_level;
    cycle->log->debug_level = lcf->debug_level;
    memcpy(cycle->log->file, lcf->file.data, lcf->file.len + 1);

    return LXL_CONF_OK;
}
示例#2
0
static int
lxl_dfss_collect_sync_push_fid(char *base_path)
{
	size_t		 	len;
	DIR	  		   *dir;
	FILE  		   *fp;
	struct dirent  *de;
	char   			buf[1024], b[1024];
	lxl_dfs_fid_t  *dfs_fid;

	lxl_log_error(LXL_LOG_INFO, 0, "dfss collect sync push fid %s", base_path);

	dir = opendir(base_path);
	if (dir == NULL) {
		lxl_log_error(LXL_LOG_ERROR, errno, "opendir(%s) failed", base_path);
		return -1;
	}

	while ((de = readdir(dir)) != NULL) {
		if (S_ISREG(de->d_type)
			&& strncmp(de->d_name, LXL_DFSS_SYNCLOG_FILE_PREFIX, strlen(LXL_DFSS_SYNCLOG_FILE_PREFIX)) == 0) {
			fp = fopen(de->d_name, "r");
			if (fp == NULL) {
				lxl_log_error(LXL_LOG_ERROR, errno, "fopen(%s) failed", de->d_name);
				goto failed;
			}

			while (fgets(buf, sizeof(buf), fp) != NULL) {
				len = strlen(buf);		
				buf[len - 1] = '\0';
				dfs_fid = lxl_alloc(sizeof(lxl_dfs_fid_t));
				if (dfs_fid == NULL) {
					fclose(fp);
					goto failed;
				}

				dfs_fid->fid.data = lxl_alloc(len + 1);
				if (dfs_fid->fid.data == NULL) {
					fclose(fp);
					goto failed;
				}

				lxl_str_memcpy(&dfs_fid->fid, buf, len);
				lxl_list_add_tail(&lxl_dfss_sync_push_list, &dfs_fid->list);
			}

			fclose(fp);
		}
	}

	closedir(dir);

	return 0;

failed:

	closedir(dir);

	return -1;
}
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;
}
示例#4
0
static int	 
lxl_event_module_init(lxl_cycle_t *cycle)
{
	void 			  ***cf;
	struct rlimit		 rlmt;
	lxl_int_t			 limit;
	lxl_core_conf_t		*ccf;
	lxl_event_conf_t	*ecf;
	
	ccf = (lxl_core_conf_t *) lxl_get_conf(cycle->conf_ctx, lxl_core_module);
	cf = lxl_get_conf(cycle->conf_ctx, lxl_events_module);
	ecf = (*cf)[lxl_event_core_module.ctx_index];
	lxl_log_error(LXL_LOG_INFO, 0, "using the \"%s\" event method", ecf->name);

	if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
		lxl_log_error(LXL_LOG_ALERT, errno, "getrlimit(RLIMIT_NOFILE) failed, ignored");
	} else {
		if (ecf->connections > (lxl_uint_t) rlmt.rlim_cur
			&& (ccf->rlimit_nofile == LXL_CONF_UNSET || ecf->connections > (lxl_uint_t) ccf->rlimit_nofile)) {
			limit = (ccf->rlimit_nofile == LXL_CONF_UNSET) ? (lxl_int_t) rlmt.rlim_cur : ccf->rlimit_nofile;
			lxl_log_error(LXL_LOG_WARN, 0, "%lu worker_connections exceed open file resouse limit: %ld", 
							ecf->connections, limit);
		}
	}

	return 0;
}
示例#5
0
lxl_int_t
lxl_event_connect_peer_udp(lxl_connection_t **c)
{
	int fd;
	lxl_connection_t *connection;

	lxl_log_debug(LXL_LOG_DEBUG_EVENT, 0, "event udp connect peer");
	fd = socket(AF_INET, SOCK_DGRAM, 0);
	if (fd == -1) {
		lxl_log_error(LXL_LOG_ERROR, errno, "socket() failed");
		return -1;
	}

	lxl_log_debug(LXL_LOG_DEBUG_EVENT, 0, "socket %d", fd);
	connection = lxl_get_connection(fd);
	if (connection == NULL) {
		lxl_log_error(LXL_LOG_WARN, 0, "get connection() failed");
		close(fd);
		return -1;
	}
	
	// nonblock
	connection->udp = 1;
	connection->closefd = 1;
	connection->fd = fd;
	//connection->sockaddr = *sa;
	//connection->socklen = len;
	connection->recv = lxl_recvfrom;
	connection->send = lxl_sendto;
	*c = connection;

	return 0;
}
示例#6
0
static int 
lxl_dns_load_named_root(char *file)
{
	FILE *fp;
	char buf[512];
	char argv[5][64];
	lxl_int_t  argc;
	lxl_uint_t line;

	fp = fopen(file, "r");
	if (fp == NULL) {
		lxl_log_error(LXL_LOG_ALERT, errno, "fopen(%s) failed", file);
		return -1;
	}

	line = 0;
	while ((fgets(buf, sizeof(buf), fp)) != NULL) {
		++line;
#if LXL_DEBUG
		//lxl_log_debug(LXL_LOG_DEBUG_DNS, 0, "source buf %s, line %lu", buf, line);
#endif
		lxl_strtrim(buf, " \t\r\n");
		if (buf[0] == ';' || buf[0] == '#' || buf[0] == '\0') {
			continue;
		} else {
#if LXL_DEBUG
			//lxl_log_debug(LXL_LOG_DEBUG_DNS, 0, "line %lu trim buf: %s", line, buf);
#endif
			// lxl_strtrim(buf, "#");
		//	lxl_dns_parse(buf, argv);
			argc = sscanf(buf, "%s %s %s %s %s", argv[0], argv[1], argv[2], argv[3], argv[4]);
			if (argc == -1) {
				lxl_log_error(LXL_LOG_ALERT, errno, "ssconf failed");
				continue;
			}
			
			if (lxl_dns_parse_root_rr(argc, argv, line, NULL) == -1) {
				lxl_log_error(LXL_LOG_ERROR, 0, "dns parse root rr failed");
				fclose(fp);
				return -1;
			}
		}
	}

	fclose(fp);

	return 0;
}
示例#7
0
int
lxl_dfss_parse_request(lxl_dfss_request_t *r, char *data) 
{
	uint16_t  new_qtype;

	/* time suport*/
	r->new_request = 1;
	snprintf(r->rid, sizeof(r->rid), "%08lx%08x", lxl_current_sec, lxl_dfss_request_seed);
	++lxl_dfss_request_seed;

	r->request_header.body_n = ntohl(*((uint32_t *) data));
	r->request_header.flen = ntohs(*((uint16_t *) (data + 4)));
	if (r->first_qtype) {
		new_qtype = ntohs(*(uint16_t *) (data + 6));
		if (r->request_header.qtype != new_qtype) {
			lxl_log_error(LXL_LOG_WARN, 0, "dfss new qtype: %04X not same first qtype: %04X", new_qtype, r->request_header.qtype);
			// rcode r->header.rcode = 1
			return -1;
		}
	} else {
		r->request_header.qtype = ntohs(*((uint16_t *) (data + 6)));
		r->first_qtype = 1;
	}

	//snprintf(r->rid, 17, "%08lX%08X", lxl_current_sec, lxl_dfss_request_seed);
	//++lxl_dfss_request_seed;
	// switch(qtype)
	return 0;
}
示例#8
0
static void 
lxl_dfss_writer(lxl_dfss_request_t *r)
{
	int				   rc;
	ssize_t			   n;
	lxl_buf_t		  *b;
	lxl_event_t  	  *wev;
	lxl_connection_t  *c;

	lxl_log_debug(LXL_LOG_DEBUG_DFS, 0, "dfss writer handler");

	c = r->connection;
	wev = c->write;
	b = r->out;

	if (wev->timedout) {
		// delayed
		lxl_log_error(LXL_LOG_INFO, 0, "client timed out");
		c->timedout = 1;
		lxl_dfss_finalize_request(r, LXL_DFS_REQUEST_TIMEOUT);
		return;
	}

	n = c->send(c, b->pos, b->last - b->pos);
	if (n == LXL_ERROR) {
		c->error = 1;
		lxl_dfss_close_request(r, 0);
		return;
	}

	if (n == LXL_EAGAIN) {
		goto done;
	}

	b->pos += n;
	if (b->pos < b->last) {
		goto done;
	}

	lxl_log_debug(LXL_LOG_DEBUG_DFS, 0, "dfss writer done");

	lxl_dfss_finalize_request(r, LXL_OK);

	return;

done:

	if (!wev->timer_set) {
		lxl_add_timer(wev, 6 * 1000);
	}

	if (lxl_handle_write_event(wev, 0) != 0) {
		lxl_dfss_close_request(r, 0);
	}

	return;
}
示例#9
0
void
lxl_dfss_server_start(void)
{
	lxl_log_error(LXL_LOG_INFO, 0, "dfss server start");

	lxl_dfss_update_system_state();
	lxl_add_timer(&lxl_dfss_report_state_event, 100);
	lxl_add_timer(&lxl_dfss_sync_push_event, 100);
}
示例#10
0
static char *
lxl_event_init_conf(lxl_cycle_t *cycle, void *conf)
{
	if (lxl_get_conf(cycle->conf_ctx, lxl_events_module) == NULL) {
		lxl_log_error(LXL_LOG_EMERG, 0, "no \"events\" section in configueation");
		return LXL_CONF_ERROR;
	}

	return LXL_CONF_OK;
}
示例#11
0
void 
lxl_dfss_sync_pull(void)
{
	lxl_log_error(LXL_LOG_INFO, 0, "dfss sync pull");
	if (lxl_list_empty(&lxl_dfss_sync_pull_list)) {
		lxl_dfss_server_start();
	} else {
		lxl_dfss_sync_pull_handler(&lxl_dfss_sync_pull_event);
	}
}
示例#12
0
static int	 
lxl_daemon(void)
{
	int fd;

	switch (fork()) {
	case -1:
		lxl_log_error(LXL_LOG_EMERG, errno, "fork failed");
		return -1;

	case 0:
		break;

	default:
		exit(0);
	}

	lxl_pid = getpid();

	if (setsid() == -1) {
		lxl_log_error(LXL_LOG_EMERG, errno, "setsid() failed");
		return -1;
	}

	umask(0);
	
	fd = open("/dev/null", O_RDWR);
	if (fd == -1) {
		lxl_log_error(LXL_LOG_EMERG, errno, "open(/dev/null) failed");
		return -1;
	}

	if (dup2(fd, STDIN_FILENO) == -1) {
		lxl_log_error(LXL_LOG_EMERG, errno, "dup2(STDIN_FILENO) failed");
		return -1;
	}

	if (dup2(fd, STDOUT_FILENO) == -1) {
		lxl_log_error(LXL_LOG_EMERG, errno, "dup2(STDOUT_FILENO) failed");
		return -1;
	}

#if 0
	if (dup2(fd, STDERROR_FILENO) == -1) {
		lxl_log_error(LXL_LOG_EMERG, errno, "dup2(STDERROR_FILENO) failed");
		return -1;
	}
#endif 

	if (fd > STDERR_FILENO) {
		if (close(fd) == -1) {
			lxl_log_error(LXL_LOG_EMERG, errno, "close failed");
			return -1;
		}
	}
	
	return 0;
}
示例#13
0
static void
lxl_dfss_process_request(lxl_dfss_request_t *r) 
{
	lxl_int_t		   rc;
	lxl_connection_t  *c;

	lxl_log_debug(LXL_LOG_DEBUG_DFS, 0, "dfss process request");

	c = r->connection;
	if (c->read->timer_set) {
		lxl_del_timer(c->read);
	}
	
	c->read->handler = lxl_dfss_request_handler;
	c->write->handler = lxl_dfss_request_handler;
	r->read_event_handler = lxl_dfss_block_reading;

	//r->read_event_handler =  block_reading
	switch (r->request_header.qtype) {
	case LXL_DFS_UPLOAD:
		r->handler = lxl_dfss_upload_done_handle_request;
		rc = lxl_dfss_read_client_request_body(r, lxl_dfss_tracker_init);

		break;

	case LXL_DFS_UPLOAD_SC:
		r->handler  = lxl_dfss_upload_sc_sync_handle_request;
		rc = lxl_dfss_read_client_request_body(r, lxl_dfss_tracker_init);
	
		break;
	
	case LXL_DFS_STORAGE_SYNC_PUSH:
		rc = lxl_dfss_read_client_request_body(r, lxl_dfss_sync_push_done_handle_request);

		break;

	case LXL_DFS_DOWNLOAD:
		rc = lxl_dfss_read_client_request_body(r, lxl_dfss_download_handle_request);

		break;

	case LXL_DFS_DELETE:
		rc = LXL_DFS_NOT_IMPLEMENTED;
		break;

	default:
		lxl_log_error(LXL_LOG_ERROR, 0, "dfss Unknow request qtype: %04X", r->request_header.qtype);
		break;
	} 

	if (rc > LXL_DFS_OK) {
		lxl_dfss_finalize_request(r, rc);
	}
}
示例#14
0
static char *
lxl_event_core_init_conf(lxl_cycle_t *cycle, void *conf)
{
	lxl_event_conf_t *ecf = (lxl_event_conf_t *) conf;

	lxl_uint_t i;
	lxl_module_t *module;
	lxl_event_module_t *event_module;

	module = NULL;

#if LXL_HAVE_EPOLL

		module = &lxl_epoll_module;

#endif 

#if LXL_HAVE_KQUEUE

		/* module = &lxl_kqueue_module */

#endif

	if (module == NULL) {
		for (i = 0; lxl_modules[i]; ++i) {
			if (lxl_modules[i]->type != LXL_EVENT_MODULE) {
				continue;
			}

			event_module = lxl_modules[i]->ctx;
			if (event_module->name->len == event_core_name.len 
					&& memcmp(event_module->name->data, event_core_name.data, event_core_name.len) == 0) {
				continue;
			}

			module = lxl_modules[i];
			break;
		}
	}

	if (module == NULL) {
		lxl_log_error(LXL_LOG_EMERG, 0, "no events module found");
		return LXL_CONF_ERROR;
	}

	lxl_conf_init_uint_value(ecf->connections, DEFAULT_CONNECTIONS);
	cycle->connection_n = ecf->connections;
	lxl_conf_init_uint_value(ecf->use, module->ctx_index);
	lxl_conf_init_value(ecf->accept_mutex, 0);
	
	return LXL_CONF_OK;
}
示例#15
0
static void 
lxl_dfss_process_request_header(lxl_event_t *rev) 
{
	//int rc;
	size_t				 nread;
	ssize_t 			 n;
	lxl_buf_t 			*b;
	lxl_connection_t 	*c;
	lxl_dfss_request_t  *r;
	
	c = rev->data;
	r = c->data;
	lxl_log_debug(LXL_LOG_DEBUG_DFS, 0, "dfss process request header");

	if (rev->timedout) {
		lxl_log_error(LXL_LOG_ERROR, 0, "client timed out");
		c->timedout = 1;
		lxl_dfss_close_request(r, LXL_DFS_REQUEST_TIMEOUT);
		return;
	}

	b = r->header_buf;
	nread = b->last - b->pos;
	//lxl_log_debug(LXL_LOG_DEBUG_DFS, 0, "dfss read byte:%lu", b->last - b->pos);
	if (nread < sizeof(lxl_dfs_request_header_t)) {
		n = lxl_dfss_read_request_header(r);
		if (n == LXL_EAGAIN || n == LXL_ERROR) {
			return;
		}

		nread = b->last - b->pos;
	}

	lxl_log_debug(LXL_LOG_DEBUG_DFS, 0, "dfss read byte:%lu", nread);
	if (nread >= sizeof(lxl_dfs_request_header_t)) {
		if (lxl_dfss_parse_request(r, b->pos) == -1) {
			lxl_dfss_finalize_request(r, r->response_header.rcode);
			return;
		}

		lxl_log_debug(LXL_LOG_DEBUG_DFS, 0, "dfss process request: %s, %u, %hu, 0x%04x", 
					r->rid, r->request_header.body_n, r->request_header.flen, r->request_header.qtype);
		// b->pos = b->start;
		// b->last = b->start;
		//b->last = b->pos;
		b->pos += 8;
		lxl_dfss_process_request(r);
	}
}
示例#16
0
static void
lxl_start_worker_process(lxl_cycle_t *cycle, lxl_int_t n)
{
	pid_t pid;
	lxl_int_t i;
	
	lxl_log_error(LXL_LOG_INFO, 0, "start worker process");
	for (i = 0; i < n; ++i) {
		pid = fork();
		switch (pid) {
			case -1:
				lxl_log_error(LXL_LOG_ALERT, errno, "fork() failed");
				lxl_log_flush();
				return;

			case 0:
				lxl_pid = getpid();
				lxl_worker_process_cycle(cycle, i);

			default:
				break;
		}
	}
}
示例#17
0
static void 
lxl_worker_process_init(lxl_cycle_t *cycle, lxl_int_t worker)
{
	lxl_uint_t i;

	lxl_log_error(LXL_LOG_INFO, 0, "worker process init");

	for (i = 0; lxl_modules[i]; ++i) {
		if (lxl_modules[i]->init_process) {
			if (lxl_modules[i]->init_process(cycle) == -1) {
				/* fatal */
				lxl_log_flush();
				exit(2);
			}
		}
	}
}
示例#18
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;
}
示例#19
0
static void 
lxl_close_accepted_connection(lxl_connection_t *c, lxl_uint_t flags)
{
	lxl_log_debug(LXL_LOG_DEBUG_EVENT, 0, "close accepted connection socket %d", c->fd);

	lxl_free_connection(c);

	if (flags == LXL_CONNECTION_CLOSE_SOCKET) {
		if (close(c->fd) == -1) {
			lxl_log_error(LXL_LOG_ALERT, errno, "close socket %d failed", c->fd);
		}
	}
	c->fd = -1;

	if (c->pool) {
		lxl_destroy_pool(c->pool);
	}
}
示例#20
0
static int
lxl_send_lowat(lxl_connection_t *c, size_t lowat)
{
	int sndlowat;

	if (c->sndlowat || lowat == 0) {
		return 0;
	}

	sndlowat = (int) lowat;
	if (setsockopt(c->fd, SOL_SOCKET, SO_SNDLOWAT, &sndlowat, sizeof(int)) == -1) {
		lxl_log_error(LXL_LOG_ERROR, errno, "setsockopt(SO_SNDLOWAT) failed");
		return -1;
	}

	c->sndlowat = 1;

	return 0;
}
示例#21
0
static ssize_t 
lxl_dfss_read_request_header(lxl_dfss_request_t *r) 
{
	ssize_t 		   n, rc;
	lxl_event_t 	  *rev;
	lxl_connection_t  *c;

	c = r->connection;
	rev = c->read;

	if (rev->ready) {
		n = c->recv(c, r->header_buf->last, r->header_buf->end - r->header_buf->last);
	} else {
		n = LXL_EAGAIN;
	} 

	switch (n) {
	case LXL_EAGAIN:
		rc = LXL_EAGAIN;
		//  lxl_add_timer;
		if (lxl_handle_read_event(rev) != 0) {
			lxl_dfss_close_request(r, LXL_DFS_SERVER_ERROR);
		}
		break;

	case 0:
		lxl_log_error(LXL_LOG_INFO, 0, "client prematurely closed connection");
	case LXL_ERROR:
		// base request
		c->error = 1;
		//lxl_dfss_finalize_request(r, LXL_ERROR);
		lxl_dfss_close_request(r, 0);
		rc = LXL_ERROR;
		break;

	default:
		r->header_buf->last += n;
		rc = n;
	}

	return rc;
} 
示例#22
0
static void
lxl_dfst_storage_init_request(lxl_dfst_request_t *r)
{
	lxl_dfst_upstream_t			  *u;
	lxl_dfst_upstream_srv_conf_t  *uscf;

	lxl_dfst_storage_create_request(r);

	u = r->storage;
	uscf = u->conf->upstream;

	if (uscf == NULL) {
		lxl_log_error(LXL_LOG_ALERT, 0, "no upstream configuration");
		return;
	}

	if (uscf->peer.init(r, uscf, u) != 0) {
		// lxl_dfst
	}

	lxl_dfst_storage_connect(r, u);
}
示例#23
0
lxl_int_t
lxl_hash_sample(lxl_uint_t nelts)
{
	char name[64];
	lxl_uint_t i, len, *value;
	lxl_pool_t *pool;
	lxl_hash_t hash;

	pool = lxl_create_pool(1024);
	lxl_hash_init(&hash, pool, nelts, lxl_hash_key);
	for (i = 0; i < 40; ++i) {
		value = lxl_alloc(sizeof(lxl_uint_t));
		*value = i;
		len = snprintf((char *) name, sizeof(name), "www.%lutaobao%lu.com", i, 40 - i);
		if (lxl_hash_add(&hash, name, len, value) == -1) {
			lxl_log_error(LXL_LOG_DEBUG, 0, "add exist value %lu, len %lu", *value, len);
		} else {
			lxl_log_error(LXL_LOG_DEBUG, 0, "add value %lu, len %lu", *value, len);
		}
	}

	for (i = 8; i < 18; ++i) {
		len = snprintf((char *) name, sizeof(name), "www.%lutaobao%lu.com", i, 40 - i);
		value = lxl_hash_del(&hash, name, len);
		if (value) {
			lxl_log_error(LXL_LOG_DEBUG,0, "hash delete name %s, key %lu", name, *value);
			//lxl_free(value);
		} else {
			lxl_log_error(LXL_LOG_DEBUG,0, "hash not find name %s", name);
		}
	}

	for (i = 0; i < 40; ++i) {
		len = snprintf((char *) name, sizeof(name), "www.%lutaobao%lu.com", i, 40 - i);
		value = lxl_hash_find(&hash, name, len);
		if (value) {
			lxl_log_error(LXL_LOG_DEBUG, 0, "hash find value %lu", *value);
		} else {
			lxl_log_error(LXL_LOG_DEBUG,0, "hash not find name %s", name);
		}
	}
	
	return 0;
}
示例#24
0
static void
lxl_dfst_storage_connect(lxl_dfst_request_t *r, lxl_dfst_upstream_t *u)
{
	int				   rc;
	lxl_connection_t  *c;

	rc = lxl_event_connect_peer(&u->peer);
	lxl_log_debug(LXL_LOG_DEBUG_DFS, 0, "dfst storage connect: %d", rc);
	if (rc == LXL_ERROR) {
		// finialize
		return;
	}

	if (rc == LXL_BUSY) {
		lxl_log_error(LXL_LOG_ERROR, 0, "dfst no live storages");
		lxl_dfst_storage_next(r, u, LXL_DFST_UPSTREAM_FT_NOLIVE);
		return;
	}

	if (rc == LXL_DECLINED) {
		lxl_dfst_storage_next(r, u, LXL_DFST_UPSTREAM_FT_ERROR);
		return;
	}

	c = u->peer.connection;
	c->data = r;
	c->write->handler = lxl_dfst_storage_handler;
	c->read->handler = lxl_dfst_storage_handler;

	if (rc == LXL_EAGAIN) {
		lxl_add_timer(c->write, 3000);
		return;
	}

	lxl_dfst_storage_send_request(r, u);
}
示例#25
0
void 
lxl_event_accept_udp(lxl_event_t *ev)
{
#define LXL_UDP_SIZE	512
	ssize_t n;
	char buffer[LXL_UDP_SIZE], ip[INET6_ADDRSTRLEN];
	socklen_t socklen;
	struct sockaddr sockaddr;
	struct sockaddr_in *sin;
	lxl_listening_t *ls;
	lxl_connection_t *c, *lc;

	lc = ev->data;
	ls = lc->listening;
	for (; ;) {
		socklen = sizeof(struct sockaddr);	
		n = recvfrom(lc->fd, buffer, LXL_UDP_SIZE, 0, &sockaddr, &socklen);
		if (n != -1) {
			c = lxl_get_connection(lc->fd);
			if (c == NULL) {
				return;
			}

			c->pool = lxl_create_pool(ls->pool_size);
			if (c->pool == NULL) {
				lxl_close_accepted_connection(c, LXL_CONNECTION_NOT_CLOSE_SOCKET);
				return;
			}

			c->buffer = lxl_create_temp_buf(c->pool, LXL_UDP_SIZE);
			if (c->buffer == NULL) {
				lxl_close_accepted_connection(c, LXL_CONNECTION_NOT_CLOSE_SOCKET);
				return;
			}

			memcpy(c->buffer->pos, buffer, n);
			c->buffer->last += n;
			c->udp = 1;
			//c->closefd = 0;
			c->fd_noclose = 1;
			c->sockaddr = sockaddr;
			c->socklen = socklen;
			//c->recv = lxl_recvfrom;	
			c->send = lxl_sendto;	/* udp only send */
			c->listening = ls;
			// c->write->ready = 1;

			sin = (struct sockaddr_in *) &sockaddr;
			lxl_log_debug(LXL_LOG_DEBUG_EVENT, 0, "recvfrom() %s fd:%d nbyte:%ld", 
						inet_ntop(AF_INET, &sin->sin_addr, ip, INET6_ADDRSTRLEN), lc->fd, n);

			ls->handler(c);
		} else {
			if (errno == EAGAIN || errno == EWOULDBLOCK) {
				lxl_log_debug(LXL_LOG_DEBUG_EVENT, errno, "recvfrom() not ready");
				return;
			} else if (errno == EINTR) {
				lxl_log_debug(LXL_LOG_DEBUG_EVENT, errno, "recvfrom() failed");
				continue;
			} else {
				lxl_log_error(LXL_LOG_ALERT, errno, "recvfrom() failed");
				return;
			}
		}
	}
}
示例#26
0
void
lxl_event_accept(lxl_event_t *ev)
{
	int 				 fd;
	char 				 ip[INET6_ADDRSTRLEN];
	socklen_t 			 socklen;
	struct sockaddr 	 sockaddr;
	struct sockaddr_in  *sin;
	lxl_listening_t 	*ls;
	lxl_connection_t 	*c, *lc;
	
	lc = ev->data;
	ls = lc->listening;
	ev->ready = 0;	// ?
	for (; ;) {
		// accept4();
		//socklen = sizeof(struct sockaddr);
		fd = accept(lc->fd, &sockaddr, &socklen);
		if (fd != -1) {
			c = lxl_get_connection(fd);
			if (c == NULL) {
				if (close(fd) == -1) {
					lxl_log_error(LXL_LOG_ALERT, errno, "close() socket failed");
				}
				return;
			}

			c->pool = lxl_create_pool(ls->pool_size);
			if (c->pool == NULL) {
				lxl_close_accepted_connection(c, LXL_CONNECTION_CLOSE_SOCKET);
				return;
			}

			if (lxl_nonblocking(fd) == -1) {
				lxl_log_error(LXL_LOG_ALERT, errno, "nonblocking failed");
				lxl_close_accepted_connection(c, LXL_CONNECTION_CLOSE_SOCKET);
				return;
			}
	
			c->udp = 0;
			//c->closefd = 1;
			c->recv = lxl_recv;
			c->send = lxl_send;
			c->listening = ls;
			c->write->ready = 1;
			
			sin = (struct sockaddr_in *) &sockaddr;
			lxl_log_debug(LXL_LOG_DEBUG_EVENT, 0, "accept %s fd:%d", 
					inet_ntop(AF_INET, &sin->sin_addr, ip, INET6_ADDRSTRLEN), fd);

			ls->handler(c); 
		} else {
			if (errno == EAGAIN || errno == EWOULDBLOCK) {
				lxl_log_debug(LXL_LOG_DEBUG_EVENT, errno, "accept() not ready");
				return;
			} else if (errno == EINTR || errno == ECONNABORTED) {
				lxl_log_error(LXL_LOG_WARN, errno, "accept() failed");
				continue;
			} else {
				lxl_log_error(LXL_LOG_ERROR, errno, "accept() failed");
			}
			
			return;
		}
	}
}
示例#27
0
void 
lxl_dns_data_dump(void)
{
	int fd;
	char file[32];
	char buffer[4096];

	snprintf(file, sizeof(file), "dump_zone_data.%u", lxl_pid);
	lxl_log_error(LXL_LOG_INFO, 0, "dns dump data to file %s", file);
	fd = open(file, O_CREAT|O_WRONLY|O_TRUNC, 0666);
	if (fd == -1) {
		lxl_log_error(LXL_LOG_ALERT, errno, "open(%s) failed", file);
		return;
	}

	ssize_t n;
	char zname[256];
	lxl_uint_t i, j, count, nelts;
	lxl_list_t *list1, *list2;
    lxl_hash_elt_t *tmp, *next, **buckets;
	lxl_dns_rr_t rr;
	lxl_dns_zone_t *zone;
	lxl_dns_rrset_t *rrset;
	lxl_dns_rdata_t *rdata;

    nelts = lxl_dns_hash.nelts;
	for (i = 0; i < nelts; ++i) {
		j = 0;
		buckets = &lxl_dns_hash.buckets[i];
		tmp = *buckets;
		while (tmp){
			next = tmp->next;
			zone = (lxl_dns_zone_t *) tmp->value;
			lxl_dns_label_to_dot(tmp->name, zname);
			n = snprintf(buffer, 4096, "\nzone:[%s]\texpires:[%lu]\tbuckets:[%lu,%lu]\n", zname, lxl_current_sec - zone->update_sec, i, j);
			write(fd, buffer, n);
			for (list1 = lxl_list_head(&zone->rrset_list); list1 != lxl_list_sentinel(&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;
				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;
					count = lxl_dns_data_dump_rr(buffer, &rr);
					if (count > 0) {
						n = write(fd, buffer, count);
						if (n == -1) {
							lxl_log_error(LXL_LOG_ALERT, errno, "write(%d) failed", fd);
						}
					} else {
						lxl_log_error(LXL_LOG_ALERT, 0, "lxl_dns_data_dump_rr(name:%s type:0x%04x) failed", rr.name, rr.type);
					}
				}
			}

			tmp = next;
			++j;
		}   
	} 

	lxl_log_error(LXL_LOG_INFO, 0, "dns data dump suceed");
}
示例#28
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_dns_parse_root_rr(lxl_int_t argc, char (*argv)[64], lxl_uint_t line, void *args)
{
	/* . ttl class type data */
	uint16_t type;
	size_t len;
	char *dns_str_type, *data;
	lxl_dns_rr_t rr;
	lxl_dns_rdata_t *rdata;

	if (argc == 4) {
		dns_str_type = argv[2];
		data = argv[3];
	} else if (argc == 5) {
		dns_str_type = argv[3];
		data = argv[4];
	} else {
		lxl_log_error(LXL_LOG_EMERG, 0, "need 4 | 5 argv, line %lu argc is %ld", line, argc);
		return -1;
	}

	type = lxl_dns_get_rr_type(dns_str_type, strlen(dns_str_type));
	if (type == LXL_DNS_INVALID_TYPE) {
		lxl_log_error(LXL_LOG_EMERG, 0, "unknow dns type %s, line %lu", dns_str_type, line);
		return -1;
	}

	if(type != LXL_DNS_A && type != LXL_DNS_AAAA && type != LXL_DNS_NS) {
		lxl_log_error(LXL_LOG_EMERG, 0, "root domain(.) suport A AAAA NS, not suport type %s", dns_str_type);
		return -1;
	}

	len = strlen(data);
	rdata = lxl_dns_rdata_create(data, len, type);
	if (rdata == NULL) {
		return -1;
	}

	len = strlen(argv[0]);
	lxl_dns_domain_dot_to_label(argv[0], len);
	if (len == 1) {
		rr.nlen = 1;
	} else {
		rr.nlen = len + 1;
	}
	rr.name = argv[0];
	rr.type = type;
	rr.ttl = atoi(argv[1]);
	LXL_DNS_CHECK_TTL(rr.ttl);
	rr.expires_sec = lxl_current_sec + rr.ttl;
	rr.update_flags = LXL_DNS_RR_NORMAL_TYPE;
	rr.rdlength = rdata->rdlength;
	rr.rdata = rdata->rdata;
	rr.soa_nlen = 0;
	rr.soa_flags = LXL_DNS_RRSET_NORMAL_TYPE;

	if (lxl_dns_rr_add(lxl_dns_pool, lxl_dns_root_zone, &rr) == 1) {
		lxl_free(rdata);
		return -1;
	}

	lxl_free(rdata);
	return 1; 
}
示例#30
0
char *
lxl_conf_parse(lxl_conf_t *cf, lxl_str_t *filename)
{
	int fd;
	lxl_int_t rc;
	lxl_buf_t buf;
	lxl_conf_file_t conf_file;
	enum {
		parse_file = 0,
		parse_block
	} type;

	if (filename) {
		fd = open(filename->data, O_RDONLY);
		if (fd == -1) {
			lxl_conf_log_error(LXL_LOG_EMERG, cf, errno, "open() \"%s\" failed", filename->data);
			return LXL_CONF_ERROR;
		}

		cf->conf_file = &conf_file;
		if (lxl_fd_info(fd, &cf->conf_file->file.info) == -1) {
			lxl_log_error(LXL_LOG_EMERG, errno, lxl_fd_info_n " \"%s\" failed", filename->data);
		}

		cf->conf_file->buffer = &buf;
		buf.start = lxl_alloc(LXL_CONF_BUFFER);
		if (buf.start == NULL) {
			goto failed;
		}

		buf.pos =  buf.start;
		buf.last = buf.start;
		buf.end = buf.last + LXL_CONF_BUFFER;
		cf->conf_file->line = 1;

		cf->conf_file->file.fd = fd;
		cf->conf_file->file.name.len = filename->len;
		cf->conf_file->file.name.data = filename->data;
		cf->conf_file->file.offset = 0;
		cf->conf_file->file.sys_offset = 0;

		type = parse_file;
	} else {
		type = parse_block;
	}

	for (; ;) {
		rc = lxl_conf_read_token(cf);

#if 1
		lxl_uint_t i, nelts;
		lxl_str_t *value;
		nelts = lxl_array_nelts(cf->args);
		value = lxl_array_elts(cf->args);
		for (i = 0; i < nelts; ++i) {
			lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "args: index:%lu value:%s", i, value[i].data);
		}
#endif
		
		if (rc == LXL_ERROR) {
			goto done;
		}

		if (rc == LXL_CONF_BLOCK_DONE) {
			if (type != parse_block) {
				lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "unexpected end of file, unexpected \"}\"");
			}

			goto done;
		}

		if (rc == LXL_CONF_FILE_DONE) {
			if (type == parse_block) {
				lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "unexpected end of file, expecting \"}\"");
				goto failed;
			}

			goto done;
		}

		/*if (cf->handler) {
			
		}*/

		rc = lxl_conf_handler(cf, rc);
		
		if (rc == -1) {
			goto failed;
		}
	}

failed:
	
	rc = LXL_ERROR;

done:

	if (filename) {
		if (cf->conf_file->buffer->start) {
			lxl_free(cf->conf_file->buffer->start);
		} 

		if (close(fd) == -1) {
			lxl_log_error(LXL_LOG_ALERT, errno, "close() %s failed", filename->data);
			return LXL_CONF_ERROR;

		}
	}

	if (rc == LXL_ERROR) {
		return LXL_CONF_ERROR;
	}
	
	return LXL_CONF_OK;
	
}