summaryrefslogtreecommitdiff
path: root/kernel/network
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/network')
-rw-r--r--kernel/network/arp.c23
-rw-r--r--kernel/network/arp.h1
-rw-r--r--kernel/network/ethernet.c1
-rw-r--r--kernel/network/tcp.c9
4 files changed, 30 insertions, 4 deletions
diff --git a/kernel/network/arp.c b/kernel/network/arp.c
index bd3cd24..576a09a 100644
--- a/kernel/network/arp.c
+++ b/kernel/network/arp.c
@@ -5,6 +5,15 @@
#include <network/ethernet.h>
#include <stdio.h>
#include <string.h>
+#include <interrupts.h>
+
+ipv4_t gateway;
+ipv4_t bitmask;
+
+void setup_network(ipv4_t _gateway, ipv4_t _bitmask) {
+ gateway = _gateway;
+ bitmask = _bitmask;
+}
struct ARP_DATA {
u16 htype; // Hardware type
@@ -84,7 +93,20 @@ void send_arp_request(const u8 ip[4]) {
send_ethernet_packet(broadcast, 0x0806, (u8 *)&data, sizeof(data));
}
+int ip_inside_network(const ipv4_t ip) {
+ if ((ip.d & bitmask.d) == (gateway.d & bitmask.d)) {
+ return 1;
+ }
+ return 0;
+}
+
int get_mac_from_ip(const u8 ip[4], u8 mac[6]) {
+ ipv4_t tmp_ip;
+ memcpy(tmp_ip.a, ip, sizeof(u8[4]));
+ if (!ip_inside_network(tmp_ip)) {
+ return get_mac_from_ip(gateway.a, mac);
+ }
+
for (int i = 0; i < 10; i++) {
if (0 != memcmp(arp_table[i].ip, ip, sizeof(u8[4]))) {
continue;
@@ -93,6 +115,7 @@ int get_mac_from_ip(const u8 ip[4], u8 mac[6]) {
return 1;
}
klog("ARP cache miss", LOG_NOTE);
+ enable_interrupts();
send_arp_request(ip);
// TODO: Maybe wait a bit?
for (int i = 0; i < 10; i++) {
diff --git a/kernel/network/arp.h b/kernel/network/arp.h
index 6e53fd9..3795a62 100644
--- a/kernel/network/arp.h
+++ b/kernel/network/arp.h
@@ -1,5 +1,6 @@
#include <typedefs.h>
+void setup_network(ipv4_t _gateway, ipv4_t _bitmask);
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/ethernet.c b/kernel/network/ethernet.c
index 4fe6a94..9fa5ef8 100644
--- a/kernel/network/ethernet.c
+++ b/kernel/network/ethernet.c
@@ -73,6 +73,7 @@ void send_ethernet_packet(u8 mac_dst[6], u16 type, u8 *payload,
u64 buffer_size =
sizeof(struct ETHERNET_HEADER) + payload_length + sizeof(u32);
u8 *buffer = kmalloc(buffer_size);
+ memset(buffer, 0, buffer_size);
u8 *buffer_start = buffer;
struct ETHERNET_HEADER *eth_header = (struct ETHERNET_HEADER *)buffer;
buffer += sizeof(struct ETHERNET_HEADER);
diff --git a/kernel/network/tcp.c b/kernel/network/tcp.c
index 717c7db..fb3c052 100644
--- a/kernel/network/tcp.c
+++ b/kernel/network/tcp.c
@@ -190,6 +190,7 @@ void handle_tcp(u8 src_ip[4], const u8 *payload, u32 payload_length) {
}
struct TcpConnection *incoming_connection = tcp_find_connection(dst_port);
+ kprintf("dst_port: %d\n", dst_port);
if (incoming_connection) {
incoming_connection->unhandled_packet = 1;
if (0 != (flags & RST)) {
@@ -212,9 +213,9 @@ void handle_tcp(u8 src_ip[4], const u8 *payload, u32 payload_length) {
tcp_send_ack(incoming_connection);
}
- if (0 != (flags & PSH)) {
- u16 tcp_payload_length =
- payload_length - header->data_offset * sizeof(u32);
+ // if (0 != (flags & PSH)) {
+ u16 tcp_payload_length = payload_length - header->data_offset * sizeof(u32);
+ if (tcp_payload_length > 0) {
int len = fifo_object_write(
(u8 *)(payload + header->data_offset * sizeof(u32)), 0,
tcp_payload_length, incoming_connection->data_file);
@@ -232,7 +233,7 @@ void handle_tcp(u8 src_ip[4], const u8 *payload, u32 payload_length) {
// is closed.
}
} else {
- assert(NULL);
+ return;
}
}
/*