diff options
author | Anton Kling <anton@kling.gg> | 2023-10-27 22:17:24 +0200 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2023-10-30 21:49:48 +0100 |
commit | d50d18c9da3a125f0196bec89802dec1c5b0b800 (patch) | |
tree | d9a4e74906c5ab51708d148ad3728cc155a9893d /network/udp.c | |
parent | 4f9ed7087cb58683d9423ab771ad76b31dac5514 (diff) |
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.
Diffstat (limited to 'network/udp.c')
-rw-r--r-- | network/udp.c | 19 |
1 files changed, 17 insertions, 2 deletions
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 <assert.h> #include <network/bytes.h> +#include <network/ipv4.h> #include <network/udp.h> #include <socket.h> +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; |