Updated network related code so now it should work as expected

This commit is contained in:
Alex
2023-01-09 03:32:17 +02:00
parent ad16d361dc
commit 4f6c7e8a0d
20 changed files with 603 additions and 246 deletions

View File

@ -1,13 +1,15 @@
#include <net/nc.hpp>
#include <net/ipv4.hpp>
#include <net/dhcp.hpp>
#include <net/eth.hpp>
#include <net/arp.hpp>
#include <net/ipv4.hpp>
#include <net/ntp.hpp>
#include <net/dns.hpp>
#include <net/udp.hpp>
#include <net/dhcp.hpp>
#include <debug.h>
#include "../kernel.h"
#include "../DAPI.hpp"
#include "../Fex.hpp"
@ -37,7 +39,7 @@ namespace NetworkInterfaceManager
// Unregister all events
for (size_t i = 0; i < RegisteredEvents.size(); i++)
RegisteredEvents.remove(i);
RegisteredEvents.remove(i);
// Delete all interfaces and their callbacks and free the memory
delete mem;
@ -64,6 +66,12 @@ namespace NetworkInterfaceManager
debug("Network Card: %s; MAC: %#lx", Iface->Name, Iface->MAC.ToHex());
}
#ifdef DEBUG_NETWORK
#define DbgWriteScreen(x, ...) KPrint(x, ##__VA_ARGS__)
#else
#define DbgWriteScreen(x, ...) debug(x, ##__VA_ARGS__)
#endif
void NetworkInterface::StartNetworkStack()
{
TaskManager->GetCurrentThread()->SetPriority(100);
@ -79,22 +87,46 @@ namespace NetworkInterfaceManager
error("No network device found!");
else
{
DbgWriteScreen("Using %s (%s) as the default network device", DefaultDevice->Name, DefaultDevice->MAC.ToString());
NetworkEthernet::Ethernet *eth = new NetworkEthernet::Ethernet(DefaultDevice); // Use the first device found as the ethernet device
NetworkARP::ARP *arp = new NetworkARP::ARP(eth);
NetworkIPv4::IPv4 *ipv4 = new NetworkIPv4::IPv4(arp, eth);
NetworkUDP::UDP *udp = new NetworkUDP::UDP(ipv4, DefaultDevice);
NetworkUDP::Socket *DHCP_Socket = udp->Connect({.Address = {0xFF, 0xFF, 0xFF, 0xFF}}, 67);
NetworkUDP::Socket *DHCP_Socket = udp->Connect(InternetProtocol() /* Default value is 255.255.255.255 */, 67);
NetworkDHCP::DHCP *dhcp = new NetworkDHCP::DHCP(DHCP_Socket, DefaultDevice);
udp->Bind(DHCP_Socket, dhcp);
dhcp->Request();
DefaultDevice->IP = InternetProtocol4().FromHex(b32(dhcp->IP.ToHex()));
DefaultDevice->IP.v4.FromHex(dhcp->IP.v4.ToHex());
ipv4->SubNetworkMaskIP = dhcp->SubNetworkMask;
ipv4->GatewayIP = dhcp->Gateway;
arp->Broadcast(dhcp->Gateway);
trace("IP: %d.%d.%d.%d", dhcp->IP.Address[3], dhcp->IP.Address[2], dhcp->IP.Address[1], dhcp->IP.Address[0]);
trace("SubNetwork Mask: %d.%d.%d.%d", dhcp->SubNetworkMask.Address[3], dhcp->SubNetworkMask.Address[2], dhcp->SubNetworkMask.Address[1], dhcp->SubNetworkMask.Address[0]);
trace("Gateway: %d.%d.%d.%d", dhcp->Gateway.Address[3], dhcp->Gateway.Address[2], dhcp->Gateway.Address[1], dhcp->Gateway.Address[0]);
trace("DNS: %d.%d.%d.%d", dhcp->DomainNameSystem.Address[3], dhcp->DomainNameSystem.Address[2], dhcp->DomainNameSystem.Address[1], dhcp->DomainNameSystem.Address[0]);
TaskManager->Sleep(200);
DbgWriteScreen("IP: %s", dhcp->IP.v4.ToStringLittleEndian());
DbgWriteScreen("SubNetwork Mask: %s", dhcp->SubNetworkMask.v4.ToStringLittleEndian());
DbgWriteScreen("Gateway: %s", dhcp->Gateway.v4.ToStringLittleEndian());
DbgWriteScreen("DNS: %s", dhcp->DomainNameSystem.v4.ToStringLittleEndian());
TaskManager->Sleep(200);
/*
I HAVE TO REWRITE THE ENTIRE NETWORK STACK BECAUSE IT'S A MESS. I HATE BYTE SWAPPING!!!!!
Rules to follow:
- The functions MUST have little endian parameters and return values.
*/
/* TODO: This is a quick workaround we need DNS resolver asap. IP is time-a-g.nist.gov; https://tf.nist.gov/tf-cgi/servers.cgi */
InternetProtocol ip = {.v4 = {.Address = {129, 6, 15, 28}},
.v6 = {.Address = {}}};
NetworkUDP::Socket *NTP_Socket = udp->Connect(ip, 123);
NetworkNTP::NTP *ntp = new NetworkNTP::NTP(NTP_Socket);
udp->Bind(NTP_Socket, ntp);
int UnixTimestamp = ntp->ReadTime();
Time::Clock time = Time::ConvertFromUnix(UnixTimestamp);
DbgWriteScreen("NTP: %d - %d.%d.%d %d:%d:%d", UnixTimestamp,
time.Day, time.Month, time.Year,
time.Hour, time.Minute, time.Second);
TaskManager->Sleep(200);
/* TODO: Store everything in an vector and initialize all network cards */
}