Peal — Hacking Apply – pt IV hidden PTY code



#include <sys/select.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <stropts.h>
#include <sys/conf.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <termios.h>
#include <sys/stream.h>
#include <sys/stropts.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>

#define max(x,y)	((x) > (y) ? (x) : (y))

#define BUFF_SIZE 8192   // 5000	
char buff[BUFF_SIZE];
char buff2[BUFF_SIZE] = 
"\x01\x41\x41\x41\x01\x42\x42\x42\x01\x43\x43\x43\x01\x44\x44\x44"
"\x01\x45\x45\x45\x01\x46\x46\x46\x01\x47\x47\x47\x01\x48\x48\x48"
"\x01\x49\x49\x49\x01\x4a\x4a\x4a\x01\x4b\x4b\x4b\x01\x4c\x4c\x4c"
"\x01\x4d\x4d\x4d\x01\x4e\x4e\x4e\x01\x4f\x4f\x4f\x01\x50\x50\x50"
"\x01\x51\x51\x51\x01\x52\x52\x52\x01\x53\x53\x53\x01\x54\x54\x54"
"\x01\x55\x55\x55\x01\x56\x56\x56\x01\x57\x57\x57\x01\x58\x58\x58"
"\x01\x59\x59\x59\x01\x5a\x5a\x5a\x02\x41\x41\x41\x02\x42\x42\x42"
"\x02\x43\x43\x43\x02\x44\x44\x44\x02\x45\x45\x45\x02\x46\x46\x46"
"\x02\x47\x47\x47\x02\x48\x48\x48\x02\x49\x49\x49\x02\x4a\x4a\x4a"
"\x02\x4b\x4b\x4b\x02\x4c\x4c\x4c\x02\x4d\x4d\x4d\x02\x4e\x4e\x4e"
"\x02\x4f\x4f\x4f\x02\x50\x50\x50\x02\x51\x51\x51\x02\x52\x52\x52"
"\x02\x53\x53\x53\x02\x54\x54\x54\x02\x55\x55\x55\x02\x56\x56\x56"
"\x02\x57\x57\x57\x02\x58\x58\x58\x02\x59\x59\x59\x02\x5a\x5a\x5a"
"\x03\x41\x41\x41\x03\x42\x42\x42\x03\x43\x43\x43\x03\x44\x44\x44"
"\x03\x45\x45\x45\x03\x46\x46\x46\x03\x47\x47\x47\x03\x48\x48\x48"
"\x03\x49\x49\x49\x03\x4a\x4a\x4a\x03\x4b\x4b\x4b\x03\x4c\x4c\x4c"
"\x03\x4d\x4d\x4d\x03\x4e\x4e\x4e\x03\x4f\x4f\x4f\x03\x50\x50\x50"
"\x03\x51\x51\x51\x03\x52\x52\x52\x03\x53\x53\x53\x03\x54\x54\x54"
"\x03\x55\x55\x55\x03\x56\x56\x56\x03\x57\x57\x57\x03\x58\x58\x58"
"\x03\x59\x59\x59\x03\x5a\x5a\x5a\x04\x41\x41\x41\x04\x42\x42\x42"
"\x04\x43\x43\x43\x04\x44\x44\x44\x04\x45\x45\x45\x04\x46\x46\x46"
"\x04\x47\x47\x47\x04\x48\x48\x48\x04\x49\x49\x49\x04\x4a\x4a\x4a"
"\x04\x4b\x4b\x4b\x04\x4c\x4c\x4c\x04\x4d\x4d\x4d\x04\x4e\x4e\x4e"
"\x04\x4f\x4f\x4f\x04\x50\x50\x50\x04\x51\x51\x51\x04\x52\x52\x52"
"\x04\x53\x53\x53\x04\x54\x54\x54\x04\x55\x55\x55\x04\x56\x56\x56"
"\x04\x57\x57\x57\x04\x58\x58\x58\x04\x59\x59\x59\x04\x5a\x5a\x5a"
"\x05\x41\x41\x41\x05\x42\x42\x42\x05\x43\x43\x43\x05\x44\x44\x44"
"\x05\x45\x45\x45\x05\x46\x46\x46\x05\x47\x47\x47\x05\x48\x48\x48"
"\x05\x49\x49\x49\x05\x4a\x4a\x4a\x05\x4b\x4b\x4b\x05\x4c\x4c\x4c"
"\x05\x4d\x4d\x4d\x05\x4e\x4e\x4e\x05\x4f\x4f\x4f\x05\x50\x50\x50"
"\x05\x51\x51\x51\x05\x52\x52\x52\x05\x53\x53\x53\x05\x54\x54\x54"
"\x05\x55\x55\x55\x05\x56\x56\x56\x05\x57\x57\x57\x05\x58\x58\x58"
"\x05\x59\x59\x59\x05\x5a\x5a\x5a\x06\x41\x41\x41\x06\x42\x42\x42"
"\x06\x43\x43\x43\x06\x44\x44\x44\x06\x45\x45\x45\x06\x46\x46\x46"
"\x06\x47\x47\x47\x06\x48\x48\x48\x06\x49\x49\x49\x06\x4a\x4a\x4a"
"\x06\x4b\x4b\x4b\x06\x4c\x4c\x4c\x06\x4d\x4d\x4d\x06\x4e\x4e\x4e"
"\x06\x4f\x4f\x4f\x06\x50\x50\x50\x06\x51\x51\x51\x06\x52\x52\x52"
"\x06\x53\x53\x53\x06\x54\x54\x54\x06\x55\x55\x55\x06\x56\x56\x56"
"\x06\x57\x57\x57\x06\x58\x58\x58\x06\x59\x59\x59\x06\x5a\x5a\x5a"
"\x07\x41\x41\x41\x07\x42\x42\x42\x07\x43\x43\x43\x07\x44\x44\x44"
"\x07\x45\x45\x45\x07\x46\x46\x46\x07\x47\x47\x47\x07\x48\x48\x48"
"\x07\x49\x49\x49\x07\x4a\x4a\x4a\x07\x4b\x4b\x4b\x07\x4c\x4c\x4c"
"\x07\x4d\x4d\x4d\x07\x4e\x4e\x4e\x07\x4f\x4f\x4f\x07\x50\x50\x50"
"\x07\x51\x51\x51\x07\x52\x52\x52\x07\x53\x53\x53\x07\x54\x54\x54"
"\x07\x55\x55\x55\x07\x56\x56\x56\x07\x57\x57\x57\x07\x58\x58\x58"
"\x07\x59\x59\x59\x07\x5a\x5a\x5a\x08\x41\x41\x41\x08\x42\x42\x42"
"\x08\x43\x43\x43\x08\x44\x44\x44\x08\x45\x45\x45\x08\x46\x46\x46"
"\x08\x47\x47\x47\x08\x48\x48\x48\x08\x49\x49\x49\x08\x4a\x4a\x4a"
"\x08\x4b\x4b\x4b\x08\x4c\x4c\x4c\x08\x4d\x4d\x4d\x08\x4e\x4e\x4e"
"\x08\x4f\x4f\x4f\x08\x50\x50\x50\x08\x51\x51\x51\x08\x52\x52\x52"
"\x08\x53\x53\x53\x08\x54\x54\x54\x08\x55\x55\x55\x08\x56\x56\x56"
"\x08\x57\x57\x57\x08\x58\x58\x58\x08\x59\x59\x59\x08\x5a\x5a\x5a"
"\x09\x41\x41\x41\x09\x42\x42\x42\x09\x43\x43\x43\x09\x44\x44\x44"
"\x09\x45\x45\x45\x09\x46\x46\x46\x09\x47\x47\x47\x09\x48\x48\x48"
"\x09\x49\x49\x49\x09\x4a\x4a\x4a\x09\x4b\x4b\x4b\x09\x4c\x4c\x4c"
"\x09\x4d\x4d\x4d\x09\x4e\x4e\x4e\x09\x4f\x4f\x4f\x09\x50\x50\x50"
"\x09\x51\x51\x51\x09\x52\x52\x52\x09\x53\x53\x53\x09\x54\x54\x54"
"\x09\x55\x55\x55\x09\x56\x56\x56\x09\x57\x57\x57\x09\x58\x58\x58"
"\x09\x59\x59\x59\x09\x5a\x5a\x5a\x0a\x41\x41\x41\x0a\x42\x42\x42"
"\x0a\x43\x43\x43\x0a\x44\x44\x44\x0a\x45\x45\x45\x0a\x46\x46\x46"
"\x0a\x47\x47\x47\x0a\x48\x48\x48\x0a\x49\x49\x49\x0a\x4a\x4a\x4a"
"\x0a\x4b\x4b\x4b\x0a\x4c\x4c\x4c\x0a\x4d\x4d\x4d\x0a\x4e\x4e\x4e"
"\x0a\x4f\x4f\x4f\x0a\x50\x50\x50\x0a\x51\x51\x51\x0a\x52\x52\x52"
"\x0a\x53\x53\x53\x0a\x54\x54\x54\x0a\x55\x55\x55\x0a\x56\x56\x56"
"\x0a\x57\x57\x57\x0a\x58\x58\x58\x0a\x59\x59\x59\x0a\x5a\x5a\x5a"
"\x0b\x41\x41\x41\x0b\x42\x42\x42\x0b\x43\x43\x43\x0b\x44\x44\x44"
"\x0b\x45\x45\x45\x0b\x46\x46\x46\x0b\x47\x47\x47\x0b\x48\x48\x48"
"\x0b\x49\x49\x49\x0b\x4a\x4a\x4a\x0b\x4b\x4b\x4b\x0b\x4c\x4c\x4c"
"\x0b\x4d\x4d\x4d\x0b\x4e\x4e\x4e\x0b\x4f\x4f\x4f\x0b\x50\x50\x50"
"\x0b\x51\x51\x51\x0b\x52\x52\x52\x0b\x53\x53\x53\x0b\x54\x54\x54"
"\x0b\x55\x55\x55\x0b\x56\x56\x56\x0b\x57\x57\x57\x0b\x58\x58\x58"
"\x0b\x59\x59\x59\x0b\x5a\x5a\x5a\x0c\x41\x41\x41\x0c\x42\x42\x42"
"\x0c\x43\x43\x43\x0c\x44\x44\x44\x0c\x45\x45\x45\x0c\x46\x46\x46"
"\x0c\x47\x47\x47\x0c\x48\x48\x48\x0c\x49\x49\x49\x0c\x4a\x4a\x4a"
"\x0c\x4b\x4b\x4b\x0c\x4c\x4c\x4c\x0c\x4d\x4d\x4d\x0c\x4e\x4e\x4e"
"\x0c\x4f\x4f\x4f\x0c\x50\x50\x50\x0c\x51\x51\x51\x0c\x52\x52\x52"
"\x0c\x53\x53\x53\x0c\x54\x54\x54\x0c\x55\x55\x55\x0c\x56\x56\x56"
"\x0c\x57\x57\x57\x0c\x58\x58\x58\x0c\x59\x59\x59\x0c\x5a\x5a\x5a"
"\x0d\x41\x41\x41\x0d\x42\x42\x42\x0d\x43\x43\x43\x0d\x44\x44\x44"
"\x0d\x45\x45\x45\x0d\x46\x46\x46\x0d\x47\x47\x47\x0d\x48\x48\x48"
"\x0d\x49\x49\x49\x0d\x4a\x4a\x4a\x0d\x4b\x4b\x4b\x0d\x4c\x4c\x4c"
"\x0d\x4d\x4d\x4d\x0d\x4e\x4e\x4e\x0d\x4f\x4f\x4f\x0d\x50\x50\x50"
"\x0d\x51\x51\x51\x0d\x52\x52\x52\x0d\x53\x53\x53\x0d\x54\x54\x54"
"\x0d\x55\x55\x55\x0d\x56\x56\x56\x0d\x57\x57\x57\x0d\x58\x58\x58"
"\x0d\x59\x59\x59\x0d\x5a\x5a\x5a\x0e\x41\x41\x41\x0e\x42\x42\x42"
"\x0e\x43\x43\x43\x0e\x44\x44\x44\x0e\x45\x45\x45\x0e\x46\x46\x46"
"\x0e\x47\x47\x47\x0e\x48\x48\x48\x0e\x49\x49\x49\x0e\x4a\x4a\x4a"
"\x0e\x4b\x4b\x4b\x0e\x4c\x4c\x4c\x0e\x4d\x4d\x4d\x0e\x4e\x4e\x4e"
"\x0e\x4f\x4f\x4f\x0e\x50\x50\x50\x0e\x51\x51\x51\x0e\x52\x52\x52"
"\x0e\x53\x53\x53\x0e\x54\x54\x54\x0e\x55\x55\x55\x0e\x56\x56\x56"
"\x0e\x57\x57\x57\x0e\x58\x58\x58\x0e\x59\x59\x59\x0e\x5a\x5a\x5a"
"\x0f\x41\x41\x41\x0f\x42\x42\x42\x0f\x43\x43\x43\x0f\x44\x44\x44"
"\x0f\x45\x45\x45\x0f\x46\x46\x46\x0f\x47\x47\x47\x0f\x48\x48\x48"
"\x0f\x49\x49\x49\x0f\x4a\x4a\x4a\x0f\x4b\x4b\x4b\x0f\x4c\x4c\x4c"
"\x0f\x4d\x4d\x4d\x0f\x4e\x4e\x4e\x0f\x4f\x4f\x4f\x0f\x50\x50\x50"
"\x0f\x51\x51\x51\x0f\x52\x52\x52\x0f\x53\x53\x53\x0f\x54\x54\x54"
"\x0f\x55\x55\x55\x0f\x56\x56\x56\x0f\x57\x57\x57\x0f\x58\x58\x58"
"\x0f\x59\x59\x59\x0f\x5a\x5a\x5a\x10\x41\x41\x41\x10\x42\x42\x42"
"\x10\x43\x43\x43\x10\x44\x44\x44\x10\x45\x45\x45\x10\x46\x46\x46"
"\x10\x47\x47\x47\x10\x48\x48\x48\x10\x49\x49\x49\x10\x4a\x4a\x4a";

