1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "remoting/test/fake_network_dispatcher.h"
#include <stddef.h>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/task/single_thread_task_runner.h"
#include "net/base/io_buffer.h"
namespace remoting {
FakeNetworkDispatcher::FakeNetworkDispatcher() : allocated_address_(0) {}
FakeNetworkDispatcher::~FakeNetworkDispatcher() {
CHECK(nodes_.empty());
}
webrtc::IPAddress FakeNetworkDispatcher::AllocateAddress() {
in6_addr addr;
memset(&addr, 0, sizeof(addr));
// fc00::/7 is reserved for unique local addresses.
addr.s6_addr[0] = 0xfc;
// Copy |allocated_address_| to the end of |addr|.
++allocated_address_;
for (size_t i = 0; i < sizeof(allocated_address_); ++i) {
addr.s6_addr[15 - i] = (allocated_address_ >> (8 * i)) & 0xff;
}
return webrtc::IPAddress(addr);
}
void FakeNetworkDispatcher::AddNode(Node* node) {
DCHECK(node->GetThread()->BelongsToCurrentThread());
base::AutoLock auto_lock(nodes_lock_);
DCHECK(!base::Contains(nodes_, node->GetAddress()));
nodes_[node->GetAddress()] = node;
}
void FakeNetworkDispatcher::RemoveNode(Node* node) {
DCHECK(node->GetThread()->BelongsToCurrentThread());
base::AutoLock auto_lock(nodes_lock_);
DCHECK(nodes_[node->GetAddress()] == node);
nodes_.erase(node->GetAddress());
}
void FakeNetworkDispatcher::DeliverPacket(
const webrtc::SocketAddress& from,
const webrtc::SocketAddress& to,
const scoped_refptr<net::IOBuffer>& data,
int data_size) {
Node* node;
{
base::AutoLock auto_lock(nodes_lock_);
auto node_it = nodes_.find(to.ipaddr());
if (node_it == nodes_.end()) {
LOG(ERROR) << "Tried to deliver packet to unknown target: "
<< to.ToString();
return;
}
node = node_it->second;
// Check if |node| belongs to a different thread and post a task in that
// case.
scoped_refptr<base::SingleThreadTaskRunner> task_runner = node->GetThread();
if (!task_runner->BelongsToCurrentThread()) {
task_runner->PostTask(
FROM_HERE, base::BindOnce(&FakeNetworkDispatcher::DeliverPacket, this,
from, to, data, data_size));
return;
}
}
// Call ReceivePacket() without lock held. It's safe because at this point we
// know that |node| belongs to the current thread.
node->ReceivePacket(from, to, data, data_size);
}
} // namespace remoting
|