telnet.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. #if ENABLE_FEATURE_TELNET_WIDTH \
  335. || ENABLE_FEATURE_TELNET_TTYPE \
  336. || ENABLE_FEATURE_TELNET_AUTOLOGIN
  337. static void put_iac4_msb_lsb(unsigned x_y_z_t)
  338. {
  339. put_iac2_msb_lsb(x_y_z_t >> 16);
  340. put_iac2_msb_lsb(x_y_z_t); /* "... & 0xffff" is implicit */
  341. }
  342. #define put_iac4_x_y_z_t(x,y,z,t) put_iac4_msb_lsb(((x)<<24) + ((y)<<16) + ((z)<<8) + (t))
  343. #endif
  344. static void put_iac3_IAC_x_y_merged(unsigned wwdd_and_c)
  345. {
  346. put_iac(IAC);
  347. put_iac2_msb_lsb(wwdd_and_c);
  348. }
  349. #define put_iac3_IAC_x_y(wwdd,c) put_iac3_IAC_x_y_merged(((wwdd)<<8) + (c))
  350. #if ENABLE_FEATURE_TELNET_TTYPE
  351. static void put_iac_subopt(byte c, char *str)
  352. {
  353. put_iac4_x_y_z_t(IAC, SB, c, 0);
  354. while (*str)
  355. put_iac(*str++);
  356. put_iac2_x_y(IAC, SE);
  357. }
  358. #endif
  359. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  360. static void put_iac_subopt_autologin(void)
  361. {
  362. const char *p;
  363. put_iac4_x_y_z_t(IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_IS);
  364. put_iac4_x_y_z_t(NEW_ENV_VAR, 'U', 'S', 'E'); /* "USER" */
  365. put_iac2_x_y('R', NEW_ENV_VALUE);
  366. p = G.autologin;
  367. while (*p)
  368. put_iac(*p++);
  369. put_iac2_x_y(IAC, SE);
  370. }
  371. #endif
  372. #if ENABLE_FEATURE_TELNET_WIDTH
  373. static void put_iac_naws(byte c, int x, int y)
  374. {
  375. put_iac3_IAC_x_y(SB, c);
  376. put_iac4_msb_lsb((x << 16) + y);
  377. put_iac2_x_y(IAC, SE);
  378. }
  379. #endif
  380. static void setConMode(void)
  381. {
  382. if (G.telflags & UF_ECHO) {
  383. if (G.charmode == CHM_TRY) {
  384. G.charmode = CHM_ON;
  385. printf("\r\nEntering %s mode"
  386. "\r\nEscape character is '^%c'.\r\n", "character", ']');
  387. rawmode();
  388. }
  389. } else {
  390. if (G.charmode != CHM_OFF) {
  391. G.charmode = CHM_OFF;
  392. printf("\r\nEntering %s mode"
  393. "\r\nEscape character is '^%c'.\r\n", "line", 'C');
  394. cookmode();
  395. }
  396. }
  397. }
  398. static void will_charmode(void)
  399. {
  400. G.charmode = CHM_TRY;
  401. G.telflags |= (UF_ECHO | UF_SGA);
  402. setConMode();
  403. put_iac3_IAC_x_y(DO, TELOPT_ECHO);
  404. put_iac3_IAC_x_y(DO, TELOPT_SGA);
  405. iac_flush();
  406. }
  407. static void do_linemode(void)
  408. {
  409. G.charmode = CHM_TRY;
  410. G.telflags &= ~(UF_ECHO | UF_SGA);
  411. setConMode();
  412. put_iac3_IAC_x_y(DONT, TELOPT_ECHO);
  413. put_iac3_IAC_x_y(DONT, TELOPT_SGA);
  414. iac_flush();
  415. }
  416. static void to_notsup(char c)
  417. {
  418. if (G.telwish == WILL)
  419. put_iac3_IAC_x_y(DONT, c);
  420. else if (G.telwish == DO)
  421. put_iac3_IAC_x_y(WONT, c);
  422. }
  423. static void to_echo(void)
  424. {
  425. /* if server requests ECHO, don't agree */
  426. if (G.telwish == DO) {
  427. put_iac3_IAC_x_y(WONT, TELOPT_ECHO);
  428. return;
  429. }
  430. if (G.telwish == DONT)
  431. return;
  432. if (G.telflags & UF_ECHO) {
  433. if (G.telwish == WILL)
  434. return;
  435. } else if (G.telwish == WONT)
  436. return;
  437. if (G.charmode != CHM_OFF)
  438. G.telflags ^= UF_ECHO;
  439. if (G.telflags & UF_ECHO)
  440. put_iac3_IAC_x_y(DO, TELOPT_ECHO);
  441. else
  442. put_iac3_IAC_x_y(DONT, TELOPT_ECHO);
  443. setConMode();
  444. full_write1_str("\r\n"); /* sudden modec */
  445. }
  446. static void to_sga(void)
  447. {
  448. /* daemon always sends will/wont, client do/dont */
  449. if (G.telflags & UF_SGA) {
  450. if (G.telwish == WILL)
  451. return;
  452. } else if (G.telwish == WONT)
  453. return;
  454. G.telflags ^= UF_SGA; /* toggle */
  455. if (G.telflags & UF_SGA)
  456. put_iac3_IAC_x_y(DO, TELOPT_SGA);
  457. else
  458. put_iac3_IAC_x_y(DONT, TELOPT_SGA);
  459. }
  460. #if ENABLE_FEATURE_TELNET_TTYPE
  461. static void to_ttype(void)
  462. {
  463. /* Tell server we will (or won't) do TTYPE */
  464. if (G.ttype)
  465. put_iac3_IAC_x_y(WILL, TELOPT_TTYPE);
  466. else
  467. put_iac3_IAC_x_y(WONT, TELOPT_TTYPE);
  468. }
  469. #endif
  470. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  471. static void to_new_environ(void)
  472. {
  473. /* Tell server we will (or will not) do AUTOLOGIN */
  474. if (G.autologin)
  475. put_iac3_IAC_x_y(WILL, TELOPT_NEW_ENVIRON);
  476. else
  477. put_iac3_IAC_x_y(WONT, TELOPT_NEW_ENVIRON);
  478. }
  479. #endif
  480. #if ENABLE_FEATURE_TELNET_WIDTH
  481. static void to_naws(void)
  482. {
  483. /* Tell server we will do NAWS */
  484. put_iac3_IAC_x_y(WILL, TELOPT_NAWS);
  485. }
  486. #endif
  487. static void telopt(byte c)
  488. {
  489. switch (c) {
  490. case TELOPT_ECHO:
  491. to_echo(); break;
  492. case TELOPT_SGA:
  493. to_sga(); break;
  494. #if ENABLE_FEATURE_TELNET_TTYPE
  495. case TELOPT_TTYPE:
  496. to_ttype(); break;
  497. #endif
  498. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  499. case TELOPT_NEW_ENVIRON:
  500. to_new_environ(); break;
  501. #endif
  502. #if ENABLE_FEATURE_TELNET_WIDTH
  503. case TELOPT_NAWS:
  504. to_naws();
  505. put_iac_naws(c, G.win_width, G.win_height);
  506. break;
  507. #endif
  508. default:
  509. to_notsup(c);
  510. break;
  511. }
  512. }
  513. /* subnegotiation -- ignore all (except TTYPE,NAWS) */
  514. static void subneg(byte c)
  515. {
  516. switch (G.telstate) {
  517. case TS_SUB1:
  518. if (c == IAC)
  519. G.telstate = TS_SUB2;
  520. #if ENABLE_FEATURE_TELNET_TTYPE
  521. else
  522. if (c == TELOPT_TTYPE && G.ttype)
  523. put_iac_subopt(TELOPT_TTYPE, G.ttype);
  524. #endif
  525. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  526. else
  527. if (c == TELOPT_NEW_ENVIRON && G.autologin)
  528. put_iac_subopt_autologin();
  529. #endif
  530. break;
  531. case TS_SUB2:
  532. if (c == SE) {
  533. G.telstate = TS_COPY;
  534. return;
  535. }
  536. G.telstate = TS_SUB1;
  537. break;
  538. }
  539. }
  540. static void rawmode(void)
  541. {
  542. if (G.do_termios)
  543. tcsetattr(0, TCSADRAIN, &G.termios_raw);
  544. }
  545. static void cookmode(void)
  546. {
  547. if (G.do_termios)
  548. tcsetattr(0, TCSADRAIN, &G.termios_def);
  549. }
  550. int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  551. int telnet_main(int argc UNUSED_PARAM, char **argv)
  552. {
  553. char *host;
  554. int port;
  555. int len;
  556. struct pollfd ufds[2];
  557. INIT_G();
  558. #if ENABLE_FEATURE_TELNET_TTYPE
  559. G.ttype = getenv("TERM");
  560. #endif
  561. if (tcgetattr(0, &G.termios_def) >= 0) {
  562. G.do_termios = 1;
  563. G.termios_raw = G.termios_def;
  564. cfmakeraw(&G.termios_raw);
  565. }
  566. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  567. if (1 == getopt32(argv, "al:", &G.autologin)) {
  568. /* Only -a without -l USER picks $USER from envvar */
  569. G.autologin = getenv("USER");
  570. }
  571. argv += optind;
  572. #else
  573. argv++;
  574. #endif
  575. if (!*argv)
  576. bb_show_usage();
  577. host = *argv++;
  578. port = *argv ? bb_lookup_port(*argv++, "tcp", 23)
  579. : bb_lookup_std_port("telnet", "tcp", 23);
  580. if (*argv) /* extra params?? */
  581. bb_show_usage();
  582. xmove_fd(create_and_connect_stream_or_die(host, port), netfd);
  583. printf("Connected to %s\n", host);
  584. setsockopt_keepalive(netfd);
  585. #if ENABLE_FEATURE_TELNET_WIDTH
  586. get_terminal_width_height(0, &G.win_width, &G.win_height);
  587. //TODO: support dynamic resize?
  588. #endif
  589. signal(SIGINT, record_signo);
  590. ufds[0].fd = STDIN_FILENO;
  591. ufds[0].events = POLLIN;
  592. ufds[1].fd = netfd;
  593. ufds[1].events = POLLIN;
  594. while (1) {
  595. if (poll(ufds, 2, -1) < 0) {
  596. /* error, ignore and/or log something, bay go to loop */
  597. if (bb_got_signal)
  598. con_escape();
  599. else
  600. sleep1();
  601. continue;
  602. }
  603. // FIXME: reads can block. Need full bidirectional buffering.
  604. if (ufds[0].revents) {
  605. len = safe_read(STDIN_FILENO, G.buf, DATABUFSIZE);
  606. if (len <= 0)
  607. doexit(EXIT_SUCCESS);
  608. handle_net_output(len);
  609. }
  610. if (ufds[1].revents) {
  611. len = safe_read(netfd, G.buf, DATABUFSIZE);
  612. if (len <= 0) {
  613. full_write1_str("Connection closed by foreign host\r\n");
  614. doexit(EXIT_FAILURE);
  615. }
  616. handle_net_input(len);
  617. }
  618. } /* while (1) */
  619. }