/* * Copyright 2004 The WebRTC Project Authors. All rights reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree.
*/
// Tests that when sending from internal_addr to external_addrs through the // NAT type specified by nat_type, all external addrs receive the sent packet // and, if exp_same is true, all use the same mapped-address on the NAT. void TestSend(SocketServer* internal, const SocketAddress& internal_addr,
SocketServer* external, const SocketAddress external_addrs[4],
NATType nat_type, bool exp_same) {
Thread th_int(internal);
Thread th_ext(external);
th_int.Start();
th_ext.Start();
SocketAddress server_addr = internal_addr;
server_addr.SetPort(0); // Auto-select a port
NATServer* nat = new NATServer(nat_type, th_int, internal, server_addr, server_addr,
th_ext, external, external_addrs[0]);
NATSocketFactory* natsf = new NATSocketFactory(
internal, nat->internal_udp_address(), nat->internal_tcp_address());
TestClient* in;
th_int.BlockingCall([&] { in = CreateTestClient(natsf, internal_addr); });
TestClient* out[4];
th_ext.BlockingCall([&] { for (int i = 0; i < 4; i++)
out[i] = CreateTestClient(external, external_addrs[i]);
});
constchar* buf = "filter_test";
size_t len = strlen(buf);
delete nat; delete natsf; delete in; for (int i = 0; i < 4; i++) delete out[i];
}
// Tests that when sending from external_addrs to internal_addr, the packet // is delivered according to the specified filter_ip and filter_port rules. void TestRecv(SocketServer* internal, const SocketAddress& internal_addr,
SocketServer* external, const SocketAddress external_addrs[4],
NATType nat_type, bool filter_ip, bool filter_port) {
Thread th_int(internal);
Thread th_ext(external);
SocketAddress server_addr = internal_addr;
server_addr.SetPort(0); // Auto-select a port
th_int.Start();
th_ext.Start();
NATServer* nat = new NATServer(nat_type, th_int, internal, server_addr, server_addr,
th_ext, external, external_addrs[0]);
NATSocketFactory* natsf = new NATSocketFactory(
internal, nat->internal_udp_address(), nat->internal_tcp_address());
TestClient* in = nullptr;
th_int.BlockingCall([&] { in = CreateTestClient(natsf, internal_addr); });
TestClient* out[4];
th_ext.BlockingCall([&] { for (int i = 0; i < 4; i++)
out[i] = CreateTestClient(external, external_addrs[i]);
});
constchar* buf = "filter_test";
size_t len = strlen(buf);
bool TestConnectivity(const SocketAddress& src, const IPAddress& dst) { // The physical NAT tests require connectivity to the selected ip from the // internal address used for the NAT. Things like firewalls can break that, so // check to see if it's worth even trying with this ip.
std::unique_ptr<PhysicalSocketServer> pss(new PhysicalSocketServer());
std::unique_ptr<Socket> client(pss->CreateSocket(src.family(), SOCK_DGRAM));
std::unique_ptr<Socket> server(pss->CreateSocket(src.family(), SOCK_DGRAM)); if (client->Bind(SocketAddress(src.ipaddr(), 0)) != 0 ||
server->Bind(SocketAddress(dst, 0)) != 0) { returnfalse;
} constchar* buf = "hello other socket";
size_t len = strlen(buf); int sent = client->SendTo(buf, len, server->GetLocalAddress());
Thread::Current()->SleepMs(100);
rtc::Buffer payload;
Socket::ReceiveBuffer receive_buffer(payload); int received = server->RecvFrom(receive_buffer); return received == sent && ::memcmp(buf, payload.data(), len) == 0;
}
void TestPhysicalInternal(const SocketAddress& int_addr) {
webrtc::test::ScopedKeyValueConfig field_trials;
rtc::AutoThread main_thread;
PhysicalSocketServer socket_server;
BasicNetworkManager network_manager(nullptr, &socket_server, &field_trials);
network_manager.StartUpdating(); // Process pending messages so the network list is updated.
Thread::Current()->ProcessMessages(0);
SocketAddress ext_addr1(int_addr);
SocketAddress ext_addr2; // Find an available IP with matching family. The test breaks if int_addr // can't talk to ip, so check for connectivity as well. for (const Network* const network : networks) { const IPAddress& ip = network->GetBestIP(); if (ip.family() == int_addr.family() && TestConnectivity(int_addr, ip)) {
ext_addr2.SetIP(ip); break;
}
} if (ext_addr2.IsNil()) {
RTC_LOG(LS_WARNING) << "No available IP of same family as "
<< int_addr.ToString(); return;
}
RTC_LOG(LS_INFO) << "selected ip " << ext_addr2.ipaddr().ToString();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.