]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/uIP_Demo_IAR_ARM7/uip/cgi.c
748cc1b5d6da719294b819781a06efa8d847a97b
[freertos] / FreeRTOS / Demo / uIP_Demo_IAR_ARM7 / uip / cgi.c
1 /**\r
2  * \addtogroup httpd\r
3  * @{\r
4  */\r
5 \r
6 /**\r
7  * \file\r
8  * HTTP server script language C functions file.\r
9  * \author Adam Dunkels <adam@dunkels.com>\r
10  *\r
11  * This file contains functions that are called by the web server\r
12  * scripts. The functions takes one argument, and the return value is\r
13  * interpreted as follows. A zero means that the function did not\r
14  * complete and should be invoked for the next packet as well. A\r
15  * non-zero value indicates that the function has completed and that\r
16  * the web server should move along to the next script line.\r
17  *\r
18  */\r
19 \r
20 /*\r
21  * Copyright (c) 2001, Adam Dunkels.\r
22  * All rights reserved.\r
23  *\r
24  * Redistribution and use in source and binary forms, with or without\r
25  * modification, are permitted provided that the following conditions\r
26  * are met:\r
27  * 1. Redistributions of source code must retain the above copyright\r
28  *    notice, this list of conditions and the following disclaimer.\r
29  * 2. Redistributions in binary form must reproduce the above copyright\r
30  *    notice, this list of conditions and the following disclaimer in the\r
31  *    documentation and/or other materials provided with the distribution.\r
32  * 3. The name of the author may not be used to endorse or promote\r
33  *    products derived from this software without specific prior\r
34  *    written permission.\r
35  *\r
36  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS\r
37  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
38  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
39  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY\r
40  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
41  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE\r
42  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
43  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\r
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
45  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
46  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
47  *\r
48  * This file is part of the uIP TCP/IP stack.\r
49  *\r
50  * $Id: cgi.c,v 1.23.2.4 2003/10/07 13:22:27 adam Exp $\r
51  *\r
52  */\r
53 \r
54 #include "uip.h"\r
55 #include "cgi.h"\r
56 #include "httpd.h"\r
57 #include "fs.h"\r
58 \r
59 #include <stdio.h>\r
60 #include <string.h>\r
61 \r
62 static u8_t print_stats(u8_t next);\r
63 static u8_t file_stats(u8_t next);\r
64 static u8_t tcp_stats(u8_t next);\r
65 static u8_t rtos_stats(u8_t next);\r
66 \r
67 cgifunction cgitab[] = {\r
68   print_stats,   /* CGI function "a" */\r
69   file_stats,    /* CGI function "b" */\r
70   tcp_stats,      /* CGI function "c" */\r
71   rtos_stats    /* CGI function "d" */\r
72 };\r
73 \r
74 static const char closed[] =   /*  "CLOSED",*/\r
75 {0x43, 0x4c, 0x4f, 0x53, 0x45, 0x44, 0};\r
76 static const char syn_rcvd[] = /*  "SYN-RCVD",*/\r
77 {0x53, 0x59, 0x4e, 0x2d, 0x52, 0x43, 0x56,\r
78  0x44,  0};\r
79 static const char syn_sent[] = /*  "SYN-SENT",*/\r
80 {0x53, 0x59, 0x4e, 0x2d, 0x53, 0x45, 0x4e,\r
81  0x54,  0};\r
82 static const char established[] = /*  "ESTABLISHED",*/\r
83 {0x45, 0x53, 0x54, 0x41, 0x42, 0x4c, 0x49, 0x53, 0x48,\r
84  0x45, 0x44, 0};\r
85 static const char fin_wait_1[] = /*  "FIN-WAIT-1",*/\r
86 {0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49,\r
87  0x54, 0x2d, 0x31, 0};\r
88 static const char fin_wait_2[] = /*  "FIN-WAIT-2",*/\r
89 {0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49,\r
90  0x54, 0x2d, 0x32, 0};\r
91 static const char closing[] = /*  "CLOSING",*/\r
92 {0x43, 0x4c, 0x4f, 0x53, 0x49,\r
93  0x4e, 0x47, 0};\r
94 static const char time_wait[] = /*  "TIME-WAIT,"*/\r
95 {0x54, 0x49, 0x4d, 0x45, 0x2d, 0x57, 0x41,\r
96  0x49, 0x54, 0};\r
97 static const char last_ack[] = /*  "LAST-ACK"*/\r
98 {0x4c, 0x41, 0x53, 0x54, 0x2d, 0x41, 0x43,\r
99  0x4b, 0};\r
100 \r
101 static const char *states[] = {\r
102   closed,\r
103   syn_rcvd,\r
104   syn_sent,\r
105   established,\r
106   fin_wait_1,\r
107   fin_wait_2,\r
108   closing,\r
109   time_wait,\r
110   last_ack};\r
111 \r
112 \r
113 /*-----------------------------------------------------------------------------------*/\r
114 /* print_stats:\r
115  *\r
116  * Prints out a part of the uIP statistics. The statistics data is\r
117  * written into the uip_appdata buffer. It overwrites any incoming\r
118  * packet.\r
119  */\r
120 static u8_t\r
121 print_stats(u8_t next)\r
122 {\r
123 #if UIP_STATISTICS\r
124   u16_t i, j;\r
125   u8_t *buf;\r
126   u16_t *databytes;\r
127 \r
128   if(next) {\r
129     /* If our last data has been acknowledged, we move on the next\r
130        chunk of statistics. */\r
131     hs->count = hs->count + 4;\r
132     if(hs->count >= sizeof(struct uip_stats)/sizeof(u16_t)) {\r
133       /* We have printed out all statistics, so we return 1 to\r
134          indicate that we are done. */\r
135       return 1;\r
136     }\r
137   }\r
138 \r
139   /* Write part of the statistics into the uip_appdata buffer. */\r
140   databytes = (u16_t *)&uip_stat + hs->count;\r
141   buf       = (u8_t *)uip_appdata;\r
142 \r
143   j = 4 + 1;\r
144   i = hs->count;\r
145   while (i < sizeof(struct uip_stats)/sizeof(u16_t) && --j > 0) {\r
146     sprintf((char *)buf, "%5u\r\n", *databytes);\r
147     ++databytes;\r
148     buf += 6;\r
149     ++i;\r
150   }\r
151 \r
152   /* Send the data. */\r
153   uip_send(uip_appdata, buf - uip_appdata);\r
154 \r
155   return 0;\r
156 #else\r
157   return 1;\r
158 #endif /* UIP_STATISTICS */\r
159 }\r
160 /*-----------------------------------------------------------------------------------*/\r
161 static u8_t\r
162 file_stats(u8_t next)\r
163 {\r
164   /* We use sprintf() to print the number of file accesses to a\r
165      particular file (given as an argument to the function in the\r
166      script). We then use uip_send() to actually send the data. */\r
167   if(next) {\r
168     return 1;\r
169   }\r
170   uip_send(uip_appdata, sprintf((char *)uip_appdata, "%5u", fs_count(&hs->script[4])));\r
171   return 0;\r
172 }\r
173 /*-----------------------------------------------------------------------------------*/\r
174 static u8_t\r
175 tcp_stats(u8_t next)\r
176 {\r
177   struct uip_conn *conn;\r
178 \r
179   if(next) {\r
180     /* If the previously sent data has been acknowledged, we move\r
181        forward one connection. */\r
182     if(++hs->count == UIP_CONNS) {\r
183       /* If all connections has been printed out, we are done and\r
184          return 1. */\r
185       return 1;\r
186     }\r
187   }\r
188 \r
189   conn = &uip_conns[hs->count];\r
190   if((conn->tcpstateflags & TS_MASK) == CLOSED) {\r
191     uip_send(uip_appdata, sprintf((char *)uip_appdata,\r
192                                   "<tr align=\"center\"><td>-</td><td>-</td><td>%u</td><td>%u</td><td>%c %c</td></tr>\r\n",\r
193                                   conn->nrtx,\r
194                                   conn->timer,\r
195                                   (uip_outstanding(conn))? '*':' ',\r
196                                   (uip_stopped(conn))? '!':' '));\r
197   } else {\r
198     uip_send(uip_appdata, sprintf((char *)uip_appdata,\r
199                                   "<tr align=\"center\"><td>%u.%u.%u.%u:%u</td><td>%s</td><td>%u</td><td>%u</td><td>%c %c</td></tr>\r\n",\r
200                                   htons(conn->ripaddr[0]) >> 8,\r
201                                   htons(conn->ripaddr[0]) & 0xff,\r
202                                   htons(conn->ripaddr[1]) >> 8,\r
203                                   htons(conn->ripaddr[1]) & 0xff,\r
204                                   htons(conn->rport),\r
205                                   states[conn->tcpstateflags & TS_MASK],\r
206                                   conn->nrtx,\r
207                                   conn->timer,\r
208                                   (uip_outstanding(conn))? '*':' ',\r
209                                   (uip_stopped(conn))? '!':' '));\r
210   }\r
211   return 0;\r
212 }\r
213 /*-----------------------------------------------------------------------------------*/\r
214 \r
215 static u8_t\r
216 rtos_stats(u8_t next)\r
217 {\r
218 static char cTraceBuffer[ 1024 ];\r
219 extern void ( vTaskList )( char * );\r
220 \r
221         vTaskList( cTraceBuffer );\r
222         uip_send( ( void * ) cTraceBuffer, strlen( cTraceBuffer ) );\r
223 \r
224         return 1;\r
225 }\r