2014-12-03 02:00:36 +03:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "ListenSocket.h"
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include "ConnectionOrientedSocket.h"
|
2015-04-28 11:18:12 +03:00
|
|
|
#include "DataSocket.h"
|
2014-12-03 02:00:36 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
#include "UnixSocketConnector.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
|
|
|
//
|
|
|
|
// ListenSocketIO
|
|
|
|
//
|
|
|
|
|
2015-04-30 13:55:37 +03:00
|
|
|
class ListenSocketIO final
|
|
|
|
: public UnixSocketWatcher
|
|
|
|
, public SocketIOBase
|
2014-12-03 02:00:36 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
class ListenTask;
|
|
|
|
|
|
|
|
ListenSocketIO(MessageLoop* mIOLoop,
|
|
|
|
ListenSocket* aListenSocket,
|
|
|
|
UnixSocketConnector* aConnector,
|
|
|
|
const nsACString& aAddress);
|
|
|
|
~ListenSocketIO();
|
|
|
|
|
2015-04-30 13:55:37 +03:00
|
|
|
void GetSocketAddr(nsAString& aAddrStr) const;
|
2014-12-03 02:00:36 +03:00
|
|
|
|
|
|
|
// Task callback methods
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run bind/listen to prepare for further runs of accept()
|
|
|
|
*/
|
|
|
|
void Listen(ConnectionOrientedSocketIO* aCOSocketIO);
|
|
|
|
|
|
|
|
// I/O callback methods
|
|
|
|
//
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
void OnConnected() override;
|
|
|
|
void OnError(const char* aFunction, int aErrno) override;
|
|
|
|
void OnListening() override;
|
2015-05-08 10:43:01 +03:00
|
|
|
void OnSocketCanAcceptWithoutBlocking() override;
|
2014-12-03 02:00:36 +03:00
|
|
|
|
2015-04-30 13:55:37 +03:00
|
|
|
// Methods for |SocketIOBase|
|
|
|
|
//
|
|
|
|
|
|
|
|
SocketBase* GetSocketBase() override;
|
|
|
|
|
|
|
|
bool IsShutdownOnMainThread() const override;
|
|
|
|
bool IsShutdownOnIOThread() const override;
|
|
|
|
|
|
|
|
void ShutdownOnMainThread() override;
|
|
|
|
void ShutdownOnIOThread() override;
|
|
|
|
|
2014-12-03 02:00:36 +03:00
|
|
|
private:
|
|
|
|
void FireSocketError();
|
|
|
|
|
|
|
|
// Set up flags on file descriptor.
|
|
|
|
static bool SetSocketFlags(int aFd);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Consumer pointer. Non-thread safe RefPtr, so should only be manipulated
|
|
|
|
* directly from main thread. All non-main-thread accesses should happen with
|
|
|
|
* mIO as container.
|
|
|
|
*/
|
|
|
|
RefPtr<ListenSocket> mListenSocket;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connector object used to create the connection we are currently using.
|
|
|
|
*/
|
|
|
|
nsAutoPtr<UnixSocketConnector> mConnector;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If true, do not requeue whatever task we're running
|
|
|
|
*/
|
|
|
|
bool mShuttingDownOnIOThread;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Address we are connecting to, assuming we are creating a client connection.
|
|
|
|
*/
|
|
|
|
nsCString mAddress;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Size of the socket address struct
|
|
|
|
*/
|
|
|
|
socklen_t mAddrSize;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Address struct of the socket currently in use
|
|
|
|
*/
|
|
|
|
sockaddr_any mAddr;
|
|
|
|
|
|
|
|
ConnectionOrientedSocketIO* mCOSocketIO;
|
|
|
|
};
|
|
|
|
|
|
|
|
ListenSocketIO::ListenSocketIO(MessageLoop* mIOLoop,
|
|
|
|
ListenSocket* aListenSocket,
|
|
|
|
UnixSocketConnector* aConnector,
|
|
|
|
const nsACString& aAddress)
|
2015-04-28 11:18:12 +03:00
|
|
|
: UnixSocketWatcher(mIOLoop)
|
|
|
|
, SocketIOBase()
|
|
|
|
, mListenSocket(aListenSocket)
|
|
|
|
, mConnector(aConnector)
|
|
|
|
, mShuttingDownOnIOThread(false)
|
|
|
|
, mAddress(aAddress)
|
|
|
|
, mCOSocketIO(nullptr)
|
2014-12-03 02:00:36 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mListenSocket);
|
|
|
|
MOZ_ASSERT(mConnector);
|
|
|
|
}
|
|
|
|
|
|
|
|
ListenSocketIO::~ListenSocketIO()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(IsShutdownOnMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListenSocketIO::GetSocketAddr(nsAString& aAddrStr) const
|
|
|
|
{
|
|
|
|
if (!mConnector) {
|
|
|
|
NS_WARNING("No connector to get socket address from!");
|
|
|
|
aAddrStr.Truncate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mConnector->GetSocketAddr(mAddr, aAddrStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListenSocketIO::Listen(ConnectionOrientedSocketIO* aCOSocketIO)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
|
|
|
MOZ_ASSERT(mConnector);
|
|
|
|
MOZ_ASSERT(aCOSocketIO);
|
|
|
|
|
|
|
|
if (!IsOpen()) {
|
|
|
|
int fd = mConnector->Create();
|
|
|
|
if (fd < 0) {
|
|
|
|
NS_WARNING("Cannot create socket fd!");
|
|
|
|
FireSocketError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!SetSocketFlags(fd)) {
|
|
|
|
NS_WARNING("Cannot set socket flags!");
|
|
|
|
FireSocketError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SetFd(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
mCOSocketIO = aCOSocketIO;
|
|
|
|
|
|
|
|
// This will set things we don't particularly care about, but
|
|
|
|
// it will hand back the correct structure size which is what
|
|
|
|
// we do care about.
|
|
|
|
if (!mConnector->CreateAddr(true, mAddrSize, mAddr, nullptr)) {
|
|
|
|
NS_WARNING("Cannot create socket address!");
|
|
|
|
FireSocketError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calls OnListening on success, or OnError otherwise
|
|
|
|
nsresult rv = UnixSocketWatcher::Listen(
|
|
|
|
reinterpret_cast<struct sockaddr*>(&mAddr), mAddrSize);
|
|
|
|
NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListenSocketIO::OnConnected()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
|
|
|
|
|
|
|
NS_NOTREACHED("Invalid call to |ListenSocketIO::OnConnected|");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListenSocketIO::OnListening()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
|
|
|
MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING);
|
|
|
|
|
|
|
|
if (!mConnector->SetUpListenSocket(GetFd())) {
|
|
|
|
NS_WARNING("Could not set up listen socket!");
|
|
|
|
FireSocketError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddWatchers(READ_WATCHER, true);
|
2015-01-20 05:17:19 +03:00
|
|
|
|
|
|
|
/* We signal a successful 'connection' to a local address for listening. */
|
2015-04-29 12:19:28 +03:00
|
|
|
NS_DispatchToMainThread(
|
|
|
|
new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));
|
2014-12-03 02:00:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListenSocketIO::OnError(const char* aFunction, int aErrno)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
|
|
|
|
|
|
|
UnixFdWatcher::OnError(aFunction, aErrno);
|
|
|
|
FireSocketError();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListenSocketIO::FireSocketError()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
|
|
|
|
|
|
|
// Clean up watchers, statuses, fds
|
|
|
|
Close();
|
|
|
|
|
|
|
|
// Tell the main thread we've errored
|
2015-04-29 12:19:28 +03:00
|
|
|
NS_DispatchToMainThread(
|
|
|
|
new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_ERROR));
|
2014-12-03 02:00:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ListenSocketIO::SetSocketFlags(int aFd)
|
|
|
|
{
|
|
|
|
static const int reuseaddr = 1;
|
|
|
|
|
|
|
|
// Set socket addr to be reused even if kernel is still waiting to close
|
|
|
|
int res = setsockopt(aFd, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
&reuseaddr, sizeof(reuseaddr));
|
|
|
|
if (res < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set close-on-exec bit.
|
|
|
|
int flags = TEMP_FAILURE_RETRY(fcntl(aFd, F_GETFD));
|
|
|
|
if (-1 == flags) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
flags |= FD_CLOEXEC;
|
|
|
|
if (-1 == TEMP_FAILURE_RETRY(fcntl(aFd, F_SETFD, flags))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set non-blocking status flag.
|
|
|
|
flags = TEMP_FAILURE_RETRY(fcntl(aFd, F_GETFL));
|
|
|
|
if (-1 == flags) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
if (-1 == TEMP_FAILURE_RETRY(fcntl(aFd, F_SETFL, flags))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-08 10:43:01 +03:00
|
|
|
void
|
|
|
|
ListenSocketIO::OnSocketCanAcceptWithoutBlocking()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
|
|
|
MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING);
|
|
|
|
MOZ_ASSERT(mCOSocketIO);
|
|
|
|
|
|
|
|
struct sockaddr_storage addr;
|
|
|
|
socklen_t addrLen = sizeof(addr);
|
|
|
|
int fd = TEMP_FAILURE_RETRY(accept(GetFd(),
|
|
|
|
reinterpret_cast<struct sockaddr*>(&addr), &addrLen));
|
|
|
|
if (fd < 0) {
|
|
|
|
OnError("accept", errno);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoveWatchers(READ_WATCHER|WRITE_WATCHER);
|
|
|
|
|
|
|
|
mCOSocketIO->Accept(fd,
|
|
|
|
reinterpret_cast<union sockaddr_any*>(&addr),
|
|
|
|
addrLen);
|
|
|
|
}
|
|
|
|
|
2015-04-30 13:55:37 +03:00
|
|
|
// |SocketIOBase|
|
|
|
|
|
|
|
|
SocketBase*
|
|
|
|
ListenSocketIO::GetSocketBase()
|
|
|
|
{
|
|
|
|
return mListenSocket.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ListenSocketIO::IsShutdownOnMainThread() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
return mListenSocket == nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ListenSocketIO::IsShutdownOnIOThread() const
|
|
|
|
{
|
|
|
|
return mShuttingDownOnIOThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListenSocketIO::ShutdownOnMainThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!IsShutdownOnMainThread());
|
|
|
|
|
|
|
|
mListenSocket = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListenSocketIO::ShutdownOnIOThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!mShuttingDownOnIOThread);
|
|
|
|
|
|
|
|
Close(); // will also remove fd from I/O loop
|
|
|
|
mShuttingDownOnIOThread = true;
|
|
|
|
}
|
|
|
|
|
2014-12-03 02:00:36 +03:00
|
|
|
//
|
|
|
|
// Socket tasks
|
|
|
|
//
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class ListenSocketIO::ListenTask final
|
2014-12-03 02:00:36 +03:00
|
|
|
: public SocketIOTask<ListenSocketIO>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ListenTask(ListenSocketIO* aIO, ConnectionOrientedSocketIO* aCOSocketIO)
|
|
|
|
: SocketIOTask<ListenSocketIO>(aIO)
|
|
|
|
, mCOSocketIO(aCOSocketIO)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mCOSocketIO);
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
void Run() override
|
2014-12-03 02:00:36 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!IsCanceled()) {
|
|
|
|
GetIO()->Listen(mCOSocketIO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ConnectionOrientedSocketIO* mCOSocketIO;
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// UnixSocketConsumer
|
|
|
|
//
|
|
|
|
|
|
|
|
ListenSocket::ListenSocket()
|
|
|
|
: mIO(nullptr)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
ListenSocket::~ListenSocket()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListenSocket::Close()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!mIO) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// From this point on, we consider mIO as being deleted. We sever
|
|
|
|
// the relationship here so any future calls to listen or connect
|
|
|
|
// will create a new implementation.
|
|
|
|
mIO->ShutdownOnMainThread();
|
|
|
|
|
2015-04-29 12:19:28 +03:00
|
|
|
XRE_GetIOMessageLoop()->PostTask(FROM_HERE, new SocketIOShutdownTask(mIO));
|
2014-12-03 02:00:36 +03:00
|
|
|
|
|
|
|
mIO = nullptr;
|
|
|
|
|
|
|
|
NotifyDisconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ListenSocket::Listen(UnixSocketConnector* aConnector,
|
|
|
|
ConnectionOrientedSocket* aCOSocket)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aConnector);
|
|
|
|
MOZ_ASSERT(aCOSocket);
|
|
|
|
|
|
|
|
nsAutoPtr<UnixSocketConnector> connector(aConnector);
|
|
|
|
|
|
|
|
if (mIO) {
|
|
|
|
NS_WARNING("Socket already connecting/connected!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIO = new ListenSocketIO(
|
|
|
|
XRE_GetIOMessageLoop(), this, connector.forget(), EmptyCString());
|
|
|
|
|
|
|
|
// Prepared I/O object, now start listening.
|
|
|
|
return Listen(aCOSocket);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ListenSocket::Listen(ConnectionOrientedSocket* aCOSocket)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mIO);
|
|
|
|
MOZ_ASSERT(aCOSocket);
|
|
|
|
|
|
|
|
SetConnectionStatus(SOCKET_LISTENING);
|
|
|
|
|
|
|
|
XRE_GetIOMessageLoop()->PostTask(
|
|
|
|
FROM_HERE, new ListenSocketIO::ListenTask(mIO, aCOSocket->GetIO()));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListenSocket::GetSocketAddr(nsAString& aAddrStr)
|
|
|
|
{
|
|
|
|
aAddrStr.Truncate();
|
|
|
|
if (!mIO || GetConnectionStatus() != SOCKET_CONNECTED) {
|
|
|
|
NS_WARNING("No socket currently open!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mIO->GetSocketAddr(aAddrStr);
|
|
|
|
}
|
|
|
|
|
2015-04-30 13:55:37 +03:00
|
|
|
// |SocketBase|
|
|
|
|
|
|
|
|
void
|
|
|
|
ListenSocket::CloseSocket()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
2014-12-03 02:00:36 +03:00
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|