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.

This commit is contained in:
Adam Dunkels 2013-03-17 10:49:11 +01:00
parent 0a135eeba1
commit 0b882cd516
2 changed files with 19 additions and 5 deletions

View file

@ -72,8 +72,8 @@ struct telnetd_state {
#define STATE_WONT 3 #define STATE_WONT 3
#define STATE_DO 4 #define STATE_DO 4
#define STATE_DONT 5 #define STATE_DONT 5
#define STATE_CLOSE 6 #define STATE_CLOSE 6
struct timer silence_timer;
}; };
static struct telnetd_state s; static struct telnetd_state s;
@ -101,6 +101,8 @@ static struct telnetd_buf buf;
static uint8_t connected; static uint8_t connected;
#define MAX_SILENCE_TIME (CLOCK_SECOND * 30)
#define MIN(a, b) ((a) < (b)? (a): (b)) #define MIN(a, b) ((a) < (b)? (a): (b))
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static void static void
@ -357,6 +359,7 @@ telnetd_appcall(void *ts)
s.state = STATE_NORMAL; s.state = STATE_NORMAL;
connected = 1; connected = 1;
shell_start(); shell_start();
timer_set(&s.silence_timer, MAX_SILENCE_TIME);
ts = (char *)0; ts = (char *)0;
} else { } else {
uip_send(telnetd_reject_text, strlen(telnetd_reject_text)); uip_send(telnetd_reject_text, strlen(telnetd_reject_text));
@ -378,9 +381,11 @@ telnetd_appcall(void *ts)
connected = 0; connected = 0;
} }
if(uip_acked()) { if(uip_acked()) {
timer_set(&s.silence_timer, MAX_SILENCE_TIME);
acked(); acked();
} }
if(uip_newdata()) { if(uip_newdata()) {
timer_set(&s.silence_timer, MAX_SILENCE_TIME);
newdata(); newdata();
} }
if(uip_rexmit() || if(uip_rexmit() ||
@ -389,15 +394,22 @@ telnetd_appcall(void *ts)
uip_connected() || uip_connected() ||
uip_poll()) { uip_poll()) {
senddata(); senddata();
if(s.numsent > 0) {
timer_set(&s.silence_timer, MAX_SILENCE_TIME);
}
} }
} else {
if(uip_poll()) { if(uip_poll()) {
if(ts == (char *)10) { if(timer_expired(&s.silence_timer)) {
uip_close(); uip_close();
} else { tcp_markconn(uip_conn, NULL);
tcp_markconn(uip_conn, (char *)ts + 1);
} }
} }
} }
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void
telnetd_init(void)
{
process_start(&telnetd_process, NULL);
}
/*---------------------------------------------------------------------------*/

View file

@ -38,6 +38,8 @@
PROCESS_NAME(telnetd_process); PROCESS_NAME(telnetd_process);
void telnetd_init(void);
void telnetd_gui_eventhandler(process_event_t ev, process_data_t data); void telnetd_gui_eventhandler(process_event_t ev, process_data_t data);
void telnetd_appcall(void *data); void telnetd_appcall(void *data);
void telnetd_gui_init(void); void telnetd_gui_init(void);