struct termios tty_state_orig, tty_state_mod;
pid_t cpid = -1;
int fdmax = 0;

int fd_master=-1, fd_slave=-1;
int logfd=-1;

pid_t shell_pid=-1;


// signal handlers 

void sighup (int sig)
{
	sig = sig;
	/*if (close(fd_slave) < 0)
		perror("Close slave failed");
	if (close(fd_master) < 0)
		perror("Close master failed: ");
	if (close(logfd) < 0)
		perror("Close master failed: ");*/
	exit(0);
}

void sigpipe (int sig)
{
	sig = sig;
	//snoopfd = -1;
	
	signal (SIGPIPE, sigpipe);
}

void sigchld (int sig)
{
	int status, pid;
	sig = sig;
	
	wait(&status);
	raise (SIGHUP);
	
	signal (SIGCHLD, sigchld);
}

// taken from snooptty
int stty_raw (int fd)
{
	struct termios tty_state;
	int i;
	
	if (tcgetattr(fd, &tty_state) < 0)
	{
		perror("tcgetattr failed: ");
		return (-1);
	}
	
	tty_state_orig = tty_state;
	
	tty_state.c_lflag &= ~(ICANON | IEXTEN | ISIG | ECHO);
	tty_state.c_iflag &= ~(ICRNL | INPCK | ISTRIP | IXON | BRKINT);
	tty_state.c_oflag &= ~OPOST;
	tty_state.c_cflag |= CS8;
	
	tty_state.c_cc[VMIN]  = 1;
	tty_state.c_cc[VTIME] = 0;
	
	tty_state_mod = tty_state;
	
	if (tcsetattr(fd, TCSAFLUSH, &tty_state) < 0)
	{
		perror("tcgetattr failed: ");
		return (-1);
	}
	
	return (0);
}
/*
void errorf (char *fmt, ...)
{
	va_list args;
	
	va_start (args, fmt);
	vfprintf (stderr, fmt, args);
	exit (1);
}

int fdprintf (int fd, char *fmt, ...)
{
	va_list args;
	int r;
	char str[256];
	
	va_start (args, fmt);
	r = vsprintf(str, fmt, args);
	write (fd, str, r);
	
	return (r);
}
*/

