-
Notifications
You must be signed in to change notification settings - Fork 0
/
tty-over-tcp.c
361 lines (288 loc) · 8.82 KB
/
tty-over-tcp.c
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* -lutil */
/* Что будет, если одна из сторон завершится? */
/* ^] обрабатывается на стороне логического сервера, что не логично */
/* Если прибить logical-server с помощью Ctrl-C (независимо от tcp-серверности, проверено), то logical-client падает без вывода всякого сообщения об ошибке */
/* В каких случаях нужно печатать \n перед сообщением? */
/* Баг: запускаем logical-client, в нём mc, выходим, выходим из logical-client, размеры терминала по-прежнему неправильные, русский не работает */
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <termios.h>
#include <pty.h>
#include <err.h>
#include <libsh.h>
#include "tcp.h"
/* tcp-сервер подключает только одного клиента */
bool logical_server;
int sock;
int master;
void my_log(const char *message)
{
if (!logical_server)
{
fprintf(stderr, "logical-client: I'm not logical-server, but my_log(...) is called\r\n");
return;
}
fprintf(stderr, "logical-server: %s\n", message);
dprintf(sock, "logical-server: %s\r\n", message);
}
void chld_handler(int unused)
{
/*if (verbose){
fprintf(logfp, "[CLD] Child terminated\n");
}*/
for (;;){
char c;
int read_returned = read(master, &c, 1);
if (read_returned == -1){
/* Child closed tty */
/*if (verbose){
fprintf(logfp, "[EOF] Child stdout -> parent stdout\n");
}*/
break;
}else if (read_returned == 0){
my_log("cannot read from master: EOF");
exit (EXIT_FAILURE);
}
/*if (verbose){
fprintf(logfp, "[ %c ] Child stdout -> parent stdout\n", c);
}*/
switch (write(sock, &c, 1)){
case -1:
my_log(strerror(errno));
my_log("cannot write to socket");
exit (EXIT_FAILURE);
case 0:
my_log("write(sock) returned 0");
exit (EXIT_FAILURE);
}
}
my_log("child terminated, calling wait(...)...");
int status;
if (wait(&status) == -1)
{
my_log(strerror(errno));
my_log("wait");
exit (EXIT_FAILURE);
}
my_log("done, terminating");
if (WIFEXITED(status)){
exit (WEXITSTATUS(status));
}else if (WIFSIGNALED(status)){
exit (EXIT_FAILURE);
}else
{
my_log("child terminated in unusual way");
exit (EXIT_FAILURE);
}
}
int main(int argc, char *argv[])
{
sh_init (argv[0]);
if (argc < 4){
fprintf(stderr,
"Usage:\n"
"tty-over-tcp tcp-server [+]PORT logical-server COMMAND [ARG]...\n"
"tty-over-tcp tcp-server [+]PORT logical-client\n"
"tty-over-tcp tcp-client HOST [+]PORT logical-server COMMAND [ARG]...\n"
"tty-over-tcp tcp-client HOST [+]PORT logical-client\n"
);
exit (EXIT_FAILURE);
}
++argv;
fprintf(stderr, "logical-client-or-server: establishing connection...\n");
sock = mini_tcp_client_or_server_plus(&argv);
if (sock == -1)
{
exit (EXIT_FAILURE);
}
fprintf(stderr, "logical-client-or-server: done\n");
if (strcmp(argv[0], "logical-server") == 0){
logical_server = true;
}else if (strcmp(argv[0], "logical-client") == 0){
logical_server = false;
}else
{
fprintf(stderr, "Usage\n");
exit (EXIT_FAILURE);
}
/* На всякий случай */
setvbuf (stdin, NULL, _IONBF, 0);
setvbuf (stdout, NULL, _IONBF, 0);
setvbuf (stderr, NULL, _IONBF, 0);
if (logical_server){
/* logical-server */
FILE *fsock = fdopen(sock, "r");
setvbuf (fsock, NULL, _IONBF, 0);
char *term = NULL;
{
size_t term_size = 0;
if (getdelim(&term, &term_size, '\0', fsock) == -1){
my_log("cannot read TERM");
exit (EXIT_FAILURE);
}
}
my_log("calling forkpty(...)...");
pid_t pid = forkpty(&master, NULL, NULL, NULL);
switch (pid){
case -1:
my_log(strerror(errno));
my_log("is /dev/pts mounted?");
exit (EXIT_FAILURE);
case 0:
/* Child */
close(sock);
setenv("TERM", term, 1 /* Overwrite */);
execvp(argv[1], argv + 1);
my_log(strerror(errno));
exit (EXIT_FAILURE);
}
my_log("done, calling signal...");
/* Parent */
/* Copied from my program "pty" */
if (signal(SIGCHLD, &chld_handler) == SIG_ERR){
my_log(strerror(errno));
my_log("signal");
exit (EXIT_FAILURE);
}
my_log("done, working (press ^] to exit)");
bool eof_send = false;
for (;;){
fd_set set;
FD_ZERO(&set);
FD_SET(sock, &set);
FD_SET(master, &set);
if (select(100, &set, NULL, NULL, NULL) == -1){
if (errno == EINTR){
continue;
}else{
my_log(strerror(errno));
my_log("select");
exit (EXIT_FAILURE);
}
}
if (FD_ISSET(sock, &set)){
char c;
int read_returned = read(sock, &c, 1);
if (read_returned == -1){
my_log(strerror(errno));
my_log("cannot read from socket");
exit (EXIT_FAILURE);
}else if (read_returned == 0){
/*if (verbose && !eof_send){
fprintf(logfp, "[EOF] Parent stdin -> child stdin\n");
eof_send = true;
}*/
}else if (c == 29 /* ^] */){
my_log("^], exiting");
exit (EXIT_SUCCESS);
}else{
/*if (verbose){
fprintf(logfp, "[ %c ] Parent stdin -> child stdin\n", c);
}*/
switch (write(master, &c, 1)){
case -1:
my_log(strerror(errno));
my_log("cannot write to master");
exit (EXIT_FAILURE);
case 0:
my_log("write(master) returned 0");
exit (EXIT_FAILURE);
}
}
}
if (FD_ISSET(master, &set)){
char c;
int read_returned = read(master, &c, 1);
if (read_returned == -1){
/* Child closed tty */
/*if (verbose){
fprintf(logfp, "[EOF] Child stdout -> parent stdout\n");
}*/
continue;
}else if (read_returned == 0){
my_log("cannot read from master: EOF");
exit (EXIT_FAILURE);
}
/*if (verbose){
fprintf(logfp, "[ %c ] Child stdout -> parent stdout\n", c);
}*/
switch (write(sock, &c, 1)){
case -1:
my_log(strerror(errno));
my_log("cannot write to socket");
exit (EXIT_FAILURE);
case 0:
my_log("write(sock) returned 0");
exit (EXIT_FAILURE);
}
}
}
/* NOTREACHED */
}else{
/* logical-client */
const char *term = getenv("TERM");
if (term == NULL)
{
term = "";
}
if (*term == '\0')
{
fprintf(stderr, "logical-client: warning: TERM is empty or not set\n");
}
if (write(sock, term, strlen(term) + 1) != strlen(term) + 1){
fprintf(stderr, "logical-client: error: cannot write TERM to socket\n");
exit (EXIT_FAILURE);
}
fprintf(stderr, "logical-client: preparing terminal...\n");
{
struct termios term;
tcgetattr(fileno(stdin), &term);
// Copied from man cfmakeraw (I cannot write cfmakeraw, because Interix has no this function)
term.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR |
ICRNL | IXON);
term.c_oflag &= ~OPOST;
term.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
term.c_cflag &= ~(CSIZE | PARENB);
term.c_cflag |= CS8;
// End of man
tcsetattr(fileno(stdin), 0, &term);
}
fprintf(stderr, "logical-client: done, working\r\n");
{
struct sh_multicat_t m[2] = {{0}};
m[0].src = 0;
m[0].dst = sock;
m[0].flags = sh_rclose | sh_wshutdownw;
m[1].src = sock;
m[1].dst = 1;
m[1].flags = sh_wclose;
sh_multicat (m, 2);
}
{
struct termios term;
tcgetattr(fileno(stdin), &term);
// Copied from man cfmakeraw (I cannot write cfmakeraw, because Interix has no this function)
term.c_iflag |= (IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR |
ICRNL | IXON);
term.c_oflag |= OPOST;
term.c_lflag |= (ECHO | ECHONL | ICANON | ISIG | IEXTEN);
term.c_cflag |= (CSIZE | PARENB);
term.c_cflag &= ~CS8;
// End of man
tcsetattr(fileno(stdin), 0, &term);
}
exit (EXIT_SUCCESS);
/* NOTREACHED */
}
exit (EXIT_SUCCESS); /* For compiler */
}