supdup.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. /*
  2. * Supdup backend
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <limits.h>
  7. #include "putty.h"
  8. /*
  9. * TTYOPT FUNCTION BITS (36-bit bitmasks)
  10. */
  11. #define TOALT 0200000000000LL // Characters 0175 and 0176 are converted to altmode (0033) on input
  12. #define TOCLC 0100000000000LL // (user option bit) Convert lower-case input to upper-case
  13. #define TOERS 0040000000000LL // Selective erase is supported
  14. #define TOMVB 0010000000000LL // Backspacing is supported
  15. #define TOSAI 0004000000000LL // Stanford/ITS extended ASCII graphics character set is supported
  16. #define TOSA1 0002000000000LL // (user option bit) Characters 0001-0037 displayed using Stanford/ITS chars
  17. #define TOOVR 0001000000000LL // Overprinting is supported
  18. #define TOMVU 0000400000000LL // Moving cursor upwards is supported
  19. #define TOMOR 0000200000000LL // (user option bit) System should provide **MORE** processing
  20. #define TOROL 0000100000000LL // (user option bit) Terminal should scroll instead of wrapping
  21. #define TOLWR 0000020000000LL // Lowercase characters are supported
  22. #define TOFCI 0000010000000LL // Terminal can generate CONTROL and META characters
  23. #define TOLID 0000002000000LL // Line insert/delete operations supported
  24. #define TOCID 0000001000000LL // Character insert/delete operations supported
  25. #define TPCBS 0000000000040LL // Terminal is using the "intelligent terminal protocol" (must be on)
  26. #define TPORS 0000000000010LL // Server should process output resets
  27. // Initialization words (36-bit constants)
  28. #define WORDS 0777773000000 // Negative number of config words to send (6) in high 18 bits
  29. #define TCTYP 0000000000007 // Defines the terminal type (MUST be 7)
  30. #define TTYROL 0000000000001 // Scroll amount for terminal (1 line at a time)
  31. // %TD opcodes
  32. //
  33. #define TDMOV 0200 // Cursor positioning
  34. #define TDMV1 0201 // Internal cursor positioning
  35. #define TDEOF 0202 // Erase to end of screen
  36. #define TDEOL 0203 // Erase to end of line
  37. #define TDDLF 0204 // Clear the character the cursor is on
  38. #define TDCRL 0207 // Carriage return
  39. #define TDNOP 0210 // No-op; should be ignored.
  40. #define TDBS 0211 // Backspace (not in official SUPDUP spec)
  41. #define TDLF 0212 // Linefeed (not in official SUPDUP spec)
  42. #define TDCR 0213 // Carriage Return (ditto)
  43. #define TDORS 0214 // Output reset
  44. #define TDQOT 0215 // Quotes the following character
  45. #define TDFS 0216 // Non-destructive forward space
  46. #define TDMV0 0217 // General cursor positioning code
  47. #define TDCLR 0220 // Erase the screen, home cursor
  48. #define TDBEL 0221 // Generate an audio tone, bell, whatever
  49. #define TDILP 0223 // Insert blank lines at the cursor
  50. #define TDDLP 0224 // Delete lines at the cursor
  51. #define TDICP 0225 // Insert blanks at cursor
  52. #define TDDCP 0226 // Delete characters at cursor
  53. #define TDBOW 0227 // Display black chars on white screen
  54. #define TDRST 0230 // Reset %TDBOW
  55. /* Maximum number of octets following a %TD code. */
  56. #define TD_ARGS_MAX 4
  57. typedef struct supdup_tag Supdup;
  58. struct supdup_tag
  59. {
  60. Socket *s;
  61. bool socket_connected;
  62. bool closed_on_socket_error;
  63. Seat *seat;
  64. LogContext *logctx;
  65. Ldisc *ldisc;
  66. int term_width, term_height;
  67. char *description;
  68. long long ttyopt;
  69. long tcmxv;
  70. long tcmxh;
  71. bool sent_location;
  72. Conf *conf;
  73. int bufsize;
  74. enum {
  75. CONNECTING, // waiting for %TDNOP from server after sending connection params
  76. CONNECTED // %TDNOP received, connected.
  77. } state;
  78. enum {
  79. TD_TOPLEVEL,
  80. TD_ARGS,
  81. TD_ARGSDONE
  82. } tdstate;
  83. int td_code;
  84. int td_argcount;
  85. char td_args[TD_ARGS_MAX];
  86. int td_argindex;
  87. void (*print) (strbuf *outbuf, int c);
  88. Pinger *pinger;
  89. Plug plug;
  90. Backend backend;
  91. Interactor interactor;
  92. };
  93. #define SUPDUP_MAX_BACKLOG 4096
  94. static void c_write(Supdup *supdup, unsigned char *buf, int len)
  95. {
  96. size_t backlog = seat_stdout(supdup->seat, buf, len);
  97. sk_set_frozen(supdup->s, backlog > SUPDUP_MAX_BACKLOG);
  98. }
  99. static void supdup_send_location(Supdup *supdup)
  100. {
  101. char locHeader[] = { 0300, 0302 };
  102. char* locString = conf_get_str(supdup->conf, CONF_supdup_location);
  103. sk_write(supdup->s, locHeader, sizeof(locHeader));
  104. sk_write(supdup->s, locString, strlen(locString) + 1); // include NULL terminator
  105. }
  106. static void print_ascii(strbuf *outbuf, int c)
  107. {
  108. /* In ASCII mode, ignore control characters. The server shouldn't
  109. send them. */
  110. if (c >= 040 && c < 0177)
  111. put_byte (outbuf, c);
  112. }
  113. static void print_its(strbuf *outbuf, int c)
  114. {
  115. /* The ITS character set is documented in RFC 734. */
  116. static const char *map[] = {
  117. "\xc2\xb7", "\342\206\223", "\316\261", "\316\262",
  118. "\342\210\247", "\302\254", "\316\265", "\317\200",
  119. "\316\273", "\xce\xb3", "\xce\xb4", "\xe2\x86\x91",
  120. "\xc2\xb1", "\xe2\x8a\x95", "\342\210\236", "\342\210\202",
  121. "\342\212\202", "\342\212\203", "\342\210\251", "\342\210\252",
  122. "\342\210\200", "\342\210\203", "\xe2\x8a\x97", "\342\206\224",
  123. "\xe2\x86\x90", "\342\206\222", "\xe2\x89\xa0", "\xe2\x97\x8a",
  124. "\342\211\244", "\342\211\245", "\342\211\241", "\342\210\250",
  125. " ", "!", "\"", "#", "$", "%", "&", "'",
  126. "(", ")", "*", "+", ",", "-", ".", "/",
  127. "0", "1", "2", "3", "4", "5", "6", "7",
  128. "8", "9", ":", ";", "<", "=", ">", "?",
  129. "@", "A", "B", "C", "D", "E", "F", "G",
  130. "H", "I", "J", "K", "L", "M", "N", "O",
  131. "P", "Q", "R", "S", "T", "U", "V", "W",
  132. "X", "Y", "Z", "[", "\\", "]", "^", "_",
  133. "`", "a", "b", "c", "d", "e", "f", "g",
  134. "h", "i", "j", "k", "l", "m", "n", "o",
  135. "p", "q", "r", "s", "t", "u", "v", "w",
  136. "x", "y", "z", "{", "|", "}", "~", "\xe2\x88\xab"
  137. };
  138. put_data (outbuf, map[c], strlen(map[c]));
  139. }
  140. static void print_waits(strbuf *outbuf, int c)
  141. {
  142. /* The WAITS character set used at the Stanford AI Lab is documented
  143. here: https://www.saildart.org/allow/sail-charset-utf8.html */
  144. static const char *map[] = {
  145. "", "\342\206\223", "\316\261", "\316\262",
  146. "\342\210\247", "\302\254", "\316\265", "\317\200",
  147. "\316\273", "", "", "",
  148. "", "", "\342\210\236", "\342\210\202",
  149. "\342\212\202", "\342\212\203", "\342\210\251", "\342\210\252",
  150. "\342\210\200", "\342\210\203", "\xe2\x8a\x97", "\342\206\224",
  151. "_", "\342\206\222", "~", "\xe2\x89\xa0",
  152. "\342\211\244", "\342\211\245", "\342\211\241", "\342\210\250",
  153. " ", "!", "\"", "#", "$", "%", "&", "'",
  154. "(", ")", "*", "+", ",", "-", ".", "/",
  155. "0", "1", "2", "3", "4", "5", "6", "7",
  156. "8", "9", ":", ";", "<", "=", ">", "?",
  157. "@", "A", "B", "C", "D", "E", "F", "G",
  158. "H", "I", "J", "K", "L", "M", "N", "O",
  159. "P", "Q", "R", "S", "T", "U", "V", "W",
  160. "X", "Y", "Z", "[", "\\", "]", "\xe2\x86\x91", "\xe2\x86\x90",
  161. "`", "a", "b", "c", "d", "e", "f", "g",
  162. "h", "i", "j", "k", "l", "m", "n", "o",
  163. "p", "q", "r", "s", "t", "u", "v", "w",
  164. "x", "y", "z", "{", "|", "\xe2\x97\x8a", "}", ""
  165. };
  166. put_data (outbuf, map[c], strlen(map[c]));
  167. }
  168. static void do_toplevel(Supdup *supdup, strbuf *outbuf, int c)
  169. {
  170. // Toplevel: Waiting for a %TD code or a printable character
  171. if (c >= 0200) {
  172. // Handle SUPDUP %TD codes (codes greater than or equal to 200)
  173. supdup->td_argindex = 0;
  174. supdup->td_code = c;
  175. switch (c) {
  176. case TDMOV:
  177. // %TD codes using 4 arguments
  178. supdup->td_argcount = 4;
  179. supdup->tdstate = TD_ARGS;
  180. break;
  181. case TDMV0:
  182. case TDMV1:
  183. // %TD codes using 2 arguments
  184. supdup->td_argcount = 2;
  185. supdup->tdstate = TD_ARGS;
  186. break;
  187. case TDQOT:
  188. case TDILP:
  189. case TDDLP:
  190. case TDICP:
  191. case TDDCP:
  192. // %TD codes using 1 argument
  193. supdup->td_argcount = 1;
  194. supdup->tdstate = TD_ARGS;
  195. break;
  196. case TDEOF:
  197. case TDEOL:
  198. case TDDLF:
  199. case TDCRL:
  200. case TDNOP:
  201. case TDORS:
  202. case TDFS:
  203. case TDCLR:
  204. case TDBEL:
  205. case TDBOW:
  206. case TDRST:
  207. case TDBS:
  208. case TDCR:
  209. case TDLF:
  210. // %TD codes using 0 arguments
  211. supdup->td_argcount = 0;
  212. supdup->tdstate = TD_ARGSDONE;
  213. break;
  214. default:
  215. // Unhandled, ignore
  216. break;
  217. }
  218. } else {
  219. supdup->print(outbuf, c);
  220. }
  221. }
  222. static void do_args(Supdup *supdup, strbuf *outbuf, int c)
  223. {
  224. // Collect up args for %TD code
  225. if (supdup->td_argindex < TD_ARGS_MAX) {
  226. supdup->td_args[supdup->td_argindex] = c;
  227. supdup->td_argindex++;
  228. if (supdup->td_argcount == supdup->td_argindex) {
  229. // No more args, %TD code is ready to go.
  230. supdup->tdstate = TD_ARGSDONE;
  231. }
  232. } else {
  233. // Should never hit this state, if we do we will just
  234. // return to TOPLEVEL.
  235. supdup->tdstate = TD_TOPLEVEL;
  236. }
  237. }
  238. static void do_argsdone(Supdup *supdup, strbuf *outbuf, int c)
  239. {
  240. char buf[4];
  241. int x, y;
  242. // Arguments for %TD code have been collected; dispatch based
  243. // on the %TD code we're handling.
  244. switch (supdup->td_code) {
  245. case TDMOV:
  246. /*
  247. General cursor position code. Followed by four bytes;
  248. the first two are the "old" vertical and horizontal
  249. positions and may be ignored. The next two are the new
  250. vertical and horizontal positions. The cursor should be
  251. moved to this position.
  252. */
  253. // We only care about the new position.
  254. put_fmt(outbuf, "\033[%d;%dH", supdup->td_args[2]+1, supdup->td_args[3]+1);
  255. break;
  256. case TDMV0:
  257. case TDMV1:
  258. /*
  259. General cursor position code. Followed by two bytes;
  260. the new vertical and horizontal positions.
  261. */
  262. put_fmt(outbuf, "\033[%d;%dH", supdup->td_args[0]+1, supdup->td_args[1]+1);
  263. break;
  264. case TDEOF:
  265. /*
  266. Erase to end of screen. This is an optional function
  267. since many terminals do not support this. If the
  268. terminal does not support this function, it should be
  269. treated the same as %TDEOL.
  270. %TDEOF does an erase to end of line, then erases all
  271. lines lower on the screen than the cursor. The cursor
  272. does not move.
  273. */
  274. put_fmt(outbuf, "\033[J");
  275. break;
  276. case TDEOL:
  277. /*
  278. Erase to end of line. This erases the character
  279. position the cursor is at and all positions to the right
  280. on the same line. The cursor does not move.
  281. */
  282. put_fmt(outbuf, "\033[K");
  283. break;
  284. case TDDLF:
  285. /*
  286. Clear the character position the cursor is on. The
  287. cursor does not move.
  288. */
  289. put_fmt(outbuf, "\033[X");
  290. break;
  291. case TDCRL:
  292. /*
  293. If the cursor is not on the bottom line of the screen,
  294. move cursor to the beginning of the next line and clear
  295. that line. If the cursor is at the bottom line, scroll
  296. up.
  297. */
  298. put_fmt(outbuf, "\015\012");
  299. break;
  300. case TDNOP:
  301. /*
  302. No-op; should be ignored.
  303. */
  304. break;
  305. case TDORS:
  306. /*
  307. Output reset. This code serves as a data mark for
  308. aborting output much as IAC DM does in the ordinary
  309. TELNET protocol.
  310. */
  311. outbuf->len = 0;
  312. if (!seat_get_cursor_position(supdup->seat, &x, &y))
  313. x = y = 0;
  314. buf[0] = 034;
  315. buf[1] = 020;
  316. buf[2] = y;
  317. buf[3] = x;
  318. sk_write(supdup->s, buf, 4);
  319. break;
  320. case TDQOT:
  321. /*
  322. Quotes the following character. This is used when
  323. sending 8-bit codes which are not %TD codes, for
  324. instance when loading programs into an intelligent
  325. terminal. The following character should be passed
  326. through intact to the terminal.
  327. */
  328. put_byte(outbuf, supdup->td_args[0]);
  329. break;
  330. case TDFS:
  331. /*
  332. Non-destructive forward space. The cursor moves right
  333. one position; this code will not be sent at the end of a
  334. line.
  335. */
  336. put_fmt(outbuf, "\033[C");
  337. break;
  338. case TDCLR:
  339. /*
  340. Erase the screen. Home the cursor to the top left hand
  341. corner of the screen.
  342. */
  343. put_fmt(outbuf, "\033[2J\033[H");
  344. break;
  345. case TDBEL:
  346. /*
  347. Generate an audio tone, bell, whatever.
  348. */
  349. put_fmt(outbuf, "\007");
  350. break;
  351. case TDILP:
  352. /*
  353. Insert blank lines at the cursor; followed by a byte
  354. containing a count of the number of blank lines to
  355. insert. The cursor is unmoved. The line the cursor is
  356. on and all lines below it move down; lines moved off the
  357. bottom of the screen are lost.
  358. */
  359. put_fmt(outbuf, "\033[%dL", supdup->td_args[0]);
  360. break;
  361. case TDDLP:
  362. /*
  363. Delete lines at the cursor; followed by a count. The
  364. cursor is unmoved. The first line deleted is the one
  365. the cursor is on. Lines below those deleted move up.
  366. Newly- created lines at the bottom of the screen are
  367. blank.
  368. */
  369. put_fmt(outbuf, "\033[%dM", supdup->td_args[0]);
  370. break;
  371. case TDICP:
  372. /*
  373. Insert blank character positions at the cursor; followed
  374. by a count. The cursor is unmoved. The character the
  375. cursor is on and all characters to the right on the
  376. current line move to the right; characters moved off the
  377. end of the line are lost.
  378. */
  379. put_fmt(outbuf, "\033[%d@", supdup->td_args[0]);
  380. break;
  381. case TDDCP:
  382. /*
  383. Delete characters at the cursor; followed by a count.
  384. The cursor is unmoved. The first character deleted is
  385. the one the cursor is on. Newly-created characters at
  386. the end of the line are blank.
  387. */
  388. put_fmt(outbuf, "\033[%dP", supdup->td_args[0]);
  389. break;
  390. case TDBOW:
  391. case TDRST:
  392. /*
  393. Display black characters on white screen.
  394. HIGHLY OPTIONAL.
  395. */
  396. // Since this is HIGHLY OPTIONAL, I'm not going
  397. // to implement it yet.
  398. break;
  399. /*
  400. * Non-standard (whatever "standard" means here) SUPDUP
  401. * commands. These are used (at the very least) by
  402. * Genera's SUPDUP implementation. Cannot find any
  403. * official documentation, behavior is based on UNIX
  404. * SUPDUP implementation from MIT.
  405. */
  406. case TDBS:
  407. /*
  408. * Backspace -- move cursor back one character (does not
  409. * appear to wrap...)
  410. */
  411. put_byte(outbuf, '\010');
  412. break;
  413. case TDLF:
  414. /*
  415. * Linefeed -- move cursor down one line (again, no wrapping)
  416. */
  417. put_byte(outbuf, '\012');
  418. break;
  419. case TDCR:
  420. /*
  421. * Carriage return -- move cursor to start of current line.
  422. */
  423. put_byte(outbuf, '\015');
  424. break;
  425. }
  426. // Return to top level to pick up the next %TD code or
  427. // printable character.
  428. supdup->tdstate = TD_TOPLEVEL;
  429. }
  430. static void term_out_supdup(Supdup *supdup, strbuf *outbuf, int c)
  431. {
  432. if (supdup->tdstate == TD_TOPLEVEL) {
  433. do_toplevel (supdup, outbuf, c);
  434. } else if (supdup->tdstate == TD_ARGS) {
  435. do_args (supdup, outbuf, c);
  436. }
  437. // If all arguments for a %TD code are ready, we will execute the code now.
  438. if (supdup->tdstate == TD_ARGSDONE) {
  439. do_argsdone (supdup, outbuf, c);
  440. }
  441. }
  442. static void do_supdup_read(Supdup *supdup, const char *buf, size_t len)
  443. {
  444. strbuf *outbuf = strbuf_new();
  445. while (len--) {
  446. int c = (unsigned char)*buf++;
  447. switch (supdup->state) {
  448. case CONNECTING:
  449. // "Following the transmission of the terminal options by
  450. // the user, the server should respond with an ASCII
  451. // greeting message, terminated with a %TDNOP code..."
  452. if (TDNOP == c) {
  453. // Greeting done, switch to the CONNECTED state.
  454. supdup->state = CONNECTED;
  455. supdup->tdstate = TD_TOPLEVEL;
  456. } else {
  457. // Forward the greeting message (which is straight
  458. // ASCII, no controls) on so it gets displayed TODO:
  459. // filter out only printable chars?
  460. put_byte(outbuf, c);
  461. }
  462. break;
  463. case CONNECTED:
  464. // "All transmissions from the server after the %TDNOP
  465. // [see above] are either printing characters or virtual
  466. // terminal display codes." Forward these on to the
  467. // frontend which will decide what to do with them.
  468. term_out_supdup(supdup, outbuf, c);
  469. /*
  470. * Hack to make Symbolics Genera SUPDUP happy: Wait until
  471. * after we're connected (finished the initial handshake
  472. * and have gotten additional data) before sending the
  473. * location string. For some reason doing so earlier
  474. * causes the Symbolics SUPDUP to end up in an odd state.
  475. */
  476. if (!supdup->sent_location) {
  477. supdup_send_location(supdup);
  478. supdup->sent_location = true;
  479. }
  480. break;
  481. }
  482. if (outbuf->len >= 4096) {
  483. c_write(supdup, outbuf->u, outbuf->len);
  484. outbuf->len = 0;
  485. }
  486. }
  487. if (outbuf->len)
  488. c_write(supdup, outbuf->u, outbuf->len);
  489. strbuf_free(outbuf);
  490. }
  491. static void supdup_log(Plug *plug, Socket *s, PlugLogType type, SockAddr *addr,
  492. int port, const char *error_msg, int error_code)
  493. {
  494. Supdup *supdup = container_of(plug, Supdup, plug);
  495. backend_socket_log(supdup->seat, supdup->logctx, s, type, addr, port,
  496. error_msg, error_code,
  497. supdup->conf, supdup->socket_connected);
  498. if (type == PLUGLOG_CONNECT_SUCCESS) {
  499. supdup->socket_connected = true;
  500. if (supdup->ldisc)
  501. ldisc_check_sendok(supdup->ldisc);
  502. }
  503. }
  504. static void supdup_closing(Plug *plug, PlugCloseType type,
  505. const char *error_msg)
  506. {
  507. Supdup *supdup = container_of(plug, Supdup, plug);
  508. /*
  509. * We don't implement independent EOF in each direction for Telnet
  510. * connections; as soon as we get word that the remote side has
  511. * sent us EOF, we wind up the whole connection.
  512. */
  513. if (supdup->s) {
  514. sk_close(supdup->s);
  515. supdup->s = NULL;
  516. if (error_msg)
  517. supdup->closed_on_socket_error = true;
  518. seat_notify_remote_exit(supdup->seat);
  519. seat_notify_remote_disconnect(supdup->seat);
  520. }
  521. if (type != PLUGCLOSE_NORMAL) {
  522. logevent(supdup->logctx, error_msg);
  523. if (type != PLUGCLOSE_USER_ABORT)
  524. seat_connection_fatal(supdup->seat, "%s", error_msg);
  525. }
  526. /* Otherwise, the remote side closed the connection normally. */
  527. }
  528. static void supdup_receive(Plug *plug, int urgent, const char *data, size_t len)
  529. {
  530. Supdup *supdup = container_of(plug, Supdup, plug);
  531. do_supdup_read(supdup, data, len);
  532. }
  533. static void supdup_sent(Plug *plug, size_t bufsize)
  534. {
  535. Supdup *supdup = container_of(plug, Supdup, plug);
  536. supdup->bufsize = bufsize;
  537. seat_sent(supdup->seat, supdup->bufsize);
  538. }
  539. static void supdup_send_36bits(Supdup *supdup, unsigned long long thirtysix)
  540. {
  541. //
  542. // From RFC734:
  543. // "Each word is sent through the 8-bit connection as six
  544. // 6-bit bytes, most-significant first."
  545. //
  546. // Split the 36-bit word into 6 6-bit "bytes", packed into
  547. // 8-bit bytes and send, most-significant byte first.
  548. //
  549. for (int i = 5; i >= 0; i--) {
  550. char sixBits = (thirtysix >> (i * 6)) & 077;
  551. sk_write(supdup->s, &sixBits, 1);
  552. }
  553. }
  554. static void supdup_send_config(Supdup *supdup)
  555. {
  556. supdup_send_36bits(supdup, WORDS); // negative length
  557. supdup_send_36bits(supdup, TCTYP); // terminal type
  558. supdup_send_36bits(supdup, supdup->ttyopt); // options
  559. supdup_send_36bits(supdup, supdup->tcmxv); // height
  560. supdup_send_36bits(supdup, supdup->tcmxh); // width
  561. supdup_send_36bits(supdup, TTYROL); // scroll amount
  562. }
  563. static char *supdup_description(Interactor *itr)
  564. {
  565. Supdup *supdup = container_of(itr, Supdup, interactor);
  566. return dupstr(supdup->description);
  567. }
  568. static LogPolicy *supdup_logpolicy(Interactor *itr)
  569. {
  570. Supdup *supdup = container_of(itr, Supdup, interactor);
  571. return log_get_policy(supdup->logctx);
  572. }
  573. static Seat *supdup_get_seat(Interactor *itr)
  574. {
  575. Supdup *supdup = container_of(itr, Supdup, interactor);
  576. return supdup->seat;
  577. }
  578. static void supdup_set_seat(Interactor *itr, Seat *seat)
  579. {
  580. Supdup *supdup = container_of(itr, Supdup, interactor);
  581. supdup->seat = seat;
  582. }
  583. static const InteractorVtable Supdup_interactorvt = {
  584. .description = supdup_description,
  585. .logpolicy = supdup_logpolicy,
  586. .get_seat = supdup_get_seat,
  587. .set_seat = supdup_set_seat,
  588. };
  589. /*
  590. * Called to set up the Supdup connection.
  591. *
  592. * Returns an error message, or NULL on success.
  593. *
  594. * Also places the canonical host name into `realhost'. It must be
  595. * freed by the caller.
  596. */
  597. static char *supdup_init(const BackendVtable *x, Seat *seat,
  598. Backend **backend_handle,
  599. LogContext *logctx, Conf *conf,
  600. const char *host, int port, char **realhost,
  601. bool nodelay, bool keepalive)
  602. {
  603. static const PlugVtable fn_table = {
  604. .log = supdup_log,
  605. .closing = supdup_closing,
  606. .receive = supdup_receive,
  607. .sent = supdup_sent,
  608. };
  609. SockAddr *addr;
  610. const char *err;
  611. Supdup *supdup;
  612. char *loghost;
  613. int addressfamily;
  614. const char *utf8 = "\033%G";
  615. supdup = snew(struct supdup_tag);
  616. memset(supdup, 0, sizeof(Supdup));
  617. supdup->plug.vt = &fn_table;
  618. supdup->backend.vt = &supdup_backend;
  619. supdup->interactor.vt = &Supdup_interactorvt;
  620. supdup->backend.interactor = &supdup->interactor;
  621. supdup->logctx = logctx;
  622. supdup->conf = conf_copy(conf);
  623. supdup->s = NULL;
  624. supdup->socket_connected = false;
  625. supdup->closed_on_socket_error = false;
  626. supdup->seat = seat;
  627. supdup->term_width = conf_get_int(supdup->conf, CONF_width);
  628. supdup->term_height = conf_get_int(supdup->conf, CONF_height);
  629. supdup->pinger = NULL;
  630. supdup->sent_location = false;
  631. supdup->description = default_description(supdup->backend.vt, host, port);
  632. *backend_handle = &supdup->backend;
  633. switch (conf_get_int(supdup->conf, CONF_supdup_ascii_set)) {
  634. case SUPDUP_CHARSET_ASCII:
  635. supdup->print = print_ascii;
  636. break;
  637. case SUPDUP_CHARSET_ITS:
  638. supdup->print = print_its;
  639. break;
  640. case SUPDUP_CHARSET_WAITS:
  641. supdup->print = print_waits;
  642. break;
  643. }
  644. /*
  645. * Try to find host.
  646. */
  647. {
  648. char *buf;
  649. addressfamily = conf_get_int(supdup->conf, CONF_addressfamily);
  650. buf = dupprintf("Looking up host \"%s\"%s", host,
  651. (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
  652. (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
  653. "")));
  654. logevent(supdup->logctx, buf);
  655. sfree(buf);
  656. }
  657. addr = name_lookup(host, port, realhost, supdup->conf, addressfamily, NULL, "");
  658. if ((err = sk_addr_error(addr)) != NULL) {
  659. sk_addr_free(addr);
  660. return dupstr(err);
  661. }
  662. if (port < 0)
  663. port = 0137; /* default supdup port */
  664. /*
  665. * Open socket.
  666. */
  667. supdup->s = new_connection(addr, *realhost, port, false, true,
  668. nodelay, keepalive, &supdup->plug, supdup->conf,
  669. &supdup->interactor);
  670. if ((err = sk_socket_error(supdup->s)) != NULL)
  671. return dupstr(err);
  672. supdup->pinger = pinger_new(supdup->conf, &supdup->backend);
  673. /*
  674. * We can send special commands from the start.
  675. */
  676. seat_update_specials_menu(supdup->seat);
  677. /*
  678. * loghost overrides realhost, if specified.
  679. */
  680. loghost = conf_get_str(supdup->conf, CONF_loghost);
  681. if (*loghost) {
  682. char *colon;
  683. sfree(*realhost);
  684. *realhost = dupstr(loghost);
  685. colon = host_strrchr(*realhost, ':');
  686. if (colon)
  687. *colon++ = '\0';
  688. }
  689. /*
  690. * Set up TTYOPTS based on config
  691. */
  692. int ascii_set = conf_get_int(supdup->conf, CONF_supdup_ascii_set);
  693. int more_processing = conf_get_bool(supdup->conf, CONF_supdup_more);
  694. int scrolling = conf_get_bool(supdup->conf, CONF_supdup_scroll);
  695. supdup->ttyopt =
  696. TOERS |
  697. TOMVB |
  698. (ascii_set == SUPDUP_CHARSET_ASCII ? 0 : TOSAI | TOSA1) |
  699. TOMVU |
  700. TOLWR |
  701. TOLID |
  702. TOCID |
  703. TPCBS |
  704. (scrolling ? TOROL : 0) |
  705. (more_processing ? TOMOR : 0) |
  706. TPORS;
  707. supdup->tcmxh = supdup->term_width - 1; // -1 "..one column is used to indicate line continuation."
  708. supdup->tcmxv = supdup->term_height;
  709. /*
  710. * Send our configuration words to the server
  711. */
  712. supdup_send_config(supdup);
  713. /*
  714. * We next expect a connection message followed by %TDNOP from the server
  715. */
  716. supdup->state = CONNECTING;
  717. seat_set_trust_status(supdup->seat, false);
  718. /* Make sure the terminal is in UTF-8 mode. */
  719. c_write(supdup, (unsigned char *)utf8, strlen(utf8));
  720. return NULL;
  721. }
  722. static void supdup_free(Backend *be)
  723. {
  724. Supdup *supdup = container_of(be, Supdup, backend);
  725. if (is_tempseat(supdup->seat))
  726. tempseat_free(supdup->seat);
  727. if (supdup->s)
  728. sk_close(supdup->s);
  729. if (supdup->pinger)
  730. pinger_free(supdup->pinger);
  731. conf_free(supdup->conf);
  732. sfree(supdup->description);
  733. sfree(supdup);
  734. }
  735. /*
  736. * Reconfigure the Supdup backend.
  737. */
  738. static void supdup_reconfig(Backend *be, Conf *conf)
  739. {
  740. /* Nothing to do; SUPDUP cannot be reconfigured while running. */
  741. }
  742. /*
  743. * Called to send data down the Supdup connection.
  744. */
  745. static void supdup_send(Backend *be, const char *buf, size_t len)
  746. {
  747. Supdup *supdup = container_of(be, Supdup, backend);
  748. char c;
  749. int i;
  750. if (supdup->s == NULL)
  751. return;
  752. for (i = 0; i < len; i++) {
  753. if (buf[i] == 034)
  754. supdup->bufsize = sk_write(supdup->s, "\034\034", 2);
  755. else {
  756. c = buf[i] & 0177;
  757. supdup->bufsize = sk_write(supdup->s, &c, 1);
  758. }
  759. }
  760. }
  761. /*
  762. * Called to query the current socket sendability status.
  763. */
  764. static size_t supdup_sendbuffer(Backend *be)
  765. {
  766. Supdup *supdup = container_of(be, Supdup, backend);
  767. return supdup->bufsize;
  768. }
  769. /*
  770. * Called to set the size of the window from Supdup's POV.
  771. */
  772. static void supdup_size(Backend *be, int width, int height)
  773. {
  774. Supdup *supdup = container_of(be, Supdup, backend);
  775. supdup->term_width = width;
  776. supdup->term_height = height;
  777. //
  778. // SUPDUP does not support resizing the terminal after connection
  779. // establishment.
  780. //
  781. }
  782. /*
  783. * Send Telnet special codes.
  784. */
  785. static void supdup_special(Backend *be, SessionSpecialCode code, int arg)
  786. {
  787. }
  788. static const SessionSpecial *supdup_get_specials(Backend *be)
  789. {
  790. return NULL;
  791. }
  792. static bool supdup_connected(Backend *be)
  793. {
  794. Supdup *supdup = container_of(be, Supdup, backend);
  795. return supdup->s != NULL;
  796. }
  797. static bool supdup_sendok(Backend *be)
  798. {
  799. Supdup *supdup = container_of(be, Supdup, backend);
  800. return supdup->socket_connected;
  801. }
  802. static void supdup_unthrottle(Backend *be, size_t backlog)
  803. {
  804. Supdup *supdup = container_of(be, Supdup, backend);
  805. sk_set_frozen(supdup->s, backlog > SUPDUP_MAX_BACKLOG);
  806. }
  807. static bool supdup_ldisc(Backend *be, int option)
  808. {
  809. /* No support for echoing or local editing. */
  810. return false;
  811. }
  812. static void supdup_provide_ldisc(Backend *be, Ldisc *ldisc)
  813. {
  814. Supdup *supdup = container_of(be, Supdup, backend);
  815. supdup->ldisc = ldisc;
  816. }
  817. static int supdup_exitcode(Backend *be)
  818. {
  819. Supdup *supdup = container_of(be, Supdup, backend);
  820. if (supdup->s != NULL)
  821. return -1; /* still connected */
  822. else if (supdup->closed_on_socket_error)
  823. return INT_MAX; /* a socket error counts as an unclean exit */
  824. else
  825. /* Supdup doesn't transmit exit codes back to the client */
  826. return 0;
  827. }
  828. /*
  829. * cfg_info for Supdup does nothing at all.
  830. */
  831. static int supdup_cfg_info(Backend *be)
  832. {
  833. return 0;
  834. }
  835. const BackendVtable supdup_backend = {
  836. .init = supdup_init,
  837. .free = supdup_free,
  838. .reconfig = supdup_reconfig,
  839. .send = supdup_send,
  840. .sendbuffer = supdup_sendbuffer,
  841. .size = supdup_size,
  842. .special = supdup_special,
  843. .get_specials = supdup_get_specials,
  844. .connected = supdup_connected,
  845. .exitcode = supdup_exitcode,
  846. .sendok = supdup_sendok,
  847. .ldisc_option_state = supdup_ldisc,
  848. .provide_ldisc = supdup_provide_ldisc,
  849. .unthrottle = supdup_unthrottle,
  850. .cfg_info = supdup_cfg_info,
  851. .id = "supdup",
  852. .displayname_tc = "SUPDUP",
  853. .displayname_lc = "SUPDUP", /* proper name, so capitalise it anyway */
  854. .protocol = PROT_SUPDUP,
  855. .default_port = 0137,
  856. .flags = BACKEND_RESIZE_FORBIDDEN | BACKEND_NEEDS_TERMINAL,
  857. };