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

@ -43,7 +43,7 @@ namespace NetworkARP
struct DiscoveredAddress
{
MediaAccessControl MAC;
InternetProtocol4 IP;
InternetProtocol IP;
};
class ARP : public NetworkEthernet::EthernetEvents
@ -60,9 +60,9 @@ namespace NetworkARP
};
Vector<NetworkARP::DiscoveredAddress *> DiscoveredAddresses;
DiscoveredAddress *ManageDA(DAType Type, InternetProtocol4 IP, MediaAccessControl MAC);
DiscoveredAddress *Search(InternetProtocol4 TargetIP);
DiscoveredAddress *Update(InternetProtocol4 TargetIP, MediaAccessControl TargetMAC);
DiscoveredAddress *ManageDiscoveredAddresses(DAType Type, InternetProtocol IP, MediaAccessControl MAC);
DiscoveredAddress *Search(InternetProtocol TargetIP);
DiscoveredAddress *Update(InternetProtocol TargetIP, MediaAccessControl TargetMAC);
bool OnEthernetPacketReceived(uint8_t *Data, uint64_t Length);
public:
@ -75,14 +75,14 @@ namespace NetworkARP
* @param IP The IP address to resolve. (Little-endian)
* @return uint48_t The MAC address of the IP address.
*/
uint48_t Resolve(InternetProtocol4 IP);
uint48_t Resolve(InternetProtocol IP);
/**
* @brief Broadcast an ARP packet.
*
* @param IP The IP address to broadcast.
*/
void Broadcast(InternetProtocol4 IP);
void Broadcast(InternetProtocol IP);
};
}

View File

@ -143,23 +143,22 @@ namespace NetworkDHCP
void CreatePacket(DHCPHeader *Packet, uint8_t MessageType, uint32_t RequestIP);
void *GetOption(DHCPHeader *Packet, uint8_t Type);
void OnUDPPacketReceived(NetworkUDP::Socket *Socket, uint8_t *Data, uint64_t Length);
public:
/** @brief IP address (Little-endian) */
InternetProtocol4 IP = {.Address = {0x0, 0x0, 0x0, 0x0}};
InternetProtocol IP = {};
/** @brief Gateway address (Little-endian) */
InternetProtocol4 Gateway = {.Address = {0x0, 0x0, 0x0, 0x0}};
InternetProtocol Gateway = {};
/** @brief Subnet mask (Little-endian) */
InternetProtocol4 SubNetworkMask = {.Address = {0x0, 0x0, 0x0, 0x0}};
InternetProtocol SubNetworkMask = {};
/** @brief DNS server address (Little-endian) */
InternetProtocol4 DomainNameSystem = {.Address = {0x0, 0x0, 0x0, 0x0}};
InternetProtocol DomainNameSystem = {};
DHCP(NetworkUDP::Socket *Socket, NetworkInterfaceManager::DeviceInterface *Interface);
~DHCP();
void Request();
void Request(InternetProtocol4 IP);
virtual void OnUDPPacketReceived(NetworkUDP::Socket *Socket, uint8_t *Data, uint64_t Length);
void Request(InternetProtocol IP);
};
}

20
include/net/dns.hpp Normal file
View File

@ -0,0 +1,20 @@
#ifndef __FENNIX_KERNEL_DNS_H__
#define __FENNIX_KERNEL_DNS_H__
#include <net/udp.hpp>
#include <types.h>
namespace NetworkDNS
{
class DNS : public NetworkUDP::UDPEvents
{
private:
NetworkUDP::Socket *UDPSocket;
public:
DNS(NetworkUDP::Socket *Socket);
~DNS();
};
}
#endif // !__FENNIX_KERNEL_DNS_H__

View File

