2006-06-18 00:41:10 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2003, Adam Dunkels.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
|
|
|
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki desktop OS.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-02-09 18:15:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "contiki-lib.h"
|
2006-06-18 00:41:10 +02:00
|
|
|
#include "contiki-net.h"
|
|
|
|
#include "lib/petsciiconv.h"
|
|
|
|
#include "shell.h"
|
|
|
|
|
2008-02-09 18:15:58 +01:00
|
|
|
#include "telnetd.h"
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
#define ISO_nl 0x0a
|
|
|
|
#define ISO_cr 0x0d
|
|
|
|
|
2008-02-24 21:43:28 +01:00
|
|
|
PROCESS(telnetd_process, "Telnet server");
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2008-02-09 18:15:58 +01:00
|
|
|
AUTOSTART_PROCESSES(&telnetd_process);
|
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
#ifndef TELNETD_CONF_LINELEN
|
2008-02-09 19:51:56 +01:00
|
|
|
#define TELNETD_CONF_LINELEN 80
|
2006-06-18 00:41:10 +02:00
|
|
|
#endif
|
|
|
|
#ifndef TELNETD_CONF_NUMLINES
|
2008-02-09 19:51:56 +01:00
|
|
|
#define TELNETD_CONF_NUMLINES 25
|
2006-06-18 00:41:10 +02:00
|
|
|
#endif
|
|
|
|
|
2011-05-21 21:03:04 +02:00
|
|
|
#ifdef TELNETD_CONF_REJECT
|
|
|
|
extern char telnetd_reject_text[];
|
|
|
|
#else
|
|
|
|
static char telnetd_reject_text[] =
|
|
|
|
"Too many connections, please try again later.";
|
|
|
|
#endif
|
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
struct telnetd_state {
|
2008-02-09 18:15:58 +01:00
|
|
|
char buf[TELNETD_CONF_LINELEN + 1];
|
2006-06-18 00:41:10 +02:00
|
|
|
char bufptr;
|
2008-02-24 21:43:28 +01:00
|
|
|
uint16_t numsent;
|
2012-02-20 20:13:50 +01:00
|
|
|
uint8_t state;
|
2006-06-18 00:41:10 +02:00
|
|
|
#define STATE_NORMAL 0
|
|
|
|
#define STATE_IAC 1
|
|
|
|
#define STATE_WILL 2
|
|
|
|
#define STATE_WONT 3
|
|
|
|
#define STATE_DO 4
|
|
|
|
#define STATE_DONT 5
|
|
|
|
|
|
|
|
#define STATE_CLOSE 6
|
|
|
|
};
|
|
|
|
static struct telnetd_state s;
|
|
|
|
|
|
|
|
#define TELNET_IAC 255
|
|
|
|
#define TELNET_WILL 251
|
|
|
|
#define TELNET_WONT 252
|
|
|
|
#define TELNET_DO 253
|
|
|
|
#define TELNET_DONT 254
|
2008-02-24 21:43:28 +01:00
|
|
|
|
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct telnetd_buf {
|
|
|
|
char bufmem[TELNETD_CONF_NUMLINES * TELNETD_CONF_LINELEN];
|
|
|
|
int ptr;
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct telnetd_buf buf;
|
|
|
|
|
2011-05-21 21:03:04 +02:00
|
|
|
static uint8_t connected;
|
|
|
|
|
2008-02-24 21:43:28 +01:00
|
|
|
#define MIN(a, b) ((a) < (b)? (a): (b))
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
buf_init(struct telnetd_buf *buf)
|
|
|
|
{
|
|
|
|
buf->ptr = 0;
|
|
|
|
buf->size = TELNETD_CONF_NUMLINES * TELNETD_CONF_LINELEN;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
buf_append(struct telnetd_buf *buf, const char *data, int len)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2008-02-24 21:43:28 +01:00
|
|
|
int copylen;
|
|
|
|
|
|
|
|
PRINTF("buf_append len %d (%d) '%.*s'\n", len, buf->ptr, len, data);
|
|
|
|
copylen = MIN(len, buf->size - buf->ptr);
|
|
|
|
memcpy(&buf->bufmem[buf->ptr], data, copylen);
|
2011-04-16 21:10:22 +02:00
|
|
|
petsciiconv_toascii(&buf->bufmem[buf->ptr], copylen);
|
2008-02-24 21:43:28 +01:00
|
|
|
buf->ptr += copylen;
|
|
|
|
|
|
|
|
return copylen;
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-24 21:43:28 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 00:41:10 +02:00
|
|
|
static void
|
2008-02-24 21:43:28 +01:00
|
|
|
buf_copyto(struct telnetd_buf *buf, char *to, int len)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2008-02-24 21:43:28 +01:00
|
|
|
memcpy(to, &buf->bufmem[0], len);
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-24 21:43:28 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
buf_pop(struct telnetd_buf *buf, int len)
|
|
|
|
{
|
|
|
|
int poplen;
|
|
|
|
|
|
|
|
PRINTF("buf_pop len %d (%d)\n", len, buf->ptr);
|
|
|
|
poplen = MIN(len, buf->ptr);
|
|
|
|
memcpy(&buf->bufmem[0], &buf->bufmem[poplen], buf->ptr - poplen);
|
|
|
|
buf->ptr -= poplen;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
buf_len(struct telnetd_buf *buf)
|
|
|
|
{
|
|
|
|
return buf->ptr;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 00:41:10 +02:00
|
|
|
void
|
|
|
|
telnetd_quit(void)
|
|
|
|
{
|
2008-02-29 00:50:02 +01:00
|
|
|
shell_quit();
|
2006-06-18 00:41:10 +02:00
|
|
|
#if TELNETD_CONF_GUI
|
|
|
|
telnetd_gui_quit();
|
|
|
|
#endif /* TELNETD_CONF_GUI */
|
|
|
|
process_exit(&telnetd_process);
|
|
|
|
LOADER_UNLOAD();
|
|
|
|
}
|
2008-02-24 21:43:28 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 00:41:10 +02:00
|
|
|
void
|
|
|
|
shell_prompt(char *str)
|
|
|
|
{
|
2008-02-29 00:59:05 +01:00
|
|
|
buf_append(&buf, str, (int)strlen(str));
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-24 21:43:28 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 00:41:10 +02:00
|
|
|
void
|
2008-02-09 18:15:58 +01:00
|
|
|
shell_default_output(const char *str1, int len1, const char *str2, int len2)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2008-10-31 19:07:13 +01:00
|
|
|
if(len1 > 0 && str1[len1 - 1] == '\n') {
|
2008-02-09 18:15:58 +01:00
|
|
|
--len1;
|
|
|
|
}
|
2008-10-31 19:07:13 +01:00
|
|
|
if(len2 > 0 && str2[len2 - 1] == '\n') {
|
2008-02-09 18:15:58 +01:00
|
|
|
--len2;
|
|
|
|
}
|
|
|
|
|
2008-02-24 21:43:28 +01:00
|
|
|
/* PRINTF("shell_default_output: %.*s %.*s\n", len1, str1, len2, str2);*/
|
|
|
|
|
2007-04-14 00:15:52 +02:00
|
|
|
#if TELNETD_CONF_GUI
|
2008-02-09 18:15:58 +01:00
|
|
|
telnetd_gui_output(str1, len1, str2, len2);
|
2007-04-14 00:15:52 +02:00
|
|
|
#endif /* TELNETD_CONF_GUI */
|
2008-02-24 21:43:28 +01:00
|
|
|
buf_append(&buf, str1, len1);
|
|
|
|
buf_append(&buf, str2, len2);
|
|
|
|
buf_append(&buf, "\r\n", 2);
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-24 21:43:28 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2011-04-16 21:10:22 +02:00
|
|
|
void
|
|
|
|
shell_exit(void)
|
|
|
|
{
|
|
|
|
s.state = STATE_CLOSE;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 00:41:10 +02:00
|
|
|
PROCESS_THREAD(telnetd_process, ev, data)
|
|
|
|
{
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
shell_init();
|
2008-02-09 18:15:58 +01:00
|
|
|
|
2007-04-14 00:15:52 +02:00
|
|
|
#if TELNETD_CONF_GUI
|
|
|
|
telnetd_gui_init();
|
|
|
|
#endif /* TELNETD_CONF_GUI */
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2011-05-21 21:03:04 +02:00
|
|
|
petsciiconv_toascii(telnetd_reject_text, strlen(telnetd_reject_text));
|
|
|
|
|
2011-04-17 12:46:42 +02:00
|
|
|
tcp_listen(UIP_HTONS(23));
|
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
while(1) {
|
|
|
|
PROCESS_WAIT_EVENT();
|
|
|
|
if(ev == tcpip_event) {
|
|
|
|
telnetd_appcall(data);
|
|
|
|
} else if(ev == PROCESS_EVENT_EXIT) {
|
|
|
|
telnetd_quit();
|
|
|
|
} else {
|
|
|
|
#if TELNETD_CONF_GUI
|
|
|
|
telnetd_gui_eventhandler(ev, data);
|
|
|
|
#endif /* TELNETD_CONF_GUI */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
2008-02-24 21:43:28 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 00:41:10 +02:00
|
|
|
static void
|
|
|
|
acked(void)
|
|
|
|
{
|
2008-02-24 21:43:28 +01:00
|
|
|
buf_pop(&buf, s.numsent);
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-24 21:43:28 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 00:41:10 +02:00
|
|
|
static void
|
|
|
|
senddata(void)
|
|
|
|
{
|
2008-02-24 21:43:28 +01:00
|
|
|
int len;
|
|
|
|
len = MIN(buf_len(&buf), uip_mss());
|
|
|
|
PRINTF("senddata len %d\n", len);
|
|
|
|
buf_copyto(&buf, uip_appdata, len);
|
|
|
|
uip_send(uip_appdata, len);
|
|
|
|
s.numsent = len;
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-24 21:43:28 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 00:41:10 +02:00
|
|
|
static void
|
2012-02-20 20:13:50 +01:00
|
|
|
get_char(uint8_t c)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2008-02-24 21:43:28 +01:00
|
|
|
PRINTF("telnetd: get_char '%c' %d %d\n", c, c, s.bufptr);
|
|
|
|
|
|
|
|
if(c == 0) {
|
2006-06-18 00:41:10 +02:00
|
|
|
return;
|
|
|
|
}
|
2008-02-24 21:43:28 +01:00
|
|
|
|
|
|
|
if(c != ISO_nl && c != ISO_cr) {
|
|
|
|
s.buf[(int)s.bufptr] = c;
|
|
|
|
++s.bufptr;
|
|
|
|
}
|
|
|
|
if(((c == ISO_nl || c == ISO_cr) && s.bufptr > 0) ||
|
|
|
|
s.bufptr == sizeof(s.buf)) {
|
|
|
|
if(s.bufptr < sizeof(s.buf)) {
|
2006-06-18 00:41:10 +02:00
|
|
|
s.buf[(int)s.bufptr] = 0;
|
|
|
|
}
|
2008-02-24 21:43:28 +01:00
|
|
|
petsciiconv_topetscii(s.buf, TELNETD_CONF_LINELEN);
|
|
|
|
PRINTF("telnetd: get_char '%.*s'\n", s.bufptr, s.buf);
|
2008-02-09 18:15:58 +01:00
|
|
|
shell_input(s.buf, s.bufptr);
|
2006-06-18 00:41:10 +02:00
|
|
|
s.bufptr = 0;
|
|
|
|
}
|
|
|
|
}
|
2008-02-24 21:43:28 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 00:41:10 +02:00
|
|
|
static void
|
2012-02-20 20:13:50 +01:00
|
|
|
sendopt(uint8_t option, uint8_t value)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2008-02-24 21:43:28 +01:00
|
|
|
char line[4];
|
|
|
|
line[0] = (char)TELNET_IAC;
|
|
|
|
line[1] = option;
|
|
|
|
line[2] = value;
|
|
|
|
line[3] = 0;
|
2011-04-16 21:10:22 +02:00
|
|
|
petsciiconv_topetscii(line, 4);
|
2008-02-24 21:43:28 +01:00
|
|
|
buf_append(&buf, line, 4);
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2008-02-24 21:43:28 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 00:41:10 +02:00
|
|
|
static void
|
|
|
|
newdata(void)
|
|
|
|
{
|
2012-02-20 20:13:50 +01:00
|
|
|
uint16_t len;
|
|
|
|
uint8_t c;
|
2008-02-24 21:43:28 +01:00
|
|
|
uint8_t *ptr;
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
len = uip_datalen();
|
2008-02-24 21:43:28 +01:00
|
|
|
PRINTF("newdata len %d '%.*s'\n", len, len, (char *)uip_appdata);
|
|
|
|
|
|
|
|
ptr = uip_appdata;
|
2006-06-18 00:41:10 +02:00
|
|
|
while(len > 0 && s.bufptr < sizeof(s.buf)) {
|
2008-02-24 21:43:28 +01:00
|
|
|
c = *ptr;
|
|
|
|
PRINTF("newdata char '%c' %d %d state %d\n", c, c, len, s.state);
|
|
|
|
++ptr;
|
2006-06-18 00:41:10 +02:00
|
|
|
--len;
|
|
|
|
switch(s.state) {
|
|
|
|
case STATE_IAC:
|
|
|
|
if(c == TELNET_IAC) {
|
|
|
|
get_char(c);
|
|
|
|
s.state = STATE_NORMAL;
|
|
|
|
} else {
|
|
|
|
switch(c) {
|
|
|
|
case TELNET_WILL:
|
|
|
|
s.state = STATE_WILL;
|
|
|
|
break;
|
|
|
|
case TELNET_WONT:
|
|
|
|
s.state = STATE_WONT;
|
|
|
|
break;
|
|
|
|
case TELNET_DO:
|
|
|
|
s.state = STATE_DO;
|
|
|
|
break;
|
|
|
|
case TELNET_DONT:
|
|
|
|
s.state = STATE_DONT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
s.state = STATE_NORMAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case STATE_WILL:
|
|
|
|
/* Reply with a DONT */
|
|
|
|
sendopt(TELNET_DONT, c);
|
|
|
|
s.state = STATE_NORMAL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_WONT:
|
|
|
|
/* Reply with a DONT */
|
|
|
|
sendopt(TELNET_DONT, c);
|
|
|
|
s.state = STATE_NORMAL;
|
|
|
|
break;
|
|
|
|
case STATE_DO:
|
|
|
|
/* Reply with a WONT */
|
|
|
|
sendopt(TELNET_WONT, c);
|
|
|
|
s.state = STATE_NORMAL;
|
|
|
|
break;
|
|
|
|
case STATE_DONT:
|
|
|
|
/* Reply with a WONT */
|
|
|
|
sendopt(TELNET_WONT, c);
|
|
|
|
s.state = STATE_NORMAL;
|
|
|
|
break;
|
|
|
|
case STATE_NORMAL:
|
|
|
|
if(c == TELNET_IAC) {
|
|
|
|
s.state = STATE_IAC;
|
|
|
|
} else {
|
|
|
|
get_char(c);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-02-24 21:43:28 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 00:41:10 +02:00
|
|
|
void
|
|
|
|
telnetd_appcall(void *ts)
|
|
|
|
{
|
|
|
|
if(uip_connected()) {
|
2011-05-21 21:03:04 +02:00
|
|
|
if(!connected) {
|
|
|
|
buf_init(&buf);
|
|
|
|
s.bufptr = 0;
|
|
|
|
s.state = STATE_NORMAL;
|
|
|
|
connected = 1;
|
|
|
|
shell_start();
|
|
|
|
ts = (char *)0;
|
|
|
|
} else {
|
|
|
|
uip_send(telnetd_reject_text, strlen(telnetd_reject_text));
|
|
|
|
ts = (char *)1;
|
|
|
|
}
|
|
|
|
tcp_markconn(uip_conn, ts);
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
|
2011-05-21 21:03:04 +02:00
|
|
|
if(!ts) {
|
|
|
|
if(s.state == STATE_CLOSE) {
|
|
|
|
s.state = STATE_NORMAL;
|
|
|
|
uip_close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(uip_closed() ||
|
|
|
|
uip_aborted() ||
|
|
|
|
uip_timedout()) {
|
|
|
|
shell_stop();
|
|
|
|
connected = 0;
|
|
|
|
}
|
|
|
|
if(uip_acked()) {
|
|
|
|
acked();
|
|
|
|
}
|
|
|
|
if(uip_newdata()) {
|
|
|
|
newdata();
|
|
|
|
}
|
|
|
|
if(uip_rexmit() ||
|
|
|
|
uip_newdata() ||
|
|
|
|
uip_acked() ||
|
|
|
|
uip_connected() ||
|
|
|
|
uip_poll()) {
|
|
|
|
senddata();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(uip_poll()) {
|
|
|
|
if(ts == (char *)10) {
|
|
|
|
uip_close();
|
2011-05-22 23:08:40 +02:00
|
|
|
} else {
|
|
|
|
tcp_markconn(uip_conn, (char *)ts + 1);
|
2011-05-21 21:03:04 +02:00
|
|
|
}
|
|
|
|
}
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
}
|
2008-02-24 21:43:28 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|