telnetd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Simple telnet server
  4. * Bjorn Wesen, Axis Communications AB (bjornw@axis.com)
  5. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  7. *
  8. * ---------------------------------------------------------------------------
  9. * (C) Copyright 2000, Axis Communications AB, LUND, SWEDEN
  10. ****************************************************************************
  11. *
  12. * The telnetd manpage says it all:
  13. *
  14. * Telnetd operates by allocating a pseudo-terminal device (see pty(4)) for
  15. * a client, then creating a login process which has the slave side of the
  16. * pseudo-terminal as stdin, stdout, and stderr. Telnetd manipulates the
  17. * master side of the pseudo-terminal, implementing the telnet protocol and
  18. * passing characters between the remote client and the login process.
  19. *
  20. * Vladimir Oleynik <dzo@simtreas.ru> 2001
  21. * Set process group corrections, initial busybox port
  22. */
  23. #define DEBUG 0
  24. #include "libbb.h"
  25. #include <syslog.h>
  26. #if DEBUG
  27. #define TELCMDS
  28. #define TELOPTS
  29. #endif
  30. #include <arpa/telnet.h>
  31. /* Structure that describes a session */
  32. struct tsession {
  33. struct tsession *next;
  34. int sockfd_read, sockfd_write, ptyfd;
  35. int shell_pid;
  36. /* two circular buffers */
  37. /*char *buf1, *buf2;*/
  38. /*#define TS_BUF1 ts->buf1*/
  39. /*#define TS_BUF2 TS_BUF2*/
  40. #define TS_BUF1 ((unsigned char*)(ts + 1))
  41. #define TS_BUF2 (((unsigned char*)(ts + 1)) + BUFSIZE)
  42. int rdidx1, wridx1, size1;
  43. int rdidx2, wridx2, size2;
  44. };
  45. /* Two buffers are directly after tsession in malloced memory.
  46. * Make whole thing fit in 4k */
  47. enum { BUFSIZE = (4 * 1024 - sizeof(struct tsession)) / 2 };
  48. /* Globals */
  49. static int maxfd;
  50. static struct tsession *sessions;
  51. static const char *loginpath = "/bin/login";
  52. static const char *issuefile = "/etc/issue.net";
  53. /*
  54. Remove all IAC's from buf1 (received IACs are ignored and must be removed
  55. so as to not be interpreted by the terminal). Make an uninterrupted
  56. string of characters fit for the terminal. Do this by packing
  57. all characters meant for the terminal sequentially towards the end of buf.
  58. Return a pointer to the beginning of the characters meant for the terminal.
  59. and make *num_totty the number of characters that should be sent to
  60. the terminal.
  61. Note - If an IAC (3 byte quantity) starts before (bf + len) but extends
  62. past (bf + len) then that IAC will be left unprocessed and *processed
  63. will be less than len.
  64. FIXME - if we mean to send 0xFF to the terminal then it will be escaped,
  65. what is the escape character? We aren't handling that situation here.
  66. CR-LF ->'s CR mapping is also done here, for convenience.
  67. NB: may fail to remove iacs which wrap around buffer!
  68. */
  69. static unsigned char *
  70. remove_iacs(struct tsession *ts, int *pnum_totty)
  71. {
  72. unsigned char *ptr0 = TS_BUF1 + ts->wridx1;
  73. unsigned char *ptr = ptr0;
  74. unsigned char *totty = ptr;
  75. unsigned char *end = ptr + MIN(BUFSIZE - ts->wridx1, ts->size1);
  76. int num_totty;
  77. while (ptr < end) {
  78. if (*ptr != IAC) {
  79. char c = *ptr;
  80. *totty++ = c;
  81. ptr++;
  82. /* We now map \r\n ==> \r for pragmatic reasons.
  83. * Many client implementations send \r\n when
  84. * the user hits the CarriageReturn key.
  85. */
  86. if (c == '\r' && ptr < end && (*ptr == '\n' || *ptr == '\0'))
  87. ptr++;
  88. } else {
  89. /*
  90. * TELOPT_NAWS support!
  91. */
  92. if ((ptr+2) >= end) {
  93. /* only the beginning of the IAC is in the
  94. buffer we were asked to process, we can't
  95. process this char. */
  96. break;
  97. }
  98. /*
  99. * IAC -> SB -> TELOPT_NAWS -> 4-byte -> IAC -> SE
  100. */
  101. else if (ptr[1] == SB && ptr[2] == TELOPT_NAWS) {
  102. struct winsize ws;
  103. if ((ptr+8) >= end)
  104. break; /* incomplete, can't process */
  105. ws.ws_col = (ptr[3] << 8) | ptr[4];
  106. ws.ws_row = (ptr[5] << 8) | ptr[6];
  107. ioctl(ts->ptyfd, TIOCSWINSZ, (char *)&ws);
  108. ptr += 9;
  109. } else {
  110. /* skip 3-byte IAC non-SB cmd */
  111. #if DEBUG
  112. fprintf(stderr, "Ignoring IAC %s,%s\n",
  113. TELCMD(ptr[1]), TELOPT(ptr[2]));
  114. #endif
  115. ptr += 3;
  116. }
  117. }
  118. }
  119. num_totty = totty - ptr0;
  120. *pnum_totty = num_totty;
  121. /* the difference between ptr and totty is number of iacs
  122. we removed from the stream. Adjust buf1 accordingly. */
  123. if ((ptr - totty) == 0) /* 99.999% of cases */
  124. return ptr0;
  125. ts->wridx1 += ptr - totty;
  126. ts->size1 -= ptr - totty;
  127. /* move chars meant for the terminal towards the end of the buffer */
  128. return memmove(ptr - num_totty, ptr0, num_totty);
  129. }
  130. static struct tsession *
  131. make_new_session(
  132. USE_FEATURE_TELNETD_STANDALONE(int sock)
  133. SKIP_FEATURE_TELNETD_STANDALONE(void)
  134. ) {
  135. const char *login_argv[2];
  136. struct termios termbuf;
  137. int fd, pid;
  138. char tty_name[GETPTY_BUFSIZE];
  139. struct tsession *ts = xzalloc(sizeof(struct tsession) + BUFSIZE * 2);
  140. /*ts->buf1 = (char *)(ts + 1);*/
  141. /*ts->buf2 = ts->buf1 + BUFSIZE;*/
  142. /* Got a new connection, set up a tty. */
  143. fd = getpty(tty_name);
  144. if (fd < 0) {
  145. bb_error_msg("can't create pty");
  146. return NULL;
  147. }
  148. if (fd > maxfd)
  149. maxfd = fd;
  150. ts->ptyfd = fd;
  151. ndelay_on(fd);
  152. #if ENABLE_FEATURE_TELNETD_STANDALONE
  153. ts->sockfd_read = sock;
  154. ndelay_on(sock);
  155. if (!sock) { /* We are called with fd 0 - we are in inetd mode */
  156. sock++; /* so use fd 1 for output */
  157. ndelay_on(sock);
  158. }
  159. ts->sockfd_write = sock;
  160. if (sock > maxfd)
  161. maxfd = sock;
  162. #else
  163. /* ts->sockfd_read = 0; - done by xzalloc */
  164. ts->sockfd_write = 1;
  165. ndelay_on(0);
  166. ndelay_on(1);
  167. #endif
  168. /* Make the telnet client understand we will echo characters so it
  169. * should not do it locally. We don't tell the client to run linemode,
  170. * because we want to handle line editing and tab completion and other
  171. * stuff that requires char-by-char support. */
  172. {
  173. static const char iacs_to_send[] ALIGN1 = {
  174. IAC, DO, TELOPT_ECHO,
  175. IAC, DO, TELOPT_NAWS,
  176. IAC, DO, TELOPT_LFLOW,
  177. IAC, WILL, TELOPT_ECHO,
  178. IAC, WILL, TELOPT_SGA
  179. };
  180. memcpy(TS_BUF2, iacs_to_send, sizeof(iacs_to_send));
  181. ts->rdidx2 = sizeof(iacs_to_send);
  182. ts->size2 = sizeof(iacs_to_send);
  183. }
  184. fflush(NULL); /* flush all streams */
  185. pid = vfork(); /* NOMMU-friendly */
  186. if (pid < 0) {
  187. free(ts);
  188. close(fd);
  189. /* sock will be closed by caller */
  190. bb_perror_msg("vfork");
  191. return NULL;
  192. }
  193. if (pid > 0) {
  194. /* Parent */
  195. ts->shell_pid = pid;
  196. return ts;
  197. }
  198. /* Child */
  199. /* Careful - we are after vfork! */
  200. /* make new session and process group */
  201. setsid();
  202. /* Restore default signal handling */
  203. bb_signals((1 << SIGCHLD) + (1 << SIGPIPE), SIG_DFL);
  204. /* open the child's side of the tty. */
  205. /* NB: setsid() disconnects from any previous ctty's. Therefore
  206. * we must open child's side of the tty AFTER setsid! */
  207. fd = xopen(tty_name, O_RDWR); /* becomes our ctty */
  208. dup2(fd, 0);
  209. dup2(fd, 1);
  210. dup2(fd, 2);
  211. while (fd > 2) close(fd--);
  212. tcsetpgrp(0, getpid()); /* switch this tty's process group to us */
  213. /* The pseudo-terminal allocated to the client is configured to operate in
  214. * cooked mode, and with XTABS CRMOD enabled (see tty(4)). */
  215. tcgetattr(0, &termbuf);
  216. termbuf.c_lflag |= ECHO; /* if we use readline we dont want this */
  217. termbuf.c_oflag |= ONLCR | XTABS;
  218. termbuf.c_iflag |= ICRNL;
  219. termbuf.c_iflag &= ~IXOFF;
  220. /*termbuf.c_lflag &= ~ICANON;*/
  221. tcsetattr(0, TCSANOW, &termbuf);
  222. /* Uses FILE-based I/O to stdout, but does fflush(stdout),
  223. * so should be safe with vfork.
  224. * I fear, though, that some users will have ridiculously big
  225. * issue files, and they may block writing to fd 1,
  226. * (parent is supposed to read it, but parent waits
  227. * for vforked child to exec!) */
  228. print_login_issue(issuefile, NULL);
  229. /* Exec shell / login / whatever */
  230. login_argv[0] = loginpath;
  231. login_argv[1] = NULL;
  232. /* exec busybox applet (if PREFER_APPLETS=y), if that fails,
  233. * exec external program */
  234. BB_EXECVP(loginpath, (char **)login_argv);
  235. /* _exit is safer with vfork, and we shouldn't send message
  236. * to remote clients anyway */
  237. _exit(1); /*bb_perror_msg_and_die("execv %s", loginpath);*/
  238. }
  239. /* Must match getopt32 string */
  240. enum {
  241. OPT_WATCHCHILD = (1 << 2), /* -K */
  242. OPT_INETD = (1 << 3) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -i */
  243. OPT_PORT = (1 << 4) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -p */
  244. OPT_FOREGROUND = (1 << 6) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -F */
  245. };
  246. #if ENABLE_FEATURE_TELNETD_STANDALONE
  247. static void
  248. free_session(struct tsession *ts)
  249. {
  250. struct tsession *t = sessions;
  251. if (option_mask32 & OPT_INETD)
  252. exit(0);
  253. /* Unlink this telnet session from the session list */
  254. if (t == ts)
  255. sessions = ts->next;
  256. else {
  257. while (t->next != ts)
  258. t = t->next;
  259. t->next = ts->next;
  260. }
  261. #if 0
  262. /* It was said that "normal" telnetd just closes ptyfd,
  263. * doesn't send SIGKILL. When we close ptyfd,
  264. * kernel sends SIGHUP to processes having slave side opened. */
  265. kill(ts->shell_pid, SIGKILL);
  266. wait4(ts->shell_pid, NULL, 0, NULL);
  267. #endif
  268. close(ts->ptyfd);
  269. close(ts->sockfd_read);
  270. /* We do not need to close(ts->sockfd_write), it's the same
  271. * as sockfd_read unless we are in inetd mode. But in inetd mode
  272. * we do not reach this */
  273. free(ts);
  274. /* Scan all sessions and find new maxfd */
  275. maxfd = 0;
  276. ts = sessions;
  277. while (ts) {
  278. if (maxfd < ts->ptyfd)
  279. maxfd = ts->ptyfd;
  280. if (maxfd < ts->sockfd_read)
  281. maxfd = ts->sockfd_read;
  282. #if 0
  283. /* Again, sockfd_write == sockfd_read here */
  284. if (maxfd < ts->sockfd_write)
  285. maxfd = ts->sockfd_write;
  286. #endif
  287. ts = ts->next;
  288. }
  289. }
  290. #else /* !FEATURE_TELNETD_STANDALONE */
  291. /* Used in main() only, thus "return 0" actually is exit(0). */
  292. #define free_session(ts) return 0
  293. #endif
  294. static void handle_sigchld(int sig ATTRIBUTE_UNUSED)
  295. {
  296. pid_t pid;
  297. struct tsession *ts;
  298. /* Looping: more than one child may have exited */
  299. while (1) {
  300. pid = wait_any_nohang(NULL);
  301. if (pid <= 0)
  302. break;
  303. ts = sessions;
  304. while (ts) {
  305. if (ts->shell_pid == pid) {
  306. ts->shell_pid = -1;
  307. break;
  308. }
  309. ts = ts->next;
  310. }
  311. }
  312. }
  313. int telnetd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  314. int telnetd_main(int argc ATTRIBUTE_UNUSED, char **argv)
  315. {
  316. fd_set rdfdset, wrfdset;
  317. unsigned opt;
  318. int count;
  319. struct tsession *ts;
  320. #if ENABLE_FEATURE_TELNETD_STANDALONE
  321. #define IS_INETD (opt & OPT_INETD)
  322. int master_fd = master_fd; /* be happy, gcc */
  323. unsigned portnbr = 23;
  324. char *opt_bindaddr = NULL;
  325. char *opt_portnbr;
  326. #else
  327. enum {
  328. IS_INETD = 1,
  329. master_fd = -1,
  330. portnbr = 23,
  331. };
  332. #endif
  333. /* Even if !STANDALONE, we accept (and ignore) -i, thus people
  334. * don't need to guess whether it's ok to pass -i to us */
  335. opt = getopt32(argv, "f:l:Ki" USE_FEATURE_TELNETD_STANDALONE("p:b:F"),
  336. &issuefile, &loginpath
  337. USE_FEATURE_TELNETD_STANDALONE(, &opt_portnbr, &opt_bindaddr));
  338. if (!IS_INETD /*&& !re_execed*/) {
  339. /* inform that we start in standalone mode?
  340. * May be useful when people forget to give -i */
  341. /*bb_error_msg("listening for connections");*/
  342. if (!(opt & OPT_FOREGROUND)) {
  343. /* DAEMON_CHDIR_ROOT was giving inconsistent
  344. * behavior with/without -F, -i */
  345. bb_daemonize_or_rexec(0 /*was DAEMON_CHDIR_ROOT*/, argv);
  346. }
  347. }
  348. /* Redirect log to syslog early, if needed */
  349. if (IS_INETD || !(opt & OPT_FOREGROUND)) {
  350. openlog(applet_name, 0, LOG_USER);
  351. logmode = LOGMODE_SYSLOG;
  352. }
  353. USE_FEATURE_TELNETD_STANDALONE(
  354. if (opt & OPT_PORT)
  355. portnbr = xatou16(opt_portnbr);
  356. );
  357. /* Used to check access(loginpath, X_OK) here. Pointless.
  358. * exec will do this for us for free later. */
  359. #if ENABLE_FEATURE_TELNETD_STANDALONE
  360. if (IS_INETD) {
  361. sessions = make_new_session(0);
  362. if (!sessions) /* pty opening or vfork problem, exit */
  363. return 1; /* make_new_session prints error message */
  364. } else {
  365. master_fd = create_and_bind_stream_or_die(opt_bindaddr, portnbr);
  366. xlisten(master_fd, 1);
  367. }
  368. #else
  369. sessions = make_new_session();
  370. if (!sessions) /* pty opening or vfork problem, exit */
  371. return 1; /* make_new_session prints error message */
  372. #endif
  373. /* We don't want to die if just one session is broken */
  374. signal(SIGPIPE, SIG_IGN);
  375. if (opt & OPT_WATCHCHILD)
  376. signal(SIGCHLD, handle_sigchld);
  377. else /* prevent dead children from becoming zombies */
  378. signal(SIGCHLD, SIG_IGN);
  379. /*
  380. This is how the buffers are used. The arrows indicate the movement
  381. of data.
  382. +-------+ wridx1++ +------+ rdidx1++ +----------+
  383. | | <-------------- | buf1 | <-------------- | |
  384. | | size1-- +------+ size1++ | |
  385. | pty | | socket |
  386. | | rdidx2++ +------+ wridx2++ | |
  387. | | --------------> | buf2 | --------------> | |
  388. +-------+ size2++ +------+ size2-- +----------+
  389. size1: "how many bytes are buffered for pty between rdidx1 and wridx1?"
  390. size2: "how many bytes are buffered for socket between rdidx2 and wridx2?"
  391. Each session has got two buffers. Buffers are circular. If sizeN == 0,
  392. buffer is empty. If sizeN == BUFSIZE, buffer is full. In both these cases
  393. rdidxN == wridxN.
  394. */
  395. again:
  396. FD_ZERO(&rdfdset);
  397. FD_ZERO(&wrfdset);
  398. /* Select on the master socket, all telnet sockets and their
  399. * ptys if there is room in their session buffers.
  400. * NB: scalability problem: we recalculate entire bitmap
  401. * before each select. Can be a problem with 500+ connections. */
  402. ts = sessions;
  403. while (ts) {
  404. struct tsession *next = ts->next; /* in case we free ts. */
  405. if (ts->shell_pid == -1) {
  406. /* Child died and we detected that */
  407. free_session(ts);
  408. } else {
  409. if (ts->size1 > 0) /* can write to pty */
  410. FD_SET(ts->ptyfd, &wrfdset);
  411. if (ts->size1 < BUFSIZE) /* can read from socket */
  412. FD_SET(ts->sockfd_read, &rdfdset);
  413. if (ts->size2 > 0) /* can write to socket */
  414. FD_SET(ts->sockfd_write, &wrfdset);
  415. if (ts->size2 < BUFSIZE) /* can read from pty */
  416. FD_SET(ts->ptyfd, &rdfdset);
  417. }
  418. ts = next;
  419. }
  420. if (!IS_INETD) {
  421. FD_SET(master_fd, &rdfdset);
  422. /* This is needed because free_session() does not
  423. * take master_fd into account when it finds new
  424. * maxfd among remaining fd's */
  425. if (master_fd > maxfd)
  426. maxfd = master_fd;
  427. }
  428. count = select(maxfd + 1, &rdfdset, &wrfdset, NULL, NULL);
  429. if (count < 0)
  430. goto again; /* EINTR or ENOMEM */
  431. #if ENABLE_FEATURE_TELNETD_STANDALONE
  432. /* First check for and accept new sessions. */
  433. if (!IS_INETD && FD_ISSET(master_fd, &rdfdset)) {
  434. int fd;
  435. struct tsession *new_ts;
  436. fd = accept(master_fd, NULL, NULL);
  437. if (fd < 0)
  438. goto again;
  439. /* Create a new session and link it into our active list */
  440. new_ts = make_new_session(fd);
  441. if (new_ts) {
  442. new_ts->next = sessions;
  443. sessions = new_ts;
  444. } else {
  445. close(fd);
  446. }
  447. }
  448. #endif
  449. /* Then check for data tunneling. */
  450. ts = sessions;
  451. while (ts) { /* For all sessions... */
  452. struct tsession *next = ts->next; /* in case we free ts. */
  453. if (/*ts->size1 &&*/ FD_ISSET(ts->ptyfd, &wrfdset)) {
  454. int num_totty;
  455. unsigned char *ptr;
  456. /* Write to pty from buffer 1. */
  457. ptr = remove_iacs(ts, &num_totty);
  458. count = safe_write(ts->ptyfd, ptr, num_totty);
  459. if (count < 0) {
  460. if (errno == EAGAIN)
  461. goto skip1;
  462. goto kill_session;
  463. }
  464. ts->size1 -= count;
  465. ts->wridx1 += count;
  466. if (ts->wridx1 >= BUFSIZE) /* actually == BUFSIZE */
  467. ts->wridx1 = 0;
  468. }
  469. skip1:
  470. if (/*ts->size2 &&*/ FD_ISSET(ts->sockfd_write, &wrfdset)) {
  471. /* Write to socket from buffer 2. */
  472. count = MIN(BUFSIZE - ts->wridx2, ts->size2);
  473. count = safe_write(ts->sockfd_write, TS_BUF2 + ts->wridx2, count);
  474. if (count < 0) {
  475. if (errno == EAGAIN)
  476. goto skip2;
  477. goto kill_session;
  478. }
  479. ts->size2 -= count;
  480. ts->wridx2 += count;
  481. if (ts->wridx2 >= BUFSIZE) /* actually == BUFSIZE */
  482. ts->wridx2 = 0;
  483. }
  484. skip2:
  485. /* Should not be needed, but... remove_iacs is actually buggy
  486. * (it cannot process iacs which wrap around buffer's end)!
  487. * Since properly fixing it requires writing bigger code,
  488. * we rely instead on this code making it virtually impossible
  489. * to have wrapped iac (people don't type at 2k/second).
  490. * It also allows for bigger reads in common case. */
  491. if (ts->size1 == 0) {
  492. ts->rdidx1 = 0;
  493. ts->wridx1 = 0;
  494. }
  495. if (ts->size2 == 0) {
  496. ts->rdidx2 = 0;
  497. ts->wridx2 = 0;
  498. }
  499. if (/*ts->size1 < BUFSIZE &&*/ FD_ISSET(ts->sockfd_read, &rdfdset)) {
  500. /* Read from socket to buffer 1. */
  501. count = MIN(BUFSIZE - ts->rdidx1, BUFSIZE - ts->size1);
  502. count = safe_read(ts->sockfd_read, TS_BUF1 + ts->rdidx1, count);
  503. if (count <= 0) {
  504. if (count < 0 && errno == EAGAIN)
  505. goto skip3;
  506. goto kill_session;
  507. }
  508. /* Ignore trailing NUL if it is there */
  509. if (!TS_BUF1[ts->rdidx1 + count - 1]) {
  510. --count;
  511. }
  512. ts->size1 += count;
  513. ts->rdidx1 += count;
  514. if (ts->rdidx1 >= BUFSIZE) /* actually == BUFSIZE */
  515. ts->rdidx1 = 0;
  516. }
  517. skip3:
  518. if (/*ts->size2 < BUFSIZE &&*/ FD_ISSET(ts->ptyfd, &rdfdset)) {
  519. /* Read from pty to buffer 2. */
  520. count = MIN(BUFSIZE - ts->rdidx2, BUFSIZE - ts->size2);
  521. count = safe_read(ts->ptyfd, TS_BUF2 + ts->rdidx2, count);
  522. if (count <= 0) {
  523. if (count < 0 && errno == EAGAIN)
  524. goto skip4;
  525. goto kill_session;
  526. }
  527. ts->size2 += count;
  528. ts->rdidx2 += count;
  529. if (ts->rdidx2 >= BUFSIZE) /* actually == BUFSIZE */
  530. ts->rdidx2 = 0;
  531. }
  532. skip4:
  533. ts = next;
  534. continue;
  535. kill_session:
  536. free_session(ts);
  537. ts = next;
  538. }
  539. goto again;
  540. }