author | viric@mandarina |
Mon, 28 Apr 2008 21:37:25 +0200 | |
changeset 90 | f172b95795d8 |
parent 82 | 5cbe47923060 |
child 94 | 330324fc7c20 |
permissions | -rw-r--r-- |
53 | 1 |
/* |
2 |
Terminal Mixer - multi-point multi-user access to terminal applications |
|
3 |
Copyright (C) 2007 LluĂs Batlle i Rossell |
|
4 |
||
5 |
Please find the license in the provided COPYING file. |
|
6 |
*/ |
|
21 | 7 |
#include <stdlib.h> |
24 | 8 |
#include <unistd.h> |
21 | 9 |
#include <assert.h> |
26
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
25
diff
changeset
|
10 |
#include <errno.h> |
21 | 11 |
#include <sys/select.h> |
12 |
||
13 |
#include "main.h" |
|
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
14 |
#include "filter.h" |
21 | 15 |
#include "handlers.h" |
16 |
||
17 |
/* it uses app_stdin, app_stdout, app_stderr. |
|
18 |
* They have value -1 if not opened. */ |
|
19 |
||
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
20 |
static struct FilterRules *fr; |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
21 |
|
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
22 |
void write_newline_cb(struct FilterRules *myfr, |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
23 |
const struct FFilter *ff, char *obuf, int *olen, |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
24 |
const char *ibuf, int pos) |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
25 |
{ |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
26 |
obuf[(*olen)++] = '\n'; |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
27 |
} |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
28 |
|
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
29 |
void app_control_start() |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
30 |
{ |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
31 |
struct FFilter *ff; |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
32 |
fr = new_filter_rules(); |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
33 |
|
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
34 |
/* We don't pay attention to any telnet command */ |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
35 |
ff = new_ftelnet(); |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
36 |
add_ffilter(fr, ff); |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
37 |
|
72 | 38 |
/* The raw terminal will send \r, |
39 |
* telnet will send \r\0. If we filter all \0, we're fine. |
|
40 |
* I used to change \r to \n here... It works better if I don't. */ |
|
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
41 |
|
72 | 42 |
/* We should trim the telnet's no-operation characters '\0'. */ |
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
43 |
ff = new_fstring_len("\0", 1); |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
44 |
add_ffilter(fr, ff); |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
45 |
} |
21 | 46 |
|
47 |
void app_control_shutdown() |
|
48 |
{ |
|
49 |
} |
|
50 |
||
51 |
void app_control_prepare_read_fdset(fd_set *read_set, int *maxfd) |
|
52 |
{ |
|
53 |
if (app_stdout != -1) |
|
54 |
{ |
|
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
55 |
debugmsg("Considering app_stdout %i", app_stdout); |
21 | 56 |
FD_SET(app_stdout, read_set); |
57 |
*maxfd = max(*maxfd, app_stdout); |
|
58 |
} |
|
59 |
||
25 | 60 |
if (app_stderr != -1 && app_stdout != app_stderr) |
21 | 61 |
{ |
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
62 |
debugmsg("Considering app_stderr %i", app_stderr); |
21 | 63 |
FD_SET(app_stderr, read_set); |
64 |
*maxfd = max(*maxfd, app_stderr); |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
/* Return -1, finished stdout/stderr. Else, return 0. */ |
|
69 |
int app_control_process_read_fdset(fd_set *read_set) |
|
70 |
{ |
|
71 |
if (app_stdout != -1 && FD_ISSET(app_stdout, read_set)) |
|
72 |
{ |
|
73 |
int res; |
|
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
74 |
debugmsg("Read app_stdout %i", app_stdout); |
21 | 75 |
res = read(app_stdout, stream_buffer, stream_buffer_size); |
26
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
25
diff
changeset
|
76 |
if (res == -1 && errno == EIO) |
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
25
diff
changeset
|
77 |
{ |
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
25
diff
changeset
|
78 |
/* I've noticed that when the app dies, read() returns EIO */ |
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
25
diff
changeset
|
79 |
app_stdout = -1; |
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
25
diff
changeset
|
80 |
app_stdin = -1; |
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
25
diff
changeset
|
81 |
return -1; |
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
25
diff
changeset
|
82 |
} |
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
25
diff
changeset
|
83 |
if (res == -1 ) |
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
25
diff
changeset
|
84 |
error("Error reading from app."); |
21 | 85 |
if (res == 0) |
86 |
{ |
|
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
87 |
debugmsg("Closing app_stdout %i", app_stdout); |
21 | 88 |
close(app_stdout); |
41 | 89 |
if (!command_line.s_param.nohup) |
90 |
close(1); |
|
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
91 |
app_stdout = -1; |
26
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
25
diff
changeset
|
92 |
if (app_stdout == app_stderr || app_stderr == -1) |
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
93 |
{ |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
94 |
app_stderr = -1; |
21 | 95 |
return -1; |
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
96 |
} |
21 | 97 |
} else |
98 |
{ |
|
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
99 |
hex_dump("from app", stream_buffer, res); |
41 | 100 |
if (!command_line.s_param.nohup) |
101 |
write(1, stream_buffer, res); |
|
33 | 102 |
if (command_line.s_param.serve_unix) |
103 |
s_unix_send_to_connected(stream_buffer, res); |
|
104 |
if (command_line.s_param.serve_tcp) |
|
105 |
s_tcp_send_to_connected(stream_buffer, res); |
|
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
44
diff
changeset
|
106 |
#ifdef linux |
44 | 107 |
if (command_line.s_param.serve_eth) |
108 |
s_eth_send_to_connected(stream_buffer, res); |
|
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
44
diff
changeset
|
109 |
#endif /* linux */ |
21 | 110 |
} |
111 |
} |
|
25 | 112 |
if (app_stderr != -1 && app_stdout != app_stderr && |
113 |
FD_ISSET(app_stderr, read_set)) |
|
21 | 114 |
{ |
115 |
int res; |
|
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
116 |
debugmsg("Read app_stderr %i", app_stdout); |
21 | 117 |
res = read(app_stderr, stream_buffer, stream_buffer_size); |
118 |
if (res == 0) |
|
119 |
{ |
|
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
120 |
debugmsg("Closing app_stderr %i", app_stderr); |
21 | 121 |
close(app_stderr); |
122 |
close(2); /* MOVE */ |
|
123 |
app_stderr = -1; |
|
124 |
if (app_stdout == -1) |
|
125 |
return -1; |
|
126 |
} else |
|
127 |
{ |
|
41 | 128 |
if (!command_line.s_param.nohup) |
129 |
write(2, stream_buffer, res); |
|
33 | 130 |
|
131 |
if (command_line.s_param.serve_unix) |
|
132 |
s_unix_send_to_connected(stream_buffer, res); |
|
133 |
if (command_line.s_param.serve_tcp) |
|
134 |
s_tcp_send_to_connected(stream_buffer, res); |
|
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
44
diff
changeset
|
135 |
#ifdef linux |
44 | 136 |
if (command_line.s_param.serve_eth) |
137 |
s_eth_send_to_connected(stream_buffer, res); |
|
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
44
diff
changeset
|
138 |
#endif /* linux */ |
21 | 139 |
} |
140 |
} |
|
141 |
return 0; |
|
142 |
} |
|
143 |
||
32
df110f784648
Added read-only clients, added stdin-close option to clients.
lbatlle@npdl268.bpo.hp.com
parents:
26
diff
changeset
|
144 |
void app_control_local_send_to_stdin(const char *buffer, size_t size) |
21 | 145 |
{ |
146 |
if (size == 0) |
|
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
147 |
{ |
21 | 148 |
close(app_stdin); |
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
149 |
if (app_stdout == app_stdin) |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
150 |
app_stdout = -1; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
151 |
if (app_stderr == app_stdin) |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
152 |
app_stderr = -1; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
153 |
} |
21 | 154 |
else |
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
155 |
{ |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
156 |
hex_dump("from local to app", buffer, size); |
21 | 157 |
write(app_stdin, buffer, size); |
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
158 |
} |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
159 |
} |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
160 |
|
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
161 |
static void write_data_to_app_stdin(const char *buffer, size_t size) |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
162 |
{ |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
163 |
if (size > 0) |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
164 |
{ |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
165 |
hex_dump("to app", buffer, size); |
41 | 166 |
if( !command_line.s_param.nohup && |
167 |
command_line.s_param.echo_in_local_terminal) |
|
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
168 |
write(1, buffer, size); |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
169 |
write(app_stdin, buffer, size); |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
170 |
} |
21 | 171 |
} |
32
df110f784648
Added read-only clients, added stdin-close option to clients.
lbatlle@npdl268.bpo.hp.com
parents:
26
diff
changeset
|
172 |
|
df110f784648
Added read-only clients, added stdin-close option to clients.
lbatlle@npdl268.bpo.hp.com
parents:
26
diff
changeset
|
173 |
void app_control_remote_send_to_stdin(const char *buffer, size_t size) |
df110f784648
Added read-only clients, added stdin-close option to clients.
lbatlle@npdl268.bpo.hp.com
parents:
26
diff
changeset
|
174 |
{ |
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
175 |
hex_dump("from client prefilter", buffer, size); |
32
df110f784648
Added read-only clients, added stdin-close option to clients.
lbatlle@npdl268.bpo.hp.com
parents:
26
diff
changeset
|
176 |
if (command_line.s_param.client_can_write) |
df110f784648
Added read-only clients, added stdin-close option to clients.
lbatlle@npdl268.bpo.hp.com
parents:
26
diff
changeset
|
177 |
{ |
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
178 |
int osize; |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
179 |
if (size > 0) |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
180 |
{ |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
181 |
filter_stream(fr, ostream_buffer, &osize, |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
182 |
buffer, size); |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
183 |
if (osize > 0) |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
184 |
write_data_to_app_stdin(ostream_buffer, osize); |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
185 |
} else if (size == 0) |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
186 |
{ |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
187 |
filter_flush(fr, ostream_buffer, &osize); |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
188 |
if (osize > 0) |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
189 |
write_data_to_app_stdin(ostream_buffer, osize); |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
190 |
} |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
191 |
|
32
df110f784648
Added read-only clients, added stdin-close option to clients.
lbatlle@npdl268.bpo.hp.com
parents:
26
diff
changeset
|
192 |
if (size == 0 && command_line.s_param.client_may_close_app_stdin) |
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
193 |
{ |
32
df110f784648
Added read-only clients, added stdin-close option to clients.
lbatlle@npdl268.bpo.hp.com
parents:
26
diff
changeset
|
194 |
close(app_stdin); |
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
195 |
if (app_stdout == app_stdin) |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
196 |
app_stdout = -1; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
197 |
if (app_stderr == app_stdin) |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
198 |
app_stderr = -1; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
72
diff
changeset
|
199 |
} |
32
df110f784648
Added read-only clients, added stdin-close option to clients.
lbatlle@npdl268.bpo.hp.com
parents:
26
diff
changeset
|
200 |
} |
df110f784648
Added read-only clients, added stdin-close option to clients.
lbatlle@npdl268.bpo.hp.com
parents:
26
diff
changeset
|
201 |
} |
66
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
53
diff
changeset
|
202 |
|
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
53
diff
changeset
|
203 |
void app_control_avoid_sending(fd_set *read_set) |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
53
diff
changeset
|
204 |
{ |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
53
diff
changeset
|
205 |
} |