void finish()
{
	remove("/home/bazz/latest/pty/test.txt");
	if (close(fd_slave) < 0)
		perror("Close slave failed");
	if (close(fd_master) < 0)
		perror("Close master failed: ");
	if (close(logfd) < 0)
		perror("Close master failed: ");
	
	//kill(shell_pid, 9);
	if (tcsetattr(STDIN_FILENO, TCSANOW, &tty_state_orig) < 0)
        {
                perror("tcgetattr failed: ");
                return (-1);
        }
}

int main (int argc, char *argv[], char **envp)
{
	if (!isatty(STDIN_FILENO))
		printf ("stdin is not a tty\n");
	
	atexit(finish);
	//init 
	signal (SIGPIPE, sigpipe);
	signal (SIGCHLD, sigchld);
	signal (SIGHUP, sighup);
	//
	shell_pid = getppid();
	
	//memset(buff2, 0x41, BUFF_SIZE);	
	// Open Master device
	printf("Opening master device\n"); ////sleep(1);
	
	fd_master = open("/dev/ptmx", O_RDWR);
	if (fd_master < 0)
		perror("opening master failed\n");
	
	/// Grantpt
	printf("Doing Grantpt()\n"); ////sleep(1);
	if (grantpt(fd_master) < 0)
		perror("Grantpt Master failed\n");
	
	///Unlockpt
	printf("Unlocking pt\n"); ////sleep(1);
	if (unlockpt(fd_master) < 0)
		perror("Unlockpt Master failed\n");
	
	
	struct termios term;
	struct winsize twin;
	printf("getting stdin termios\n");
	if (tcgetattr (STDIN_FILENO, &term) < 0)
		perror("tcgetattr failed\n");
	printf("getting window settings\n");
	if (ioctl (STDIN_FILENO, TIOCGWINSZ, (char *) &twin) < 0)
		perror("ioctl TCIOCGWINSZ failed\n");
	
	// ptsname()
	char *slavename;
	printf("---Getting slave name\n"); ////sleep(1);
	slavename = ptsname(fd_master);
	if (slavename == NULL)
		perror("---ptsname failed\n");
	
	// opening Slave
	printf("---Opening slave\n"); ////sleep(1);
	fd_slave = open(slavename, O_RDWR);
	if (fd_slave < 0)
	{
		printf("---open(%s) failed\n",slavename);
		perror("---");
	}
	
	
	
	////
	// THE BIG FORK
	/////////
	/////////
	
	printf("Forking\n"); sleep(1);
	cpid = fork();
	
	if (cpid == 0) 
	{
		
		printf("in CHILD: \n");
		
		//setsid()
		printf("---Setsid()\n");
		if (setsid() < 0)
			perror("---setsid failed\n");
		
		// Pushing modules to slave
		printf("---Pushing ptem module to slave\n"); ////sleep(1);
		if (ioctl(fd_slave, I_PUSH, "ptem") == -1)
			perror("---pushing ptem module failed\n");
		
		printf("---Pushing ldterm module to slave\n"); ////sleep(1);
		if (ioctl(fd_slave, I_PUSH, "ldterm") == -1)
			perror("---Pushing ldterm module failed\n");
		
		printf("---setting term settings\n");
		fflush(stdout);
		if (tcsetattr(fd_slave, TCSANOW, &term) < 0)
			perror ("---can't set termios\n");
		
		printf("---setting window settings\n");
		if (ioctl(fd_slave, TIOCSWINSZ, &twin) < 0)
			perror ("---can't set winsize\n");
		
		
		if (dup2(fd_slave, STDIN_FILENO) != STDIN_FILENO)
			perror ("---can't dup2 into stdin\n");
		
		if (dup2(fd_slave, STDOUT_FILENO) != STDOUT_FILENO)
			perror ("---can't dup2 into stdout\n");
		
		if (dup2(fd_slave, STDERR_FILENO) != STDERR_FILENO)
			perror ("---can't dup2 into stderr\n");
		
		if (fd_slave > STDERR_FILENO)
			close (fd_slave);
	
		stty_raw(STDIN_FILENO);	
		//char **args = (char *[]){ "-i", (char*)0 };
		//
		//atexit(finish);
	        execle("/bin/bash","/bin/bash",0,envp);	
		if (execle("/home/bazz/latest/invoke2","/home/bazz/latest/invoke2", "v", (char*) 0, envp) < 0)
			perror("---execle error!\n");
		
		//----XXXXXXXXX
		/*struct termios t;
		tcgetattr(fd_slave, &t);
		stty_raw(fd_slave);
		printf("\n\n---Successfully set slave to RAW settings\n\n");
		*/
		
		//if (close(fd_slave) < 0)
			//perror("---Close slave failed: ");
		
		//printf("---Closed Slave\n");
		
		//printf("---Closing master fd\n");
		//if (close(fd_master) < 0)
			//perror("---Closing master fd failed\n");
		
		printf("---exiting\n");
		fflush(stdout);
		sleep(5);
		exit(-1);
	}
	
	if (cpid > 0)
	{
		fd_set readset;
		int cexit; // child exit code
		int len, n, sel, susp = 0;
		char logfile[] = "/home/bazz/latest/pty/log.txt";
		
		
		logfd = open(logfile, O_CREAT|O_APPEND|O_WRONLY, S_IRWXU|S_IRWXG|S_IRWXO);
		if (logfd < 0)
			perror("opening log file failed\n");
		
		printf("In Parent: \n");
		/*printf("Telling master to forward SIGINT signals to slave\n"); ////sleep(1);
		if (ioctl(fd_master, TIOCSIGNAL, SIGINT) < 0)
			perror("Setting master to forward sigint to slave failed\n");*/
		
		
	
		stty_raw (STDIN_FILENO);
		//stty_raw (fd_master);
		
		fdmax = max(STDIN_FILENO, fd_slave);
		while (1)
		{
			do
			{
				FD_ZERO (&readset);
				FD_SET (STDIN_FILENO, &readset);
				FD_SET (fd_master, &readset);
				
				sel = select(fdmax + 1, &readset, NULL, NULL, NULL);
			}
			while (sel == -1 && errno == EINTR);
			if (sel == -1 && errno != EINTR)
			{
				printf ("select failed. errno = %d\n", errno);
				perror("");
			}
			
			if (FD_ISSET(STDIN_FILENO, &readset))
			{
				if ((n = read(STDIN_FILENO, buff, BUFF_SIZE)) < 1)
					exit (0);

				buff[n] = 0;
				if (strchr(buff,4))
				{				  	
					int i;
					//buff2[6000] = 0x0a;
					//buff2[6001] = 0x00; 
					write (fd_master,buff2,1600);
					//write (fd_master,"\x0a",1);
					//fflush(fd_master);
					//for (i=0;i < 421; i++)
					//	write (STDIN_FILENO, buff2[i], 1);
					continue;	
				}
					
				write (logfd, buff, n);
				write (fd_master, buff, n);
			}
			
			if (FD_ISSET(fd_master, &readset))
			{
				if ((n = read(fd_master, buff, BUFF_SIZE)) < 1)
					exit (0);
				
				write (STDOUT_FILENO, buff, n);
			}
			
		}
		
		
		if (close(fd_master) < 0)
			perror("Close master failed: ");
		
		
		printf("Closed master\nQuitting..\n");
		printf("Waiting for child..\n"); sleep(1);
		wait(&cexit);
	}
	else {
		perror("Fork failed\n");
	}

	return 0;
	
	
}

