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/. */
|
|
|
|
|
|
|
|
// Original author: ekr@rtfm.com
|
|
|
|
|
|
|
|
/* Some source code here from nICEr. Copyright is:
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Implementation of nICEr/nr_socket that is tied to the Gecko
|
|
|
|
// SocketTransportService.
|
|
|
|
|
|
|
|
#ifndef nr_socket_prsock__
|
|
|
|
#define nr_socket_prsock__
|
|
|
|
|
2018-06-05 20:10:16 +03:00
|
|
|
#include <memory>
|
2013-06-19 15:08:06 +04:00
|
|
|
#include <queue>
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
#include "nspr.h"
|
|
|
|
#include "prio.h"
|
|
|
|
|
2020-01-23 20:50:51 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsASocketHandler.h"
|
|
|
|
#include "nsXPCOM.h"
|
2013-02-16 06:33:31 +04:00
|
|
|
#include "nsIEventTarget.h"
|
2013-06-19 15:08:06 +04:00
|
|
|
#include "nsIUDPSocketChild.h"
|
2014-08-19 03:14:00 +04:00
|
|
|
#include "nsProxyRelease.h"
|
2015-05-29 17:13:33 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2018-05-10 01:13:35 +03:00
|
|
|
#include "mediapacket.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
#include "m_cpp_utils.h"
|
2013-06-19 15:08:06 +04:00
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2014-05-20 06:16:38 +04:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2015-05-29 17:13:33 +03:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2019-02-26 00:50:42 +03:00
|
|
|
// nICEr includes
|
|
|
|
extern "C" {
|
|
|
|
#include "transport_addr.h"
|
|
|
|
#include "async_wait.h"
|
|
|
|
}
|
|
|
|
|
2013-12-06 22:20:19 +04:00
|
|
|
// Stub declaration for nICEr type
|
|
|
|
typedef struct nr_socket_vtbl_ nr_socket_vtbl;
|
2014-04-23 12:15:25 +04:00
|
|
|
typedef struct nr_socket_ nr_socket;
|
2013-12-06 22:20:19 +04:00
|
|
|
|
2016-02-27 22:12:07 +03:00
|
|
|
#if defined(MOZILLA_INTERNAL_API)
|
2015-01-05 10:49:50 +03:00
|
|
|
namespace mozilla {
|
2018-06-05 20:10:16 +03:00
|
|
|
class NrSocketProxyConfig;
|
2015-01-05 10:49:50 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
#endif
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2013-03-09 00:11:17 +04:00
|
|
|
namespace net {
|
|
|
|
union NetAddr;
|
|
|
|
}
|
|
|
|
|
2019-05-09 09:55:05 +03:00
|
|
|
namespace dom {
|
|
|
|
class UDPSocketChild;
|
|
|
|
} // namespace dom
|
|
|
|
|
2013-06-19 15:08:06 +04:00
|
|
|
class NrSocketBase {
|
2012-10-03 00:04:58 +04:00
|
|
|
public:
|
2013-12-06 22:20:19 +04:00
|
|
|
NrSocketBase() : connect_invoked_(false), poll_flags_(0) {
|
2012-10-03 00:04:58 +04:00
|
|
|
memset(cbs_, 0, sizeof(cbs_));
|
|
|
|
memset(cb_args_, 0, sizeof(cb_args_));
|
2013-06-19 15:08:06 +04:00
|
|
|
memset(&my_addr_, 0, sizeof(my_addr_));
|
|
|
|
}
|
|
|
|
virtual ~NrSocketBase() {}
|
|
|
|
|
2016-01-21 02:25:26 +03:00
|
|
|
// Factory method; will create either an NrSocket, NrUdpSocketIpc, or
|
|
|
|
// NrTcpSocketIpc as appropriate.
|
2019-05-01 11:47:10 +03:00
|
|
|
static int CreateSocket(nr_transport_addr* addr, RefPtr<NrSocketBase>* sock,
|
|
|
|
const std::shared_ptr<NrSocketProxyConfig>& config);
|
|
|
|
static bool IsForbiddenAddress(nr_transport_addr* addr);
|
2016-01-21 02:25:26 +03:00
|
|
|
|
2013-06-19 15:08:06 +04:00
|
|
|
// the nr_socket APIs
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual int create(nr_transport_addr* addr) = 0;
|
|
|
|
virtual int sendto(const void* msg, size_t len, int flags,
|
|
|
|
nr_transport_addr* to) = 0;
|
|
|
|
virtual int recvfrom(void* buf, size_t maxlen, size_t* len, int flags,
|
|
|
|
nr_transport_addr* from) = 0;
|
|
|
|
virtual int getaddr(nr_transport_addr* addrp) = 0;
|
2013-06-19 15:08:06 +04:00
|
|
|
virtual void close() = 0;
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual int connect(nr_transport_addr* addr) = 0;
|
|
|
|
virtual int write(const void* msg, size_t len, size_t* written) = 0;
|
|
|
|
virtual int read(void* buf, size_t maxlen, size_t* len) = 0;
|
2014-04-23 12:15:25 +04:00
|
|
|
virtual int listen(int backlog) = 0;
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual int accept(nr_transport_addr* addrp, nr_socket** sockp) = 0;
|
2013-06-19 15:08:06 +04:00
|
|
|
|
|
|
|
// Implementations of the async_event APIs
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual int async_wait(int how, NR_async_cb cb, void* cb_arg, char* function,
|
2013-06-19 15:08:06 +04:00
|
|
|
int line);
|
|
|
|
virtual int cancel(int how);
|
|
|
|
|
|
|
|
// nsISupport reference counted interface
|
2017-01-25 22:51:34 +03:00
|
|
|
NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
|
2013-06-19 15:08:06 +04:00
|
|
|
|
|
|
|
uint32_t poll_flags() { return poll_flags_; }
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual nr_socket_vtbl* vtbl(); // To access in test classes.
|
2013-12-06 22:20:19 +04:00
|
|
|
|
2014-05-20 06:16:38 +04:00
|
|
|
static TimeStamp short_term_violation_time();
|
|
|
|
static TimeStamp long_term_violation_time();
|
2019-05-01 11:47:10 +03:00
|
|
|
const nr_transport_addr& my_addr() const { return my_addr_; }
|
2014-05-20 06:16:38 +04:00
|
|
|
|
2013-06-19 15:08:06 +04:00
|
|
|
void fire_callback(int how);
|
2013-12-06 22:20:19 +04:00
|
|
|
|
2016-01-21 02:25:26 +03:00
|
|
|
protected:
|
2013-12-06 22:20:19 +04:00
|
|
|
bool connect_invoked_;
|
2013-06-19 15:08:06 +04:00
|
|
|
nr_transport_addr my_addr_;
|
|
|
|
|
|
|
|
private:
|
|
|
|
NR_async_cb cbs_[NR_ASYNC_WAIT_WRITE + 1];
|
2019-05-01 11:47:10 +03:00
|
|
|
void* cb_args_[NR_ASYNC_WAIT_WRITE + 1];
|
2013-06-19 15:08:06 +04:00
|
|
|
uint32_t poll_flags_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class NrSocket : public NrSocketBase, public nsASocketHandler {
|
|
|
|
public:
|
|
|
|
NrSocket() : fd_(nullptr) {}
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
// Implement nsASocket
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual void OnSocketReady(PRFileDesc* fd, int16_t outflags) override;
|
|
|
|
virtual void OnSocketDetached(PRFileDesc* fd) override;
|
|
|
|
virtual void IsLocal(bool* aIsLocal) override;
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual uint64_t ByteCountSent() override { return 0; }
|
|
|
|
virtual uint64_t ByteCountReceived() override { return 0; }
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
// nsISupports methods
|
2013-07-19 06:23:32 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
// Implementations of the async_event APIs
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual int async_wait(int how, NR_async_cb cb, void* cb_arg, char* function,
|
2015-03-21 19:28:04 +03:00
|
|
|
int line) override;
|
|
|
|
virtual int cancel(int how) override;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
// Implementations of the nr_socket APIs
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual int create(nr_transport_addr* addr)
|
2015-03-21 19:28:04 +03:00
|
|
|
override; // (really init, but it's called create)
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual int sendto(const void* msg, size_t len, int flags,
|
|
|
|
nr_transport_addr* to) override;
|
|
|
|
virtual int recvfrom(void* buf, size_t maxlen, size_t* len, int flags,
|
|
|
|
nr_transport_addr* from) override;
|
|
|
|
virtual int getaddr(nr_transport_addr* addrp) override;
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void close() override;
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual int connect(nr_transport_addr* addr) override;
|
|
|
|
virtual int write(const void* msg, size_t len, size_t* written) override;
|
|
|
|
virtual int read(void* buf, size_t maxlen, size_t* len) override;
|
2014-04-23 12:15:25 +04:00
|
|
|
virtual int listen(int backlog) override;
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual int accept(nr_transport_addr* addrp, nr_socket** sockp) override;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2015-06-11 01:27:12 +03:00
|
|
|
protected:
|
2014-06-24 20:36:44 +04:00
|
|
|
virtual ~NrSocket() {
|
|
|
|
if (fd_) PR_Close(fd_);
|
|
|
|
}
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
DISALLOW_COPY_ASSIGN(NrSocket);
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
PRFileDesc* fd_;
|
2013-02-16 06:33:31 +04:00
|
|
|
nsCOMPtr<nsIEventTarget> ststhread_;
|
2012-10-03 00:04:58 +04:00
|
|
|
};
|
|
|
|
|
2013-06-19 15:08:06 +04:00
|
|
|
struct nr_udp_message {
|
2020-01-23 20:50:51 +03:00
|
|
|
nr_udp_message(const PRNetAddr& from, nsAutoPtr<MediaPacket>& data)
|
|
|
|
: from(from), data(data) {}
|
2013-06-19 15:08:06 +04:00
|
|
|
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(nr_udp_message);
|
|
|
|
|
|
|
|
PRNetAddr from;
|
2020-01-23 20:50:51 +03:00
|
|
|
nsAutoPtr<MediaPacket> data;
|
2013-06-19 15:08:06 +04:00
|
|
|
|
|
|
|
private:
|
2014-06-20 15:08:24 +04:00
|
|
|
~nr_udp_message() {}
|
2013-06-19 15:08:06 +04:00
|
|
|
DISALLOW_COPY_ASSIGN(nr_udp_message);
|
|
|
|
};
|
|
|
|
|
2014-12-24 03:22:02 +03:00
|
|
|
class NrSocketIpc : public NrSocketBase {
|
2013-06-19 15:08:06 +04:00
|
|
|
public:
|
|
|
|
enum NrSocketIpcState {
|
|
|
|
NR_INIT,
|
|
|
|
NR_CONNECTING,
|
|
|
|
NR_CONNECTED,
|
|
|
|
NR_CLOSING,
|
|
|
|
NR_CLOSED,
|
|
|
|
};
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NrSocketIpc(nsIEventTarget* aThread);
|
2015-01-05 10:49:50 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
nsCOMPtr<nsIEventTarget> sts_thread_;
|
|
|
|
// Note: for UDP PBackground, this is a thread held by SingletonThreadHolder.
|
|
|
|
// For TCP PNecko, this is MainThread (and TCPSocket requires MainThread
|
|
|
|
// currently)
|
|
|
|
const nsCOMPtr<nsIEventTarget> io_thread_;
|
|
|
|
virtual ~NrSocketIpc(){};
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_ASSIGN(NrSocketIpc);
|
|
|
|
};
|
|
|
|
|
|
|
|
class NrUdpSocketIpc : public NrSocketIpc {
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(NrUdpSocketIpc, override)
|
2014-12-24 03:22:02 +03:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP CallListenerError(const nsACString& message,
|
|
|
|
const nsACString& filename,
|
2014-12-24 03:22:02 +03:00
|
|
|
uint32_t line_number);
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP CallListenerReceivedData(const nsACString& host, uint16_t port,
|
2019-05-09 09:55:07 +03:00
|
|
|
const nsTArray<uint8_t>& data);
|
2014-12-24 03:22:02 +03:00
|
|
|
NS_IMETHODIMP CallListenerOpened();
|
2016-01-22 10:47:01 +03:00
|
|
|
NS_IMETHODIMP CallListenerConnected();
|
2014-12-24 03:22:02 +03:00
|
|
|
NS_IMETHODIMP CallListenerClosed();
|
2013-06-19 15:08:06 +04:00
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
NrUdpSocketIpc();
|
2013-06-19 15:08:06 +04:00
|
|
|
|
|
|
|
// Implementations of the NrSocketBase APIs
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual int create(nr_transport_addr* addr) override;
|
|
|
|
virtual int sendto(const void* msg, size_t len, int flags,
|
|
|
|
nr_transport_addr* to) override;
|
|
|
|
virtual int recvfrom(void* buf, size_t maxlen, size_t* len, int flags,
|
|
|
|
nr_transport_addr* from) override;
|
|
|
|
virtual int getaddr(nr_transport_addr* addrp) override;
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void close() override;
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual int connect(nr_transport_addr* addr) override;
|
|
|
|
virtual int write(const void* msg, size_t len, size_t* written) override;
|
|
|
|
virtual int read(void* buf, size_t maxlen, size_t* len) override;
|
2014-04-23 12:15:25 +04:00
|
|
|
virtual int listen(int backlog) override;
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual int accept(nr_transport_addr* addrp, nr_socket** sockp) override;
|
2013-06-19 15:08:06 +04:00
|
|
|
|
|
|
|
private:
|
2015-01-05 10:49:50 +03:00
|
|
|
virtual ~NrUdpSocketIpc();
|
2014-06-24 20:36:44 +04:00
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
DISALLOW_COPY_ASSIGN(NrUdpSocketIpc);
|
2015-05-29 17:13:33 +03:00
|
|
|
|
2016-01-22 10:47:01 +03:00
|
|
|
nsresult SetAddress(); // Set the local address from parent info.
|
|
|
|
|
2015-05-29 17:13:33 +03:00
|
|
|
// Main or private thread executors of the NrSocketBase APIs
|
2019-05-01 11:47:10 +03:00
|
|
|
void create_i(const nsACString& host, const uint16_t port);
|
|
|
|
void connect_i(const nsACString& host, const uint16_t port);
|
2020-01-23 20:50:51 +03:00
|
|
|
void sendto_i(const net::NetAddr& addr, nsAutoPtr<MediaPacket> buf);
|
2015-05-29 17:13:33 +03:00
|
|
|
void close_i();
|
|
|
|
#if defined(MOZILLA_INTERNAL_API) && !defined(MOZILLA_XPCOMRT_API)
|
2019-05-09 09:55:05 +03:00
|
|
|
static void destroy_i(dom::UDPSocketChild* aChild,
|
2020-01-23 20:50:51 +03:00
|
|
|
nsCOMPtr<nsIEventTarget>& aStsThread);
|
2015-05-29 17:13:33 +03:00
|
|
|
#endif
|
2013-06-19 15:08:06 +04:00
|
|
|
// STS thread executor
|
2015-10-18 08:24:48 +03:00
|
|
|
void recv_callback_s(RefPtr<nr_udp_message> msg);
|
2013-06-19 15:08:06 +04:00
|
|
|
|
2015-01-05 10:49:50 +03:00
|
|
|
ReentrantMonitor monitor_; // protects err_and state_
|
2013-06-19 15:08:06 +04:00
|
|
|
bool err_;
|
|
|
|
NrSocketIpcState state_;
|
2015-01-05 10:49:50 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
std::queue<RefPtr<nr_udp_message>> received_msgs_;
|
2013-06-19 15:08:06 +04:00
|
|
|
|
2019-05-09 09:55:05 +03:00
|
|
|
// only accessed from the io_thread
|
|
|
|
RefPtr<dom::UDPSocketChild> socket_child_;
|
2013-06-19 15:08:06 +04:00
|
|
|
};
|
|
|
|
|
2014-12-24 03:22:02 +03:00
|
|
|
// The socket child holds onto one of these, which just passes callbacks
|
|
|
|
// through and makes sure the ref to the NrSocketIpc is released on STS.
|
2015-01-05 10:49:50 +03:00
|
|
|
class NrUdpSocketIpcProxy : public nsIUDPSocketInternal {
|
2014-12-24 03:22:02 +03:00
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIUDPSOCKETINTERNAL
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult Init(const RefPtr<NrUdpSocketIpc>& socket);
|
2014-12-24 03:22:02 +03:00
|
|
|
|
|
|
|
private:
|
2015-01-05 10:49:50 +03:00
|
|
|
virtual ~NrUdpSocketIpcProxy();
|
2014-12-24 03:22:02 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<NrUdpSocketIpc> socket_;
|
2014-12-24 03:22:02 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> sts_thread_;
|
|
|
|
};
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
int nr_netaddr_to_transport_addr(const net::NetAddr* netaddr,
|
|
|
|
nr_transport_addr* addr, int protocol);
|
|
|
|
int nr_praddr_to_transport_addr(const PRNetAddr* praddr,
|
|
|
|
nr_transport_addr* addr, int protocol,
|
2013-12-06 22:20:19 +04:00
|
|
|
int keep);
|
2019-05-01 11:47:10 +03:00
|
|
|
int nr_transport_addr_get_addrstring_and_port(nr_transport_addr* addr,
|
|
|
|
nsACString* host, int32_t* port);
|
2012-10-03 00:04:58 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
#endif
|