From: Joshua Wise <joshua@rebirth.joshuawise.com> Date: Tue, 11 Nov 2008 04:35:23 +0000 (-0500) Subject: Add httpd. X-Git-Url: http://git.joshuawise.com/netwatch.git/commitdiff_plain/6f9272bdabcda81425ec1aaecf26a160535d4749 Add httpd. --- diff --git a/aseg-paging/Makefile b/aseg-paging/Makefile index a8d72cd..2a50935 100644 --- a/aseg-paging/Makefile +++ b/aseg-paging/Makefile @@ -40,6 +40,8 @@ OBJS = ../ich2/smi.o \ ../pci/pci-raw.o \ ../pci/pci-bother.o \ ../net/net.o \ + ../net/http/fs.o \ + ../net/http/httpd.o \ ../net/3c90x.o \ ../lib/minilib.o \ ../lib/doprnt.o \ diff --git a/include/minilib.h b/include/minilib.h index ef4f302..7d66efb 100644 --- a/include/minilib.h +++ b/include/minilib.h @@ -8,6 +8,7 @@ extern void memset(void *dest, int data, int bytes); extern void memmove(void *dest, void *src, int bytes); extern int memcmp(const char *a2, const char *a1, int bytes); extern int strcmp(const char *a2, const char *a1); +extern int strncmp(const char *a2, const char *a1, int n); extern int strlen(const char *c); extern void strcpy(char *a2, const char *a1); extern void puts(const char *c); diff --git a/lib/minilib.c b/lib/minilib.c index c1d36b6..e90f7af 100644 --- a/lib/minilib.c +++ b/lib/minilib.c @@ -50,6 +50,16 @@ int strcmp (const char *a2, const char *a1) { } } +int strncmp (const char *a2, const char *a1, int n) { + while (n--) { + if (*a2 != *a1) return 1; + if (*a2 == 0) return 0; + a1++; + a2++; + } + return 0; +} + int strlen(const char *c) { int l = 0; diff --git a/lwip/src/include/lwipopts.h b/lwip/src/include/lwipopts.h index 4ad4440..c2ef625 100644 --- a/lwip/src/include/lwipopts.h +++ b/lwip/src/include/lwipopts.h @@ -11,40 +11,7 @@ #define LWIP_DHCP 1 -#undef LWIP_DEBUG - -#define ETHARP_DEBUG LWIP_DBG_ON -#define NETIF_DEBUG LWIP_DBG_ON -#define PBUF_DEBUG LWIP_DBG_ON -#define API_LIB_DEBUG LWIP_DBG_OFF -#define API_MSG_DEBUG LWIP_DBG_OFF -#define SOCKETS_DEBUG LWIP_DBG_OFF -#define ICMP_DEBUG LWIP_DBG_OFF -#define IGMP_DEBUG LWIP_DBG_OFF -#define INET_DEBUG LWIP_DBG_OFF -#define IP_DEBUG LWIP_DBG_ON -#define IP_REASS_DEBUG LWIP_DBG_OFF -#define RAW_DEBUG LWIP_DBG_OFF -#define MEM_DEBUG LWIP_DBG_ON -#define MEMP_DEBUG LWIP_DBG_ON -#define SYS_DEBUG LWIP_DBG_OFF -#define TCP_DEBUG LWIP_DBG_ON -#define TCP_INPUT_DEBUG LWIP_DBG_ON -#define TCP_FR_DEBUG LWIP_DBG_OFF -#define TCP_RTO_DEBUG LWIP_DBG_OFF -#define TCP_CWND_DEBUG LWIP_DBG_OFF -#define TCP_WND_DEBUG LWIP_DBG_OFF -#define TCP_OUTPUT_DEBUG LWIP_DBG_OFF -#define TCP_RST_DEBUG LWIP_DBG_OFF -#define TCP_QLEN_DEBUG LWIP_DBG_OFF -#define UDP_DEBUG LWIP_DBG_OFF -#define TCPIP_DEBUG LWIP_DBG_OFF -#define PPP_DEBUG LWIP_DBG_OFF -#define SLIP_DEBUG LWIP_DBG_OFF -#define DHCP_DEBUG LWIP_DBG_OFF -#define AUTOIP_DEBUG LWIP_DBG_OFF -#define SNMP_MSG_DEBUG LWIP_DBG_OFF -#define SNMP_MIB_DEBUG LWIP_DBG_OFF -#define DNS_DEBUG LWIP_DBG_OFF +#define MEM_SIZE 65536 +#define TCP_SND_BUF 2048 #endif diff --git a/net/http/fs.c b/net/http/fs.c new file mode 100644 index 0000000..9e6358d --- /dev/null +++ b/net/http/fs.c @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2001-2003 Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * This file is part of the lwIP TCP/IP stack. + * + * Author: Adam Dunkels <adam@sics.se> + * + */ +#include "lwip/def.h" +#include "fs.h" +#include "fsdata.h" +#include "fsdata.c" + +/*-----------------------------------------------------------------------------------*/ + +void fill_regs(struct fs_file *file) +{ + static unsigned char buf[2048]; + + sprintf(buf, + "<html><head><title>Registers</title></head><body>" + "<p>At the time you requested this page, the system's registers were:</p>" + "<tt><pre>" + "%%eax: 0x%08x %%ebx: 0x%08x %%ecx: 0x%08x %%edx: 0x%08x\n" + "%%ebp: 0x%08x %%esi: 0x%08x %%edi: 0x%08x %%esp: 0x%08x\n" + "%%cr0: 0x%08x %%cr3: 0x%08x %%eip: 0x%08x %%eflags: 0x%08x\n" + "</pre></tt></body></html>", + *(unsigned long*)0xAFFD0, + *(unsigned long*)0xAFFDC, + *(unsigned long*)0xAFFD4, + *(unsigned long*)0xAFFD8, + *(unsigned long*)0xAFFE4, + *(unsigned long*)0xAFFE8, + *(unsigned long*)0xAFFEC, + *(unsigned long*)0xAFFE0, + *(unsigned long*)0xAFFFC, + *(unsigned long*)0xAFFF8, + *(unsigned long*)0xAFFF0, + *(unsigned long*)0xAFFF4); + + + file->data = buf; + file->len = strlen(buf); +} + + +/*-----------------------------------------------------------------------------------*/ +int +fs_open(const char *name, struct fs_file *file) +{ + const struct fsdata_file *f; + + /* /registers.html is CGI */ + if (!strcmp(name, "/registers.html")) + { + fill_regs(file); + return 1; + } + + for(f = FS_ROOT; + f != NULL; + f = f->next) { + if (!strcmp(name, (const char*)f->name)) { + file->data = f->data; + file->len = f->len; + return 1; + } + } + file->data = "You clown..."; + file->len = 9; + return 0; +} +/*-----------------------------------------------------------------------------------*/ diff --git a/net/http/fs.h b/net/http/fs.h new file mode 100644 index 0000000..1f7d434 --- /dev/null +++ b/net/http/fs.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2001-2003 Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * This file is part of the lwIP TCP/IP stack. + * + * Author: Adam Dunkels <adam@sics.se> + * + */ +#ifndef __FS_H__ +#define __FS_H__ + +struct fs_file { + const unsigned char *data; + int len; +}; + +/* file must be allocated by caller and will be filled in + by the function. */ +int fs_open(const char *name, struct fs_file *file); + +#endif /* __FS_H__ */ diff --git a/net/http/fsdata.c b/net/http/fsdata.c new file mode 100644 index 0000000..7195a70 --- /dev/null +++ b/net/http/fsdata.c @@ -0,0 +1,14 @@ +static const unsigned char data_404_html[] = + "<html><head><title>404 Not Found</title></head>" + "<body><h1>404 Not Found</h1><p>This is probably not the machine you're looking for...</p></body>" + "</html>"; + +static const unsigned char data_index_html[] = + "<html><head><title>NetWatch!</title></head>" + "<body><h1>NetWatch!</h1><iframe src=\"registers.html\" height=300></body>" + "</html>"; + +const struct fsdata_file file_404_html[] = {{NULL, "/404.html", data_404_html, sizeof(data_404_html)}}; +const struct fsdata_file file_index_html[] = {{file_404_html, "/index.html", data_index_html, sizeof(data_index_html)}}; + +#define FS_ROOT file_index_html diff --git a/net/http/fsdata.h b/net/http/fsdata.h new file mode 100644 index 0000000..a6c5262 --- /dev/null +++ b/net/http/fsdata.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2001-2003 Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * This file is part of the lwIP TCP/IP stack. + * + * Author: Adam Dunkels <adam@sics.se> + * + */ +#ifndef __FSDATA_H__ +#define __FSDATA_H__ + +struct fsdata_file { + const struct fsdata_file *next; + const unsigned char *name; + const unsigned char *data; + const int len; +}; + +#endif /* __FSDATA_H__ */ diff --git a/net/http/httpd.c b/net/http/httpd.c new file mode 100644 index 0000000..e8cdd53 --- /dev/null +++ b/net/http/httpd.c @@ -0,0 +1,258 @@ +/* + * Copyright (c) 2001-2003 Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * This file is part of the lwIP TCP/IP stack. + * + * Author: Adam Dunkels <adam@sics.se> + * + */ + +#include <minilib.h> +#include <output.h> +#include "lwip/debug.h" + +#include "lwip/stats.h" + +#include "httpd.h" + +#include "lwip/tcp.h" + +#include "fs.h" + +struct http_state { + u32_t left; + const unsigned char *file; + u8_t retries; +}; + +/*-----------------------------------------------------------------------------------*/ +static void +conn_err(void *arg, err_t err) +{ + struct http_state *hs; + + LWIP_UNUSED_ARG(err); + + hs = arg; + mem_free(hs); +} +/*-----------------------------------------------------------------------------------*/ +static void +close_conn(struct tcp_pcb *pcb, struct http_state *hs) +{ + tcp_arg(pcb, NULL); + tcp_sent(pcb, NULL); + tcp_recv(pcb, NULL); + mem_free(hs); + tcp_close(pcb); +} +/*-----------------------------------------------------------------------------------*/ +static void +send_data(struct tcp_pcb *pcb, struct http_state *hs) +{ + err_t err; + u16_t len; + + /* We cannot send more data than space available in the send + buffer. */ + if (tcp_sndbuf(pcb) < hs->left) { + len = tcp_sndbuf(pcb); + } else { + len = hs->left; + LWIP_ASSERT((len == hs->left), "hs->left did not fit into u16_t!"); + } + + do { + err = tcp_write(pcb, hs->file, len, 0); + if (err == ERR_MEM) { + outputf("Insufficient memory to send %d", len); + len /= 2; + } + } while (err == ERR_MEM && len > 1); + + if (err == ERR_OK) { + hs->file += len; + hs->left -= len; + } else { + outputf("send_data: error %s len %d %d\n", lwip_strerr(err), len, tcp_sndbuf(pcb)); + } +} +/*-----------------------------------------------------------------------------------*/ +static err_t +http_poll(void *arg, struct tcp_pcb *pcb) +{ + struct http_state *hs; + + hs = arg; + + /* printf("Polll\n");*/ + if (hs == NULL) { + /* printf("Null, close\n");*/ + tcp_abort(pcb); + return ERR_ABRT; + } else { + ++hs->retries; + if (hs->retries == 4) { + tcp_abort(pcb); + return ERR_ABRT; + } + send_data(pcb, hs); + } + + return ERR_OK; +} +/*-----------------------------------------------------------------------------------*/ +static err_t +http_sent(void *arg, struct tcp_pcb *pcb, u16_t len) +{ + struct http_state *hs; + + LWIP_UNUSED_ARG(len); + + hs = arg; + + hs->retries = 0; + + if (hs->left > 0) { + send_data(pcb, hs); + } else { + close_conn(pcb, hs); + } + + return ERR_OK; +} +/*-----------------------------------------------------------------------------------*/ +static err_t +http_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) +{ + int i; + char *data; + struct fs_file file; + struct http_state *hs; + + hs = arg; + + if (err == ERR_OK && p != NULL) { + + /* Inform TCP that we have taken the data. */ + tcp_recved(pcb, p->tot_len); + + if (hs->file == NULL) { + data = p->payload; + + if (strncmp(data, "GET ", 4) == 0) { + for(i = 0; i < 40; i++) { + if (((char *)data + 4)[i] == ' ' || + ((char *)data + 4)[i] == '\r' || + ((char *)data + 4)[i] == '\n') { + ((char *)data + 4)[i] = 0; + } + } + + outputf("httpd: serving %s", (char*)data+4); + + if (*(char *)(data + 4) == '/' && + *(char *)(data + 5) == 0) { + fs_open("/index.html", &file); + } else if (!fs_open((char *)data + 4, &file)) { + fs_open("/404.html", &file); + } + + hs->file = file.data; + LWIP_ASSERT((file.len >= 0), "File length must be positive!"); + hs->left = file.len; + + pbuf_free(p); + send_data(pcb, hs); + + /* Tell TCP that we wish be to informed of data that has been + successfully sent by a call to the http_sent() function. */ + tcp_sent(pcb, http_sent); + } else { + pbuf_free(p); + close_conn(pcb, hs); + } + } else { + pbuf_free(p); + } + } + + if (err == ERR_OK && p == NULL) { + close_conn(pcb, hs); + } + return ERR_OK; +} +/*-----------------------------------------------------------------------------------*/ +static err_t +http_accept(void *arg, struct tcp_pcb *pcb, err_t err) +{ + struct http_state *hs; + + LWIP_UNUSED_ARG(arg); + LWIP_UNUSED_ARG(err); + + tcp_setprio(pcb, TCP_PRIO_MIN); + + /* Allocate memory for the structure that holds the state of the + connection. */ + hs = (struct http_state *)mem_malloc(sizeof(struct http_state)); + + if (hs == NULL) { + outputf("http_accept: Out of memory\n"); + return ERR_MEM; + } + + /* Initialize the structure. */ + hs->file = NULL; + hs->left = 0; + hs->retries = 0; + + /* Tell TCP that this is the structure we wish to be passed for our + callbacks. */ + tcp_arg(pcb, hs); + + /* Tell TCP that we wish to be informed of incoming data by a call + to the http_recv() function. */ + tcp_recv(pcb, http_recv); + + tcp_err(pcb, conn_err); + + tcp_poll(pcb, http_poll, 4); + return ERR_OK; +} +/*-----------------------------------------------------------------------------------*/ +void +httpd_init(void) +{ + struct tcp_pcb *pcb; + + pcb = tcp_new(); + tcp_bind(pcb, IP_ADDR_ANY, 80); + pcb = tcp_listen(pcb); + tcp_accept(pcb, http_accept); +} +/*-----------------------------------------------------------------------------------*/ + diff --git a/net/http/httpd.h b/net/http/httpd.h new file mode 100644 index 0000000..4602fd4 --- /dev/null +++ b/net/http/httpd.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2001-2003 Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * This file is part of the lwIP TCP/IP stack. + * + * Author: Adam Dunkels <adam@sics.se> + * + */ +#ifndef __HTTPD_H__ +#define __HTTPD_H__ + +void httpd_init(void); + +#endif /* __HTTPD_H__ */ diff --git a/net/net.c b/net/net.c index f920dbc..8390f04 100644 --- a/net/net.c +++ b/net/net.c @@ -154,8 +154,11 @@ int eth_register(struct nic *nic) void eth_init() { + extern void httpd_init(); + /* Required for DMA to work. :( */ smram_tseg_set_state(SMRAM_TSEG_OPEN); lwip_init(); pci_probe_driver(a3c90x_driver); + httpd_init(); }