WebRTC collects network card address information source code analysis

I. Introduction

        The two parties in the WebRTC audio and video call need to exchange candidate information and then perform connectivity detection. Therefore, each party in the communication needs to collect its own host address, mapping address, etc. By collecting network card information, we can know the address information of the network card configuration.

2. Collect network card information

1. When to collect network card information

        The time for WebRTC to collect network card information is to call PeerConnection::SetLocalDescription, which will call JsepTransportController::MaybeStartGathering to collect candidates.

        JsepTransportController::MaybeStartGathering will call P2PTransportChannel::MaybeStartGathering, which then calls PortAllocator::CreateSession to create BasicPortAllocatorSession.

  

        BasicNetworkManager::StartUpdating is called in the BasicPortAllocatorSession constructor, which sends KUpdateNetworksMessage message to the network thread.

        After receiving KUpdateNetworksMessage, call BasicNetworkManager::UpdateNetworksContinually, then call BasicNetworkManager::UpdateNetworksOnce, and finally call CreateNetworks to start collecting network card information.

 

2. CreateNetworks

        Windows uses GetAdaptersAddresses to obtain the address information associated with the network card. For a detailed introduction to this API, please see here . The following only introduces the key parameter information used in WebRTC.

IPHLPAPI_DLL_LINKAGE ULONG GetAdaptersAddresses(
  [in]      ULONG                 Family,
  [in]      ULONG                 Flags,
  [in]      PVOID                 Reserved,
  [in, out] PIP_ADAPTER_ADDRESSES AdapterAddresses,
  [in, out] PULONG                SizePointer
);

The values ​​and meanings of Family are as follows.

value meaning
AF_UNSPEC Return IPv4 and IPv6 type address information
OF_INET Return only IPv4 address information
AF_INET6 Return only IPv6 address information

Some values ​​and corresponding meanings of Flags are as follows (only the Flags used in WebRTC are introduced).

value meaning

GAA_FLAG_SKIP_DNS_SERVER

Do not return DNS address

GAA_FLAG_SKIP_ANYCAST

Do not return IPv6 arbitrary addresses

GAA_FLAG_SKIP_MULTICAST

does not return a multicast address

GAA_FLAG_INCLUDE_PREFIX

Returns a list of IP address prefixes on the adapter

Reserved : Reserved parameters, just set to 0.

AdapterAddress : address output parameter, it is the pointer of IP_ADAPTER_ADDRESSES data, all IP_ADAPTER_ADDRESSES information can be traversed through the Next pointer in this data structure, for the system whose Windows version is greater than or equal to VISTA, IP_ADAPTER_ADDRESSES is IP_ADAPTER_ADDRESSES_LH, otherwise IP_ADAPTER_ADDRESSES is IP_ADAPTER_ADD RESSES_XP, below IP_ADAPTER_ADDRESSES_LH will be used as an example to illustrate the meaning of important information in the data structure.

SizePointer : Output parameter, indicating the size of the buffer pointed to by AdapterAddress.

        The code for WebRTC to call GetAdaptersAddresses to obtain network card information is as follows.

        Each address information is represented by a Network structure, and all Networks are finally stored in NetworkMap, and the logic of traversing to obtain IP is as follows.

 

 

3. Obtain the default address information

        After obtaining the NetworkList information, it will obtain the local default IPv4 and IPv6 addresses. The acquisition method is to create a local socket, and then connect to the Google DNS service. After the connection between the socket and the remote address is completed, the local socket can be obtained through getsockname. the address of.

Guess you like

Origin blog.csdn.net/weixin_38102771/article/details/123563460