feat(net): add Unix domain socket support to InetAddress and Socket - #399
feat(net): add Unix domain socket support to InetAddress and Socket#399SBALAVIGNESH123 wants to merge 2 commits into
Conversation
…eptor, and TcpServer
|
Fixed the clang-format lint issues — all checks should be green now. |
There was a problem hiding this comment.
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
InetAddressto 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 infcntl/ioctlsocketbeing invoked with an invalid fd and may clobbererrnobefore the real error handling. Checksock < 0immediately 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); |
| if (localaddr.isUnixDomain()) | ||
| { | ||
| // Remove stale socket file before binding | ||
| ::unlink(localaddr.toUnixPath().c_str()); | ||
| ret = ::bind(sockFd_, |
| * @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. |
| 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); | ||
| } |
| /** | ||
| * @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)); | ||
| } |
|
@SBALAVIGNESH123 thanks so much for your patch, please refer to Copilot's suggestions and make some changes. |
|
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. |
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
UnixDomainTagtag struct for type-safe constructor disambiguationInetAddress(const std::string &unixPath, UnixDomainTag)constructor with 108-char path validationsockaddr_unfor AF_UNIX addressesisUnixDomain(),toUnixPath(), andunixDomainSockLen()helpers#ifndef _WIN32Socket
createNonblockingSocketOrDie: usesprotocol = 0for AF_UNIX (IPPROTO_TCP is invalid for Unix sockets)bindAddress()withunlink()before bind andchmod(0660)after for UDSaccept()to usesockaddr_storageinstead ofsockaddr_in6to accommodate the largersockaddr_unstructureTCP_NODELAY) are correctly skipped for AF_UNIX socketsAcceptor & TcpServer
sockaddr_storagethrough the accept pathAll changes are C++14 compatible and gated behind
#ifndef _WIN32for zero Windows impact.This is needed for drogonframework/drogon#2494 which adds UDS listener support to Drogon.