/* do the main server loop */

/*while (1)
{
	do
	{
		FD_ZERO (&readset);
		FD_SET (STDIN_FILENO, &readset);
		FD_SET (ptyfd, &readset);
		if (snoopfd >= 0)
			FD_SET (snoopfd, &readset);
		else if (use_socket && authfd == -1)
			FD_SET (servfd, &readset);
		
		sel = select(fdmax + 1, &readset, NULL, NULL, NULL);
	}
	while (sel == -1 && errno == EINTR);
	if (sel == -1 && errno != EINTR)
		errorf ("select failed. errno = %d\n", errno);
		
		if (FD_ISSET(STDIN_FILENO, &readset))
		{
			if ((n = read(STDIN_FILENO, buff, BUFF_SIZE)) < 1)
				exit (0);
			
			write (ptyfd, buff, n);
		}
	
	if ((snoopfd >= 0) && FD_ISSET(snoopfd, &readset))
	{
		n = read(snoopfd, buff, BUFF_SIZE);
		if (!n || ((*buff == TERM_CHAR) && (n == 1) && use_socket))
		{
			close (snoopfd);
			snoopfd = -1;
		}
		else if ((*buff == SUSP_CHAR) && (n == 1) && use_socket)
		{
			if (susp = !susp)
				fdprintf (snoopfd, "\r\nSnoop suspended. %s (ASCII %d) to resume.\r\n",
									SC_STRING, SUSP_CHAR);
			else
				fdprintf (snoopfd, "Snoop resumes...\r\n");
		}
		else
			write (ptyfd, buff, n);
	}
	
	if (FD_ISSET(ptyfd, &readset))
	{
		if ((n = read(ptyfd, buff, BUFF_SIZE)) < 1)
			exit (0);
		
		write (STDOUT_FILENO, buff, n);
		if (!susp && (snoopfd >= 0))
			write (snoopfd, buff, n);
	}
	
}
	*/

