-
Notifications
You must be signed in to change notification settings - Fork 6
/
machine-connection.cc
428 lines (391 loc) · 11.6 KB
/
machine-connection.cc
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; -*-
* (c) [email protected]. Free Software. GNU Public License v3.0 and above
*/
#include "machine-connection.h"
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <charconv>
#include <map>
#include <string>
#if defined(__APPLE__)
#include <IOKit/serial/ioss.h>
#endif
#if defined(__APPLE__) && !defined(USE_TERMIOS)
#define USE_TERMIOS
#endif
#ifdef USE_TERMIOS
#include <termios.h>
#else
#include <asm/termbits.h>
#endif
#ifdef USE_TERMIOS
using tty_termios_t = struct termios;
#else
using tty_termios_t = struct termios2;
#endif
#ifdef USE_TERMIOS
// With termios, there is only a distinct set of available speeds.
static std::map<int, speed_t> AvailableTTYSpeeds();
#endif
bool MaybeAppleSpecificFallback(int fd, int bps) {
#ifdef IOSSIOSPEED
// Apple has a specific ioctl to set arbitrary speed.
speed_t speed = bps;
return ioctl(fd, IOSSIOSPEED, &speed) == 0;
#else
return false;
#endif
}
static bool SetTTYSpeed(int fd, tty_termios_t *tty, int speed_number) {
if (speed_number < 0) {
fprintf(stderr, "Invalid speed %d\n", speed_number);
return false;
}
#ifdef USE_TERMIOS
speed_t speed = B115200;
const std::map<int, speed_t> selectable_speeds = AvailableTTYSpeeds();
const auto found = selectable_speeds.find(speed_number);
if (found == selectable_speeds.end()) {
if (MaybeAppleSpecificFallback(fd, speed_number)) {
return true; // averted :)
}
fprintf(stderr, "Invalid speed '%d'; valid speeds are [", speed_number);
const char *sep = "";
for (const auto &[speed, _] : selectable_speeds) {
fprintf(stderr, "%s%d", sep, speed);
sep = ", ";
}
fprintf(stderr, "]\n");
return false;
}
speed = found->second;
if (cfsetospeed(tty, speed) < 0 || cfsetispeed(tty, speed) < 0) {
fprintf(stderr, "While attempting to set speed to %d bps\n",
speed_number);
perror("Issue while setting tty-speed");
return false;
}
#else
/* Clear the current output baud rate and fill a new value */
tty->c_cflag &= ~CBAUD;
tty->c_cflag |= BOTHER;
tty->c_ospeed = speed_number;
/* Clear the current input baud rate and fill a new value */
tty->c_cflag &= ~(CBAUD << IBSHIFT);
tty->c_cflag |= BOTHER << IBSHIFT;
tty->c_ispeed = speed_number;
#endif
return true;
}
static bool SetTTYParams(int fd, std::string_view parameters) {
tty_termios_t tty;
#ifdef USE_TERMIOS
if (tcgetattr(fd, &tty) < 0) {
perror("tcgetattr() failed. Is this a tty ?");
return false;
}
#else
if (ioctl(fd, TCGETS2, &tty)) {
perror("ioctl(TCGETS2) failed. Is this a tty ?");
return false;
}
#endif
// Some generic settings, possibly overridden later
tty.c_cflag |= (CLOCAL | CREAD); // no modem controls
tty.c_cflag &= ~CSIZE; // Reset size as we want to choose ..
tty.c_cflag |= CS8; // 8 .. bits
tty.c_cflag &= ~PARENB; // N
tty.c_cflag &= ~CSTOPB; // 1
tty.c_cflag |= CRTSCTS; // Hardware flow-control
// Terminal magic. Non-canonical mode
tty.c_iflag &=
~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tty.c_oflag &= ~OPOST;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 1;
SetTTYSpeed(fd, &tty, 115200);
while (!parameters.empty()) {
const auto pos = parameters.find(',');
const auto part_len =
(pos != std::string_view::npos) ? pos + 1 : parameters.size();
std::string_view param = parameters.substr(0, part_len);
parameters.remove_prefix(part_len);
if (param[0] == 'b' || param[0] == 'B') {
int s;
if (auto r = std::from_chars(param.begin() + 1, param.end(), s);
r.ec == std::errc()) {
if (!SetTTYSpeed(fd, &tty, s)) return false;
}
continue;
}
// Flags can be with optional positive or negative prefix.
bool flag_positive = true;
if (param[0] == '+') {
param = param.substr(1);
} else if (param[0] == '-') {
flag_positive = false;
param = param.substr(1);
}
if (param == "crtscts") {
if (flag_positive) {
tty.c_cflag |= CRTSCTS;
} else {
tty.c_cflag &= ~CRTSCTS;
}
} else {
fprintf(stderr, "Unknown option %.*s\n", (int)param.size(),
param.data());
return false;
}
}
#ifdef USE_TERMIOS
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
printf("Error from tcsetattr: %s\n", strerror(errno));
return false;
}
#else
if (ioctl(fd, TCSETS2, &tty)) {
printf("ioctl(TCSETS2) failed: %s\n", strerror(errno));
return false;
}
#endif
return true;
}
// Wait for input to become ready for read or timeout reached.
// If the file-descriptor becomes readable, returns number of milli-seconds
// left.
// Returns 0 on timeout (i.e. no millis left and nothing to be read).
// Returns -1 on error.
static int AwaitReadReady(int fd, int timeout_millis) {
fd_set read_fds;
FD_ZERO(&read_fds);
FD_SET(fd, &read_fds);
struct timeval tv;
tv.tv_sec = timeout_millis / 1000;
tv.tv_usec = (timeout_millis % 1000) * 1000;
FD_SET(fd, &read_fds);
int s = select(fd + 1, &read_fds, NULL, NULL, &tv);
if (s < 0) return -1;
return tv.tv_usec / 1000;
}
/*
*
* Public interface functions
*
*/
int OpenTCPSocket(const char *host) {
char *host_copy = NULL;
const char *port = "8888";
const char *colon_pos;
if ((colon_pos = strchr(host, ':')) != NULL) {
port = colon_pos + 1;
host_copy = strdup(host);
host_copy[colon_pos - host] = '\0';
host = host_copy;
}
struct addrinfo addr_hints = {};
addr_hints.ai_family = AF_INET;
addr_hints.ai_socktype = SOCK_STREAM;
struct addrinfo *addr_result = NULL;
int rc;
if ((rc = getaddrinfo(host, port, &addr_hints, &addr_result)) != 0) {
// We're in OpenTCPSocket(), because opening as a tty failed before,
// so make reference of that in this error message.
fprintf(stderr,
"Not a tty and "
"can't resolve as TCP endpoint '%s' (port %s): %s\n",
host, port, gai_strerror(rc));
free(host_copy);
return -1;
}
free(host_copy);
if (addr_result == NULL) return -1;
int fd = socket(addr_result->ai_family, addr_result->ai_socktype,
addr_result->ai_protocol);
if (fd >= 0 &&
connect(fd, addr_result->ai_addr, addr_result->ai_addrlen) < 0) {
perror("TCP connect()");
close(fd);
fd = -1;
}
freeaddrinfo(addr_result);
return fd;
}
static int OpenTTY(std::string_view descriptor) {
auto first_comma = descriptor.find(',');
const std::string path(descriptor.substr(0, first_comma));
const std::string_view tty_params = (first_comma != std::string_view::npos)
? descriptor.substr(first_comma + 1)
: "";
const int fd = open(path.c_str(), O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0) {
return -1;
}
if (!SetTTYParams(fd, tty_params)) {
return -1;
}
return fd;
}
MachineConnection::MachineConnection(int to_machine, int from_machine)
: output_fd_(to_machine),
input_fd_(from_machine),
reader_(input_fd_, (1 << 16), false) {}
MachineConnection::~MachineConnection() { close(output_fd_); }
MachineConnection *MachineConnection::Open(const char *descriptor) {
if (descriptor == nullptr) return nullptr;
if (strcmp(descriptor, "-") == 0) {
return new MachineConnection(STDOUT_FILENO, STDIN_FILENO);
}
if (const int fd = OpenTTY(descriptor); fd >= 0) {
return new MachineConnection(fd, fd);
}
if (const int fd = OpenTCPSocket(descriptor); fd >= 0) {
return new MachineConnection(fd, fd);
}
return nullptr;
}
int MachineConnection::DiscardPendingInput(int timeout_ms,
FILE *echo_discarded) {
if (input_fd_ < 0) return 0;
int total_bytes = 0;
char buf[128];
while (AwaitReadReady(input_fd_, timeout_ms) > 0) {
int r = read(input_fd_, buf, sizeof(buf));
if (r < 0) {
perror("reading trouble");
return -1;
}
total_bytes += r;
if (r > 0 && echo_discarded) {
fwrite(buf, r, 1, echo_discarded);
}
}
return total_bytes;
}
// Write buffer to fd.
static bool reliable_write(int fd, const char *buffer, int len) {
while (len) {
int w = write(fd, buffer, len);
if (w < 0) return false;
len -= w;
buffer += w;
}
return true;
}
// Write the sequence of string-views to file-descriptor.
// Needs "scratch_buffer" to be large enough to contain all of them.
bool MachineConnection::WriteBlocks(
char *scratch_buffer, const std::vector<std::string_view> &blocks) {
// Note: not using writev(), as snippets can be a lot and writev() has a
// bunch of limitations (e.g. UIO_MAXIOV == 1024). Thus simply
// reassmbling into buffer.
char *pos = scratch_buffer;
for (auto block : blocks) {
memcpy(pos, block.data(), block.size());
pos += block.size();
}
return reliable_write(output_fd_, scratch_buffer, pos - scratch_buffer);
}
#ifdef USE_TERMIOS
// Termios specifies speeds with macros and there is no easy way to figure
// out which exist, and if the speed_t is actually just the number itself, as
// this is hidden in the API abstraction.
// This builds a map of available speeds to the corresponding Bxxx constant.
static std::map<int, speed_t> AvailableTTYSpeeds() {
std::map<int, speed_t> result;
// The following are from the manpage. There are more at the lower end,
// but they are not really used these days.
#ifdef B1200
result[1200] = B1200;
#endif
#ifdef B1800
result[1800] = B1800;
#endif
#ifdef B2400
result[2400] = B2400;
#endif
#ifdef B4800
result[4800] = B4800;
#endif
#ifdef B9600
result[9600] = B9600;
#endif
#ifdef B19200
result[19200] = B19200;
#endif
#ifdef B38400
result[38400] = B38400;
#endif
#ifdef B57600
result[57600] = B57600;
#endif
#ifdef B76800
result[76800] = B76800;
#endif
#ifdef B115200
result[115200] = B115200;
#endif
#ifdef B153600
result[153600] = B153600;
#endif
#ifdef B230400
result[230400] = B230400;
#endif
#ifdef B250000
result[250000] = B250000;
#endif
#ifdef B307200
result[307200] = B307200;
#endif
#ifdef B460800
result[460800] = B460800;
#endif
#ifdef B500000
result[500000] = B500000;
#endif
#ifdef B576000
result[576000] = B576000;
#endif
#ifdef B614400
result[614400] = B614400;
#endif
#ifdef B921600
result[921600] = B921600;
#endif
#ifdef B1000000
result[1000000] = B1000000;
#endif
#ifdef B1152000
result[1152000] = B1152000;
#endif
#ifdef B1500000
result[1500000] = B1500000;
#endif
#ifdef B2000000
result[2000000] = B2000000;
#endif
#ifdef B2500000
result[2500000] = B2500000;
#endif
#ifdef B3000000
result[3000000] = B3000000;
#endif
#ifdef B3500000
result[3500000] = B3500000;
#endif
#ifdef B4000000
result[4000000] = B4000000;
#endif
return result;
}
#endif