summaryrefslogtreecommitdiff
path: root/network/udp.c
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2023-10-27 00:48:21 +0200
committerAnton Kling <anton@kling.gg>2023-10-30 21:49:48 +0100
commit5026f823fa2708404302aa59d03401635a435c0b (patch)
tree03d8db6da25416fa27b9744ae60df2cfa5fc1d2b /network/udp.c
parentf8e15da04472f5ed6a26e588de4a23cb3e1ba20b (diff)
Kernel/Networking/LibC: Add syscalls and libc functions for UDP
This allows a UDP server to be created in userland and read data. Currently it can't send data and is very very simplistic. Code is horrible and probably needs some fixing until it can be further built upon.
Diffstat (limited to 'network/udp.c')
-rw-r--r--network/udp.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/network/udp.c b/network/udp.c
new file mode 100644
index 0000000..fb8ba44
--- /dev/null
+++ b/network/udp.c
@@ -0,0 +1,26 @@
+#include <assert.h>
+#include <network/bytes.h>
+#include <network/udp.h>
+#include <socket.h>
+
+void handle_udp(const uint8_t *payload, uint32_t packet_length) {
+ assert(packet_length >= 8);
+ uint16_t source_port = ntohs(*(uint16_t *)payload);
+ uint16_t dst_port = ntohs(*(uint16_t *)(payload + 2));
+ uint16_t length = ntohs(*(uint16_t *)(payload + 4));
+ assert(length == packet_length);
+ kprintf("source_port: %d\n", source_port);
+ kprintf("dst_port: %d\n", dst_port);
+ uint32_t data_length = length - 8;
+ const uint8_t *data = payload + 8;
+
+ // Find the open port
+ OPEN_INET_SOCKET *in_s = find_open_udp_port(htons(dst_port));
+ assert(in_s);
+ SOCKET *s = in_s->s;
+ vfs_fd_t *fifo_file = s->ptr_socket_fd;
+ raw_vfs_pwrite(fifo_file, (char *)data, data_length, 0);
+ for (uint32_t i = 0; i < data_length; i++) {
+ kprintf("%c", data[i]);
+ }
+}