2 * \addtogroup exampleapps
\r
7 * \defgroup telnetd Telnet server
\r
10 * The uIP telnet server provides a command based interface to uIP. It
\r
11 * allows using the "telnet" application to access uIP, and implements
\r
12 * the required telnet option negotiation.
\r
14 * The code is structured in a way which makes it possible to add
\r
15 * commands without having to rewrite the main telnet code. The main
\r
16 * telnet code calls two callback functions, telnetd_connected() and
\r
17 * telnetd_input(), when a telnet connection has been established and
\r
18 * when a line of text arrives on a telnet connection. These two
\r
19 * functions can be implemented in a way which suits the particular
\r
20 * application or environment in which the uIP system is intended to
\r
23 * The uIP distribution contains an example telnet shell
\r
24 * implementation that provides a basic set of commands.
\r
29 * Implementation of the Telnet server.
\r
30 * \author Adam Dunkels <adam@dunkels.com>
\r
34 * Copyright (c) 2003, Adam Dunkels.
\r
35 * All rights reserved.
\r
37 * Redistribution and use in source and binary forms, with or without
\r
38 * modification, are permitted provided that the following conditions
\r
40 * 1. Redistributions of source code must retain the above copyright
\r
41 * notice, this list of conditions and the following disclaimer.
\r
42 * 2. Redistributions in binary form must reproduce the above copyright
\r
43 * notice, this list of conditions and the following disclaimer in the
\r
44 * documentation and/or other materials provided with the distribution.
\r
45 * 3. The name of the author may not be used to endorse or promote
\r
46 * products derived from this software without specific prior
\r
47 * written permission.
\r
49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
\r
50 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
\r
51 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
\r
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
\r
53 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
\r
55 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\r
56 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
\r
57 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
\r
58 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
\r
59 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
61 * This file is part of the uIP TCP/IP stack.
\r
63 * $Id: telnetd.c,v 1.1.2.2 2003/10/07 13:47:50 adam Exp $
\r
69 #include "telnetd.h"
\r
75 MEMB(linemem, TELNETD_LINELEN, TELNETD_NUMLINES);
\r
79 #define STATE_NORMAL 0
\r
81 #define STATE_WILL 2
\r
82 #define STATE_WONT 3
\r
84 #define STATE_DONT 5
\r
85 #define STATE_CLOSE 6
\r
87 #define TELNET_IAC 255
\r
88 #define TELNET_WILL 251
\r
89 #define TELNET_WONT 252
\r
90 #define TELNET_DO 253
\r
91 #define TELNET_DONT 254
\r
92 /*-----------------------------------------------------------------------------------*/
\r
96 return memb_alloc(&linemem);
\r
98 /*-----------------------------------------------------------------------------------*/
\r
100 dealloc_line(char *line)
\r
102 memb_free(&linemem, line);
\r
104 /*-----------------------------------------------------------------------------------*/
\r
106 sendline(struct telnetd_state *s, char *line)
\r
108 static unsigned int i;
\r
109 for(i = 0; i < TELNETD_NUMLINES; ++i) {
\r
110 if(s->lines[i] == NULL) {
\r
111 s->lines[i] = line;
\r
115 if(i == TELNETD_NUMLINES) {
\r
116 dealloc_line(line);
\r
119 /*-----------------------------------------------------------------------------------*/
\r
121 * Close a telnet session.
\r
123 * This function can be called from a telnet command in order to close
\r
126 * \param s The connection which is to be closed.
\r
129 /*-----------------------------------------------------------------------------------*/
\r
131 telnetd_close(struct telnetd_state *s)
\r
133 s->state = STATE_CLOSE;
\r
135 /*-----------------------------------------------------------------------------------*/
\r
137 * Print a prompt on a telnet connection.
\r
139 * This function can be called by the telnet command shell in order to
\r
140 * print out a command prompt.
\r
142 * \param s A telnet connection.
\r
144 * \param str The command prompt.
\r
147 /*-----------------------------------------------------------------------------------*/
\r
149 telnetd_prompt(struct telnetd_state *s, char *str)
\r
152 line = alloc_line();
\r
154 strncpy(line, str, TELNETD_LINELEN);
\r
158 /*-----------------------------------------------------------------------------------*/
\r
160 * Print out a string on a telnet connection.
\r
162 * This function can be called from a telnet command parser in order
\r
163 * to print out a string of text on the connection. The two strings
\r
164 * given as arguments to the function will be concatenated, a carrige
\r
165 * return and a new line character will be added, and the line is
\r
168 * \param s The telnet connection.
\r
170 * \param str1 The first string.
\r
172 * \param str2 The second string.
\r
175 /*-----------------------------------------------------------------------------------*/
\r
177 telnetd_output(struct telnetd_state *s, char *str1, char *str2)
\r
179 static unsigned len;
\r
182 line = alloc_line();
\r
184 len = strlen(str1);
\r
185 strncpy(line, str1, TELNETD_LINELEN);
\r
186 if(len < TELNETD_LINELEN) {
\r
187 strncpy(line + len, str2, TELNETD_LINELEN - len);
\r
189 len = strlen(line);
\r
190 if(len < TELNETD_LINELEN - 2) {
\r
191 line[len] = ISO_cr;
\r
192 line[len+1] = ISO_nl;
\r
198 /*-----------------------------------------------------------------------------------*/
\r
200 * Initialize the telnet server.
\r
202 * This function will perform the necessary initializations and start
\r
203 * listening on TCP port 23.
\r
205 /*-----------------------------------------------------------------------------------*/
\r
209 memb_init(&linemem);
\r
210 uip_listen(HTONS(23));
\r
212 /*-----------------------------------------------------------------------------------*/
\r
214 acked(struct telnetd_state *s)
\r
216 dealloc_line(s->lines[0]);
\r
217 for(i = 1; i < TELNETD_NUMLINES; ++i) {
\r
218 s->lines[i - 1] = s->lines[i];
\r
221 /*-----------------------------------------------------------------------------------*/
\r
223 senddata(struct telnetd_state *s)
\r
225 if(s->lines[0] != NULL) {
\r
226 uip_send(s->lines[0], strlen(s->lines[0]));
\r
229 /*-----------------------------------------------------------------------------------*/
\r
231 getchar(struct telnetd_state *s, u8_t c)
\r
237 s->buf[(int)s->bufptr] = c;
\r
238 if(s->buf[(int)s->bufptr] == ISO_nl ||
\r
239 s->bufptr == sizeof(s->buf) - 1) {
\r
240 if(s->bufptr > 0) {
\r
241 s->buf[(int)s->bufptr] = 0;
\r
243 telnetd_input(s, s->buf);
\r
249 /*-----------------------------------------------------------------------------------*/
\r
251 sendopt(struct telnetd_state *s, u8_t option, u8_t value)
\r
254 line = alloc_line();
\r
256 line[0] = TELNET_IAC;
\r
263 /*-----------------------------------------------------------------------------------*/
\r
265 newdata(struct telnetd_state *s)
\r
271 len = uip_datalen();
\r
273 while(len > 0 && s->bufptr < sizeof(s->buf)) {
\r
279 if(c == TELNET_IAC) {
\r
281 s->state = STATE_NORMAL;
\r
285 s->state = STATE_WILL;
\r
288 s->state = STATE_WONT;
\r
291 s->state = STATE_DO;
\r
294 s->state = STATE_DONT;
\r
297 s->state = STATE_NORMAL;
\r
303 /* Reply with a DONT */
\r
304 sendopt(s, TELNET_DONT, c);
\r
305 s->state = STATE_NORMAL;
\r
309 /* Reply with a DONT */
\r
310 sendopt(s, TELNET_DONT, c);
\r
311 s->state = STATE_NORMAL;
\r
314 /* Reply with a WONT */
\r
315 sendopt(s, TELNET_WONT, c);
\r
316 s->state = STATE_NORMAL;
\r
319 /* Reply with a WONT */
\r
320 sendopt(s, TELNET_WONT, c);
\r
321 s->state = STATE_NORMAL;
\r
324 if(c == TELNET_IAC) {
\r
325 s->state = STATE_IAC;
\r
336 /*-----------------------------------------------------------------------------------*/
\r
340 struct telnetd_state *s;
\r
342 s = (struct telnetd_state *)uip_conn->appstate;
\r
344 if(uip_connected()) {
\r
346 for(i = 0; i < TELNETD_NUMLINES; ++i) {
\r
347 s->lines[i] = NULL;
\r
350 s->state = STATE_NORMAL;
\r
352 telnetd_connected(s);
\r
357 if(s->state == STATE_CLOSE) {
\r
358 s->state = STATE_NORMAL;
\r
364 telnetd_output(s, "Connection closed", "");
\r
368 if(uip_aborted()) {
\r
369 telnetd_output(s, "Connection reset", "");
\r
372 if(uip_timedout()) {
\r
373 telnetd_output(s, "Connection timed out", "");
\r
380 if(uip_newdata()) {
\r
388 } else if(uip_poll()) {
\r
392 /*-----------------------------------------------------------------------------------*/
\r