2003-11-15 03:13:59 +03:00
|
|
|
/* vim:set ts=2 sw=2 et cindent: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2003-11-15 03:13:59 +03:00
|
|
|
|
|
|
|
#include "nsSocketTransport2.h"
|
|
|
|
#include "nsServerSocket.h"
|
2006-05-11 02:49:04 +04:00
|
|
|
#include "nsProxyRelease.h"
|
2003-11-15 03:13:59 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2003-11-15 03:13:59 +03:00
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "prnetdb.h"
|
|
|
|
#include "prio.h"
|
2013-09-19 17:54:42 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2012-06-06 07:18:25 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-05-05 09:22:55 +04:00
|
|
|
#include "mozilla/Endian.h"
|
2012-12-24 01:08:43 +04:00
|
|
|
#include "mozilla/net/DNS.h"
|
2013-09-23 07:35:05 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsIFile.h"
|
2003-11-15 03:13:59 +03:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
using namespace mozilla;
|
2012-12-24 01:08:43 +04:00
|
|
|
using namespace mozilla::net;
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
|
2003-11-15 03:13:59 +03:00
|
|
|
static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
typedef void (nsServerSocket:: *nsServerSocketFunc)(void);
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
PostEvent(nsServerSocket *s, nsServerSocketFunc func)
|
|
|
|
{
|
2010-04-21 03:21:35 +04:00
|
|
|
nsCOMPtr<nsIRunnable> ev = NS_NewRunnableMethod(s, func);
|
2003-11-15 03:13:59 +03:00
|
|
|
if (!ev)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2011-01-19 00:39:21 +03:00
|
|
|
if (!gSocketTransportService)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
return gSocketTransportService->Dispatch(ev, NS_DISPATCH_NORMAL);
|
2003-11-15 03:13:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsServerSocket
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsServerSocket::nsServerSocket()
|
2014-09-19 19:25:00 +04:00
|
|
|
: mFD(nullptr)
|
|
|
|
, mLock("nsServerSocket.mLock")
|
2011-10-17 18:59:28 +04:00
|
|
|
, mAttached(false)
|
2013-03-27 05:22:40 +04:00
|
|
|
, mKeepWhenOffline(false)
|
2003-11-15 03:13:59 +03:00
|
|
|
{
|
|
|
|
// we want to be able to access the STS directly, and it may not have been
|
|
|
|
// constructed yet. the STS constructor sets gSocketTransportService.
|
|
|
|
if (!gSocketTransportService)
|
|
|
|
{
|
2011-01-19 00:39:21 +03:00
|
|
|
// This call can fail if we're offline, for example.
|
2003-11-15 03:13:59 +03:00
|
|
|
nsCOMPtr<nsISocketTransportService> sts =
|
|
|
|
do_GetService(kSocketTransportServiceCID);
|
|
|
|
}
|
|
|
|
// make sure the STS sticks around as long as we do
|
2011-01-19 00:39:21 +03:00
|
|
|
NS_IF_ADDREF(gSocketTransportService);
|
2003-11-15 03:13:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsServerSocket::~nsServerSocket()
|
|
|
|
{
|
|
|
|
Close(); // just in case :)
|
|
|
|
|
|
|
|
// release our reference to the STS
|
|
|
|
nsSocketTransportService *serv = gSocketTransportService;
|
2011-01-19 00:39:21 +03:00
|
|
|
NS_IF_RELEASE(serv);
|
2003-11-15 03:13:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsServerSocket::OnMsgClose()
|
|
|
|
{
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG(("nsServerSocket::OnMsgClose [this=%p]\n", this));
|
2003-11-15 03:13:59 +03:00
|
|
|
|
|
|
|
if (NS_FAILED(mCondition))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// tear down socket. this signals the STS to detach our socket handler.
|
|
|
|
mCondition = NS_BINDING_ABORTED;
|
|
|
|
|
|
|
|
// if we are attached, then we'll close the socket in our OnSocketDetached.
|
|
|
|
// otherwise, call OnSocketDetached from here.
|
|
|
|
if (!mAttached)
|
|
|
|
OnSocketDetached(mFD);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsServerSocket::OnMsgAttach()
|
|
|
|
{
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG(("nsServerSocket::OnMsgAttach [this=%p]\n", this));
|
2003-11-15 03:13:59 +03:00
|
|
|
|
|
|
|
if (NS_FAILED(mCondition))
|
|
|
|
return;
|
|
|
|
|
|
|
|
mCondition = TryAttach();
|
|
|
|
|
|
|
|
// if we hit an error while trying to attach then bail...
|
|
|
|
if (NS_FAILED(mCondition))
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mAttached, "should not be attached already");
|
|
|
|
OnSocketDetached(mFD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsServerSocket::TryAttach()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2011-01-19 00:39:21 +03:00
|
|
|
if (!gSocketTransportService)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2003-11-15 03:13:59 +03:00
|
|
|
//
|
|
|
|
// find out if it is going to be ok to attach another socket to the STS.
|
|
|
|
// if not then we have to wait for the STS to tell us that it is ok.
|
|
|
|
// the notification is asynchronous, which means that when we could be
|
|
|
|
// in a race to call AttachSocket once notified. for this reason, when
|
|
|
|
// we get notified, we just re-enter this function. as a result, we are
|
|
|
|
// sure to ask again before calling AttachSocket. in this way we deal
|
|
|
|
// with the race condition. though it isn't the most elegant solution,
|
|
|
|
// it is far simpler than trying to build a system that would guarantee
|
|
|
|
// FIFO ordering (which wouldn't even be that valuable IMO). see bug
|
|
|
|
// 194402 for more info.
|
|
|
|
//
|
|
|
|
if (!gSocketTransportService->CanAttachSocket())
|
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2010-04-21 03:21:35 +04:00
|
|
|
NS_NewRunnableMethod(this, &nsServerSocket::OnMsgAttach);
|
2003-11-15 03:13:59 +03:00
|
|
|
if (!event)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsresult rv = gSocketTransportService->NotifyWhenCanAttachSocket(event);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// ok, we can now attach our socket to the STS for polling
|
|
|
|
//
|
|
|
|
rv = gSocketTransportService->AttachSocket(mFD, this);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mAttached = true;
|
2003-11-15 03:13:59 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
// now, configure our poll flags for listening...
|
|
|
|
//
|
|
|
|
mPollFlags = (PR_POLL_READ | PR_POLL_EXCEPT);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-09-19 19:25:00 +04:00
|
|
|
void
|
|
|
|
nsServerSocket::CreateClientTransport(PRFileDesc* aClientFD,
|
|
|
|
const NetAddr& aClientAddr)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsSocketTransport> trans = new nsSocketTransport;
|
2014-09-19 19:25:00 +04:00
|
|
|
if (NS_WARN_IF(!trans)) {
|
|
|
|
mCondition = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = trans->InitWithConnectedSocket(aClientFD, &aClientAddr);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mCondition = rv;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mListener->OnSocketAccepted(this, trans);
|
|
|
|
}
|
|
|
|
|
2003-11-15 03:13:59 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsServerSocket::nsASocketHandler
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
nsServerSocket::OnSocketReady(PRFileDesc *fd, int16_t outFlags)
|
2003-11-15 03:13:59 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(mCondition), "oops");
|
|
|
|
NS_ASSERTION(mFD == fd, "wrong file descriptor");
|
2005-01-27 20:43:46 +03:00
|
|
|
NS_ASSERTION(outFlags != -1, "unexpected timeout condition reached");
|
2005-01-26 05:13:14 +03:00
|
|
|
|
2003-11-15 03:13:59 +03:00
|
|
|
if (outFlags & (PR_POLL_ERR | PR_POLL_HUP | PR_POLL_NVAL))
|
|
|
|
{
|
|
|
|
NS_WARNING("error polling on listening socket");
|
|
|
|
mCondition = NS_ERROR_UNEXPECTED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRFileDesc *clientFD;
|
2012-12-24 01:08:43 +04:00
|
|
|
PRNetAddr prClientAddr;
|
|
|
|
NetAddr clientAddr;
|
2003-11-15 03:13:59 +03:00
|
|
|
|
2013-09-06 19:06:23 +04:00
|
|
|
// NSPR doesn't tell us the peer address's length (as provided by the
|
|
|
|
// 'accept' system call), so we can't distinguish between named,
|
|
|
|
// unnamed, and abstract peer addresses. Clear prClientAddr first, so
|
|
|
|
// that the path will at least be reliably empty for unnamed and
|
|
|
|
// abstract addresses, and not garbage when the peer is unnamed.
|
|
|
|
memset(&prClientAddr, 0, sizeof(prClientAddr));
|
|
|
|
|
2012-12-24 01:08:43 +04:00
|
|
|
clientFD = PR_Accept(mFD, &prClientAddr, PR_INTERVAL_NO_WAIT);
|
|
|
|
PRNetAddrToNetAddr(&prClientAddr, &clientAddr);
|
2014-09-19 19:25:00 +04:00
|
|
|
if (!clientFD) {
|
2003-11-15 03:13:59 +03:00
|
|
|
NS_WARNING("PR_Accept failed");
|
|
|
|
mCondition = NS_ERROR_UNEXPECTED;
|
2014-09-19 19:25:00 +04:00
|
|
|
return;
|
2003-11-15 03:13:59 +03:00
|
|
|
}
|
2014-09-19 19:25:00 +04:00
|
|
|
|
|
|
|
// Accept succeeded, create socket transport and notify consumer
|
|
|
|
CreateClientTransport(clientFD, clientAddr);
|
2003-11-15 03:13:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsServerSocket::OnSocketDetached(PRFileDesc *fd)
|
|
|
|
{
|
|
|
|
// force a failure condition if none set; maybe the STS is shutting down :-/
|
|
|
|
if (NS_SUCCEEDED(mCondition))
|
|
|
|
mCondition = NS_ERROR_ABORT;
|
|
|
|
|
|
|
|
if (mFD)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mFD == fd, "wrong file descriptor");
|
|
|
|
PR_Close(mFD);
|
2012-07-30 18:20:58 +04:00
|
|
|
mFD = nullptr;
|
2003-11-15 03:13:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mListener)
|
|
|
|
{
|
|
|
|
mListener->OnStopListening(this, mCondition);
|
|
|
|
|
|
|
|
// need to atomically clear mListener. see our Close() method.
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIServerSocketListener *listener = nullptr;
|
2006-05-11 02:49:04 +04:00
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2006-05-11 02:49:04 +04:00
|
|
|
mListener.swap(listener);
|
|
|
|
}
|
|
|
|
// XXX we need to proxy the release to the listener's target thread to work
|
|
|
|
// around bug 337492.
|
|
|
|
if (listener)
|
|
|
|
NS_ProxyRelease(mListenerTarget, listener);
|
2003-11-15 03:13:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-18 03:45:10 +04:00
|
|
|
void
|
|
|
|
nsServerSocket::IsLocal(bool *aIsLocal)
|
|
|
|
{
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_UNIX)
|
2013-09-06 19:06:23 +04:00
|
|
|
// Unix-domain sockets are always local.
|
|
|
|
if (mAddr.raw.family == PR_AF_LOCAL)
|
|
|
|
{
|
|
|
|
*aIsLocal = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-09-18 03:45:10 +04:00
|
|
|
// If bound to loopback, this server socket only accepts local connections.
|
|
|
|
*aIsLocal = PR_IsNetAddrType(&mAddr, PR_IpAddrLoopback);
|
|
|
|
}
|
2003-11-15 03:13:59 +03:00
|
|
|
|
2013-03-27 05:22:40 +04:00
|
|
|
void
|
|
|
|
nsServerSocket::KeepWhenOffline(bool *aKeepWhenOffline)
|
|
|
|
{
|
|
|
|
*aKeepWhenOffline = mKeepWhenOffline;
|
|
|
|
}
|
|
|
|
|
2003-11-15 03:13:59 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsServerSocket::nsISupports
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsServerSocket, nsIServerSocket)
|
2003-11-15 03:13:59 +03:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsServerSocket::nsIServerSocket
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsServerSocket::Init(int32_t aPort, bool aLoopbackOnly, int32_t aBackLog)
|
2013-03-27 05:22:40 +04:00
|
|
|
{
|
|
|
|
return InitSpecialConnection(aPort, aLoopbackOnly ? LoopbackOnly : 0, aBackLog);
|
|
|
|
}
|
|
|
|
|
2013-09-06 19:06:23 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsServerSocket::InitWithFilename(nsIFile *aPath, uint32_t aPermissions, int32_t aBacklog)
|
|
|
|
{
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_UNIX)
|
2013-09-06 19:06:23 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsAutoCString path;
|
|
|
|
rv = aPath->GetNativePath(path);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
// Create a Unix domain PRNetAddr referring to the given path.
|
|
|
|
PRNetAddr addr;
|
|
|
|
if (path.Length() > sizeof(addr.local.path) - 1)
|
|
|
|
return NS_ERROR_FILE_NAME_TOO_LONG;
|
|
|
|
addr.local.family = PR_AF_LOCAL;
|
|
|
|
memcpy(addr.local.path, path.get(), path.Length());
|
|
|
|
addr.local.path[path.Length()] = '\0';
|
|
|
|
|
|
|
|
rv = InitWithAddress(&addr, aBacklog);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
return aPath->SetPermissions(aPermissions);
|
|
|
|
#else
|
|
|
|
return NS_ERROR_SOCKET_ADDRESS_NOT_SUPPORTED;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-03-27 05:22:40 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsServerSocket::InitSpecialConnection(int32_t aPort, nsServerSocketFlag aFlags,
|
|
|
|
int32_t aBackLog)
|
2003-11-15 03:13:59 +03:00
|
|
|
{
|
|
|
|
PRNetAddrValue val;
|
|
|
|
PRNetAddr addr;
|
|
|
|
|
|
|
|
if (aPort < 0)
|
|
|
|
aPort = 0;
|
2013-03-27 05:22:40 +04:00
|
|
|
if (aFlags & nsIServerSocket::LoopbackOnly)
|
2003-11-15 03:13:59 +03:00
|
|
|
val = PR_IpAddrLoopback;
|
|
|
|
else
|
|
|
|
val = PR_IpAddrAny;
|
|
|
|
PR_SetNetAddr(val, PR_AF_INET, aPort, &addr);
|
|
|
|
|
2013-03-27 05:22:40 +04:00
|
|
|
mKeepWhenOffline = ((aFlags & nsIServerSocket::KeepWhenOffline) != 0);
|
2003-11-15 03:13:59 +03:00
|
|
|
return InitWithAddress(&addr, aBackLog);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsServerSocket::InitWithAddress(const PRNetAddr *aAddr, int32_t aBackLog)
|
2003-11-15 03:13:59 +03:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(mFD == nullptr, NS_ERROR_ALREADY_INITIALIZED);
|
2014-09-19 19:25:00 +04:00
|
|
|
nsresult rv;
|
2003-11-15 03:13:59 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
// configure listening socket...
|
|
|
|
//
|
|
|
|
|
|
|
|
mFD = PR_OpenTCPSocket(aAddr->raw.family);
|
|
|
|
if (!mFD)
|
|
|
|
{
|
|
|
|
NS_WARNING("unable to create server socket");
|
2013-09-06 19:06:22 +04:00
|
|
|
return ErrorAccordingToNSPR(PR_GetError());
|
2003-11-15 03:13:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PRSocketOptionData opt;
|
|
|
|
|
|
|
|
opt.option = PR_SockOpt_Reuseaddr;
|
2011-10-17 18:59:28 +04:00
|
|
|
opt.value.reuse_addr = true;
|
2003-11-15 03:13:59 +03:00
|
|
|
PR_SetSocketOption(mFD, &opt);
|
|
|
|
|
|
|
|
opt.option = PR_SockOpt_Nonblocking;
|
2011-10-17 18:59:28 +04:00
|
|
|
opt.value.non_blocking = true;
|
2003-11-15 03:13:59 +03:00
|
|
|
PR_SetSocketOption(mFD, &opt);
|
|
|
|
|
|
|
|
if (PR_Bind(mFD, aAddr) != PR_SUCCESS)
|
|
|
|
{
|
|
|
|
NS_WARNING("failed to bind socket");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aBackLog < 0)
|
|
|
|
aBackLog = 5; // seems like a reasonable default
|
|
|
|
|
|
|
|
if (PR_Listen(mFD, aBackLog) != PR_SUCCESS)
|
|
|
|
{
|
|
|
|
NS_WARNING("cannot listen on socket");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the resulting socket address, which may be different than what
|
|
|
|
// we passed to bind.
|
|
|
|
if (PR_GetSockName(mFD, &mAddr) != PR_SUCCESS)
|
|
|
|
{
|
|
|
|
NS_WARNING("cannot get socket name");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-09-19 19:25:00 +04:00
|
|
|
// Set any additional socket defaults needed by child classes
|
|
|
|
rv = SetSocketDefaults();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2003-11-15 03:13:59 +03:00
|
|
|
// wait until AsyncListen is called before polling the socket for
|
|
|
|
// client connections.
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
fail:
|
2014-09-19 19:25:00 +04:00
|
|
|
rv = ErrorAccordingToNSPR(PR_GetError());
|
2003-11-15 03:13:59 +03:00
|
|
|
Close();
|
2013-09-06 19:06:22 +04:00
|
|
|
return rv;
|
2003-11-15 03:13:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsServerSocket::Close()
|
|
|
|
{
|
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2003-11-15 03:13:59 +03:00
|
|
|
// we want to proxy the close operation to the socket thread if a listener
|
|
|
|
// has been set. otherwise, we should just close the socket here...
|
|
|
|
if (!mListener)
|
|
|
|
{
|
|
|
|
if (mFD)
|
|
|
|
{
|
|
|
|
PR_Close(mFD);
|
2012-07-30 18:20:58 +04:00
|
|
|
mFD = nullptr;
|
2003-11-15 03:13:59 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PostEvent(this, &nsServerSocket::OnMsgClose);
|
|
|
|
}
|
|
|
|
|
2011-08-19 19:47:43 +04:00
|
|
|
namespace {
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class ServerSocketListenerProxy final : public nsIServerSocketListener
|
2011-08-19 19:47:43 +04:00
|
|
|
{
|
2014-06-24 20:36:44 +04:00
|
|
|
~ServerSocketListenerProxy() {}
|
|
|
|
|
2011-08-19 19:47:43 +04:00
|
|
|
public:
|
2014-08-05 17:20:24 +04:00
|
|
|
explicit ServerSocketListenerProxy(nsIServerSocketListener* aListener)
|
2013-03-19 07:39:54 +04:00
|
|
|
: mListener(new nsMainThreadPtrHolder<nsIServerSocketListener>(aListener))
|
2011-08-19 19:47:43 +04:00
|
|
|
, mTargetThread(do_GetCurrentThread())
|
|
|
|
{ }
|
|
|
|
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2011-08-19 19:47:43 +04:00
|
|
|
NS_DECL_NSISERVERSOCKETLISTENER
|
|
|
|
|
|
|
|
class OnSocketAcceptedRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2013-07-19 20:53:12 +04:00
|
|
|
OnSocketAcceptedRunnable(const nsMainThreadPtrHandle<nsIServerSocketListener>& aListener,
|
2011-08-19 19:47:43 +04:00
|
|
|
nsIServerSocket* aServ,
|
|
|
|
nsISocketTransport* aTransport)
|
|
|
|
: mListener(aListener)
|
|
|
|
, mServ(aServ)
|
|
|
|
, mTransport(aTransport)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
|
|
|
private:
|
2013-03-19 07:39:54 +04:00
|
|
|
nsMainThreadPtrHandle<nsIServerSocketListener> mListener;
|
2011-08-19 19:47:43 +04:00
|
|
|
nsCOMPtr<nsIServerSocket> mServ;
|
|
|
|
nsCOMPtr<nsISocketTransport> mTransport;
|
|
|
|
};
|
|
|
|
|
|
|
|
class OnStopListeningRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2013-07-19 20:53:12 +04:00
|
|
|
OnStopListeningRunnable(const nsMainThreadPtrHandle<nsIServerSocketListener>& aListener,
|
2011-08-19 19:47:43 +04:00
|
|
|
nsIServerSocket* aServ,
|
|
|
|
nsresult aStatus)
|
|
|
|
: mListener(aListener)
|
|
|
|
, mServ(aServ)
|
|
|
|
, mStatus(aStatus)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
|
|
|
private:
|
2013-03-19 07:39:54 +04:00
|
|
|
nsMainThreadPtrHandle<nsIServerSocketListener> mListener;
|
2011-08-19 19:47:43 +04:00
|
|
|
nsCOMPtr<nsIServerSocket> mServ;
|
|
|
|
nsresult mStatus;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2013-03-19 07:39:54 +04:00
|
|
|
nsMainThreadPtrHandle<nsIServerSocketListener> mListener;
|
2011-08-19 19:47:43 +04:00
|
|
|
nsCOMPtr<nsIEventTarget> mTargetThread;
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(ServerSocketListenerProxy,
|
|
|
|
nsIServerSocketListener)
|
2011-08-19 19:47:43 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ServerSocketListenerProxy::OnSocketAccepted(nsIServerSocket* aServ,
|
|
|
|
nsISocketTransport* aTransport)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<OnSocketAcceptedRunnable> r =
|
2011-08-19 19:47:43 +04:00
|
|
|
new OnSocketAcceptedRunnable(mListener, aServ, aTransport);
|
|
|
|
return mTargetThread->Dispatch(r, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ServerSocketListenerProxy::OnStopListening(nsIServerSocket* aServ,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<OnStopListeningRunnable> r =
|
2011-08-19 19:47:43 +04:00
|
|
|
new OnStopListeningRunnable(mListener, aServ, aStatus);
|
|
|
|
return mTargetThread->Dispatch(r, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ServerSocketListenerProxy::OnSocketAcceptedRunnable::Run()
|
|
|
|
{
|
|
|
|
mListener->OnSocketAccepted(mServ, mTransport);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ServerSocketListenerProxy::OnStopListeningRunnable::Run()
|
|
|
|
{
|
|
|
|
mListener->OnStopListening(mServ, mStatus);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2011-08-19 19:47:43 +04:00
|
|
|
|
2003-11-15 03:13:59 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsServerSocket::AsyncListen(nsIServerSocketListener *aListener)
|
|
|
|
{
|
|
|
|
// ensuring mFD implies ensuring mLock
|
|
|
|
NS_ENSURE_TRUE(mFD, NS_ERROR_NOT_INITIALIZED);
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(mListener == nullptr, NS_ERROR_IN_PROGRESS);
|
2003-11-15 03:13:59 +03:00
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2011-08-19 19:47:43 +04:00
|
|
|
mListener = new ServerSocketListenerProxy(aListener);
|
2006-05-11 02:49:04 +04:00
|
|
|
mListenerTarget = NS_GetCurrentThread();
|
2003-11-15 03:13:59 +03:00
|
|
|
}
|
2014-09-19 19:25:00 +04:00
|
|
|
|
|
|
|
// Child classes may need to do additional setup just before listening begins
|
|
|
|
nsresult rv = OnSocketListen();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2003-11-15 03:13:59 +03:00
|
|
|
return PostEvent(this, &nsServerSocket::OnMsgAttach);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsServerSocket::GetPort(int32_t *aResult)
|
2003-11-15 03:13:59 +03:00
|
|
|
{
|
|
|
|
// no need to enter the lock here
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t port;
|
2003-11-15 03:13:59 +03:00
|
|
|
if (mAddr.raw.family == PR_AF_INET)
|
|
|
|
port = mAddr.inet.port;
|
2013-09-06 19:06:23 +04:00
|
|
|
else if (mAddr.raw.family == PR_AF_INET6)
|
2003-11-15 03:13:59 +03:00
|
|
|
port = mAddr.ipv6.port;
|
2013-09-06 19:06:23 +04:00
|
|
|
else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2013-05-05 09:22:55 +04:00
|
|
|
*aResult = static_cast<int32_t>(NetworkEndian::readUint16(&port));
|
2003-11-15 03:13:59 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsServerSocket::GetAddress(PRNetAddr *aResult)
|
|
|
|
{
|
|
|
|
// no need to enter the lock here
|
|
|
|
memcpy(aResult, &mAddr, sizeof(mAddr));
|
|
|
|
return NS_OK;
|
|
|
|
}
|