1
0
mirror of https://github.com/anope/anope.git synced 2026-07-01 02:46:37 +02:00

Add support for linking over UNIX sockets.

This commit is contained in:
Sadie Powell
2022-12-15 13:07:22 +00:00
parent 5fa3d8f929
commit dfdcd3021a
13 changed files with 85 additions and 43 deletions
+25 -10
View File
@@ -52,6 +52,8 @@ size_t sockaddrs::size() const
return sizeof(sa4);
case AF_INET6:
return sizeof(sa6);
case AF_UNIX:
return sizeof(saun);
default:
break;
}
@@ -67,6 +69,8 @@ int sockaddrs::port() const
return ntohs(sa4.sin_port);
case AF_INET6:
return ntohs(sa6.sin6_port);
case AF_UNIX:
return 0;
default:
break;
}
@@ -88,6 +92,8 @@ Anope::string sockaddrs::addr() const
if (inet_ntop(AF_INET6, &sa6.sin6_addr, address, sizeof(address)))
return address;
break;
case AF_UNIX:
return saun.sun_path;
default:
break;
}
@@ -187,6 +193,15 @@ void sockaddrs::pton(int type, const Anope::string &address, int pport)
}
break;
}
case AF_UNIX:
{
if (address.length() < sizeof(saun.sun_path))
{
saun.sun_family = AF_UNIX;
memcpy(&saun.sun_path, address.c_str(), address.length() + 1);
}
break;
}
default:
break;
}
@@ -432,7 +447,7 @@ SocketFlag SocketIO::FinishAccept(ClientSocket *cs)
void SocketIO::Bind(Socket *s, const Anope::string &ip, int port)
{
s->bindaddr.pton(s->IsIPv6() ? AF_INET6 : AF_INET, ip, port);
s->bindaddr.pton(s->GetFamily(), ip, port);
if (bind(s->GetFD(), &s->bindaddr.sa, s->bindaddr.size()) == -1)
throw SocketException("Unable to bind to address: " + Anope::LastError());
}
@@ -440,7 +455,7 @@ void SocketIO::Bind(Socket *s, const Anope::string &ip, int port)
void SocketIO::Connect(ConnectionSocket *s, const Anope::string &target, int port)
{
s->flags[SF_CONNECTING] = s->flags[SF_CONNECTED] = false;
s->conaddr.pton(s->IsIPv6() ? AF_INET6 : AF_INET, target, port);
s->conaddr.pton(s->GetFamily(), target, port);
int c = connect(s->GetFD(), &s->conaddr.sa, s->conaddr.size());
if (c == -1)
{
@@ -488,12 +503,12 @@ Socket::Socket()
throw CoreException("Socket::Socket() ?");
}
Socket::Socket(int s, bool i, int type)
Socket::Socket(int s, bool f, int type)
{
this->io = &NormalSocketIO;
this->ipv6 = i;
this->family = f;
if (s == -1)
this->sock = socket(this->ipv6 ? AF_INET6 : AF_INET, type, 0);
this->sock = socket(this->family, type, 0);
else
this->sock = s;
this->SetBlocking(false);
@@ -510,16 +525,16 @@ Socket::~Socket()
SocketEngine::Sockets.erase(this->sock);
}
int Socket::GetFamily() const
{
return family;
}
int Socket::GetFD() const
{
return sock;
}
bool Socket::IsIPv6() const
{
return ipv6;
}
bool Socket::SetBlocking(bool state)
{
int f = fcntl(this->GetFD(), F_GETFL, 0);