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.

103 lines
3.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 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 "p2p/base/stun_server.h"
  11. #include <utility>
  12. #include "rtc_base/byte_buffer.h"
  13. #include "rtc_base/logging.h"
  14. namespace cricket {
  15. StunServer::StunServer(rtc::AsyncUDPSocket* socket) : socket_(socket) {
  16. socket_->SignalReadPacket.connect(this, &StunServer::OnPacket);
  17. }
  18. StunServer::~StunServer() {
  19. socket_->SignalReadPacket.disconnect(this);
  20. }
  21. void StunServer::OnPacket(rtc::AsyncPacketSocket* socket,
  22. const char* buf,
  23. size_t size,
  24. const rtc::SocketAddress& remote_addr,
  25. const int64_t& /* packet_time_us */) {
  26. // Parse the STUN message; eat any messages that fail to parse.
  27. rtc::ByteBufferReader bbuf(buf, size);
  28. StunMessage msg;
  29. if (!msg.Read(&bbuf)) {
  30. return;
  31. }
  32. // TODO(?): If unknown non-optional (<= 0x7fff) attributes are found, send a
  33. // 420 "Unknown Attribute" response.
  34. // Send the message to the appropriate handler function.
  35. switch (msg.type()) {
  36. case STUN_BINDING_REQUEST:
  37. OnBindingRequest(&msg, remote_addr);
  38. break;
  39. default:
  40. SendErrorResponse(msg, remote_addr, 600, "Operation Not Supported");
  41. }
  42. }
  43. void StunServer::OnBindingRequest(StunMessage* msg,
  44. const rtc::SocketAddress& remote_addr) {
  45. StunMessage response;
  46. GetStunBindResponse(msg, remote_addr, &response);
  47. SendResponse(response, remote_addr);
  48. }
  49. void StunServer::SendErrorResponse(const StunMessage& msg,
  50. const rtc::SocketAddress& addr,
  51. int error_code,
  52. const char* error_desc) {
  53. StunMessage err_msg;
  54. err_msg.SetType(GetStunErrorResponseType(msg.type()));
  55. err_msg.SetTransactionID(msg.transaction_id());
  56. auto err_code = StunAttribute::CreateErrorCode();
  57. err_code->SetCode(error_code);
  58. err_code->SetReason(error_desc);
  59. err_msg.AddAttribute(std::move(err_code));
  60. SendResponse(err_msg, addr);
  61. }
  62. void StunServer::SendResponse(const StunMessage& msg,
  63. const rtc::SocketAddress& addr) {
  64. rtc::ByteBufferWriter buf;
  65. msg.Write(&buf);
  66. rtc::PacketOptions options;
  67. if (socket_->SendTo(buf.Data(), buf.Length(), addr, options) < 0)
  68. RTC_LOG_ERR(LS_ERROR) << "sendto";
  69. }
  70. void StunServer::GetStunBindResponse(StunMessage* request,
  71. const rtc::SocketAddress& remote_addr,
  72. StunMessage* response) const {
  73. response->SetType(STUN_BINDING_RESPONSE);
  74. response->SetTransactionID(request->transaction_id());
  75. // Tell the user the address that we received their request from.
  76. std::unique_ptr<StunAddressAttribute> mapped_addr;
  77. if (request->IsLegacy()) {
  78. mapped_addr = StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS);
  79. } else {
  80. mapped_addr = StunAttribute::CreateXorAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
  81. }
  82. mapped_addr->SetAddress(remote_addr);
  83. response->AddAttribute(std::move(mapped_addr));
  84. }
  85. } // namespace cricket