author | viric <viriketo@gmail.com> |
Wed, 16 Feb 2011 20:57:32 +0100 | |
branch | saveflie |
changeset 94 | 330324fc7c20 |
parent 86 | c972d3312fbd |
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 |
*/ |
|
23 | 7 |
#include <stdio.h> |
8 |
#include <stdlib.h> |
|
9 |
#include <sys/types.h> |
|
10 |
#include <sys/select.h> |
|
11 |
#include <unistd.h> |
|
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
12 |
#include <signal.h> |
23 | 13 |
#include <errno.h> |
14 |
#include <string.h> |
|
15 |
||
16 |
#include "main.h" |
|
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
29
diff
changeset
|
17 |
#include "filter.h" |
23 | 18 |
#include "handlers.h" |
19 |
||
70
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
20 |
struct FilterRules *client_recv_fr = 0; |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
21 |
struct FilterRules *client_termin_fr = 0; |
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
29
diff
changeset
|
22 |
|
24 | 23 |
static void loop(Net_c_prepare_read_fdset net_prepare, |
24 |
Net_c_process_read_fdset net_process, |
|
25 |
Net_c_send net_send) |
|
23 | 26 |
{ |
27 |
fd_set read_set; |
|
86
c972d3312fbd
Better variable names: sigold to sigempty
lbatlle@npdl268.bpo.hp.com
parents:
84
diff
changeset
|
28 |
sigset_t sigempty,signoalarm; |
23 | 29 |
int maxfd; |
24 | 30 |
int res; |
23 | 31 |
|
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
32 |
/* Prepare the sigset for blocking alarm */ |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
33 |
sigemptyset(&signoalarm); |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
34 |
sigaddset(&signoalarm, SIGALRM); |
86
c972d3312fbd
Better variable names: sigold to sigempty
lbatlle@npdl268.bpo.hp.com
parents:
84
diff
changeset
|
35 |
sigemptyset(&sigempty); |
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
36 |
|
23 | 37 |
do |
38 |
{ |
|
39 |
FD_ZERO(&read_set); |
|
40 |
||
41 |
FD_SET(0, &read_set); /* stdin */ |
|
24 | 42 |
maxfd = 0; |
43 |
||
44 |
net_prepare(&read_set, &maxfd); |
|
23 | 45 |
|
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
46 |
/* Prepare checking for timeout */ |
86
c972d3312fbd
Better variable names: sigold to sigempty
lbatlle@npdl268.bpo.hp.com
parents:
84
diff
changeset
|
47 |
sigprocmask(SIG_BLOCK, &signoalarm, 0); |
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
48 |
|
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
49 |
#ifdef linux |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
50 |
if (command_line.s_param.serve_eth) |
84
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
76
diff
changeset
|
51 |
{ |
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
76
diff
changeset
|
52 |
int res; |
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
53 |
/* This will check if the timeout occurred, |
84
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
76
diff
changeset
|
54 |
* and will not do anything otherwise. |
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
76
diff
changeset
|
55 |
Will return false when the connection is lost. */ |
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
76
diff
changeset
|
56 |
res = eth_proto_process_timeouts(); |
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
76
diff
changeset
|
57 |
if (!res) |
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
76
diff
changeset
|
58 |
{ |
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
76
diff
changeset
|
59 |
printf("Connection lost (no ACK received).\n"); |
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
76
diff
changeset
|
60 |
break; |
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
76
diff
changeset
|
61 |
} |
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
76
diff
changeset
|
62 |
} |
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
63 |
#endif |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
64 |
|
86
c972d3312fbd
Better variable names: sigold to sigempty
lbatlle@npdl268.bpo.hp.com
parents:
84
diff
changeset
|
65 |
res = pselect(maxfd + 1, &read_set, 0, 0, 0, &sigempty); |
66
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
66 |
#ifdef linux |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
67 |
if (command_line.c_param.transport == ETHERNET) |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
68 |
/* If there isn't a good result, we quit */ |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
69 |
if (!eth_proto_process_timeouts()) |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
70 |
{ |
84
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
76
diff
changeset
|
71 |
printf("Connection lost (no ACK received).\n"); |
66
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
72 |
break; |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
73 |
} |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
74 |
#endif |
23 | 75 |
if (res == -1) |
76 |
{ |
|
77 |
if (errno == EINTR) |
|
78 |
continue; |
|
79 |
else |
|
80 |
error("Error in select()"); |
|
81 |
} |
|
82 |
||
24 | 83 |
res = net_process(&read_set); |
84 |
if (res == -1) /* EOF */ |
|
85 |
break; |
|
86 |
||
23 | 87 |
if (FD_ISSET(0, &read_set)) |
88 |
{ |
|
89 |
res = read(0, stream_buffer, stream_buffer_size); |
|
70
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
90 |
if (client_termin_fr) |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
91 |
{ |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
92 |
int olen; |
76
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
93 |
/* We use ostream_buffer2 for data from the |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
94 |
* keyboard to the tm client, because |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
95 |
* *_client.c use the ostream_buffer for data |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
96 |
* from the server to us (tm client). */ |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
97 |
hex_dump("Client prefilter", stream_buffer, res); |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
98 |
filter_stream(client_termin_fr, ostream_buffer2, &olen, |
70
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
99 |
stream_buffer, |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
100 |
res); |
76
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
101 |
if (olen > 0) |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
102 |
net_send(ostream_buffer2, olen); |
70
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
103 |
} |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
104 |
else |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
105 |
net_send(stream_buffer, res); |
23 | 106 |
if (res == 0) /* EOF */ |
107 |
break; |
|
108 |
} |
|
109 |
} while (1); |
|
24 | 110 |
} |
23 | 111 |
|
76
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
112 |
void filtercb_tildes(struct FilterRules *fr, const struct FFilter *ff, |
70
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
113 |
char *obuf, int *olen, const char *ibuf, int pos) |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
114 |
{ |
76
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
115 |
dump_line("filtercb_tildes: '%c'\n", ibuf[pos+3]); |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
116 |
if (ibuf[pos+3] == '.') |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
117 |
kill(getpid(), SIGINT); |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
118 |
else |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
119 |
{ |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
120 |
obuf[(*olen)++] = '~'; |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
121 |
obuf[(*olen)++] = '~'; |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
122 |
obuf[(*olen)++] = '~'; |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
123 |
} |
70
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
124 |
} |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
125 |
|
24 | 126 |
int client() |
127 |
{ |
|
128 |
Net_c_prepare_read_fdset net_prepare_read_fdset; |
|
129 |
Net_c_process_read_fdset net_process_read_fdset; |
|
130 |
Net_c_send net_send; |
|
131 |
||
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
29
diff
changeset
|
132 |
/* Prepare the filter */ |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
29
diff
changeset
|
133 |
{ |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
29
diff
changeset
|
134 |
struct FFilter *ff; |
70
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
135 |
client_recv_fr = new_filter_rules(); |
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
29
diff
changeset
|
136 |
ff = new_ftelnet(); |
70
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
137 |
add_ffilter(client_recv_fr, ff); |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
138 |
if (command_line.c_param.raw_mode) |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
139 |
{ |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
140 |
client_termin_fr = new_filter_rules(); |
76
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
141 |
ff = new_ftildes(); |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
142 |
ff->callback = filtercb_tildes; |
70
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
143 |
add_ffilter(client_termin_fr, ff); |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
144 |
} |
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
29
diff
changeset
|
145 |
} |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
29
diff
changeset
|
146 |
|
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
147 |
if (command_line.c_param.transport == UNIX) |
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
148 |
/* Will be 'tcp', 'ether', ... */ |
24 | 149 |
{ |
150 |
c_unix_connect_socket(); |
|
151 |
net_prepare_read_fdset = c_unix_prepare_read_fdset; |
|
152 |
net_process_read_fdset = c_unix_process_read_fdset; |
|
153 |
net_send = c_unix_send; |
|
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
43
diff
changeset
|
154 |
} |
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
43
diff
changeset
|
155 |
#ifdef linux |
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
43
diff
changeset
|
156 |
else if (command_line.c_param.transport == ETHERNET) |
43
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
36
diff
changeset
|
157 |
{ |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
36
diff
changeset
|
158 |
c_eth_init(); |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
36
diff
changeset
|
159 |
net_prepare_read_fdset = c_eth_prepare_read_fdset; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
36
diff
changeset
|
160 |
net_process_read_fdset = c_eth_process_read_fdset; |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
36
diff
changeset
|
161 |
net_send = c_eth_send_to_connected; |
24 | 162 |
} |
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
43
diff
changeset
|
163 |
#endif /* linux */ |
24 | 164 |
|
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
165 |
if (command_line.c_param.raw_mode) |
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
166 |
prepare_user_terminal(); |
26
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
24
diff
changeset
|
167 |
|
24 | 168 |
loop(net_prepare_read_fdset, net_process_read_fdset, net_send); |
169 |
||
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
170 |
if (command_line.c_param.raw_mode) |
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
171 |
restore_user_terminal(); |
26
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
24
diff
changeset
|
172 |
|
23 | 173 |
return 0; |
174 |
} |