Cleanup: Use common IP overhead definitions in test and prod code

This avoid duplication. As part of this moving the overhead calculation
to the IP address class so it's easier to find and more natural to use.

Bug: webrtc:9883
Change-Id: If4d865f445bc1a302572896932966ce30294e339
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/169445
Commit-Queue: Sebastian Jansson <srte@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#30657}
This commit is contained in:
Sebastian Jansson 2020-03-02 11:32:23 +01:00 committed by Commit Bot
parent 61f74d91f8
commit db5d7e470f
7 changed files with 28 additions and 40 deletions

View file

@ -9,17 +9,9 @@
*/ */
#include "api/test/network_emulation/network_emulation_interfaces.h" #include "api/test/network_emulation/network_emulation_interfaces.h"
#include "rtc_base/net_helper.h"
namespace webrtc { namespace webrtc {
namespace {
constexpr int kIPv4HeaderSize = 20;
constexpr int kIPv6HeaderSize = 40;
constexpr int kUdpHeaderSize = 8;
int IpHeaderSize(const rtc::SocketAddress& address) {
return (address.family() == AF_INET) ? kIPv4HeaderSize : kIPv6HeaderSize;
}
} // namespace
EmulatedIpPacket::EmulatedIpPacket(const rtc::SocketAddress& from, EmulatedIpPacket::EmulatedIpPacket(const rtc::SocketAddress& from,
const rtc::SocketAddress& to, const rtc::SocketAddress& to,
rtc::CopyOnWriteBuffer data, rtc::CopyOnWriteBuffer data,
@ -28,7 +20,8 @@ EmulatedIpPacket::EmulatedIpPacket(const rtc::SocketAddress& from,
: from(from), : from(from),
to(to), to(to),
data(data), data(data),
headers_size(IpHeaderSize(to) + application_overhead + kUdpHeaderSize), headers_size(to.ipaddr().overhead() + application_overhead +
cricket::kUdpHeaderSize),
arrival_time(arrival_time) { arrival_time(arrival_time) {
RTC_DCHECK(to.family() == AF_INET || to.family() == AF_INET6); RTC_DCHECK(to.family() == AF_INET || to.family() == AF_INET6);
} }

View file

@ -1682,8 +1682,7 @@ void P2PTransportChannel::SwitchSelectedConnection(Connection* conn,
selected_connection_->remote_candidate().network_id(); selected_connection_->remote_candidate().network_id();
network_route_->last_sent_packet_id = last_sent_packet_id_; network_route_->last_sent_packet_id = last_sent_packet_id_;
network_route_->packet_overhead = network_route_->packet_overhead =
GetIpOverhead( selected_connection_->local_candidate().address().ipaddr().overhead() +
selected_connection_->local_candidate().address().family()) +
GetProtocolOverhead(selected_connection_->local_candidate().protocol()); GetProtocolOverhead(selected_connection_->local_candidate().protocol());
} else { } else {
RTC_LOG(LS_INFO) << ToString() << ": No selected connection"; RTC_LOG(LS_INFO) << ToString() << ": No selected connection";

View file

@ -10,8 +10,6 @@
#include "rtc_base/async_packet_socket.h" #include "rtc_base/async_packet_socket.h"
#include "rtc_base/net_helper.h"
namespace rtc { namespace rtc {
PacketTimeUpdateParams::PacketTimeUpdateParams() = default; PacketTimeUpdateParams::PacketTimeUpdateParams() = default;
@ -35,12 +33,7 @@ void CopySocketInformationToPacketInfo(size_t packet_size_bytes,
bool is_connectionless, bool is_connectionless,
rtc::PacketInfo* info) { rtc::PacketInfo* info) {
info->packet_size_bytes = packet_size_bytes; info->packet_size_bytes = packet_size_bytes;
// TODO(srte): Make sure that the family of the local socket is always set info->ip_overhead_bytes = socket_from.GetLocalAddress().ipaddr().overhead();
// in the VirtualSocket implementation and remove this check.
int family = socket_from.GetLocalAddress().family();
if (family != 0) {
info->ip_overhead_bytes = cricket::GetIpOverhead(family);
}
} }
} // namespace rtc } // namespace rtc

View file

@ -53,6 +53,17 @@ uint32_t IPAddress::v4AddressAsHostOrderInteger() const {
} }
} }
int IPAddress::overhead() const {
switch (family_) {
case AF_INET: // IPv4
return 20;
case AF_INET6: // IPv6
return 40;
default:
return 0;
}
}
bool IPAddress::IsNil() const { bool IPAddress::IsNil() const {
return IPIsUnspec(*this); return IPIsUnspec(*this);
} }

View file

@ -111,6 +111,9 @@ class RTC_EXPORT IPAddress {
// For socketaddress' benefit. Returns the IP in host byte order. // For socketaddress' benefit. Returns the IP in host byte order.
uint32_t v4AddressAsHostOrderInteger() const; uint32_t v4AddressAsHostOrderInteger() const;
// Get the network layer overhead per packet based on the IP address family.
int overhead() const;
// Whether this is an unspecified IP address. // Whether this is an unspecified IP address.
bool IsNil() const; bool IsNil() const;

View file

@ -10,9 +10,6 @@
#include "rtc_base/net_helper.h" #include "rtc_base/net_helper.h"
#include "rtc_base/checks.h"
#include "rtc_base/ip_address.h"
namespace cricket { namespace cricket {
const char UDP_PROTOCOL_NAME[] = "udp"; const char UDP_PROTOCOL_NAME[] = "udp";
@ -20,23 +17,15 @@ const char TCP_PROTOCOL_NAME[] = "tcp";
const char SSLTCP_PROTOCOL_NAME[] = "ssltcp"; const char SSLTCP_PROTOCOL_NAME[] = "ssltcp";
const char TLS_PROTOCOL_NAME[] = "tls"; const char TLS_PROTOCOL_NAME[] = "tls";
int GetIpOverhead(int addr_family) {
switch (addr_family) {
case AF_INET: // IPv4
return 20;
case AF_INET6: // IPv6
return 40;
default:
RTC_NOTREACHED() << "Invaild address family.";
return 0;
}
}
int GetProtocolOverhead(const std::string& protocol) { int GetProtocolOverhead(const std::string& protocol) {
if (protocol == TCP_PROTOCOL_NAME || protocol == SSLTCP_PROTOCOL_NAME) { if (protocol == TCP_PROTOCOL_NAME || protocol == SSLTCP_PROTOCOL_NAME) {
return 20; return kTcpHeaderSize;
} } else if (protocol == UDP_PROTOCOL_NAME) {
return kUdpHeaderSize;
} else {
// TODO(srte): We should crash on unexpected input and handle TLS correctly.
return 8; return 8;
} }
}
} // namespace cricket } // namespace cricket

View file

@ -21,8 +21,8 @@ extern const char TCP_PROTOCOL_NAME[];
extern const char SSLTCP_PROTOCOL_NAME[]; extern const char SSLTCP_PROTOCOL_NAME[];
extern const char TLS_PROTOCOL_NAME[]; extern const char TLS_PROTOCOL_NAME[];
// Get the network layer overhead per packet based on the IP address family. constexpr int kTcpHeaderSize = 20;
int GetIpOverhead(int addr_family); constexpr int kUdpHeaderSize = 8;
// Get the transport layer overhead per packet based on the protocol. // Get the transport layer overhead per packet based on the protocol.
int GetProtocolOverhead(const std::string& protocol); int GetProtocolOverhead(const std::string& protocol);