@ -57,7 +57,14 @@ namespace NetworkEthernet
/** @brief Get driver interface
* @return Driver interface
*/
NetworkInterfaceManager::DeviceInterface *GetInterface() { return this->Interface; }
NetworkInterfaceManager::DeviceInterface *GetInterface()
{
netdbg("Interface: %#lx (MAC: %s; IPv4: %s; IPv6: %s)", this->Interface,
this->Interface->MAC.ToString(),
this->Interface->IP.v4.ToStringLittleEndian(),
this->Interface->IP.v6.ToStringLittleEndian());
return this->Interface;
}
Ethernet(NetworkInterfaceManager::DeviceInterface *Interface);
~Ethernet();

View File

@ -15,7 +15,8 @@ namespace NetworkIPv4
uint8_t TypeOfService;
uint16_t TotalLength;
uint16_t Identification;
uint16_t FlagsAndFragmentOffset;
uint8_t Flags;
uint8_t FragmentOffset;
uint8_t TimeToLive;
uint8_t Protocol;
uint16_t HeaderChecksum;
@ -76,8 +77,8 @@ namespace NetworkIPv4
virtual bool OnEthernetPacketReceived(uint8_t *Data, uint64_t Length);
public:
InternetProtocol4 GatewayIP = {.Address = {0xFF, 0xFF, 0xFF, 0xFF}};
InternetProtocol4 SubNetworkMaskIP = {.Address = {0xFF, 0xFF, 0xFF, 0xFF}};
InternetProtocol GatewayIP;
InternetProtocol SubNetworkMaskIP;
IPv4(NetworkARP::ARP *ARP, NetworkEthernet::Ethernet *Ethernet);
~IPv4();
@ -89,7 +90,7 @@ namespace NetworkIPv4
* @param Protocol The protocol of the packet.
* @param DestinationIP The IP address of the destination. (Big-endian)
*/
void Send(uint8_t *Data, uint64_t Length, uint8_t Protocol, InternetProtocol4 DestinationIP);
void Send(uint8_t *Data, uint64_t Length, uint8_t Protocol, InternetProtocol DestinationIP);
};
class IPv4Events
@ -104,7 +105,7 @@ namespace NetworkIPv4
public:
uint8_t GetProtocol() { return Protocol; }
virtual bool OnIPv4PacketReceived(InternetProtocol4 SourceIP, InternetProtocol4 DestinationIP, uint8_t *Data, uint64_t Length)
virtual bool OnIPv4PacketReceived(InternetProtocol SourceIP, InternetProtocol DestinationIP, uint8_t *Data, uint64_t Length)
{
warn("Not implemented.");
return false;

View File

@ -22,7 +22,7 @@ namespace NetworkInterfaceManager
MediaAccessControl MAC;
/** @brief Device interface IP address (Big-endian) */
InternetProtocol4 IP;
InternetProtocol IP;
/** @brief Reserved */
void *DriverCallBackAddress;

View File

@ -2,8 +2,9 @@
#define __FENNIX_KERNEL_NETWORK_H__
#include <types.h>
#include <printf.h>
// #define DEBUG_NETWORK 1
#define DEBUG_NETWORK 1
#ifdef DEBUG_NETWORK
#define netdbg(m, ...) debug(m, ##__VA_ARGS__)
@ -25,9 +26,16 @@ typedef __UINT64_TYPE__ uint48_t;
#define b48(x) (((((x)&0x0000000000ff) << 40) | (((x)&0x00000000ff00) << 24) | (((x)&0x000000ff0000) << 8) | (((x)&0x0000ff000000) >> 8) | (((x)&0x00ff00000000) >> 24) | (((x)&0xff0000000000) >> 40)))
#define b64(x) __builtin_bswap64(x)
enum Endianness
{
LITTLE_ENDIAN,
BIG_ENDIAN
};
struct MediaAccessControl
{
uint8_t Address[6];
uint8_t Address[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Endianness Endianess = LITTLE_ENDIAN;
inline bool operator==(const MediaAccessControl &lhs) const
{
@ -96,53 +104,114 @@ struct MediaAccessControl
this->Address[4] != 0xFF ||
this->Address[5] != 0xFF);
}
char *ToString()
{
static char Buffer[18];
sprintf(Buffer, "%02X:%02X:%02X:%02X:%02X:%02X", this->Address[0], this->Address[1], this->Address[2], this->Address[3], this->Address[4], this->Address[5]);
return Buffer;
}
};
struct InternetProtocol4
/* There's a confusion between LSB and MSB. Not sure if "ToStringLittleEndian" and "ToStringBigEndian" are implemented correctly.
Because x86 is a LSB architecture, I'm assuming that the "ToStringLittleEndian" is correct? */
struct InternetProtocol
{
uint8_t Address[4];
inline bool operator==(const InternetProtocol4 &lhs) const
struct Version4
{
return lhs.Address[0] == this->Address[0] &&
lhs.Address[1] == this->Address[1] &&
lhs.Address[2] == this->Address[2] &&
lhs.Address[3] == this->Address[3];
}
uint8_t Address[4] = {255, 255, 255, 255};
Endianness Endianess = LITTLE_ENDIAN;
inline bool operator==(const uint32_t &lhs) const
inline bool operator==(const InternetProtocol::Version4 &lhs) const
{
return lhs.Address[0] == this->Address[0] &&
lhs.Address[1] == this->Address[1] &&
lhs.Address[2] == this->Address[2] &&
lhs.Address[3] == this->Address[3];
}
inline bool operator==(const uint32_t &lhs) const
{
InternetProtocol::Version4 IP;
IP.Address[0] = (uint8_t)((lhs >> 24) & 0xFF);
IP.Address[1] = (uint8_t)((lhs >> 16) & 0xFF);
IP.Address[2] = (uint8_t)((lhs >> 8) & 0xFF);
IP.Address[3] = (uint8_t)(lhs & 0xFF);
return IP.Address[0] == this->Address[0] &&
IP.Address[1] == this->Address[1] &&
IP.Address[2] == this->Address[2] &&
IP.Address[3] == this->Address[3];
}
inline bool operator!=(const InternetProtocol::Version4 &lhs) const { return !(*this == lhs); }
inline bool operator!=(const uint32_t &lhs) const { return !(*this == lhs); }
inline uint32_t ToHex()
{
return ((uint64_t)this->Address[0] << 24) |
((uint64_t)this->Address[1] << 16) |
((uint64_t)this->Address[2] << 8) |
((uint64_t)this->Address[3]);
}
inline InternetProtocol::Version4 FromHex(uint32_t Hex)
{
this->Address[0] = (uint8_t)((Hex >> 24) & 0xFF);
this->Address[1] = (uint8_t)((Hex >> 16) & 0xFF);
this->Address[2] = (uint8_t)((Hex >> 8) & 0xFF);
this->Address[3] = (uint8_t)(Hex & 0xFF);
return *this;
}
char *ToStringLittleEndian()
{
static char Buffer[16];
sprintf(Buffer, "%d.%d.%d.%d", this->Address[0], this->Address[1], this->Address[2], this->Address[3]);
return Buffer;
}
char *ToStringBigEndian()
{
static char Buffer[16];
sprintf(Buffer, "%d.%d.%d.%d", this->Address[3], this->Address[2], this->Address[1], this->Address[0]);
return Buffer;
}
} v4;
struct Version6
{
InternetProtocol4 IP;
IP.Address[0] = (uint8_t)((lhs >> 24) & 0xFF);
IP.Address[1] = (uint8_t)((lhs >> 16) & 0xFF);
IP.Address[2] = (uint8_t)((lhs >> 8) & 0xFF);
IP.Address[3] = (uint8_t)(lhs & 0xFF);
uint16_t Address[8] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
Endianness Endianess = LITTLE_ENDIAN;
return IP.Address[0] == this->Address[0] &&
IP.Address[1] == this->Address[1] &&
IP.Address[2] == this->Address[2] &&
IP.Address[3] == this->Address[3];
}
inline bool operator==(const InternetProtocol::Version6 &lhs) const
{
return lhs.Address[0] == this->Address[0] &&
lhs.Address[1] == this->Address[1] &&
lhs.Address[2] == this->Address[2] &&
lhs.Address[3] == this->Address[3] &&
lhs.Address[4] == this->Address[4] &&
lhs.Address[5] == this->Address[5] &&
lhs.Address[6] == this->Address[6] &&
lhs.Address[7] == this->Address[7];
}
inline bool operator!=(const InternetProtocol4 &lhs) const { return !(*this == lhs); }
inline bool operator!=(const uint32_t &lhs) const { return !(*this == lhs); }
inline bool operator!=(const InternetProtocol::Version6 &lhs) const { return !(*this == lhs); }
inline uint32_t ToHex()
{
return ((uint64_t)this->Address[0] << 24) |
((uint64_t)this->Address[1] << 16) |
((uint64_t)this->Address[2] << 8) |
((uint64_t)this->Address[3]);
}
char *ToStringLittleEndian()
{
static char Buffer[40];
sprintf(Buffer, "%04X:%04X:%04X:%04X:%04X:%04X:%04X:%04X", this->Address[0], this->Address[1], this->Address[2], this->Address[3], this->Address[4], this->Address[5], this->Address[6], this->Address[7]);
return Buffer;
}
inline InternetProtocol4 FromHex(uint32_t Hex)
{
this->Address[0] = (uint8_t)((Hex >> 24) & 0xFF);
this->Address[1] = (uint8_t)((Hex >> 16) & 0xFF);
this->Address[2] = (uint8_t)((Hex >> 8) & 0xFF);
this->Address[3] = (uint8_t)(Hex & 0xFF);
return *this;
}
char *ToStringBigEndian()
{
static char Buffer[40];
sprintf(Buffer, "%04X:%04X:%04X:%04X:%04X:%04X:%04X:%04X", this->Address[7], this->Address[6], this->Address[5], this->Address[4], this->Address[3], this->Address[2], this->Address[1], this->Address[0]);
return Buffer;
}
} v6;
};
uint16_t CalculateChecksum(uint16_t *Data, uint64_t Length);

