aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-11-19 18:53:48 +0100
committerMarvin Borner2020-11-19 18:53:48 +0100
commitd8036aa78139c7890d8adde6ee937929623dbffb (patch)
tree306420b0c89769052cd31297de489884584797f1
parenteafd1550ec8fe8887674e9be79f9704bf3a39a8a (diff)
Reimplemented basic webserver
-rw-r--r--apps/Makefile2
-rw-r--r--apps/server.c28
-rw-r--r--kernel/features/net.c26
3 files changed, 41 insertions, 15 deletions
diff --git a/apps/Makefile b/apps/Makefile
index ea212d9..8f0e150 100644
--- a/apps/Makefile
+++ b/apps/Makefile
@@ -1,6 +1,6 @@
# MIT License, Copyright (c) 2020 Marvin Borner
-COBJS = init.o wm.o mandelbrot.o window.o exec.o files.o test.o cc.o browser.o
+COBJS = init.o wm.o mandelbrot.o window.o exec.o files.o test.o cc.o browser.o server.o
CC = ccache ../cross/opt/bin/i686-elf-gcc
LD = ccache ../cross/opt/bin/i686-elf-ld
OC = ccache ../cross/opt/bin/i686-elf-objcopy
diff --git a/apps/server.c b/apps/server.c
new file mode 100644
index 0000000..646e876
--- /dev/null
+++ b/apps/server.c
@@ -0,0 +1,28 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#include <assert.h>
+#include <net.h>
+#include <print.h>
+#include <str.h>
+
+#define PORT 8000
+#define RESP "HTTP/1.1 200\r\nContent-Length: 14\r\nConnection: close\r\n\r\n<h1>Hallo</h1>"
+
+int main()
+{
+ printf("Server running on port %d\n", PORT);
+
+ while (1) {
+ struct socket *socket = net_open(S_TCP);
+ assert(socket);
+ socket->src_port = PORT;
+ socket->state = S_CONNECTED;
+ char buf[4096] = { 0 };
+ net_receive(socket, buf, 4096);
+ printf("%s\n", buf);
+ net_send(socket, strdup(RESP), strlen(RESP));
+ /* net_close(socket); // TODO: Fix */
+ }
+
+ return 0;
+}
diff --git a/kernel/features/net.c b/kernel/features/net.c
index b58c1da..257d52b 100644
--- a/kernel/features/net.c
+++ b/kernel/features/net.c
@@ -405,8 +405,6 @@ static void dhcp_handle_packet(struct dhcp_packet *packet)
}
// enum tcp_state { TCP_LISTEN, TCP_SYN_SENT, TCP_SYN_RECIEVED, TCP_ESTABLISHED, TCP_FIN_WAIT_1, TCP_FIN_WAIT_2, TCP_CLOSE_WAIT, TCP_CLOSING, TCP_LAST_ACK, TCP_TIME_WAIT, TCP_CLOSED };
-#define http_res "HTTP/1.1 200\r\nContent-Length: 14\r\nConnection: close\r\n\r\n<h1>Hallo</h1>"
-#define http_req "GET / HTTP/1.1\r\nHost: google.de\r\n\r\n"
static void tcp_handle_packet(struct tcp_packet *packet, u32 dst, int len)
{
printf("TCP Port: %d\n", ntohs(packet->dst_port));
@@ -429,7 +427,7 @@ static void tcp_handle_packet(struct tcp_packet *packet, u32 dst, int len)
// Serve
if (tcp->state == 0 && (flags & 0xff) == TCP_FLAG_SYN) {
- socket->ip_addr = dst;
+ socket->ip_addr = ntohl(dst);
socket->dst_port = ntohs(packet->src_port);
tcp->ack_no = recv_seq + 1;
tcp->seq_no = 1000;
@@ -437,24 +435,24 @@ static void tcp_handle_packet(struct tcp_packet *packet, u32 dst, int len)
tcp->state++;
return;
} else if (tcp->state == 1 && (flags & 0xff) == TCP_FLAG_ACK) {
- /* assert(recv_ack == seq_no + 1); */
-
tcp->state++;
return;
} else if (tcp->state == 2 && (flags & 0xff) == (TCP_FLAG_ACK | TCP_FLAG_PSH)) {
- /* assert(recv_ack == seq_no + 1); */
-
- /* for (u32 i = 0; i < data_length; ++i) { */
- /* if (packet->data[i]) */
- /* printf("%c", packet->data[i]); */
- /* } */
+ struct socket_data *sdata = malloc(sizeof(*sdata));
+ sdata->length = data_length;
+ if (sdata->length) {
+ sdata->data = malloc(data_length);
+ memcpy(sdata->data, packet->data, data_length);
+ } else {
+ sdata->data = NULL;
+ }
+ list_add(socket->packets, sdata);
+ proc_from_pid(socket->pid)->state = PROC_RUNNING;
tcp->ack_no += data_length;
tcp->seq_no++;
tcp_send_packet(socket, TCP_FLAG_ACK, NULL, 0);
- /* tcp_send_packet(socket, TCP_FLAG_PSH | TCP_FLAG_ACK, strdup(http_res), */
- /* strlen(http_res)); */
socket->state = S_CONNECTED;
tcp->state++;
@@ -463,7 +461,7 @@ static void tcp_handle_packet(struct tcp_packet *packet, u32 dst, int len)
tcp->ack_no = recv_seq + 1;
tcp->seq_no = recv_ack;
- /* tcp->state++; */
+ tcp->state++; // ?
return;
} else if (tcp->state == 4 && (flags & 0xff) == (TCP_FLAG_ACK | TCP_FLAG_FIN)) {
tcp->ack_no = recv_seq + 1;