// I think we have to do these after a fork
////
// Not sure whether to tell slave/master to be stream terminal
// so i will tell both, no i will tell master
//if (setsid() < 0)
//perror("setsid failed\n");
//if (ioctl(fd_slave, M_SETOPTS, SO_ISTTY) < 0)
//perror("Slave M_SETOPTS to SO_ISTTY failed\n");

// we can fiddle wit this if the program fails



// Now that we have an adequate master and slave
// lets do some testing
// simple tests

/*
 write fd_master "hello"
 read fd_slave
 printf
 
 opposite
 */

////sleep(2);
//////Test #2
// Make slave raw

//sleep(3);

/*int nbytes1;
 char str1[] = "hello";
 char buf[100];
 int i;
 
 for (i=0; i< 100; i++) {buf[i] = 0;}
 printf("\nTesting the master and slave\n"
 "-------------------------------\n");
 sleep(3);
 
 printf("Writing \"%s\" to master\n", str1);
 sleep (1);
 nbytes1 = write(fd_master, str1, strlen(str1));
 if (nbytes1 != strlen(str1))
 printf("-nbytes- wasnt strlen(%s)+1\n", str1);
 
 printf("wrote %d bytes to master\n", nbytes1); sleep(1);
 
 for (i=0; i<=10; i++)
 { printf("str1[%d] = %d\n", i,(int)str1[i]); }
 
 sleep(4);*/



