mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 21:07:34 +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
7 changed files with 42 additions and 4 deletions
|
@ -1,5 +1,6 @@
|
||||||
#include <LibCore/CLocalSocket.h>
|
#include <LibCore/CLocalSocket.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
CLocalSocket::CLocalSocket(CObject* parent)
|
CLocalSocket::CLocalSocket(CObject* parent)
|
||||||
: CSocket(CSocket::Type::Local, parent)
|
: CSocket(CSocket::Type::Local, parent)
|
||||||
|
@ -17,3 +18,11 @@ CLocalSocket::CLocalSocket(CObject* parent)
|
||||||
CLocalSocket::~CLocalSocket()
|
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:
|
public:
|
||||||
explicit CLocalSocket(CObject* parent = nullptr);
|
explicit CLocalSocket(CObject* parent = nullptr);
|
||||||
virtual ~CLocalSocket() override;
|
virtual ~CLocalSocket() override;
|
||||||
|
|
||||||
|
virtual bool bind(const CSocketAddress&) override;
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,14 +2,13 @@
|
||||||
#include <LibCore/CSocket.h>
|
#include <LibCore/CSocket.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/un.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
CSocket::CSocket(Type type, CObject* parent)
|
CSocket::CSocket(Type type, CObject* parent)
|
||||||
: CIODevice(parent)
|
: CIODevice(parent)
|
||||||
|
@ -131,3 +130,10 @@ bool CSocket::send(const ByteBuffer& data)
|
||||||
ASSERT(nsent == data.size());
|
ASSERT(nsent == data.size());
|
||||||
return true;
|
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&, int port);
|
||||||
bool connect(const CSocketAddress&);
|
bool connect(const CSocketAddress&);
|
||||||
|
|
||||||
|
virtual bool bind(const CSocketAddress&) = 0;
|
||||||
|
|
||||||
|
bool listen();
|
||||||
|
|
||||||
ByteBuffer receive(int max_size);
|
ByteBuffer receive(int max_size);
|
||||||
bool send(const ByteBuffer&);
|
bool send(const ByteBuffer&);
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/IPv4Address.h>
|
#include <AK/IPv4Address.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/un.h>
|
||||||
|
|
||||||
class CSocketAddress {
|
class CSocketAddress {
|
||||||
public:
|
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:
|
private:
|
||||||
Type m_type { Type::Invalid };
|
Type m_type { Type::Invalid };
|
||||||
IPv4Address m_ipv4_address;
|
IPv4Address m_ipv4_address;
|
||||||
|
|
|
@ -17,3 +17,8 @@ CTCPSocket::CTCPSocket(CObject* parent)
|
||||||
CTCPSocket::~CTCPSocket()
|
CTCPSocket::~CTCPSocket()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CTCPSocket::bind(const CSocketAddress&)
|
||||||
|
{
|
||||||
|
ASSERT_NOT_REACHED();
|
||||||
|
}
|
||||||
|
|
|
@ -6,5 +6,5 @@ public:
|
||||||
explicit CTCPSocket(CObject* parent = nullptr);
|
explicit CTCPSocket(CObject* parent = nullptr);
|
||||||
virtual ~CTCPSocket() override;
|
virtual ~CTCPSocket() override;
|
||||||
|
|
||||||
private:
|
virtual bool bind(const CSocketAddress&) override;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue