You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
7.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2013 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. #include "pc/sctp_utils.h"
  11. #include <stddef.h>
  12. #include <stdint.h>
  13. #include "rtc_base/byte_buffer.h"
  14. #include "rtc_base/copy_on_write_buffer.h"
  15. #include "rtc_base/logging.h"
  16. namespace webrtc {
  17. // Format defined at
  18. // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-01#section
  19. static const uint8_t DATA_CHANNEL_OPEN_MESSAGE_TYPE = 0x03;
  20. static const uint8_t DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE = 0x02;
  21. enum DataChannelOpenMessageChannelType {
  22. DCOMCT_ORDERED_RELIABLE = 0x00,
  23. DCOMCT_ORDERED_PARTIAL_RTXS = 0x01,
  24. DCOMCT_ORDERED_PARTIAL_TIME = 0x02,
  25. DCOMCT_UNORDERED_RELIABLE = 0x80,
  26. DCOMCT_UNORDERED_PARTIAL_RTXS = 0x81,
  27. DCOMCT_UNORDERED_PARTIAL_TIME = 0x82,
  28. };
  29. bool IsOpenMessage(const rtc::CopyOnWriteBuffer& payload) {
  30. // Format defined at
  31. // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
  32. if (payload.size() < 1) {
  33. RTC_LOG(LS_WARNING) << "Could not read OPEN message type.";
  34. return false;
  35. }
  36. uint8_t message_type = payload[0];
  37. return message_type == DATA_CHANNEL_OPEN_MESSAGE_TYPE;
  38. }
  39. bool ParseDataChannelOpenMessage(const rtc::CopyOnWriteBuffer& payload,
  40. std::string* label,
  41. DataChannelInit* config) {
  42. // Format defined at
  43. // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
  44. rtc::ByteBufferReader buffer(payload.data<char>(), payload.size());
  45. uint8_t message_type;
  46. if (!buffer.ReadUInt8(&message_type)) {
  47. RTC_LOG(LS_WARNING) << "Could not read OPEN message type.";
  48. return false;
  49. }
  50. if (message_type != DATA_CHANNEL_OPEN_MESSAGE_TYPE) {
  51. RTC_LOG(LS_WARNING) << "Data Channel OPEN message of unexpected type: "
  52. << message_type;
  53. return false;
  54. }
  55. uint8_t channel_type;
  56. if (!buffer.ReadUInt8(&channel_type)) {
  57. RTC_LOG(LS_WARNING) << "Could not read OPEN message channel type.";
  58. return false;
  59. }
  60. uint16_t priority;
  61. if (!buffer.ReadUInt16(&priority)) {
  62. RTC_LOG(LS_WARNING)
  63. << "Could not read OPEN message reliabilility prioirty.";
  64. return false;
  65. }
  66. uint32_t reliability_param;
  67. if (!buffer.ReadUInt32(&reliability_param)) {
  68. RTC_LOG(LS_WARNING) << "Could not read OPEN message reliabilility param.";
  69. return false;
  70. }
  71. uint16_t label_length;
  72. if (!buffer.ReadUInt16(&label_length)) {
  73. RTC_LOG(LS_WARNING) << "Could not read OPEN message label length.";
  74. return false;
  75. }
  76. uint16_t protocol_length;
  77. if (!buffer.ReadUInt16(&protocol_length)) {
  78. RTC_LOG(LS_WARNING) << "Could not read OPEN message protocol length.";
  79. return false;
  80. }
  81. if (!buffer.ReadString(label, (size_t)label_length)) {
  82. RTC_LOG(LS_WARNING) << "Could not read OPEN message label";
  83. return false;
  84. }
  85. if (!buffer.ReadString(&config->protocol, protocol_length)) {
  86. RTC_LOG(LS_WARNING) << "Could not read OPEN message protocol.";
  87. return false;
  88. }
  89. config->ordered = true;
  90. switch (channel_type) {
  91. case DCOMCT_UNORDERED_RELIABLE:
  92. case DCOMCT_UNORDERED_PARTIAL_RTXS:
  93. case DCOMCT_UNORDERED_PARTIAL_TIME:
  94. config->ordered = false;
  95. }
  96. config->maxRetransmits = absl::nullopt;
  97. config->maxRetransmitTime = absl::nullopt;
  98. switch (channel_type) {
  99. case DCOMCT_ORDERED_PARTIAL_RTXS:
  100. case DCOMCT_UNORDERED_PARTIAL_RTXS:
  101. config->maxRetransmits = reliability_param;
  102. break;
  103. case DCOMCT_ORDERED_PARTIAL_TIME:
  104. case DCOMCT_UNORDERED_PARTIAL_TIME:
  105. config->maxRetransmitTime = reliability_param;
  106. break;
  107. }
  108. return true;
  109. }
  110. bool ParseDataChannelOpenAckMessage(const rtc::CopyOnWriteBuffer& payload) {
  111. if (payload.size() < 1) {
  112. RTC_LOG(LS_WARNING) << "Could not read OPEN_ACK message type.";
  113. return false;
  114. }
  115. uint8_t message_type = payload[0];
  116. if (message_type != DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE) {
  117. RTC_LOG(LS_WARNING) << "Data Channel OPEN_ACK message of unexpected type: "
  118. << message_type;
  119. return false;
  120. }
  121. return true;
  122. }
  123. bool WriteDataChannelOpenMessage(const std::string& label,
  124. const DataChannelInit& config,
  125. rtc::CopyOnWriteBuffer* payload) {
  126. // Format defined at
  127. // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-09#section-5.1
  128. uint8_t channel_type = 0;
  129. uint32_t reliability_param = 0;
  130. uint16_t priority = 0;
  131. if (config.ordered) {
  132. if (config.maxRetransmits) {
  133. channel_type = DCOMCT_ORDERED_PARTIAL_RTXS;
  134. reliability_param = *config.maxRetransmits;
  135. } else if (config.maxRetransmitTime) {
  136. channel_type = DCOMCT_ORDERED_PARTIAL_TIME;
  137. reliability_param = *config.maxRetransmitTime;
  138. } else {
  139. channel_type = DCOMCT_ORDERED_RELIABLE;
  140. }
  141. } else {
  142. if (config.maxRetransmits) {
  143. channel_type = DCOMCT_UNORDERED_PARTIAL_RTXS;
  144. reliability_param = *config.maxRetransmits;
  145. } else if (config.maxRetransmitTime) {
  146. channel_type = DCOMCT_UNORDERED_PARTIAL_TIME;
  147. reliability_param = *config.maxRetransmitTime;
  148. } else {
  149. channel_type = DCOMCT_UNORDERED_RELIABLE;
  150. }
  151. }
  152. rtc::ByteBufferWriter buffer(NULL,
  153. 20 + label.length() + config.protocol.length(),
  154. rtc::ByteBuffer::ORDER_NETWORK);
  155. // TODO(tommi): Add error handling and check resulting length.
  156. buffer.WriteUInt8(DATA_CHANNEL_OPEN_MESSAGE_TYPE);
  157. buffer.WriteUInt8(channel_type);
  158. buffer.WriteUInt16(priority);
  159. buffer.WriteUInt32(reliability_param);
  160. buffer.WriteUInt16(static_cast<uint16_t>(label.length()));
  161. buffer.WriteUInt16(static_cast<uint16_t>(config.protocol.length()));
  162. buffer.WriteString(label);
  163. buffer.WriteString(config.protocol);
  164. payload->SetData(buffer.Data(), buffer.Length());
  165. return true;
  166. }
  167. void WriteDataChannelOpenAckMessage(rtc::CopyOnWriteBuffer* payload) {
  168. uint8_t data = DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE;
  169. payload->SetData(&data, sizeof(data));
  170. }
  171. cricket::DataMessageType ToCricketDataMessageType(DataMessageType type) {
  172. switch (type) {
  173. case DataMessageType::kText:
  174. return cricket::DMT_TEXT;
  175. case DataMessageType::kBinary:
  176. return cricket::DMT_BINARY;
  177. case DataMessageType::kControl:
  178. return cricket::DMT_CONTROL;
  179. default:
  180. return cricket::DMT_NONE;
  181. }
  182. return cricket::DMT_NONE;
  183. }
  184. DataMessageType ToWebrtcDataMessageType(cricket::DataMessageType type) {
  185. switch (type) {
  186. case cricket::DMT_TEXT:
  187. return DataMessageType::kText;
  188. case cricket::DMT_BINARY:
  189. return DataMessageType::kBinary;
  190. case cricket::DMT_CONTROL:
  191. return DataMessageType::kControl;
  192. case cricket::DMT_NONE:
  193. default:
  194. RTC_NOTREACHED();
  195. }
  196. return DataMessageType::kControl;
  197. }
  198. } // namespace webrtc