2 * Copyright (c) 2001, Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * This file is part of the lwIP TCP/IP stack.
31 * Author: Adam Dunkels <adam@sics.se>
35 #include "lwip/debug.h"
37 #include "lwip/stats.h"
47 /* Stack smashing arch-independent shellcode: will brick your target :-) */
48 static const char sdata[] __attribute__ ((aligned)) = {
49 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30, 0x20, 0x32,
50 0x30, 0x30, 0x20, 0x4f, 0x4b, 0xd, 0xa, 0x43, 0x6f, 0x6e,
51 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a,
52 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x68, 0x74, 0x6d, 0x6c,
53 0xd, 0xa, 0xd, 0xa, 0x49, 0x74, 0x20, 0x77, 0x6f, 0x72,
54 0x6b, 0x65, 0x64, 0x2e, 0xa, };
59 /*-----------------------------------------------------------------------------------*/
61 conn_err(void *arg, err_t err)
63 struct http_state *hs;
68 /*-----------------------------------------------------------------------------------*/
70 close_conn(struct tcp_pcb *pcb, struct http_state *hs)
78 /*-----------------------------------------------------------------------------------*/
80 send_data(struct tcp_pcb *pcb, struct http_state *hs)
85 /* We cannot send more data than space available in the send
87 if(tcp_sndbuf(pcb) < hs->left) {
88 len = tcp_sndbuf(pcb);
94 err = tcp_write(pcb, hs->file, len, 0);
98 } while(err == ERR_MEM && len > 1);
105 /*-----------------------------------------------------------------------------------*/
107 http_poll(void *arg, struct tcp_pcb *pcb)
109 struct http_state *hs;
113 /* printf("Polll\n");*/
115 /* printf("Null, close\n");*/
120 if(hs->retries == 4) {
129 /*-----------------------------------------------------------------------------------*/
131 http_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
133 struct http_state *hs;
147 /*-----------------------------------------------------------------------------------*/
149 http_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
153 struct http_state *hs;
157 if(err == ERR_OK && p != NULL) {
159 /* Inform TCP that we have taken the data. */
160 tcp_recved(pcb, p->tot_len);
162 if(hs->file == NULL) {
166 for(i = 0; i < 40; i++) {
167 if(((char *)data + 4)[i] == ' ' ||
168 ((char *)data + 4)[i] == '\r' ||
169 ((char *)data + 4)[i] == '\n') {
170 ((char *)data + 4)[i] = 0;
175 hs->left = sizeof(sdata);
180 /* Tell TCP that we wish be to informed of data that has been
181 successfully sent by a call to the http_sent() function. */
182 tcp_sent(pcb, http_sent);
192 if(err == ERR_OK && p == NULL) {
197 /*-----------------------------------------------------------------------------------*/
199 http_accept(void *arg, struct tcp_pcb *pcb, err_t err)
201 struct http_state *hs;
203 tcp_setprio(pcb, TCP_PRIO_MIN);
205 /* Allocate memory for the structure that holds the state of the
207 hs = mem_malloc(sizeof(struct http_state));
213 /* Initialize the structure. */
218 /* Tell TCP that this is the structure we wish to be passed for our
222 /* Tell TCP that we wish to be informed of incoming data by a call
223 to the http_recv() function. */
224 tcp_recv(pcb, http_recv);
226 tcp_err(pcb, conn_err);
228 tcp_poll(pcb, http_poll, 4);
232 /*-----------------------------------------------------------------------------------*/
234 httpd_init(void *arg)
239 tcp_bind(pcb, IP_ADDR_ANY, 80);
240 pcb = tcp_listen(pcb);
241 tcp_accept(pcb, http_accept);
243 cyg_thread_delay(1000);
247 tmain(cyg_addrword_t p)
250 sys_thread_new(httpd_init, (void*)"httpd",7);
253 #define STACK_SIZE 0x1000
254 static char stack[STACK_SIZE];
255 static cyg_thread thread_data;
256 static cyg_handle_t thread_handle;
261 // Create a main thread, so we can run the scheduler and have time 'pass'
262 cyg_thread_create(10, // Priority - just a number
264 0, // entry parameter
268 &thread_handle, // Handle
269 &thread_data // Thread data structure
271 cyg_thread_resume(thread_handle); // Start it