//////Test #2
// Make slave raw
/*struct termios t;
 tcgetattr(fd_slave, &t);
 stty_raw(fd_slave);
 printf("\n\nSuccessfully set slave to RAW settings\n\n");
 //sleep(3);*/

// Test 3
//master to slave again with raw settings
//
/*int nbytes2;
 char str2[] = "derp";
 printf("Testing the master and slave\n"
 "-------------------------------\n"
 "Writing \"%s\" to master\n", str2);
 //sleep(2);
 nbytes2 = write(fd_master, str2, strlen(str2));
 if (nbytes2 != strlen(str2))
 printf("-nbytes- wasnt strlen(%s)+1\n", str2);
 
 printf("wrote %d bytes to master\n", nbytes2); //sleep(1);
 //sleep(1);
 printf("reading %d bytes from slave\n", nbytes2); //sleep(1);
 if (read(fd_slave, buf, nbytes2) < 0)
 perror("read error\n");
 
 for (i=0; i<=10; i++)
 { printf("buf[%d] = %d, str2[%d] = %d\n", i, (int)buf[i],i,(int)str2[i]); }
 
 printf("here is the output: %s\n", buf);
 
 //sleep(4);
 */

//Test 4 
// Salve to master
//
//
/*char str3[] = "Poope!";
 char buf2[100];
 for (i=0; i<100; i++) { buf2[i] = 0; }
 int nbytes3 = strlen("Poope!");*/

