|
|
1.1 root 1: /* spyon.c */
2:
3: /* Synchronet for *nix node spy */
4:
5: /* $Id: spyon.c,v 1.7 2006/05/08 18:58:21 deuce Exp $ */
6:
7: /****************************************************************************
8: * @format.tab-size 4 (Plain Text/Source Code File Header) *
9: * @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
10: * *
11: * Copyright 2003 Rob Swindell - http://www.synchro.net/copyright.html *
12: * *
13: * This program is free software; you can redistribute it and/or *
14: * modify it under the terms of the GNU General Public License *
15: * as published by the Free Software Foundation; either version 2 *
16: * of the License, or (at your option) any later version. *
17: * See the GNU General Public License for more details: gpl.txt or *
18: * http://www.fsf.org/copyleft/gpl.html *
19: * *
20: * Anonymous FTP access to the most recent released source is available at *
21: * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net *
22: * *
23: * Anonymous CVS access to the development source and modification history *
24: * is available at cvs.synchro.net:/cvsroot/sbbs, example: *
25: * cvs -d :pserver:[email protected]:/cvsroot/sbbs login *
26: * (just hit return, no password is necessary) *
27: * cvs -d :pserver:[email protected]:/cvsroot/sbbs checkout src *
28: * *
29: * For Synchronet coding style and modification guidelines, see *
30: * http://www.synchro.net/source.html *
31: * *
32: * You are encouraged to submit any modifications (preferably in Unix diff *
33: * format) via e-mail to [email protected] *
34: * *
35: * Note: If this box doesn't appear square, then you need to fix your tabs. *
36: ****************************************************************************/
37:
38: #include <unistd.h>
39: #include <stdlib.h>
40: #include "sockwrap.h" /* Must go before <sys/un.h> */
41: #include <sys/un.h>
42: #include <stdio.h>
43: #include <string.h>
44: #include "sockwrap.h"
45: #include "spyon.h"
46: #include "cterm.h"
47: #include "ciolib.h"
48:
49: int spyon(char *sockname) {
50: SOCKET spy_sock=INVALID_SOCKET;
51: struct sockaddr_un spy_name;
52: socklen_t spy_len;
53: unsigned char key;
54: unsigned char buf;
55: int i;
56: fd_set rd;
57: BOOL b;
58: int retval=0;
59: char ANSIbuf[32];
60: int parsing=0;
61: int telnet_strip=0;
62: struct text_info ti;
63: char *scrn;
64:
65: /* ToDo Test for it actually being a socket! */
66: /* Well, it will fail to connect won't it? */
67:
68: if((spy_sock=socket(PF_UNIX,SOCK_STREAM,0))==INVALID_SOCKET) {
69: printf("ERROR %d creating local spy socket", errno);
70: return(SPY_NOSOCKET);
71: }
72:
73: spy_name.sun_family=AF_UNIX;
74: SAFECOPY(spy_name.sun_path,sockname);
75: #ifdef SUN_LEN
76: spy_len=SUN_LEN(&spy_name);
77: #else
78: spy_len=sizeof(struct sockaddr_un);
79: #endif
80: if(connect(spy_sock,(struct sockaddr *)&spy_name,spy_len)) {
81: return(SPY_NOCONNECT);
82: }
83: i=1;
84:
85: gettextinfo(&ti);
86: scrn=(char *)alloca(ti.screenwidth*ti.screenheight*2);
87: gettext(1,1,ti.screenwidth,ti.screenheight,scrn);
88: textcolor(YELLOW);
89: textbackground(BLUE);
90: clrscr();
91: gotoxy(1,ti.screenheight);
92: cputs("Local spy mode... press CTRL-C to return to monitor");
93: clreol();
94: cterm_init(ti.screenheight-1,ti.screenwidth,1,1,0,NULL);
95: while(spy_sock!=INVALID_SOCKET) {
96: struct timeval tv;
97: tv.tv_sec=0;
98: tv.tv_usec=100;
99: FD_ZERO(&rd);
100: FD_SET(spy_sock,&rd);
101: if((select(spy_sock+1,&rd,NULL,NULL,&tv))<0) {
102: close(spy_sock);
103: spy_sock=INVALID_SOCKET;
104: retval=SPY_SELECTFAILED;
105: break;
106: }
107: if(!socket_check(spy_sock,NULL,&b,0)) {
108: close(spy_sock);
109: spy_sock=INVALID_SOCKET;
110: retval=SPY_SOCKETLOST;
111: break;
112: }
113: if(spy_sock != INVALID_SOCKET && FD_ISSET(spy_sock,&rd)) {
114: if((i=read(spy_sock,&buf,1))==1) {
115: if(telnet_strip) {
116: telnet_strip++;
117: if(buf==255 && telnet_strip==2) {
118: telnet_strip=0;
119: cterm_write(&buf,1,NULL,0,NULL);
120: }
121: if(telnet_strip==3)
122: telnet_strip=0;
123: }
124: else
125: if(buf==255)
126: telnet_strip=1;
127: else
128: cterm_write(&buf,1,NULL,0,NULL);
129: }
130: else if(i<0) {
131: close(spy_sock);
132: spy_sock=INVALID_SOCKET;
133: retval=SPY_SOCKETLOST;
134: break;
135: }
136: }
137: if(kbhit()) {
138: key=getch();
139: /* Check for control keys */
140: switch(key) {
141: case 0: /* Extended keys */
142: case 0xff:
143: getch();
144: break;
145: case 3: /* CTRL-C */
146: close(spy_sock);
147: spy_sock=INVALID_SOCKET;
148: retval=SPY_CLOSED;
149: break;
150: default:
151: write(spy_sock,&key,1);
152: }
153: }
154: }
155: puttext(1,1,ti.screenwidth,ti.screenheight,scrn);
156: window(ti.winleft,ti.wintop,ti.winright,ti.winbottom);
157: textattr(ti.attribute);
158: gotoxy(ti.curx,ti.cury);
159: return(retval);
160: }
161:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.