Commit 8cc6a2f2 authored by Amol Bhave's avatar Amol Bhave Committed by Facebook Github Bot

Add support for UNIX datagram sockets to AsyncUDPSocket

Summary:
Add support for AF_UNIX datagram sockets in AsyncUDPSocket class.
For UNIX sockets, the third argument to the socket() call should be 0,
and getPort() throws. Fix these instances.

Reviewed By: yfeldblum

Differential Revision: D15073543

fbshipit-source-id: 14627d256a7f15104cbc22bc4b5ce03694c90405
parent 90e278b9
......@@ -56,8 +56,10 @@ AsyncUDPSocket::~AsyncUDPSocket() {
}
void AsyncUDPSocket::bind(const folly::SocketAddress& address) {
NetworkSocket socket =
netops::socket(address.getFamily(), SOCK_DGRAM, IPPROTO_UDP);
NetworkSocket socket = netops::socket(
address.getFamily(),
SOCK_DGRAM,
address.getFamily() != AF_UNIX ? IPPROTO_UDP : 0);
if (socket == NetworkSocket()) {
throw AsyncSocketException(
AsyncSocketException::NOT_OPEN,
......@@ -171,7 +173,7 @@ void AsyncUDPSocket::bind(const folly::SocketAddress& address) {
// attach to EventHandler
EventHandler::changeHandlerFD(fd_);
if (address.getPort() != 0) {
if (address.getFamily() == AF_UNIX || address.getPort() != 0) {
localAddress_ = address;
} else {
localAddress_.setFromLocalAddress(fd_);
......
......@@ -27,6 +27,7 @@
#include <folly/portability/GMock.h>
#include <folly/portability/GTest.h>
#include <folly/portability/Sockets.h>
#include <folly/portability/Unistd.h>
using folly::AsyncTimeout;
using folly::AsyncUDPServerSocket;
......@@ -612,6 +613,19 @@ TEST_F(AsyncUDPSocketTest, TestBound) {
EXPECT_TRUE(socket.isBound());
}
TEST_F(AsyncUDPSocketTest, TestBoundUnixSocket) {
const auto kTmpUnixSocketPath{"/tmp/tmp_unix_socket"};
AsyncUDPSocket socket(&evb_);
EXPECT_FALSE(socket.isBound());
SCOPE_EXIT {
::unlink(kTmpUnixSocketPath);
};
::unlink(kTmpUnixSocketPath);
socket.bind(folly::SocketAddress::makeFromPath(kTmpUnixSocketPath));
EXPECT_TRUE(socket.isBound());
socket.close();
}
TEST_F(AsyncUDPSocketTest, TestAttachAfterDetachEvbWithReadCallback) {
socket_->resumeRead(&readCb);
EXPECT_TRUE(socket_->isHandlerRegistered());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment