transport_description.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright 2012 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef P2P_BASE_TRANSPORT_DESCRIPTION_H_
  11. #define P2P_BASE_TRANSPORT_DESCRIPTION_H_
  12. #include <memory>
  13. #include <string>
  14. #include <vector>
  15. #include "absl/algorithm/container.h"
  16. #include "absl/types/optional.h"
  17. #include "api/rtc_error.h"
  18. #include "p2p/base/p2p_constants.h"
  19. #include "rtc_base/ssl_fingerprint.h"
  20. #include "rtc_base/system/rtc_export.h"
  21. namespace cricket {
  22. // SEC_ENABLED and SEC_REQUIRED should only be used if the session
  23. // was negotiated over TLS, to protect the inline crypto material
  24. // exchange.
  25. // SEC_DISABLED: No crypto in outgoing offer, ignore any supplied crypto.
  26. // SEC_ENABLED: Crypto in outgoing offer and answer (if supplied in offer).
  27. // SEC_REQUIRED: Crypto in outgoing offer and answer. Fail any offer with absent
  28. // or unsupported crypto.
  29. // TODO(deadbeef): Remove this or rename it to something more appropriate, like
  30. // SdesPolicy.
  31. enum SecurePolicy { SEC_DISABLED, SEC_ENABLED, SEC_REQUIRED };
  32. // Whether our side of the call is driving the negotiation, or the other side.
  33. enum IceRole { ICEROLE_CONTROLLING = 0, ICEROLE_CONTROLLED, ICEROLE_UNKNOWN };
  34. // ICE RFC 5245 implementation type.
  35. enum IceMode {
  36. ICEMODE_FULL, // As defined in http://tools.ietf.org/html/rfc5245#section-4.1
  37. ICEMODE_LITE // As defined in http://tools.ietf.org/html/rfc5245#section-4.2
  38. };
  39. // RFC 4145 - http://tools.ietf.org/html/rfc4145#section-4
  40. // 'active': The endpoint will initiate an outgoing connection.
  41. // 'passive': The endpoint will accept an incoming connection.
  42. // 'actpass': The endpoint is willing to accept an incoming
  43. // connection or to initiate an outgoing connection.
  44. enum ConnectionRole {
  45. CONNECTIONROLE_NONE = 0,
  46. CONNECTIONROLE_ACTIVE,
  47. CONNECTIONROLE_PASSIVE,
  48. CONNECTIONROLE_ACTPASS,
  49. CONNECTIONROLE_HOLDCONN,
  50. };
  51. struct IceParameters {
  52. // Constructs an IceParameters from a user-provided ufrag/pwd combination.
  53. // Returns a SyntaxError if the ufrag or pwd are malformed.
  54. static RTC_EXPORT webrtc::RTCErrorOr<IceParameters> Parse(
  55. absl::string_view raw_ufrag,
  56. absl::string_view raw_pwd);
  57. // TODO(honghaiz): Include ICE mode in this structure to match the ORTC
  58. // struct:
  59. // http://ortc.org/wp-content/uploads/2016/03/ortc.html#idl-def-RTCIceParameters
  60. std::string ufrag;
  61. std::string pwd;
  62. bool renomination = false;
  63. IceParameters() = default;
  64. IceParameters(const std::string& ice_ufrag,
  65. const std::string& ice_pwd,
  66. bool ice_renomination)
  67. : ufrag(ice_ufrag), pwd(ice_pwd), renomination(ice_renomination) {}
  68. bool operator==(const IceParameters& other) const {
  69. return ufrag == other.ufrag && pwd == other.pwd &&
  70. renomination == other.renomination;
  71. }
  72. bool operator!=(const IceParameters& other) const {
  73. return !(*this == other);
  74. }
  75. // Validate IceParameters, returns a SyntaxError if the ufrag or pwd are
  76. // malformed.
  77. webrtc::RTCError Validate() const;
  78. };
  79. extern const char CONNECTIONROLE_ACTIVE_STR[];
  80. extern const char CONNECTIONROLE_PASSIVE_STR[];
  81. extern const char CONNECTIONROLE_ACTPASS_STR[];
  82. extern const char CONNECTIONROLE_HOLDCONN_STR[];
  83. constexpr auto* ICE_OPTION_TRICKLE = "trickle";
  84. constexpr auto* ICE_OPTION_RENOMINATION = "renomination";
  85. bool StringToConnectionRole(const std::string& role_str, ConnectionRole* role);
  86. bool ConnectionRoleToString(const ConnectionRole& role, std::string* role_str);
  87. struct TransportDescription {
  88. TransportDescription();
  89. TransportDescription(const std::vector<std::string>& transport_options,
  90. const std::string& ice_ufrag,
  91. const std::string& ice_pwd,
  92. IceMode ice_mode,
  93. ConnectionRole role,
  94. const rtc::SSLFingerprint* identity_fingerprint);
  95. TransportDescription(const std::string& ice_ufrag,
  96. const std::string& ice_pwd);
  97. TransportDescription(const TransportDescription& from);
  98. ~TransportDescription();
  99. TransportDescription& operator=(const TransportDescription& from);
  100. // TODO(deadbeef): Rename to HasIceOption, etc.
  101. bool HasOption(const std::string& option) const {
  102. return absl::c_linear_search(transport_options, option);
  103. }
  104. void AddOption(const std::string& option) {
  105. transport_options.push_back(option);
  106. }
  107. bool secure() const { return identity_fingerprint != nullptr; }
  108. IceParameters GetIceParameters() const {
  109. return IceParameters(ice_ufrag, ice_pwd,
  110. HasOption(ICE_OPTION_RENOMINATION));
  111. }
  112. static rtc::SSLFingerprint* CopyFingerprint(const rtc::SSLFingerprint* from) {
  113. if (!from)
  114. return NULL;
  115. return new rtc::SSLFingerprint(*from);
  116. }
  117. // These are actually ICE options (appearing in the ice-options attribute in
  118. // SDP).
  119. // TODO(deadbeef): Rename to ice_options.
  120. std::vector<std::string> transport_options;
  121. std::string ice_ufrag;
  122. std::string ice_pwd;
  123. IceMode ice_mode;
  124. ConnectionRole connection_role;
  125. std::unique_ptr<rtc::SSLFingerprint> identity_fingerprint;
  126. };
  127. } // namespace cricket
  128. #endif // P2P_BASE_TRANSPORT_DESCRIPTION_H_