2022-08-28 19:02:06 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: AuNetSocket.Unix.cpp
|
|
|
|
Date: 2022-8-26
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include "Networking.hpp"
|
|
|
|
#include "AuNetSocket.hpp"
|
|
|
|
#include "AuNetEndpoint.hpp"
|
|
|
|
#include "AuNetWorker.hpp"
|
|
|
|
#include "AuIPAddress.hpp"
|
|
|
|
#include "AuNetError.hpp"
|
|
|
|
#include <Source/IO/Loop/LSHandle.hpp>
|
2023-08-19 14:05:14 +00:00
|
|
|
#if !defined(SOCK_CLOEXEC)
|
|
|
|
#define SOCK_CLOEXEC 0
|
|
|
|
#endif
|
2022-08-28 19:02:06 +00:00
|
|
|
|
|
|
|
namespace Aurora::IO::Net
|
|
|
|
{
|
|
|
|
static NetError GetLastNetError()
|
|
|
|
{
|
|
|
|
NetError error;
|
|
|
|
NetError_SetCurrent(error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
Socket::Socket(struct NetInterface *pInterface,
|
|
|
|
struct NetWorker *pWorker,
|
|
|
|
const AuSPtr<ISocketDriver> &pSocketDriver,
|
2023-11-29 13:55:01 +00:00
|
|
|
AuUInt osHandle,
|
2023-12-04 21:11:32 +00:00
|
|
|
AuSPtr<ISocketServer> pParent,
|
|
|
|
SocketServer *pParent2) :
|
|
|
|
SocketBase(pInterface, pWorker, pSocketDriver, osHandle, pParent, pParent2)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Socket::Socket(struct NetInterface *pInterface,
|
|
|
|
struct NetWorker *pWorker,
|
|
|
|
const AuSPtr<ISocketDriver> &pSocketDriver,
|
2023-08-11 15:51:42 +00:00
|
|
|
const AuPair<NetHostname, AuUInt16> &endpoint,
|
|
|
|
AuNet::ETransportProtocol eProtocol) :
|
|
|
|
SocketBase(pInterface, pWorker, pSocketDriver, endpoint, eProtocol)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Socket::Socket(struct NetInterface *pInterface,
|
|
|
|
struct NetWorker *pWorker,
|
|
|
|
const AuSPtr<ISocketDriver> &pSocketDriver,
|
|
|
|
const NetEndpoint &endpoint) :
|
2022-08-28 19:02:06 +00:00
|
|
|
SocketBase(pInterface, pWorker, pSocketDriver, endpoint)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Socket::Socket(NetInterface *pInterface,
|
|
|
|
NetWorker *pWorker,
|
|
|
|
const AuSPtr<ISocketDriver> &pSocketDriver,
|
|
|
|
const NetSocketConnectMany &connectMany) :
|
|
|
|
SocketBase(pInterface, pWorker, pSocketDriver, connectMany)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Socket::~Socket()
|
|
|
|
{
|
|
|
|
CloseSocket();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Socket::CloseSocket()
|
|
|
|
{
|
2023-08-11 15:51:42 +00:00
|
|
|
this->osHandle_ = 0;
|
|
|
|
AuResetMember(this->osHandleOwner_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Socket::RenewSocket()
|
|
|
|
{
|
|
|
|
if (!this->SendPreestablish())
|
|
|
|
{
|
|
|
|
SysPushErrorIO("Preestablish drop");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-04 21:11:32 +00:00
|
|
|
if (this->bHasRemoteMany_ && this->connectMany_.names.size())
|
2023-08-11 15:51:42 +00:00
|
|
|
{
|
2023-09-23 18:33:08 +00:00
|
|
|
if (this->connectMany_.names[0].byEndpoint)
|
2023-09-23 08:25:18 +00:00
|
|
|
{
|
2023-09-23 18:33:08 +00:00
|
|
|
this->remoteEndpoint_ = this->connectMany_.names[0].byEndpoint.value();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto val = this->connectMany_.names[0].byHost.value();
|
|
|
|
this->remoteEndpoint_.ip = val.netHostname.address;
|
|
|
|
this->remoteEndpoint_.transportProtocol = val.protocol;
|
2023-09-23 08:25:18 +00:00
|
|
|
}
|
2023-08-11 15:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this->CloseSocket();
|
|
|
|
|
|
|
|
this->osHandle_ = ::socket(
|
2023-09-18 18:04:09 +00:00
|
|
|
IPToDomain(this->remoteEndpoint_),
|
|
|
|
TransportToPlatformType(this->remoteEndpoint_) | SOCK_CLOEXEC,
|
2023-08-11 15:51:42 +00:00
|
|
|
0
|
|
|
|
);
|
|
|
|
|
2023-08-19 14:05:14 +00:00
|
|
|
bool bMakeCloseExec { !bool(SOCK_CLOEXEC) };
|
|
|
|
|
|
|
|
if (this->osHandle_ == -1)
|
|
|
|
{
|
|
|
|
this->osHandle_ = ::socket(
|
|
|
|
IPToDomain(this->remoteEndpoint_),
|
|
|
|
TransportToPlatformType(this->remoteEndpoint_),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
|
|
|
bMakeCloseExec = true;
|
|
|
|
}
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
if (this->osHandle_ == -1)
|
|
|
|
{
|
|
|
|
this->SendErrorNoStream(GetLastNetError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-19 14:05:14 +00:00
|
|
|
if (bMakeCloseExec)
|
|
|
|
{
|
|
|
|
(void)this->MakeCloseonexec();
|
|
|
|
}
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
this->osHandleOwner_ = AuIO::IOHandleShared();
|
|
|
|
this->osHandleOwner_->InitFromPairMove((int)this->osHandle_, (int)this->osHandle_);
|
|
|
|
|
|
|
|
if (!this->PrepareConnectOperations())
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2023-08-11 15:51:42 +00:00
|
|
|
this->bForceFailConstruct_ = true;
|
|
|
|
return;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Socket::FinishConstructAsync()
|
|
|
|
{
|
2023-09-23 08:25:18 +00:00
|
|
|
if (this->resolveLater.size() || this->bResolving_)
|
2023-08-19 14:05:14 +00:00
|
|
|
{
|
2023-09-23 08:25:18 +00:00
|
|
|
if (!this->TryStartResolve())
|
|
|
|
{
|
|
|
|
this->SendErrorNoStream(GetLastNetError());
|
|
|
|
return;
|
|
|
|
}
|
2023-08-19 14:05:14 +00:00
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-23 08:25:18 +00:00
|
|
|
RenewSocket();
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::PrepareConnectOperations()
|
|
|
|
{
|
|
|
|
return this->MakeNonblocking();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Socket::UpdateNagleAnyThread(bool bDisableNagle)
|
|
|
|
{
|
|
|
|
if (this->remoteEndpoint_.transportProtocol != ETransportProtocol::eProtocolTCP)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int flag = bDisableNagle ? 1 : 0;
|
|
|
|
int result = setsockopt((int)this->osHandle_, /* socket affected */
|
|
|
|
IPPROTO_TCP, /* set option at TCP level */
|
|
|
|
TCP_NODELAY, /* name of option */
|
|
|
|
(char *) &flag, /* the cast is historical cruft */
|
|
|
|
sizeof(int));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::UpdateLocalEndpoint()
|
|
|
|
{
|
|
|
|
socklen_t iLen { (int)this->endpointSize_ };
|
|
|
|
|
|
|
|
if (::getsockname(this->osHandle_, (sockaddr *)this->localEndpoint_.hint, &iLen) == -1)
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeoptimizeEndpoint(this->localEndpoint_);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::UpdateRemoteEndpoint()
|
|
|
|
{
|
|
|
|
socklen_t iLen { (int)this->endpointSize_ };
|
|
|
|
|
|
|
|
if (::getpeername(this->osHandle_, (sockaddr *)this->remoteEndpoint_.hint, &iLen) == -1)
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeoptimizeEndpoint(this->remoteEndpoint_);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::TryBindAnyLocal()
|
|
|
|
{
|
|
|
|
struct sockaddr_in addr {};
|
|
|
|
addr.sin_family = EIPProtocolIsValid(this->remoteEndpoint_.ip.ip) ?
|
|
|
|
IPToDomain(this->remoteEndpoint_) :
|
|
|
|
AF_UNSPEC;
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
addr.sin_port = 0;
|
|
|
|
return ::bind(this->osHandle_,
|
|
|
|
(sockaddr *)&addr, sizeof(addr)) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::ConnectOverlapped()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::ConnectNonblocking()
|
|
|
|
{
|
|
|
|
bool bStatus = ::connect(this->osHandle_,
|
|
|
|
AuReinterpretCast<sockaddr>(this->remoteEndpoint_.hint),
|
|
|
|
this->endpointSize_) == 0;
|
|
|
|
|
|
|
|
auto pWaitHandle = AuMakeShared<Loop::LSHandle>(-1, this->osHandle_);
|
|
|
|
if (!pWaitHandle)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->connectOperation.UpdateTrigger(pWaitHandle);
|
|
|
|
return this->connectOperation.FinishOperation(AuSharedFromThis(),
|
|
|
|
AuSPtr<INetWorker>(AuSharedFromThis(), this->ToWorker()),
|
|
|
|
bStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::ConnectBlocking()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::MakeNonblocking()
|
|
|
|
{
|
|
|
|
int flags = ::fcntl(this->osHandle_, F_GETFL, 0);
|
|
|
|
if (flags == -1)
|
|
|
|
{
|
|
|
|
NetError error;
|
|
|
|
NetError_SetCurrent(error);
|
|
|
|
this->SendErrorNoStream(error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
|
|
|
|
return ::fcntl(this->osHandle_, F_SETFL, flags) == 0;
|
|
|
|
}
|
|
|
|
|
2022-12-16 03:48:04 +00:00
|
|
|
bool Socket::MakeCloseonexec()
|
|
|
|
{
|
|
|
|
int flags = ::fcntl(this->osHandle_, F_GETFL, 0);
|
|
|
|
if (flags == -1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
flags |= FD_CLOEXEC;
|
|
|
|
|
|
|
|
return ::fcntl(this->osHandle_, F_SETFL, flags) == 0;
|
|
|
|
}
|
|
|
|
|
2022-12-16 00:41:01 +00:00
|
|
|
void Socket::Shutdown(bool bNow)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2022-12-16 00:41:01 +00:00
|
|
|
if (bNow)
|
|
|
|
{
|
|
|
|
this->SendEnd();
|
|
|
|
::shutdown(this->osHandle_, SHUT_RDWR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!this->socketChannel_.outputChannel.AsWritableByteBuffer()->RemainingBytes())
|
|
|
|
{
|
|
|
|
this->Shutdown(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->socketChannel_.outputChannel.bShutdownOnComplete = true;
|
|
|
|
this->socketChannel_.ScheduleOutOfFrameWrite();
|
|
|
|
}
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
}
|