2014-04-15 23:38:55 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2003-11-15 03:13:59 +03:00
|
|
|
/* vim:set ts=4 sw=4 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-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
#include "nsSocketTransport2.h"
|
2013-07-10 13:57:34 +04:00
|
|
|
|
|
|
|
#include "mozilla/Attributes.h"
|
2015-07-01 14:51:00 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
#include "nsIOService.h"
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "nsNetSegmentUtils.h"
|
2012-02-29 15:19:00 +04:00
|
|
|
#include "nsNetAddr.h"
|
2003-10-06 05:46:31 +04:00
|
|
|
#include "nsTransportUtils.h"
|
2005-03-25 06:41:33 +03:00
|
|
|
#include "nsProxyInfo.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
#include "nsNetCID.h"
|
2013-05-28 17:48:23 +04:00
|
|
|
#include "nsNetUtil.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsAutoPtr.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "plstr.h"
|
|
|
|
#include "prerr.h"
|
2012-09-29 02:39:20 +04:00
|
|
|
#include "NetworkActivityMonitor.h"
|
2014-05-29 02:28:03 +04:00
|
|
|
#include "NSSErrorsService.h"
|
2016-11-21 06:43:06 +03:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2014-11-05 23:01:53 +03:00
|
|
|
#include "mozilla/net/NeckoChild.h"
|
2013-09-19 17:54:39 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
#include "nsISocketProviderService.h"
|
|
|
|
#include "nsISocketProvider.h"
|
|
|
|
#include "nsISSLSocketControl.h"
|
|
|
|
#include "nsIPipe.h"
|
2007-07-25 00:17:11 +04:00
|
|
|
#include "nsIClassInfoImpl.h"
|
2013-09-22 07:04:57 +04:00
|
|
|
#include "nsURLHelper.h"
|
|
|
|
#include "nsIDNSService.h"
|
|
|
|
#include "nsIDNSRecord.h"
|
|
|
|
#include "nsICancelable.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-02-06 23:51:38 +04:00
|
|
|
#include "nsPrintfCString.h"
|
2016-06-07 16:17:42 +03:00
|
|
|
#include "xpcpublic.h"
|
2014-02-06 23:51:38 +04:00
|
|
|
|
2013-08-26 03:56:53 +04:00
|
|
|
#if defined(XP_WIN)
|
2016-02-04 06:42:00 +03:00
|
|
|
#include "ShutdownLayer.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
#endif
|
|
|
|
|
2014-02-06 23:51:38 +04:00
|
|
|
/* Following inclusions required for keepalive config not supported by NSPR. */
|
|
|
|
#include "private/pprio.h"
|
|
|
|
#if defined(XP_WIN)
|
2014-02-07 15:51:08 +04:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <mstcpip.h>
|
2014-02-06 23:51:38 +04:00
|
|
|
#elif defined(XP_UNIX)
|
|
|
|
#include <errno.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#endif
|
|
|
|
/* End keepalive config inclusions. */
|
|
|
|
|
2015-07-01 14:51:00 +03:00
|
|
|
#define SUCCESSFUL_CONNECTING_TO_IPV4_ADDRESS 0
|
|
|
|
#define UNSUCCESSFUL_CONNECTING_TO_IPV4_ADDRESS 1
|
|
|
|
#define SUCCESSFUL_CONNECTING_TO_IPV6_ADDRESS 2
|
|
|
|
#define UNSUCCESSFUL_CONNECTING_TO_IPV6_ADDRESS 3
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static NS_DEFINE_CID(kSocketProviderServiceCID, NS_SOCKETPROVIDERSERVICE_CID);
|
|
|
|
static NS_DEFINE_CID(kDNSServiceCID, NS_DNSSERVICE_CID);
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2016-05-19 05:02:57 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class nsSocketEvent : public Runnable
|
2003-10-06 05:46:31 +04:00
|
|
|
{
|
|
|
|
public:
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketEvent(nsSocketTransport *transport, uint32_t type,
|
2012-07-30 18:20:58 +04:00
|
|
|
nsresult status = NS_OK, nsISupports *param = nullptr)
|
2006-05-10 21:30:15 +04:00
|
|
|
: mTransport(transport)
|
|
|
|
, mType(type)
|
2003-10-06 05:46:31 +04:00
|
|
|
, mStatus(status)
|
|
|
|
, mParam(param)
|
2006-05-10 21:30:15 +04:00
|
|
|
{}
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2003-10-06 05:46:31 +04:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
mTransport->OnSocketEvent(mType, mStatus, mParam);
|
|
|
|
return NS_OK;
|
2003-10-06 05:46:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsSocketTransport> mTransport;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mType;
|
2003-10-06 05:46:31 +04:00
|
|
|
nsresult mStatus;
|
|
|
|
nsCOMPtr<nsISupports> mParam;
|
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
//#define TEST_CONNECT_ERRORS
|
|
|
|
#ifdef TEST_CONNECT_ERRORS
|
|
|
|
#include <stdlib.h>
|
|
|
|
static PRErrorCode RandomizeConnectError(PRErrorCode code)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// To test out these errors, load http://www.yahoo.com/. It should load
|
2006-02-23 12:36:43 +03:00
|
|
|
// correctly despite the random occurrence of these errors.
|
2003-01-18 04:27:53 +03:00
|
|
|
//
|
|
|
|
int n = rand();
|
|
|
|
if (n > RAND_MAX/2) {
|
|
|
|
struct {
|
|
|
|
PRErrorCode err_code;
|
|
|
|
const char *err_name;
|
|
|
|
}
|
|
|
|
errors[] = {
|
|
|
|
//
|
|
|
|
// These errors should be recoverable provided there is another
|
2003-09-12 00:32:33 +04:00
|
|
|
// IP address in mDNSRecord.
|
2003-01-18 04:27:53 +03:00
|
|
|
//
|
|
|
|
{ PR_CONNECT_REFUSED_ERROR, "PR_CONNECT_REFUSED_ERROR" },
|
|
|
|
{ PR_CONNECT_TIMEOUT_ERROR, "PR_CONNECT_TIMEOUT_ERROR" },
|
|
|
|
//
|
|
|
|
// This error will cause this socket transport to error out;
|
|
|
|
// however, if the consumer is HTTP, then the HTTP transaction
|
|
|
|
// should be restarted when this error occurs.
|
|
|
|
//
|
|
|
|
{ PR_CONNECT_RESET_ERROR, "PR_CONNECT_RESET_ERROR" },
|
|
|
|
};
|
|
|
|
n = n % (sizeof(errors)/sizeof(errors[0]));
|
|
|
|
code = errors[n].err_code;
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG(("simulating NSPR error %d [%s]\n", code, errors[n].err_name));
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2013-03-03 12:04:38 +04:00
|
|
|
nsresult
|
2003-01-18 04:27:53 +03:00
|
|
|
ErrorAccordingToNSPR(PRErrorCode errorCode)
|
|
|
|
{
|
2007-02-18 07:24:05 +03:00
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
2003-01-18 04:27:53 +03:00
|
|
|
switch (errorCode) {
|
|
|
|
case PR_WOULD_BLOCK_ERROR:
|
|
|
|
rv = NS_BASE_STREAM_WOULD_BLOCK;
|
|
|
|
break;
|
|
|
|
case PR_CONNECT_ABORTED_ERROR:
|
|
|
|
case PR_CONNECT_RESET_ERROR:
|
|
|
|
rv = NS_ERROR_NET_RESET;
|
|
|
|
break;
|
|
|
|
case PR_END_OF_FILE_ERROR: // XXX document this correlation
|
|
|
|
rv = NS_ERROR_NET_INTERRUPT;
|
|
|
|
break;
|
|
|
|
case PR_CONNECT_REFUSED_ERROR:
|
2013-09-06 19:06:22 +04:00
|
|
|
// We lump the following NSPR codes in with PR_CONNECT_REFUSED_ERROR. We
|
|
|
|
// could get better diagnostics by adding distinct XPCOM error codes for
|
|
|
|
// each of these, but there are a lot of places in Gecko that check
|
|
|
|
// specifically for NS_ERROR_CONNECTION_REFUSED, all of which would need to
|
|
|
|
// be checked.
|
|
|
|
case PR_NETWORK_UNREACHABLE_ERROR:
|
|
|
|
case PR_HOST_UNREACHABLE_ERROR:
|
2004-06-16 19:09:34 +04:00
|
|
|
case PR_ADDRESS_NOT_AVAILABLE_ERROR:
|
2004-11-29 00:35:48 +03:00
|
|
|
// Treat EACCES as a soft error since (at least on Linux) connect() returns
|
|
|
|
// EACCES when an IPv6 connection is blocked by a firewall. See bug 270784.
|
|
|
|
case PR_NO_ACCESS_RIGHTS_ERROR:
|
2003-01-18 04:27:53 +03:00
|
|
|
rv = NS_ERROR_CONNECTION_REFUSED;
|
|
|
|
break;
|
2013-09-06 19:06:22 +04:00
|
|
|
case PR_ADDRESS_NOT_SUPPORTED_ERROR:
|
|
|
|
rv = NS_ERROR_SOCKET_ADDRESS_NOT_SUPPORTED;
|
|
|
|
break;
|
2003-01-28 22:19:08 +03:00
|
|
|
case PR_IO_TIMEOUT_ERROR:
|
2003-01-18 04:27:53 +03:00
|
|
|
case PR_CONNECT_TIMEOUT_ERROR:
|
|
|
|
rv = NS_ERROR_NET_TIMEOUT;
|
|
|
|
break;
|
2013-09-06 19:06:22 +04:00
|
|
|
case PR_OUT_OF_MEMORY_ERROR:
|
|
|
|
// These really indicate that the descriptor table filled up, or that the
|
|
|
|
// kernel ran out of network buffers - but nobody really cares which part of
|
|
|
|
// the system ran out of memory.
|
|
|
|
case PR_PROC_DESC_TABLE_FULL_ERROR:
|
|
|
|
case PR_SYS_DESC_TABLE_FULL_ERROR:
|
|
|
|
case PR_INSUFFICIENT_RESOURCES_ERROR:
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
case PR_ADDRESS_IN_USE_ERROR:
|
|
|
|
rv = NS_ERROR_SOCKET_ADDRESS_IN_USE;
|
|
|
|
break;
|
|
|
|
// These filename-related errors can arise when using Unix-domain sockets.
|
|
|
|
case PR_FILE_NOT_FOUND_ERROR:
|
|
|
|
rv = NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
break;
|
|
|
|
case PR_IS_DIRECTORY_ERROR:
|
|
|
|
rv = NS_ERROR_FILE_IS_DIRECTORY;
|
|
|
|
break;
|
|
|
|
case PR_LOOP_ERROR:
|
|
|
|
rv = NS_ERROR_FILE_UNRESOLVABLE_SYMLINK;
|
|
|
|
break;
|
|
|
|
case PR_NAME_TOO_LONG_ERROR:
|
|
|
|
rv = NS_ERROR_FILE_NAME_TOO_LONG;
|
|
|
|
break;
|
|
|
|
case PR_NO_DEVICE_SPACE_ERROR:
|
|
|
|
rv = NS_ERROR_FILE_NO_DEVICE_SPACE;
|
|
|
|
break;
|
|
|
|
case PR_NOT_DIRECTORY_ERROR:
|
|
|
|
rv = NS_ERROR_FILE_NOT_DIRECTORY;
|
|
|
|
break;
|
|
|
|
case PR_READ_ONLY_FILESYSTEM_ERROR:
|
|
|
|
rv = NS_ERROR_FILE_READ_ONLY;
|
|
|
|
break;
|
2016-12-21 13:20:28 +03:00
|
|
|
case PR_BAD_ADDRESS_ERROR:
|
|
|
|
rv = NS_ERROR_UNKNOWN_HOST;
|
|
|
|
break;
|
2003-01-18 04:27:53 +03:00
|
|
|
default:
|
2016-05-19 05:02:57 +03:00
|
|
|
if (psm::IsNSSErrorCode(errorCode)) {
|
|
|
|
rv = psm::GetXPCOMFromNSSError(errorCode);
|
2014-05-29 02:28:03 +04:00
|
|
|
}
|
2007-12-04 21:53:14 +03:00
|
|
|
break;
|
2013-09-06 19:06:22 +04:00
|
|
|
|
|
|
|
// NSPR's socket code can return these, but they're not worth breaking out
|
|
|
|
// into their own error codes, distinct from NS_ERROR_FAILURE:
|
|
|
|
//
|
|
|
|
// PR_BAD_DESCRIPTOR_ERROR
|
|
|
|
// PR_INVALID_ARGUMENT_ERROR
|
|
|
|
// PR_NOT_SOCKET_ERROR
|
|
|
|
// PR_NOT_TCP_SOCKET_ERROR
|
|
|
|
// These would indicate a bug internal to the component.
|
|
|
|
//
|
|
|
|
// PR_PROTOCOL_NOT_SUPPORTED_ERROR
|
|
|
|
// This means that we can't use the given "protocol" (like
|
|
|
|
// IPPROTO_TCP or IPPROTO_UDP) with a socket of the given type. As
|
|
|
|
// above, this indicates an internal bug.
|
|
|
|
//
|
|
|
|
// PR_IS_CONNECTED_ERROR
|
|
|
|
// This indicates that we've applied a system call like 'bind' or
|
|
|
|
// 'connect' to a socket that is already connected. The socket
|
|
|
|
// components manage each file descriptor's state, and in some cases
|
|
|
|
// handle this error result internally. We shouldn't be returning
|
|
|
|
// this to our callers.
|
|
|
|
//
|
|
|
|
// PR_IO_ERROR
|
|
|
|
// This is so vague that NS_ERROR_FAILURE is just as good.
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG(("ErrorAccordingToNSPR [in=%d out=%" PRIx32 "]\n", errorCode,
|
|
|
|
static_cast<uint32_t>(rv)));
|
2003-01-18 04:27:53 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// socket input stream impl
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsSocketInputStream::nsSocketInputStream(nsSocketTransport *trans)
|
|
|
|
: mTransport(trans)
|
|
|
|
, mReaderRefCnt(0)
|
|
|
|
, mCondition(NS_OK)
|
2003-10-06 05:46:31 +04:00
|
|
|
, mCallbackFlags(0)
|
2003-01-18 04:27:53 +03:00
|
|
|
, mByteCount(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSocketInputStream::~nsSocketInputStream()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// called on the socket transport thread...
|
|
|
|
//
|
|
|
|
// condition : failure code if socket has been closed
|
|
|
|
//
|
|
|
|
void
|
|
|
|
nsSocketInputStream::OnSocketReady(nsresult condition)
|
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG(("nsSocketInputStream::OnSocketReady [this=%p cond=%" PRIx32 "]\n",
|
|
|
|
this, static_cast<uint32_t>(condition)));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2003-10-09 05:54:07 +04:00
|
|
|
NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
nsCOMPtr<nsIInputStreamCallback> callback;
|
2003-01-18 04:27:53 +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(mTransport->mLock);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
// update condition, but be careful not to erase an already
|
|
|
|
// existing error condition.
|
|
|
|
if (NS_SUCCEEDED(mCondition))
|
|
|
|
mCondition = condition;
|
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
// ignore event if only waiting for closure and not closed.
|
|
|
|
if (NS_FAILED(mCondition) || !(mCallbackFlags & WAIT_CLOSURE_ONLY)) {
|
|
|
|
callback = mCallback;
|
2012-07-30 18:20:58 +04:00
|
|
|
mCallback = nullptr;
|
2003-10-06 05:46:31 +04:00
|
|
|
mCallbackFlags = 0;
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
if (callback)
|
|
|
|
callback->OnInputStreamReady(this);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE(nsSocketInputStream,
|
|
|
|
nsIInputStream,
|
|
|
|
nsIAsyncInputStream)
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2003-01-18 04:27:53 +03:00
|
|
|
nsSocketInputStream::AddRef()
|
|
|
|
{
|
2013-07-19 06:24:13 +04:00
|
|
|
++mReaderRefCnt;
|
2003-01-18 04:27:53 +03:00
|
|
|
return mTransport->AddRef();
|
|
|
|
}
|
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2003-01-18 04:27:53 +03:00
|
|
|
nsSocketInputStream::Release()
|
|
|
|
{
|
2013-07-19 06:24:13 +04:00
|
|
|
if (--mReaderRefCnt == 0)
|
2003-01-18 04:27:53 +03:00
|
|
|
Close();
|
|
|
|
return mTransport->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketInputStream::Close()
|
|
|
|
{
|
2003-10-06 05:46:31 +04:00
|
|
|
return CloseWithStatus(NS_BASE_STREAM_CLOSED);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketInputStream::Available(uint64_t *avail)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2013-06-05 03:32:31 +04:00
|
|
|
SOCKET_LOG(("nsSocketInputStream::Available [this=%p]\n", this));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
*avail = 0;
|
|
|
|
|
|
|
|
PRFileDesc *fd;
|
|
|
|
{
|
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(mTransport->mLock);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
if (NS_FAILED(mCondition))
|
|
|
|
return mCondition;
|
|
|
|
|
|
|
|
fd = mTransport->GetFD_Locked();
|
|
|
|
if (!fd)
|
2006-03-07 23:43:41 +03:00
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// cannot hold lock while calling NSPR. (worried about the fact that PSM
|
|
|
|
// synchronously proxies notifications over to the UI thread, which could
|
|
|
|
// mistakenly try to re-enter this code.)
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t n = PR_Available(fd);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2011-12-21 00:14:54 +04:00
|
|
|
// PSM does not implement PR_Available() so do a best approximation of it
|
|
|
|
// with MSG_PEEK
|
|
|
|
if ((n == -1) && (PR_GetError() == PR_NOT_IMPLEMENTED_ERROR)) {
|
|
|
|
char c;
|
|
|
|
|
|
|
|
n = PR_Recv(fd, &c, 1, PR_MSG_PEEK, 0);
|
2013-06-05 03:32:31 +04:00
|
|
|
SOCKET_LOG(("nsSocketInputStream::Available [this=%p] "
|
2011-12-21 00:14:54 +04:00
|
|
|
"using PEEK backup n=%d]\n", this, n));
|
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
nsresult rv;
|
|
|
|
{
|
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(mTransport->mLock);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
mTransport->ReleaseFD_Locked(fd);
|
|
|
|
|
|
|
|
if (n >= 0)
|
|
|
|
*avail = n;
|
|
|
|
else {
|
|
|
|
PRErrorCode code = PR_GetError();
|
|
|
|
if (code == PR_WOULD_BLOCK_ERROR)
|
2006-03-07 23:43:41 +03:00
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
mCondition = ErrorAccordingToNSPR(code);
|
|
|
|
}
|
|
|
|
rv = mCondition;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
mTransport->OnInputClosed(rv);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketInputStream::Read(char *buf, uint32_t count, uint32_t *countRead)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2013-06-05 03:32:31 +04:00
|
|
|
SOCKET_LOG(("nsSocketInputStream::Read [this=%p count=%u]\n", this, count));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
*countRead = 0;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
PRFileDesc* fd = nullptr;
|
2003-01-18 04:27:53 +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(mTransport->mLock);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
if (NS_FAILED(mCondition))
|
|
|
|
return (mCondition == NS_BASE_STREAM_CLOSED) ? NS_OK : mCondition;
|
|
|
|
|
|
|
|
fd = mTransport->GetFD_Locked();
|
|
|
|
if (!fd)
|
|
|
|
return NS_BASE_STREAM_WOULD_BLOCK;
|
|
|
|
}
|
|
|
|
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG((" calling PR_Read [count=%u]\n", count));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
// cannot hold lock while calling NSPR. (worried about the fact that PSM
|
|
|
|
// synchronously proxies notifications over to the UI thread, which could
|
|
|
|
// mistakenly try to re-enter this code.)
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t n = PR_Read(fd, buf, count);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG((" PR_Read returned [n=%d]\n", n));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2012-04-08 01:25:00 +04:00
|
|
|
nsresult rv = NS_OK;
|
2003-01-18 04:27:53 +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(mTransport->mLock);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2004-06-10 02:40:02 +04:00
|
|
|
#ifdef ENABLE_SOCKET_TRACING
|
|
|
|
if (n > 0)
|
|
|
|
mTransport->TraceInBuf(buf, n);
|
|
|
|
#endif
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
mTransport->ReleaseFD_Locked(fd);
|
|
|
|
|
|
|
|
if (n > 0)
|
|
|
|
mByteCount += (*countRead = n);
|
|
|
|
else if (n < 0) {
|
|
|
|
PRErrorCode code = PR_GetError();
|
|
|
|
if (code == PR_WOULD_BLOCK_ERROR)
|
|
|
|
return NS_BASE_STREAM_WOULD_BLOCK;
|
|
|
|
mCondition = ErrorAccordingToNSPR(code);
|
|
|
|
}
|
|
|
|
rv = mCondition;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
mTransport->OnInputClosed(rv);
|
2003-04-29 20:12:18 +04:00
|
|
|
|
|
|
|
// only send this notification if we have indeed read some data.
|
|
|
|
// see bug 196827 for an example of why this is important.
|
|
|
|
if (n > 0)
|
2012-08-01 12:17:09 +04:00
|
|
|
mTransport->SendStatus(NS_NET_STATUS_RECEIVING_FROM);
|
2003-01-18 04:27:53 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketInputStream::ReadSegments(nsWriteSegmentFun writer, void *closure,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count, uint32_t *countRead)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
|
|
|
// socket stream is unbuffered
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsSocketInputStream::IsNonBlocking(bool *nonblocking)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
*nonblocking = true;
|
2003-01-18 04:27:53 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2003-10-06 05:46:31 +04:00
|
|
|
nsSocketInputStream::CloseWithStatus(nsresult reason)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG(("nsSocketInputStream::CloseWithStatus [this=%p reason=%" PRIx32 "]\n", this,
|
|
|
|
static_cast<uint32_t>(reason)));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
// may be called from any thread
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
{
|
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(mTransport->mLock);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(mCondition))
|
|
|
|
rv = mCondition = reason;
|
|
|
|
else
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
mTransport->OnInputClosed(rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2003-10-06 05:46:31 +04:00
|
|
|
nsSocketInputStream::AsyncWait(nsIInputStreamCallback *callback,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t flags,
|
|
|
|
uint32_t amount,
|
2003-10-06 05:46:31 +04:00
|
|
|
nsIEventTarget *target)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2013-06-05 03:32:31 +04:00
|
|
|
SOCKET_LOG(("nsSocketInputStream::AsyncWait [this=%p]\n", this));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2013-06-13 20:24:49 +04:00
|
|
|
bool hasError = false;
|
2003-01-18 04:27:53 +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(mTransport->mLock);
|
2016-01-28 23:17:57 +03:00
|
|
|
|
2006-03-03 19:27:35 +03:00
|
|
|
if (callback && target) {
|
2003-04-19 23:45:17 +04:00
|
|
|
//
|
2003-01-18 04:27:53 +03:00
|
|
|
// build event proxy
|
2003-04-19 23:45:17 +04:00
|
|
|
//
|
2013-04-04 11:05:18 +04:00
|
|
|
mCallback = NS_NewInputStreamReadyEvent(callback, target);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
else
|
2003-10-06 05:46:31 +04:00
|
|
|
mCallback = callback;
|
2013-06-13 20:24:49 +04:00
|
|
|
mCallbackFlags = flags;
|
|
|
|
|
|
|
|
hasError = NS_FAILED(mCondition);
|
|
|
|
} // unlock mTransport->mLock
|
|
|
|
|
|
|
|
if (hasError) {
|
|
|
|
// OnSocketEvent will call OnInputStreamReady with an error code after
|
|
|
|
// going through the event loop. We do this because most socket callers
|
|
|
|
// do not expect AsyncWait() to synchronously execute the OnInputStreamReady
|
|
|
|
// callback.
|
|
|
|
mTransport->PostEvent(nsSocketTransport::MSG_INPUT_PENDING);
|
|
|
|
} else {
|
2006-07-26 04:12:39 +04:00
|
|
|
mTransport->OnInputPending();
|
2013-06-13 20:24:49 +04:00
|
|
|
}
|
2006-07-26 04:12:39 +04:00
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// socket output stream impl
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsSocketOutputStream::nsSocketOutputStream(nsSocketTransport *trans)
|
|
|
|
: mTransport(trans)
|
|
|
|
, mWriterRefCnt(0)
|
|
|
|
, mCondition(NS_OK)
|
2003-10-06 05:46:31 +04:00
|
|
|
, mCallbackFlags(0)
|
2003-01-18 04:27:53 +03:00
|
|
|
, mByteCount(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSocketOutputStream::~nsSocketOutputStream()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// called on the socket transport thread...
|
|
|
|
//
|
|
|
|
// condition : failure code if socket has been closed
|
|
|
|
//
|
|
|
|
void
|
|
|
|
nsSocketOutputStream::OnSocketReady(nsresult condition)
|
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG(("nsSocketOutputStream::OnSocketReady [this=%p cond=%" PRIx32 "]\n",
|
|
|
|
this, static_cast<uint32_t>(condition)));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2003-10-09 05:54:07 +04:00
|
|
|
NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
nsCOMPtr<nsIOutputStreamCallback> callback;
|
2003-01-18 04:27:53 +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(mTransport->mLock);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
// update condition, but be careful not to erase an already
|
|
|
|
// existing error condition.
|
|
|
|
if (NS_SUCCEEDED(mCondition))
|
|
|
|
mCondition = condition;
|
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
// ignore event if only waiting for closure and not closed.
|
|
|
|
if (NS_FAILED(mCondition) || !(mCallbackFlags & WAIT_CLOSURE_ONLY)) {
|
|
|
|
callback = mCallback;
|
2012-07-30 18:20:58 +04:00
|
|
|
mCallback = nullptr;
|
2003-10-06 05:46:31 +04:00
|
|
|
mCallbackFlags = 0;
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
if (callback)
|
|
|
|
callback->OnOutputStreamReady(this);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE(nsSocketOutputStream,
|
|
|
|
nsIOutputStream,
|
|
|
|
nsIAsyncOutputStream)
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2003-01-18 04:27:53 +03:00
|
|
|
nsSocketOutputStream::AddRef()
|
|
|
|
{
|
2013-07-19 06:24:13 +04:00
|
|
|
++mWriterRefCnt;
|
2003-01-18 04:27:53 +03:00
|
|
|
return mTransport->AddRef();
|
|
|
|
}
|
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2003-01-18 04:27:53 +03:00
|
|
|
nsSocketOutputStream::Release()
|
|
|
|
{
|
2013-07-19 06:24:13 +04:00
|
|
|
if (--mWriterRefCnt == 0)
|
2003-01-18 04:27:53 +03:00
|
|
|
Close();
|
|
|
|
return mTransport->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketOutputStream::Close()
|
|
|
|
{
|
2003-10-06 05:46:31 +04:00
|
|
|
return CloseWithStatus(NS_BASE_STREAM_CLOSED);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketOutputStream::Flush()
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketOutputStream::Write(const char *buf, uint32_t count, uint32_t *countWritten)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2013-06-05 03:32:31 +04:00
|
|
|
SOCKET_LOG(("nsSocketOutputStream::Write [this=%p count=%u]\n", this, count));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
*countWritten = 0;
|
|
|
|
|
2012-05-14 22:49:18 +04:00
|
|
|
// A write of 0 bytes can be used to force the initial SSL handshake, so do
|
|
|
|
// not reject that.
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
PRFileDesc* fd = nullptr;
|
2003-01-18 04:27:53 +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(mTransport->mLock);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
if (NS_FAILED(mCondition))
|
|
|
|
return mCondition;
|
|
|
|
|
|
|
|
fd = mTransport->GetFD_Locked();
|
|
|
|
if (!fd)
|
|
|
|
return NS_BASE_STREAM_WOULD_BLOCK;
|
|
|
|
}
|
|
|
|
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG((" calling PR_Write [count=%u]\n", count));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
// cannot hold lock while calling NSPR. (worried about the fact that PSM
|
|
|
|
// synchronously proxies notifications over to the UI thread, which could
|
|
|
|
// mistakenly try to re-enter this code.)
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t n = PR_Write(fd, buf, count);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG((" PR_Write returned [n=%d]\n", n));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2012-04-08 01:25:00 +04:00
|
|
|
nsresult rv = NS_OK;
|
2003-01-18 04:27:53 +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(mTransport->mLock);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2004-06-10 02:40:02 +04:00
|
|
|
#ifdef ENABLE_SOCKET_TRACING
|
2012-04-08 01:25:00 +04:00
|
|
|
if (n > 0)
|
|
|
|
mTransport->TraceOutBuf(buf, n);
|
2004-06-10 02:40:02 +04:00
|
|
|
#endif
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
mTransport->ReleaseFD_Locked(fd);
|
|
|
|
|
|
|
|
if (n > 0)
|
|
|
|
mByteCount += (*countWritten = n);
|
|
|
|
else if (n < 0) {
|
|
|
|
PRErrorCode code = PR_GetError();
|
|
|
|
if (code == PR_WOULD_BLOCK_ERROR)
|
|
|
|
return NS_BASE_STREAM_WOULD_BLOCK;
|
|
|
|
mCondition = ErrorAccordingToNSPR(code);
|
|
|
|
}
|
|
|
|
rv = mCondition;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
mTransport->OnOutputClosed(rv);
|
2003-04-29 20:12:18 +04:00
|
|
|
|
|
|
|
// only send this notification if we have indeed written some data.
|
|
|
|
// see bug 196827 for an example of why this is important.
|
|
|
|
if (n > 0)
|
2012-08-01 12:17:09 +04:00
|
|
|
mTransport->SendStatus(NS_NET_STATUS_SENDING_TO);
|
2003-01-18 04:27:53 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketOutputStream::WriteSegments(nsReadSegmentFun reader, void *closure,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count, uint32_t *countRead)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
|
|
|
// socket stream is unbuffered
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2016-08-12 10:36:22 +03:00
|
|
|
nsresult
|
2003-01-18 04:27:53 +03:00
|
|
|
nsSocketOutputStream::WriteFromSegments(nsIInputStream *input,
|
|
|
|
void *closure,
|
|
|
|
const char *fromSegment,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t offset,
|
|
|
|
uint32_t count,
|
|
|
|
uint32_t *countRead)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
|
|
|
nsSocketOutputStream *self = (nsSocketOutputStream *) closure;
|
|
|
|
return self->Write(fromSegment, count, countRead);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketOutputStream::WriteFrom(nsIInputStream *stream, uint32_t count, uint32_t *countRead)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
|
|
|
return stream->ReadSegments(WriteFromSegments, this, count, countRead);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsSocketOutputStream::IsNonBlocking(bool *nonblocking)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
*nonblocking = true;
|
2003-01-18 04:27:53 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2003-10-06 05:46:31 +04:00
|
|
|
nsSocketOutputStream::CloseWithStatus(nsresult reason)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG(("nsSocketOutputStream::CloseWithStatus [this=%p reason=%" PRIx32 "]\n", this,
|
|
|
|
static_cast<uint32_t>(reason)));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
// may be called from any thread
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
{
|
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(mTransport->mLock);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(mCondition))
|
|
|
|
rv = mCondition = reason;
|
|
|
|
else
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
mTransport->OnOutputClosed(rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2003-10-06 05:46:31 +04:00
|
|
|
nsSocketOutputStream::AsyncWait(nsIOutputStreamCallback *callback,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t flags,
|
|
|
|
uint32_t amount,
|
2003-10-06 05:46:31 +04:00
|
|
|
nsIEventTarget *target)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2013-06-05 03:32:31 +04:00
|
|
|
SOCKET_LOG(("nsSocketOutputStream::AsyncWait [this=%p]\n", this));
|
2003-01-18 04:27:53 +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(mTransport->mLock);
|
2016-01-28 23:17:57 +03:00
|
|
|
|
2006-03-03 19:27:35 +03:00
|
|
|
if (callback && target) {
|
2003-04-19 23:45:17 +04:00
|
|
|
//
|
2003-01-18 04:27:53 +03:00
|
|
|
// build event proxy
|
2003-04-19 23:45:17 +04:00
|
|
|
//
|
2013-04-04 11:05:18 +04:00
|
|
|
mCallback = NS_NewOutputStreamReadyEvent(callback, target);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
else
|
2003-10-06 05:46:31 +04:00
|
|
|
mCallback = callback;
|
|
|
|
|
|
|
|
mCallbackFlags = flags;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2003-04-19 23:45:17 +04:00
|
|
|
mTransport->OnOutputPending();
|
2003-01-18 04:27:53 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// socket transport impl
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsSocketTransport::nsSocketTransport()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mTypes(nullptr)
|
2003-01-18 04:27:53 +03:00
|
|
|
, mTypeCount(0)
|
|
|
|
, mPort(0)
|
|
|
|
, mProxyPort(0)
|
2015-04-09 18:31:59 +03:00
|
|
|
, mOriginPort(0)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mProxyTransparent(false)
|
|
|
|
, mProxyTransparentResolvesHost(false)
|
2014-04-16 17:52:43 +04:00
|
|
|
, mHttpsProxy(false)
|
2008-11-04 19:05:46 +03:00
|
|
|
, mConnectionFlags(0)
|
2016-12-22 10:38:06 +03:00
|
|
|
, mReuseAddrPort(false)
|
2003-01-18 04:27:53 +03:00
|
|
|
, mState(STATE_CLOSED)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mAttached(false)
|
|
|
|
, mInputClosed(true)
|
|
|
|
, mOutputClosed(true)
|
|
|
|
, mResolving(false)
|
2012-01-27 01:04:54 +04:00
|
|
|
, mNetAddrIsSet(false)
|
2016-01-15 20:16:01 +03:00
|
|
|
, mSelfAddrIsSet(false)
|
2016-06-02 09:47:00 +03:00
|
|
|
, mNetAddrPreResolved(false)
|
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
|
|
|
, mLock("nsSocketTransport.mLock")
|
2015-01-07 08:39:46 +03:00
|
|
|
, mFD(this)
|
2003-01-18 04:27:53 +03:00
|
|
|
, mFDref(0)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mFDconnected(false)
|
2014-04-16 07:00:39 +04:00
|
|
|
, mSocketTransportService(gSocketTransportService)
|
2015-01-07 08:39:46 +03:00
|
|
|
, mInput(this)
|
|
|
|
, mOutput(this)
|
2010-06-03 06:25:01 +04:00
|
|
|
, mQoSBits(0x00)
|
2014-02-06 23:51:38 +04:00
|
|
|
, mKeepaliveEnabled(false)
|
|
|
|
, mKeepaliveIdleTimeS(-1)
|
|
|
|
, mKeepaliveRetryIntervalS(-1)
|
|
|
|
, mKeepaliveProbeCount(-1)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2013-07-01 20:40:12 +04:00
|
|
|
SOCKET_LOG(("creating nsSocketTransport @%p\n", this));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2012-09-28 10:57:33 +04:00
|
|
|
mTimeouts[TIMEOUT_CONNECT] = UINT16_MAX; // no timeout
|
|
|
|
mTimeouts[TIMEOUT_READ_WRITE] = UINT16_MAX; // no timeout
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsSocketTransport::~nsSocketTransport()
|
|
|
|
{
|
2013-07-01 20:40:12 +04:00
|
|
|
SOCKET_LOG(("destroying nsSocketTransport @%p\n", this));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-04-16 17:52:43 +04:00
|
|
|
CleanupTypes();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSocketTransport::CleanupTypes()
|
|
|
|
{
|
2003-01-18 04:27:53 +03:00
|
|
|
// cleanup socket type info
|
|
|
|
if (mTypes) {
|
2014-04-16 17:52:43 +04:00
|
|
|
for (uint32_t i = 0; i < mTypeCount; ++i) {
|
2003-01-18 04:27:53 +03:00
|
|
|
PL_strfree(mTypes[i]);
|
2014-04-16 17:52:43 +04:00
|
|
|
}
|
2003-10-09 05:54:07 +04:00
|
|
|
free(mTypes);
|
2014-04-16 17:52:43 +04:00
|
|
|
mTypes = nullptr;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2014-04-16 17:52:43 +04:00
|
|
|
mTypeCount = 0;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketTransport::Init(const char **types, uint32_t typeCount,
|
|
|
|
const nsACString &host, uint16_t port,
|
2015-04-09 18:31:59 +03:00
|
|
|
const nsACString &hostRoute, uint16_t portRoute,
|
2005-03-25 06:41:33 +03:00
|
|
|
nsIProxyInfo *givenProxyInfo)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2005-03-25 06:41:33 +03:00
|
|
|
nsCOMPtr<nsProxyInfo> proxyInfo;
|
|
|
|
if (givenProxyInfo) {
|
|
|
|
proxyInfo = do_QueryInterface(givenProxyInfo);
|
|
|
|
NS_ENSURE_ARG(proxyInfo);
|
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
// init socket type info
|
|
|
|
|
2015-04-09 18:31:59 +03:00
|
|
|
mOriginHost = host;
|
|
|
|
mOriginPort = port;
|
|
|
|
if (!hostRoute.IsEmpty()) {
|
|
|
|
mHost = hostRoute;
|
|
|
|
mPort = portRoute;
|
|
|
|
} else {
|
|
|
|
mHost = host;
|
|
|
|
mPort = port;
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-04-16 17:52:43 +04:00
|
|
|
if (proxyInfo) {
|
|
|
|
mHttpsProxy = proxyInfo->IsHTTPS();
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
const char *proxyType = nullptr;
|
2015-11-25 00:56:00 +03:00
|
|
|
mProxyInfo = proxyInfo;
|
2003-01-18 04:27:53 +03:00
|
|
|
if (proxyInfo) {
|
|
|
|
mProxyPort = proxyInfo->Port();
|
|
|
|
mProxyHost = proxyInfo->Host();
|
|
|
|
// grab proxy type (looking for "socks" for example)
|
|
|
|
proxyType = proxyInfo->Type();
|
2014-04-16 17:52:43 +04:00
|
|
|
if (proxyType && (proxyInfo->IsHTTP() ||
|
|
|
|
proxyInfo->IsHTTPS() ||
|
|
|
|
proxyInfo->IsDirect() ||
|
|
|
|
!strcmp(proxyType, "unknown"))) {
|
2012-07-30 18:20:58 +04:00
|
|
|
proxyType = nullptr;
|
2014-04-16 17:52:43 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2015-04-09 18:31:59 +03:00
|
|
|
SOCKET_LOG(("nsSocketTransport::Init [this=%p host=%s:%hu origin=%s:%d proxy=%s:%hu]\n",
|
|
|
|
this, mHost.get(), mPort, mOriginHost.get(), mOriginPort,
|
|
|
|
mProxyHost.get(), mProxyPort));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
// include proxy type as a socket type if proxy type is not "http"
|
2012-07-30 18:20:58 +04:00
|
|
|
mTypeCount = typeCount + (proxyType != nullptr);
|
2005-06-18 02:58:18 +04:00
|
|
|
if (!mTypeCount)
|
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2005-06-18 02:58:18 +04:00
|
|
|
// if we have socket types, then the socket provider service had
|
|
|
|
// better exist!
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsISocketProviderService> spserv =
|
|
|
|
do_GetService(kSocketProviderServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
mTypes = (char **) malloc(mTypeCount * sizeof(char *));
|
|
|
|
if (!mTypes)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
// now verify that each socket type has a registered socket provider.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0, type = 0; i < mTypeCount; ++i) {
|
2003-01-18 04:27:53 +03:00
|
|
|
// store socket types
|
2005-06-18 02:58:18 +04:00
|
|
|
if (i == 0 && proxyType)
|
|
|
|
mTypes[i] = PL_strdup(proxyType);
|
|
|
|
else
|
|
|
|
mTypes[i] = PL_strdup(types[type++]);
|
|
|
|
|
|
|
|
if (!mTypes[i]) {
|
|
|
|
mTypeCount = i;
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsISocketProvider> provider;
|
|
|
|
rv = spserv->GetSocketProvider(mTypes[i], getter_AddRefs(provider));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("no registered socket provider");
|
|
|
|
return rv;
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2005-06-18 02:58:18 +04:00
|
|
|
// note if socket type corresponds to a transparent proxy
|
2005-09-23 01:38:12 +04:00
|
|
|
// XXX don't hardcode SOCKS here (use proxy info's flags instead).
|
2005-06-18 02:58:18 +04:00
|
|
|
if ((strcmp(mTypes[i], "socks") == 0) ||
|
|
|
|
(strcmp(mTypes[i], "socks4") == 0)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mProxyTransparent = true;
|
2004-12-16 05:46:12 +03:00
|
|
|
|
2005-06-18 02:58:18 +04:00
|
|
|
if (proxyInfo->Flags() & nsIProxyInfo::TRANSPARENT_PROXY_RESOLVES_HOST) {
|
|
|
|
// we want the SOCKS layer to send the hostname
|
|
|
|
// and port to the proxy and let it do the DNS.
|
2011-10-17 18:59:28 +04:00
|
|
|
mProxyTransparentResolvesHost = true;
|
2004-12-16 05:46:12 +03:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-06-02 09:47:00 +03:00
|
|
|
nsresult
|
|
|
|
nsSocketTransport::InitPreResolved(const char **socketTypes, uint32_t typeCount,
|
|
|
|
const nsACString &host, uint16_t port,
|
|
|
|
const nsACString &hostRoute, uint16_t portRoute,
|
|
|
|
nsIProxyInfo *proxyInfo,
|
|
|
|
const mozilla::net::NetAddr* addr)
|
|
|
|
{
|
|
|
|
nsresult rv = Init(socketTypes, typeCount, host, port, hostRoute, portRoute, proxyInfo);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mNetAddr = *addr;
|
|
|
|
mNetAddrPreResolved = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-06 19:06:23 +04:00
|
|
|
nsresult
|
|
|
|
nsSocketTransport::InitWithFilename(const char *filename)
|
|
|
|
{
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_UNIX)
|
2013-09-06 19:06:23 +04:00
|
|
|
size_t filenameLength = strlen(filename);
|
|
|
|
|
|
|
|
if (filenameLength > sizeof(mNetAddr.local.path) - 1)
|
|
|
|
return NS_ERROR_FILE_NAME_TOO_LONG;
|
|
|
|
|
|
|
|
mHost.Assign(filename);
|
|
|
|
mPort = 0;
|
|
|
|
mTypeCount = 0;
|
|
|
|
|
|
|
|
mNetAddr.local.family = AF_LOCAL;
|
|
|
|
memcpy(mNetAddr.local.path, filename, filenameLength);
|
|
|
|
mNetAddr.local.path[filenameLength] = '\0';
|
|
|
|
mNetAddrIsSet = true;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
#else
|
|
|
|
return NS_ERROR_SOCKET_ADDRESS_NOT_SUPPORTED;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-11-15 03:13:59 +03:00
|
|
|
nsresult
|
2012-12-24 01:08:43 +04:00
|
|
|
nsSocketTransport::InitWithConnectedSocket(PRFileDesc *fd, const NetAddr *addr)
|
2003-11-15 03:13:59 +03:00
|
|
|
{
|
2016-01-15 20:16:01 +03:00
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
2013-12-17 04:46:09 +04:00
|
|
|
NS_ASSERTION(!mFD.IsInitialized(), "already initialized");
|
2003-11-15 03:13:59 +03:00
|
|
|
|
2013-09-06 19:06:23 +04:00
|
|
|
char buf[kNetAddrMaxCStrBufSize];
|
2012-12-24 01:08:43 +04:00
|
|
|
NetAddrToString(addr, buf, sizeof(buf));
|
2003-11-15 03:13:59 +03:00
|
|
|
mHost.Assign(buf);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t port;
|
2012-12-24 01:08:43 +04:00
|
|
|
if (addr->raw.family == AF_INET)
|
2003-11-15 03:13:59 +03:00
|
|
|
port = addr->inet.port;
|
2013-09-06 19:06:23 +04:00
|
|
|
else if (addr->raw.family == AF_INET6)
|
2012-12-24 01:08:43 +04:00
|
|
|
port = addr->inet6.port;
|
2013-09-06 19:06:23 +04:00
|
|
|
else
|
|
|
|
port = 0;
|
2012-12-24 01:08:43 +04:00
|
|
|
mPort = ntohs(port);
|
2003-11-15 03:13:59 +03:00
|
|
|
|
2012-12-24 01:08:43 +04:00
|
|
|
memcpy(&mNetAddr, addr, sizeof(NetAddr));
|
2003-11-15 03:13:59 +03:00
|
|
|
|
|
|
|
mPollFlags = (PR_POLL_READ | PR_POLL_WRITE | PR_POLL_EXCEPT);
|
2005-01-26 05:13:14 +03:00
|
|
|
mPollTimeout = mTimeouts[TIMEOUT_READ_WRITE];
|
2003-11-15 03:13:59 +03:00
|
|
|
mState = STATE_TRANSFERRING;
|
2016-01-15 20:16:01 +03:00
|
|
|
SetSocketName(fd);
|
2012-01-27 01:04:54 +04:00
|
|
|
mNetAddrIsSet = true;
|
2003-11-15 03:13:59 +03:00
|
|
|
|
2013-12-17 04:46:09 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
mFD = fd;
|
|
|
|
mFDref = 1;
|
|
|
|
mFDconnected = 1;
|
|
|
|
}
|
2003-11-15 03:13:59 +03:00
|
|
|
|
|
|
|
// make sure new socket is non-blocking
|
|
|
|
PRSocketOptionData opt;
|
|
|
|
opt.option = PR_SockOpt_Nonblocking;
|
2011-10-17 18:59:28 +04:00
|
|
|
opt.value.non_blocking = true;
|
2013-12-17 04:46:09 +04:00
|
|
|
PR_SetSocketOption(fd, &opt);
|
2003-11-15 03:13:59 +03:00
|
|
|
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG(("nsSocketTransport::InitWithConnectedSocket [this=%p addr=%s:%hu]\n",
|
2003-11-15 03:13:59 +03:00
|
|
|
this, mHost.get(), mPort));
|
|
|
|
|
|
|
|
// jump to InitiateSocket to get ourselves attached to the STS poll list.
|
|
|
|
return PostEvent(MSG_RETRY_INIT_SOCKET);
|
|
|
|
}
|
|
|
|
|
2014-09-19 19:25:00 +04:00
|
|
|
nsresult
|
|
|
|
nsSocketTransport::InitWithConnectedSocket(PRFileDesc* aFD,
|
|
|
|
const NetAddr* aAddr,
|
|
|
|
nsISupports* aSecInfo)
|
|
|
|
{
|
|
|
|
mSecInfo = aSecInfo;
|
|
|
|
return InitWithConnectedSocket(aFD, aAddr);
|
|
|
|
}
|
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketTransport::PostEvent(uint32_t type, nsresult status, nsISupports *param)
|
2003-10-06 05:46:31 +04:00
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG(("nsSocketTransport::PostEvent [this=%p type=%u status=%" PRIx32 " param=%p]\n",
|
|
|
|
this, type, static_cast<uint32_t>(status), param));
|
2003-11-15 03:13:59 +03:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event = new nsSocketEvent(this, type, status, param);
|
2003-10-06 05:46:31 +04:00
|
|
|
if (!event)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2014-04-16 07:00:39 +04:00
|
|
|
return mSocketTransportService->Dispatch(event, NS_DISPATCH_NORMAL);
|
2003-10-06 05:46:31 +04:00
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
void
|
|
|
|
nsSocketTransport::SendStatus(nsresult status)
|
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG(("nsSocketTransport::SendStatus [this=%p status=%" PRIx32 "]\n", this,
|
|
|
|
static_cast<uint32_t>(status)));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsITransportEventSink> sink;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint64_t progress;
|
2003-01-18 04:27:53 +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);
|
2003-01-18 04:27:53 +03:00
|
|
|
sink = mEventSink;
|
|
|
|
switch (status) {
|
2012-08-01 12:17:09 +04:00
|
|
|
case NS_NET_STATUS_SENDING_TO:
|
2003-01-18 04:27:53 +03:00
|
|
|
progress = mOutput.ByteCount();
|
|
|
|
break;
|
2012-08-01 12:17:09 +04:00
|
|
|
case NS_NET_STATUS_RECEIVING_FROM:
|
2003-01-18 04:27:53 +03:00
|
|
|
progress = mInput.ByteCount();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
progress = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-01-08 22:48:52 +03:00
|
|
|
if (sink) {
|
|
|
|
sink->OnTransportStatus(this, status, progress, -1);
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSocketTransport::ResolveHost()
|
|
|
|
{
|
2013-11-22 01:35:44 +04:00
|
|
|
SOCKET_LOG(("nsSocketTransport::ResolveHost [this=%p %s:%d%s]\n",
|
|
|
|
this, SocketHost().get(), SocketPort(),
|
|
|
|
mConnectionFlags & nsSocketTransport::BYPASS_CACHE ?
|
|
|
|
" bypass cache" : ""));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
nsresult rv;
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2016-06-02 09:47:00 +03:00
|
|
|
if (mNetAddrPreResolved) {
|
|
|
|
mState = STATE_RESOLVING;
|
|
|
|
return PostEvent(MSG_DNS_LOOKUP_COMPLETE, NS_OK, nullptr);
|
|
|
|
}
|
|
|
|
|
2005-09-23 01:38:12 +04:00
|
|
|
if (!mProxyHost.IsEmpty()) {
|
|
|
|
if (!mProxyTransparent || mProxyTransparentResolvesHost) {
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_UNIX)
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!mNetAddrIsSet || mNetAddr.raw.family != AF_LOCAL,
|
|
|
|
"Unix domain sockets can't be used with proxies");
|
2013-09-06 19:06:23 +04:00
|
|
|
#endif
|
2005-09-23 01:38:12 +04:00
|
|
|
// When not resolving mHost locally, we still want to ensure that
|
|
|
|
// it only contains valid characters. See bug 304904 for details.
|
2014-07-18 23:05:41 +04:00
|
|
|
// Sometimes the end host is not yet known and mHost is *
|
|
|
|
if (!net_IsValidHostName(mHost) &&
|
|
|
|
!mHost.Equals(NS_LITERAL_CSTRING("*"))) {
|
|
|
|
SOCKET_LOG((" invalid hostname %s\n", mHost.get()));
|
2005-09-23 01:38:12 +04:00
|
|
|
return NS_ERROR_UNKNOWN_HOST;
|
2014-07-18 23:05:41 +04:00
|
|
|
}
|
2005-09-23 01:38:12 +04:00
|
|
|
}
|
|
|
|
if (mProxyTransparentResolvesHost) {
|
|
|
|
// Name resolution is done on the server side. Just pretend
|
|
|
|
// client resolution is complete, this will get picked up later.
|
|
|
|
// since we don't need to do DNS now, we bypass the resolving
|
|
|
|
// step by initializing mNetAddr to an empty address, but we
|
|
|
|
// must keep the port. The SOCKS IO layer will use the hostname
|
|
|
|
// we send it when it's created, rather than the empty address
|
|
|
|
// we send with the connect call.
|
|
|
|
mState = STATE_RESOLVING;
|
2012-12-24 01:08:43 +04:00
|
|
|
mNetAddr.raw.family = AF_INET;
|
|
|
|
mNetAddr.inet.port = htons(SocketPort());
|
|
|
|
mNetAddr.inet.ip = htonl(INADDR_ANY);
|
2012-07-30 18:20:58 +04:00
|
|
|
return PostEvent(MSG_DNS_LOOKUP_COMPLETE, NS_OK, nullptr);
|
2005-09-23 01:38:12 +04:00
|
|
|
}
|
2004-12-16 05:46:12 +03:00
|
|
|
}
|
|
|
|
|
2003-10-07 09:11:41 +04:00
|
|
|
nsCOMPtr<nsIDNSService> dns = do_GetService(kDNSServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mResolving = true;
|
2003-10-07 09:11:41 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t dnsFlags = 0;
|
2008-11-04 19:05:46 +03:00
|
|
|
if (mConnectionFlags & nsSocketTransport::BYPASS_CACHE)
|
|
|
|
dnsFlags = nsIDNSService::RESOLVE_BYPASS_CACHE;
|
2011-07-04 13:47:09 +04:00
|
|
|
if (mConnectionFlags & nsSocketTransport::DISABLE_IPV6)
|
|
|
|
dnsFlags |= nsIDNSService::RESOLVE_DISABLE_IPV6;
|
2013-01-30 02:49:35 +04:00
|
|
|
if (mConnectionFlags & nsSocketTransport::DISABLE_IPV4)
|
|
|
|
dnsFlags |= nsIDNSService::RESOLVE_DISABLE_IPV4;
|
|
|
|
|
|
|
|
NS_ASSERTION(!(dnsFlags & nsIDNSService::RESOLVE_DISABLE_IPV6) ||
|
|
|
|
!(dnsFlags & nsIDNSService::RESOLVE_DISABLE_IPV4),
|
|
|
|
"Setting both RESOLVE_DISABLE_IPV6 and RESOLVE_DISABLE_IPV4");
|
2008-11-04 19:05:46 +03:00
|
|
|
|
2012-08-01 12:17:09 +04:00
|
|
|
SendStatus(NS_NET_STATUS_RESOLVING_HOST);
|
2015-04-09 18:31:59 +03:00
|
|
|
|
|
|
|
if (!SocketHost().Equals(mOriginHost)) {
|
|
|
|
SOCKET_LOG(("nsSocketTransport %p origin %s doing dns for %s\n",
|
|
|
|
this, mOriginHost.get(), SocketHost().get()));
|
|
|
|
}
|
2017-02-15 12:48:15 +03:00
|
|
|
rv = dns->AsyncResolveExtendedNative(SocketHost(), dnsFlags, mNetworkInterfaceId,
|
|
|
|
this, nullptr, mOriginAttributes,
|
|
|
|
getter_AddRefs(mDNSRequest));
|
2003-10-07 09:11:41 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG((" advancing to STATE_RESOLVING\n"));
|
2003-10-07 09:11:41 +04:00
|
|
|
mState = STATE_RESOLVING;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsSocketTransport::BuildSocket(PRFileDesc *&fd, bool &proxyTransparent, bool &usingSSL)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2013-06-05 03:32:31 +04:00
|
|
|
SOCKET_LOG(("nsSocketTransport::BuildSocket [this=%p]\n", this));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
proxyTransparent = false;
|
|
|
|
usingSSL = false;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
if (mTypeCount == 0) {
|
2003-09-12 00:32:33 +04:00
|
|
|
fd = PR_OpenTCPSocket(mNetAddr.raw.family);
|
2003-01-18 04:27:53 +03:00
|
|
|
rv = fd ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
else {
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_UNIX)
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!mNetAddrIsSet || mNetAddr.raw.family != AF_LOCAL,
|
|
|
|
"Unix domain sockets can't be used with socket types");
|
2013-09-06 19:06:23 +04:00
|
|
|
#endif
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
fd = nullptr;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsISocketProviderService> spserv =
|
|
|
|
do_GetService(kSocketProviderServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2015-04-09 18:31:59 +03:00
|
|
|
// by setting host to mOriginHost, instead of mHost we send the
|
|
|
|
// SocketProvider (e.g. PSM) the origin hostname but can still do DNS
|
|
|
|
// on an explicit alternate service host name
|
|
|
|
const char *host = mOriginHost.get();
|
|
|
|
int32_t port = (int32_t) mOriginPort;
|
2015-11-25 00:56:00 +03:00
|
|
|
nsCOMPtr<nsIProxyInfo> proxyInfo = mProxyInfo;
|
2015-04-11 00:27:27 +03:00
|
|
|
uint32_t controlFlags = 0;
|
2004-12-16 05:46:12 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i;
|
2003-01-18 04:27:53 +03:00
|
|
|
for (i=0; i<mTypeCount; ++i) {
|
|
|
|
nsCOMPtr<nsISocketProvider> provider;
|
|
|
|
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG((" pushing io layer [%u:%s]\n", i, mTypes[i]));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
rv = spserv->GetSocketProvider(mTypes[i], getter_AddRefs(provider));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break;
|
|
|
|
|
2004-12-16 05:46:12 +03:00
|
|
|
if (mProxyTransparentResolvesHost)
|
2015-04-11 00:27:27 +03:00
|
|
|
controlFlags |= nsISocketProvider::PROXY_RESOLVES_HOST;
|
2009-02-18 01:06:52 +03:00
|
|
|
|
|
|
|
if (mConnectionFlags & nsISocketTransport::ANONYMOUS_CONNECT)
|
2015-04-11 00:27:27 +03:00
|
|
|
controlFlags |= nsISocketProvider::ANONYMOUS_CONNECT;
|
2004-12-16 05:46:12 +03:00
|
|
|
|
2012-06-30 18:34:17 +04:00
|
|
|
if (mConnectionFlags & nsISocketTransport::NO_PERMANENT_STORAGE)
|
2015-04-11 00:27:27 +03:00
|
|
|
controlFlags |= nsISocketProvider::NO_PERMANENT_STORAGE;
|
2012-06-30 18:34:17 +04:00
|
|
|
|
2015-04-09 18:31:59 +03:00
|
|
|
if (mConnectionFlags & nsISocketTransport::MITM_OK)
|
|
|
|
controlFlags |= nsISocketProvider::MITM_OK;
|
|
|
|
|
2016-12-03 00:49:23 +03:00
|
|
|
if (mConnectionFlags & nsISocketTransport::BE_CONSERVATIVE)
|
|
|
|
controlFlags |= nsISocketProvider::BE_CONSERVATIVE;
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
nsCOMPtr<nsISupports> secinfo;
|
|
|
|
if (i == 0) {
|
|
|
|
// if this is the first type, we'll want the
|
|
|
|
// service to allocate a new socket
|
2014-04-16 17:52:43 +04:00
|
|
|
|
|
|
|
// when https proxying we want to just connect to the proxy as if
|
|
|
|
// it were the end host (i.e. expect the proxy's cert)
|
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
rv = provider->NewSocket(mNetAddr.raw.family,
|
2015-11-25 00:56:00 +03:00
|
|
|
mHttpsProxy ? mProxyHost.get() : host,
|
|
|
|
mHttpsProxy ? mProxyPort : port,
|
2016-11-21 06:43:06 +03:00
|
|
|
proxyInfo, mOriginAttributes,
|
2015-04-11 00:27:27 +03:00
|
|
|
controlFlags, &fd,
|
2004-12-16 05:46:12 +03:00
|
|
|
getter_AddRefs(secinfo));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && !fd) {
|
|
|
|
NS_NOTREACHED("NewSocket succeeded but failed to create a PRFileDesc");
|
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// the socket has already been allocated,
|
|
|
|
// so we just want the service to add itself
|
|
|
|
// to the stack (such as pushing an io layer)
|
2003-09-12 00:32:33 +04:00
|
|
|
rv = provider->AddToSocket(mNetAddr.raw.family,
|
2015-11-25 00:56:00 +03:00
|
|
|
host, port, proxyInfo,
|
2016-11-21 06:43:06 +03:00
|
|
|
mOriginAttributes, controlFlags, fd,
|
2004-12-16 05:46:12 +03:00
|
|
|
getter_AddRefs(secinfo));
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2016-10-12 10:32:22 +03:00
|
|
|
|
2015-04-11 00:27:27 +03:00
|
|
|
// controlFlags = 0; not used below this point...
|
2003-01-18 04:27:53 +03:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// if the service was ssl or starttls, we want to hold onto the socket info
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isSSL = (strcmp(mTypes[i], "ssl") == 0);
|
2003-01-18 04:27:53 +03:00
|
|
|
if (isSSL || (strcmp(mTypes[i], "starttls") == 0)) {
|
|
|
|
// remember security info and give notification callbacks to PSM...
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> callbacks;
|
|
|
|
{
|
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-01-18 04:27:53 +03:00
|
|
|
mSecInfo = secinfo;
|
|
|
|
callbacks = mCallbacks;
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG((" [secinfo=%p callbacks=%p]\n", mSecInfo.get(), mCallbacks.get()));
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
// don't call into PSM while holding mLock!!
|
|
|
|
nsCOMPtr<nsISSLSocketControl> secCtrl(do_QueryInterface(secinfo));
|
|
|
|
if (secCtrl)
|
|
|
|
secCtrl->SetNotificationCallbacks(callbacks);
|
|
|
|
// remember if socket type is SSL so we can ProxyStartSSL if need be.
|
|
|
|
usingSSL = isSSL;
|
|
|
|
}
|
|
|
|
else if ((strcmp(mTypes[i], "socks") == 0) ||
|
|
|
|
(strcmp(mTypes[i], "socks4") == 0)) {
|
|
|
|
// since socks is transparent, any layers above
|
|
|
|
// it do not have to worry about proxy stuff
|
2015-11-25 00:56:00 +03:00
|
|
|
proxyInfo = nullptr;
|
2011-10-17 18:59:28 +04:00
|
|
|
proxyTransparent = true;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG((" error pushing io layer [%u:%s rv=%" PRIx32 "]\n", i, mTypes[i],
|
|
|
|
static_cast<uint32_t>(rv)));
|
2015-07-07 12:13:00 +03:00
|
|
|
if (fd) {
|
2016-03-19 06:25:00 +03:00
|
|
|
CloseSocket(fd,
|
|
|
|
mSocketTransportService->IsTelemetryEnabledAndNotSleepPhase());
|
2015-07-07 12:13:00 +03:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
}
|
2003-01-24 08:32:06 +03:00
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSocketTransport::InitiateSocket()
|
|
|
|
{
|
2013-06-05 03:32:31 +04:00
|
|
|
SOCKET_LOG(("nsSocketTransport::InitiateSocket [this=%p]\n", this));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
nsresult rv;
|
2014-04-15 23:38:55 +04:00
|
|
|
bool isLocal;
|
|
|
|
IsLocal(&isLocal);
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2016-01-14 12:42:00 +03:00
|
|
|
if (gIOService->IsNetTearingDown()) {
|
2015-05-29 09:04:00 +03:00
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
2013-09-06 19:06:23 +04:00
|
|
|
if (gIOService->IsOffline()) {
|
|
|
|
if (!isLocal)
|
|
|
|
return NS_ERROR_OFFLINE;
|
2014-04-15 23:38:55 +04:00
|
|
|
} else if (!isLocal) {
|
2014-11-05 23:01:53 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// all IP networking has to be done from the parent
|
|
|
|
if (NS_SUCCEEDED(mCondition) &&
|
|
|
|
((mNetAddr.raw.family == AF_INET) || (mNetAddr.raw.family == AF_INET6))) {
|
|
|
|
MOZ_ASSERT(!IsNeckoChild());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-04-15 23:38:55 +04:00
|
|
|
if (NS_SUCCEEDED(mCondition) &&
|
2016-06-07 16:17:42 +03:00
|
|
|
xpc::AreNonLocalConnectionsDisabled() &&
|
2014-04-15 23:38:55 +04:00
|
|
|
!(IsIPAddrAny(&mNetAddr) || IsIPAddrLocal(&mNetAddr))) {
|
|
|
|
nsAutoCString ipaddr;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsNetAddr> netaddr = new nsNetAddr(&mNetAddr);
|
2014-04-15 23:38:55 +04:00
|
|
|
netaddr->GetAddress(ipaddr);
|
|
|
|
fprintf_stderr(stderr,
|
2014-08-04 18:21:35 +04:00
|
|
|
"FATAL ERROR: Non-local network connections are disabled and a connection "
|
2014-08-05 17:03:22 +04:00
|
|
|
"attempt to %s (%s) was made.\nYou should only access hostnames "
|
2014-04-15 23:38:55 +04:00
|
|
|
"available via the test networking proxy (if running mochitests) "
|
2014-08-04 18:21:35 +04:00
|
|
|
"or from a test-specific httpd.js server (if running xpcshell tests). "
|
|
|
|
"Browser services should be disabled or redirected to a local server.\n",
|
2014-04-15 23:38:55 +04:00
|
|
|
mHost.get(), ipaddr.get());
|
|
|
|
MOZ_CRASH("Attempting to connect to non-local address!");
|
|
|
|
}
|
2013-09-06 19:06:23 +04:00
|
|
|
}
|
2012-09-18 03:45:10 +04:00
|
|
|
|
2014-07-24 18:05:36 +04:00
|
|
|
// Hosts/Proxy Hosts that are Local IP Literals should not be speculatively
|
|
|
|
// connected - Bug 853423.
|
|
|
|
if (mConnectionFlags & nsISocketTransport::DISABLE_RFC1918 &&
|
|
|
|
IsIPAddrLocal(&mNetAddr)) {
|
2013-11-11 10:59:40 +04:00
|
|
|
if (SOCKET_LOG_ENABLED()) {
|
|
|
|
nsAutoCString netAddrCString;
|
|
|
|
netAddrCString.SetCapacity(kIPv6CStrBufSize);
|
|
|
|
if (!NetAddrToString(&mNetAddr,
|
|
|
|
netAddrCString.BeginWriting(),
|
|
|
|
kIPv6CStrBufSize))
|
|
|
|
netAddrCString = NS_LITERAL_CSTRING("<IP-to-string failed>");
|
2014-07-24 18:05:36 +04:00
|
|
|
SOCKET_LOG(("nsSocketTransport::InitiateSocket skipping "
|
|
|
|
"speculative connection for host [%s:%d] proxy "
|
|
|
|
"[%s:%d] with Local IP address [%s]",
|
2013-11-11 10:59:40 +04:00
|
|
|
mHost.get(), mPort, mProxyHost.get(), mProxyPort,
|
|
|
|
netAddrCString.get()));
|
|
|
|
}
|
2014-10-02 08:38:53 +04:00
|
|
|
mCondition = NS_ERROR_CONNECTION_REFUSED;
|
|
|
|
OnSocketDetached(nullptr);
|
|
|
|
return mCondition;
|
2013-11-11 10:59:40 +04:00
|
|
|
}
|
|
|
|
|
2003-04-09 02:18:10 +04: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.
|
|
|
|
//
|
2014-04-16 07:00:39 +04:00
|
|
|
if (!mSocketTransportService->CanAttachSocket()) {
|
2006-05-10 21:30:15 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
new nsSocketEvent(this, MSG_RETRY_INIT_SOCKET);
|
2003-10-06 05:46:31 +04:00
|
|
|
if (!event)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-04-16 07:00:39 +04:00
|
|
|
return mSocketTransportService->NotifyWhenCanAttachSocket(event);
|
2003-10-06 05:46:31 +04:00
|
|
|
}
|
2003-04-09 02:18:10 +04:00
|
|
|
|
2003-11-15 03:13:59 +03:00
|
|
|
//
|
|
|
|
// if we already have a connected socket, then just attach and return.
|
|
|
|
//
|
2013-12-17 04:46:09 +04:00
|
|
|
if (mFD.IsInitialized()) {
|
2014-04-16 07:00:39 +04:00
|
|
|
rv = mSocketTransportService->AttachSocket(mFD, this);
|
2003-11-15 03:13:59 +03:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2011-10-17 18:59:28 +04:00
|
|
|
mAttached = true;
|
2003-11-15 03:13:59 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
//
|
|
|
|
// create new socket fd, push io layers, etc.
|
|
|
|
//
|
|
|
|
PRFileDesc *fd;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool proxyTransparent;
|
|
|
|
bool usingSSL;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
rv = BuildSocket(fd, proxyTransparent, usingSSL);
|
2003-01-18 04:27:53 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG((" BuildSocket failed [rv=%" PRIx32 "]\n", static_cast<uint32_t>(rv)));
|
2003-01-18 04:27:53 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-09-29 02:39:20 +04:00
|
|
|
// Attach network activity monitor
|
2016-05-19 05:02:57 +03:00
|
|
|
NetworkActivityMonitor::AttachIOLayer(fd);
|
2012-09-29 02:39:20 +04:00
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
PRStatus status;
|
|
|
|
|
|
|
|
// Make the socket non-blocking...
|
2003-01-24 08:32:06 +03:00
|
|
|
PRSocketOptionData opt;
|
2003-01-18 04:27:53 +03:00
|
|
|
opt.option = PR_SockOpt_Nonblocking;
|
2011-10-17 18:59:28 +04:00
|
|
|
opt.value.non_blocking = true;
|
2003-01-18 04:27:53 +03:00
|
|
|
status = PR_SetSocketOption(fd, &opt);
|
2003-01-24 08:32:06 +03:00
|
|
|
NS_ASSERTION(status == PR_SUCCESS, "unable to make socket non-blocking");
|
2009-02-09 20:31:44 +03:00
|
|
|
|
2016-12-22 10:38:06 +03:00
|
|
|
if (mReuseAddrPort) {
|
|
|
|
SOCKET_LOG((" Setting port/addr reuse socket options\n"));
|
|
|
|
|
|
|
|
// Set ReuseAddr for TCP sockets to enable having several
|
|
|
|
// sockets bound to same local IP and port
|
|
|
|
PRSocketOptionData opt_reuseaddr;
|
|
|
|
opt_reuseaddr.option = PR_SockOpt_Reuseaddr;
|
|
|
|
opt_reuseaddr.value.reuse_addr = PR_TRUE;
|
|
|
|
status = PR_SetSocketOption(fd, &opt_reuseaddr);
|
|
|
|
if (status != PR_SUCCESS) {
|
|
|
|
SOCKET_LOG((" Couldn't set reuse addr socket option: %d\n",
|
|
|
|
status));
|
|
|
|
}
|
|
|
|
|
|
|
|
// And also set ReusePort for platforms supporting this socket option
|
|
|
|
PRSocketOptionData opt_reuseport;
|
|
|
|
opt_reuseport.option = PR_SockOpt_Reuseport;
|
|
|
|
opt_reuseport.value.reuse_port = PR_TRUE;
|
|
|
|
status = PR_SetSocketOption(fd, &opt_reuseport);
|
|
|
|
if (status != PR_SUCCESS
|
|
|
|
&& PR_GetError() != PR_OPERATION_NOT_SUPPORTED_ERROR) {
|
|
|
|
SOCKET_LOG((" Couldn't set reuse port socket option: %d\n",
|
|
|
|
status));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-08 22:36:56 +04:00
|
|
|
// disable the nagle algorithm - if we rely on it to coalesce writes into
|
|
|
|
// full packets the final packet of a multi segment POST/PUT or pipeline
|
|
|
|
// sequence is delayed a full rtt
|
|
|
|
opt.option = PR_SockOpt_NoDelay;
|
2011-10-17 18:59:28 +04:00
|
|
|
opt.value.no_delay = true;
|
2011-04-08 22:36:56 +04:00
|
|
|
PR_SetSocketOption(fd, &opt);
|
|
|
|
|
2009-02-09 20:31:44 +03:00
|
|
|
// if the network.tcp.sendbuffer preference is set, use it to size SO_SNDBUF
|
|
|
|
// The Windows default of 8KB is too small and as of vista sp1, autotuning
|
|
|
|
// only applies to receive window
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t sndBufferSize;
|
2014-04-16 07:00:39 +04:00
|
|
|
mSocketTransportService->GetSendBufferSize(&sndBufferSize);
|
2009-02-09 20:31:44 +03:00
|
|
|
if (sndBufferSize > 0) {
|
|
|
|
opt.option = PR_SockOpt_SendBufferSize;
|
|
|
|
opt.value.send_buffer_size = sndBufferSize;
|
|
|
|
PR_SetSocketOption(fd, &opt);
|
|
|
|
}
|
|
|
|
|
2010-06-03 06:25:01 +04:00
|
|
|
if (mQoSBits) {
|
|
|
|
opt.option = PR_SockOpt_IpTypeOfService;
|
|
|
|
opt.value.tos = mQoSBits;
|
|
|
|
PR_SetSocketOption(fd, &opt);
|
|
|
|
}
|
|
|
|
|
2016-02-04 06:42:00 +03:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
// The linger is turned off by default. This is not a hard close, but
|
|
|
|
// closesocket should return immediately and operating system tries to send
|
|
|
|
// remaining data for certain, implementation specific, amount of time.
|
|
|
|
// https://msdn.microsoft.com/en-us/library/ms739165.aspx
|
|
|
|
//
|
|
|
|
// Turn the linger option on an set the interval to 0. This will cause hard
|
|
|
|
// close of the socket.
|
|
|
|
opt.option = PR_SockOpt_Linger;
|
|
|
|
opt.value.linger.polarity = 1;
|
|
|
|
opt.value.linger.linger = 0;
|
|
|
|
PR_SetSocketOption(fd, &opt);
|
|
|
|
#endif
|
|
|
|
|
2003-01-24 08:32:06 +03:00
|
|
|
// inform socket transport about this newly created socket...
|
2014-04-16 07:00:39 +04:00
|
|
|
rv = mSocketTransportService->AttachSocket(fd, this);
|
2003-01-24 08:32:06 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2016-03-19 06:25:00 +03:00
|
|
|
CloseSocket(fd,
|
|
|
|
mSocketTransportService->IsTelemetryEnabledAndNotSleepPhase());
|
2003-01-24 08:32:06 +03:00
|
|
|
return rv;
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
mAttached = true;
|
2003-01-24 08:32:06 +03:00
|
|
|
|
|
|
|
// assign mFD so that we can properly handle OnSocketDetached before we've
|
|
|
|
// established a connection.
|
|
|
|
{
|
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-01-24 08:32:06 +03:00
|
|
|
mFD = fd;
|
|
|
|
mFDref = 1;
|
2011-10-17 18:59:28 +04:00
|
|
|
mFDconnected = false;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG((" advancing to STATE_CONNECTING\n"));
|
2003-01-18 04:27:53 +03:00
|
|
|
mState = STATE_CONNECTING;
|
2005-01-26 05:13:14 +03:00
|
|
|
mPollTimeout = mTimeouts[TIMEOUT_CONNECT];
|
2012-08-01 12:17:09 +04:00
|
|
|
SendStatus(NS_NET_STATUS_CONNECTING_TO);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2010-10-28 21:09:08 +04:00
|
|
|
if (SOCKET_LOG_ENABLED()) {
|
2013-09-06 19:06:23 +04:00
|
|
|
char buf[kNetAddrMaxCStrBufSize];
|
2012-12-24 01:08:43 +04:00
|
|
|
NetAddrToString(&mNetAddr, buf, sizeof(buf));
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG((" trying address: %s\n", buf));
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
|
|
|
|
2013-09-06 19:06:23 +04:00
|
|
|
//
|
|
|
|
// Initiate the connect() to the host...
|
2003-01-18 04:27:53 +03:00
|
|
|
//
|
2012-12-24 01:08:43 +04:00
|
|
|
PRNetAddr prAddr;
|
2014-10-07 13:24:57 +04:00
|
|
|
{
|
|
|
|
if (mBindAddr) {
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
NetAddrToPRNetAddr(mBindAddr.get(), &prAddr);
|
|
|
|
status = PR_Bind(fd, &prAddr);
|
|
|
|
if (status != PR_SUCCESS) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mBindAddr = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-24 01:08:43 +04:00
|
|
|
NetAddrToPRNetAddr(&mNetAddr, &prAddr);
|
2013-03-28 21:38:04 +04:00
|
|
|
|
2016-03-24 17:10:00 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
// Find the real tcp socket and set non-blocking once again!
|
|
|
|
// Bug 1158189.
|
|
|
|
PRFileDesc *bottom = PR_GetIdentitiesLayer(fd, PR_NSPR_IO_LAYER);
|
|
|
|
if (bottom) {
|
|
|
|
PROsfd osfd = PR_FileDesc2NativeHandle(bottom);
|
|
|
|
u_long nonblocking = 1;
|
|
|
|
if (ioctlsocket(osfd, FIONBIO, &nonblocking) != 0) {
|
|
|
|
NS_WARNING("Socket could not be set non-blocking!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-07-07 12:13:00 +03:00
|
|
|
// We use PRIntervalTime here because we need
|
|
|
|
// nsIOService::LastOfflineStateChange time and
|
|
|
|
// nsIOService::LastConectivityChange time to be atomic.
|
|
|
|
PRIntervalTime connectStarted = 0;
|
2016-03-19 06:25:00 +03:00
|
|
|
if (gSocketTransportService->IsTelemetryEnabledAndNotSleepPhase()) {
|
2015-07-07 12:13:00 +03:00
|
|
|
connectStarted = PR_IntervalNow();
|
|
|
|
}
|
|
|
|
|
2012-12-24 01:08:43 +04:00
|
|
|
status = PR_Connect(fd, &prAddr, NS_SOCKET_CONNECT_TIMEOUT);
|
2015-07-07 12:13:00 +03:00
|
|
|
|
2016-03-19 06:25:00 +03:00
|
|
|
if (gSocketTransportService->IsTelemetryEnabledAndNotSleepPhase() &&
|
|
|
|
connectStarted) {
|
2015-07-07 12:13:00 +03:00
|
|
|
SendPRBlockingTelemetry(connectStarted,
|
|
|
|
Telemetry::PRCONNECT_BLOCKING_TIME_NORMAL,
|
|
|
|
Telemetry::PRCONNECT_BLOCKING_TIME_SHUTDOWN,
|
|
|
|
Telemetry::PRCONNECT_BLOCKING_TIME_CONNECTIVITY_CHANGE,
|
|
|
|
Telemetry::PRCONNECT_BLOCKING_TIME_LINK_CHANGE,
|
|
|
|
Telemetry::PRCONNECT_BLOCKING_TIME_OFFLINE);
|
|
|
|
}
|
|
|
|
|
2003-01-24 08:32:06 +03:00
|
|
|
if (status == PR_SUCCESS) {
|
|
|
|
//
|
|
|
|
// we are connected!
|
|
|
|
//
|
|
|
|
OnSocketConnected();
|
|
|
|
}
|
|
|
|
else {
|
2003-01-18 04:27:53 +03:00
|
|
|
PRErrorCode code = PR_GetError();
|
|
|
|
#if defined(TEST_CONNECT_ERRORS)
|
|
|
|
code = RandomizeConnectError(code);
|
|
|
|
#endif
|
|
|
|
//
|
|
|
|
// If the PR_Connect(...) would block, then poll for a connection.
|
|
|
|
//
|
|
|
|
if ((PR_WOULD_BLOCK_ERROR == code) || (PR_IN_PROGRESS_ERROR == code))
|
|
|
|
mPollFlags = (PR_POLL_EXCEPT | PR_POLL_WRITE);
|
|
|
|
//
|
|
|
|
// If the socket is already connected, then return success...
|
|
|
|
//
|
|
|
|
else if (PR_IS_CONNECTED_ERROR == code) {
|
|
|
|
//
|
|
|
|
// we are connected!
|
|
|
|
//
|
2003-01-24 08:32:06 +03:00
|
|
|
OnSocketConnected();
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
if (mSecInfo && !mProxyHost.IsEmpty() && proxyTransparent && usingSSL) {
|
|
|
|
// if the connection phase is finished, and the ssl layer has
|
|
|
|
// been pushed, and we were proxying (transparently; ie. nothing
|
|
|
|
// has to happen in the protocol layer above us), it's time for
|
|
|
|
// the ssl to start doing it's thing.
|
|
|
|
nsCOMPtr<nsISSLSocketControl> secCtrl =
|
|
|
|
do_QueryInterface(mSecInfo);
|
|
|
|
if (secCtrl) {
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG((" calling ProxyStartSSL()\n"));
|
2003-01-18 04:27:53 +03:00
|
|
|
secCtrl->ProxyStartSSL();
|
|
|
|
}
|
|
|
|
// XXX what if we were forced to poll on the socket for a successful
|
|
|
|
// connection... wouldn't we need to call ProxyStartSSL after a call
|
|
|
|
// to PR_ConnectContinue indicates that we are connected?
|
|
|
|
//
|
|
|
|
// XXX this appears to be what the old socket transport did. why
|
|
|
|
// isn't this broken?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//
|
2011-02-24 16:10:08 +03:00
|
|
|
// A SOCKS request was rejected; get the actual error code from
|
|
|
|
// the OS error
|
|
|
|
//
|
|
|
|
else if (PR_UNKNOWN_ERROR == code &&
|
|
|
|
mProxyTransparent &&
|
|
|
|
!mProxyHost.IsEmpty()) {
|
|
|
|
code = PR_GetOSError();
|
|
|
|
rv = ErrorAccordingToNSPR(code);
|
|
|
|
}
|
|
|
|
//
|
2003-01-18 04:27:53 +03:00
|
|
|
// The connection was refused...
|
|
|
|
//
|
|
|
|
else {
|
2016-03-19 06:25:00 +03:00
|
|
|
if (gSocketTransportService->IsTelemetryEnabledAndNotSleepPhase() &&
|
|
|
|
connectStarted) {
|
|
|
|
SendPRBlockingTelemetry(connectStarted,
|
|
|
|
Telemetry::PRCONNECT_FAIL_BLOCKING_TIME_NORMAL,
|
|
|
|
Telemetry::PRCONNECT_FAIL_BLOCKING_TIME_SHUTDOWN,
|
|
|
|
Telemetry::PRCONNECT_FAIL_BLOCKING_TIME_CONNECTIVITY_CHANGE,
|
|
|
|
Telemetry::PRCONNECT_FAIL_BLOCKING_TIME_LINK_CHANGE,
|
|
|
|
Telemetry::PRCONNECT_FAIL_BLOCKING_TIME_OFFLINE);
|
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
rv = ErrorAccordingToNSPR(code);
|
|
|
|
if ((rv == NS_ERROR_CONNECTION_REFUSED) && !mProxyHost.IsEmpty())
|
|
|
|
rv = NS_ERROR_PROXY_CONNECTION_REFUSED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2003-01-18 04:27:53 +03:00
|
|
|
nsSocketTransport::RecoverFromError()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_FAILED(mCondition), "there should be something wrong");
|
|
|
|
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG(("nsSocketTransport::RecoverFromError [this=%p state=%x cond=%" PRIx32 "]\n",
|
|
|
|
this, mState, static_cast<uint32_t>(mCondition)));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_UNIX)
|
2013-09-06 19:06:23 +04:00
|
|
|
// Unix domain connections don't have multiple addresses to try,
|
|
|
|
// so the recovery techniques here don't apply.
|
|
|
|
if (mNetAddrIsSet && mNetAddr.raw.family == AF_LOCAL)
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
// can only recover from errors in these states
|
|
|
|
if (mState != STATE_RESOLVING && mState != STATE_CONNECTING)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2011-11-02 20:18:54 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2003-02-14 00:33:58 +03:00
|
|
|
// OK to check this outside mLock
|
|
|
|
NS_ASSERTION(!mFDconnected, "socket should not be connected");
|
|
|
|
|
2013-07-03 00:41:19 +04:00
|
|
|
// all connection failures need to be reported to DNS so that the next
|
|
|
|
// time we will use a different address if available.
|
|
|
|
if (mState == STATE_CONNECTING && mDNSRecord) {
|
|
|
|
mDNSRecord->ReportUnusable(SocketPort());
|
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
// can only recover from these errors
|
|
|
|
if (mCondition != NS_ERROR_CONNECTION_REFUSED &&
|
|
|
|
mCondition != NS_ERROR_PROXY_CONNECTION_REFUSED &&
|
|
|
|
mCondition != NS_ERROR_NET_TIMEOUT &&
|
|
|
|
mCondition != NS_ERROR_UNKNOWN_HOST &&
|
|
|
|
mCondition != NS_ERROR_UNKNOWN_PROXY_HOST)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool tryAgain = false;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2016-02-26 16:00:00 +03:00
|
|
|
if ((mState == STATE_CONNECTING) && mDNSRecord &&
|
2016-03-19 06:25:00 +03:00
|
|
|
mSocketTransportService->IsTelemetryEnabledAndNotSleepPhase()) {
|
2015-07-01 14:51:00 +03:00
|
|
|
if (mNetAddr.raw.family == AF_INET) {
|
|
|
|
Telemetry::Accumulate(Telemetry::IPV4_AND_IPV6_ADDRESS_CONNECTIVITY,
|
|
|
|
UNSUCCESSFUL_CONNECTING_TO_IPV4_ADDRESS);
|
|
|
|
} else if (mNetAddr.raw.family == AF_INET6) {
|
|
|
|
Telemetry::Accumulate(Telemetry::IPV4_AND_IPV6_ADDRESS_CONNECTIVITY,
|
|
|
|
UNSUCCESSFUL_CONNECTING_TO_IPV6_ADDRESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 02:49:35 +04:00
|
|
|
if (mConnectionFlags & (DISABLE_IPV6 | DISABLE_IPV4) &&
|
2011-11-02 20:18:54 +04:00
|
|
|
mCondition == NS_ERROR_UNKNOWN_HOST &&
|
|
|
|
mState == STATE_RESOLVING &&
|
|
|
|
!mProxyTransparentResolvesHost) {
|
|
|
|
SOCKET_LOG((" trying lookup again with both ipv4/ipv6 enabled\n"));
|
2013-01-30 02:49:35 +04:00
|
|
|
mConnectionFlags &= ~(DISABLE_IPV6 | DISABLE_IPV4);
|
2011-11-02 20:18:54 +04:00
|
|
|
tryAgain = true;
|
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
// try next ip address only if past the resolver stage...
|
2003-09-12 00:32:33 +04:00
|
|
|
if (mState == STATE_CONNECTING && mDNSRecord) {
|
|
|
|
nsresult rv = mDNSRecord->GetNextAddr(SocketPort(), &mNetAddr);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG((" trying again with next ip address\n"));
|
2011-10-17 18:59:28 +04:00
|
|
|
tryAgain = true;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2013-01-30 02:49:35 +04:00
|
|
|
else if (mConnectionFlags & (DISABLE_IPV6 | DISABLE_IPV4)) {
|
2011-11-02 20:18:54 +04:00
|
|
|
// Drop state to closed. This will trigger new round of DNS
|
|
|
|
// resolving bellow.
|
2013-01-30 02:49:35 +04:00
|
|
|
// XXX Could be optimized to only switch the flags to save duplicate
|
|
|
|
// connection attempts.
|
|
|
|
SOCKET_LOG((" failed to connect all ipv4-only or ipv6-only hosts,"
|
2011-11-02 20:18:54 +04:00
|
|
|
" trying lookup/connect again with both ipv4/ipv6\n"));
|
|
|
|
mState = STATE_CLOSED;
|
2013-01-30 02:49:35 +04:00
|
|
|
mConnectionFlags &= ~(DISABLE_IPV6 | DISABLE_IPV4);
|
2011-11-02 20:18:54 +04:00
|
|
|
tryAgain = true;
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// prepare to try again.
|
|
|
|
if (tryAgain) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t msg;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
if (mState == STATE_CONNECTING) {
|
|
|
|
mState = STATE_RESOLVING;
|
|
|
|
msg = MSG_DNS_LOOKUP_COMPLETE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mState = STATE_CLOSED;
|
|
|
|
msg = MSG_ENSURE_CONNECT;
|
|
|
|
}
|
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
rv = PostEvent(msg, NS_OK);
|
2003-01-18 04:27:53 +03:00
|
|
|
if (NS_FAILED(rv))
|
2011-10-17 18:59:28 +04:00
|
|
|
tryAgain = false;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return tryAgain;
|
|
|
|
}
|
|
|
|
|
|
|
|
// called on the socket thread only
|
|
|
|
void
|
|
|
|
nsSocketTransport::OnMsgInputClosed(nsresult reason)
|
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG(("nsSocketTransport::OnMsgInputClosed [this=%p reason=%" PRIx32 "]\n",
|
|
|
|
this, static_cast<uint32_t>(reason)));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2003-10-09 05:54:07 +04:00
|
|
|
NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mInputClosed = true;
|
2003-10-09 05:54:07 +04:00
|
|
|
// check if event should affect entire transport
|
2003-01-18 04:27:53 +03:00
|
|
|
if (NS_FAILED(reason) && (reason != NS_BASE_STREAM_CLOSED))
|
|
|
|
mCondition = reason; // XXX except if NS_FAILED(mCondition), right??
|
|
|
|
else if (mOutputClosed)
|
|
|
|
mCondition = NS_BASE_STREAM_CLOSED; // XXX except if NS_FAILED(mCondition), right??
|
|
|
|
else {
|
|
|
|
if (mState == STATE_TRANSFERRING)
|
|
|
|
mPollFlags &= ~PR_POLL_READ;
|
|
|
|
mInput.OnSocketReady(reason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// called on the socket thread only
|
|
|
|
void
|
|
|
|
nsSocketTransport::OnMsgOutputClosed(nsresult reason)
|
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG(("nsSocketTransport::OnMsgOutputClosed [this=%p reason=%" PRIx32 "]\n",
|
|
|
|
this, static_cast<uint32_t>(reason)));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2003-10-09 05:54:07 +04:00
|
|
|
NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mOutputClosed = true;
|
2003-10-09 05:54:07 +04:00
|
|
|
// check if event should affect entire transport
|
2003-01-18 04:27:53 +03:00
|
|
|
if (NS_FAILED(reason) && (reason != NS_BASE_STREAM_CLOSED))
|
|
|
|
mCondition = reason; // XXX except if NS_FAILED(mCondition), right??
|
|
|
|
else if (mInputClosed)
|
|
|
|
mCondition = NS_BASE_STREAM_CLOSED; // XXX except if NS_FAILED(mCondition), right??
|
|
|
|
else {
|
|
|
|
if (mState == STATE_TRANSFERRING)
|
|
|
|
mPollFlags &= ~PR_POLL_WRITE;
|
|
|
|
mOutput.OnSocketReady(reason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-01-24 08:32:06 +03:00
|
|
|
nsSocketTransport::OnSocketConnected()
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2016-01-15 20:16:01 +03:00
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG((" advancing to STATE_TRANSFERRING\n"));
|
2003-01-24 08:32:06 +03:00
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
mPollFlags = (PR_POLL_READ | PR_POLL_WRITE | PR_POLL_EXCEPT);
|
2005-01-26 05:13:14 +03:00
|
|
|
mPollTimeout = mTimeouts[TIMEOUT_READ_WRITE];
|
2003-01-24 08:32:06 +03:00
|
|
|
mState = STATE_TRANSFERRING;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2016-01-15 20:16:01 +03:00
|
|
|
// Set the m*AddrIsSet flags only when state has reached TRANSFERRING
|
2012-01-27 01:04:54 +04:00
|
|
|
// because we need to make sure its value does not change due to failover
|
|
|
|
mNetAddrIsSet = true;
|
|
|
|
|
2003-01-24 08:32:06 +03:00
|
|
|
// assign mFD (must do this within the transport lock), but take care not
|
|
|
|
// to trample over mFDref if mFD is already set.
|
2003-01-18 04:27:53 +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);
|
2013-12-17 04:46:09 +04:00
|
|
|
NS_ASSERTION(mFD.IsInitialized(), "no socket");
|
2003-01-24 08:32:06 +03:00
|
|
|
NS_ASSERTION(mFDref == 1, "wrong socket ref count");
|
2016-01-15 20:16:01 +03:00
|
|
|
SetSocketName(mFD);
|
2011-10-17 18:59:28 +04:00
|
|
|
mFDconnected = true;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2008-03-20 01:12:28 +03:00
|
|
|
|
2014-02-06 23:51:38 +04:00
|
|
|
// Ensure keepalive is configured correctly if previously enabled.
|
|
|
|
if (mKeepaliveEnabled) {
|
|
|
|
nsresult rv = SetKeepaliveEnabledInternal(true);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG((" SetKeepaliveEnabledInternal failed rv[0x%" PRIx32 "]",
|
|
|
|
static_cast<uint32_t>(rv)));
|
2014-02-06 23:51:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 12:17:09 +04:00
|
|
|
SendStatus(NS_NET_STATUS_CONNECTED_TO);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2016-01-15 20:16:01 +03:00
|
|
|
void
|
|
|
|
nsSocketTransport::SetSocketName(PRFileDesc *fd)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
|
|
|
if (mSelfAddrIsSet) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRNetAddr prAddr;
|
|
|
|
memset(&prAddr, 0, sizeof(prAddr));
|
|
|
|
if (PR_GetSockName(fd, &prAddr) == PR_SUCCESS) {
|
|
|
|
PRNetAddrToNetAddr(&prAddr, &mSelfAddr);
|
|
|
|
mSelfAddrIsSet = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
PRFileDesc *
|
|
|
|
nsSocketTransport::GetFD_Locked()
|
|
|
|
{
|
2013-12-17 04:46:09 +04:00
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
|
2003-01-24 08:32:06 +03:00
|
|
|
// mFD is not available to the streams while disconnected.
|
|
|
|
if (!mFDconnected)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2003-01-24 08:32:06 +03:00
|
|
|
|
2013-12-17 04:46:09 +04:00
|
|
|
if (mFD.IsInitialized())
|
2003-01-18 04:27:53 +03:00
|
|
|
mFDref++;
|
2003-01-24 08:32:06 +03:00
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
return mFD;
|
|
|
|
}
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class ThunkPRClose : public Runnable
|
2013-04-10 05:45:46 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-08-05 17:20:50 +04:00
|
|
|
explicit ThunkPRClose(PRFileDesc *fd) : mFD(fd) {}
|
2013-04-10 05:45:46 +04:00
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2013-04-10 05:45:46 +04:00
|
|
|
{
|
2016-01-26 13:15:00 +03:00
|
|
|
nsSocketTransport::CloseSocket(mFD,
|
2016-03-19 06:25:00 +03:00
|
|
|
gSocketTransportService->IsTelemetryEnabledAndNotSleepPhase());
|
2013-04-10 05:45:46 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
PRFileDesc *mFD;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
STS_PRCloseOnSocketTransport(PRFileDesc *fd)
|
|
|
|
{
|
|
|
|
if (gSocketTransportService) {
|
|
|
|
// Can't PR_Close() a socket off STS thread. Thunk it to STS to die
|
|
|
|
// FIX - Should use RUN_ON_THREAD once it's generally available
|
|
|
|
// RUN_ON_THREAD(gSocketThread,WrapRunnableNM(&PR_Close, mFD);
|
|
|
|
gSocketTransportService->Dispatch(new ThunkPRClose(fd), NS_DISPATCH_NORMAL);
|
|
|
|
} else {
|
|
|
|
// something horrible has happened
|
|
|
|
NS_ASSERTION(gSocketTransportService, "No STS service");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
void
|
|
|
|
nsSocketTransport::ReleaseFD_Locked(PRFileDesc *fd)
|
|
|
|
{
|
2013-12-17 04:46:09 +04:00
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
NS_ASSERTION(mFD == fd, "wrong fd");
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG(("JIMB: ReleaseFD_Locked: mFDref = %" PRIuPTR "\n", mFDref));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
if (--mFDref == 0) {
|
2016-01-18 10:20:00 +03:00
|
|
|
if (gIOService->IsNetTearingDown() &&
|
|
|
|
((PR_IntervalNow() - gIOService->NetTearingDownStarted()) >
|
|
|
|
gSocketTransportService->MaxTimeForPrClosePref())) {
|
|
|
|
// If shutdown last to long, let the socket leak and do not close it.
|
|
|
|
SOCKET_LOG(("Intentional leak"));
|
|
|
|
} else if (PR_GetCurrentThread() == gSocketThread) {
|
2013-06-05 03:32:31 +04:00
|
|
|
SOCKET_LOG(("nsSocketTransport: calling PR_Close [this=%p]\n", this));
|
2016-03-19 06:25:00 +03:00
|
|
|
CloseSocket(mFD,
|
|
|
|
mSocketTransportService->IsTelemetryEnabledAndNotSleepPhase());
|
2013-04-10 05:45:46 +04:00
|
|
|
} else {
|
|
|
|
// Can't PR_Close() a socket off STS thread. Thunk it to STS to die
|
|
|
|
STS_PRCloseOnSocketTransport(mFD);
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
mFD = nullptr;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// socket event handler impl
|
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketTransport::OnSocketEvent(uint32_t type, nsresult status, nsISupports *param)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG(("nsSocketTransport::OnSocketEvent [this=%p type=%u status=%" PRIx32 " param=%p]\n",
|
|
|
|
this, type, static_cast<uint32_t>(status), param));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
if (NS_FAILED(mCondition)) {
|
2003-04-19 23:45:17 +04:00
|
|
|
// block event since we're apparently already dead.
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG((" blocking event [condition=%" PRIx32 "]\n",
|
|
|
|
static_cast<uint32_t>(mCondition)));
|
2003-04-19 23:45:17 +04:00
|
|
|
//
|
|
|
|
// notify input/output streams in case either has a pending notify.
|
|
|
|
//
|
|
|
|
mInput.OnSocketReady(mCondition);
|
|
|
|
mOutput.OnSocketReady(mCondition);
|
2003-10-06 05:46:31 +04:00
|
|
|
return;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case MSG_ENSURE_CONNECT:
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG((" MSG_ENSURE_CONNECT\n"));
|
2003-01-18 04:27:53 +03:00
|
|
|
//
|
|
|
|
// ensure that we have created a socket, attached it, and have a
|
|
|
|
// connection.
|
|
|
|
//
|
2013-09-06 19:06:23 +04:00
|
|
|
if (mState == STATE_CLOSED) {
|
|
|
|
// Unix domain sockets are ready to connect; mNetAddr is all we
|
|
|
|
// need. Internet address families require a DNS lookup (or possibly
|
|
|
|
// several) before we can connect.
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_UNIX)
|
2013-09-06 19:06:23 +04:00
|
|
|
if (mNetAddrIsSet && mNetAddr.raw.family == AF_LOCAL)
|
|
|
|
mCondition = InitiateSocket();
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
mCondition = ResolveHost();
|
|
|
|
|
2013-11-21 02:55:44 +04:00
|
|
|
} else {
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG((" ignoring redundant event\n"));
|
2013-11-21 02:55:44 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MSG_DNS_LOOKUP_COMPLETE:
|
2011-05-21 14:03:36 +04:00
|
|
|
if (mDNSRequest) // only send this if we actually resolved anything
|
2012-08-01 12:17:09 +04:00
|
|
|
SendStatus(NS_NET_STATUS_RESOLVED_HOST);
|
2011-05-21 14:03:36 +04:00
|
|
|
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG((" MSG_DNS_LOOKUP_COMPLETE\n"));
|
2016-11-10 06:11:27 +03:00
|
|
|
mDNSRequest = nullptr;
|
2003-10-06 05:46:31 +04:00
|
|
|
if (param) {
|
2007-07-08 11:08:04 +04:00
|
|
|
mDNSRecord = static_cast<nsIDNSRecord *>(param);
|
2014-07-24 18:05:36 +04:00
|
|
|
mDNSRecord->GetNextAddr(SocketPort(), &mNetAddr);
|
2003-09-12 00:32:33 +04:00
|
|
|
}
|
2003-10-06 05:46:31 +04:00
|
|
|
// status contains DNS lookup status
|
|
|
|
if (NS_FAILED(status)) {
|
2006-07-06 14:19:36 +04:00
|
|
|
// When using a HTTP proxy, NS_ERROR_UNKNOWN_HOST means the HTTP
|
|
|
|
// proxy host is not found, so we fixup the error code.
|
|
|
|
// For SOCKS proxies (mProxyTransparent == true), the socket
|
|
|
|
// transport resolves the real host here, so there's no fixup
|
|
|
|
// (see bug 226943).
|
|
|
|
if ((status == NS_ERROR_UNKNOWN_HOST) && !mProxyTransparent &&
|
|
|
|
!mProxyHost.IsEmpty())
|
2003-01-18 04:27:53 +03:00
|
|
|
mCondition = NS_ERROR_UNKNOWN_PROXY_HOST;
|
|
|
|
else
|
2003-10-06 05:46:31 +04:00
|
|
|
mCondition = status;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2016-06-02 09:47:00 +03:00
|
|
|
else if (mState == STATE_RESOLVING) {
|
2003-01-18 04:27:53 +03:00
|
|
|
mCondition = InitiateSocket();
|
2016-06-02 09:47:00 +03:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
break;
|
|
|
|
|
2003-04-09 02:18:10 +04:00
|
|
|
case MSG_RETRY_INIT_SOCKET:
|
|
|
|
mCondition = InitiateSocket();
|
|
|
|
break;
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
case MSG_INPUT_CLOSED:
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG((" MSG_INPUT_CLOSED\n"));
|
2003-10-06 05:46:31 +04:00
|
|
|
OnMsgInputClosed(status);
|
2003-01-18 04:27:53 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MSG_INPUT_PENDING:
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG((" MSG_INPUT_PENDING\n"));
|
2003-01-18 04:27:53 +03:00
|
|
|
OnMsgInputPending();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MSG_OUTPUT_CLOSED:
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG((" MSG_OUTPUT_CLOSED\n"));
|
2003-10-06 05:46:31 +04:00
|
|
|
OnMsgOutputClosed(status);
|
2003-01-18 04:27:53 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MSG_OUTPUT_PENDING:
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG((" MSG_OUTPUT_PENDING\n"));
|
2003-01-18 04:27:53 +03:00
|
|
|
OnMsgOutputPending();
|
|
|
|
break;
|
2005-02-01 18:22:20 +03:00
|
|
|
case MSG_TIMEOUT_CHANGED:
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG((" MSG_TIMEOUT_CHANGED\n"));
|
2005-02-01 18:22:20 +03:00
|
|
|
mPollTimeout = mTimeouts[(mState == STATE_TRANSFERRING)
|
|
|
|
? TIMEOUT_READ_WRITE : TIMEOUT_CONNECT];
|
|
|
|
break;
|
2003-01-18 04:27:53 +03:00
|
|
|
default:
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG((" unhandled event!\n"));
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(mCondition)) {
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG((" after event [this=%p cond=%" PRIx32 "]\n", this,
|
|
|
|
static_cast<uint32_t>(mCondition)));
|
2003-01-18 04:27:53 +03:00
|
|
|
if (!mAttached) // need to process this error ourselves...
|
2012-07-30 18:20:58 +04:00
|
|
|
OnSocketDetached(nullptr);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2003-02-19 00:02:49 +03:00
|
|
|
else if (mPollFlags == PR_POLL_EXCEPT)
|
|
|
|
mPollFlags = 0; // make idle
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// socket handler impl
|
|
|
|
|
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketTransport::OnSocketReady(PRFileDesc *fd, int16_t outFlags)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2013-06-05 03:32:31 +04:00
|
|
|
SOCKET_LOG(("nsSocketTransport::OnSocketReady [this=%p outFlags=%hd]\n",
|
2003-02-19 00:02:49 +03:00
|
|
|
this, outFlags));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2005-01-26 05:13:14 +03:00
|
|
|
if (outFlags == -1) {
|
2010-10-28 21:09:08 +04:00
|
|
|
SOCKET_LOG(("socket timeout expired\n"));
|
2005-01-26 05:13:14 +03:00
|
|
|
mCondition = NS_ERROR_NET_TIMEOUT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-02-14 00:33:58 +03:00
|
|
|
if (mState == STATE_TRANSFERRING) {
|
2003-02-19 00:02:49 +03:00
|
|
|
// if waiting to write and socket is writable or hit an exception.
|
|
|
|
if ((mPollFlags & PR_POLL_WRITE) && (outFlags & ~PR_POLL_READ)) {
|
|
|
|
// assume that we won't need to poll any longer (the stream will
|
|
|
|
// request that we poll again if it is still pending).
|
|
|
|
mPollFlags &= ~PR_POLL_WRITE;
|
|
|
|
mOutput.OnSocketReady(NS_OK);
|
2003-02-14 00:33:58 +03:00
|
|
|
}
|
2003-02-19 00:02:49 +03:00
|
|
|
// if waiting to read and socket is readable or hit an exception.
|
|
|
|
if ((mPollFlags & PR_POLL_READ) && (outFlags & ~PR_POLL_WRITE)) {
|
|
|
|
// assume that we won't need to poll any longer (the stream will
|
|
|
|
// request that we poll again if it is still pending).
|
|
|
|
mPollFlags &= ~PR_POLL_READ;
|
|
|
|
mInput.OnSocketReady(NS_OK);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2005-01-26 05:13:14 +03:00
|
|
|
// Update poll timeout in case it was changed
|
|
|
|
mPollTimeout = mTimeouts[TIMEOUT_READ_WRITE];
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2016-01-14 16:53:00 +03:00
|
|
|
else if ((mState == STATE_CONNECTING) && !gIOService->IsNetTearingDown()) {
|
|
|
|
// We do not need to do PR_ConnectContinue when we are already
|
|
|
|
// shutting down.
|
2015-07-07 12:13:00 +03:00
|
|
|
|
|
|
|
// We use PRIntervalTime here because we need
|
|
|
|
// nsIOService::LastOfflineStateChange time and
|
|
|
|
// nsIOService::LastConectivityChange time to be atomic.
|
|
|
|
PRIntervalTime connectStarted = 0;
|
2016-03-19 06:25:00 +03:00
|
|
|
if (gSocketTransportService->IsTelemetryEnabledAndNotSleepPhase()) {
|
2015-07-07 12:13:00 +03:00
|
|
|
connectStarted = PR_IntervalNow();
|
|
|
|
}
|
|
|
|
|
2003-02-19 00:02:49 +03:00
|
|
|
PRStatus status = PR_ConnectContinue(fd, outFlags);
|
2015-07-07 12:13:00 +03:00
|
|
|
|
2016-03-19 06:25:00 +03:00
|
|
|
if (gSocketTransportService->IsTelemetryEnabledAndNotSleepPhase() &&
|
|
|
|
connectStarted) {
|
2015-07-07 12:13:00 +03:00
|
|
|
SendPRBlockingTelemetry(connectStarted,
|
|
|
|
Telemetry::PRCONNECTCONTINUE_BLOCKING_TIME_NORMAL,
|
|
|
|
Telemetry::PRCONNECTCONTINUE_BLOCKING_TIME_SHUTDOWN,
|
|
|
|
Telemetry::PRCONNECTCONTINUE_BLOCKING_TIME_CONNECTIVITY_CHANGE,
|
|
|
|
Telemetry::PRCONNECTCONTINUE_BLOCKING_TIME_LINK_CHANGE,
|
|
|
|
Telemetry::PRCONNECTCONTINUE_BLOCKING_TIME_OFFLINE);
|
|
|
|
}
|
|
|
|
|
2003-02-19 00:02:49 +03:00
|
|
|
if (status == PR_SUCCESS) {
|
|
|
|
//
|
|
|
|
// we are connected!
|
|
|
|
//
|
|
|
|
OnSocketConnected();
|
2015-07-01 14:51:00 +03:00
|
|
|
|
2016-03-19 06:25:00 +03:00
|
|
|
if (mSocketTransportService->IsTelemetryEnabledAndNotSleepPhase()) {
|
2015-07-01 14:51:00 +03:00
|
|
|
if (mNetAddr.raw.family == AF_INET) {
|
|
|
|
Telemetry::Accumulate(
|
|
|
|
Telemetry::IPV4_AND_IPV6_ADDRESS_CONNECTIVITY,
|
|
|
|
SUCCESSFUL_CONNECTING_TO_IPV4_ADDRESS);
|
|
|
|
} else if (mNetAddr.raw.family == AF_INET6) {
|
|
|
|
Telemetry::Accumulate(
|
|
|
|
Telemetry::IPV4_AND_IPV6_ADDRESS_CONNECTIVITY,
|
|
|
|
SUCCESSFUL_CONNECTING_TO_IPV6_ADDRESS);
|
|
|
|
}
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
else {
|
2003-02-19 00:02:49 +03:00
|
|
|
PRErrorCode code = PR_GetError();
|
2003-01-18 04:27:53 +03:00
|
|
|
#if defined(TEST_CONNECT_ERRORS)
|
2003-02-19 00:02:49 +03:00
|
|
|
code = RandomizeConnectError(code);
|
2003-01-18 04:27:53 +03:00
|
|
|
#endif
|
2003-02-19 00:02:49 +03:00
|
|
|
//
|
|
|
|
// If the connect is still not ready, then continue polling...
|
|
|
|
//
|
|
|
|
if ((PR_WOULD_BLOCK_ERROR == code) || (PR_IN_PROGRESS_ERROR == code)) {
|
|
|
|
// Set up the select flags for connect...
|
|
|
|
mPollFlags = (PR_POLL_EXCEPT | PR_POLL_WRITE);
|
2005-01-26 05:13:14 +03:00
|
|
|
// Update poll timeout in case it was changed
|
|
|
|
mPollTimeout = mTimeouts[TIMEOUT_CONNECT];
|
2011-02-24 16:10:08 +03:00
|
|
|
}
|
|
|
|
//
|
|
|
|
// The SOCKS proxy rejected our request. Find out why.
|
|
|
|
//
|
|
|
|
else if (PR_UNKNOWN_ERROR == code &&
|
|
|
|
mProxyTransparent &&
|
|
|
|
!mProxyHost.IsEmpty()) {
|
|
|
|
code = PR_GetOSError();
|
|
|
|
mCondition = ErrorAccordingToNSPR(code);
|
|
|
|
}
|
2003-02-19 00:02:49 +03:00
|
|
|
else {
|
2003-01-18 04:27:53 +03:00
|
|
|
//
|
2003-02-19 00:02:49 +03:00
|
|
|
// else, the connection failed...
|
2003-01-18 04:27:53 +03:00
|
|
|
//
|
2003-02-19 00:02:49 +03:00
|
|
|
mCondition = ErrorAccordingToNSPR(code);
|
|
|
|
if ((mCondition == NS_ERROR_CONNECTION_REFUSED) && !mProxyHost.IsEmpty())
|
|
|
|
mCondition = NS_ERROR_PROXY_CONNECTION_REFUSED;
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG((" connection failed! [reason=%" PRIx32 "]\n",
|
|
|
|
static_cast<uint32_t>(mCondition)));
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-14 16:53:00 +03:00
|
|
|
else if ((mState == STATE_CONNECTING) && gIOService->IsNetTearingDown()) {
|
|
|
|
// We do not need to do PR_ConnectContinue when we are already
|
|
|
|
// shutting down.
|
|
|
|
SOCKET_LOG(("We are in shutdown so skip PR_ConnectContinue and set "
|
|
|
|
"and error.\n"));
|
|
|
|
mCondition = NS_ERROR_ABORT;
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
else {
|
|
|
|
NS_ERROR("unexpected socket state");
|
|
|
|
mCondition = NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2003-02-19 00:02:49 +03:00
|
|
|
|
|
|
|
if (mPollFlags == PR_POLL_EXCEPT)
|
|
|
|
mPollFlags = 0; // make idle
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// called on the socket thread only
|
|
|
|
void
|
|
|
|
nsSocketTransport::OnSocketDetached(PRFileDesc *fd)
|
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG(("nsSocketTransport::OnSocketDetached [this=%p cond=%" PRIx32 "]\n",
|
|
|
|
this, static_cast<uint32_t>(mCondition)));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2003-10-09 05:54:07 +04:00
|
|
|
NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
// if we didn't initiate this detach, then be sure to pass an error
|
|
|
|
// condition up to our consumers. (e.g., STS is shutting down.)
|
2013-11-16 08:36:29 +04:00
|
|
|
if (NS_SUCCEEDED(mCondition)) {
|
|
|
|
if (gIOService->IsOffline()) {
|
|
|
|
mCondition = NS_ERROR_OFFLINE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mCondition = NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
}
|
2004-08-04 21:43:33 +04:00
|
|
|
|
2015-05-29 09:06:00 +03:00
|
|
|
// If we are not shutting down try again.
|
2016-01-14 12:42:00 +03:00
|
|
|
if (!gIOService->IsNetTearingDown() && RecoverFromError())
|
2003-01-18 04:27:53 +03:00
|
|
|
mCondition = NS_OK;
|
|
|
|
else {
|
|
|
|
mState = STATE_CLOSED;
|
|
|
|
|
|
|
|
// make sure there isn't any pending DNS request
|
|
|
|
if (mDNSRequest) {
|
2005-04-06 05:33:28 +04:00
|
|
|
mDNSRequest->Cancel(NS_ERROR_ABORT);
|
2016-11-10 06:11:27 +03:00
|
|
|
mDNSRequest = nullptr;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// notify input/output streams
|
|
|
|
//
|
|
|
|
mInput.OnSocketReady(mCondition);
|
|
|
|
mOutput.OnSocketReady(mCondition);
|
|
|
|
}
|
|
|
|
|
2005-04-06 05:35:06 +04:00
|
|
|
// break any potential reference cycle between the security info object
|
|
|
|
// and ourselves by resetting its notification callbacks object. see
|
|
|
|
// bug 285991 for details.
|
|
|
|
nsCOMPtr<nsISSLSocketControl> secCtrl = do_QueryInterface(mSecInfo);
|
|
|
|
if (secCtrl)
|
2012-07-30 18:20:58 +04:00
|
|
|
secCtrl->SetNotificationCallbacks(nullptr);
|
2005-04-06 05:35:06 +04:00
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
// finally, release our reference to the socket (must do this within
|
2008-02-14 14:12:50 +03:00
|
|
|
// the transport lock) possibly closing the socket. Also release our
|
|
|
|
// listeners to break potential refcount cycles.
|
2010-12-03 00:29:09 +03:00
|
|
|
|
2010-12-03 03:22:45 +03:00
|
|
|
// We should be careful not to release mEventSink and mCallbacks while
|
|
|
|
// we're locked, because releasing it might require acquiring the lock
|
|
|
|
// again, so we just null out mEventSink and mCallbacks while we're
|
|
|
|
// holding the lock, and let the stack based objects' destuctors take
|
|
|
|
// care of destroying it if needed.
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> ourCallbacks;
|
2010-12-03 00:29:09 +03:00
|
|
|
nsCOMPtr<nsITransportEventSink> ourEventSink;
|
2003-01-18 04:27:53 +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);
|
2013-12-17 04:46:09 +04:00
|
|
|
if (mFD.IsInitialized()) {
|
2003-01-18 04:27:53 +03:00
|
|
|
ReleaseFD_Locked(mFD);
|
2003-01-24 08:32:06 +03:00
|
|
|
// flag mFD as unusable; this prevents other consumers from
|
|
|
|
// acquiring a reference to mFD.
|
2011-10-17 18:59:28 +04:00
|
|
|
mFDconnected = false;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2008-10-10 20:02:30 +04:00
|
|
|
|
|
|
|
// We must release mCallbacks and mEventSink to avoid memory leak
|
|
|
|
// but only when RecoverFromError() above failed. Otherwise we lose
|
|
|
|
// link with UI and security callbacks on next connection attempt
|
|
|
|
// round. That would lead e.g. to a broken certificate exception page.
|
|
|
|
if (NS_FAILED(mCondition)) {
|
2010-12-03 03:22:45 +03:00
|
|
|
mCallbacks.swap(ourCallbacks);
|
2010-12-03 00:29:09 +03:00
|
|
|
mEventSink.swap(ourEventSink);
|
2008-10-10 20:02:30 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-18 03:45:10 +04:00
|
|
|
void
|
|
|
|
nsSocketTransport::IsLocal(bool *aIsLocal)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
2013-09-06 19:06:23 +04:00
|
|
|
|
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 (mNetAddr.raw.family == PR_AF_LOCAL)
|
|
|
|
{
|
|
|
|
*aIsLocal = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-12-24 01:08:43 +04:00
|
|
|
*aIsLocal = IsLoopBackAddress(&mNetAddr);
|
2012-09-18 03:45:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// xpcom api
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsSocketTransport,
|
|
|
|
nsISocketTransport,
|
|
|
|
nsITransport,
|
|
|
|
nsIDNSListener,
|
2015-01-30 18:55:07 +03:00
|
|
|
nsIClassInfo,
|
|
|
|
nsIInterfaceRequestor)
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsSocketTransport,
|
|
|
|
nsISocketTransport,
|
|
|
|
nsITransport,
|
2015-01-30 18:55:07 +03:00
|
|
|
nsIDNSListener,
|
|
|
|
nsIInterfaceRequestor)
|
2008-02-21 23:39:20 +03:00
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketTransport::OpenInputStream(uint32_t flags,
|
|
|
|
uint32_t segsize,
|
|
|
|
uint32_t segcount,
|
2003-01-18 04:27:53 +03:00
|
|
|
nsIInputStream **result)
|
|
|
|
{
|
2013-06-05 03:32:31 +04:00
|
|
|
SOCKET_LOG(("nsSocketTransport::OpenInputStream [this=%p flags=%x]\n",
|
2003-01-18 04:27:53 +03:00
|
|
|
this, flags));
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(!mInput.IsReferenced(), NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> pipeIn;
|
|
|
|
|
|
|
|
if (!(flags & OPEN_UNBUFFERED) || (flags & OPEN_BLOCKING)) {
|
|
|
|
// XXX if the caller wants blocking, then the caller also gets buffered!
|
2011-09-29 10:19:26 +04:00
|
|
|
//bool openBuffered = !(flags & OPEN_UNBUFFERED);
|
|
|
|
bool openBlocking = (flags & OPEN_BLOCKING);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
net_ResolveSegmentParams(segsize, segcount);
|
|
|
|
|
|
|
|
// create a pipe
|
|
|
|
nsCOMPtr<nsIAsyncOutputStream> pipeOut;
|
|
|
|
rv = NS_NewPipe2(getter_AddRefs(pipeIn), getter_AddRefs(pipeOut),
|
2012-01-10 07:43:52 +04:00
|
|
|
!openBlocking, true, segsize, segcount);
|
2003-01-18 04:27:53 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// async copy from socket to pipe
|
2014-04-16 07:00:39 +04:00
|
|
|
rv = NS_AsyncCopy(&mInput, pipeOut, mSocketTransportService,
|
2003-10-06 05:46:31 +04:00
|
|
|
NS_ASYNCCOPY_VIA_WRITESEGMENTS, segsize);
|
2003-01-18 04:27:53 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
*result = pipeIn;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*result = &mInput;
|
|
|
|
|
|
|
|
// flag input stream as open
|
2011-10-17 18:59:28 +04:00
|
|
|
mInputClosed = false;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
rv = PostEvent(MSG_ENSURE_CONNECT);
|
2003-01-18 04:27:53 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
NS_ADDREF(*result);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketTransport::OpenOutputStream(uint32_t flags,
|
|
|
|
uint32_t segsize,
|
|
|
|
uint32_t segcount,
|
2003-01-18 04:27:53 +03:00
|
|
|
nsIOutputStream **result)
|
|
|
|
{
|
2013-06-05 03:32:31 +04:00
|
|
|
SOCKET_LOG(("nsSocketTransport::OpenOutputStream [this=%p flags=%x]\n",
|
2003-01-18 04:27:53 +03:00
|
|
|
this, flags));
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(!mOutput.IsReferenced(), NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIAsyncOutputStream> pipeOut;
|
|
|
|
if (!(flags & OPEN_UNBUFFERED) || (flags & OPEN_BLOCKING)) {
|
|
|
|
// XXX if the caller wants blocking, then the caller also gets buffered!
|
2011-09-29 10:19:26 +04:00
|
|
|
//bool openBuffered = !(flags & OPEN_UNBUFFERED);
|
|
|
|
bool openBlocking = (flags & OPEN_BLOCKING);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
net_ResolveSegmentParams(segsize, segcount);
|
|
|
|
|
|
|
|
// create a pipe
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> pipeIn;
|
|
|
|
rv = NS_NewPipe2(getter_AddRefs(pipeIn), getter_AddRefs(pipeOut),
|
2012-01-10 07:43:52 +04:00
|
|
|
true, !openBlocking, segsize, segcount);
|
2003-01-18 04:27:53 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// async copy from socket to pipe
|
2014-04-16 07:00:39 +04:00
|
|
|
rv = NS_AsyncCopy(pipeIn, &mOutput, mSocketTransportService,
|
2003-10-06 05:46:31 +04:00
|
|
|
NS_ASYNCCOPY_VIA_READSEGMENTS, segsize);
|
2003-01-18 04:27:53 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
*result = pipeOut;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*result = &mOutput;
|
|
|
|
|
|
|
|
// flag output stream as open
|
2011-10-17 18:59:28 +04:00
|
|
|
mOutputClosed = false;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
rv = PostEvent(MSG_ENSURE_CONNECT);
|
2003-01-18 04:27:53 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
NS_ADDREF(*result);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketTransport::Close(nsresult reason)
|
|
|
|
{
|
|
|
|
if (NS_SUCCEEDED(reason))
|
|
|
|
reason = NS_BASE_STREAM_CLOSED;
|
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
mInput.CloseWithStatus(reason);
|
|
|
|
mOutput.CloseWithStatus(reason);
|
2003-01-18 04:27:53 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketTransport::GetSecurityInfo(nsISupports **secinfo)
|
|
|
|
{
|
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-01-18 04:27:53 +03:00
|
|
|
NS_IF_ADDREF(*secinfo = mSecInfo);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketTransport::GetSecurityCallbacks(nsIInterfaceRequestor **callbacks)
|
|
|
|
{
|
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-01-18 04:27:53 +03:00
|
|
|
NS_IF_ADDREF(*callbacks = mCallbacks);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketTransport::SetSecurityCallbacks(nsIInterfaceRequestor *callbacks)
|
|
|
|
{
|
2013-05-28 17:48:23 +04:00
|
|
|
nsCOMPtr<nsIInterfaceRequestor> threadsafeCallbacks;
|
|
|
|
NS_NewNotificationCallbacksAggregation(callbacks, nullptr,
|
|
|
|
NS_GetCurrentThread(),
|
|
|
|
getter_AddRefs(threadsafeCallbacks));
|
|
|
|
|
2011-03-31 23:38:30 +04:00
|
|
|
nsCOMPtr<nsISupports> secinfo;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
2013-05-28 17:48:23 +04:00
|
|
|
mCallbacks = threadsafeCallbacks;
|
2011-03-31 23:38:30 +04:00
|
|
|
SOCKET_LOG(("Reset callbacks for secinfo=%p callbacks=%p\n",
|
|
|
|
mSecInfo.get(), mCallbacks.get()));
|
|
|
|
|
|
|
|
secinfo = mSecInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't call into PSM while holding mLock!!
|
|
|
|
nsCOMPtr<nsISSLSocketControl> secCtrl(do_QueryInterface(secinfo));
|
|
|
|
if (secCtrl)
|
2013-05-28 17:48:23 +04:00
|
|
|
secCtrl->SetNotificationCallbacks(threadsafeCallbacks);
|
2011-03-31 23:38:30 +04:00
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketTransport::SetEventSink(nsITransportEventSink *sink,
|
2003-10-06 05:46:31 +04:00
|
|
|
nsIEventTarget *target)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsITransportEventSink> temp;
|
2003-10-06 05:46:31 +04:00
|
|
|
if (target) {
|
|
|
|
nsresult rv = net_NewTransportEventSinkProxy(getter_AddRefs(temp),
|
|
|
|
sink, target);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2003-01-18 04:27:53 +03:00
|
|
|
sink = temp.get();
|
|
|
|
}
|
|
|
|
|
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-01-18 04:27:53 +03:00
|
|
|
mEventSink = sink;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsSocketTransport::IsAlive(bool *result)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
*result = false;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2013-12-17 04:46:09 +04:00
|
|
|
nsresult conditionWhileLocked = NS_OK;
|
|
|
|
PRFileDescAutoLock fd(this, &conditionWhileLocked);
|
|
|
|
if (NS_FAILED(conditionWhileLocked) || !fd.IsInitialized()) {
|
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX do some idle-time based checks??
|
|
|
|
|
2011-06-15 18:24:09 +04:00
|
|
|
char c;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t rval = PR_Recv(fd, &c, 1, PR_MSG_PEEK, 0);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2011-06-15 18:24:09 +04:00
|
|
|
if ((rval > 0) || (rval < 0 && PR_GetError() == PR_WOULD_BLOCK_ERROR))
|
2011-10-17 18:59:28 +04:00
|
|
|
*result = true;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketTransport::GetHost(nsACString &host)
|
|
|
|
{
|
|
|
|
host = SocketHost();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketTransport::GetPort(int32_t *port)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
*port = (int32_t) SocketPort();
|
2003-01-18 04:27:53 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-28 06:21:37 +03:00
|
|
|
NS_IMETHODIMP
|
2017-03-10 05:17:23 +03:00
|
|
|
nsSocketTransport::GetNetworkInterfaceId(nsACString &aNetworkInterfaceId)
|
2015-01-28 06:21:37 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
|
|
|
aNetworkInterfaceId = mNetworkInterfaceId;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-03-10 05:17:23 +03:00
|
|
|
nsSocketTransport::SetNetworkInterfaceId(const nsACString &aNetworkInterfaceId)
|
2015-01-28 06:21:37 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
|
|
|
mNetworkInterfaceId = aNetworkInterfaceId;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-10-12 10:32:22 +03:00
|
|
|
NS_IMETHODIMP
|
2016-11-21 06:43:06 +03:00
|
|
|
nsSocketTransport::GetScriptableOriginAttributes(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JS::Value> aOriginAttributes)
|
2016-10-12 10:32:22 +03:00
|
|
|
{
|
2016-11-21 06:43:06 +03:00
|
|
|
if (NS_WARN_IF(!ToJSValue(aCx, mOriginAttributes, aOriginAttributes))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2016-10-12 10:32:22 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-11-21 06:43:06 +03:00
|
|
|
nsSocketTransport::SetScriptableOriginAttributes(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aOriginAttributes)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
NS_ENSURE_FALSE(mFD.IsInitialized(), NS_ERROR_FAILURE);
|
|
|
|
|
2017-01-12 19:38:48 +03:00
|
|
|
OriginAttributes attrs;
|
2016-11-21 06:43:06 +03:00
|
|
|
if (!aOriginAttributes.isObject() || !attrs.Init(aCx, aOriginAttributes)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
mOriginAttributes = attrs;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2017-01-12 19:38:48 +03:00
|
|
|
nsSocketTransport::GetOriginAttributes(OriginAttributes* aOriginAttributes)
|
2016-11-21 06:43:06 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aOriginAttributes);
|
|
|
|
*aOriginAttributes = mOriginAttributes;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2017-01-12 19:38:48 +03:00
|
|
|
nsSocketTransport::SetOriginAttributes(const OriginAttributes& aOriginAttributes)
|
2016-10-12 10:32:22 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
NS_ENSURE_FALSE(mFD.IsInitialized(), NS_ERROR_FAILURE);
|
|
|
|
|
2016-11-21 06:43:06 +03:00
|
|
|
mOriginAttributes = aOriginAttributes;
|
2016-10-12 10:32:22 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
NS_IMETHODIMP
|
2012-12-24 01:08:43 +04:00
|
|
|
nsSocketTransport::GetPeerAddr(NetAddr *addr)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2003-09-12 00:32:33 +04:00
|
|
|
// once we are in the connected state, mNetAddr will not change.
|
|
|
|
// so if we can verify that we are in the connected state, then
|
|
|
|
// we can freely access mNetAddr from any thread without being
|
|
|
|
// inside a critical section.
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2012-01-27 01:04:54 +04:00
|
|
|
if (!mNetAddrIsSet) {
|
|
|
|
SOCKET_LOG(("nsSocketTransport::GetPeerAddr [this=%p state=%d] "
|
|
|
|
"NOT_AVAILABLE because not yet connected.", this, mState));
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2012-12-24 01:08:43 +04:00
|
|
|
memcpy(addr, &mNetAddr, sizeof(NetAddr));
|
2003-01-18 04:27:53 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-04-30 03:07:05 +04:00
|
|
|
NS_IMETHODIMP
|
2012-12-24 01:08:43 +04:00
|
|
|
nsSocketTransport::GetSelfAddr(NetAddr *addr)
|
2004-04-30 03:07:05 +04:00
|
|
|
{
|
2016-01-15 20:16:01 +03:00
|
|
|
// once we are in the connected state, mSelfAddr will not change.
|
|
|
|
// so if we can verify that we are in the connected state, then
|
|
|
|
// we can freely access mSelfAddr from any thread without being
|
|
|
|
// inside a critical section.
|
2004-04-30 03:07:05 +04:00
|
|
|
|
2016-01-15 20:16:01 +03:00
|
|
|
if (!mSelfAddrIsSet) {
|
|
|
|
SOCKET_LOG(("nsSocketTransport::GetSelfAddr [this=%p state=%d] "
|
|
|
|
"NOT_AVAILABLE because not yet connected.", this, mState));
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2012-12-24 01:08:43 +04:00
|
|
|
}
|
2004-04-30 03:07:05 +04:00
|
|
|
|
2016-01-15 20:16:01 +03:00
|
|
|
memcpy(addr, &mSelfAddr, sizeof(NetAddr));
|
|
|
|
return NS_OK;
|
2004-04-30 03:07:05 +04:00
|
|
|
}
|
|
|
|
|
2014-10-07 13:24:57 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketTransport::Bind(NetAddr *aLocalAddr)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aLocalAddr);
|
|
|
|
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
if (mAttached) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBindAddr = new NetAddr();
|
|
|
|
memcpy(mBindAddr.get(), aLocalAddr, sizeof(NetAddr));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-29 15:19:00 +04:00
|
|
|
NS_IMETHODIMP
|
2012-07-07 00:14:07 +04:00
|
|
|
nsSocketTransport::GetScriptablePeerAddr(nsINetAddr * *addr)
|
2012-02-29 15:19:00 +04:00
|
|
|
{
|
2012-12-24 01:08:43 +04:00
|
|
|
NetAddr rawAddr;
|
2012-02-29 15:19:00 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
rv = GetPeerAddr(&rawAddr);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
NS_ADDREF(*addr = new nsNetAddr(&rawAddr));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-07-07 00:14:07 +04:00
|
|
|
nsSocketTransport::GetScriptableSelfAddr(nsINetAddr * *addr)
|
2012-02-29 15:19:00 +04:00
|
|
|
{
|
2012-12-24 01:08:43 +04:00
|
|
|
NetAddr rawAddr;
|
2012-02-29 15:19:00 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
rv = GetSelfAddr(&rawAddr);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
NS_ADDREF(*addr = new nsNetAddr(&rawAddr));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-01-26 05:13:14 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketTransport::GetTimeout(uint32_t type, uint32_t *value)
|
2005-01-26 05:13:14 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_MAX(type, nsISocketTransport::TIMEOUT_READ_WRITE);
|
2012-08-22 19:56:38 +04:00
|
|
|
*value = (uint32_t) mTimeouts[type];
|
2005-01-26 05:13:14 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketTransport::SetTimeout(uint32_t type, uint32_t value)
|
2005-01-26 05:13:14 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_MAX(type, nsISocketTransport::TIMEOUT_READ_WRITE);
|
|
|
|
// truncate overly large timeout values.
|
2013-01-15 16:22:03 +04:00
|
|
|
mTimeouts[type] = (uint16_t) std::min<uint32_t>(value, UINT16_MAX);
|
2005-02-01 18:22:20 +03:00
|
|
|
PostEvent(MSG_TIMEOUT_CHANGED);
|
2005-01-26 05:13:14 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-12-22 10:38:06 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketTransport::SetReuseAddrPort(bool reuseAddrPort)
|
|
|
|
{
|
|
|
|
mReuseAddrPort = reuseAddrPort;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-03 06:25:01 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketTransport::SetQoSBits(uint8_t aQoSBits)
|
2010-06-03 06:25:01 +04:00
|
|
|
{
|
|
|
|
// Don't do any checking here of bits. Why? Because as of RFC-4594
|
|
|
|
// several different Class Selector and Assured Forwarding values
|
|
|
|
// have been defined, but that isn't to say more won't be added later.
|
|
|
|
// In that case, any checking would be an impediment to interoperating
|
|
|
|
// with newer QoS definitions.
|
|
|
|
|
|
|
|
mQoSBits = aQoSBits;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketTransport::GetQoSBits(uint8_t *aQoSBits)
|
2010-06-03 06:25:01 +04:00
|
|
|
{
|
|
|
|
*aQoSBits = mQoSBits;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-07 04:48:20 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketTransport::GetRecvBufferSize(uint32_t *aSize)
|
|
|
|
{
|
2013-12-17 04:46:09 +04:00
|
|
|
PRFileDescAutoLock fd(this);
|
|
|
|
if (!fd.IsInitialized())
|
2012-10-07 04:48:20 +04:00
|
|
|
return NS_ERROR_NOT_CONNECTED;
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
PRSocketOptionData opt;
|
|
|
|
opt.option = PR_SockOpt_RecvBufferSize;
|
2013-12-17 04:46:09 +04:00
|
|
|
if (PR_GetSocketOption(fd, &opt) == PR_SUCCESS)
|
2012-10-07 04:48:20 +04:00
|
|
|
*aSize = opt.value.recv_buffer_size;
|
|
|
|
else
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketTransport::GetSendBufferSize(uint32_t *aSize)
|
|
|
|
{
|
2013-12-17 04:46:09 +04:00
|
|
|
PRFileDescAutoLock fd(this);
|
|
|
|
if (!fd.IsInitialized())
|
2012-10-07 04:48:20 +04:00
|
|
|
return NS_ERROR_NOT_CONNECTED;
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
PRSocketOptionData opt;
|
|
|
|
opt.option = PR_SockOpt_SendBufferSize;
|
2013-12-17 04:46:09 +04:00
|
|
|
if (PR_GetSocketOption(fd, &opt) == PR_SUCCESS)
|
2012-10-07 04:48:20 +04:00
|
|
|
*aSize = opt.value.send_buffer_size;
|
|
|
|
else
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketTransport::SetRecvBufferSize(uint32_t aSize)
|
|
|
|
{
|
2013-12-17 04:46:09 +04:00
|
|
|
PRFileDescAutoLock fd(this);
|
|
|
|
if (!fd.IsInitialized())
|
2012-10-07 04:48:20 +04:00
|
|
|
return NS_ERROR_NOT_CONNECTED;
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
PRSocketOptionData opt;
|
|
|
|
opt.option = PR_SockOpt_RecvBufferSize;
|
|
|
|
opt.value.recv_buffer_size = aSize;
|
2013-12-17 04:46:09 +04:00
|
|
|
if (PR_SetSocketOption(fd, &opt) != PR_SUCCESS)
|
2012-10-07 04:48:20 +04:00
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketTransport::SetSendBufferSize(uint32_t aSize)
|
|
|
|
{
|
2013-12-17 04:46:09 +04:00
|
|
|
PRFileDescAutoLock fd(this);
|
|
|
|
if (!fd.IsInitialized())
|
2012-10-07 04:48:20 +04:00
|
|
|
return NS_ERROR_NOT_CONNECTED;
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
PRSocketOptionData opt;
|
|
|
|
opt.option = PR_SockOpt_SendBufferSize;
|
|
|
|
opt.value.send_buffer_size = aSize;
|
2013-12-17 04:46:09 +04:00
|
|
|
if (PR_SetSocketOption(fd, &opt) != PR_SUCCESS)
|
2012-10-07 04:48:20 +04:00
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
NS_IMETHODIMP
|
2005-04-06 05:33:28 +04:00
|
|
|
nsSocketTransport::OnLookupComplete(nsICancelable *request,
|
2003-09-12 00:32:33 +04:00
|
|
|
nsIDNSRecord *rec,
|
|
|
|
nsresult status)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2003-10-07 09:11:41 +04:00
|
|
|
// flag host lookup complete for the benefit of the ResolveHost method.
|
2011-10-17 18:59:28 +04:00
|
|
|
mResolving = false;
|
2003-10-07 09:11:41 +04:00
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
nsresult rv = PostEvent(MSG_DNS_LOOKUP_COMPLETE, status, rec);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
// if posting a message fails, then we should assume that the socket
|
|
|
|
// transport has been shutdown. this should never happen! if it does
|
|
|
|
// it means that the socket transport service was shutdown before the
|
|
|
|
// DNS service.
|
2003-10-06 05:46:31 +04:00
|
|
|
if (NS_FAILED(rv))
|
2003-01-18 04:27:53 +03:00
|
|
|
NS_WARNING("unable to post DNS lookup complete message");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2004-06-10 02:40:02 +04:00
|
|
|
|
2015-01-30 18:55:07 +03:00
|
|
|
// nsIInterfaceRequestor
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketTransport::GetInterface(const nsIID &iid, void **result)
|
|
|
|
{
|
|
|
|
if (iid.Equals(NS_GET_IID(nsIDNSRecord))) {
|
|
|
|
return mDNSRecord ?
|
|
|
|
mDNSRecord->QueryInterface(iid, result) : NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
return this->QueryInterface(iid, result);
|
|
|
|
}
|
|
|
|
|
2006-12-27 01:14:29 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketTransport::GetInterfaces(uint32_t *count, nsIID * **array)
|
2006-12-27 01:14:29 +03:00
|
|
|
{
|
|
|
|
return NS_CI_INTERFACE_GETTER_NAME(nsSocketTransport)(count, array);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-03-29 17:52:54 +03:00
|
|
|
nsSocketTransport::GetScriptableHelper(nsIXPCScriptable **_retval)
|
2006-12-27 01:14:29 +03:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*_retval = nullptr;
|
2006-12-27 01:14:29 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketTransport::GetContractID(char * *aContractID)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aContractID = nullptr;
|
2006-12-27 01:14:29 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketTransport::GetClassDescription(char * *aClassDescription)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aClassDescription = nullptr;
|
2006-12-27 01:14:29 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketTransport::GetClassID(nsCID * *aClassID)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aClassID = nullptr;
|
2006-12-27 01:14:29 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketTransport::GetFlags(uint32_t *aFlags)
|
2006-12-27 01:14:29 +03:00
|
|
|
{
|
|
|
|
*aFlags = nsIClassInfo::THREADSAFE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketTransport::GetClassIDNoAlloc(nsCID *aClassIDNoAlloc)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-04 19:05:46 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketTransport::GetConnectionFlags(uint32_t *value)
|
2008-11-04 19:05:46 +03:00
|
|
|
{
|
|
|
|
*value = mConnectionFlags;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketTransport::SetConnectionFlags(uint32_t value)
|
2008-11-04 19:05:46 +03:00
|
|
|
{
|
|
|
|
mConnectionFlags = value;
|
2012-12-08 02:50:43 +04:00
|
|
|
mIsPrivate = value & nsISocketTransport::NO_PERMANENT_STORAGE;
|
2008-11-04 19:05:46 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-06 23:51:38 +04:00
|
|
|
void
|
|
|
|
nsSocketTransport::OnKeepaliveEnabledPrefChange(bool aEnabled)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
|
|
|
|
|
|
|
// The global pref toggles keepalive as a system feature; it only affects
|
|
|
|
// an individual socket if keepalive has been specifically enabled for it.
|
|
|
|
// So, ensure keepalive is configured correctly if previously enabled.
|
|
|
|
if (mKeepaliveEnabled) {
|
|
|
|
nsresult rv = SetKeepaliveEnabledInternal(aEnabled);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG((" SetKeepaliveEnabledInternal [%s] failed rv[0x%" PRIx32 "]",
|
|
|
|
aEnabled ? "enable" : "disable", static_cast<uint32_t>(rv)));
|
2014-02-06 23:51:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSocketTransport::SetKeepaliveEnabledInternal(bool aEnable)
|
|
|
|
{
|
2014-04-23 01:38:05 +04:00
|
|
|
MOZ_ASSERT(mKeepaliveIdleTimeS > 0 &&
|
2014-02-06 23:51:38 +04:00
|
|
|
mKeepaliveIdleTimeS <= kMaxTCPKeepIdle);
|
2014-04-23 01:38:05 +04:00
|
|
|
MOZ_ASSERT(mKeepaliveRetryIntervalS > 0 &&
|
2014-02-06 23:51:38 +04:00
|
|
|
mKeepaliveRetryIntervalS <= kMaxTCPKeepIntvl);
|
2014-04-23 01:38:05 +04:00
|
|
|
MOZ_ASSERT(mKeepaliveProbeCount > 0 &&
|
2014-02-06 23:51:38 +04:00
|
|
|
mKeepaliveProbeCount <= kMaxTCPKeepCount);
|
|
|
|
|
|
|
|
PRFileDescAutoLock fd(this);
|
|
|
|
if (NS_WARN_IF(!fd.IsInitialized())) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only enable if keepalives are globally enabled, but ensure other
|
|
|
|
// options are set correctly on the fd.
|
2014-04-16 07:00:39 +04:00
|
|
|
bool enable = aEnable && mSocketTransportService->IsKeepaliveEnabled();
|
2014-02-06 23:51:38 +04:00
|
|
|
nsresult rv = fd.SetKeepaliveVals(enable,
|
|
|
|
mKeepaliveIdleTimeS,
|
|
|
|
mKeepaliveRetryIntervalS,
|
|
|
|
mKeepaliveProbeCount);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG((" SetKeepaliveVals failed rv[0x%" PRIx32 "]", static_cast<uint32_t>(rv)));
|
2014-02-06 23:51:38 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = fd.SetKeepaliveEnabled(enable);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG((" SetKeepaliveEnabled failed rv[0x%" PRIx32 "]", static_cast<uint32_t>(rv)));
|
2014-02-06 23:51:38 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketTransport::GetKeepaliveEnabled(bool *aResult)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aResult);
|
|
|
|
|
|
|
|
*aResult = mKeepaliveEnabled;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSocketTransport::EnsureKeepaliveValsAreInitialized()
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
int32_t val = -1;
|
|
|
|
if (mKeepaliveIdleTimeS == -1) {
|
2014-04-16 07:00:39 +04:00
|
|
|
rv = mSocketTransportService->GetKeepaliveIdleTime(&val);
|
2014-02-06 23:51:38 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
mKeepaliveIdleTimeS = val;
|
|
|
|
}
|
|
|
|
if (mKeepaliveRetryIntervalS == -1) {
|
2014-04-16 07:00:39 +04:00
|
|
|
rv = mSocketTransportService->GetKeepaliveRetryInterval(&val);
|
2014-02-06 23:51:38 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
mKeepaliveRetryIntervalS = val;
|
|
|
|
}
|
|
|
|
if (mKeepaliveProbeCount == -1) {
|
2014-04-16 07:00:39 +04:00
|
|
|
rv = mSocketTransportService->GetKeepaliveProbeCount(&val);
|
2014-02-06 23:51:38 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
mKeepaliveProbeCount = val;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketTransport::SetKeepaliveEnabled(bool aEnable)
|
|
|
|
{
|
|
|
|
#if defined(XP_WIN) || defined(XP_UNIX) || defined(XP_MACOSX)
|
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
|
|
|
|
|
|
|
if (aEnable == mKeepaliveEnabled) {
|
|
|
|
SOCKET_LOG(("nsSocketTransport::SetKeepaliveEnabled [%p] already %s.",
|
|
|
|
this, aEnable ? "enabled" : "disabled"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (aEnable) {
|
|
|
|
rv = EnsureKeepaliveValsAreInitialized();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
SOCKET_LOG((" SetKeepaliveEnabled [%p] "
|
2016-12-16 06:16:31 +03:00
|
|
|
"error [0x%" PRIx32 "] initializing keepalive vals",
|
|
|
|
this, static_cast<uint32_t>(rv)));
|
2014-02-06 23:51:38 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SOCKET_LOG(("nsSocketTransport::SetKeepaliveEnabled [%p] "
|
|
|
|
"%s, idle time[%ds] retry interval[%ds] packet count[%d]: "
|
|
|
|
"globally %s.",
|
|
|
|
this, aEnable ? "enabled" : "disabled",
|
|
|
|
mKeepaliveIdleTimeS, mKeepaliveRetryIntervalS,
|
|
|
|
mKeepaliveProbeCount,
|
2014-04-16 07:00:39 +04:00
|
|
|
mSocketTransportService->IsKeepaliveEnabled() ?
|
2014-02-06 23:51:38 +04:00
|
|
|
"enabled" : "disabled"));
|
|
|
|
|
|
|
|
// Set mKeepaliveEnabled here so that state is maintained; it is possible
|
|
|
|
// that we're in between fds, e.g. the 1st IP address failed, so we're about
|
|
|
|
// to retry on a 2nd from the DNS record.
|
|
|
|
mKeepaliveEnabled = aEnable;
|
|
|
|
|
|
|
|
rv = SetKeepaliveEnabledInternal(aEnable);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2016-12-16 06:16:31 +03:00
|
|
|
SOCKET_LOG((" SetKeepaliveEnabledInternal failed rv[0x%" PRIx32 "]",
|
|
|
|
static_cast<uint32_t>(rv)));
|
2014-02-06 23:51:38 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
#else /* !(defined(XP_WIN) || defined(XP_UNIX) || defined(XP_MACOSX)) */
|
|
|
|
SOCKET_LOG(("nsSocketTransport::SetKeepaliveEnabled unsupported platform"));
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSocketTransport::SetKeepaliveVals(int32_t aIdleTime,
|
|
|
|
int32_t aRetryInterval)
|
|
|
|
{
|
|
|
|
#if defined(XP_WIN) || defined(XP_UNIX) || defined(XP_MACOSX)
|
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
|
|
|
if (NS_WARN_IF(aIdleTime <= 0 || kMaxTCPKeepIdle < aIdleTime)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
if (NS_WARN_IF(aRetryInterval <= 0 ||
|
|
|
|
kMaxTCPKeepIntvl < aRetryInterval)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aIdleTime == mKeepaliveIdleTimeS &&
|
|
|
|
aRetryInterval == mKeepaliveRetryIntervalS) {
|
|
|
|
SOCKET_LOG(("nsSocketTransport::SetKeepaliveVals [%p] idle time "
|
|
|
|
"already %ds and retry interval already %ds.",
|
|
|
|
this, mKeepaliveIdleTimeS,
|
|
|
|
mKeepaliveRetryIntervalS));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
mKeepaliveIdleTimeS = aIdleTime;
|
|
|
|
mKeepaliveRetryIntervalS = aRetryInterval;
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (mKeepaliveProbeCount == -1) {
|
|
|
|
int32_t val = -1;
|
2014-04-16 07:00:39 +04:00
|
|
|
nsresult rv = mSocketTransportService->GetKeepaliveProbeCount(&val);
|
2014-02-06 23:51:38 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
mKeepaliveProbeCount = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
SOCKET_LOG(("nsSocketTransport::SetKeepaliveVals [%p] "
|
|
|
|
"keepalive %s, idle time[%ds] retry interval[%ds] "
|
|
|
|
"packet count[%d]",
|
|
|
|
this, mKeepaliveEnabled ? "enabled" : "disabled",
|
|
|
|
mKeepaliveIdleTimeS, mKeepaliveRetryIntervalS,
|
|
|
|
mKeepaliveProbeCount));
|
|
|
|
|
|
|
|
PRFileDescAutoLock fd(this);
|
|
|
|
if (NS_WARN_IF(!fd.IsInitialized())) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = fd.SetKeepaliveVals(mKeepaliveEnabled,
|
|
|
|
mKeepaliveIdleTimeS,
|
|
|
|
mKeepaliveRetryIntervalS,
|
|
|
|
mKeepaliveProbeCount);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
#else
|
|
|
|
SOCKET_LOG(("nsSocketTransport::SetKeepaliveVals unsupported platform"));
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
#endif
|
|
|
|
}
|
2008-11-04 19:05:46 +03:00
|
|
|
|
2004-06-10 02:40:02 +04:00
|
|
|
#ifdef ENABLE_SOCKET_TRACING
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include "prenv.h"
|
|
|
|
|
|
|
|
static void
|
2012-08-22 19:56:38 +04:00
|
|
|
DumpBytesToFile(const char *path, const char *header, const char *buf, int32_t n)
|
2004-06-10 02:40:02 +04:00
|
|
|
{
|
|
|
|
FILE *fp = fopen(path, "a");
|
|
|
|
|
|
|
|
fprintf(fp, "\n%s [%d bytes]\n", header, n);
|
|
|
|
|
|
|
|
const unsigned char *p;
|
|
|
|
while (n) {
|
|
|
|
p = (const unsigned char *) buf;
|
|
|
|
|
2013-01-15 16:22:03 +04:00
|
|
|
int32_t i, row_max = std::min(16, n);
|
2004-06-10 02:40:02 +04:00
|
|
|
|
|
|
|
for (i = 0; i < row_max; ++i)
|
|
|
|
fprintf(fp, "%02x ", *p++);
|
|
|
|
for (i = row_max; i < 16; ++i)
|
|
|
|
fprintf(fp, " ");
|
|
|
|
|
|
|
|
p = (const unsigned char *) buf;
|
|
|
|
for (i = 0; i < row_max; ++i, ++p) {
|
|
|
|
if (isprint(*p))
|
|
|
|
fprintf(fp, "%c", *p);
|
|
|
|
else
|
|
|
|
fprintf(fp, ".");
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
buf += row_max;
|
|
|
|
n -= row_max;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketTransport::TraceInBuf(const char *buf, int32_t n)
|
2004-06-10 02:40:02 +04:00
|
|
|
{
|
|
|
|
char *val = PR_GetEnv("NECKO_SOCKET_TRACE_LOG");
|
|
|
|
if (!val || !*val)
|
|
|
|
return;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString header;
|
2014-05-22 07:48:52 +04:00
|
|
|
header.AssignLiteral("Reading from: ");
|
|
|
|
header.Append(mHost);
|
2004-06-10 02:40:02 +04:00
|
|
|
header.Append(':');
|
|
|
|
header.AppendInt(mPort);
|
|
|
|
|
|
|
|
DumpBytesToFile(val, header.get(), buf, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSocketTransport::TraceOutBuf(const char *buf, int32_t n)
|
2004-06-10 02:40:02 +04:00
|
|
|
{
|
|
|
|
char *val = PR_GetEnv("NECKO_SOCKET_TRACE_LOG");
|
|
|
|
if (!val || !*val)
|
|
|
|
return;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString header;
|
2014-05-22 07:48:52 +04:00
|
|
|
header.AssignLiteral("Writing to: ");
|
|
|
|
header.Append(mHost);
|
2004-06-10 02:40:02 +04:00
|
|
|
header.Append(':');
|
|
|
|
header.AppendInt(mPort);
|
|
|
|
|
|
|
|
DumpBytesToFile(val, header.get(), buf, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2014-02-06 23:51:38 +04:00
|
|
|
|
|
|
|
static void LogNSPRError(const char* aPrefix, const void *aObjPtr)
|
|
|
|
{
|
2015-05-07 22:52:44 +03:00
|
|
|
#if defined(DEBUG)
|
2014-02-06 23:51:38 +04:00
|
|
|
PRErrorCode errCode = PR_GetError();
|
|
|
|
int errLen = PR_GetErrorTextLength();
|
|
|
|
nsAutoCString errStr;
|
|
|
|
if (errLen > 0) {
|
|
|
|
errStr.SetLength(errLen);
|
|
|
|
PR_GetErrorText(errStr.BeginWriting());
|
|
|
|
}
|
|
|
|
NS_WARNING(nsPrintfCString(
|
|
|
|
"%s [%p] NSPR error[0x%x] %s.",
|
|
|
|
aPrefix ? aPrefix : "nsSocketTransport", aObjPtr, errCode,
|
|
|
|
errLen > 0 ? errStr.BeginReading() : "<no error text>").get());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSocketTransport::PRFileDescAutoLock::SetKeepaliveEnabled(bool aEnable)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
|
|
|
MOZ_ASSERT(!(aEnable && !gSocketTransportService->IsKeepaliveEnabled()),
|
|
|
|
"Cannot enable keepalive if global pref is disabled!");
|
|
|
|
if (aEnable && !gSocketTransportService->IsKeepaliveEnabled()) {
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRSocketOptionData opt;
|
|
|
|
|
|
|
|
opt.option = PR_SockOpt_Keepalive;
|
|
|
|
opt.value.keep_alive = aEnable;
|
|
|
|
PRStatus status = PR_SetSocketOption(mFd, &opt);
|
|
|
|
if (NS_WARN_IF(status != PR_SUCCESS)) {
|
|
|
|
LogNSPRError("nsSocketTransport::PRFileDescAutoLock::SetKeepaliveEnabled",
|
|
|
|
mSocketTransport);
|
|
|
|
return ErrorAccordingToNSPR(PR_GetError());
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void LogOSError(const char *aPrefix, const void *aObjPtr)
|
|
|
|
{
|
2015-05-07 22:52:44 +03:00
|
|
|
#if defined(DEBUG)
|
2014-02-06 23:51:38 +04:00
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
DWORD errCode = WSAGetLastError();
|
|
|
|
LPVOID errMessage;
|
|
|
|
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL,
|
|
|
|
errCode,
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(LPTSTR) &errMessage,
|
|
|
|
0, NULL);
|
|
|
|
#else
|
|
|
|
int errCode = errno;
|
|
|
|
char *errMessage = strerror(errno);
|
|
|
|
#endif
|
|
|
|
NS_WARNING(nsPrintfCString(
|
|
|
|
"%s [%p] OS error[0x%x] %s",
|
|
|
|
aPrefix ? aPrefix : "nsSocketTransport", aObjPtr, errCode,
|
|
|
|
errMessage ? errMessage : "<no error text>").get());
|
|
|
|
#ifdef XP_WIN
|
|
|
|
LocalFree(errMessage);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX PR_SetSockOpt does not support setting keepalive values, so native
|
|
|
|
* handles and platform specific apis (setsockopt, WSAIOCtl) are used in this
|
|
|
|
* file. Requires inclusion of NSPR private/pprio.h, and platform headers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSocketTransport::PRFileDescAutoLock::SetKeepaliveVals(bool aEnabled,
|
|
|
|
int aIdleTime,
|
|
|
|
int aRetryInterval,
|
|
|
|
int aProbeCount)
|
|
|
|
{
|
|
|
|
#if defined(XP_WIN) || defined(XP_UNIX) || defined(XP_MACOSX)
|
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == gSocketThread, "wrong thread");
|
|
|
|
if (NS_WARN_IF(aIdleTime <= 0 || kMaxTCPKeepIdle < aIdleTime)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
if (NS_WARN_IF(aRetryInterval <= 0 ||
|
|
|
|
kMaxTCPKeepIntvl < aRetryInterval)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
if (NS_WARN_IF(aProbeCount <= 0 || kMaxTCPKeepCount < aProbeCount)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
PROsfd sock = PR_FileDesc2NativeHandle(mFd);
|
|
|
|
if (NS_WARN_IF(sock == -1)) {
|
|
|
|
LogNSPRError("nsSocketTransport::PRFileDescAutoLock::SetKeepaliveVals",
|
|
|
|
mSocketTransport);
|
|
|
|
return ErrorAccordingToNSPR(PR_GetError());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
// Windows allows idle time and retry interval to be set; NOT ping count.
|
|
|
|
struct tcp_keepalive keepalive_vals = {
|
2016-03-24 01:17:44 +03:00
|
|
|
(u_long)aEnabled,
|
2014-02-06 23:51:38 +04:00
|
|
|
// Windows uses msec.
|
2016-03-24 01:17:44 +03:00
|
|
|
(u_long)(aIdleTime * 1000UL),
|
|
|
|
(u_long)(aRetryInterval * 1000UL)
|
2014-02-06 23:51:38 +04:00
|
|
|
};
|
|
|
|
DWORD bytes_returned;
|
|
|
|
int err = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &keepalive_vals,
|
|
|
|
sizeof(keepalive_vals), NULL, 0, &bytes_returned, NULL,
|
|
|
|
NULL);
|
|
|
|
if (NS_WARN_IF(err)) {
|
|
|
|
LogOSError("nsSocketTransport WSAIoctl failed", mSocketTransport);
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
|
2015-05-06 14:51:51 +03:00
|
|
|
#elif defined(XP_DARWIN)
|
|
|
|
// Darwin uses sec; only supports idle time being set.
|
2014-02-06 23:51:38 +04:00
|
|
|
int err = setsockopt(sock, IPPROTO_TCP, TCP_KEEPALIVE,
|
|
|
|
&aIdleTime, sizeof(aIdleTime));
|
|
|
|
if (NS_WARN_IF(err)) {
|
|
|
|
LogOSError("nsSocketTransport Failed setting TCP_KEEPALIVE",
|
|
|
|
mSocketTransport);
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
#elif defined(XP_UNIX)
|
2014-02-13 16:19:39 +04:00
|
|
|
// Not all *nix OSes support the following setsockopt() options
|
|
|
|
// ... but we assume they are supported in the Android kernel;
|
|
|
|
// build errors will tell us if they are not.
|
|
|
|
#if defined(ANDROID) || defined(TCP_KEEPIDLE)
|
|
|
|
// Idle time until first keepalive probe; interval between ack'd probes; seconds.
|
2014-02-06 23:51:38 +04:00
|
|
|
int err = setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE,
|
|
|
|
&aIdleTime, sizeof(aIdleTime));
|
|
|
|
if (NS_WARN_IF(err)) {
|
|
|
|
LogOSError("nsSocketTransport Failed setting TCP_KEEPIDLE",
|
|
|
|
mSocketTransport);
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2014-02-13 16:19:39 +04:00
|
|
|
#endif
|
|
|
|
#if defined(ANDROID) || defined(TCP_KEEPINTVL)
|
|
|
|
// Interval between unack'd keepalive probes; seconds.
|
2014-02-06 23:51:38 +04:00
|
|
|
err = setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL,
|
|
|
|
&aRetryInterval, sizeof(aRetryInterval));
|
|
|
|
if (NS_WARN_IF(err)) {
|
|
|
|
LogOSError("nsSocketTransport Failed setting TCP_KEEPINTVL",
|
|
|
|
mSocketTransport);
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2014-02-13 16:19:39 +04:00
|
|
|
#endif
|
|
|
|
#if defined(ANDROID) || defined(TCP_KEEPCNT)
|
|
|
|
// Number of unack'd keepalive probes before connection times out.
|
2014-02-06 23:51:38 +04:00
|
|
|
err = setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT,
|
|
|
|
&aProbeCount, sizeof(aProbeCount));
|
|
|
|
if (NS_WARN_IF(err)) {
|
|
|
|
LogOSError("nsSocketTransport Failed setting TCP_KEEPCNT",
|
|
|
|
mSocketTransport);
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2014-02-13 16:19:39 +04:00
|
|
|
#endif
|
2014-02-06 23:51:38 +04:00
|
|
|
return NS_OK;
|
|
|
|
#else
|
|
|
|
MOZ_ASSERT(false, "nsSocketTransport::PRFileDescAutoLock::SetKeepaliveVals "
|
|
|
|
"called on unsupported platform!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
#endif
|
|
|
|
}
|
2015-07-07 12:13:00 +03:00
|
|
|
|
2016-01-26 13:15:00 +03:00
|
|
|
void
|
2016-02-04 06:42:00 +03:00
|
|
|
nsSocketTransport::CloseSocket(PRFileDesc *aFd, bool aTelemetryEnabled)
|
|
|
|
{
|
|
|
|
#if defined(XP_WIN)
|
2016-05-19 05:02:57 +03:00
|
|
|
AttachShutdownLayer(aFd);
|
2016-02-04 06:42:00 +03:00
|
|
|
#endif
|
2016-01-26 13:15:00 +03:00
|
|
|
|
|
|
|
// We use PRIntervalTime here because we need
|
|
|
|
// nsIOService::LastOfflineStateChange time and
|
|
|
|
// nsIOService::LastConectivityChange time to be atomic.
|
|
|
|
PRIntervalTime closeStarted;
|
|
|
|
if (aTelemetryEnabled) {
|
|
|
|
closeStarted = PR_IntervalNow();
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_Close(aFd);
|
|
|
|
|
|
|
|
if (aTelemetryEnabled) {
|
|
|
|
SendPRBlockingTelemetry(closeStarted,
|
|
|
|
Telemetry::PRCLOSE_TCP_BLOCKING_TIME_NORMAL,
|
|
|
|
Telemetry::PRCLOSE_TCP_BLOCKING_TIME_SHUTDOWN,
|
|
|
|
Telemetry::PRCLOSE_TCP_BLOCKING_TIME_CONNECTIVITY_CHANGE,
|
|
|
|
Telemetry::PRCLOSE_TCP_BLOCKING_TIME_LINK_CHANGE,
|
|
|
|
Telemetry::PRCLOSE_TCP_BLOCKING_TIME_OFFLINE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 12:13:00 +03:00
|
|
|
void
|
|
|
|
nsSocketTransport::SendPRBlockingTelemetry(PRIntervalTime aStart,
|
2017-02-15 22:15:15 +03:00
|
|
|
Telemetry::HistogramID aIDNormal,
|
|
|
|
Telemetry::HistogramID aIDShutdown,
|
|
|
|
Telemetry::HistogramID aIDConnectivityChange,
|
|
|
|
Telemetry::HistogramID aIDLinkChange,
|
|
|
|
Telemetry::HistogramID aIDOffline)
|
2015-07-07 12:13:00 +03:00
|
|
|
{
|
|
|
|
PRIntervalTime now = PR_IntervalNow();
|
2016-01-14 12:42:00 +03:00
|
|
|
if (gIOService->IsNetTearingDown()) {
|
2015-07-07 12:13:00 +03:00
|
|
|
Telemetry::Accumulate(aIDShutdown,
|
|
|
|
PR_IntervalToMilliseconds(now - aStart));
|
|
|
|
|
|
|
|
} else if (PR_IntervalToSeconds(now - gIOService->LastConnectivityChange())
|
|
|
|
< 60) {
|
|
|
|
Telemetry::Accumulate(aIDConnectivityChange,
|
|
|
|
PR_IntervalToMilliseconds(now - aStart));
|
|
|
|
} else if (PR_IntervalToSeconds(now - gIOService->LastNetworkLinkChange())
|
|
|
|
< 60) {
|
|
|
|
Telemetry::Accumulate(aIDLinkChange,
|
|
|
|
PR_IntervalToMilliseconds(now - aStart));
|
|
|
|
|
|
|
|
} else if (PR_IntervalToSeconds(now - gIOService->LastOfflineStateChange())
|
|
|
|
< 60) {
|
|
|
|
Telemetry::Accumulate(aIDOffline,
|
|
|
|
PR_IntervalToMilliseconds(now - aStart));
|
|
|
|
} else {
|
|
|
|
Telemetry::Accumulate(aIDNormal,
|
|
|
|
PR_IntervalToMilliseconds(now - aStart));
|
|
|
|
}
|
|
|
|
}
|
2016-05-19 05:02:57 +03:00
|
|
|
|
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|