diff --git a/api/test/network_emulation/network_emulation_interfaces.cc b/api/test/network_emulation/network_emulation_interfaces.cc index e023334af7..ac2eb1d971 100644 --- a/api/test/network_emulation/network_emulation_interfaces.cc +++ b/api/test/network_emulation/network_emulation_interfaces.cc @@ -9,17 +9,9 @@ */ #include "api/test/network_emulation/network_emulation_interfaces.h" +#include "rtc_base/net_helper.h" + 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, const rtc::SocketAddress& to, rtc::CopyOnWriteBuffer data, @@ -28,7 +20,8 @@ EmulatedIpPacket::EmulatedIpPacket(const rtc::SocketAddress& from, : from(from), to(to), data(data), - headers_size(IpHeaderSize(to) + application_overhead + kUdpHeaderSize), + headers_size(to.ipaddr().overhead() + application_overhead + + cricket::kUdpHeaderSize), arrival_time(arrival_time) { RTC_DCHECK(to.family() == AF_INET || to.family() == AF_INET6); } diff --git a/p2p/base/p2p_transport_channel.cc b/p2p/base/p2p_transport_channel.cc index 4e18cd43cb..4e56d161ab 100644 --- a/p2p/base/p2p_transport_channel.cc +++ b/p2p/base/p2p_transport_channel.cc @@ -1682,8 +1682,7 @@ void P2PTransportChannel::SwitchSelectedConnection(Connection* conn, selected_connection_->remote_candidate().network_id(); network_route_->last_sent_packet_id = last_sent_packet_id_; network_route_->packet_overhead = - GetIpOverhead( - selected_connection_->local_candidate().address().family()) + + selected_connection_->local_candidate().address().ipaddr().overhead() + GetProtocolOverhead(selected_connection_->local_candidate().protocol()); } else { RTC_LOG(LS_INFO) << ToString() << ": No selected connection"; diff --git a/rtc_base/async_packet_socket.cc b/rtc_base/async_packet_socket.cc index a42725c424..d5435d71d0 100644 --- a/rtc_base/async_packet_socket.cc +++ b/rtc_base/async_packet_socket.cc @@ -10,8 +10,6 @@ #include "rtc_base/async_packet_socket.h" -#include "rtc_base/net_helper.h" - namespace rtc { PacketTimeUpdateParams::PacketTimeUpdateParams() = default; @@ -35,12 +33,7 @@ void CopySocketInformationToPacketInfo(size_t packet_size_bytes, bool is_connectionless, rtc::PacketInfo* info) { info->packet_size_bytes = packet_size_bytes; - // TODO(srte): Make sure that the family of the local socket is always set - // in the VirtualSocket implementation and remove this check. - int family = socket_from.GetLocalAddress().family(); - if (family != 0) { - info->ip_overhead_bytes = cricket::GetIpOverhead(family); - } + info->ip_overhead_bytes = socket_from.GetLocalAddress().ipaddr().overhead(); } } // namespace rtc diff --git a/rtc_base/ip_address.cc b/rtc_base/ip_address.cc index cf7ffa8b90..9dd534c2b5 100644 --- a/rtc_base/ip_address.cc +++ b/rtc_base/ip_address.cc @@ -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 { return IPIsUnspec(*this); } diff --git a/rtc_base/ip_address.h b/rtc_base/ip_address.h index 6d857afe84..ae135a69dc 100644 --- a/rtc_base/ip_address.h +++ b/rtc_base/ip_address.h @@ -111,6 +111,9 @@ class RTC_EXPORT IPAddress { // For socketaddress' benefit. Returns the IP in host byte order. 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. bool IsNil() const; diff --git a/rtc_base/net_helper.cc b/rtc_base/net_helper.cc index 7dcb599933..893b500d56 100644 --- a/rtc_base/net_helper.cc +++ b/rtc_base/net_helper.cc @@ -10,9 +10,6 @@ #include "rtc_base/net_helper.h" -#include "rtc_base/checks.h" -#include "rtc_base/ip_address.h" - namespace cricket { const char UDP_PROTOCOL_NAME[] = "udp"; @@ -20,23 +17,15 @@ const char TCP_PROTOCOL_NAME[] = "tcp"; const char SSLTCP_PROTOCOL_NAME[] = "ssltcp"; 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) { 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 diff --git a/rtc_base/net_helper.h b/rtc_base/net_helper.h index e42502bb18..9abbbdefb2 100644 --- a/rtc_base/net_helper.h +++ b/rtc_base/net_helper.h @@ -21,8 +21,8 @@ extern const char TCP_PROTOCOL_NAME[]; extern const char SSLTCP_PROTOCOL_NAME[]; extern const char TLS_PROTOCOL_NAME[]; -// Get the network layer overhead per packet based on the IP address family. -int GetIpOverhead(int addr_family); +constexpr int kTcpHeaderSize = 20; +constexpr int kUdpHeaderSize = 8; // Get the transport layer overhead per packet based on the protocol. int GetProtocolOverhead(const std::string& protocol);