Example #1
0
/*---------------------------------------------------------------------*/
static void
print_version(void)
{
	TRACE_FUNC_START();

	TRACE_LOG(PLATFORM_NAME" Version %s\n",
		  VERSION);
	TRACE_FLUSH();
	
	TRACE_LUA_FUNC_END();
}
Example #2
0
/*---------------------------------------------------------------------*/
static void
do_rshell(lua_State *L, char *rshell_args)
{
	TRACE_LUA_FUNC_START();
	int csock, status, rc;
	char input_line[LUA_MAXINPUT];
	uint32_t input_len;
	char msg_back;

	csock = connect_to_bricks_server(rshell_args);
	input_len = 0;

        while (!stop_processing && (status = loadline(L)) != -1) {
		strcpy(input_line, lua_tostring(L, 1));
		input_len = strlen(input_line);
		input_line[input_len] = REMOTE_LUA_CMD_DELIM;
		/* next send the entire command */ 
		/* also sending the last char as REMOTE_LUA_CMD_DELIM that serves */
		/* as a delimiter */
		rc = write(csock, input_line, input_len + 1);
		if (rc == -1) goto write_error;
		
		/* 
		 * keep on reading till you get 'REMOTE_LUA_CMD_DELIM' delimiter 
		 * or the server closes the socket
		 */
		do {
			rc = read(csock, &msg_back, 1);
			if (rc <= 0 || msg_back == REMOTE_LUA_CMD_DELIM) break;
			rc = write(2, &msg_back, 1);
		} while (1);

		fflush(stdout);
		lua_saveline(L, 1);
		lua_remove(L, 1);  /* remove line */
        }
        lua_settop(L, 0);  /* clear stack */
        TRACE_FLUSH();
	
	TRACE_LUA_FUNC_END();
	close(csock);
	return;

 write_error:
	TRACE_ERR("Failed to send message (%s) of len: %d to server!\n",
		  input_line, input_len);
}
Example #3
0
/**
 * XXX - This function needs to be revised..
 * Services incoming request from userland applications and takes
 * necessary actions. The actions can be: (i) passing packets to userland
 * apps etc.
 */
static void
process_request_backend(engine *eng, struct kevent chlist[])
{
	TRACE_BACKEND_FUNC_START();
	int client_sock, c, total_read, fd;
	struct sockaddr_in client;
	int read_size;
	char client_message[2000];
	req_block *rb;
	char channelname[20];
	total_read = read_size = 0;

	/* accept connection from an incoming client */
	client_sock = accept(eng->listen_fd, (struct sockaddr *)&client, (socklen_t *)&c);
	if (client_sock < 0) {
		TRACE_LOG("accept failed: %s\n", strerror(errno));
		TRACE_BACKEND_FUNC_END();
		return;
	}
     
	/* Receive a message from client */
	while ((read_size = recv(client_sock, 
				 &client_message[total_read], 
				 sizeof(req_block) - total_read, 0)) > 0) {
		total_read += read_size;
		if ((unsigned)total_read >= sizeof(req_block)) break;
	}

	TRACE_DEBUG_LOG("Total bytes read from listening socket: %d\n", 
			total_read);
	
	/* parse new rule */
	rb = (req_block *)client_message;
	TRACE_DEBUG_LOG("Got a new rule\n");
	TRACE_DEBUG_LOG("Target: %d\n", rb->t);
	TRACE_DEBUG_LOG("TargetArgs.pid: %d\n", rb->targs.pid);
	TRACE_DEBUG_LOG("TargetArgs.proc_name: %s\n", rb->targs.proc_name);
	TRACE_FLUSH();

	//snprintf(channelname, 20, "vale%s%d:s", 
	//	 rb->targs.proc_name, rb->targs.pid);
	snprintf(channelname, 20, "%s%d", 
		 rb->targs.proc_name, rb->targs.pid);

	Rule *r = add_new_rule(eng, NULL, rb->t);

	/* create communication back channel */
	fd = eng->iom.create_channel(eng, r, channelname/*, client_sock*/);
	EV_SET(&chlist[fd], fd, EVFILT_READ | EVFILT_WRITE, EV_ADD, 0, 0, NULL);

	/* add client sock to the polling layer */
	fd = client_sock;
	EV_SET(&chlist[fd], fd, EVFILT_READ | EVFILT_WRITE, EV_ADD, 0, 0, NULL);
	
	/* continue listening */
	fd = eng->listen_fd;
	EV_SET(&chlist[fd], fd, EVFILT_READ | EVFILT_WRITE, EV_ADD, 0, 0, NULL);
	
	TRACE_BACKEND_FUNC_END();
	return;
}
Example #4
0
/**
 * XXX - This function is being revised
 * Services incoming request from userland applications and takes
 * necessary actions. The actions can be: (i) passing packets to userland
 * apps etc.
 */
static void
process_request_backend(engine *eng, int epoll_fd)
{
	TRACE_BACKEND_FUNC_START();
	int client_sock, c, total_read;
	struct sockaddr_in client;
	int read_size;
	char client_message[2000];
	struct epoll_event ev;
	req_block *rb;
	resp_block respb;

	total_read = read_size = 0;
	respb.len = sizeof(resp_block);

	/* accept connection from an incoming client */
	client_sock = accept(eng->listen_fd, (struct sockaddr *)&client, (socklen_t *)&c);
	if (client_sock < 0) {
		TRACE_LOG("accept failed: %s\n", strerror(errno));
		TRACE_BACKEND_FUNC_END();
		return;
	}
     
	/* Receive a message from client */
	while ((read_size = recv(client_sock, 
				 &client_message[total_read], 
				 sizeof(req_block) - total_read, 0)) > 0) {
		total_read += read_size;
		if ((unsigned)total_read >= sizeof(req_block)) break;
	}

	TRACE_DEBUG_LOG("Total bytes read from listening socket: %d\n", 
			total_read);
	
	/* parse new rule */
	rb = (req_block *)client_message;
	TRACE_DEBUG_LOG("Got a new request\n");
	TRACE_DEBUG_LOG("\tLen: %u\n", rb->len);
	TRACE_DEBUG_LOG("\tInterface name: %s\n", rb->ifname);
	TRACE_FLUSH();

#if 0
	respb.flag = (process_filter_request(eng, rb->ifname, &rb->f) == -1) ?
		0 : 1;
#else
	(void)rb;
	respb.flag = 1;
#endif
	
	/* write the response back to the app */
	if (write(client_sock, &respb, sizeof(respb)) == -1) {
		TRACE_LOG("Write failed due to %s\n", 
			  strerror(errno));
	}

	/* add client sock to the polling layer */
	ev.data.fd = client_sock;
	ev.events = EPOLLIN | EPOLLOUT;
	if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, ev.data.fd, &ev) == -1) {
		TRACE_LOG("Engine %s failed to exe epoll_ctl for fd: %d\n",
			  eng->name, epoll_fd);
		TRACE_BACKEND_FUNC_END();
		return;
	}
	
	/* for the moment, close the client socket immediately */
	close(client_sock);

	/* continue listening */
	ev.data.fd = eng->listen_fd;
	ev.events = EPOLLIN | EPOLLOUT;
	if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, ev.data.fd, &ev) == -1) {
		TRACE_LOG("Engine %s failed to exe epoll_ctl for fd: %d\n",
			  eng->name, epoll_fd);
		TRACE_BACKEND_FUNC_END();
		return;
	}
	
	TRACE_BACKEND_FUNC_END();
	return;
}