Преглед изворни кода

LibCore: Add CSocket::bind() (virtual) and CSocket::listen().

These will be useful for implementing server sockets.
Andreas Kling пре 6 година
родитељ
комит
be2b585ca6

+ 9 - 0
Libraries/LibCore/CLocalSocket.cpp

@@ -1,5 +1,6 @@
 #include <LibCore/CLocalSocket.h>
 #include <sys/socket.h>
+#include <errno.h>
 
 CLocalSocket::CLocalSocket(CObject* parent)
     : CSocket(CSocket::Type::Local, parent)
@@ -17,3 +18,11 @@ CLocalSocket::CLocalSocket(CObject* parent)
 CLocalSocket::~CLocalSocket()
 {
 }
+
+bool CLocalSocket::bind(const CSocketAddress& address)
+{
+    auto un = address.to_sockaddr_un();
+    int rc = ::bind(fd(), (const sockaddr*)&un, sizeof(un));
+    set_error(errno);
+    return rc == 0;
+}

+ 2 - 0
Libraries/LibCore/CLocalSocket.h

@@ -6,4 +6,6 @@ class CLocalSocket final : public CSocket {
 public:
     explicit CLocalSocket(CObject* parent = nullptr);
     virtual ~CLocalSocket() override;
+
+    virtual bool bind(const CSocketAddress&) override;
 };

+ 9 - 3
Libraries/LibCore/CSocket.cpp

@@ -2,14 +2,13 @@
 #include <LibCore/CSocket.h>
 #include <arpa/inet.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <netdb.h>
 #include <netinet/in.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <unistd.h>
-#include <fcntl.h>
 #include <sys/socket.h>
-#include <sys/un.h>
+#include <unistd.h>
 
 CSocket::CSocket(Type type, CObject* parent)
     : CIODevice(parent)
@@ -131,3 +130,10 @@ bool CSocket::send(const ByteBuffer& data)
     ASSERT(nsent == data.size());
     return true;
 }
+
+bool CSocket::listen()
+{
+    int rc = ::listen(fd(), 5);
+    set_error(errno);
+    return rc == 0;
+}

+ 4 - 0
Libraries/LibCore/CSocket.h

@@ -22,6 +22,10 @@ public:
     bool connect(const CSocketAddress&, int port);
     bool connect(const CSocketAddress&);
 
+    virtual bool bind(const CSocketAddress&) = 0;
+
+    bool listen();
+
     ByteBuffer receive(int max_size);
     bool send(const ByteBuffer&);
 

+ 12 - 0
Libraries/LibCore/CSocketAddress.h

@@ -1,6 +1,8 @@
 #pragma once
 
 #include <AK/IPv4Address.h>
+#include <sys/socket.h>
+#include <sys/un.h>
 
 class CSocketAddress {
 public:
@@ -41,6 +43,16 @@ public:
         }
     }
 
+    sockaddr_un to_sockaddr_un() const
+    {
+        ASSERT(type() == Type::Local);
+        sockaddr_un address;
+        address.sun_family = AF_LOCAL;
+        RELEASE_ASSERT(m_local_address.length() < (int)sizeof(address.sun_path));
+        strcpy(address.sun_path, m_local_address.characters());
+        return address;
+    }
+
 private:
     Type m_type { Type::Invalid };
     IPv4Address m_ipv4_address;

+ 5 - 0
Libraries/LibCore/CTCPSocket.cpp

@@ -17,3 +17,8 @@ CTCPSocket::CTCPSocket(CObject* parent)
 CTCPSocket::~CTCPSocket()
 {
 }
+
+bool CTCPSocket::bind(const CSocketAddress&)
+{
+    ASSERT_NOT_REACHED();
+}

+ 1 - 1
Libraries/LibCore/CTCPSocket.h

@@ -6,5 +6,5 @@ public:
     explicit CTCPSocket(CObject* parent = nullptr);
     virtual ~CTCPSocket() override;
 
-private:
+    virtual bool bind(const CSocketAddress&) override;
 };