dtlsrole.go 2.4 KB

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