Kernel: Allow Socket subclasses to fail construction

For example, socket(AF_INET) should only succeed for valid SOCK_TYPEs.
This commit is contained in:
Andreas Kling 2020-01-23 18:11:14 +01:00
parent a93f35ac71
commit 03d73cbaae
5 changed files with 7 additions and 6 deletions

View file

@ -51,13 +51,15 @@ Lockable<HashTable<IPv4Socket*>>& IPv4Socket::all_sockets()
return *s_table;
}
NonnullRefPtr<IPv4Socket> IPv4Socket::create(int type, int protocol)
KResultOr<NonnullRefPtr<Socket>> IPv4Socket::create(int type, int protocol)
{
if (type == SOCK_STREAM)
return TCPSocket::create(protocol);
if (type == SOCK_DGRAM)
return UDPSocket::create(protocol);
return adopt(*new IPv4Socket(type, protocol));
if (type == SOCK_RAW)
return adopt(*new IPv4Socket(type, protocol));
return KResult(-EINVAL);
}
IPv4Socket::IPv4Socket(int type, int protocol)

View file

@ -41,7 +41,7 @@ class TCPSocket;
class IPv4Socket : public Socket {
public:
static NonnullRefPtr<IPv4Socket> create(int type, int protocol);
static KResultOr<NonnullRefPtr<Socket>> create(int type, int protocol);
virtual ~IPv4Socket() override;
static Lockable<HashTable<IPv4Socket*>>& all_sockets();

View file

@ -50,7 +50,7 @@ void LocalSocket::for_each(Function<void(LocalSocket&)> callback)
callback(socket);
}
NonnullRefPtr<LocalSocket> LocalSocket::create(int type)
KResultOr<NonnullRefPtr<Socket>> LocalSocket::create(int type)
{
return adopt(*new LocalSocket(type));
}

View file

@ -35,7 +35,7 @@ class FileDescription;
class LocalSocket final : public Socket, public InlineLinkedListNode<LocalSocket> {
friend class InlineLinkedListNode<LocalSocket>;
public:
static NonnullRefPtr<LocalSocket> create(int type);
static KResultOr<NonnullRefPtr<Socket>> create(int type);
virtual ~LocalSocket() override;
static void for_each(Function<void(LocalSocket&)>);

View file

@ -37,7 +37,6 @@
KResultOr<NonnullRefPtr<Socket>> Socket::create(int domain, int type, int protocol)
{
(void)protocol;
switch (domain) {
case AF_LOCAL:
return LocalSocket::create(type & SOCK_TYPE_MASK);