dtlsrole.go 2.5 KB

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