Contiki 2.5
httpd-cgi.c
1 /*
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.
13  * 3. The name of the author may not be used to endorse or promote
14  * 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  *
31  * $Id: httpd-cgi.c,v 1.2 2010/10/19 18:29:05 adamdunkels Exp $
32  *
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 
46 #include "contiki-net.h"
47 #include "httpd.h"
48 #include "httpd-cgi.h"
49 #include "httpd-fs.h"
50 
51 #include "lib/petsciiconv.h"
52 
53 #include <stdio.h>
54 #include <string.h>
55 
56 
57 static struct httpd_cgi_call *calls = NULL;
58 
59 /*struct cgifunction {
60  char *name;
61  httpd_cgifunction function;
62 };
63 
64 static struct cgifunction cgitab[] = {
65  {"file-stats", file_stats},
66  {"tcp-connections", tcp_stats},
67  {"processes", processes},
68  {NULL, NULL}
69  };*/
70 
71 
72 static const char closed[] = /* "CLOSED",*/
73 {0x43, 0x4c, 0x4f, 0x53, 0x45, 0x44, 0};
74 static const char syn_rcvd[] = /* "SYN-RCVD",*/
75 {0x53, 0x59, 0x4e, 0x2d, 0x52, 0x43, 0x56,
76  0x44, 0};
77 static const char syn_sent[] = /* "SYN-SENT",*/
78 {0x53, 0x59, 0x4e, 0x2d, 0x53, 0x45, 0x4e,
79  0x54, 0};
80 static const char established[] = /* "ESTABLISHED",*/
81 {0x45, 0x53, 0x54, 0x41, 0x42, 0x4c, 0x49, 0x53, 0x48,
82  0x45, 0x44, 0};
83 static const char fin_wait_1[] = /* "FIN-WAIT-1",*/
84 {0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49,
85  0x54, 0x2d, 0x31, 0};
86 static const char fin_wait_2[] = /* "FIN-WAIT-2",*/
87 {0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49,
88  0x54, 0x2d, 0x32, 0};
89 static const char closing[] = /* "CLOSING",*/
90 {0x43, 0x4c, 0x4f, 0x53, 0x49,
91  0x4e, 0x47, 0};
92 static const char time_wait[] = /* "TIME-WAIT,"*/
93 {0x54, 0x49, 0x4d, 0x45, 0x2d, 0x57, 0x41,
94  0x49, 0x54, 0};
95 static const char last_ack[] = /* "LAST-ACK"*/
96 {0x4c, 0x41, 0x53, 0x54, 0x2d, 0x41, 0x43,
97  0x4b, 0};
98 static const char none[] = "NONE";
99 static const char init[] = "INIT";
100 static const char running[] = "RUNNING";
101 static const char needs_poll[] = "NEEDS POLL";
102 
103 static const char *states[] = {
104  closed,
105  syn_rcvd,
106  syn_sent,
107  established,
108  fin_wait_1,
109  fin_wait_2,
110  closing,
111  time_wait,
112  last_ack,
113  none,
114  init,
115  running,
116  needs_poll};
117 
118 
119 /*---------------------------------------------------------------------------*/
120 static
121 PT_THREAD(nullfunction(struct httpd_state *s, char *ptr))
122 {
123  PSOCK_BEGIN(&s->sout);
124  PSOCK_END(&s->sout);
125 }
126 /*---------------------------------------------------------------------------*/
127 httpd_cgifunction
128 httpd_cgi(char *name)
129 {
130  struct httpd_cgi_call *f;
131 
132  /* Find the matching name in the table, return the function. */
133  for(f = calls; f != NULL; f = f->next) {
134  if(strncmp(f->name, name, strlen(f->name)) == 0) {
135  return f->function;
136  }
137  }
138  return nullfunction;
139 }
140 /*---------------------------------------------------------------------------*/
141 static unsigned short
142 generate_file_stats(void *arg)
143 {
144  char *f = (char *)arg;
145  return sprintf((char *)uip_appdata, "%5u", httpd_fs_count(f));
146 }
147 /*---------------------------------------------------------------------------*/
148 static
149 PT_THREAD(file_stats(struct httpd_state *s, char *ptr))
150 {
151  PSOCK_BEGIN(&s->sout);
152 
153  PSOCK_GENERATOR_SEND(&s->sout, generate_file_stats, (void *) (strchr(ptr, ' ') + 1));
154 
155  PSOCK_END(&s->sout);
156 }
157 /*---------------------------------------------------------------------------*/
158 static unsigned short
159 make_tcp_stats(void *arg)
160 {
161  struct uip_conn *conn;
162  struct httpd_state *s = (struct httpd_state *)arg;
163 
164  conn = &uip_conns[s->u.count];
165  return sprintf((char *)uip_appdata,
166  "<tr align=\"center\"><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",
167  uip_htons(conn->lport),
168  conn->ripaddr.u8[0],
169  conn->ripaddr.u8[1],
170  conn->ripaddr.u8[2],
171  conn->ripaddr.u8[3],
172  uip_htons(conn->rport),
173  states[conn->tcpstateflags & UIP_TS_MASK],
174  conn->nrtx,
175  conn->timer,
176  (uip_outstanding(conn))? '*':' ',
177  (uip_stopped(conn))? '!':' ');
178 }
179 /*---------------------------------------------------------------------------*/
180 static
181 PT_THREAD(tcp_stats(struct httpd_state *s, char *ptr))
182 {
183 
184  PSOCK_BEGIN(&s->sout);
185 
186  for(s->u.count = 0; s->u.count < UIP_CONNS; ++s->u.count) {
187  if((uip_conns[s->u.count].tcpstateflags & UIP_TS_MASK) != UIP_CLOSED) {
188  PSOCK_GENERATOR_SEND(&s->sout, make_tcp_stats, s);
189  }
190  }
191 
192  PSOCK_END(&s->sout);
193 }
194 /*---------------------------------------------------------------------------*/
195 static unsigned short
196 make_processes(void *p)
197 {
198  char name[40];
199 
200  strncpy(name, PROCESS_NAME_STRING((struct process *)p), 40);
201  petsciiconv_toascii(name, 40);
202 
203  return sprintf((char *)uip_appdata,
204  "<tr align=\"center\"><td>%p</td><td>%s</td><td>%p</td><td>%s</td></tr>\r\n",
205  p, name,
206  (char *)((struct process *)p)->thread,
207  states[9 + ((struct process *)p)->state]);
208 }
209 /*---------------------------------------------------------------------------*/
210 static
211 PT_THREAD(processes(struct httpd_state *s, char *ptr))
212 {
213  PSOCK_BEGIN(&s->sout);
214  for(s->u.ptr = PROCESS_LIST(); s->u.ptr != NULL; s->u.ptr = ((struct process *)s->u.ptr)->next) {
215  PSOCK_GENERATOR_SEND(&s->sout, make_processes, s->u.ptr);
216  }
217  PSOCK_END(&s->sout);
218 }
219 /*---------------------------------------------------------------------------*/
220 void
221 httpd_cgi_add(struct httpd_cgi_call *c)
222 {
223  struct httpd_cgi_call *l;
224 
225  c->next = NULL;
226  if(calls == NULL) {
227  calls = c;
228  } else {
229  for(l = calls; l->next != NULL; l = l->next);
230  l->next = c;
231  }
232 }
233 /*---------------------------------------------------------------------------*/
234 
235 HTTPD_CGI_CALL(file, "file-stats", file_stats);
236 HTTPD_CGI_CALL(tcp, "tcp-connections", tcp_stats);
237 HTTPD_CGI_CALL(proc, "processes", processes);
238 
239 void
240 httpd_cgi_init(void)
241 {
242  httpd_cgi_add(&file);
243  httpd_cgi_add(&tcp);
244  httpd_cgi_add(&proc);
245 }
246 /*---------------------------------------------------------------------------*/