Bladeren bron

Kernel: Allow Socket subclasses to fail construction

For example, socket(AF_INET) should only succeed for valid SOCK_TYPEs.
Andreas Kling 5 jaren geleden
bovenliggende
commit
03d73cbaae
5 gewijzigde bestanden met toevoegingen van 7 en 6 verwijderingen
  1. 4 2
      Kernel/Net/IPv4Socket.cpp
  2. 1 1
      Kernel/Net/IPv4Socket.h
  3. 1 1
      Kernel/Net/LocalSocket.cpp
  4. 1 1
      Kernel/Net/LocalSocket.h
  5. 0 1
      Kernel/Net/Socket.cpp

+ 4 - 2
Kernel/Net/IPv4Socket.cpp

@@ -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)

+ 1 - 1
Kernel/Net/IPv4Socket.h

@@ -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();

+ 1 - 1
Kernel/Net/LocalSocket.cpp

@@ -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));
 }

+ 1 - 1
Kernel/Net/LocalSocket.h

@@ -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&)>);

+ 0 - 1
Kernel/Net/Socket.cpp

@@ -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);