github.com/pion/webrtc/v3@v3.2.24/dtlsrole.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  package webrtc
     5  
     6  import (
     7  	"github.com/pion/sdp/v3"
     8  )
     9  
    10  // DTLSRole indicates the role of the DTLS transport.
    11  type DTLSRole byte
    12  
    13  const (
    14  	// DTLSRoleAuto defines the DTLS role is determined based on
    15  	// the resolved ICE role: the ICE controlled role acts as the DTLS
    16  	// client and the ICE controlling role acts as the DTLS server.
    17  	DTLSRoleAuto DTLSRole = iota + 1
    18  
    19  	// DTLSRoleClient defines the DTLS client role.
    20  	DTLSRoleClient
    21  
    22  	// DTLSRoleServer defines the DTLS server role.
    23  	DTLSRoleServer
    24  )
    25  
    26  const (
    27  	// https://tools.ietf.org/html/rfc5763
    28  	/*
    29  		The answerer MUST use either a
    30  		setup attribute value of setup:active or setup:passive.  Note that
    31  		if the answerer uses setup:passive, then the DTLS handshake will
    32  		not begin until the answerer is received, which adds additional
    33  		latency. setup:active allows the answer and the DTLS handshake to
    34  		occur in parallel.  Thus, setup:active is RECOMMENDED.
    35  	*/
    36  	defaultDtlsRoleAnswer = DTLSRoleClient
    37  	/*
    38  		The endpoint that is the offerer MUST use the setup attribute
    39  		value of setup:actpass and be prepared to receive a client_hello
    40  		before it receives the answer.
    41  	*/
    42  	defaultDtlsRoleOffer = DTLSRoleAuto
    43  )
    44  
    45  func (r DTLSRole) String() string {
    46  	switch r {
    47  	case DTLSRoleAuto:
    48  		return "auto"
    49  	case DTLSRoleClient:
    50  		return "client"
    51  	case DTLSRoleServer:
    52  		return "server"
    53  	default:
    54  		return unknownStr
    55  	}
    56  }
    57  
    58  // Iterate a SessionDescription from a remote to determine if an explicit
    59  // role can been determined from it. The decision is made from the first role we we parse.
    60  // If no role can be found we return DTLSRoleAuto
    61  func dtlsRoleFromRemoteSDP(sessionDescription *sdp.SessionDescription) DTLSRole {
    62  	if sessionDescription == nil {
    63  		return DTLSRoleAuto
    64  	}
    65  
    66  	for _, mediaSection := range sessionDescription.MediaDescriptions {
    67  		for _, attribute := range mediaSection.Attributes {
    68  			if attribute.Key == "setup" {
    69  				switch attribute.Value {
    70  				case sdp.ConnectionRoleActive.String():
    71  					return DTLSRoleClient
    72  				case sdp.ConnectionRolePassive.String():
    73  					return DTLSRoleServer
    74  				default:
    75  					return DTLSRoleAuto
    76  				}
    77  			}
    78  		}
    79  	}
    80  
    81  	return DTLSRoleAuto
    82  }
    83  
    84  func connectionRoleFromDtlsRole(d DTLSRole) sdp.ConnectionRole {
    85  	switch d {
    86  	case DTLSRoleClient:
    87  		return sdp.ConnectionRoleActive
    88  	case DTLSRoleServer:
    89  		return sdp.ConnectionRolePassive
    90  	case DTLSRoleAuto:
    91  		return sdp.ConnectionRoleActpass
    92  	default:
    93  		return sdp.ConnectionRole(0)
    94  	}
    95  }