View File

@ -8,31 +8,141 @@ namespace NetworkNTP
{
struct NTPHeader
{
uint8_t LIv;
uint8_t VN;
uint8_t Mode;
/** @brief Leap indicator
* 00 - no warning
* 01 - last minute has 61 seconds
* 10 - last minute has 59 seconds
* 11 - alarm condition (clock not synchronized)
*/
uint8_t LeapIndicator : 2;
/** @brief Version number of the protocol
* 3 - IPv4 only
* 4 - IPv4, IPv6 and OSI
* 5 - IPv4, IPv6 and OSI
* 6 - IPv4, IPv6 and OSI
* 7 - IPv4, IPv6 and OSI
*/
uint8_t VersionNumber : 3;
/** @brief Mode
* 0 - reserved
* 1 - symmetric active
* 2 - symmetric passive
* 3 - client
* 4 - server
* 5 - broadcast
* 6 - reserved for NTP control message
* 7 - reserved for private use
*/
uint8_t Mode : 3;
/** @brief Stratum
* 0 - unspecified or unavailable
* 1 - primary reference (e.g. radio clock)
* 2-15 - secondary reference (via NTP or SNTP)
* 16 - unsynchronized
* 17-255 - reserved
*/
uint8_t Stratum;
/** @brief Polling interval
* 4 - 16 seconds
* 5 - 32 seconds
* 6 - 64 seconds
* 7 - 128 seconds
* 8 - 256 seconds
* 9 - 512 seconds
* 10 - 1024 seconds
* 11 - 2048 seconds
* 12 - 4096 seconds
* 13 - 8192 seconds
* 14 - 16384 seconds
* 15 - 32768 seconds
*/
uint8_t Poll;
/** @brief Precision
* -6 - 0.015625 seconds
* -5 - 0.03125 seconds
* -4 - 0.0625 seconds
* -3 - 0.125 seconds
* -2 - 0.25 seconds
* -1 - 0.5 seconds
* 0 - 1 second
* 1 - 2 seconds
* 2 - 4 seconds
* 3 - 8 seconds
* 4 - 16 seconds
* 5 - 32 seconds
* 6 - 64 seconds
* 7 - 128 seconds
*/
uint8_t Precision;
/** @brief Root delay
* Total round trip delay to the primary reference source
*/
uint32_t RootDelay;
/** @brief Root dispersion
* Nominal error relative to the primary reference source
*/
uint32_t RootDispersion;
uint32_t ReferenceID;
uint32_t ReferenceTimestamp;
uint32_t OriginateTimestamp;
uint32_t ReceiveTimestamp;
uint32_t TransmitTimestamp;
/** @brief Reference identifier
* 0x00000000 - unspecified
* 0x00000001 - radio clock
* 0x00000002 - atomic clock
* 0x00000003 - GPS receiver
* 0x00000004 - local oscillator
* 0x00000005 - LORAN-C receiver
* 0x00000006 - microprocessor
* 0x00000007 - internet
* 0x00000008 - FLL
* 0x00000009 - other
* 0x0000000A - WWV
* 0x0000000B - WWVB
* 0x0000000C - WWVH
* 0x0000000D - NIST dialup
* 0x0000000E - telephone
* 0x0000000F - reserved
*/
uint32_t ReferenceIdentifier;
/** @brief Reference timestamp
* The time at which the clock was last set or corrected
*/
uint32_t ReferenceTimestamp[2];
/** @brief Originate timestamp
* The time at which the request departed the client for the server
*/
uint32_t OriginateTimestamp[2];
/** @brief Receive timestamp
* The time at which the request arrived at the server
*/
uint32_t ReceiveTimestamp[2];
/** @brief Transmit timestamp
* The time at which the reply departed the server for the client
*/
uint32_t TransmitTimestamp[2];
/** @brief Message authentication code
* Key identifier
* @note Only when the NTP authentication scheme is used
*/
// uint32_t MessageAuthenticationCode;
} __attribute__((packed));
class NTP : public NetworkUDP::UDPEvents
{
private:
NetworkUDP::Socket *Socket;
NetworkUDP::Socket *UDPSocket;
bool TimeReceived = false;
NTPHeader NTPPacket;
virtual void OnUDPPacketReceived(NetworkUDP::Socket *Socket, uint8_t *Data, uint64_t Length);
public:
NTP(NetworkUDP::Socket *Socket);
~NTP();
void ReadTime();
/**
* @brief Get the time from the NTP server
*
* @return Unix Timestamp
*/
int ReadTime();
};
}

