2012-10-03 00:04:58 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
/*
|
|
|
|
Modified version of nr_socket_local, adapted for NSPR
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
Original code from nICEr and nrappkit.
|
|
|
|
|
|
|
|
nICEr copyright:
|
|
|
|
|
|
|
|
Copyright (c) 2007, Adobe Systems, Incorporated
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are
|
|
|
|
met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of Adobe Systems, Network Resonance nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived from
|
|
|
|
this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
nrappkit copyright:
|
|
|
|
|
|
|
|
Copyright (C) 2001-2003, Network Resonance, Inc.
|
|
|
|
Copyright (C) 2006, Network Resonance, Inc.
|
|
|
|
All Rights Reserved
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
3. Neither the name of Network Resonance, Inc. nor the name of any
|
|
|
|
contributors to this software may be used to endorse or promote
|
|
|
|
products derived from this software without specific prior written
|
|
|
|
permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
ekr@rtfm.com Thu Dec 20 20:14:49 2001
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <csi_platform.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "nspr.h"
|
|
|
|
#include "prerror.h"
|
|
|
|
#include "prio.h"
|
|
|
|
#include "prnetdb.h"
|
|
|
|
|
2013-03-09 00:11:17 +04:00
|
|
|
#include "mozilla/net/DNS.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsASocketHandler.h"
|
|
|
|
#include "nsISocketTransportService.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2013-06-19 15:08:06 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
#include "nsXPCOM.h"
|
2013-06-19 15:08:06 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
2013-02-16 06:33:31 +04:00
|
|
|
#include "runnable_utils.h"
|
2015-01-05 10:49:50 +03:00
|
|
|
#include "mozilla/SyncRunnable.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "mozilla/dom/TCPSocketBinding.h"
|
|
|
|
#include "nsITCPSocketCallback.h"
|
|
|
|
|
|
|
|
#if defined(MOZILLA_INTERNAL_API) && !defined(MOZILLA_XPCOMRT_API)
|
|
|
|
// csi_platform.h deep in nrappkit defines LOG_INFO and LOG_WARNING
|
|
|
|
#ifdef LOG_INFO
|
|
|
|
#define LOG_TEMP_INFO LOG_INFO
|
|
|
|
#undef LOG_INFO
|
|
|
|
#endif
|
|
|
|
#ifdef LOG_WARNING
|
|
|
|
#define LOG_TEMP_WARNING LOG_WARNING
|
|
|
|
#undef LOG_WARNING
|
|
|
|
#endif
|
|
|
|
#if defined(LOG_DEBUG)
|
|
|
|
#define LOG_TEMP_DEBUG LOG_DEBUG
|
|
|
|
#undef LOG_DEBUG
|
|
|
|
#endif
|
|
|
|
#undef strlcpy
|
|
|
|
|
|
|
|
// TCPSocketChild.h doesn't include TypedArray.h
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
class ArrayBuffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#include "mozilla/dom/network/TCPSocketChild.h"
|
|
|
|
|
|
|
|
#ifdef LOG_TEMP_INFO
|
|
|
|
#define LOG_INFO LOG_TEMP_INFO
|
|
|
|
#endif
|
|
|
|
#ifdef LOG_TEMP_WARNING
|
|
|
|
#define LOG_WARNING LOG_TEMP_WARNING
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LOG_TEMP_DEBUG
|
|
|
|
#define LOG_DEBUG LOG_TEMP_DEBUG
|
|
|
|
#endif
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#ifdef LOG_DEBUG
|
|
|
|
#undef LOG_DEBUG
|
|
|
|
#endif
|
|
|
|
// cloned from csi_platform.h. Win32 doesn't like how we hide symbols
|
|
|
|
#define LOG_DEBUG 7
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "nr_api.h"
|
|
|
|
#include "async_wait.h"
|
|
|
|
#include "nr_socket.h"
|
|
|
|
#include "nr_socket_local.h"
|
2013-11-17 20:27:44 +04:00
|
|
|
#include "stun_hint.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
}
|
|
|
|
#include "nr_socket_prsock.h"
|
2013-11-17 20:27:44 +04:00
|
|
|
#include "simpletokenbucket.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
// Implement the nsISupports ref counting
|
|
|
|
namespace mozilla {
|
|
|
|
|
2015-05-29 17:13:33 +03:00
|
|
|
#if defined(MOZILLA_INTERNAL_API) && !defined(MOZILLA_XPCOMRT_API)
|
|
|
|
class SingletonThreadHolder final
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
~SingletonThreadHolder()
|
|
|
|
{
|
|
|
|
r_log(LOG_GENERIC,LOG_DEBUG,"Deleting SingletonThreadHolder");
|
2015-11-18 23:03:25 +03:00
|
|
|
MOZ_ASSERT(!mThread, "SingletonThreads should be Released and shut down before exit!");
|
|
|
|
if (mThread) {
|
2015-05-29 17:13:33 +03:00
|
|
|
mThread->Shutdown();
|
|
|
|
mThread = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DISALLOW_COPY_ASSIGN(SingletonThreadHolder);
|
|
|
|
|
|
|
|
public:
|
2015-11-18 23:03:25 +03:00
|
|
|
// Must be threadsafe for StaticRefPtr/ClearOnShutdown
|
2015-05-29 17:13:33 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SingletonThreadHolder)
|
|
|
|
|
|
|
|
explicit SingletonThreadHolder(const nsCSubstring& aName)
|
|
|
|
: mName(aName)
|
|
|
|
{
|
|
|
|
mParentThread = NS_GetCurrentThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIThread* GetThread() {
|
|
|
|
return mThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Keep track of how many instances are using a SingletonThreadHolder.
|
|
|
|
* When no one is using it, shut it down
|
|
|
|
*/
|
|
|
|
MozExternalRefCountType AddUse()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mParentThread == NS_GetCurrentThread());
|
|
|
|
MOZ_ASSERT(int32_t(mUseCount) >= 0, "illegal refcnt");
|
|
|
|
nsrefcnt count = ++mUseCount;
|
|
|
|
if (count == 1) {
|
|
|
|
// idle -> in-use
|
|
|
|
nsresult rv = NS_NewThread(getter_AddRefs(mThread));
|
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv) && mThread,
|
|
|
|
"Should successfully create mtransport I/O thread");
|
|
|
|
NS_SetThreadName(mThread, mName);
|
|
|
|
r_log(LOG_GENERIC,LOG_DEBUG,"Created wrapped SingletonThread %p",
|
|
|
|
mThread.get());
|
|
|
|
}
|
|
|
|
r_log(LOG_GENERIC,LOG_DEBUG,"AddUse: %lu", (unsigned long) count);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
MozExternalRefCountType ReleaseUse()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mParentThread == NS_GetCurrentThread());
|
|
|
|
nsrefcnt count = --mUseCount;
|
|
|
|
MOZ_ASSERT(int32_t(mUseCount) >= 0, "illegal refcnt");
|
|
|
|
if (count == 0) {
|
|
|
|
// in-use -> idle -- no one forcing it to remain instantiated
|
|
|
|
r_log(LOG_GENERIC,LOG_DEBUG,"Shutting down wrapped SingletonThread %p",
|
|
|
|
mThread.get());
|
|
|
|
mThread->Shutdown();
|
|
|
|
mThread = nullptr;
|
2015-11-18 23:03:25 +03:00
|
|
|
// It'd be nice to use a timer instead... But be careful of
|
|
|
|
// xpcom-shutdown-threads in that case
|
2015-05-29 17:13:33 +03:00
|
|
|
}
|
|
|
|
r_log(LOG_GENERIC,LOG_DEBUG,"ReleaseUse: %lu", (unsigned long) count);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCString mName;
|
|
|
|
nsAutoRefCnt mUseCount;
|
|
|
|
nsCOMPtr<nsIThread> mParentThread;
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
|
|
|
};
|
|
|
|
|
|
|
|
static StaticRefPtr<SingletonThreadHolder> sThread;
|
|
|
|
|
|
|
|
static void ClearSingletonOnShutdown()
|
|
|
|
{
|
|
|
|
ClearOnShutdown(&sThread);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
static nsIThread* GetIOThreadAndAddUse_s()
|
|
|
|
{
|
|
|
|
// Always runs on STS thread!
|
|
|
|
#if defined(MOZILLA_INTERNAL_API) && !defined(MOZILLA_XPCOMRT_API)
|
|
|
|
// We need to safely release this on shutdown to avoid leaks
|
|
|
|
if (!sThread) {
|
|
|
|
sThread = new SingletonThreadHolder(NS_LITERAL_CSTRING("mtransport"));
|
|
|
|
NS_DispatchToMainThread(mozilla::WrapRunnableNM(&ClearSingletonOnShutdown));
|
|
|
|
}
|
|
|
|
// Mark that we're using the shared thread and need it to stick around
|
|
|
|
sThread->AddUse();
|
|
|
|
return sThread->GetThread();
|
|
|
|
#else
|
|
|
|
static nsCOMPtr<nsIThread> sThread;
|
|
|
|
if (!sThread) {
|
|
|
|
(void) NS_NewNamedThread("mtransport", getter_AddRefs(sThread));
|
|
|
|
}
|
|
|
|
return sThread;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
NrSocketIpc::NrSocketIpc(nsIEventTarget *aThread)
|
|
|
|
: io_thread_(aThread)
|
|
|
|
{}
|
|
|
|
|
2014-05-20 06:16:38 +04:00
|
|
|
static TimeStamp nr_socket_short_term_violation_time;
|
|
|
|
static TimeStamp nr_socket_long_term_violation_time;
|
|
|
|
|
|
|
|
TimeStamp NrSocketBase::short_term_violation_time() {
|
|
|
|
return nr_socket_short_term_violation_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeStamp NrSocketBase::long_term_violation_time() {
|
|
|
|
return nr_socket_long_term_violation_time;
|
|
|
|
}
|
|
|
|
|
2013-06-19 15:08:06 +04:00
|
|
|
// NrSocketBase implementation
|
2012-10-03 00:04:58 +04:00
|
|
|
// async_event APIs
|
2013-06-19 15:08:06 +04:00
|
|
|
int NrSocketBase::async_wait(int how, NR_async_cb cb, void *cb_arg,
|
|
|
|
char *function, int line) {
|
2012-10-03 00:04:58 +04:00
|
|
|
uint16_t flag;
|
|
|
|
|
|
|
|
switch (how) {
|
|
|
|
case NR_ASYNC_WAIT_READ:
|
|
|
|
flag = PR_POLL_READ;
|
|
|
|
break;
|
|
|
|
case NR_ASYNC_WAIT_WRITE:
|
|
|
|
flag = PR_POLL_WRITE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return R_BAD_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
cbs_[how] = cb;
|
|
|
|
cb_args_[how] = cb_arg;
|
2013-06-19 15:08:06 +04:00
|
|
|
poll_flags_ |= flag;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-19 15:08:06 +04:00
|
|
|
int NrSocketBase::cancel(int how) {
|
2012-10-03 00:04:58 +04:00
|
|
|
uint16_t flag;
|
|
|
|
|
|
|
|
switch (how) {
|
|
|
|
case NR_ASYNC_WAIT_READ:
|
|
|
|
flag = PR_POLL_READ;
|
|
|
|
break;
|
|
|
|
case NR_ASYNC_WAIT_WRITE:
|
|
|
|
flag = PR_POLL_WRITE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return R_BAD_ARGS;
|
|
|
|
}
|
|
|
|
|
2013-06-19 15:08:06 +04:00
|
|
|
poll_flags_ &= ~flag;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-19 15:08:06 +04:00
|
|
|
void NrSocketBase::fire_callback(int how) {
|
2012-10-03 00:04:58 +04:00
|
|
|
// This can't happen unless we are armed because we only set
|
|
|
|
// the flags if we are armed
|
|
|
|
MOZ_ASSERT(cbs_[how]);
|
|
|
|
|
|
|
|
// Now cancel so that we need to be re-armed. Note that
|
|
|
|
// the re-arming probably happens in the callback we are
|
|
|
|
// about to fire.
|
|
|
|
cancel(how);
|
|
|
|
|
|
|
|
cbs_[how](this, how, cb_args_[how]);
|
|
|
|
}
|
|
|
|
|
2013-06-19 15:08:06 +04:00
|
|
|
// NrSocket implementation
|
|
|
|
NS_IMPL_ISUPPORTS0(NrSocket)
|
|
|
|
|
|
|
|
|
|
|
|
// The nsASocket callbacks
|
|
|
|
void NrSocket::OnSocketReady(PRFileDesc *fd, int16_t outflags) {
|
2014-04-23 12:15:25 +04:00
|
|
|
if (outflags & PR_POLL_READ & poll_flags())
|
2013-06-19 15:08:06 +04:00
|
|
|
fire_callback(NR_ASYNC_WAIT_READ);
|
2014-04-23 12:15:25 +04:00
|
|
|
if (outflags & PR_POLL_WRITE & poll_flags())
|
2013-06-19 15:08:06 +04:00
|
|
|
fire_callback(NR_ASYNC_WAIT_WRITE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NrSocket::OnSocketDetached(PRFileDesc *fd) {
|
2014-04-23 12:15:25 +04:00
|
|
|
r_log(LOG_GENERIC, LOG_DEBUG, "Socket %p detached", fd);
|
2013-06-19 15:08:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void NrSocket::IsLocal(bool *aIsLocal) {
|
|
|
|
// TODO(jesup): better check? Does it matter? (likely no)
|
|
|
|
*aIsLocal = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// async_event APIs
|
|
|
|
int NrSocket::async_wait(int how, NR_async_cb cb, void *cb_arg,
|
|
|
|
char *function, int line) {
|
|
|
|
int r = NrSocketBase::async_wait(how, cb, cb_arg, function, line);
|
|
|
|
|
|
|
|
if (!r) {
|
|
|
|
mPollFlags = poll_flags();
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NrSocket::cancel(int how) {
|
|
|
|
int r = NrSocketBase::cancel(how);
|
|
|
|
|
|
|
|
if (!r) {
|
|
|
|
mPollFlags = poll_flags();
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
// Helper functions for addresses
|
|
|
|
static int nr_transport_addr_to_praddr(nr_transport_addr *addr,
|
|
|
|
PRNetAddr *naddr)
|
|
|
|
{
|
|
|
|
int _status;
|
|
|
|
|
|
|
|
memset(naddr, 0, sizeof(*naddr));
|
|
|
|
|
|
|
|
switch(addr->protocol){
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
break;
|
|
|
|
case IPPROTO_UDP:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ABORT(R_BAD_ARGS);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(addr->ip_version){
|
|
|
|
case NR_IPV4:
|
|
|
|
naddr->inet.family = PR_AF_INET;
|
|
|
|
naddr->inet.port = addr->u.addr4.sin_port;
|
|
|
|
naddr->inet.ip = addr->u.addr4.sin_addr.s_addr;
|
|
|
|
break;
|
|
|
|
case NR_IPV6:
|
|
|
|
naddr->ipv6.family = PR_AF_INET6;
|
|
|
|
naddr->ipv6.port = addr->u.addr6.sin6_port;
|
2015-04-22 23:02:50 +03:00
|
|
|
naddr->ipv6.flowinfo = addr->u.addr6.sin6_flowinfo;
|
|
|
|
memcpy(&naddr->ipv6.ip, &addr->u.addr6.sin6_addr, sizeof(in6_addr));
|
|
|
|
naddr->ipv6.scope_id = addr->u.addr6.sin6_scope_id;
|
2012-10-03 00:04:58 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ABORT(R_BAD_ARGS);
|
|
|
|
}
|
|
|
|
|
|
|
|
_status = 0;
|
|
|
|
abort:
|
|
|
|
return(_status);
|
|
|
|
}
|
|
|
|
|
2013-06-19 15:08:06 +04:00
|
|
|
//XXX schien@mozilla.com: copy from PRNetAddrToNetAddr,
|
|
|
|
// should be removed after fix the link error in signaling_unittests
|
|
|
|
static int praddr_to_netaddr(const PRNetAddr *prAddr, net::NetAddr *addr)
|
|
|
|
{
|
|
|
|
int _status;
|
|
|
|
|
|
|
|
switch (prAddr->raw.family) {
|
|
|
|
case PR_AF_INET:
|
|
|
|
addr->inet.family = AF_INET;
|
|
|
|
addr->inet.port = prAddr->inet.port;
|
|
|
|
addr->inet.ip = prAddr->inet.ip;
|
|
|
|
break;
|
|
|
|
case PR_AF_INET6:
|
|
|
|
addr->inet6.family = AF_INET6;
|
|
|
|
addr->inet6.port = prAddr->ipv6.port;
|
|
|
|
addr->inet6.flowinfo = prAddr->ipv6.flowinfo;
|
|
|
|
memcpy(&addr->inet6.ip, &prAddr->ipv6.ip, sizeof(addr->inet6.ip.u8));
|
|
|
|
addr->inet6.scope_id = prAddr->ipv6.scope_id;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
ABORT(R_BAD_ARGS);
|
|
|
|
}
|
|
|
|
|
|
|
|
_status = 0;
|
|
|
|
abort:
|
|
|
|
return(_status);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nr_transport_addr_to_netaddr(nr_transport_addr *addr,
|
|
|
|
net::NetAddr *naddr)
|
|
|
|
{
|
|
|
|
int r, _status;
|
|
|
|
PRNetAddr praddr;
|
|
|
|
|
|
|
|
if((r = nr_transport_addr_to_praddr(addr, &praddr))) {
|
|
|
|
ABORT(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
if((r = praddr_to_netaddr(&praddr, naddr))) {
|
|
|
|
ABORT(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
_status = 0;
|
|
|
|
abort:
|
|
|
|
return(_status);
|
|
|
|
}
|
|
|
|
|
2013-03-09 00:11:17 +04:00
|
|
|
int nr_netaddr_to_transport_addr(const net::NetAddr *netaddr,
|
2013-12-06 22:20:19 +04:00
|
|
|
nr_transport_addr *addr, int protocol)
|
2013-03-09 00:11:17 +04:00
|
|
|
{
|
|
|
|
int _status;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
switch(netaddr->raw.family) {
|
|
|
|
case AF_INET:
|
|
|
|
if ((r = nr_ip4_port_to_transport_addr(ntohl(netaddr->inet.ip),
|
|
|
|
ntohs(netaddr->inet.port),
|
2013-12-06 22:20:19 +04:00
|
|
|
protocol, addr)))
|
2013-03-09 00:11:17 +04:00
|
|
|
ABORT(r);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2015-08-01 04:18:02 +03:00
|
|
|
if ((r = nr_ip6_port_to_transport_addr((in6_addr *)&netaddr->inet6.ip.u8,
|
|
|
|
ntohs(netaddr->inet6.port),
|
|
|
|
protocol, addr)))
|
|
|
|
ABORT(r);
|
|
|
|
break;
|
2013-03-09 00:11:17 +04:00
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
ABORT(R_BAD_ARGS);
|
|
|
|
}
|
2016-01-22 10:47:01 +03:00
|
|
|
_status = 0;
|
2013-03-09 00:11:17 +04:00
|
|
|
abort:
|
|
|
|
return(_status);
|
|
|
|
}
|
|
|
|
|
2013-01-05 23:40:34 +04:00
|
|
|
int nr_praddr_to_transport_addr(const PRNetAddr *praddr,
|
2013-12-06 22:20:19 +04:00
|
|
|
nr_transport_addr *addr, int protocol,
|
|
|
|
int keep)
|
2012-10-03 00:04:58 +04:00
|
|
|
{
|
|
|
|
int _status;
|
|
|
|
int r;
|
|
|
|
struct sockaddr_in ip4;
|
2015-04-22 23:02:50 +03:00
|
|
|
struct sockaddr_in6 ip6;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
switch(praddr->raw.family) {
|
|
|
|
case PR_AF_INET:
|
|
|
|
ip4.sin_family = PF_INET;
|
|
|
|
ip4.sin_addr.s_addr = praddr->inet.ip;
|
|
|
|
ip4.sin_port = praddr->inet.port;
|
|
|
|
if ((r = nr_sockaddr_to_transport_addr((sockaddr *)&ip4,
|
2013-12-06 22:20:19 +04:00
|
|
|
protocol, keep,
|
2013-03-09 00:11:17 +04:00
|
|
|
addr)))
|
2012-10-03 00:04:58 +04:00
|
|
|
ABORT(r);
|
|
|
|
break;
|
|
|
|
case PR_AF_INET6:
|
2015-04-22 23:02:50 +03:00
|
|
|
ip6.sin6_family = PF_INET6;
|
|
|
|
ip6.sin6_port = praddr->ipv6.port;
|
|
|
|
ip6.sin6_flowinfo = praddr->ipv6.flowinfo;
|
|
|
|
memcpy(&ip6.sin6_addr, &praddr->ipv6.ip, sizeof(in6_addr));
|
|
|
|
ip6.sin6_scope_id = praddr->ipv6.scope_id;
|
|
|
|
if ((r = nr_sockaddr_to_transport_addr((sockaddr *)&ip6,protocol,keep,addr)))
|
|
|
|
ABORT(r);
|
2012-10-03 00:04:58 +04:00
|
|
|
break;
|
|
|
|
default:
|
2013-03-09 00:11:17 +04:00
|
|
|
MOZ_ASSERT(false);
|
2012-10-03 00:04:58 +04:00
|
|
|
ABORT(R_BAD_ARGS);
|
|
|
|
}
|
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
_status = 0;
|
2012-10-03 00:04:58 +04:00
|
|
|
abort:
|
|
|
|
return(_status);
|
|
|
|
}
|
|
|
|
|
2013-06-19 15:08:06 +04:00
|
|
|
/*
|
|
|
|
* nr_transport_addr_get_addrstring_and_port
|
|
|
|
* convert nr_transport_addr to IP address string and port number
|
|
|
|
*/
|
|
|
|
int nr_transport_addr_get_addrstring_and_port(nr_transport_addr *addr,
|
|
|
|
nsACString *host, int32_t *port) {
|
|
|
|
int r, _status;
|
|
|
|
char addr_string[64];
|
|
|
|
|
|
|
|
// We cannot directly use |nr_transport_addr.as_string| because it contains
|
|
|
|
// more than ip address, therefore, we need to explicity convert it
|
|
|
|
// from |nr_transport_addr_get_addrstring|.
|
|
|
|
if ((r=nr_transport_addr_get_addrstring(addr, addr_string, sizeof(addr_string)))) {
|
|
|
|
ABORT(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((r=nr_transport_addr_get_port(addr, port))) {
|
|
|
|
ABORT(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
*host = addr_string;
|
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
_status = 0;
|
2013-06-19 15:08:06 +04:00
|
|
|
abort:
|
|
|
|
return(_status);
|
|
|
|
}
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
// nr_socket APIs (as member functions)
|
|
|
|
int NrSocket::create(nr_transport_addr *addr) {
|
|
|
|
int r,_status;
|
|
|
|
|
|
|
|
PRStatus status;
|
|
|
|
PRNetAddr naddr;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsISocketTransportService> stservice =
|
|
|
|
do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
|
|
|
|
|
|
|
|
if (!NS_SUCCEEDED(rv)) {
|
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if((r=nr_transport_addr_to_praddr(addr, &naddr)))
|
|
|
|
ABORT(r);
|
|
|
|
|
2013-12-06 22:20:19 +04:00
|
|
|
switch (addr->protocol) {
|
|
|
|
case IPPROTO_UDP:
|
2015-04-22 23:02:50 +03:00
|
|
|
if (!(fd_ = PR_OpenUDPSocket(naddr.raw.family))) {
|
|
|
|
r_log(LOG_GENERIC,LOG_CRIT,"Couldn't create UDP socket, "
|
|
|
|
"family=%d, err=%d", naddr.raw.family, PR_GetError());
|
2013-12-06 22:20:19 +04:00
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IPPROTO_TCP:
|
2015-04-22 23:02:50 +03:00
|
|
|
if (!(fd_ = PR_OpenTCPSocket(naddr.raw.family))) {
|
|
|
|
r_log(LOG_GENERIC,LOG_CRIT,"Couldn't create TCP socket, "
|
|
|
|
"family=%d, err=%d", naddr.raw.family, PR_GetError());
|
2013-12-06 22:20:19 +04:00
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
}
|
2014-04-23 12:15:25 +04:00
|
|
|
// 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) {
|
2015-06-19 09:02:38 +03:00
|
|
|
r_log(LOG_GENERIC, LOG_CRIT,
|
|
|
|
"Couldn't set reuse addr socket option: %d", status);
|
2014-04-23 12:15:25 +04:00
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
}
|
|
|
|
// 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) {
|
|
|
|
if (PR_GetError() != PR_OPERATION_NOT_SUPPORTED_ERROR) {
|
2015-06-19 09:02:38 +03:00
|
|
|
r_log(LOG_GENERIC, LOG_CRIT,
|
|
|
|
"Couldn't set reuse port socket option: %d", status);
|
2014-04-23 12:15:25 +04:00
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
}
|
|
|
|
}
|
2015-06-19 09:02:38 +03:00
|
|
|
// Try to speedup packet delivery by disabling TCP Nagle
|
|
|
|
PRSocketOptionData opt_nodelay;
|
|
|
|
opt_nodelay.option = PR_SockOpt_NoDelay;
|
|
|
|
opt_nodelay.value.no_delay = PR_TRUE;
|
|
|
|
status = PR_SetSocketOption(fd_, &opt_nodelay);
|
|
|
|
if (status != PR_SUCCESS) {
|
|
|
|
r_log(LOG_GENERIC, LOG_WARNING,
|
|
|
|
"Couldn't set Nodelay socket option: %d", status);
|
|
|
|
}
|
2013-12-06 22:20:19 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ABORT(R_INTERNAL);
|
2012-10-03 00:04:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
status = PR_Bind(fd_, &naddr);
|
|
|
|
if (status != PR_SUCCESS) {
|
|
|
|
r_log(LOG_GENERIC,LOG_CRIT,"Couldn't bind socket to address %s",
|
|
|
|
addr->as_string);
|
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
}
|
|
|
|
|
2013-05-21 05:16:07 +04:00
|
|
|
r_log(LOG_GENERIC,LOG_DEBUG,"Creating socket %p with addr %s",
|
2012-10-03 00:04:58 +04:00
|
|
|
fd_, addr->as_string);
|
|
|
|
nr_transport_addr_copy(&my_addr_,addr);
|
|
|
|
|
|
|
|
/* If we have a wildcard port, patch up the addr */
|
|
|
|
if(nr_transport_addr_is_wildcard(addr)){
|
|
|
|
status = PR_GetSockName(fd_, &naddr);
|
|
|
|
if (status != PR_SUCCESS){
|
|
|
|
r_log(LOG_GENERIC, LOG_CRIT, "Couldn't get sock name for socket");
|
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
}
|
|
|
|
|
2013-12-06 22:20:19 +04:00
|
|
|
if((r=nr_praddr_to_transport_addr(&naddr,&my_addr_,addr->protocol,1)))
|
2012-10-03 00:04:58 +04:00
|
|
|
ABORT(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set nonblocking
|
2014-04-23 12:15:25 +04:00
|
|
|
PRSocketOptionData opt_nonblock;
|
|
|
|
opt_nonblock.option = PR_SockOpt_Nonblocking;
|
|
|
|
opt_nonblock.value.non_blocking = PR_TRUE;
|
|
|
|
status = PR_SetSocketOption(fd_, &opt_nonblock);
|
2012-10-03 00:04:58 +04:00
|
|
|
if (status != PR_SUCCESS) {
|
|
|
|
r_log(LOG_GENERIC, LOG_CRIT, "Couldn't make socket nonblocking");
|
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
}
|
|
|
|
|
2013-02-16 06:33:31 +04:00
|
|
|
// Remember our thread.
|
|
|
|
ststhread_ = do_QueryInterface(stservice, &rv);
|
|
|
|
if (!NS_SUCCEEDED(rv))
|
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
// Finally, register with the STS
|
|
|
|
rv = stservice->AttachSocket(fd_, this);
|
|
|
|
if (!NS_SUCCEEDED(rv)) {
|
2014-04-23 12:15:25 +04:00
|
|
|
r_log(LOG_GENERIC, LOG_CRIT, "Couldn't attach socket to STS");
|
2012-10-03 00:04:58 +04:00
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
_status = 0;
|
|
|
|
|
|
|
|
abort:
|
|
|
|
return(_status);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should be called on the STS thread.
|
|
|
|
int NrSocket::sendto(const void *msg, size_t len,
|
|
|
|
int flags, nr_transport_addr *to) {
|
2013-02-16 06:33:31 +04:00
|
|
|
ASSERT_ON_THREAD(ststhread_);
|
2012-10-03 00:04:58 +04:00
|
|
|
int r,_status;
|
|
|
|
PRNetAddr naddr;
|
|
|
|
int32_t status;
|
|
|
|
|
|
|
|
if ((r=nr_transport_addr_to_praddr(to, &naddr)))
|
|
|
|
ABORT(r);
|
|
|
|
|
|
|
|
if(fd_==nullptr)
|
|
|
|
ABORT(R_EOD);
|
|
|
|
|
2013-11-17 20:27:44 +04:00
|
|
|
if (nr_is_stun_request_message((UCHAR*)msg, len)) {
|
|
|
|
// Global rate limiting for stun requests, to mitigate the ice hammer DoS
|
|
|
|
// (see http://tools.ietf.org/html/draft-thomson-mmusic-ice-webrtc)
|
|
|
|
|
|
|
|
// Tolerate rate of 8k/sec, for one second.
|
2014-08-12 01:53:51 +04:00
|
|
|
static SimpleTokenBucket burst(16384*1, 16384);
|
2014-08-22 02:35:04 +04:00
|
|
|
// Tolerate rate of 7.2k/sec over twenty seconds.
|
|
|
|
static SimpleTokenBucket sustained(7372*20, 7372);
|
2013-11-17 20:27:44 +04:00
|
|
|
|
|
|
|
// Check number of tokens in each bucket.
|
2014-05-20 06:16:38 +04:00
|
|
|
if (burst.getTokens(UINT32_MAX) < len) {
|
2013-11-17 20:27:44 +04:00
|
|
|
r_log(LOG_GENERIC, LOG_ERR,
|
2014-05-20 06:16:38 +04:00
|
|
|
"Short term global rate limit for STUN requests exceeded.");
|
2014-08-12 01:53:51 +04:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
nr_socket_short_term_violation_time = TimeStamp::Now();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Bug 1013007
|
|
|
|
#if !EARLY_BETA_OR_EARLIER
|
|
|
|
ABORT(R_WOULDBLOCK);
|
|
|
|
#else
|
2014-03-08 10:57:15 +04:00
|
|
|
MOZ_ASSERT(false,
|
2014-05-20 06:16:38 +04:00
|
|
|
"Short term global rate limit for STUN requests exceeded. Go "
|
|
|
|
"bug bcampen@mozilla.com if you weren't intentionally "
|
|
|
|
"spamming ICE candidates, or don't know what that means.");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sustained.getTokens(UINT32_MAX) < len) {
|
|
|
|
r_log(LOG_GENERIC, LOG_ERR,
|
|
|
|
"Long term global rate limit for STUN requests exceeded.");
|
2014-08-12 01:53:51 +04:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
nr_socket_long_term_violation_time = TimeStamp::Now();
|
|
|
|
#endif
|
|
|
|
// Bug 1013007
|
|
|
|
#if !EARLY_BETA_OR_EARLIER
|
|
|
|
ABORT(R_WOULDBLOCK);
|
|
|
|
#else
|
2014-05-20 06:16:38 +04:00
|
|
|
MOZ_ASSERT(false,
|
|
|
|
"Long term global rate limit for STUN requests exceeded. Go "
|
|
|
|
"bug bcampen@mozilla.com if you weren't intentionally "
|
|
|
|
"spamming ICE candidates, or don't know what that means.");
|
|
|
|
#endif
|
2013-11-17 20:27:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Take len tokens from both buckets.
|
|
|
|
// (not threadsafe, but no problem since this is only called from STS)
|
|
|
|
burst.getTokens(len);
|
|
|
|
sustained.getTokens(len);
|
|
|
|
}
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
// TODO: Convert flags?
|
|
|
|
status = PR_SendTo(fd_, msg, len, flags, &naddr, PR_INTERVAL_NO_WAIT);
|
|
|
|
if (status < 0 || (size_t)status != len) {
|
2013-07-22 19:48:24 +04:00
|
|
|
if (PR_GetError() == PR_WOULD_BLOCK_ERROR)
|
|
|
|
ABORT(R_WOULDBLOCK);
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2015-04-22 23:02:50 +03:00
|
|
|
r_log(LOG_GENERIC, LOG_INFO, "Error in sendto %s: %d",
|
|
|
|
to->as_string, PR_GetError());
|
2012-10-03 00:04:58 +04:00
|
|
|
ABORT(R_IO_ERROR);
|
|
|
|
}
|
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
_status = 0;
|
2012-10-03 00:04:58 +04:00
|
|
|
abort:
|
|
|
|
return(_status);
|
|
|
|
}
|
|
|
|
|
|
|
|
int NrSocket::recvfrom(void * buf, size_t maxlen,
|
2013-12-06 22:20:19 +04:00
|
|
|
size_t *len, int flags,
|
|
|
|
nr_transport_addr *from) {
|
2013-02-16 06:33:31 +04:00
|
|
|
ASSERT_ON_THREAD(ststhread_);
|
2012-10-03 00:04:58 +04:00
|
|
|
int r,_status;
|
|
|
|
PRNetAddr nfrom;
|
|
|
|
int32_t status;
|
|
|
|
|
|
|
|
status = PR_RecvFrom(fd_, buf, maxlen, flags, &nfrom, PR_INTERVAL_NO_WAIT);
|
|
|
|
if (status <= 0) {
|
2014-05-02 21:49:00 +04:00
|
|
|
if (PR_GetError() == PR_WOULD_BLOCK_ERROR)
|
|
|
|
ABORT(R_WOULDBLOCK);
|
2015-06-11 01:27:12 +03:00
|
|
|
r_log(LOG_GENERIC, LOG_INFO, "Error in recvfrom: %d", (int)PR_GetError());
|
2012-10-03 00:04:58 +04:00
|
|
|
ABORT(R_IO_ERROR);
|
|
|
|
}
|
2016-01-22 10:47:01 +03:00
|
|
|
*len = status;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2013-12-06 22:20:19 +04:00
|
|
|
if((r=nr_praddr_to_transport_addr(&nfrom,from,my_addr_.protocol,0)))
|
2012-10-03 00:04:58 +04:00
|
|
|
ABORT(r);
|
|
|
|
|
|
|
|
//r_log(LOG_GENERIC,LOG_DEBUG,"Read %d bytes from %s",*len,addr->as_string);
|
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
_status = 0;
|
2012-10-03 00:04:58 +04:00
|
|
|
abort:
|
|
|
|
return(_status);
|
|
|
|
}
|
|
|
|
|
|
|
|
int NrSocket::getaddr(nr_transport_addr *addrp) {
|
2013-02-16 06:33:31 +04:00
|
|
|
ASSERT_ON_THREAD(ststhread_);
|
2012-10-03 00:04:58 +04:00
|
|
|
return nr_transport_addr_copy(addrp, &my_addr_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the socket so that the STS will detach and then kill it
|
|
|
|
void NrSocket::close() {
|
2013-02-16 06:33:31 +04:00
|
|
|
ASSERT_ON_THREAD(ststhread_);
|
2012-10-03 00:04:58 +04:00
|
|
|
mCondition = NS_BASE_STREAM_CLOSED;
|
|
|
|
}
|
2013-06-19 15:08:06 +04:00
|
|
|
|
2013-12-06 22:20:19 +04:00
|
|
|
|
|
|
|
int NrSocket::connect(nr_transport_addr *addr) {
|
|
|
|
ASSERT_ON_THREAD(ststhread_);
|
|
|
|
int r,_status;
|
|
|
|
PRNetAddr naddr;
|
2015-08-12 17:53:15 +03:00
|
|
|
int32_t connect_status, getsockname_status;
|
2013-12-06 22:20:19 +04:00
|
|
|
|
|
|
|
if ((r=nr_transport_addr_to_praddr(addr, &naddr)))
|
|
|
|
ABORT(r);
|
|
|
|
|
|
|
|
if(!fd_)
|
|
|
|
ABORT(R_EOD);
|
|
|
|
|
|
|
|
// Note: this just means we tried to connect, not that we
|
|
|
|
// are actually live.
|
|
|
|
connect_invoked_ = true;
|
2015-08-12 17:53:15 +03:00
|
|
|
connect_status = PR_Connect(fd_, &naddr, PR_INTERVAL_NO_WAIT);
|
|
|
|
if (connect_status != PR_SUCCESS) {
|
|
|
|
if (PR_GetError() != PR_IN_PROGRESS_ERROR)
|
|
|
|
ABORT(R_IO_ERROR);
|
|
|
|
}
|
2013-12-06 22:20:19 +04:00
|
|
|
|
2015-08-12 17:53:15 +03:00
|
|
|
// If our local address is wildcard, then fill in the
|
|
|
|
// address now.
|
|
|
|
if(nr_transport_addr_is_wildcard(&my_addr_)){
|
|
|
|
getsockname_status = PR_GetSockName(fd_, &naddr);
|
|
|
|
if (getsockname_status != PR_SUCCESS){
|
|
|
|
r_log(LOG_GENERIC, LOG_CRIT, "Couldn't get sock name for socket");
|
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
}
|
2013-12-06 22:20:19 +04:00
|
|
|
|
2015-08-12 17:53:15 +03:00
|
|
|
if((r=nr_praddr_to_transport_addr(&naddr,&my_addr_,addr->protocol,1)))
|
|
|
|
ABORT(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now return the WOULDBLOCK if needed.
|
|
|
|
if (connect_status != PR_SUCCESS) {
|
|
|
|
ABORT(R_WOULDBLOCK);
|
2013-12-06 22:20:19 +04:00
|
|
|
}
|
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
_status = 0;
|
2013-12-06 22:20:19 +04:00
|
|
|
abort:
|
|
|
|
return(_status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int NrSocket::write(const void *msg, size_t len, size_t *written) {
|
|
|
|
ASSERT_ON_THREAD(ststhread_);
|
|
|
|
int _status;
|
|
|
|
int32_t status;
|
|
|
|
|
|
|
|
if (!connect_invoked_)
|
|
|
|
ABORT(R_FAILED);
|
|
|
|
|
|
|
|
status = PR_Write(fd_, msg, len);
|
|
|
|
if (status < 0) {
|
|
|
|
if (PR_GetError() == PR_WOULD_BLOCK_ERROR)
|
|
|
|
ABORT(R_WOULDBLOCK);
|
2014-04-23 12:15:25 +04:00
|
|
|
r_log(LOG_GENERIC, LOG_INFO, "Error in write");
|
2013-12-06 22:20:19 +04:00
|
|
|
ABORT(R_IO_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
*written = status;
|
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
_status = 0;
|
2013-12-06 22:20:19 +04:00
|
|
|
abort:
|
|
|
|
return _status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NrSocket::read(void* buf, size_t maxlen, size_t *len) {
|
|
|
|
ASSERT_ON_THREAD(ststhread_);
|
|
|
|
int _status;
|
|
|
|
int32_t status;
|
|
|
|
|
|
|
|
if (!connect_invoked_)
|
|
|
|
ABORT(R_FAILED);
|
|
|
|
|
|
|
|
status = PR_Read(fd_, buf, maxlen);
|
|
|
|
if (status < 0) {
|
|
|
|
if (PR_GetError() == PR_WOULD_BLOCK_ERROR)
|
|
|
|
ABORT(R_WOULDBLOCK);
|
2014-04-23 12:15:25 +04:00
|
|
|
r_log(LOG_GENERIC, LOG_INFO, "Error in read");
|
2013-12-06 22:20:19 +04:00
|
|
|
ABORT(R_IO_ERROR);
|
|
|
|
}
|
|
|
|
if (status == 0)
|
|
|
|
ABORT(R_EOD);
|
|
|
|
|
|
|
|
*len = (size_t)status; // Guaranteed to be > 0
|
|
|
|
_status = 0;
|
|
|
|
abort:
|
|
|
|
return(_status);
|
|
|
|
}
|
|
|
|
|
2014-04-23 12:15:25 +04:00
|
|
|
int NrSocket::listen(int backlog) {
|
|
|
|
ASSERT_ON_THREAD(ststhread_);
|
|
|
|
int32_t status;
|
|
|
|
int _status;
|
|
|
|
|
|
|
|
assert(fd_);
|
|
|
|
status = PR_Listen(fd_, backlog);
|
|
|
|
if (status != PR_SUCCESS) {
|
2016-01-21 02:25:26 +03:00
|
|
|
r_log(LOG_GENERIC, LOG_CRIT, "%s: PR_GetError() == %d",
|
|
|
|
__FUNCTION__, PR_GetError());
|
2014-04-23 12:15:25 +04:00
|
|
|
ABORT(R_IO_ERROR);
|
|
|
|
}
|
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
_status = 0;
|
2014-04-23 12:15:25 +04:00
|
|
|
abort:
|
|
|
|
return(_status);
|
|
|
|
}
|
|
|
|
|
|
|
|
int NrSocket::accept(nr_transport_addr *addrp, nr_socket **sockp) {
|
|
|
|
ASSERT_ON_THREAD(ststhread_);
|
|
|
|
int _status, r;
|
|
|
|
PRStatus status;
|
2015-06-19 09:02:38 +03:00
|
|
|
PRFileDesc *prfd;
|
2014-04-23 12:15:25 +04:00
|
|
|
PRNetAddr nfrom;
|
|
|
|
NrSocket *sock=nullptr;
|
|
|
|
nsresult rv;
|
2015-06-19 09:02:38 +03:00
|
|
|
PRSocketOptionData opt_nonblock, opt_nodelay;
|
2014-04-23 12:15:25 +04:00
|
|
|
nsCOMPtr<nsISocketTransportService> stservice =
|
|
|
|
do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
}
|
|
|
|
|
2015-06-19 09:02:38 +03:00
|
|
|
if(!fd_)
|
|
|
|
ABORT(R_EOD);
|
|
|
|
|
|
|
|
prfd = PR_Accept(fd_, &nfrom, PR_INTERVAL_NO_WAIT);
|
2014-04-23 12:15:25 +04:00
|
|
|
|
2015-06-19 09:02:38 +03:00
|
|
|
if (!prfd) {
|
2014-04-23 12:15:25 +04:00
|
|
|
if (PR_GetError() == PR_WOULD_BLOCK_ERROR)
|
|
|
|
ABORT(R_WOULDBLOCK);
|
|
|
|
|
|
|
|
ABORT(R_IO_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
sock = new NrSocket();
|
|
|
|
|
2015-06-19 09:02:38 +03:00
|
|
|
sock->fd_=prfd;
|
2014-04-23 12:15:25 +04:00
|
|
|
nr_transport_addr_copy(&sock->my_addr_, &my_addr_);
|
|
|
|
|
2015-06-19 09:02:38 +03:00
|
|
|
if((r=nr_praddr_to_transport_addr(&nfrom, addrp, my_addr_.protocol, 0)))
|
|
|
|
ABORT(r);
|
|
|
|
|
2014-04-23 12:15:25 +04:00
|
|
|
// Set nonblocking
|
2015-06-19 09:02:38 +03:00
|
|
|
opt_nonblock.option = PR_SockOpt_Nonblocking;
|
|
|
|
opt_nonblock.value.non_blocking = PR_TRUE;
|
|
|
|
status = PR_SetSocketOption(prfd, &opt_nonblock);
|
2014-04-23 12:15:25 +04:00
|
|
|
if (status != PR_SUCCESS) {
|
2015-06-19 09:02:38 +03:00
|
|
|
r_log(LOG_GENERIC, LOG_CRIT,
|
|
|
|
"Failed to make accepted socket nonblocking: %d", status);
|
2014-04-23 12:15:25 +04:00
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
}
|
2015-06-19 09:02:38 +03:00
|
|
|
// Disable TCP Nagle
|
|
|
|
opt_nodelay.option = PR_SockOpt_NoDelay;
|
|
|
|
opt_nodelay.value.no_delay = PR_TRUE;
|
|
|
|
status = PR_SetSocketOption(prfd, &opt_nodelay);
|
|
|
|
if (status != PR_SUCCESS) {
|
|
|
|
r_log(LOG_GENERIC, LOG_WARNING,
|
|
|
|
"Failed to set Nodelay on accepted socket: %d", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should fail only with OOM
|
|
|
|
if ((r=nr_socket_create_int(static_cast<void *>(sock), sock->vtbl(), sockp)))
|
|
|
|
ABORT(r);
|
2014-04-23 12:15:25 +04:00
|
|
|
|
|
|
|
// Remember our thread.
|
|
|
|
sock->ststhread_ = do_QueryInterface(stservice, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
|
|
|
|
// Finally, register with the STS
|
2015-06-19 09:02:38 +03:00
|
|
|
rv = stservice->AttachSocket(prfd, sock);
|
2014-04-23 12:15:25 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
sock->connect_invoked_ = true;
|
|
|
|
|
|
|
|
// Add a reference so that we can delete it in destroy()
|
|
|
|
sock->AddRef();
|
2016-01-22 10:47:01 +03:00
|
|
|
_status = 0;
|
2014-04-23 12:15:25 +04:00
|
|
|
abort:
|
|
|
|
if (_status) {
|
|
|
|
delete sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
return(_status);
|
|
|
|
}
|
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
NS_IMPL_ISUPPORTS(NrUdpSocketIpcProxy, nsIUDPSocketInternal)
|
2014-12-24 03:22:02 +03:00
|
|
|
|
|
|
|
nsresult
|
2015-10-18 08:24:48 +03:00
|
|
|
NrUdpSocketIpcProxy::Init(const RefPtr<NrUdpSocketIpc>& socket)
|
2014-12-24 03:22:02 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
sts_thread_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MOZ_ASSERT(false, "Failed to get STS thread");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
socket_ = socket;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
NrUdpSocketIpcProxy::~NrUdpSocketIpcProxy()
|
2014-12-24 03:22:02 +03:00
|
|
|
{
|
|
|
|
// Send our ref to STS to be released
|
|
|
|
RUN_ON_THREAD(sts_thread_,
|
|
|
|
mozilla::WrapRelease(socket_.forget()),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// IUDPSocketInternal interfaces
|
|
|
|
// callback while error happened in UDP socket operation
|
2015-01-05 10:49:50 +03:00
|
|
|
NS_IMETHODIMP NrUdpSocketIpcProxy::CallListenerError(const nsACString &message,
|
|
|
|
const nsACString &filename,
|
|
|
|
uint32_t line_number) {
|
2014-12-24 03:22:02 +03:00
|
|
|
return socket_->CallListenerError(message, filename, line_number);
|
|
|
|
}
|
|
|
|
|
|
|
|
// callback while receiving UDP packet
|
2015-01-05 10:49:50 +03:00
|
|
|
NS_IMETHODIMP NrUdpSocketIpcProxy::CallListenerReceivedData(const nsACString &host,
|
|
|
|
uint16_t port,
|
|
|
|
const uint8_t *data,
|
|
|
|
uint32_t data_length) {
|
2014-12-24 03:22:02 +03:00
|
|
|
return socket_->CallListenerReceivedData(host, port, data, data_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
// callback while UDP socket is opened
|
2015-01-05 10:49:50 +03:00
|
|
|
NS_IMETHODIMP NrUdpSocketIpcProxy::CallListenerOpened() {
|
2014-12-24 03:22:02 +03:00
|
|
|
return socket_->CallListenerOpened();
|
|
|
|
}
|
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
// callback while UDP socket is connected
|
|
|
|
NS_IMETHODIMP NrUdpSocketIpcProxy::CallListenerConnected() {
|
|
|
|
return socket_->CallListenerConnected();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-12-24 03:22:02 +03:00
|
|
|
// callback while UDP socket is closed
|
2015-01-05 10:49:50 +03:00
|
|
|
NS_IMETHODIMP NrUdpSocketIpcProxy::CallListenerClosed() {
|
2014-12-24 03:22:02 +03:00
|
|
|
return socket_->CallListenerClosed();
|
|
|
|
}
|
2013-06-19 15:08:06 +04:00
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
// NrUdpSocketIpc Implementation
|
|
|
|
NrUdpSocketIpc::NrUdpSocketIpc()
|
|
|
|
: NrSocketIpc(GetIOThreadAndAddUse_s()),
|
|
|
|
monitor_("NrUdpSocketIpc"),
|
|
|
|
err_(false),
|
|
|
|
state_(NR_INIT) {
|
2013-06-19 15:08:06 +04:00
|
|
|
}
|
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
NrUdpSocketIpc::~NrUdpSocketIpc()
|
2015-05-29 17:13:33 +03:00
|
|
|
{
|
|
|
|
// also guarantees socket_child_ is released from the io_thread, and
|
|
|
|
// tells the SingletonThreadHolder we're done with it
|
|
|
|
|
|
|
|
#if defined(MOZILLA_INTERNAL_API) && !defined(MOZILLA_XPCOMRT_API)
|
|
|
|
// close(), but transfer the socket_child_ reference to die as well
|
|
|
|
RUN_ON_THREAD(io_thread_,
|
2015-01-05 10:49:50 +03:00
|
|
|
mozilla::WrapRunnableNM(&NrUdpSocketIpc::release_child_i,
|
2015-05-29 17:13:33 +03:00
|
|
|
socket_child_.forget().take(),
|
|
|
|
sts_thread_),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-06-19 15:08:06 +04:00
|
|
|
// IUDPSocketInternal interfaces
|
|
|
|
// callback while error happened in UDP socket operation
|
2015-01-05 10:49:50 +03:00
|
|
|
NS_IMETHODIMP NrUdpSocketIpc::CallListenerError(const nsACString &message,
|
|
|
|
const nsACString &filename,
|
|
|
|
uint32_t line_number) {
|
2015-05-29 17:13:33 +03:00
|
|
|
ASSERT_ON_THREAD(io_thread_);
|
2013-06-19 15:08:06 +04:00
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
r_log(LOG_GENERIC, LOG_ERR, "UDP socket error:%s at %s:%d this=%p",
|
|
|
|
message.BeginReading(), filename.BeginReading(), line_number, (void*) this );
|
2013-06-19 15:08:06 +04:00
|
|
|
|
|
|
|
ReentrantMonitorAutoEnter mon(monitor_);
|
|
|
|
err_ = true;
|
|
|
|
monitor_.NotifyAll();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// callback while receiving UDP packet
|
2015-01-05 10:49:50 +03:00
|
|
|
NS_IMETHODIMP NrUdpSocketIpc::CallListenerReceivedData(const nsACString &host,
|
|
|
|
uint16_t port,
|
|
|
|
const uint8_t *data,
|
|
|
|
uint32_t data_length) {
|
2015-05-29 17:13:33 +03:00
|
|
|
ASSERT_ON_THREAD(io_thread_);
|
2013-06-19 15:08:06 +04:00
|
|
|
|
|
|
|
PRNetAddr addr;
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(monitor_);
|
|
|
|
|
|
|
|
if (PR_SUCCESS != PR_StringToNetAddr(host.BeginReading(), &addr)) {
|
|
|
|
err_ = true;
|
|
|
|
MOZ_ASSERT(false, "Failed to convert remote host to PRNetAddr");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use PR_IpAddrNull to avoid address being reset to 0.
|
|
|
|
if (PR_SUCCESS != PR_SetNetAddr(PR_IpAddrNull, addr.raw.family, port, &addr)) {
|
|
|
|
err_ = true;
|
|
|
|
MOZ_ASSERT(false, "Failed to set port in PRNetAddr");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoPtr<DataBuffer> buf(new DataBuffer(data, data_length));
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nr_udp_message> msg(new nr_udp_message(addr, buf));
|
2013-06-19 15:08:06 +04:00
|
|
|
|
|
|
|
RUN_ON_THREAD(sts_thread_,
|
2015-10-18 08:24:48 +03:00
|
|
|
mozilla::WrapRunnable(RefPtr<NrUdpSocketIpc>(this),
|
2015-01-05 10:49:50 +03:00
|
|
|
&NrUdpSocketIpc::recv_callback_s,
|
2013-06-19 15:08:06 +04:00
|
|
|
msg),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-01-22 12:32:50 +03:00
|
|
|
nsresult NrUdpSocketIpc::SetAddress() {
|
2014-05-06 14:32:25 +04:00
|
|
|
uint16_t port;
|
|
|
|
if (NS_FAILED(socket_child_->GetLocalPort(&port))) {
|
|
|
|
err_ = true;
|
|
|
|
MOZ_ASSERT(false, "Failed to get local port");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-06-19 15:08:06 +04:00
|
|
|
|
2014-05-06 14:32:25 +04:00
|
|
|
nsAutoCString address;
|
|
|
|
if(NS_FAILED(socket_child_->GetLocalAddress(address))) {
|
|
|
|
err_ = true;
|
|
|
|
MOZ_ASSERT(false, "Failed to get local address");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-06-19 15:08:06 +04:00
|
|
|
|
2014-05-06 14:32:25 +04:00
|
|
|
PRNetAddr praddr;
|
|
|
|
if (PR_SUCCESS != PR_InitializeNetAddr(PR_IpAddrAny, port, &praddr)) {
|
|
|
|
err_ = true;
|
|
|
|
MOZ_ASSERT(false, "Failed to set port in PRNetAddr");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-06-19 15:08:06 +04:00
|
|
|
|
2014-05-06 14:32:25 +04:00
|
|
|
if (PR_SUCCESS != PR_StringToNetAddr(address.BeginReading(), &praddr)) {
|
|
|
|
err_ = true;
|
|
|
|
MOZ_ASSERT(false, "Failed to convert local host to PRNetAddr");
|
|
|
|
return NS_OK;
|
2013-06-19 15:08:06 +04:00
|
|
|
}
|
|
|
|
|
2014-05-06 14:32:25 +04:00
|
|
|
nr_transport_addr expected_addr;
|
|
|
|
if(nr_transport_addr_copy(&expected_addr, &my_addr_)) {
|
|
|
|
err_ = true;
|
|
|
|
MOZ_ASSERT(false, "Failed to copy my_addr_");
|
|
|
|
}
|
2013-06-19 15:08:06 +04:00
|
|
|
|
2014-05-06 14:32:25 +04:00
|
|
|
if (nr_praddr_to_transport_addr(&praddr, &my_addr_, IPPROTO_UDP, 1)) {
|
|
|
|
err_ = true;
|
|
|
|
MOZ_ASSERT(false, "Failed to copy local host to my_addr_");
|
|
|
|
}
|
2013-06-19 15:08:06 +04:00
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
if (!nr_transport_addr_is_wildcard(&expected_addr) &&
|
|
|
|
nr_transport_addr_cmp(&expected_addr, &my_addr_,
|
2014-05-06 14:32:25 +04:00
|
|
|
NR_TRANSPORT_ADDR_CMP_MODE_ADDR)) {
|
2013-06-19 15:08:06 +04:00
|
|
|
err_ = true;
|
2014-05-06 14:32:25 +04:00
|
|
|
MOZ_ASSERT(false, "Address of opened socket is not expected");
|
2013-06-19 15:08:06 +04:00
|
|
|
}
|
2014-05-06 14:32:25 +04:00
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// callback while UDP socket is opened
|
|
|
|
NS_IMETHODIMP NrUdpSocketIpc::CallListenerOpened() {
|
|
|
|
ASSERT_ON_THREAD(io_thread_);
|
|
|
|
ReentrantMonitorAutoEnter mon(monitor_);
|
|
|
|
|
|
|
|
r_log(LOG_GENERIC, LOG_DEBUG, "UDP socket opened this=%p", (void*) this);
|
|
|
|
nsresult rv = SetAddress();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mon.NotifyAll();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// callback while UDP socket is connected
|
|
|
|
NS_IMETHODIMP NrUdpSocketIpc::CallListenerConnected() {
|
|
|
|
ASSERT_ON_THREAD(io_thread_);
|
|
|
|
|
|
|
|
ReentrantMonitorAutoEnter mon(monitor_);
|
|
|
|
|
|
|
|
r_log(LOG_GENERIC, LOG_DEBUG, "UDP socket connected this=%p", (void*) this);
|
|
|
|
MOZ_ASSERT(state_ == NR_CONNECTED);
|
|
|
|
|
|
|
|
nsresult rv = SetAddress();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mon.NotifyAll();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
r_log(LOG_GENERIC, LOG_INFO, "Exit UDP socket connected");
|
2014-05-06 14:32:25 +04:00
|
|
|
mon.NotifyAll();
|
|
|
|
|
2013-06-19 15:08:06 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-06 14:32:25 +04:00
|
|
|
// callback while UDP socket is closed
|
2015-01-05 10:49:50 +03:00
|
|
|
NS_IMETHODIMP NrUdpSocketIpc::CallListenerClosed() {
|
2015-05-29 17:13:33 +03:00
|
|
|
ASSERT_ON_THREAD(io_thread_);
|
2013-06-19 15:08:06 +04:00
|
|
|
|
|
|
|
ReentrantMonitorAutoEnter mon(monitor_);
|
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
r_log(LOG_GENERIC, LOG_DEBUG, "UDP socket closed this=%p", (void*) this);
|
2014-05-06 14:32:25 +04:00
|
|
|
MOZ_ASSERT(state_ == NR_CONNECTED || state_ == NR_CLOSING);
|
|
|
|
state_ = NR_CLOSED;
|
2013-06-19 15:08:06 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
//
|
|
|
|
// NrSocketBase methods.
|
|
|
|
//
|
|
|
|
int NrUdpSocketIpc::create(nr_transport_addr *addr) {
|
2013-06-19 15:08:06 +04:00
|
|
|
ASSERT_ON_THREAD(sts_thread_);
|
|
|
|
|
|
|
|
int r, _status;
|
|
|
|
nsresult rv;
|
|
|
|
int32_t port;
|
|
|
|
nsCString host;
|
|
|
|
|
|
|
|
ReentrantMonitorAutoEnter mon(monitor_);
|
|
|
|
|
|
|
|
if (state_ != NR_INIT) {
|
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
sts_thread_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MOZ_ASSERT(false, "Failed to get STS thread");
|
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((r=nr_transport_addr_get_addrstring_and_port(addr, &host, &port))) {
|
|
|
|
ABORT(r);
|
|
|
|
}
|
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
// wildcard address will be resolved at NrUdpSocketIpc::CallListenerVoid
|
2013-06-19 15:08:06 +04:00
|
|
|
if ((r=nr_transport_addr_copy(&my_addr_, addr))) {
|
|
|
|
ABORT(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
state_ = NR_CONNECTING;
|
|
|
|
|
2015-05-29 17:13:33 +03:00
|
|
|
RUN_ON_THREAD(io_thread_,
|
2015-10-18 08:24:48 +03:00
|
|
|
mozilla::WrapRunnable(RefPtr<NrUdpSocketIpc>(this),
|
2015-01-05 10:49:50 +03:00
|
|
|
&NrUdpSocketIpc::create_i,
|
2013-06-19 15:08:06 +04:00
|
|
|
host, static_cast<uint16_t>(port)),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
// Wait until socket creation complete.
|
|
|
|
mon.Wait();
|
|
|
|
|
|
|
|
if (err_) {
|
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
state_ = NR_CONNECTED;
|
|
|
|
|
|
|
|
_status = 0;
|
|
|
|
abort:
|
|
|
|
return(_status);
|
|
|
|
}
|
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
int NrUdpSocketIpc::sendto(const void *msg, size_t len, int flags,
|
2013-06-19 15:08:06 +04:00
|
|
|
nr_transport_addr *to) {
|
|
|
|
ASSERT_ON_THREAD(sts_thread_);
|
|
|
|
|
|
|
|
ReentrantMonitorAutoEnter mon(monitor_);
|
|
|
|
|
|
|
|
//If send err happened before, simply return the error.
|
|
|
|
if (err_) {
|
|
|
|
return R_IO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state_ != NR_CONNECTED) {
|
|
|
|
return R_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int r;
|
|
|
|
net::NetAddr addr;
|
|
|
|
if ((r=nr_transport_addr_to_netaddr(to, &addr))) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoPtr<DataBuffer> buf(new DataBuffer(static_cast<const uint8_t*>(msg), len));
|
|
|
|
|
2015-05-29 17:13:33 +03:00
|
|
|
RUN_ON_THREAD(io_thread_,
|
2015-10-18 08:24:48 +03:00
|
|
|
mozilla::WrapRunnable(RefPtr<NrUdpSocketIpc>(this),
|
2015-01-05 10:49:50 +03:00
|
|
|
&NrUdpSocketIpc::sendto_i,
|
2013-06-19 15:08:06 +04:00
|
|
|
addr, buf),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
void NrUdpSocketIpc::close() {
|
2016-01-22 10:47:01 +03:00
|
|
|
r_log(LOG_GENERIC, LOG_DEBUG, "NrUdpSocketIpc::close()");
|
|
|
|
|
2013-06-19 15:08:06 +04:00
|
|
|
ASSERT_ON_THREAD(sts_thread_);
|
|
|
|
|
|
|
|
ReentrantMonitorAutoEnter mon(monitor_);
|
|
|
|
state_ = NR_CLOSING;
|
|
|
|
|
2015-05-29 17:13:33 +03:00
|
|
|
RUN_ON_THREAD(io_thread_,
|
2015-10-18 08:24:48 +03:00
|
|
|
mozilla::WrapRunnable(RefPtr<NrUdpSocketIpc>(this),
|
2015-01-05 10:49:50 +03:00
|
|
|
&NrUdpSocketIpc::close_i),
|
2013-06-19 15:08:06 +04:00
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
//remove all enqueued messages
|
2015-10-18 08:24:48 +03:00
|
|
|
std::queue<RefPtr<nr_udp_message> > empty;
|
2013-06-19 15:08:06 +04:00
|
|
|
std::swap(received_msgs_, empty);
|
|
|
|
}
|
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
int NrUdpSocketIpc::recvfrom(void *buf, size_t maxlen, size_t *len, int flags,
|
2013-06-19 15:08:06 +04:00
|
|
|
nr_transport_addr *from) {
|
|
|
|
ASSERT_ON_THREAD(sts_thread_);
|
|
|
|
|
|
|
|
ReentrantMonitorAutoEnter mon(monitor_);
|
|
|
|
|
|
|
|
int r, _status;
|
|
|
|
uint32_t consumed_len;
|
|
|
|
|
|
|
|
*len = 0;
|
|
|
|
|
|
|
|
if (state_ != NR_CONNECTED) {
|
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (received_msgs_.empty()) {
|
|
|
|
ABORT(R_WOULDBLOCK);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nr_udp_message> msg(received_msgs_.front());
|
2013-06-19 15:08:06 +04:00
|
|
|
|
|
|
|
received_msgs_.pop();
|
|
|
|
|
2013-12-06 22:20:19 +04:00
|
|
|
if ((r=nr_praddr_to_transport_addr(&msg->from, from, IPPROTO_UDP, 0))) {
|
2013-06-19 15:08:06 +04:00
|
|
|
err_ = true;
|
|
|
|
MOZ_ASSERT(false, "Get bogus address for received UDP packet");
|
|
|
|
ABORT(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
consumed_len = std::min(maxlen, msg->data->len());
|
|
|
|
if (consumed_len < msg->data->len()) {
|
|
|
|
r_log(LOG_GENERIC, LOG_DEBUG, "Partial received UDP packet will be discard");
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buf, msg->data->data(), consumed_len);
|
|
|
|
*len = consumed_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
_status = 0;
|
|
|
|
abort:
|
|
|
|
return(_status);
|
|
|
|
}
|
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
int NrUdpSocketIpc::getaddr(nr_transport_addr *addrp) {
|
2013-06-19 15:08:06 +04:00
|
|
|
ASSERT_ON_THREAD(sts_thread_);
|
|
|
|
|
|
|
|
ReentrantMonitorAutoEnter mon(monitor_);
|
|
|
|
|
|
|
|
if (state_ != NR_CONNECTED) {
|
|
|
|
return R_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nr_transport_addr_copy(addrp, &my_addr_);
|
|
|
|
}
|
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
int NrUdpSocketIpc::connect(nr_transport_addr *addr) {
|
2016-01-22 10:47:01 +03:00
|
|
|
int r,_status;
|
|
|
|
int32_t port;
|
|
|
|
nsCString host;
|
|
|
|
|
|
|
|
ReentrantMonitorAutoEnter mon(monitor_);
|
|
|
|
r_log(LOG_GENERIC, LOG_DEBUG, "NrUdpSocketIpc::connect(%s) this=%p", addr->as_string,
|
|
|
|
(void*) this);
|
|
|
|
|
|
|
|
if ((r=nr_transport_addr_get_addrstring_and_port(addr, &host, &port))) {
|
|
|
|
ABORT(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
RUN_ON_THREAD(io_thread_,
|
|
|
|
mozilla::WrapRunnable(RefPtr<NrUdpSocketIpc>(this),
|
|
|
|
&NrUdpSocketIpc::connect_i,
|
|
|
|
host, static_cast<uint16_t>(port)),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
// Wait until connect() completes.
|
|
|
|
mon.Wait();
|
|
|
|
|
|
|
|
r_log(LOG_GENERIC, LOG_DEBUG, "NrUdpSocketIpc::connect this=%p completed err_ = %s",
|
|
|
|
(void*) this, err_ ? "true" : "false");
|
|
|
|
|
|
|
|
if (err_) {
|
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
_status = 0;
|
|
|
|
abort:
|
|
|
|
return _status;
|
2013-12-06 22:20:19 +04:00
|
|
|
}
|
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
int NrUdpSocketIpc::write(const void *msg, size_t len, size_t *written) {
|
2013-12-06 22:20:19 +04:00
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return R_INTERNAL;
|
|
|
|
}
|
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
int NrUdpSocketIpc::read(void* buf, size_t maxlen, size_t *len) {
|
2013-12-06 22:20:19 +04:00
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return R_INTERNAL;
|
|
|
|
}
|
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
int NrUdpSocketIpc::listen(int backlog) {
|
2014-04-23 12:15:25 +04:00
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return R_INTERNAL;
|
|
|
|
}
|
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
int NrUdpSocketIpc::accept(nr_transport_addr *addrp, nr_socket **sockp) {
|
2014-04-23 12:15:25 +04:00
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return R_INTERNAL;
|
|
|
|
}
|
|
|
|
|
2015-05-29 17:13:33 +03:00
|
|
|
// IO thread executors
|
2015-01-05 10:49:50 +03:00
|
|
|
void NrUdpSocketIpc::create_i(const nsACString &host, const uint16_t port) {
|
2015-05-29 17:13:33 +03:00
|
|
|
ASSERT_ON_THREAD(io_thread_);
|
2013-06-19 15:08:06 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
2014-08-19 03:14:00 +04:00
|
|
|
nsCOMPtr<nsIUDPSocketChild> socketChild = do_CreateInstance("@mozilla.org/udp-socket-child;1", &rv);
|
2013-06-19 15:08:06 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2015-01-05 10:49:50 +03:00
|
|
|
ReentrantMonitorAutoEnter mon(monitor_);
|
2013-06-19 15:08:06 +04:00
|
|
|
err_ = true;
|
|
|
|
MOZ_ASSERT(false, "Failed to create UDPSocketChild");
|
2014-05-06 14:32:25 +04:00
|
|
|
return;
|
2013-06-19 15:08:06 +04:00
|
|
|
}
|
|
|
|
|
2015-05-29 17:14:14 +03:00
|
|
|
// This can spin the event loop; don't do that with the monitor held
|
|
|
|
socketChild->SetBackgroundSpinsEvents();
|
|
|
|
|
|
|
|
ReentrantMonitorAutoEnter mon(monitor_);
|
|
|
|
if (!socket_child_) {
|
|
|
|
socket_child_ = socketChild;
|
|
|
|
socket_child_->SetFilterName(nsCString("stun"));
|
|
|
|
} else {
|
|
|
|
socketChild = nullptr;
|
|
|
|
}
|
2013-11-29 20:15:26 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<NrUdpSocketIpcProxy> proxy(new NrUdpSocketIpcProxy);
|
2014-12-24 03:22:02 +03:00
|
|
|
rv = proxy->Init(this);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
err_ = true;
|
|
|
|
mon.NotifyAll();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-29 17:14:14 +03:00
|
|
|
// XXX bug 1126232 - don't use null Principal!
|
2015-04-08 22:35:00 +03:00
|
|
|
if (NS_FAILED(socket_child_->Bind(proxy, nullptr, host, port,
|
2014-05-06 14:32:25 +04:00
|
|
|
/* reuse = */ false,
|
|
|
|
/* loopback = */ false))) {
|
2013-06-19 15:08:06 +04:00
|
|
|
err_ = true;
|
|
|
|
MOZ_ASSERT(false, "Failed to create UDP socket");
|
2014-05-06 14:32:25 +04:00
|
|
|
mon.NotifyAll();
|
|
|
|
return;
|
2013-06-19 15:08:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
void NrUdpSocketIpc::connect_i(const nsACString &host, const uint16_t port) {
|
|
|
|
ASSERT_ON_THREAD(io_thread_);
|
|
|
|
nsresult rv;
|
|
|
|
ReentrantMonitorAutoEnter mon(monitor_);
|
|
|
|
|
|
|
|
RefPtr<NrUdpSocketIpcProxy> proxy(new NrUdpSocketIpcProxy);
|
|
|
|
rv = proxy->Init(this);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
err_ = true;
|
|
|
|
mon.NotifyAll();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(socket_child_->Connect(proxy, host, port))) {
|
|
|
|
err_ = true;
|
|
|
|
MOZ_ASSERT(false, "Failed to connect UDP socket");
|
|
|
|
mon.NotifyAll();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
void NrUdpSocketIpc::sendto_i(const net::NetAddr &addr, nsAutoPtr<DataBuffer> buf) {
|
2015-05-29 17:13:33 +03:00
|
|
|
ASSERT_ON_THREAD(io_thread_);
|
2013-06-19 15:08:06 +04:00
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
ReentrantMonitorAutoEnter mon(monitor_);
|
|
|
|
|
2015-05-29 17:13:33 +03:00
|
|
|
if (!socket_child_) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
err_ = true;
|
|
|
|
return;
|
|
|
|
}
|
2013-06-19 15:08:06 +04:00
|
|
|
if (NS_FAILED(socket_child_->SendWithAddress(&addr,
|
|
|
|
buf->data(),
|
|
|
|
buf->len()))) {
|
|
|
|
err_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
void NrUdpSocketIpc::close_i() {
|
2015-05-29 17:13:33 +03:00
|
|
|
ASSERT_ON_THREAD(io_thread_);
|
2013-06-19 15:08:06 +04:00
|
|
|
|
|
|
|
if (socket_child_) {
|
|
|
|
socket_child_->Close();
|
|
|
|
socket_child_ = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-29 17:13:33 +03:00
|
|
|
#if defined(MOZILLA_INTERNAL_API) && !defined(MOZILLA_XPCOMRT_API)
|
|
|
|
// close(), but transfer the socket_child_ reference to die as well
|
|
|
|
// static
|
2015-01-05 10:49:50 +03:00
|
|
|
void NrUdpSocketIpc::release_child_i(nsIUDPSocketChild* aChild,
|
|
|
|
nsCOMPtr<nsIEventTarget> sts_thread) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIUDPSocketChild> socket_child_ref =
|
2015-05-29 17:13:33 +03:00
|
|
|
already_AddRefed<nsIUDPSocketChild>(aChild);
|
|
|
|
if (socket_child_ref) {
|
|
|
|
socket_child_ref->Close();
|
|
|
|
}
|
|
|
|
// Tell SingletonThreadHolder we're done with it
|
|
|
|
RUN_ON_THREAD(sts_thread,
|
2015-01-05 10:49:50 +03:00
|
|
|
mozilla::WrapRunnableNM(&NrUdpSocketIpc::release_use_s),
|
2015-05-29 17:13:33 +03:00
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
void NrUdpSocketIpc::release_use_s() {
|
2015-05-29 17:13:33 +03:00
|
|
|
sThread->ReleaseUse();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
void NrUdpSocketIpc::recv_callback_s(RefPtr<nr_udp_message> msg) {
|
2013-06-19 15:08:06 +04:00
|
|
|
ASSERT_ON_THREAD(sts_thread_);
|
|
|
|
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(monitor_);
|
|
|
|
if (state_ != NR_CONNECTED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//enqueue received message
|
|
|
|
received_msgs_.push(msg);
|
|
|
|
|
|
|
|
if ((poll_flags() & PR_POLL_READ)) {
|
|
|
|
fire_callback(NR_ASYNC_WAIT_READ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
#if defined(MOZILLA_INTERNAL_API) && !defined(MOZILLA_XPCOMRT_API)
|
|
|
|
// TCPSocket.
|
|
|
|
class NrTcpSocketIpc::TcpSocketReadyRunner: public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit TcpSocketReadyRunner(NrTcpSocketIpc *sck)
|
|
|
|
: socket_(sck) {}
|
|
|
|
|
|
|
|
NS_IMETHODIMP Run() {
|
|
|
|
socket_->maybe_post_socket_ready();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<NrTcpSocketIpc> socket_;
|
2015-01-05 10:49:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(NrTcpSocketIpc,
|
|
|
|
nsITCPSocketCallback)
|
|
|
|
|
|
|
|
NrTcpSocketIpc::NrTcpSocketIpc(nsIThread* aThread)
|
|
|
|
: NrSocketIpc(static_cast<nsIEventTarget*>(aThread)),
|
|
|
|
mirror_state_(NR_INIT),
|
|
|
|
state_(NR_INIT),
|
|
|
|
buffered_bytes_(0),
|
|
|
|
tracking_number_(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
NrTcpSocketIpc::~NrTcpSocketIpc()
|
|
|
|
{
|
|
|
|
// also guarantees socket_child_ is released from the io_thread
|
|
|
|
|
|
|
|
// close(), but transfer the socket_child_ reference to die as well
|
|
|
|
RUN_ON_THREAD(io_thread_,
|
|
|
|
mozilla::WrapRunnableNM(&NrTcpSocketIpc::release_child_i,
|
|
|
|
socket_child_.forget().take(),
|
|
|
|
sts_thread_),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsITCPSocketCallback methods
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP NrTcpSocketIpc::UpdateReadyState(uint32_t aReadyState) {
|
|
|
|
NrSocketIpcState temp = NR_INIT;
|
|
|
|
switch (static_cast<dom::TCPReadyState>(aReadyState)) {
|
|
|
|
case dom::TCPReadyState::Connecting:
|
|
|
|
temp = NR_CONNECTING;
|
|
|
|
break;
|
|
|
|
case dom::TCPReadyState::Open:
|
|
|
|
temp = NR_CONNECTED;
|
|
|
|
break;
|
|
|
|
case dom::TCPReadyState::Closing:
|
|
|
|
temp = NR_CLOSING;
|
|
|
|
break;
|
|
|
|
case dom::TCPReadyState::Closed:
|
|
|
|
temp = NR_CLOSED;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Invalid ReadyState");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (mirror_state_ != temp) {
|
|
|
|
mirror_state_ = temp;
|
|
|
|
RUN_ON_THREAD(sts_thread_,
|
2015-10-18 08:24:48 +03:00
|
|
|
mozilla::WrapRunnable(RefPtr<NrTcpSocketIpc>(this),
|
2015-01-05 10:49:50 +03:00
|
|
|
&NrTcpSocketIpc::update_state_s,
|
|
|
|
temp),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP NrTcpSocketIpc::UpdateBufferedAmount(uint32_t buffered_amount,
|
|
|
|
uint32_t tracking_number) {
|
|
|
|
RUN_ON_THREAD(sts_thread_,
|
2015-10-18 08:24:48 +03:00
|
|
|
mozilla::WrapRunnable(RefPtr<NrTcpSocketIpc>(this),
|
2015-01-05 10:49:50 +03:00
|
|
|
&NrTcpSocketIpc::message_sent_s,
|
|
|
|
buffered_amount,
|
|
|
|
tracking_number),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP NrTcpSocketIpc::FireDataArrayEvent(const nsAString& aType,
|
|
|
|
const InfallibleTArray<uint8_t>& buffer) {
|
|
|
|
// Called when we received data.
|
|
|
|
uint8_t *buf = const_cast<uint8_t*>(buffer.Elements());
|
|
|
|
|
|
|
|
nsAutoPtr<DataBuffer> data_buf(new DataBuffer(buf, buffer.Length()));
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nr_tcp_message> msg = new nr_tcp_message(data_buf);
|
2015-01-05 10:49:50 +03:00
|
|
|
|
|
|
|
RUN_ON_THREAD(sts_thread_,
|
2015-10-18 08:24:48 +03:00
|
|
|
mozilla::WrapRunnable(RefPtr<NrTcpSocketIpc>(this),
|
2015-01-05 10:49:50 +03:00
|
|
|
&NrTcpSocketIpc::recv_message_s,
|
|
|
|
msg),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP NrTcpSocketIpc::FireErrorEvent(const nsAString &type,
|
|
|
|
const nsAString &name) {
|
|
|
|
r_log(LOG_GENERIC, LOG_ERR,
|
|
|
|
"Error from TCPSocketChild: type: %s, name: %s",
|
|
|
|
NS_LossyConvertUTF16toASCII(type).get(), NS_LossyConvertUTF16toASCII(name).get());
|
|
|
|
socket_child_ = nullptr;
|
|
|
|
|
|
|
|
mirror_state_ = NR_CLOSED;
|
|
|
|
RUN_ON_THREAD(sts_thread_,
|
2015-10-18 08:24:48 +03:00
|
|
|
mozilla::WrapRunnable(RefPtr<NrTcpSocketIpc>(this),
|
2015-01-05 10:49:50 +03:00
|
|
|
&NrTcpSocketIpc::update_state_s,
|
|
|
|
NR_CLOSED),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// methods of nsITCPSocketCallback that we are not going to implement.
|
|
|
|
|
|
|
|
NS_IMETHODIMP NrTcpSocketIpc::FireDataEvent(JSContext* aCx,
|
|
|
|
const nsAString &type,
|
|
|
|
const JS::HandleValue data) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP NrTcpSocketIpc::FireDataStringEvent(const nsAString &type,
|
|
|
|
const nsACString &data) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP NrTcpSocketIpc::FireEvent(const nsAString &type) {
|
|
|
|
// XXX support type.mData == 'close' at least
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// NrSocketBase methods.
|
|
|
|
//
|
|
|
|
int NrTcpSocketIpc::create(nr_transport_addr *addr) {
|
|
|
|
int r, _status;
|
|
|
|
nsresult rv;
|
|
|
|
int32_t port;
|
|
|
|
nsCString host;
|
|
|
|
|
|
|
|
if (state_ != NR_INIT) {
|
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
sts_thread_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MOZ_ASSERT(false, "Failed to get STS thread");
|
|
|
|
ABORT(R_INTERNAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check
|
|
|
|
if ((r=nr_transport_addr_get_addrstring_and_port(addr, &host, &port))) {
|
|
|
|
ABORT(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((r=nr_transport_addr_copy(&my_addr_, addr))) {
|
|
|
|
ABORT(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
_status = 0;
|
|
|
|
abort:
|
|
|
|
return(_status);
|
|
|
|
}
|
|
|
|
|
|
|
|
int NrTcpSocketIpc::sendto(const void *msg, size_t len,
|
|
|
|
int flags, nr_transport_addr *to) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return R_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NrTcpSocketIpc::recvfrom(void * buf, size_t maxlen,
|
|
|
|
size_t *len, int flags,
|
|
|
|
nr_transport_addr *from) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return R_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NrTcpSocketIpc::getaddr(nr_transport_addr *addrp) {
|
|
|
|
ASSERT_ON_THREAD(sts_thread_);
|
|
|
|
return nr_transport_addr_copy(addrp, &my_addr_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NrTcpSocketIpc::close() {
|
|
|
|
ASSERT_ON_THREAD(sts_thread_);
|
|
|
|
|
|
|
|
if (state_ == NR_CLOSED || state_ == NR_CLOSING) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
state_ = NR_CLOSING;
|
|
|
|
|
|
|
|
RUN_ON_THREAD(io_thread_,
|
2015-10-18 08:24:48 +03:00
|
|
|
mozilla::WrapRunnable(RefPtr<NrTcpSocketIpc>(this),
|
2015-01-05 10:49:50 +03:00
|
|
|
&NrTcpSocketIpc::close_i),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
//remove all enqueued messages
|
2015-10-18 08:24:48 +03:00
|
|
|
std::queue<RefPtr<nr_tcp_message>> empty;
|
2015-01-05 10:49:50 +03:00
|
|
|
std::swap(msg_queue_, empty);
|
|
|
|
}
|
|
|
|
|
|
|
|
int NrTcpSocketIpc::connect(nr_transport_addr *addr) {
|
|
|
|
nsCString remote_addr, local_addr;
|
|
|
|
int32_t remote_port, local_port;
|
|
|
|
int r, _status;
|
|
|
|
if ((r=nr_transport_addr_get_addrstring_and_port(addr,
|
|
|
|
&remote_addr,
|
|
|
|
&remote_port))) {
|
|
|
|
ABORT(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((r=nr_transport_addr_get_addrstring_and_port(&my_addr_,
|
|
|
|
&local_addr,
|
|
|
|
&local_port))) {
|
|
|
|
MOZ_ASSERT(false); // shouldn't fail as it was sanity-checked in ::create()
|
|
|
|
ABORT(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
state_ = mirror_state_ = NR_CONNECTING;
|
|
|
|
RUN_ON_THREAD(io_thread_,
|
2015-10-18 08:24:48 +03:00
|
|
|
mozilla::WrapRunnable(RefPtr<NrTcpSocketIpc>(this),
|
2015-01-05 10:49:50 +03:00
|
|
|
&NrTcpSocketIpc::connect_i,
|
|
|
|
remote_addr,
|
|
|
|
static_cast<uint16_t>(remote_port),
|
|
|
|
local_addr,
|
|
|
|
static_cast<uint16_t>(local_port)),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
// Make caller wait for ready to write.
|
|
|
|
_status = R_WOULDBLOCK;
|
|
|
|
abort:
|
|
|
|
return _status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NrTcpSocketIpc::write(const void *msg, size_t len, size_t *written) {
|
|
|
|
ASSERT_ON_THREAD(sts_thread_);
|
|
|
|
int _status = 0;
|
|
|
|
if (state_ != NR_CONNECTED) {
|
|
|
|
ABORT(R_FAILED);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffered_bytes_ + len >= nsITCPSocketCallback::BUFFER_SIZE) {
|
|
|
|
ABORT(R_WOULDBLOCK);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffered_bytes_ += len;
|
|
|
|
{
|
|
|
|
InfallibleTArray<uint8_t>* arr = new InfallibleTArray<uint8_t>();
|
|
|
|
arr->AppendElements(static_cast<const uint8_t*>(msg), len);
|
|
|
|
// keep track of un-acknowleged writes by tracking number.
|
|
|
|
writes_in_flight_.push_back(len);
|
|
|
|
RUN_ON_THREAD(io_thread_,
|
2015-10-18 08:24:48 +03:00
|
|
|
mozilla::WrapRunnable(RefPtr<NrTcpSocketIpc>(this),
|
2015-01-05 10:49:50 +03:00
|
|
|
&NrTcpSocketIpc::write_i,
|
|
|
|
nsAutoPtr<InfallibleTArray<uint8_t>>(arr),
|
|
|
|
++tracking_number_),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
*written = len;
|
|
|
|
abort:
|
|
|
|
return _status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NrTcpSocketIpc::read(void* buf, size_t maxlen, size_t *len) {
|
|
|
|
int _status = 0;
|
|
|
|
if (state_ != NR_CONNECTED) {
|
|
|
|
ABORT(R_FAILED);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg_queue_.size() == 0) {
|
|
|
|
ABORT(R_WOULDBLOCK);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nr_tcp_message> msg(msg_queue_.front());
|
2015-01-05 10:49:50 +03:00
|
|
|
size_t consumed_len = std::min(maxlen, msg->unread_bytes());
|
|
|
|
memcpy(buf, msg->reading_pointer(), consumed_len);
|
|
|
|
if (consumed_len < msg->unread_bytes()) {
|
|
|
|
// There is still something left in buffer.
|
|
|
|
msg->read_bytes += consumed_len;
|
|
|
|
} else {
|
|
|
|
msg_queue_.pop();
|
|
|
|
}
|
|
|
|
*len = consumed_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
abort:
|
|
|
|
return _status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NrTcpSocketIpc::listen(int backlog) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return R_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NrTcpSocketIpc::accept(nr_transport_addr *addrp, nr_socket **sockp) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return R_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NrTcpSocketIpc::connect_i(const nsACString &remote_addr,
|
|
|
|
uint16_t remote_port,
|
|
|
|
const nsACString &local_addr,
|
|
|
|
uint16_t local_port) {
|
|
|
|
ASSERT_ON_THREAD(io_thread_);
|
|
|
|
mirror_state_ = NR_CONNECTING;
|
|
|
|
|
|
|
|
dom::TCPSocketChild* child = new dom::TCPSocketChild(NS_ConvertUTF8toUTF16(remote_addr), remote_port);
|
|
|
|
socket_child_ = child;
|
|
|
|
|
|
|
|
// XXX remove remote!
|
|
|
|
socket_child_->SendWindowlessOpenBind(this,
|
|
|
|
remote_addr, remote_port,
|
|
|
|
local_addr, local_port,
|
|
|
|
/* use ssl */ false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NrTcpSocketIpc::write_i(nsAutoPtr<InfallibleTArray<uint8_t>> arr,
|
|
|
|
uint32_t tracking_number) {
|
|
|
|
ASSERT_ON_THREAD(io_thread_);
|
|
|
|
if (!socket_child_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
socket_child_->SendSendArray(*arr, tracking_number);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NrTcpSocketIpc::close_i() {
|
|
|
|
ASSERT_ON_THREAD(io_thread_);
|
|
|
|
mirror_state_ = NR_CLOSING;
|
|
|
|
if (!socket_child_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
socket_child_->SendClose();
|
|
|
|
}
|
|
|
|
|
|
|
|
// close(), but transfer the socket_child_ reference to die as well
|
|
|
|
// static
|
|
|
|
void NrTcpSocketIpc::release_child_i(dom::TCPSocketChild* aChild,
|
|
|
|
nsCOMPtr<nsIEventTarget> sts_thread) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<dom::TCPSocketChild> socket_child_ref =
|
2015-01-05 10:49:50 +03:00
|
|
|
already_AddRefed<dom::TCPSocketChild>(aChild);
|
|
|
|
if (socket_child_ref) {
|
|
|
|
socket_child_ref->SendClose();
|
|
|
|
}
|
|
|
|
// io_thread_ is MainThread, so no use to release
|
|
|
|
}
|
|
|
|
|
|
|
|
void NrTcpSocketIpc::message_sent_s(uint32_t buffered_amount,
|
|
|
|
uint32_t tracking_number) {
|
|
|
|
ASSERT_ON_THREAD(sts_thread_);
|
|
|
|
|
|
|
|
size_t num_unacked_writes = tracking_number_ - tracking_number;
|
|
|
|
while (writes_in_flight_.size() > num_unacked_writes) {
|
|
|
|
writes_in_flight_.pop_front();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t unacked_write_len : writes_in_flight_) {
|
|
|
|
buffered_amount += unacked_write_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
r_log(LOG_GENERIC, LOG_ERR,
|
|
|
|
"UpdateBufferedAmount: (tracking %u): %u, waiting: %s",
|
|
|
|
tracking_number, buffered_amount,
|
|
|
|
(poll_flags() & PR_POLL_WRITE) ? "yes" : "no");
|
|
|
|
|
|
|
|
buffered_bytes_ = buffered_amount;
|
|
|
|
maybe_post_socket_ready();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NrTcpSocketIpc::recv_message_s(nr_tcp_message *msg) {
|
|
|
|
ASSERT_ON_THREAD(sts_thread_);
|
|
|
|
msg_queue_.push(msg);
|
|
|
|
maybe_post_socket_ready();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NrTcpSocketIpc::update_state_s(NrSocketIpcState next_state) {
|
|
|
|
ASSERT_ON_THREAD(sts_thread_);
|
|
|
|
// only allow valid transitions
|
|
|
|
switch (state_) {
|
|
|
|
case NR_CONNECTING:
|
|
|
|
if (next_state == NR_CONNECTED) {
|
|
|
|
state_ = NR_CONNECTED;
|
|
|
|
maybe_post_socket_ready();
|
|
|
|
} else {
|
|
|
|
state_ = next_state; // all states are valid from CONNECTING
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NR_CONNECTED:
|
|
|
|
if (next_state != NR_CONNECTING) {
|
|
|
|
state_ = next_state;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NR_CLOSING:
|
|
|
|
if (next_state == NR_CLOSED) {
|
|
|
|
state_ = next_state;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NR_CLOSED:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("update_state_s while in illegal state");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NrTcpSocketIpc::maybe_post_socket_ready() {
|
|
|
|
bool has_event = false;
|
|
|
|
if (state_ == NR_CONNECTED) {
|
|
|
|
if (poll_flags() & PR_POLL_WRITE) {
|
|
|
|
// This effectively polls via the event loop until the
|
|
|
|
// NR_ASYNC_WAIT_WRITE is no longer armed.
|
|
|
|
if (buffered_bytes_ < nsITCPSocketCallback::BUFFER_SIZE) {
|
|
|
|
r_log(LOG_GENERIC, LOG_INFO, "Firing write callback (%u)",
|
|
|
|
(uint32_t)buffered_bytes_);
|
|
|
|
fire_callback(NR_ASYNC_WAIT_WRITE);
|
|
|
|
has_event = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (poll_flags() & PR_POLL_READ) {
|
|
|
|
if (msg_queue_.size()) {
|
|
|
|
r_log(LOG_GENERIC, LOG_INFO, "Firing read callback (%u)",
|
|
|
|
(uint32_t)msg_queue_.size());
|
|
|
|
fire_callback(NR_ASYNC_WAIT_READ);
|
|
|
|
has_event = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If any event has been posted, we post a runnable to see
|
|
|
|
// if the events have to be posted again.
|
|
|
|
if (has_event) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<TcpSocketReadyRunner> runnable = new TcpSocketReadyRunner(this);
|
2015-01-05 10:49:50 +03:00
|
|
|
NS_DispatchToCurrentThread(runnable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
} // close namespace
|
|
|
|
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
// Bridge to the nr_socket interface
|
|
|
|
static int nr_socket_local_destroy(void **objp);
|
|
|
|
static int nr_socket_local_sendto(void *obj,const void *msg, size_t len,
|
|
|
|
int flags, nr_transport_addr *to);
|
|
|
|
static int nr_socket_local_recvfrom(void *obj,void * restrict buf,
|
|
|
|
size_t maxlen, size_t *len, int flags, nr_transport_addr *from);
|
|
|
|
static int nr_socket_local_getfd(void *obj, NR_SOCKET *fd);
|
|
|
|
static int nr_socket_local_getaddr(void *obj, nr_transport_addr *addrp);
|
|
|
|
static int nr_socket_local_close(void *obj);
|
2013-12-06 22:20:19 +04:00
|
|
|
static int nr_socket_local_connect(void *sock, nr_transport_addr *addr);
|
|
|
|
static int nr_socket_local_write(void *obj,const void *msg, size_t len,
|
|
|
|
size_t *written);
|
|
|
|
static int nr_socket_local_read(void *obj,void * restrict buf, size_t maxlen,
|
|
|
|
size_t *len);
|
2014-04-23 12:15:25 +04:00
|
|
|
static int nr_socket_local_listen(void *obj, int backlog);
|
|
|
|
static int nr_socket_local_accept(void *obj, nr_transport_addr *addrp,
|
|
|
|
nr_socket **sockp);
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
static nr_socket_vtbl nr_socket_local_vtbl={
|
2014-04-23 12:15:25 +04:00
|
|
|
2,
|
2012-10-03 00:04:58 +04:00
|
|
|
nr_socket_local_destroy,
|
|
|
|
nr_socket_local_sendto,
|
|
|
|
nr_socket_local_recvfrom,
|
|
|
|
nr_socket_local_getfd,
|
|
|
|
nr_socket_local_getaddr,
|
2013-12-06 22:20:19 +04:00
|
|
|
nr_socket_local_connect,
|
|
|
|
nr_socket_local_write,
|
|
|
|
nr_socket_local_read,
|
2014-04-23 12:15:25 +04:00
|
|
|
nr_socket_local_close,
|
|
|
|
nr_socket_local_listen,
|
|
|
|
nr_socket_local_accept
|
2012-10-03 00:04:58 +04:00
|
|
|
};
|
|
|
|
|
2016-01-21 02:25:26 +03:00
|
|
|
/* static */
|
|
|
|
int
|
|
|
|
NrSocketBase::CreateSocket(nr_transport_addr *addr, RefPtr<NrSocketBase> *sock)
|
|
|
|
{
|
2015-01-05 10:49:50 +03:00
|
|
|
int r, _status;
|
2013-06-19 15:08:06 +04:00
|
|
|
|
|
|
|
// create IPC bridge for content process
|
2015-07-04 04:29:00 +03:00
|
|
|
if (XRE_IsParentProcess()) {
|
2016-01-21 02:25:26 +03:00
|
|
|
*sock = new NrSocket();
|
2013-06-19 15:08:06 +04:00
|
|
|
} else {
|
2015-01-05 10:49:50 +03:00
|
|
|
switch (addr->protocol) {
|
|
|
|
case IPPROTO_UDP:
|
2016-01-21 02:25:26 +03:00
|
|
|
*sock = new NrUdpSocketIpc();
|
2015-01-05 10:49:50 +03:00
|
|
|
break;
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
#if defined(MOZILLA_INTERNAL_API) && !defined(MOZILLA_XPCOMRT_API)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIThread> main_thread;
|
|
|
|
NS_GetMainThread(getter_AddRefs(main_thread));
|
2016-01-21 02:25:26 +03:00
|
|
|
*sock = new NrTcpSocketIpc(main_thread.get());
|
2015-01-05 10:49:50 +03:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
ABORT(R_REJECTED);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
2013-06-19 15:08:06 +04:00
|
|
|
}
|
|
|
|
|
2016-01-21 02:25:26 +03:00
|
|
|
r = (*sock)->create(addr);
|
2012-10-03 00:04:58 +04:00
|
|
|
if (r)
|
|
|
|
ABORT(r);
|
|
|
|
|
2016-01-21 02:25:26 +03:00
|
|
|
_status = 0;
|
|
|
|
abort:
|
|
|
|
if (_status) {
|
|
|
|
*sock = nullptr;
|
|
|
|
}
|
|
|
|
return _status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nr_socket_local_create(void *obj, nr_transport_addr *addr, nr_socket **sockp) {
|
|
|
|
RefPtr<NrSocketBase> sock;
|
|
|
|
int r, _status;
|
|
|
|
|
|
|
|
r = NrSocketBase::CreateSocket(addr, &sock);
|
|
|
|
if (r) {
|
|
|
|
ABORT(r);
|
|
|
|
}
|
|
|
|
|
2013-12-06 22:20:19 +04:00
|
|
|
r = nr_socket_create_int(static_cast<void *>(sock),
|
|
|
|
sock->vtbl(), sockp);
|
2012-10-03 00:04:58 +04:00
|
|
|
if (r)
|
|
|
|
ABORT(r);
|
|
|
|
|
2013-12-06 22:20:19 +04:00
|
|
|
_status = 0;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2014-11-15 02:58:56 +03:00
|
|
|
{
|
|
|
|
// We will release this reference in destroy(), not exactly the normal
|
|
|
|
// ownership model, but it is what it is.
|
|
|
|
NrSocketBase* dummy = sock.forget().take();
|
|
|
|
(void)dummy;
|
2012-10-03 00:04:58 +04:00
|
|
|
}
|
2014-11-15 02:58:56 +03:00
|
|
|
|
|
|
|
abort:
|
2012-10-03 00:04:58 +04:00
|
|
|
return _status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int nr_socket_local_destroy(void **objp) {
|
|
|
|
if(!objp || !*objp)
|
|
|
|
return 0;
|
|
|
|
|
2013-06-19 15:08:06 +04:00
|
|
|
NrSocketBase *sock = static_cast<NrSocketBase *>(*objp);
|
2016-01-22 10:47:01 +03:00
|
|
|
*objp = 0;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
sock->close(); // Signal STS that we want not to listen
|
|
|
|
sock->Release(); // Decrement the ref count
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nr_socket_local_sendto(void *obj,const void *msg, size_t len,
|
|
|
|
int flags, nr_transport_addr *addr) {
|
2013-06-19 15:08:06 +04:00
|
|
|
NrSocketBase *sock = static_cast<NrSocketBase *>(obj);
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
return sock->sendto(msg, len, flags, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nr_socket_local_recvfrom(void *obj,void * restrict buf,
|
|
|
|
size_t maxlen, size_t *len, int flags,
|
|
|
|
nr_transport_addr *addr) {
|
2013-06-19 15:08:06 +04:00
|
|
|
NrSocketBase *sock = static_cast<NrSocketBase *>(obj);
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
return sock->recvfrom(buf, maxlen, len, flags, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nr_socket_local_getfd(void *obj, NR_SOCKET *fd) {
|
2013-06-19 15:08:06 +04:00
|
|
|
NrSocketBase *sock = static_cast<NrSocketBase *>(obj);
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
*fd = sock;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nr_socket_local_getaddr(void *obj, nr_transport_addr *addrp) {
|
2013-06-19 15:08:06 +04:00
|
|
|
NrSocketBase *sock = static_cast<NrSocketBase *>(obj);
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
return sock->getaddr(addrp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int nr_socket_local_close(void *obj) {
|
2013-06-19 15:08:06 +04:00
|
|
|
NrSocketBase *sock = static_cast<NrSocketBase *>(obj);
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
sock->close();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-06 22:20:19 +04:00
|
|
|
static int nr_socket_local_write(void *obj, const void *msg, size_t len,
|
|
|
|
size_t *written) {
|
|
|
|
NrSocket *sock = static_cast<NrSocket *>(obj);
|
|
|
|
|
|
|
|
return sock->write(msg, len, written);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nr_socket_local_read(void *obj, void * restrict buf, size_t maxlen,
|
|
|
|
size_t *len) {
|
|
|
|
NrSocket *sock = static_cast<NrSocket *>(obj);
|
|
|
|
|
|
|
|
return sock->read(buf, maxlen, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nr_socket_local_connect(void *obj, nr_transport_addr *addr) {
|
|
|
|
NrSocket *sock = static_cast<NrSocket *>(obj);
|
|
|
|
|
|
|
|
return sock->connect(addr);
|
|
|
|
}
|
|
|
|
|
2014-04-23 12:15:25 +04:00
|
|
|
static int nr_socket_local_listen(void *obj, int backlog) {
|
|
|
|
NrSocket *sock = static_cast<NrSocket *>(obj);
|
|
|
|
|
|
|
|
return sock->listen(backlog);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nr_socket_local_accept(void *obj, nr_transport_addr *addrp,
|
|
|
|
nr_socket **sockp) {
|
|
|
|
NrSocket *sock = static_cast<NrSocket *>(obj);
|
|
|
|
|
|
|
|
return sock->accept(addrp, sockp);
|
|
|
|
}
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
// Implement async api
|
|
|
|
int NR_async_wait(NR_SOCKET sock, int how, NR_async_cb cb,void *cb_arg,
|
|
|
|
char *function,int line) {
|
2013-06-19 15:08:06 +04:00
|
|
|
NrSocketBase *s = static_cast<NrSocketBase *>(sock);
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
return s->async_wait(how, cb, cb_arg, function, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
int NR_async_cancel(NR_SOCKET sock,int how) {
|
2013-06-19 15:08:06 +04:00
|
|
|
NrSocketBase *s = static_cast<NrSocketBase *>(sock);
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
return s->cancel(how);
|
|
|
|
}
|
|
|
|
|
2013-12-06 22:20:19 +04:00
|
|
|
nr_socket_vtbl* NrSocketBase::vtbl() {
|
|
|
|
return &nr_socket_local_vtbl;
|
|
|
|
}
|