telnetd.ctrlSQ.patch 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. From: "Doug Graham" <dgraham@nortel.com>
  2. Date: 2009-01-22 07:20
  3. Hello,
  4. Busybox's telnetd does not disable local (client-side) flow control
  5. properly. It does not put the pty into packet mode and then notify the
  6. client whenever flow control is disabled by an application running under
  7. its control. The result is that ^S/^Q are not passed through to the
  8. application, which is painful when the application is an emacs variant.
  9. I suppose that support for this might be considered bloat, but the
  10. included patch only adds about 200 bytes of text to x86 busybox and 300
  11. bytes to mipsel busybox. Please consider applying.
  12. =============================
  13. NB: the patch doesn't work as-is because we now have iac_safe_write()
  14. which quotes IACs on output.
  15. =============================
  16. Docs:
  17. The following ioctl(2) calls apply only to pseudo terminals:
  18. TIOCSTOP Stops output to a terminal (e.g. like typing ^S). Takes no parameter.
  19. TIOCSTART Restarts output (stopped by TIOCSTOP or by typing ^S). Takes no parameter.
  20. TIOCPKT Enable/disable packet mode. When applied to the master side of a pseudo terminal, each
  21. subsequent read(2) from the terminal will return data written on the slave part of the pseudo terminal preceded by a
  22. zero byte (symbolically defined as TIOCPKT_DATA), or a single byte reflecting control status information.
  23. In the latter case, the byte is an inclusive-or of zero or more of the bits:
  24. TIOCPKT_FLUSHREAD whenever the read queue for the terminal is flushed.
  25. TIOCPKT_FLUSHWRITE whenever the write queue for the terminal is flushed.
  26. TIOCPKT_STOP whenever output to the terminal is stopped a la ^S.
  27. TIOCPKT_START whenever output to the terminal is restarted.
  28. TIOCPKT_DOSTOP whenever t_stopc is ^S and t_startc is ^Q.
  29. TIOCPKT_NOSTOP whenever the start and stop characters are not ^S/^Q.
  30. While this mode is in use, the presence of control status information to be read from the master side may be detected
  31. by a select(2) for exceptional conditions.
  32. This mode is used by rlogin(1) and rlogind(8) to implement a remote-echoed, locally ^S/^Q flow-controlled remote login
  33. with proper back-flushing of output; it can be used by other similar programs.
  34. TIOCUCNTL Enable/disable a mode that allows a small number of simple user ioctl(2) commands to be passed through
  35. the pseudo-terminal, using a protocol similar to that of TIOCPKT. The TIOCUCNTL and TIOCPKT modes are mutually
  36. exclusive. This mode is enabled from the master side of a pseudo terminal. Each subsequent read(2) from the master side
  37. will return data written on the slave part of the pseudo terminal preceded by a zero byte, or a single byte reflecting a
  38. user control operation on the slave side. A user control command consists of a special ioctl(2) operation with no data;
  39. the command is given as UIOCCMD (n), where n is a number in the range 1-255. The operation value n will be received as
  40. a single byte on the next read(2) from the master side. The ioctl(2) UIOCCMD (0) is a no-op that may be used to probe
  41. for the existence of this facility. As with TIOCPKT mode, command operations may be detected with a select(2) for
  42. exceptional conditions.
  43. --- busybox-1.13.2/networking/telnetd.c 2009/01/21 20:02:39 1.1
  44. +++ busybox-1.13.2/networking/telnetd.c 2009/01/22 00:35:28
  45. @@ -38,6 +38,9 @@
  46. int sockfd_read, sockfd_write, ptyfd;
  47. int shell_pid;
  48. +#ifdef TIOCPKT
  49. + int flowstate;
  50. +#endif
  51. /* two circular buffers */
  52. /*char *buf1, *buf2;*/
  53. /*#define TS_BUF1 ts->buf1*/
  54. @@ -170,6 +173,9 @@
  55. int fd, pid;
  56. char tty_name[GETPTY_BUFSIZE];
  57. struct tsession *ts = xzalloc(sizeof(struct tsession) + BUFSIZE * 2);
  58. +#ifdef TIOCPKT
  59. + int on = 1;
  60. +#endif
  61. /*ts->buf1 = (char *)(ts + 1);*/
  62. /*ts->buf2 = ts->buf1 + BUFSIZE;*/
  63. @@ -180,6 +186,10 @@
  64. maxfd = fd;
  65. ts->ptyfd = fd;
  66. ndelay_on(fd);
  67. +#ifdef TIOCPKT
  68. + ioctl(fd, TIOCPKT, &on);
  69. + ts->flowstate = TIOCPKT_DOSTOP;
  70. +#endif
  71. #if ENABLE_FEATURE_TELNETD_STANDALONE
  72. ts->sockfd_read = sock;
  73. /* SO_KEEPALIVE by popular demand */
  74. @@ -385,6 +395,16 @@
  75. portnbr = 23,
  76. };
  77. #endif
  78. +#ifdef TIOCPKT
  79. + int control;
  80. + static const char lflow_on[] =
  81. + {IAC, SB, TELOPT_LFLOW, LFLOW_ON, IAC, SE};
  82. + static const char lflow_off[] =
  83. + {IAC, SB, TELOPT_LFLOW, LFLOW_OFF, IAC, SE};
  84. +# define RESERVED sizeof(lflow_on)
  85. +#else
  86. +# define RESERVED 0
  87. +#endif
  88. /* Even if !STANDALONE, we accept (and ignore) -i, thus people
  89. * don't need to guess whether it's ok to pass -i to us */
  90. opt = getopt32(argv, "f:l:Ki" IF_FEATURE_TELNETD_STANDALONE("p:b:F"),
  91. @@ -475,7 +495,7 @@
  92. FD_SET(ts->sockfd_read, &rdfdset);
  93. if (ts->size2 > 0) /* can write to socket */
  94. FD_SET(ts->sockfd_write, &wrfdset);
  95. - if (ts->size2 < BUFSIZE) /* can read from pty */
  96. + if (ts->size2 < (BUFSIZE - RESERVED)) /* can read from pty */
  97. FD_SET(ts->ptyfd, &rdfdset);
  98. }
  99. ts = next;
  100. @@ -593,6 +613,52 @@
  101. goto skip4;
  102. goto kill_session;
  103. }
  104. +#ifdef TIOCPKT
  105. + control = TS_BUF2[ts->rdidx2];
  106. + if (--count > 0 && control == TIOCPKT_DATA) {
  107. + /*
  108. + * If we are in packet mode, and we have
  109. + * just read a chunk of actual data from
  110. + * the pty, then there is the TIOCPKT_DATA
  111. + * byte (zero) that we have got to remove
  112. + * somehow. If there were no chars in
  113. + * TS_BUF2 before we did this read, then
  114. + * we can optimize by just advancing wridx2.
  115. + * Otherwise we have to copy the new data down
  116. + * to close the gap (Could use readv() instead).
  117. + */
  118. + if (ts->size2 == 0)
  119. + ts->wridx2++;
  120. + else {
  121. + memmove(TS_BUF2 + ts->rdidx2,
  122. + TS_BUF2 + ts->rdidx2 + 1, count);
  123. + }
  124. + }
  125. +
  126. + /*
  127. + * If the flow control state changed, notify
  128. + * the client. If "control" is not TIOCPKT_DATA,
  129. + * then there are no data bytes to worry about.
  130. + */
  131. + if ((control & (TIOCPKT_DOSTOP|TIOCPKT_NOSTOP)) != 0
  132. + && ts->flowstate != (control & TIOCPKT_DOSTOP)) {
  133. + const char *p = ts->flowstate ? lflow_off : lflow_on;
  134. +
  135. + /*
  136. + * We know we have enough free slots available
  137. + * (see RESERVED) but they are not necessarily
  138. + * contiguous; we may have to wrap.
  139. + */
  140. + for (count = sizeof(lflow_on); count > 0; count--) {
  141. + TS_BUF2[ts->rdidx2++] = *p++;
  142. + if (ts->rdidx2 >= BUFSIZE)
  143. + ts->rdidx2 = 0;
  144. + ts->size2++;
  145. + }
  146. +
  147. + ts->flowstate = control & TIOCPKT_DOSTOP;
  148. + }
  149. +#endif /* TIOCPKT */
  150. ts->size2 += count;
  151. ts->rdidx2 += count;
  152. if (ts->rdidx2 >= BUFSIZE) /* actually == BUFSIZE */
  153. --Doug
  154. _______________________________________________
  155. busybox mailing list
  156. busybox@busybox.net
  157. http://lists.busybox.net/mailman/listinfo/busybox