Skip to content

Commit dec4474

Browse files
committed
restructure
1 parent 9bfc3f8 commit dec4474

File tree

15 files changed

+30
-37
lines changed

15 files changed

+30
-37
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
C_SOURCES = $(wildcard kernel/*.c drivers/*.c cpu/*.c libc/*.c apps/*.c)
2-
HEADERS = $(wildcard kernel/*.h drivers/*.h cpu/*.h libc/*.h apps/*.h)
1+
C_SOURCES = $(wildcard kernel/*.c drivers/*.c cpu/*.c libc/*.c apps/*.c net/*.c)
2+
HEADERS = $(wildcard kernel/*.h drivers/*.h cpu/*.h libc/*.h apps/*.h net/*.h)
33

44
BUILD_DIR = _build
55
OBJ = $(patsubst %.c, $(BUILD_DIR)/%.o, $(C_SOURCES)) $(BUILD_DIR)/cpu/interrupt.o $(BUILD_DIR)/cpu/task_switch.o
@@ -12,7 +12,7 @@ CFLAGS = -g -O0 -m32 -fno-pie -ffreestanding -nostdlib -fno-builtin -nodefaultli
1212

1313
NETWORKING = -netdev tap,id=my_tap0,ifname=tap0 -device rtl8139,netdev=my_tap0
1414

15-
$(shell mkdir -p $(BUILD_DIR)/boot $(BUILD_DIR)/kernel $(BUILD_DIR)/drivers $(BUILD_DIR)/cpu $(BUILD_DIR)/libc $(BUILD_DIR)/apps)
15+
$(shell mkdir -p $(BUILD_DIR)/boot $(BUILD_DIR)/kernel $(BUILD_DIR)/drivers $(BUILD_DIR)/cpu $(BUILD_DIR)/libc $(BUILD_DIR)/apps $(BUILD_DIR)/net)
1616

1717
all: format bits $(BUILD_DIR)/kernel.elf disk/fat16.img
1818

TODO

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ vfs:
2323
- can connect to outside atleast on qemu com1
2424
- 9p could use udp
2525

26+
networking:
27+
- ethernet
28+
- dhcp not working
29+
- full of memory leaks, no queue to handle requests
30+
2631
build disk:
2732
(if i have 9p this is kind of irrelevant)
2833
on generation save the img, remove it from clean and keep backups

kernel/kernel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void kernel_main(void) {
5151
ip_addr[2] = 2;
5252
ip_addr[3] = 15;
5353
char *str = "this is a message sent from the OS";
54-
54+
5555
/*
5656
ethernet_send_packet(mac_addr, (void *)str, strlen(str), 0x0021);
5757
ip_send_packet(ip_addr, (void *)str, strlen(str));

drivers/arp.c renamed to net/arp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include "arp.h"
2+
#include "drivers/serial.h"
23
#include "libc/heap.h"
34
#include "libc/mem.h"
45
#include "network.h"
56
#include "rtl8139.h"
6-
#include "serial.h"
77

88
arp_table_entry_t arp_table[512];
99
int arp_table_size;
File renamed without changes.

drivers/dhcp.c renamed to net/dhcp.c

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,49 +87,46 @@ void dhcp_request(uint8_t *requested_ip) {
8787

8888
uint8_t *options = packet.options;
8989

90-
// Magic cookie
90+
// magic cookie
9191
*((uint32_t *)(options)) = htonl(DHCP_MAGIC_COOKIE);
9292
options += 4;
9393

94-
// Message type (REQUEST)
94+
// message type (REQUEST)
9595
uint8_t msg_type = DHCP_REQUEST;
9696
options = add_dhcp_option(options, DHCP_OPT_MSG_TYPE, 1, &msg_type);
9797

98-
// Client identifier
98+
// client identifier
9999
uint8_t client_id[7];
100-
client_id[0] = 1; // Hardware type (Ethernet)
100+
client_id[0] = 0x1; // hardware type (Ethernet)
101101
get_mac_addr(client_id + 1);
102102
options = add_dhcp_option(options, DHCP_OPT_CLIENT_ID, 7, client_id);
103103

104-
// Requested IP address
105104
if (requested_ip) {
106105
options = add_dhcp_option(options, DHCP_OPT_REQUESTED_IP, 4, requested_ip);
107106
}
108107

109-
// Server identifier (if we have it)
108+
// server identifier (if we have it)
110109
if (dhcp_server_ip != 0) {
111110
options =
112111
add_dhcp_option(options, 54, 4, &dhcp_server_ip); // 54 = Server ID
113112
}
114113

115-
// Host name (optional)
114+
// host name (optional)
116115
char hostname[] = "os-dev";
117116
options =
118117
add_dhcp_option(options, DHCP_OPT_HOST_NAME, strlen(hostname), hostname);
119118

120-
// Parameter request list
119+
// parameter request list
121120
uint8_t params[] = {
122-
DHCP_OPT_SUBNET_MASK, // Subnet mask
123-
DHCP_OPT_ROUTER, // Router
121+
DHCP_OPT_SUBNET_MASK, // subnet mask
122+
DHCP_OPT_ROUTER, // router
124123
DHCP_OPT_DNS // DNS server
125124
};
126125
options =
127126
add_dhcp_option(options, DHCP_OPT_PARAM_REQ_LIST, sizeof(params), params);
128127

129-
// End option
130128
*(options++) = DHCP_OPT_END;
131129

132-
// Send the packet
133130
udp_send_packet(dst_ip, DHCP_CLIENT, DHCP_SERVER, &packet,
134131
sizeof(dhcp_packet_t));
135132
}
@@ -139,7 +136,7 @@ void *get_dhcp_option(dhcp_packet_t *packet, uint8_t type) {
139136
return NULL;
140137
}
141138

142-
uint8_t *options = packet->options + 4; // Skip magic cookie
139+
uint8_t *options = packet->options + 4; // skip magic cookie
143140

144141
while (options < packet->options + sizeof(packet->options)) {
145142
uint8_t curr_type = *options;
@@ -174,10 +171,9 @@ void dhcp_handle_packet(dhcp_packet_t *packet) {
174171
return;
175172
}
176173

177-
// Get message type
178174
uint8_t *type_ptr = get_dhcp_option(packet, DHCP_OPT_MSG_TYPE);
179175
if (!type_ptr) {
180-
return; // No message type option
176+
return;
181177
}
182178

183179
uint8_t msg_type = *type_ptr;
File renamed without changes.

drivers/ip.c renamed to net/ip.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include "ip.h"
22
#include "arp.h"
33
#include "dhcp.h"
4+
#include "drivers/serial.h"
45
#include "libc/string.h"
56
#include "network.h"
6-
#include "serial.h"
77

88
uint8_t my_ip[] = {10, 0, 2, 14};
99
uint8_t test_target_ip[] = {10, 0, 2, 15};
@@ -46,51 +46,43 @@ uint16_t ip_calculate_checksum(ip_packet_t *packet) {
4646
}
4747

4848
void ip_send_packet(uint8_t *dst_ip, void *data, uint32_t len) {
49-
// Print what we're trying to send
5049
serial_debug("sending ip packet to %d.%d.%d.%d with data length %d",
5150
dst_ip[0], dst_ip[1], dst_ip[2], dst_ip[3], len);
5251

53-
// Allocate memory for the packet
5452
ip_packet_t *packet = (ip_packet_t *)kmalloc(sizeof(ip_packet_t) + len);
5553

56-
// Fill in the IP header
5754
packet->version_ihl = (4 << 4) | 5; // IPv4, header length 5 words (20 bytes)
5855
serial_debug("version_ihl = %x", packet->version_ihl);
5956

6057
packet->tos = 0;
6158

62-
// Total length = header + data
6359
uint16_t total_length = sizeof(ip_packet_t) + len;
6460
packet->length = htons(total_length);
6561
serial_debug("total length = %d (%x in network order)", total_length,
6662
packet->length);
6763

68-
// Set identification field
6964
static uint16_t ip_id = 0;
7065
packet->id = htons(ip_id++);
7166

72-
// No fragmentation
67+
// no fragmentation
7368
packet->flags_fragment = htons(0);
7469

7570
// TTL and protocol
7671
packet->ttl = 64;
7772
packet->protocol = PROTOCOL_UDP; // Assuming UDP for DHCP
7873

7974
uint8_t my_ip_address[4] = {0, 0, 0, 0};
80-
// Set source and destination addresses
75+
8176
memcpy(packet->src_ip, my_ip_address, 4);
8277
memcpy(packet->dst_ip, dst_ip, 4);
8378

84-
// Calculate checksum
85-
packet->header_checksum = 0; // Must be zero for calculation
79+
packet->header_checksum = 0;
8680
packet->header_checksum =
87-
ip_calculate_checksum(packet); // Standard IP header is 20 bytes
81+
ip_calculate_checksum(packet);
8882
serial_debug("ip checksum = %x", ntohs(packet->header_checksum));
8983

90-
// Copy the data
9184
memcpy(packet->data, data, len);
9285

93-
// Get the MAC address for the destination IP
9486
uint8_t dst_mac[6];
9587
if (arp_lookup(dst_ip, dst_mac) != 0) {
9688
serial_debug("no MAC found for IP %d.%d.%d.%d - sending ARP request",
File renamed without changes.

drivers/network.c renamed to net/network.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#include "network.h"
22
#include "arp.h"
3+
#include "drivers/serial.h"
34
#include "ip.h"
45
#include "libc/heap.h"
56
#include "libc/mem.h"
67
#include "rtl8139.h"
7-
#include "serial.h"
88

99
int ethernet_send_packet(uint8_t *dst_mac_addr, uint8_t *data, int len,
1010
uint16_t protocol) {

0 commit comments

Comments
 (0)