diff options
author | Anton Kling <anton@kling.gg> | 2023-11-10 15:47:08 +0100 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2023-11-10 15:47:08 +0100 |
commit | 9a1f977e39d8e9fcb6a9cb2a612f4743e802221d (patch) | |
tree | 1fc53f6e80eb40d24274f2f8967d584b88c6d664 /kernel/network | |
parent | 0cb4afef6da5488a128e5aaece435e9aa5f5797e (diff) |
Kernel Style: Change uint*_t -> u*
Diffstat (limited to 'kernel/network')
-rw-r--r-- | kernel/network/arp.c | 68 | ||||
-rw-r--r-- | kernel/network/arp.h | 8 | ||||
-rw-r--r-- | kernel/network/bytes.c | 6 | ||||
-rw-r--r-- | kernel/network/bytes.h | 8 | ||||
-rw-r--r-- | kernel/network/ethernet.c | 40 | ||||
-rw-r--r-- | kernel/network/ethernet.h | 8 | ||||
-rw-r--r-- | kernel/network/ipv4.c | 56 | ||||
-rw-r--r-- | kernel/network/ipv4.h | 8 | ||||
-rw-r--r-- | kernel/network/tcp.c | 140 | ||||
-rw-r--r-- | kernel/network/tcp.h | 8 | ||||
-rw-r--r-- | kernel/network/udp.c | 32 | ||||
-rw-r--r-- | kernel/network/udp.h | 8 |
12 files changed, 195 insertions, 195 deletions
diff --git a/kernel/network/arp.c b/kernel/network/arp.c index 179f6c1..ddeb553 100644 --- a/kernel/network/arp.c +++ b/kernel/network/arp.c @@ -7,29 +7,29 @@ #include <string.h> struct ARP_DATA { - uint16_t htype; // Hardware type - uint16_t ptype; // Protocol type - uint8_t hlen; // Hardware address length (Ethernet = 6) - uint8_t plen; // Protocol address length (IPv4 = 4) - uint16_t opcode; // ARP Operation Code - uint8_t srchw[6]; // Source hardware address - hlen bytes (see above) - uint8_t srcpr[4]; // Source protocol address - plen bytes (see above). + u16 htype; // Hardware type + u16 ptype; // Protocol type + u8 hlen; // Hardware address length (Ethernet = 6) + u8 plen; // Protocol address length (IPv4 = 4) + u16 opcode; // ARP Operation Code + u8 srchw[6]; // Source hardware address - hlen bytes (see above) + u8 srcpr[4]; // Source protocol address - plen bytes (see above). // If IPv4 can just be a "u32" type. - uint8_t dsthw[6]; // Destination hardware address - hlen bytes (see above) - uint8_t dstpr[4]; // Destination protocol address - plen bytes (see + u8 dsthw[6]; // Destination hardware address - hlen bytes (see above) + u8 dstpr[4]; // Destination protocol address - plen bytes (see // above). If IPv4 can just be a "u32" type. }; struct ARP_TABLE_ENTRY { - uint8_t is_used; - uint8_t mac[6]; - uint8_t ip[4]; + u8 is_used; + u8 mac[6]; + u8 ip[4]; }; struct ARP_TABLE_ENTRY arp_table[10] = {0}; // FIXME: This is hardcoded, don't do this. -uint8_t ip_address[4] = {10, 0, 2, 15}; +u8 ip_address[4] = {10, 0, 2, 15}; struct ARP_TABLE_ENTRY *find_arp_entry_to_use(void) { // This does not need to find a "free" entry as a ARP table is @@ -42,7 +42,7 @@ struct ARP_TABLE_ENTRY *find_arp_entry_to_use(void) { return &arp_table[0]; } -void print_mac(const char *str, uint8_t *mac) { +void print_mac(const char *str, u8 *mac) { kprintf("%s: ", str); for (int i = 0; i < 6; i++) { kprintf("%x", mac[i]); @@ -52,7 +52,7 @@ void print_mac(const char *str, uint8_t *mac) { kprintf("\n"); } -void print_ip(const char *str, const uint8_t *ip) { +void print_ip(const char *str, const u8 *ip) { kprintf("%s: ", str); for (int i = 0; i < 4; i++) { kprintf("%d", ip[i]); @@ -62,7 +62,7 @@ void print_ip(const char *str, const uint8_t *ip) { kprintf("\n"); } -void send_arp_request(const uint8_t ip[4]) { +void send_arp_request(const u8 ip[4]) { struct ARP_DATA data; data.htype = htons(1); data.ptype = htons(0x0800); @@ -72,37 +72,37 @@ void send_arp_request(const uint8_t ip[4]) { data.opcode = htons(0x0001); get_mac_address(data.srchw); - memcpy(data.srcpr, ip_address, sizeof(uint8_t[4])); + memcpy(data.srcpr, ip_address, sizeof(u8[4])); - memset(data.dsthw, 0, sizeof(uint8_t[6])); - memcpy(data.dstpr, ip, sizeof(uint8_t[4])); + memset(data.dsthw, 0, sizeof(u8[6])); + memcpy(data.dstpr, ip, sizeof(u8[4])); - uint8_t broadcast[6]; + u8 broadcast[6]; memset(broadcast, 0xFF, sizeof(broadcast)); - send_ethernet_packet(broadcast, 0x0806, (uint8_t *)&data, sizeof(data)); + send_ethernet_packet(broadcast, 0x0806, (u8 *)&data, sizeof(data)); } -int get_mac_from_ip(const uint8_t ip[4], uint8_t mac[6]) { +int get_mac_from_ip(const u8 ip[4], u8 mac[6]) { print_ip("ARP GETTING MAC FROM IP: ", ip); for (int i = 0; i < 10; i++) { - if (0 != memcmp(arp_table[i].ip, ip, sizeof(uint8_t[4]))) + if (0 != memcmp(arp_table[i].ip, ip, sizeof(u8[4]))) continue; - memcpy(mac, arp_table[i].mac, sizeof(uint8_t[6])); + memcpy(mac, arp_table[i].mac, sizeof(u8[6])); return 1; } klog("ARP cache miss", LOG_NOTE); send_arp_request(ip); // TODO: Maybe wait a bit? for (int i = 0; i < 10; i++) { - if (0 != memcmp(arp_table[i].ip, ip, sizeof(uint8_t[4]))) + if (0 != memcmp(arp_table[i].ip, ip, sizeof(u8[4]))) continue; - memcpy(mac, arp_table[i].mac, sizeof(uint8_t[6])); + memcpy(mac, arp_table[i].mac, sizeof(u8[6])); return 1; } return 0; } -void handle_arp(const uint8_t *payload) { +void handle_arp(const u8 *payload) { struct ARP_DATA *data = (struct ARP_DATA *)payload; // Assert that communication is over ethernet @@ -120,7 +120,7 @@ void handle_arp(const uint8_t *payload) { print_mac("dsthw: ", data->dsthw); print_ip("dstpr: ", data->dstpr); - assert(0 == memcmp(data->dstpr, ip_address, sizeof(uint8_t[4]))); + assert(0 == memcmp(data->dstpr, ip_address, sizeof(u8[4]))); // Now we have to construct a ARP response struct ARP_DATA response; @@ -130,19 +130,19 @@ void handle_arp(const uint8_t *payload) { response.hlen = 6; response.plen = 4; get_mac_address(response.srchw); - memcpy(response.srcpr, ip_address, sizeof(uint8_t[4])); + memcpy(response.srcpr, ip_address, sizeof(u8[4])); - memcpy(response.dsthw, data->srchw, sizeof(uint8_t[6])); - memcpy(response.dstpr, data->srcpr, sizeof(uint8_t[4])); + memcpy(response.dsthw, data->srchw, sizeof(u8[6])); + memcpy(response.dstpr, data->srcpr, sizeof(u8[4])); - send_ethernet_packet(data->srchw, 0x0806, (uint8_t *)&response, + send_ethernet_packet(data->srchw, 0x0806, (u8 *)&response, sizeof(response)); } else if (0x0002 /*arp_response*/ == ntohs(data->opcode)) { // Find a entry to fill struct ARP_TABLE_ENTRY *entry = find_arp_entry_to_use(); entry->is_used = 1; - memcpy(entry->mac, data->srchw, sizeof(uint8_t[6])); - memcpy(entry->ip, data->srcpr, sizeof(uint8_t[4])); + memcpy(entry->mac, data->srchw, sizeof(u8[6])); + memcpy(entry->ip, data->srcpr, sizeof(u8[4])); print_ip("Added ip: ", entry->ip); } else { kprintf("GOT A ARP REQEUST WITH TYPE: %x\n", ntohs(data->opcode)); diff --git a/kernel/network/arp.h b/kernel/network/arp.h index 1e42dff..6e53fd9 100644 --- a/kernel/network/arp.h +++ b/kernel/network/arp.h @@ -1,5 +1,5 @@ -#include <stdint.h> +#include <typedefs.h> -int get_mac_from_ip(const uint8_t ip[4], uint8_t mac[6]); -void send_arp_request(const uint8_t ip[4]); -void handle_arp(const uint8_t *payload); +int get_mac_from_ip(const u8 ip[4], u8 mac[6]); +void send_arp_request(const u8 ip[4]); +void handle_arp(const u8 *payload); diff --git a/kernel/network/bytes.c b/kernel/network/bytes.c index 94afa73..f502caf 100644 --- a/kernel/network/bytes.c +++ b/kernel/network/bytes.c @@ -1,10 +1,10 @@ #include <network/bytes.h> -uint16_t ntohs(uint16_t net) { return (net >> 8) | (net << 8); } +u16 ntohs(u16 net) { return (net >> 8) | (net << 8); } -uint16_t htons(uint16_t net) { return (net >> 8) | (net << 8); } +u16 htons(u16 net) { return (net >> 8) | (net << 8); } -uint32_t htonl(uint32_t net) { +u32 htonl(u32 net) { return (((net & 0x000000FF) << 24) | ((net & 0x0000FF00) << 8) | ((net & 0x00FF0000) >> 8) | ((net & 0xFF000000) >> 24)); } diff --git a/kernel/network/bytes.h b/kernel/network/bytes.h index c291589..b71b9a4 100644 --- a/kernel/network/bytes.h +++ b/kernel/network/bytes.h @@ -1,5 +1,5 @@ -#include <stdint.h> +#include <typedefs.h> -uint16_t ntohs(uint16_t net); -uint16_t htons(uint16_t net); -uint32_t htonl(uint32_t net); +u16 ntohs(u16 net); +u16 htons(u16 net); +u32 htonl(u32 net); diff --git a/kernel/network/ethernet.c b/kernel/network/ethernet.c index e97ccbd..5cf1a54 100644 --- a/kernel/network/ethernet.c +++ b/kernel/network/ethernet.c @@ -8,16 +8,16 @@ #include <stdio.h> struct ETHERNET_HEADER { - uint8_t mac_dst[6]; - uint8_t mac_src[6]; - uint16_t type; + u8 mac_dst[6]; + u8 mac_src[6]; + u16 type; }; -uint32_t crc32(const char *buf, size_t len) { - static uint32_t table[256]; +u32 crc32(const char *buf, size_t len) { + static u32 table[256]; static int have_table = 0; - uint32_t rem; - uint8_t octet; + u32 rem; + u8 octet; int i, j; const char *p, *q; @@ -36,7 +36,7 @@ uint32_t crc32(const char *buf, size_t len) { have_table = 1; } - uint32_t crc = 0xFFFFFFFF; + u32 crc = 0xFFFFFFFF; q = buf + len; for (p = buf; p < q; p++) { octet = *p; @@ -45,17 +45,17 @@ uint32_t crc32(const char *buf, size_t len) { return ~crc; } -void handle_ethernet(const uint8_t *packet, uint64_t packet_length) { +void handle_ethernet(const u8 *packet, u64 packet_length) { struct ETHERNET_HEADER *eth_header = (struct ETHERNET_HEADER *)packet; packet += sizeof(struct ETHERNET_HEADER); - const uint8_t *payload = packet; + const u8 *payload = packet; packet += packet_length - sizeof(struct ETHERNET_HEADER); - uint32_t crc = *((uint32_t *)packet - 1); + u32 crc = *((u32 *)packet - 1); kprintf("PACKET crc: %x\n", crc); kprintf("OUR OWN CALCULATED crc: %x\n", crc32((const char *)eth_header, (packet_length - 4))); - uint16_t type = ntohs(eth_header->type); + u16 type = ntohs(eth_header->type); switch (type) { case 0x0806: handle_arp(payload); @@ -69,22 +69,22 @@ void handle_ethernet(const uint8_t *packet, uint64_t packet_length) { } } -void send_ethernet_packet(uint8_t mac_dst[6], uint16_t type, uint8_t *payload, - uint64_t payload_length) { +void send_ethernet_packet(u8 mac_dst[6], u16 type, u8 *payload, + u64 payload_length) { // FIXME: Janky allocation, do this better - uint64_t buffer_size = - sizeof(struct ETHERNET_HEADER) + payload_length + sizeof(uint32_t); - uint8_t *buffer = kmalloc(buffer_size); - uint8_t *buffer_start = buffer; + u64 buffer_size = + sizeof(struct ETHERNET_HEADER) + payload_length + sizeof(u32); + u8 *buffer = kmalloc(buffer_size); + u8 *buffer_start = buffer; struct ETHERNET_HEADER *eth_header = (struct ETHERNET_HEADER *)buffer; buffer += sizeof(struct ETHERNET_HEADER); memcpy(buffer, payload, payload_length); buffer += payload_length; - memcpy(eth_header->mac_dst, mac_dst, sizeof(uint8_t[6])); + memcpy(eth_header->mac_dst, mac_dst, sizeof(u8[6])); get_mac_address(eth_header->mac_src); eth_header->type = htons(type); - *(uint32_t *)(buffer) = + *(u32 *)(buffer) = htonl(crc32((const char *)buffer_start, buffer_size - 4)); assert(rtl8139_send_data(buffer_start, buffer_size)); diff --git a/kernel/network/ethernet.h b/kernel/network/ethernet.h index 0fdcee3..ffb7893 100644 --- a/kernel/network/ethernet.h +++ b/kernel/network/ethernet.h @@ -1,5 +1,5 @@ -#include <stdint.h> +#include <typedefs.h> -void handle_ethernet(const uint8_t *packet, uint64_t packet_length); -void send_ethernet_packet(uint8_t mac_dst[6], uint16_t type, uint8_t *payload, - uint64_t payload_length); +void handle_ethernet(const u8 *packet, u64 packet_length); +void send_ethernet_packet(u8 mac_dst[6], u16 type, u8 *payload, + u64 payload_length); diff --git a/kernel/network/ipv4.c b/kernel/network/ipv4.c index 7f480a3..891e74f 100644 --- a/kernel/network/ipv4.c +++ b/kernel/network/ipv4.c @@ -9,16 +9,16 @@ #include <network/udp.h> #include <string.h> -uint16_t ip_checksum(void *vdata, size_t length) { +u16 ip_checksum(void *vdata, size_t length) { // Cast the data pointer to one that can be indexed. char *data = (char *)vdata; // Initialise the accumulator. - uint32_t acc = 0xffff; + u32 acc = 0xffff; // Handle complete 16-bit blocks. for (size_t i = 0; i + 1 < length; i += 2) { - uint16_t word; + u16 word; memcpy(&word, data + i, 2); acc += ntohs(word); if (acc > 0xffff) { @@ -28,7 +28,7 @@ uint16_t ip_checksum(void *vdata, size_t length) { // Handle any partial block at the end of the data. if (length & 1) { - uint16_t word = 0; + u16 word = 0; memcpy(&word, data + length - 1, 1); acc += ntohs(word); if (acc > 0xffff) { @@ -40,27 +40,27 @@ uint16_t ip_checksum(void *vdata, size_t length) { return htons(~acc); } -extern uint8_t ip_address[4]; -void send_ipv4_packet(uint32_t ip, uint8_t protocol, const uint8_t *payload, - uint16_t length) { - uint8_t header[20] = {0}; +extern u8 ip_address[4]; +void send_ipv4_packet(u32 ip, u8 protocol, const u8 *payload, + u16 length) { + u8 header[20] = {0}; header[0] = (4 /*version*/ << 4) | (5 /*IHL*/); - *((uint16_t *)(header + 2)) = htons(length + 20); + *((u16 *)(header + 2)) = htons(length + 20); header[8 /*TTL*/] = 0xF8; header[9] = protocol; - memcpy(header + 12 /*src_ip*/, ip_address, sizeof(uint8_t[4])); - memcpy(header + 16, &ip, sizeof(uint8_t[4])); + memcpy(header + 12 /*src_ip*/, ip_address, sizeof(u8[4])); + memcpy(header + 16, &ip, sizeof(u8[4])); - *((uint16_t *)(header + 10 /*checksum*/)) = ip_checksum(header, 20); - uint16_t packet_length = length + 20; - uint8_t *packet = kmalloc(packet_length); + *((u16 *)(header + 10 /*checksum*/)) = ip_checksum(header, 20); + u16 packet_length = length + 20; + u8 *packet = kmalloc(packet_length); memcpy(packet, header, 20); memcpy(packet + 20, payload, length); - uint8_t mac[6]; - uint8_t ip_copy[4]; // TODO: Do I need to do this? - memcpy(ip_copy, &ip, sizeof(uint8_t[4])); + u8 mac[6]; + u8 ip_copy[4]; // TODO: Do I need to do this? + memcpy(ip_copy, &ip, sizeof(u8[4])); for (; !get_mac_from_ip(ip_copy, mac);) ; kprintf("pre send_ethernet: %x\n", pit_num_ms()); @@ -69,29 +69,29 @@ void send_ipv4_packet(uint32_t ip, uint8_t protocol, const uint8_t *payload, kfree(packet); } -void handle_ipv4(const uint8_t *payload, uint32_t packet_length) { +void handle_ipv4(const u8 *payload, u32 packet_length) { assert(packet_length > 4); - uint16_t saved_checksum = *(uint16_t *)(payload + 10); - *(uint16_t *)(payload + 10) = 0; - uint16_t calc_checksum = ip_checksum((uint8_t *)payload, 20); - *(uint16_t *)(payload + 10) = saved_checksum; + u16 saved_checksum = *(u16 *)(payload + 10); + *(u16 *)(payload + 10) = 0; + u16 calc_checksum = ip_checksum((u8 *)payload, 20); + *(u16 *)(payload + 10) = saved_checksum; assert(calc_checksum == saved_checksum); - uint8_t version = (*payload & 0xF0) >> 4; - uint8_t IHL = (*payload & 0xF); + u8 version = (*payload & 0xF0) >> 4; + u8 IHL = (*payload & 0xF); kprintf("version: %x\n", version); assert(4 == version); assert(5 == IHL); - uint16_t ipv4_total_length = ntohs(*(uint16_t *)(payload + 2)); + u16 ipv4_total_length = ntohs(*(u16 *)(payload + 2)); assert(ipv4_total_length >= 20); // Make sure the ipv4 header is not trying to get uninitalized memory assert(ipv4_total_length <= packet_length); - uint8_t src_ip[4]; - memcpy(src_ip, payload + 12, sizeof(uint8_t[4])); + u8 src_ip[4]; + memcpy(src_ip, payload + 12, sizeof(u8[4])); - uint8_t protocol = *(payload + 9); + u8 protocol = *(payload + 9); switch (protocol) { case 0x6: handle_tcp(src_ip, payload + 20, ipv4_total_length - 20); diff --git a/kernel/network/ipv4.h b/kernel/network/ipv4.h index f578202..cd15fce 100644 --- a/kernel/network/ipv4.h +++ b/kernel/network/ipv4.h @@ -1,5 +1,5 @@ -#include <stdint.h> +#include <typedefs.h> -void handle_ipv4(const uint8_t *payload, uint32_t packet_length); -void send_ipv4_packet(uint32_t ip, uint8_t protocol, const uint8_t *payload, - uint16_t length); +void handle_ipv4(const u8 *payload, u32 packet_length); +void send_ipv4_packet(u32 ip, u8 protocol, const u8 *payload, + u16 length); diff --git a/kernel/network/tcp.c b/kernel/network/tcp.c index c20f046..6c693cf 100644 --- a/kernel/network/tcp.c +++ b/kernel/network/tcp.c @@ -4,7 +4,7 @@ #include <network/ipv4.h> #include <network/udp.h> #include <socket.h> -extern uint8_t ip_address[4]; +extern u8 ip_address[4]; #define CWR (1 << 7) #define ECE (1 << 6) @@ -16,56 +16,56 @@ extern uint8_t ip_address[4]; #define FIN (1 << 0) struct __attribute__((__packed__)) TCP_HEADER { - uint16_t src_port; - uint16_t dst_port; - uint32_t seq_num; - uint32_t ack_num; - uint8_t reserved : 4; - uint8_t data_offset : 4; - uint8_t flags; - uint16_t window_size; - uint16_t checksum; - uint16_t urgent_pointer; + u16 src_port; + u16 dst_port; + u32 seq_num; + u32 ack_num; + u8 reserved : 4; + u8 data_offset : 4; + u8 flags; + u16 window_size; + u16 checksum; + u16 urgent_pointer; }; struct __attribute__((__packed__)) PSEUDO_TCP_HEADER { - uint32_t src_addr; - uint32_t dst_addr; - uint8_t zero; - uint8_t protocol; - uint16_t tcp_length; - uint16_t src_port; - uint16_t dst_port; - uint32_t seq_num; - uint32_t ack_num; - uint8_t reserved : 4; - uint8_t data_offset : 4; - uint8_t flags; - uint16_t window_size; - uint16_t checksum_zero; // ????? - uint16_t urgent_pointer; + u32 src_addr; + u32 dst_addr; + u8 zero; + u8 protocol; + u16 tcp_length; + u16 src_port; + u16 dst_port; + u32 seq_num; + u32 ack_num; + u8 reserved : 4; + u8 data_offset : 4; + u8 flags; + u16 window_size; + u16 checksum_zero; // ????? + u16 urgent_pointer; }; -uint16_t tcp_checksum(uint16_t *buffer, int size) { +u16 tcp_checksum(u16 *buffer, int size) { unsigned long cksum = 0; while (size > 1) { cksum += *buffer++; - size -= sizeof(uint16_t); + size -= sizeof(u16); } if (size) - cksum += *(uint8_t *)buffer; + cksum += *(u8 *)buffer; cksum = (cksum >> 16) + (cksum & 0xffff); cksum += (cksum >> 16); - return (uint16_t)(~cksum); + return (u16)(~cksum); } -void tcp_calculate_checksum(uint8_t src_ip[4], uint8_t dst_ip[4], - const uint8_t *payload, uint16_t payload_length, +void tcp_calculate_checksum(u8 src_ip[4], u8 dst_ip[4], + const u8 *payload, u16 payload_length, struct TCP_HEADER *header) { struct PSEUDO_TCP_HEADER ps = {0}; - memcpy(&ps.src_addr, src_ip, sizeof(uint32_t)); - memcpy(&ps.dst_addr, dst_ip, sizeof(uint32_t)); + memcpy(&ps.src_addr, src_ip, sizeof(u32)); + memcpy(&ps.dst_addr, dst_ip, sizeof(u32)); ps.protocol = 6; ps.tcp_length = htons(20 + payload_length); ps.src_port = header->src_port; @@ -79,10 +79,10 @@ void tcp_calculate_checksum(uint8_t src_ip[4], uint8_t dst_ip[4], ps.urgent_pointer = header->urgent_pointer; // ps.options = 0; int buffer_length = sizeof(ps) + payload_length; - uint8_t buffer[buffer_length]; + u8 buffer[buffer_length]; memcpy(buffer, &ps, sizeof(ps)); memcpy(buffer + sizeof(ps), payload, payload_length); - header->checksum = tcp_checksum((uint16_t *)buffer, buffer_length); + header->checksum = tcp_checksum((u16 *)buffer, buffer_length); } void tcp_close_connection(struct INCOMING_TCP_CONNECTION *inc) { @@ -96,21 +96,21 @@ void tcp_close_connection(struct INCOMING_TCP_CONNECTION *inc) { header.flags = FIN | ACK; header.window_size = htons(512); header.urgent_pointer = 0; - uint32_t dst_ip; + u32 dst_ip; memcpy(&dst_ip, inc->ip, sizeof(dst_ip)); - uint8_t payload[0]; - uint16_t payload_length = 0; - tcp_calculate_checksum(ip_address, inc->ip, (const uint8_t *)payload, + u8 payload[0]; + u16 payload_length = 0; + tcp_calculate_checksum(ip_address, inc->ip, (const u8 *)payload, payload_length, &header); int send_len = sizeof(header) + payload_length; - uint8_t send_buffer[send_len]; + u8 send_buffer[send_len]; memcpy(send_buffer, &header, sizeof(header)); memcpy(send_buffer + sizeof(header), payload, payload_length); send_ipv4_packet(dst_ip, 6, send_buffer, send_len); } -void send_tcp_packet(struct INCOMING_TCP_CONNECTION *inc, uint8_t *payload, - uint16_t payload_length) { +void send_tcp_packet(struct INCOMING_TCP_CONNECTION *inc, u8 *payload, + u16 payload_length) { struct TCP_HEADER header = {0}; header.src_port = htons(inc->dst_port); header.dst_port = inc->n_port; @@ -121,12 +121,12 @@ void send_tcp_packet(struct INCOMING_TCP_CONNECTION *inc, uint8_t *payload, header.flags = PSH | ACK; header.window_size = htons(512); header.urgent_pointer = 0; - uint32_t dst_ip; + u32 dst_ip; memcpy(&dst_ip, inc->ip, sizeof(dst_ip)); - tcp_calculate_checksum(ip_address, inc->ip, (const uint8_t *)payload, + tcp_calculate_checksum(ip_address, inc->ip, (const u8 *)payload, payload_length, &header); int send_len = sizeof(header) + payload_length; - uint8_t send_buffer[send_len]; + u8 send_buffer[send_len]; memcpy(send_buffer, &header, sizeof(header)); memcpy(send_buffer + sizeof(header), payload, payload_length); send_ipv4_packet(dst_ip, 6, send_buffer, send_len); @@ -134,9 +134,9 @@ void send_tcp_packet(struct INCOMING_TCP_CONNECTION *inc, uint8_t *payload, inc->seq_num += payload_length; } -void send_empty_tcp_message(struct INCOMING_TCP_CONNECTION *inc, uint8_t flags, - uint32_t inc_seq_num, uint16_t n_dst_port, - uint16_t n_src_port) { +void send_empty_tcp_message(struct INCOMING_TCP_CONNECTION *inc, u8 flags, + u32 inc_seq_num, u16 n_dst_port, + u16 n_src_port) { struct TCP_HEADER header = {0}; header.src_port = n_dst_port; header.dst_port = n_src_port; @@ -148,28 +148,28 @@ void send_empty_tcp_message(struct INCOMING_TCP_CONNECTION *inc, uint8_t flags, header.window_size = htons(512); // TODO: What does this actually mean? header.urgent_pointer = 0; char payload[0]; - tcp_calculate_checksum(ip_address, inc->ip, (const uint8_t *)payload, 0, + tcp_calculate_checksum(ip_address, inc->ip, (const u8 *)payload, 0, &header); - uint32_t dst_ip; + u32 dst_ip; memcpy(&dst_ip, inc->ip, sizeof(dst_ip)); - send_ipv4_packet(dst_ip, 6, (const uint8_t *)&header, sizeof(header)); + send_ipv4_packet(dst_ip, 6, (const u8 *)&header, sizeof(header)); } -void handle_tcp(uint8_t src_ip[4], const uint8_t *payload, - uint32_t payload_length) { +void handle_tcp(u8 src_ip[4], const u8 *payload, + u32 payload_length) { const struct TCP_HEADER *inc_header = (const struct TCP_HEADER *)payload; - uint16_t n_src_port = *(uint16_t *)(payload); - uint16_t n_dst_port = *(uint16_t *)(payload + 2); - uint32_t n_seq_num = *(uint32_t *)(payload + 4); - uint32_t n_ack_num = *(uint32_t *)(payload + 8); + u16 n_src_port = *(u16 *)(payload); + u16 n_dst_port = *(u16 *)(payload + 2); + u32 n_seq_num = *(u32 *)(payload + 4); + u32 n_ack_num = *(u32 *)(payload + 8); - uint8_t flags = *(payload + 13); + u8 flags = *(payload + 13); - uint16_t src_port = htons(n_src_port); + u16 src_port = htons(n_src_port); (void)src_port; - uint16_t dst_port = htons(n_dst_port); - uint32_t seq_num = htonl(n_seq_num); - uint32_t ack_num = htonl(n_ack_num); + u16 dst_port = htons(n_dst_port); + u32 seq_num = htonl(n_seq_num); + u32 ack_num = htonl(n_ack_num); (void)ack_num; if (SYN == flags) { @@ -177,7 +177,7 @@ void handle_tcp(uint8_t src_ip[4], const uint8_t *payload, struct INCOMING_TCP_CONNECTION *inc; if (!(inc = handle_incoming_tcp_connection(src_ip, n_src_port, dst_port))) return; - memcpy(inc->ip, src_ip, sizeof(uint8_t[4])); + memcpy(inc->ip, src_ip, sizeof(u8[4])); inc->seq_num = 0; inc->ack_num = seq_num + 1; inc->connection_closed = 0; @@ -206,10 +206,10 @@ void handle_tcp(uint8_t src_ip[4], const uint8_t *payload, } if (flags & PSH) { kprintf("send ipv4 packet: %x\n", pit_num_ms()); - uint16_t tcp_payload_length = - payload_length - inc_header->data_offset * sizeof(uint32_t); + u16 tcp_payload_length = + payload_length - inc_header->data_offset * sizeof(u32); fifo_object_write( - (uint8_t *)(payload + inc_header->data_offset * sizeof(uint32_t)), 0, + (u8 *)(payload + inc_header->data_offset * sizeof(u32)), 0, tcp_payload_length, inc->data_file); // Send back a ACK @@ -225,11 +225,11 @@ void handle_tcp(uint8_t src_ip[4], const uint8_t *payload, header.window_size = htons(512); // TODO: What does this actually mean? header.urgent_pointer = 0; char payload[0]; - tcp_calculate_checksum(ip_address, src_ip, (const uint8_t *)payload, 0, + tcp_calculate_checksum(ip_address, src_ip, (const u8 *)payload, 0, &header); - uint32_t dst_ip; + u32 dst_ip; memcpy(&dst_ip, src_ip, sizeof(dst_ip)); - send_ipv4_packet(dst_ip, 6, (const uint8_t *)&header, sizeof(header)); + send_ipv4_packet(dst_ip, 6, (const u8 *)&header, sizeof(header)); kprintf("after ipv4 packet: %x\n", pit_num_ms()); return; } diff --git a/kernel/network/tcp.h b/kernel/network/tcp.h index fbd6065..7143e4d 100644 --- a/kernel/network/tcp.h +++ b/kernel/network/tcp.h @@ -1,5 +1,5 @@ -void handle_tcp(uint8_t src_ip[4], const uint8_t *payload, - uint32_t payload_length); -void send_tcp_packet(struct INCOMING_TCP_CONNECTION *inc, uint8_t *payload, - uint16_t payload_length); +void handle_tcp(u8 src_ip[4], const u8 *payload, + u32 payload_length); +void send_tcp_packet(struct INCOMING_TCP_CONNECTION *inc, u8 *payload, + u16 payload_length); void tcp_close_connection(struct INCOMING_TCP_CONNECTION *s); diff --git a/kernel/network/udp.c b/kernel/network/udp.c index 23411da..8c28610 100644 --- a/kernel/network/udp.c +++ b/kernel/network/udp.c @@ -5,33 +5,33 @@ #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}; + const u8 *payload, u16 payload_length) { + u16 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); + u16 packet_length = sizeof(header) + payload_length; + u8 *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) { +void handle_udp(u8 src_ip[4], const u8 *payload, + u32 packet_length) { assert(packet_length >= 8); // n_.* means network format(big endian) // h_.* means host format((probably) little endian) - uint16_t n_source_port = *(uint16_t *)payload; - uint16_t h_source_port = ntohs(n_source_port); + u16 n_source_port = *(u16 *)payload; + u16 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)); + u16 h_dst_port = ntohs(*(u16 *)(payload + 2)); + u16 h_length = ntohs(*(u16 *)(payload + 4)); assert(h_length == packet_length); - uint16_t data_length = h_length - 8; - const uint8_t *data = payload + 8; + u16 data_length = h_length - 8; + const u8 *data = payload + 8; // Find the open port OPEN_INET_SOCKET *in_s = find_open_udp_port(htons(h_dst_port)); @@ -44,12 +44,12 @@ void handle_udp(uint8_t src_ip[4], const uint8_t *payload, struct sockaddr_in /*{ sa_family_t sin_family; union { - uint32_t s_addr; + u32 s_addr; } sin_addr; - uint16_t sin_port; + u16 sin_port; }*/ in; in.sin_family = AF_INET; - memcpy(&in.sin_addr.s_addr, src_ip, sizeof(uint32_t)); + memcpy(&in.sin_addr.s_addr, src_ip, sizeof(u32)); in.sin_port = n_source_port; socklen_t sock_length = sizeof(struct sockaddr_in); @@ -57,7 +57,7 @@ void handle_udp(uint8_t src_ip[4], const uint8_t *payload, raw_vfs_pwrite(fifo_file, &in, sizeof(in), 0); // Write the UDP payload length(not including header) - raw_vfs_pwrite(fifo_file, &data_length, sizeof(uint16_t), 0); + raw_vfs_pwrite(fifo_file, &data_length, sizeof(u16), 0); // Write the UDP payload raw_vfs_pwrite(fifo_file, (char *)data, data_length, 0); diff --git a/kernel/network/udp.h b/kernel/network/udp.h index c30b8c8..ea1aafc 100644 --- a/kernel/network/udp.h +++ b/kernel/network/udp.h @@ -1,7 +1,7 @@ #include <socket.h> -#include <stdint.h> +#include <typedefs.h> -void handle_udp(uint8_t src_ip[4], const uint8_t *payload, - uint32_t packet_length); +void handle_udp(u8 src_ip[4], const u8 *payload, + u32 packet_length); void send_udp_packet(struct sockaddr_in *src, const struct sockaddr_in *dst, - const uint8_t *payload, uint16_t payload_length); + const u8 *payload, u16 payload_length); |