SayoriOS 0.3.3

udp.c
1 #include "net/udp.h"
2 #include "mem/vmm.h"
3 #include "net/endianess.h"
4 #include "io/ports.h"
5 #include "net/ipv4.h"
6 #include "debug/hexview.h"
7 #include "net/dhcp.h"
8
9 uint16_t udp_calculate_checksum(udp_packet_t * packet) {
10 // UDP checksum is optional in IPv4
11 return 0;
12 }
13
14 void udp_send_packet(netcard_entry_t* card, uint8_t * dst_ip, uint16_t src_port, uint16_t dst_port, void * data, int len) {
15 size_t length = sizeof(udp_packet_t) + len;
16
17 udp_packet_t* packet = kcalloc(1, length);
18
19 packet->src_port = htons(src_port);
20 packet->dst_port = htons(dst_port);
21 packet->length = htons(length);
22 packet->checksum = udp_calculate_checksum(packet);
23
24 memcpy((char*)packet + sizeof(udp_packet_t), data, len);
25
26 qemu_log("UDP Packet sent");
27 ipv4_send_packet(card, dst_ip, packet, length, IP_PROTOCOL_UDP);
28 }
29
30 void udp_handle_packet(netcard_entry_t *card, udp_packet_t *packet) {
31 uint16_t src_port = ntohs(packet->src_port);
32 uint16_t dst_port = ntohs(packet->dst_port);
33 uint16_t length = ntohs(packet->length);
34
35 void* data_ptr = (char*)packet + sizeof(udp_packet_t);
36
37 qemu_log("UDP: Source port: %d; Destination port: %d; Length: %d", src_port, dst_port, length);
38
39 // hexview_advanced(data_ptr, data_len, 16, true, new_qemu_printf);
40
41 if(dst_port == 68) {
42 dhcp_handle_packet(card, data_ptr);
43 }
44 }
memcpy
void * memcpy(void *restrict destination, const void *restrict source, size_t n)
Копирование непересекающихся массивов используя SSE.
Definition: string.c:173
netcard_entry_t
Definition: cards.h:5