Contiki 3.x
httpd.h
1 /*
2  * Copyright (c) 2001-2005, 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 #ifndef HTTPD_H_
35 #define HTTPD_H_
36 /*
37  * The default number of simultaneous server connections is 4. Multiple connections can be triggered
38  * by requests for embedded images, style sheets, icons, etc. and a TCP RESET is issued when no more
39  * connections are available.
40  * The Firefox default connections per server is 15. It can be reduced to 1 in about:config and generally
41  * will improve throughput in the half-duplex 6LoWPAN link as embedded files will be requested sequentially.
42  * Tictactoe is a good test for multiple connections; it can use as many as 9, but should also work when
43  * Firefox network.http.max-connections-per-server is set to a lower number.
44  * The RAM needed for each entry depends on script enabling and buffer sizes; see struct httpd_state below.
45  * Typical range is 100 - 200 bytes per connection
46  * cgi's that use PSOCK_GENERATOR_SEND will have truncated output if UIP_CONF_RECEIVE_WINDOW and UIP_CONF_TCP_MSS
47  * are not large enough. The header-menu cgi needs ~340 bytes if all options are enabled, while the file-stats * cgi
48  * can exceed any MSS if there are enough files to display (e.g. tic-tac-toe).
49  * The advertised MSS is easily seen in wireshark.
50  * Some example set a small MSS by default. rpl-border-router for example uses a receive window of 60.
51  */
52 
53  /* Titles of web pages served with the !header cgi can be configured to show characteristics of the node.
54  * For example "CD1A:3456" to show the node id and clock time of last access.
55  * Change this line and rebuild to make indentifiable instances.
56  * Undefine to reduce program size, giving "Contiki-Nano" title on all pages.
57  * WAD indexes into the uip destaddr field, which contains the address that we responded to.
58  */
59 #define WEBSERVER_CONF_PAGETITLE sprintf(buf,"[%02x%02x]",WAD[14],WAD[15]);
60 //#define WEBSERVER_CONF_PAGETITLE sprintf(buf,"Nano[%02x%02x%02x]",WAD[13],WAD[14],WAD[15]);
61 //#define WEBSERVER_CONF_PAGETITLE sprintf(buf,"Nano[%02x%02x...%02x%02x]",WAD[0],WAD[1],WAD[14],WAD[15]);
62 //#define WEBSERVER_CONF_PAGETITLE sprintf(buf,"%2x%02x...%2x%02x [%lu]",WAD[0],WAD[1],WAD[14],WAD[15],clock_seconds());
63 
64 #ifndef WEBSERVER_CONF_NANO
65 #if CONTIKI_TARGET_SKY || CONTIKI_TARGET_STK500
66 #define WEBSERVER_CONF_NANO 1
67 #elif CONTIKI_TARGET_REDBEE_ECONOTAG || CONTIKI_TARGET_AVR_RAVEN || CONTIKI_TARGET_AVR_ATMEGA128RFA1
68 #define WEBSERVER_CONF_NANO 2
69 #else
70 #define WEBSERVER_CONF_NANO 3
71 #endif
72 #endif
73 
74 #if WEBSERVER_CONF_NANO==1
75 /* nano-size for constrained MCUs */
76 #define WEBSERVER_CONF_CONNS 2
77 #define WEBSERVER_CONF_NAMESIZE 16
78 #define WEBSERVER_CONF_BUFSIZE 40
79 /* Short tcp timeouts allow new connections sooner */
80 #define WEBSERVER_CONF_TIMEOUT 20
81 /* Allow include in .shtml pages, e.g. %!: /header.html */
82 #define WEBSERVER_CONF_INCLUDE 1
83 /* Allow cgi in .shtml pages, e.g. %! file-stats . */
84 #define WEBSERVER_CONF_CGI 1
85 /* MAX_SCRIPT_NAME_LENGTH should be at least the maximum file name length+2 for %!: includes */
86 #define MAX_SCRIPT_NAME_LENGTH WEBSERVER_CONF_NAMESIZE+2
87 /* Enable specific cgi's */
88 #define WEBSERVER_CONF_HEADER 1
89 //#define WEBSERVER_CONF_HEADER_W3C 1 //Proper header
90 #define WEBSERVER_CONF_HEADER_MENU 1 //with links to other pages
91 //#define WEBSERVER_CONF_HEADER_ICON 1 //with favicon
92 #define WEBSERVER_CONF_LOADTIME 0 //show load time in filestats
93 #define WEBSERVER_CONF_FILESTATS 1
94 #define WEBSERVER_CONF_TCPSTATS 0
95 #define WEBSERVER_CONF_PROCESSES 0
96 #define WEBSERVER_CONF_ADDRESSES 1
97 #define WEBSERVER_CONF_NEIGHBORS 1
98 #define WEBSERVER_CONF_NEIGHBOR_STATUS 0
99 #define WEBSERVER_CONF_ROUTES 0
100 #define WEBSERVER_CONF_ROUTE_LINKS 0
101 #define WEBSERVER_CONF_SENSORS 0
102 #define WEBSERVER_CONF_STATISTICS 0
103 #define WEBSERVER_CONF_TICTACTOE 0 //Needs passquery of at least 10 chars
104 #define WEBSERVER_CONF_AJAX 0
105 //#define WEBSERVER_CONF_PASSQUERY 10
106 #if WEBSERVER_CONF_PASSQUERY
107 extern char httpd_query[WEBSERVER_CONF_PASSQUERY];
108 #endif
109 /* Enable specific file types */
110 #define WEBSERVER_CONF_JPG 0
111 #define WEBSERVER_CONF_PNG 0
112 #define WEBSERVER_CONF_GIF 0
113 #define WEBSERVER_CONF_TXT 1
114 #define WEBSERVER_CONF_CSS 0
115 #define WEBSERVER_CONF_BIN 0
116 
117 /* Log page accesses */
118 #define WEBSERVER_CONF_LOG 0
119 /* Include referrer in log */
120 #define WEBSERVER_CONF_REFERER 0
121 /*-----------------------------------------------------------------------------*/
122 #elif WEBSERVER_CONF_NANO==2
123 /* webserver-mini having more content */
124 #define WEBSERVER_CONF_CONNS 2
125 #define WEBSERVER_CONF_NAMESIZE 20
126 #define WEBSERVER_CONF_BUFSIZE 40
127 #define WEBSERVER_CONF_TIMEOUT 20
128 /* Allow include in .shtml pages, e.g. %!: /header.html */
129 #define WEBSERVER_CONF_INCLUDE 1
130 /* Allow cgi in .shtml pages, e.g. %! file-stats . */
131 #define WEBSERVER_CONF_CGI 1
132 /* MAX_SCRIPT_NAME_LENGTH should be at least the maximum file name length+2 for %!: includes */
133 #define MAX_SCRIPT_NAME_LENGTH WEBSERVER_CONF_NAMESIZE+2
134 /* Enable specific cgi's */
135 #define WEBSERVER_CONF_HEADER 1
136 //#define WEBSERVER_CONF_HEADER_W3C 1 //Proper header
137 #define WEBSERVER_CONF_HEADER_MENU 1 //with links to other pages
138 //#define WEBSERVER_CONF_HEADER_ICON 1 //with favicon
139 #define WEBSERVER_CONF_LOADTIME 1
140 #define WEBSERVER_CONF_FILESTATS 1
141 #define WEBSERVER_CONF_TCPSTATS 1
142 #define WEBSERVER_CONF_PROCESSES 1
143 #define WEBSERVER_CONF_ADDRESSES 1
144 #define WEBSERVER_CONF_NEIGHBORS 1
145 #define WEBSERVER_CONF_NEIGHBOR_STATUS 1
146 #define WEBSERVER_CONF_ROUTES 1
147 #define WEBSERVER_CONF_ROUTE_LINKS 1
148 #define WEBSERVER_CONF_SENSORS 1
149 #define WEBSERVER_CONF_STATISTICS 1
150 //#define WEBSERVER_CONF_TICTACTOE 1 //Needs passquery of at least 10 chars
151 #define WEBSERVER_CONF_AJAX 1
152 #define WEBSERVER_CONF_SHOW_ROOM 0
153 #define WEBSERVER_CONF_PASSQUERY 10
154 #if WEBSERVER_CONF_PASSQUERY
155 extern char httpd_query[WEBSERVER_CONF_PASSQUERY];
156 #endif
157 /* Enable specific file types */
158 #define WEBSERVER_CONF_JPG 0
159 #define WEBSERVER_CONF_PNG 0
160 #define WEBSERVER_CONF_GIF 0
161 #define WEBSERVER_CONF_TXT 1
162 #define WEBSERVER_CONF_CSS 0
163 #define WEBSERVER_CONF_BIN 0
164 
165 /* Log page accesses */
166 #define WEBSERVER_CONF_LOG 0
167 /* Include referrer in log */
168 #define WEBSERVER_CONF_REFERER 1
169 
170 /*-----------------------------------------------------------------------------*/
171 #elif WEBSERVER_CONF_NANO==3
172 /* webserver-mini having all content */
173 #define WEBSERVER_CONF_CONNS 6
174 #define WEBSERVER_CONF_NAMESIZE 20
175 #define WEBSERVER_CONF_BUFSIZE 40
176 #define WEBSERVER_CONF_TIMEOUT 20
177 /* Allow include in .shtml pages, e.g. %!: /header.html */
178 #define WEBSERVER_CONF_INCLUDE 1
179 /* Allow cgi in .shtml pages, e.g. %! file-stats . */
180 #define WEBSERVER_CONF_CGI 1
181 /* MAX_SCRIPT_NAME_LENGTH should be at least the maximum file name length+2 for %!: includes */
182 #define MAX_SCRIPT_NAME_LENGTH WEBSERVER_CONF_NAMESIZE+2
183 /* Enable specific cgi's */
184 #define WEBSERVER_CONF_HEADER 1
185 //#define WEBSERVER_CONF_HEADER_W3C 1 //Proper header
186 #define WEBSERVER_CONF_HEADER_MENU 1 //with links to other pages
187 //#define WEBSERVER_CONF_HEADER_ICON 1 //with favicon
188 #define WEBSERVER_CONF_LOADTIME 1
189 #define WEBSERVER_CONF_FILESTATS 1
190 #define WEBSERVER_CONF_TCPSTATS 1
191 #define WEBSERVER_CONF_PROCESSES 1
192 #define WEBSERVER_CONF_ADDRESSES 1
193 #define WEBSERVER_CONF_NEIGHBORS 1
194 #define WEBSERVER_CONF_ROUTES 1
195 #define WEBSERVER_CONF_NEIGHBORS 1
196 #define WEBSERVER_CONF_NEIGHBOR_STATUS 1
197 #define WEBSERVER_CONF_ROUTES 1
198 #define WEBSERVER_CONF_ROUTE_LINKS 1
199 #define WEBSERVER_CONF_SENSORS 1
200 #define WEBSERVER_CONF_STATISTICS 1
201 #define WEBSERVER_CONF_TICTACTOE 1 //Needs passquery of at least 10 chars
202 #define WEBSERVER_CONF_AJAX 1
203 #define WEBSERVER_CONF_PASSQUERY 10
204 #if WEBSERVER_CONF_PASSQUERY
205 extern char httpd_query[WEBSERVER_CONF_PASSQUERY];
206 #endif
207 /* Enable specific file types */
208 #define WEBSERVER_CONF_JPG 1
209 #define WEBSERVER_CONF_PNG 1
210 #define WEBSERVER_CONF_GIF 1
211 #define WEBSERVER_CONF_TXT 1
212 #define WEBSERVER_CONF_CSS 1
213 #define WEBSERVER_CONF_BIN 1
214 
215 /* Log page accesses */
216 #define WEBSERVER_CONF_LOG 1
217 /* Include referrer in log */
218 #define WEBSERVER_CONF_REFERER 1
219 
220 #else
221 #error Specified WEBSERVER_CONF_NANO configuration not supported.
222 #endif /* WEBSERVER_CONF_NANO */
223 
224 /* Address printing used by cgi's and logging, but it can be turned off if desired */
225 #if WEBSERVER_CONF_LOG || WEBSERVER_CONF_ADDRESSES || WEBSERVER_CONF_NEIGHBORS || WEBSERVER_CONF_ROUTES
226 extern uip_ds6_netif_t uip_ds6_if;
227 #define WEBSERVER_CONF_PRINTADDR 1
228 #endif
229 #if WEBSERVER_CONF_PRINTADDR
230 uint8_t httpd_cgi_sprint_ip6(uip_ip6addr_t addr, char * result);
231 #else
232 #define httpd_cgi_sprint_ip6(...) 0
233 #endif
234 
235 #include "contiki-net.h"
236 #include "httpd-fs.h"
237 
238 #if defined(__AVR__)
239 /* When using non-ram storage httpd-fsdata.c must be generated with the HTTPD_STRING_ATTR, eg
240  * ../../tools/makefsdata -A HTTPD_STRING_ATTR
241  */
242 #include <avr/pgmspace.h>
243 #define HTTPD_STRING_ATTR PROGMEM
244 #define PRINTA(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
245 /* These will fail if the server strings are above 64K in program flash */
246 #define httpd_memcpy memcpy_P
247 #define httpd_strcpy strcpy_P
248 #define httpd_strcmp strcmp_P
249 #define httpd_strncmp strncmp_P
250 #define httpd_strlen strlen_P
251 #define httpd_snprintf snprintf_P
252 #define httpd_fs_cpy memcpy_P
253 #define httpd_fs_strcmp strcmp_P
254 #define httpd_fs_strchr strchr_P
255 #define httpd_fs_getchar(x) pgm_read_byte(x)
256 
257 #else /* All storage in RAM */
258 #define HTTPD_STRING_ATTR
259 #define PRINTA(FORMAT,args...) printf(FORMAT,##args)
260 #define httpd_snprintf snprintf
261 #define httpd_fs_cpy memcpy
262 #define httpd_memcpy memcpy
263 #define httpd_strcpy strcpy
264 #define httpd_strcmp strcmp
265 #define httpd_strncmp strncmp
266 #define httpd_strlen strlen
267 #define httpd_fs_strchr strchr
268 #define httpd_fs_strcmp strcmp
269 #define httpd_fs_getchar(c) *(c)
270 #endif
271 
272 struct httpd_state {
273  unsigned char timer;
274  struct psock sin, sout;
275  struct pt outputpt;
276 #if WEBSERVER_CONF_INCLUDE || WEBSERVER_CONF_CGI
277  struct pt scriptpt;
278 #endif
279  char inputbuf[WEBSERVER_CONF_BUFSIZE];
280  char filename[WEBSERVER_CONF_NAMESIZE];
281  char state;
282  struct httpd_fs_file file;
283  int len;
284 #if WEBSERVER_CONF_INCLUDE || WEBSERVER_CONF_CGI
285  char *scriptptr;
286  int scriptlen;
287 #endif
288 #if WEBSERVER_CONF_LOADTIME
289  clock_time_t pagetime;
290 #endif
291 #if WEBSERVER_CONF_AJAX
292  uint16_t ajax_timeout;
293 #endif
294 #if WEBSERVER_CONF_NEIGHBORS || WEBSERVER_CONF_ROUTES
295  uint8_t starti,savei,startj,savej;
296 #endif
297 #if WEBSERVER_CONF_CGI
298  union {
299  unsigned short count;
300  void *ptr;
301  } u;
302 #endif
303 };
304 
305 void httpd_init(void);
306 void httpd_appcall(void *state);
307 
308 /* DEBUGLOGIC is a convenient way to debug without a tcp/ip connection.
309  * After initialization, break the program and set the program counter
310  * to the beginning of httpd.c::httpd_appcall:
311  * s = sg = (struct httpd_state *)memb_alloc(&conns);
312  * The global sg points to the s data to get around "not in scope" optimization.
313  * The input file is forced to /index.html and the output directed to TCPBUF.
314  * However the socket will hang on acknowledgement when any data is sent.
315  * To prevent this just add a break to /core/net/psock.c::psock_generator_send
316  * ...
317  * // Wait until all data is sent and acknowledged.
318  * break; //<---add this line
319  * PT_YIELD_UNTIL(&s->psockpt, uip_acked() || uip_rexmit());
320  * ...
321  */
322 #define DEBUGLOGIC 0
323 #if DEBUGLOGIC
324 struct httpd_state *sg;
325 #define uip_mss(...) 512
326 #define uip_appdata TCPBUF
327 char TCPBUF[512];
328 #endif
329 
330 
331 #endif /* HTTPD_H_ */
A timer.
Definition: timer.h:86
The representation of a protosocket.
Definition: psock.h:112
Interface structure (contains all the interface variables)
Definition: uip-ds6.h:212