author | viric@llimona |
Mon, 08 Oct 2007 12:59:27 +0200 | |
changeset 79 | 7d316733d4b1 |
parent 76 | 5c0b9c9f9801 |
child 84 | 150a622f26ea |
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; |
|
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
28 |
sigset_t sigold,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); |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
35 |
|
23 | 36 |
do |
37 |
{ |
|
38 |
FD_ZERO(&read_set); |
|
39 |
||
40 |
FD_SET(0, &read_set); /* stdin */ |
|
24 | 41 |
maxfd = 0; |
42 |
||
43 |
net_prepare(&read_set, &maxfd); |
|
23 | 44 |
|
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
45 |
/* Prepare checking for timeout */ |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
46 |
sigprocmask(SIG_BLOCK, &signoalarm, &sigold); |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
47 |
|
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
48 |
#ifdef linux |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
49 |
if (command_line.s_param.serve_eth) |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
50 |
/* This will check if the timeout occurred, |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
51 |
* and will not do anything otherwise. */ |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
52 |
eth_proto_process_timeouts(); |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
53 |
#endif |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
54 |
|
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
55 |
res = pselect(maxfd + 1, &read_set, 0, 0, 0, &sigold); |
66
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
56 |
#ifdef linux |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
57 |
if (command_line.c_param.transport == ETHERNET) |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
58 |
/* 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
|
59 |
if (!eth_proto_process_timeouts()) |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
60 |
{ |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
61 |
error("Connection lost (no ACK received).\n"); |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
62 |
break; |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
63 |
} |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
64 |
#endif |
23 | 65 |
if (res == -1) |
66 |
{ |
|
67 |
if (errno == EINTR) |
|
68 |
continue; |
|
69 |
else |
|
70 |
error("Error in select()"); |
|
71 |
} |
|
72 |
||
24 | 73 |
res = net_process(&read_set); |
74 |
if (res == -1) /* EOF */ |
|
75 |
break; |
|
76 |
||
23 | 77 |
if (FD_ISSET(0, &read_set)) |
78 |
{ |
|
79 |
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
|
80 |
if (client_termin_fr) |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
81 |
{ |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
82 |
int olen; |
76
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
83 |
/* 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
|
84 |
* keyboard to the tm client, because |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
85 |
* *_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
|
86 |
* from the server to us (tm client). */ |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
87 |
hex_dump("Client prefilter", stream_buffer, res); |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
88 |
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
|
89 |
stream_buffer, |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
90 |
res); |
76
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
91 |
if (olen > 0) |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
92 |
net_send(ostream_buffer2, olen); |
70
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
93 |
} |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
94 |
else |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
95 |
net_send(stream_buffer, res); |
23 | 96 |
if (res == 0) /* EOF */ |
97 |
break; |
|
98 |
} |
|
99 |
} while (1); |
|
24 | 100 |
} |
23 | 101 |
|
76
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
102 |
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
|
103 |
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
|
104 |
{ |
76
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
105 |
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
|
106 |
if (ibuf[pos+3] == '.') |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
107 |
kill(getpid(), SIGINT); |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
108 |
else |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
109 |
{ |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
110 |
obuf[(*olen)++] = '~'; |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
111 |
obuf[(*olen)++] = '~'; |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
112 |
obuf[(*olen)++] = '~'; |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
113 |
} |
70
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
114 |
} |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
115 |
|
24 | 116 |
int client() |
117 |
{ |
|
118 |
Net_c_prepare_read_fdset net_prepare_read_fdset; |
|
119 |
Net_c_process_read_fdset net_process_read_fdset; |
|
120 |
Net_c_send net_send; |
|
121 |
||
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
29
diff
changeset
|
122 |
/* Prepare the filter */ |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
29
diff
changeset
|
123 |
{ |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
29
diff
changeset
|
124 |
struct FFilter *ff; |
70
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
125 |
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
|
126 |
ff = new_ftelnet(); |
70
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
127 |
add_ffilter(client_recv_fr, ff); |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
128 |
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
|
129 |
{ |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
130 |
client_termin_fr = new_filter_rules(); |
76
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
131 |
ff = new_ftildes(); |
5c0b9c9f9801
Fixed filter, and the client now parses ~~~ and ~~. (closing client)
viric@llimona
parents:
70
diff
changeset
|
132 |
ff->callback = filtercb_tildes; |
70
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
133 |
add_ffilter(client_termin_fr, ff); |
51e9b56b487b
New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents:
67
diff
changeset
|
134 |
} |
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
29
diff
changeset
|
135 |
} |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
29
diff
changeset
|
136 |
|
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
137 |
if (command_line.c_param.transport == UNIX) |
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
138 |
/* Will be 'tcp', 'ether', ... */ |
24 | 139 |
{ |
140 |
c_unix_connect_socket(); |
|
141 |
net_prepare_read_fdset = c_unix_prepare_read_fdset; |
|
142 |
net_process_read_fdset = c_unix_process_read_fdset; |
|
143 |
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
|
144 |
} |
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
43
diff
changeset
|
145 |
#ifdef linux |
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
43
diff
changeset
|
146 |
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
|
147 |
{ |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
36
diff
changeset
|
148 |
c_eth_init(); |
625794738afc
Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
36
diff
changeset
|
149 |
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
|
150 |
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
|
151 |
net_send = c_eth_send_to_connected; |
24 | 152 |
} |
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
43
diff
changeset
|
153 |
#endif /* linux */ |
24 | 154 |
|
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
155 |
if (command_line.c_param.raw_mode) |
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
156 |
prepare_user_terminal(); |
26
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
24
diff
changeset
|
157 |
|
24 | 158 |
loop(net_prepare_read_fdset, net_process_read_fdset, net_send); |
159 |
||
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
160 |
if (command_line.c_param.raw_mode) |
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
161 |
restore_user_terminal(); |
26
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
24
diff
changeset
|
162 |
|
23 | 163 |
return 0; |
164 |
} |