From 6551964ad66de7bc7ccb636c54351be2a260612b Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 23 May 2025 23:31:58 +0200 Subject: feat(kernel/libSystem): Lots of improvements, see below. what: - Reworked NetworkDevice. - Reworked RTL8139 driver. - Don't assert fCleanup on NetworkDevice destructor. - Add new Ref types in libSystem. Signed-off-by: Amlal El Mahrouss --- dev/kernel/src/FS/Ext2+FileSystemParser.cc | 2 +- dev/kernel/src/FS/HeFS+FileSystemParser.cc | 2 +- dev/kernel/src/FS/NeFS+FileSystemParser.cc | 2 +- dev/kernel/src/Network/NetworkDevice.cc | 6 +- dev/kernel/src/User.cc | 129 ----------------------------- dev/kernel/src/UserMgr.cc | 129 +++++++++++++++++++++++++++++ dev/kernel/src/UserProcessScheduler.cc | 1 - 7 files changed, 135 insertions(+), 136 deletions(-) delete mode 100644 dev/kernel/src/User.cc create mode 100644 dev/kernel/src/UserMgr.cc (limited to 'dev/kernel/src') diff --git a/dev/kernel/src/FS/Ext2+FileSystemParser.cc b/dev/kernel/src/FS/Ext2+FileSystemParser.cc index a3acac73..80449ed9 100644 --- a/dev/kernel/src/FS/Ext2+FileSystemParser.cc +++ b/dev/kernel/src/FS/Ext2+FileSystemParser.cc @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/dev/kernel/src/FS/HeFS+FileSystemParser.cc b/dev/kernel/src/FS/HeFS+FileSystemParser.cc index 155c9b9f..a18abaf8 100644 --- a/dev/kernel/src/FS/HeFS+FileSystemParser.cc +++ b/dev/kernel/src/FS/HeFS+FileSystemParser.cc @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/dev/kernel/src/FS/NeFS+FileSystemParser.cc b/dev/kernel/src/FS/NeFS+FileSystemParser.cc index f1746a58..97b2ff36 100644 --- a/dev/kernel/src/FS/NeFS+FileSystemParser.cc +++ b/dev/kernel/src/FS/NeFS+FileSystemParser.cc @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/dev/kernel/src/Network/NetworkDevice.cc b/dev/kernel/src/Network/NetworkDevice.cc index 51c43b48..02cb0e73 100644 --- a/dev/kernel/src/Network/NetworkDevice.cc +++ b/dev/kernel/src/Network/NetworkDevice.cc @@ -11,7 +11,7 @@ namespace Kernel { /// \brief Getter for fNetworkName. /// \return Network device name. const Char* NetworkDevice::Name() const { - return this->fNetworkName; + return "/devices/net{}"; } /// \brief Setter for fNetworkName. @@ -20,9 +20,9 @@ Boolean NetworkDevice::Name(const Char* name) { if (*name == 0) return NO; - if (rt_string_len(name) > cNetworkNameLen) return NO; + if (rt_string_len(name) > kNetworkNameLen) return NO; - rt_copy_memory((VoidPtr) name, (VoidPtr) this->fNetworkName, rt_string_len(name)); + rt_copy_memory((VoidPtr) name, (VoidPtr) this->Name(), rt_string_len(this->Name())); return YES; } diff --git a/dev/kernel/src/User.cc b/dev/kernel/src/User.cc deleted file mode 100644 index 1859be12..00000000 --- a/dev/kernel/src/User.cc +++ /dev/null @@ -1,129 +0,0 @@ -/* - * ======================================================== - * - * NeKernel - * Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. - * - * File: User.cc - * Purpose: User class, used to provide authentication and security. - * - * ======================================================== - */ - -#include -#include -#include -#include -#include - -#define kStdUserType (0xEE) -#define kSuperUserType (0xEF) - -/// @file User.cc -/// @brief Multi-user support. - -namespace Kernel { -namespace Detail { - //////////////////////////////////////////////////////////// - /// \brief Constructs a password by hashing the password. - /// \param password password to hash. - /// \return the hashed password - //////////////////////////////////////////////////////////// - STATIC UInt64 user_fnv_generator(const Char* password, User* user) { - if (!password || !user) return 1; - if (*password == 0) return 1; - - kout << "user_fnv_generator: Hashing user password...\r"; - - const UInt64 FNV_OFFSET_BASIS = 0xcbf29ce484222325ULL; - const UInt64 FNV_PRIME = 0x100000001b3ULL; - - UInt64 hash = FNV_OFFSET_BASIS; - - while (*password) { - hash ^= (Utf8Char) (*password++); - hash *= FNV_PRIME; - } - - kout << "user_fnv_generator: Hashed user password.\r"; - - return 0; - } -} // namespace Detail - -//////////////////////////////////////////////////////////// -/// @brief User ring constructor. -//////////////////////////////////////////////////////////// -User::User(const Int32& sel, const Char* user_name) : mUserRing((UserRingKind) sel) { - MUST_PASS(sel >= 0); - rt_copy_memory((VoidPtr) user_name, this->mUserName, rt_string_len(user_name)); -} - -//////////////////////////////////////////////////////////// -/// @brief User ring constructor. -//////////////////////////////////////////////////////////// -User::User(const UserRingKind& ring_kind, const Char* user_name) : mUserRing(ring_kind) { - rt_copy_memory((VoidPtr) user_name, this->mUserName, rt_string_len(user_name)); -} - -//////////////////////////////////////////////////////////// -/// @brief User destructor class. -//////////////////////////////////////////////////////////// -User::~User() = default; - -Bool User::Save(const UserPublicKey password) noexcept { - if (!password || *password == 0) return No; - - this->mUserFNV = Detail::user_fnv_generator(password, this); - - kout << "User::Save: Saved password successfully...\r"; - - return Yes; -} - -Bool User::Login(const UserPublicKey password) noexcept { - if (!password || !*password) return No; - - // now check if the password matches. - if (this->mUserFNV == Detail::user_fnv_generator(password, this)) { - kout << "User::Login: Password matches.\r"; - return Yes; - } - - kout << "User::Login: Password doesn't match.\r"; - return No; -} - -Bool User::operator==(const User& lhs) { - return lhs.mUserRing == this->mUserRing; -} - -Bool User::operator!=(const User& lhs) { - return lhs.mUserRing != this->mUserRing; -} - -//////////////////////////////////////////////////////////// -/// @brief Returns the user's name. -//////////////////////////////////////////////////////////// - -Char* User::Name() noexcept { - return this->mUserName; -} - -//////////////////////////////////////////////////////////// -/// @brief Returns the user's ring. -/// @return The king of ring the user is attached to. -//////////////////////////////////////////////////////////// - -const UserRingKind& User::Ring() noexcept { - return this->mUserRing; -} - -Bool User::IsStdUser() noexcept { - return this->Ring() == UserRingKind::kRingStdUser; -} - -Bool User::IsSuperUser() noexcept { - return this->Ring() == UserRingKind::kRingSuperUser; -} -} // namespace Kernel diff --git a/dev/kernel/src/UserMgr.cc b/dev/kernel/src/UserMgr.cc new file mode 100644 index 00000000..c41b445b --- /dev/null +++ b/dev/kernel/src/UserMgr.cc @@ -0,0 +1,129 @@ +/* + * ======================================================== + * + * NeKernel + * Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + * + * File: User.cc + * Purpose: User class, used to provide authentication and security. + * + * ======================================================== + */ + +#include +#include +#include +#include +#include + +#define kStdUserType (0xEE) +#define kSuperUserType (0xEF) + +/// @file User.cc +/// @brief Multi-user support. + +namespace Kernel { +namespace Detail { + //////////////////////////////////////////////////////////// + /// \brief Constructs a password by hashing the password. + /// \param password password to hash. + /// \return the hashed password + //////////////////////////////////////////////////////////// + STATIC UInt64 user_fnv_generator(const Char* password, User* user) { + if (!password || !user) return 1; + if (*password == 0) return 1; + + kout << "user_fnv_generator: Hashing user password...\r"; + + const UInt64 FNV_OFFSET_BASIS = 0xcbf29ce484222325ULL; + const UInt64 FNV_PRIME = 0x100000001b3ULL; + + UInt64 hash = FNV_OFFSET_BASIS; + + while (*password) { + hash ^= (Utf8Char) (*password++); + hash *= FNV_PRIME; + } + + kout << "user_fnv_generator: Hashed user password.\r"; + + return 0; + } +} // namespace Detail + +//////////////////////////////////////////////////////////// +/// @brief User ring constructor. +//////////////////////////////////////////////////////////// +User::User(const Int32& sel, const Char* user_name) : mUserRing((UserRingKind) sel) { + MUST_PASS(sel >= 0); + rt_copy_memory((VoidPtr) user_name, this->mUserName, rt_string_len(user_name)); +} + +//////////////////////////////////////////////////////////// +/// @brief User ring constructor. +//////////////////////////////////////////////////////////// +User::User(const UserRingKind& ring_kind, const Char* user_name) : mUserRing(ring_kind) { + rt_copy_memory((VoidPtr) user_name, this->mUserName, rt_string_len(user_name)); +} + +//////////////////////////////////////////////////////////// +/// @brief User destructor class. +//////////////////////////////////////////////////////////// +User::~User() = default; + +Bool User::Save(const UserPublicKey password) noexcept { + if (!password || *password == 0) return No; + + this->mUserFNV = Detail::user_fnv_generator(password, this); + + kout << "User::Save: Saved password successfully...\r"; + + return Yes; +} + +Bool User::Login(const UserPublicKey password) noexcept { + if (!password || !*password) return No; + + // now check if the password matches. + if (this->mUserFNV == Detail::user_fnv_generator(password, this)) { + kout << "User::Login: Password matches.\r"; + return Yes; + } + + kout << "User::Login: Password doesn't match.\r"; + return No; +} + +Bool User::operator==(const User& lhs) { + return lhs.mUserRing == this->mUserRing; +} + +Bool User::operator!=(const User& lhs) { + return lhs.mUserRing != this->mUserRing; +} + +//////////////////////////////////////////////////////////// +/// @brief Returns the user's name. +//////////////////////////////////////////////////////////// + +Char* User::Name() noexcept { + return this->mUserName; +} + +//////////////////////////////////////////////////////////// +/// @brief Returns the user's ring. +/// @return The king of ring the user is attached to. +//////////////////////////////////////////////////////////// + +const UserRingKind& User::Ring() noexcept { + return this->mUserRing; +} + +Bool User::IsStdUser() noexcept { + return this->Ring() == UserRingKind::kRingStdUser; +} + +Bool User::IsSuperUser() noexcept { + return this->Ring() == UserRingKind::kRingSuperUser; +} +} // namespace Kernel diff --git a/dev/kernel/src/UserProcessScheduler.cc b/dev/kernel/src/UserProcessScheduler.cc index 5996adca..77421f5f 100644 --- a/dev/kernel/src/UserProcessScheduler.cc +++ b/dev/kernel/src/UserProcessScheduler.cc @@ -21,7 +21,6 @@ #include #include #include -#include "NeKit/Macros.h" ///! BUGS: 0 -- cgit v1.2.3