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