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() { ...@@ -56,8 +56,10 @@ AsyncUDPSocket::~AsyncUDPSocket() {
} }
void AsyncUDPSocket::bind(const folly::SocketAddress& address) { void AsyncUDPSocket::bind(const folly::SocketAddress& address) {
NetworkSocket socket = NetworkSocket socket = netops::socket(
netops::socket(address.getFamily(), SOCK_DGRAM, IPPROTO_UDP); address.getFamily(),
SOCK_DGRAM,
address.getFamily() != AF_UNIX ? IPPROTO_UDP : 0);
if (socket == NetworkSocket()) { if (socket == NetworkSocket()) {
throw AsyncSocketException( throw AsyncSocketException(
AsyncSocketException::NOT_OPEN, AsyncSocketException::NOT_OPEN,
...@@ -171,7 +173,7 @@ void AsyncUDPSocket::bind(const folly::SocketAddress& address) { ...@@ -171,7 +173,7 @@ void AsyncUDPSocket::bind(const folly::SocketAddress& address) {
// attach to EventHandler // attach to EventHandler
EventHandler::changeHandlerFD(fd_); EventHandler::changeHandlerFD(fd_);
if (address.getPort() != 0) { if (address.getFamily() == AF_UNIX || address.getPort() != 0) {
localAddress_ = address; localAddress_ = address;
} else { } else {
localAddress_.setFromLocalAddress(fd_); localAddress_.setFromLocalAddress(fd_);
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <folly/portability/GMock.h> #include <folly/portability/GMock.h>
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
#include <folly/portability/Sockets.h> #include <folly/portability/Sockets.h>
#include <folly/portability/Unistd.h>
using folly::AsyncTimeout; using folly::AsyncTimeout;
using folly::AsyncUDPServerSocket; using folly::AsyncUDPServerSocket;
...@@ -612,6 +613,19 @@ TEST_F(AsyncUDPSocketTest, TestBound) { ...@@ -612,6 +613,19 @@ TEST_F(AsyncUDPSocketTest, TestBound) {
EXPECT_TRUE(socket.isBound()); 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) { TEST_F(AsyncUDPSocketTest, TestAttachAfterDetachEvbWithReadCallback) {
socket_->resumeRead(&readCb); socket_->resumeRead(&readCb);
EXPECT_TRUE(socket_->isHandlerRegistered()); 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