Contiki 3.x
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  *
32  */
33 
34 /*
35  * This file includes functions that are called by the web server
36  * scripts. The functions takes no argument, and the return value is
37  * interpreted as follows. A zero means that the function did not
38  * complete and should be invoked for the next packet as well. A
39  * non-zero value indicates that the function has completed and that
40  * the web server should move along to the next script line.
41  *
42  */
43 
44 #include <stdio.h>
45 #include <string.h>
46 
47 #include "contiki-net.h"
48 #include "httpd.h"
49 #include "httpd-cgi.h"
50 #include "httpd-fs.h"
51 
52 #include "lib/petsciiconv.h"
53 
54 static struct httpd_cgi_call *calls = NULL;
55 
56 static const char closed[] = /* "CLOSED",*/
57 {0x43, 0x4c, 0x4f, 0x53, 0x45, 0x44, 0};
58 static const char syn_rcvd[] = /* "SYN-RCVD",*/
59 {0x53, 0x59, 0x4e, 0x2d, 0x52, 0x43, 0x56,
60  0x44, 0};
61 static const char syn_sent[] = /* "SYN-SENT",*/
62 {0x53, 0x59, 0x4e, 0x2d, 0x53, 0x45, 0x4e,
63  0x54, 0};
64 static const char established[] = /* "ESTABLISHED",*/
65 {0x45, 0x53, 0x54, 0x41, 0x42, 0x4c, 0x49,
66  0x53, 0x48, 0x45, 0x44, 0};
67 static const char fin_wait_1[] = /* "FIN-WAIT-1",*/
68 {0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49,
69  0x54, 0x2d, 0x31, 0};
70 static const char fin_wait_2[] = /* "FIN-WAIT-2",*/
71 {0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49,
72  0x54, 0x2d, 0x32, 0};
73 static const char closing[] = /* "CLOSING",*/
74 {0x43, 0x4c, 0x4f, 0x53, 0x49,
75  0x4e, 0x47, 0};
76 static const char time_wait[] = /* "TIME-WAIT,"*/
77 {0x54, 0x49, 0x4d, 0x45, 0x2d, 0x57, 0x41,
78  0x49, 0x54, 0};
79 static const char last_ack[] = /* "LAST-ACK"*/
80 {0x4c, 0x41, 0x53, 0x54, 0x2d, 0x41, 0x43,
81  0x4b, 0};
82 static const char none[] = /* "NONE"*/
83 {0x4e, 0x4f, 0x4e, 0x45, 0};
84 static const char running[] = /* "RUNNING"*/
85 {0x52, 0x55, 0x4e, 0x4e, 0x49, 0x4e, 0x47,
86  0};
87 static const char called[] = /* "CALLED"*/
88 {0x43, 0x41, 0x4c, 0x4c, 0x45, 0x44, 0};
89 static const char file_name[] = /* "file-stats"*/
90 {0x66, 0x69, 0x6c, 0x65, 0x2d, 0x73, 0x74,
91  0x61, 0x74, 0x73, 0};
92 static const char tcp_name[] = /* "tcp-connections"*/
93 {0x74, 0x63, 0x70, 0x2d, 0x63, 0x6f, 0x6e,
94  0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e,
95  0x73, 0};
96 static const char proc_name[] = /* "processes"*/
97 {0x70, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73,
98  0x65, 0x73, 0};
99 
100 static const char *states[] = {
101  closed,
102  syn_rcvd,
103  syn_sent,
104  established,
105  fin_wait_1,
106  fin_wait_2,
107  closing,
108  time_wait,
109  last_ack,
110  none,
111  running,
112  called};
113 
114 /*---------------------------------------------------------------------------*/
115 static
116 PT_THREAD(nullfunction(struct httpd_state *s, char *ptr))
117 {
118  PSOCK_BEGIN(&s->sout);
119  PSOCK_END(&s->sout);
120 }
121 /*---------------------------------------------------------------------------*/
122 httpd_cgifunction
123 httpd_cgi(char *name)
124 {
125  struct httpd_cgi_call *f;
126 
127  /* Find the matching name in the table, return the function. */
128  for(f = calls; f != NULL; f = f->next) {
129  if(strncmp(f->name, name, strlen(f->name)) == 0) {
130  return f->function;
131  }
132  }
133  return nullfunction;
134 }
135 /*---------------------------------------------------------------------------*/
136 static unsigned short
137 generate_file_stats(void *arg)
138 {
139  char *f = (char *)arg;
140  return snprintf((char *)uip_appdata, uip_mss(), "%5u", httpd_fs_count(f));
141 }
142 /*---------------------------------------------------------------------------*/
143 static
144 PT_THREAD(file_stats(struct httpd_state *s, char *ptr))
145 {
146  PSOCK_BEGIN(&s->sout);
147 
148  PSOCK_GENERATOR_SEND(&s->sout, generate_file_stats, (void *) (strchr(ptr, ' ') + 1));
149 
150  PSOCK_END(&s->sout);
151 }
152 /*---------------------------------------------------------------------------*/
153 static unsigned short
154 make_tcp_stats(void *arg)
155 {
156  struct uip_conn *conn;
157  struct httpd_state *s = (struct httpd_state *)arg;
158  conn = &uip_conns[s->u.count];
159 
160 #if UIP_CONF_IPV6
161  char buf[48];
162  httpd_sprint_ip6(conn->ripaddr, buf);
163  return snprintf((char *)uip_appdata, uip_mss(),
164  "<tr align=\"center\"><td>%d</td><td>%s:%u</td><td>%s</td><td>%u</td><td>%u</td><td>%c %c</td></tr>\r\n",
165  uip_htons(conn->lport),
166  buf,
167  uip_htons(conn->rport),
168  states[conn->tcpstateflags & UIP_TS_MASK],
169  conn->nrtx,
170  conn->timer,
171  (uip_outstanding(conn))? '*':' ',
172  (uip_stopped(conn))? '!':' ');
173 #else
174  return snprintf((char *)uip_appdata, uip_mss(),
175  "<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",
176  uip_htons(conn->lport),
177  conn->ripaddr.u8[0],
178  conn->ripaddr.u8[1],
179  conn->ripaddr.u8[2],
180  conn->ripaddr.u8[3],
181  uip_htons(conn->rport),
182  states[conn->tcpstateflags & UIP_TS_MASK],
183  conn->nrtx,
184  conn->timer,
185  (uip_outstanding(conn))? '*':' ',
186  (uip_stopped(conn))? '!':' ');
187 #endif /* UIP_CONF_IPV6 */
188 }
189 /*---------------------------------------------------------------------------*/
190 static
191 PT_THREAD(tcp_stats(struct httpd_state *s, char *ptr))
192 {
193 
194  PSOCK_BEGIN(&s->sout);
195 
196  for(s->u.count = 0; s->u.count < UIP_CONNS; ++s->u.count) {
197  if((uip_conns[s->u.count].tcpstateflags & UIP_TS_MASK) != UIP_CLOSED) {
198  PSOCK_GENERATOR_SEND(&s->sout, make_tcp_stats, s);
199  }
200  }
201 
202  PSOCK_END(&s->sout);
203 }
204 /*---------------------------------------------------------------------------*/
205 static unsigned short
206 make_processes(void *p)
207 {
208  char name[40];
209 
210  strncpy(name, PROCESS_NAME_STRING((struct process *)p), 40);
211  petsciiconv_toascii(name, 40);
212 
213  return snprintf((char *)uip_appdata, uip_mss(),
214  "<tr align=\"center\"><td>%p</td><td>%s</td><td>%p</td><td>%s</td></tr>\r\n",
215  p, name,
216  *((char **)&(((struct process *)p)->thread)),
217  states[9 + ((struct process *)p)->state]);
218 }
219 /*---------------------------------------------------------------------------*/
220 static
221 PT_THREAD(processes(struct httpd_state *s, char *ptr))
222 {
223  PSOCK_BEGIN(&s->sout);
224  for(s->u.ptr = PROCESS_LIST(); s->u.ptr != NULL; s->u.ptr = ((struct process *)s->u.ptr)->next) {
225  PSOCK_GENERATOR_SEND(&s->sout, make_processes, s->u.ptr);
226  }
227  PSOCK_END(&s->sout);
228 }
229 #if WEBSERVER_CONF_STATUSPAGE && UIP_CONF_IPV6
230 /* These cgi's are invoked by the status.shtml page in /apps/webserver/httpd-fs.
231  * To keep the webserver build small that 160 byte page is not present in the
232  * default httpd-fsdata.c file. Run the PERL script /../../tools/makefsdata from the
233  * /apps/webserver/ directory to include it. Delete status.shtml before running
234  * the script if you want to exclude it again.
235  * NB: Webserver builds on all platforms will use the current httpd-fsdata.c file. The added 160 bytes
236  * could overflow memory on the smaller platforms.
237  */
238 /*---------------------------------------------------------------------------*/
239 #define HTTPD_STRING_ATTR
240 #define httpd_snprintf snprintf
241 #define httpd_cgi_sprint_ip6 httpd_sprint_ip6
242 
243 static const char httpd_cgi_addrh[] HTTPD_STRING_ATTR = "<code>";
244 static const char httpd_cgi_addrf[] HTTPD_STRING_ATTR = "</code>[Room for %u more]";
245 static const char httpd_cgi_addrb[] HTTPD_STRING_ATTR = "<br>";
246 static const char httpd_cgi_addrn[] HTTPD_STRING_ATTR = "(none)<br>";
247 extern uip_ds6_netif_t uip_ds6_if;
248 
249 static unsigned short
250 make_addresses(void *p)
251 {
252 uint8_t i,j=0;
253 uint16_t numprinted;
254  numprinted = httpd_snprintf((char *)uip_appdata, uip_mss(),httpd_cgi_addrh);
255  for (i=0; i<UIP_DS6_ADDR_NB;i++) {
256  if (uip_ds6_if.addr_list[i].isused) {
257  j++;
258  numprinted += httpd_cgi_sprint_ip6(uip_ds6_if.addr_list[i].ipaddr, uip_appdata + numprinted);
259  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrb);
260  }
261  }
262 //if (j==0) numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrn);
263  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrf, UIP_DS6_ADDR_NB-j);
264  return numprinted;
265 }
266 /*---------------------------------------------------------------------------*/
267 static
268 PT_THREAD(addresses(struct httpd_state *s, char *ptr))
269 {
270  PSOCK_BEGIN(&s->sout);
271 
272  PSOCK_GENERATOR_SEND(&s->sout, make_addresses, s->u.ptr);
273 
274  PSOCK_END(&s->sout);
275 }
276 /*---------------------------------------------------------------------------*/
277 static unsigned short
278 make_neighbors(void *p)
279 {
280 uint8_t i,j=0;
281 uint16_t numprinted;
282  numprinted = httpd_snprintf((char *)uip_appdata, uip_mss(),httpd_cgi_addrh);
283  uip_ds6_nbr_t *nbr;
284  for(nbr = nbr_table_head(ds6_neighbors);
285  nbr != NULL;
286  nbr = nbr_table_next(ds6_neighbors, nbr)) {
287  j++;
288  numprinted += httpd_cgi_sprint_ip6(nbr->ipaddr, uip_appdata + numprinted);
289  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrb);
290  }
291 //if (j==0) numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrn);
292  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrf,NBR_TABLE_MAX_NEIGHBORS-j);
293  return numprinted;
294 }
295 /*---------------------------------------------------------------------------*/
296 static
297 PT_THREAD(neighbors(struct httpd_state *s, char *ptr))
298 {
299  PSOCK_BEGIN(&s->sout);
300 
301  PSOCK_GENERATOR_SEND(&s->sout, make_neighbors, s->u.ptr);
302 
303  PSOCK_END(&s->sout);
304 }
305 /*---------------------------------------------------------------------------*/
306 static unsigned short
307 make_routes(void *p)
308 {
309  static const char httpd_cgi_rtes1[] HTTPD_STRING_ATTR = "(%u (via ";
310  static const char httpd_cgi_rtes2[] HTTPD_STRING_ATTR = ") %lus<br>";
311  static const char httpd_cgi_rtes3[] HTTPD_STRING_ATTR = ")<br>";
312  uint8_t i,j=0;
313  uint16_t numprinted;
314  uip_ds6_route_t *r;
315 
316  numprinted = httpd_snprintf((char *)uip_appdata, uip_mss(),httpd_cgi_addrh);
317  for(r = uip_ds6_route_head();
318  r != NULL;
319  r = uip_ds6_route_next(r)) {
320  j++;
321  numprinted += httpd_cgi_sprint_ip6(r->ipaddr, uip_appdata + numprinted);
322  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_rtes1, r->length);
323  numprinted += httpd_cgi_sprint_ip6(uip_ds6_route_nexthop(r), uip_appdata + numprinted);
324  if(r->state.lifetime < 3600) {
325  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_rtes2, r->state.lifetime);
326  } else {
327  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_rtes3);
328  }
329  }
330  if (j==0) numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrn);
331  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrf,UIP_DS6_ROUTE_NB-j);
332  return numprinted;
333 }
334 /*---------------------------------------------------------------------------*/
335 static
336 PT_THREAD(routes(struct httpd_state *s, char *ptr))
337 {
338  PSOCK_BEGIN(&s->sout);
339 
340  PSOCK_GENERATOR_SEND(&s->sout, make_routes, s->u.ptr);
341 
342  PSOCK_END(&s->sout);
343 }
344 #endif /* WEBSERVER_CONF_STATUSPAGE */
345 /*---------------------------------------------------------------------------*/
346 void
347 httpd_cgi_add(struct httpd_cgi_call *c)
348 {
349  struct httpd_cgi_call *l;
350 
351  c->next = NULL;
352  if(calls == NULL) {
353  calls = c;
354  } else {
355  for(l = calls; l->next != NULL; l = l->next);
356  l->next = c;
357  }
358 }
359 /*---------------------------------------------------------------------------*/
360 #if WEBSERVER_CONF_STATUSPAGE && UIP_CONF_IPV6
361 static const char adrs_name[] HTTPD_STRING_ATTR = "addresses";
362 static const char nbrs_name[] HTTPD_STRING_ATTR = "neighbors";
363 static const char rtes_name[] HTTPD_STRING_ATTR = "routes";
364 #endif
365 HTTPD_CGI_CALL(file, file_name, file_stats);
366 HTTPD_CGI_CALL(tcp, tcp_name, tcp_stats);
367 HTTPD_CGI_CALL(proc, proc_name, processes);
368 #if WEBSERVER_CONF_STATUSPAGE && UIP_CONF_IPV6
369 HTTPD_CGI_CALL(adrs, adrs_name, addresses);
370 HTTPD_CGI_CALL(nbrs, nbrs_name, neighbors);
371 HTTPD_CGI_CALL(rtes, rtes_name, routes);
372 #endif
373 
374 void
375 httpd_cgi_init(void)
376 {
377  httpd_cgi_add(&file);
378  httpd_cgi_add(&tcp);
379  httpd_cgi_add(&proc);
380 #if WEBSERVER_CONF_STATUSPAGE && UIP_CONF_IPV6
381  httpd_cgi_add(&adrs);
382  httpd_cgi_add(&nbrs);
383  httpd_cgi_add(&rtes);
384 #endif
385 }
386 /*---------------------------------------------------------------------------*/
#define UIP_CONNS
The maximum number of simultaneously open TCP connections.
Definition: uipopt.h:419
An entry in the routing table.
#define PSOCK_BEGIN(psock)
Start the protosocket protothread in a function.
Definition: psock.h:164
Representation of a uIP TCP connection.
Definition: uip.h:1336
uint8_t nrtx
The number of retransmissions for the last segment sent.
Definition: uip.h:1359
#define uip_mss()
Get the current maximum segment size that can be sent on the current connection.
Definition: uip.h:838
uint16_t lport
The local TCP port, in network byte order.
Definition: uip.h:1339
#define NULL
The null pointer.
uint8_t timer
The retransmission timer.
Definition: uip.h:1358
#define PT_THREAD(name_args)
Declaration of a protothread.
Definition: pt.h:99
uint16_t rport
The local remote TCP port, in network byte order.
Definition: uip.h:1340
PETSCII/ASCII conversion functions.
#define PSOCK_GENERATOR_SEND(psock, generator, arg)
Generate data with a function and send it.
Definition: psock.h:225
CCIF uint16_t uip_htons(uint16_t val)
Convert a 16-bit quantity from host byte order to network byte order.
Definition: uip6.c:2298
#define PSOCK_END(psock)
Declare the end of a protosocket&#39;s protothread.
Definition: psock.h:348
uint8_t tcpstateflags
TCP state and flags.
Definition: uip.h:1357
Interface structure (contains all the interface variables)
Definition: uip-ds6.h:212
#define uip_stopped(conn)
Find out if the current connection has been previously stopped with uip_stop().
Definition: uip.h:700
uip_ipaddr_t ripaddr
The IP address of the remote host.
Definition: uip.h:1337
uip_appdata
Pointer to the application data in the packet buffer.
Definition: tcp_loader.c:74
An entry in the nbr cache.
Definition: uip-ds6-nbr.h:70