From 0b882cd516d5fa63472ce137b17e015e3a459f1c Mon Sep 17 00:00:00 2001 From: Adam Dunkels Date: Sun, 17 Mar 2013 10:49:11 +0100 Subject: [PATCH] Telnetd improvement: allow specifying a maximum silence time and kill the connection after that time. This is to avoid the telnet connection getting stuck forever if the connecting host reboots. --- apps/telnetd/telnetd.c | 22 +++++++++++++++++----- apps/telnetd/telnetd.h | 2 ++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/apps/telnetd/telnetd.c b/apps/telnetd/telnetd.c index e0967e6c2..95786442b 100644 --- a/apps/telnetd/telnetd.c +++ b/apps/telnetd/telnetd.c @@ -72,8 +72,8 @@ struct telnetd_state { #define STATE_WONT 3 #define STATE_DO 4 #define STATE_DONT 5 - #define STATE_CLOSE 6 + struct timer silence_timer; }; static struct telnetd_state s; @@ -101,6 +101,8 @@ static struct telnetd_buf buf; static uint8_t connected; +#define MAX_SILENCE_TIME (CLOCK_SECOND * 30) + #define MIN(a, b) ((a) < (b)? (a): (b)) /*---------------------------------------------------------------------------*/ static void @@ -357,6 +359,7 @@ telnetd_appcall(void *ts) s.state = STATE_NORMAL; connected = 1; shell_start(); + timer_set(&s.silence_timer, MAX_SILENCE_TIME); ts = (char *)0; } else { uip_send(telnetd_reject_text, strlen(telnetd_reject_text)); @@ -378,9 +381,11 @@ telnetd_appcall(void *ts) connected = 0; } if(uip_acked()) { + timer_set(&s.silence_timer, MAX_SILENCE_TIME); acked(); } if(uip_newdata()) { + timer_set(&s.silence_timer, MAX_SILENCE_TIME); newdata(); } if(uip_rexmit() || @@ -389,15 +394,22 @@ telnetd_appcall(void *ts) uip_connected() || uip_poll()) { senddata(); + if(s.numsent > 0) { + timer_set(&s.silence_timer, MAX_SILENCE_TIME); + } } - } else { if(uip_poll()) { - if(ts == (char *)10) { + if(timer_expired(&s.silence_timer)) { uip_close(); - } else { - tcp_markconn(uip_conn, (char *)ts + 1); + tcp_markconn(uip_conn, NULL); } } } } /*---------------------------------------------------------------------------*/ +void +telnetd_init(void) +{ + process_start(&telnetd_process, NULL); +} +/*---------------------------------------------------------------------------*/ diff --git a/apps/telnetd/telnetd.h b/apps/telnetd/telnetd.h index 3c9d6c015..9db26ebec 100644 --- a/apps/telnetd/telnetd.h +++ b/apps/telnetd/telnetd.h @@ -38,6 +38,8 @@ PROCESS_NAME(telnetd_process); +void telnetd_init(void); + void telnetd_gui_eventhandler(process_event_t ev, process_data_t data); void telnetd_appcall(void *data); void telnetd_gui_init(void);