WebRTC collection server reflection address candidate source code analysis

I. Introduction

        In this blog , we introduced WebRTC candidates and focused on analyzing the process of WebRTC collecting host candidates. This article will introduce how WebRTC collects Server Reflexive Addresses.

        Server reflection candidates are obtained by sending a STUN Binding Request to the STUN server, and the STUN server returns the client's NAT-mapped external network address.

2. Source code analysis

1. Send a STUN Binding Request

        As shown below, UDPPort::OnLocalAddressReady first calls MaybeSetDefaultLocalAddress, then calls AddAddress to add host type candidates, and then calls MaybePrepareStunCandidate() to prepare for collecting STUN type candidates (server reflection address candidates).

         If the STUN server address is configured, enter the branch of executing SendStunBindingRequests().

        If the configured STUN server address is a domain name, execute ResolveStunAddress, wait for the domain name to be resolved into an IP, and then send a StunBindingRequest to the STUN server, otherwise enter the else if branch, and send a Binding Request through StunRequestManager::Send.

        StunRequestManager::Send will call StunRequestManager::SendDelayed, which will execute StunRequest::Construct to construct Message internally, and then insert the message event into the network thread for processing.

2. Receive and analyze STUN Binding Response

        When UDP receives the network data packet, it will call UDPPort::OnReadPacket. If the peer address is the STUN server address we configured, it means that this may be a STUN message sent by the STUN server to the local end, and then enter StunRequestManager::CheckResponse processing .

        StunRequestManager::CheckResponse first checks the UDP data size, if it is less than 20 bytes, it means that it cannot be a STUN message, directly return false, and then read the TransactionId according to the STUN message format, if the local end has not sent a request for this TransactionId Then return false, then construct StunMessage and enter StunRequestManager::CheckResponse(StunMessage* msg) to continue processing.

        StunRequestManager::CheckResponse checks the STUN message type, if it is a successful response, execute StunRequest::OnResponse (StunRequest is a base class, for StunBindingRequest, it will call StunBindingRequest::OnResponse), if it is an error response, execute StunRequest::OnErrorResponse.

         StunBindingRequest::OnReponse checks the STUN_ATTR_MAPPED_ADDRESS attribute of the STUN Message, gets the ipaddr and port after taking it out, and calls back UDPPort::OnStunBindingRequestSucceeded.

         UDPPort::OnStunBindingRequestSucceeded Add stun_reflected_addr as a Server Reflexive Address candidate through AddAddress, so far the collection of server reflection candidates is completed.

Guess you like

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