View File

@ -49,21 +49,21 @@ namespace NetworkUDP
UDP(NetworkIPv4::IPv4 *ipv4, NetworkInterfaceManager::DeviceInterface *Interface);
~UDP();
virtual Socket *Connect(InternetProtocol4 IP, uint16_t Port);
virtual Socket *Connect(InternetProtocol IP, uint16_t Port);
virtual Socket *Listen(uint16_t Port);
virtual void Disconnect(Socket *Socket);
virtual void Send(Socket *Socket, uint8_t *Data, uint64_t Length);
virtual void Bind(Socket *Socket, UDPEvents *EventHandler);
virtual bool OnIPv4PacketReceived(InternetProtocol4 SourceIP, InternetProtocol4 DestinationIP, uint8_t *Data, uint64_t Length);
virtual bool OnIPv4PacketReceived(InternetProtocol SourceIP, InternetProtocol DestinationIP, uint8_t *Data, uint64_t Length);
};
class Socket
{
public:
InternetProtocol4 LocalIP = {.Address = {0xFF, 0xFF, 0xFF, 0xFF}};
InternetProtocol LocalIP;
uint16_t LocalPort = 0;
InternetProtocol4 RemoteIP = {.Address = {0xFF, 0xFF, 0xFF, 0xFF}};
InternetProtocol RemoteIP;
uint16_t RemotePort = 0;
bool Listening = false;
UDPEvents *EventHandler = nullptr;