blob: 495141b92de2f825c1707a3f524034aefb089a78 [file] [log] [blame]
adamdunkelsc7cc92a2003-05-28 05:21:49 +00001/*
2 * Copyright (c) 2001, 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.
adamdunkels06f897e2004-06-06 05:59:20 +000013 * 3. The name of the author may not be used to endorse or promote
adamdunkelsc7cc92a2003-05-28 05:21:49 +000014 * 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 uIP TCP/IP stack.
30 *
ryohjic615a2d2007-06-02 07:32:05 +000031 * $Id: httpd-cgi.c,v 1.10 2007/06/02 07:32:06 ryohji Exp $
adamdunkelsc7cc92a2003-05-28 05:21:49 +000032 *
33 */
34
35/*
36 * This file includes functions that are called by the web server
37 * scripts. The functions takes no argument, and the return value is
38 * interpreted as follows. A zero means that the function did not
39 * complete and should be invoked for the next packet as well. A
40 * non-zero value indicates that the function has completed and that
41 * the web server should move along to the next script line.
42 *
43 */
44
45#include "uip.h"
46#include "httpd.h"
47#include "httpd-cgi.h"
48#include "httpd-fs.h"
49
adamdunkels9f20c052003-06-30 20:41:14 +000050#include "petsciiconv.h"
51
adamdunkelsc7cc92a2003-05-28 05:21:49 +000052#include <stdio.h>
53#include <string.h>
54
adamdunkelsb63552d2004-09-12 07:15:00 +000055static PT_THREAD(file_stats(struct httpd_state *s, char *arg));
56static PT_THREAD(tcp_stats(struct httpd_state *s, char *arg));
57static PT_THREAD(processes(struct httpd_state *s, char *arg));
adamdunkelsc7cc92a2003-05-28 05:21:49 +000058
adamdunkelsf29d8882005-02-27 09:33:50 +000059struct cgifunction {
60 char *name;
61 httpd_cgifunction function;
adamdunkelsc7cc92a2003-05-28 05:21:49 +000062};
63
adamdunkelsf29d8882005-02-27 09:33:50 +000064static struct cgifunction cgitab[] = {
65 {"file-stats", file_stats},
66 {"tcp-connections", tcp_stats},
67 {"processes", processes},
68 {NULL, NULL}
69};
70
71
adamdunkelsc7cc92a2003-05-28 05:21:49 +000072static const char closed[] = /* "CLOSED",*/
73{0x43, 0x4c, 0x4f, 0x53, 0x45, 0x44, 0};
74static const char syn_rcvd[] = /* "SYN-RCVD",*/
75{0x53, 0x59, 0x4e, 0x2d, 0x52, 0x43, 0x56,
76 0x44, 0};
77static const char syn_sent[] = /* "SYN-SENT",*/
78{0x53, 0x59, 0x4e, 0x2d, 0x53, 0x45, 0x4e,
79 0x54, 0};
80static const char established[] = /* "ESTABLISHED",*/
81{0x45, 0x53, 0x54, 0x41, 0x42, 0x4c, 0x49, 0x53, 0x48,
82 0x45, 0x44, 0};
83static const char fin_wait_1[] = /* "FIN-WAIT-1",*/
84{0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49,
85 0x54, 0x2d, 0x31, 0};
86static const char fin_wait_2[] = /* "FIN-WAIT-2",*/
87{0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49,
88 0x54, 0x2d, 0x32, 0};
89static const char closing[] = /* "CLOSING",*/
90{0x43, 0x4c, 0x4f, 0x53, 0x49,
91 0x4e, 0x47, 0};
92static const char time_wait[] = /* "TIME-WAIT,"*/
93{0x54, 0x49, 0x4d, 0x45, 0x2d, 0x57, 0x41,
94 0x49, 0x54, 0};
95static const char last_ack[] = /* "LAST-ACK"*/
96{0x4c, 0x41, 0x53, 0x54, 0x2d, 0x41, 0x43,
97 0x4b, 0};
98
99static const char *states[] = {
100 closed,
101 syn_rcvd,
102 syn_sent,
103 established,
104 fin_wait_1,
105 fin_wait_2,
106 closing,
107 time_wait,
108 last_ack};
109
110
adamdunkelsf29d8882005-02-27 09:33:50 +0000111/*---------------------------------------------------------------------------*/
112static
113PT_THREAD(nullfunction(struct httpd_state *s, char *ptr))
114{
115 PSOCK_BEGIN(&s->sout);
116 PSOCK_END(&s->sout);
117}
118/*---------------------------------------------------------------------------*/
119httpd_cgifunction
120httpd_cgi(char *name)
121{
122 struct cgifunction *f;
123
124 /* Find the matching name in the table, return the function. */
125 for(f = cgitab; f->name != NULL; ++f) {
126 if(strncmp(f->name, name, strlen(f->name)) == 0) {
127 return f->function;
128 }
129 }
130 return nullfunction;
131}
132/*---------------------------------------------------------------------------*/
adamdunkelsb63552d2004-09-12 07:15:00 +0000133static unsigned short
adamdunkelsd251e842004-09-13 21:48:42 +0000134generate_file_stats(void *arg)
adamdunkelsc7cc92a2003-05-28 05:21:49 +0000135{
adamdunkelsd251e842004-09-13 21:48:42 +0000136 char *f = (char *)arg;
adamdunkelsb63552d2004-09-12 07:15:00 +0000137 return sprintf((char *)uip_appdata, "%5u", httpd_fs_count(f));
adamdunkelsc7cc92a2003-05-28 05:21:49 +0000138}
adamdunkelsf29d8882005-02-27 09:33:50 +0000139/*---------------------------------------------------------------------------*/
adamdunkelsb63552d2004-09-12 07:15:00 +0000140static
141PT_THREAD(file_stats(struct httpd_state *s, char *ptr))
142{
oliverschmidta900bff2005-02-23 21:18:05 +0000143 PSOCK_BEGIN(&s->sout);
adamdunkelsb63552d2004-09-12 07:15:00 +0000144
adamdunkelsf29d8882005-02-27 09:33:50 +0000145 PSOCK_GENERATOR_SEND(&s->sout, generate_file_stats, strchr(ptr, ' ') + 1);
adamdunkelsb63552d2004-09-12 07:15:00 +0000146
oliverschmidta900bff2005-02-23 21:18:05 +0000147 PSOCK_END(&s->sout);
adamdunkelsb63552d2004-09-12 07:15:00 +0000148}
adamdunkelsf29d8882005-02-27 09:33:50 +0000149/*---------------------------------------------------------------------------*/
adamdunkelsb63552d2004-09-12 07:15:00 +0000150static unsigned short
adamdunkelsd251e842004-09-13 21:48:42 +0000151make_tcp_stats(void *arg)
adamdunkelsc7cc92a2003-05-28 05:21:49 +0000152{
adamdunkelsd251e842004-09-13 21:48:42 +0000153 struct uip_conn *conn;
154 struct httpd_state *s = (struct httpd_state *)arg;
155
adamdunkelsb63552d2004-09-12 07:15:00 +0000156 conn = &uip_conns[s->count];
157 return sprintf((char *)uip_appdata,
158 "<tr><td>%d</td><td>%u.%u.%u.%u:%u</td><td>%s</td><td>%u</td><td>%u</td><td>%c %c</td></tr>\r\n",
159 htons(conn->lport),
160 htons(conn->ripaddr[0]) >> 8,
161 htons(conn->ripaddr[0]) & 0xff,
162 htons(conn->ripaddr[1]) >> 8,
163 htons(conn->ripaddr[1]) & 0xff,
164 htons(conn->rport),
165 states[conn->tcpstateflags & TS_MASK],
166 conn->nrtx,
167 conn->timer,
168 (uip_outstanding(conn))? '*':' ',
169 (uip_stopped(conn))? '!':' ');
adamdunkels9f20c052003-06-30 20:41:14 +0000170}
adamdunkelsf29d8882005-02-27 09:33:50 +0000171/*---------------------------------------------------------------------------*/
adamdunkelsb63552d2004-09-12 07:15:00 +0000172static
173PT_THREAD(tcp_stats(struct httpd_state *s, char *ptr))
adamdunkels9f20c052003-06-30 20:41:14 +0000174{
adamdunkelsb63552d2004-09-12 07:15:00 +0000175
oliverschmidta900bff2005-02-23 21:18:05 +0000176 PSOCK_BEGIN(&s->sout);
adamdunkelsb63552d2004-09-12 07:15:00 +0000177
178 for(s->count = 0; s->count < UIP_CONNS; ++s->count) {
179 if((uip_conns[s->count].tcpstateflags & TS_MASK) != CLOSED) {
oliverschmidta900bff2005-02-23 21:18:05 +0000180 PSOCK_GENERATOR_SEND(&s->sout, make_tcp_stats, s);
adamdunkelsb63552d2004-09-12 07:15:00 +0000181 }
182 }
183
oliverschmidta900bff2005-02-23 21:18:05 +0000184 PSOCK_END(&s->sout);
adamdunkelsb63552d2004-09-12 07:15:00 +0000185}
adamdunkelsf29d8882005-02-27 09:33:50 +0000186/*---------------------------------------------------------------------------*/
adamdunkelsb63552d2004-09-12 07:15:00 +0000187static unsigned short
adamdunkelsd251e842004-09-13 21:48:42 +0000188make_processes(void *s)
adamdunkelsb63552d2004-09-12 07:15:00 +0000189{
adamdunkelsd251e842004-09-13 21:48:42 +0000190 struct ek_proc *p = (struct ek_proc *)s;
adamdunkels9f20c052003-06-30 20:41:14 +0000191 char name[40];
192
adamdunkels9f20c052003-06-30 20:41:14 +0000193 strncpy(name, p->name, 40);
194 petsciiconv_toascii(name, 40);
adamdunkels0dce2c92004-09-12 20:52:37 +0000195
adamdunkelsb63552d2004-09-12 07:15:00 +0000196 return sprintf((char *)uip_appdata,
adamdunkelsd251e842004-09-13 21:48:42 +0000197 "<tr align=\"center\"><td>%3d</td><td>%s</td><td>0x%02x</td><td>%p</td><td>%p</td><td>%p</td></tr>\r\n",
adamdunkels0dce2c92004-09-12 20:52:37 +0000198 p->id, name, p->prio,
adamdunkelsb63552d2004-09-12 07:15:00 +0000199 p->pollhandler, p->eventhandler, p->procstate);
adamdunkels9f20c052003-06-30 20:41:14 +0000200}
adamdunkelsf29d8882005-02-27 09:33:50 +0000201/*---------------------------------------------------------------------------*/
adamdunkelsb63552d2004-09-12 07:15:00 +0000202static
203PT_THREAD(processes(struct httpd_state *s, char *ptr))
adamdunkels1f40cee2003-08-05 13:52:24 +0000204{
ryohjic615a2d2007-06-02 07:32:05 +0000205 static struct ek_proc *p;
adamdunkels0dce2c92004-09-12 20:52:37 +0000206
oliverschmidta900bff2005-02-23 21:18:05 +0000207 PSOCK_BEGIN(&s->sout);
adamdunkels1f40cee2003-08-05 13:52:24 +0000208
adamdunkels0dce2c92004-09-12 20:52:37 +0000209 for(s->count = 0; s->count < EK_CONF_MAXPROCS; ++s->count) {
210 p = ek_process(s->count);
211 if(p != NULL) {
oliverschmidta900bff2005-02-23 21:18:05 +0000212 PSOCK_GENERATOR_SEND(&s->sout, make_processes, p);
adamdunkels0dce2c92004-09-12 20:52:37 +0000213 }
214 }
adamdunkels9f20c052003-06-30 20:41:14 +0000215
oliverschmidta900bff2005-02-23 21:18:05 +0000216 PSOCK_END(&s->sout);
adamdunkels9f20c052003-06-30 20:41:14 +0000217}
adamdunkelsf29d8882005-02-27 09:33:50 +0000218/*---------------------------------------------------------------------------*/