umonitor.c 2.14 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "sockwrap.h"
#include <poll.h>
#include <termios.h>
#include <sys/un.h>
#include <stdio.h>

void usage(char *cmd)  {
	printf("Usage: %s <socket>\n\n Where socket is the filename of a unix domain socket\n\n",cmd);
	exit(0);
}

void spyon(char *sockname)  {
	SOCKET		spy_sock=INVALID_SOCKET;
	struct sockaddr_un spy_name;
	socklen_t	spy_len;
	char		key;
	char		buf[1];
	int		i;
	struct	termios	tio;
	struct	termios	old_tio;
	struct pollfd pset[2];

	/* ToDo Test for it actually being a socket! */

	if((spy_sock=socket(PF_LOCAL,SOCK_STREAM,0))==INVALID_SOCKET)  {
		printf("ERROR %d creating local spy socket", errno);
		return;
	}
	
	spy_name.sun_family=AF_LOCAL;
	SAFECOPY(spy_name.sun_path,sockname);
	spy_len=SUN_LEN(&spy_name);
	if(connect(spy_sock,(struct sockaddr *)&spy_name,spy_len))  {
		printf("Could not connect to %s\n",spy_name.sun_path);
		return;
	}
	i=1;

	tcgetattr(STDIN_FILENO,&old_tio);
	tcgetattr(STDIN_FILENO,&tio);
deuce's avatar
deuce committed
41
	cfmakeraw(&tio);
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
	tcsetattr(STDIN_FILENO,TCSANOW,&tio);
	pset[0].fd=STDIN_FILENO;
	pset[0].events=POLLIN;
	pset[1].fd=spy_sock;
	pset[1].events=POLLIN;
	while(spy_sock!=INVALID_SOCKET)  {
		if((poll(pset,2,INFTIM))<0)  {
			close(spy_sock);
			spy_sock=INVALID_SOCKET;
		}
		if(pset[0].revents)  {
			if(pset[0].revents&(POLLNVAL|POLLHUP|POLLERR))  {
				close(spy_sock);
				spy_sock=INVALID_SOCKET;
			}
			else  {
deuce's avatar
deuce committed
58
59
60
61
62
63
64
65
66
67
68
				if((i=read(STDIN_FILENO,&key,1))==1)  {
					/* Check for control keys */
					switch(key)  {
						case CTRL('c'):
							printf("\r\n\r\nConnection closed.\r\n");
							close(spy_sock);
							spy_sock=INVALID_SOCKET;
							return;
						default:
							write(spy_sock,&key,1);
					}
69
				}
deuce's avatar
deuce committed
70
				else if(i<0) {
71
72
73
74
75
76
77
78
79
80
81
					close(spy_sock);
					spy_sock=INVALID_SOCKET;
				}
			}
		}
		if(pset[1].revents)  {
			if(pset[1].revents&(POLLNVAL|POLLHUP|POLLERR))  {
				close(spy_sock);
				spy_sock=INVALID_SOCKET;
			}
			else  {
deuce's avatar
deuce committed
82
				if((i=read(spy_sock,&buf,1))==1)  {
83
					write(STDOUT_FILENO,buf,1);
deuce's avatar
deuce committed
84
85
				}
				else if(i<0) {
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
					close(spy_sock);
					spy_sock=INVALID_SOCKET;
				}
			}
		}
	}
	tcsetattr(STDIN_FILENO,TCSANOW,&old_tio);
}

int main(int argc, char** argv)  {
	if(argc==1)
		usage(argv[0]);

	spyon(argv[1]);
}