mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
LibCore: Add CSocket::bind() (virtual) and CSocket::listen().
These will be useful for implementing server sockets.
This commit is contained in:
parent
2f373a27a2
commit
be2b585ca6
Notes:
sideshowbarker
2024-07-19 13:02:45 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/be2b585ca6a
7 changed files with 42 additions and 4 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -6,4 +6,6 @@ class CLocalSocket final : public CSocket {
|
|||
public:
|
||||
explicit CLocalSocket(CObject* parent = nullptr);
|
||||
virtual ~CLocalSocket() override;
|
||||
|
||||
virtual bool bind(const CSocketAddress&) override;
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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&);
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -17,3 +17,8 @@ CTCPSocket::CTCPSocket(CObject* parent)
|
|||
CTCPSocket::~CTCPSocket()
|
||||
{
|
||||
}
|
||||
|
||||
bool CTCPSocket::bind(const CSocketAddress&)
|
||||
{
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
|
|
@ -6,5 +6,5 @@ public:
|
|||
explicit CTCPSocket(CObject* parent = nullptr);
|
||||
virtual ~CTCPSocket() override;
|
||||
|
||||
private:
|
||||
virtual bool bind(const CSocketAddress&) override;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue