telnet.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * telnet implementation for busybox
  4. *
  5. * Author: Tomi Ollila <too@iki.fi>
  6. * Copyright (C) 1994-2000 by Tomi Ollila
  7. *
  8. * Created: Thu Apr 7 13:29:41 1994 too
  9. * Last modified: Fri Jun 9 14:34:24 2000 too
  10. *
  11. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  12. *
  13. * HISTORY
  14. * Revision 3.1 1994/04/17 11:31:54 too
  15. * initial revision
  16. * Modified 2000/06/13 for inclusion into BusyBox by Erik Andersen <andersen@codepoet.org>
  17. * Modified 2001/05/07 to add ability to pass TTYPE to remote host by Jim McQuillan
  18. * <jam@ltsp.org>
  19. * Modified 2004/02/11 to add ability to pass the USER variable to remote host
  20. * by Fernando Silveira <swrh@gmx.net>
  21. */
  22. //config:config TELNET
  23. //config: bool "telnet (8.8 kb)"
  24. //config: default y
  25. //config: help
  26. //config: Telnet is an interface to the TELNET protocol, but is also commonly
  27. //config: used to test other simple protocols.
  28. //config:
  29. //config:config FEATURE_TELNET_TTYPE
  30. //config: bool "Pass TERM type to remote host"
  31. //config: default y
  32. //config: depends on TELNET
  33. //config: help
  34. //config: Setting this option will forward the TERM environment variable to the
  35. //config: remote host you are connecting to. This is useful to make sure that
  36. //config: things like ANSI colors and other control sequences behave.
  37. //config:
  38. //config:config FEATURE_TELNET_AUTOLOGIN
  39. //config: bool "Pass USER type to remote host"
  40. //config: default y
  41. //config: depends on TELNET
  42. //config: help
  43. //config: Setting this option will forward the USER environment variable to the
  44. //config: remote host you are connecting to. This is useful when you need to
  45. //config: log into a machine without telling the username (autologin). This
  46. //config: option enables '-a' and '-l USER' options.
  47. //config:
  48. //config:config FEATURE_TELNET_WIDTH
  49. //config: bool "Enable window size autodetection"
  50. //config: default y
  51. //config: depends on TELNET
  52. //applet:IF_TELNET(APPLET(telnet, BB_DIR_USR_BIN, BB_SUID_DROP))
  53. //kbuild:lib-$(CONFIG_TELNET) += telnet.o
  54. //usage:#if ENABLE_FEATURE_TELNET_AUTOLOGIN
  55. //usage:#define telnet_trivial_usage
  56. //usage: "[-a] [-l USER] HOST [PORT]"
  57. //usage:#define telnet_full_usage "\n\n"
  58. //usage: "Connect to telnet server\n"
  59. //usage: "\n -a Automatic login with $USER variable"
  60. //usage: "\n -l USER Automatic login as USER"
  61. //usage:
  62. //usage:#else
  63. //usage:#define telnet_trivial_usage
  64. //usage: "HOST [PORT]"
  65. //usage:#define telnet_full_usage "\n\n"
  66. //usage: "Connect to telnet server"
  67. //usage:#endif
  68. #include <arpa/telnet.h>
  69. #include <netinet/in.h>
  70. #include "libbb.h"
  71. #include "common_bufsiz.h"
  72. #ifdef __BIONIC__
  73. /* should be in arpa/telnet.h */
  74. # define IAC 255 /* interpret as command: */
  75. # define DONT 254 /* you are not to use option */
  76. # define DO 253 /* please, you use option */
  77. # define WONT 252 /* I won't use option */
  78. # define WILL 251 /* I will use option */
  79. # define SB 250 /* interpret as subnegotiation */
  80. # define SE 240 /* end sub negotiation */
  81. # define TELOPT_ECHO 1 /* echo */
  82. # define TELOPT_SGA 3 /* suppress go ahead */
  83. # define TELOPT_TTYPE 24 /* terminal type */
  84. # define TELOPT_NAWS 31 /* window size */
  85. #endif
  86. enum {
  87. DATABUFSIZE = 128,
  88. IACBUFSIZE = 128,
  89. CHM_TRY = 0,
  90. CHM_ON = 1,
  91. CHM_OFF = 2,
  92. UF_ECHO = 0x01,
  93. UF_SGA = 0x02,
  94. TS_NORMAL = 0,
  95. TS_COPY = 1,
  96. TS_IAC = 2,
  97. TS_OPT = 3,
  98. TS_SUB1 = 4,
  99. TS_SUB2 = 5,
  100. TS_CR = 6,
  101. };
  102. typedef unsigned char byte;
  103. enum { netfd = 3 };
  104. struct globals {
  105. int iaclen; /* could even use byte, but it's a loss on x86 */
  106. byte telstate; /* telnet negotiation state from network input */
  107. byte telwish; /* DO, DONT, WILL, WONT */
  108. byte charmode;
  109. byte telflags;
  110. byte do_termios;
  111. #if ENABLE_FEATURE_TELNET_TTYPE
  112. char *ttype;
  113. #endif
  114. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  115. const char *autologin;
  116. #endif
  117. #if ENABLE_FEATURE_TELNET_WIDTH
  118. unsigned win_width, win_height;
  119. #endif
  120. /* same buffer used both for network and console read/write */
  121. char buf[DATABUFSIZE];
  122. /* buffer to handle telnet negotiations */
  123. char iacbuf[IACBUFSIZE];
  124. struct termios termios_def;
  125. struct termios termios_raw;
  126. } FIX_ALIASING;
  127. #define G (*(struct globals*)bb_common_bufsiz1)
  128. #define INIT_G() do { \
  129. setup_common_bufsiz(); \
  130. BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
  131. } while (0)
  132. static void rawmode(void);
  133. static void cookmode(void);
  134. static void do_linemode(void);
  135. static void will_charmode(void);
  136. static void telopt(byte c);
  137. static void subneg(byte c);
  138. static void iac_flush(void)
  139. {
  140. if (G.iaclen != 0) {
  141. full_write(netfd, G.iacbuf, G.iaclen);
  142. G.iaclen = 0;
  143. }
  144. }
  145. static void doexit(int ev) NORETURN;
  146. static void doexit(int ev)
  147. {
  148. cookmode();
  149. exit(ev);
  150. }
  151. static void con_escape(void)
  152. {
  153. char b;
  154. if (bb_got_signal) /* came from line mode... go raw */
  155. rawmode();
  156. full_write1_str("\r\nConsole escape. Commands are:\r\n\n"
  157. " l go to line mode\r\n"
  158. " c go to character mode\r\n"
  159. " z suspend telnet\r\n"
  160. " e exit telnet\r\n");
  161. if (read(STDIN_FILENO, &b, 1) <= 0)
  162. doexit(EXIT_FAILURE);
  163. switch (b) {
  164. case 'l':
  165. if (!bb_got_signal) {
  166. do_linemode();
  167. goto ret;
  168. }
  169. break;
  170. case 'c':
  171. if (bb_got_signal) {
  172. will_charmode();
  173. goto ret;
  174. }
  175. break;
  176. case 'z':
  177. cookmode();
  178. kill(0, SIGTSTP);
  179. rawmode();
  180. break;
  181. case 'e':
  182. doexit(EXIT_SUCCESS);
  183. }
  184. full_write1_str("continuing...\r\n");
  185. if (bb_got_signal)
  186. cookmode();
  187. ret:
  188. bb_got_signal = 0;
  189. }
  190. static void handle_net_output(int len)
  191. {
  192. byte outbuf[2 * DATABUFSIZE];
  193. byte *dst = outbuf;
  194. byte *src = (byte*)G.buf;
  195. byte *end = src + len;
  196. while (src < end) {
  197. byte c = *src++;
  198. if (c == 0x1d) {
  199. con_escape();
  200. return;
  201. }
  202. *dst = c;
  203. if (c == IAC)
  204. *++dst = c; /* IAC -> IAC IAC */
  205. else
  206. if (c == '\r' || c == '\n') {
  207. /* Enter key sends '\r' in raw mode and '\n' in cooked one.
  208. *
  209. * See RFC 1123 3.3.1 Telnet End-of-Line Convention.
  210. * Using CR LF instead of other allowed possibilities
  211. * like CR NUL - easier to talk to HTTP/SMTP servers.
  212. */
  213. *dst = '\r'; /* Enter -> CR LF */
  214. *++dst = '\n';
  215. }
  216. #if 0
  217. /* putty's "special commands" mode does this: */
  218. /* Korenix 3005 switch needs at least the backspace tweak */
  219. if (c == 0x08 || c == 0x7f) { /* ctrl+h || backspace */
  220. *dst = IAC;
  221. *++dst = EC;
  222. }
  223. if (c == 0x03) { /* ctrl+c */
  224. *dst = IAC;
  225. *++dst = IP;
  226. }
  227. #endif
  228. dst++;
  229. }
  230. if (dst - outbuf != 0)
  231. full_write(netfd, outbuf, dst - outbuf);
  232. }
  233. static void handle_net_input(int len)
  234. {
  235. byte c;
  236. int i;
  237. int cstart = 0;
  238. i = 0;
  239. //bb_error_msg("[%u,'%.*s']", G.telstate, len, G.buf);
  240. if (G.telstate == TS_NORMAL) { /* most typical state */
  241. while (i < len) {
  242. c = G.buf[i];
  243. i++;
  244. if (c == IAC) /* unlikely */
  245. goto got_IAC;
  246. if (c != '\r') /* likely */
  247. continue;
  248. G.telstate = TS_CR;
  249. cstart = i;
  250. goto got_special;
  251. }
  252. full_write(STDOUT_FILENO, G.buf, len);
  253. return;
  254. got_IAC:
  255. G.telstate = TS_IAC;
  256. cstart = i - 1;
  257. got_special: ;
  258. }
  259. for (; i < len; i++) {
  260. c = G.buf[i];
  261. switch (G.telstate) {
  262. case TS_CR:
  263. /* Prev char was CR. If cur one is NUL, ignore it.
  264. * See RFC 1123 section 3.3.1 for discussion of telnet EOL handling.
  265. */
  266. G.telstate = TS_COPY;
  267. if (c == '\0')
  268. break;
  269. /* else: fall through - need to handle CR IAC ... properly */
  270. case TS_COPY: /* Prev char was ordinary */
  271. /* Similar to NORMAL, but in TS_COPY we need to copy bytes */
  272. if (c == IAC)
  273. G.telstate = TS_IAC;
  274. else {
  275. G.buf[cstart++] = c;
  276. if (c == '\r')
  277. G.telstate = TS_CR;
  278. }
  279. break;
  280. case TS_IAC: /* Prev char was IAC */
  281. switch (c) {
  282. case IAC: /* IAC IAC -> one IAC */
  283. G.buf[cstart++] = c;
  284. G.telstate = TS_COPY;
  285. break;
  286. case SB:
  287. G.telstate = TS_SUB1;
  288. break;
  289. case DO:
  290. case DONT:
  291. case WILL:
  292. case WONT:
  293. G.telwish = c;
  294. G.telstate = TS_OPT;
  295. break;
  296. /* DATA MARK must be added later */
  297. default:
  298. G.telstate = TS_COPY;
  299. }
  300. break;
  301. case TS_OPT: /* Prev chars were IAC WILL/WONT/DO/DONT */
  302. telopt(c);
  303. G.telstate = TS_COPY;
  304. break;
  305. case TS_SUB1: /* Subnegotiation */
  306. case TS_SUB2: /* Subnegotiation */
  307. subneg(c); /* can change G.telstate */
  308. break;
  309. }
  310. }
  311. /* We had some IACs, or CR */
  312. iac_flush();
  313. if (G.telstate == TS_COPY) /* we aren't in the middle of IAC */
  314. G.telstate = TS_NORMAL;
  315. if (cstart != 0)
  316. full_write(STDOUT_FILENO, G.buf, cstart);
  317. }
  318. static void put_iac(int c)
  319. {
  320. int iaclen = G.iaclen;
  321. if (iaclen >= IACBUFSIZE) {
  322. iac_flush();
  323. iaclen = 0;
  324. }
  325. G.iacbuf[iaclen] = c; /* "... & 0xff" is implicit */
  326. G.iaclen = iaclen + 1;
  327. }
  328. static void put_iac2_msb_lsb(unsigned x_y)
  329. {
  330. put_iac(x_y >> 8); /* "... & 0xff" is implicit */
  331. put_iac(x_y); /* "... & 0xff" is implicit */
  332. }
  333. #define put_iac2_x_y(x,y) put_iac2_msb_lsb(((x)<<8) + (y))
  334. static void put_iac4_msb_lsb(unsigned x_y_z_t)
  335. {
  336. put_iac2_msb_lsb(x_y_z_t >> 16);
  337. put_iac2_msb_lsb(x_y_z_t); /* "... & 0xffff" is implicit */
  338. }
  339. #define put_iac4_x_y_z_t(x,y,z,t) put_iac4_msb_lsb(((x)<<24) + ((y)<<16) + ((z)<<8) + (t))
  340. static void put_iac3_IAC_x_y_merged(unsigned wwdd_and_c)
  341. {
  342. put_iac(IAC);
  343. put_iac2_msb_lsb(wwdd_and_c);
  344. }
  345. #define put_iac3_IAC_x_y(wwdd,c) put_iac3_IAC_x_y_merged(((wwdd)<<8) + (c))
  346. #if ENABLE_FEATURE_TELNET_TTYPE
  347. static void put_iac_subopt(byte c, char *str)
  348. {
  349. put_iac4_x_y_z_t(IAC, SB, c, 0);
  350. while (*str)
  351. put_iac(*str++);
  352. put_iac2_x_y(IAC, SE);
  353. }
  354. #endif
  355. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  356. static void put_iac_subopt_autologin(void)
  357. {
  358. const char *p;
  359. put_iac4_x_y_z_t(IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_IS);
  360. put_iac4_x_y_z_t(NEW_ENV_VAR, 'U', 'S', 'E'); /* "USER" */
  361. put_iac2_x_y('R', NEW_ENV_VALUE);
  362. p = G.autologin;
  363. while (*p)
  364. put_iac(*p++);
  365. put_iac2_x_y(IAC, SE);
  366. }
  367. #endif
  368. #if ENABLE_FEATURE_TELNET_WIDTH
  369. static void put_iac_naws(byte c, int x, int y)
  370. {
  371. put_iac3_IAC_x_y(SB, c);
  372. put_iac4_msb_lsb((x << 16) + y);
  373. put_iac2_x_y(IAC, SE);
  374. }
  375. #endif
  376. static void setConMode(void)
  377. {
  378. if (G.telflags & UF_ECHO) {
  379. if (G.charmode == CHM_TRY) {
  380. G.charmode = CHM_ON;
  381. printf("\r\nEntering %s mode"
  382. "\r\nEscape character is '^%c'.\r\n", "character", ']');
  383. rawmode();
  384. }
  385. } else {
  386. if (G.charmode != CHM_OFF) {
  387. G.charmode = CHM_OFF;
  388. printf("\r\nEntering %s mode"
  389. "\r\nEscape character is '^%c'.\r\n", "line", 'C');
  390. cookmode();
  391. }
  392. }
  393. }
  394. static void will_charmode(void)
  395. {
  396. G.charmode = CHM_TRY;
  397. G.telflags |= (UF_ECHO | UF_SGA);
  398. setConMode();
  399. put_iac3_IAC_x_y(DO, TELOPT_ECHO);
  400. put_iac3_IAC_x_y(DO, TELOPT_SGA);
  401. iac_flush();
  402. }
  403. static void do_linemode(void)
  404. {
  405. G.charmode = CHM_TRY;
  406. G.telflags &= ~(UF_ECHO | UF_SGA);
  407. setConMode();
  408. put_iac3_IAC_x_y(DONT, TELOPT_ECHO);
  409. put_iac3_IAC_x_y(DONT, TELOPT_SGA);
  410. iac_flush();
  411. }
  412. static void to_notsup(char c)
  413. {
  414. if (G.telwish == WILL)
  415. put_iac3_IAC_x_y(DONT, c);
  416. else if (G.telwish == DO)
  417. put_iac3_IAC_x_y(WONT, c);
  418. }
  419. static void to_echo(void)
  420. {
  421. /* if server requests ECHO, don't agree */
  422. if (G.telwish == DO) {
  423. put_iac3_IAC_x_y(WONT, TELOPT_ECHO);
  424. return;
  425. }
  426. if (G.telwish == DONT)
  427. return;
  428. if (G.telflags & UF_ECHO) {
  429. if (G.telwish == WILL)
  430. return;
  431. } else if (G.telwish == WONT)
  432. return;
  433. if (G.charmode != CHM_OFF)
  434. G.telflags ^= UF_ECHO;
  435. if (G.telflags & UF_ECHO)
  436. put_iac3_IAC_x_y(DO, TELOPT_ECHO);
  437. else
  438. put_iac3_IAC_x_y(DONT, TELOPT_ECHO);
  439. setConMode();
  440. full_write1_str("\r\n"); /* sudden modec */
  441. }
  442. static void to_sga(void)
  443. {
  444. /* daemon always sends will/wont, client do/dont */
  445. if (G.telflags & UF_SGA) {
  446. if (G.telwish == WILL)
  447. return;
  448. } else if (G.telwish == WONT)
  449. return;
  450. G.telflags ^= UF_SGA; /* toggle */
  451. if (G.telflags & UF_SGA)
  452. put_iac3_IAC_x_y(DO, TELOPT_SGA);
  453. else
  454. put_iac3_IAC_x_y(DONT, TELOPT_SGA);
  455. }
  456. #if ENABLE_FEATURE_TELNET_TTYPE
  457. static void to_ttype(void)
  458. {
  459. /* Tell server we will (or won't) do TTYPE */
  460. if (G.ttype)
  461. put_iac3_IAC_x_y(WILL, TELOPT_TTYPE);
  462. else
  463. put_iac3_IAC_x_y(WONT, TELOPT_TTYPE);
  464. }
  465. #endif
  466. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  467. static void to_new_environ(void)
  468. {
  469. /* Tell server we will (or will not) do AUTOLOGIN */
  470. if (G.autologin)
  471. put_iac3_IAC_x_y(WILL, TELOPT_NEW_ENVIRON);
  472. else
  473. put_iac3_IAC_x_y(WONT, TELOPT_NEW_ENVIRON);
  474. }
  475. #endif
  476. #if ENABLE_FEATURE_TELNET_WIDTH
  477. static void to_naws(void)
  478. {
  479. /* Tell server we will do NAWS */
  480. put_iac3_IAC_x_y(WILL, TELOPT_NAWS);
  481. }
  482. #endif
  483. static void telopt(byte c)
  484. {
  485. switch (c) {
  486. case TELOPT_ECHO:
  487. to_echo(); break;
  488. case TELOPT_SGA:
  489. to_sga(); break;
  490. #if ENABLE_FEATURE_TELNET_TTYPE
  491. case TELOPT_TTYPE:
  492. to_ttype(); break;
  493. #endif
  494. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  495. case TELOPT_NEW_ENVIRON:
  496. to_new_environ(); break;
  497. #endif
  498. #if ENABLE_FEATURE_TELNET_WIDTH
  499. case TELOPT_NAWS:
  500. to_naws();
  501. put_iac_naws(c, G.win_width, G.win_height);
  502. break;
  503. #endif
  504. default:
  505. to_notsup(c);
  506. break;
  507. }
  508. }
  509. /* subnegotiation -- ignore all (except TTYPE,NAWS) */
  510. static void subneg(byte c)
  511. {
  512. switch (G.telstate) {
  513. case TS_SUB1:
  514. if (c == IAC)
  515. G.telstate = TS_SUB2;
  516. #if ENABLE_FEATURE_TELNET_TTYPE
  517. else
  518. if (c == TELOPT_TTYPE && G.ttype)
  519. put_iac_subopt(TELOPT_TTYPE, G.ttype);
  520. #endif
  521. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  522. else
  523. if (c == TELOPT_NEW_ENVIRON && G.autologin)
  524. put_iac_subopt_autologin();
  525. #endif
  526. break;
  527. case TS_SUB2:
  528. if (c == SE) {
  529. G.telstate = TS_COPY;
  530. return;
  531. }
  532. G.telstate = TS_SUB1;
  533. break;
  534. }
  535. }
  536. static void rawmode(void)
  537. {
  538. if (G.do_termios)
  539. tcsetattr(0, TCSADRAIN, &G.termios_raw);
  540. }
  541. static void cookmode(void)
  542. {
  543. if (G.do_termios)
  544. tcsetattr(0, TCSADRAIN, &G.termios_def);
  545. }
  546. int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  547. int telnet_main(int argc UNUSED_PARAM, char **argv)
  548. {
  549. char *host;
  550. int port;
  551. int len;
  552. struct pollfd ufds[2];
  553. INIT_G();
  554. #if ENABLE_FEATURE_TELNET_TTYPE
  555. G.ttype = getenv("TERM");
  556. #endif
  557. if (tcgetattr(0, &G.termios_def) >= 0) {
  558. G.do_termios = 1;
  559. G.termios_raw = G.termios_def;
  560. cfmakeraw(&G.termios_raw);
  561. }
  562. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  563. if (1 == getopt32(argv, "al:", &G.autologin)) {
  564. /* Only -a without -l USER picks $USER from envvar */
  565. G.autologin = getenv("USER");
  566. }
  567. argv += optind;
  568. #else
  569. argv++;
  570. #endif
  571. if (!*argv)
  572. bb_show_usage();
  573. host = *argv++;
  574. port = *argv ? bb_lookup_port(*argv++, "tcp", 23)
  575. : bb_lookup_std_port("telnet", "tcp", 23);
  576. if (*argv) /* extra params?? */
  577. bb_show_usage();
  578. xmove_fd(create_and_connect_stream_or_die(host, port), netfd);
  579. printf("Connected to %s\n", host);
  580. setsockopt_keepalive(netfd);
  581. #if ENABLE_FEATURE_TELNET_WIDTH
  582. get_terminal_width_height(0, &G.win_width, &G.win_height);
  583. //TODO: support dynamic resize?
  584. #endif
  585. signal(SIGINT, record_signo);
  586. ufds[0].fd = STDIN_FILENO;
  587. ufds[0].events = POLLIN;
  588. ufds[1].fd = netfd;
  589. ufds[1].events = POLLIN;
  590. while (1) {
  591. if (poll(ufds, 2, -1) < 0) {
  592. /* error, ignore and/or log something, bay go to loop */
  593. if (bb_got_signal)
  594. con_escape();
  595. else
  596. sleep(1);
  597. continue;
  598. }
  599. // FIXME: reads can block. Need full bidirectional buffering.
  600. if (ufds[0].revents) {
  601. len = safe_read(STDIN_FILENO, G.buf, DATABUFSIZE);
  602. if (len <= 0)
  603. doexit(EXIT_SUCCESS);
  604. handle_net_output(len);
  605. }
  606. if (ufds[1].revents) {
  607. len = safe_read(netfd, G.buf, DATABUFSIZE);
  608. if (len <= 0) {
  609. full_write1_str("Connection closed by foreign host\r\n");
  610. doexit(EXIT_FAILURE);
  611. }
  612. handle_net_input(len);
  613. }
  614. } /* while (1) */
  615. }