gecko-dev/media/mtransport/nricestunaddr.cpp
Michael Froman f0c929ff2d Bug 1345511 - pt 2 - add IPC mechanism for getting stun addrs on main process. r=bwc
PStunAddrsRequest.ipdl defines the new IPC protocol to get stun
  addrs on the main process.
StunAddrsRequestChild requests the stun addrs from the parent.
StunAddrsRequestParent uses a static method on NrIceCtx to get the
  stun addrs from the STS thead and sends the addrs back to the
  child process.
NrIceStunAddr (nricestunaddr.{cpp|h}) wraps nr_local_addr and makes
  it easier to serialize/deserialize over IPC.
NrIceStunAddrMessageUtils follows the pattern used by other Necko
  IPC classes to define top-level serialization/deserialization
  calls used by the IPC framework.

Modifications under netwerk/ipc are to connect the new IPC
protocol to get stun addrs to PNecko since it is a network
related IPC protocol.

MozReview-Commit-ID: GyEapBe5krl

--HG--
extra : rebase_source : c650d6aa4f7928bcae6032424303869074a755d4
2017-03-21 19:59:05 -05:00

106 lines
3.0 KiB
C++

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "logging.h"
// nICEr includes
extern "C" {
#include "nr_api.h"
#include "r_memory.h"
#include "local_addr.h"
}
// Local includes
#include "nricestunaddr.h"
namespace mozilla {
MOZ_MTLOG_MODULE("mtransport")
NrIceStunAddr::NrIceStunAddr()
: localAddr_(new nr_local_addr)
{
memset(localAddr_, 0, sizeof(nr_local_addr));
}
NrIceStunAddr::NrIceStunAddr(const nr_local_addr* addr)
: localAddr_(new nr_local_addr)
{
nr_local_addr_copy(localAddr_,
const_cast<nr_local_addr*>(addr));
}
NrIceStunAddr::NrIceStunAddr(const NrIceStunAddr& rhs)
: localAddr_(new nr_local_addr)
{
nr_local_addr_copy(localAddr_,
const_cast<nr_local_addr*>(rhs.localAddr_));
}
NrIceStunAddr::~NrIceStunAddr()
{
delete localAddr_;
}
size_t
NrIceStunAddr::SerializationBufferSize() const
{
return sizeof(nr_local_addr);
}
nsresult
NrIceStunAddr::Serialize(char* buffer, size_t buffer_size) const
{
if (buffer_size != sizeof(nr_local_addr)) {
MOZ_MTLOG(ML_ERROR, "Failed trying to serialize NrIceStunAddr, "
"input buffer length (" << buffer_size <<
") does not match required length ("
<< sizeof(nr_local_addr) << ")");
MOZ_ASSERT(false, "Failed to serialize NrIceStunAddr, bad buffer size");
return NS_ERROR_FAILURE;
}
nr_local_addr* toAddr = (nr_local_addr*)buffer;
if (nr_local_addr_copy(toAddr, localAddr_)) {
MOZ_MTLOG(ML_ERROR, "Failed trying to serialize NrIceStunAddr, "
"could not copy nr_local_addr.");
MOZ_ASSERT(false, "Failed to serialize NrIceStunAddr, nr_local_addr_copy failed");
return NS_ERROR_FAILURE;
}
// don't serialize what will be a bad addr when we deserialize
toAddr->addr.addr = nullptr;
return NS_OK;
}
nsresult
NrIceStunAddr::Deserialize(const char* buffer, size_t buffer_size)
{
if (buffer_size != sizeof(nr_local_addr)) {
MOZ_MTLOG(ML_ERROR, "Failed trying to deserialize NrIceStunAddr, "
"input buffer length (" << buffer_size <<
") does not match required length ("
<< sizeof(nr_local_addr) << ")");
MOZ_ASSERT(false, "Failed to deserialize NrIceStunAddr, bad buffer size");
return NS_ERROR_FAILURE;
}
nr_local_addr* from_addr =
const_cast<nr_local_addr*>((const nr_local_addr*)buffer);
// At this point, from_addr->addr.addr is invalid (null), but will
// be fixed by nr_local_addr_copy.
if (nr_local_addr_copy(localAddr_, from_addr)) {
MOZ_MTLOG(ML_ERROR, "Failed trying to deserialize NrIceStunAddr, "
"could not copy nr_local_addr.");
MOZ_ASSERT(false, "Failed to deserialize NrIceStunAddr, nr_local_addr_copy failed");
return NS_ERROR_FAILURE;
}
return NS_OK;
}
} // namespace mozilla