Ejemplo n.º 1
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;
}
Ejemplo n.º 2
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;
}
Ejemplo n.º 3
0
int
lxl_event_process_init(lxl_cycle_t *cycle)
{
	lxl_uint_t 			i, nelts;
	lxl_connection_t 	*c, *next;
	lxl_listening_t 	*ls;
	lxl_core_conf_t 	*ccf;
	lxl_event_conf_t 	*ecf;
	lxl_event_module_t  *m;

	ccf = (lxl_core_conf_t *) lxl_get_conf(cycle->conf_ctx, lxl_core_module);
	ecf = (*(lxl_get_conf(cycle->conf_ctx, lxl_events_module))) [lxl_event_core_module.ctx_index];

	if (ccf->worker_process > 1 && ecf->accept_mutex) {
		lxl_use_accept_mutex = 1;
	} else {
		lxl_use_accept_mutex = 0;
	}
	lxl_accept_disabled = 1;

	//lxl_list_init(&lxl_posted_event_list);
	lxl_event_timer_init();

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

		if (lxl_modules[i]->ctx_index != ecf->use) {
			continue;
		}

		m = lxl_modules[i]->ctx;
		if (m->actions.init(cycle) != 0) {
			/* fatal */
			exit(2);
		}

		break;
	}
	
	cycle->connections = lxl_alloc(cycle->connection_n * sizeof(lxl_connection_t));
	if (cycle->connections == NULL) {
		return -1;
	}

	cycle->read_events = lxl_alloc(cycle->connection_n * sizeof(lxl_event_t));
	if (cycle->read_events == NULL) {
		return -1;
	}

	for (i = 0; i < cycle->connection_n; ++i) {
		cycle->read_events[i].closed = 1;
	}

	cycle->write_events = lxl_alloc(cycle->connection_n * sizeof(lxl_event_t));
	if (cycle->write_events == NULL) {
		return -1;
	}

	for (i = 0; i < cycle->connection_n; ++i) {
		cycle->write_events[i].closed = 1;
	}

	next = NULL;
	i = cycle->connection_n;
	c = cycle->connections;
	do {
		--i;
		c[i].data = next;
		c[i].fd = -1;
		c[i].read = &cycle->read_events[i];
		c[i].write = &cycle->write_events[i];
		next = &c[i];
	} while (i);
	cycle->free_connection_n = cycle->connection_n;
	cycle->free_connections = next;

	ls = lxl_array_elts(&cycle->listening);
	nelts = lxl_array_nelts(&cycle->listening);
	for (i = 0; i < nelts; ++i) {
		c = lxl_get_connection(ls[i].fd);
		if (c == NULL) {
			return -1;
		}

		c->listening = &ls[i];
		ls[i].connection = c;
		//rev = c->read;
		c->read->accept = 1;
		if (ls[i].type == SOCK_STREAM) {
			c->udp = 0;
			//c->closefd = 1;
			c->read->handler = lxl_event_accept;	/* tcp or udp */
		} else {
			c->udp = 1;
			//c->closefd = 1;
			c->read->handler = lxl_event_accept_udp;
			/*c->recv = lxl_recvfrom;
			c->send = lxl_sendto;*/
		}

		if (lxl_use_accept_mutex) {
			continue;
		}

		if (lxl_add_event(c->read, LXL_READ_EVENT, LXL_LEVEL_EVENT) == -1) {
			return -1;
		}
	}

	return 0;	
}
Ejemplo n.º 4
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;
			}
		}
	}
}
Ejemplo n.º 5
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;
		}
	}
}
Ejemplo n.º 6
0
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;
}