/*printf("Testing the master and slave\n"
 "-------------------------------\n"
 "Writing \"%s\" to slave\n", str3);*/
//sleep(2);
//nbytes3 = write(fd_slave, str3, strlen(str3));
//if (nbytes3 != strlen(str3))
//printf("-nbytes- wasnt strlen(%s)+1\n", str3);

//printf("wrote %d bytes to slave\n", nbytes3); //sleep(1);
/*printf("reading %d bytes from master\n\n", nbytes3); //sleep(1);
 if (read(fd_master, buf2, nbytes3) < 0)
 perror("read error\n");
 
 //buf2[5] = '\n';
 //buf2[7] = 0;
 
 for (i=0; i < 10; i++) {
 printf("buf2[%d] = %d\n", i,(int)buf2[i]);
 } sleep(4);
 
 printf("here is the output: ");
 fflush(stdout);
 if (write(STDIN_FILENO, buf2, nbytes3) < 0)
 perror("---write to stdin failed\n");
 printf("\n");
 //sleep(2);*/

/*
 if (strcmp(buf2, str1) == 0)
 {
 printf("But that seems wrong??\n"); //sleep(1);
 printf("Apparently master buffers what it writes, as well\nas what it receives\n"); //sleep(1);
 printf("Let's read some more from the master..\n\n"); //sleep(1);
 
 
 printf("reading %d bytes from master\n", nbytes2);
 if (read(fd_master, buf2, nbytes2) < 0)
 perror("read error\n");
 
 for (i=0; i < 10; i++) {
 printf("buf2[%d] = %d, str1[%d] = %d\n", i, (int)buf2[i],i,(int)str2[i]);
 }
 
 printf("here is the output: %s\n\n", buf2);
 //sleep(1);
 printf("reading %d bytes from master\n", nbytes3);
 if (read(fd_master, buf2, nbytes3) < 0)
 perror("read error\n");
 
 for (i=0; i < 10; i++) {
 printf("buf2[%d] = %d, str1[%d] = %d\n", i, (int)buf2[i],i,(int)str3[i]);
 }
 
 printf("here is the output: %s\n\n", buf2);
 
 //sleep(3);
 }*/


// lets try reading form the slave then
//printf("reading %d of bytes from slave\n\n", nbytes1);
//if (read(fd_slave, buf2, nbytes3) < 0)
//perror("read error\n");
////sleep(1);
//printf("from slave: %s", buf2);

Posted in Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Skip to toolbar