3328 |
povik |
1 |
/** |
|
|
2 |
* \addtogroup telnetd |
|
|
3 |
* @{ |
|
|
4 |
*/ |
|
|
5 |
|
|
|
6 |
/** |
|
|
7 |
* \file |
|
|
8 |
* An example telnet server shell |
|
|
9 |
* \author Adam Dunkels <adam@dunkels.com> |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
/* |
|
|
13 |
* Copyright (c) 2003, Adam Dunkels. |
|
|
14 |
* All rights reserved. |
|
|
15 |
* |
|
|
16 |
* Redistribution and use in source and binary forms, with or without |
|
|
17 |
* modification, are permitted provided that the following conditions |
|
|
18 |
* are met: |
|
|
19 |
* 1. Redistributions of source code must retain the above copyright |
|
|
20 |
* notice, this list of conditions and the following disclaimer. |
|
|
21 |
* 2. Redistributions in binary form must reproduce the above copyright |
|
|
22 |
* notice, this list of conditions and the following disclaimer in the |
|
|
23 |
* documentation and/or other materials provided with the distribution. |
|
|
24 |
* 3. The name of the author may not be used to endorse or promote |
|
|
25 |
* products derived from this software without specific prior |
|
|
26 |
* written permission. |
|
|
27 |
* |
|
|
28 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
|
|
29 |
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
|
30 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
|
31 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
|
|
32 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
|
33 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
|
|
34 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
|
35 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
|
36 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
|
37 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
|
38 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
39 |
* |
|
|
40 |
* This file is part of the Contiki desktop OS. |
|
|
41 |
* |
|
|
42 |
* $Id: telnetd-shell.c,v 1.1.2.1 2003/10/06 22:56:22 adam Exp $ |
|
|
43 |
* |
|
|
44 |
*/ |
|
|
45 |
|
|
|
46 |
#include "uip.h" |
|
|
47 |
#include "telnetd.h" |
|
|
48 |
#include <string.h> |
|
|
49 |
|
|
|
50 |
struct ptentry { |
|
|
51 |
char c; |
|
|
52 |
void (* pfunc)(struct telnetd_state *s, char *str); |
|
|
53 |
}; |
|
|
54 |
|
|
|
55 |
/*-----------------------------------------------------------------------------------*/ |
|
|
56 |
static void |
|
|
57 |
parse(struct telnetd_state *s, register char *str, struct ptentry *t) |
|
|
58 |
{ |
|
|
59 |
register struct ptentry *p; |
|
|
60 |
char *sstr; |
|
|
61 |
|
|
|
62 |
sstr = str; |
|
|
63 |
|
|
|
64 |
/* Loop over the parse table entries in t in order to find one that |
|
|
65 |
matches the first character in str. */ |
|
|
66 |
for(p = t; p->c != 0; ++p) { |
|
|
67 |
if(*str == p->c) { |
|
|
68 |
/* Skip rest of the characters up to the first space. */ |
|
|
69 |
while(*str != ' ') { |
|
|
70 |
++str; |
|
|
71 |
} |
|
|
72 |
|
|
|
73 |
/* Skip all spaces.*/ |
|
|
74 |
while(*str == ' ') { |
|
|
75 |
++str; |
|
|
76 |
} |
|
|
77 |
|
|
|
78 |
/* Call parse table entry function and return. */ |
|
|
79 |
p->pfunc(s, str); |
|
|
80 |
return; |
|
|
81 |
} |
|
|
82 |
} |
|
|
83 |
|
|
|
84 |
/* Did not find matching entry in parse table. We just call the |
|
|
85 |
default handler supplied by the caller and return. */ |
|
|
86 |
p->pfunc(s, str); |
|
|
87 |
} |
|
|
88 |
/*-----------------------------------------------------------------------------------*/ |
|
|
89 |
static void |
|
|
90 |
exitt(struct telnetd_state *s, char *str) |
|
|
91 |
{ |
|
|
92 |
telnetd_close(s); |
|
|
93 |
} |
|
|
94 |
/*-----------------------------------------------------------------------------------*/ |
|
|
95 |
static void |
|
|
96 |
inttostr(register char *str, unsigned int i) |
|
|
97 |
{ |
|
|
98 |
str[0] = '0' + i / 100; |
|
|
99 |
if(str[0] == '0') { |
|
|
100 |
str[0] = ' '; |
|
|
101 |
} |
|
|
102 |
str[1] = '0' + (i / 10) % 10; |
|
|
103 |
if(str[1] == '0') { |
|
|
104 |
str[1] = ' '; |
|
|
105 |
} |
|
|
106 |
str[2] = '0' + i % 10; |
|
|
107 |
str[3] = ' '; |
|
|
108 |
str[4] = 0; |
|
|
109 |
} |
|
|
110 |
/*-----------------------------------------------------------------------------------*/ |
|
|
111 |
static void |
|
|
112 |
stats(struct telnetd_state *s, char *strr) |
|
|
113 |
{ |
|
|
114 |
char str[10]; |
|
|
115 |
|
|
|
116 |
inttostr(str, uip_stat.ip.recv); |
|
|
117 |
telnetd_output(s, "IP packets received ", str); |
|
|
118 |
inttostr(str, uip_stat.ip.sent); |
|
|
119 |
telnetd_output(s, "IP packets sent ", str); |
|
|
120 |
inttostr(str, uip_stat.ip.drop); |
|
|
121 |
telnetd_output(s, "IP packets dropped ", str); |
|
|
122 |
|
|
|
123 |
inttostr(str, uip_stat.icmp.recv); |
|
|
124 |
telnetd_output(s, "ICMP packets received ", str); |
|
|
125 |
inttostr(str, uip_stat.icmp.sent); |
|
|
126 |
telnetd_output(s, "ICMP packets sent ", str); |
|
|
127 |
inttostr(str, uip_stat.icmp.drop); |
|
|
128 |
telnetd_output(s, "ICMP packets dropped ", str); |
|
|
129 |
|
|
|
130 |
inttostr(str, uip_stat.tcp.recv); |
|
|
131 |
telnetd_output(s, "TCP packets received ", str); |
|
|
132 |
inttostr(str, uip_stat.tcp.sent); |
|
|
133 |
telnetd_output(s, "TCP packets sent ", str); |
|
|
134 |
inttostr(str, uip_stat.tcp.drop); |
|
|
135 |
telnetd_output(s, "TCP packets dropped ", str); |
|
|
136 |
inttostr(str, uip_stat.tcp.rexmit); |
|
|
137 |
telnetd_output(s, "TCP packets retransmitted ", str); |
|
|
138 |
inttostr(str, uip_stat.tcp.synrst); |
|
|
139 |
telnetd_output(s, "TCP connection attempts ", str); |
|
|
140 |
} |
|
|
141 |
/*-----------------------------------------------------------------------------------*/ |
|
|
142 |
static void |
|
|
143 |
help(struct telnetd_state *s, char *str) |
|
|
144 |
{ |
|
|
145 |
telnetd_output(s, "Available commands:", ""); |
|
|
146 |
telnetd_output(s, "stats - show uIP statistics", ""); |
|
|
147 |
telnetd_output(s, "exit - exit shell", ""); |
|
|
148 |
telnetd_output(s, "? - show this help", ""); |
|
|
149 |
} |
|
|
150 |
/*-----------------------------------------------------------------------------------*/ |
|
|
151 |
static void |
|
|
152 |
none(struct telnetd_state *s, char *str) |
|
|
153 |
{ |
|
|
154 |
if(strlen(str) > 0) { |
|
|
155 |
telnetd_output(s, "Unknown command", ""); |
|
|
156 |
} |
|
|
157 |
} |
|
|
158 |
/*-----------------------------------------------------------------------------------*/ |
|
|
159 |
static struct ptentry configparsetab[] = |
|
|
160 |
{{'s', stats}, |
|
|
161 |
{'e', exitt}, |
|
|
162 |
{'?', help}, |
|
|
163 |
|
|
|
164 |
/* Default action */ |
|
|
165 |
{0, none}}; |
|
|
166 |
/*-----------------------------------------------------------------------------------*/ |
|
|
167 |
void |
|
|
168 |
telnetd_connected(struct telnetd_state *s) |
|
|
169 |
{ |
|
|
170 |
telnetd_output(s, "uIP command shell", ""); |
|
|
171 |
telnetd_output(s, "Type '?' for help", ""); |
|
|
172 |
telnetd_prompt(s, "uIP-0.9> "); |
|
|
173 |
} |
|
|
174 |
/*-----------------------------------------------------------------------------------*/ |
|
|
175 |
void |
|
|
176 |
telnetd_input(struct telnetd_state *s, char *cmd) |
|
|
177 |
{ |
|
|
178 |
parse(s, cmd, configparsetab); |
|
|
179 |
telnetd_prompt(s, "uIP-0.9> "); |
|
|
180 |
} |
|
|
181 |
/*-----------------------------------------------------------------------------------*/ |