From d50d18c9da3a125f0196bec89802dec1c5b0b800 Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Fri, 27 Oct 2023 22:17:24 +0200 Subject: Kernel/LibC/Networking: Be able to send UDP messages Now it can send UDP messages to a specific IP address and libc has enough to create a basic UDP ECHO server, that is kinda cool. --- network/udp.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'network/udp.c') diff --git a/network/udp.c b/network/udp.c index 29f17e1..23411da 100644 --- a/network/udp.c +++ b/network/udp.c @@ -1,8 +1,24 @@ #include #include +#include #include #include +void send_udp_packet(struct sockaddr_in *src, const struct sockaddr_in *dst, + const uint8_t *payload, uint16_t payload_length) { + uint16_t header[4] = {0}; + header[0] = src->sin_port; + header[1] = dst->sin_port; + header[2] = htons(payload_length + 8); + + uint16_t packet_length = sizeof(header) + payload_length; + uint8_t *packet = kmalloc(packet_length); + memcpy(packet, header, sizeof(header)); + memcpy(packet + sizeof(header), payload, payload_length); + send_ipv4_packet(dst->sin_addr.s_addr, 0x11, packet, packet_length); + kfree(packet); +} + void handle_udp(uint8_t src_ip[4], const uint8_t *payload, uint32_t packet_length) { assert(packet_length >= 8); @@ -10,11 +26,10 @@ void handle_udp(uint8_t src_ip[4], const uint8_t *payload, // h_.* means host format((probably) little endian) uint16_t n_source_port = *(uint16_t *)payload; uint16_t h_source_port = ntohs(n_source_port); + (void)h_source_port; uint16_t h_dst_port = ntohs(*(uint16_t *)(payload + 2)); uint16_t h_length = ntohs(*(uint16_t *)(payload + 4)); assert(h_length == packet_length); - kprintf("source_port: %d\n", h_source_port); - kprintf("dst_port: %d\n", h_dst_port); uint16_t data_length = h_length - 8; const uint8_t *data = payload + 8; -- cgit v1.2.3