Skip to content

feat(net): add Unix domain socket support to InetAddress and Socket - #399

Open
SBALAVIGNESH123 wants to merge 2 commits into
an-tao:masterfrom
SBALAVIGNESH123:master
Open

feat(net): add Unix domain socket support to InetAddress and Socket#399
SBALAVIGNESH123 wants to merge 2 commits into
an-tao:masterfrom
SBALAVIGNESH123:master

Conversation

@SBALAVIGNESH123

Copy link
Copy Markdown

This PR adds native Unix domain socket (AF_UNIX) support to Trantor's networking layer, enabling higher-performance same-host communication for applications like reverse proxy setups.

Changes

InetAddress

  • Added UnixDomainTag tag struct for type-safe constructor disambiguation
  • Added InetAddress(const std::string &unixPath, UnixDomainTag) constructor with 108-char path validation
  • Extended the address union with sockaddr_un for AF_UNIX addresses
  • Added isUnixDomain(), toUnixPath(), and unixDomainSockLen() helpers
  • All UDS code is guarded with #ifndef _WIN32

Socket

  • Fixed createNonblockingSocketOrDie: uses protocol = 0 for AF_UNIX (IPPROTO_TCP is invalid for Unix sockets)
  • Updated bindAddress() with unlink() before bind and chmod(0660) after for UDS
  • Updated accept() to use sockaddr_storage instead of sockaddr_in6 to accommodate the larger sockaddr_un structure
  • TCP-specific options (TCP_NODELAY) are correctly skipped for AF_UNIX sockets

Acceptor & TcpServer

  • Updated to pass sockaddr_storage through the accept path
  • Guard TCP-only socket options for UDS connections

All changes are C++14 compatible and gated behind #ifndef _WIN32 for zero Windows impact.

This is needed for drogonframework/drogon#2494 which adds UDS listener support to Drogon.

@SBALAVIGNESH123

Copy link
Copy Markdown
Author

Fixed the clang-format lint issues — all checks should be green now.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds Unix domain socket (AF_UNIX) support to Trantor’s networking primitives so servers/clients can use high-performance same-host IPC endpoints alongside existing TCP/IP addresses.

Changes:

  • Extend InetAddress to represent AF_UNIX endpoints (path storage, helpers, sockaddr length handling).
  • Update socket creation/bind/connect/accept paths to correctly support AF_UNIX (protocol selection, sockaddr sizing, TCP-only options gating).
  • Adjust accept pipeline (Acceptor/TcpServer) to carry a generic sockaddr container and handle UDS local addressing.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
trantor/net/inner/Socket.h Uses dynamic protocol selection and getSockAddrLen() for connect; nonblocking socket creation updated for UDS.
trantor/net/inner/Socket.cc Adds UDS bind behavior (unlink/chmod), broadens accept buffer to sockaddr_storage, and skips TCP options for UDS.
trantor/net/inner/Acceptor.cc Skips TCP/IP-only socket options and port auto-detection when listening on UDS.
trantor/net/TcpServer.cc Uses acceptor address as the local address for UDS connections.
trantor/net/InetAddress.h Adds UDS constructor/tag and helpers; introduces getSockAddrLen() for correct syscall sizing.
trantor/net/InetAddress.cc Implements UDS constructor/path accessors and updates string/utility methods for UDS.
Comments suppressed due to low confidence (1)

trantor/net/inner/Socket.h:50

  • On non-Linux builds, setNonBlockAndCloseOnExec(sock) is called even when ::socket() fails (sock < 0). That results in fcntl/ioctlsocket being invoked with an invalid fd and may clobber errno before the real error handling. Check sock < 0 immediately after ::socket() and only set flags on success.
        int sock = static_cast<int>(::socket(family, SOCK_STREAM, protocol));
        setNonBlockAndCloseOnExec(sock);
#endif
        if (sock < 0)
        {
            LOG_SYSERR << "sockets::createNonblockingOrDie";
            exit(1);

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

int connfd =
static_cast<int>(::accept(sockFd_, (struct sockaddr *)&addr6, &size));
static_cast<int>(::accept(sockFd_, (struct sockaddr *)&addr, &size));
setNonBlockAndCloseOnExec(connfd);
Comment on lines +65 to +69
if (localaddr.isUnixDomain())
{
// Remove stale socket file before binding
::unlink(localaddr.toUnixPath().c_str());
ret = ::bind(sockFd_,
Comment thread trantor/net/InetAddress.h
* @brief Constructs a Unix domain socket endpoint with the given path.
*
* @param unixPath The filesystem path for the Unix domain socket.
* @param tag Use trantor::UnixDomain to select this constructor.
Comment on lines +115 to +127
InetAddress::InetAddress(const std::string &unixPath, UnixDomainTag)
: isUnspecified_(false), isUnixDomain_(true)
{
memset(&addrUn_, 0, sizeof(addrUn_));
addrUn_.sun_family = AF_UNIX;
if (unixPath.size() >= sizeof(addrUn_.sun_path))
{
LOG_FATAL << "Unix socket path too long: " << unixPath << " (max "
<< sizeof(addrUn_.sun_path) - 1 << " chars)";
abort();
}
strncpy(addrUn_.sun_path, unixPath.c_str(), sizeof(addrUn_.sun_path) - 1);
}
Comment thread trantor/net/InetAddress.h
Comment on lines +274 to +289
/**
* @brief Return the size of the sockaddr struct appropriate for this
* address type.
*
* @return socklen_t
*/
socklen_t getSockAddrLen() const
{
#ifndef _WIN32
if (isUnixDomain_)
return static_cast<socklen_t>(sizeof(struct sockaddr_un));
#endif
if (isIpV6_)
return static_cast<socklen_t>(sizeof(struct sockaddr_in6));
return static_cast<socklen_t>(sizeof(struct sockaddr_in));
}
@an-tao

an-tao commented Apr 27, 2026

Copy link
Copy Markdown
Owner

@SBALAVIGNESH123 thanks so much for your patch, please refer to Copilot's suggestions and make some changes.

@SBALAVIGNESH123

Copy link
Copy Markdown
Author

Thanks @an-tao, will make the changes and update the PR.

Quick question — are there any open engineering positions on your team? I'm looking for full-time opportunities and would love to explore if there's a fit. I've been enjoying contributing to the project and would be excited to do it professionally.

@an-tao

an-tao commented Apr 28, 2026

Copy link
Copy Markdown
Owner

Thanks @an-tao, will make the changes and update the PR.

Quick question — are there any open engineering positions on your team? I'm looking for full-time opportunities and would love to explore if there's a fit. I've been enjoying contributing to the project and would be excited to do it professionally.

Sorry, we don't have such positions available. The maintainers of trantor and drogon are all volunteers and are not paid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants