Contiki 3.x
telnetd.c
1 /*
2  * Copyright (c) 2003, Adam Dunkels.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote
14  * products derived from this software without specific prior
15  * written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * This file is part of the Contiki desktop OS.
30  *
31  *
32  */
33 
34 #include <string.h>
35 
36 #include "contiki-lib.h"
37 #include "contiki-net.h"
38 #include "lib/petsciiconv.h"
39 #include "shell.h"
40 
41 #include "telnetd.h"
42 
43 #define ISO_nl 0x0a
44 #define ISO_cr 0x0d
45 
46 PROCESS(telnetd_process, "Telnet server");
47 
48 AUTOSTART_PROCESSES(&telnetd_process);
49 
50 #ifndef TELNETD_CONF_LINELEN
51 #define TELNETD_CONF_LINELEN 80
52 #endif
53 #ifndef TELNETD_CONF_NUMLINES
54 #define TELNETD_CONF_NUMLINES 25
55 #endif
56 
57 #ifdef TELNETD_CONF_REJECT
58 extern char telnetd_reject_text[];
59 #else
60 static char telnetd_reject_text[] =
61  "Too many connections, please try again later.";
62 #endif
63 
64 struct telnetd_state {
65  char buf[TELNETD_CONF_LINELEN + 1];
66  char bufptr;
67  uint16_t numsent;
68  uint8_t state;
69 #define STATE_NORMAL 0
70 #define STATE_IAC 1
71 #define STATE_WILL 2
72 #define STATE_WONT 3
73 #define STATE_DO 4
74 #define STATE_DONT 5
75 #define STATE_CLOSE 6
76  struct timer silence_timer;
77 };
78 static struct telnetd_state s;
79 
80 #define TELNET_IAC 255
81 #define TELNET_WILL 251
82 #define TELNET_WONT 252
83 #define TELNET_DO 253
84 #define TELNET_DONT 254
85 
86 #define DEBUG 0
87 #if DEBUG
88 #include <stdio.h>
89 #define PRINTF(...) printf(__VA_ARGS__)
90 #else
91 #define PRINTF(...)
92 #endif
93 
94 struct telnetd_buf {
95  char bufmem[TELNETD_CONF_NUMLINES * TELNETD_CONF_LINELEN];
96  int ptr;
97  int size;
98 };
99 
100 static struct telnetd_buf buf;
101 
102 static uint8_t connected;
103 
104 #define MAX_SILENCE_TIME (CLOCK_SECOND * 30)
105 
106 #define MIN(a, b) ((a) < (b)? (a): (b))
107 /*---------------------------------------------------------------------------*/
108 static void
109 buf_init(struct telnetd_buf *buf)
110 {
111  buf->ptr = 0;
112  buf->size = TELNETD_CONF_NUMLINES * TELNETD_CONF_LINELEN;
113 }
114 /*---------------------------------------------------------------------------*/
115 static int
116 buf_append(struct telnetd_buf *buf, const char *data, int len)
117 {
118  int copylen;
119 
120  PRINTF("buf_append len %d (%d) '%.*s'\n", len, buf->ptr, len, data);
121  copylen = MIN(len, buf->size - buf->ptr);
122  memcpy(&buf->bufmem[buf->ptr], data, copylen);
123  petsciiconv_toascii(&buf->bufmem[buf->ptr], copylen);
124  buf->ptr += copylen;
125 
126  return copylen;
127 }
128 /*---------------------------------------------------------------------------*/
129 static void
130 buf_copyto(struct telnetd_buf *buf, char *to, int len)
131 {
132  memcpy(to, &buf->bufmem[0], len);
133 }
134 /*---------------------------------------------------------------------------*/
135 static void
136 buf_pop(struct telnetd_buf *buf, int len)
137 {
138  int poplen;
139 
140  PRINTF("buf_pop len %d (%d)\n", len, buf->ptr);
141  poplen = MIN(len, buf->ptr);
142  memcpy(&buf->bufmem[0], &buf->bufmem[poplen], buf->ptr - poplen);
143  buf->ptr -= poplen;
144 }
145 /*---------------------------------------------------------------------------*/
146 static int
147 buf_len(struct telnetd_buf *buf)
148 {
149  return buf->ptr;
150 }
151 /*---------------------------------------------------------------------------*/
152 void
153 telnetd_quit(void)
154 {
155  shell_quit();
156 #if TELNETD_CONF_GUI
157  telnetd_gui_quit();
158 #endif /* TELNETD_CONF_GUI */
159  process_exit(&telnetd_process);
160  LOADER_UNLOAD();
161 }
162 /*---------------------------------------------------------------------------*/
163 void
164 shell_prompt(char *str)
165 {
166  buf_append(&buf, str, (int)strlen(str));
167 }
168 /*---------------------------------------------------------------------------*/
169 void
170 shell_default_output(const char *str1, int len1, const char *str2, int len2)
171 {
172  if(len1 > 0 && str1[len1 - 1] == '\n') {
173  --len1;
174  }
175  if(len2 > 0 && str2[len2 - 1] == '\n') {
176  --len2;
177  }
178 
179  /* PRINTF("shell_default_output: %.*s %.*s\n", len1, str1, len2, str2);*/
180 
181 #if TELNETD_CONF_GUI
182  telnetd_gui_output(str1, len1, str2, len2);
183 #endif /* TELNETD_CONF_GUI */
184  buf_append(&buf, str1, len1);
185  buf_append(&buf, str2, len2);
186  buf_append(&buf, "\r\n", 2);
187 }
188 /*---------------------------------------------------------------------------*/
189 void
190 shell_exit(void)
191 {
192  s.state = STATE_CLOSE;
193 }
194 /*---------------------------------------------------------------------------*/
195 PROCESS_THREAD(telnetd_process, ev, data)
196 {
197  PROCESS_BEGIN();
198 
199  shell_init();
200 
201 #if TELNETD_CONF_GUI
202  telnetd_gui_init();
203 #endif /* TELNETD_CONF_GUI */
204 
205  petsciiconv_toascii(telnetd_reject_text, strlen(telnetd_reject_text));
206 
207  tcp_listen(UIP_HTONS(23));
208 
209  while(1) {
211  if(ev == tcpip_event) {
212  telnetd_appcall(data);
213  } else if(ev == PROCESS_EVENT_EXIT) {
214  telnetd_quit();
215  } else {
216 #if TELNETD_CONF_GUI
217  telnetd_gui_eventhandler(ev, data);
218 #endif /* TELNETD_CONF_GUI */
219  }
220  }
221 
222  PROCESS_END();
223 }
224 /*---------------------------------------------------------------------------*/
225 static void
226 acked(void)
227 {
228  buf_pop(&buf, s.numsent);
229 }
230 /*---------------------------------------------------------------------------*/
231 static void
232 senddata(void)
233 {
234  int len;
235  len = MIN(buf_len(&buf), uip_mss());
236  PRINTF("senddata len %d\n", len);
237  buf_copyto(&buf, uip_appdata, len);
238  uip_send(uip_appdata, len);
239  s.numsent = len;
240 }
241 /*---------------------------------------------------------------------------*/
242 static void
243 get_char(uint8_t c)
244 {
245  PRINTF("telnetd: get_char '%c' %d %d\n", c, c, s.bufptr);
246 
247  if(c == 0) {
248  return;
249  }
250 
251  if(c != ISO_nl && c != ISO_cr) {
252  s.buf[(int)s.bufptr] = c;
253  ++s.bufptr;
254  }
255  if(((c == ISO_nl || c == ISO_cr) && s.bufptr > 0) ||
256  s.bufptr == sizeof(s.buf)) {
257  if(s.bufptr < sizeof(s.buf)) {
258  s.buf[(int)s.bufptr] = 0;
259  }
260  petsciiconv_topetscii(s.buf, TELNETD_CONF_LINELEN);
261  PRINTF("telnetd: get_char '%.*s'\n", s.bufptr, s.buf);
262  shell_input(s.buf, s.bufptr);
263  s.bufptr = 0;
264  }
265 }
266 /*---------------------------------------------------------------------------*/
267 static void
268 sendopt(uint8_t option, uint8_t value)
269 {
270  char line[4];
271  line[0] = (char)TELNET_IAC;
272  line[1] = option;
273  line[2] = value;
274  line[3] = 0;
275  petsciiconv_topetscii(line, 4);
276  buf_append(&buf, line, 4);
277 }
278 /*---------------------------------------------------------------------------*/
279 static void
280 newdata(void)
281 {
282  uint16_t len;
283  uint8_t c;
284  uint8_t *ptr;
285 
286  len = uip_datalen();
287  PRINTF("newdata len %d '%.*s'\n", len, len, (char *)uip_appdata);
288 
289  ptr = uip_appdata;
290  while(len > 0 && s.bufptr < sizeof(s.buf)) {
291  c = *ptr;
292  PRINTF("newdata char '%c' %d %d state %d\n", c, c, len, s.state);
293  ++ptr;
294  --len;
295  switch(s.state) {
296  case STATE_IAC:
297  if(c == TELNET_IAC) {
298  get_char(c);
299  s.state = STATE_NORMAL;
300  } else {
301  switch(c) {
302  case TELNET_WILL:
303  s.state = STATE_WILL;
304  break;
305  case TELNET_WONT:
306  s.state = STATE_WONT;
307  break;
308  case TELNET_DO:
309  s.state = STATE_DO;
310  break;
311  case TELNET_DONT:
312  s.state = STATE_DONT;
313  break;
314  default:
315  s.state = STATE_NORMAL;
316  break;
317  }
318  }
319  break;
320  case STATE_WILL:
321  /* Reply with a DONT */
322  sendopt(TELNET_DONT, c);
323  s.state = STATE_NORMAL;
324  break;
325 
326  case STATE_WONT:
327  /* Reply with a DONT */
328  sendopt(TELNET_DONT, c);
329  s.state = STATE_NORMAL;
330  break;
331  case STATE_DO:
332  /* Reply with a WONT */
333  sendopt(TELNET_WONT, c);
334  s.state = STATE_NORMAL;
335  break;
336  case STATE_DONT:
337  /* Reply with a WONT */
338  sendopt(TELNET_WONT, c);
339  s.state = STATE_NORMAL;
340  break;
341  case STATE_NORMAL:
342  if(c == TELNET_IAC) {
343  s.state = STATE_IAC;
344  } else {
345  get_char(c);
346  }
347  break;
348  }
349  }
350 }
351 /*---------------------------------------------------------------------------*/
352 void
353 telnetd_appcall(void *ts)
354 {
355  if(uip_connected()) {
356  if(!connected) {
357  buf_init(&buf);
358  s.bufptr = 0;
359  s.state = STATE_NORMAL;
360  connected = 1;
361  shell_start();
362  timer_set(&s.silence_timer, MAX_SILENCE_TIME);
363  ts = (char *)0;
364  } else {
365  uip_send(telnetd_reject_text, strlen(telnetd_reject_text));
366  ts = (char *)1;
367  }
368  tcp_markconn(uip_conn, ts);
369  }
370 
371  if(!ts) {
372  if(s.state == STATE_CLOSE) {
373  s.state = STATE_NORMAL;
374  uip_close();
375  return;
376  }
377  if(uip_closed() ||
378  uip_aborted() ||
379  uip_timedout()) {
380  shell_stop();
381  connected = 0;
382  }
383  if(uip_acked()) {
384  timer_set(&s.silence_timer, MAX_SILENCE_TIME);
385  acked();
386  }
387  if(uip_newdata()) {
388  timer_set(&s.silence_timer, MAX_SILENCE_TIME);
389  newdata();
390  }
391  if(uip_rexmit() ||
392  uip_newdata() ||
393  uip_acked() ||
394  uip_connected() ||
395  uip_poll()) {
396  senddata();
397  if(s.numsent > 0) {
398  timer_set(&s.silence_timer, MAX_SILENCE_TIME);
399  }
400  }
401  if(uip_poll()) {
402  if(timer_expired(&s.silence_timer)) {
403  uip_close();
404  tcp_markconn(uip_conn, NULL);
405  }
406  }
407  }
408 }
409 /*---------------------------------------------------------------------------*/
410 void
411 telnetd_init(void)
412 {
413  process_start(&telnetd_process, NULL);
414 }
415 /*---------------------------------------------------------------------------*/
void shell_prompt(char *str)
Print a prompt.
Definition: serial-shell.c:82
void shell_stop(void)
Stop the shell.
Definition: shell.c:570
A timer.
Definition: timer.h:86
void shell_init(void)
Initialize the shell.
Definition: shell.c:501
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
Representation of a uIP TCP connection.
Definition: uip.h:1336
#define uip_aborted()
Has the connection been aborted by the other end?
Definition: uip.h:781
CCIF void uip_send(const void *data, int len)
Send data on the current connection.
Definition: uip6.c:2310
#define uip_newdata()
Is new incoming data available?
Definition: uip.h:738
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition: timer.c:64
#define uip_mss()
Get the current maximum segment size that can be sent on the current connection.
Definition: uip.h:838
Main header file for the Contiki shell
#define NULL
The null pointer.
void shell_input(char *commandline, int commandline_len)
Send a line of input to the shell.
Definition: shell.c:359
#define uip_poll()
Is the connection being polled by uIP?
Definition: uip.h:817
void shell_quit(void)
Quit the shell.
Definition: shell.c:576
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
Definition: uip.h:1238
void shell_start(void)
Start the shell.
Definition: shell.c:562
#define uip_acked()
Has previously sent data been acknowledged?
Definition: uip.h:749
#define uip_connected()
Has the connection just been connected?
Definition: uip.h:761
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
PETSCII/ASCII conversion functions.
#define LOADER_UNLOAD()
Unload a program from memory.
Definition: loader.h:104
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
Definition: process.h:141
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define uip_close()
Close the current connection.
Definition: uip.h:671
#define uip_datalen()
The length of any incoming data that is currently available (if available) in the uip_appdata buffer...
Definition: uip.h:651
process_event_t tcpip_event
The uIP event.
Definition: tcpip.c:75
#define uip_timedout()
Has the connection timed out?
Definition: uip.h:791
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
CCIF void tcp_listen(uint16_t port)
Open a TCP port.
void shell_default_output(const char *text1, int len1, const char *text2, int len2)
Print a line of output from the shell.
Definition: serial-shell.c:58
#define uip_closed()
Has the connection been closed by the other end?
Definition: uip.h:771
void shell_exit(void)
Request shell exit.
Definition: serial-shell.c:89
int timer_expired(struct timer *t)
Check if a timer has expired.
Definition: timer.c:121
void process_exit(struct process *p)
Cause a process to exit.
Definition: process.c:202
uip_appdata
Pointer to the application data in the packet buffer.
Definition: tcp_loader.c:74
#define uip_rexmit()
Do we need to retransmit previously data?
Definition: uip.h:803