Hello Genode!
+ I am bold ;-) + + +This is a small test page."; /* HTML page */ + + +/** + * Handle a single client's request. + * + * \param conn socket connected to the client + */ +void http_server_serve(int conn) { + char buf[1024]; + ssize_t buflen; + + /* Read the data from the port, blocking if nothing yet there. + We assume the request (the part we care about) is in one packet */ + buflen = lwip_recv(conn, buf, 1024, 0); + PLOG("Packet received!"); + + /* Ignore all receive errors */ + if (buflen > 0) { + + /* Is this an HTTP GET command? (only check the first 5 chars, since + there are other formats for GET, and we're keeping it very simple)*/ + if (buflen >= 5 && + buf[0] == 'G' && + buf[1] == 'E' && + buf[2] == 'T' && + buf[3] == ' ' && + buf[4] == '/' ) { + + PLOG("Will send response"); + + /* Send http header */ + lwip_send(conn, http_html_hdr, Genode::strlen(http_html_hdr), 0); + + /* Send our HTML page */ + lwip_send(conn, http_index_html, Genode::strlen(http_index_html), 0); + } + } +} + + +int main() +{ + int s; + + lwip_tcpip_init(); + + /* Initialize network stack */ + if (lwip_nic_init(inet_addr("10.0.2.55"), inet_addr("255.255.255.0"), inet_addr("10.0.2.1"))) { + PERR("We got no IP address!"); + return -1; + } + + PLOG("Create new socket ..."); + if((s = lwip_socket(AF_INET, SOCK_STREAM, 0)) < 0) { + PERR("No socket available!"); + return -1; + } + + PLOG("Now, I will bind ..."); + struct sockaddr_in in_addr; + in_addr.sin_family = AF_INET; + in_addr.sin_port = htons(80); + in_addr.sin_addr.s_addr = INADDR_ANY; + if(lwip_bind(s, (struct sockaddr*)&in_addr, sizeof(in_addr))) { + PERR("bind failed!"); + return -1; + } + + PLOG("Now, I will listen ..."); + if(lwip_listen(s, 5)) { + PERR("listen failed!"); + return -1; + } + + PLOG("Start the server loop ..."); + while(true) { + struct sockaddr addr; + socklen_t len = sizeof(addr); + int client = lwip_accept(s, &addr, &len); + if(client < 0) { + PWRN("Invalid socket from accept!"); + continue; + } + http_server_serve(client); + lwip_close(client); + } + return 0; +} diff --git a/libports/src/test/lwip/http_srv_static/target.mk b/libports/src/test/lwip/http_srv_static/target.mk new file mode 100644 index 0000000000..a01a6bd023 --- /dev/null +++ b/libports/src/test/lwip/http_srv_static/target.mk @@ -0,0 +1,5 @@ +TARGET = test-lwip_httpsrv_static +LIBS = cxx env lwip libc libc_log +SRC_CC = main.cc + +INC_DIR += $(REP_DIR)/src/lib/lwip/include