コード例 #1
0
ファイル: daemon.c プロジェクト: GNS3/vpcs
static void 
daemon_proc(int sock, int fdtty)
{
	char *goodbye = "\r\nGood-bye\r\n";
	int sock_cli;
	struct sockaddr_in cli;
	int slen;
	int rc;
	int fsess = 1;

	slen = sizeof(cli);
	while (1) {
		cmd_quit = 0;
		sock_cli = accept(sock, (struct sockaddr *) &cli, (socklen_t *)&slen);
		if (sock_cli < 0) 
			continue;

		set_telnet_mode(sock_cli);
		set_nonblock(fdtty);

		/* to show the prompt if it is not the first connection */
		if (!fsess)
			rc = write(fdtty, "\n", 2);
		
		fsess = 0;
		while (!cmd_quit) {
			if ((rc = pipe_rw(fdtty, sock_cli)) < 0)
				break;
			rc = pipe_rw(sock_cli, fdtty);
			/* error */
			if (rc < 0)
				break;
			/* time out */
			if (rc == 0)
				continue;	
		}	
		pipe_rw(fdtty, sock_cli);
		rc = write(sock_cli, goodbye, strlen(goodbye));
		close(sock_cli);
	}
}
コード例 #2
0
ファイル: daemon.c プロジェクト: dlintott/vpcs
static void 
daemon_proc(int sock, int fdtty)
{
	int sock_cli;
	struct sockaddr_in cli;
	int slen;
	fd_set set;
	struct timeval tv;
	u_char buf[8192];
	int i;

	slen = sizeof(cli);
	while (1) {
		cmd_quit = 0;
		sock_cli = accept(sock, (struct sockaddr *) &cli, (socklen_t *)&slen);
		if (sock_cli < 0) 
			continue;
		
		set_telnet_mode(sock_cli);
			
		while (!cmd_quit) {
			FD_ZERO(&set);
			FD_SET(sock_cli, &set);
			FD_SET(fdtty, &set);
			
			/* wait 100ms */
			tv.tv_sec = 0;
			tv.tv_usec = 100000; 
			i = select((fdtty > sock_cli) ? (fdtty+1) : (sock_cli+1),
			    &set, NULL, NULL, &tv);
			
			/* error */
			if (i < 0)
				break;
			/* time out */
			if (i == 0)
				continue;
				
			if (FD_ISSET(fdtty, &set)) {
				memset(buf, 0, sizeof(buf));
				i = read(fdtty, buf, sizeof(buf));
				if (i <= 0)
					break;
				
				if (write(sock_cli, buf, i) <= 0) 
					break;
			}
			if (FD_ISSET(sock_cli, &set)) {
				memset(buf, 0, sizeof(buf));
				i = read(sock_cli, buf, sizeof(buf));
				if (i <= 0)
					break;

				if (buf[0] != 0xff && write(fdtty, buf, i) <= 0) 
					break;
			}
		}
		strcpy((char *)buf, "\r\nGood-bye\r\n");
		i = write(sock_cli, buf, strlen((char *)buf));
		close(sock_cli);
	}
}
コード例 #3
0
ファイル: hv.c プロジェクト: dlintott/vpcs
static void 
loop(void)
{
	struct sockaddr_in cli;
	int slen;
	u_char buf[256], *p;
	int i;
	fd_set set;
	struct timeval tv;
		
	slen = sizeof(cli);
	
	
			
	while (cmd_quit != 2) {
		cmd_quit = 0;
		sock_cli = accept(sock, (struct sockaddr *) &cli, 
		    (socklen_t *)&slen);
		if (sock_cli < 0) 
			continue;
		
		set_telnet_mode(sock_cli);

		pthread_create(&pid_master, NULL, pty_master, NULL);
		pthread_create(&pid_salve, NULL, pty_slave, NULL);

		fcntl(0, F_SETFL, fcntl(sock_cli, F_GETFL) | O_NONBLOCK);

		while (!cmd_quit) {
			FD_ZERO(&set);
			FD_SET(sock_cli, &set);
			
			/* wait 1s */
			tv.tv_sec = 1;
			tv.tv_usec = 0; 
			i = select(sock_cli + 1, &set, NULL, NULL, &tv);
			
			/* error */
			if (i < 0)
				break;
			/* time out */
			if (i == 0)
				continue;
			
			if (FD_ISSET(sock_cli, &set)) {	    
				memset(buf, 0, sizeof(buf));
				i = read(sock_cli, buf, sizeof(buf));
				if (i < 0)
					break;
				if (i == 0)
					continue;
				p = buf;
	
				/* skip telnet IAC... */
				if (*p == 0xff)
					while (*p && !isprint(*p))
						p++;
	
				if (*p == '\0')
					continue;
	
				i = i - (p - buf);
	
				while (i > 0 && *(p + i - 1) == '\0')
					i--;
		
				if (i <= 0)
					continue;
				
				if (*p == CTRLD) {
					p = buf;
					strcpy((char *)buf, "disconnect\n");
					i = strlen((char *)buf);
					i = write(ptyfdm, p, i);
					usleep(100);
					strcpy((char *)buf, "\n");
					i = strlen((char *)buf);					
				}
				
				/* ignore pty error */
				if (write(ptyfdm, p, i))
					;
			}
		}
		
		pthread_cancel(pid_master);
		pthread_cancel(pid_salve);
		
		close(sock_cli);
	}
}